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