1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "sync/internal_api/sync_rollback_manager.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/run_loop.h"
9 #include "sync/internal_api/public/read_node.h"
10 #include "sync/internal_api/public/read_transaction.h"
11 #include "sync/internal_api/public/sessions/sync_session_snapshot.h"
12 #include "sync/internal_api/public/test/test_internal_components_factory.h"
13 #include "sync/internal_api/public/write_node.h"
14 #include "sync/internal_api/public/write_transaction.h"
15 #include "sync/internal_api/sync_backup_manager.h"
16 #include "sync/syncable/entry.h"
17 #include "sync/test/engine/fake_model_worker.h"
18 #include "sync/test/test_directory_backing_store.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
24 using ::testing::DoDefault
;
25 using ::testing::Invoke
;
26 using ::testing::Truly
;
27 using ::testing::WithArgs
;
33 class TestChangeDelegate
: public SyncManager::ChangeDelegate
{
35 TestChangeDelegate() {
36 ON_CALL(*this, OnChangesApplied(_
, _
, _
, _
))
38 WithArgs
<3>(Invoke(this,
39 &TestChangeDelegate::VerifyDeletes
)));
42 void add_expected_delete(int64 v
) {
43 expected_deletes_
.insert(v
);
46 MOCK_METHOD4(OnChangesApplied
,
47 void(ModelType model_type
,
49 const BaseTransaction
* trans
,
50 const ImmutableChangeRecordList
& changes
));
51 MOCK_METHOD1(OnChangesComplete
, void(ModelType model_type
));
54 void VerifyDeletes(const ImmutableChangeRecordList
& changes
) {
55 std::set
<int64
> deleted
;
56 for (size_t i
= 0; i
< changes
.Get().size(); ++i
) {
57 const ChangeRecord
& change
= (changes
.Get())[i
];
58 EXPECT_EQ(ChangeRecord::ACTION_DELETE
, change
.action
);
59 EXPECT_TRUE(deleted
.find(change
.id
) == deleted
.end());
60 deleted
.insert(change
.id
);
62 EXPECT_TRUE(expected_deletes_
== deleted
);
65 std::set
<int64
> expected_deletes_
;
68 class SyncRollbackManagerTest
: public testing::Test
,
69 public SyncManager::Observer
{
71 void SetUp() override
{
72 CHECK(temp_dir_
.CreateUniqueTempDir());
74 worker_
= new FakeModelWorker(GROUP_UI
);
77 MOCK_METHOD1(OnSyncCycleCompleted
,
78 void(const sessions::SyncSessionSnapshot
&));
79 MOCK_METHOD1(OnConnectionStatusChange
, void(ConnectionStatus
));
80 MOCK_METHOD4(OnInitializationComplete
,
81 void(const WeakHandle
<JsBackend
>&,
82 const WeakHandle
<DataTypeDebugInfoListener
>&,
84 MOCK_METHOD1(OnActionableError
, void(const SyncProtocolError
&));
85 MOCK_METHOD1(OnMigrationRequested
, void(ModelTypeSet
));;
86 MOCK_METHOD1(OnProtocolEvent
, void(const ProtocolEvent
&));
88 void OnConfigDone(bool success
) {
92 int64
CreateEntry(UserShare
* user_share
, ModelType type
,
93 const std::string
& client_tag
) {
94 WriteTransaction
trans(FROM_HERE
, user_share
);
95 WriteNode
node(&trans
);
96 EXPECT_EQ(WriteNode::INIT_SUCCESS
,
97 node
.InitUniqueByCreation(type
, client_tag
));
98 return node
.GetEntry()->GetMetahandle();
101 void InitManager(SyncManager
* manager
, ModelTypeSet types
,
102 TestChangeDelegate
* delegate
,
103 InternalComponentsFactory::StorageOption storage_option
) {
107 EXPECT_CALL(*this, OnInitializationComplete(_
, _
, _
, _
))
108 .WillOnce(WithArgs
<2>(Invoke(this,
109 &SyncRollbackManagerTest::HandleInit
)));
111 manager
->AddObserver(this);
113 base::RunLoop run_loop
;
114 SyncManager::InitArgs args
;
115 args
.database_location
= temp_dir_
.path();
116 args
.service_url
= GURL("https://example.com/");
117 args
.workers
.push_back(worker_
);
118 args
.change_delegate
= delegate
;
120 InternalComponentsFactory::StorageOption storage_used
;
121 args
.internal_components_factory
.reset(new TestInternalComponentsFactory(
122 InternalComponentsFactory::Switches(), storage_option
, &storage_used
));
123 manager
->Init(&args
);
124 EXPECT_EQ(storage_option
, storage_used
);
125 loop_
.PostTask(FROM_HERE
, run_loop
.QuitClosure());
129 // Create and persist an entry by unique tag in DB.
130 void PrepopulateDb(ModelType type
, const std::string
& client_tag
) {
131 SyncBackupManager backup_manager
;
132 TestChangeDelegate delegate
;
133 InitManager(&backup_manager
, ModelTypeSet(type
), &delegate
,
134 InternalComponentsFactory::STORAGE_ON_DISK_DEFERRED
);
135 CreateEntry(backup_manager
.GetUserShare(), type
, client_tag
);
136 backup_manager
.ShutdownOnSyncThread(SWITCH_MODE_SYNC
);
139 // Verify entry with |client_tag| exists in sync directory.
140 bool VerifyEntry(UserShare
* user_share
, ModelType type
,
141 const std::string
& client_tag
) {
142 ReadTransaction
trans(FROM_HERE
, user_share
);
143 ReadNode
node(&trans
);
144 return BaseNode::INIT_OK
== node
.InitByClientTagLookup(type
, client_tag
);
148 void ConfigureSyncer() {
149 manager_
->ConfigureSyncer(
150 CONFIGURE_REASON_NEW_CLIENT
,
152 ModelTypeSet(), ModelTypeSet(), ModelTypeSet(),
153 ModelSafeRoutingInfo(),
154 base::Bind(&SyncRollbackManagerTest::OnConfigDone
,
155 base::Unretained(this), true),
156 base::Bind(&SyncRollbackManagerTest::OnConfigDone
,
157 base::Unretained(this), false));
160 void HandleInit(bool success
) {
162 loop_
.PostTask(FROM_HERE
,
163 base::Bind(&SyncRollbackManagerTest::ConfigureSyncer
,
164 base::Unretained(this)));
166 manager_
->ShutdownOnSyncThread(STOP_SYNC
);
170 base::ScopedTempDir temp_dir_
;
171 scoped_refptr
<ModelSafeWorker
> worker_
;
172 base::MessageLoop loop_
; // Needed for WeakHandle
173 SyncManager
* manager_
;
177 bool IsRollbackDoneAction(SyncProtocolError e
) {
178 return e
.action
== syncer::ROLLBACK_DONE
;
181 TEST_F(SyncRollbackManagerTest
, RollbackBasic
) {
182 PrepopulateDb(PREFERENCES
, "pref1");
184 TestChangeDelegate delegate
;
185 SyncRollbackManager rollback_manager
;
186 InitManager(&rollback_manager
, ModelTypeSet(PREFERENCES
), &delegate
,
187 InternalComponentsFactory::STORAGE_ON_DISK
);
189 // Simulate a new entry added during type initialization.
191 CreateEntry(rollback_manager
.GetUserShare(), PREFERENCES
, "pref2");
193 delegate
.add_expected_delete(new_pref_id
);
194 EXPECT_CALL(delegate
, OnChangesApplied(_
, _
, _
, _
))
196 .WillOnce(DoDefault());
197 EXPECT_CALL(delegate
, OnChangesComplete(_
)).Times(1);
198 EXPECT_CALL(*this, OnActionableError(Truly(IsRollbackDoneAction
))).Times(1);
200 ModelSafeRoutingInfo routing_info
;
201 routing_info
[PREFERENCES
] = GROUP_UI
;
202 rollback_manager
.StartSyncingNormally(routing_info
, base::Time());
205 TEST_F(SyncRollbackManagerTest
, NoRollbackOfTypesNotBackedUp
) {
206 PrepopulateDb(PREFERENCES
, "pref1");
208 TestChangeDelegate delegate
;
209 SyncRollbackManager rollback_manager
;
210 InitManager(&rollback_manager
, ModelTypeSet(PREFERENCES
, APPS
), &delegate
,
211 InternalComponentsFactory::STORAGE_ON_DISK
);
213 // Simulate new entry added during type initialization.
215 CreateEntry(rollback_manager
.GetUserShare(), PREFERENCES
, "pref2");
216 CreateEntry(rollback_manager
.GetUserShare(), APPS
, "app1");
218 delegate
.add_expected_delete(new_pref_id
);
219 EXPECT_CALL(delegate
, OnChangesApplied(_
, _
, _
, _
))
221 .WillOnce(DoDefault());
222 EXPECT_CALL(delegate
, OnChangesComplete(_
)).Times(1);
224 ModelSafeRoutingInfo routing_info
;
225 routing_info
[PREFERENCES
] = GROUP_UI
;
226 rollback_manager
.StartSyncingNormally(routing_info
, base::Time());
228 // APP entry is still valid.
229 EXPECT_TRUE(VerifyEntry(rollback_manager
.GetUserShare(), APPS
, "app1"));
232 TEST_F(SyncRollbackManagerTest
, BackupDbNotChangedOnAbort
) {
233 PrepopulateDb(PREFERENCES
, "pref1");
235 TestChangeDelegate delegate
;
236 scoped_ptr
<SyncRollbackManager
> rollback_manager(
237 new SyncRollbackManager
);
238 InitManager(rollback_manager
.get(), ModelTypeSet(PREFERENCES
), &delegate
,
239 InternalComponentsFactory::STORAGE_ON_DISK
);
241 // Simulate a new entry added during type initialization.
242 CreateEntry(rollback_manager
->GetUserShare(), PREFERENCES
, "pref2");
244 // Manager was shut down before sync starts.
245 rollback_manager
->ShutdownOnSyncThread(STOP_SYNC
);
247 // Verify new entry was not persisted.
248 rollback_manager
.reset(new SyncRollbackManager
);
249 InitManager(rollback_manager
.get(), ModelTypeSet(PREFERENCES
), &delegate
,
250 InternalComponentsFactory::STORAGE_ON_DISK
);
251 EXPECT_FALSE(VerifyEntry(rollback_manager
->GetUserShare(), PREFERENCES
,
255 TEST_F(SyncRollbackManagerTest
, OnInitializationFailure
) {
256 // Test graceful shutdown on initialization failure.
257 scoped_ptr
<SyncRollbackManager
> rollback_manager(
258 new SyncRollbackManager
);
259 InitManager(rollback_manager
.get(), ModelTypeSet(PREFERENCES
), NULL
,
260 InternalComponentsFactory::STORAGE_ON_DISK
);
263 } // anonymous namespace
265 } // namespace syncer