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 821 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
832 ASSERT_FALSE(recovery.get()); | 832 ASSERT_FALSE(recovery.get()); |
833 | 833 |
834 ASSERT_TRUE(expecter.SawExpectedErrors()); | 834 ASSERT_TRUE(expecter.SawExpectedErrors()); |
835 } | 835 } |
836 | 836 |
837 // Verify that the failure was in the right place with the expected code. | 837 // Verify that the failure was in the right place with the expected code. |
838 tester.ExpectBucketCount(kEventHistogramName, kEventEnum, 1); | 838 tester.ExpectBucketCount(kEventHistogramName, kEventEnum, 1); |
839 tester.ExpectBucketCount(kErrorHistogramName, SQLITE_NOTADB, 1); | 839 tester.ExpectBucketCount(kErrorHistogramName, SQLITE_NOTADB, 1); |
840 } | 840 } |
841 | 841 |
| 842 // Helper for SQLRecoveryTest.PageSize. Creates a fresh db based on db_prefix, |
| 843 // with the given initial page size, and verifies it against the expected size. |
| 844 // Then changes to the final page size and recovers, verifying that the |
| 845 // recovered database ends up with the expected final page size. |
| 846 void TestPageSize(const base::FilePath& db_prefix, |
| 847 int initial_page_size, |
| 848 const std::string& expected_initial_page_size, |
| 849 int final_page_size, |
| 850 const std::string& expected_final_page_size) { |
| 851 const char kCreateSql[] = "CREATE TABLE x (t TEXT)"; |
| 852 const char kInsertSql1[] = "INSERT INTO x VALUES ('This is a test')"; |
| 853 const char kInsertSql2[] = "INSERT INTO x VALUES ('That was a test')"; |
| 854 const char kSelectSql[] = "SELECT * FROM x ORDER BY t"; |
| 855 |
| 856 const base::FilePath db_path = db_prefix.InsertBeforeExtensionASCII( |
| 857 base::IntToString(initial_page_size)); |
| 858 sql::Connection::Delete(db_path); |
| 859 sql::Connection db; |
| 860 db.set_page_size(initial_page_size); |
| 861 ASSERT_TRUE(db.Open(db_path)); |
| 862 ASSERT_TRUE(db.Execute(kCreateSql)); |
| 863 ASSERT_TRUE(db.Execute(kInsertSql1)); |
| 864 ASSERT_TRUE(db.Execute(kInsertSql2)); |
| 865 ASSERT_EQ(expected_initial_page_size, |
| 866 ExecuteWithResult(&db, "PRAGMA page_size")); |
| 867 |
| 868 // Recovery will use the page size set in the connection object, which may not |
| 869 // match the file's page size. |
| 870 db.set_page_size(final_page_size); |
| 871 sql::Recovery::RecoverDatabase(&db, db_path); |
| 872 |
| 873 // Recovery poisoned the handle, must re-open. |
| 874 db.Close(); |
| 875 |
| 876 // Make sure the page size is read from the file. |
| 877 db.set_page_size(0); |
| 878 ASSERT_TRUE(db.Open(db_path)); |
| 879 ASSERT_EQ(expected_final_page_size, |
| 880 ExecuteWithResult(&db, "PRAGMA page_size")); |
| 881 EXPECT_EQ("That was a test\nThis is a test", |
| 882 ExecuteWithResults(&db, kSelectSql, "|", "\n")); |
| 883 } |
| 884 |
| 885 // Verify that sql::Recovery maintains the page size, and the virtual table |
| 886 // works with page sizes other than SQLite's default. Also verify the case |
| 887 // where the default page size has changed. |
| 888 TEST_F(SQLRecoveryTest, PageSize) { |
| 889 const std::string default_page_size = |
| 890 ExecuteWithResult(&db(), "PRAGMA page_size"); |
| 891 |
| 892 // The database should have the default page size after recovery. |
| 893 EXPECT_NO_FATAL_FAILURE( |
| 894 TestPageSize(db_path(), 0, default_page_size, 0, default_page_size)); |
| 895 |
| 896 // Sync user 32k pages. |
| 897 EXPECT_NO_FATAL_FAILURE( |
| 898 TestPageSize(db_path(), 32768, "32768", 32768, "32768")); |
| 899 |
| 900 // Many clients use 4k pages. This is the SQLite default after 3.12.0. |
| 901 EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path(), 4096, "4096", 4096, "4096")); |
| 902 |
| 903 // 1k is the default page size before 3.12.0. |
| 904 EXPECT_NO_FATAL_FAILURE(TestPageSize(db_path(), 1024, "1024", 1024, "1024")); |
| 905 |
| 906 // Databases with no page size specified should recover with the new default |
| 907 // page size. 2k has never been the default page size. |
| 908 ASSERT_NE("2048", default_page_size); |
| 909 EXPECT_NO_FATAL_FAILURE( |
| 910 TestPageSize(db_path(), 2048, "2048", 0, default_page_size)); |
| 911 } |
| 912 |
842 } // namespace | 913 } // namespace |
OLD | NEW |