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_file.h" | |
11 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
14 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
15 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
17 #include "sql/connection.h" | 18 #include "sql/connection.h" |
18 #include "sql/statement.h" | 19 #include "sql/statement.h" |
19 #include "sql/test/scoped_error_ignorer.h" | 20 #include "sql/test/scoped_error_ignorer.h" |
20 #include "sql/test/test_helpers.h" | 21 #include "sql/test/test_helpers.h" |
21 #include "sync/base/sync_export.h" | 22 #include "sync/base/sync_export.h" |
22 #include "sync/internal_api/public/base/node_ordinal.h" | 23 #include "sync/internal_api/public/base/node_ordinal.h" |
23 #include "sync/protocol/bookmark_specifics.pb.h" | 24 #include "sync/protocol/bookmark_specifics.pb.h" |
24 #include "sync/protocol/sync.pb.h" | 25 #include "sync/protocol/sync.pb.h" |
25 #include "sync/syncable/directory.h" | 26 #include "sync/syncable/directory.h" |
26 #include "sync/syncable/directory_backing_store.h" | 27 #include "sync/syncable/directory_backing_store.h" |
27 #include "sync/syncable/on_disk_directory_backing_store.h" | 28 #include "sync/syncable/on_disk_directory_backing_store.h" |
28 #include "sync/syncable/syncable-inl.h" | 29 #include "sync/syncable/syncable-inl.h" |
29 #include "sync/test/test_directory_backing_store.h" | 30 #include "sync/test/test_directory_backing_store.h" |
30 #include "sync/util/time.h" | 31 #include "sync/util/time.h" |
31 #include "testing/gtest/include/gtest/gtest-param-test.h" | 32 #include "testing/gtest/include/gtest/gtest-param-test.h" |
32 | 33 |
34 namespace syncer { | |
35 namespace syncable { | |
33 namespace { | 36 namespace { |
34 | 37 |
35 // A handler that simply sets |catastrophic_error_handler_was_called| to true. | 38 // A handler that simply sets |catastrophic_error_handler_was_called| to true. |
36 void CatastrophicErrorHandler(bool* catastrophic_error_handler_was_called) { | 39 void CatastrophicErrorHandler(bool* catastrophic_error_handler_was_called) { |
37 *catastrophic_error_handler_was_called = true; | 40 *catastrophic_error_handler_was_called = true; |
38 } | 41 } |
39 | 42 |
43 // Create a dirty EntryKernel with an ID derived from |id|. | |
44 scoped_ptr<EntryKernel> CreateEntry(int id) { | |
45 scoped_ptr<EntryKernel> entry(new EntryKernel()); | |
46 entry->put(ID, Id::CreateFromClientString(base::Int64ToString(id))); | |
47 entry->put(META_HANDLE, id); | |
48 entry->mark_dirty(NULL); | |
49 return entry; | |
50 } | |
51 | |
40 } // namespace | 52 } // namespace |
41 | 53 |
42 namespace syncer { | |
43 namespace syncable { | |
44 | |
45 SYNC_EXPORT_PRIVATE extern const int32 kCurrentDBVersion; | 54 SYNC_EXPORT_PRIVATE extern const int32 kCurrentDBVersion; |
46 | 55 |
47 class MigrationTest : public testing::TestWithParam<int> { | 56 class MigrationTest : public testing::TestWithParam<int> { |
48 public: | 57 public: |
49 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } | 58 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } |
50 | 59 |
51 protected: | 60 protected: |
52 std::string GetUsername() { | 61 std::string GetUsername() { |
53 return "nick@chromium.org"; | 62 return "nick@chromium.org"; |
54 } | 63 } |
(...skipping 3963 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
4018 ASSERT_FALSE(dbs->db_->has_error_callback()); | 4027 ASSERT_FALSE(dbs->db_->has_error_callback()); |
4019 // Set one and see that it was set. | 4028 // Set one and see that it was set. |
4020 dbs->SetCatastrophicErrorHandler( | 4029 dbs->SetCatastrophicErrorHandler( |
4021 base::Bind(&CatastrophicErrorHandler, nullptr)); | 4030 base::Bind(&CatastrophicErrorHandler, nullptr)); |
4022 ASSERT_TRUE(dbs->db_->has_error_callback()); | 4031 ASSERT_TRUE(dbs->db_->has_error_callback()); |
4023 // Recreate the Connection and see that the handler remains set. | 4032 // Recreate the Connection and see that the handler remains set. |
4024 dbs->ResetAndCreateConnection(); | 4033 dbs->ResetAndCreateConnection(); |
4025 ASSERT_TRUE(dbs->db_->has_error_callback()); | 4034 ASSERT_TRUE(dbs->db_->has_error_callback()); |
4026 } | 4035 } |
4027 | 4036 |
4028 // Verify that database corruption will trigger the catastrohpic error handler. | 4037 // Verify that database corruption encountered during Load will trigger the |
4029 TEST_F(DirectoryBackingStoreTest, CatastrophicErrorHandler_Invocation) { | 4038 // catastrohpic error handler. |
4039 TEST_F(DirectoryBackingStoreTest, | |
4040 CatastrophicErrorHandler_InvocationDuringLoad) { | |
4030 bool was_called = false; | 4041 bool was_called = false; |
4031 const base::Closure handler = | 4042 const base::Closure handler = |
4032 base::Bind(&CatastrophicErrorHandler, &was_called); | 4043 base::Bind(&CatastrophicErrorHandler, &was_called); |
4033 { | 4044 { |
4034 scoped_ptr<OnDiskDirectoryBackingStoreForTest> dbs( | 4045 scoped_ptr<OnDiskDirectoryBackingStoreForTest> dbs( |
4035 new OnDiskDirectoryBackingStoreForTest(GetUsername(), | 4046 new OnDiskDirectoryBackingStoreForTest(GetUsername(), |
4036 GetDatabasePath())); | 4047 GetDatabasePath())); |
4037 dbs->SetCatastrophicErrorHandler(handler); | 4048 dbs->SetCatastrophicErrorHandler(handler); |
4038 ASSERT_TRUE(dbs->db_->has_error_callback()); | 4049 ASSERT_TRUE(dbs->db_->has_error_callback()); |
4039 // Load the DB, and save one entry. | 4050 // Load the DB, and save one entry. |
4040 ASSERT_TRUE(LoadAndIgnoreReturnedData(dbs.get())); | 4051 ASSERT_TRUE(LoadAndIgnoreReturnedData(dbs.get())); |
4041 ASSERT_FALSE(dbs->DidFailFirstOpenAttempt()); | 4052 ASSERT_FALSE(dbs->DidFailFirstOpenAttempt()); |
4042 Directory::SaveChangesSnapshot snapshot; | 4053 Directory::SaveChangesSnapshot snapshot; |
4043 scoped_ptr<EntryKernel> entry(new EntryKernel()); | 4054 snapshot.dirty_metas.insert(CreateEntry(2).release()); |
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)); | 4055 ASSERT_TRUE(dbs->SaveChanges(snapshot)); |
4049 } | 4056 } |
4050 | 4057 |
4051 base::RunLoop().RunUntilIdle(); | 4058 base::RunLoop().RunUntilIdle(); |
4052 // No catastrophic errors have happened. See that it hasn't be called yet. | 4059 // No catastrophic errors have happened. See that it hasn't be called yet. |
4053 ASSERT_FALSE(was_called); | 4060 ASSERT_FALSE(was_called); |
4054 | 4061 |
4055 // Corrupt the DB. Some forms of corruption (like this one) will be detected | 4062 // Corrupt the DB. Some forms of corruption (like this one) will be detected |
4056 // upon loading the Sync DB. | 4063 // upon loading the Sync DB. |
4057 ASSERT_TRUE(sql::test::CorruptSizeInHeader(GetDatabasePath())); | 4064 ASSERT_TRUE(sql::test::CorruptSizeInHeader(GetDatabasePath())); |
(...skipping 16 matching lines...) Expand all Loading... | |
4074 ASSERT_TRUE(dbs->DidFailFirstOpenAttempt()); | 4081 ASSERT_TRUE(dbs->DidFailFirstOpenAttempt()); |
4075 } | 4082 } |
4076 | 4083 |
4077 // At this point the handler has been posted but not executed. | 4084 // At this point the handler has been posted but not executed. |
4078 ASSERT_FALSE(was_called); | 4085 ASSERT_FALSE(was_called); |
4079 // Pump the message loop and see that it is executed. | 4086 // Pump the message loop and see that it is executed. |
4080 base::RunLoop().RunUntilIdle(); | 4087 base::RunLoop().RunUntilIdle(); |
4081 ASSERT_TRUE(was_called); | 4088 ASSERT_TRUE(was_called); |
4082 } | 4089 } |
4083 | 4090 |
4091 // Verify that database corruption encountered during SaveChanges will trigger | |
4092 // the catastrohpic error handler. | |
4093 TEST_F(DirectoryBackingStoreTest, | |
4094 CatastrophicErrorHandler_InvocationDuringSaveChanges) { | |
4095 bool was_called = false; | |
4096 const base::Closure handler = | |
4097 base::Bind(&CatastrophicErrorHandler, &was_called); | |
4098 | |
4099 // Create a DB with many entries. The DB must be large enough that sqlite | |
4100 // won't keep it all in memory. If this test fails, you may need to increase | |
Nicolas Zea
2015/04/23 21:53:27
Hmm, do we control how many entries SQLite will ke
maniscalco
2015/04/23 22:57:48
It's complicated and I don't fully understand it.
| |
4101 // the number of entries written to the DB. | |
4102 scoped_ptr<OnDiskDirectoryBackingStoreForTest> dbs( | |
4103 new OnDiskDirectoryBackingStoreForTest(GetUsername(), GetDatabasePath())); | |
4104 dbs->SetCatastrophicErrorHandler(handler); | |
4105 ASSERT_TRUE(dbs->db_->has_error_callback()); | |
4106 ASSERT_TRUE(LoadAndIgnoreReturnedData(dbs.get())); | |
4107 ASSERT_FALSE(dbs->DidFailFirstOpenAttempt()); | |
4108 Directory::SaveChangesSnapshot snapshot; | |
4109 const int num_entries = 4000; | |
4110 for (int i = 0; i < num_entries; ++i) { | |
4111 snapshot.dirty_metas.insert(CreateEntry(i).release()); | |
4112 } | |
4113 ASSERT_TRUE(dbs->SaveChanges(snapshot)); | |
4114 | |
4115 // Corrupt the DB by write a bunch of zeros at the beginning. | |
4116 { | |
4117 // Because the file is already open for writing (see dbs above), it's | |
4118 // important that we open it in a sharing compatible way for platforms that | |
4119 // have the concept of shared/exclusive file access (e.g. Windows). | |
4120 base::ScopedFILE db_file(base::OpenFile(GetDatabasePath(), "wb")); | |
4121 ASSERT_TRUE(db_file.get()); | |
4122 const std::string zeros(4096, '\0'); | |
4123 ASSERT_EQ(1U, fwrite(zeros.data(), zeros.size(), 1, db_file.get())); | |
4124 } | |
4125 | |
4126 // Attempt to save all those entries again. See that it fails (because of the | |
4127 // corruption). | |
4128 ASSERT_FALSE(dbs->SaveChanges(snapshot)); | |
4129 // At this point the handler has been posted but not executed. | |
4130 ASSERT_FALSE(was_called); | |
4131 // Pump the message loop and see that it is executed. | |
4132 base::RunLoop().RunUntilIdle(); | |
4133 ASSERT_TRUE(was_called); | |
4134 } | |
4135 | |
4084 } // namespace syncable | 4136 } // namespace syncable |
4085 } // namespace syncer | 4137 } // namespace syncer |
OLD | NEW |