OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" |
13 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
14 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
15 #include "sql/connection.h" | 17 #include "sql/connection.h" |
16 #include "sql/statement.h" | 18 #include "sql/statement.h" |
| 19 #include "sql/test/scoped_error_ignorer.h" |
| 20 #include "sql/test/test_helpers.h" |
17 #include "sync/base/sync_export.h" | 21 #include "sync/base/sync_export.h" |
18 #include "sync/internal_api/public/base/node_ordinal.h" | 22 #include "sync/internal_api/public/base/node_ordinal.h" |
19 #include "sync/protocol/bookmark_specifics.pb.h" | 23 #include "sync/protocol/bookmark_specifics.pb.h" |
20 #include "sync/protocol/sync.pb.h" | 24 #include "sync/protocol/sync.pb.h" |
21 #include "sync/syncable/directory.h" | 25 #include "sync/syncable/directory.h" |
22 #include "sync/syncable/directory_backing_store.h" | 26 #include "sync/syncable/directory_backing_store.h" |
23 #include "sync/syncable/on_disk_directory_backing_store.h" | 27 #include "sync/syncable/on_disk_directory_backing_store.h" |
24 #include "sync/syncable/syncable-inl.h" | 28 #include "sync/syncable/syncable-inl.h" |
25 #include "sync/test/test_directory_backing_store.h" | 29 #include "sync/test/test_directory_backing_store.h" |
26 #include "sync/util/time.h" | 30 #include "sync/util/time.h" |
27 #include "testing/gtest/include/gtest/gtest-param-test.h" | 31 #include "testing/gtest/include/gtest/gtest-param-test.h" |
28 | 32 |
| 33 namespace { |
| 34 |
| 35 // A handler that simply sets |catastrophic_error_handler_was_called| to true. |
| 36 void CatastrophicErrorHandler(bool* catastrophic_error_handler_was_called) { |
| 37 *catastrophic_error_handler_was_called = true; |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
29 namespace syncer { | 42 namespace syncer { |
30 namespace syncable { | 43 namespace syncable { |
31 | 44 |
32 SYNC_EXPORT_PRIVATE extern const int32 kCurrentDBVersion; | 45 SYNC_EXPORT_PRIVATE extern const int32 kCurrentDBVersion; |
33 | 46 |
34 class MigrationTest : public testing::TestWithParam<int> { | 47 class MigrationTest : public testing::TestWithParam<int> { |
35 public: | 48 public: |
36 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } | 49 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } |
37 | 50 |
38 protected: | 51 protected: |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 SetUpVersion89Database(connection); // Prepopulates data. | 95 SetUpVersion89Database(connection); // Prepopulates data. |
83 scoped_ptr<TestDirectoryBackingStore> dbs( | 96 scoped_ptr<TestDirectoryBackingStore> dbs( |
84 new TestDirectoryBackingStore(GetUsername(), connection)); | 97 new TestDirectoryBackingStore(GetUsername(), connection)); |
85 ASSERT_EQ(kCurrentDBVersion, dbs->GetVersion()); | 98 ASSERT_EQ(kCurrentDBVersion, dbs->GetVersion()); |
86 | 99 |
87 ASSERT_TRUE(LoadAndIgnoreReturnedData(dbs.get())); | 100 ASSERT_TRUE(LoadAndIgnoreReturnedData(dbs.get())); |
88 ASSERT_FALSE(dbs->needs_column_refresh()); | 101 ASSERT_FALSE(dbs->needs_column_refresh()); |
89 } | 102 } |
90 | 103 |
91 private: | 104 private: |
| 105 base::MessageLoop message_loop_; |
92 base::ScopedTempDir temp_dir_; | 106 base::ScopedTempDir temp_dir_; |
93 }; | 107 }; |
94 | 108 |
95 class DirectoryBackingStoreTest : public MigrationTest {}; | 109 class DirectoryBackingStoreTest : public MigrationTest {}; |
96 | 110 |
97 #if defined(OS_WIN) | 111 #if defined(OS_WIN) |
98 | 112 |
99 // On Windows, we used to store timestamps in FILETIME format. | 113 // On Windows, we used to store timestamps in FILETIME format. |
100 #define LEGACY_META_PROTO_TIMES_1 129079956640320000LL | 114 #define LEGACY_META_PROTO_TIMES_1 129079956640320000LL |
101 #define LEGACY_META_PROTO_TIMES_2 128976886618480000LL | 115 #define LEGACY_META_PROTO_TIMES_2 128976886618480000LL |
(...skipping 3886 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3988 int pageSize = 0; | 4002 int pageSize = 0; |
3989 dbs->GetDatabasePageSize(&pageSize); | 4003 dbs->GetDatabasePageSize(&pageSize); |
3990 EXPECT_TRUE(32768 != pageSize); | 4004 EXPECT_TRUE(32768 != pageSize); |
3991 dbs->db_->set_page_size(32768); | 4005 dbs->db_->set_page_size(32768); |
3992 dbs->IncreasePageSizeTo32K(); | 4006 dbs->IncreasePageSizeTo32K(); |
3993 pageSize = 0; | 4007 pageSize = 0; |
3994 dbs->GetDatabasePageSize(&pageSize); | 4008 dbs->GetDatabasePageSize(&pageSize); |
3995 EXPECT_EQ(32768, pageSize); | 4009 EXPECT_EQ(32768, pageSize); |
3996 } | 4010 } |
3997 | 4011 |
| 4012 // See that a catastrophic error handler remains set across instances of the |
| 4013 // underlying sql:Connection. |
| 4014 TEST_F(DirectoryBackingStoreTest, CatastrophicErrorHandler_KeptAcrossReset) { |
| 4015 scoped_ptr<OnDiskDirectoryBackingStoreForTest> dbs( |
| 4016 new OnDiskDirectoryBackingStoreForTest(GetUsername(), GetDatabasePath())); |
| 4017 // See that by default there is no catastrophic error handler. |
| 4018 ASSERT_FALSE(dbs->db_->has_error_callback()); |
| 4019 // Set one and see that it was set. |
| 4020 dbs->SetCatastrophicErrorHandler( |
| 4021 base::Bind(&CatastrophicErrorHandler, nullptr)); |
| 4022 ASSERT_TRUE(dbs->db_->has_error_callback()); |
| 4023 // Recreate the Connection and see that the handler remains set. |
| 4024 dbs->ResetAndCreateConnection(); |
| 4025 ASSERT_TRUE(dbs->db_->has_error_callback()); |
| 4026 } |
| 4027 |
| 4028 // Verify that database corruption will trigger the catastrohpic error handler. |
| 4029 TEST_F(DirectoryBackingStoreTest, CatastrophicErrorHandler_Invocation) { |
| 4030 bool was_called = false; |
| 4031 const base::Closure handler = |
| 4032 base::Bind(&CatastrophicErrorHandler, &was_called); |
| 4033 { |
| 4034 scoped_ptr<OnDiskDirectoryBackingStoreForTest> dbs( |
| 4035 new OnDiskDirectoryBackingStoreForTest(GetUsername(), |
| 4036 GetDatabasePath())); |
| 4037 dbs->SetCatastrophicErrorHandler(handler); |
| 4038 ASSERT_TRUE(dbs->db_->has_error_callback()); |
| 4039 // Load the DB, and save one entry. |
| 4040 ASSERT_TRUE(LoadAndIgnoreReturnedData(dbs.get())); |
| 4041 ASSERT_FALSE(dbs->DidFailFirstOpenAttempt()); |
| 4042 Directory::SaveChangesSnapshot snapshot; |
| 4043 scoped_ptr<EntryKernel> entry(new EntryKernel()); |
| 4044 entry->put(ID, Id::CreateFromClientString("test_entry")); |
| 4045 entry->put(META_HANDLE, 2); |
| 4046 entry->mark_dirty(NULL); |
| 4047 snapshot.dirty_metas.insert(entry.release()); |
| 4048 ASSERT_TRUE(dbs->SaveChanges(snapshot)); |
| 4049 } |
| 4050 |
| 4051 base::RunLoop().RunUntilIdle(); |
| 4052 // No catastrophic errors have happened. See that it hasn't be called yet. |
| 4053 ASSERT_FALSE(was_called); |
| 4054 |
| 4055 // Corrupt the DB. Some forms of corruption (like this one) will be detected |
| 4056 // upon loading the Sync DB. |
| 4057 ASSERT_TRUE(sql::test::CorruptSizeInHeader(GetDatabasePath())); |
| 4058 |
| 4059 { |
| 4060 scoped_ptr<OnDiskDirectoryBackingStoreForTest> dbs( |
| 4061 new OnDiskDirectoryBackingStoreForTest(GetUsername(), |
| 4062 GetDatabasePath())); |
| 4063 dbs->SetCatastrophicErrorHandler(handler); |
| 4064 ASSERT_TRUE(dbs->db_->has_error_callback()); |
| 4065 { |
| 4066 // The corruption will be detected when we attempt to load the data. Use a |
| 4067 // ScopedErrorIgnorer to ensure we don't crash in debug builds. |
| 4068 sql::ScopedErrorIgnorer error_ignorer; |
| 4069 error_ignorer.IgnoreError(SQLITE_CORRUPT); |
| 4070 ASSERT_TRUE(LoadAndIgnoreReturnedData(dbs.get())); |
| 4071 ASSERT_TRUE(error_ignorer.CheckIgnoredErrors()); |
| 4072 } |
| 4073 // See that the first open failed as expected. |
| 4074 ASSERT_TRUE(dbs->DidFailFirstOpenAttempt()); |
| 4075 } |
| 4076 |
| 4077 // At this point the handler has been posted but not executed. |
| 4078 ASSERT_FALSE(was_called); |
| 4079 // Pump the message loop and see that it is executed. |
| 4080 base::RunLoop().RunUntilIdle(); |
| 4081 ASSERT_TRUE(was_called); |
| 4082 } |
| 4083 |
3998 } // namespace syncable | 4084 } // namespace syncable |
3999 } // namespace syncer | 4085 } // namespace syncer |
OLD | NEW |