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 <stddef.h> | 5 #include <stddef.h> |
6 #include <stdint.h> | 6 #include <stdint.h> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/files/scoped_file.h" | 10 #include "base/files/scoped_file.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/metrics/statistics_recorder.h" | 14 #include "base/metrics/statistics_recorder.h" |
15 #include "base/test/histogram_tester.h" | 15 #include "base/test/histogram_tester.h" |
16 #include "base/trace_event/process_memory_dump.h" | 16 #include "base/trace_event/process_memory_dump.h" |
17 #include "sql/connection.h" | 17 #include "sql/connection.h" |
18 #include "sql/connection_memory_dump_provider.h" | 18 #include "sql/connection_memory_dump_provider.h" |
19 #include "sql/correct_sql_test_base.h" | 19 #include "sql/correct_sql_test_base.h" |
20 #include "sql/meta_table.h" | 20 #include "sql/meta_table.h" |
21 #include "sql/statement.h" | 21 #include "sql/statement.h" |
22 #include "sql/test/error_callback_support.h" | 22 #include "sql/test/error_callback_support.h" |
23 #include "sql/test/scoped_error_ignorer.h" | 23 #include "sql/test/scoped_error_expecter.h" |
24 #include "sql/test/test_helpers.h" | 24 #include "sql/test/test_helpers.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
26 #include "third_party/sqlite/sqlite3.h" | 26 #include "third_party/sqlite/sqlite3.h" |
27 | 27 |
28 namespace sql { | 28 namespace sql { |
29 namespace test { | 29 namespace test { |
30 | 30 |
31 // Replaces the database time source with an object that steps forward 1ms on | 31 // Replaces the database time source with an object that steps forward 1ms on |
32 // each check, and which can be jumped forward an arbitrary amount of time | 32 // each check, and which can be jumped forward an arbitrary amount of time |
33 // programmatically. | 33 // programmatically. |
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
351 | 351 |
352 TEST_F(SQLConnectionTest, Rollback) { | 352 TEST_F(SQLConnectionTest, Rollback) { |
353 ASSERT_TRUE(db().BeginTransaction()); | 353 ASSERT_TRUE(db().BeginTransaction()); |
354 ASSERT_TRUE(db().BeginTransaction()); | 354 ASSERT_TRUE(db().BeginTransaction()); |
355 EXPECT_EQ(2, db().transaction_nesting()); | 355 EXPECT_EQ(2, db().transaction_nesting()); |
356 db().RollbackTransaction(); | 356 db().RollbackTransaction(); |
357 EXPECT_FALSE(db().CommitTransaction()); | 357 EXPECT_FALSE(db().CommitTransaction()); |
358 EXPECT_TRUE(db().BeginTransaction()); | 358 EXPECT_TRUE(db().BeginTransaction()); |
359 } | 359 } |
360 | 360 |
361 // Test the scoped error ignorer by attempting to insert a duplicate | 361 // Test the scoped error expecter by attempting to insert a duplicate |
362 // value into an index. | 362 // value into an index. |
363 TEST_F(SQLConnectionTest, ScopedIgnoreError) { | 363 TEST_F(SQLConnectionTest, ScopedErrorExpecter) { |
364 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; | 364 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
365 ASSERT_TRUE(db().Execute(kCreateSql)); | 365 ASSERT_TRUE(db().Execute(kCreateSql)); |
366 ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)")); | 366 ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
367 | 367 |
368 { | 368 { |
369 sql::ScopedErrorIgnorer ignore_errors; | 369 sql::test::ScopedErrorExpecter expecter; |
370 ignore_errors.IgnoreError(SQLITE_CONSTRAINT); | 370 expecter.ExpectError(SQLITE_CONSTRAINT); |
371 ASSERT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); | 371 ASSERT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
372 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 372 ASSERT_TRUE(expecter.SawExpectedErrors()); |
373 } | 373 } |
374 } | 374 } |
375 | 375 |
376 // Test that clients of GetUntrackedStatement() can test corruption-handling | 376 // Test that clients of GetUntrackedStatement() can test corruption-handling |
377 // with ScopedErrorIgnorer. | 377 // with ScopedErrorExpecter. |
378 TEST_F(SQLConnectionTest, ScopedIgnoreUntracked) { | 378 TEST_F(SQLConnectionTest, ScopedIgnoreUntracked) { |
379 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; | 379 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
380 ASSERT_TRUE(db().Execute(kCreateSql)); | 380 ASSERT_TRUE(db().Execute(kCreateSql)); |
381 ASSERT_FALSE(db().DoesTableExist("bar")); | 381 ASSERT_FALSE(db().DoesTableExist("bar")); |
382 ASSERT_TRUE(db().DoesTableExist("foo")); | 382 ASSERT_TRUE(db().DoesTableExist("foo")); |
383 ASSERT_TRUE(db().DoesColumnExist("foo", "id")); | 383 ASSERT_TRUE(db().DoesColumnExist("foo", "id")); |
384 db().Close(); | 384 db().Close(); |
385 | 385 |
386 // Corrupt the database so that nothing works, including PRAGMAs. | 386 // Corrupt the database so that nothing works, including PRAGMAs. |
387 ASSERT_TRUE(CorruptSizeInHeaderOfDB()); | 387 ASSERT_TRUE(CorruptSizeInHeaderOfDB()); |
388 | 388 |
389 { | 389 { |
390 sql::ScopedErrorIgnorer ignore_errors; | 390 sql::test::ScopedErrorExpecter expecter; |
391 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 391 expecter.ExpectError(SQLITE_CORRUPT); |
392 ASSERT_TRUE(db().Open(db_path())); | 392 ASSERT_TRUE(db().Open(db_path())); |
393 ASSERT_FALSE(db().DoesTableExist("bar")); | 393 ASSERT_FALSE(db().DoesTableExist("bar")); |
394 ASSERT_FALSE(db().DoesTableExist("foo")); | 394 ASSERT_FALSE(db().DoesTableExist("foo")); |
395 ASSERT_FALSE(db().DoesColumnExist("foo", "id")); | 395 ASSERT_FALSE(db().DoesColumnExist("foo", "id")); |
396 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 396 ASSERT_TRUE(expecter.SawExpectedErrors()); |
397 } | 397 } |
398 } | 398 } |
399 | 399 |
400 TEST_F(SQLConnectionTest, ErrorCallback) { | 400 TEST_F(SQLConnectionTest, ErrorCallback) { |
401 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; | 401 const char* kCreateSql = "CREATE TABLE foo (id INTEGER UNIQUE)"; |
402 ASSERT_TRUE(db().Execute(kCreateSql)); | 402 ASSERT_TRUE(db().Execute(kCreateSql)); |
403 ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)")); | 403 ASSERT_TRUE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
404 | 404 |
405 int error = SQLITE_OK; | 405 int error = SQLITE_OK; |
406 { | 406 { |
407 sql::ScopedErrorCallback sec( | 407 sql::ScopedErrorCallback sec( |
408 &db(), base::Bind(&sql::CaptureErrorCallback, &error)); | 408 &db(), base::Bind(&sql::CaptureErrorCallback, &error)); |
409 EXPECT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); | 409 EXPECT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
410 | 410 |
411 // Later versions of SQLite throw SQLITE_CONSTRAINT_UNIQUE. The specific | 411 // Later versions of SQLite throw SQLITE_CONSTRAINT_UNIQUE. The specific |
412 // sub-error isn't really important. | 412 // sub-error isn't really important. |
413 EXPECT_EQ(SQLITE_CONSTRAINT, (error&0xff)); | 413 EXPECT_EQ(SQLITE_CONSTRAINT, (error&0xff)); |
414 } | 414 } |
415 | 415 |
416 // Callback is no longer in force due to reset. | 416 // Callback is no longer in force due to reset. |
417 { | 417 { |
418 error = SQLITE_OK; | 418 error = SQLITE_OK; |
419 sql::ScopedErrorIgnorer ignore_errors; | 419 sql::test::ScopedErrorExpecter expecter; |
420 ignore_errors.IgnoreError(SQLITE_CONSTRAINT); | 420 expecter.ExpectError(SQLITE_CONSTRAINT); |
421 ASSERT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); | 421 ASSERT_FALSE(db().Execute("INSERT INTO foo (id) VALUES (12)")); |
422 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 422 ASSERT_TRUE(expecter.SawExpectedErrors()); |
423 EXPECT_EQ(SQLITE_OK, error); | 423 EXPECT_EQ(SQLITE_OK, error); |
424 } | 424 } |
425 | 425 |
426 // base::Bind() can curry arguments to be passed by const reference | 426 // base::Bind() can curry arguments to be passed by const reference |
427 // to the callback function. If the callback function calls | 427 // to the callback function. If the callback function calls |
428 // re/set_error_callback(), the storage for those arguments can be | 428 // re/set_error_callback(), the storage for those arguments can be |
429 // deleted while the callback function is still executing. | 429 // deleted while the callback function is still executing. |
430 // | 430 // |
431 // RefCounter() counts how many objects are live using an external | 431 // RefCounter() counts how many objects are live using an external |
432 // count. The same counter is passed to the callback, so that it | 432 // count. The same counter is passed to the callback, so that it |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
609 db().Close(); | 609 db().Close(); |
610 sql::Connection::Delete(db_path()); | 610 sql::Connection::Delete(db_path()); |
611 ASSERT_FALSE(GetPathExists(db_path())); | 611 ASSERT_FALSE(GetPathExists(db_path())); |
612 | 612 |
613 WriteJunkToDatabase(SQLTestBase::TYPE_OVERWRITE_AND_TRUNCATE); | 613 WriteJunkToDatabase(SQLTestBase::TYPE_OVERWRITE_AND_TRUNCATE); |
614 ASSERT_TRUE(GetPathExists(db_path())); | 614 ASSERT_TRUE(GetPathExists(db_path())); |
615 | 615 |
616 // SQLite will successfully open the handle, but fail when running PRAGMA | 616 // SQLite will successfully open the handle, but fail when running PRAGMA |
617 // statements that access the database. | 617 // statements that access the database. |
618 { | 618 { |
619 sql::ScopedErrorIgnorer ignore_errors; | 619 sql::test::ScopedErrorExpecter expecter; |
620 | 620 |
621 // Earlier versions of Chromium compiled against SQLite 3.6.7.3, which | 621 // Earlier versions of Chromium compiled against SQLite 3.6.7.3, which |
622 // returned SQLITE_IOERR_SHORT_READ in this case. Some platforms may still | 622 // returned SQLITE_IOERR_SHORT_READ in this case. Some platforms may still |
623 // compile against an earlier SQLite via USE_SYSTEM_SQLITE. | 623 // compile against an earlier SQLite via USE_SYSTEM_SQLITE. |
624 if (ignore_errors.SQLiteLibVersionNumber() < 3008005) { | 624 if (expecter.SQLiteLibVersionNumber() < 3008005) { |
625 ignore_errors.IgnoreError(SQLITE_IOERR_SHORT_READ); | 625 expecter.ExpectError(SQLITE_IOERR_SHORT_READ); |
626 } else { | 626 } else { |
627 ignore_errors.IgnoreError(SQLITE_NOTADB); | 627 expecter.ExpectError(SQLITE_NOTADB); |
628 } | 628 } |
629 | 629 |
630 EXPECT_TRUE(db().Open(db_path())); | 630 EXPECT_TRUE(db().Open(db_path())); |
631 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 631 ASSERT_TRUE(expecter.SawExpectedErrors()); |
632 } | 632 } |
633 EXPECT_TRUE(db().Raze()); | 633 EXPECT_TRUE(db().Raze()); |
634 db().Close(); | 634 db().Close(); |
635 | 635 |
636 // Now empty, the open should open an empty database. | 636 // Now empty, the open should open an empty database. |
637 EXPECT_TRUE(db().Open(db_path())); | 637 EXPECT_TRUE(db().Open(db_path())); |
638 EXPECT_EQ(0, SqliteMasterCount(&db())); | 638 EXPECT_EQ(0, SqliteMasterCount(&db())); |
639 } | 639 } |
640 | 640 |
641 // Verify that Raze() can handle a database overwritten with garbage. | 641 // Verify that Raze() can handle a database overwritten with garbage. |
642 TEST_F(SQLConnectionTest, RazeNOTADB2) { | 642 TEST_F(SQLConnectionTest, RazeNOTADB2) { |
643 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 643 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
644 ASSERT_TRUE(db().Execute(kCreateSql)); | 644 ASSERT_TRUE(db().Execute(kCreateSql)); |
645 ASSERT_EQ(1, SqliteMasterCount(&db())); | 645 ASSERT_EQ(1, SqliteMasterCount(&db())); |
646 db().Close(); | 646 db().Close(); |
647 | 647 |
648 WriteJunkToDatabase(SQLTestBase::TYPE_OVERWRITE); | 648 WriteJunkToDatabase(SQLTestBase::TYPE_OVERWRITE); |
649 | 649 |
650 // SQLite will successfully open the handle, but will fail with | 650 // SQLite will successfully open the handle, but will fail with |
651 // SQLITE_NOTADB on pragma statemenets which attempt to read the | 651 // SQLITE_NOTADB on pragma statemenets which attempt to read the |
652 // corrupted header. | 652 // corrupted header. |
653 { | 653 { |
654 sql::ScopedErrorIgnorer ignore_errors; | 654 sql::test::ScopedErrorExpecter expecter; |
655 ignore_errors.IgnoreError(SQLITE_NOTADB); | 655 expecter.ExpectError(SQLITE_NOTADB); |
656 EXPECT_TRUE(db().Open(db_path())); | 656 EXPECT_TRUE(db().Open(db_path())); |
657 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 657 ASSERT_TRUE(expecter.SawExpectedErrors()); |
658 } | 658 } |
659 EXPECT_TRUE(db().Raze()); | 659 EXPECT_TRUE(db().Raze()); |
660 db().Close(); | 660 db().Close(); |
661 | 661 |
662 // Now empty, the open should succeed with an empty database. | 662 // Now empty, the open should succeed with an empty database. |
663 EXPECT_TRUE(db().Open(db_path())); | 663 EXPECT_TRUE(db().Open(db_path())); |
664 EXPECT_EQ(0, SqliteMasterCount(&db())); | 664 EXPECT_EQ(0, SqliteMasterCount(&db())); |
665 } | 665 } |
666 | 666 |
667 // Test that a callback from Open() can raze the database. This is | 667 // Test that a callback from Open() can raze the database. This is |
668 // essential for cases where the Open() can fail entirely, so the | 668 // essential for cases where the Open() can fail entirely, so the |
669 // Raze() cannot happen later. Additionally test that when the | 669 // Raze() cannot happen later. Additionally test that when the |
670 // callback does this during Open(), the open is retried and succeeds. | 670 // callback does this during Open(), the open is retried and succeeds. |
671 TEST_F(SQLConnectionTest, RazeCallbackReopen) { | 671 TEST_F(SQLConnectionTest, RazeCallbackReopen) { |
672 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 672 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
673 ASSERT_TRUE(db().Execute(kCreateSql)); | 673 ASSERT_TRUE(db().Execute(kCreateSql)); |
674 ASSERT_EQ(1, SqliteMasterCount(&db())); | 674 ASSERT_EQ(1, SqliteMasterCount(&db())); |
675 db().Close(); | 675 db().Close(); |
676 | 676 |
677 // Corrupt the database so that nothing works, including PRAGMAs. | 677 // Corrupt the database so that nothing works, including PRAGMAs. |
678 ASSERT_TRUE(CorruptSizeInHeaderOfDB()); | 678 ASSERT_TRUE(CorruptSizeInHeaderOfDB()); |
679 | 679 |
680 // Open() will succeed, even though the PRAGMA calls within will | 680 // Open() will succeed, even though the PRAGMA calls within will |
681 // fail with SQLITE_CORRUPT, as will this PRAGMA. | 681 // fail with SQLITE_CORRUPT, as will this PRAGMA. |
682 { | 682 { |
683 sql::ScopedErrorIgnorer ignore_errors; | 683 sql::test::ScopedErrorExpecter expecter; |
684 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 684 expecter.ExpectError(SQLITE_CORRUPT); |
685 ASSERT_TRUE(db().Open(db_path())); | 685 ASSERT_TRUE(db().Open(db_path())); |
686 ASSERT_FALSE(db().Execute("PRAGMA auto_vacuum")); | 686 ASSERT_FALSE(db().Execute("PRAGMA auto_vacuum")); |
687 db().Close(); | 687 db().Close(); |
688 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 688 ASSERT_TRUE(expecter.SawExpectedErrors()); |
689 } | 689 } |
690 | 690 |
691 db().set_error_callback(base::Bind(&SQLConnectionTest::RazeErrorCallback, | 691 db().set_error_callback(base::Bind(&SQLConnectionTest::RazeErrorCallback, |
692 base::Unretained(this), | 692 base::Unretained(this), |
693 SQLITE_CORRUPT)); | 693 SQLITE_CORRUPT)); |
694 | 694 |
695 // When the PRAGMA calls in Open() raise SQLITE_CORRUPT, the error | 695 // When the PRAGMA calls in Open() raise SQLITE_CORRUPT, the error |
696 // callback will call RazeAndClose(). Open() will then fail and be | 696 // callback will call RazeAndClose(). Open() will then fail and be |
697 // retried. The second Open() on the empty database will succeed | 697 // retried. The second Open() on the empty database will succeed |
698 // cleanly. | 698 // cleanly. |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
959 EXPECT_TRUE(other_db.Execute("CREATE TABLE bar (a, b)")); | 959 EXPECT_TRUE(other_db.Execute("CREATE TABLE bar (a, b)")); |
960 EXPECT_TRUE(other_db.Execute("INSERT INTO bar VALUES ('hello', 'world')")); | 960 EXPECT_TRUE(other_db.Execute("INSERT INTO bar VALUES ('hello', 'world')")); |
961 } | 961 } |
962 | 962 |
963 // Cannot see the attached database, yet. | 963 // Cannot see the attached database, yet. |
964 EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); | 964 EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); |
965 | 965 |
966 // Attach fails in a transaction. | 966 // Attach fails in a transaction. |
967 EXPECT_TRUE(db().BeginTransaction()); | 967 EXPECT_TRUE(db().BeginTransaction()); |
968 { | 968 { |
969 sql::ScopedErrorIgnorer ignore_errors; | 969 sql::test::ScopedErrorExpecter expecter; |
970 ignore_errors.IgnoreError(SQLITE_ERROR); | 970 expecter.ExpectError(SQLITE_ERROR); |
971 EXPECT_FALSE(db().AttachDatabase(attach_path, kAttachmentPoint)); | 971 EXPECT_FALSE(db().AttachDatabase(attach_path, kAttachmentPoint)); |
972 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 972 ASSERT_TRUE(expecter.SawExpectedErrors()); |
973 } | 973 } |
974 | 974 |
975 // Attach succeeds when the transaction is closed. | 975 // Attach succeeds when the transaction is closed. |
976 db().RollbackTransaction(); | 976 db().RollbackTransaction(); |
977 EXPECT_TRUE(db().AttachDatabase(attach_path, kAttachmentPoint)); | 977 EXPECT_TRUE(db().AttachDatabase(attach_path, kAttachmentPoint)); |
978 EXPECT_TRUE(db().IsSQLValid("SELECT count(*) from other.bar")); | 978 EXPECT_TRUE(db().IsSQLValid("SELECT count(*) from other.bar")); |
979 | 979 |
980 // Queries can touch both databases. | 980 // Queries can touch both databases. |
981 EXPECT_TRUE(db().Execute("INSERT INTO foo SELECT a, b FROM other.bar")); | 981 EXPECT_TRUE(db().Execute("INSERT INTO foo SELECT a, b FROM other.bar")); |
982 { | 982 { |
983 sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM foo")); | 983 sql::Statement s(db().GetUniqueStatement("SELECT COUNT(*) FROM foo")); |
984 ASSERT_TRUE(s.Step()); | 984 ASSERT_TRUE(s.Step()); |
985 EXPECT_EQ(1, s.ColumnInt(0)); | 985 EXPECT_EQ(1, s.ColumnInt(0)); |
986 } | 986 } |
987 | 987 |
988 // Detach also fails in a transaction. | 988 // Detach also fails in a transaction. |
989 EXPECT_TRUE(db().BeginTransaction()); | 989 EXPECT_TRUE(db().BeginTransaction()); |
990 { | 990 { |
991 sql::ScopedErrorIgnorer ignore_errors; | 991 sql::test::ScopedErrorExpecter expecter; |
992 ignore_errors.IgnoreError(SQLITE_ERROR); | 992 expecter.ExpectError(SQLITE_ERROR); |
993 EXPECT_FALSE(db().DetachDatabase(kAttachmentPoint)); | 993 EXPECT_FALSE(db().DetachDatabase(kAttachmentPoint)); |
994 EXPECT_TRUE(db().IsSQLValid("SELECT count(*) from other.bar")); | 994 EXPECT_TRUE(db().IsSQLValid("SELECT count(*) from other.bar")); |
995 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 995 ASSERT_TRUE(expecter.SawExpectedErrors()); |
996 } | 996 } |
997 | 997 |
998 // Detach succeeds outside of a transaction. | 998 // Detach succeeds outside of a transaction. |
999 db().RollbackTransaction(); | 999 db().RollbackTransaction(); |
1000 EXPECT_TRUE(db().DetachDatabase(kAttachmentPoint)); | 1000 EXPECT_TRUE(db().DetachDatabase(kAttachmentPoint)); |
1001 | 1001 |
1002 EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); | 1002 EXPECT_FALSE(db().IsSQLValid("SELECT count(*) from other.bar")); |
1003 } | 1003 } |
1004 | 1004 |
1005 TEST_F(SQLConnectionTest, Basic_QuickIntegrityCheck) { | 1005 TEST_F(SQLConnectionTest, Basic_QuickIntegrityCheck) { |
1006 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 1006 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
1007 ASSERT_TRUE(db().Execute(kCreateSql)); | 1007 ASSERT_TRUE(db().Execute(kCreateSql)); |
1008 EXPECT_TRUE(db().QuickIntegrityCheck()); | 1008 EXPECT_TRUE(db().QuickIntegrityCheck()); |
1009 db().Close(); | 1009 db().Close(); |
1010 | 1010 |
1011 ASSERT_TRUE(CorruptSizeInHeaderOfDB()); | 1011 ASSERT_TRUE(CorruptSizeInHeaderOfDB()); |
1012 | 1012 |
1013 { | 1013 { |
1014 sql::ScopedErrorIgnorer ignore_errors; | 1014 sql::test::ScopedErrorExpecter expecter; |
1015 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 1015 expecter.ExpectError(SQLITE_CORRUPT); |
1016 ASSERT_TRUE(db().Open(db_path())); | 1016 ASSERT_TRUE(db().Open(db_path())); |
1017 EXPECT_FALSE(db().QuickIntegrityCheck()); | 1017 EXPECT_FALSE(db().QuickIntegrityCheck()); |
1018 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 1018 ASSERT_TRUE(expecter.SawExpectedErrors()); |
1019 } | 1019 } |
1020 } | 1020 } |
1021 | 1021 |
1022 TEST_F(SQLConnectionTest, Basic_FullIntegrityCheck) { | 1022 TEST_F(SQLConnectionTest, Basic_FullIntegrityCheck) { |
1023 const std::string kOk("ok"); | 1023 const std::string kOk("ok"); |
1024 std::vector<std::string> messages; | 1024 std::vector<std::string> messages; |
1025 | 1025 |
1026 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; | 1026 const char* kCreateSql = "CREATE TABLE foo (id INTEGER PRIMARY KEY, value)"; |
1027 ASSERT_TRUE(db().Execute(kCreateSql)); | 1027 ASSERT_TRUE(db().Execute(kCreateSql)); |
1028 EXPECT_TRUE(db().FullIntegrityCheck(&messages)); | 1028 EXPECT_TRUE(db().FullIntegrityCheck(&messages)); |
1029 EXPECT_EQ(1u, messages.size()); | 1029 EXPECT_EQ(1u, messages.size()); |
1030 EXPECT_EQ(kOk, messages[0]); | 1030 EXPECT_EQ(kOk, messages[0]); |
1031 db().Close(); | 1031 db().Close(); |
1032 | 1032 |
1033 ASSERT_TRUE(CorruptSizeInHeaderOfDB()); | 1033 ASSERT_TRUE(CorruptSizeInHeaderOfDB()); |
1034 | 1034 |
1035 { | 1035 { |
1036 sql::ScopedErrorIgnorer ignore_errors; | 1036 sql::test::ScopedErrorExpecter expecter; |
1037 ignore_errors.IgnoreError(SQLITE_CORRUPT); | 1037 expecter.ExpectError(SQLITE_CORRUPT); |
1038 ASSERT_TRUE(db().Open(db_path())); | 1038 ASSERT_TRUE(db().Open(db_path())); |
1039 EXPECT_TRUE(db().FullIntegrityCheck(&messages)); | 1039 EXPECT_TRUE(db().FullIntegrityCheck(&messages)); |
1040 EXPECT_LT(1u, messages.size()); | 1040 EXPECT_LT(1u, messages.size()); |
1041 EXPECT_NE(kOk, messages[0]); | 1041 EXPECT_NE(kOk, messages[0]); |
1042 ASSERT_TRUE(ignore_errors.CheckIgnoredErrors()); | 1042 ASSERT_TRUE(expecter.SawExpectedErrors()); |
1043 } | 1043 } |
1044 | 1044 |
1045 // TODO(shess): CorruptTableOrIndex could be used to produce a | 1045 // TODO(shess): CorruptTableOrIndex could be used to produce a |
1046 // file that would pass the quick check and fail the full check. | 1046 // file that would pass the quick check and fail the full check. |
1047 } | 1047 } |
1048 | 1048 |
1049 // Test Sqlite.Stats histogram for execute-oriented calls. | 1049 // Test Sqlite.Stats histogram for execute-oriented calls. |
1050 TEST_F(SQLConnectionTest, EventsExecute) { | 1050 TEST_F(SQLConnectionTest, EventsExecute) { |
1051 // Re-open with histogram tag. | 1051 // Re-open with histogram tag. |
1052 db().Close(); | 1052 db().Close(); |
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1488 if (DLOG_IS_ON(FATAL)) { | 1488 if (DLOG_IS_ON(FATAL)) { |
1489 db().set_error_callback(base::Bind(&IgnoreErrorCallback)); | 1489 db().set_error_callback(base::Bind(&IgnoreErrorCallback)); |
1490 ASSERT_DEATH({ | 1490 ASSERT_DEATH({ |
1491 db().GetUniqueStatement("SELECT x"); | 1491 db().GetUniqueStatement("SELECT x"); |
1492 }, "SQL compile error no such column: x"); | 1492 }, "SQL compile error no such column: x"); |
1493 } | 1493 } |
1494 #endif | 1494 #endif |
1495 } | 1495 } |
1496 | 1496 |
1497 } // namespace sql | 1497 } // namespace sql |
OLD | NEW |