OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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 "base/message_loop/message_loop.h" | 5 #include "base/message_loop/message_loop.h" |
6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
| 7 #include "base/strings/string_number_conversions.h" |
7 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
8 #include "base/time/time.h" | 9 #include "base/time/time.h" |
9 #include "chrome/browser/sync/profile_sync_service.h" | 10 #include "chrome/browser/sync/profile_sync_service.h" |
| 11 #include "chrome/browser/sync/test/integration/bookmarks_helper.h" |
| 12 #include "chrome/browser/sync/test/integration/single_client_status_change_check
er.h" |
| 13 #include "chrome/browser/sync/test/integration/sync_integration_test_util.h" |
10 #include "chrome/browser/sync/test/integration/sync_test.h" | 14 #include "chrome/browser/sync/test/integration/sync_test.h" |
11 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "sync/syncable/directory.h" |
| 17 #include "sync/test/directory_backing_store_corruption_testing.h" |
| 18 #include "url/gurl.h" |
12 | 19 |
13 using content::BrowserThread; | 20 using content::BrowserThread; |
| 21 using sync_integration_test_util::AwaitCommitActivityCompletion; |
| 22 using syncer::syncable::corruption_testing::kNumEntriesRequiredForCorruption; |
| 23 using syncer::syncable::corruption_testing::CorruptDatabase; |
14 | 24 |
15 class SingleClientDirectorySyncTest : public SyncTest { | 25 class SingleClientDirectorySyncTest : public SyncTest { |
16 public: | 26 public: |
17 SingleClientDirectorySyncTest() : SyncTest(SINGLE_CLIENT) {} | 27 SingleClientDirectorySyncTest() : SyncTest(SINGLE_CLIENT) {} |
18 ~SingleClientDirectorySyncTest() override {} | 28 ~SingleClientDirectorySyncTest() override {} |
19 | 29 |
20 private: | 30 private: |
21 DISALLOW_COPY_AND_ASSIGN(SingleClientDirectorySyncTest); | 31 DISALLOW_COPY_AND_ASSIGN(SingleClientDirectorySyncTest); |
22 }; | 32 }; |
23 | 33 |
24 void SignalEvent(base::WaitableEvent* e) { | 34 void SignalEvent(base::WaitableEvent* e) { |
25 e->Signal(); | 35 e->Signal(); |
26 } | 36 } |
27 | 37 |
28 bool WaitForExistingTasksOnLoop(base::MessageLoop* loop) { | 38 bool WaitForExistingTasksOnLoop(base::MessageLoop* loop) { |
29 base::WaitableEvent e(true, false); | 39 base::WaitableEvent e(true, false); |
30 loop->PostTask(FROM_HERE, base::Bind(&SignalEvent, &e)); | 40 loop->PostTask(FROM_HERE, base::Bind(&SignalEvent, &e)); |
31 // Timeout stolen from StatusChangeChecker::GetTimeoutDuration(). | 41 // Timeout stolen from StatusChangeChecker::GetTimeoutDuration(). |
32 return e.TimedWait(base::TimeDelta::FromSeconds(45)); | 42 return e.TimedWait(base::TimeDelta::FromSeconds(45)); |
33 } | 43 } |
34 | 44 |
| 45 // A status change checker that waits for an unrecoverable sync error to occur. |
| 46 class SyncUnrecoverableErrorChecker : public SingleClientStatusChangeChecker { |
| 47 public: |
| 48 explicit SyncUnrecoverableErrorChecker(ProfileSyncService* service) |
| 49 : SingleClientStatusChangeChecker(service) {} |
| 50 |
| 51 bool IsExitConditionSatisfied() override { |
| 52 return service()->HasUnrecoverableError(); |
| 53 } |
| 54 |
| 55 std::string GetDebugMessage() const override { |
| 56 return "Sync Unrecoverable Error"; |
| 57 } |
| 58 }; |
| 59 |
35 IN_PROC_BROWSER_TEST_F(SingleClientDirectorySyncTest, | 60 IN_PROC_BROWSER_TEST_F(SingleClientDirectorySyncTest, |
36 StopThenDisableDeletesDirectory) { | 61 StopThenDisableDeletesDirectory) { |
37 ASSERT_TRUE(SetupSync()) << "SetupSync() failed."; | 62 ASSERT_TRUE(SetupSync()) << "SetupSync() failed."; |
38 ProfileSyncService* sync_service = GetSyncService(0); | 63 ProfileSyncService* sync_service = GetSyncService(0); |
39 base::FilePath directory_path = sync_service->GetDirectoryPathForTest(); | 64 base::FilePath directory_path = sync_service->GetDirectoryPathForTest(); |
40 ASSERT_TRUE(base::DirectoryExists(directory_path)); | 65 ASSERT_TRUE(base::DirectoryExists(directory_path)); |
41 sync_service->StopAndSuppress(); | 66 sync_service->StopAndSuppress(); |
42 sync_service->DisableForUser(); | 67 sync_service->DisableForUser(); |
43 | 68 |
44 // Wait for StartupController::StartUp()'s tasks to finish. | 69 // Wait for StartupController::StartUp()'s tasks to finish. |
45 base::RunLoop run_loop; | 70 base::RunLoop run_loop; |
46 base::MessageLoop::current()->PostTask(FROM_HERE, run_loop.QuitClosure()); | 71 base::MessageLoop::current()->PostTask(FROM_HERE, run_loop.QuitClosure()); |
47 run_loop.Run(); | 72 run_loop.Run(); |
48 // Wait for the directory deletion to finish. | 73 // Wait for the directory deletion to finish. |
49 base::MessageLoop* sync_loop = sync_service->GetSyncLoopForTest(); | 74 base::MessageLoop* sync_loop = sync_service->GetSyncLoopForTest(); |
50 ASSERT_TRUE(WaitForExistingTasksOnLoop(sync_loop)); | 75 ASSERT_TRUE(WaitForExistingTasksOnLoop(sync_loop)); |
51 | 76 |
52 ASSERT_FALSE(base::DirectoryExists(directory_path)); | 77 ASSERT_FALSE(base::DirectoryExists(directory_path)); |
53 } | 78 } |
| 79 |
| 80 // Verify that when the sync directory's backing store becomes corrupted, we |
| 81 // trigger an unrecoverable error and delete the database. |
| 82 // |
| 83 // If this test fails, see the definition of kNumEntriesRequiredForCorruption |
| 84 // for one possible cause. |
| 85 IN_PROC_BROWSER_TEST_F(SingleClientDirectorySyncTest, |
| 86 DeleteDirectoryWhenCorrupted) { |
| 87 ASSERT_TRUE(SetupClients()) << "SetupClients() failed."; |
| 88 // Sync and wait for syncing to complete. |
| 89 ASSERT_TRUE(SetupSync()) << "SetupSync() failed."; |
| 90 ASSERT_TRUE(AwaitCommitActivityCompletion(GetSyncService((0)))); |
| 91 ASSERT_TRUE(bookmarks_helper::ModelMatchesVerifier(0)); |
| 92 |
| 93 // Flush the directory to the backing store and wait until the flush |
| 94 // completes. |
| 95 ProfileSyncService* sync_service = GetSyncService(0); |
| 96 sync_service->FlushDirectory(); |
| 97 base::MessageLoop* sync_loop = sync_service->GetSyncLoopForTest(); |
| 98 ASSERT_TRUE(WaitForExistingTasksOnLoop(sync_loop)); |
| 99 |
| 100 // Now corrupt the database. |
| 101 const base::FilePath directory_path(sync_service->GetDirectoryPathForTest()); |
| 102 const base::FilePath sync_db(directory_path.Append( |
| 103 syncer::syncable::Directory::kSyncDatabaseFilename)); |
| 104 ASSERT_TRUE(CorruptDatabase(sync_db)); |
| 105 |
| 106 // Write a bunch of bookmarks and flush the directory to ensure sync notices |
| 107 // the corruption. The key here is to force sync to actually write a lot of |
| 108 // data to its DB so it will see the corruption we introduced above. |
| 109 const GURL url("https://www.google.com"); |
| 110 const bookmarks::BookmarkNode* top = bookmarks_helper::AddFolder( |
| 111 0, bookmarks_helper::GetOtherNode(0), 0, "top"); |
| 112 for (int i = 0; i < kNumEntriesRequiredForCorruption; ++i) { |
| 113 ASSERT_TRUE( |
| 114 bookmarks_helper::AddURL(0, top, 0, base::Int64ToString(i), url)); |
| 115 } |
| 116 sync_service->FlushDirectory(); |
| 117 |
| 118 // Wait for an unrecoverable error to occur. |
| 119 SyncUnrecoverableErrorChecker checker(sync_service); |
| 120 checker.Wait(); |
| 121 ASSERT_TRUE(!checker.TimedOut()); |
| 122 ASSERT_TRUE(sync_service->HasUnrecoverableError()); |
| 123 |
| 124 // Wait until the sync loop has processed any existing tasks and see that the |
| 125 // directory no longer exists. |
| 126 ASSERT_TRUE(WaitForExistingTasksOnLoop(sync_loop)); |
| 127 ASSERT_FALSE(base::DirectoryExists(directory_path)); |
| 128 } |
OLD | NEW |