Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / sync / internal_api / sync_manager_impl_unittest.cc
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.
4
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.
8
9 #include <cstddef>
10 #include <map>
11
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/message_loop/message_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/test_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"
76 #include "url/gurl.h"
77
78 using base::ExpectDictStringValue;
79 using testing::_;
80 using testing::DoAll;
81 using testing::InSequence;
82 using testing::Return;
83 using testing::SaveArg;
84 using testing::StrictMock;
85
86 namespace syncer {
87
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;
95
96 namespace {
97
98 // Makes a non-folder child of the root node.  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   ReadNode root_node(&trans);
105   root_node.InitByRootLookup();
106   WriteNode node(&trans);
107   WriteNode::InitUniqueByCreationResult result =
108       node.InitUniqueByCreation(model_type, root_node, client_tag);
109   EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
110   node.SetIsFolder(false);
111   return node.GetId();
112 }
113
114 // Makes a folder child of a non-root node. Returns the id of the
115 // newly-created node.
116 int64 MakeFolderWithParent(UserShare* share,
117                            ModelType model_type,
118                            int64 parent_id,
119                            BaseNode* predecessor) {
120   WriteTransaction trans(FROM_HERE, share);
121   ReadNode parent_node(&trans);
122   EXPECT_EQ(BaseNode::INIT_OK, parent_node.InitByIdLookup(parent_id));
123   WriteNode node(&trans);
124   EXPECT_TRUE(node.InitBookmarkByCreation(parent_node, predecessor));
125   node.SetIsFolder(true);
126   return node.GetId();
127 }
128
129 int64 MakeBookmarkWithParent(UserShare* share,
130                              int64 parent_id,
131                              BaseNode* predecessor) {
132   WriteTransaction trans(FROM_HERE, share);
133   ReadNode parent_node(&trans);
134   EXPECT_EQ(BaseNode::INIT_OK, parent_node.InitByIdLookup(parent_id));
135   WriteNode node(&trans);
136   EXPECT_TRUE(node.InitBookmarkByCreation(parent_node, predecessor));
137   return node.GetId();
138 }
139
140 // Creates the "synced" root node for a particular datatype. We use the syncable
141 // methods here so that the syncer treats these nodes as if they were already
142 // received from the server.
143 int64 MakeServerNodeForType(UserShare* share,
144                             ModelType model_type) {
145   sync_pb::EntitySpecifics specifics;
146   AddDefaultFieldValue(model_type, &specifics);
147   syncable::WriteTransaction trans(
148       FROM_HERE, syncable::UNITTEST, share->directory.get());
149   // Attempt to lookup by nigori tag.
150   std::string type_tag = ModelTypeToRootTag(model_type);
151   syncable::Id node_id = syncable::Id::CreateFromServerId(type_tag);
152   syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM,
153                                node_id);
154   EXPECT_TRUE(entry.good());
155   entry.PutBaseVersion(1);
156   entry.PutServerVersion(1);
157   entry.PutIsUnappliedUpdate(false);
158   entry.PutServerParentId(syncable::GetNullId());
159   entry.PutServerIsDir(true);
160   entry.PutIsDir(true);
161   entry.PutServerSpecifics(specifics);
162   entry.PutUniqueServerTag(type_tag);
163   entry.PutNonUniqueName(type_tag);
164   entry.PutIsDel(false);
165   entry.PutSpecifics(specifics);
166   return entry.GetMetahandle();
167 }
168
169 // Simulates creating a "synced" node as a child of the root datatype node.
170 int64 MakeServerNode(UserShare* share, ModelType model_type,
171                      const std::string& client_tag,
172                      const std::string& hashed_tag,
173                      const sync_pb::EntitySpecifics& specifics) {
174   syncable::WriteTransaction trans(
175       FROM_HERE, syncable::UNITTEST, share->directory.get());
176   syncable::Entry root_entry(&trans, syncable::GET_TYPE_ROOT, model_type);
177   EXPECT_TRUE(root_entry.good());
178   syncable::Id root_id = root_entry.GetId();
179   syncable::Id node_id = syncable::Id::CreateFromServerId(client_tag);
180   syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM,
181                                node_id);
182   EXPECT_TRUE(entry.good());
183   entry.PutBaseVersion(1);
184   entry.PutServerVersion(1);
185   entry.PutIsUnappliedUpdate(false);
186   entry.PutServerParentId(root_id);
187   entry.PutParentId(root_id);
188   entry.PutServerIsDir(false);
189   entry.PutIsDir(false);
190   entry.PutServerSpecifics(specifics);
191   entry.PutNonUniqueName(client_tag);
192   entry.PutUniqueClientTag(hashed_tag);
193   entry.PutIsDel(false);
194   entry.PutSpecifics(specifics);
195   return entry.GetMetahandle();
196 }
197
198 }  // namespace
199
200 class SyncApiTest : public testing::Test {
201  public:
202   virtual void SetUp() {
203     test_user_share_.SetUp();
204   }
205
206   virtual void TearDown() {
207     test_user_share_.TearDown();
208   }
209
210  protected:
211   // Create an entry with the given |model_type|, |client_tag| and
212   // |attachment_metadata|.
213   void CreateEntryWithAttachmentMetadata(
214       const ModelType& model_type,
215       const std::string& client_tag,
216       const sync_pb::AttachmentMetadata& attachment_metadata);
217
218   // Attempts to load the entry specified by |model_type| and |client_tag| and
219   // returns the lookup result code.
220   BaseNode::InitByLookupResult LookupEntryByClientTag(
221       const ModelType& model_type,
222       const std::string& client_tag);
223
224   // Replace the entry specified by |model_type| and |client_tag| with a
225   // tombstone.
226   void ReplaceWithTombstone(const ModelType& model_type,
227                             const std::string& client_tag);
228
229   // Save changes to the Directory, destroy it then reload it.
230   bool ReloadDir();
231
232   UserShare* user_share();
233   syncable::Directory* dir();
234   SyncEncryptionHandler* encryption_handler();
235
236  private:
237   base::MessageLoop message_loop_;
238   TestUserShare test_user_share_;
239 };
240
241 UserShare* SyncApiTest::user_share() {
242   return test_user_share_.user_share();
243 }
244
245 syncable::Directory* SyncApiTest::dir() {
246   return test_user_share_.user_share()->directory.get();
247 }
248
249 SyncEncryptionHandler* SyncApiTest::encryption_handler() {
250   return test_user_share_.encryption_handler();
251 }
252
253 bool SyncApiTest::ReloadDir() {
254   return test_user_share_.Reload();
255 }
256
257 void SyncApiTest::CreateEntryWithAttachmentMetadata(
258     const ModelType& model_type,
259     const std::string& client_tag,
260     const sync_pb::AttachmentMetadata& attachment_metadata) {
261   syncer::WriteTransaction trans(FROM_HERE, user_share());
262   syncer::ReadNode root_node(&trans);
263   root_node.InitByRootLookup();
264   syncer::WriteNode node(&trans);
265   ASSERT_EQ(node.InitUniqueByCreation(model_type, root_node, client_tag),
266             syncer::WriteNode::INIT_SUCCESS);
267   node.SetAttachmentMetadata(attachment_metadata);
268 }
269
270 BaseNode::InitByLookupResult SyncApiTest::LookupEntryByClientTag(
271     const ModelType& model_type,
272     const std::string& client_tag) {
273   syncer::ReadTransaction trans(FROM_HERE, user_share());
274   syncer::ReadNode node(&trans);
275   return node.InitByClientTagLookup(model_type, client_tag);
276 }
277
278 void SyncApiTest::ReplaceWithTombstone(const ModelType& model_type,
279                                        const std::string& client_tag) {
280   syncer::WriteTransaction trans(FROM_HERE, user_share());
281   syncer::WriteNode node(&trans);
282   ASSERT_EQ(node.InitByClientTagLookup(model_type, client_tag),
283             syncer::WriteNode::INIT_OK);
284   node.Tombstone();
285 }
286
287 TEST_F(SyncApiTest, SanityCheckTest) {
288   {
289     ReadTransaction trans(FROM_HERE, user_share());
290     EXPECT_TRUE(trans.GetWrappedTrans());
291   }
292   {
293     WriteTransaction trans(FROM_HERE, user_share());
294     EXPECT_TRUE(trans.GetWrappedTrans());
295   }
296   {
297     // No entries but root should exist
298     ReadTransaction trans(FROM_HERE, user_share());
299     ReadNode node(&trans);
300     // Metahandle 1 can be root, sanity check 2
301     EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD, node.InitByIdLookup(2));
302   }
303 }
304
305 TEST_F(SyncApiTest, BasicTagWrite) {
306   {
307     ReadTransaction trans(FROM_HERE, user_share());
308     ReadNode root_node(&trans);
309     root_node.InitByRootLookup();
310     EXPECT_EQ(root_node.GetFirstChildId(), 0);
311   }
312
313   ignore_result(MakeNode(user_share(), BOOKMARKS, "testtag"));
314
315   {
316     ReadTransaction trans(FROM_HERE, user_share());
317     ReadNode node(&trans);
318     EXPECT_EQ(BaseNode::INIT_OK,
319               node.InitByClientTagLookup(BOOKMARKS, "testtag"));
320
321     ReadNode root_node(&trans);
322     root_node.InitByRootLookup();
323     EXPECT_NE(node.GetId(), 0);
324     EXPECT_EQ(node.GetId(), root_node.GetFirstChildId());
325   }
326 }
327
328 TEST_F(SyncApiTest, ModelTypesSiloed) {
329   {
330     WriteTransaction trans(FROM_HERE, user_share());
331     ReadNode root_node(&trans);
332     root_node.InitByRootLookup();
333     EXPECT_EQ(root_node.GetFirstChildId(), 0);
334   }
335
336   ignore_result(MakeNode(user_share(), BOOKMARKS, "collideme"));
337   ignore_result(MakeNode(user_share(), PREFERENCES, "collideme"));
338   ignore_result(MakeNode(user_share(), AUTOFILL, "collideme"));
339
340   {
341     ReadTransaction trans(FROM_HERE, user_share());
342
343     ReadNode bookmarknode(&trans);
344     EXPECT_EQ(BaseNode::INIT_OK,
345               bookmarknode.InitByClientTagLookup(BOOKMARKS,
346                   "collideme"));
347
348     ReadNode prefnode(&trans);
349     EXPECT_EQ(BaseNode::INIT_OK,
350               prefnode.InitByClientTagLookup(PREFERENCES,
351                   "collideme"));
352
353     ReadNode autofillnode(&trans);
354     EXPECT_EQ(BaseNode::INIT_OK,
355               autofillnode.InitByClientTagLookup(AUTOFILL,
356                   "collideme"));
357
358     EXPECT_NE(bookmarknode.GetId(), prefnode.GetId());
359     EXPECT_NE(autofillnode.GetId(), prefnode.GetId());
360     EXPECT_NE(bookmarknode.GetId(), autofillnode.GetId());
361   }
362 }
363
364 TEST_F(SyncApiTest, ReadMissingTagsFails) {
365   {
366     ReadTransaction trans(FROM_HERE, user_share());
367     ReadNode node(&trans);
368     EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD,
369               node.InitByClientTagLookup(BOOKMARKS,
370                   "testtag"));
371   }
372   {
373     WriteTransaction trans(FROM_HERE, user_share());
374     WriteNode node(&trans);
375     EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_NOT_GOOD,
376               node.InitByClientTagLookup(BOOKMARKS,
377                   "testtag"));
378   }
379 }
380
381 // TODO(chron): Hook this all up to the server and write full integration tests
382 //              for update->undelete behavior.
383 TEST_F(SyncApiTest, TestDeleteBehavior) {
384   int64 node_id;
385   int64 folder_id;
386   std::string test_title("test1");
387
388   {
389     WriteTransaction trans(FROM_HERE, user_share());
390     ReadNode root_node(&trans);
391     root_node.InitByRootLookup();
392
393     // we'll use this spare folder later
394     WriteNode folder_node(&trans);
395     EXPECT_TRUE(folder_node.InitBookmarkByCreation(root_node, NULL));
396     folder_id = folder_node.GetId();
397
398     WriteNode wnode(&trans);
399     WriteNode::InitUniqueByCreationResult result =
400         wnode.InitUniqueByCreation(BOOKMARKS, root_node, "testtag");
401     EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
402     wnode.SetIsFolder(false);
403     wnode.SetTitle(test_title);
404
405     node_id = wnode.GetId();
406   }
407
408   // Ensure we can delete something with a tag.
409   {
410     WriteTransaction trans(FROM_HERE, user_share());
411     WriteNode wnode(&trans);
412     EXPECT_EQ(BaseNode::INIT_OK,
413               wnode.InitByClientTagLookup(BOOKMARKS,
414                   "testtag"));
415     EXPECT_FALSE(wnode.GetIsFolder());
416     EXPECT_EQ(wnode.GetTitle(), test_title);
417
418     wnode.Tombstone();
419   }
420
421   // Lookup of a node which was deleted should return failure,
422   // but have found some data about the node.
423   {
424     ReadTransaction trans(FROM_HERE, user_share());
425     ReadNode node(&trans);
426     EXPECT_EQ(BaseNode::INIT_FAILED_ENTRY_IS_DEL,
427               node.InitByClientTagLookup(BOOKMARKS,
428                   "testtag"));
429     // Note that for proper function of this API this doesn't need to be
430     // filled, we're checking just to make sure the DB worked in this test.
431     EXPECT_EQ(node.GetTitle(), test_title);
432   }
433
434   {
435     WriteTransaction trans(FROM_HERE, user_share());
436     ReadNode folder_node(&trans);
437     EXPECT_EQ(BaseNode::INIT_OK, folder_node.InitByIdLookup(folder_id));
438
439     WriteNode wnode(&trans);
440     // This will undelete the tag.
441     WriteNode::InitUniqueByCreationResult result =
442         wnode.InitUniqueByCreation(BOOKMARKS, folder_node, "testtag");
443     EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
444     EXPECT_EQ(wnode.GetIsFolder(), false);
445     EXPECT_EQ(wnode.GetParentId(), folder_node.GetId());
446     EXPECT_EQ(wnode.GetId(), node_id);
447     EXPECT_NE(wnode.GetTitle(), test_title);  // Title should be cleared
448     wnode.SetTitle(test_title);
449   }
450
451   // Now look up should work.
452   {
453     ReadTransaction trans(FROM_HERE, user_share());
454     ReadNode node(&trans);
455     EXPECT_EQ(BaseNode::INIT_OK,
456               node.InitByClientTagLookup(BOOKMARKS,
457                     "testtag"));
458     EXPECT_EQ(node.GetTitle(), test_title);
459     EXPECT_EQ(node.GetModelType(), BOOKMARKS);
460   }
461 }
462
463 TEST_F(SyncApiTest, WriteAndReadPassword) {
464   KeyParams params = {"localhost", "username", "passphrase"};
465   {
466     ReadTransaction trans(FROM_HERE, user_share());
467     trans.GetCryptographer()->AddKey(params);
468   }
469   {
470     WriteTransaction trans(FROM_HERE, user_share());
471     ReadNode root_node(&trans);
472     root_node.InitByRootLookup();
473
474     WriteNode password_node(&trans);
475     WriteNode::InitUniqueByCreationResult result =
476         password_node.InitUniqueByCreation(PASSWORDS,
477                                            root_node, "foo");
478     EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
479     sync_pb::PasswordSpecificsData data;
480     data.set_password_value("secret");
481     password_node.SetPasswordSpecifics(data);
482   }
483   {
484     ReadTransaction trans(FROM_HERE, user_share());
485     ReadNode root_node(&trans);
486     root_node.InitByRootLookup();
487
488     ReadNode password_node(&trans);
489     EXPECT_EQ(BaseNode::INIT_OK,
490               password_node.InitByClientTagLookup(PASSWORDS, "foo"));
491     const sync_pb::PasswordSpecificsData& data =
492         password_node.GetPasswordSpecifics();
493     EXPECT_EQ("secret", data.password_value());
494   }
495 }
496
497 TEST_F(SyncApiTest, WriteEncryptedTitle) {
498   KeyParams params = {"localhost", "username", "passphrase"};
499   {
500     ReadTransaction trans(FROM_HERE, user_share());
501     trans.GetCryptographer()->AddKey(params);
502   }
503   encryption_handler()->EnableEncryptEverything();
504   int bookmark_id;
505   {
506     WriteTransaction trans(FROM_HERE, user_share());
507     ReadNode root_node(&trans);
508     root_node.InitByRootLookup();
509
510     WriteNode bookmark_node(&trans);
511     ASSERT_TRUE(bookmark_node.InitBookmarkByCreation(root_node, NULL));
512     bookmark_id = bookmark_node.GetId();
513     bookmark_node.SetTitle("foo");
514
515     WriteNode pref_node(&trans);
516     WriteNode::InitUniqueByCreationResult result =
517         pref_node.InitUniqueByCreation(PREFERENCES, root_node, "bar");
518     ASSERT_EQ(WriteNode::INIT_SUCCESS, result);
519     pref_node.SetTitle("bar");
520   }
521   {
522     ReadTransaction trans(FROM_HERE, user_share());
523     ReadNode root_node(&trans);
524     root_node.InitByRootLookup();
525
526     ReadNode bookmark_node(&trans);
527     ASSERT_EQ(BaseNode::INIT_OK, bookmark_node.InitByIdLookup(bookmark_id));
528     EXPECT_EQ("foo", bookmark_node.GetTitle());
529     EXPECT_EQ(kEncryptedString,
530               bookmark_node.GetEntry()->GetNonUniqueName());
531
532     ReadNode pref_node(&trans);
533     ASSERT_EQ(BaseNode::INIT_OK,
534               pref_node.InitByClientTagLookup(PREFERENCES,
535                                               "bar"));
536     EXPECT_EQ(kEncryptedString, pref_node.GetTitle());
537   }
538 }
539
540 TEST_F(SyncApiTest, BaseNodeSetSpecifics) {
541   int64 child_id = MakeNode(user_share(), BOOKMARKS, "testtag");
542   WriteTransaction trans(FROM_HERE, user_share());
543   WriteNode node(&trans);
544   EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id));
545
546   sync_pb::EntitySpecifics entity_specifics;
547   entity_specifics.mutable_bookmark()->set_url("http://www.google.com");
548
549   EXPECT_NE(entity_specifics.SerializeAsString(),
550             node.GetEntitySpecifics().SerializeAsString());
551   node.SetEntitySpecifics(entity_specifics);
552   EXPECT_EQ(entity_specifics.SerializeAsString(),
553             node.GetEntitySpecifics().SerializeAsString());
554 }
555
556 TEST_F(SyncApiTest, BaseNodeSetSpecificsPreservesUnknownFields) {
557   int64 child_id = MakeNode(user_share(), BOOKMARKS, "testtag");
558   WriteTransaction trans(FROM_HERE, user_share());
559   WriteNode node(&trans);
560   EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id));
561   EXPECT_TRUE(node.GetEntitySpecifics().unknown_fields().empty());
562
563   sync_pb::EntitySpecifics entity_specifics;
564   entity_specifics.mutable_bookmark()->set_url("http://www.google.com");
565   entity_specifics.mutable_unknown_fields()->AddFixed32(5, 100);
566   node.SetEntitySpecifics(entity_specifics);
567   EXPECT_FALSE(node.GetEntitySpecifics().unknown_fields().empty());
568
569   entity_specifics.mutable_unknown_fields()->Clear();
570   node.SetEntitySpecifics(entity_specifics);
571   EXPECT_FALSE(node.GetEntitySpecifics().unknown_fields().empty());
572 }
573
574 TEST_F(SyncApiTest, EmptyTags) {
575   WriteTransaction trans(FROM_HERE, user_share());
576   ReadNode root_node(&trans);
577   root_node.InitByRootLookup();
578   WriteNode node(&trans);
579   std::string empty_tag;
580   WriteNode::InitUniqueByCreationResult result =
581       node.InitUniqueByCreation(TYPED_URLS, root_node, empty_tag);
582   EXPECT_NE(WriteNode::INIT_SUCCESS, result);
583   EXPECT_EQ(BaseNode::INIT_FAILED_PRECONDITION,
584             node.InitByClientTagLookup(TYPED_URLS, empty_tag));
585 }
586
587 // Test counting nodes when the type's root node has no children.
588 TEST_F(SyncApiTest, GetTotalNodeCountEmpty) {
589   int64 type_root = MakeServerNodeForType(user_share(), BOOKMARKS);
590   {
591     ReadTransaction trans(FROM_HERE, user_share());
592     ReadNode type_root_node(&trans);
593     EXPECT_EQ(BaseNode::INIT_OK,
594               type_root_node.InitByIdLookup(type_root));
595     EXPECT_EQ(1, type_root_node.GetTotalNodeCount());
596   }
597 }
598
599 // Test counting nodes when there is one child beneath the type's root.
600 TEST_F(SyncApiTest, GetTotalNodeCountOneChild) {
601   int64 type_root = MakeServerNodeForType(user_share(), BOOKMARKS);
602   int64 parent = MakeFolderWithParent(user_share(), BOOKMARKS, type_root, NULL);
603   {
604     ReadTransaction trans(FROM_HERE, user_share());
605     ReadNode type_root_node(&trans);
606     EXPECT_EQ(BaseNode::INIT_OK,
607               type_root_node.InitByIdLookup(type_root));
608     EXPECT_EQ(2, type_root_node.GetTotalNodeCount());
609     ReadNode parent_node(&trans);
610     EXPECT_EQ(BaseNode::INIT_OK,
611               parent_node.InitByIdLookup(parent));
612     EXPECT_EQ(1, parent_node.GetTotalNodeCount());
613   }
614 }
615
616 // Test counting nodes when there are multiple children beneath the type root,
617 // and one of those children has children of its own.
618 TEST_F(SyncApiTest, GetTotalNodeCountMultipleChildren) {
619   int64 type_root = MakeServerNodeForType(user_share(), BOOKMARKS);
620   int64 parent = MakeFolderWithParent(user_share(), BOOKMARKS, type_root, NULL);
621   ignore_result(MakeFolderWithParent(user_share(), BOOKMARKS, type_root, NULL));
622   int64 child1 = MakeFolderWithParent(user_share(), BOOKMARKS, parent, NULL);
623   ignore_result(MakeBookmarkWithParent(user_share(), parent, NULL));
624   ignore_result(MakeBookmarkWithParent(user_share(), child1, NULL));
625
626   {
627     ReadTransaction trans(FROM_HERE, user_share());
628     ReadNode type_root_node(&trans);
629     EXPECT_EQ(BaseNode::INIT_OK,
630               type_root_node.InitByIdLookup(type_root));
631     EXPECT_EQ(6, type_root_node.GetTotalNodeCount());
632     ReadNode node(&trans);
633     EXPECT_EQ(BaseNode::INIT_OK,
634               node.InitByIdLookup(parent));
635     EXPECT_EQ(4, node.GetTotalNodeCount());
636   }
637 }
638
639 // Verify that Directory keeps track of which attachments are referenced by
640 // which entries.
641 TEST_F(SyncApiTest, AttachmentLinking) {
642   // Add an entry with an attachment.
643   std::string tag1("some tag");
644   syncer::AttachmentId attachment_id(syncer::AttachmentId::Create());
645   sync_pb::AttachmentMetadata attachment_metadata;
646   sync_pb::AttachmentMetadataRecord* record = attachment_metadata.add_record();
647   *record->mutable_id() = attachment_id.GetProto();
648   ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id.GetProto()));
649   CreateEntryWithAttachmentMetadata(PREFERENCES, tag1, attachment_metadata);
650
651   // See that the directory knows it's linked.
652   ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id.GetProto()));
653
654   // Add a second entry referencing the same attachment.
655   std::string tag2("some other tag");
656   CreateEntryWithAttachmentMetadata(PREFERENCES, tag2, attachment_metadata);
657
658   // See that the directory knows it's still linked.
659   ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id.GetProto()));
660
661   // Tombstone the first entry.
662   ReplaceWithTombstone(syncer::PREFERENCES, tag1);
663
664   // See that the attachment is still considered linked because the entry hasn't
665   // been purged from the Directory.
666   ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id.GetProto()));
667
668   // Save changes and see that the entry is truly gone.
669   ASSERT_TRUE(dir()->SaveChanges());
670   ASSERT_EQ(LookupEntryByClientTag(PREFERENCES, tag1),
671             syncer::WriteNode::INIT_FAILED_ENTRY_NOT_GOOD);
672
673   // However, the attachment is still linked.
674   ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id.GetProto()));
675
676   // Save, destroy, and recreate the directory.  See that it's still linked.
677   ASSERT_TRUE(ReloadDir());
678   ASSERT_TRUE(dir()->IsAttachmentLinked(attachment_id.GetProto()));
679
680   // Tombstone the second entry, save changes, see that it's truly gone.
681   ReplaceWithTombstone(syncer::PREFERENCES, tag2);
682   ASSERT_TRUE(dir()->SaveChanges());
683   ASSERT_EQ(LookupEntryByClientTag(PREFERENCES, tag2),
684             syncer::WriteNode::INIT_FAILED_ENTRY_NOT_GOOD);
685
686   // Finally, the attachment is no longer linked.
687   ASSERT_FALSE(dir()->IsAttachmentLinked(attachment_id.GetProto()));
688 }
689
690 namespace {
691
692 class TestHttpPostProviderInterface : public HttpPostProviderInterface {
693  public:
694   virtual ~TestHttpPostProviderInterface() {}
695
696   virtual void SetExtraRequestHeaders(const char* headers) OVERRIDE {}
697   virtual void SetURL(const char* url, int port) OVERRIDE {}
698   virtual void SetPostPayload(const char* content_type,
699                               int content_length,
700                               const char* content) OVERRIDE {}
701   virtual bool MakeSynchronousPost(int* error_code, int* response_code)
702       OVERRIDE {
703     return false;
704   }
705   virtual int GetResponseContentLength() const OVERRIDE {
706     return 0;
707   }
708   virtual const char* GetResponseContent() const OVERRIDE {
709     return "";
710   }
711   virtual const std::string GetResponseHeaderValue(
712       const std::string& name) const OVERRIDE {
713     return std::string();
714   }
715   virtual void Abort() OVERRIDE {}
716 };
717
718 class TestHttpPostProviderFactory : public HttpPostProviderFactory {
719  public:
720   virtual ~TestHttpPostProviderFactory() {}
721   virtual void Init(const std::string& user_agent) OVERRIDE { }
722   virtual HttpPostProviderInterface* Create() OVERRIDE {
723     return new TestHttpPostProviderInterface();
724   }
725   virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE {
726     delete static_cast<TestHttpPostProviderInterface*>(http);
727   }
728 };
729
730 class SyncManagerObserverMock : public SyncManager::Observer {
731  public:
732   MOCK_METHOD1(OnSyncCycleCompleted,
733                void(const SyncSessionSnapshot&));  // NOLINT
734   MOCK_METHOD4(OnInitializationComplete,
735                void(const WeakHandle<JsBackend>&,
736                     const WeakHandle<DataTypeDebugInfoListener>&,
737                     bool,
738                     syncer::ModelTypeSet));  // NOLINT
739   MOCK_METHOD1(OnConnectionStatusChange, void(ConnectionStatus));  // NOLINT
740   MOCK_METHOD1(OnUpdatedToken, void(const std::string&));  // NOLINT
741   MOCK_METHOD1(OnActionableError, void(const SyncProtocolError&));  // NOLINT
742   MOCK_METHOD1(OnMigrationRequested, void(syncer::ModelTypeSet));  // NOLINT
743   MOCK_METHOD1(OnProtocolEvent, void(const ProtocolEvent&));  // NOLINT
744 };
745
746 class SyncEncryptionHandlerObserverMock
747     : public SyncEncryptionHandler::Observer {
748  public:
749   MOCK_METHOD2(OnPassphraseRequired,
750                void(PassphraseRequiredReason,
751                     const sync_pb::EncryptedData&));  // NOLINT
752   MOCK_METHOD0(OnPassphraseAccepted, void());  // NOLINT
753   MOCK_METHOD2(OnBootstrapTokenUpdated,
754                void(const std::string&, BootstrapTokenType type));  // NOLINT
755   MOCK_METHOD2(OnEncryptedTypesChanged,
756                void(ModelTypeSet, bool));  // NOLINT
757   MOCK_METHOD0(OnEncryptionComplete, void());  // NOLINT
758   MOCK_METHOD1(OnCryptographerStateChanged, void(Cryptographer*));  // NOLINT
759   MOCK_METHOD2(OnPassphraseTypeChanged, void(PassphraseType,
760                                              base::Time));  // NOLINT
761 };
762
763 }  // namespace
764
765 class SyncManagerTest : public testing::Test,
766                         public SyncManager::ChangeDelegate {
767  protected:
768   enum NigoriStatus {
769     DONT_WRITE_NIGORI,
770     WRITE_TO_NIGORI
771   };
772
773   enum EncryptionStatus {
774     UNINITIALIZED,
775     DEFAULT_ENCRYPTION,
776     FULL_ENCRYPTION
777   };
778
779   SyncManagerTest()
780       : sync_manager_("Test sync manager") {
781     switches_.encryption_method =
782         InternalComponentsFactory::ENCRYPTION_KEYSTORE;
783   }
784
785   virtual ~SyncManagerTest() {
786   }
787
788   // Test implementation.
789   void SetUp() {
790     ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
791
792     extensions_activity_ = new ExtensionsActivity();
793
794     SyncCredentials credentials;
795     credentials.email = "foo@bar.com";
796     credentials.sync_token = "sometoken";
797     OAuth2TokenService::ScopeSet scope_set;
798     scope_set.insert(GaiaConstants::kChromeSyncOAuth2Scope);
799     credentials.scope_set = scope_set;
800
801     sync_manager_.AddObserver(&manager_observer_);
802     EXPECT_CALL(manager_observer_, OnInitializationComplete(_, _, _, _)).
803         WillOnce(DoAll(SaveArg<0>(&js_backend_),
804             SaveArg<2>(&initialization_succeeded_)));
805
806     EXPECT_FALSE(js_backend_.IsInitialized());
807
808     std::vector<scoped_refptr<ModelSafeWorker> > workers;
809     ModelSafeRoutingInfo routing_info;
810     GetModelSafeRoutingInfo(&routing_info);
811
812     // This works only because all routing info types are GROUP_PASSIVE.
813     // If we had types in other groups, we would need additional workers
814     // to support them.
815     scoped_refptr<ModelSafeWorker> worker = new FakeModelWorker(GROUP_PASSIVE);
816     workers.push_back(worker);
817
818     SyncManager::InitArgs args;
819     args.database_location = temp_dir_.path();
820     args.service_url = GURL("https://example.com/");
821     args.post_factory =
822         scoped_ptr<HttpPostProviderFactory>(new TestHttpPostProviderFactory());
823     args.workers = workers;
824     args.extensions_activity = extensions_activity_.get(),
825     args.change_delegate = this;
826     args.credentials = credentials;
827     args.invalidator_client_id = "fake_invalidator_client_id";
828     args.internal_components_factory.reset(GetFactory());
829     args.encryptor = &encryptor_;
830     args.unrecoverable_error_handler.reset(new TestUnrecoverableErrorHandler);
831     args.cancelation_signal = &cancelation_signal_;
832     sync_manager_.Init(&args);
833
834     sync_manager_.GetEncryptionHandler()->AddObserver(&encryption_observer_);
835
836     EXPECT_TRUE(js_backend_.IsInitialized());
837     EXPECT_EQ(InternalComponentsFactory::STORAGE_ON_DISK,
838               storage_used_);
839
840     if (initialization_succeeded_) {
841       for (ModelSafeRoutingInfo::iterator i = routing_info.begin();
842            i != routing_info.end(); ++i) {
843         type_roots_[i->first] = MakeServerNodeForType(
844             sync_manager_.GetUserShare(), i->first);
845       }
846     }
847
848     PumpLoop();
849   }
850
851   void TearDown() {
852     sync_manager_.RemoveObserver(&manager_observer_);
853     sync_manager_.ShutdownOnSyncThread(STOP_SYNC);
854     PumpLoop();
855   }
856
857   void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) {
858     (*out)[NIGORI] = GROUP_PASSIVE;
859     (*out)[DEVICE_INFO] = GROUP_PASSIVE;
860     (*out)[EXPERIMENTS] = GROUP_PASSIVE;
861     (*out)[BOOKMARKS] = GROUP_PASSIVE;
862     (*out)[THEMES] = GROUP_PASSIVE;
863     (*out)[SESSIONS] = GROUP_PASSIVE;
864     (*out)[PASSWORDS] = GROUP_PASSIVE;
865     (*out)[PREFERENCES] = GROUP_PASSIVE;
866     (*out)[PRIORITY_PREFERENCES] = GROUP_PASSIVE;
867     (*out)[ARTICLES] = GROUP_PASSIVE;
868   }
869
870   ModelTypeSet GetEnabledTypes() {
871     ModelSafeRoutingInfo routing_info;
872     GetModelSafeRoutingInfo(&routing_info);
873     return GetRoutingInfoTypes(routing_info);
874   }
875
876   virtual void OnChangesApplied(
877       ModelType model_type,
878       int64 model_version,
879       const BaseTransaction* trans,
880       const ImmutableChangeRecordList& changes) OVERRIDE {}
881
882   virtual void OnChangesComplete(ModelType model_type) OVERRIDE {}
883
884   // Helper methods.
885   bool SetUpEncryption(NigoriStatus nigori_status,
886                        EncryptionStatus encryption_status) {
887     UserShare* share = sync_manager_.GetUserShare();
888
889     // We need to create the nigori node as if it were an applied server update.
890     int64 nigori_id = GetIdForDataType(NIGORI);
891     if (nigori_id == kInvalidId)
892       return false;
893
894     // Set the nigori cryptographer information.
895     if (encryption_status == FULL_ENCRYPTION)
896       sync_manager_.GetEncryptionHandler()->EnableEncryptEverything();
897
898     WriteTransaction trans(FROM_HERE, share);
899     Cryptographer* cryptographer = trans.GetCryptographer();
900     if (!cryptographer)
901       return false;
902     if (encryption_status != UNINITIALIZED) {
903       KeyParams params = {"localhost", "dummy", "foobar"};
904       cryptographer->AddKey(params);
905     } else {
906       DCHECK_NE(nigori_status, WRITE_TO_NIGORI);
907     }
908     if (nigori_status == WRITE_TO_NIGORI) {
909       sync_pb::NigoriSpecifics nigori;
910       cryptographer->GetKeys(nigori.mutable_encryption_keybag());
911       share->directory->GetNigoriHandler()->UpdateNigoriFromEncryptedTypes(
912           &nigori,
913           trans.GetWrappedTrans());
914       WriteNode node(&trans);
915       EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(nigori_id));
916       node.SetNigoriSpecifics(nigori);
917     }
918     return cryptographer->is_ready();
919   }
920
921   int64 GetIdForDataType(ModelType type) {
922     if (type_roots_.count(type) == 0)
923       return 0;
924     return type_roots_[type];
925   }
926
927   void PumpLoop() {
928     message_loop_.RunUntilIdle();
929   }
930
931   void SetJsEventHandler(const WeakHandle<JsEventHandler>& event_handler) {
932     js_backend_.Call(FROM_HERE, &JsBackend::SetJsEventHandler,
933                      event_handler);
934     PumpLoop();
935   }
936
937   // Looks up an entry by client tag and resets IS_UNSYNCED value to false.
938   // Returns true if entry was previously unsynced, false if IS_UNSYNCED was
939   // already false.
940   bool ResetUnsyncedEntry(ModelType type,
941                           const std::string& client_tag) {
942     UserShare* share = sync_manager_.GetUserShare();
943     syncable::WriteTransaction trans(
944         FROM_HERE, syncable::UNITTEST, share->directory.get());
945     const std::string hash = syncable::GenerateSyncableHash(type, client_tag);
946     syncable::MutableEntry entry(&trans, syncable::GET_BY_CLIENT_TAG,
947                                  hash);
948     EXPECT_TRUE(entry.good());
949     if (!entry.GetIsUnsynced())
950       return false;
951     entry.PutIsUnsynced(false);
952     return true;
953   }
954
955   virtual InternalComponentsFactory* GetFactory() {
956     return new TestInternalComponentsFactory(
957         GetSwitches(), InternalComponentsFactory::STORAGE_IN_MEMORY,
958         &storage_used_);
959   }
960
961   // Returns true if we are currently encrypting all sync data.  May
962   // be called on any thread.
963   bool EncryptEverythingEnabledForTest() {
964     return sync_manager_.GetEncryptionHandler()->EncryptEverythingEnabled();
965   }
966
967   // Gets the set of encrypted types from the cryptographer
968   // Note: opens a transaction.  May be called from any thread.
969   ModelTypeSet GetEncryptedTypes() {
970     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
971     return GetEncryptedTypesWithTrans(&trans);
972   }
973
974   ModelTypeSet GetEncryptedTypesWithTrans(BaseTransaction* trans) {
975     return trans->GetDirectory()->GetNigoriHandler()->
976         GetEncryptedTypes(trans->GetWrappedTrans());
977   }
978
979   void SimulateInvalidatorEnabledForTest(bool is_enabled) {
980     DCHECK(sync_manager_.thread_checker_.CalledOnValidThread());
981     sync_manager_.SetInvalidatorEnabled(is_enabled);
982   }
983
984   void SetProgressMarkerForType(ModelType type, bool set) {
985     if (set) {
986       sync_pb::DataTypeProgressMarker marker;
987       marker.set_token("token");
988       marker.set_data_type_id(GetSpecificsFieldNumberFromModelType(type));
989       sync_manager_.directory()->SetDownloadProgress(type, marker);
990     } else {
991       sync_pb::DataTypeProgressMarker marker;
992       sync_manager_.directory()->SetDownloadProgress(type, marker);
993     }
994   }
995
996   InternalComponentsFactory::Switches GetSwitches() const {
997     return switches_;
998   }
999
1000  private:
1001   // Needed by |sync_manager_|.
1002   base::MessageLoop message_loop_;
1003   // Needed by |sync_manager_|.
1004   base::ScopedTempDir temp_dir_;
1005   // Sync Id's for the roots of the enabled datatypes.
1006   std::map<ModelType, int64> type_roots_;
1007   scoped_refptr<ExtensionsActivity> extensions_activity_;
1008
1009  protected:
1010   FakeEncryptor encryptor_;
1011   SyncManagerImpl sync_manager_;
1012   CancelationSignal cancelation_signal_;
1013   WeakHandle<JsBackend> js_backend_;
1014   bool initialization_succeeded_;
1015   StrictMock<SyncManagerObserverMock> manager_observer_;
1016   StrictMock<SyncEncryptionHandlerObserverMock> encryption_observer_;
1017   InternalComponentsFactory::Switches switches_;
1018   InternalComponentsFactory::StorageOption storage_used_;
1019 };
1020
1021 TEST_F(SyncManagerTest, GetAllNodesForTypeTest) {
1022   ModelSafeRoutingInfo routing_info;
1023   GetModelSafeRoutingInfo(&routing_info);
1024   sync_manager_.StartSyncingNormally(routing_info);
1025
1026   scoped_ptr<base::ListValue> node_list(
1027       sync_manager_.GetAllNodesForType(syncer::PREFERENCES));
1028
1029   // Should have one node: the type root node.
1030   ASSERT_EQ(1U, node_list->GetSize());
1031
1032   const base::DictionaryValue* first_result;
1033   ASSERT_TRUE(node_list->GetDictionary(0, &first_result));
1034   EXPECT_TRUE(first_result->HasKey("ID"));
1035   EXPECT_TRUE(first_result->HasKey("NON_UNIQUE_NAME"));
1036 }
1037
1038 TEST_F(SyncManagerTest, RefreshEncryptionReady) {
1039   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1040   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
1041   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1042   EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false));
1043
1044   sync_manager_.GetEncryptionHandler()->Init();
1045   PumpLoop();
1046
1047   const ModelTypeSet encrypted_types = GetEncryptedTypes();
1048   EXPECT_TRUE(encrypted_types.Has(PASSWORDS));
1049   EXPECT_FALSE(EncryptEverythingEnabledForTest());
1050
1051   {
1052     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1053     ReadNode node(&trans);
1054     EXPECT_EQ(BaseNode::INIT_OK,
1055               node.InitByIdLookup(GetIdForDataType(NIGORI)));
1056     sync_pb::NigoriSpecifics nigori = node.GetNigoriSpecifics();
1057     EXPECT_TRUE(nigori.has_encryption_keybag());
1058     Cryptographer* cryptographer = trans.GetCryptographer();
1059     EXPECT_TRUE(cryptographer->is_ready());
1060     EXPECT_TRUE(cryptographer->CanDecrypt(nigori.encryption_keybag()));
1061   }
1062 }
1063
1064 // Attempt to refresh encryption when nigori not downloaded.
1065 TEST_F(SyncManagerTest, RefreshEncryptionNotReady) {
1066   // Don't set up encryption (no nigori node created).
1067
1068   // Should fail. Triggers an OnPassphraseRequired because the cryptographer
1069   // is not ready.
1070   EXPECT_CALL(encryption_observer_, OnPassphraseRequired(_, _)).Times(1);
1071   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1072   EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false));
1073   sync_manager_.GetEncryptionHandler()->Init();
1074   PumpLoop();
1075
1076   const ModelTypeSet encrypted_types = GetEncryptedTypes();
1077   EXPECT_TRUE(encrypted_types.Has(PASSWORDS));  // Hardcoded.
1078   EXPECT_FALSE(EncryptEverythingEnabledForTest());
1079 }
1080
1081 // Attempt to refresh encryption when nigori is empty.
1082 TEST_F(SyncManagerTest, RefreshEncryptionEmptyNigori) {
1083   EXPECT_TRUE(SetUpEncryption(DONT_WRITE_NIGORI, DEFAULT_ENCRYPTION));
1084   EXPECT_CALL(encryption_observer_, OnEncryptionComplete()).Times(1);
1085   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1086   EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false));
1087
1088   // Should write to nigori.
1089   sync_manager_.GetEncryptionHandler()->Init();
1090   PumpLoop();
1091
1092   const ModelTypeSet encrypted_types = GetEncryptedTypes();
1093   EXPECT_TRUE(encrypted_types.Has(PASSWORDS));  // Hardcoded.
1094   EXPECT_FALSE(EncryptEverythingEnabledForTest());
1095
1096   {
1097     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1098     ReadNode node(&trans);
1099     EXPECT_EQ(BaseNode::INIT_OK,
1100               node.InitByIdLookup(GetIdForDataType(NIGORI)));
1101     sync_pb::NigoriSpecifics nigori = node.GetNigoriSpecifics();
1102     EXPECT_TRUE(nigori.has_encryption_keybag());
1103     Cryptographer* cryptographer = trans.GetCryptographer();
1104     EXPECT_TRUE(cryptographer->is_ready());
1105     EXPECT_TRUE(cryptographer->CanDecrypt(nigori.encryption_keybag()));
1106   }
1107 }
1108
1109 TEST_F(SyncManagerTest, EncryptDataTypesWithNoData) {
1110   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1111   EXPECT_CALL(encryption_observer_,
1112               OnEncryptedTypesChanged(
1113                   HasModelTypes(EncryptableUserTypes()), true));
1114   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
1115   sync_manager_.GetEncryptionHandler()->EnableEncryptEverything();
1116   EXPECT_TRUE(EncryptEverythingEnabledForTest());
1117 }
1118
1119 TEST_F(SyncManagerTest, EncryptDataTypesWithData) {
1120   size_t batch_size = 5;
1121   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1122
1123   // Create some unencrypted unsynced data.
1124   int64 folder = MakeFolderWithParent(sync_manager_.GetUserShare(),
1125                                       BOOKMARKS,
1126                                       GetIdForDataType(BOOKMARKS),
1127                                       NULL);
1128   // First batch_size nodes are children of folder.
1129   size_t i;
1130   for (i = 0; i < batch_size; ++i) {
1131     MakeBookmarkWithParent(sync_manager_.GetUserShare(), folder, NULL);
1132   }
1133   // Next batch_size nodes are a different type and on their own.
1134   for (; i < 2*batch_size; ++i) {
1135     MakeNode(sync_manager_.GetUserShare(), SESSIONS,
1136              base::StringPrintf("%" PRIuS "", i));
1137   }
1138   // Last batch_size nodes are a third type that will not need encryption.
1139   for (; i < 3*batch_size; ++i) {
1140     MakeNode(sync_manager_.GetUserShare(), THEMES,
1141              base::StringPrintf("%" PRIuS "", i));
1142   }
1143
1144   {
1145     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1146     EXPECT_TRUE(GetEncryptedTypesWithTrans(&trans).Equals(
1147         SyncEncryptionHandler::SensitiveTypes()));
1148     EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1149         trans.GetWrappedTrans(),
1150         BOOKMARKS,
1151         false /* not encrypted */));
1152     EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1153         trans.GetWrappedTrans(),
1154         SESSIONS,
1155         false /* not encrypted */));
1156     EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1157         trans.GetWrappedTrans(),
1158         THEMES,
1159         false /* not encrypted */));
1160   }
1161
1162   EXPECT_CALL(encryption_observer_,
1163               OnEncryptedTypesChanged(
1164                   HasModelTypes(EncryptableUserTypes()), true));
1165   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
1166   sync_manager_.GetEncryptionHandler()->EnableEncryptEverything();
1167   EXPECT_TRUE(EncryptEverythingEnabledForTest());
1168   {
1169     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1170     EXPECT_TRUE(GetEncryptedTypesWithTrans(&trans).Equals(
1171         EncryptableUserTypes()));
1172     EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1173         trans.GetWrappedTrans(),
1174         BOOKMARKS,
1175         true /* is encrypted */));
1176     EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1177         trans.GetWrappedTrans(),
1178         SESSIONS,
1179         true /* is encrypted */));
1180     EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1181         trans.GetWrappedTrans(),
1182         THEMES,
1183         true /* is encrypted */));
1184   }
1185
1186   // Trigger's a ReEncryptEverything with new passphrase.
1187   testing::Mock::VerifyAndClearExpectations(&encryption_observer_);
1188   EXPECT_CALL(encryption_observer_,
1189               OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1190   EXPECT_CALL(encryption_observer_, OnPassphraseAccepted());
1191   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
1192   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1193   EXPECT_CALL(encryption_observer_,
1194               OnPassphraseTypeChanged(CUSTOM_PASSPHRASE, _));
1195   sync_manager_.GetEncryptionHandler()->SetEncryptionPassphrase(
1196       "new_passphrase", true);
1197   EXPECT_TRUE(EncryptEverythingEnabledForTest());
1198   {
1199     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1200     EXPECT_TRUE(GetEncryptedTypesWithTrans(&trans).Equals(
1201         EncryptableUserTypes()));
1202     EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1203         trans.GetWrappedTrans(),
1204         BOOKMARKS,
1205         true /* is encrypted */));
1206     EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1207         trans.GetWrappedTrans(),
1208         SESSIONS,
1209         true /* is encrypted */));
1210     EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1211         trans.GetWrappedTrans(),
1212         THEMES,
1213         true /* is encrypted */));
1214   }
1215   // Calling EncryptDataTypes with an empty encrypted types should not trigger
1216   // a reencryption and should just notify immediately.
1217   testing::Mock::VerifyAndClearExpectations(&encryption_observer_);
1218   EXPECT_CALL(encryption_observer_,
1219               OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN)).Times(0);
1220   EXPECT_CALL(encryption_observer_, OnPassphraseAccepted()).Times(0);
1221   EXPECT_CALL(encryption_observer_, OnEncryptionComplete()).Times(0);
1222   sync_manager_.GetEncryptionHandler()->EnableEncryptEverything();
1223 }
1224
1225 // Test that when there are no pending keys and the cryptographer is not
1226 // initialized, we add a key based on the current GAIA password.
1227 // (case 1 in SyncManager::SyncInternal::SetEncryptionPassphrase)
1228 TEST_F(SyncManagerTest, SetInitialGaiaPass) {
1229   EXPECT_FALSE(SetUpEncryption(DONT_WRITE_NIGORI, UNINITIALIZED));
1230   EXPECT_CALL(encryption_observer_,
1231               OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1232   EXPECT_CALL(encryption_observer_, OnPassphraseAccepted());
1233   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
1234   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1235   sync_manager_.GetEncryptionHandler()->SetEncryptionPassphrase(
1236       "new_passphrase",
1237       false);
1238   EXPECT_EQ(IMPLICIT_PASSPHRASE,
1239             sync_manager_.GetEncryptionHandler()->GetPassphraseType());
1240   EXPECT_FALSE(EncryptEverythingEnabledForTest());
1241   {
1242     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1243     ReadNode node(&trans);
1244     EXPECT_EQ(BaseNode::INIT_OK, node.InitTypeRoot(NIGORI));
1245     sync_pb::NigoriSpecifics nigori = node.GetNigoriSpecifics();
1246     Cryptographer* cryptographer = trans.GetCryptographer();
1247     EXPECT_TRUE(cryptographer->is_ready());
1248     EXPECT_TRUE(cryptographer->CanDecrypt(nigori.encryption_keybag()));
1249   }
1250 }
1251
1252 // Test that when there are no pending keys and we have on the old GAIA
1253 // password, we update and re-encrypt everything with the new GAIA password.
1254 // (case 1 in SyncManager::SyncInternal::SetEncryptionPassphrase)
1255 TEST_F(SyncManagerTest, UpdateGaiaPass) {
1256   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1257   Cryptographer verifier(&encryptor_);
1258   {
1259     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1260     Cryptographer* cryptographer = trans.GetCryptographer();
1261     std::string bootstrap_token;
1262     cryptographer->GetBootstrapToken(&bootstrap_token);
1263     verifier.Bootstrap(bootstrap_token);
1264   }
1265   EXPECT_CALL(encryption_observer_,
1266               OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1267   EXPECT_CALL(encryption_observer_, OnPassphraseAccepted());
1268   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
1269   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1270   sync_manager_.GetEncryptionHandler()->SetEncryptionPassphrase(
1271       "new_passphrase",
1272       false);
1273   EXPECT_EQ(IMPLICIT_PASSPHRASE,
1274             sync_manager_.GetEncryptionHandler()->GetPassphraseType());
1275   EXPECT_FALSE(EncryptEverythingEnabledForTest());
1276   {
1277     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1278     Cryptographer* cryptographer = trans.GetCryptographer();
1279     EXPECT_TRUE(cryptographer->is_ready());
1280     // Verify the default key has changed.
1281     sync_pb::EncryptedData encrypted;
1282     cryptographer->GetKeys(&encrypted);
1283     EXPECT_FALSE(verifier.CanDecrypt(encrypted));
1284   }
1285 }
1286
1287 // Sets a new explicit passphrase. This should update the bootstrap token
1288 // and re-encrypt everything.
1289 // (case 2 in SyncManager::SyncInternal::SetEncryptionPassphrase)
1290 TEST_F(SyncManagerTest, SetPassphraseWithPassword) {
1291   Cryptographer verifier(&encryptor_);
1292   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1293   {
1294     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1295     // Store the default (soon to be old) key.
1296     Cryptographer* cryptographer = trans.GetCryptographer();
1297     std::string bootstrap_token;
1298     cryptographer->GetBootstrapToken(&bootstrap_token);
1299     verifier.Bootstrap(bootstrap_token);
1300
1301     ReadNode root_node(&trans);
1302     root_node.InitByRootLookup();
1303
1304     WriteNode password_node(&trans);
1305     WriteNode::InitUniqueByCreationResult result =
1306         password_node.InitUniqueByCreation(PASSWORDS,
1307                                            root_node, "foo");
1308     EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
1309     sync_pb::PasswordSpecificsData data;
1310     data.set_password_value("secret");
1311     password_node.SetPasswordSpecifics(data);
1312   }
1313     EXPECT_CALL(encryption_observer_,
1314               OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1315   EXPECT_CALL(encryption_observer_, OnPassphraseAccepted());
1316   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
1317   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1318   EXPECT_CALL(encryption_observer_,
1319       OnPassphraseTypeChanged(CUSTOM_PASSPHRASE, _));
1320   sync_manager_.GetEncryptionHandler()->SetEncryptionPassphrase(
1321       "new_passphrase",
1322       true);
1323   EXPECT_EQ(CUSTOM_PASSPHRASE,
1324             sync_manager_.GetEncryptionHandler()->GetPassphraseType());
1325   EXPECT_FALSE(EncryptEverythingEnabledForTest());
1326   {
1327     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1328     Cryptographer* cryptographer = trans.GetCryptographer();
1329     EXPECT_TRUE(cryptographer->is_ready());
1330     // Verify the default key has changed.
1331     sync_pb::EncryptedData encrypted;
1332     cryptographer->GetKeys(&encrypted);
1333     EXPECT_FALSE(verifier.CanDecrypt(encrypted));
1334
1335     ReadNode password_node(&trans);
1336     EXPECT_EQ(BaseNode::INIT_OK,
1337               password_node.InitByClientTagLookup(PASSWORDS,
1338                                                   "foo"));
1339     const sync_pb::PasswordSpecificsData& data =
1340         password_node.GetPasswordSpecifics();
1341     EXPECT_EQ("secret", data.password_value());
1342   }
1343 }
1344
1345 // Manually set the pending keys in the cryptographer/nigori to reflect the data
1346 // being encrypted with a new (unprovided) GAIA password, then supply the
1347 // password.
1348 // (case 7 in SyncManager::SyncInternal::SetDecryptionPassphrase)
1349 TEST_F(SyncManagerTest, SupplyPendingGAIAPass) {
1350   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1351   Cryptographer other_cryptographer(&encryptor_);
1352   {
1353     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1354     Cryptographer* cryptographer = trans.GetCryptographer();
1355     std::string bootstrap_token;
1356     cryptographer->GetBootstrapToken(&bootstrap_token);
1357     other_cryptographer.Bootstrap(bootstrap_token);
1358
1359     // Now update the nigori to reflect the new keys, and update the
1360     // cryptographer to have pending keys.
1361     KeyParams params = {"localhost", "dummy", "passphrase2"};
1362     other_cryptographer.AddKey(params);
1363     WriteNode node(&trans);
1364     EXPECT_EQ(BaseNode::INIT_OK, node.InitTypeRoot(NIGORI));
1365     sync_pb::NigoriSpecifics nigori;
1366     other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1367     cryptographer->SetPendingKeys(nigori.encryption_keybag());
1368     EXPECT_TRUE(cryptographer->has_pending_keys());
1369     node.SetNigoriSpecifics(nigori);
1370   }
1371   EXPECT_CALL(encryption_observer_,
1372               OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1373   EXPECT_CALL(encryption_observer_, OnPassphraseAccepted());
1374   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
1375   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1376   sync_manager_.GetEncryptionHandler()->SetDecryptionPassphrase("passphrase2");
1377   EXPECT_EQ(IMPLICIT_PASSPHRASE,
1378             sync_manager_.GetEncryptionHandler()->GetPassphraseType());
1379   EXPECT_FALSE(EncryptEverythingEnabledForTest());
1380   {
1381     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1382     Cryptographer* cryptographer = trans.GetCryptographer();
1383     EXPECT_TRUE(cryptographer->is_ready());
1384     // Verify we're encrypting with the new key.
1385     sync_pb::EncryptedData encrypted;
1386     cryptographer->GetKeys(&encrypted);
1387     EXPECT_TRUE(other_cryptographer.CanDecrypt(encrypted));
1388   }
1389 }
1390
1391 // Manually set the pending keys in the cryptographer/nigori to reflect the data
1392 // being encrypted with an old (unprovided) GAIA password. Attempt to supply
1393 // the current GAIA password and verify the bootstrap token is updated. Then
1394 // supply the old GAIA password, and verify we re-encrypt all data with the
1395 // new GAIA password.
1396 // (cases 4 and 5 in SyncManager::SyncInternal::SetEncryptionPassphrase)
1397 TEST_F(SyncManagerTest, SupplyPendingOldGAIAPass) {
1398   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1399   Cryptographer other_cryptographer(&encryptor_);
1400   {
1401     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1402     Cryptographer* cryptographer = trans.GetCryptographer();
1403     std::string bootstrap_token;
1404     cryptographer->GetBootstrapToken(&bootstrap_token);
1405     other_cryptographer.Bootstrap(bootstrap_token);
1406
1407     // Now update the nigori to reflect the new keys, and update the
1408     // cryptographer to have pending keys.
1409     KeyParams params = {"localhost", "dummy", "old_gaia"};
1410     other_cryptographer.AddKey(params);
1411     WriteNode node(&trans);
1412     EXPECT_EQ(BaseNode::INIT_OK, node.InitTypeRoot(NIGORI));
1413     sync_pb::NigoriSpecifics nigori;
1414     other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1415     node.SetNigoriSpecifics(nigori);
1416     cryptographer->SetPendingKeys(nigori.encryption_keybag());
1417
1418     // other_cryptographer now contains all encryption keys, and is encrypting
1419     // with the newest gaia.
1420     KeyParams new_params = {"localhost", "dummy", "new_gaia"};
1421     other_cryptographer.AddKey(new_params);
1422   }
1423   // The bootstrap token should have been updated. Save it to ensure it's based
1424   // on the new GAIA password.
1425   std::string bootstrap_token;
1426   EXPECT_CALL(encryption_observer_,
1427               OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN))
1428       .WillOnce(SaveArg<0>(&bootstrap_token));
1429   EXPECT_CALL(encryption_observer_, OnPassphraseRequired(_,_));
1430   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1431   sync_manager_.GetEncryptionHandler()->SetEncryptionPassphrase(
1432       "new_gaia",
1433       false);
1434   EXPECT_EQ(IMPLICIT_PASSPHRASE,
1435             sync_manager_.GetEncryptionHandler()->GetPassphraseType());
1436   EXPECT_FALSE(EncryptEverythingEnabledForTest());
1437   testing::Mock::VerifyAndClearExpectations(&encryption_observer_);
1438   {
1439     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1440     Cryptographer* cryptographer = trans.GetCryptographer();
1441     EXPECT_TRUE(cryptographer->is_initialized());
1442     EXPECT_FALSE(cryptographer->is_ready());
1443     // Verify we're encrypting with the new key, even though we have pending
1444     // keys.
1445     sync_pb::EncryptedData encrypted;
1446     other_cryptographer.GetKeys(&encrypted);
1447     EXPECT_TRUE(cryptographer->CanDecrypt(encrypted));
1448   }
1449   EXPECT_CALL(encryption_observer_,
1450               OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1451   EXPECT_CALL(encryption_observer_, OnPassphraseAccepted());
1452   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
1453   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1454   sync_manager_.GetEncryptionHandler()->SetEncryptionPassphrase(
1455       "old_gaia",
1456       false);
1457   EXPECT_EQ(IMPLICIT_PASSPHRASE,
1458             sync_manager_.GetEncryptionHandler()->GetPassphraseType());
1459   {
1460     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1461     Cryptographer* cryptographer = trans.GetCryptographer();
1462     EXPECT_TRUE(cryptographer->is_ready());
1463
1464     // Verify we're encrypting with the new key.
1465     sync_pb::EncryptedData encrypted;
1466     other_cryptographer.GetKeys(&encrypted);
1467     EXPECT_TRUE(cryptographer->CanDecrypt(encrypted));
1468
1469     // Verify the saved bootstrap token is based on the new gaia password.
1470     Cryptographer temp_cryptographer(&encryptor_);
1471     temp_cryptographer.Bootstrap(bootstrap_token);
1472     EXPECT_TRUE(temp_cryptographer.CanDecrypt(encrypted));
1473   }
1474 }
1475
1476 // Manually set the pending keys in the cryptographer/nigori to reflect the data
1477 // being encrypted with an explicit (unprovided) passphrase, then supply the
1478 // passphrase.
1479 // (case 9 in SyncManager::SyncInternal::SetDecryptionPassphrase)
1480 TEST_F(SyncManagerTest, SupplyPendingExplicitPass) {
1481   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1482   Cryptographer other_cryptographer(&encryptor_);
1483   {
1484     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1485     Cryptographer* cryptographer = trans.GetCryptographer();
1486     std::string bootstrap_token;
1487     cryptographer->GetBootstrapToken(&bootstrap_token);
1488     other_cryptographer.Bootstrap(bootstrap_token);
1489
1490     // Now update the nigori to reflect the new keys, and update the
1491     // cryptographer to have pending keys.
1492     KeyParams params = {"localhost", "dummy", "explicit"};
1493     other_cryptographer.AddKey(params);
1494     WriteNode node(&trans);
1495     EXPECT_EQ(BaseNode::INIT_OK, node.InitTypeRoot(NIGORI));
1496     sync_pb::NigoriSpecifics nigori;
1497     other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1498     cryptographer->SetPendingKeys(nigori.encryption_keybag());
1499     EXPECT_TRUE(cryptographer->has_pending_keys());
1500     nigori.set_keybag_is_frozen(true);
1501     node.SetNigoriSpecifics(nigori);
1502   }
1503   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1504   EXPECT_CALL(encryption_observer_,
1505               OnPassphraseTypeChanged(CUSTOM_PASSPHRASE, _));
1506   EXPECT_CALL(encryption_observer_, OnPassphraseRequired(_, _));
1507   EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false));
1508   sync_manager_.GetEncryptionHandler()->Init();
1509   EXPECT_CALL(encryption_observer_,
1510               OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1511   EXPECT_CALL(encryption_observer_, OnPassphraseAccepted());
1512   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
1513   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1514   sync_manager_.GetEncryptionHandler()->SetDecryptionPassphrase("explicit");
1515   EXPECT_EQ(CUSTOM_PASSPHRASE,
1516             sync_manager_.GetEncryptionHandler()->GetPassphraseType());
1517   EXPECT_FALSE(EncryptEverythingEnabledForTest());
1518   {
1519     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1520     Cryptographer* cryptographer = trans.GetCryptographer();
1521     EXPECT_TRUE(cryptographer->is_ready());
1522     // Verify we're encrypting with the new key.
1523     sync_pb::EncryptedData encrypted;
1524     cryptographer->GetKeys(&encrypted);
1525     EXPECT_TRUE(other_cryptographer.CanDecrypt(encrypted));
1526   }
1527 }
1528
1529 // Manually set the pending keys in the cryptographer/nigori to reflect the data
1530 // being encrypted with a new (unprovided) GAIA password, then supply the
1531 // password as a user-provided password.
1532 // This is the android case 7/8.
1533 TEST_F(SyncManagerTest, SupplyPendingGAIAPassUserProvided) {
1534   EXPECT_FALSE(SetUpEncryption(DONT_WRITE_NIGORI, UNINITIALIZED));
1535   Cryptographer other_cryptographer(&encryptor_);
1536   {
1537     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1538     Cryptographer* cryptographer = trans.GetCryptographer();
1539     // Now update the nigori to reflect the new keys, and update the
1540     // cryptographer to have pending keys.
1541     KeyParams params = {"localhost", "dummy", "passphrase"};
1542     other_cryptographer.AddKey(params);
1543     WriteNode node(&trans);
1544     EXPECT_EQ(BaseNode::INIT_OK, node.InitTypeRoot(NIGORI));
1545     sync_pb::NigoriSpecifics nigori;
1546     other_cryptographer.GetKeys(nigori.mutable_encryption_keybag());
1547     node.SetNigoriSpecifics(nigori);
1548     cryptographer->SetPendingKeys(nigori.encryption_keybag());
1549     EXPECT_FALSE(cryptographer->is_ready());
1550   }
1551   EXPECT_CALL(encryption_observer_,
1552               OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1553   EXPECT_CALL(encryption_observer_, OnPassphraseAccepted());
1554   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
1555   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1556   sync_manager_.GetEncryptionHandler()->SetEncryptionPassphrase(
1557       "passphrase",
1558       false);
1559   EXPECT_EQ(IMPLICIT_PASSPHRASE,
1560             sync_manager_.GetEncryptionHandler()->GetPassphraseType());
1561   EXPECT_FALSE(EncryptEverythingEnabledForTest());
1562   {
1563     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1564     Cryptographer* cryptographer = trans.GetCryptographer();
1565     EXPECT_TRUE(cryptographer->is_ready());
1566   }
1567 }
1568
1569 TEST_F(SyncManagerTest, SetPassphraseWithEmptyPasswordNode) {
1570   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1571   int64 node_id = 0;
1572   std::string tag = "foo";
1573   {
1574     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1575     ReadNode root_node(&trans);
1576     root_node.InitByRootLookup();
1577
1578     WriteNode password_node(&trans);
1579     WriteNode::InitUniqueByCreationResult result =
1580         password_node.InitUniqueByCreation(PASSWORDS, root_node, tag);
1581     EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
1582     node_id = password_node.GetId();
1583   }
1584   EXPECT_CALL(encryption_observer_,
1585               OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1586   EXPECT_CALL(encryption_observer_, OnPassphraseAccepted());
1587   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
1588   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1589   EXPECT_CALL(encryption_observer_,
1590       OnPassphraseTypeChanged(CUSTOM_PASSPHRASE, _));
1591   sync_manager_.GetEncryptionHandler()->SetEncryptionPassphrase(
1592       "new_passphrase",
1593       true);
1594   EXPECT_EQ(CUSTOM_PASSPHRASE,
1595             sync_manager_.GetEncryptionHandler()->GetPassphraseType());
1596   EXPECT_FALSE(EncryptEverythingEnabledForTest());
1597   {
1598     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1599     ReadNode password_node(&trans);
1600     EXPECT_EQ(BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY,
1601               password_node.InitByClientTagLookup(PASSWORDS,
1602                                                   tag));
1603   }
1604   {
1605     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1606     ReadNode password_node(&trans);
1607     EXPECT_EQ(BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY,
1608               password_node.InitByIdLookup(node_id));
1609   }
1610 }
1611
1612 TEST_F(SyncManagerTest, NudgeDelayTest) {
1613   EXPECT_EQ(sync_manager_.GetNudgeDelayTimeDelta(BOOKMARKS),
1614       base::TimeDelta::FromMilliseconds(
1615           SyncManagerImpl::GetDefaultNudgeDelay()));
1616
1617   EXPECT_EQ(sync_manager_.GetNudgeDelayTimeDelta(AUTOFILL),
1618       base::TimeDelta::FromSeconds(
1619           kDefaultShortPollIntervalSeconds));
1620
1621   EXPECT_EQ(sync_manager_.GetNudgeDelayTimeDelta(PREFERENCES),
1622       base::TimeDelta::FromMilliseconds(
1623           SyncManagerImpl::GetPreferencesNudgeDelay()));
1624 }
1625
1626 // Friended by WriteNode, so can't be in an anonymouse namespace.
1627 TEST_F(SyncManagerTest, EncryptBookmarksWithLegacyData) {
1628   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1629   std::string title;
1630   SyncAPINameToServerName("Google", &title);
1631   std::string url = "http://www.google.com";
1632   std::string raw_title2 = "..";  // An invalid cosmo title.
1633   std::string title2;
1634   SyncAPINameToServerName(raw_title2, &title2);
1635   std::string url2 = "http://www.bla.com";
1636
1637   // Create a bookmark using the legacy format.
1638   int64 node_id1 = MakeNode(sync_manager_.GetUserShare(),
1639       BOOKMARKS,
1640       "testtag");
1641   int64 node_id2 = MakeNode(sync_manager_.GetUserShare(),
1642       BOOKMARKS,
1643       "testtag2");
1644   {
1645     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1646     WriteNode node(&trans);
1647     EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(node_id1));
1648
1649     sync_pb::EntitySpecifics entity_specifics;
1650     entity_specifics.mutable_bookmark()->set_url(url);
1651     node.SetEntitySpecifics(entity_specifics);
1652
1653     // Set the old style title.
1654     syncable::MutableEntry* node_entry = node.entry_;
1655     node_entry->PutNonUniqueName(title);
1656
1657     WriteNode node2(&trans);
1658     EXPECT_EQ(BaseNode::INIT_OK, node2.InitByIdLookup(node_id2));
1659
1660     sync_pb::EntitySpecifics entity_specifics2;
1661     entity_specifics2.mutable_bookmark()->set_url(url2);
1662     node2.SetEntitySpecifics(entity_specifics2);
1663
1664     // Set the old style title.
1665     syncable::MutableEntry* node_entry2 = node2.entry_;
1666     node_entry2->PutNonUniqueName(title2);
1667   }
1668
1669   {
1670     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1671     ReadNode node(&trans);
1672     EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(node_id1));
1673     EXPECT_EQ(BOOKMARKS, node.GetModelType());
1674     EXPECT_EQ(title, node.GetTitle());
1675     EXPECT_EQ(title, node.GetBookmarkSpecifics().title());
1676     EXPECT_EQ(url, node.GetBookmarkSpecifics().url());
1677
1678     ReadNode node2(&trans);
1679     EXPECT_EQ(BaseNode::INIT_OK, node2.InitByIdLookup(node_id2));
1680     EXPECT_EQ(BOOKMARKS, node2.GetModelType());
1681     // We should de-canonicalize the title in GetTitle(), but the title in the
1682     // specifics should be stored in the server legal form.
1683     EXPECT_EQ(raw_title2, node2.GetTitle());
1684     EXPECT_EQ(title2, node2.GetBookmarkSpecifics().title());
1685     EXPECT_EQ(url2, node2.GetBookmarkSpecifics().url());
1686   }
1687
1688   {
1689     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1690     EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1691         trans.GetWrappedTrans(),
1692         BOOKMARKS,
1693         false /* not encrypted */));
1694   }
1695
1696   EXPECT_CALL(encryption_observer_,
1697               OnEncryptedTypesChanged(
1698                   HasModelTypes(EncryptableUserTypes()), true));
1699   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
1700   sync_manager_.GetEncryptionHandler()->EnableEncryptEverything();
1701   EXPECT_TRUE(EncryptEverythingEnabledForTest());
1702
1703   {
1704     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1705     EXPECT_TRUE(GetEncryptedTypesWithTrans(&trans).Equals(
1706         EncryptableUserTypes()));
1707     EXPECT_TRUE(syncable::VerifyDataTypeEncryptionForTest(
1708         trans.GetWrappedTrans(),
1709         BOOKMARKS,
1710         true /* is encrypted */));
1711
1712     ReadNode node(&trans);
1713     EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(node_id1));
1714     EXPECT_EQ(BOOKMARKS, node.GetModelType());
1715     EXPECT_EQ(title, node.GetTitle());
1716     EXPECT_EQ(title, node.GetBookmarkSpecifics().title());
1717     EXPECT_EQ(url, node.GetBookmarkSpecifics().url());
1718
1719     ReadNode node2(&trans);
1720     EXPECT_EQ(BaseNode::INIT_OK, node2.InitByIdLookup(node_id2));
1721     EXPECT_EQ(BOOKMARKS, node2.GetModelType());
1722     // We should de-canonicalize the title in GetTitle(), but the title in the
1723     // specifics should be stored in the server legal form.
1724     EXPECT_EQ(raw_title2, node2.GetTitle());
1725     EXPECT_EQ(title2, node2.GetBookmarkSpecifics().title());
1726     EXPECT_EQ(url2, node2.GetBookmarkSpecifics().url());
1727   }
1728 }
1729
1730 // Create a bookmark and set the title/url, then verify the data was properly
1731 // set. This replicates the unique way bookmarks have of creating sync nodes.
1732 // See BookmarkChangeProcessor::PlaceSyncNode(..).
1733 TEST_F(SyncManagerTest, CreateLocalBookmark) {
1734   std::string title = "title";
1735   std::string url = "url";
1736   {
1737     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1738     ReadNode bookmark_root(&trans);
1739     ASSERT_EQ(BaseNode::INIT_OK, bookmark_root.InitTypeRoot(BOOKMARKS));
1740     WriteNode node(&trans);
1741     ASSERT_TRUE(node.InitBookmarkByCreation(bookmark_root, NULL));
1742     node.SetIsFolder(false);
1743     node.SetTitle(title);
1744
1745     sync_pb::BookmarkSpecifics bookmark_specifics(node.GetBookmarkSpecifics());
1746     bookmark_specifics.set_url(url);
1747     node.SetBookmarkSpecifics(bookmark_specifics);
1748   }
1749   {
1750     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1751     ReadNode bookmark_root(&trans);
1752     ASSERT_EQ(BaseNode::INIT_OK, bookmark_root.InitTypeRoot(BOOKMARKS));
1753     int64 child_id = bookmark_root.GetFirstChildId();
1754
1755     ReadNode node(&trans);
1756     ASSERT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(child_id));
1757     EXPECT_FALSE(node.GetIsFolder());
1758     EXPECT_EQ(title, node.GetTitle());
1759     EXPECT_EQ(url, node.GetBookmarkSpecifics().url());
1760   }
1761 }
1762
1763 // Verifies WriteNode::UpdateEntryWithEncryption does not make unnecessary
1764 // changes.
1765 TEST_F(SyncManagerTest, UpdateEntryWithEncryption) {
1766   std::string client_tag = "title";
1767   sync_pb::EntitySpecifics entity_specifics;
1768   entity_specifics.mutable_bookmark()->set_url("url");
1769   entity_specifics.mutable_bookmark()->set_title("title");
1770   MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag,
1771                  syncable::GenerateSyncableHash(BOOKMARKS,
1772                                                 client_tag),
1773                  entity_specifics);
1774   // New node shouldn't start off unsynced.
1775   EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
1776   // Manually change to the same data. Should not set is_unsynced.
1777   {
1778     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1779     WriteNode node(&trans);
1780     EXPECT_EQ(BaseNode::INIT_OK,
1781               node.InitByClientTagLookup(BOOKMARKS, client_tag));
1782     node.SetEntitySpecifics(entity_specifics);
1783   }
1784   EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
1785
1786   // Encrypt the datatatype, should set is_unsynced.
1787   EXPECT_CALL(encryption_observer_,
1788               OnEncryptedTypesChanged(
1789                   HasModelTypes(EncryptableUserTypes()), true));
1790   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
1791   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION));
1792
1793   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1794   EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, true));
1795   sync_manager_.GetEncryptionHandler()->Init();
1796   PumpLoop();
1797   {
1798     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1799     ReadNode node(&trans);
1800     EXPECT_EQ(BaseNode::INIT_OK,
1801               node.InitByClientTagLookup(BOOKMARKS, client_tag));
1802     const syncable::Entry* node_entry = node.GetEntry();
1803     const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics();
1804     EXPECT_TRUE(specifics.has_encrypted());
1805     EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName());
1806     Cryptographer* cryptographer = trans.GetCryptographer();
1807     EXPECT_TRUE(cryptographer->is_ready());
1808     EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey(
1809         specifics.encrypted()));
1810   }
1811   EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
1812
1813   // Set a new passphrase. Should set is_unsynced.
1814   testing::Mock::VerifyAndClearExpectations(&encryption_observer_);
1815   EXPECT_CALL(encryption_observer_,
1816               OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
1817   EXPECT_CALL(encryption_observer_, OnPassphraseAccepted());
1818   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
1819   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1820   EXPECT_CALL(encryption_observer_,
1821       OnPassphraseTypeChanged(CUSTOM_PASSPHRASE, _));
1822   sync_manager_.GetEncryptionHandler()->SetEncryptionPassphrase(
1823       "new_passphrase",
1824       true);
1825   {
1826     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1827     ReadNode node(&trans);
1828     EXPECT_EQ(BaseNode::INIT_OK,
1829               node.InitByClientTagLookup(BOOKMARKS, client_tag));
1830     const syncable::Entry* node_entry = node.GetEntry();
1831     const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics();
1832     EXPECT_TRUE(specifics.has_encrypted());
1833     EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName());
1834     Cryptographer* cryptographer = trans.GetCryptographer();
1835     EXPECT_TRUE(cryptographer->is_ready());
1836     EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey(
1837         specifics.encrypted()));
1838   }
1839   EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
1840
1841   // Force a re-encrypt everything. Should not set is_unsynced.
1842   testing::Mock::VerifyAndClearExpectations(&encryption_observer_);
1843   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
1844   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
1845   EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, true));
1846
1847   sync_manager_.GetEncryptionHandler()->Init();
1848   PumpLoop();
1849
1850   {
1851     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1852     ReadNode node(&trans);
1853     EXPECT_EQ(BaseNode::INIT_OK,
1854               node.InitByClientTagLookup(BOOKMARKS, client_tag));
1855     const syncable::Entry* node_entry = node.GetEntry();
1856     const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics();
1857     EXPECT_TRUE(specifics.has_encrypted());
1858     EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName());
1859     Cryptographer* cryptographer = trans.GetCryptographer();
1860     EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey(
1861         specifics.encrypted()));
1862   }
1863   EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
1864
1865   // Manually change to the same data. Should not set is_unsynced.
1866   {
1867     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1868     WriteNode node(&trans);
1869     EXPECT_EQ(BaseNode::INIT_OK,
1870               node.InitByClientTagLookup(BOOKMARKS, client_tag));
1871     node.SetEntitySpecifics(entity_specifics);
1872     const syncable::Entry* node_entry = node.GetEntry();
1873     const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics();
1874     EXPECT_TRUE(specifics.has_encrypted());
1875     EXPECT_FALSE(node_entry->GetIsUnsynced());
1876     EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName());
1877     Cryptographer* cryptographer = trans.GetCryptographer();
1878     EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey(
1879         specifics.encrypted()));
1880   }
1881   EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
1882
1883   // Manually change to different data. Should set is_unsynced.
1884   {
1885     entity_specifics.mutable_bookmark()->set_url("url2");
1886     entity_specifics.mutable_bookmark()->set_title("title2");
1887     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1888     WriteNode node(&trans);
1889     EXPECT_EQ(BaseNode::INIT_OK,
1890               node.InitByClientTagLookup(BOOKMARKS, client_tag));
1891     node.SetEntitySpecifics(entity_specifics);
1892     const syncable::Entry* node_entry = node.GetEntry();
1893     const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics();
1894     EXPECT_TRUE(specifics.has_encrypted());
1895     EXPECT_TRUE(node_entry->GetIsUnsynced());
1896     EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName());
1897     Cryptographer* cryptographer = trans.GetCryptographer();
1898     EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey(
1899                     specifics.encrypted()));
1900   }
1901 }
1902
1903 // Passwords have their own handling for encryption. Verify it does not result
1904 // in unnecessary writes via SetEntitySpecifics.
1905 TEST_F(SyncManagerTest, UpdatePasswordSetEntitySpecificsNoChange) {
1906   std::string client_tag = "title";
1907   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1908   sync_pb::EntitySpecifics entity_specifics;
1909   {
1910     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1911     Cryptographer* cryptographer = trans.GetCryptographer();
1912     sync_pb::PasswordSpecificsData data;
1913     data.set_password_value("secret");
1914     cryptographer->Encrypt(
1915         data,
1916         entity_specifics.mutable_password()->
1917             mutable_encrypted());
1918   }
1919   MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, client_tag,
1920                  syncable::GenerateSyncableHash(PASSWORDS,
1921                                                 client_tag),
1922                  entity_specifics);
1923   // New node shouldn't start off unsynced.
1924   EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag));
1925
1926   // Manually change to the same data via SetEntitySpecifics. Should not set
1927   // is_unsynced.
1928   {
1929     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1930     WriteNode node(&trans);
1931     EXPECT_EQ(BaseNode::INIT_OK,
1932               node.InitByClientTagLookup(PASSWORDS, client_tag));
1933     node.SetEntitySpecifics(entity_specifics);
1934   }
1935   EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag));
1936 }
1937
1938 // Passwords have their own handling for encryption. Verify it does not result
1939 // in unnecessary writes via SetPasswordSpecifics.
1940 TEST_F(SyncManagerTest, UpdatePasswordSetPasswordSpecifics) {
1941   std::string client_tag = "title";
1942   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1943   sync_pb::EntitySpecifics entity_specifics;
1944   {
1945     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1946     Cryptographer* cryptographer = trans.GetCryptographer();
1947     sync_pb::PasswordSpecificsData data;
1948     data.set_password_value("secret");
1949     cryptographer->Encrypt(
1950         data,
1951         entity_specifics.mutable_password()->
1952             mutable_encrypted());
1953   }
1954   MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, client_tag,
1955                  syncable::GenerateSyncableHash(PASSWORDS,
1956                                                 client_tag),
1957                  entity_specifics);
1958   // New node shouldn't start off unsynced.
1959   EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag));
1960
1961   // Manually change to the same data via SetPasswordSpecifics. Should not set
1962   // is_unsynced.
1963   {
1964     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1965     WriteNode node(&trans);
1966     EXPECT_EQ(BaseNode::INIT_OK,
1967               node.InitByClientTagLookup(PASSWORDS, client_tag));
1968     node.SetPasswordSpecifics(node.GetPasswordSpecifics());
1969   }
1970   EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag));
1971
1972   // Manually change to different data. Should set is_unsynced.
1973   {
1974     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1975     WriteNode node(&trans);
1976     EXPECT_EQ(BaseNode::INIT_OK,
1977               node.InitByClientTagLookup(PASSWORDS, client_tag));
1978     Cryptographer* cryptographer = trans.GetCryptographer();
1979     sync_pb::PasswordSpecificsData data;
1980     data.set_password_value("secret2");
1981     cryptographer->Encrypt(
1982         data,
1983         entity_specifics.mutable_password()->mutable_encrypted());
1984     node.SetPasswordSpecifics(data);
1985     const syncable::Entry* node_entry = node.GetEntry();
1986     EXPECT_TRUE(node_entry->GetIsUnsynced());
1987   }
1988 }
1989
1990 // Passwords have their own handling for encryption. Verify setting a new
1991 // passphrase updates the data.
1992 TEST_F(SyncManagerTest, UpdatePasswordNewPassphrase) {
1993   std::string client_tag = "title";
1994   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1995   sync_pb::EntitySpecifics entity_specifics;
1996   {
1997     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1998     Cryptographer* cryptographer = trans.GetCryptographer();
1999     sync_pb::PasswordSpecificsData data;
2000     data.set_password_value("secret");
2001     cryptographer->Encrypt(
2002         data,
2003         entity_specifics.mutable_password()->mutable_encrypted());
2004   }
2005   MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, client_tag,
2006                  syncable::GenerateSyncableHash(PASSWORDS,
2007                                                 client_tag),
2008                  entity_specifics);
2009   // New node shouldn't start off unsynced.
2010   EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag));
2011
2012   // Set a new passphrase. Should set is_unsynced.
2013   testing::Mock::VerifyAndClearExpectations(&encryption_observer_);
2014   EXPECT_CALL(encryption_observer_,
2015               OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN));
2016   EXPECT_CALL(encryption_observer_, OnPassphraseAccepted());
2017   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
2018   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
2019   EXPECT_CALL(encryption_observer_,
2020       OnPassphraseTypeChanged(CUSTOM_PASSPHRASE, _));
2021   sync_manager_.GetEncryptionHandler()->SetEncryptionPassphrase(
2022       "new_passphrase",
2023       true);
2024   EXPECT_EQ(CUSTOM_PASSPHRASE,
2025             sync_manager_.GetEncryptionHandler()->GetPassphraseType());
2026   EXPECT_TRUE(ResetUnsyncedEntry(PASSWORDS, client_tag));
2027 }
2028
2029 // Passwords have their own handling for encryption. Verify it does not result
2030 // in unnecessary writes via ReencryptEverything.
2031 TEST_F(SyncManagerTest, UpdatePasswordReencryptEverything) {
2032   std::string client_tag = "title";
2033   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
2034   sync_pb::EntitySpecifics entity_specifics;
2035   {
2036     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2037     Cryptographer* cryptographer = trans.GetCryptographer();
2038     sync_pb::PasswordSpecificsData data;
2039     data.set_password_value("secret");
2040     cryptographer->Encrypt(
2041         data,
2042         entity_specifics.mutable_password()->mutable_encrypted());
2043   }
2044   MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, client_tag,
2045                  syncable::GenerateSyncableHash(PASSWORDS,
2046                                                 client_tag),
2047                  entity_specifics);
2048   // New node shouldn't start off unsynced.
2049   EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag));
2050
2051   // Force a re-encrypt everything. Should not set is_unsynced.
2052   testing::Mock::VerifyAndClearExpectations(&encryption_observer_);
2053   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
2054   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
2055   EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false));
2056   sync_manager_.GetEncryptionHandler()->Init();
2057   PumpLoop();
2058   EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag));
2059 }
2060
2061 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for bookmarks
2062 // when we write the same data, but does set it when we write new data.
2063 TEST_F(SyncManagerTest, SetBookmarkTitle) {
2064   std::string client_tag = "title";
2065   sync_pb::EntitySpecifics entity_specifics;
2066   entity_specifics.mutable_bookmark()->set_url("url");
2067   entity_specifics.mutable_bookmark()->set_title("title");
2068   MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag,
2069                  syncable::GenerateSyncableHash(BOOKMARKS,
2070                                                 client_tag),
2071                  entity_specifics);
2072   // New node shouldn't start off unsynced.
2073   EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2074
2075   // Manually change to the same title. Should not set is_unsynced.
2076   {
2077     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2078     WriteNode node(&trans);
2079     EXPECT_EQ(BaseNode::INIT_OK,
2080               node.InitByClientTagLookup(BOOKMARKS, client_tag));
2081     node.SetTitle(client_tag);
2082   }
2083   EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2084
2085   // Manually change to new title. Should set is_unsynced.
2086   {
2087     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2088     WriteNode node(&trans);
2089     EXPECT_EQ(BaseNode::INIT_OK,
2090               node.InitByClientTagLookup(BOOKMARKS, client_tag));
2091     node.SetTitle("title2");
2092   }
2093   EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2094 }
2095
2096 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for encrypted
2097 // bookmarks when we write the same data, but does set it when we write new
2098 // data.
2099 TEST_F(SyncManagerTest, SetBookmarkTitleWithEncryption) {
2100   std::string client_tag = "title";
2101   sync_pb::EntitySpecifics entity_specifics;
2102   entity_specifics.mutable_bookmark()->set_url("url");
2103   entity_specifics.mutable_bookmark()->set_title("title");
2104   MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag,
2105                  syncable::GenerateSyncableHash(BOOKMARKS,
2106                                                 client_tag),
2107                  entity_specifics);
2108   // New node shouldn't start off unsynced.
2109   EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2110
2111   // Encrypt the datatatype, should set is_unsynced.
2112   EXPECT_CALL(encryption_observer_,
2113               OnEncryptedTypesChanged(
2114                   HasModelTypes(EncryptableUserTypes()), true));
2115   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
2116   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION));
2117   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
2118   EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, true));
2119   sync_manager_.GetEncryptionHandler()->Init();
2120   PumpLoop();
2121   EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2122
2123   // Manually change to the same title. Should not set is_unsynced.
2124   // NON_UNIQUE_NAME should be kEncryptedString.
2125   {
2126     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2127     WriteNode node(&trans);
2128     EXPECT_EQ(BaseNode::INIT_OK,
2129               node.InitByClientTagLookup(BOOKMARKS, client_tag));
2130     node.SetTitle(client_tag);
2131     const syncable::Entry* node_entry = node.GetEntry();
2132     const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics();
2133     EXPECT_TRUE(specifics.has_encrypted());
2134     EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName());
2135   }
2136   EXPECT_FALSE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2137
2138   // Manually change to new title. Should set is_unsynced. NON_UNIQUE_NAME
2139   // should still be kEncryptedString.
2140   {
2141     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2142     WriteNode node(&trans);
2143     EXPECT_EQ(BaseNode::INIT_OK,
2144               node.InitByClientTagLookup(BOOKMARKS, client_tag));
2145     node.SetTitle("title2");
2146     const syncable::Entry* node_entry = node.GetEntry();
2147     const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics();
2148     EXPECT_TRUE(specifics.has_encrypted());
2149     EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName());
2150   }
2151   EXPECT_TRUE(ResetUnsyncedEntry(BOOKMARKS, client_tag));
2152 }
2153
2154 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for non-bookmarks
2155 // when we write the same data, but does set it when we write new data.
2156 TEST_F(SyncManagerTest, SetNonBookmarkTitle) {
2157   std::string client_tag = "title";
2158   sync_pb::EntitySpecifics entity_specifics;
2159   entity_specifics.mutable_preference()->set_name("name");
2160   entity_specifics.mutable_preference()->set_value("value");
2161   MakeServerNode(sync_manager_.GetUserShare(),
2162                  PREFERENCES,
2163                  client_tag,
2164                  syncable::GenerateSyncableHash(PREFERENCES,
2165                                                 client_tag),
2166                  entity_specifics);
2167   // New node shouldn't start off unsynced.
2168   EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES, client_tag));
2169
2170   // Manually change to the same title. Should not set is_unsynced.
2171   {
2172     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2173     WriteNode node(&trans);
2174     EXPECT_EQ(BaseNode::INIT_OK,
2175               node.InitByClientTagLookup(PREFERENCES, client_tag));
2176     node.SetTitle(client_tag);
2177   }
2178   EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES, client_tag));
2179
2180   // Manually change to new title. Should set is_unsynced.
2181   {
2182     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2183     WriteNode node(&trans);
2184     EXPECT_EQ(BaseNode::INIT_OK,
2185               node.InitByClientTagLookup(PREFERENCES, client_tag));
2186     node.SetTitle("title2");
2187   }
2188   EXPECT_TRUE(ResetUnsyncedEntry(PREFERENCES, client_tag));
2189 }
2190
2191 // Verify SetTitle(..) doesn't unnecessarily set IS_UNSYNCED for encrypted
2192 // non-bookmarks when we write the same data or when we write new data
2193 // data (should remained kEncryptedString).
2194 TEST_F(SyncManagerTest, SetNonBookmarkTitleWithEncryption) {
2195   std::string client_tag = "title";
2196   sync_pb::EntitySpecifics entity_specifics;
2197   entity_specifics.mutable_preference()->set_name("name");
2198   entity_specifics.mutable_preference()->set_value("value");
2199   MakeServerNode(sync_manager_.GetUserShare(),
2200                  PREFERENCES,
2201                  client_tag,
2202                  syncable::GenerateSyncableHash(PREFERENCES,
2203                                                 client_tag),
2204                  entity_specifics);
2205   // New node shouldn't start off unsynced.
2206   EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES, client_tag));
2207
2208   // Encrypt the datatatype, should set is_unsynced.
2209   EXPECT_CALL(encryption_observer_,
2210               OnEncryptedTypesChanged(
2211                   HasModelTypes(EncryptableUserTypes()), true));
2212   EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
2213   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION));
2214   EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
2215   EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, true));
2216   sync_manager_.GetEncryptionHandler()->Init();
2217   PumpLoop();
2218   EXPECT_TRUE(ResetUnsyncedEntry(PREFERENCES, client_tag));
2219
2220   // Manually change to the same title. Should not set is_unsynced.
2221   // NON_UNIQUE_NAME should be kEncryptedString.
2222   {
2223     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2224     WriteNode node(&trans);
2225     EXPECT_EQ(BaseNode::INIT_OK,
2226               node.InitByClientTagLookup(PREFERENCES, client_tag));
2227     node.SetTitle(client_tag);
2228     const syncable::Entry* node_entry = node.GetEntry();
2229     const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics();
2230     EXPECT_TRUE(specifics.has_encrypted());
2231     EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName());
2232   }
2233   EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES, client_tag));
2234
2235   // Manually change to new title. Should not set is_unsynced because the
2236   // NON_UNIQUE_NAME should still be kEncryptedString.
2237   {
2238     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2239     WriteNode node(&trans);
2240     EXPECT_EQ(BaseNode::INIT_OK,
2241               node.InitByClientTagLookup(PREFERENCES, client_tag));
2242     node.SetTitle("title2");
2243     const syncable::Entry* node_entry = node.GetEntry();
2244     const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics();
2245     EXPECT_TRUE(specifics.has_encrypted());
2246     EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName());
2247     EXPECT_FALSE(node_entry->GetIsUnsynced());
2248   }
2249 }
2250
2251 // Ensure that titles are truncated to 255 bytes, and attempting to reset
2252 // them to their longer version does not set IS_UNSYNCED.
2253 TEST_F(SyncManagerTest, SetLongTitle) {
2254   const int kNumChars = 512;
2255   const std::string kClientTag = "tag";
2256   std::string title(kNumChars, '0');
2257   sync_pb::EntitySpecifics entity_specifics;
2258   entity_specifics.mutable_preference()->set_name("name");
2259   entity_specifics.mutable_preference()->set_value("value");
2260   MakeServerNode(sync_manager_.GetUserShare(),
2261                  PREFERENCES,
2262                  "short_title",
2263                  syncable::GenerateSyncableHash(PREFERENCES,
2264                                                 kClientTag),
2265                  entity_specifics);
2266   // New node shouldn't start off unsynced.
2267   EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES, kClientTag));
2268
2269   // Manually change to the long title. Should set is_unsynced.
2270   {
2271     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2272     WriteNode node(&trans);
2273     EXPECT_EQ(BaseNode::INIT_OK,
2274               node.InitByClientTagLookup(PREFERENCES, kClientTag));
2275     node.SetTitle(title);
2276     EXPECT_EQ(node.GetTitle(), title.substr(0, 255));
2277   }
2278   EXPECT_TRUE(ResetUnsyncedEntry(PREFERENCES, kClientTag));
2279
2280   // Manually change to the same title. Should not set is_unsynced.
2281   {
2282     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2283     WriteNode node(&trans);
2284     EXPECT_EQ(BaseNode::INIT_OK,
2285               node.InitByClientTagLookup(PREFERENCES, kClientTag));
2286     node.SetTitle(title);
2287     EXPECT_EQ(node.GetTitle(), title.substr(0, 255));
2288   }
2289   EXPECT_FALSE(ResetUnsyncedEntry(PREFERENCES, kClientTag));
2290
2291   // Manually change to new title. Should set is_unsynced.
2292   {
2293     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2294     WriteNode node(&trans);
2295     EXPECT_EQ(BaseNode::INIT_OK,
2296               node.InitByClientTagLookup(PREFERENCES, kClientTag));
2297     node.SetTitle("title2");
2298   }
2299   EXPECT_TRUE(ResetUnsyncedEntry(PREFERENCES, kClientTag));
2300 }
2301
2302 // Create an encrypted entry when the cryptographer doesn't think the type is
2303 // marked for encryption. Ensure reads/writes don't break and don't unencrypt
2304 // the data.
2305 TEST_F(SyncManagerTest, SetPreviouslyEncryptedSpecifics) {
2306   std::string client_tag = "tag";
2307   std::string url = "url";
2308   std::string url2 = "new_url";
2309   std::string title = "title";
2310   sync_pb::EntitySpecifics entity_specifics;
2311   EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
2312   {
2313     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2314     Cryptographer* crypto = trans.GetCryptographer();
2315     sync_pb::EntitySpecifics bm_specifics;
2316     bm_specifics.mutable_bookmark()->set_title("title");
2317     bm_specifics.mutable_bookmark()->set_url("url");
2318     sync_pb::EncryptedData encrypted;
2319     crypto->Encrypt(bm_specifics, &encrypted);
2320     entity_specifics.mutable_encrypted()->CopyFrom(encrypted);
2321     AddDefaultFieldValue(BOOKMARKS, &entity_specifics);
2322   }
2323   MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag,
2324                  syncable::GenerateSyncableHash(BOOKMARKS,
2325                                                 client_tag),
2326                  entity_specifics);
2327
2328   {
2329     // Verify the data.
2330     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2331     ReadNode node(&trans);
2332     EXPECT_EQ(BaseNode::INIT_OK,
2333               node.InitByClientTagLookup(BOOKMARKS, client_tag));
2334     EXPECT_EQ(title, node.GetTitle());
2335     EXPECT_EQ(url, node.GetBookmarkSpecifics().url());
2336   }
2337
2338   {
2339     // Overwrite the url (which overwrites the specifics).
2340     WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2341     WriteNode node(&trans);
2342     EXPECT_EQ(BaseNode::INIT_OK,
2343               node.InitByClientTagLookup(BOOKMARKS, client_tag));
2344
2345     sync_pb::BookmarkSpecifics bookmark_specifics(node.GetBookmarkSpecifics());
2346     bookmark_specifics.set_url(url2);
2347     node.SetBookmarkSpecifics(bookmark_specifics);
2348   }
2349
2350   {
2351     // Verify it's still encrypted and it has the most recent url.
2352     ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2353     ReadNode node(&trans);
2354     EXPECT_EQ(BaseNode::INIT_OK,
2355               node.InitByClientTagLookup(BOOKMARKS, client_tag));
2356     EXPECT_EQ(title, node.GetTitle());
2357     EXPECT_EQ(url2, node.GetBookmarkSpecifics().url());
2358     const syncable::Entry* node_entry = node.GetEntry();
2359     EXPECT_EQ(kEncryptedString, node_entry->GetNonUniqueName());
2360     const sync_pb::EntitySpecifics& specifics = node_entry->GetSpecifics();
2361     EXPECT_TRUE(specifics.has_encrypted());
2362   }
2363 }
2364
2365 // Verify transaction version of a model type is incremented when node of
2366 // that type is updated.
2367 TEST_F(SyncManagerTest, IncrementTransactionVersion) {
2368   ModelSafeRoutingInfo routing_info;
2369   GetModelSafeRoutingInfo(&routing_info);
2370
2371   {
2372     ReadTransaction read_trans(FROM_HERE, sync_manager_.GetUserShare());
2373     for (ModelSafeRoutingInfo::iterator i = routing_info.begin();
2374          i != routing_info.end(); ++i) {
2375       // Transaction version is incremented when SyncManagerTest::SetUp()
2376       // creates a node of each type.
2377       EXPECT_EQ(1,
2378                 sync_manager_.GetUserShare()->directory->
2379                     GetTransactionVersion(i->first));
2380     }
2381   }
2382
2383   // Create bookmark node to increment transaction version of bookmark model.
2384   std::string client_tag = "title";
2385   sync_pb::EntitySpecifics entity_specifics;
2386   entity_specifics.mutable_bookmark()->set_url("url");
2387   entity_specifics.mutable_bookmark()->set_title("title");
2388   MakeServerNode(sync_manager_.GetUserShare(), BOOKMARKS, client_tag,
2389                  syncable::GenerateSyncableHash(BOOKMARKS,
2390                                                 client_tag),
2391                  entity_specifics);
2392
2393   {
2394     ReadTransaction read_trans(FROM_HERE, sync_manager_.GetUserShare());
2395     for (ModelSafeRoutingInfo::iterator i = routing_info.begin();
2396          i != routing_info.end(); ++i) {
2397       EXPECT_EQ(i->first == BOOKMARKS ? 2 : 1,
2398                 sync_manager_.GetUserShare()->directory->
2399                     GetTransactionVersion(i->first));
2400     }
2401   }
2402 }
2403
2404 class MockSyncScheduler : public FakeSyncScheduler {
2405  public:
2406   MockSyncScheduler() : FakeSyncScheduler() {}
2407   virtual ~MockSyncScheduler() {}
2408
2409   MOCK_METHOD1(Start, void(SyncScheduler::Mode));
2410   MOCK_METHOD1(ScheduleConfiguration, void(const ConfigurationParams&));
2411 };
2412
2413 class ComponentsFactory : public TestInternalComponentsFactory {
2414  public:
2415   ComponentsFactory(const Switches& switches,
2416                     SyncScheduler* scheduler_to_use,
2417                     sessions::SyncSessionContext** session_context,
2418                     InternalComponentsFactory::StorageOption* storage_used)
2419       : TestInternalComponentsFactory(
2420           switches, InternalComponentsFactory::STORAGE_IN_MEMORY, storage_used),
2421         scheduler_to_use_(scheduler_to_use),
2422         session_context_(session_context) {}
2423   virtual ~ComponentsFactory() {}
2424
2425   virtual scoped_ptr<SyncScheduler> BuildScheduler(
2426       const std::string& name,
2427       sessions::SyncSessionContext* context,
2428       CancelationSignal* stop_handle) OVERRIDE {
2429     *session_context_ = context;
2430     return scheduler_to_use_.Pass();
2431   }
2432
2433  private:
2434   scoped_ptr<SyncScheduler> scheduler_to_use_;
2435   sessions::SyncSessionContext** session_context_;
2436 };
2437
2438 class SyncManagerTestWithMockScheduler : public SyncManagerTest {
2439  public:
2440   SyncManagerTestWithMockScheduler() : scheduler_(NULL) {}
2441   virtual InternalComponentsFactory* GetFactory() OVERRIDE {
2442     scheduler_ = new MockSyncScheduler();
2443     return new ComponentsFactory(GetSwitches(), scheduler_, &session_context_,
2444                                  &storage_used_);
2445   }
2446
2447   MockSyncScheduler* scheduler() { return scheduler_; }
2448   sessions::SyncSessionContext* session_context() {
2449       return session_context_;
2450   }
2451
2452  private:
2453   MockSyncScheduler* scheduler_;
2454   sessions::SyncSessionContext* session_context_;
2455 };
2456
2457 // Test that the configuration params are properly created and sent to
2458 // ScheduleConfigure. No callback should be invoked. Any disabled datatypes
2459 // should be purged.
2460 TEST_F(SyncManagerTestWithMockScheduler, BasicConfiguration) {
2461   ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION;
2462   ModelTypeSet types_to_download(BOOKMARKS, PREFERENCES);
2463   ModelSafeRoutingInfo new_routing_info;
2464   GetModelSafeRoutingInfo(&new_routing_info);
2465   ModelTypeSet enabled_types = GetRoutingInfoTypes(new_routing_info);
2466   ModelTypeSet disabled_types = Difference(ModelTypeSet::All(), enabled_types);
2467
2468   ConfigurationParams params;
2469   EXPECT_CALL(*scheduler(), Start(SyncScheduler::CONFIGURATION_MODE));
2470   EXPECT_CALL(*scheduler(), ScheduleConfiguration(_)).
2471       WillOnce(SaveArg<0>(&params));
2472
2473   // Set data for all types.
2474   ModelTypeSet protocol_types = ProtocolTypes();
2475   for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good();
2476        iter.Inc()) {
2477     SetProgressMarkerForType(iter.Get(), true);
2478   }
2479
2480   CallbackCounter ready_task_counter, retry_task_counter;
2481   sync_manager_.ConfigureSyncer(
2482       reason,
2483       types_to_download,
2484       disabled_types,
2485       ModelTypeSet(),
2486       ModelTypeSet(),
2487       new_routing_info,
2488       base::Bind(&CallbackCounter::Callback,
2489                  base::Unretained(&ready_task_counter)),
2490       base::Bind(&CallbackCounter::Callback,
2491                  base::Unretained(&retry_task_counter)));
2492   EXPECT_EQ(0, ready_task_counter.times_called());
2493   EXPECT_EQ(0, retry_task_counter.times_called());
2494   EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION,
2495             params.source);
2496   EXPECT_TRUE(types_to_download.Equals(params.types_to_download));
2497   EXPECT_EQ(new_routing_info, params.routing_info);
2498
2499   // Verify all the disabled types were purged.
2500   EXPECT_TRUE(sync_manager_.InitialSyncEndedTypes().Equals(
2501       enabled_types));
2502   EXPECT_TRUE(sync_manager_.GetTypesWithEmptyProgressMarkerToken(
2503       ModelTypeSet::All()).Equals(disabled_types));
2504 }
2505
2506 // Test that on a reconfiguration (configuration where the session context
2507 // already has routing info), only those recently disabled types are purged.
2508 TEST_F(SyncManagerTestWithMockScheduler, ReConfiguration) {
2509   ConfigureReason reason = CONFIGURE_REASON_RECONFIGURATION;
2510   ModelTypeSet types_to_download(BOOKMARKS, PREFERENCES);
2511   ModelTypeSet disabled_types = ModelTypeSet(THEMES, SESSIONS);
2512   ModelSafeRoutingInfo old_routing_info;
2513   ModelSafeRoutingInfo new_routing_info;
2514   GetModelSafeRoutingInfo(&old_routing_info);
2515   new_routing_info = old_routing_info;
2516   new_routing_info.erase(THEMES);
2517   new_routing_info.erase(SESSIONS);
2518   ModelTypeSet enabled_types = GetRoutingInfoTypes(new_routing_info);
2519
2520   ConfigurationParams params;
2521   EXPECT_CALL(*scheduler(), Start(SyncScheduler::CONFIGURATION_MODE));
2522   EXPECT_CALL(*scheduler(), ScheduleConfiguration(_)).
2523       WillOnce(SaveArg<0>(&params));
2524
2525   // Set data for all types except those recently disabled (so we can verify
2526   // only those recently disabled are purged) .
2527   ModelTypeSet protocol_types = ProtocolTypes();
2528   for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good();
2529        iter.Inc()) {
2530     if (!disabled_types.Has(iter.Get())) {
2531       SetProgressMarkerForType(iter.Get(), true);
2532     } else {
2533       SetProgressMarkerForType(iter.Get(), false);
2534     }
2535   }
2536
2537   // Set the context to have the old routing info.
2538   session_context()->SetRoutingInfo(old_routing_info);
2539
2540   CallbackCounter ready_task_counter, retry_task_counter;
2541   sync_manager_.ConfigureSyncer(
2542       reason,
2543       types_to_download,
2544       ModelTypeSet(),
2545       ModelTypeSet(),
2546       ModelTypeSet(),
2547       new_routing_info,
2548       base::Bind(&CallbackCounter::Callback,
2549                  base::Unretained(&ready_task_counter)),
2550       base::Bind(&CallbackCounter::Callback,
2551                  base::Unretained(&retry_task_counter)));
2552   EXPECT_EQ(0, ready_task_counter.times_called());
2553   EXPECT_EQ(0, retry_task_counter.times_called());
2554   EXPECT_EQ(sync_pb::GetUpdatesCallerInfo::RECONFIGURATION,
2555             params.source);
2556   EXPECT_TRUE(types_to_download.Equals(params.types_to_download));
2557   EXPECT_EQ(new_routing_info, params.routing_info);
2558
2559   // Verify only the recently disabled types were purged.
2560   EXPECT_TRUE(sync_manager_.GetTypesWithEmptyProgressMarkerToken(
2561       ProtocolTypes()).Equals(disabled_types));
2562 }
2563
2564 // Test that PurgePartiallySyncedTypes purges only those types that have not
2565 // fully completed their initial download and apply.
2566 TEST_F(SyncManagerTest, PurgePartiallySyncedTypes) {
2567   ModelSafeRoutingInfo routing_info;
2568   GetModelSafeRoutingInfo(&routing_info);
2569   ModelTypeSet enabled_types = GetRoutingInfoTypes(routing_info);
2570
2571   UserShare* share = sync_manager_.GetUserShare();
2572
2573   // The test harness automatically initializes all types in the routing info.
2574   // Check that autofill is not among them.
2575   ASSERT_FALSE(enabled_types.Has(AUTOFILL));
2576
2577   // Further ensure that the test harness did not create its root node.
2578   {
2579     syncable::ReadTransaction trans(FROM_HERE, share->directory.get());
2580     syncable::Entry autofill_root_node(&trans,
2581                                        syncable::GET_TYPE_ROOT,
2582                                        AUTOFILL);
2583     ASSERT_FALSE(autofill_root_node.good());
2584   }
2585
2586   // One more redundant check.
2587   ASSERT_FALSE(sync_manager_.InitialSyncEndedTypes().Has(AUTOFILL));
2588
2589   // Give autofill a progress marker.
2590   sync_pb::DataTypeProgressMarker autofill_marker;
2591   autofill_marker.set_data_type_id(
2592       GetSpecificsFieldNumberFromModelType(AUTOFILL));
2593   autofill_marker.set_token("token");
2594   share->directory->SetDownloadProgress(AUTOFILL, autofill_marker);
2595
2596   // Also add a pending autofill root node update from the server.
2597   TestEntryFactory factory_(share->directory.get());
2598   int autofill_meta = factory_.CreateUnappliedRootNode(AUTOFILL);
2599
2600   // Preferences is an enabled type.  Check that the harness initialized it.
2601   ASSERT_TRUE(enabled_types.Has(PREFERENCES));
2602   ASSERT_TRUE(sync_manager_.InitialSyncEndedTypes().Has(PREFERENCES));
2603
2604   // Give preferencse a progress marker.
2605   sync_pb::DataTypeProgressMarker prefs_marker;
2606   prefs_marker.set_data_type_id(
2607       GetSpecificsFieldNumberFromModelType(PREFERENCES));
2608   prefs_marker.set_token("token");
2609   share->directory->SetDownloadProgress(PREFERENCES, prefs_marker);
2610
2611   // Add a fully synced preferences node under the root.
2612   std::string pref_client_tag = "prefABC";
2613   std::string pref_hashed_tag = "hashXYZ";
2614   sync_pb::EntitySpecifics pref_specifics;
2615   AddDefaultFieldValue(PREFERENCES, &pref_specifics);
2616   int pref_meta = MakeServerNode(
2617       share, PREFERENCES, pref_client_tag, pref_hashed_tag, pref_specifics);
2618
2619   // And now, the purge.
2620   EXPECT_TRUE(sync_manager_.PurgePartiallySyncedTypes());
2621
2622   // Ensure that autofill lost its progress marker, but preferences did not.
2623   ModelTypeSet empty_tokens =
2624       sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All());
2625   EXPECT_TRUE(empty_tokens.Has(AUTOFILL));
2626   EXPECT_FALSE(empty_tokens.Has(PREFERENCES));
2627
2628   // Ensure that autofill lots its node, but preferences did not.
2629   {
2630     syncable::ReadTransaction trans(FROM_HERE, share->directory.get());
2631     syncable::Entry autofill_node(&trans, GET_BY_HANDLE, autofill_meta);
2632     syncable::Entry pref_node(&trans, GET_BY_HANDLE, pref_meta);
2633     EXPECT_FALSE(autofill_node.good());
2634     EXPECT_TRUE(pref_node.good());
2635   }
2636 }
2637
2638 // Test CleanupDisabledTypes properly purges all disabled types as specified
2639 // by the previous and current enabled params.
2640 TEST_F(SyncManagerTest, PurgeDisabledTypes) {
2641   ModelSafeRoutingInfo routing_info;
2642   GetModelSafeRoutingInfo(&routing_info);
2643   ModelTypeSet enabled_types = GetRoutingInfoTypes(routing_info);
2644   ModelTypeSet disabled_types = Difference(ModelTypeSet::All(), enabled_types);
2645
2646   // The harness should have initialized the enabled_types for us.
2647   EXPECT_TRUE(enabled_types.Equals(sync_manager_.InitialSyncEndedTypes()));
2648
2649   // Set progress markers for all types.
2650   ModelTypeSet protocol_types = ProtocolTypes();
2651   for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good();
2652        iter.Inc()) {
2653     SetProgressMarkerForType(iter.Get(), true);
2654   }
2655
2656   // Verify all the enabled types remain after cleanup, and all the disabled
2657   // types were purged.
2658   sync_manager_.PurgeDisabledTypes(disabled_types,
2659                                    ModelTypeSet(),
2660                                    ModelTypeSet());
2661   EXPECT_TRUE(enabled_types.Equals(sync_manager_.InitialSyncEndedTypes()));
2662   EXPECT_TRUE(disabled_types.Equals(
2663       sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All())));
2664
2665   // Disable some more types.
2666   disabled_types.Put(BOOKMARKS);
2667   disabled_types.Put(PREFERENCES);
2668   ModelTypeSet new_enabled_types =
2669       Difference(ModelTypeSet::All(), disabled_types);
2670
2671   // Verify only the non-disabled types remain after cleanup.
2672   sync_manager_.PurgeDisabledTypes(disabled_types,
2673                                    ModelTypeSet(),
2674                                    ModelTypeSet());
2675   EXPECT_TRUE(new_enabled_types.Equals(sync_manager_.InitialSyncEndedTypes()));
2676   EXPECT_TRUE(disabled_types.Equals(
2677       sync_manager_.GetTypesWithEmptyProgressMarkerToken(ModelTypeSet::All())));
2678 }
2679
2680 // Test PurgeDisabledTypes properly unapplies types by deleting their local data
2681 // and preserving their server data and progress marker.
2682 TEST_F(SyncManagerTest, PurgeUnappliedTypes) {
2683   ModelSafeRoutingInfo routing_info;
2684   GetModelSafeRoutingInfo(&routing_info);
2685   ModelTypeSet unapplied_types = ModelTypeSet(BOOKMARKS, PREFERENCES);
2686   ModelTypeSet enabled_types = GetRoutingInfoTypes(routing_info);
2687   ModelTypeSet disabled_types = Difference(ModelTypeSet::All(), enabled_types);
2688
2689   // The harness should have initialized the enabled_types for us.
2690   EXPECT_TRUE(enabled_types.Equals(sync_manager_.InitialSyncEndedTypes()));
2691
2692   // Set progress markers for all types.
2693   ModelTypeSet protocol_types = ProtocolTypes();
2694   for (ModelTypeSet::Iterator iter = protocol_types.First(); iter.Good();
2695        iter.Inc()) {
2696     SetProgressMarkerForType(iter.Get(), true);
2697   }
2698
2699   // Add the following kinds of items:
2700   // 1. Fully synced preference.
2701   // 2. Locally created preference, server unknown, unsynced
2702   // 3. Locally deleted preference, server known, unsynced
2703   // 4. Server deleted preference, locally known.
2704   // 5. Server created preference, locally unknown, unapplied.
2705   // 6. A fully synced bookmark (no unique_client_tag).
2706   UserShare* share = sync_manager_.GetUserShare();
2707   sync_pb::EntitySpecifics pref_specifics;
2708   AddDefaultFieldValue(PREFERENCES, &pref_specifics);
2709   sync_pb::EntitySpecifics bm_specifics;
2710   AddDefaultFieldValue(BOOKMARKS, &bm_specifics);
2711   int pref1_meta = MakeServerNode(
2712       share, PREFERENCES, "pref1", "hash1", pref_specifics);
2713   int64 pref2_meta = MakeNode(share, PREFERENCES, "pref2");
2714   int pref3_meta = MakeServerNode(
2715       share, PREFERENCES, "pref3", "hash3", pref_specifics);
2716   int pref4_meta = MakeServerNode(
2717       share, PREFERENCES, "pref4", "hash4", pref_specifics);
2718   int pref5_meta = MakeServerNode(
2719       share, PREFERENCES, "pref5", "hash5", pref_specifics);
2720   int bookmark_meta = MakeServerNode(
2721       share, BOOKMARKS, "bookmark", "", bm_specifics);
2722
2723   {
2724     syncable::WriteTransaction trans(FROM_HERE,
2725                                      syncable::SYNCER,
2726                                      share->directory.get());
2727     // Pref's 1 and 2 are already set up properly.
2728     // Locally delete pref 3.
2729     syncable::MutableEntry pref3(&trans, GET_BY_HANDLE, pref3_meta);
2730     pref3.PutIsDel(true);
2731     pref3.PutIsUnsynced(true);
2732     // Delete pref 4 at the server.
2733     syncable::MutableEntry pref4(&trans, GET_BY_HANDLE, pref4_meta);
2734     pref4.PutServerIsDel(true);
2735     pref4.PutIsUnappliedUpdate(true);
2736     pref4.PutServerVersion(2);
2737     // Pref 5 is an new unapplied update.
2738     syncable::MutableEntry pref5(&trans, GET_BY_HANDLE, pref5_meta);
2739     pref5.PutIsUnappliedUpdate(true);
2740     pref5.PutIsDel(true);
2741     pref5.PutBaseVersion(-1);
2742     // Bookmark is already set up properly
2743   }
2744
2745   // Take a snapshot to clear all the dirty bits.
2746   share->directory.get()->SaveChanges();
2747
2748   // Now request a purge for the unapplied types.
2749   disabled_types.PutAll(unapplied_types);
2750   sync_manager_.PurgeDisabledTypes(disabled_types,
2751                                    ModelTypeSet(),
2752                                    unapplied_types);
2753
2754   // Verify the unapplied types still have progress markers and initial sync
2755   // ended after cleanup.
2756   EXPECT_TRUE(sync_manager_.InitialSyncEndedTypes().HasAll(unapplied_types));
2757   EXPECT_TRUE(
2758       sync_manager_.GetTypesWithEmptyProgressMarkerToken(unapplied_types).
2759           Empty());
2760
2761   // Ensure the items were unapplied as necessary.
2762   {
2763     syncable::ReadTransaction trans(FROM_HERE, share->directory.get());
2764     syncable::Entry pref_node(&trans, GET_BY_HANDLE, pref1_meta);
2765     ASSERT_TRUE(pref_node.good());
2766     EXPECT_TRUE(pref_node.GetKernelCopy().is_dirty());
2767     EXPECT_FALSE(pref_node.GetIsUnsynced());
2768     EXPECT_TRUE(pref_node.GetIsUnappliedUpdate());
2769     EXPECT_TRUE(pref_node.GetIsDel());
2770     EXPECT_GT(pref_node.GetServerVersion(), 0);
2771     EXPECT_EQ(pref_node.GetBaseVersion(), -1);
2772
2773     // Pref 2 should just be locally deleted.
2774     syncable::Entry pref2_node(&trans, GET_BY_HANDLE, pref2_meta);
2775     ASSERT_TRUE(pref2_node.good());
2776     EXPECT_TRUE(pref2_node.GetKernelCopy().is_dirty());
2777     EXPECT_FALSE(pref2_node.GetIsUnsynced());
2778     EXPECT_TRUE(pref2_node.GetIsDel());
2779     EXPECT_FALSE(pref2_node.GetIsUnappliedUpdate());
2780     EXPECT_TRUE(pref2_node.GetIsDel());
2781     EXPECT_EQ(pref2_node.GetServerVersion(), 0);
2782     EXPECT_EQ(pref2_node.GetBaseVersion(), -1);
2783
2784     syncable::Entry pref3_node(&trans, GET_BY_HANDLE, pref3_meta);
2785     ASSERT_TRUE(pref3_node.good());
2786     EXPECT_TRUE(pref3_node.GetKernelCopy().is_dirty());
2787     EXPECT_FALSE(pref3_node.GetIsUnsynced());
2788     EXPECT_TRUE(pref3_node.GetIsUnappliedUpdate());
2789     EXPECT_TRUE(pref3_node.GetIsDel());
2790     EXPECT_GT(pref3_node.GetServerVersion(), 0);
2791     EXPECT_EQ(pref3_node.GetBaseVersion(), -1);
2792
2793     syncable::Entry pref4_node(&trans, GET_BY_HANDLE, pref4_meta);
2794     ASSERT_TRUE(pref4_node.good());
2795     EXPECT_TRUE(pref4_node.GetKernelCopy().is_dirty());
2796     EXPECT_FALSE(pref4_node.GetIsUnsynced());
2797     EXPECT_TRUE(pref4_node.GetIsUnappliedUpdate());
2798     EXPECT_TRUE(pref4_node.GetIsDel());
2799     EXPECT_GT(pref4_node.GetServerVersion(), 0);
2800     EXPECT_EQ(pref4_node.GetBaseVersion(), -1);
2801
2802     // Pref 5 should remain untouched.
2803     syncable::Entry pref5_node(&trans, GET_BY_HANDLE, pref5_meta);
2804     ASSERT_TRUE(pref5_node.good());
2805     EXPECT_FALSE(pref5_node.GetKernelCopy().is_dirty());
2806     EXPECT_FALSE(pref5_node.GetIsUnsynced());
2807     EXPECT_TRUE(pref5_node.GetIsUnappliedUpdate());
2808     EXPECT_TRUE(pref5_node.GetIsDel());
2809     EXPECT_GT(pref5_node.GetServerVersion(), 0);
2810     EXPECT_EQ(pref5_node.GetBaseVersion(), -1);
2811
2812     syncable::Entry bookmark_node(&trans, GET_BY_HANDLE, bookmark_meta);
2813     ASSERT_TRUE(bookmark_node.good());
2814     EXPECT_TRUE(bookmark_node.GetKernelCopy().is_dirty());
2815     EXPECT_FALSE(bookmark_node.GetIsUnsynced());
2816     EXPECT_TRUE(bookmark_node.GetIsUnappliedUpdate());
2817     EXPECT_TRUE(bookmark_node.GetIsDel());
2818     EXPECT_GT(bookmark_node.GetServerVersion(), 0);
2819     EXPECT_EQ(bookmark_node.GetBaseVersion(), -1);
2820   }
2821 }
2822
2823 // A test harness to exercise the code that processes and passes changes from
2824 // the "SYNCER"-WriteTransaction destructor, through the SyncManager, to the
2825 // ChangeProcessor.
2826 class SyncManagerChangeProcessingTest : public SyncManagerTest {
2827  public:
2828   virtual void OnChangesApplied(
2829       ModelType model_type,
2830       int64 model_version,
2831       const BaseTransaction* trans,
2832       const ImmutableChangeRecordList& changes) OVERRIDE {
2833     last_changes_ = changes;
2834   }
2835
2836   virtual void OnChangesComplete(ModelType model_type) OVERRIDE {}
2837
2838   const ImmutableChangeRecordList& GetRecentChangeList() {
2839     return last_changes_;
2840   }
2841
2842   UserShare* share() {
2843     return sync_manager_.GetUserShare();
2844   }
2845
2846   // Set some flags so our nodes reasonably approximate the real world scenario
2847   // and can get past CheckTreeInvariants.
2848   //
2849   // It's never going to be truly accurate, since we're squashing update
2850   // receipt, processing and application into a single transaction.
2851   void SetNodeProperties(syncable::MutableEntry *entry) {
2852     entry->PutId(id_factory_.NewServerId());
2853     entry->PutBaseVersion(10);
2854     entry->PutServerVersion(10);
2855   }
2856
2857   // Looks for the given change in the list.  Returns the index at which it was
2858   // found.  Returns -1 on lookup failure.
2859   size_t FindChangeInList(int64 id, ChangeRecord::Action action) {
2860     SCOPED_TRACE(id);
2861     for (size_t i = 0; i < last_changes_.Get().size(); ++i) {
2862       if (last_changes_.Get()[i].id == id
2863           && last_changes_.Get()[i].action == action) {
2864         return i;
2865       }
2866     }
2867     ADD_FAILURE() << "Failed to find specified change";
2868     return static_cast<size_t>(-1);
2869   }
2870
2871   // Returns the current size of the change list.
2872   //
2873   // Note that spurious changes do not necessarily indicate a problem.
2874   // Assertions on change list size can help detect problems, but it may be
2875   // necessary to reduce their strictness if the implementation changes.
2876   size_t GetChangeListSize() {
2877     return last_changes_.Get().size();
2878   }
2879
2880   void ClearChangeList() { last_changes_ = ImmutableChangeRecordList(); }
2881
2882  protected:
2883   ImmutableChangeRecordList last_changes_;
2884   TestIdFactory id_factory_;
2885 };
2886
2887 // Test creation of a folder and a bookmark.
2888 TEST_F(SyncManagerChangeProcessingTest, AddBookmarks) {
2889   int64 type_root = GetIdForDataType(BOOKMARKS);
2890   int64 folder_id = kInvalidId;
2891   int64 child_id = kInvalidId;
2892
2893   // Create a folder and a bookmark under it.
2894   {
2895     syncable::WriteTransaction trans(
2896         FROM_HERE, syncable::SYNCER, share()->directory.get());
2897     syncable::Entry root(&trans, syncable::GET_BY_HANDLE, type_root);
2898     ASSERT_TRUE(root.good());
2899
2900     syncable::MutableEntry folder(&trans, syncable::CREATE,
2901                                   BOOKMARKS, root.GetId(), "folder");
2902     ASSERT_TRUE(folder.good());
2903     SetNodeProperties(&folder);
2904     folder.PutIsDir(true);
2905     folder_id = folder.GetMetahandle();
2906
2907     syncable::MutableEntry child(&trans, syncable::CREATE,
2908                                  BOOKMARKS, folder.GetId(), "child");
2909     ASSERT_TRUE(child.good());
2910     SetNodeProperties(&child);
2911     child_id = child.GetMetahandle();
2912   }
2913
2914   // The closing of the above scope will delete the transaction.  Its processed
2915   // changes should be waiting for us in a member of the test harness.
2916   EXPECT_EQ(2UL, GetChangeListSize());
2917
2918   // We don't need to check these return values here.  The function will add a
2919   // non-fatal failure if these changes are not found.
2920   size_t folder_change_pos =
2921       FindChangeInList(folder_id, ChangeRecord::ACTION_ADD);
2922   size_t child_change_pos =
2923       FindChangeInList(child_id, ChangeRecord::ACTION_ADD);
2924
2925   // Parents are delivered before children.
2926   EXPECT_LT(folder_change_pos, child_change_pos);
2927 }
2928
2929 // Test moving a bookmark into an empty folder.
2930 TEST_F(SyncManagerChangeProcessingTest, MoveBookmarkIntoEmptyFolder) {
2931   int64 type_root = GetIdForDataType(BOOKMARKS);
2932   int64 folder_b_id = kInvalidId;
2933   int64 child_id = kInvalidId;
2934
2935   // Create two folders.  Place a child under folder A.
2936   {
2937     syncable::WriteTransaction trans(
2938         FROM_HERE, syncable::SYNCER, share()->directory.get());
2939     syncable::Entry root(&trans, syncable::GET_BY_HANDLE, type_root);
2940     ASSERT_TRUE(root.good());
2941
2942     syncable::MutableEntry folder_a(&trans, syncable::CREATE,
2943                                     BOOKMARKS, root.GetId(), "folderA");
2944     ASSERT_TRUE(folder_a.good());
2945     SetNodeProperties(&folder_a);
2946     folder_a.PutIsDir(true);
2947
2948     syncable::MutableEntry folder_b(&trans, syncable::CREATE,
2949                                     BOOKMARKS, root.GetId(), "folderB");
2950     ASSERT_TRUE(folder_b.good());
2951     SetNodeProperties(&folder_b);
2952     folder_b.PutIsDir(true);
2953     folder_b_id = folder_b.GetMetahandle();
2954
2955     syncable::MutableEntry child(&trans, syncable::CREATE,
2956                                  BOOKMARKS, folder_a.GetId(),
2957                                  "child");
2958     ASSERT_TRUE(child.good());
2959     SetNodeProperties(&child);
2960     child_id = child.GetMetahandle();
2961   }
2962
2963   // Close that transaction.  The above was to setup the initial scenario.  The
2964   // real test starts now.
2965
2966   // Move the child from folder A to folder B.
2967   {
2968     syncable::WriteTransaction trans(
2969         FROM_HERE, syncable::SYNCER, share()->directory.get());
2970
2971     syncable::Entry folder_b(&trans, syncable::GET_BY_HANDLE, folder_b_id);
2972     syncable::MutableEntry child(&trans, syncable::GET_BY_HANDLE, child_id);
2973
2974     child.PutParentId(folder_b.GetId());
2975   }
2976
2977   EXPECT_EQ(1UL, GetChangeListSize());
2978
2979   // Verify that this was detected as a real change.  An early version of the
2980   // UniquePosition code had a bug where moves from one folder to another were
2981   // ignored unless the moved node's UniquePosition value was also changed in
2982   // some way.
2983   FindChangeInList(child_id, ChangeRecord::ACTION_UPDATE);
2984 }
2985
2986 // Test moving a bookmark into a non-empty folder.
2987 TEST_F(SyncManagerChangeProcessingTest, MoveIntoPopulatedFolder) {
2988   int64 type_root = GetIdForDataType(BOOKMARKS);
2989   int64 child_a_id = kInvalidId;
2990   int64 child_b_id = kInvalidId;
2991
2992   // Create two folders.  Place one child each under folder A and folder B.
2993   {
2994     syncable::WriteTransaction trans(
2995         FROM_HERE, syncable::SYNCER, share()->directory.get());
2996     syncable::Entry root(&trans, syncable::GET_BY_HANDLE, type_root);
2997     ASSERT_TRUE(root.good());
2998
2999     syncable::MutableEntry folder_a(&trans, syncable::CREATE,
3000                                     BOOKMARKS, root.GetId(), "folderA");
3001     ASSERT_TRUE(folder_a.good());
3002     SetNodeProperties(&folder_a);
3003     folder_a.PutIsDir(true);
3004
3005     syncable::MutableEntry folder_b(&trans, syncable::CREATE,
3006                                     BOOKMARKS, root.GetId(), "folderB");
3007     ASSERT_TRUE(folder_b.good());
3008     SetNodeProperties(&folder_b);
3009     folder_b.PutIsDir(true);
3010
3011     syncable::MutableEntry child_a(&trans, syncable::CREATE,
3012                                    BOOKMARKS, folder_a.GetId(),
3013                                    "childA");
3014     ASSERT_TRUE(child_a.good());
3015     SetNodeProperties(&child_a);
3016     child_a_id = child_a.GetMetahandle();
3017
3018     syncable::MutableEntry child_b(&trans, syncable::CREATE,
3019                                    BOOKMARKS, folder_b.GetId(),
3020                                    "childB");
3021     SetNodeProperties(&child_b);
3022     child_b_id = child_b.GetMetahandle();
3023   }
3024
3025   // Close that transaction.  The above was to setup the initial scenario.  The
3026   // real test starts now.
3027
3028   {
3029     syncable::WriteTransaction trans(
3030         FROM_HERE, syncable::SYNCER, share()->directory.get());
3031
3032     syncable::MutableEntry child_a(&trans, syncable::GET_BY_HANDLE, child_a_id);
3033     syncable::MutableEntry child_b(&trans, syncable::GET_BY_HANDLE, child_b_id);
3034
3035     // Move child A from folder A to folder B and update its position.
3036     child_a.PutParentId(child_b.GetParentId());
3037     child_a.PutPredecessor(child_b.GetId());
3038   }
3039
3040   EXPECT_EQ(1UL, GetChangeListSize());
3041
3042   // Verify that only child a is in the change list.
3043   // (This function will add a failure if the lookup fails.)
3044   FindChangeInList(child_a_id, ChangeRecord::ACTION_UPDATE);
3045 }
3046
3047 // Tests the ordering of deletion changes.
3048 TEST_F(SyncManagerChangeProcessingTest, DeletionsAndChanges) {
3049   int64 type_root = GetIdForDataType(BOOKMARKS);
3050   int64 folder_a_id = kInvalidId;
3051   int64 folder_b_id = kInvalidId;
3052   int64 child_id = kInvalidId;
3053
3054   // Create two folders.  Place a child under folder A.
3055   {
3056     syncable::WriteTransaction trans(
3057         FROM_HERE, syncable::SYNCER, share()->directory.get());
3058     syncable::Entry root(&trans, syncable::GET_BY_HANDLE, type_root);
3059     ASSERT_TRUE(root.good());
3060
3061     syncable::MutableEntry folder_a(&trans, syncable::CREATE,
3062                                     BOOKMARKS, root.GetId(), "folderA");
3063     ASSERT_TRUE(folder_a.good());
3064     SetNodeProperties(&folder_a);
3065     folder_a.PutIsDir(true);
3066     folder_a_id = folder_a.GetMetahandle();
3067
3068     syncable::MutableEntry folder_b(&trans, syncable::CREATE,
3069                                     BOOKMARKS, root.GetId(), "folderB");
3070     ASSERT_TRUE(folder_b.good());
3071     SetNodeProperties(&folder_b);
3072     folder_b.PutIsDir(true);
3073     folder_b_id = folder_b.GetMetahandle();
3074
3075     syncable::MutableEntry child(&trans, syncable::CREATE,
3076                                  BOOKMARKS, folder_a.GetId(),
3077                                  "child");
3078     ASSERT_TRUE(child.good());
3079     SetNodeProperties(&child);
3080     child_id = child.GetMetahandle();
3081   }
3082
3083   // Close that transaction.  The above was to setup the initial scenario.  The
3084   // real test starts now.
3085
3086   {
3087     syncable::WriteTransaction trans(
3088         FROM_HERE, syncable::SYNCER, share()->directory.get());
3089
3090     syncable::MutableEntry folder_a(
3091         &trans, syncable::GET_BY_HANDLE, folder_a_id);
3092     syncable::MutableEntry folder_b(
3093         &trans, syncable::GET_BY_HANDLE, folder_b_id);
3094     syncable::MutableEntry child(&trans, syncable::GET_BY_HANDLE, child_id);
3095
3096     // Delete folder B and its child.
3097     child.PutIsDel(true);
3098     folder_b.PutIsDel(true);
3099
3100     // Make an unrelated change to folder A.
3101     folder_a.PutNonUniqueName("NewNameA");
3102   }
3103
3104   EXPECT_EQ(3UL, GetChangeListSize());
3105
3106   size_t folder_a_pos =
3107       FindChangeInList(folder_a_id, ChangeRecord::ACTION_UPDATE);
3108   size_t folder_b_pos =
3109       FindChangeInList(folder_b_id, ChangeRecord::ACTION_DELETE);
3110   size_t child_pos = FindChangeInList(child_id, ChangeRecord::ACTION_DELETE);
3111
3112   // Deletes should appear before updates.
3113   EXPECT_LT(child_pos, folder_a_pos);
3114   EXPECT_LT(folder_b_pos, folder_a_pos);
3115 }
3116
3117 // See that attachment metadata changes are not filtered out by
3118 // SyncManagerImpl::VisiblePropertiesDiffer.
3119 TEST_F(SyncManagerChangeProcessingTest, AttachmentMetadataOnlyChanges) {
3120   // Create an article with no attachments.  See that a change is generated.
3121   int64 article_id = kInvalidId;
3122   {
3123     syncable::WriteTransaction trans(
3124         FROM_HERE, syncable::SYNCER, share()->directory.get());
3125     int64 type_root = GetIdForDataType(ARTICLES);
3126     syncable::Entry root(&trans, syncable::GET_BY_HANDLE, type_root);
3127     ASSERT_TRUE(root.good());
3128     syncable::MutableEntry article(
3129         &trans, syncable::CREATE, ARTICLES, root.GetId(), "article");
3130     ASSERT_TRUE(article.good());
3131     SetNodeProperties(&article);
3132     article_id = article.GetMetahandle();
3133   }
3134   ASSERT_EQ(1UL, GetChangeListSize());
3135   FindChangeInList(article_id, ChangeRecord::ACTION_ADD);
3136   ClearChangeList();
3137
3138   // Modify the article by adding one attachment.  Don't touch anything else.
3139   // See that a change is generated.
3140   {
3141     syncable::WriteTransaction trans(
3142         FROM_HERE, syncable::SYNCER, share()->directory.get());
3143     syncable::MutableEntry article(&trans, syncable::GET_BY_HANDLE, article_id);
3144     sync_pb::AttachmentMetadata metadata;
3145     *metadata.add_record()->mutable_id() = CreateAttachmentIdProto();
3146     article.PutAttachmentMetadata(metadata);
3147   }
3148   ASSERT_EQ(1UL, GetChangeListSize());
3149   FindChangeInList(article_id, ChangeRecord::ACTION_UPDATE);
3150   ClearChangeList();
3151
3152   // Modify the article by replacing its attachment with a different one.  See
3153   // that a change is generated.
3154   {
3155     syncable::WriteTransaction trans(
3156         FROM_HERE, syncable::SYNCER, share()->directory.get());
3157     syncable::MutableEntry article(&trans, syncable::GET_BY_HANDLE, article_id);
3158     sync_pb::AttachmentMetadata metadata = article.GetAttachmentMetadata();
3159     *metadata.add_record()->mutable_id() = CreateAttachmentIdProto();
3160     article.PutAttachmentMetadata(metadata);
3161   }
3162   ASSERT_EQ(1UL, GetChangeListSize());
3163   FindChangeInList(article_id, ChangeRecord::ACTION_UPDATE);
3164   ClearChangeList();
3165
3166   // Modify the article by replacing its attachment metadata with the same
3167   // attachment metadata.  No change should be generated.
3168   {
3169     syncable::WriteTransaction trans(
3170         FROM_HERE, syncable::SYNCER, share()->directory.get());
3171     syncable::MutableEntry article(&trans, syncable::GET_BY_HANDLE, article_id);
3172     article.PutAttachmentMetadata(article.GetAttachmentMetadata());
3173   }
3174   ASSERT_EQ(0UL, GetChangeListSize());
3175 }
3176
3177 // During initialization SyncManagerImpl loads sqlite database. If it fails to
3178 // do so it should fail initialization. This test verifies this behavior.
3179 // Test reuses SyncManagerImpl initialization from SyncManagerTest but overrides
3180 // InternalComponentsFactory to return DirectoryBackingStore that always fails
3181 // to load.
3182 class SyncManagerInitInvalidStorageTest : public SyncManagerTest {
3183  public:
3184   SyncManagerInitInvalidStorageTest() {
3185   }
3186
3187   virtual InternalComponentsFactory* GetFactory() OVERRIDE {
3188     return new TestInternalComponentsFactory(
3189         GetSwitches(), InternalComponentsFactory::STORAGE_INVALID,
3190         &storage_used_);
3191   }
3192 };
3193
3194 // SyncManagerInitInvalidStorageTest::GetFactory will return
3195 // DirectoryBackingStore that ensures that SyncManagerImpl::OpenDirectory fails.
3196 // SyncManagerImpl initialization is done in SyncManagerTest::SetUp. This test's
3197 // task is to ensure that SyncManagerImpl reported initialization failure in
3198 // OnInitializationComplete callback.
3199 TEST_F(SyncManagerInitInvalidStorageTest, FailToOpenDatabase) {
3200   EXPECT_FALSE(initialization_succeeded_);
3201 }
3202
3203 }  // namespace syncer