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 | |
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 to ensure the sync DB notices the corruption. | |
pval...(no longer on Chromium)
2015/04/27 19:09:34
Re: "sync DB notices the corruption"
Does this ha
maniscalco
2015/04/27 19:16:41
Good point. Flushing the directory is an importan
| |
117 for (int i = 0; i < kNumEntriesRequiredForCorruption; ++i) { | |
118 ASSERT_TRUE( | |
119 bookmarks_helper::AddURL(0, top, 0, base::Int64ToString(i), url)); | |
120 } | |
121 sync_service->FlushDirectory(); | |
122 | |
123 // Wait for an unrecoverable error to occur. | |
124 SyncUnrecoverableErrorChecker checker(sync_service); | |
125 checker.Wait(); | |
126 ASSERT_TRUE(!checker.TimedOut()); | |
127 ASSERT_TRUE(sync_service->HasUnrecoverableError()); | |
128 | |
129 // Wait until the sync loop has processed any existing tasks and see that the | |
130 // directory no longer exists. | |
131 ASSERT_TRUE(WaitForExistingTasksOnLoop(sync_loop)); | |
132 ASSERT_FALSE(base::DirectoryExists(directory_path)); | |
133 } | |
OLD | NEW |