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" | |
11 #include "base/files/scoped_file.h" | |
12 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
13 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
14 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
15 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
16 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
17 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
18 #include "sql/connection.h" | 16 #include "sql/connection.h" |
19 #include "sql/statement.h" | 17 #include "sql/statement.h" |
20 #include "sql/test/scoped_error_ignorer.h" | 18 #include "sql/test/scoped_error_ignorer.h" |
21 #include "sql/test/test_helpers.h" | 19 #include "sql/test/test_helpers.h" |
22 #include "sync/base/sync_export.h" | 20 #include "sync/base/sync_export.h" |
23 #include "sync/internal_api/public/base/node_ordinal.h" | 21 #include "sync/internal_api/public/base/node_ordinal.h" |
24 #include "sync/protocol/bookmark_specifics.pb.h" | 22 #include "sync/protocol/bookmark_specifics.pb.h" |
25 #include "sync/protocol/sync.pb.h" | 23 #include "sync/protocol/sync.pb.h" |
26 #include "sync/syncable/directory.h" | 24 #include "sync/syncable/directory.h" |
27 #include "sync/syncable/directory_backing_store.h" | 25 #include "sync/syncable/directory_backing_store.h" |
28 #include "sync/syncable/on_disk_directory_backing_store.h" | 26 #include "sync/syncable/on_disk_directory_backing_store.h" |
29 #include "sync/syncable/syncable-inl.h" | 27 #include "sync/syncable/syncable-inl.h" |
| 28 #include "sync/test/directory_backing_store_corruption_testing.h" |
30 #include "sync/test/test_directory_backing_store.h" | 29 #include "sync/test/test_directory_backing_store.h" |
31 #include "sync/util/time.h" | 30 #include "sync/util/time.h" |
32 #include "testing/gtest/include/gtest/gtest-param-test.h" | 31 #include "testing/gtest/include/gtest/gtest-param-test.h" |
33 | 32 |
34 namespace syncer { | 33 namespace syncer { |
35 namespace syncable { | 34 namespace syncable { |
36 namespace { | 35 namespace { |
37 | 36 |
38 // A handler that simply sets |catastrophic_error_handler_was_called| to true. | 37 // A handler that simply sets |catastrophic_error_handler_was_called| to true. |
39 void CatastrophicErrorHandler(bool* catastrophic_error_handler_was_called) { | 38 void CatastrophicErrorHandler(bool* catastrophic_error_handler_was_called) { |
(...skipping 4048 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4088 ASSERT_TRUE(was_called); | 4087 ASSERT_TRUE(was_called); |
4089 } | 4088 } |
4090 | 4089 |
4091 // Verify that database corruption encountered during SaveChanges will trigger | 4090 // Verify that database corruption encountered during SaveChanges will trigger |
4092 // the catastrohpic error handler. | 4091 // the catastrohpic error handler. |
4093 TEST_F(DirectoryBackingStoreTest, | 4092 TEST_F(DirectoryBackingStoreTest, |
4094 CatastrophicErrorHandler_InvocationDuringSaveChanges) { | 4093 CatastrophicErrorHandler_InvocationDuringSaveChanges) { |
4095 bool was_called = false; | 4094 bool was_called = false; |
4096 const base::Closure handler = | 4095 const base::Closure handler = |
4097 base::Bind(&CatastrophicErrorHandler, &was_called); | 4096 base::Bind(&CatastrophicErrorHandler, &was_called); |
4098 | |
4099 // Create a DB with many entries. | 4097 // Create a DB with many entries. |
4100 scoped_ptr<OnDiskDirectoryBackingStoreForTest> dbs( | 4098 scoped_ptr<OnDiskDirectoryBackingStoreForTest> dbs( |
4101 new OnDiskDirectoryBackingStoreForTest(GetUsername(), GetDatabasePath())); | 4099 new OnDiskDirectoryBackingStoreForTest(GetUsername(), GetDatabasePath())); |
4102 dbs->SetCatastrophicErrorHandler(handler); | 4100 dbs->SetCatastrophicErrorHandler(handler); |
4103 ASSERT_TRUE(dbs->db_->has_error_callback()); | 4101 ASSERT_TRUE(dbs->db_->has_error_callback()); |
4104 ASSERT_TRUE(LoadAndIgnoreReturnedData(dbs.get())); | 4102 ASSERT_TRUE(LoadAndIgnoreReturnedData(dbs.get())); |
4105 ASSERT_FALSE(dbs->DidFailFirstOpenAttempt()); | 4103 ASSERT_FALSE(dbs->DidFailFirstOpenAttempt()); |
4106 Directory::SaveChangesSnapshot snapshot; | 4104 Directory::SaveChangesSnapshot snapshot; |
4107 const int num_entries = 4000; | 4105 for (int i = 0; i < corruption_testing::kNumEntriesRequiredForCorruption; |
4108 for (int i = 0; i < num_entries; ++i) { | 4106 ++i) { |
4109 snapshot.dirty_metas.insert(CreateEntry(i).release()); | 4107 snapshot.dirty_metas.insert(CreateEntry(i).release()); |
4110 } | 4108 } |
4111 ASSERT_TRUE(dbs->SaveChanges(snapshot)); | 4109 ASSERT_TRUE(dbs->SaveChanges(snapshot)); |
4112 | 4110 // Corrupt it. |
4113 // Corrupt the DB by write a bunch of zeros at the beginning. | 4111 ASSERT_TRUE(corruption_testing::CorruptDatabase(GetDatabasePath())); |
4114 { | |
4115 // Because the file is already open for writing (see dbs above), it's | |
4116 // important that we open it in a sharing compatible way for platforms that | |
4117 // have the concept of shared/exclusive file access (e.g. Windows). | |
4118 base::ScopedFILE db_file(base::OpenFile(GetDatabasePath(), "wb")); | |
4119 ASSERT_TRUE(db_file.get()); | |
4120 const std::string zeros(4096, '\0'); | |
4121 ASSERT_EQ(1U, fwrite(zeros.data(), zeros.size(), 1, db_file.get())); | |
4122 } | |
4123 | |
4124 // Attempt to save all those entries again. See that it fails (because of the | 4112 // Attempt to save all those entries again. See that it fails (because of the |
4125 // corruption). | 4113 // corruption). |
4126 // | 4114 // |
4127 // If this test fails because SaveChanges returned true, it may mean that you | 4115 // If this test fails because SaveChanges returned true, it may mean you need |
4128 // need to increase the number of entries written to the DB. Try increasing | 4116 // to increase the number of entries written to the DB. See also |
4129 // the value of num_entries above. The value needs to be large enough to force | 4117 // |kNumEntriesRequiredForCorruption|. |
4130 // the underlying DB to be read from disk before writing. The value *may* | |
4131 // depend on the underlying DB page size as well as the DB's cache_size | |
4132 // PRAGMA. | |
4133 ASSERT_FALSE(dbs->SaveChanges(snapshot)); | 4118 ASSERT_FALSE(dbs->SaveChanges(snapshot)); |
4134 // At this point the handler has been posted but not executed. | 4119 // At this point the handler has been posted but not executed. |
4135 ASSERT_FALSE(was_called); | 4120 ASSERT_FALSE(was_called); |
4136 // Pump the message loop and see that it is executed. | 4121 // Pump the message loop and see that it is executed. |
4137 base::RunLoop().RunUntilIdle(); | 4122 base::RunLoop().RunUntilIdle(); |
4138 ASSERT_TRUE(was_called); | 4123 ASSERT_TRUE(was_called); |
4139 } | 4124 } |
4140 | 4125 |
4141 } // namespace syncable | 4126 } // namespace syncable |
4142 } // namespace syncer | 4127 } // namespace syncer |
OLD | NEW |