OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Unit tests for the SyncApi. Note that a lot of the underlying | 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 | 6 // functionality is provided by the Syncable layer, which has its own |
7 // unit tests. We'll test SyncApi specific things in this harness. | 7 // unit tests. We'll test SyncApi specific things in this harness. |
8 | 8 |
| 9 #include <map> |
| 10 |
9 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/format_macros.h" |
10 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
11 #include "base/scoped_ptr.h" | 14 #include "base/scoped_ptr.h" |
12 #include "base/scoped_temp_dir.h" | 15 #include "base/scoped_temp_dir.h" |
13 #include "base/string_number_conversions.h" | 16 #include "base/string_number_conversions.h" |
| 17 #include "base/string_util.h" |
14 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
15 #include "base/values.h" | 19 #include "base/values.h" |
16 #include "chrome/browser/browser_thread.h" | 20 #include "chrome/browser/browser_thread.h" |
| 21 #include "chrome/browser/sync/engine/model_safe_worker.h" |
17 #include "chrome/browser/sync/engine/syncapi.h" | 22 #include "chrome/browser/sync/engine/syncapi.h" |
18 #include "chrome/browser/sync/js_arg_list.h" | 23 #include "chrome/browser/sync/js_arg_list.h" |
19 #include "chrome/browser/sync/js_backend.h" | 24 #include "chrome/browser/sync/js_backend.h" |
20 #include "chrome/browser/sync/js_event_handler.h" | 25 #include "chrome/browser/sync/js_event_handler.h" |
21 #include "chrome/browser/sync/js_event_router.h" | 26 #include "chrome/browser/sync/js_event_router.h" |
22 #include "chrome/browser/sync/js_test_util.h" | 27 #include "chrome/browser/sync/js_test_util.h" |
23 #include "chrome/browser/sync/protocol/password_specifics.pb.h" | 28 #include "chrome/browser/sync/protocol/password_specifics.pb.h" |
24 #include "chrome/browser/sync/protocol/proto_value_conversions.h" | 29 #include "chrome/browser/sync/protocol/proto_value_conversions.h" |
| 30 #include "chrome/browser/sync/sessions/sync_session.h" |
25 #include "chrome/browser/sync/syncable/directory_manager.h" | 31 #include "chrome/browser/sync/syncable/directory_manager.h" |
| 32 #include "chrome/browser/sync/syncable/nigori_util.h" |
26 #include "chrome/browser/sync/syncable/syncable.h" | 33 #include "chrome/browser/sync/syncable/syncable.h" |
| 34 #include "chrome/browser/sync/syncable/syncable_id.h" |
| 35 #include "chrome/browser/sync/util/cryptographer.h" |
27 #include "chrome/test/sync/engine/test_directory_setter_upper.h" | 36 #include "chrome/test/sync/engine/test_directory_setter_upper.h" |
28 #include "chrome/test/values_test_util.h" | 37 #include "chrome/test/values_test_util.h" |
29 #include "jingle/notifier/base/notifier_options.h" | 38 #include "jingle/notifier/base/notifier_options.h" |
30 #include "testing/gmock/include/gmock/gmock.h" | 39 #include "testing/gmock/include/gmock/gmock.h" |
31 #include "testing/gtest/include/gtest/gtest.h" | 40 #include "testing/gtest/include/gtest/gtest.h" |
32 | 41 |
| 42 using browser_sync::Cryptographer; |
33 using browser_sync::HasArgsAsList; | 43 using browser_sync::HasArgsAsList; |
34 using browser_sync::KeyParams; | 44 using browser_sync::KeyParams; |
35 using browser_sync::JsArgList; | 45 using browser_sync::JsArgList; |
36 using browser_sync::MockJsEventHandler; | 46 using browser_sync::MockJsEventHandler; |
37 using browser_sync::MockJsEventRouter; | 47 using browser_sync::MockJsEventRouter; |
| 48 using browser_sync::ModelSafeRoutingInfo; |
| 49 using browser_sync::ModelSafeWorker; |
| 50 using browser_sync::ModelSafeWorkerRegistrar; |
| 51 using browser_sync::sessions::SyncSessionSnapshot; |
| 52 using syncable::ModelType; |
| 53 using syncable::ModelTypeSet; |
38 using test::ExpectDictionaryValue; | 54 using test::ExpectDictionaryValue; |
39 using test::ExpectStringValue; | 55 using test::ExpectStringValue; |
40 using testing::_; | 56 using testing::_; |
41 using testing::Invoke; | 57 using testing::Invoke; |
42 using testing::SaveArg; | 58 using testing::SaveArg; |
43 using testing::StrictMock; | 59 using testing::StrictMock; |
44 | 60 |
45 namespace sync_api { | 61 namespace sync_api { |
46 | 62 |
47 namespace { | 63 namespace { |
48 | 64 |
49 void ExpectInt64Value(int64 expected_value, | 65 void ExpectInt64Value(int64 expected_value, |
50 const DictionaryValue& value, const std::string& key) { | 66 const DictionaryValue& value, const std::string& key) { |
51 std::string int64_str; | 67 std::string int64_str; |
52 EXPECT_TRUE(value.GetString(key, &int64_str)); | 68 EXPECT_TRUE(value.GetString(key, &int64_str)); |
53 int64 val = 0; | 69 int64 val = 0; |
54 EXPECT_TRUE(base::StringToInt64(int64_str, &val)); | 70 EXPECT_TRUE(base::StringToInt64(int64_str, &val)); |
55 EXPECT_EQ(expected_value, val); | 71 EXPECT_EQ(expected_value, val); |
56 } | 72 } |
57 | 73 |
58 // Makes a non-folder child of the root node. Returns the id of the | 74 // Makes a non-folder child of the root node. Returns the id of the |
59 // newly-created node. | 75 // newly-created node. |
60 int64 MakeNode(UserShare* share, | 76 int64 MakeNode(UserShare* share, |
61 syncable::ModelType model_type, | 77 ModelType model_type, |
62 const std::string& client_tag) { | 78 const std::string& client_tag) { |
63 WriteTransaction trans(share); | 79 WriteTransaction trans(share); |
64 ReadNode root_node(&trans); | 80 ReadNode root_node(&trans); |
65 root_node.InitByRootLookup(); | 81 root_node.InitByRootLookup(); |
66 WriteNode node(&trans); | 82 WriteNode node(&trans); |
67 EXPECT_TRUE(node.InitUniqueByCreation(model_type, root_node, client_tag)); | 83 EXPECT_TRUE(node.InitUniqueByCreation(model_type, root_node, client_tag)); |
68 node.SetIsFolder(false); | 84 node.SetIsFolder(false); |
69 return node.GetId(); | 85 return node.GetId(); |
70 } | 86 } |
71 | 87 |
| 88 // Make a folder as a child of the root node. Returns the id of the |
| 89 // newly-created node. |
| 90 int64 MakeFolder(UserShare* share, |
| 91 syncable::ModelType model_type, |
| 92 const std::string& client_tag) { |
| 93 WriteTransaction trans(share); |
| 94 ReadNode root_node(&trans); |
| 95 root_node.InitByRootLookup(); |
| 96 WriteNode node(&trans); |
| 97 EXPECT_TRUE(node.InitUniqueByCreation(model_type, root_node, client_tag)); |
| 98 node.SetIsFolder(true); |
| 99 return node.GetId(); |
| 100 } |
| 101 |
| 102 // Makes a non-folder child of a non-root node. Returns the id of the |
| 103 // newly-created node. |
| 104 int64 MakeNodeWithParent(UserShare* share, |
| 105 ModelType model_type, |
| 106 const std::string& client_tag, |
| 107 int64 parent_id) { |
| 108 WriteTransaction trans(share); |
| 109 ReadNode parent_node(&trans); |
| 110 parent_node.InitByIdLookup(parent_id); |
| 111 WriteNode node(&trans); |
| 112 EXPECT_TRUE(node.InitUniqueByCreation(model_type, parent_node, client_tag)); |
| 113 node.SetIsFolder(false); |
| 114 return node.GetId(); |
| 115 } |
| 116 |
| 117 // Makes a folder child of a non-root node. Returns the id of the |
| 118 // newly-created node. |
| 119 int64 MakeFolderWithParent(UserShare* share, |
| 120 ModelType model_type, |
| 121 int64 parent_id, |
| 122 BaseNode* predecessor) { |
| 123 WriteTransaction trans(share); |
| 124 ReadNode parent_node(&trans); |
| 125 parent_node.InitByIdLookup(parent_id); |
| 126 WriteNode node(&trans); |
| 127 EXPECT_TRUE(node.InitByCreation(model_type, parent_node, predecessor)); |
| 128 node.SetIsFolder(true); |
| 129 return node.GetId(); |
| 130 } |
| 131 |
| 132 // Creates the "synced" root node for a particular datatype. We use the syncable |
| 133 // methods here so that the syncer treats these nodes as if they were already |
| 134 // received from the server. |
| 135 int64 MakeServerNodeForType(UserShare* share, |
| 136 ModelType model_type) { |
| 137 sync_pb::EntitySpecifics specifics; |
| 138 syncable::AddDefaultExtensionValue(model_type, &specifics); |
| 139 syncable::ScopedDirLookup dir(share->dir_manager.get(), share->name); |
| 140 EXPECT_TRUE(dir.good()); |
| 141 syncable::WriteTransaction trans(dir, syncable::UNITTEST, __FILE__, __LINE__); |
| 142 // Attempt to lookup by nigori tag. |
| 143 std::string type_tag = syncable::ModelTypeToRootTag(model_type); |
| 144 syncable::Id node_id = syncable::Id::CreateFromServerId(type_tag); |
| 145 syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM, |
| 146 node_id); |
| 147 EXPECT_TRUE(entry.good()); |
| 148 entry.Put(syncable::BASE_VERSION, 1); |
| 149 entry.Put(syncable::SERVER_VERSION, 1); |
| 150 entry.Put(syncable::IS_UNAPPLIED_UPDATE, false); |
| 151 entry.Put(syncable::SERVER_PARENT_ID, syncable::kNullId); |
| 152 entry.Put(syncable::SERVER_IS_DIR, true); |
| 153 entry.Put(syncable::IS_DIR, true); |
| 154 entry.Put(syncable::SERVER_SPECIFICS, specifics); |
| 155 entry.Put(syncable::UNIQUE_SERVER_TAG, type_tag); |
| 156 entry.Put(syncable::NON_UNIQUE_NAME, type_tag); |
| 157 entry.Put(syncable::IS_DEL, false); |
| 158 entry.Put(syncable::SPECIFICS, specifics); |
| 159 return entry.Get(syncable::META_HANDLE); |
| 160 } |
| 161 |
72 } // namespace | 162 } // namespace |
73 | 163 |
74 class SyncApiTest : public testing::Test { | 164 class SyncApiTest : public testing::Test { |
75 public: | 165 public: |
76 virtual void SetUp() { | 166 virtual void SetUp() { |
77 setter_upper_.SetUp(); | 167 setter_upper_.SetUp(); |
78 share_.dir_manager.reset(setter_upper_.manager()); | 168 share_.dir_manager.reset(setter_upper_.manager()); |
79 share_.name = setter_upper_.name(); | 169 share_.name = setter_upper_.name(); |
80 } | 170 } |
81 | 171 |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 WriteTransaction trans(&share_); | 281 WriteTransaction trans(&share_); |
192 WriteNode node(&trans); | 282 WriteNode node(&trans); |
193 EXPECT_FALSE(node.InitByClientTagLookup(syncable::BOOKMARKS, | 283 EXPECT_FALSE(node.InitByClientTagLookup(syncable::BOOKMARKS, |
194 "testtag")); | 284 "testtag")); |
195 } | 285 } |
196 } | 286 } |
197 | 287 |
198 // TODO(chron): Hook this all up to the server and write full integration tests | 288 // TODO(chron): Hook this all up to the server and write full integration tests |
199 // for update->undelete behavior. | 289 // for update->undelete behavior. |
200 TEST_F(SyncApiTest, TestDeleteBehavior) { | 290 TEST_F(SyncApiTest, TestDeleteBehavior) { |
201 | |
202 int64 node_id; | 291 int64 node_id; |
203 int64 folder_id; | 292 int64 folder_id; |
204 std::wstring test_title(L"test1"); | 293 std::wstring test_title(L"test1"); |
205 | 294 |
206 { | 295 { |
207 WriteTransaction trans(&share_); | 296 WriteTransaction trans(&share_); |
208 ReadNode root_node(&trans); | 297 ReadNode root_node(&trans); |
209 root_node.InitByRootLookup(); | 298 root_node.InitByRootLookup(); |
210 | 299 |
211 // we'll use this spare folder later | 300 // we'll use this spare folder later |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 ExpectInt64Value(node.GetId(), value, "id"); | 398 ExpectInt64Value(node.GetId(), value, "id"); |
310 ExpectInt64Value(node.GetModificationTime(), value, "modificationTime"); | 399 ExpectInt64Value(node.GetModificationTime(), value, "modificationTime"); |
311 ExpectInt64Value(node.GetParentId(), value, "parentId"); | 400 ExpectInt64Value(node.GetParentId(), value, "parentId"); |
312 { | 401 { |
313 bool is_folder = false; | 402 bool is_folder = false; |
314 EXPECT_TRUE(value.GetBoolean("isFolder", &is_folder)); | 403 EXPECT_TRUE(value.GetBoolean("isFolder", &is_folder)); |
315 EXPECT_EQ(node.GetIsFolder(), is_folder); | 404 EXPECT_EQ(node.GetIsFolder(), is_folder); |
316 } | 405 } |
317 ExpectStringValue(WideToUTF8(node.GetTitle()), value, "title"); | 406 ExpectStringValue(WideToUTF8(node.GetTitle()), value, "title"); |
318 { | 407 { |
319 syncable::ModelType expected_model_type = node.GetModelType(); | 408 ModelType expected_model_type = node.GetModelType(); |
320 std::string type_str; | 409 std::string type_str; |
321 EXPECT_TRUE(value.GetString("type", &type_str)); | 410 EXPECT_TRUE(value.GetString("type", &type_str)); |
322 if (expected_model_type >= syncable::FIRST_REAL_MODEL_TYPE) { | 411 if (expected_model_type >= syncable::FIRST_REAL_MODEL_TYPE) { |
323 syncable::ModelType model_type = | 412 ModelType model_type = |
324 syncable::ModelTypeFromString(type_str); | 413 syncable::ModelTypeFromString(type_str); |
325 EXPECT_EQ(expected_model_type, model_type); | 414 EXPECT_EQ(expected_model_type, model_type); |
326 } else if (expected_model_type == syncable::TOP_LEVEL_FOLDER) { | 415 } else if (expected_model_type == syncable::TOP_LEVEL_FOLDER) { |
327 EXPECT_EQ("Top-level folder", type_str); | 416 EXPECT_EQ("Top-level folder", type_str); |
328 } else if (expected_model_type == syncable::UNSPECIFIED) { | 417 } else if (expected_model_type == syncable::UNSPECIFIED) { |
329 EXPECT_EQ("Unspecified", type_str); | 418 EXPECT_EQ("Unspecified", type_str); |
330 } else { | 419 } else { |
331 ADD_FAILURE(); | 420 ADD_FAILURE(); |
332 } | 421 } |
333 } | 422 } |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 if (record.extra.get()) { | 502 if (record.extra.get()) { |
414 expected_extra_value.reset(record.extra->ToValue()); | 503 expected_extra_value.reset(record.extra->ToValue()); |
415 } | 504 } |
416 Value* extra_value = NULL; | 505 Value* extra_value = NULL; |
417 EXPECT_EQ(record.extra.get() != NULL, | 506 EXPECT_EQ(record.extra.get() != NULL, |
418 node_value->Get("extra", &extra_value)); | 507 node_value->Get("extra", &extra_value)); |
419 EXPECT_TRUE(Value::Equals(extra_value, expected_extra_value.get())); | 508 EXPECT_TRUE(Value::Equals(extra_value, expected_extra_value.get())); |
420 } | 509 } |
421 } | 510 } |
422 | 511 |
423 class MockExtraChangeRecordData : public SyncManager::ExtraChangeRecordData { | 512 class MockExtraChangeRecordData |
| 513 : public SyncManager::ExtraPasswordChangeRecordData { |
424 public: | 514 public: |
425 MOCK_CONST_METHOD0(ToValue, DictionaryValue*()); | 515 MOCK_CONST_METHOD0(ToValue, DictionaryValue*()); |
426 }; | 516 }; |
427 | 517 |
428 } // namespace | 518 } // namespace |
429 | 519 |
430 TEST_F(SyncApiTest, ChangeRecordToValue) { | 520 TEST_F(SyncApiTest, ChangeRecordToValue) { |
431 int64 child_id = MakeNode(&share_, syncable::BOOKMARKS, "testtag"); | 521 int64 child_id = MakeNode(&share_, syncable::BOOKMARKS, "testtag"); |
432 sync_pb::EntitySpecifics child_specifics; | 522 sync_pb::EntitySpecifics child_specifics; |
433 { | 523 { |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
500 virtual ~TestHttpPostProviderFactory() {} | 590 virtual ~TestHttpPostProviderFactory() {} |
501 virtual HttpPostProviderInterface* Create() { | 591 virtual HttpPostProviderInterface* Create() { |
502 NOTREACHED(); | 592 NOTREACHED(); |
503 return NULL; | 593 return NULL; |
504 } | 594 } |
505 virtual void Destroy(HttpPostProviderInterface* http) { | 595 virtual void Destroy(HttpPostProviderInterface* http) { |
506 NOTREACHED(); | 596 NOTREACHED(); |
507 } | 597 } |
508 }; | 598 }; |
509 | 599 |
510 class SyncManagerTest : public testing::Test { | 600 class SyncManagerObserverMock : public SyncManager::Observer { |
| 601 public: |
| 602 MOCK_METHOD4(OnChangesApplied, |
| 603 void(ModelType, |
| 604 const BaseTransaction*, |
| 605 const SyncManager::ChangeRecord*, |
| 606 int)); // NOLINT |
| 607 MOCK_METHOD1(OnChangesComplete, void(ModelType)); // NOLINT |
| 608 MOCK_METHOD1(OnSyncCycleCompleted, |
| 609 void(const SyncSessionSnapshot*)); // NOLINT |
| 610 MOCK_METHOD0(OnInitializationComplete, void()); // NOLINT |
| 611 MOCK_METHOD1(OnAuthError, void(const GoogleServiceAuthError&)); // NOLINT |
| 612 MOCK_METHOD1(OnPassphraseRequired, void(bool)); // NOLINT |
| 613 MOCK_METHOD1(OnPassphraseAccepted, void(const std::string&)); // NOLINT |
| 614 MOCK_METHOD0(OnPaused, void()); // NOLINT |
| 615 MOCK_METHOD0(OnResumed, void()); // NOLINT |
| 616 MOCK_METHOD0(OnStopSyncingPermanently, void()); // NOLINT |
| 617 MOCK_METHOD1(OnUpdatedToken, void(const std::string&)); // NOLINT |
| 618 MOCK_METHOD0(OnClearServerDataFailed, void()); // NOLINT |
| 619 MOCK_METHOD0(OnClearServerDataSucceeded, void()); // NOLINT |
| 620 MOCK_METHOD1(OnEncryptionComplete, void(const ModelTypeSet&)); // NOLINT |
| 621 }; |
| 622 |
| 623 class SyncManagerTest : public testing::Test, |
| 624 public ModelSafeWorkerRegistrar { |
511 protected: | 625 protected: |
512 SyncManagerTest() : ui_thread_(BrowserThread::UI, &ui_loop_) {} | 626 SyncManagerTest() : ui_thread_(BrowserThread::UI, &ui_loop_) {} |
513 | 627 |
| 628 // Test implementation. |
514 void SetUp() { | 629 void SetUp() { |
515 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 630 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
516 sync_manager_.Init(temp_dir_.path(), "bogus", 0, false, | 631 sync_manager_.Init(temp_dir_.path(), "bogus", 0, false, |
517 new TestHttpPostProviderFactory(), NULL, "bogus", | 632 new TestHttpPostProviderFactory(), this, "bogus", |
518 SyncCredentials(), notifier::NotifierOptions(), | 633 SyncCredentials(), notifier::NotifierOptions(), |
519 "", true /* setup_for_test_mode */); | 634 "", true /* setup_for_test_mode */); |
| 635 sync_manager_.AddObserver(&observer_); |
| 636 ModelSafeRoutingInfo routes; |
| 637 GetModelSafeRoutingInfo(&routes); |
| 638 for (ModelSafeRoutingInfo::iterator i = routes.begin(); i != routes.end(); |
| 639 ++i) { |
| 640 EXPECT_CALL(observer_, OnChangesApplied(i->first, _, _, 1)) |
| 641 .RetiresOnSaturation(); |
| 642 EXPECT_CALL(observer_, OnChangesComplete(i->first)) |
| 643 .RetiresOnSaturation(); |
| 644 type_roots_[i->first] = MakeServerNodeForType( |
| 645 sync_manager_.GetUserShare(), i->first); |
| 646 } |
520 } | 647 } |
521 | 648 |
522 void TearDown() { | 649 void TearDown() { |
| 650 sync_manager_.RemoveObserver(&observer_); |
523 sync_manager_.Shutdown(); | 651 sync_manager_.Shutdown(); |
524 } | 652 } |
525 | 653 |
| 654 // ModelSafeWorkerRegistrar implementation. |
| 655 virtual void GetWorkers(std::vector<ModelSafeWorker*>* out) { |
| 656 NOTIMPLEMENTED(); |
| 657 out->clear(); |
| 658 } |
| 659 virtual void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) { |
| 660 (*out)[syncable::NIGORI] = browser_sync::GROUP_PASSIVE; |
| 661 (*out)[syncable::BOOKMARKS] = browser_sync::GROUP_PASSIVE; |
| 662 (*out)[syncable::THEMES] = browser_sync::GROUP_PASSIVE; |
| 663 (*out)[syncable::SESSIONS] = browser_sync::GROUP_PASSIVE; |
| 664 (*out)[syncable::PASSWORDS] = browser_sync::GROUP_PASSIVE; |
| 665 } |
| 666 |
| 667 // Helper methods. |
| 668 bool SetUpEncryption() { |
| 669 // We need to create the nigori node as if it were an applied server update. |
| 670 UserShare* share = sync_manager_.GetUserShare(); |
| 671 int64 nigori_id = GetIdForDataType(syncable::NIGORI); |
| 672 if (nigori_id == kInvalidId) |
| 673 return false; |
| 674 |
| 675 // Set the nigori cryptographer information. |
| 676 Cryptographer* cryptographer = share->dir_manager->cryptographer(); |
| 677 if (!cryptographer) |
| 678 return false; |
| 679 KeyParams params = {"localhost", "dummy", "foobar"}; |
| 680 cryptographer->AddKey(params); |
| 681 sync_pb::NigoriSpecifics nigori; |
| 682 cryptographer->GetKeys(nigori.mutable_encrypted()); |
| 683 WriteTransaction trans(share); |
| 684 WriteNode node(&trans); |
| 685 node.InitByIdLookup(nigori_id); |
| 686 node.SetNigoriSpecifics(nigori); |
| 687 return cryptographer->is_ready(); |
| 688 } |
| 689 |
| 690 int64 GetIdForDataType(ModelType type) { |
| 691 if (type_roots_.count(type) == 0) |
| 692 return 0; |
| 693 return type_roots_[type]; |
| 694 } |
| 695 |
526 private: | 696 private: |
527 // Needed by |ui_thread_|. | 697 // Needed by |ui_thread_|. |
528 MessageLoopForUI ui_loop_; | 698 MessageLoopForUI ui_loop_; |
529 // Needed by |sync_manager_|. | 699 // Needed by |sync_manager_|. |
530 BrowserThread ui_thread_; | 700 BrowserThread ui_thread_; |
531 // Needed by |sync_manager_|. | 701 // Needed by |sync_manager_|. |
532 ScopedTempDir temp_dir_; | 702 ScopedTempDir temp_dir_; |
| 703 // Sync Id's for the roots of the enabled datatypes. |
| 704 std::map<ModelType, int64> type_roots_; |
533 | 705 |
534 protected: | 706 protected: |
535 SyncManager sync_manager_; | 707 SyncManager sync_manager_; |
| 708 StrictMock<SyncManagerObserverMock> observer_; |
536 }; | 709 }; |
537 | 710 |
538 TEST_F(SyncManagerTest, ParentJsEventRouter) { | 711 TEST_F(SyncManagerTest, ParentJsEventRouter) { |
539 StrictMock<MockJsEventRouter> event_router; | 712 StrictMock<MockJsEventRouter> event_router; |
540 browser_sync::JsBackend* js_backend = sync_manager_.GetJsBackend(); | 713 browser_sync::JsBackend* js_backend = sync_manager_.GetJsBackend(); |
541 EXPECT_EQ(NULL, js_backend->GetParentJsEventRouter()); | 714 EXPECT_EQ(NULL, js_backend->GetParentJsEventRouter()); |
542 js_backend->SetParentJsEventRouter(&event_router); | 715 js_backend->SetParentJsEventRouter(&event_router); |
543 EXPECT_EQ(&event_router, js_backend->GetParentJsEventRouter()); | 716 EXPECT_EQ(&event_router, js_backend->GetParentJsEventRouter()); |
544 js_backend->RemoveParentJsEventRouter(); | 717 js_backend->RemoveParentJsEventRouter(); |
545 EXPECT_EQ(NULL, js_backend->GetParentJsEventRouter()); | 718 EXPECT_EQ(NULL, js_backend->GetParentJsEventRouter()); |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
796 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); | 969 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); |
797 | 970 |
798 js_backend->SetParentJsEventRouter(&event_router); | 971 js_backend->SetParentJsEventRouter(&event_router); |
799 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); | 972 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); |
800 js_backend->RemoveParentJsEventRouter(); | 973 js_backend->RemoveParentJsEventRouter(); |
801 | 974 |
802 sync_manager_.TriggerOnIncomingNotificationForTest(empty_model_types); | 975 sync_manager_.TriggerOnIncomingNotificationForTest(empty_model_types); |
803 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); | 976 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); |
804 } | 977 } |
805 | 978 |
| 979 TEST_F(SyncManagerTest, EncryptDataTypesWithNoData) { |
| 980 EXPECT_TRUE(SetUpEncryption()); |
| 981 ModelTypeSet encrypted_types; |
| 982 encrypted_types.insert(syncable::BOOKMARKS); |
| 983 // Even though Passwords isn't marked for encryption, it's enabled, so it |
| 984 // should automatically be added to the response of OnEncryptionComplete. |
| 985 ModelTypeSet expected_types = encrypted_types; |
| 986 expected_types.insert(syncable::PASSWORDS); |
| 987 EXPECT_CALL(observer_, OnEncryptionComplete(expected_types)); |
| 988 sync_manager_.EncryptDataTypes(encrypted_types); |
| 989 { |
| 990 ReadTransaction trans(sync_manager_.GetUserShare()); |
| 991 EXPECT_EQ(encrypted_types, |
| 992 GetEncryptedDataTypes(trans.GetWrappedTrans())); |
| 993 } |
| 994 } |
| 995 |
| 996 TEST_F(SyncManagerTest, EncryptDataTypesWithData) { |
| 997 size_t batch_size = 5; |
| 998 EXPECT_TRUE(SetUpEncryption()); |
| 999 |
| 1000 // Create some unencrypted unsynced data. |
| 1001 int64 folder = MakeFolderWithParent(sync_manager_.GetUserShare(), |
| 1002 syncable::BOOKMARKS, |
| 1003 GetIdForDataType(syncable::BOOKMARKS), |
| 1004 NULL); |
| 1005 // First batch_size nodes are children of folder. |
| 1006 size_t i; |
| 1007 for (i = 0; i < batch_size; ++i) { |
| 1008 MakeNodeWithParent(sync_manager_.GetUserShare(), syncable::BOOKMARKS, |
| 1009 StringPrintf("%"PRIuS"", i), folder); |
| 1010 } |
| 1011 // Next batch_size nodes are a different type and on their own. |
| 1012 for (; i < 2*batch_size; ++i) { |
| 1013 MakeNodeWithParent(sync_manager_.GetUserShare(), syncable::SESSIONS, |
| 1014 StringPrintf("%"PRIuS"", i), |
| 1015 GetIdForDataType(syncable::SESSIONS)); |
| 1016 } |
| 1017 // Last batch_size nodes are a third type that will not need encryption. |
| 1018 for (; i < 3*batch_size; ++i) { |
| 1019 MakeNodeWithParent(sync_manager_.GetUserShare(), syncable::THEMES, |
| 1020 StringPrintf("%"PRIuS"", i), |
| 1021 GetIdForDataType(syncable::THEMES)); |
| 1022 } |
| 1023 |
| 1024 { |
| 1025 ReadTransaction trans(sync_manager_.GetUserShare()); |
| 1026 EXPECT_TRUE(syncable::VerifyDataTypeEncryption(trans.GetWrappedTrans(), |
| 1027 syncable::BOOKMARKS, |
| 1028 false /* not encrypted */)); |
| 1029 EXPECT_TRUE(syncable::VerifyDataTypeEncryption(trans.GetWrappedTrans(), |
| 1030 syncable::SESSIONS, |
| 1031 false /* not encrypted */)); |
| 1032 EXPECT_TRUE(syncable::VerifyDataTypeEncryption(trans.GetWrappedTrans(), |
| 1033 syncable::THEMES, |
| 1034 false /* not encrypted */)); |
| 1035 } |
| 1036 |
| 1037 ModelTypeSet encrypted_types; |
| 1038 encrypted_types.insert(syncable::BOOKMARKS); |
| 1039 encrypted_types.insert(syncable::SESSIONS); |
| 1040 encrypted_types.insert(syncable::PASSWORDS); |
| 1041 EXPECT_CALL(observer_, OnEncryptionComplete(encrypted_types)); |
| 1042 sync_manager_.EncryptDataTypes(encrypted_types); |
| 1043 |
| 1044 { |
| 1045 ReadTransaction trans(sync_manager_.GetUserShare()); |
| 1046 encrypted_types.erase(syncable::PASSWORDS); // Not stored in nigori node. |
| 1047 EXPECT_EQ(encrypted_types, |
| 1048 GetEncryptedDataTypes(trans.GetWrappedTrans())); |
| 1049 EXPECT_TRUE(syncable::VerifyDataTypeEncryption(trans.GetWrappedTrans(), |
| 1050 syncable::BOOKMARKS, |
| 1051 true /* is encrypted */)); |
| 1052 EXPECT_TRUE(syncable::VerifyDataTypeEncryption(trans.GetWrappedTrans(), |
| 1053 syncable::SESSIONS, |
| 1054 true /* is encrypted */)); |
| 1055 EXPECT_TRUE(syncable::VerifyDataTypeEncryption(trans.GetWrappedTrans(), |
| 1056 syncable::THEMES, |
| 1057 false /* not encrypted */)); |
| 1058 } |
| 1059 } |
| 1060 |
806 } // namespace | 1061 } // namespace |
807 | 1062 |
808 } // namespace browser_sync | 1063 } // namespace browser_sync |
OLD | NEW |