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