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/engine/directory_update_handler.h"
7 #include "base/compiler_specific.h"
8 #include "base/containers/scoped_ptr_map.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "sync/engine/syncer_proto_util.h"
12 #include "sync/internal_api/public/base/attachment_id_proto.h"
13 #include "sync/internal_api/public/base/model_type.h"
14 #include "sync/internal_api/public/test/test_entry_factory.h"
15 #include "sync/protocol/sync.pb.h"
16 #include "sync/sessions/directory_type_debug_info_emitter.h"
17 #include "sync/sessions/status_controller.h"
18 #include "sync/syncable/directory.h"
19 #include "sync/syncable/entry.h"
20 #include "sync/syncable/mutable_entry.h"
21 #include "sync/syncable/syncable_model_neutral_write_transaction.h"
22 #include "sync/syncable/syncable_proto_util.h"
23 #include "sync/syncable/syncable_read_transaction.h"
24 #include "sync/syncable/syncable_write_transaction.h"
25 #include "sync/test/engine/fake_model_worker.h"
26 #include "sync/test/engine/test_directory_setter_upper.h"
27 #include "sync/test/engine/test_id_factory.h"
28 #include "sync/test/engine/test_syncable_utils.h"
29 #include "testing/gtest/include/gtest/gtest.h"
34 using syncable::UNITTEST
;
36 static const int64 kDefaultVersion
= 1000;
38 // A test harness for tests that focus on processing updates.
40 // Update processing is what occurs when we first download updates. It converts
41 // the received protobuf message into information in the syncable::Directory.
42 // Any invalid or redundant updates will be dropped at this point.
43 class DirectoryUpdateHandlerProcessUpdateTest
: public ::testing::Test
{
45 DirectoryUpdateHandlerProcessUpdateTest()
46 : ui_worker_(new FakeModelWorker(GROUP_UI
)) {
49 ~DirectoryUpdateHandlerProcessUpdateTest() override
{}
51 void SetUp() override
{ dir_maker_
.SetUp(); }
53 void TearDown() override
{ dir_maker_
.TearDown(); }
55 syncable::Directory
* dir() {
56 return dir_maker_
.directory();
60 scoped_ptr
<sync_pb::SyncEntity
> CreateUpdate(
61 const std::string
& id
,
62 const std::string
& parent
,
63 const ModelType
& type
);
65 // This exists mostly to give tests access to the protected member function.
66 // Warning: This takes the syncable directory lock.
67 void UpdateSyncEntities(
68 DirectoryUpdateHandler
* handler
,
69 const SyncEntityList
& applicable_updates
,
70 sessions::StatusController
* status
);
72 // Another function to access private member functions.
73 void UpdateProgressMarkers(
74 DirectoryUpdateHandler
* handler
,
75 const sync_pb::DataTypeProgressMarker
& progress
);
77 scoped_refptr
<FakeModelWorker
> ui_worker() {
81 bool EntryExists(const std::string
& id
) {
82 syncable::ReadTransaction
trans(FROM_HERE
, dir());
83 syncable::Entry
e(&trans
, syncable::GET_BY_ID
,
84 Id::CreateFromServerId(id
));
85 return e
.good() && !e
.GetIsDel();
88 bool TypeRootExists(ModelType model_type
) {
89 syncable::ReadTransaction
trans(FROM_HERE
, dir());
90 syncable::Entry
e(&trans
, syncable::GET_TYPE_ROOT
, model_type
);
91 return e
.good() && !e
.GetIsDel();
95 // Used in the construction of DirectoryTypeDebugInfoEmitters.
96 base::ObserverList
<TypeDebugInfoObserver
> type_observers_
;
99 base::MessageLoop loop_
; // Needed to initialize the directory.
100 TestDirectorySetterUpper dir_maker_
;
101 scoped_refptr
<FakeModelWorker
> ui_worker_
;
104 scoped_ptr
<sync_pb::SyncEntity
>
105 DirectoryUpdateHandlerProcessUpdateTest::CreateUpdate(
106 const std::string
& id
,
107 const std::string
& parent
,
108 const ModelType
& type
) {
109 scoped_ptr
<sync_pb::SyncEntity
> e(new sync_pb::SyncEntity());
110 e
->set_id_string(id
);
111 e
->set_parent_id_string(parent
);
112 e
->set_non_unique_name(id
);
114 e
->set_version(kDefaultVersion
);
115 AddDefaultFieldValue(type
, e
->mutable_specifics());
119 void DirectoryUpdateHandlerProcessUpdateTest::UpdateSyncEntities(
120 DirectoryUpdateHandler
* handler
,
121 const SyncEntityList
& applicable_updates
,
122 sessions::StatusController
* status
) {
123 syncable::ModelNeutralWriteTransaction
trans(FROM_HERE
, UNITTEST
, dir());
124 handler
->UpdateSyncEntities(&trans
, applicable_updates
, status
);
127 void DirectoryUpdateHandlerProcessUpdateTest::UpdateProgressMarkers(
128 DirectoryUpdateHandler
* handler
,
129 const sync_pb::DataTypeProgressMarker
& progress
) {
130 handler
->UpdateProgressMarker(progress
);
133 static const char kCacheGuid
[] = "IrcjZ2jyzHDV9Io4+zKcXQ==";
135 // Test that the bookmark tag is set on newly downloaded items.
136 TEST_F(DirectoryUpdateHandlerProcessUpdateTest
, NewBookmarkTag
) {
137 DirectoryTypeDebugInfoEmitter
emitter(BOOKMARKS
, &type_observers_
);
138 DirectoryUpdateHandler
handler(dir(), BOOKMARKS
, ui_worker(), &emitter
);
139 sync_pb::GetUpdatesResponse gu_response
;
140 sessions::StatusController status
;
142 // Add a bookmark item to the update message.
143 std::string root
= Id::GetRoot().GetServerId();
144 Id server_id
= Id::CreateFromServerId("b1");
145 scoped_ptr
<sync_pb::SyncEntity
> e
=
146 CreateUpdate(SyncableIdToProto(server_id
), root
, BOOKMARKS
);
147 e
->set_originator_cache_guid(
148 std::string(kCacheGuid
, arraysize(kCacheGuid
)-1));
149 Id client_id
= Id::CreateFromClientString("-2");
150 e
->set_originator_client_item_id(client_id
.GetServerId());
151 e
->set_position_in_parent(0);
153 // Add it to the applicable updates list.
154 SyncEntityList bookmark_updates
;
155 bookmark_updates
.push_back(e
.get());
157 // Process the update.
158 UpdateSyncEntities(&handler
, bookmark_updates
, &status
);
160 syncable::ReadTransaction
trans(FROM_HERE
, dir());
161 syncable::Entry
entry(&trans
, syncable::GET_BY_ID
, server_id
);
162 ASSERT_TRUE(entry
.good());
163 EXPECT_TRUE(UniquePosition::IsValidSuffix(entry
.GetUniqueBookmarkTag()));
164 EXPECT_TRUE(entry
.GetServerUniquePosition().IsValid());
166 // If this assertion fails, that might indicate that the algorithm used to
167 // generate bookmark tags has been modified. This could have implications for
168 // bookmark ordering. Please make sure you know what you're doing if you
169 // intend to make such a change.
170 EXPECT_EQ("6wHRAb3kbnXV5GHrejp4/c1y5tw=", entry
.GetUniqueBookmarkTag());
173 // Test the receipt of a type root node.
174 TEST_F(DirectoryUpdateHandlerProcessUpdateTest
,
175 ReceiveServerCreatedBookmarkFolders
) {
176 DirectoryTypeDebugInfoEmitter
emitter(BOOKMARKS
, &type_observers_
);
177 DirectoryUpdateHandler
handler(dir(), BOOKMARKS
, ui_worker(), &emitter
);
178 sync_pb::GetUpdatesResponse gu_response
;
179 sessions::StatusController status
;
181 // Create an update that mimics the bookmark root.
182 Id server_id
= Id::CreateFromServerId("xyz");
183 std::string root
= Id::GetRoot().GetServerId();
184 scoped_ptr
<sync_pb::SyncEntity
> e
=
185 CreateUpdate(SyncableIdToProto(server_id
), root
, BOOKMARKS
);
186 e
->set_server_defined_unique_tag("google_chrome_bookmarks");
189 // Add it to the applicable updates list.
190 SyncEntityList bookmark_updates
;
191 bookmark_updates
.push_back(e
.get());
193 EXPECT_FALSE(SyncerProtoUtil::ShouldMaintainPosition(*e
));
196 UpdateSyncEntities(&handler
, bookmark_updates
, &status
);
198 // Verify the results.
199 syncable::ReadTransaction
trans(FROM_HERE
, dir());
200 syncable::Entry
entry(&trans
, syncable::GET_BY_ID
, server_id
);
201 ASSERT_TRUE(entry
.good());
203 EXPECT_FALSE(entry
.ShouldMaintainPosition());
204 EXPECT_FALSE(entry
.GetUniquePosition().IsValid());
205 EXPECT_FALSE(entry
.GetServerUniquePosition().IsValid());
206 EXPECT_TRUE(entry
.GetUniqueBookmarkTag().empty());
209 // Test the receipt of a non-bookmark item.
210 TEST_F(DirectoryUpdateHandlerProcessUpdateTest
, ReceiveNonBookmarkItem
) {
211 DirectoryTypeDebugInfoEmitter
emitter(AUTOFILL
, &type_observers_
);
212 DirectoryUpdateHandler
handler(dir(), AUTOFILL
, ui_worker(), &emitter
);
213 sync_pb::GetUpdatesResponse gu_response
;
214 sessions::StatusController status
;
216 std::string root
= Id::GetRoot().GetServerId();
217 Id server_id
= Id::CreateFromServerId("xyz");
218 scoped_ptr
<sync_pb::SyncEntity
> e
=
219 CreateUpdate(SyncableIdToProto(server_id
), root
, AUTOFILL
);
220 e
->set_server_defined_unique_tag("9PGRuKdX5sHyGMB17CvYTXuC43I=");
222 // Add it to the applicable updates list.
223 SyncEntityList autofill_updates
;
224 autofill_updates
.push_back(e
.get());
226 EXPECT_FALSE(SyncerProtoUtil::ShouldMaintainPosition(*e
));
229 UpdateSyncEntities(&handler
, autofill_updates
, &status
);
231 syncable::ReadTransaction
trans(FROM_HERE
, dir());
232 syncable::Entry
entry(&trans
, syncable::GET_BY_ID
, server_id
);
233 ASSERT_TRUE(entry
.good());
235 EXPECT_FALSE(entry
.ShouldMaintainPosition());
236 EXPECT_FALSE(entry
.GetUniquePosition().IsValid());
237 EXPECT_FALSE(entry
.GetServerUniquePosition().IsValid());
238 EXPECT_TRUE(entry
.GetUniqueBookmarkTag().empty());
241 // Tests the setting of progress markers.
242 TEST_F(DirectoryUpdateHandlerProcessUpdateTest
, ProcessNewProgressMarkers
) {
243 DirectoryTypeDebugInfoEmitter
emitter(BOOKMARKS
, &type_observers_
);
244 DirectoryUpdateHandler
handler(dir(), BOOKMARKS
, ui_worker(), &emitter
);
246 sync_pb::DataTypeProgressMarker progress
;
247 progress
.set_data_type_id(GetSpecificsFieldNumberFromModelType(BOOKMARKS
));
248 progress
.set_token("token");
250 UpdateProgressMarkers(&handler
, progress
);
252 sync_pb::DataTypeProgressMarker saved
;
253 dir()->GetDownloadProgress(BOOKMARKS
, &saved
);
255 EXPECT_EQ(progress
.token(), saved
.token());
256 EXPECT_EQ(progress
.data_type_id(), saved
.data_type_id());
259 TEST_F(DirectoryUpdateHandlerProcessUpdateTest
, GarbageCollectionByVersion
) {
260 DirectoryTypeDebugInfoEmitter
emitter(SYNCED_NOTIFICATIONS
, &type_observers_
);
261 DirectoryUpdateHandler
handler(dir(), SYNCED_NOTIFICATIONS
,
262 ui_worker(), &emitter
);
263 sessions::StatusController status
;
265 sync_pb::DataTypeProgressMarker progress
;
266 progress
.set_data_type_id(
267 GetSpecificsFieldNumberFromModelType(SYNCED_NOTIFICATIONS
));
268 progress
.set_token("token");
269 progress
.mutable_gc_directive()->set_version_watermark(kDefaultVersion
+ 10);
271 sync_pb::DataTypeContext context
;
272 context
.set_data_type_id(
273 GetSpecificsFieldNumberFromModelType(SYNCED_NOTIFICATIONS
));
274 context
.set_context("context");
275 context
.set_version(1);
277 scoped_ptr
<sync_pb::SyncEntity
> e1
=
278 CreateUpdate(SyncableIdToProto(Id::CreateFromServerId("e1")), "",
279 SYNCED_NOTIFICATIONS
);
281 scoped_ptr
<sync_pb::SyncEntity
> e2
=
282 CreateUpdate(SyncableIdToProto(Id::CreateFromServerId("e2")), "",
283 SYNCED_NOTIFICATIONS
);
284 e2
->set_version(kDefaultVersion
+ 100);
286 // Add to the applicable updates list.
287 SyncEntityList updates
;
288 updates
.push_back(e1
.get());
289 updates
.push_back(e2
.get());
291 // Process and apply updates.
294 handler
.ProcessGetUpdatesResponse(progress
, context
, updates
, &status
));
295 handler
.ApplyUpdates(&status
);
297 // Verify none is deleted because they are unapplied during GC.
298 EXPECT_TRUE(TypeRootExists(SYNCED_NOTIFICATIONS
));
299 EXPECT_TRUE(EntryExists(e1
->id_string()));
300 EXPECT_TRUE(EntryExists(e2
->id_string()));
302 // Process and apply again. Old entry is deleted but not root.
303 progress
.mutable_gc_directive()->set_version_watermark(kDefaultVersion
+ 20);
305 handler
.ProcessGetUpdatesResponse(
306 progress
, context
, SyncEntityList(), &status
));
307 handler
.ApplyUpdates(&status
);
308 EXPECT_FALSE(EntryExists(e1
->id_string()));
309 EXPECT_TRUE(EntryExists(e2
->id_string()));
312 TEST_F(DirectoryUpdateHandlerProcessUpdateTest
, ContextVersion
) {
313 DirectoryTypeDebugInfoEmitter
emitter(SYNCED_NOTIFICATIONS
, &type_observers_
);
314 DirectoryUpdateHandler
handler(dir(), SYNCED_NOTIFICATIONS
,
315 ui_worker(), &emitter
);
316 sessions::StatusController status
;
317 int field_number
= GetSpecificsFieldNumberFromModelType(SYNCED_NOTIFICATIONS
);
319 sync_pb::DataTypeProgressMarker progress
;
320 progress
.set_data_type_id(
321 GetSpecificsFieldNumberFromModelType(SYNCED_NOTIFICATIONS
));
322 progress
.set_token("token");
324 sync_pb::DataTypeContext old_context
;
325 old_context
.set_version(1);
326 old_context
.set_context("data");
327 old_context
.set_data_type_id(field_number
);
329 scoped_ptr
<sync_pb::SyncEntity
> e1
=
330 CreateUpdate(SyncableIdToProto(Id::CreateFromServerId("e1")), "",
331 SYNCED_NOTIFICATIONS
);
333 SyncEntityList updates
;
334 updates
.push_back(e1
.get());
336 // The first response should be processed fine.
338 handler
.ProcessGetUpdatesResponse(
339 progress
, old_context
, updates
, &status
));
340 handler
.ApplyUpdates(&status
);
342 // The PREFERENCES root should be auto-created.
343 EXPECT_TRUE(TypeRootExists(SYNCED_NOTIFICATIONS
));
345 EXPECT_TRUE(EntryExists(e1
->id_string()));
348 sync_pb::DataTypeContext dir_context
;
349 syncable::ReadTransaction
trans(FROM_HERE
, dir());
350 trans
.directory()->GetDataTypeContext(
351 &trans
, SYNCED_NOTIFICATIONS
, &dir_context
);
352 EXPECT_EQ(old_context
.SerializeAsString(), dir_context
.SerializeAsString());
355 sync_pb::DataTypeContext new_context
;
356 new_context
.set_version(0);
357 new_context
.set_context("old");
358 new_context
.set_data_type_id(field_number
);
360 scoped_ptr
<sync_pb::SyncEntity
> e2
=
361 CreateUpdate(SyncableIdToProto(Id::CreateFromServerId("e2")), "",
362 SYNCED_NOTIFICATIONS
);
364 updates
.push_back(e2
.get());
366 // The second response, with an old context version, should result in an
367 // error and the updates should be dropped.
368 EXPECT_EQ(DATATYPE_TRIGGERED_RETRY
,
369 handler
.ProcessGetUpdatesResponse(
370 progress
, new_context
, updates
, &status
));
371 handler
.ApplyUpdates(&status
);
373 EXPECT_FALSE(EntryExists(e2
->id_string()));
376 sync_pb::DataTypeContext dir_context
;
377 syncable::ReadTransaction
trans(FROM_HERE
, dir());
378 trans
.directory()->GetDataTypeContext(
379 &trans
, SYNCED_NOTIFICATIONS
, &dir_context
);
380 EXPECT_EQ(old_context
.SerializeAsString(), dir_context
.SerializeAsString());
384 // See that updates containing attachment metadata are applied
385 // (i.e. server_attachment_metadata is copied to attachment_metadata).
386 TEST_F(DirectoryUpdateHandlerProcessUpdateTest
,
387 ProcessAndApplyUpdatesWithAttachments
) {
388 DirectoryTypeDebugInfoEmitter
emitter(ARTICLES
, &type_observers_
);
389 DirectoryUpdateHandler
handler(dir(), ARTICLES
, ui_worker(), &emitter
);
390 sessions::StatusController status
;
392 sync_pb::DataTypeProgressMarker progress
;
393 progress
.set_data_type_id(GetSpecificsFieldNumberFromModelType(ARTICLES
));
394 progress
.set_token("token");
395 progress
.mutable_gc_directive()->set_version_watermark(kDefaultVersion
+ 10);
397 sync_pb::DataTypeContext context
;
398 context
.set_data_type_id(GetSpecificsFieldNumberFromModelType(ARTICLES
));
399 context
.set_context("context");
400 context
.set_version(1);
402 scoped_ptr
<sync_pb::SyncEntity
> e1
= CreateUpdate(
403 SyncableIdToProto(Id::CreateFromServerId("e1")), "", ARTICLES
);
404 sync_pb::AttachmentIdProto
* attachment_id
= e1
->add_attachment_id();
405 *attachment_id
= CreateAttachmentIdProto(0, 0);
407 SyncEntityList updates
;
408 updates
.push_back(e1
.get());
410 // Process and apply updates.
413 handler
.ProcessGetUpdatesResponse(progress
, context
, updates
, &status
));
414 handler
.ApplyUpdates(&status
);
416 ASSERT_TRUE(TypeRootExists(ARTICLES
));
417 ASSERT_TRUE(EntryExists(e1
->id_string()));
419 syncable::ReadTransaction
trans(FROM_HERE
, dir());
420 syncable::Entry
e(&trans
,
422 Id::CreateFromServerId(e1
->id_string()));
424 // See that the attachment_metadata is correct.
425 sync_pb::AttachmentMetadata attachment_metadata
= e
.GetAttachmentMetadata();
426 ASSERT_EQ(1, attachment_metadata
.record_size());
427 ASSERT_EQ(attachment_id
->SerializeAsString(),
428 attachment_metadata
.record(0).id().SerializeAsString());
429 ASSERT_TRUE(attachment_metadata
.record(0).is_on_server());
431 // See that attachment_metadata and server_attachment_metadata are equal.
432 ASSERT_EQ(attachment_metadata
.SerializeAsString(),
433 e
.GetServerAttachmentMetadata().SerializeAsString());
437 // A test harness for tests that focus on applying updates.
439 // Update application is performed when we want to take updates that were
440 // previously downloaded, processed, and stored in our syncable::Directory
441 // and use them to update our local state (both the Directory's local state
442 // and the model's local state, though these tests focus only on the Directory's
445 // This is kept separate from the update processing test in part for historical
446 // reasons, and in part because these tests may require a bit more infrastrcture
447 // in the future. Update application should happen on a different thread a lot
448 // of the time so these tests may end up requiring more infrastructure than the
449 // update processing tests. Currently, we're bypassing most of those issues by
450 // using FakeModelWorkers, so there's not much difference between the two test
452 class DirectoryUpdateHandlerApplyUpdateTest
: public ::testing::Test
{
454 DirectoryUpdateHandlerApplyUpdateTest()
455 : ui_worker_(new FakeModelWorker(GROUP_UI
)),
456 password_worker_(new FakeModelWorker(GROUP_PASSWORD
)),
457 passive_worker_(new FakeModelWorker(GROUP_PASSIVE
)),
458 bookmarks_emitter_(BOOKMARKS
, &type_observers_
),
459 passwords_emitter_(PASSWORDS
, &type_observers_
),
460 articles_emitter_(ARTICLES
, &type_observers_
) {}
462 void SetUp() override
{
464 entry_factory_
.reset(new TestEntryFactory(directory()));
466 update_handler_map_
.insert(
468 make_scoped_ptr(new DirectoryUpdateHandler(
469 directory(), BOOKMARKS
, ui_worker_
, &bookmarks_emitter_
)));
470 update_handler_map_
.insert(
472 make_scoped_ptr(new DirectoryUpdateHandler(
473 directory(), PASSWORDS
, password_worker_
, &passwords_emitter_
)));
474 update_handler_map_
.insert(
475 ARTICLES
, make_scoped_ptr(new DirectoryUpdateHandler(
476 directory(), ARTICLES
, ui_worker_
, &articles_emitter_
)));
479 void TearDown() override
{ dir_maker_
.TearDown(); }
481 const UpdateCounters
& GetBookmarksUpdateCounters() {
482 return bookmarks_emitter_
.GetUpdateCounters();
485 const UpdateCounters
& GetPasswordsUpdateCounters() {
486 return passwords_emitter_
.GetUpdateCounters();
489 const UpdateCounters
& GetArticlesUpdateCounters() {
490 return articles_emitter_
.GetUpdateCounters();
494 void ApplyBookmarkUpdates(sessions::StatusController
* status
) {
495 update_handler_map_
.find(BOOKMARKS
)->second
->ApplyUpdates(status
);
498 void ApplyPasswordUpdates(sessions::StatusController
* status
) {
499 update_handler_map_
.find(PASSWORDS
)->second
->ApplyUpdates(status
);
502 void ApplyArticlesUpdates(sessions::StatusController
* status
) {
503 update_handler_map_
.find(ARTICLES
)->second
->ApplyUpdates(status
);
506 TestEntryFactory
* entry_factory() {
507 return entry_factory_
.get();
510 syncable::Directory
* directory() {
511 return dir_maker_
.directory();
515 base::MessageLoop loop_
; // Needed to initialize the directory.
516 TestDirectorySetterUpper dir_maker_
;
517 scoped_ptr
<TestEntryFactory
> entry_factory_
;
519 scoped_refptr
<FakeModelWorker
> ui_worker_
;
520 scoped_refptr
<FakeModelWorker
> password_worker_
;
521 scoped_refptr
<FakeModelWorker
> passive_worker_
;
523 base::ObserverList
<TypeDebugInfoObserver
> type_observers_
;
524 DirectoryTypeDebugInfoEmitter bookmarks_emitter_
;
525 DirectoryTypeDebugInfoEmitter passwords_emitter_
;
526 DirectoryTypeDebugInfoEmitter articles_emitter_
;
528 base::ScopedPtrMap
<ModelType
, scoped_ptr
<UpdateHandler
>> update_handler_map_
;
532 sync_pb::EntitySpecifics
DefaultBookmarkSpecifics() {
533 sync_pb::EntitySpecifics result
;
534 AddDefaultFieldValue(BOOKMARKS
, &result
);
539 // Test update application for a few bookmark items.
540 TEST_F(DirectoryUpdateHandlerApplyUpdateTest
, SimpleBookmark
) {
541 sessions::StatusController status
;
543 std::string root_server_id
= Id::GetRoot().GetServerId();
544 int64 parent_handle
=
545 entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
546 "parent", DefaultBookmarkSpecifics(), root_server_id
);
548 entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
549 "child", DefaultBookmarkSpecifics(), "parent");
551 ApplyBookmarkUpdates(&status
);
553 const UpdateCounters
& counter
= GetBookmarksUpdateCounters();
554 EXPECT_EQ(0, counter
.num_encryption_conflict_application_failures
)
555 << "Simple update shouldn't result in conflicts";
556 EXPECT_EQ(0, counter
.num_hierarchy_conflict_application_failures
)
557 << "Simple update shouldn't result in conflicts";
558 EXPECT_EQ(2, counter
.num_updates_applied
)
559 << "All items should have been successfully applied";
562 syncable::ReadTransaction
trans(FROM_HERE
, directory());
564 syncable::Entry
parent(&trans
, syncable::GET_BY_HANDLE
, parent_handle
);
565 syncable::Entry
child(&trans
, syncable::GET_BY_HANDLE
, child_handle
);
567 ASSERT_TRUE(parent
.good());
568 ASSERT_TRUE(child
.good());
570 EXPECT_FALSE(parent
.GetIsUnsynced());
571 EXPECT_FALSE(parent
.GetIsUnappliedUpdate());
572 EXPECT_FALSE(child
.GetIsUnsynced());
573 EXPECT_FALSE(child
.GetIsUnappliedUpdate());
577 // Test that the applicator can handle updates delivered out of order.
578 TEST_F(DirectoryUpdateHandlerApplyUpdateTest
,
579 BookmarkChildrenBeforeParent
) {
580 // Start with some bookmarks whose parents are unknown.
581 std::string root_server_id
= Id::GetRoot().GetServerId();
582 int64 a_handle
= entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
583 "a_child_created_first", DefaultBookmarkSpecifics(), "parent");
584 int64 x_handle
= entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
585 "x_child_created_first", DefaultBookmarkSpecifics(), "parent");
587 // Update application will fail.
588 sessions::StatusController status1
;
589 ApplyBookmarkUpdates(&status1
);
590 EXPECT_EQ(0, status1
.num_updates_applied());
591 EXPECT_EQ(2, status1
.num_hierarchy_conflicts());
594 syncable::ReadTransaction
trans(FROM_HERE
, directory());
596 syncable::Entry
a(&trans
, syncable::GET_BY_HANDLE
, a_handle
);
597 syncable::Entry
x(&trans
, syncable::GET_BY_HANDLE
, x_handle
);
599 ASSERT_TRUE(a
.good());
600 ASSERT_TRUE(x
.good());
602 EXPECT_TRUE(a
.GetIsUnappliedUpdate());
603 EXPECT_TRUE(x
.GetIsUnappliedUpdate());
606 // Now add their parent and a few siblings.
607 entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
608 "parent", DefaultBookmarkSpecifics(), root_server_id
);
609 entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
610 "a_child_created_second", DefaultBookmarkSpecifics(), "parent");
611 entry_factory()->CreateUnappliedNewBookmarkItemWithParent(
612 "x_child_created_second", DefaultBookmarkSpecifics(), "parent");
614 // Update application will succeed.
615 sessions::StatusController status2
;
616 ApplyBookmarkUpdates(&status2
);
617 EXPECT_EQ(5, status2
.num_updates_applied())
618 << "All updates should have been successfully applied";
621 syncable::ReadTransaction
trans(FROM_HERE
, directory());
623 syncable::Entry
a(&trans
, syncable::GET_BY_HANDLE
, a_handle
);
624 syncable::Entry
x(&trans
, syncable::GET_BY_HANDLE
, x_handle
);
626 ASSERT_TRUE(a
.good());
627 ASSERT_TRUE(x
.good());
629 EXPECT_FALSE(a
.GetIsUnappliedUpdate());
630 EXPECT_FALSE(x
.GetIsUnappliedUpdate());
634 // Try to apply changes on an item that is both IS_UNSYNCED and
635 // IS_UNAPPLIED_UPDATE. Conflict resolution should be performed.
636 TEST_F(DirectoryUpdateHandlerApplyUpdateTest
, SimpleBookmarkConflict
) {
637 int64 handle
= entry_factory()->CreateUnappliedAndUnsyncedBookmarkItem("x");
639 int original_server_version
= -10;
641 syncable::ReadTransaction
trans(FROM_HERE
, directory());
642 syncable::Entry
e(&trans
, syncable::GET_BY_HANDLE
, handle
);
643 original_server_version
= e
.GetServerVersion();
644 ASSERT_NE(original_server_version
, e
.GetBaseVersion());
645 EXPECT_TRUE(e
.GetIsUnsynced());
648 sessions::StatusController status
;
649 ApplyBookmarkUpdates(&status
);
651 const UpdateCounters
& counters
= GetBookmarksUpdateCounters();
652 EXPECT_EQ(1, counters
.num_server_overwrites
)
653 << "Unsynced and unapplied item conflict should be resolved";
654 EXPECT_EQ(0, counters
.num_updates_applied
)
655 << "Update should not be applied; we should override the server.";
658 syncable::ReadTransaction
trans(FROM_HERE
, directory());
659 syncable::Entry
e(&trans
, syncable::GET_BY_HANDLE
, handle
);
660 ASSERT_TRUE(e
.good());
661 EXPECT_EQ(original_server_version
, e
.GetServerVersion());
662 EXPECT_EQ(original_server_version
, e
.GetBaseVersion());
663 EXPECT_FALSE(e
.GetIsUnappliedUpdate());
665 // The unsynced flag will remain set until we successfully commit the item.
666 EXPECT_TRUE(e
.GetIsUnsynced());
670 // Create a simple conflict that is also a hierarchy conflict. If we were to
671 // follow the normal "server wins" logic, we'd end up violating hierarchy
672 // constraints. The hierarchy conflict must take precedence. We can not allow
673 // the update to be applied. The item must remain in the conflict state.
674 TEST_F(DirectoryUpdateHandlerApplyUpdateTest
, HierarchyAndSimpleConflict
) {
675 // Create a simply-conflicting item. It will start with valid parent ids.
676 int64 handle
= entry_factory()->CreateUnappliedAndUnsyncedBookmarkItem(
677 "orphaned_by_server");
679 // Manually set the SERVER_PARENT_ID to bad value.
680 // A bad parent indicates a hierarchy conflict.
681 syncable::WriteTransaction
trans(FROM_HERE
, UNITTEST
, directory());
682 syncable::MutableEntry
entry(&trans
, syncable::GET_BY_HANDLE
, handle
);
683 ASSERT_TRUE(entry
.good());
685 entry
.PutServerParentId(TestIdFactory::MakeServer("bogus_parent"));
688 sessions::StatusController status
;
689 ApplyBookmarkUpdates(&status
);
691 const UpdateCounters
& counters
= GetBookmarksUpdateCounters();
692 EXPECT_EQ(0, counters
.num_updates_applied
);
693 EXPECT_EQ(0, counters
.num_server_overwrites
);
694 EXPECT_EQ(1, counters
.num_hierarchy_conflict_application_failures
);
697 syncable::ReadTransaction
trans(FROM_HERE
, directory());
698 syncable::Entry
e(&trans
, syncable::GET_BY_HANDLE
, handle
);
699 ASSERT_TRUE(e
.good());
700 EXPECT_TRUE(e
.GetIsUnappliedUpdate());
701 EXPECT_TRUE(e
.GetIsUnsynced());
705 // Attempt to apply an udpate that would create a bookmark folder loop. This
706 // application should fail.
707 TEST_F(DirectoryUpdateHandlerApplyUpdateTest
, BookmarkFolderLoop
) {
708 // Item 'X' locally has parent of 'root'. Server is updating it to have
711 // Create it as a child of root node.
712 int64 handle
= entry_factory()->CreateSyncedItem("X", BOOKMARKS
, true);
715 syncable::WriteTransaction
trans(FROM_HERE
, UNITTEST
, directory());
716 syncable::MutableEntry
entry(&trans
, syncable::GET_BY_HANDLE
, handle
);
717 ASSERT_TRUE(entry
.good());
719 // Re-parent from root to "Y"
720 entry
.PutServerVersion(entry_factory()->GetNextRevision());
721 entry
.PutIsUnappliedUpdate(true);
722 entry
.PutServerParentId(TestIdFactory::MakeServer("Y"));
725 // Item 'Y' is child of 'X'.
726 entry_factory()->CreateUnsyncedItem(
727 TestIdFactory::MakeServer("Y"), TestIdFactory::MakeServer("X"), "Y", true,
730 // If the server's update were applied, we would have X be a child of Y, and Y
731 // as a child of X. That's a directory loop. The UpdateApplicator should
732 // prevent the update from being applied and note that this is a hierarchy
735 sessions::StatusController status
;
736 ApplyBookmarkUpdates(&status
);
738 // This should count as a hierarchy conflict.
739 const UpdateCounters
& counters
= GetBookmarksUpdateCounters();
740 EXPECT_EQ(1, counters
.num_hierarchy_conflict_application_failures
);
743 syncable::ReadTransaction
trans(FROM_HERE
, directory());
744 syncable::Entry
e(&trans
, syncable::GET_BY_HANDLE
, handle
);
745 ASSERT_TRUE(e
.good());
746 EXPECT_TRUE(e
.GetIsUnappliedUpdate());
747 EXPECT_FALSE(e
.GetIsUnsynced());
751 // Test update application where the update has been orphaned by a local folder
752 // deletion. The update application attempt should fail.
753 TEST_F(DirectoryUpdateHandlerApplyUpdateTest
,
754 HierarchyConflictDeletedParent
) {
755 // Create a locally deleted parent item.
757 entry_factory()->CreateUnsyncedItem(
758 Id::CreateFromServerId("parent"), TestIdFactory::root(),
759 "parent", true, BOOKMARKS
, &parent_handle
);
761 syncable::WriteTransaction
trans(FROM_HERE
, UNITTEST
, directory());
762 syncable::MutableEntry
entry(&trans
,
763 syncable::GET_BY_HANDLE
,
765 entry
.PutIsDel(true);
768 // Create an incoming child from the server.
769 int64 child_handle
= entry_factory()->CreateUnappliedNewItemWithParent(
770 "child", DefaultBookmarkSpecifics(), "parent");
772 // The server's update may seem valid to some other client, but on this client
773 // that new item's parent no longer exists. The update should not be applied
774 // and the update applicator should indicate this is a hierarchy conflict.
776 sessions::StatusController status
;
777 ApplyBookmarkUpdates(&status
);
778 const UpdateCounters
& counters
= GetBookmarksUpdateCounters();
779 EXPECT_EQ(1, counters
.num_hierarchy_conflict_application_failures
);
782 syncable::ReadTransaction
trans(FROM_HERE
, directory());
783 syncable::Entry
child(&trans
, syncable::GET_BY_HANDLE
, child_handle
);
784 ASSERT_TRUE(child
.good());
785 EXPECT_TRUE(child
.GetIsUnappliedUpdate());
786 EXPECT_FALSE(child
.GetIsUnsynced());
790 // Attempt to apply an update that deletes a folder where the folder has
791 // locally-created children. The update application should fail.
792 TEST_F(DirectoryUpdateHandlerApplyUpdateTest
,
793 HierarchyConflictDeleteNonEmptyDirectory
) {
794 // Create a server-deleted folder as a child of root node.
795 int64 parent_handle
=
796 entry_factory()->CreateSyncedItem("parent", BOOKMARKS
, true);
798 syncable::WriteTransaction
trans(FROM_HERE
, UNITTEST
, directory());
799 syncable::MutableEntry
entry(&trans
,
800 syncable::GET_BY_HANDLE
,
802 ASSERT_TRUE(entry
.good());
804 // Delete it on the server.
805 entry
.PutServerVersion(entry_factory()->GetNextRevision());
806 entry
.PutIsUnappliedUpdate(true);
807 entry
.PutServerParentId(TestIdFactory::root());
808 entry
.PutServerIsDel(true);
811 // Create a local child of the server-deleted directory.
812 entry_factory()->CreateUnsyncedItem(
813 TestIdFactory::MakeServer("child"), TestIdFactory::MakeServer("parent"),
814 "child", false, BOOKMARKS
, NULL
);
816 // The server's request to delete the directory must be ignored, otherwise our
817 // unsynced new child would be orphaned. This is a hierarchy conflict.
819 sessions::StatusController status
;
820 ApplyBookmarkUpdates(&status
);
822 // This should count as a hierarchy conflict.
823 const UpdateCounters
& counters
= GetBookmarksUpdateCounters();
824 EXPECT_EQ(1, counters
.num_hierarchy_conflict_application_failures
);
827 syncable::ReadTransaction
trans(FROM_HERE
, directory());
828 syncable::Entry
parent(&trans
, syncable::GET_BY_HANDLE
, parent_handle
);
829 ASSERT_TRUE(parent
.good());
830 EXPECT_TRUE(parent
.GetIsUnappliedUpdate());
831 EXPECT_FALSE(parent
.GetIsUnsynced());
835 // Attempt to apply updates where the updated item's parent is not known to this
836 // client. The update application attempt should fail.
837 TEST_F(DirectoryUpdateHandlerApplyUpdateTest
,
838 HierarchyConflictUnknownParent
) {
839 // We shouldn't be able to do anything with either of these items.
840 int64 x_handle
= entry_factory()->CreateUnappliedNewItemWithParent(
841 "some_item", DefaultBookmarkSpecifics(), "unknown_parent");
842 int64 y_handle
= entry_factory()->CreateUnappliedNewItemWithParent(
843 "some_other_item", DefaultBookmarkSpecifics(), "some_item");
845 sessions::StatusController status
;
846 ApplyBookmarkUpdates(&status
);
848 const UpdateCounters
& counters
= GetBookmarksUpdateCounters();
849 EXPECT_EQ(2, counters
.num_hierarchy_conflict_application_failures
)
850 << "All updates with an unknown ancestors should be in conflict";
851 EXPECT_EQ(0, counters
.num_updates_applied
)
852 << "No item with an unknown ancestor should be applied";
855 syncable::ReadTransaction
trans(FROM_HERE
, directory());
856 syncable::Entry
x(&trans
, syncable::GET_BY_HANDLE
, x_handle
);
857 syncable::Entry
y(&trans
, syncable::GET_BY_HANDLE
, y_handle
);
858 ASSERT_TRUE(x
.good());
859 ASSERT_TRUE(y
.good());
860 EXPECT_TRUE(x
.GetIsUnappliedUpdate());
861 EXPECT_TRUE(y
.GetIsUnappliedUpdate());
862 EXPECT_FALSE(x
.GetIsUnsynced());
863 EXPECT_FALSE(y
.GetIsUnsynced());
867 // Attempt application of a mix of items. Some update application attempts will
868 // fail due to hierarchy conflicts. Others should succeed.
869 TEST_F(DirectoryUpdateHandlerApplyUpdateTest
, ItemsBothKnownAndUnknown
) {
870 // See what happens when there's a mixture of good and bad updates.
871 std::string root_server_id
= Id::GetRoot().GetServerId();
872 int64 u1_handle
= entry_factory()->CreateUnappliedNewItemWithParent(
873 "first_unknown_item", DefaultBookmarkSpecifics(), "unknown_parent");
874 int64 k1_handle
= entry_factory()->CreateUnappliedNewItemWithParent(
875 "first_known_item", DefaultBookmarkSpecifics(), root_server_id
);
876 int64 u2_handle
= entry_factory()->CreateUnappliedNewItemWithParent(
877 "second_unknown_item", DefaultBookmarkSpecifics(), "unknown_parent");
878 int64 k2_handle
= entry_factory()->CreateUnappliedNewItemWithParent(
879 "second_known_item", DefaultBookmarkSpecifics(), "first_known_item");
880 int64 k3_handle
= entry_factory()->CreateUnappliedNewItemWithParent(
881 "third_known_item", DefaultBookmarkSpecifics(), "fourth_known_item");
882 int64 k4_handle
= entry_factory()->CreateUnappliedNewItemWithParent(
883 "fourth_known_item", DefaultBookmarkSpecifics(), root_server_id
);
885 sessions::StatusController status
;
886 ApplyBookmarkUpdates(&status
);
888 const UpdateCounters
& counters
= GetBookmarksUpdateCounters();
889 EXPECT_EQ(2, counters
.num_hierarchy_conflict_application_failures
)
890 << "The updates with unknown ancestors should be in conflict";
891 EXPECT_EQ(4, counters
.num_updates_applied
)
892 << "The updates with known ancestors should be successfully applied";
895 syncable::ReadTransaction
trans(FROM_HERE
, directory());
896 syncable::Entry
u1(&trans
, syncable::GET_BY_HANDLE
, u1_handle
);
897 syncable::Entry
u2(&trans
, syncable::GET_BY_HANDLE
, u2_handle
);
898 syncable::Entry
k1(&trans
, syncable::GET_BY_HANDLE
, k1_handle
);
899 syncable::Entry
k2(&trans
, syncable::GET_BY_HANDLE
, k2_handle
);
900 syncable::Entry
k3(&trans
, syncable::GET_BY_HANDLE
, k3_handle
);
901 syncable::Entry
k4(&trans
, syncable::GET_BY_HANDLE
, k4_handle
);
902 ASSERT_TRUE(u1
.good());
903 ASSERT_TRUE(u2
.good());
904 ASSERT_TRUE(k1
.good());
905 ASSERT_TRUE(k2
.good());
906 ASSERT_TRUE(k3
.good());
907 ASSERT_TRUE(k4
.good());
908 EXPECT_TRUE(u1
.GetIsUnappliedUpdate());
909 EXPECT_TRUE(u2
.GetIsUnappliedUpdate());
910 EXPECT_FALSE(k1
.GetIsUnappliedUpdate());
911 EXPECT_FALSE(k2
.GetIsUnappliedUpdate());
912 EXPECT_FALSE(k3
.GetIsUnappliedUpdate());
913 EXPECT_FALSE(k4
.GetIsUnappliedUpdate());
917 // Attempt application of password upates where the passphrase is known.
918 TEST_F(DirectoryUpdateHandlerApplyUpdateTest
, DecryptablePassword
) {
919 // Decryptable password updates should be applied.
920 Cryptographer
* cryptographer
;
922 // Storing the cryptographer separately is bad, but for this test we
924 syncable::ReadTransaction
trans(FROM_HERE
, directory());
925 cryptographer
= directory()->GetCryptographer(&trans
);
928 KeyParams params
= {"localhost", "dummy", "foobar"};
929 cryptographer
->AddKey(params
);
931 sync_pb::EntitySpecifics specifics
;
932 sync_pb::PasswordSpecificsData data
;
933 data
.set_origin("http://example.com");
935 cryptographer
->Encrypt(data
,
936 specifics
.mutable_password()->mutable_encrypted());
938 entry_factory()->CreateUnappliedNewItem("item", specifics
, false);
940 sessions::StatusController status
;
941 ApplyPasswordUpdates(&status
);
943 const UpdateCounters
& counters
= GetPasswordsUpdateCounters();
944 EXPECT_EQ(1, counters
.num_updates_applied
)
945 << "The updates that can be decrypted should be applied";
948 syncable::ReadTransaction
trans(FROM_HERE
, directory());
949 syncable::Entry
e(&trans
, syncable::GET_BY_HANDLE
, handle
);
950 ASSERT_TRUE(e
.good());
951 EXPECT_FALSE(e
.GetIsUnappliedUpdate());
952 EXPECT_FALSE(e
.GetIsUnsynced());
956 // Attempt application of encrypted items when the passphrase is not known.
957 TEST_F(DirectoryUpdateHandlerApplyUpdateTest
, UndecryptableData
) {
958 // Undecryptable updates should not be applied.
959 sync_pb::EntitySpecifics encrypted_bookmark
;
960 encrypted_bookmark
.mutable_encrypted();
961 AddDefaultFieldValue(BOOKMARKS
, &encrypted_bookmark
);
962 std::string root_server_id
= Id::GetRoot().GetServerId();
963 int64 folder_handle
= entry_factory()->CreateUnappliedNewItemWithParent(
967 int64 bookmark_handle
= entry_factory()->CreateUnappliedNewItem(
971 sync_pb::EntitySpecifics encrypted_password
;
972 encrypted_password
.mutable_password();
973 int64 password_handle
= entry_factory()->CreateUnappliedNewItem(
978 sessions::StatusController status
;
979 ApplyBookmarkUpdates(&status
);
980 ApplyPasswordUpdates(&status
);
982 const UpdateCounters
& bm_counters
= GetBookmarksUpdateCounters();
983 EXPECT_EQ(2, bm_counters
.num_encryption_conflict_application_failures
)
984 << "Updates that can't be decrypted should be in encryption conflict";
985 EXPECT_EQ(0, bm_counters
.num_updates_applied
)
986 << "No update that can't be decrypted should be applied";
988 const UpdateCounters
& pw_counters
= GetPasswordsUpdateCounters();
989 EXPECT_EQ(1, pw_counters
.num_encryption_conflict_application_failures
)
990 << "Updates that can't be decrypted should be in encryption conflict";
991 EXPECT_EQ(0, pw_counters
.num_updates_applied
)
992 << "No update that can't be decrypted should be applied";
995 syncable::ReadTransaction
trans(FROM_HERE
, directory());
996 syncable::Entry
folder(&trans
, syncable::GET_BY_HANDLE
, folder_handle
);
997 syncable::Entry
bm(&trans
, syncable::GET_BY_HANDLE
, bookmark_handle
);
998 syncable::Entry
pw(&trans
, syncable::GET_BY_HANDLE
, password_handle
);
999 ASSERT_TRUE(folder
.good());
1000 ASSERT_TRUE(bm
.good());
1001 ASSERT_TRUE(pw
.good());
1002 EXPECT_TRUE(folder
.GetIsUnappliedUpdate());
1003 EXPECT_TRUE(bm
.GetIsUnappliedUpdate());
1004 EXPECT_TRUE(pw
.GetIsUnappliedUpdate());
1008 // Test a mix of decryptable and undecryptable updates.
1009 TEST_F(DirectoryUpdateHandlerApplyUpdateTest
, SomeUndecryptablePassword
) {
1010 Cryptographer
* cryptographer
;
1012 int64 decryptable_handle
= -1;
1013 int64 undecryptable_handle
= -1;
1015 // Only decryptable password updates should be applied.
1017 sync_pb::EntitySpecifics specifics
;
1018 sync_pb::PasswordSpecificsData data
;
1019 data
.set_origin("http://example.com/1");
1021 syncable::ReadTransaction
trans(FROM_HERE
, directory());
1022 cryptographer
= directory()->GetCryptographer(&trans
);
1024 KeyParams params
= {"localhost", "dummy", "foobar"};
1025 cryptographer
->AddKey(params
);
1027 cryptographer
->Encrypt(data
,
1028 specifics
.mutable_password()->mutable_encrypted());
1030 decryptable_handle
=
1031 entry_factory()->CreateUnappliedNewItem("item1", specifics
, false);
1034 // Create a new cryptographer, independent of the one in the session.
1035 Cryptographer
other_cryptographer(cryptographer
->encryptor());
1036 KeyParams params
= {"localhost", "dummy", "bazqux"};
1037 other_cryptographer
.AddKey(params
);
1039 sync_pb::EntitySpecifics specifics
;
1040 sync_pb::PasswordSpecificsData data
;
1041 data
.set_origin("http://example.com/2");
1043 other_cryptographer
.Encrypt(data
,
1044 specifics
.mutable_password()->mutable_encrypted());
1045 undecryptable_handle
=
1046 entry_factory()->CreateUnappliedNewItem("item2", specifics
, false);
1049 sessions::StatusController status
;
1050 ApplyPasswordUpdates(&status
);
1052 const UpdateCounters
& counters
= GetPasswordsUpdateCounters();
1053 EXPECT_EQ(1, counters
.num_encryption_conflict_application_failures
)
1054 << "The updates that can't be decrypted should be in encryption "
1056 EXPECT_EQ(1, counters
.num_updates_applied
)
1057 << "The undecryptable password update shouldn't be applied";
1060 syncable::ReadTransaction
trans(FROM_HERE
, directory());
1061 syncable::Entry
e1(&trans
, syncable::GET_BY_HANDLE
, decryptable_handle
);
1062 syncable::Entry
e2(&trans
, syncable::GET_BY_HANDLE
, undecryptable_handle
);
1063 ASSERT_TRUE(e1
.good());
1064 ASSERT_TRUE(e2
.good());
1065 EXPECT_FALSE(e1
.GetIsUnappliedUpdate());
1066 EXPECT_TRUE(e2
.GetIsUnappliedUpdate());
1070 TEST_F(DirectoryUpdateHandlerApplyUpdateTest
,
1071 SimpleConflictDifferentAttachmentMetadata
) {
1072 const bool is_folder
= false;
1073 sync_pb::EntitySpecifics specifics
;
1074 *specifics
.mutable_article() = sync_pb::ArticleSpecifics();
1075 int64 handle
= entry_factory()->CreateSyncedItem("art1", ARTICLES
, is_folder
);
1077 sync_pb::AttachmentIdProto local_attachment_id
=
1078 CreateAttachmentIdProto(0, 0);
1079 sync_pb::AttachmentIdProto server_attachment_id
=
1080 CreateAttachmentIdProto(0, 0);
1082 // Add an attachment to the local attachment metadata.
1083 sync_pb::AttachmentMetadata local_metadata
;
1084 sync_pb::AttachmentMetadataRecord
* local_record
= local_metadata
.add_record();
1085 *local_record
->mutable_id() = local_attachment_id
;
1086 local_record
->set_is_on_server(true);
1087 entry_factory()->SetLocalAttachmentMetadataForItem(handle
, local_metadata
);
1089 // Add a different attachment to the server attachment metadata.
1090 sync_pb::AttachmentMetadata server_metadata
;
1091 sync_pb::AttachmentMetadataRecord
* server_record
=
1092 server_metadata
.add_record();
1093 *server_record
->mutable_id() = server_attachment_id
;
1094 server_record
->set_is_on_server(true);
1095 entry_factory()->SetServerAttachmentMetadataForItem(handle
, server_metadata
);
1097 // At this point we have a simple conflict. The server says art1 should have
1098 // server_attachment_id, but the local sync engine says it should have
1099 // local_attachment_id.
1101 sessions::StatusController status
;
1102 ApplyArticlesUpdates(&status
);
1104 // See that the server won.
1105 const UpdateCounters
& counters
= GetArticlesUpdateCounters();
1106 EXPECT_EQ(1, counters
.num_updates_applied
);
1107 EXPECT_EQ(1, counters
.num_local_overwrites
);
1108 EXPECT_EQ(0, counters
.num_server_overwrites
);
1109 local_metadata
= entry_factory()->GetLocalAttachmentMetadataForItem(handle
);
1110 EXPECT_EQ(server_metadata
.SerializeAsString(),
1111 local_metadata
.SerializeAsString());
1114 TEST_F(DirectoryUpdateHandlerApplyUpdateTest
,
1115 SimpleConflictSameAttachmentMetadataDifferentOrder
) {
1116 const bool is_folder
= false;
1117 sync_pb::EntitySpecifics specifics
;
1118 *specifics
.mutable_article() = sync_pb::ArticleSpecifics();
1119 int64 handle
= entry_factory()->CreateSyncedItem("art1", ARTICLES
, is_folder
);
1121 sync_pb::AttachmentIdProto id1
= CreateAttachmentIdProto(0, 0);
1122 sync_pb::AttachmentIdProto id2
= CreateAttachmentIdProto(0, 0);
1124 // Add id1, then id2 to the local attachment metadata.
1125 sync_pb::AttachmentMetadata local_metadata
;
1126 sync_pb::AttachmentMetadataRecord
* record
= local_metadata
.add_record();
1127 *record
->mutable_id() = id1
;
1128 record
->set_is_on_server(true);
1129 record
= local_metadata
.add_record();
1130 *record
->mutable_id() = id2
;
1131 record
->set_is_on_server(true);
1132 entry_factory()->SetLocalAttachmentMetadataForItem(handle
, local_metadata
);
1134 // Add id1 and id2 to the server attachment metadata, but in reverse order.
1135 sync_pb::AttachmentMetadata server_metadata
;
1136 record
= server_metadata
.add_record();
1137 *record
->mutable_id() = id2
;
1138 record
->set_is_on_server(true);
1139 record
= local_metadata
.add_record();
1140 *record
->mutable_id() = id1
;
1141 record
->set_is_on_server(true);
1142 entry_factory()->SetServerAttachmentMetadataForItem(handle
, server_metadata
);
1144 // At this point we have a (false) conflict.
1146 sessions::StatusController status
;
1147 ApplyArticlesUpdates(&status
);
1149 // See that the server won.
1150 const UpdateCounters
& counters
= GetArticlesUpdateCounters();
1151 EXPECT_EQ(1, counters
.num_updates_applied
);
1152 EXPECT_EQ(1, counters
.num_local_overwrites
);
1153 EXPECT_EQ(0, counters
.num_server_overwrites
);
1154 local_metadata
= entry_factory()->GetLocalAttachmentMetadataForItem(handle
);
1155 EXPECT_EQ(server_metadata
.SerializeAsString(),
1156 local_metadata
.SerializeAsString());
1159 } // namespace syncer