1 // Copyright 2012 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 // Unit tests for the SyncApi. Note that a lot of the underlying
6 // functionality is provided by the Syncable layer, which has its own
7 // unit tests. We'll test SyncApi specific things in this harness.
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/compiler_specific.h"
15 #include "base/files/scoped_temp_dir.h"
16 #include "base/format_macros.h"
17 #include "base/location.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/run_loop.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/stringprintf.h"
22 #include "base/strings/utf_string_conversions.h"
23 #include "base/test/values_test_util.h"
24 #include "base/values.h"
25 #include "google_apis/gaia/gaia_constants.h"
26 #include "sync/engine/sync_scheduler.h"
27 #include "sync/internal_api/public/base/attachment_id_proto.h"
28 #include "sync/internal_api/public/base/cancelation_signal.h"
29 #include "sync/internal_api/public/base/model_type_test_util.h"
30 #include "sync/internal_api/public/change_record.h"
31 #include "sync/internal_api/public/engine/model_safe_worker.h"
32 #include "sync/internal_api/public/engine/polling_constants.h"
33 #include "sync/internal_api/public/events/protocol_event.h"
34 #include "sync/internal_api/public/http_post_provider_factory.h"
35 #include "sync/internal_api/public/http_post_provider_interface.h"
36 #include "sync/internal_api/public/read_node.h"
37 #include "sync/internal_api/public/read_transaction.h"
38 #include "sync/internal_api/public/test/test_entry_factory.h"
39 #include "sync/internal_api/public/test/test_internal_components_factory.h"
40 #include "sync/internal_api/public/test/test_user_share.h"
41 #include "sync/internal_api/public/write_node.h"
42 #include "sync/internal_api/public/write_transaction.h"
43 #include "sync/internal_api/sync_encryption_handler_impl.h"
44 #include "sync/internal_api/sync_manager_impl.h"
45 #include "sync/internal_api/syncapi_internal.h"
46 #include "sync/js/js_backend.h"
47 #include "sync/js/js_event_handler.h"
48 #include "sync/js/js_test_util.h"
49 #include "sync/protocol/bookmark_specifics.pb.h"
50 #include "sync/protocol/encryption.pb.h"
51 #include "sync/protocol/extension_specifics.pb.h"
52 #include "sync/protocol/password_specifics.pb.h"
53 #include "sync/protocol/preference_specifics.pb.h"
54 #include "sync/protocol/proto_value_conversions.h"
55 #include "sync/protocol/sync.pb.h"
56 #include "sync/sessions/sync_session.h"
57 #include "sync/syncable/directory.h"
58 #include "sync/syncable/entry.h"
59 #include "sync/syncable/mutable_entry.h"
60 #include "sync/syncable/nigori_util.h"
61 #include "sync/syncable/syncable_id.h"
62 #include "sync/syncable/syncable_read_transaction.h"
63 #include "sync/syncable/syncable_util.h"
64 #include "sync/syncable/syncable_write_transaction.h"
65 #include "sync/test/callback_counter.h"
66 #include "sync/test/engine/fake_model_worker.h"
67 #include "sync/test/engine/fake_sync_scheduler.h"
68 #include "sync/test/engine/test_id_factory.h"
69 #include "sync/test/fake_encryptor.h"
70 #include "sync/util/cryptographer.h"
71 #include "sync/util/extensions_activity.h"
72 #include "sync/util/mock_unrecoverable_error_handler.h"
73 #include "sync/util/time.h"
74 #include "testing/gmock/include/gmock/gmock.h"
75 #include "testing/gtest/include/gtest/gtest.h"
78 using base::ExpectDictStringValue
;
81 using testing::InSequence
;
82 using testing::Return
;
83 using testing::SaveArg
;
84 using testing::StrictMock
;
88 using sessions::SyncSessionSnapshot
;
89 using syncable::GET_BY_HANDLE
;
90 using syncable::IS_DEL
;
91 using syncable::IS_UNSYNCED
;
92 using syncable::NON_UNIQUE_NAME
;
93 using syncable::SPECIFICS
;
94 using syncable::kEncryptedString
;
98 // Makes a child node under the type root folder. Returns the id of the
99 // newly-created node.
100 int64
MakeNode(UserShare
* share
,
101 ModelType model_type
,
102 const std::string
& client_tag
) {
103 WriteTransaction
trans(FROM_HERE
, share
);
104 WriteNode
node(&trans
);
105 WriteNode::InitUniqueByCreationResult result
=
106 node
.InitUniqueByCreation(model_type
, client_tag
);
107 EXPECT_EQ(WriteNode::INIT_SUCCESS
, result
);
108 node
.SetIsFolder(false);
112 // Makes a non-folder child of the root node. Returns the id of the
113 // newly-created node.
114 int64
MakeNodeWithRoot(UserShare
* share
,
115 ModelType model_type
,
116 const std::string
& client_tag
) {
117 WriteTransaction
trans(FROM_HERE
, share
);
118 ReadNode
root_node(&trans
);
119 root_node
.InitByRootLookup();
120 WriteNode
node(&trans
);
121 WriteNode::InitUniqueByCreationResult result
=
122 node
.InitUniqueByCreation(model_type
, root_node
, client_tag
);
123 EXPECT_EQ(WriteNode::INIT_SUCCESS
, result
);
124 node
.SetIsFolder(false);
128 // Makes a folder child of a non-root node. Returns the id of the
129 // newly-created node.
130 int64
MakeFolderWithParent(UserShare
* share
,
131 ModelType model_type
,
133 BaseNode
* predecessor
) {
134 WriteTransaction
trans(FROM_HERE
, share
);
135 ReadNode
parent_node(&trans
);
136 EXPECT_EQ(BaseNode::INIT_OK
, parent_node
.InitByIdLookup(parent_id
));
137 WriteNode
node(&trans
);
138 EXPECT_TRUE(node
.InitBookmarkByCreation(parent_node
, predecessor
));
139 node
.SetIsFolder(true);
143 int64
MakeBookmarkWithParent(UserShare
* share
,
145 BaseNode
* predecessor
) {
146 WriteTransaction
trans(FROM_HERE
, share
);
147 ReadNode
parent_node(&trans
);
148 EXPECT_EQ(BaseNode::INIT_OK
, parent_node
.InitByIdLookup(parent_id
));
149 WriteNode
node(&trans
);
150 EXPECT_TRUE(node
.InitBookmarkByCreation(parent_node
, predecessor
));
154 // Creates the "synced" root node for a particular datatype. We use the syncable
155 // methods here so that the syncer treats these nodes as if they were already
156 // received from the server.
157 int64
MakeTypeRoot(UserShare
* share
, ModelType model_type
) {
158 sync_pb::EntitySpecifics specifics
;
159 AddDefaultFieldValue(model_type
, &specifics
);
160 syncable::WriteTransaction
trans(
161 FROM_HERE
, syncable::UNITTEST
, share
->directory
.get());
162 // Attempt to lookup by nigori tag.
163 std::string type_tag
= ModelTypeToRootTag(model_type
);
164 syncable::Id node_id
= syncable::Id::CreateFromServerId(type_tag
);
165 syncable::MutableEntry
entry(&trans
, syncable::CREATE_NEW_UPDATE_ITEM
,
167 EXPECT_TRUE(entry
.good());
168 entry
.PutBaseVersion(1);
169 entry
.PutServerVersion(1);
170 entry
.PutIsUnappliedUpdate(false);
171 entry
.PutParentId(syncable::Id::GetRoot());
172 entry
.PutServerParentId(syncable::Id::GetRoot());
173 entry
.PutServerIsDir(true);
174 entry
.PutIsDir(true);
175 entry
.PutServerSpecifics(specifics
);
176 entry
.PutSpecifics(specifics
);
177 entry
.PutUniqueServerTag(type_tag
);
178 entry
.PutNonUniqueName(type_tag
);
179 entry
.PutIsDel(false);
180 return entry
.GetMetahandle();
183 // Simulates creating a "synced" node as a child of the root datatype node.
184 int64
MakeServerNode(UserShare
* share
, ModelType model_type
,
185 const std::string
& client_tag
,
186 const std::string
& hashed_tag
,
187 const sync_pb::EntitySpecifics
& specifics
) {
188 syncable::WriteTransaction
trans(
189 FROM_HERE
, syncable::UNITTEST
, share
->directory
.get());
190 syncable::Entry
root_entry(&trans
, syncable::GET_TYPE_ROOT
, model_type
);
191 EXPECT_TRUE(root_entry
.good());
192 syncable::Id root_id
= root_entry
.GetId();
193 syncable::Id node_id
= syncable::Id::CreateFromServerId(client_tag
);
194 syncable::MutableEntry
entry(&trans
, syncable::CREATE_NEW_UPDATE_ITEM
,
196 EXPECT_TRUE(entry
.good());
197 entry
.PutBaseVersion(1);
198 entry
.PutServerVersion(1);
199 entry
.PutIsUnappliedUpdate(false);
200 entry
.PutServerParentId(root_id
);
201 entry
.PutParentId(root_id
);
202 entry
.PutServerIsDir(false);
203 entry
.PutIsDir(false);
204 entry
.PutServerSpecifics(specifics
);
205 entry
.PutSpecifics(specifics
);
206 entry
.PutNonUniqueName(client_tag
);
207 entry
.PutUniqueClientTag(hashed_tag
);
208 entry
.PutIsDel(false);
209 return entry
.GetMetahandle();
212 int GetTotalNodeCount(UserShare
* share
, int64 root
) {
213 ReadTransaction
trans(FROM_HERE
, share
);
214 ReadNode
node(&trans
);
215 EXPECT_EQ(BaseNode::INIT_OK
, node
.InitByIdLookup(root
));
216 return node
.GetTotalNodeCount();
221 class SyncApiTest
: public testing::Test
{
223 void SetUp() override
{ test_user_share_
.SetUp(); }
225 void TearDown() override
{ test_user_share_
.TearDown(); }
228 // Create an entry with the given |model_type|, |client_tag| and
229 // |attachment_metadata|.
230 void CreateEntryWithAttachmentMetadata(
231 const ModelType
& model_type
,
232 const std::string
& client_tag
,
233 const sync_pb::AttachmentMetadata
& attachment_metadata
);
235 // Attempts to load the entry specified by |model_type| and |client_tag| and
236 // returns the lookup result code.
237 BaseNode::InitByLookupResult
LookupEntryByClientTag(
238 const ModelType
& model_type
,
239 const std::string
& client_tag
);
241 // Replace the entry specified by |model_type| and |client_tag| with a
243 void ReplaceWithTombstone(const ModelType
& model_type
,
244 const std::string
& client_tag
);
246 // Save changes to the Directory, destroy it then reload it.
249 UserShare
* user_share();
250 syncable::Directory
* dir();
251 SyncEncryptionHandler
* encryption_handler();
254 base::MessageLoop message_loop_
;
255 TestUserShare test_user_share_
;
258 UserShare
* SyncApiTest::user_share() {
259 return test_user_share_
.user_share();
262 syncable::Directory
* SyncApiTest::dir() {
263 return test_user_share_
.user_share()->directory
.get();
266 SyncEncryptionHandler
* SyncApiTest::encryption_handler() {
267 return test_user_share_
.encryption_handler();
270 bool SyncApiTest::ReloadDir() {
271 return test_user_share_
.Reload();
274 void SyncApiTest::CreateEntryWithAttachmentMetadata(
275 const ModelType
& model_type
,
276 const std::string
& client_tag
,
277 const sync_pb::AttachmentMetadata
& attachment_metadata
) {
278 syncer::WriteTransaction
trans(FROM_HERE
, user_share());
279 syncer::ReadNode
root_node(&trans
);
280 root_node
.InitByRootLookup();
281 syncer::WriteNode
node(&trans
);
282 ASSERT_EQ(node
.InitUniqueByCreation(model_type
, root_node
, client_tag
),
283 syncer::WriteNode::INIT_SUCCESS
);
284 node
.SetAttachmentMetadata(attachment_metadata
);
287 BaseNode::InitByLookupResult
SyncApiTest::LookupEntryByClientTag(
288 const ModelType
& model_type
,
289 const std::string
& client_tag
) {
290 syncer::ReadTransaction
trans(FROM_HERE
, user_share());
291 syncer::ReadNode
node(&trans
);
292 return node
.InitByClientTagLookup(model_type
, client_tag
);
295 void SyncApiTest::ReplaceWithTombstone(const ModelType
& model_type
,
296 const std::string
& client_tag
) {
297 syncer::WriteTransaction
trans(FROM_HERE
, user_share());
298 syncer::WriteNode
node(&trans
);
299 ASSERT_EQ(node
.InitByClientTagLookup(model_type
, client_tag
),
300 syncer::WriteNode::INIT_OK
);
304 TEST_F(SyncApiTest
, SanityCheckTest
) {
306 ReadTransaction
trans(FROM_HERE
, user_share());
307 EXPECT_TRUE(trans
.GetWrappedTrans());
310 WriteTransaction
trans(FROM_HERE
, user_share());
311 EXPECT_TRUE(trans
.GetWrappedTrans());
314 // No entries but root should exist
315 ReadTransaction
trans(FROM_HERE
, user_share());
316 ReadNode
node(&trans
);
317 // Metahandle 1 can be root, sanity check 2
318 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD
, node
.InitByIdLookup(2));
322 TEST_F(SyncApiTest
, BasicTagWrite
) {
324 ReadTransaction
trans(FROM_HERE
, user_share());
325 ReadNode
root_node(&trans
);
326 root_node
.InitByRootLookup();
327 EXPECT_EQ(kInvalidId
, root_node
.GetFirstChildId());
330 ignore_result(MakeNodeWithRoot(user_share(), BOOKMARKS
, "testtag"));
333 ReadTransaction
trans(FROM_HERE
, user_share());
334 ReadNode
node(&trans
);
335 EXPECT_EQ(BaseNode::INIT_OK
,
336 node
.InitByClientTagLookup(BOOKMARKS
, "testtag"));
337 EXPECT_NE(0, node
.GetId());
339 ReadNode
root_node(&trans
);
340 root_node
.InitByRootLookup();
341 EXPECT_EQ(node
.GetId(), root_node
.GetFirstChildId());
345 TEST_F(SyncApiTest
, BasicTagWriteWithImplicitParent
) {
346 int64 type_root
= MakeTypeRoot(user_share(), PREFERENCES
);
349 ReadTransaction
trans(FROM_HERE
, user_share());
350 ReadNode
type_root_node(&trans
);
351 EXPECT_EQ(BaseNode::INIT_OK
, type_root_node
.InitByIdLookup(type_root
));
352 EXPECT_EQ(kInvalidId
, type_root_node
.GetFirstChildId());
355 ignore_result(MakeNode(user_share(), PREFERENCES
, "testtag"));
358 ReadTransaction
trans(FROM_HERE
, user_share());
359 ReadNode
node(&trans
);
360 EXPECT_EQ(BaseNode::INIT_OK
,
361 node
.InitByClientTagLookup(PREFERENCES
, "testtag"));
362 EXPECT_EQ(kInvalidId
, node
.GetParentId());
364 ReadNode
type_root_node(&trans
);
365 EXPECT_EQ(BaseNode::INIT_OK
, type_root_node
.InitByIdLookup(type_root
));
366 EXPECT_EQ(node
.GetId(), type_root_node
.GetFirstChildId());
370 TEST_F(SyncApiTest
, ModelTypesSiloed
) {
372 WriteTransaction
trans(FROM_HERE
, user_share());
373 ReadNode
root_node(&trans
);
374 root_node
.InitByRootLookup();
375 EXPECT_EQ(root_node
.GetFirstChildId(), 0);
378 ignore_result(MakeNodeWithRoot(user_share(), BOOKMARKS
, "collideme"));
379 ignore_result(MakeNodeWithRoot(user_share(), PREFERENCES
, "collideme"));
380 ignore_result(MakeNodeWithRoot(user_share(), AUTOFILL
, "collideme"));
383 ReadTransaction
trans(FROM_HERE
, user_share());
385 ReadNode
bookmarknode(&trans
);
386 EXPECT_EQ(BaseNode::INIT_OK
,
387 bookmarknode
.InitByClientTagLookup(BOOKMARKS
,
390 ReadNode
prefnode(&trans
);
391 EXPECT_EQ(BaseNode::INIT_OK
,
392 prefnode
.InitByClientTagLookup(PREFERENCES
,
395 ReadNode
autofillnode(&trans
);
396 EXPECT_EQ(BaseNode::INIT_OK
,
397 autofillnode
.InitByClientTagLookup(AUTOFILL
,
400 EXPECT_NE(bookmarknode
.GetId(), prefnode
.GetId());
401 EXPECT_NE(autofillnode
.GetId(), prefnode
.GetId());
402 EXPECT_NE(bookmarknode
.GetId(), autofillnode
.GetId());
406 TEST_F(SyncApiTest
, ReadMissingTagsFails
) {
408 ReadTransaction
trans(FROM_HERE
, user_share());
409 ReadNode
node(&trans
);
410 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD
,
411 node
.InitByClientTagLookup(BOOKMARKS
,
415 WriteTransaction
trans(FROM_HERE
, user_share());
416 WriteNode
node(&trans
);
417 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD
,
418 node
.InitByClientTagLookup(BOOKMARKS
,
423 // TODO(chron): Hook this all up to the server and write full integration tests
424 // for update->undelete behavior.
425 TEST_F(SyncApiTest
, TestDeleteBehavior
) {
428 std::string
test_title("test1");
431 WriteTransaction
trans(FROM_HERE
, user_share());
432 ReadNode
root_node(&trans
);
433 root_node
.InitByRootLookup();
435 // we'll use this spare folder later
436 WriteNode
folder_node(&trans
);
437 EXPECT_TRUE(folder_node
.InitBookmarkByCreation(root_node
, NULL
));
438 folder_id
= folder_node
.GetId();
440 WriteNode
wnode(&trans
);
441 WriteNode::InitUniqueByCreationResult result
=
442 wnode
.InitUniqueByCreation(BOOKMARKS
, root_node
, "testtag");
443 EXPECT_EQ(WriteNode::INIT_SUCCESS
, result
);
444 wnode
.SetIsFolder(false);
445 wnode
.SetTitle(test_title
);
447 node_id
= wnode
.GetId();
450 // Ensure we can delete something with a tag.
452 WriteTransaction
trans(FROM_HERE
, user_share());
453 WriteNode
wnode(&trans
);
454 EXPECT_EQ(BaseNode::INIT_OK
,
455 wnode
.InitByClientTagLookup(BOOKMARKS
,
457 EXPECT_FALSE(wnode
.GetIsFolder());
458 EXPECT_EQ(wnode
.GetTitle(), test_title
);
463 // Lookup of a node which was deleted should return failure,
464 // but have found some data about the node.
466 ReadTransaction
trans(FROM_HERE
, user_share());
467 ReadNode
node(&trans
);
468 EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_IS_DEL
,
469 node
.InitByClientTagLookup(BOOKMARKS
,
471 // Note that for proper function of this API this doesn't need to be
472 // filled, we're checking just to make sure the DB worked in this test.
473 EXPECT_EQ(node
.GetTitle(), test_title
);
477 WriteTransaction
trans(FROM_HERE
, user_share());
478 ReadNode
folder_node(&trans
);
479 EXPECT_EQ(BaseNode::INIT_OK
, folder_node
.InitByIdLookup(folder_id
));
481 WriteNode
wnode(&trans
);
482 // This will undelete the tag.
483 WriteNode::InitUniqueByCreationResult result
=
484 wnode
.InitUniqueByCreation(BOOKMARKS
, folder_node
, "testtag");
485 EXPECT_EQ(WriteNode::INIT_SUCCESS
, result
);
486 EXPECT_EQ(wnode
.GetIsFolder(), false);
487 EXPECT_EQ(wnode
.GetParentId(), folder_node
.GetId());
488 EXPECT_EQ(wnode
.GetId(), node_id
);
489 EXPECT_NE(wnode
.GetTitle(), test_title
); // Title should be cleared
490 wnode
.SetTitle(test_title
);
493 // Now look up should work.
495 ReadTransaction
trans(FROM_HERE
, user_share());
496 ReadNode
node(&trans
);
497 EXPECT_EQ(BaseNode::INIT_OK
,
498 node
.InitByClientTagLookup(BOOKMARKS
,
500 EXPECT_EQ(node
.GetTitle(), test_title
);
501 EXPECT_EQ(node
.GetModelType(), BOOKMARKS
);
505 TEST_F(SyncApiTest
, WriteAndReadPassword
) {
506 KeyParams params
= {"localhost", "username", "passphrase"};
508 ReadTransaction
trans(FROM_HERE
, user_share());
509 trans
.GetCryptographer()->AddKey(params
);
512 WriteTransaction
trans(FROM_HERE
, user_share());
513 ReadNode
root_node(&trans
);
514 root_node
.InitByRootLookup();
516 WriteNode
password_node(&trans
);
517 WriteNode::InitUniqueByCreationResult result
=
518 password_node
.InitUniqueByCreation(PASSWORDS
,
520 EXPECT_EQ(WriteNode::INIT_SUCCESS
, result
);
521 sync_pb::PasswordSpecificsData data
;
522 data
.set_password_value("secret");
523 password_node
.SetPasswordSpecifics(data
);
526 ReadTransaction
trans(FROM_HERE
, user_share());
528 ReadNode
password_node(&trans
);
529 EXPECT_EQ(BaseNode::INIT_OK
,
530 password_node
.InitByClientTagLookup(PASSWORDS
, "foo"));
531 const sync_pb::PasswordSpecificsData
& data
=
532 password_node
.GetPasswordSpecifics();
533 EXPECT_EQ("secret", data
.password_value());
537 TEST_F(SyncApiTest
, WriteEncryptedTitle
) {
538 KeyParams params
= {"localhost", "username", "passphrase"};
540 ReadTransaction
trans(FROM_HERE
, user_share());
541 trans
.GetCryptographer()->AddKey(params
);
543 encryption_handler()->EnableEncryptEverything();
546 WriteTransaction
trans(FROM_HERE
, user_share());
547 ReadNode
root_node(&trans
);
548 root_node
.InitByRootLookup();
550 WriteNode
bookmark_node(&trans
);
551 ASSERT_TRUE(bookmark_node
.InitBookmarkByCreation(root_node
, NULL
));
552 bookmark_id
= bookmark_node
.GetId();
553 bookmark_node
.SetTitle("foo");
555 WriteNode
pref_node(&trans
);
556 WriteNode::InitUniqueByCreationResult result
=
557 pref_node
.InitUniqueByCreation(PREFERENCES
, root_node
, "bar");
558 ASSERT_EQ(WriteNode::INIT_SUCCESS
, result
);
559 pref_node
.SetTitle("bar");
562 ReadTransaction
trans(FROM_HERE
, user_share());
564 ReadNode
bookmark_node(&trans
);
565 ASSERT_EQ(BaseNode::INIT_OK
, bookmark_node
.InitByIdLookup(bookmark_id
));
566 EXPECT_EQ("foo", bookmark_node
.GetTitle());
567 EXPECT_EQ(kEncryptedString
,
568 bookmark_node
.GetEntry()->GetNonUniqueName());
570 ReadNode
pref_node(&trans
);
571 ASSERT_EQ(BaseNode::INIT_OK
,
572 pref_node
.InitByClientTagLookup(PREFERENCES
,
574 EXPECT_EQ(kEncryptedString
, pref_node
.GetTitle());
578 // Non-unique name should not be empty. For bookmarks non-unique name is copied
579 // from bookmark title. This test verifies that setting bookmark title to ""
580 // results in single space title and non-unique name in internal representation.
581 // GetTitle should still return empty string.
582 TEST_F(SyncApiTest
, WriteEmptyBookmarkTitle
) {
585 WriteTransaction
trans(FROM_HERE
, user_share());
586 ReadNode
root_node(&trans
);
587 root_node
.InitByRootLookup();
589 WriteNode
bookmark_node(&trans
);
590 ASSERT_TRUE(bookmark_node
.InitBookmarkByCreation(root_node
, NULL
));
591 bookmark_id
= bookmark_node
.GetId();
592 bookmark_node
.SetTitle("");
595 ReadTransaction
trans(FROM_HERE
, user_share());
597 ReadNode
bookmark_node(&trans
);
598 ASSERT_EQ(BaseNode::INIT_OK
, bookmark_node
.InitByIdLookup(bookmark_id
));
599 EXPECT_EQ("", bookmark_node
.GetTitle());
600 EXPECT_EQ(" ", bookmark_node
.GetEntry()->GetSpecifics().bookmark().title());
601 EXPECT_EQ(" ", bookmark_node
.GetEntry()->GetNonUniqueName());
605 TEST_F(SyncApiTest
, BaseNodeSetSpecifics
) {
606 int64 child_id
= MakeNodeWithRoot(user_share(), BOOKMARKS
, "testtag");
607 WriteTransaction
trans(FROM_HERE
, user_share());
608 WriteNode
node(&trans
);
609 EXPECT_EQ(BaseNode::INIT_OK
, node
.InitByIdLookup(child_id
));
611 sync_pb::EntitySpecifics entity_specifics
;
612 entity_specifics
.mutable_bookmark()->set_url("http://www.google.com");
614 EXPECT_NE(entity_specifics
.SerializeAsString(),
615 node
.GetEntitySpecifics().SerializeAsString());
616 node
.SetEntitySpecifics(entity_specifics
);
617 EXPECT_EQ(entity_specifics
.SerializeAsString(),
618 node
.GetEntitySpecifics().SerializeAsString());
621 TEST_F(SyncApiTest
, BaseNodeSetSpecificsPreservesUnknownFields
) {
622 int64 child_id
= MakeNodeWithRoot(user_share(), BOOKMARKS
, "testtag");
623 WriteTransaction
trans(FROM_HERE
, user_share());
624 WriteNode
node(&trans
);
625 EXPECT_EQ(BaseNode::INIT_OK
, node
.InitByIdLookup(child_id
));
626 EXPECT_TRUE(node
.GetEntitySpecifics().unknown_fields().empty());
628 sync_pb::EntitySpecifics entity_specifics
;
629 entity_specifics
.mutable_bookmark()->set_url("http://www.google.com");
630 entity_specifics
.mutable_unknown_fields()->AddFixed32(5, 100);
631 node
.SetEntitySpecifics(entity_specifics
);
632 EXPECT_FALSE(node
.GetEntitySpecifics().unknown_fields().empty());
634 entity_specifics
.mutable_unknown_fields()->Clear();
635 node
.SetEntitySpecifics(entity_specifics
);
636 EXPECT_FALSE(node
.GetEntitySpecifics().unknown_fields().empty());
639 TEST_F(SyncApiTest
, EmptyTags
) {
640 WriteTransaction
trans(FROM_HERE
, user_share());
641 ReadNode
root_node(&trans
);
642 root_node
.InitByRootLookup();
643 WriteNode
node(&trans
);
644 std::string empty_tag
;
645 WriteNode::InitUniqueByCreationResult result
=
646 node
.InitUniqueByCreation(TYPED_URLS
, root_node
, empty_tag
);
647 EXPECT_NE(WriteNode::INIT_SUCCESS
, result
);
648 EXPECT_EQ(BaseNode::INIT_FAILED_PRECONDITION
,
649 node
.InitByClientTagLookup(TYPED_URLS
, empty_tag
));
652 // Test counting nodes when the type's root node has no children.
653 TEST_F(SyncApiTest
, GetTotalNodeCountEmpty
) {
654 int64 type_root
= MakeTypeRoot(user_share(), BOOKMARKS
);
655 EXPECT_EQ(1, GetTotalNodeCount(user_share(), type_root
));
658 // Test counting nodes when there is one child beneath the type's root.
659 TEST_F(SyncApiTest
, GetTotalNodeCountOneChild
) {
660 int64 type_root
= MakeTypeRoot(user_share(), BOOKMARKS
);
661 int64 parent
= MakeFolderWithParent(user_share(), BOOKMARKS
, type_root
, NULL
);
662 EXPECT_EQ(2, GetTotalNodeCount(user_share(), type_root
));
663 EXPECT_EQ(1, GetTotalNodeCount(user_share(), parent
));
666 // Test counting nodes when there are multiple children beneath the type root,
667 // and one of those children has children of its own.
668 TEST_F(SyncApiTest
, GetTotalNodeCountMultipleChildren
) {
669 int64 type_root
= MakeTypeRoot(user_share(), BOOKMARKS
);
670 int64 parent
= MakeFolderWithParent(user_share(), BOOKMARKS
, type_root
, NULL
);
671 ignore_result(MakeFolderWithParent(user_share(), BOOKMARKS
, type_root
, NULL
));
672 int64 child1
= MakeFolderWithParent(user_share(), BOOKMARKS
, parent
, NULL
);
673 ignore_result(MakeBookmarkWithParent(user_share(), parent
, NULL
));
674 ignore_result(MakeBookmarkWithParent(user_share(), child1
, NULL
));
675 EXPECT_EQ(6, GetTotalNodeCount(user_share(), type_root
));
676 EXPECT_EQ(4, GetTotalNodeCount(user_share(), parent
));
679 // Verify that Directory keeps track of which attachments are referenced by
681 TEST_F(SyncApiTest
, AttachmentLinking
) {
682 // Add an entry with an attachment.
683 std::string
tag1("some tag");
684 syncer::AttachmentId
attachment_id(syncer::AttachmentId::Create(0, 0));
685 sync_pb::AttachmentMetadata attachment_metadata
;
686 sync_pb::AttachmentMetadataRecord
* record
= attachment_metadata
.add_record();
687 *record
->mutable_id() = attachment_id
.GetProto();
688 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id
.GetProto()));
689 CreateEntryWithAttachmentMetadata(PREFERENCES
, tag1
, attachment_metadata
);
691 // See that the directory knows it's linked.
692 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id
.GetProto()));
694 // Add a second entry referencing the same attachment.
695 std::string
tag2("some other tag");
696 CreateEntryWithAttachmentMetadata(PREFERENCES
, tag2
, attachment_metadata
);
698 // See that the directory knows it's still linked.
699 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id
.GetProto()));
701 // Tombstone the first entry.
702 ReplaceWithTombstone(syncer::PREFERENCES
, tag1
);
704 // See that the attachment is still considered linked because the entry hasn't
705 // been purged from the Directory.
706 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id
.GetProto()));
708 // Save changes and see that the entry is truly gone.
709 ASSERT_TRUE(dir()->SaveChanges());
710 ASSERT_EQ(LookupEntryByClientTag(PREFERENCES
, tag1
),
711 syncer::WriteNode::INIT_FAILED_ENTRY_NOT_GOOD
);
713 // However, the attachment is still linked.
714 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id
.GetProto()));
716 // Save, destroy, and recreate the directory. See that it's still linked.
717 ASSERT_TRUE(ReloadDir());
718 ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id
.GetProto()));
720 // Tombstone the second entry, save changes, see that it's truly gone.
721 ReplaceWithTombstone(syncer::PREFERENCES
, tag2
);
722 ASSERT_TRUE(dir()->SaveChanges());
723 ASSERT_EQ(LookupEntryByClientTag(PREFERENCES
, tag2
),
724 syncer::WriteNode::INIT_FAILED_ENTRY_NOT_GOOD
);
726 // Finally, the attachment is no longer linked.
727 ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id
.GetProto()));
730 // This tests directory integrity in the case of creating a new unique node
731 // with client tag matching that of an existing unapplied node with server only
732 // data. See crbug.com/505761.
733 TEST_F(SyncApiTest
, WriteNode_UniqueByCreation_UndeleteCase
) {
734 int64 preferences_root
= MakeTypeRoot(user_share(), PREFERENCES
);
736 // Create a node with server only data.
739 syncable::WriteTransaction
trans(FROM_HERE
, syncable::UNITTEST
,
740 user_share()->directory
.get());
741 syncable::MutableEntry
entry(&trans
, syncable::CREATE_NEW_UPDATE_ITEM
,
742 syncable::Id::CreateFromServerId("foo1"));
743 DCHECK(entry
.good());
744 entry
.PutServerVersion(10);
745 entry
.PutIsUnappliedUpdate(true);
746 sync_pb::EntitySpecifics specifics
;
747 AddDefaultFieldValue(PREFERENCES
, &specifics
);
748 entry
.PutServerSpecifics(specifics
);
749 const std::string hash
= syncable::GenerateSyncableHash(PREFERENCES
, "foo");
750 entry
.PutUniqueClientTag(hash
);
751 item1
= entry
.GetMetahandle();
754 // Verify that the server-only item is invisible as a child of
755 // of |preferences_root| because at this point it should have the
756 // "deleted" flag set.
757 EXPECT_EQ(1, GetTotalNodeCount(user_share(), preferences_root
));
759 // Create a client node with the same tag as the node above.
760 int64 item2
= MakeNode(user_share(), PREFERENCES
, "foo");
761 // Expect this to be the same directory entry as |item1|.
762 EXPECT_EQ(item1
, item2
);
763 // Expect it to be visible as a child of |preferences_root|.
764 EXPECT_EQ(2, GetTotalNodeCount(user_share(), preferences_root
));
766 // Tombstone the new item
768 WriteTransaction
trans(FROM_HERE
, user_share());
769 WriteNode
node(&trans
);
770 EXPECT_EQ(BaseNode::INIT_OK
, node
.InitByIdLookup(item1
));
774 // Verify that it is gone from the index.
775 EXPECT_EQ(1, GetTotalNodeCount(user_share(), preferences_root
));
780 class TestHttpPostProviderInterface
: public HttpPostProviderInterface
{
782 ~TestHttpPostProviderInterface() override
{}
784 void SetExtraRequestHeaders(const char* headers
) override
{}
785 void SetURL(const char* url
, int port
) override
{}
786 void SetPostPayload(const char* content_type
,
788 const char* content
) override
{}
789 bool MakeSynchronousPost(int* error_code
, int* response_code
) override
{
792 int GetResponseContentLength() const override
{ return 0; }
793 const char* GetResponseContent() const override
{ return ""; }
794 const std::string
GetResponseHeaderValue(
795 const std::string
& name
) const override
{
796 return std::string();
798 void Abort() override
{}
801 class TestHttpPostProviderFactory
: public HttpPostProviderFactory
{
803 ~TestHttpPostProviderFactory() override
{}
804 void Init(const std::string
& user_agent
) override
{}
805 HttpPostProviderInterface
* Create() override
{
806 return new TestHttpPostProviderInterface();
808 void Destroy(HttpPostProviderInterface
* http
) override
{
809 delete static_cast<TestHttpPostProviderInterface
*>(http
);
813 class SyncManagerObserverMock
: public SyncManager::Observer
{
815 MOCK_METHOD1(OnSyncCycleCompleted
,
816 void(const SyncSessionSnapshot
&)); // NOLINT
817 MOCK_METHOD4(OnInitializationComplete
,
818 void(const WeakHandle
<JsBackend
>&,
819 const WeakHandle
<DataTypeDebugInfoListener
>&,
821 syncer::ModelTypeSet
)); // NOLINT
822 MOCK_METHOD1(OnConnectionStatusChange
, void(ConnectionStatus
)); // NOLINT
823 MOCK_METHOD1(OnUpdatedToken
, void(const std::string
&)); // NOLINT
824 MOCK_METHOD1(OnActionableError
, void(const SyncProtocolError
&)); // NOLINT
825 MOCK_METHOD1(OnMigrationRequested
, void(syncer::ModelTypeSet
)); // NOLINT
826 MOCK_METHOD1(OnProtocolEvent
, void(const ProtocolEvent
&)); // NOLINT
829 class SyncEncryptionHandlerObserverMock
830 : public SyncEncryptionHandler::Observer
{
832 MOCK_METHOD2(OnPassphraseRequired
,
833 void(PassphraseRequiredReason
,
834 const sync_pb::EncryptedData
&)); // NOLINT
835 MOCK_METHOD0(OnPassphraseAccepted
, void()); // NOLINT
836 MOCK_METHOD2(OnBootstrapTokenUpdated
,
837 void(const std::string
&, BootstrapTokenType type
)); // NOLINT
838 MOCK_METHOD2(OnEncryptedTypesChanged
,
839 void(ModelTypeSet
, bool)); // NOLINT
840 MOCK_METHOD0(OnEncryptionComplete
, void()); // NOLINT
841 MOCK_METHOD1(OnCryptographerStateChanged
, void(Cryptographer
*)); // NOLINT
842 MOCK_METHOD2(OnPassphraseTypeChanged
, void(PassphraseType
,
843 base::Time
)); // NOLINT
844 MOCK_METHOD1(OnLocalSetPassphraseEncryption
,
845 void(const SyncEncryptionHandler::NigoriState
&)); // NOLINT
850 class SyncManagerTest
: public testing::Test
,
851 public SyncManager::ChangeDelegate
{
858 enum EncryptionStatus
{
865 : sync_manager_("Test sync manager") {
866 switches_
.encryption_method
=
867 InternalComponentsFactory::ENCRYPTION_KEYSTORE
;
870 virtual ~SyncManagerTest() {
873 // Test implementation.
875 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
877 extensions_activity_
= new ExtensionsActivity();
879 SyncCredentials credentials
;
880 credentials
.email
= "foo@bar.com";
881 credentials
.sync_token
= "sometoken";
882 OAuth2TokenService::ScopeSet scope_set
;
883 scope_set
.insert(GaiaConstants::kChromeSyncOAuth2Scope
);
884 credentials
.scope_set
= scope_set
;
886 sync_manager_
.AddObserver(&manager_observer_
);
887 EXPECT_CALL(manager_observer_
, OnInitializationComplete(_
, _
, _
, _
)).
888 WillOnce(DoAll(SaveArg
<0>(&js_backend_
),
889 SaveArg
<2>(&initialization_succeeded_
)));
891 EXPECT_FALSE(js_backend_
.IsInitialized());
893 std::vector
<scoped_refptr
<ModelSafeWorker
> > workers
;
894 ModelSafeRoutingInfo routing_info
;
895 GetModelSafeRoutingInfo(&routing_info
);
897 // This works only because all routing info types are GROUP_PASSIVE.
898 // If we had types in other groups, we would need additional workers
900 scoped_refptr
<ModelSafeWorker
> worker
= new FakeModelWorker(GROUP_PASSIVE
);
901 workers
.push_back(worker
);
903 SyncManager::InitArgs args
;
904 args
.database_location
= temp_dir_
.path();
905 args
.service_url
= GURL("https://example.com/");
907 scoped_ptr
<HttpPostProviderFactory
>(new TestHttpPostProviderFactory());
908 args
.workers
= workers
;
909 args
.extensions_activity
= extensions_activity_
.get(),
910 args
.change_delegate
= this;
911 args
.credentials
= credentials
;
912 args
.invalidator_client_id
= "fake_invalidator_client_id";
913 args
.internal_components_factory
.reset(GetFactory());
914 args
.encryptor
= &encryptor_
;
915 args
.unrecoverable_error_handler
=
916 MakeWeakHandle(mock_unrecoverable_error_handler_
.GetWeakPtr());
917 args
.cancelation_signal
= &cancelation_signal_
;
918 sync_manager_
.Init(&args
);
920 sync_manager_
.GetEncryptionHandler()->AddObserver(&encryption_observer_
);
922 EXPECT_TRUE(js_backend_
.IsInitialized());
923 EXPECT_EQ(InternalComponentsFactory::STORAGE_ON_DISK
,
926 if (initialization_succeeded_
) {
927 for (ModelSafeRoutingInfo::iterator i
= routing_info
.begin();
928 i
!= routing_info
.end(); ++i
) {
929 type_roots_
[i
->first
] =
930 MakeTypeRoot(sync_manager_
.GetUserShare(), i
->first
);
938 sync_manager_
.RemoveObserver(&manager_observer_
);
939 sync_manager_
.ShutdownOnSyncThread(STOP_SYNC
);
943 void GetModelSafeRoutingInfo(ModelSafeRoutingInfo
* out
) {
944 (*out
)[NIGORI
] = GROUP_PASSIVE
;
945 (*out
)[DEVICE_INFO
] = GROUP_PASSIVE
;
946 (*out
)[EXPERIMENTS
] = GROUP_PASSIVE
;
947 (*out
)[BOOKMARKS
] = GROUP_PASSIVE
;
948 (*out
)[THEMES
] = GROUP_PASSIVE
;
949 (*out
)[SESSIONS
] = GROUP_PASSIVE
;
950 (*out
)[PASSWORDS
] = GROUP_PASSIVE
;
951 (*out
)[PREFERENCES
] = GROUP_PASSIVE
;
952 (*out
)[PRIORITY_PREFERENCES
] = GROUP_PASSIVE
;
953 (*out
)[ARTICLES
] = GROUP_PASSIVE
;
956 ModelTypeSet
GetEnabledTypes() {
957 ModelSafeRoutingInfo routing_info
;
958 GetModelSafeRoutingInfo(&routing_info
);
959 return GetRoutingInfoTypes(routing_info
);
962 void OnChangesApplied(
963 ModelType model_type
,
965 const BaseTransaction
* trans
,
966 const ImmutableChangeRecordList
& changes
) override
{}
968 void OnChangesComplete(ModelType model_type
) override
{}
971 bool SetUpEncryption(NigoriStatus nigori_status
,
972 EncryptionStatus encryption_status
) {
973 UserShare
* share
= sync_manager_
.GetUserShare();
975 // We need to create the nigori node as if it were an applied server update.
976 int64 nigori_id
= GetIdForDataType(NIGORI
);
977 if (nigori_id
== kInvalidId
)
980 // Set the nigori cryptographer information.
981 if (encryption_status
== FULL_ENCRYPTION
)
982 sync_manager_
.GetEncryptionHandler()->EnableEncryptEverything();
984 WriteTransaction
trans(FROM_HERE
, share
);
985 Cryptographer
* cryptographer
= trans
.GetCryptographer();
988 if (encryption_status
!= UNINITIALIZED
) {
989 KeyParams params
= {"localhost", "dummy", "foobar"};
990 cryptographer
->AddKey(params
);
992 DCHECK_NE(nigori_status
, WRITE_TO_NIGORI
);
994 if (nigori_status
== WRITE_TO_NIGORI
) {
995 sync_pb::NigoriSpecifics nigori
;
996 cryptographer
->GetKeys(nigori
.mutable_encryption_keybag());
997 share
->directory
->GetNigoriHandler()->UpdateNigoriFromEncryptedTypes(
999 trans
.GetWrappedTrans());
1000 WriteNode
node(&trans
);
1001 EXPECT_EQ(BaseNode::INIT_OK
, node
.InitByIdLookup(nigori_id
));
1002 node
.SetNigoriSpecifics(nigori
);
1004 return cryptographer
->is_ready();
1007 int64
GetIdForDataType(ModelType type
) {
1008 if (type_roots_
.count(type
) == 0)
1010 return type_roots_
[type
];
1014 base::RunLoop().RunUntilIdle();
1017 void SetJsEventHandler(const WeakHandle
<JsEventHandler
>& event_handler
) {
1018 js_backend_
.Call(FROM_HERE
, &JsBackend::SetJsEventHandler
,
1023 // Looks up an entry by client tag and resets IS_UNSYNCED value to false.
1024 // Returns true if entry was previously unsynced, false if IS_UNSYNCED was
1026 bool ResetUnsyncedEntry(ModelType type
,
1027 const std::string
& client_tag
) {
1028 UserShare
* share
= sync_manager_
.GetUserShare();
1029 syncable::WriteTransaction
trans(
1030 FROM_HERE
, syncable::UNITTEST
, share
->directory
.get());
1031 const std::string hash
= syncable::GenerateSyncableHash(type
, client_tag
);
1032 syncable::MutableEntry
entry(&trans
, syncable::GET_BY_CLIENT_TAG
,
1034 EXPECT_TRUE(entry
.good());
1035 if (!entry
.GetIsUnsynced())
1037 entry
.PutIsUnsynced(false);
1041 virtual InternalComponentsFactory
* GetFactory() {
1042 return new TestInternalComponentsFactory(
1043 GetSwitches(), InternalComponentsFactory::STORAGE_IN_MEMORY
,
1047 // Returns true if we are currently encrypting all sync data. May
1048 // be called on any thread.
1049 bool IsEncryptEverythingEnabledForTest() {
1050 return sync_manager_
.GetEncryptionHandler()->IsEncryptEverythingEnabled();
1053 // Gets the set of encrypted types from the cryptographer
1054 // Note: opens a transaction. May be called from any thread.
1055 ModelTypeSet
GetEncryptedTypes() {
1056 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1057 return GetEncryptedTypesWithTrans(&trans
);
1060 ModelTypeSet
GetEncryptedTypesWithTrans(BaseTransaction
* trans
) {
1061 return trans
->GetDirectory()->GetNigoriHandler()->
1062 GetEncryptedTypes(trans
->GetWrappedTrans());
1065 void SimulateInvalidatorEnabledForTest(bool is_enabled
) {
1066 DCHECK(sync_manager_
.thread_checker_
.CalledOnValidThread());
1067 sync_manager_
.SetInvalidatorEnabled(is_enabled
);
1070 void SetProgressMarkerForType(ModelType type
, bool set
) {
1072 sync_pb::DataTypeProgressMarker marker
;
1073 marker
.set_token("token");
1074 marker
.set_data_type_id(GetSpecificsFieldNumberFromModelType(type
));
1075 sync_manager_
.directory()->SetDownloadProgress(type
, marker
);
1077 sync_pb::DataTypeProgressMarker marker
;
1078 sync_manager_
.directory()->SetDownloadProgress(type
, marker
);
1082 InternalComponentsFactory::Switches
GetSwitches() const {
1086 void ExpectPassphraseAcceptance() {
1087 EXPECT_CALL(encryption_observer_
, OnPassphraseAccepted());
1088 EXPECT_CALL(encryption_observer_
, OnEncryptionComplete());
1089 EXPECT_CALL(encryption_observer_
, OnCryptographerStateChanged(_
));
1092 void SetImplicitPassphraseAndCheck(const std::string
& passphrase
) {
1093 sync_manager_
.GetEncryptionHandler()->SetEncryptionPassphrase(
1096 EXPECT_EQ(IMPLICIT_PASSPHRASE
,
1097 sync_manager_
.GetEncryptionHandler()->GetPassphraseType());
1100 void SetCustomPassphraseAndCheck(const std::string
& passphrase
) {
1101 EXPECT_CALL(encryption_observer_
,
1102 OnPassphraseTypeChanged(CUSTOM_PASSPHRASE
, _
));
1103 sync_manager_
.GetEncryptionHandler()->SetEncryptionPassphrase(
1106 EXPECT_EQ(CUSTOM_PASSPHRASE
,
1107 sync_manager_
.GetEncryptionHandler()->GetPassphraseType());
1110 bool HasUnrecoverableError() {
1111 return mock_unrecoverable_error_handler_
.invocation_count() > 0;
1115 // Needed by |sync_manager_|.
1116 base::MessageLoop message_loop_
;
1117 // Needed by |sync_manager_|.
1118 base::ScopedTempDir temp_dir_
;
1119 // Sync Id's for the roots of the enabled datatypes.
1120 std::map
<ModelType
, int64
> type_roots_
;
1121 scoped_refptr
<ExtensionsActivity
> extensions_activity_
;
1124 FakeEncryptor encryptor_
;
1125 SyncManagerImpl sync_manager_
;
1126 CancelationSignal cancelation_signal_
;
1127 WeakHandle
<JsBackend
> js_backend_
;
1128 bool initialization_succeeded_
;
1129 StrictMock
<SyncManagerObserverMock
> manager_observer_
;
1130 StrictMock
<SyncEncryptionHandlerObserverMock
> encryption_observer_
;
1131 InternalComponentsFactory::Switches switches_
;
1132 InternalComponentsFactory::StorageOption storage_used_
;
1133 MockUnrecoverableErrorHandler mock_unrecoverable_error_handler_
;
1136 TEST_F(SyncManagerTest
, GetAllNodesForTypeTest
) {
1137 ModelSafeRoutingInfo routing_info
;
1138 GetModelSafeRoutingInfo(&routing_info
);
1139 sync_manager_
.StartSyncingNormally(routing_info
, base::Time());
1141 scoped_ptr
<base::ListValue
> node_list(
1142 sync_manager_
.GetAllNodesForType(syncer::PREFERENCES
));
1144 // Should have one node: the type root node.
1145 ASSERT_EQ(1U, node_list
->GetSize());
1147 const base::DictionaryValue
* first_result
;
1148 ASSERT_TRUE(node_list
->GetDictionary(0, &first_result
));
1149 EXPECT_TRUE(first_result
->HasKey("ID"));
1150 EXPECT_TRUE(first_result
->HasKey("NON_UNIQUE_NAME"));
1153 TEST_F(SyncManagerTest
, RefreshEncryptionReady
) {
1154 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, DEFAULT_ENCRYPTION
));
1155 EXPECT_CALL(encryption_observer_
, OnEncryptionComplete());
1156 EXPECT_CALL(encryption_observer_
, OnCryptographerStateChanged(_
));
1157 EXPECT_CALL(encryption_observer_
, OnEncryptedTypesChanged(_
, false));
1159 sync_manager_
.GetEncryptionHandler()->Init();
1162 const ModelTypeSet encrypted_types
= GetEncryptedTypes();
1163 EXPECT_TRUE(encrypted_types
.Has(PASSWORDS
));
1164 EXPECT_FALSE(IsEncryptEverythingEnabledForTest());
1167 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1168 ReadNode
node(&trans
);
1169 EXPECT_EQ(BaseNode::INIT_OK
,
1170 node
.InitByIdLookup(GetIdForDataType(NIGORI
)));
1171 sync_pb::NigoriSpecifics nigori
= node
.GetNigoriSpecifics();
1172 EXPECT_TRUE(nigori
.has_encryption_keybag());
1173 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1174 EXPECT_TRUE(cryptographer
->is_ready());
1175 EXPECT_TRUE(cryptographer
->CanDecrypt(nigori
.encryption_keybag()));
1179 // Attempt to refresh encryption when nigori not downloaded.
1180 TEST_F(SyncManagerTest
, RefreshEncryptionNotReady
) {
1181 // Don't set up encryption (no nigori node created).
1183 // Should fail. Triggers an OnPassphraseRequired because the cryptographer
1185 EXPECT_CALL(encryption_observer_
, OnPassphraseRequired(_
, _
)).Times(1);
1186 EXPECT_CALL(encryption_observer_
, OnCryptographerStateChanged(_
));
1187 EXPECT_CALL(encryption_observer_
, OnEncryptedTypesChanged(_
, false));
1188 sync_manager_
.GetEncryptionHandler()->Init();
1191 const ModelTypeSet encrypted_types
= GetEncryptedTypes();
1192 EXPECT_TRUE(encrypted_types
.Has(PASSWORDS
)); // Hardcoded.
1193 EXPECT_FALSE(IsEncryptEverythingEnabledForTest());
1196 // Attempt to refresh encryption when nigori is empty.
1197 TEST_F(SyncManagerTest
, RefreshEncryptionEmptyNigori
) {
1198 EXPECT_TRUE(SetUpEncryption(DONT_WRITE_NIGORI
, DEFAULT_ENCRYPTION
));
1199 EXPECT_CALL(encryption_observer_
, OnEncryptionComplete()).Times(1);
1200 EXPECT_CALL(encryption_observer_
, OnCryptographerStateChanged(_
));
1201 EXPECT_CALL(encryption_observer_
, OnEncryptedTypesChanged(_
, false));
1203 // Should write to nigori.
1204 sync_manager_
.GetEncryptionHandler()->Init();
1207 const ModelTypeSet encrypted_types
= GetEncryptedTypes();
1208 EXPECT_TRUE(encrypted_types
.Has(PASSWORDS
)); // Hardcoded.
1209 EXPECT_FALSE(IsEncryptEverythingEnabledForTest());
1212 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1213 ReadNode
node(&trans
);
1214 EXPECT_EQ(BaseNode::INIT_OK
,
1215 node
.InitByIdLookup(GetIdForDataType(NIGORI
)));
1216 sync_pb::NigoriSpecifics nigori
= node
.GetNigoriSpecifics();
1217 EXPECT_TRUE(nigori
.has_encryption_keybag());
1218 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1219 EXPECT_TRUE(cryptographer
->is_ready());
1220 EXPECT_TRUE(cryptographer
->CanDecrypt(nigori
.encryption_keybag()));
1224 TEST_F(SyncManagerTest
, EncryptDataTypesWithNoData
) {
1225 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, DEFAULT_ENCRYPTION
));
1226 EXPECT_CALL(encryption_observer_
,
1227 OnEncryptedTypesChanged(
1228 HasModelTypes(EncryptableUserTypes()), true));
1229 EXPECT_CALL(encryption_observer_
, OnEncryptionComplete());
1230 sync_manager_
.GetEncryptionHandler()->EnableEncryptEverything();
1231 EXPECT_TRUE(IsEncryptEverythingEnabledForTest());
1234 TEST_F(SyncManagerTest
, EncryptDataTypesWithData
) {
1235 size_t batch_size
= 5;
1236 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, DEFAULT_ENCRYPTION
));
1238 // Create some unencrypted unsynced data.
1239 int64 folder
= MakeFolderWithParent(sync_manager_
.GetUserShare(),
1241 GetIdForDataType(BOOKMARKS
),
1243 // First batch_size nodes are children of folder.
1245 for (i
= 0; i
< batch_size
; ++i
) {
1246 MakeBookmarkWithParent(sync_manager_
.GetUserShare(), folder
, NULL
);
1248 // Next batch_size nodes are a different type and on their own.
1249 for (; i
< 2*batch_size
; ++i
) {
1250 MakeNodeWithRoot(sync_manager_
.GetUserShare(), SESSIONS
,
1251 base::StringPrintf("%" PRIuS
"", i
));
1253 // Last batch_size nodes are a third type that will not need encryption.
1254 for (; i
< 3*batch_size
; ++i
) {
1255 MakeNodeWithRoot(sync_manager_
.GetUserShare(), THEMES
,
1256 base::StringPrintf("%" PRIuS
"", i
));
1260 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1261 EXPECT_TRUE(GetEncryptedTypesWithTrans(&trans
).Equals(
1262 SyncEncryptionHandler::SensitiveTypes()));
1263 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1264 trans
.GetWrappedTrans(),
1266 false /* not encrypted */));
1267 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1268 trans
.GetWrappedTrans(),
1270 false /* not encrypted */));
1271 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1272 trans
.GetWrappedTrans(),
1274 false /* not encrypted */));
1277 EXPECT_CALL(encryption_observer_
,
1278 OnEncryptedTypesChanged(
1279 HasModelTypes(EncryptableUserTypes()), true));
1280 EXPECT_CALL(encryption_observer_
, OnEncryptionComplete());
1281 sync_manager_
.GetEncryptionHandler()->EnableEncryptEverything();
1282 EXPECT_TRUE(IsEncryptEverythingEnabledForTest());
1284 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1285 EXPECT_TRUE(GetEncryptedTypesWithTrans(&trans
).Equals(
1286 EncryptableUserTypes()));
1287 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1288 trans
.GetWrappedTrans(),
1290 true /* is encrypted */));
1291 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1292 trans
.GetWrappedTrans(),
1294 true /* is encrypted */));
1295 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1296 trans
.GetWrappedTrans(),
1298 true /* is encrypted */));
1301 // Trigger's a ReEncryptEverything with new passphrase.
1302 testing::Mock::VerifyAndClearExpectations(&encryption_observer_
);
1303 EXPECT_CALL(encryption_observer_
,
1304 OnBootstrapTokenUpdated(_
, PASSPHRASE_BOOTSTRAP_TOKEN
));
1305 ExpectPassphraseAcceptance();
1306 SetCustomPassphraseAndCheck("new_passphrase");
1307 EXPECT_TRUE(IsEncryptEverythingEnabledForTest());
1309 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1310 EXPECT_TRUE(GetEncryptedTypesWithTrans(&trans
).Equals(
1311 EncryptableUserTypes()));
1312 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1313 trans
.GetWrappedTrans(),
1315 true /* is encrypted */));
1316 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1317 trans
.GetWrappedTrans(),
1319 true /* is encrypted */));
1320 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1321 trans
.GetWrappedTrans(),
1323 true /* is encrypted */));
1325 // Calling EncryptDataTypes with an empty encrypted types should not trigger
1326 // a reencryption and should just notify immediately.
1327 testing::Mock::VerifyAndClearExpectations(&encryption_observer_
);
1328 EXPECT_CALL(encryption_observer_
,
1329 OnBootstrapTokenUpdated(_
, PASSPHRASE_BOOTSTRAP_TOKEN
)).Times(0);
1330 EXPECT_CALL(encryption_observer_
, OnPassphraseAccepted()).Times(0);
1331 EXPECT_CALL(encryption_observer_
, OnEncryptionComplete()).Times(0);
1332 sync_manager_
.GetEncryptionHandler()->EnableEncryptEverything();
1335 // Test that when there are no pending keys and the cryptographer is not
1336 // initialized, we add a key based on the current GAIA password.
1337 // (case 1 in SyncManager::SyncInternal::SetEncryptionPassphrase)
1338 TEST_F(SyncManagerTest
, SetInitialGaiaPass
) {
1339 EXPECT_FALSE(SetUpEncryption(DONT_WRITE_NIGORI
, UNINITIALIZED
));
1340 EXPECT_CALL(encryption_observer_
,
1341 OnBootstrapTokenUpdated(_
, PASSPHRASE_BOOTSTRAP_TOKEN
));
1342 ExpectPassphraseAcceptance();
1343 SetImplicitPassphraseAndCheck("new_passphrase");
1344 EXPECT_FALSE(IsEncryptEverythingEnabledForTest());
1346 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1347 ReadNode
node(&trans
);
1348 EXPECT_EQ(BaseNode::INIT_OK
, node
.InitTypeRoot(NIGORI
));
1349 sync_pb::NigoriSpecifics nigori
= node
.GetNigoriSpecifics();
1350 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1351 EXPECT_TRUE(cryptographer
->is_ready());
1352 EXPECT_TRUE(cryptographer
->CanDecrypt(nigori
.encryption_keybag()));
1356 // Test that when there are no pending keys and we have on the old GAIA
1357 // password, we update and re-encrypt everything with the new GAIA password.
1358 // (case 1 in SyncManager::SyncInternal::SetEncryptionPassphrase)
1359 TEST_F(SyncManagerTest
, UpdateGaiaPass
) {
1360 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, DEFAULT_ENCRYPTION
));
1361 Cryptographer
verifier(&encryptor_
);
1363 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1364 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1365 std::string bootstrap_token
;
1366 cryptographer
->GetBootstrapToken(&bootstrap_token
);
1367 verifier
.Bootstrap(bootstrap_token
);
1369 EXPECT_CALL(encryption_observer_
,
1370 OnBootstrapTokenUpdated(_
, PASSPHRASE_BOOTSTRAP_TOKEN
));
1371 ExpectPassphraseAcceptance();
1372 SetImplicitPassphraseAndCheck("new_passphrase");
1373 EXPECT_FALSE(IsEncryptEverythingEnabledForTest());
1375 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1376 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1377 EXPECT_TRUE(cryptographer
->is_ready());
1378 // Verify the default key has changed.
1379 sync_pb::EncryptedData encrypted
;
1380 cryptographer
->GetKeys(&encrypted
);
1381 EXPECT_FALSE(verifier
.CanDecrypt(encrypted
));
1385 // Sets a new explicit passphrase. This should update the bootstrap token
1386 // and re-encrypt everything.
1387 // (case 2 in SyncManager::SyncInternal::SetEncryptionPassphrase)
1388 TEST_F(SyncManagerTest
, SetPassphraseWithPassword
) {
1389 Cryptographer
verifier(&encryptor_
);
1390 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, DEFAULT_ENCRYPTION
));
1392 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1393 // Store the default (soon to be old) key.
1394 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1395 std::string bootstrap_token
;
1396 cryptographer
->GetBootstrapToken(&bootstrap_token
);
1397 verifier
.Bootstrap(bootstrap_token
);
1399 ReadNode
root_node(&trans
);
1400 root_node
.InitByRootLookup();
1402 WriteNode
password_node(&trans
);
1403 WriteNode::InitUniqueByCreationResult result
=
1404 password_node
.InitUniqueByCreation(PASSWORDS
,
1406 EXPECT_EQ(WriteNode::INIT_SUCCESS
, result
);
1407 sync_pb::PasswordSpecificsData data
;
1408 data
.set_password_value("secret");
1409 password_node
.SetPasswordSpecifics(data
);
1411 EXPECT_CALL(encryption_observer_
,
1412 OnBootstrapTokenUpdated(_
, PASSPHRASE_BOOTSTRAP_TOKEN
));
1413 ExpectPassphraseAcceptance();
1414 SetCustomPassphraseAndCheck("new_passphrase");
1415 EXPECT_FALSE(IsEncryptEverythingEnabledForTest());
1417 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1418 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1419 EXPECT_TRUE(cryptographer
->is_ready());
1420 // Verify the default key has changed.
1421 sync_pb::EncryptedData encrypted
;
1422 cryptographer
->GetKeys(&encrypted
);
1423 EXPECT_FALSE(verifier
.CanDecrypt(encrypted
));
1425 ReadNode
password_node(&trans
);
1426 EXPECT_EQ(BaseNode::INIT_OK
,
1427 password_node
.InitByClientTagLookup(PASSWORDS
,
1429 const sync_pb::PasswordSpecificsData
& data
=
1430 password_node
.GetPasswordSpecifics();
1431 EXPECT_EQ("secret", data
.password_value());
1435 // Manually set the pending keys in the cryptographer/nigori to reflect the data
1436 // being encrypted with a new (unprovided) GAIA password, then supply the
1438 // (case 7 in SyncManager::SyncInternal::SetDecryptionPassphrase)
1439 TEST_F(SyncManagerTest
, SupplyPendingGAIAPass
) {
1440 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, DEFAULT_ENCRYPTION
));
1441 Cryptographer
other_cryptographer(&encryptor_
);
1443 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1444 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1445 std::string bootstrap_token
;
1446 cryptographer
->GetBootstrapToken(&bootstrap_token
);
1447 other_cryptographer
.Bootstrap(bootstrap_token
);
1449 // Now update the nigori to reflect the new keys, and update the
1450 // cryptographer to have pending keys.
1451 KeyParams params
= {"localhost", "dummy", "passphrase2"};
1452 other_cryptographer
.AddKey(params
);
1453 WriteNode
node(&trans
);
1454 EXPECT_EQ(BaseNode::INIT_OK
, node
.InitTypeRoot(NIGORI
));
1455 sync_pb::NigoriSpecifics nigori
;
1456 other_cryptographer
.GetKeys(nigori
.mutable_encryption_keybag());
1457 cryptographer
->SetPendingKeys(nigori
.encryption_keybag());
1458 EXPECT_TRUE(cryptographer
->has_pending_keys());
1459 node
.SetNigoriSpecifics(nigori
);
1461 EXPECT_CALL(encryption_observer_
,
1462 OnBootstrapTokenUpdated(_
, PASSPHRASE_BOOTSTRAP_TOKEN
));
1463 ExpectPassphraseAcceptance();
1464 sync_manager_
.GetEncryptionHandler()->SetDecryptionPassphrase("passphrase2");
1465 EXPECT_EQ(IMPLICIT_PASSPHRASE
,
1466 sync_manager_
.GetEncryptionHandler()->GetPassphraseType());
1467 EXPECT_FALSE(IsEncryptEverythingEnabledForTest());
1469 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1470 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1471 EXPECT_TRUE(cryptographer
->is_ready());
1472 // Verify we're encrypting with the new key.
1473 sync_pb::EncryptedData encrypted
;
1474 cryptographer
->GetKeys(&encrypted
);
1475 EXPECT_TRUE(other_cryptographer
.CanDecrypt(encrypted
));
1479 // Manually set the pending keys in the cryptographer/nigori to reflect the data
1480 // being encrypted with an old (unprovided) GAIA password. Attempt to supply
1481 // the current GAIA password and verify the bootstrap token is updated. Then
1482 // supply the old GAIA password, and verify we re-encrypt all data with the
1483 // new GAIA password.
1484 // (cases 4 and 5 in SyncManager::SyncInternal::SetEncryptionPassphrase)
1485 TEST_F(SyncManagerTest
, SupplyPendingOldGAIAPass
) {
1486 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, DEFAULT_ENCRYPTION
));
1487 Cryptographer
other_cryptographer(&encryptor_
);
1489 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1490 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1491 std::string bootstrap_token
;
1492 cryptographer
->GetBootstrapToken(&bootstrap_token
);
1493 other_cryptographer
.Bootstrap(bootstrap_token
);
1495 // Now update the nigori to reflect the new keys, and update the
1496 // cryptographer to have pending keys.
1497 KeyParams params
= {"localhost", "dummy", "old_gaia"};
1498 other_cryptographer
.AddKey(params
);
1499 WriteNode
node(&trans
);
1500 EXPECT_EQ(BaseNode::INIT_OK
, node
.InitTypeRoot(NIGORI
));
1501 sync_pb::NigoriSpecifics nigori
;
1502 other_cryptographer
.GetKeys(nigori
.mutable_encryption_keybag());
1503 node
.SetNigoriSpecifics(nigori
);
1504 cryptographer
->SetPendingKeys(nigori
.encryption_keybag());
1506 // other_cryptographer now contains all encryption keys, and is encrypting
1507 // with the newest gaia.
1508 KeyParams new_params
= {"localhost", "dummy", "new_gaia"};
1509 other_cryptographer
.AddKey(new_params
);
1511 // The bootstrap token should have been updated. Save it to ensure it's based
1512 // on the new GAIA password.
1513 std::string bootstrap_token
;
1514 EXPECT_CALL(encryption_observer_
,
1515 OnBootstrapTokenUpdated(_
, PASSPHRASE_BOOTSTRAP_TOKEN
))
1516 .WillOnce(SaveArg
<0>(&bootstrap_token
));
1517 EXPECT_CALL(encryption_observer_
, OnPassphraseRequired(_
,_
));
1518 EXPECT_CALL(encryption_observer_
, OnCryptographerStateChanged(_
));
1519 SetImplicitPassphraseAndCheck("new_gaia");
1520 EXPECT_FALSE(IsEncryptEverythingEnabledForTest());
1521 testing::Mock::VerifyAndClearExpectations(&encryption_observer_
);
1523 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1524 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1525 EXPECT_TRUE(cryptographer
->is_initialized());
1526 EXPECT_FALSE(cryptographer
->is_ready());
1527 // Verify we're encrypting with the new key, even though we have pending
1529 sync_pb::EncryptedData encrypted
;
1530 other_cryptographer
.GetKeys(&encrypted
);
1531 EXPECT_TRUE(cryptographer
->CanDecrypt(encrypted
));
1533 EXPECT_CALL(encryption_observer_
,
1534 OnBootstrapTokenUpdated(_
, PASSPHRASE_BOOTSTRAP_TOKEN
));
1535 ExpectPassphraseAcceptance();
1536 SetImplicitPassphraseAndCheck("old_gaia");
1538 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1539 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1540 EXPECT_TRUE(cryptographer
->is_ready());
1542 // Verify we're encrypting with the new key.
1543 sync_pb::EncryptedData encrypted
;
1544 other_cryptographer
.GetKeys(&encrypted
);
1545 EXPECT_TRUE(cryptographer
->CanDecrypt(encrypted
));
1547 // Verify the saved bootstrap token is based on the new gaia password.
1548 Cryptographer
temp_cryptographer(&encryptor_
);
1549 temp_cryptographer
.Bootstrap(bootstrap_token
);
1550 EXPECT_TRUE(temp_cryptographer
.CanDecrypt(encrypted
));
1554 // Manually set the pending keys in the cryptographer/nigori to reflect the data
1555 // being encrypted with an explicit (unprovided) passphrase, then supply the
1557 // (case 9 in SyncManager::SyncInternal::SetDecryptionPassphrase)
1558 TEST_F(SyncManagerTest
, SupplyPendingExplicitPass
) {
1559 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, DEFAULT_ENCRYPTION
));
1560 Cryptographer
other_cryptographer(&encryptor_
);
1562 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1563 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1564 std::string bootstrap_token
;
1565 cryptographer
->GetBootstrapToken(&bootstrap_token
);
1566 other_cryptographer
.Bootstrap(bootstrap_token
);
1568 // Now update the nigori to reflect the new keys, and update the
1569 // cryptographer to have pending keys.
1570 KeyParams params
= {"localhost", "dummy", "explicit"};
1571 other_cryptographer
.AddKey(params
);
1572 WriteNode
node(&trans
);
1573 EXPECT_EQ(BaseNode::INIT_OK
, node
.InitTypeRoot(NIGORI
));
1574 sync_pb::NigoriSpecifics nigori
;
1575 other_cryptographer
.GetKeys(nigori
.mutable_encryption_keybag());
1576 cryptographer
->SetPendingKeys(nigori
.encryption_keybag());
1577 EXPECT_TRUE(cryptographer
->has_pending_keys());
1578 nigori
.set_keybag_is_frozen(true);
1579 node
.SetNigoriSpecifics(nigori
);
1581 EXPECT_CALL(encryption_observer_
, OnCryptographerStateChanged(_
));
1582 EXPECT_CALL(encryption_observer_
,
1583 OnPassphraseTypeChanged(CUSTOM_PASSPHRASE
, _
));
1584 EXPECT_CALL(encryption_observer_
, OnPassphraseRequired(_
, _
));
1585 EXPECT_CALL(encryption_observer_
, OnEncryptedTypesChanged(_
, false));
1586 sync_manager_
.GetEncryptionHandler()->Init();
1587 EXPECT_CALL(encryption_observer_
,
1588 OnBootstrapTokenUpdated(_
, PASSPHRASE_BOOTSTRAP_TOKEN
));
1589 ExpectPassphraseAcceptance();
1590 sync_manager_
.GetEncryptionHandler()->SetDecryptionPassphrase("explicit");
1591 EXPECT_EQ(CUSTOM_PASSPHRASE
,
1592 sync_manager_
.GetEncryptionHandler()->GetPassphraseType());
1593 EXPECT_FALSE(IsEncryptEverythingEnabledForTest());
1595 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1596 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1597 EXPECT_TRUE(cryptographer
->is_ready());
1598 // Verify we're encrypting with the new key.
1599 sync_pb::EncryptedData encrypted
;
1600 cryptographer
->GetKeys(&encrypted
);
1601 EXPECT_TRUE(other_cryptographer
.CanDecrypt(encrypted
));
1605 // Manually set the pending keys in the cryptographer/nigori to reflect the data
1606 // being encrypted with a new (unprovided) GAIA password, then supply the
1607 // password as a user-provided password.
1608 // This is the android case 7/8.
1609 TEST_F(SyncManagerTest
, SupplyPendingGAIAPassUserProvided
) {
1610 EXPECT_FALSE(SetUpEncryption(DONT_WRITE_NIGORI
, UNINITIALIZED
));
1611 Cryptographer
other_cryptographer(&encryptor_
);
1613 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1614 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1615 // Now update the nigori to reflect the new keys, and update the
1616 // cryptographer to have pending keys.
1617 KeyParams params
= {"localhost", "dummy", "passphrase"};
1618 other_cryptographer
.AddKey(params
);
1619 WriteNode
node(&trans
);
1620 EXPECT_EQ(BaseNode::INIT_OK
, node
.InitTypeRoot(NIGORI
));
1621 sync_pb::NigoriSpecifics nigori
;
1622 other_cryptographer
.GetKeys(nigori
.mutable_encryption_keybag());
1623 node
.SetNigoriSpecifics(nigori
);
1624 cryptographer
->SetPendingKeys(nigori
.encryption_keybag());
1625 EXPECT_FALSE(cryptographer
->is_ready());
1627 EXPECT_CALL(encryption_observer_
,
1628 OnBootstrapTokenUpdated(_
, PASSPHRASE_BOOTSTRAP_TOKEN
));
1629 ExpectPassphraseAcceptance();
1630 SetImplicitPassphraseAndCheck("passphrase");
1631 EXPECT_FALSE(IsEncryptEverythingEnabledForTest());
1633 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1634 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1635 EXPECT_TRUE(cryptographer
->is_ready());
1639 TEST_F(SyncManagerTest
, SetPassphraseWithEmptyPasswordNode
) {
1640 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, DEFAULT_ENCRYPTION
));
1642 std::string tag
= "foo";
1644 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1645 ReadNode
root_node(&trans
);
1646 root_node
.InitByRootLookup();
1648 WriteNode
password_node(&trans
);
1649 WriteNode::InitUniqueByCreationResult result
=
1650 password_node
.InitUniqueByCreation(PASSWORDS
, root_node
, tag
);
1651 EXPECT_EQ(WriteNode::INIT_SUCCESS
, result
);
1652 node_id
= password_node
.GetId();
1654 EXPECT_CALL(encryption_observer_
,
1655 OnBootstrapTokenUpdated(_
, PASSPHRASE_BOOTSTRAP_TOKEN
));
1656 ExpectPassphraseAcceptance();
1657 SetCustomPassphraseAndCheck("new_passphrase");
1658 EXPECT_FALSE(IsEncryptEverythingEnabledForTest());
1660 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1661 ReadNode
password_node(&trans
);
1662 EXPECT_EQ(BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY
,
1663 password_node
.InitByClientTagLookup(PASSWORDS
,
1667 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1668 ReadNode
password_node(&trans
);
1669 EXPECT_EQ(BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY
,
1670 password_node
.InitByIdLookup(node_id
));
1674 // Friended by WriteNode, so can't be in an anonymouse namespace.
1675 TEST_F(SyncManagerTest
, EncryptBookmarksWithLegacyData
) {
1676 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, DEFAULT_ENCRYPTION
));
1678 SyncAPINameToServerName("Google", &title
);
1679 std::string url
= "http://www.google.com";
1680 std::string raw_title2
= ".."; // An invalid cosmo title.
1682 SyncAPINameToServerName(raw_title2
, &title2
);
1683 std::string url2
= "http://www.bla.com";
1685 // Create a bookmark using the legacy format.
1687 MakeNodeWithRoot(sync_manager_
.GetUserShare(), BOOKMARKS
, "testtag");
1689 MakeNodeWithRoot(sync_manager_
.GetUserShare(), BOOKMARKS
, "testtag2");
1691 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1692 WriteNode
node(&trans
);
1693 EXPECT_EQ(BaseNode::INIT_OK
, node
.InitByIdLookup(node_id1
));
1695 sync_pb::EntitySpecifics entity_specifics
;
1696 entity_specifics
.mutable_bookmark()->set_url(url
);
1697 node
.SetEntitySpecifics(entity_specifics
);
1699 // Set the old style title.
1700 syncable::MutableEntry
* node_entry
= node
.entry_
;
1701 node_entry
->PutNonUniqueName(title
);
1703 WriteNode
node2(&trans
);
1704 EXPECT_EQ(BaseNode::INIT_OK
, node2
.InitByIdLookup(node_id2
));
1706 sync_pb::EntitySpecifics entity_specifics2
;
1707 entity_specifics2
.mutable_bookmark()->set_url(url2
);
1708 node2
.SetEntitySpecifics(entity_specifics2
);
1710 // Set the old style title.
1711 syncable::MutableEntry
* node_entry2
= node2
.entry_
;
1712 node_entry2
->PutNonUniqueName(title2
);
1716 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1717 ReadNode
node(&trans
);
1718 EXPECT_EQ(BaseNode::INIT_OK
, node
.InitByIdLookup(node_id1
));
1719 EXPECT_EQ(BOOKMARKS
, node
.GetModelType());
1720 EXPECT_EQ(title
, node
.GetTitle());
1721 EXPECT_EQ(title
, node
.GetBookmarkSpecifics().title());
1722 EXPECT_EQ(url
, node
.GetBookmarkSpecifics().url());
1724 ReadNode
node2(&trans
);
1725 EXPECT_EQ(BaseNode::INIT_OK
, node2
.InitByIdLookup(node_id2
));
1726 EXPECT_EQ(BOOKMARKS
, node2
.GetModelType());
1727 // We should de-canonicalize the title in GetTitle(), but the title in the
1728 // specifics should be stored in the server legal form.
1729 EXPECT_EQ(raw_title2
, node2
.GetTitle());
1730 EXPECT_EQ(title2
, node2
.GetBookmarkSpecifics().title());
1731 EXPECT_EQ(url2
, node2
.GetBookmarkSpecifics().url());
1735 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1736 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1737 trans
.GetWrappedTrans(),
1739 false /* not encrypted */));
1742 EXPECT_CALL(encryption_observer_
,
1743 OnEncryptedTypesChanged(
1744 HasModelTypes(EncryptableUserTypes()), true));
1745 EXPECT_CALL(encryption_observer_
, OnEncryptionComplete());
1746 sync_manager_
.GetEncryptionHandler()->EnableEncryptEverything();
1747 EXPECT_TRUE(IsEncryptEverythingEnabledForTest());
1750 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1751 EXPECT_TRUE(GetEncryptedTypesWithTrans(&trans
).Equals(
1752 EncryptableUserTypes()));
1753 EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1754 trans
.GetWrappedTrans(),
1756 true /* is encrypted */));
1758 ReadNode
node(&trans
);
1759 EXPECT_EQ(BaseNode::INIT_OK
, node
.InitByIdLookup(node_id1
));
1760 EXPECT_EQ(BOOKMARKS
, node
.GetModelType());
1761 EXPECT_EQ(title
, node
.GetTitle());
1762 EXPECT_EQ(title
, node
.GetBookmarkSpecifics().title());
1763 EXPECT_EQ(url
, node
.GetBookmarkSpecifics().url());
1765 ReadNode
node2(&trans
);
1766 EXPECT_EQ(BaseNode::INIT_OK
, node2
.InitByIdLookup(node_id2
));
1767 EXPECT_EQ(BOOKMARKS
, node2
.GetModelType());
1768 // We should de-canonicalize the title in GetTitle(), but the title in the
1769 // specifics should be stored in the server legal form.
1770 EXPECT_EQ(raw_title2
, node2
.GetTitle());
1771 EXPECT_EQ(title2
, node2
.GetBookmarkSpecifics().title());
1772 EXPECT_EQ(url2
, node2
.GetBookmarkSpecifics().url());
1776 // Create a bookmark and set the title/url, then verify the data was properly
1777 // set. This replicates the unique way bookmarks have of creating sync nodes.
1778 // See BookmarkChangeProcessor::PlaceSyncNode(..).
1779 TEST_F(SyncManagerTest
, CreateLocalBookmark
) {
1780 std::string title
= "title";
1781 std::string url
= "url";
1783 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1784 ReadNode
bookmark_root(&trans
);
1785 ASSERT_EQ(BaseNode::INIT_OK
, bookmark_root
.InitTypeRoot(BOOKMARKS
));
1786 WriteNode
node(&trans
);
1787 ASSERT_TRUE(node
.InitBookmarkByCreation(bookmark_root
, NULL
));
1788 node
.SetIsFolder(false);
1789 node
.SetTitle(title
);
1791 sync_pb::BookmarkSpecifics
bookmark_specifics(node
.GetBookmarkSpecifics());
1792 bookmark_specifics
.set_url(url
);
1793 node
.SetBookmarkSpecifics(bookmark_specifics
);
1796 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1797 ReadNode
bookmark_root(&trans
);
1798 ASSERT_EQ(BaseNode::INIT_OK
, bookmark_root
.InitTypeRoot(BOOKMARKS
));
1799 int64 child_id
= bookmark_root
.GetFirstChildId();
1801 ReadNode
node(&trans
);
1802 ASSERT_EQ(BaseNode::INIT_OK
, node
.InitByIdLookup(child_id
));
1803 EXPECT_FALSE(node
.GetIsFolder());
1804 EXPECT_EQ(title
, node
.GetTitle());
1805 EXPECT_EQ(url
, node
.GetBookmarkSpecifics().url());
1809 // Verifies WriteNode::UpdateEntryWithEncryption does not make unnecessary
1811 TEST_F(SyncManagerTest
, UpdateEntryWithEncryption
) {
1812 std::string client_tag
= "title";
1813 sync_pb::EntitySpecifics entity_specifics
;
1814 entity_specifics
.mutable_bookmark()->set_url("url");
1815 entity_specifics
.mutable_bookmark()->set_title("title");
1816 MakeServerNode(sync_manager_
.GetUserShare(), BOOKMARKS
, client_tag
,
1817 syncable::GenerateSyncableHash(BOOKMARKS
,
1820 // New node shouldn't start off unsynced.
1821 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS
, client_tag
));
1822 // Manually change to the same data. Should not set is_unsynced.
1824 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1825 WriteNode
node(&trans
);
1826 EXPECT_EQ(BaseNode::INIT_OK
,
1827 node
.InitByClientTagLookup(BOOKMARKS
, client_tag
));
1828 node
.SetEntitySpecifics(entity_specifics
);
1830 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS
, client_tag
));
1832 // Encrypt the datatatype, should set is_unsynced.
1833 EXPECT_CALL(encryption_observer_
,
1834 OnEncryptedTypesChanged(
1835 HasModelTypes(EncryptableUserTypes()), true));
1836 EXPECT_CALL(encryption_observer_
, OnEncryptionComplete());
1837 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, FULL_ENCRYPTION
));
1839 EXPECT_CALL(encryption_observer_
, OnCryptographerStateChanged(_
));
1840 EXPECT_CALL(encryption_observer_
, OnEncryptedTypesChanged(_
, true));
1841 sync_manager_
.GetEncryptionHandler()->Init();
1844 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1845 ReadNode
node(&trans
);
1846 EXPECT_EQ(BaseNode::INIT_OK
,
1847 node
.InitByClientTagLookup(BOOKMARKS
, client_tag
));
1848 const syncable::Entry
* node_entry
= node
.GetEntry();
1849 const sync_pb::EntitySpecifics
& specifics
= node_entry
->GetSpecifics();
1850 EXPECT_TRUE(specifics
.has_encrypted());
1851 EXPECT_EQ(kEncryptedString
, node_entry
->GetNonUniqueName());
1852 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1853 EXPECT_TRUE(cryptographer
->is_ready());
1854 EXPECT_TRUE(cryptographer
->CanDecryptUsingDefaultKey(
1855 specifics
.encrypted()));
1857 EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS
, client_tag
));
1859 // Set a new passphrase. Should set is_unsynced.
1860 testing::Mock::VerifyAndClearExpectations(&encryption_observer_
);
1861 EXPECT_CALL(encryption_observer_
,
1862 OnBootstrapTokenUpdated(_
, PASSPHRASE_BOOTSTRAP_TOKEN
));
1863 ExpectPassphraseAcceptance();
1864 SetCustomPassphraseAndCheck("new_passphrase");
1866 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1867 ReadNode
node(&trans
);
1868 EXPECT_EQ(BaseNode::INIT_OK
,
1869 node
.InitByClientTagLookup(BOOKMARKS
, client_tag
));
1870 const syncable::Entry
* node_entry
= node
.GetEntry();
1871 const sync_pb::EntitySpecifics
& specifics
= node_entry
->GetSpecifics();
1872 EXPECT_TRUE(specifics
.has_encrypted());
1873 EXPECT_EQ(kEncryptedString
, node_entry
->GetNonUniqueName());
1874 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1875 EXPECT_TRUE(cryptographer
->is_ready());
1876 EXPECT_TRUE(cryptographer
->CanDecryptUsingDefaultKey(
1877 specifics
.encrypted()));
1879 EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS
, client_tag
));
1881 // Force a re-encrypt everything. Should not set is_unsynced.
1882 testing::Mock::VerifyAndClearExpectations(&encryption_observer_
);
1883 EXPECT_CALL(encryption_observer_
, OnEncryptionComplete());
1884 EXPECT_CALL(encryption_observer_
, OnCryptographerStateChanged(_
));
1885 EXPECT_CALL(encryption_observer_
, OnEncryptedTypesChanged(_
, true));
1887 sync_manager_
.GetEncryptionHandler()->Init();
1891 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1892 ReadNode
node(&trans
);
1893 EXPECT_EQ(BaseNode::INIT_OK
,
1894 node
.InitByClientTagLookup(BOOKMARKS
, client_tag
));
1895 const syncable::Entry
* node_entry
= node
.GetEntry();
1896 const sync_pb::EntitySpecifics
& specifics
= node_entry
->GetSpecifics();
1897 EXPECT_TRUE(specifics
.has_encrypted());
1898 EXPECT_EQ(kEncryptedString
, node_entry
->GetNonUniqueName());
1899 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1900 EXPECT_TRUE(cryptographer
->CanDecryptUsingDefaultKey(
1901 specifics
.encrypted()));
1903 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS
, client_tag
));
1905 // Manually change to the same data. Should not set is_unsynced.
1907 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1908 WriteNode
node(&trans
);
1909 EXPECT_EQ(BaseNode::INIT_OK
,
1910 node
.InitByClientTagLookup(BOOKMARKS
, client_tag
));
1911 node
.SetEntitySpecifics(entity_specifics
);
1912 const syncable::Entry
* node_entry
= node
.GetEntry();
1913 const sync_pb::EntitySpecifics
& specifics
= node_entry
->GetSpecifics();
1914 EXPECT_TRUE(specifics
.has_encrypted());
1915 EXPECT_FALSE(node_entry
->GetIsUnsynced());
1916 EXPECT_EQ(kEncryptedString
, node_entry
->GetNonUniqueName());
1917 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1918 EXPECT_TRUE(cryptographer
->CanDecryptUsingDefaultKey(
1919 specifics
.encrypted()));
1921 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS
, client_tag
));
1923 // Manually change to different data. Should set is_unsynced.
1925 entity_specifics
.mutable_bookmark()->set_url("url2");
1926 entity_specifics
.mutable_bookmark()->set_title("title2");
1927 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1928 WriteNode
node(&trans
);
1929 EXPECT_EQ(BaseNode::INIT_OK
,
1930 node
.InitByClientTagLookup(BOOKMARKS
, client_tag
));
1931 node
.SetEntitySpecifics(entity_specifics
);
1932 const syncable::Entry
* node_entry
= node
.GetEntry();
1933 const sync_pb::EntitySpecifics
& specifics
= node_entry
->GetSpecifics();
1934 EXPECT_TRUE(specifics
.has_encrypted());
1935 EXPECT_TRUE(node_entry
->GetIsUnsynced());
1936 EXPECT_EQ(kEncryptedString
, node_entry
->GetNonUniqueName());
1937 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1938 EXPECT_TRUE(cryptographer
->CanDecryptUsingDefaultKey(
1939 specifics
.encrypted()));
1943 // Passwords have their own handling for encryption. Verify it does not result
1944 // in unnecessary writes via SetEntitySpecifics.
1945 TEST_F(SyncManagerTest
, UpdatePasswordSetEntitySpecificsNoChange
) {
1946 std::string client_tag
= "title";
1947 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, DEFAULT_ENCRYPTION
));
1948 sync_pb::EntitySpecifics entity_specifics
;
1950 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1951 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1952 sync_pb::PasswordSpecificsData data
;
1953 data
.set_password_value("secret");
1954 cryptographer
->Encrypt(
1956 entity_specifics
.mutable_password()->
1957 mutable_encrypted());
1959 MakeServerNode(sync_manager_
.GetUserShare(), PASSWORDS
, client_tag
,
1960 syncable::GenerateSyncableHash(PASSWORDS
,
1963 // New node shouldn't start off unsynced.
1964 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS
, client_tag
));
1966 // Manually change to the same data via SetEntitySpecifics. Should not set
1969 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1970 WriteNode
node(&trans
);
1971 EXPECT_EQ(BaseNode::INIT_OK
,
1972 node
.InitByClientTagLookup(PASSWORDS
, client_tag
));
1973 node
.SetEntitySpecifics(entity_specifics
);
1975 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS
, client_tag
));
1978 // Passwords have their own handling for encryption. Verify it does not result
1979 // in unnecessary writes via SetPasswordSpecifics.
1980 TEST_F(SyncManagerTest
, UpdatePasswordSetPasswordSpecifics
) {
1981 std::string client_tag
= "title";
1982 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, DEFAULT_ENCRYPTION
));
1983 sync_pb::EntitySpecifics entity_specifics
;
1985 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
1986 Cryptographer
* cryptographer
= trans
.GetCryptographer();
1987 sync_pb::PasswordSpecificsData data
;
1988 data
.set_password_value("secret");
1989 cryptographer
->Encrypt(
1991 entity_specifics
.mutable_password()->
1992 mutable_encrypted());
1994 MakeServerNode(sync_manager_
.GetUserShare(), PASSWORDS
, client_tag
,
1995 syncable::GenerateSyncableHash(PASSWORDS
,
1998 // New node shouldn't start off unsynced.
1999 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS
, client_tag
));
2001 // Manually change to the same data via SetPasswordSpecifics. Should not set
2004 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2005 WriteNode
node(&trans
);
2006 EXPECT_EQ(BaseNode::INIT_OK
,
2007 node
.InitByClientTagLookup(PASSWORDS
, client_tag
));
2008 node
.SetPasswordSpecifics(node
.GetPasswordSpecifics());
2010 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS
, client_tag
));
2012 // Manually change to different data. Should set is_unsynced.
2014 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2015 WriteNode
node(&trans
);
2016 EXPECT_EQ(BaseNode::INIT_OK
,
2017 node
.InitByClientTagLookup(PASSWORDS
, client_tag
));
2018 Cryptographer
* cryptographer
= trans
.GetCryptographer();
2019 sync_pb::PasswordSpecificsData data
;
2020 data
.set_password_value("secret2");
2021 cryptographer
->Encrypt(
2023 entity_specifics
.mutable_password()->mutable_encrypted());
2024 node
.SetPasswordSpecifics(data
);
2025 const syncable::Entry
* node_entry
= node
.GetEntry();
2026 EXPECT_TRUE(node_entry
->GetIsUnsynced());
2030 // Passwords have their own handling for encryption. Verify setting a new
2031 // passphrase updates the data.
2032 TEST_F(SyncManagerTest
, UpdatePasswordNewPassphrase
) {
2033 std::string client_tag
= "title";
2034 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, DEFAULT_ENCRYPTION
));
2035 sync_pb::EntitySpecifics entity_specifics
;
2037 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2038 Cryptographer
* cryptographer
= trans
.GetCryptographer();
2039 sync_pb::PasswordSpecificsData data
;
2040 data
.set_password_value("secret");
2041 cryptographer
->Encrypt(
2043 entity_specifics
.mutable_password()->mutable_encrypted());
2045 MakeServerNode(sync_manager_
.GetUserShare(), PASSWORDS
, client_tag
,
2046 syncable::GenerateSyncableHash(PASSWORDS
,
2049 // New node shouldn't start off unsynced.
2050 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS
, client_tag
));
2052 // Set a new passphrase. Should set is_unsynced.
2053 testing::Mock::VerifyAndClearExpectations(&encryption_observer_
);
2054 EXPECT_CALL(encryption_observer_
,
2055 OnBootstrapTokenUpdated(_
, PASSPHRASE_BOOTSTRAP_TOKEN
));
2056 ExpectPassphraseAcceptance();
2057 SetCustomPassphraseAndCheck("new_passphrase");
2058 EXPECT_TRUE(ResetUnsyncedEntry(PASSWORDS
, client_tag
));
2061 // Passwords have their own handling for encryption. Verify it does not result
2062 // in unnecessary writes via ReencryptEverything.
2063 TEST_F(SyncManagerTest
, UpdatePasswordReencryptEverything
) {
2064 std::string client_tag
= "title";
2065 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, DEFAULT_ENCRYPTION
));
2066 sync_pb::EntitySpecifics entity_specifics
;
2068 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2069 Cryptographer
* cryptographer
= trans
.GetCryptographer();
2070 sync_pb::PasswordSpecificsData data
;
2071 data
.set_password_value("secret");
2072 cryptographer
->Encrypt(
2074 entity_specifics
.mutable_password()->mutable_encrypted());
2076 MakeServerNode(sync_manager_
.GetUserShare(), PASSWORDS
, client_tag
,
2077 syncable::GenerateSyncableHash(PASSWORDS
,
2080 // New node shouldn't start off unsynced.
2081 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS
, client_tag
));
2083 // Force a re-encrypt everything. Should not set is_unsynced.
2084 testing::Mock::VerifyAndClearExpectations(&encryption_observer_
);
2085 EXPECT_CALL(encryption_observer_
, OnEncryptionComplete());
2086 EXPECT_CALL(encryption_observer_
, OnCryptographerStateChanged(_
));
2087 EXPECT_CALL(encryption_observer_
, OnEncryptedTypesChanged(_
, false));
2088 sync_manager_
.GetEncryptionHandler()->Init();
2090 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS
, client_tag
));
2093 // Test that attempting to start up with corrupted password data triggers
2094 // an unrecoverable error (rather than crashing).
2095 TEST_F(SyncManagerTest
, ReencryptEverythingWithUnrecoverableErrorPasswords
) {
2096 const char kClientTag
[] = "client_tag";
2098 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, DEFAULT_ENCRYPTION
));
2099 sync_pb::EntitySpecifics entity_specifics
;
2101 // Create a synced bookmark with undecryptable data.
2102 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2104 Cryptographer
other_cryptographer(&encryptor_
);
2105 KeyParams fake_params
= {"localhost", "dummy", "fake_key"};
2106 other_cryptographer
.AddKey(fake_params
);
2107 sync_pb::PasswordSpecificsData data
;
2108 data
.set_password_value("secret");
2109 other_cryptographer
.Encrypt(
2111 entity_specifics
.mutable_password()->mutable_encrypted());
2113 // Set up the real cryptographer with a different key.
2114 KeyParams real_params
= {"localhost", "username", "real_key"};
2115 trans
.GetCryptographer()->AddKey(real_params
);
2117 MakeServerNode(sync_manager_
.GetUserShare(), PASSWORDS
, kClientTag
,
2118 syncable::GenerateSyncableHash(PASSWORDS
,
2121 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS
, kClientTag
));
2123 // Force a re-encrypt everything. Should trigger an unrecoverable error due
2124 // to being unable to decrypt the data that was previously applied.
2125 testing::Mock::VerifyAndClearExpectations(&encryption_observer_
);
2126 EXPECT_CALL(encryption_observer_
, OnEncryptionComplete());
2127 EXPECT_CALL(encryption_observer_
, OnCryptographerStateChanged(_
));
2128 EXPECT_CALL(encryption_observer_
, OnEncryptedTypesChanged(_
, false));
2129 EXPECT_FALSE(HasUnrecoverableError());
2130 sync_manager_
.GetEncryptionHandler()->Init();
2132 EXPECT_TRUE(HasUnrecoverableError());
2135 // Test that attempting to start up with corrupted bookmark data triggers
2136 // an unrecoverable error (rather than crashing).
2137 TEST_F(SyncManagerTest
, ReencryptEverythingWithUnrecoverableErrorBookmarks
) {
2138 const char kClientTag
[] = "client_tag";
2139 EXPECT_CALL(encryption_observer_
,
2140 OnEncryptedTypesChanged(
2141 HasModelTypes(EncryptableUserTypes()), true));
2142 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, FULL_ENCRYPTION
));
2143 sync_pb::EntitySpecifics entity_specifics
;
2145 // Create a synced bookmark with undecryptable data.
2146 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2148 Cryptographer
other_cryptographer(&encryptor_
);
2149 KeyParams fake_params
= {"localhost", "dummy", "fake_key"};
2150 other_cryptographer
.AddKey(fake_params
);
2151 sync_pb::EntitySpecifics bm_specifics
;
2152 bm_specifics
.mutable_bookmark()->set_title("title");
2153 bm_specifics
.mutable_bookmark()->set_url("url");
2154 sync_pb::EncryptedData encrypted
;
2155 other_cryptographer
.Encrypt(bm_specifics
, &encrypted
);
2156 entity_specifics
.mutable_encrypted()->CopyFrom(encrypted
);
2158 // Set up the real cryptographer with a different key.
2159 KeyParams real_params
= {"localhost", "username", "real_key"};
2160 trans
.GetCryptographer()->AddKey(real_params
);
2162 MakeServerNode(sync_manager_
.GetUserShare(), BOOKMARKS
, kClientTag
,
2163 syncable::GenerateSyncableHash(BOOKMARKS
,
2166 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS
, kClientTag
));
2168 // Force a re-encrypt everything. Should trigger an unrecoverable error due
2169 // to being unable to decrypt the data that was previously applied.
2170 testing::Mock::VerifyAndClearExpectations(&encryption_observer_
);
2171 EXPECT_CALL(encryption_observer_
, OnEncryptionComplete());
2172 EXPECT_CALL(encryption_observer_
, OnCryptographerStateChanged(_
));
2173 EXPECT_CALL(encryption_observer_
, OnEncryptedTypesChanged(_
, true));
2174 EXPECT_FALSE(HasUnrecoverableError());
2175 sync_manager_
.GetEncryptionHandler()->Init();
2177 EXPECT_TRUE(HasUnrecoverableError());
2180 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for bookmarks
2181 // when we write the same data, but does set it when we write new data.
2182 TEST_F(SyncManagerTest
, SetBookmarkTitle
) {
2183 std::string client_tag
= "title";
2184 sync_pb::EntitySpecifics entity_specifics
;
2185 entity_specifics
.mutable_bookmark()->set_url("url");
2186 entity_specifics
.mutable_bookmark()->set_title("title");
2187 MakeServerNode(sync_manager_
.GetUserShare(), BOOKMARKS
, client_tag
,
2188 syncable::GenerateSyncableHash(BOOKMARKS
,
2191 // New node shouldn't start off unsynced.
2192 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS
, client_tag
));
2194 // Manually change to the same title. Should not set is_unsynced.
2196 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2197 WriteNode
node(&trans
);
2198 EXPECT_EQ(BaseNode::INIT_OK
,
2199 node
.InitByClientTagLookup(BOOKMARKS
, client_tag
));
2200 node
.SetTitle(client_tag
);
2202 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS
, client_tag
));
2204 // Manually change to new title. Should set is_unsynced.
2206 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2207 WriteNode
node(&trans
);
2208 EXPECT_EQ(BaseNode::INIT_OK
,
2209 node
.InitByClientTagLookup(BOOKMARKS
, client_tag
));
2210 node
.SetTitle("title2");
2212 EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS
, client_tag
));
2215 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for encrypted
2216 // bookmarks when we write the same data, but does set it when we write new
2218 TEST_F(SyncManagerTest
, SetBookmarkTitleWithEncryption
) {
2219 std::string client_tag
= "title";
2220 sync_pb::EntitySpecifics entity_specifics
;
2221 entity_specifics
.mutable_bookmark()->set_url("url");
2222 entity_specifics
.mutable_bookmark()->set_title("title");
2223 MakeServerNode(sync_manager_
.GetUserShare(), BOOKMARKS
, client_tag
,
2224 syncable::GenerateSyncableHash(BOOKMARKS
,
2227 // New node shouldn't start off unsynced.
2228 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS
, client_tag
));
2230 // Encrypt the datatatype, should set is_unsynced.
2231 EXPECT_CALL(encryption_observer_
,
2232 OnEncryptedTypesChanged(
2233 HasModelTypes(EncryptableUserTypes()), true));
2234 EXPECT_CALL(encryption_observer_
, OnEncryptionComplete());
2235 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, FULL_ENCRYPTION
));
2236 EXPECT_CALL(encryption_observer_
, OnCryptographerStateChanged(_
));
2237 EXPECT_CALL(encryption_observer_
, OnEncryptedTypesChanged(_
, true));
2238 sync_manager_
.GetEncryptionHandler()->Init();
2240 EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS
, client_tag
));
2242 // Manually change to the same title. Should not set is_unsynced.
2243 // NON_UNIQUE_NAME should be kEncryptedString.
2245 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2246 WriteNode
node(&trans
);
2247 EXPECT_EQ(BaseNode::INIT_OK
,
2248 node
.InitByClientTagLookup(BOOKMARKS
, client_tag
));
2249 node
.SetTitle(client_tag
);
2250 const syncable::Entry
* node_entry
= node
.GetEntry();
2251 const sync_pb::EntitySpecifics
& specifics
= node_entry
->GetSpecifics();
2252 EXPECT_TRUE(specifics
.has_encrypted());
2253 EXPECT_EQ(kEncryptedString
, node_entry
->GetNonUniqueName());
2255 EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS
, client_tag
));
2257 // Manually change to new title. Should set is_unsynced. NON_UNIQUE_NAME
2258 // should still be kEncryptedString.
2260 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2261 WriteNode
node(&trans
);
2262 EXPECT_EQ(BaseNode::INIT_OK
,
2263 node
.InitByClientTagLookup(BOOKMARKS
, client_tag
));
2264 node
.SetTitle("title2");
2265 const syncable::Entry
* node_entry
= node
.GetEntry();
2266 const sync_pb::EntitySpecifics
& specifics
= node_entry
->GetSpecifics();
2267 EXPECT_TRUE(specifics
.has_encrypted());
2268 EXPECT_EQ(kEncryptedString
, node_entry
->GetNonUniqueName());
2270 EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS
, client_tag
));
2273 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for non-bookmarks
2274 // when we write the same data, but does set it when we write new data.
2275 TEST_F(SyncManagerTest
, SetNonBookmarkTitle
) {
2276 std::string client_tag
= "title";
2277 sync_pb::EntitySpecifics entity_specifics
;
2278 entity_specifics
.mutable_preference()->set_name("name");
2279 entity_specifics
.mutable_preference()->set_value("value");
2280 MakeServerNode(sync_manager_
.GetUserShare(),
2283 syncable::GenerateSyncableHash(PREFERENCES
,
2286 // New node shouldn't start off unsynced.
2287 EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES
, client_tag
));
2289 // Manually change to the same title. Should not set is_unsynced.
2291 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2292 WriteNode
node(&trans
);
2293 EXPECT_EQ(BaseNode::INIT_OK
,
2294 node
.InitByClientTagLookup(PREFERENCES
, client_tag
));
2295 node
.SetTitle(client_tag
);
2297 EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES
, client_tag
));
2299 // Manually change to new title. Should set is_unsynced.
2301 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2302 WriteNode
node(&trans
);
2303 EXPECT_EQ(BaseNode::INIT_OK
,
2304 node
.InitByClientTagLookup(PREFERENCES
, client_tag
));
2305 node
.SetTitle("title2");
2307 EXPECT_TRUE(ResetUnsyncedEntry(PREFERENCES
, client_tag
));
2310 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for encrypted
2311 // non-bookmarks when we write the same data or when we write new data
2312 // data (should remained kEncryptedString).
2313 TEST_F(SyncManagerTest
, SetNonBookmarkTitleWithEncryption
) {
2314 std::string client_tag
= "title";
2315 sync_pb::EntitySpecifics entity_specifics
;
2316 entity_specifics
.mutable_preference()->set_name("name");
2317 entity_specifics
.mutable_preference()->set_value("value");
2318 MakeServerNode(sync_manager_
.GetUserShare(),
2321 syncable::GenerateSyncableHash(PREFERENCES
,
2324 // New node shouldn't start off unsynced.
2325 EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES
, client_tag
));
2327 // Encrypt the datatatype, should set is_unsynced.
2328 EXPECT_CALL(encryption_observer_
,
2329 OnEncryptedTypesChanged(
2330 HasModelTypes(EncryptableUserTypes()), true));
2331 EXPECT_CALL(encryption_observer_
, OnEncryptionComplete());
2332 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, FULL_ENCRYPTION
));
2333 EXPECT_CALL(encryption_observer_
, OnCryptographerStateChanged(_
));
2334 EXPECT_CALL(encryption_observer_
, OnEncryptedTypesChanged(_
, true));
2335 sync_manager_
.GetEncryptionHandler()->Init();
2337 EXPECT_TRUE(ResetUnsyncedEntry(PREFERENCES
, client_tag
));
2339 // Manually change to the same title. Should not set is_unsynced.
2340 // NON_UNIQUE_NAME should be kEncryptedString.
2342 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2343 WriteNode
node(&trans
);
2344 EXPECT_EQ(BaseNode::INIT_OK
,
2345 node
.InitByClientTagLookup(PREFERENCES
, client_tag
));
2346 node
.SetTitle(client_tag
);
2347 const syncable::Entry
* node_entry
= node
.GetEntry();
2348 const sync_pb::EntitySpecifics
& specifics
= node_entry
->GetSpecifics();
2349 EXPECT_TRUE(specifics
.has_encrypted());
2350 EXPECT_EQ(kEncryptedString
, node_entry
->GetNonUniqueName());
2352 EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES
, client_tag
));
2354 // Manually change to new title. Should not set is_unsynced because the
2355 // NON_UNIQUE_NAME should still be kEncryptedString.
2357 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2358 WriteNode
node(&trans
);
2359 EXPECT_EQ(BaseNode::INIT_OK
,
2360 node
.InitByClientTagLookup(PREFERENCES
, client_tag
));
2361 node
.SetTitle("title2");
2362 const syncable::Entry
* node_entry
= node
.GetEntry();
2363 const sync_pb::EntitySpecifics
& specifics
= node_entry
->GetSpecifics();
2364 EXPECT_TRUE(specifics
.has_encrypted());
2365 EXPECT_EQ(kEncryptedString
, node_entry
->GetNonUniqueName());
2366 EXPECT_FALSE(node_entry
->GetIsUnsynced());
2370 // Ensure that titles are truncated to 255 bytes, and attempting to reset
2371 // them to their longer version does not set IS_UNSYNCED.
2372 TEST_F(SyncManagerTest
, SetLongTitle
) {
2373 const int kNumChars
= 512;
2374 const std::string kClientTag
= "tag";
2375 std::string
title(kNumChars
, '0');
2376 sync_pb::EntitySpecifics entity_specifics
;
2377 entity_specifics
.mutable_preference()->set_name("name");
2378 entity_specifics
.mutable_preference()->set_value("value");
2379 MakeServerNode(sync_manager_
.GetUserShare(),
2382 syncable::GenerateSyncableHash(PREFERENCES
,
2385 // New node shouldn't start off unsynced.
2386 EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES
, kClientTag
));
2388 // Manually change to the long title. Should set is_unsynced.
2390 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2391 WriteNode
node(&trans
);
2392 EXPECT_EQ(BaseNode::INIT_OK
,
2393 node
.InitByClientTagLookup(PREFERENCES
, kClientTag
));
2394 node
.SetTitle(title
);
2395 EXPECT_EQ(node
.GetTitle(), title
.substr(0, 255));
2397 EXPECT_TRUE(ResetUnsyncedEntry(PREFERENCES
, kClientTag
));
2399 // Manually change to the same title. Should not set is_unsynced.
2401 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2402 WriteNode
node(&trans
);
2403 EXPECT_EQ(BaseNode::INIT_OK
,
2404 node
.InitByClientTagLookup(PREFERENCES
, kClientTag
));
2405 node
.SetTitle(title
);
2406 EXPECT_EQ(node
.GetTitle(), title
.substr(0, 255));
2408 EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES
, kClientTag
));
2410 // Manually change to new title. Should set is_unsynced.
2412 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2413 WriteNode
node(&trans
);
2414 EXPECT_EQ(BaseNode::INIT_OK
,
2415 node
.InitByClientTagLookup(PREFERENCES
, kClientTag
));
2416 node
.SetTitle("title2");
2418 EXPECT_TRUE(ResetUnsyncedEntry(PREFERENCES
, kClientTag
));
2421 // Create an encrypted entry when the cryptographer doesn't think the type is
2422 // marked for encryption. Ensure reads/writes don't break and don't unencrypt
2424 TEST_F(SyncManagerTest
, SetPreviouslyEncryptedSpecifics
) {
2425 std::string client_tag
= "tag";
2426 std::string url
= "url";
2427 std::string url2
= "new_url";
2428 std::string title
= "title";
2429 sync_pb::EntitySpecifics entity_specifics
;
2430 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI
, DEFAULT_ENCRYPTION
));
2432 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2433 Cryptographer
* crypto
= trans
.GetCryptographer();
2434 sync_pb::EntitySpecifics bm_specifics
;
2435 bm_specifics
.mutable_bookmark()->set_title("title");
2436 bm_specifics
.mutable_bookmark()->set_url("url");
2437 sync_pb::EncryptedData encrypted
;
2438 crypto
->Encrypt(bm_specifics
, &encrypted
);
2439 entity_specifics
.mutable_encrypted()->CopyFrom(encrypted
);
2440 AddDefaultFieldValue(BOOKMARKS
, &entity_specifics
);
2442 MakeServerNode(sync_manager_
.GetUserShare(), BOOKMARKS
, client_tag
,
2443 syncable::GenerateSyncableHash(BOOKMARKS
,
2449 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2450 ReadNode
node(&trans
);
2451 EXPECT_EQ(BaseNode::INIT_OK
,
2452 node
.InitByClientTagLookup(BOOKMARKS
, client_tag
));
2453 EXPECT_EQ(title
, node
.GetTitle());
2454 EXPECT_EQ(url
, node
.GetBookmarkSpecifics().url());
2458 // Overwrite the url (which overwrites the specifics).
2459 WriteTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2460 WriteNode
node(&trans
);
2461 EXPECT_EQ(BaseNode::INIT_OK
,
2462 node
.InitByClientTagLookup(BOOKMARKS
, client_tag
));
2464 sync_pb::BookmarkSpecifics
bookmark_specifics(node
.GetBookmarkSpecifics());
2465 bookmark_specifics
.set_url(url2
);
2466 node
.SetBookmarkSpecifics(bookmark_specifics
);
2470 // Verify it's still encrypted and it has the most recent url.
2471 ReadTransaction
trans(FROM_HERE
, sync_manager_
.GetUserShare());
2472 ReadNode
node(&trans
);
2473 EXPECT_EQ(BaseNode::INIT_OK
,
2474 node
.InitByClientTagLookup(BOOKMARKS
, client_tag
));
2475 EXPECT_EQ(title
, node
.GetTitle());
2476 EXPECT_EQ(url2
, node
.GetBookmarkSpecifics().url());
2477 const syncable::Entry
* node_entry
= node
.GetEntry();
2478 EXPECT_EQ(kEncryptedString
, node_entry
->GetNonUniqueName());
2479 const sync_pb::EntitySpecifics
& specifics
= node_entry
->GetSpecifics();
2480 EXPECT_TRUE(specifics
.has_encrypted());
2484 // Verify transaction version of a model type is incremented when node of
2485 // that type is updated.
2486 TEST_F(SyncManagerTest
, IncrementTransactionVersion
) {
2487 ModelSafeRoutingInfo routing_info
;
2488 GetModelSafeRoutingInfo(&routing_info
);
2491 ReadTransaction
read_trans(FROM_HERE
, sync_manager_
.GetUserShare());
2492 for (ModelSafeRoutingInfo::iterator i
= routing_info
.begin();
2493 i
!= routing_info
.end(); ++i
) {
2494 // Transaction version is incremented when SyncManagerTest::SetUp()
2495 // creates a node of each type.
2497 sync_manager_
.GetUserShare()->directory
->
2498 GetTransactionVersion(i
->first
));
2502 // Create bookmark node to increment transaction version of bookmark model.
2503 std::string client_tag
= "title";
2504 sync_pb::EntitySpecifics entity_specifics
;
2505 entity_specifics
.mutable_bookmark()->set_url("url");
2506 entity_specifics
.mutable_bookmark()->set_title("title");
2507 MakeServerNode(sync_manager_
.GetUserShare(), BOOKMARKS
, client_tag
,
2508 syncable::GenerateSyncableHash(BOOKMARKS
,
2513 ReadTransaction
read_trans(FROM_HERE
, sync_manager_
.GetUserShare());
2514 for (ModelSafeRoutingInfo::iterator i
= routing_info
.begin();
2515 i
!= routing_info
.end(); ++i
) {
2516 EXPECT_EQ(i
->first
== BOOKMARKS
? 2 : 1,
2517 sync_manager_
.GetUserShare()->directory
->
2518 GetTransactionVersion(i
->first
));
2523 class MockSyncScheduler
: public FakeSyncScheduler
{
2525 MockSyncScheduler() : FakeSyncScheduler() {}
2526 virtual ~MockSyncScheduler() {}
2528 MOCK_METHOD2(Start
, void(SyncScheduler::Mode
, base::Time
));
2529 MOCK_METHOD1(ScheduleConfiguration
, void(const ConfigurationParams
&));
2532 class ComponentsFactory
: public TestInternalComponentsFactory
{
2534 ComponentsFactory(const Switches
& switches
,
2535 SyncScheduler
* scheduler_to_use
,
2536 sessions::SyncSessionContext
** session_context
,
2537 InternalComponentsFactory::StorageOption
* storage_used
)
2538 : TestInternalComponentsFactory(
2539 switches
, InternalComponentsFactory::STORAGE_IN_MEMORY
, storage_used
),
2540 scheduler_to_use_(scheduler_to_use
),
2541 session_context_(session_context
) {}
2542 ~ComponentsFactory() override
{}
2544 scoped_ptr
<SyncScheduler
> BuildScheduler(
2545 const std::string
& name
,
2546 sessions::SyncSessionContext
* context
,
2547 CancelationSignal
* stop_handle
) override
{
2548 *session_context_
= context
;
2549 return scheduler_to_use_
.Pass();
2553 scoped_ptr
<SyncScheduler
> scheduler_to_use_
;
2554 sessions::SyncSessionContext
** session_context_
;
2557 class SyncManagerTestWithMockScheduler
: public SyncManagerTest
{
2559 SyncManagerTestWithMockScheduler() : scheduler_(NULL
) {}
2560 InternalComponentsFactory
* GetFactory() override
{
2561 scheduler_
= new MockSyncScheduler();
2562 return new ComponentsFactory(GetSwitches(), scheduler_
, &session_context_
,
2566 MockSyncScheduler
* scheduler() { return scheduler_
; }
2567 sessions::SyncSessionContext
* session_context() {
2568 return session_context_
;
2572 MockSyncScheduler
* scheduler_
;
2573 sessions::SyncSessionContext
* session_context_
;
2576 // Test that the configuration params are properly created and sent to
2577 // ScheduleConfigure. No callback should be invoked. Any disabled datatypes
2578 // should be purged.
2579 TEST_F(SyncManagerTestWithMockScheduler
, BasicConfiguration
) {
2580 ConfigureReason reason
= CONFIGURE_REASON_RECONFIGURATION
;
2581 ModelTypeSet
types_to_download(BOOKMARKS
, PREFERENCES
);
2582 ModelSafeRoutingInfo new_routing_info
;
2583 GetModelSafeRoutingInfo(&new_routing_info
);
2584 ModelTypeSet enabled_types
= GetRoutingInfoTypes(new_routing_info
);
2585 ModelTypeSet disabled_types
= Difference(ModelTypeSet::All(), enabled_types
);
2587 ConfigurationParams params
;
2588 EXPECT_CALL(*scheduler(), Start(SyncScheduler::CONFIGURATION_MODE
, _
));
2589 EXPECT_CALL(*scheduler(), ScheduleConfiguration(_
)).
2590 WillOnce(SaveArg
<0>(¶ms
));
2592 // Set data for all types.
2593 ModelTypeSet protocol_types
= ProtocolTypes();
2594 for (ModelTypeSet::Iterator iter
= protocol_types
.First(); iter
.Good();
2596 SetProgressMarkerForType(iter
.Get(), true);
2599 CallbackCounter ready_task_counter
, retry_task_counter
;
2600 sync_manager_
.ConfigureSyncer(
2607 base::Bind(&CallbackCounter::Callback
,
2608 base::Unretained(&ready_task_counter
)),
2609 base::Bind(&CallbackCounter::Callback
,
2610 base::Unretained(&retry_task_counter
)));
2611 EXPECT_EQ(0, ready_task_counter
.times_called());
2612 EXPECT_EQ(0, retry_task_counter
.times_called());
2613 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION
,
2615 EXPECT_TRUE(types_to_download
.Equals(params
.types_to_download
));
2616 EXPECT_EQ(new_routing_info
, params
.routing_info
);
2618 // Verify all the disabled types were purged.
2619 EXPECT_TRUE(sync_manager_
.InitialSyncEndedTypes().Equals(
2621 EXPECT_TRUE(sync_manager_
.GetTypesWithEmptyProgressMarkerToken(
2622 ModelTypeSet::All()).Equals(disabled_types
));
2625 // Test that on a reconfiguration (configuration where the session context
2626 // already has routing info), only those recently disabled types are purged.
2627 TEST_F(SyncManagerTestWithMockScheduler
, ReConfiguration
) {
2628 ConfigureReason reason
= CONFIGURE_REASON_RECONFIGURATION
;
2629 ModelTypeSet
types_to_download(BOOKMARKS
, PREFERENCES
);
2630 ModelTypeSet disabled_types
= ModelTypeSet(THEMES
, SESSIONS
);
2631 ModelSafeRoutingInfo old_routing_info
;
2632 ModelSafeRoutingInfo new_routing_info
;
2633 GetModelSafeRoutingInfo(&old_routing_info
);
2634 new_routing_info
= old_routing_info
;
2635 new_routing_info
.erase(THEMES
);
2636 new_routing_info
.erase(SESSIONS
);
2637 ModelTypeSet enabled_types
= GetRoutingInfoTypes(new_routing_info
);
2639 ConfigurationParams params
;
2640 EXPECT_CALL(*scheduler(), Start(SyncScheduler::CONFIGURATION_MODE
, _
));
2641 EXPECT_CALL(*scheduler(), ScheduleConfiguration(_
)).
2642 WillOnce(SaveArg
<0>(¶ms
));
2644 // Set data for all types except those recently disabled (so we can verify
2645 // only those recently disabled are purged) .
2646 ModelTypeSet protocol_types
= ProtocolTypes();
2647 for (ModelTypeSet::Iterator iter
= protocol_types
.First(); iter
.Good();
2649 if (!disabled_types
.Has(iter
.Get())) {
2650 SetProgressMarkerForType(iter
.Get(), true);
2652 SetProgressMarkerForType(iter
.Get(), false);
2656 // Set the context to have the old routing info.
2657 session_context()->SetRoutingInfo(old_routing_info
);
2659 CallbackCounter ready_task_counter
, retry_task_counter
;
2660 sync_manager_
.ConfigureSyncer(
2667 base::Bind(&CallbackCounter::Callback
,
2668 base::Unretained(&ready_task_counter
)),
2669 base::Bind(&CallbackCounter::Callback
,
2670 base::Unretained(&retry_task_counter
)));
2671 EXPECT_EQ(0, ready_task_counter
.times_called());
2672 EXPECT_EQ(0, retry_task_counter
.times_called());
2673 EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION
,
2675 EXPECT_TRUE(types_to_download
.Equals(params
.types_to_download
));
2676 EXPECT_EQ(new_routing_info
, params
.routing_info
);
2678 // Verify only the recently disabled types were purged.
2679 EXPECT_TRUE(sync_manager_
.GetTypesWithEmptyProgressMarkerToken(
2680 ProtocolTypes()).Equals(disabled_types
));
2683 // Test that SyncManager::ClearServerData invokes the scheduler.
2684 TEST_F(SyncManagerTestWithMockScheduler
, ClearServerData
) {
2685 EXPECT_CALL(*scheduler(), Start(SyncScheduler::CLEAR_SERVER_DATA_MODE
, _
));
2686 CallbackCounter callback_counter
;
2687 sync_manager_
.ClearServerData(base::Bind(
2688 &CallbackCounter::Callback
, base::Unretained(&callback_counter
)));
2690 EXPECT_EQ(1, callback_counter
.times_called());
2693 // Test that PurgePartiallySyncedTypes purges only those types that have not
2694 // fully completed their initial download and apply.
2695 TEST_F(SyncManagerTest
, PurgePartiallySyncedTypes
) {
2696 ModelSafeRoutingInfo routing_info
;
2697 GetModelSafeRoutingInfo(&routing_info
);
2698 ModelTypeSet enabled_types
= GetRoutingInfoTypes(routing_info
);
2700 UserShare
* share
= sync_manager_
.GetUserShare();
2702 // The test harness automatically initializes all types in the routing info.
2703 // Check that autofill is not among them.
2704 ASSERT_FALSE(enabled_types
.Has(AUTOFILL
));
2706 // Further ensure that the test harness did not create its root node.
2708 syncable::ReadTransaction
trans(FROM_HERE
, share
->directory
.get());
2709 syncable::Entry
autofill_root_node(&trans
,
2710 syncable::GET_TYPE_ROOT
,
2712 ASSERT_FALSE(autofill_root_node
.good());
2715 // One more redundant check.
2716 ASSERT_FALSE(sync_manager_
.InitialSyncEndedTypes().Has(AUTOFILL
));
2718 // Give autofill a progress marker.
2719 sync_pb::DataTypeProgressMarker autofill_marker
;
2720 autofill_marker
.set_data_type_id(
2721 GetSpecificsFieldNumberFromModelType(AUTOFILL
));
2722 autofill_marker
.set_token("token");
2723 share
->directory
->SetDownloadProgress(AUTOFILL
, autofill_marker
);
2725 // Also add a pending autofill root node update from the server.
2726 TestEntryFactory
factory_(share
->directory
.get());
2727 int autofill_meta
= factory_
.CreateUnappliedRootNode(AUTOFILL
);
2729 // Preferences is an enabled type. Check that the harness initialized it.
2730 ASSERT_TRUE(enabled_types
.Has(PREFERENCES
));
2731 ASSERT_TRUE(sync_manager_
.InitialSyncEndedTypes().Has(PREFERENCES
));
2733 // Give preferencse a progress marker.
2734 sync_pb::DataTypeProgressMarker prefs_marker
;
2735 prefs_marker
.set_data_type_id(
2736 GetSpecificsFieldNumberFromModelType(PREFERENCES
));
2737 prefs_marker
.set_token("token");
2738 share
->directory
->SetDownloadProgress(PREFERENCES
, prefs_marker
);
2740 // Add a fully synced preferences node under the root.
2741 std::string pref_client_tag
= "prefABC";
2742 std::string pref_hashed_tag
= "hashXYZ";
2743 sync_pb::EntitySpecifics pref_specifics
;
2744 AddDefaultFieldValue(PREFERENCES
, &pref_specifics
);
2745 int pref_meta
= MakeServerNode(
2746 share
, PREFERENCES
, pref_client_tag
, pref_hashed_tag
, pref_specifics
);
2748 // And now, the purge.
2749 EXPECT_TRUE(sync_manager_
.PurgePartiallySyncedTypes());
2751 // Ensure that autofill lost its progress marker, but preferences did not.
2752 ModelTypeSet empty_tokens
=
2753 sync_manager_
.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All());
2754 EXPECT_TRUE(empty_tokens
.Has(AUTOFILL
));
2755 EXPECT_FALSE(empty_tokens
.Has(PREFERENCES
));
2757 // Ensure that autofill lots its node, but preferences did not.
2759 syncable::ReadTransaction
trans(FROM_HERE
, share
->directory
.get());
2760 syncable::Entry
autofill_node(&trans
, GET_BY_HANDLE
, autofill_meta
);
2761 syncable::Entry
pref_node(&trans
, GET_BY_HANDLE
, pref_meta
);
2762 EXPECT_FALSE(autofill_node
.good());
2763 EXPECT_TRUE(pref_node
.good());
2767 // Test CleanupDisabledTypes properly purges all disabled types as specified
2768 // by the previous and current enabled params.
2769 TEST_F(SyncManagerTest
, PurgeDisabledTypes
) {
2770 ModelSafeRoutingInfo routing_info
;
2771 GetModelSafeRoutingInfo(&routing_info
);
2772 ModelTypeSet enabled_types
= GetRoutingInfoTypes(routing_info
);
2773 ModelTypeSet disabled_types
= Difference(ModelTypeSet::All(), enabled_types
);
2775 // The harness should have initialized the enabled_types for us.
2776 EXPECT_TRUE(enabled_types
.Equals(sync_manager_
.InitialSyncEndedTypes()));
2778 // Set progress markers for all types.
2779 ModelTypeSet protocol_types
= ProtocolTypes();
2780 for (ModelTypeSet::Iterator iter
= protocol_types
.First(); iter
.Good();
2782 SetProgressMarkerForType(iter
.Get(), true);
2785 // Verify all the enabled types remain after cleanup, and all the disabled
2786 // types were purged.
2787 sync_manager_
.PurgeDisabledTypes(disabled_types
,
2790 EXPECT_TRUE(enabled_types
.Equals(sync_manager_
.InitialSyncEndedTypes()));
2791 EXPECT_TRUE(disabled_types
.Equals(
2792 sync_manager_
.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All())));
2794 // Disable some more types.
2795 disabled_types
.Put(BOOKMARKS
);
2796 disabled_types
.Put(PREFERENCES
);
2797 ModelTypeSet new_enabled_types
=
2798 Difference(ModelTypeSet::All(), disabled_types
);
2800 // Verify only the non-disabled types remain after cleanup.
2801 sync_manager_
.PurgeDisabledTypes(disabled_types
,
2804 EXPECT_TRUE(new_enabled_types
.Equals(sync_manager_
.InitialSyncEndedTypes()));
2805 EXPECT_TRUE(disabled_types
.Equals(
2806 sync_manager_
.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All())));
2809 // Test PurgeDisabledTypes properly unapplies types by deleting their local data
2810 // and preserving their server data and progress marker.
2811 TEST_F(SyncManagerTest
, PurgeUnappliedTypes
) {
2812 ModelSafeRoutingInfo routing_info
;
2813 GetModelSafeRoutingInfo(&routing_info
);
2814 ModelTypeSet unapplied_types
= ModelTypeSet(BOOKMARKS
, PREFERENCES
);
2815 ModelTypeSet enabled_types
= GetRoutingInfoTypes(routing_info
);
2816 ModelTypeSet disabled_types
= Difference(ModelTypeSet::All(), enabled_types
);
2818 // The harness should have initialized the enabled_types for us.
2819 EXPECT_TRUE(enabled_types
.Equals(sync_manager_
.InitialSyncEndedTypes()));
2821 // Set progress markers for all types.
2822 ModelTypeSet protocol_types
= ProtocolTypes();
2823 for (ModelTypeSet::Iterator iter
= protocol_types
.First(); iter
.Good();
2825 SetProgressMarkerForType(iter
.Get(), true);
2828 // Add the following kinds of items:
2829 // 1. Fully synced preference.
2830 // 2. Locally created preference, server unknown, unsynced
2831 // 3. Locally deleted preference, server known, unsynced
2832 // 4. Server deleted preference, locally known.
2833 // 5. Server created preference, locally unknown, unapplied.
2834 // 6. A fully synced bookmark (no unique_client_tag).
2835 UserShare
* share
= sync_manager_
.GetUserShare();
2836 sync_pb::EntitySpecifics pref_specifics
;
2837 AddDefaultFieldValue(PREFERENCES
, &pref_specifics
);
2838 sync_pb::EntitySpecifics bm_specifics
;
2839 AddDefaultFieldValue(BOOKMARKS
, &bm_specifics
);
2840 int pref1_meta
= MakeServerNode(
2841 share
, PREFERENCES
, "pref1", "hash1", pref_specifics
);
2842 int64 pref2_meta
= MakeNodeWithRoot(share
, PREFERENCES
, "pref2");
2843 int pref3_meta
= MakeServerNode(
2844 share
, PREFERENCES
, "pref3", "hash3", pref_specifics
);
2845 int pref4_meta
= MakeServerNode(
2846 share
, PREFERENCES
, "pref4", "hash4", pref_specifics
);
2847 int pref5_meta
= MakeServerNode(
2848 share
, PREFERENCES
, "pref5", "hash5", pref_specifics
);
2849 int bookmark_meta
= MakeServerNode(
2850 share
, BOOKMARKS
, "bookmark", "", bm_specifics
);
2853 syncable::WriteTransaction
trans(FROM_HERE
,
2855 share
->directory
.get());
2856 // Pref's 1 and 2 are already set up properly.
2857 // Locally delete pref 3.
2858 syncable::MutableEntry
pref3(&trans
, GET_BY_HANDLE
, pref3_meta
);
2859 pref3
.PutIsDel(true);
2860 pref3
.PutIsUnsynced(true);
2861 // Delete pref 4 at the server.
2862 syncable::MutableEntry
pref4(&trans
, GET_BY_HANDLE
, pref4_meta
);
2863 pref4
.PutServerIsDel(true);
2864 pref4
.PutIsUnappliedUpdate(true);
2865 pref4
.PutServerVersion(2);
2866 // Pref 5 is an new unapplied update.
2867 syncable::MutableEntry
pref5(&trans
, GET_BY_HANDLE
, pref5_meta
);
2868 pref5
.PutIsUnappliedUpdate(true);
2869 pref5
.PutIsDel(true);
2870 pref5
.PutBaseVersion(-1);
2871 // Bookmark is already set up properly
2874 // Take a snapshot to clear all the dirty bits.
2875 share
->directory
.get()->SaveChanges();
2877 // Now request a purge for the unapplied types.
2878 disabled_types
.PutAll(unapplied_types
);
2879 sync_manager_
.PurgeDisabledTypes(disabled_types
,
2883 // Verify the unapplied types still have progress markers and initial sync
2884 // ended after cleanup.
2885 EXPECT_TRUE(sync_manager_
.InitialSyncEndedTypes().HasAll(unapplied_types
));
2887 sync_manager_
.GetTypesWithEmptyProgressMarkerToken(unapplied_types
).
2890 // Ensure the items were unapplied as necessary.
2892 syncable::ReadTransaction
trans(FROM_HERE
, share
->directory
.get());
2893 syncable::Entry
pref_node(&trans
, GET_BY_HANDLE
, pref1_meta
);
2894 ASSERT_TRUE(pref_node
.good());
2895 EXPECT_TRUE(pref_node
.GetKernelCopy().is_dirty());
2896 EXPECT_FALSE(pref_node
.GetIsUnsynced());
2897 EXPECT_TRUE(pref_node
.GetIsUnappliedUpdate());
2898 EXPECT_TRUE(pref_node
.GetIsDel());
2899 EXPECT_GT(pref_node
.GetServerVersion(), 0);
2900 EXPECT_EQ(pref_node
.GetBaseVersion(), -1);
2902 // Pref 2 should just be locally deleted.
2903 syncable::Entry
pref2_node(&trans
, GET_BY_HANDLE
, pref2_meta
);
2904 ASSERT_TRUE(pref2_node
.good());
2905 EXPECT_TRUE(pref2_node
.GetKernelCopy().is_dirty());
2906 EXPECT_FALSE(pref2_node
.GetIsUnsynced());
2907 EXPECT_TRUE(pref2_node
.GetIsDel());
2908 EXPECT_FALSE(pref2_node
.GetIsUnappliedUpdate());
2909 EXPECT_TRUE(pref2_node
.GetIsDel());
2910 EXPECT_EQ(pref2_node
.GetServerVersion(), 0);
2911 EXPECT_EQ(pref2_node
.GetBaseVersion(), -1);
2913 syncable::Entry
pref3_node(&trans
, GET_BY_HANDLE
, pref3_meta
);
2914 ASSERT_TRUE(pref3_node
.good());
2915 EXPECT_TRUE(pref3_node
.GetKernelCopy().is_dirty());
2916 EXPECT_FALSE(pref3_node
.GetIsUnsynced());
2917 EXPECT_TRUE(pref3_node
.GetIsUnappliedUpdate());
2918 EXPECT_TRUE(pref3_node
.GetIsDel());
2919 EXPECT_GT(pref3_node
.GetServerVersion(), 0);
2920 EXPECT_EQ(pref3_node
.GetBaseVersion(), -1);
2922 syncable::Entry
pref4_node(&trans
, GET_BY_HANDLE
, pref4_meta
);
2923 ASSERT_TRUE(pref4_node
.good());
2924 EXPECT_TRUE(pref4_node
.GetKernelCopy().is_dirty());
2925 EXPECT_FALSE(pref4_node
.GetIsUnsynced());
2926 EXPECT_TRUE(pref4_node
.GetIsUnappliedUpdate());
2927 EXPECT_TRUE(pref4_node
.GetIsDel());
2928 EXPECT_GT(pref4_node
.GetServerVersion(), 0);
2929 EXPECT_EQ(pref4_node
.GetBaseVersion(), -1);
2931 // Pref 5 should remain untouched.
2932 syncable::Entry
pref5_node(&trans
, GET_BY_HANDLE
, pref5_meta
);
2933 ASSERT_TRUE(pref5_node
.good());
2934 EXPECT_FALSE(pref5_node
.GetKernelCopy().is_dirty());
2935 EXPECT_FALSE(pref5_node
.GetIsUnsynced());
2936 EXPECT_TRUE(pref5_node
.GetIsUnappliedUpdate());
2937 EXPECT_TRUE(pref5_node
.GetIsDel());
2938 EXPECT_GT(pref5_node
.GetServerVersion(), 0);
2939 EXPECT_EQ(pref5_node
.GetBaseVersion(), -1);
2941 syncable::Entry
bookmark_node(&trans
, GET_BY_HANDLE
, bookmark_meta
);
2942 ASSERT_TRUE(bookmark_node
.good());
2943 EXPECT_TRUE(bookmark_node
.GetKernelCopy().is_dirty());
2944 EXPECT_FALSE(bookmark_node
.GetIsUnsynced());
2945 EXPECT_TRUE(bookmark_node
.GetIsUnappliedUpdate());
2946 EXPECT_TRUE(bookmark_node
.GetIsDel());
2947 EXPECT_GT(bookmark_node
.GetServerVersion(), 0);
2948 EXPECT_EQ(bookmark_node
.GetBaseVersion(), -1);
2952 // A test harness to exercise the code that processes and passes changes from
2953 // the "SYNCER"-WriteTransaction destructor, through the SyncManager, to the
2955 class SyncManagerChangeProcessingTest
: public SyncManagerTest
{
2957 void OnChangesApplied(ModelType model_type
,
2958 int64 model_version
,
2959 const BaseTransaction
* trans
,
2960 const ImmutableChangeRecordList
& changes
) override
{
2961 last_changes_
= changes
;
2964 void OnChangesComplete(ModelType model_type
) override
{}
2966 const ImmutableChangeRecordList
& GetRecentChangeList() {
2967 return last_changes_
;
2970 UserShare
* share() {
2971 return sync_manager_
.GetUserShare();
2974 // Set some flags so our nodes reasonably approximate the real world scenario
2975 // and can get past CheckTreeInvariants.
2977 // It's never going to be truly accurate, since we're squashing update
2978 // receipt, processing and application into a single transaction.
2979 void SetNodeProperties(syncable::MutableEntry
*entry
) {
2980 entry
->PutId(id_factory_
.NewServerId());
2981 entry
->PutBaseVersion(10);
2982 entry
->PutServerVersion(10);
2985 // Looks for the given change in the list. Returns the index at which it was
2986 // found. Returns -1 on lookup failure.
2987 size_t FindChangeInList(int64 id
, ChangeRecord::Action action
) {
2989 for (size_t i
= 0; i
< last_changes_
.Get().size(); ++i
) {
2990 if (last_changes_
.Get()[i
].id
== id
2991 && last_changes_
.Get()[i
].action
== action
) {
2995 ADD_FAILURE() << "Failed to find specified change";
2996 return static_cast<size_t>(-1);
2999 // Returns the current size of the change list.
3001 // Note that spurious changes do not necessarily indicate a problem.
3002 // Assertions on change list size can help detect problems, but it may be
3003 // necessary to reduce their strictness if the implementation changes.
3004 size_t GetChangeListSize() {
3005 return last_changes_
.Get().size();
3008 void ClearChangeList() { last_changes_
= ImmutableChangeRecordList(); }
3011 ImmutableChangeRecordList last_changes_
;
3012 TestIdFactory id_factory_
;
3015 // Test creation of a folder and a bookmark.
3016 TEST_F(SyncManagerChangeProcessingTest
, AddBookmarks
) {
3017 int64 type_root
= GetIdForDataType(BOOKMARKS
);
3018 int64 folder_id
= kInvalidId
;
3019 int64 child_id
= kInvalidId
;
3021 // Create a folder and a bookmark under it.
3023 syncable::WriteTransaction
trans(
3024 FROM_HERE
, syncable::SYNCER
, share()->directory
.get());
3025 syncable::Entry
root(&trans
, syncable::GET_BY_HANDLE
, type_root
);
3026 ASSERT_TRUE(root
.good());
3028 syncable::MutableEntry
folder(&trans
, syncable::CREATE
,
3029 BOOKMARKS
, root
.GetId(), "folder");
3030 ASSERT_TRUE(folder
.good());
3031 SetNodeProperties(&folder
);
3032 folder
.PutIsDir(true);
3033 folder_id
= folder
.GetMetahandle();
3035 syncable::MutableEntry
child(&trans
, syncable::CREATE
,
3036 BOOKMARKS
, folder
.GetId(), "child");
3037 ASSERT_TRUE(child
.good());
3038 SetNodeProperties(&child
);
3039 child_id
= child
.GetMetahandle();
3042 // The closing of the above scope will delete the transaction. Its processed
3043 // changes should be waiting for us in a member of the test harness.
3044 EXPECT_EQ(2UL, GetChangeListSize());
3046 // We don't need to check these return values here. The function will add a
3047 // non-fatal failure if these changes are not found.
3048 size_t folder_change_pos
=
3049 FindChangeInList(folder_id
, ChangeRecord::ACTION_ADD
);
3050 size_t child_change_pos
=
3051 FindChangeInList(child_id
, ChangeRecord::ACTION_ADD
);
3053 // Parents are delivered before children.
3054 EXPECT_LT(folder_change_pos
, child_change_pos
);
3057 // Test creation of a preferences (with implicit parent Id)
3058 TEST_F(SyncManagerChangeProcessingTest
, AddPreferences
) {
3059 int64 item1_id
= kInvalidId
;
3060 int64 item2_id
= kInvalidId
;
3062 // Create two preferences.
3064 syncable::WriteTransaction
trans(FROM_HERE
, syncable::SYNCER
,
3065 share()->directory
.get());
3067 syncable::MutableEntry
item1(&trans
, syncable::CREATE
, PREFERENCES
,
3069 ASSERT_TRUE(item1
.good());
3070 SetNodeProperties(&item1
);
3071 item1_id
= item1
.GetMetahandle();
3073 // Need at least two items to ensure hitting all possible codepaths in
3074 // ChangeReorderBuffer::Traversal::ExpandToInclude.
3075 syncable::MutableEntry
item2(&trans
, syncable::CREATE
, PREFERENCES
,
3077 ASSERT_TRUE(item2
.good());
3078 SetNodeProperties(&item2
);
3079 item2_id
= item2
.GetMetahandle();
3082 // The closing of the above scope will delete the transaction. Its processed
3083 // changes should be waiting for us in a member of the test harness.
3084 EXPECT_EQ(2UL, GetChangeListSize());
3086 FindChangeInList(item1_id
, ChangeRecord::ACTION_ADD
);
3087 FindChangeInList(item2_id
, ChangeRecord::ACTION_ADD
);
3090 // Test moving a bookmark into an empty folder.
3091 TEST_F(SyncManagerChangeProcessingTest
, MoveBookmarkIntoEmptyFolder
) {
3092 int64 type_root
= GetIdForDataType(BOOKMARKS
);
3093 int64 folder_b_id
= kInvalidId
;
3094 int64 child_id
= kInvalidId
;
3096 // Create two folders. Place a child under folder A.
3098 syncable::WriteTransaction
trans(
3099 FROM_HERE
, syncable::SYNCER
, share()->directory
.get());
3100 syncable::Entry
root(&trans
, syncable::GET_BY_HANDLE
, type_root
);
3101 ASSERT_TRUE(root
.good());
3103 syncable::MutableEntry
folder_a(&trans
, syncable::CREATE
,
3104 BOOKMARKS
, root
.GetId(), "folderA");
3105 ASSERT_TRUE(folder_a
.good());
3106 SetNodeProperties(&folder_a
);
3107 folder_a
.PutIsDir(true);
3109 syncable::MutableEntry
folder_b(&trans
, syncable::CREATE
,
3110 BOOKMARKS
, root
.GetId(), "folderB");
3111 ASSERT_TRUE(folder_b
.good());
3112 SetNodeProperties(&folder_b
);
3113 folder_b
.PutIsDir(true);
3114 folder_b_id
= folder_b
.GetMetahandle();
3116 syncable::MutableEntry
child(&trans
, syncable::CREATE
,
3117 BOOKMARKS
, folder_a
.GetId(),
3119 ASSERT_TRUE(child
.good());
3120 SetNodeProperties(&child
);
3121 child_id
= child
.GetMetahandle();
3124 // Close that transaction. The above was to setup the initial scenario. The
3125 // real test starts now.
3127 // Move the child from folder A to folder B.
3129 syncable::WriteTransaction
trans(
3130 FROM_HERE
, syncable::SYNCER
, share()->directory
.get());
3132 syncable::Entry
folder_b(&trans
, syncable::GET_BY_HANDLE
, folder_b_id
);
3133 syncable::MutableEntry
child(&trans
, syncable::GET_BY_HANDLE
, child_id
);
3135 child
.PutParentId(folder_b
.GetId());
3138 EXPECT_EQ(1UL, GetChangeListSize());
3140 // Verify that this was detected as a real change. An early version of the
3141 // UniquePosition code had a bug where moves from one folder to another were
3142 // ignored unless the moved node's UniquePosition value was also changed in
3144 FindChangeInList(child_id
, ChangeRecord::ACTION_UPDATE
);
3147 // Test moving a bookmark into a non-empty folder.
3148 TEST_F(SyncManagerChangeProcessingTest
, MoveIntoPopulatedFolder
) {
3149 int64 type_root
= GetIdForDataType(BOOKMARKS
);
3150 int64 child_a_id
= kInvalidId
;
3151 int64 child_b_id
= kInvalidId
;
3153 // Create two folders. Place one child each under folder A and folder B.
3155 syncable::WriteTransaction
trans(
3156 FROM_HERE
, syncable::SYNCER
, share()->directory
.get());
3157 syncable::Entry
root(&trans
, syncable::GET_BY_HANDLE
, type_root
);
3158 ASSERT_TRUE(root
.good());
3160 syncable::MutableEntry
folder_a(&trans
, syncable::CREATE
,
3161 BOOKMARKS
, root
.GetId(), "folderA");
3162 ASSERT_TRUE(folder_a
.good());
3163 SetNodeProperties(&folder_a
);
3164 folder_a
.PutIsDir(true);
3166 syncable::MutableEntry
folder_b(&trans
, syncable::CREATE
,
3167 BOOKMARKS
, root
.GetId(), "folderB");
3168 ASSERT_TRUE(folder_b
.good());
3169 SetNodeProperties(&folder_b
);
3170 folder_b
.PutIsDir(true);
3172 syncable::MutableEntry
child_a(&trans
, syncable::CREATE
,
3173 BOOKMARKS
, folder_a
.GetId(),
3175 ASSERT_TRUE(child_a
.good());
3176 SetNodeProperties(&child_a
);
3177 child_a_id
= child_a
.GetMetahandle();
3179 syncable::MutableEntry
child_b(&trans
, syncable::CREATE
,
3180 BOOKMARKS
, folder_b
.GetId(),
3182 SetNodeProperties(&child_b
);
3183 child_b_id
= child_b
.GetMetahandle();
3186 // Close that transaction. The above was to setup the initial scenario. The
3187 // real test starts now.
3190 syncable::WriteTransaction
trans(
3191 FROM_HERE
, syncable::SYNCER
, share()->directory
.get());
3193 syncable::MutableEntry
child_a(&trans
, syncable::GET_BY_HANDLE
, child_a_id
);
3194 syncable::MutableEntry
child_b(&trans
, syncable::GET_BY_HANDLE
, child_b_id
);
3196 // Move child A from folder A to folder B and update its position.
3197 child_a
.PutParentId(child_b
.GetParentId());
3198 child_a
.PutPredecessor(child_b
.GetId());
3201 EXPECT_EQ(1UL, GetChangeListSize());
3203 // Verify that only child a is in the change list.
3204 // (This function will add a failure if the lookup fails.)
3205 FindChangeInList(child_a_id
, ChangeRecord::ACTION_UPDATE
);
3208 // Tests the ordering of deletion changes.
3209 TEST_F(SyncManagerChangeProcessingTest
, DeletionsAndChanges
) {
3210 int64 type_root
= GetIdForDataType(BOOKMARKS
);
3211 int64 folder_a_id
= kInvalidId
;
3212 int64 folder_b_id
= kInvalidId
;
3213 int64 child_id
= kInvalidId
;
3215 // Create two folders. Place a child under folder A.
3217 syncable::WriteTransaction
trans(
3218 FROM_HERE
, syncable::SYNCER
, share()->directory
.get());
3219 syncable::Entry
root(&trans
, syncable::GET_BY_HANDLE
, type_root
);
3220 ASSERT_TRUE(root
.good());
3222 syncable::MutableEntry
folder_a(&trans
, syncable::CREATE
,
3223 BOOKMARKS
, root
.GetId(), "folderA");
3224 ASSERT_TRUE(folder_a
.good());
3225 SetNodeProperties(&folder_a
);
3226 folder_a
.PutIsDir(true);
3227 folder_a_id
= folder_a
.GetMetahandle();
3229 syncable::MutableEntry
folder_b(&trans
, syncable::CREATE
,
3230 BOOKMARKS
, root
.GetId(), "folderB");
3231 ASSERT_TRUE(folder_b
.good());
3232 SetNodeProperties(&folder_b
);
3233 folder_b
.PutIsDir(true);
3234 folder_b_id
= folder_b
.GetMetahandle();
3236 syncable::MutableEntry
child(&trans
, syncable::CREATE
,
3237 BOOKMARKS
, folder_a
.GetId(),
3239 ASSERT_TRUE(child
.good());
3240 SetNodeProperties(&child
);
3241 child_id
= child
.GetMetahandle();
3244 // Close that transaction. The above was to setup the initial scenario. The
3245 // real test starts now.
3248 syncable::WriteTransaction
trans(
3249 FROM_HERE
, syncable::SYNCER
, share()->directory
.get());
3251 syncable::MutableEntry
folder_a(
3252 &trans
, syncable::GET_BY_HANDLE
, folder_a_id
);
3253 syncable::MutableEntry
folder_b(
3254 &trans
, syncable::GET_BY_HANDLE
, folder_b_id
);
3255 syncable::MutableEntry
child(&trans
, syncable::GET_BY_HANDLE
, child_id
);
3257 // Delete folder B and its child.
3258 child
.PutIsDel(true);
3259 folder_b
.PutIsDel(true);
3261 // Make an unrelated change to folder A.
3262 folder_a
.PutNonUniqueName("NewNameA");
3265 EXPECT_EQ(3UL, GetChangeListSize());
3267 size_t folder_a_pos
=
3268 FindChangeInList(folder_a_id
, ChangeRecord::ACTION_UPDATE
);
3269 size_t folder_b_pos
=
3270 FindChangeInList(folder_b_id
, ChangeRecord::ACTION_DELETE
);
3271 size_t child_pos
= FindChangeInList(child_id
, ChangeRecord::ACTION_DELETE
);
3273 // Deletes should appear before updates.
3274 EXPECT_LT(child_pos
, folder_a_pos
);
3275 EXPECT_LT(folder_b_pos
, folder_a_pos
);
3278 // See that attachment metadata changes are not filtered out by
3279 // SyncManagerImpl::VisiblePropertiesDiffer.
3280 TEST_F(SyncManagerChangeProcessingTest
, AttachmentMetadataOnlyChanges
) {
3281 // Create an article with no attachments. See that a change is generated.
3282 int64 article_id
= kInvalidId
;
3284 syncable::WriteTransaction
trans(
3285 FROM_HERE
, syncable::SYNCER
, share()->directory
.get());
3286 int64 type_root
= GetIdForDataType(ARTICLES
);
3287 syncable::Entry
root(&trans
, syncable::GET_BY_HANDLE
, type_root
);
3288 ASSERT_TRUE(root
.good());
3289 syncable::MutableEntry
article(
3290 &trans
, syncable::CREATE
, ARTICLES
, root
.GetId(), "article");
3291 ASSERT_TRUE(article
.good());
3292 SetNodeProperties(&article
);
3293 article_id
= article
.GetMetahandle();
3295 ASSERT_EQ(1UL, GetChangeListSize());
3296 FindChangeInList(article_id
, ChangeRecord::ACTION_ADD
);
3299 // Modify the article by adding one attachment. Don't touch anything else.
3300 // See that a change is generated.
3302 syncable::WriteTransaction
trans(
3303 FROM_HERE
, syncable::SYNCER
, share()->directory
.get());
3304 syncable::MutableEntry
article(&trans
, syncable::GET_BY_HANDLE
, article_id
);
3305 sync_pb::AttachmentMetadata metadata
;
3306 *metadata
.add_record()->mutable_id() = CreateAttachmentIdProto(0, 0);
3307 article
.PutAttachmentMetadata(metadata
);
3309 ASSERT_EQ(1UL, GetChangeListSize());
3310 FindChangeInList(article_id
, ChangeRecord::ACTION_UPDATE
);
3313 // Modify the article by replacing its attachment with a different one. See
3314 // that a change is generated.
3316 syncable::WriteTransaction
trans(
3317 FROM_HERE
, syncable::SYNCER
, share()->directory
.get());
3318 syncable::MutableEntry
article(&trans
, syncable::GET_BY_HANDLE
, article_id
);
3319 sync_pb::AttachmentMetadata metadata
= article
.GetAttachmentMetadata();
3320 *metadata
.add_record()->mutable_id() = CreateAttachmentIdProto(0, 0);
3321 article
.PutAttachmentMetadata(metadata
);
3323 ASSERT_EQ(1UL, GetChangeListSize());
3324 FindChangeInList(article_id
, ChangeRecord::ACTION_UPDATE
);
3327 // Modify the article by replacing its attachment metadata with the same
3328 // attachment metadata. No change should be generated.
3330 syncable::WriteTransaction
trans(
3331 FROM_HERE
, syncable::SYNCER
, share()->directory
.get());
3332 syncable::MutableEntry
article(&trans
, syncable::GET_BY_HANDLE
, article_id
);
3333 article
.PutAttachmentMetadata(article
.GetAttachmentMetadata());
3335 ASSERT_EQ(0UL, GetChangeListSize());
3338 // During initialization SyncManagerImpl loads sqlite database. If it fails to
3339 // do so it should fail initialization. This test verifies this behavior.
3340 // Test reuses SyncManagerImpl initialization from SyncManagerTest but overrides
3341 // InternalComponentsFactory to return DirectoryBackingStore that always fails
3343 class SyncManagerInitInvalidStorageTest
: public SyncManagerTest
{
3345 SyncManagerInitInvalidStorageTest() {
3348 InternalComponentsFactory
* GetFactory() override
{
3349 return new TestInternalComponentsFactory(
3350 GetSwitches(), InternalComponentsFactory::STORAGE_INVALID
,
3355 // SyncManagerInitInvalidStorageTest::GetFactory will return
3356 // DirectoryBackingStore that ensures that SyncManagerImpl::OpenDirectory fails.
3357 // SyncManagerImpl initialization is done in SyncManagerTest::SetUp. This test's
3358 // task is to ensure that SyncManagerImpl reported initialization failure in
3359 // OnInitializationComplete callback.
3360 TEST_F(SyncManagerInitInvalidStorageTest
, FailToOpenDatabase
) {
3361 EXPECT_FALSE(initialization_succeeded_
);
3364 } // namespace syncer