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> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
16 #include "base/files/scoped_temp_dir.h" | 16 #include "base/files/scoped_temp_dir.h" |
17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
18 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/stringprintf.h" | |
19 #include "base/test/histogram_tester.h" | 20 #include "base/test/histogram_tester.h" |
20 #include "sql/connection.h" | 21 #include "sql/connection.h" |
21 #include "sql/meta_table.h" | 22 #include "sql/meta_table.h" |
22 #include "sql/statement.h" | 23 #include "sql/statement.h" |
23 #include "sql/test/paths.h" | 24 #include "sql/test/paths.h" |
24 #include "sql/test/scoped_error_expecter.h" | 25 #include "sql/test/scoped_error_expecter.h" |
25 #include "sql/test/sql_test_base.h" | 26 #include "sql/test/sql_test_base.h" |
26 #include "sql/test/test_helpers.h" | 27 #include "sql/test/test_helpers.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
28 #include "third_party/sqlite/sqlite3.h" | 29 #include "third_party/sqlite/sqlite3.h" |
(...skipping 803 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
832 ASSERT_FALSE(recovery.get()); | 833 ASSERT_FALSE(recovery.get()); |
833 | 834 |
834 ASSERT_TRUE(expecter.SawExpectedErrors()); | 835 ASSERT_TRUE(expecter.SawExpectedErrors()); |
835 } | 836 } |
836 | 837 |
837 // Verify that the failure was in the right place with the expected code. | 838 // Verify that the failure was in the right place with the expected code. |
838 tester.ExpectBucketCount(kEventHistogramName, kEventEnum, 1); | 839 tester.ExpectBucketCount(kEventHistogramName, kEventEnum, 1); |
839 tester.ExpectBucketCount(kErrorHistogramName, SQLITE_NOTADB, 1); | 840 tester.ExpectBucketCount(kErrorHistogramName, SQLITE_NOTADB, 1); |
840 } | 841 } |
841 | 842 |
842 } // namespace | 843 // Helper for SQLRecoveryTest.PageSize. Creates a fresh db based on db_prefix, |
844 // with the given initial page size, and verifies it against the expected size. | |
845 // Then changes to the final page size and recovers, verifying that the | |
846 // recovered database ends up with the expected final page size. | |
847 void TestPageSize(const base::FilePath& db_prefix, | |
848 int initial_page_size, | |
849 const std::string& expected_initial_page_size, | |
850 int final_page_size, | |
851 const std::string& expected_final_page_size) { | |
852 SCOPED_TRACE( | |
853 base::StringPrintf("page sizes {initial=%d/%s, final=%d/%s}", | |
854 initial_page_size, expected_initial_page_size.c_str(), | |
855 final_page_size, expected_final_page_size.c_str())); | |
856 | |
857 const char kCreateSql[] = "CREATE TABLE x (t TEXT)"; | |
858 const char kInsertSql1[] = "INSERT INTO x VALUES ('This is a test')"; | |
859 const char kInsertSql2[] = "INSERT INTO x VALUES ('That was a test')"; | |
860 const char kSelectSql[] = "SELECT * FROM x ORDER BY t"; | |
861 | |
862 const base::FilePath db_path = db_prefix.InsertBeforeExtensionASCII( | |
863 base::StringPrintf("%d", initial_page_size)); | |
864 sql::Connection::Delete(db_path); | |
865 sql::Connection db; | |
866 db.set_page_size(initial_page_size); | |
867 ASSERT_TRUE(db.Open(db_path)); | |
868 ASSERT_TRUE(db.Execute(kCreateSql)); | |
869 ASSERT_TRUE(db.Execute(kInsertSql1)); | |
870 ASSERT_TRUE(db.Execute(kInsertSql2)); | |
871 ASSERT_EQ(expected_initial_page_size, | |
872 ExecuteWithResult(&db, "PRAGMA page_size")); | |
873 | |
874 // Recovery will use the page size set in the connection object, which may not | |
875 // match the file's page size. | |
876 db.set_page_size(final_page_size); | |
877 sql::Recovery::RecoverDatabase(&db, db_path); | |
878 | |
879 // Recovery poisoned the handle, must re-open. | |
880 db.Close(); | |
881 | |
882 // Make sure the page size is read from the file. | |
883 db.set_page_size(0); | |
884 ASSERT_TRUE(db.Open(db_path)); | |
885 ASSERT_EQ(expected_final_page_size, | |
886 ExecuteWithResult(&db, "PRAGMA page_size")); | |
887 EXPECT_EQ("That was a test\nThis is a test", | |
888 ExecuteWithResults(&db, kSelectSql, "|", "\n")); | |
889 } | |
890 | |
891 // Verify that sql::Recovery maintains the page size, and the virtual table | |
892 // works with page sizes other than SQLite's default. Also verify the case | |
893 // where the default page size has changed. | |
894 TEST_F(SQLRecoveryTest, PageSize) { | |
895 const std::string default_page_size = | |
896 ExecuteWithResult(&db(), "PRAGMA page_size"); | |
897 | |
898 // The database should have the default page size after recovery. | |
michaeln
2016/10/27 00:02:29
SCOPED_TRACE looks like a good tool for this
alte
Scott Hess - ex-Googler
2016/10/27 19:14:58
Indeed, and that removes the need for convoluted s
| |
899 TestPageSize(db_path(), 0, default_page_size, 0, default_page_size); | |
900 | |
901 // Sync user 32k pages. | |
902 TestPageSize(db_path(), 32768, "32768", 32768, "32768"); | |
903 | |
904 // Many clients use 4k pages. This is the SQLite default after 3.12.0. | |
905 TestPageSize(db_path(), 4096, "4096", 4096, "4096"); | |
906 | |
907 // 1k is the default page size before 3.12.0. | |
908 TestPageSize(db_path(), 1024, "1024", 1024, "1024"); | |
909 | |
910 // Databases with no page size specified should recover with the new default | |
911 // page size. 2k has never been the default page size. | |
912 ASSERT_NE("2048", default_page_size); | |
913 TestPageSize(db_path(), 2048, "2048", 0, default_page_size); | |
914 } | |
915 | |
916 } // namespac | |
OLD | NEW |