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/test/histogram_tester.h" |
19 #include "sql/connection.h" | 20 #include "sql/connection.h" |
20 #include "sql/meta_table.h" | 21 #include "sql/meta_table.h" |
21 #include "sql/statement.h" | 22 #include "sql/statement.h" |
22 #include "sql/test/paths.h" | 23 #include "sql/test/paths.h" |
23 #include "sql/test/scoped_error_expecter.h" | 24 #include "sql/test/scoped_error_expecter.h" |
24 #include "sql/test/sql_test_base.h" | 25 #include "sql/test/sql_test_base.h" |
25 #include "sql/test/test_helpers.h" | 26 #include "sql/test/test_helpers.h" |
26 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
27 #include "third_party/sqlite/sqlite3.h" | 28 #include "third_party/sqlite/sqlite3.h" |
28 | 29 |
(...skipping 796 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 | 826 |
826 // Test that the trigger works. | 827 // Test that the trigger works. |
827 ASSERT_TRUE(db().Execute("DELETE FROM x WHERE v = 'truck'")); | 828 ASSERT_TRUE(db().Execute("DELETE FROM x WHERE v = 'truck'")); |
828 EXPECT_EQ("1|turtle\n3|trailer", | 829 EXPECT_EQ("1|turtle\n3|trailer", |
829 ExecuteWithResults(&db(), kXSql, "|", "\n")); | 830 ExecuteWithResults(&db(), kXSql, "|", "\n")); |
830 EXPECT_EQ("dean|trailer\njim|telephone", | 831 EXPECT_EQ("dean|trailer\njim|telephone", |
831 ExecuteWithResults(&db(), kYSql, "|", "\n")); | 832 ExecuteWithResults(&db(), kYSql, "|", "\n")); |
832 EXPECT_EQ("trailer", ExecuteWithResults(&db(), kVSql, "|", "\n")); | 833 EXPECT_EQ("trailer", ExecuteWithResults(&db(), kVSql, "|", "\n")); |
833 } | 834 } |
834 | 835 |
| 836 // Test histograms recorded when the invalid database cannot be attached. |
| 837 TEST_F(SQLRecoveryTest, AttachFailure) { |
| 838 // Create a valid database, then write junk over the header. This should lead |
| 839 // to SQLITE_NOTADB, which will cause ATTACH to fail. |
| 840 ASSERT_TRUE(db().Execute("CREATE TABLE x (t TEXT)")); |
| 841 ASSERT_TRUE(db().Execute("INSERT INTO x VALUES ('This is a test')")); |
| 842 db().Close(); |
| 843 WriteJunkToDatabase(SQLTestBase::TYPE_OVERWRITE); |
| 844 |
| 845 const char kEventHistogramName[] = "Sqlite.RecoveryEvents"; |
| 846 const int kEventEnum = 5; // RECOVERY_FAILED_ATTACH |
| 847 const char kErrorHistogramName[] = "Sqlite.RecoveryAttachError"; |
| 848 base::HistogramTester tester; |
| 849 |
| 850 { |
| 851 sql::test::ScopedErrorExpecter expecter; |
| 852 expecter.ExpectError(SQLITE_NOTADB); |
| 853 |
| 854 // Reopen() here because it will see SQLITE_NOTADB. |
| 855 ASSERT_TRUE(Reopen()); |
| 856 |
| 857 // Begin() should fail. |
| 858 std::unique_ptr<sql::Recovery> |
| 859 recovery = sql::Recovery::Begin(&db(), db_path()); |
| 860 ASSERT_FALSE(recovery.get()); |
| 861 |
| 862 ASSERT_TRUE(expecter.SawExpectedErrors()); |
| 863 } |
| 864 |
| 865 // Verify that the failure was in the right place with the expected code. |
| 866 tester.ExpectBucketCount(kEventHistogramName, kEventEnum, 1); |
| 867 tester.ExpectBucketCount(kErrorHistogramName, SQLITE_NOTADB, 1); |
| 868 } |
| 869 |
835 } // namespace | 870 } // namespace |
OLD | NEW |