OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "sql/recovery.h" | 5 #include "sql/recovery.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
(...skipping 787 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
798 | 798 |
799 // Test that the trigger works. | 799 // Test that the trigger works. |
800 ASSERT_TRUE(db().Execute("DELETE FROM x WHERE v = 'truck'")); | 800 ASSERT_TRUE(db().Execute("DELETE FROM x WHERE v = 'truck'")); |
801 EXPECT_EQ("1|turtle\n3|trailer", | 801 EXPECT_EQ("1|turtle\n3|trailer", |
802 ExecuteWithResults(&db(), kXSql, "|", "\n")); | 802 ExecuteWithResults(&db(), kXSql, "|", "\n")); |
803 EXPECT_EQ("dean|trailer\njim|telephone", | 803 EXPECT_EQ("dean|trailer\njim|telephone", |
804 ExecuteWithResults(&db(), kYSql, "|", "\n")); | 804 ExecuteWithResults(&db(), kYSql, "|", "\n")); |
805 EXPECT_EQ("trailer", ExecuteWithResults(&db(), kVSql, "|", "\n")); | 805 EXPECT_EQ("trailer", ExecuteWithResults(&db(), kVSql, "|", "\n")); |
806 } | 806 } |
807 | 807 |
| 808 // When RecoverDatabase() encounters SQLITE_NOTADB, the database is deleted. |
| 809 TEST_F(SQLRecoveryTest, RecoverDatabaseDelete) { |
| 810 // Create a valid database, then write junk over the header. This should lead |
| 811 // to SQLITE_NOTADB, which will cause ATTACH to fail. |
| 812 ASSERT_TRUE(db().Execute("CREATE TABLE x (t TEXT)")); |
| 813 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES ('This is a test')")); |
| 814 db().Close(); |
| 815 WriteJunkToDatabase(SQLTestBase::TYPE_OVERWRITE); |
| 816 |
| 817 { |
| 818 sql::test::ScopedErrorExpecter expecter; |
| 819 expecter.ExpectError(SQLITE_NOTADB); |
| 820 |
| 821 // Reopen() here because it will see SQLITE_NOTADB. |
| 822 ASSERT_TRUE(Reopen()); |
| 823 |
| 824 // This should "recover" the database by making it valid, but empty. |
| 825 sql::Recovery::RecoverDatabase(&db(), db_path()); |
| 826 |
| 827 ASSERT_TRUE(expecter.SawExpectedErrors()); |
| 828 } |
| 829 |
| 830 // Recovery poisoned the handle, must re-open. |
| 831 db().Close(); |
| 832 ASSERT_TRUE(Reopen()); |
| 833 |
| 834 EXPECT_EQ("", GetSchema(&db())); |
| 835 } |
| 836 |
808 // Test histograms recorded when the invalid database cannot be attached. | 837 // Test histograms recorded when the invalid database cannot be attached. |
809 TEST_F(SQLRecoveryTest, AttachFailure) { | 838 TEST_F(SQLRecoveryTest, AttachFailure) { |
810 // Create a valid database, then write junk over the header. This should lead | 839 // Create a valid database, then write junk over the header. This should lead |
811 // to SQLITE_NOTADB, which will cause ATTACH to fail. | 840 // to SQLITE_NOTADB, which will cause ATTACH to fail. |
812 ASSERT_TRUE(db().Execute("CREATE TABLE x (t TEXT)")); | 841 ASSERT_TRUE(db().Execute("CREATE TABLE x (t TEXT)")); |
813 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES ('This is a test')")); | 842 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES ('This is a test')")); |
814 db().Close(); | 843 db().Close(); |
815 WriteJunkToDatabase(SQLTestBase::TYPE_OVERWRITE); | 844 WriteJunkToDatabase(SQLTestBase::TYPE_OVERWRITE); |
816 | 845 |
817 const char kEventHistogramName[] = "Sqlite.RecoveryEvents"; | 846 const char kEventHistogramName[] = "Sqlite.RecoveryEvents"; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
904 EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path(), 1024, "1024", 1024, "1024")); | 933 EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path(), 1024, "1024", 1024, "1024")); |
905 | 934 |
906 // Databases with no page size specified should recover with the new default | 935 // Databases with no page size specified should recover with the new default |
907 // page size. 2k has never been the default page size. | 936 // page size. 2k has never been the default page size. |
908 ASSERT_NE("2048", default_page_size); | 937 ASSERT_NE("2048", default_page_size); |
909 EXPECT_NO_FATAL_FAILURE( | 938 EXPECT_NO_FATAL_FAILURE( |
910 TestPageSize(db_path(), 2048, "2048", 0, default_page_size)); | 939 TestPageSize(db_path(), 2048, "2048", 0, default_page_size)); |
911 } | 940 } |
912 | 941 |
913 } // namespace | 942 } // namespace |
OLD | NEW |