OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
| 7 #include "base/logging.h" |
7 #include "sql/connection.h" | 8 #include "sql/connection.h" |
8 #include "sql/meta_table.h" | 9 #include "sql/meta_table.h" |
9 #include "sql/statement.h" | 10 #include "sql/statement.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "third_party/sqlite/sqlite3.h" | 12 #include "third_party/sqlite/sqlite3.h" |
12 | 13 |
13 class SQLConnectionTest : public testing::Test { | 14 class SQLConnectionTest : public testing::Test { |
14 public: | 15 public: |
15 SQLConnectionTest() {} | 16 SQLConnectionTest() {} |
16 | 17 |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 const char *kQuery = "SELECT COUNT(*) FROM foo"; | 273 const char *kQuery = "SELECT COUNT(*) FROM foo"; |
273 sql::Statement s(other_db.GetUniqueStatement(kQuery)); | 274 sql::Statement s(other_db.GetUniqueStatement(kQuery)); |
274 ASSERT_TRUE(s.Step()); | 275 ASSERT_TRUE(s.Step()); |
275 ASSERT_FALSE(db().Raze()); | 276 ASSERT_FALSE(db().Raze()); |
276 | 277 |
277 // Complete the statement unlocks the database. | 278 // Complete the statement unlocks the database. |
278 ASSERT_FALSE(s.Step()); | 279 ASSERT_FALSE(s.Step()); |
279 ASSERT_TRUE(db().Raze()); | 280 ASSERT_TRUE(db().Raze()); |
280 } | 281 } |
281 | 282 |
| 283 // Basic test of RazeAndClose() operation. |
| 284 TEST_F(SQLConnectionTest, RazeAndClose) { |
| 285 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 286 const char* kPopulateSql = "INSERT INTO foo (value) VALUES (12)"; |
| 287 |
| 288 // Test that RazeAndClose() closes the database, and that the |
| 289 // database is empty when re-opened. |
| 290 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 291 ASSERT_TRUE(db().Execute(kPopulateSql)); |
| 292 ASSERT_TRUE(db().RazeAndClose()); |
| 293 ASSERT_FALSE(db().is_open()); |
| 294 db().Close(); |
| 295 ASSERT_TRUE(db().Open(db_path())); |
| 296 { |
| 297 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master")); |
| 298 ASSERT_FALSE(s.Step()); |
| 299 } |
| 300 |
| 301 // Test that RazeAndClose() can break transactions. |
| 302 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 303 ASSERT_TRUE(db().Execute(kPopulateSql)); |
| 304 ASSERT_TRUE(db().BeginTransaction()); |
| 305 ASSERT_TRUE(db().RazeAndClose()); |
| 306 ASSERT_FALSE(db().is_open()); |
| 307 ASSERT_FALSE(db().CommitTransaction()); |
| 308 db().Close(); |
| 309 ASSERT_TRUE(db().Open(db_path())); |
| 310 { |
| 311 sql::Statement s(db().GetUniqueStatement("SELECT * FROM sqlite_master")); |
| 312 ASSERT_FALSE(s.Step()); |
| 313 } |
| 314 } |
| 315 |
| 316 // Test that various operations fail without crashing after |
| 317 // RazeAndClose(). |
| 318 TEST_F(SQLConnectionTest, RazeAndCloseDiagnostics) { |
| 319 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
| 320 const char* kPopulateSql = "INSERT INTO foo (value) VALUES (12)"; |
| 321 const char* kSimpleSql = "SELECT 1"; |
| 322 |
| 323 ASSERT_TRUE(db().Execute(kCreateSql)); |
| 324 ASSERT_TRUE(db().Execute(kPopulateSql)); |
| 325 |
| 326 // Test baseline expectations. |
| 327 db().Preload(); |
| 328 ASSERT_TRUE(db().DoesTableExist("foo")); |
| 329 ASSERT_TRUE(db().IsSQLValid(kSimpleSql)); |
| 330 ASSERT_EQ(SQLITE_OK, db().ExecuteAndReturnErrorCode(kSimpleSql)); |
| 331 ASSERT_TRUE(db().Execute(kSimpleSql)); |
| 332 ASSERT_TRUE(db().is_open()); |
| 333 { |
| 334 sql::Statement s(db().GetUniqueStatement(kSimpleSql)); |
| 335 ASSERT_TRUE(s.Step()); |
| 336 } |
| 337 { |
| 338 sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
| 339 ASSERT_TRUE(s.Step()); |
| 340 } |
| 341 ASSERT_TRUE(db().BeginTransaction()); |
| 342 ASSERT_TRUE(db().CommitTransaction()); |
| 343 ASSERT_TRUE(db().BeginTransaction()); |
| 344 db().RollbackTransaction(); |
| 345 |
| 346 ASSERT_TRUE(db().RazeAndClose()); |
| 347 |
| 348 // At this point, they should all fail, but not crash. |
| 349 db().Preload(); |
| 350 ASSERT_FALSE(db().DoesTableExist("foo")); |
| 351 ASSERT_FALSE(db().IsSQLValid(kSimpleSql)); |
| 352 ASSERT_EQ(SQLITE_ERROR, db().ExecuteAndReturnErrorCode(kSimpleSql)); |
| 353 ASSERT_FALSE(db().Execute(kSimpleSql)); |
| 354 ASSERT_FALSE(db().is_open()); |
| 355 { |
| 356 sql::Statement s(db().GetUniqueStatement(kSimpleSql)); |
| 357 ASSERT_FALSE(s.Step()); |
| 358 } |
| 359 { |
| 360 sql::Statement s(db().GetCachedStatement(SQL_FROM_HERE, kSimpleSql)); |
| 361 ASSERT_FALSE(s.Step()); |
| 362 } |
| 363 ASSERT_FALSE(db().BeginTransaction()); |
| 364 ASSERT_FALSE(db().CommitTransaction()); |
| 365 ASSERT_FALSE(db().BeginTransaction()); |
| 366 db().RollbackTransaction(); |
| 367 |
| 368 // Close normally to reset the poisoned flag. |
| 369 db().Close(); |
| 370 |
| 371 // DEATH tests not supported on Android or iOS. |
| 372 #if !defined(OS_ANDROID) && !defined(OS_IOS) |
| 373 // Once the real Close() has been called, various calls enforce API |
| 374 // usage by becoming fatal in debug mode. Since DEATH tests are |
| 375 // expensive, just test one of them. |
| 376 if (DLOG_IS_ON(FATAL)) { |
| 377 ASSERT_DEATH({ |
| 378 db().IsSQLValid(kSimpleSql); |
| 379 }, "Illegal use of connection without a db"); |
| 380 } |
| 381 #endif |
| 382 } |
| 383 |
| 384 // TODO(shess): Spin up a background thread to hold other_db, to more |
| 385 // closely match real life. That would also allow testing |
| 386 // RazeWithTimeout(). |
| 387 |
282 #if defined(OS_ANDROID) | 388 #if defined(OS_ANDROID) |
283 TEST_F(SQLConnectionTest, SetTempDirForSQL) { | 389 TEST_F(SQLConnectionTest, SetTempDirForSQL) { |
284 | 390 |
285 sql::MetaTable meta_table; | 391 sql::MetaTable meta_table; |
286 // Below call needs a temporary directory in sqlite3 | 392 // Below call needs a temporary directory in sqlite3 |
287 // On Android, it can pass only when the temporary directory is set. | 393 // On Android, it can pass only when the temporary directory is set. |
288 // Otherwise, sqlite3 doesn't find the correct directory to store | 394 // Otherwise, sqlite3 doesn't find the correct directory to store |
289 // temporary files and will report the error 'unable to open | 395 // temporary files and will report the error 'unable to open |
290 // database file'. | 396 // database file'. |
291 ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); | 397 ASSERT_TRUE(meta_table.Init(&db(), 4, 4)); |
292 } | 398 } |
293 #endif | 399 #endif |
294 | |
295 // TODO(shess): Spin up a background thread to hold other_db, to more | |
296 // closely match real life. That would also allow testing | |
297 // RazeWithTimeout(). | |
OLD | NEW |