1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/format_macros.h"
#include "base/location.h"
#include "base/memory/scoped_ptr.h"
#include "base/stringprintf.h"
#include "sync/engine/apply_updates_command.h"
#include "sync/engine/nigori_util.h"
#include "sync/engine/syncer.h"
#include "sync/engine/syncer_util.h"
#include "sync/internal_api/public/test/test_entry_factory.h"
#include "sync/protocol/bookmark_specifics.pb.h"
#include "sync/protocol/password_specifics.pb.h"
#include "sync/sessions/sync_session.h"
#include "sync/syncable/mutable_entry.h"
#include "sync/syncable/read_transaction.h"
#include "sync/syncable/syncable_id.h"
#include "sync/syncable/write_transaction.h"
#include "sync/test/engine/fake_model_worker.h"
#include "sync/test/engine/syncer_command_test.h"
#include "sync/test/engine/test_id_factory.h"
#include "sync/test/fake_encryptor.h"
#include "sync/util/cryptographer.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace csync {
using sessions::SyncSession;
using std::string;
using syncable::Id;
using syncable::MutableEntry;
using syncable::UNITTEST;
using syncable::WriteTransaction;
namespace {
sync_pb::EntitySpecifics DefaultBookmarkSpecifics() {
sync_pb::EntitySpecifics result;
AddDefaultFieldValue(syncable::BOOKMARKS, &result);
return result;
}
} // namespace
// A test fixture for tests exercising ApplyUpdatesCommand.
class ApplyUpdatesCommandTest : public SyncerCommandTest {
public:
protected:
ApplyUpdatesCommandTest() {}
virtual ~ApplyUpdatesCommandTest() {}
virtual void SetUp() {
workers()->clear();
mutable_routing_info()->clear();
workers()->push_back(
make_scoped_refptr(new FakeModelWorker(GROUP_UI)));
workers()->push_back(
make_scoped_refptr(new FakeModelWorker(GROUP_PASSWORD)));
(*mutable_routing_info())[syncable::BOOKMARKS] = GROUP_UI;
(*mutable_routing_info())[syncable::PASSWORDS] = GROUP_PASSWORD;
(*mutable_routing_info())[syncable::NIGORI] = GROUP_PASSIVE;
SyncerCommandTest::SetUp();
entry_factory_.reset(new TestEntryFactory(directory()));
ExpectNoGroupsToChange(apply_updates_command_);
}
ApplyUpdatesCommand apply_updates_command_;
FakeEncryptor encryptor_;
TestIdFactory id_factory_;
scoped_ptr<TestEntryFactory> entry_factory_;
private:
DISALLOW_COPY_AND_ASSIGN(ApplyUpdatesCommandTest);
};
TEST_F(ApplyUpdatesCommandTest, Simple) {
string root_server_id = syncable::GetNullId().GetServerId();
entry_factory_->CreateUnappliedNewItemWithParent("parent",
DefaultBookmarkSpecifics(),
root_server_id);
entry_factory_->CreateUnappliedNewItemWithParent("child",
DefaultBookmarkSpecifics(),
"parent");
ExpectGroupToChange(apply_updates_command_, GROUP_UI);
apply_updates_command_.ExecuteImpl(session());
sessions::StatusController* status = session()->mutable_status_controller();
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_UI);
ASSERT_TRUE(status->update_progress());
EXPECT_EQ(2, status->update_progress()->AppliedUpdatesSize())
<< "All updates should have been attempted";
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(0, status->conflict_progress()->SimpleConflictingItemsSize())
<< "Simple update shouldn't result in conflicts";
EXPECT_EQ(0, status->conflict_progress()->EncryptionConflictingItemsSize())
<< "Simple update shouldn't result in conflicts";
EXPECT_EQ(0, status->conflict_progress()->HierarchyConflictingItemsSize())
<< "Simple update shouldn't result in conflicts";
EXPECT_EQ(2, status->update_progress()->SuccessfullyAppliedUpdateCount())
<< "All items should have been successfully applied";
}
TEST_F(ApplyUpdatesCommandTest, UpdateWithChildrenBeforeParents) {
// Set a bunch of updates which are difficult to apply in the order
// they're received due to dependencies on other unseen items.
string root_server_id = syncable::GetNullId().GetServerId();
entry_factory_->CreateUnappliedNewItemWithParent(
"a_child_created_first", DefaultBookmarkSpecifics(), "parent");
entry_factory_->CreateUnappliedNewItemWithParent(
"x_child_created_first", DefaultBookmarkSpecifics(), "parent");
entry_factory_->CreateUnappliedNewItemWithParent(
"parent", DefaultBookmarkSpecifics(), root_server_id);
entry_factory_->CreateUnappliedNewItemWithParent(
"a_child_created_second", DefaultBookmarkSpecifics(), "parent");
entry_factory_->CreateUnappliedNewItemWithParent(
"x_child_created_second", DefaultBookmarkSpecifics(), "parent");
ExpectGroupToChange(apply_updates_command_, GROUP_UI);
apply_updates_command_.ExecuteImpl(session());
sessions::StatusController* status = session()->mutable_status_controller();
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_UI);
ASSERT_TRUE(status->update_progress());
EXPECT_EQ(5, status->update_progress()->AppliedUpdatesSize())
<< "All updates should have been attempted";
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(0, status->conflict_progress()->SimpleConflictingItemsSize())
<< "Simple update shouldn't result in conflicts, even if out-of-order";
EXPECT_EQ(5, status->update_progress()->SuccessfullyAppliedUpdateCount())
<< "All updates should have been successfully applied";
}
// Runs the ApplyUpdatesCommand on an item that has both local and remote
// modifications (IS_UNSYNCED and IS_UNAPPLIED_UPDATE). We expect the command
// to detect that this update can't be applied because it is in a CONFLICT
// state.
TEST_F(ApplyUpdatesCommandTest, SimpleConflict) {
entry_factory_->CreateUnappliedAndUnsyncedItem(
"item", syncable::BOOKMARKS);
ExpectGroupToChange(apply_updates_command_, GROUP_UI);
apply_updates_command_.ExecuteImpl(session());
sessions::StatusController* status = session()->mutable_status_controller();
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_UI);
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(1, status->conflict_progress()->SimpleConflictingItemsSize())
<< "Unsynced and unapplied item should be a simple conflict";
}
// Runs the ApplyUpdatesCommand on an item that has both local and remote
// modifications *and* the remote modification cannot be applied without
// violating the tree constraints. We expect the command to detect that this
// update can't be applied and that this situation can't be resolved with the
// simple conflict processing logic; it is in a CONFLICT_HIERARCHY state.
TEST_F(ApplyUpdatesCommandTest, HierarchyAndSimpleConflict) {
// Create a simply-conflicting item. It will start with valid parent ids.
int64 handle = entry_factory_->CreateUnappliedAndUnsyncedItem(
"orphaned_by_server", syncable::BOOKMARKS);
{
// Manually set the SERVER_PARENT_ID to bad value.
// A bad parent indicates a hierarchy conflict.
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry entry(&trans, syncable::GET_BY_HANDLE, handle);
ASSERT_TRUE(entry.good());
entry.Put(syncable::SERVER_PARENT_ID,
id_factory_.MakeServer("bogus_parent"));
}
ExpectGroupToChange(apply_updates_command_, GROUP_UI);
apply_updates_command_.ExecuteImpl(session());
sessions::StatusController* status = session()->mutable_status_controller();
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_UI);
EXPECT_EQ(1, status->update_progress()->AppliedUpdatesSize());
// An update that is both a simple conflict and a hierarchy conflict should be
// treated as a hierarchy conflict.
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(1, status->conflict_progress()->HierarchyConflictingItemsSize());
EXPECT_EQ(0, status->conflict_progress()->SimpleConflictingItemsSize());
}
// Runs the ApplyUpdatesCommand on an item with remote modifications that would
// create a directory loop if the update were applied. We expect the command to
// detect that this update can't be applied because it is in a
// CONFLICT_HIERARCHY state.
TEST_F(ApplyUpdatesCommandTest, HierarchyConflictDirectoryLoop) {
// Item 'X' locally has parent of 'root'. Server is updating it to have
// parent of 'Y'.
{
// Create it as a child of root node.
int64 handle = entry_factory_->CreateSyncedItem(
"X", syncable::BOOKMARKS, true);
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry entry(&trans, syncable::GET_BY_HANDLE, handle);
ASSERT_TRUE(entry.good());
// Re-parent from root to "Y"
entry.Put(syncable::SERVER_VERSION, entry_factory_->GetNextRevision());
entry.Put(syncable::IS_UNAPPLIED_UPDATE, true);
entry.Put(syncable::SERVER_PARENT_ID, id_factory_.MakeServer("Y"));
}
// Item 'Y' is child of 'X'.
entry_factory_->CreateUnsyncedItem(
id_factory_.MakeServer("Y"), id_factory_.MakeServer("X"), "Y", true,
syncable::BOOKMARKS, NULL);
// If the server's update were applied, we would have X be a child of Y, and Y
// as a child of X. That's a directory loop. The UpdateApplicator should
// prevent the update from being applied and note that this is a hierarchy
// conflict.
ExpectGroupToChange(apply_updates_command_, GROUP_UI);
apply_updates_command_.ExecuteImpl(session());
sessions::StatusController* status = session()->mutable_status_controller();
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_UI);
EXPECT_EQ(1, status->update_progress()->AppliedUpdatesSize());
// This should count as a hierarchy conflict.
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(1, status->conflict_progress()->HierarchyConflictingItemsSize());
EXPECT_EQ(0, status->conflict_progress()->SimpleConflictingItemsSize());
}
// Runs the ApplyUpdatesCommand on a directory where the server sent us an
// update to add a child to a locally deleted (and unsynced) parent. We expect
// the command to not apply the update and to indicate the update is in a
// CONFLICT_HIERARCHY state.
TEST_F(ApplyUpdatesCommandTest, HierarchyConflictDeletedParent) {
// Create a locally deleted parent item.
int64 parent_handle;
entry_factory_->CreateUnsyncedItem(
Id::CreateFromServerId("parent"), id_factory_.root(),
"parent", true, syncable::BOOKMARKS, &parent_handle);
{
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry entry(&trans, syncable::GET_BY_HANDLE, parent_handle);
entry.Put(syncable::IS_DEL, true);
}
// Create an incoming child from the server.
entry_factory_->CreateUnappliedNewItemWithParent(
"child", DefaultBookmarkSpecifics(), "parent");
// The server's update may seem valid to some other client, but on this client
// that new item's parent no longer exists. The update should not be applied
// and the update applicator should indicate this is a hierarchy conflict.
ExpectGroupToChange(apply_updates_command_, GROUP_UI);
apply_updates_command_.ExecuteImpl(session());
sessions::StatusController* status = session()->mutable_status_controller();
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_UI);
// This should count as a hierarchy conflict.
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(1, status->conflict_progress()->HierarchyConflictingItemsSize());
EXPECT_EQ(0, status->conflict_progress()->SimpleConflictingItemsSize());
}
// Runs the ApplyUpdatesCommand on a directory where the server is trying to
// delete a folder that has a recently added (and unsynced) child. We expect
// the command to not apply the update because it is in a CONFLICT_HIERARCHY
// state.
TEST_F(ApplyUpdatesCommandTest, HierarchyConflictDeleteNonEmptyDirectory) {
// Create a server-deleted directory.
{
// Create it as a child of root node.
int64 handle = entry_factory_->CreateSyncedItem(
"parent", syncable::BOOKMARKS, true);
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry entry(&trans, syncable::GET_BY_HANDLE, handle);
ASSERT_TRUE(entry.good());
// Delete it on the server.
entry.Put(syncable::SERVER_VERSION, entry_factory_->GetNextRevision());
entry.Put(syncable::IS_UNAPPLIED_UPDATE, true);
entry.Put(syncable::SERVER_PARENT_ID, id_factory_.root());
entry.Put(syncable::SERVER_IS_DEL, true);
}
// Create a local child of the server-deleted directory.
entry_factory_->CreateUnsyncedItem(
id_factory_.MakeServer("child"), id_factory_.MakeServer("parent"),
"child", false, syncable::BOOKMARKS, NULL);
// The server's request to delete the directory must be ignored, otherwise our
// unsynced new child would be orphaned. This is a hierarchy conflict.
ExpectGroupToChange(apply_updates_command_, GROUP_UI);
apply_updates_command_.ExecuteImpl(session());
sessions::StatusController* status = session()->mutable_status_controller();
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_UI);
// This should count as a hierarchy conflict.
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(1, status->conflict_progress()->HierarchyConflictingItemsSize());
EXPECT_EQ(0, status->conflict_progress()->SimpleConflictingItemsSize());
}
// Runs the ApplyUpdatesCommand on a server-created item that has a locally
// unknown parent. We expect the command to not apply the update because the
// item is in a CONFLICT_HIERARCHY state.
TEST_F(ApplyUpdatesCommandTest, HierarchyConflictUnknownParent) {
// We shouldn't be able to do anything with either of these items.
entry_factory_->CreateUnappliedNewItemWithParent(
"some_item", DefaultBookmarkSpecifics(), "unknown_parent");
entry_factory_->CreateUnappliedNewItemWithParent(
"some_other_item", DefaultBookmarkSpecifics(), "some_item");
ExpectGroupToChange(apply_updates_command_, GROUP_UI);
apply_updates_command_.ExecuteImpl(session());
sessions::StatusController* status = session()->mutable_status_controller();
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_UI);
ASSERT_TRUE(status->update_progress());
EXPECT_EQ(2, status->update_progress()->AppliedUpdatesSize())
<< "All updates should have been attempted";
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(0, status->conflict_progress()->SimpleConflictingItemsSize())
<< "Updates with unknown parent should not be treated as 'simple'"
<< " conflicts";
EXPECT_EQ(2, status->conflict_progress()->HierarchyConflictingItemsSize())
<< "All updates with an unknown ancestors should be in conflict";
EXPECT_EQ(0, status->update_progress()->SuccessfullyAppliedUpdateCount())
<< "No item with an unknown ancestor should be applied";
}
TEST_F(ApplyUpdatesCommandTest, ItemsBothKnownAndUnknown) {
// See what happens when there's a mixture of good and bad updates.
string root_server_id = syncable::GetNullId().GetServerId();
entry_factory_->CreateUnappliedNewItemWithParent(
"first_unknown_item", DefaultBookmarkSpecifics(), "unknown_parent");
entry_factory_->CreateUnappliedNewItemWithParent(
"first_known_item", DefaultBookmarkSpecifics(), root_server_id);
entry_factory_->CreateUnappliedNewItemWithParent(
"second_unknown_item", DefaultBookmarkSpecifics(), "unknown_parent");
entry_factory_->CreateUnappliedNewItemWithParent(
"second_known_item", DefaultBookmarkSpecifics(), "first_known_item");
entry_factory_->CreateUnappliedNewItemWithParent(
"third_known_item", DefaultBookmarkSpecifics(), "fourth_known_item");
entry_factory_->CreateUnappliedNewItemWithParent(
"fourth_known_item", DefaultBookmarkSpecifics(), root_server_id);
ExpectGroupToChange(apply_updates_command_, GROUP_UI);
apply_updates_command_.ExecuteImpl(session());
sessions::StatusController* status = session()->mutable_status_controller();
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_UI);
ASSERT_TRUE(status->update_progress());
EXPECT_EQ(6, status->update_progress()->AppliedUpdatesSize())
<< "All updates should have been attempted";
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(2, status->conflict_progress()->HierarchyConflictingItemsSize())
<< "The updates with unknown ancestors should be in conflict";
EXPECT_EQ(4, status->update_progress()->SuccessfullyAppliedUpdateCount())
<< "The updates with known ancestors should be successfully applied";
}
TEST_F(ApplyUpdatesCommandTest, DecryptablePassword) {
// Decryptable password updates should be applied.
Cryptographer* cryptographer;
{
// Storing the cryptographer separately is bad, but for this test we
// know it's safe.
syncable::ReadTransaction trans(FROM_HERE, directory());
cryptographer = directory()->GetCryptographer(&trans);
}
csync::KeyParams params = {"localhost", "dummy", "foobar"};
cryptographer->AddKey(params);
sync_pb::EntitySpecifics specifics;
sync_pb::PasswordSpecificsData data;
data.set_origin("http://example.com");
cryptographer->Encrypt(data,
specifics.mutable_password()->mutable_encrypted());
entry_factory_->CreateUnappliedNewItem("item", specifics, false);
ExpectGroupToChange(apply_updates_command_, GROUP_PASSWORD);
apply_updates_command_.ExecuteImpl(session());
sessions::StatusController* status = session()->mutable_status_controller();
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_PASSWORD);
ASSERT_TRUE(status->update_progress());
EXPECT_EQ(1, status->update_progress()->AppliedUpdatesSize())
<< "All updates should have been attempted";
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(0, status->conflict_progress()->SimpleConflictingItemsSize())
<< "No update should be in conflict because they're all decryptable";
EXPECT_EQ(1, status->update_progress()->SuccessfullyAppliedUpdateCount())
<< "The updates that can be decrypted should be applied";
}
TEST_F(ApplyUpdatesCommandTest, UndecryptableData) {
// Undecryptable updates should not be applied.
sync_pb::EntitySpecifics encrypted_bookmark;
encrypted_bookmark.mutable_encrypted();
AddDefaultFieldValue(syncable::BOOKMARKS, &encrypted_bookmark);
string root_server_id = syncable::GetNullId().GetServerId();
entry_factory_->CreateUnappliedNewItemWithParent(
"folder", encrypted_bookmark, root_server_id);
entry_factory_->CreateUnappliedNewItem("item2", encrypted_bookmark, false);
sync_pb::EntitySpecifics encrypted_password;
encrypted_password.mutable_password();
entry_factory_->CreateUnappliedNewItem("item3", encrypted_password, false);
ExpectGroupsToChange(apply_updates_command_, GROUP_UI, GROUP_PASSWORD);
apply_updates_command_.ExecuteImpl(session());
sessions::StatusController* status = session()->mutable_status_controller();
EXPECT_TRUE(status->HasConflictingUpdates())
<< "Updates that can't be decrypted should trigger the syncer to have "
<< "conflicting updates.";
{
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_UI);
ASSERT_TRUE(status->update_progress());
EXPECT_EQ(2, status->update_progress()->AppliedUpdatesSize())
<< "All updates should have been attempted";
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(0, status->conflict_progress()->SimpleConflictingItemsSize())
<< "The updates that can't be decrypted should not be in regular "
<< "conflict";
EXPECT_EQ(2, status->conflict_progress()->EncryptionConflictingItemsSize())
<< "The updates that can't be decrypted should be in encryption "
<< "conflict";
EXPECT_EQ(0, status->update_progress()->SuccessfullyAppliedUpdateCount())
<< "No update that can't be decrypted should be applied";
}
{
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_PASSWORD);
ASSERT_TRUE(status->update_progress());
EXPECT_EQ(1, status->update_progress()->AppliedUpdatesSize())
<< "All updates should have been attempted";
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(0, status->conflict_progress()->SimpleConflictingItemsSize())
<< "The updates that can't be decrypted should not be in regular "
<< "conflict";
EXPECT_EQ(1, status->conflict_progress()->EncryptionConflictingItemsSize())
<< "The updates that can't be decrypted should be in encryption "
<< "conflict";
EXPECT_EQ(0, status->update_progress()->SuccessfullyAppliedUpdateCount())
<< "No update that can't be decrypted should be applied";
}
}
TEST_F(ApplyUpdatesCommandTest, SomeUndecryptablePassword) {
// Only decryptable password updates should be applied.
{
sync_pb::EntitySpecifics specifics;
sync_pb::PasswordSpecificsData data;
data.set_origin("http://example.com/1");
{
syncable::ReadTransaction trans(FROM_HERE, directory());
Cryptographer* cryptographer = directory()->GetCryptographer(&trans);
KeyParams params = {"localhost", "dummy", "foobar"};
cryptographer->AddKey(params);
cryptographer->Encrypt(data,
specifics.mutable_password()->mutable_encrypted());
}
entry_factory_->CreateUnappliedNewItem("item1", specifics, false);
}
{
// Create a new cryptographer, independent of the one in the session.
Cryptographer cryptographer(&encryptor_);
KeyParams params = {"localhost", "dummy", "bazqux"};
cryptographer.AddKey(params);
sync_pb::EntitySpecifics specifics;
sync_pb::PasswordSpecificsData data;
data.set_origin("http://example.com/2");
cryptographer.Encrypt(data,
specifics.mutable_password()->mutable_encrypted());
entry_factory_->CreateUnappliedNewItem("item2", specifics, false);
}
ExpectGroupToChange(apply_updates_command_, GROUP_PASSWORD);
apply_updates_command_.ExecuteImpl(session());
sessions::StatusController* status = session()->mutable_status_controller();
EXPECT_TRUE(status->HasConflictingUpdates())
<< "Updates that can't be decrypted should trigger the syncer to have "
<< "conflicting updates.";
{
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_PASSWORD);
ASSERT_TRUE(status->update_progress());
EXPECT_EQ(2, status->update_progress()->AppliedUpdatesSize())
<< "All updates should have been attempted";
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(0, status->conflict_progress()->SimpleConflictingItemsSize())
<< "The updates that can't be decrypted should not be in regular "
<< "conflict";
EXPECT_EQ(1, status->conflict_progress()->EncryptionConflictingItemsSize())
<< "The updates that can't be decrypted should be in encryption "
<< "conflict";
EXPECT_EQ(1, status->update_progress()->SuccessfullyAppliedUpdateCount())
<< "The undecryptable password update shouldn't be applied";
}
}
TEST_F(ApplyUpdatesCommandTest, NigoriUpdate) {
// Storing the cryptographer separately is bad, but for this test we
// know it's safe.
Cryptographer* cryptographer;
syncable::ModelTypeSet encrypted_types;
encrypted_types.Put(syncable::PASSWORDS);
encrypted_types.Put(syncable::NIGORI);
{
syncable::ReadTransaction trans(FROM_HERE, directory());
cryptographer = directory()->GetCryptographer(&trans);
EXPECT_TRUE(cryptographer->GetEncryptedTypes().Equals(encrypted_types));
}
// Nigori node updates should update the Cryptographer.
Cryptographer other_cryptographer(&encryptor_);
KeyParams params = {"localhost", "dummy", "foobar"};
other_cryptographer.AddKey(params);
sync_pb::EntitySpecifics specifics;
sync_pb::NigoriSpecifics* nigori = specifics.mutable_nigori();
other_cryptographer.GetKeys(nigori->mutable_encrypted());
nigori->set_encrypt_bookmarks(true);
encrypted_types.Put(syncable::BOOKMARKS);
entry_factory_->CreateUnappliedNewItem(
syncable::ModelTypeToRootTag(syncable::NIGORI), specifics, true);
EXPECT_FALSE(cryptographer->has_pending_keys());
ExpectGroupToChange(apply_updates_command_, GROUP_PASSIVE);
apply_updates_command_.ExecuteImpl(session());
sessions::StatusController* status = session()->mutable_status_controller();
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_PASSIVE);
ASSERT_TRUE(status->update_progress());
EXPECT_EQ(1, status->update_progress()->AppliedUpdatesSize())
<< "All updates should have been attempted";
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(0, status->conflict_progress()->SimpleConflictingItemsSize())
<< "The nigori update shouldn't be in conflict";
EXPECT_EQ(1, status->update_progress()->SuccessfullyAppliedUpdateCount())
<< "The nigori update should be applied";
EXPECT_FALSE(cryptographer->is_ready());
EXPECT_TRUE(cryptographer->has_pending_keys());
EXPECT_TRUE(
cryptographer->GetEncryptedTypes()
.Equals(syncable::ModelTypeSet::All()));
}
TEST_F(ApplyUpdatesCommandTest, NigoriUpdateForDisabledTypes) {
// Storing the cryptographer separately is bad, but for this test we
// know it's safe.
Cryptographer* cryptographer;
syncable::ModelTypeSet encrypted_types;
encrypted_types.Put(syncable::PASSWORDS);
encrypted_types.Put(syncable::NIGORI);
{
syncable::ReadTransaction trans(FROM_HERE, directory());
cryptographer = directory()->GetCryptographer(&trans);
EXPECT_TRUE(cryptographer->GetEncryptedTypes().Equals(encrypted_types));
}
// Nigori node updates should update the Cryptographer.
Cryptographer other_cryptographer(&encryptor_);
KeyParams params = {"localhost", "dummy", "foobar"};
other_cryptographer.AddKey(params);
sync_pb::EntitySpecifics specifics;
sync_pb::NigoriSpecifics* nigori = specifics.mutable_nigori();
other_cryptographer.GetKeys(nigori->mutable_encrypted());
nigori->set_encrypt_sessions(true);
nigori->set_encrypt_themes(true);
encrypted_types.Put(syncable::SESSIONS);
encrypted_types.Put(syncable::THEMES);
entry_factory_->CreateUnappliedNewItem(
syncable::ModelTypeToRootTag(syncable::NIGORI), specifics, true);
EXPECT_FALSE(cryptographer->has_pending_keys());
ExpectGroupToChange(apply_updates_command_, GROUP_PASSIVE);
apply_updates_command_.ExecuteImpl(session());
sessions::StatusController* status = session()->mutable_status_controller();
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_PASSIVE);
ASSERT_TRUE(status->update_progress());
EXPECT_EQ(1, status->update_progress()->AppliedUpdatesSize())
<< "All updates should have been attempted";
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(0, status->conflict_progress()->SimpleConflictingItemsSize())
<< "The nigori update shouldn't be in conflict";
EXPECT_EQ(1, status->update_progress()->SuccessfullyAppliedUpdateCount())
<< "The nigori update should be applied";
EXPECT_FALSE(cryptographer->is_ready());
EXPECT_TRUE(cryptographer->has_pending_keys());
EXPECT_TRUE(
cryptographer->GetEncryptedTypes()
.Equals(syncable::ModelTypeSet::All()));
}
// Create some local unsynced and unencrypted data. Apply a nigori update that
// turns on encryption for the unsynced data. Ensure we properly encrypt the
// data as part of the nigori update. Apply another nigori update with no
// changes. Ensure we ignore already-encrypted unsynced data and that nothing
// breaks.
TEST_F(ApplyUpdatesCommandTest, EncryptUnsyncedChanges) {
// Storing the cryptographer separately is bad, but for this test we
// know it's safe.
Cryptographer* cryptographer;
syncable::ModelTypeSet encrypted_types;
encrypted_types.Put(syncable::PASSWORDS);
encrypted_types.Put(syncable::NIGORI);
{
syncable::ReadTransaction trans(FROM_HERE, directory());
cryptographer = directory()->GetCryptographer(&trans);
EXPECT_TRUE(cryptographer->GetEncryptedTypes().Equals(encrypted_types));
// With default encrypted_types, this should be true.
EXPECT_TRUE(VerifyUnsyncedChangesAreEncrypted(&trans, encrypted_types));
Syncer::UnsyncedMetaHandles handles;
SyncerUtil::GetUnsyncedEntries(&trans, &handles);
EXPECT_TRUE(handles.empty());
}
// Create unsynced bookmarks without encryption.
// First item is a folder
Id folder_id = id_factory_.NewLocalId();
entry_factory_->CreateUnsyncedItem(folder_id, id_factory_.root(), "folder",
true, syncable::BOOKMARKS, NULL);
// Next five items are children of the folder
size_t i;
size_t batch_s = 5;
for (i = 0; i < batch_s; ++i) {
entry_factory_->CreateUnsyncedItem(id_factory_.NewLocalId(), folder_id,
base::StringPrintf("Item %"PRIuS"", i),
false, syncable::BOOKMARKS, NULL);
}
// Next five items are children of the root.
for (; i < 2*batch_s; ++i) {
entry_factory_->CreateUnsyncedItem(
id_factory_.NewLocalId(), id_factory_.root(),
base::StringPrintf("Item %"PRIuS"", i), false,
syncable::BOOKMARKS, NULL);
}
KeyParams params = {"localhost", "dummy", "foobar"};
cryptographer->AddKey(params);
sync_pb::EntitySpecifics specifics;
sync_pb::NigoriSpecifics* nigori = specifics.mutable_nigori();
cryptographer->GetKeys(nigori->mutable_encrypted());
nigori->set_encrypt_bookmarks(true);
encrypted_types.Put(syncable::BOOKMARKS);
entry_factory_->CreateUnappliedNewItem(
syncable::ModelTypeToRootTag(syncable::NIGORI), specifics, true);
EXPECT_FALSE(cryptographer->has_pending_keys());
EXPECT_TRUE(cryptographer->is_ready());
{
// Ensure we have unsynced nodes that aren't properly encrypted.
syncable::ReadTransaction trans(FROM_HERE, directory());
EXPECT_FALSE(VerifyUnsyncedChangesAreEncrypted(&trans, encrypted_types));
Syncer::UnsyncedMetaHandles handles;
SyncerUtil::GetUnsyncedEntries(&trans, &handles);
EXPECT_EQ(2*batch_s+1, handles.size());
}
ExpectGroupToChange(apply_updates_command_, GROUP_PASSIVE);
apply_updates_command_.ExecuteImpl(session());
{
sessions::StatusController* status = session()->mutable_status_controller();
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_PASSIVE);
ASSERT_TRUE(status->update_progress());
EXPECT_EQ(1, status->update_progress()->AppliedUpdatesSize())
<< "All updates should have been attempted";
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(0, status->conflict_progress()->SimpleConflictingItemsSize())
<< "No updates should be in conflict";
EXPECT_EQ(0, status->conflict_progress()->EncryptionConflictingItemsSize())
<< "No updates should be in conflict";
EXPECT_EQ(1, status->update_progress()->SuccessfullyAppliedUpdateCount())
<< "The nigori update should be applied";
}
EXPECT_FALSE(cryptographer->has_pending_keys());
EXPECT_TRUE(cryptographer->is_ready());
{
syncable::ReadTransaction trans(FROM_HERE, directory());
// If ProcessUnsyncedChangesForEncryption worked, all our unsynced changes
// should be encrypted now.
EXPECT_TRUE(syncable::ModelTypeSet::All().Equals(
cryptographer->GetEncryptedTypes()));
EXPECT_TRUE(VerifyUnsyncedChangesAreEncrypted(&trans, encrypted_types));
Syncer::UnsyncedMetaHandles handles;
SyncerUtil::GetUnsyncedEntries(&trans, &handles);
EXPECT_EQ(2*batch_s+1, handles.size());
}
// Simulate another nigori update that doesn't change anything.
{
WriteTransaction trans(FROM_HERE, UNITTEST, directory());
MutableEntry entry(&trans, syncable::GET_BY_SERVER_TAG,
syncable::ModelTypeToRootTag(syncable::NIGORI));
ASSERT_TRUE(entry.good());
entry.Put(syncable::SERVER_VERSION, entry_factory_->GetNextRevision());
entry.Put(syncable::IS_UNAPPLIED_UPDATE, true);
}
ExpectGroupToChange(apply_updates_command_, GROUP_PASSIVE);
apply_updates_command_.ExecuteImpl(session());
{
sessions::StatusController* status = session()->mutable_status_controller();
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_PASSIVE);
ASSERT_TRUE(status->update_progress());
EXPECT_EQ(2, status->update_progress()->AppliedUpdatesSize())
<< "All updates should have been attempted";
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(0, status->conflict_progress()->SimpleConflictingItemsSize())
<< "No updates should be in conflict";
EXPECT_EQ(0, status->conflict_progress()->EncryptionConflictingItemsSize())
<< "No updates should be in conflict";
EXPECT_EQ(2, status->update_progress()->SuccessfullyAppliedUpdateCount())
<< "The nigori update should be applied";
}
EXPECT_FALSE(cryptographer->has_pending_keys());
EXPECT_TRUE(cryptographer->is_ready());
{
syncable::ReadTransaction trans(FROM_HERE, directory());
// All our changes should still be encrypted.
EXPECT_TRUE(syncable::ModelTypeSet::All().Equals(
cryptographer->GetEncryptedTypes()));
EXPECT_TRUE(VerifyUnsyncedChangesAreEncrypted(&trans, encrypted_types));
Syncer::UnsyncedMetaHandles handles;
SyncerUtil::GetUnsyncedEntries(&trans, &handles);
EXPECT_EQ(2*batch_s+1, handles.size());
}
}
TEST_F(ApplyUpdatesCommandTest, CannotEncryptUnsyncedChanges) {
// Storing the cryptographer separately is bad, but for this test we
// know it's safe.
Cryptographer* cryptographer;
syncable::ModelTypeSet encrypted_types;
encrypted_types.Put(syncable::PASSWORDS);
encrypted_types.Put(syncable::NIGORI);
{
syncable::ReadTransaction trans(FROM_HERE, directory());
cryptographer = directory()->GetCryptographer(&trans);
EXPECT_TRUE(cryptographer->GetEncryptedTypes().Equals(encrypted_types));
// With default encrypted_types, this should be true.
EXPECT_TRUE(VerifyUnsyncedChangesAreEncrypted(&trans, encrypted_types));
Syncer::UnsyncedMetaHandles handles;
SyncerUtil::GetUnsyncedEntries(&trans, &handles);
EXPECT_TRUE(handles.empty());
}
// Create unsynced bookmarks without encryption.
// First item is a folder
Id folder_id = id_factory_.NewLocalId();
entry_factory_->CreateUnsyncedItem(
folder_id, id_factory_.root(), "folder", true,
syncable::BOOKMARKS, NULL);
// Next five items are children of the folder
size_t i;
size_t batch_s = 5;
for (i = 0; i < batch_s; ++i) {
entry_factory_->CreateUnsyncedItem(id_factory_.NewLocalId(), folder_id,
base::StringPrintf("Item %"PRIuS"", i),
false, syncable::BOOKMARKS, NULL);
}
// Next five items are children of the root.
for (; i < 2*batch_s; ++i) {
entry_factory_->CreateUnsyncedItem(
id_factory_.NewLocalId(), id_factory_.root(),
base::StringPrintf("Item %"PRIuS"", i), false,
syncable::BOOKMARKS, NULL);
}
// We encrypt with new keys, triggering the local cryptographer to be unready
// and unable to decrypt data (once updated).
Cryptographer other_cryptographer(&encryptor_);
KeyParams params = {"localhost", "dummy", "foobar"};
other_cryptographer.AddKey(params);
sync_pb::EntitySpecifics specifics;
sync_pb::NigoriSpecifics* nigori = specifics.mutable_nigori();
other_cryptographer.GetKeys(nigori->mutable_encrypted());
nigori->set_encrypt_bookmarks(true);
encrypted_types.Put(syncable::BOOKMARKS);
entry_factory_->CreateUnappliedNewItem(
syncable::ModelTypeToRootTag(syncable::NIGORI), specifics, true);
EXPECT_FALSE(cryptographer->has_pending_keys());
{
// Ensure we have unsynced nodes that aren't properly encrypted.
syncable::ReadTransaction trans(FROM_HERE, directory());
EXPECT_FALSE(VerifyUnsyncedChangesAreEncrypted(&trans, encrypted_types));
Syncer::UnsyncedMetaHandles handles;
SyncerUtil::GetUnsyncedEntries(&trans, &handles);
EXPECT_EQ(2*batch_s+1, handles.size());
}
ExpectGroupToChange(apply_updates_command_, GROUP_PASSIVE);
apply_updates_command_.ExecuteImpl(session());
sessions::StatusController* status = session()->mutable_status_controller();
sessions::ScopedModelSafeGroupRestriction r(status, GROUP_PASSIVE);
ASSERT_TRUE(status->update_progress());
EXPECT_EQ(1, status->update_progress()->AppliedUpdatesSize())
<< "All updates should have been attempted";
ASSERT_TRUE(status->conflict_progress());
EXPECT_EQ(0, status->conflict_progress()->SimpleConflictingItemsSize())
<< "The unsynced changes don't trigger a blocking conflict with the "
<< "nigori update.";
EXPECT_EQ(0, status->conflict_progress()->EncryptionConflictingItemsSize())
<< "The unsynced changes don't trigger an encryption conflict with the "
<< "nigori update.";
EXPECT_EQ(1, status->update_progress()->SuccessfullyAppliedUpdateCount())
<< "The nigori update should be applied";
EXPECT_FALSE(cryptographer->is_ready());
EXPECT_TRUE(cryptographer->has_pending_keys());
{
syncable::ReadTransaction trans(FROM_HERE, directory());
// Since we have pending keys, we would have failed to encrypt, but the
// cryptographer should be updated.
EXPECT_FALSE(VerifyUnsyncedChangesAreEncrypted(&trans, encrypted_types));
EXPECT_TRUE(cryptographer->GetEncryptedTypes().Equals(
syncable::ModelTypeSet().All()));
EXPECT_FALSE(cryptographer->is_ready());
EXPECT_TRUE(cryptographer->has_pending_keys());
Syncer::UnsyncedMetaHandles handles;
SyncerUtil::GetUnsyncedEntries(&trans, &handles);
EXPECT_EQ(2*batch_s+1, handles.size());
}
}
} // namespace csync
|