Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: chrome/browser/sync/internal_api/syncapi_unittest.cc

Issue 8759019: [Sync] Add intelligent re-encryption support. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <cstddef> 9 #include <cstddef>
10 #include <map> 10 #include <map>
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 using browser_sync::JsReplyHandler; 66 using browser_sync::JsReplyHandler;
67 using browser_sync::MockJsEventHandler; 67 using browser_sync::MockJsEventHandler;
68 using browser_sync::MockJsReplyHandler; 68 using browser_sync::MockJsReplyHandler;
69 using browser_sync::ModelSafeRoutingInfo; 69 using browser_sync::ModelSafeRoutingInfo;
70 using browser_sync::ModelSafeWorker; 70 using browser_sync::ModelSafeWorker;
71 using browser_sync::ModelSafeWorkerRegistrar; 71 using browser_sync::ModelSafeWorkerRegistrar;
72 using browser_sync::sessions::SyncSessionSnapshot; 72 using browser_sync::sessions::SyncSessionSnapshot;
73 using browser_sync::WeakHandle; 73 using browser_sync::WeakHandle;
74 using content::BrowserThread; 74 using content::BrowserThread;
75 using syncable::GetAllRealModelTypes; 75 using syncable::GetAllRealModelTypes;
76 using syncable::IS_DEL;
77 using syncable::IS_UNSYNCED;
76 using syncable::kEncryptedString; 78 using syncable::kEncryptedString;
77 using syncable::ModelType; 79 using syncable::ModelType;
78 using syncable::ModelTypeSet; 80 using syncable::ModelTypeSet;
81 using syncable::NON_UNIQUE_NAME;
82 using syncable::SPECIFICS;
79 using test::ExpectDictStringValue; 83 using test::ExpectDictStringValue;
80 using testing::_; 84 using testing::_;
81 using testing::AnyNumber; 85 using testing::AnyNumber;
82 using testing::AtLeast; 86 using testing::AtLeast;
83 using testing::InSequence; 87 using testing::InSequence;
84 using testing::Invoke; 88 using testing::Invoke;
85 using testing::SaveArg; 89 using testing::SaveArg;
86 using testing::StrictMock; 90 using testing::StrictMock;
87 91
88 namespace sync_api { 92 namespace sync_api {
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 entry.Put(syncable::SERVER_IS_DIR, true); 176 entry.Put(syncable::SERVER_IS_DIR, true);
173 entry.Put(syncable::IS_DIR, true); 177 entry.Put(syncable::IS_DIR, true);
174 entry.Put(syncable::SERVER_SPECIFICS, specifics); 178 entry.Put(syncable::SERVER_SPECIFICS, specifics);
175 entry.Put(syncable::UNIQUE_SERVER_TAG, type_tag); 179 entry.Put(syncable::UNIQUE_SERVER_TAG, type_tag);
176 entry.Put(syncable::NON_UNIQUE_NAME, type_tag); 180 entry.Put(syncable::NON_UNIQUE_NAME, type_tag);
177 entry.Put(syncable::IS_DEL, false); 181 entry.Put(syncable::IS_DEL, false);
178 entry.Put(syncable::SPECIFICS, specifics); 182 entry.Put(syncable::SPECIFICS, specifics);
179 return entry.Get(syncable::META_HANDLE); 183 return entry.Get(syncable::META_HANDLE);
180 } 184 }
181 185
186 // Simulates creating a "synced" node as a child of the root datatype node.
187 int64 MakeServerNode(UserShare* share, ModelType model_type,
188 const std::string& client_tag,
189 const std::string& hashed_tag,
190 const sync_pb::EntitySpecifics& specifics) {
191 syncable::ScopedDirLookup dir(share->dir_manager.get(), share->name);
192 EXPECT_TRUE(dir.good());
193 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, dir);
194 syncable::Entry root_entry(&trans, syncable::GET_BY_SERVER_TAG,
195 syncable::ModelTypeToRootTag(model_type));
196 EXPECT_TRUE(root_entry.good());
197 syncable::Id root_id = root_entry.Get(syncable::ID);
198 syncable::Id node_id = syncable::Id::CreateFromServerId(client_tag);
199 syncable::MutableEntry entry(&trans, syncable::CREATE_NEW_UPDATE_ITEM,
200 node_id);
201 EXPECT_TRUE(entry.good());
202 entry.Put(syncable::BASE_VERSION, 1);
203 entry.Put(syncable::SERVER_VERSION, 1);
204 entry.Put(syncable::IS_UNAPPLIED_UPDATE, false);
205 entry.Put(syncable::SERVER_PARENT_ID, root_id);
206 entry.Put(syncable::PARENT_ID, root_id);
207 entry.Put(syncable::SERVER_IS_DIR, false);
208 entry.Put(syncable::IS_DIR, false);
209 entry.Put(syncable::SERVER_SPECIFICS, specifics);
210 entry.Put(syncable::NON_UNIQUE_NAME, client_tag);
211 entry.Put(syncable::UNIQUE_CLIENT_TAG, hashed_tag);
212 entry.Put(syncable::IS_DEL, false);
213 entry.Put(syncable::SPECIFICS, specifics);
214 return entry.Get(syncable::META_HANDLE);
215 }
216
182 } // namespace 217 } // namespace
183 218
184 class SyncApiTest : public testing::Test { 219 class SyncApiTest : public testing::Test {
185 public: 220 public:
186 virtual void SetUp() { 221 virtual void SetUp() {
187 test_user_share_.SetUp(); 222 test_user_share_.SetUp();
188 } 223 }
189 224
190 virtual void TearDown() { 225 virtual void TearDown() {
191 test_user_share_.TearDown(); 226 test_user_share_.TearDown();
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after
644 MOCK_METHOD1(RemoveObserver, void(sync_notifier::SyncNotifierObserver*)); 679 MOCK_METHOD1(RemoveObserver, void(sync_notifier::SyncNotifierObserver*));
645 MOCK_METHOD1(SetUniqueId, void(const std::string&)); 680 MOCK_METHOD1(SetUniqueId, void(const std::string&));
646 MOCK_METHOD1(SetState, void(const std::string&)); 681 MOCK_METHOD1(SetState, void(const std::string&));
647 MOCK_METHOD2(UpdateCredentials, 682 MOCK_METHOD2(UpdateCredentials,
648 void(const std::string&, const std::string&)); 683 void(const std::string&, const std::string&));
649 MOCK_METHOD1(UpdateEnabledTypes, 684 MOCK_METHOD1(UpdateEnabledTypes,
650 void(syncable::ModelEnumSet)); 685 void(syncable::ModelEnumSet));
651 MOCK_METHOD1(SendNotification, void(syncable::ModelEnumSet)); 686 MOCK_METHOD1(SendNotification, void(syncable::ModelEnumSet));
652 }; 687 };
653 688
689 } // namespace
690
654 class SyncManagerTest : public testing::Test, 691 class SyncManagerTest : public testing::Test,
655 public ModelSafeWorkerRegistrar, 692 public ModelSafeWorkerRegistrar,
656 public SyncManager::ChangeDelegate { 693 public SyncManager::ChangeDelegate {
657 protected: 694 protected:
695 enum NigoriStatus {
696 DONT_WRITE_NIGORI,
697 WRITE_TO_NIGORI
698 };
699
700 enum EncryptionStatus {
701 DEFAULT_ENCRYPTION,
702 FULL_ENCRYPTION
703 };
704
658 SyncManagerTest() 705 SyncManagerTest()
659 : ui_thread_(BrowserThread::UI, &ui_loop_), 706 : ui_thread_(BrowserThread::UI, &ui_loop_),
660 sync_notifier_mock_(NULL), 707 sync_notifier_mock_(NULL),
661 sync_manager_("Test sync manager"), 708 sync_manager_("Test sync manager"),
662 sync_notifier_observer_(NULL), 709 sync_notifier_observer_(NULL),
663 update_enabled_types_call_count_(0) {} 710 update_enabled_types_call_count_(0) {}
664 711
665 virtual ~SyncManagerTest() { 712 virtual ~SyncManagerTest() {
666 EXPECT_FALSE(sync_notifier_mock_); 713 EXPECT_FALSE(sync_notifier_mock_);
667 } 714 }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
740 } 787 }
741 788
742 virtual void OnChangesApplied( 789 virtual void OnChangesApplied(
743 syncable::ModelType model_type, 790 syncable::ModelType model_type,
744 const BaseTransaction* trans, 791 const BaseTransaction* trans,
745 const ImmutableChangeRecordList& changes) OVERRIDE {} 792 const ImmutableChangeRecordList& changes) OVERRIDE {}
746 793
747 virtual void OnChangesComplete(syncable::ModelType model_type) OVERRIDE {} 794 virtual void OnChangesComplete(syncable::ModelType model_type) OVERRIDE {}
748 795
749 // Helper methods. 796 // Helper methods.
750 bool SetUpEncryption(bool write_to_nigori) { 797 bool SetUpEncryption(NigoriStatus nigori_status,
798 EncryptionStatus encryption_status) {
751 // Mock the Mac Keychain service. The real Keychain can block on user input. 799 // Mock the Mac Keychain service. The real Keychain can block on user input.
752 #if defined(OS_MACOSX) 800 #if defined(OS_MACOSX)
753 Encryptor::UseMockKeychain(true); 801 Encryptor::UseMockKeychain(true);
754 #endif 802 #endif
755 803
756 UserShare* share = sync_manager_.GetUserShare(); 804 UserShare* share = sync_manager_.GetUserShare();
757 { 805 {
758 syncable::ScopedDirLookup dir(share->dir_manager.get(), share->name); 806 syncable::ScopedDirLookup dir(share->dir_manager.get(), share->name);
759 if (!dir.good()) 807 if (!dir.good())
760 return false; 808 return false;
761 dir->set_initial_sync_ended_for_type(syncable::NIGORI, true); 809 dir->set_initial_sync_ended_for_type(syncable::NIGORI, true);
762 } 810 }
763 811
764 // We need to create the nigori node as if it were an applied server update. 812 // We need to create the nigori node as if it were an applied server update.
765 int64 nigori_id = GetIdForDataType(syncable::NIGORI); 813 int64 nigori_id = GetIdForDataType(syncable::NIGORI);
766 if (nigori_id == kInvalidId) 814 if (nigori_id == kInvalidId)
767 return false; 815 return false;
768 816
769 // Set the nigori cryptographer information. 817 // Set the nigori cryptographer information.
770 WriteTransaction trans(FROM_HERE, share); 818 WriteTransaction trans(FROM_HERE, share);
771 Cryptographer* cryptographer = trans.GetCryptographer(); 819 Cryptographer* cryptographer = trans.GetCryptographer();
772 if (!cryptographer) 820 if (!cryptographer)
773 return false; 821 return false;
774 KeyParams params = {"localhost", "dummy", "foobar"}; 822 KeyParams params = {"localhost", "dummy", "foobar"};
775 cryptographer->AddKey(params); 823 cryptographer->AddKey(params);
776 if (write_to_nigori) { 824 if (encryption_status == FULL_ENCRYPTION)
825 cryptographer->set_encrypt_everything();
826 if (nigori_status == WRITE_TO_NIGORI) {
777 sync_pb::NigoriSpecifics nigori; 827 sync_pb::NigoriSpecifics nigori;
778 cryptographer->GetKeys(nigori.mutable_encrypted()); 828 cryptographer->GetKeys(nigori.mutable_encrypted());
829 cryptographer->UpdateNigoriFromEncryptedTypes(&nigori);
779 WriteNode node(&trans); 830 WriteNode node(&trans);
780 EXPECT_TRUE(node.InitByIdLookup(nigori_id)); 831 EXPECT_TRUE(node.InitByIdLookup(nigori_id));
781 node.SetNigoriSpecifics(nigori); 832 node.SetNigoriSpecifics(nigori);
782 } 833 }
783 return cryptographer->is_ready(); 834 return cryptographer->is_ready();
784 } 835 }
785 836
786 int64 GetIdForDataType(ModelType type) { 837 int64 GetIdForDataType(ModelType type) {
787 if (type_roots_.count(type) == 0) 838 if (type_roots_.count(type) == 0)
788 return 0; 839 return 0;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
820 name, args, reply_handler); 871 name, args, reply_handler);
821 PumpLoop(); 872 PumpLoop();
822 } 873 }
823 874
824 void SetJsEventHandler(const WeakHandle<JsEventHandler>& event_handler) { 875 void SetJsEventHandler(const WeakHandle<JsEventHandler>& event_handler) {
825 js_backend_.Call(FROM_HERE, &JsBackend::SetJsEventHandler, 876 js_backend_.Call(FROM_HERE, &JsBackend::SetJsEventHandler,
826 event_handler); 877 event_handler);
827 PumpLoop(); 878 PumpLoop();
828 } 879 }
829 880
881 // Looks up an entry by client tag and resets IS_UNSYNCED value to false.
882 // Returns true if entry was previously unsynced, false if IS_UNSYNCED was
883 // already false.
884 bool ResetUnsyncedEntry(syncable::ModelType type,
885 const std::string& client_tag) {
886 UserShare* share = sync_manager_.GetUserShare();
887 syncable::ScopedDirLookup dir(share->dir_manager.get(), share->name);
888 EXPECT_TRUE(dir.good());
889 syncable::WriteTransaction trans(FROM_HERE, syncable::UNITTEST, dir);
890 const std::string hash = BaseNode::GenerateSyncableHash(type, client_tag);
891 syncable::MutableEntry entry(&trans, syncable::GET_BY_CLIENT_TAG,
892 hash);
893 EXPECT_TRUE(entry.good());
894 if (!entry.Get(IS_UNSYNCED))
895 return false;
896 else
akalin 2011/12/12 22:05:22 no need for 'else' since if branch returns
Nicolas Zea 2011/12/13 00:43:30 Done.
897 entry.Put(IS_UNSYNCED, false);
898 return true;
899 }
900
830 private: 901 private:
831 // Needed by |ui_thread_|. 902 // Needed by |ui_thread_|.
832 MessageLoopForUI ui_loop_; 903 MessageLoopForUI ui_loop_;
833 // Needed by |sync_manager_|. 904 // Needed by |sync_manager_|.
834 content::TestBrowserThread ui_thread_; 905 content::TestBrowserThread ui_thread_;
835 // Needed by |sync_manager_|. 906 // Needed by |sync_manager_|.
836 ScopedTempDir temp_dir_; 907 ScopedTempDir temp_dir_;
837 // Sync Id's for the roots of the enabled datatypes. 908 // Sync Id's for the roots of the enabled datatypes.
838 std::map<ModelType, int64> type_roots_; 909 std::map<ModelType, int64> type_roots_;
839 StrictMock<SyncNotifierMock>* sync_notifier_mock_; 910 StrictMock<SyncNotifierMock>* sync_notifier_mock_;
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
1187 SetJsEventHandler(WeakHandle<JsEventHandler>()); 1258 SetJsEventHandler(WeakHandle<JsEventHandler>());
1188 1259
1189 sync_manager_.TriggerOnIncomingNotificationForTest(empty_model_types); 1260 sync_manager_.TriggerOnIncomingNotificationForTest(empty_model_types);
1190 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); 1261 sync_manager_.TriggerOnIncomingNotificationForTest(model_types);
1191 1262
1192 // Should trigger the replies. 1263 // Should trigger the replies.
1193 PumpLoop(); 1264 PumpLoop();
1194 } 1265 }
1195 1266
1196 TEST_F(SyncManagerTest, RefreshEncryptionReady) { 1267 TEST_F(SyncManagerTest, RefreshEncryptionReady) {
1197 EXPECT_TRUE(SetUpEncryption(true)); 1268 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1198 EXPECT_CALL(observer_, OnEncryptionComplete()); 1269 EXPECT_CALL(observer_, OnEncryptionComplete());
1199 sync_manager_.RefreshEncryption(); 1270 sync_manager_.RefreshEncryption();
1200 syncable::ModelTypeSet encrypted_types = 1271 syncable::ModelTypeSet encrypted_types =
1201 sync_manager_.GetEncryptedDataTypesForTest(); 1272 sync_manager_.GetEncryptedDataTypesForTest();
1202 EXPECT_EQ(1U, encrypted_types.count(syncable::PASSWORDS)); 1273 EXPECT_EQ(1U, encrypted_types.count(syncable::PASSWORDS));
1203 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); 1274 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest());
1204 { 1275 {
1205 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1276 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1206 ReadNode node(&trans); 1277 ReadNode node(&trans);
1207 EXPECT_TRUE(node.InitByIdLookup(GetIdForDataType(syncable::NIGORI))); 1278 EXPECT_TRUE(node.InitByIdLookup(GetIdForDataType(syncable::NIGORI)));
(...skipping 10 matching lines...) Expand all
1218 // Don't set up encryption (no nigori node created). 1289 // Don't set up encryption (no nigori node created).
1219 sync_manager_.RefreshEncryption(); // Should fail. 1290 sync_manager_.RefreshEncryption(); // Should fail.
1220 syncable::ModelTypeSet encrypted_types = 1291 syncable::ModelTypeSet encrypted_types =
1221 sync_manager_.GetEncryptedDataTypesForTest(); 1292 sync_manager_.GetEncryptedDataTypesForTest();
1222 EXPECT_EQ(1U, encrypted_types.count(syncable::PASSWORDS)); // Hardcoded. 1293 EXPECT_EQ(1U, encrypted_types.count(syncable::PASSWORDS)); // Hardcoded.
1223 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); 1294 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest());
1224 } 1295 }
1225 1296
1226 // Attempt to refresh encryption when nigori is empty. 1297 // Attempt to refresh encryption when nigori is empty.
1227 TEST_F(SyncManagerTest, RefreshEncryptionEmptyNigori) { 1298 TEST_F(SyncManagerTest, RefreshEncryptionEmptyNigori) {
1228 EXPECT_TRUE(SetUpEncryption(false)); 1299 EXPECT_TRUE(SetUpEncryption(DONT_WRITE_NIGORI, DEFAULT_ENCRYPTION));
1229 EXPECT_CALL(observer_, OnEncryptionComplete()); 1300 EXPECT_CALL(observer_, OnEncryptionComplete());
1230 sync_manager_.RefreshEncryption(); // Should write to nigori. 1301 sync_manager_.RefreshEncryption(); // Should write to nigori.
1231 syncable::ModelTypeSet encrypted_types = 1302 syncable::ModelTypeSet encrypted_types =
1232 sync_manager_.GetEncryptedDataTypesForTest(); 1303 sync_manager_.GetEncryptedDataTypesForTest();
1233 EXPECT_EQ(1U, encrypted_types.count(syncable::PASSWORDS)); // Hardcoded. 1304 EXPECT_EQ(1U, encrypted_types.count(syncable::PASSWORDS)); // Hardcoded.
1234 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); 1305 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest());
1235 { 1306 {
1236 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1307 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1237 ReadNode node(&trans); 1308 ReadNode node(&trans);
1238 EXPECT_TRUE(node.InitByIdLookup(GetIdForDataType(syncable::NIGORI))); 1309 EXPECT_TRUE(node.InitByIdLookup(GetIdForDataType(syncable::NIGORI)));
1239 sync_pb::NigoriSpecifics nigori = node.GetNigoriSpecifics(); 1310 sync_pb::NigoriSpecifics nigori = node.GetNigoriSpecifics();
1240 EXPECT_TRUE(nigori.has_encrypted()); 1311 EXPECT_TRUE(nigori.has_encrypted());
1241 Cryptographer* cryptographer = trans.GetCryptographer(); 1312 Cryptographer* cryptographer = trans.GetCryptographer();
1242 EXPECT_TRUE(cryptographer->is_ready()); 1313 EXPECT_TRUE(cryptographer->is_ready());
1243 EXPECT_TRUE(cryptographer->CanDecrypt(nigori.encrypted())); 1314 EXPECT_TRUE(cryptographer->CanDecrypt(nigori.encrypted()));
1244 } 1315 }
1245 } 1316 }
1246 1317
1247 TEST_F(SyncManagerTest, EncryptDataTypesWithNoData) { 1318 TEST_F(SyncManagerTest, EncryptDataTypesWithNoData) {
1248 EXPECT_TRUE(SetUpEncryption(true)); 1319 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1249 EXPECT_CALL(observer_, 1320 EXPECT_CALL(observer_,
1250 OnEncryptedTypesChanged(GetAllRealModelTypes(), true)); 1321 OnEncryptedTypesChanged(GetAllRealModelTypes(), true));
1251 EXPECT_CALL(observer_, OnEncryptionComplete()); 1322 EXPECT_CALL(observer_, OnEncryptionComplete());
1252 sync_manager_.EnableEncryptEverything(); 1323 sync_manager_.EnableEncryptEverything();
1253 EXPECT_TRUE(sync_manager_.EncryptEverythingEnabledForTest()); 1324 EXPECT_TRUE(sync_manager_.EncryptEverythingEnabledForTest());
1254 } 1325 }
1255 1326
1256 TEST_F(SyncManagerTest, EncryptDataTypesWithData) { 1327 TEST_F(SyncManagerTest, EncryptDataTypesWithData) {
1257 size_t batch_size = 5; 1328 size_t batch_size = 5;
1258 EXPECT_TRUE(SetUpEncryption(true)); 1329 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1259 1330
1260 // Create some unencrypted unsynced data. 1331 // Create some unencrypted unsynced data.
1261 int64 folder = MakeFolderWithParent(sync_manager_.GetUserShare(), 1332 int64 folder = MakeFolderWithParent(sync_manager_.GetUserShare(),
1262 syncable::BOOKMARKS, 1333 syncable::BOOKMARKS,
1263 GetIdForDataType(syncable::BOOKMARKS), 1334 GetIdForDataType(syncable::BOOKMARKS),
1264 NULL); 1335 NULL);
1265 // First batch_size nodes are children of folder. 1336 // First batch_size nodes are children of folder.
1266 size_t i; 1337 size_t i;
1267 for (i = 0; i < batch_size; ++i) { 1338 for (i = 0; i < batch_size; ++i) {
1268 MakeNodeWithParent(sync_manager_.GetUserShare(), syncable::BOOKMARKS, 1339 MakeNodeWithParent(sync_manager_.GetUserShare(), syncable::BOOKMARKS,
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
1354 // Calling EncryptDataTypes with an empty encrypted types should not trigger 1425 // Calling EncryptDataTypes with an empty encrypted types should not trigger
1355 // a reencryption and should just notify immediately. 1426 // a reencryption and should just notify immediately.
1356 // TODO(zea): add logic to ensure nothing was written. 1427 // TODO(zea): add logic to ensure nothing was written.
1357 testing::Mock::VerifyAndClearExpectations(&observer_); 1428 testing::Mock::VerifyAndClearExpectations(&observer_);
1358 EXPECT_CALL(observer_, OnPassphraseAccepted(_)).Times(0); 1429 EXPECT_CALL(observer_, OnPassphraseAccepted(_)).Times(0);
1359 EXPECT_CALL(observer_, OnEncryptionComplete()); 1430 EXPECT_CALL(observer_, OnEncryptionComplete());
1360 sync_manager_.EnableEncryptEverything(); 1431 sync_manager_.EnableEncryptEverything();
1361 } 1432 }
1362 1433
1363 TEST_F(SyncManagerTest, SetPassphraseWithPassword) { 1434 TEST_F(SyncManagerTest, SetPassphraseWithPassword) {
1364 EXPECT_TRUE(SetUpEncryption(true)); 1435 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1365 { 1436 {
1366 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1437 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1367 ReadNode root_node(&trans); 1438 ReadNode root_node(&trans);
1368 root_node.InitByRootLookup(); 1439 root_node.InitByRootLookup();
1369 1440
1370 WriteNode password_node(&trans); 1441 WriteNode password_node(&trans);
1371 EXPECT_TRUE(password_node.InitUniqueByCreation(syncable::PASSWORDS, 1442 EXPECT_TRUE(password_node.InitUniqueByCreation(syncable::PASSWORDS,
1372 root_node, "foo")); 1443 root_node, "foo"));
1373 sync_pb::PasswordSpecificsData data; 1444 sync_pb::PasswordSpecificsData data;
1374 data.set_password_value("secret"); 1445 data.set_password_value("secret");
1375 password_node.SetPasswordSpecifics(data); 1446 password_node.SetPasswordSpecifics(data);
1376 } 1447 }
1377 EXPECT_CALL(observer_, OnPassphraseAccepted(_)); 1448 EXPECT_CALL(observer_, OnPassphraseAccepted(_));
1378 EXPECT_CALL(observer_, OnEncryptionComplete()); 1449 EXPECT_CALL(observer_, OnEncryptionComplete());
1379 sync_manager_.SetPassphrase("new_passphrase", true); 1450 sync_manager_.SetPassphrase("new_passphrase", true);
1380 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); 1451 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest());
1381 { 1452 {
1382 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1453 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1383 ReadNode password_node(&trans); 1454 ReadNode password_node(&trans);
1384 EXPECT_TRUE(password_node.InitByClientTagLookup(syncable::PASSWORDS, 1455 EXPECT_TRUE(password_node.InitByClientTagLookup(syncable::PASSWORDS,
1385 "foo")); 1456 "foo"));
1386 const sync_pb::PasswordSpecificsData& data = 1457 const sync_pb::PasswordSpecificsData& data =
1387 password_node.GetPasswordSpecifics(); 1458 password_node.GetPasswordSpecifics();
1388 EXPECT_EQ("secret", data.password_value()); 1459 EXPECT_EQ("secret", data.password_value());
1389 } 1460 }
1390 } 1461 }
1391 1462
1392 TEST_F(SyncManagerTest, SetPassphraseWithEmptyPasswordNode) { 1463 TEST_F(SyncManagerTest, SetPassphraseWithEmptyPasswordNode) {
1393 EXPECT_TRUE(SetUpEncryption(true)); 1464 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1394 int64 node_id = 0; 1465 int64 node_id = 0;
1395 std::string tag = "foo"; 1466 std::string tag = "foo";
1396 { 1467 {
1397 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1468 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1398 ReadNode root_node(&trans); 1469 ReadNode root_node(&trans);
1399 root_node.InitByRootLookup(); 1470 root_node.InitByRootLookup();
1400 1471
1401 WriteNode password_node(&trans); 1472 WriteNode password_node(&trans);
1402 EXPECT_TRUE(password_node.InitUniqueByCreation(syncable::PASSWORDS, 1473 EXPECT_TRUE(password_node.InitUniqueByCreation(syncable::PASSWORDS,
1403 root_node, tag)); 1474 root_node, tag));
1404 node_id = password_node.GetId(); 1475 node_id = password_node.GetId();
1405 } 1476 }
1406 EXPECT_CALL(observer_, OnPassphraseAccepted(_)); 1477 EXPECT_CALL(observer_, OnPassphraseAccepted(_));
1407 EXPECT_CALL(observer_, OnEncryptionComplete()); 1478 EXPECT_CALL(observer_, OnEncryptionComplete());
1408 sync_manager_.SetPassphrase("new_passphrase", true); 1479 sync_manager_.SetPassphrase("new_passphrase", true);
1409 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest()); 1480 EXPECT_FALSE(sync_manager_.EncryptEverythingEnabledForTest());
1410 { 1481 {
1411 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1482 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1412 ReadNode password_node(&trans); 1483 ReadNode password_node(&trans);
1413 EXPECT_FALSE(password_node.InitByClientTagLookup(syncable::PASSWORDS, 1484 EXPECT_FALSE(password_node.InitByClientTagLookup(syncable::PASSWORDS,
1414 tag)); 1485 tag));
1415 } 1486 }
1416 { 1487 {
1417 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 1488 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1418 ReadNode password_node(&trans); 1489 ReadNode password_node(&trans);
1419 EXPECT_FALSE(password_node.InitByIdLookup(node_id)); 1490 EXPECT_FALSE(password_node.InitByIdLookup(node_id));
1420 } 1491 }
1421 } 1492 }
1422 1493
1423 } // namespace
1424
1425 // Friended by WriteNode, so can't be in an anonymouse namespace. 1494 // Friended by WriteNode, so can't be in an anonymouse namespace.
1426 TEST_F(SyncManagerTest, EncryptBookmarksWithLegacyData) { 1495 TEST_F(SyncManagerTest, EncryptBookmarksWithLegacyData) {
1427 EXPECT_TRUE(SetUpEncryption(true)); 1496 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
1428 std::string title; 1497 std::string title;
1429 SyncAPINameToServerName("Google", &title); 1498 SyncAPINameToServerName("Google", &title);
1430 std::string url = "http://www.google.com"; 1499 std::string url = "http://www.google.com";
1431 std::string raw_title2 = ".."; // An invalid cosmo title. 1500 std::string raw_title2 = ".."; // An invalid cosmo title.
1432 std::string title2; 1501 std::string title2;
1433 SyncAPINameToServerName(raw_title2, &title2); 1502 SyncAPINameToServerName(raw_title2, &title2);
1434 std::string url2 = "http://www.bla.com"; 1503 std::string url2 = "http://www.bla.com";
1435 1504
1436 // Create a bookmark using the legacy format. 1505 // Create a bookmark using the legacy format.
1437 int64 node_id1 = MakeNode(sync_manager_.GetUserShare(), 1506 int64 node_id1 = MakeNode(sync_manager_.GetUserShare(),
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
1519 EXPECT_TRUE(node2.InitByIdLookup(node_id2)); 1588 EXPECT_TRUE(node2.InitByIdLookup(node_id2));
1520 EXPECT_EQ(syncable::BOOKMARKS, node2.GetModelType()); 1589 EXPECT_EQ(syncable::BOOKMARKS, node2.GetModelType());
1521 // We should de-canonicalize the title in GetTitle(), but the title in the 1590 // We should de-canonicalize the title in GetTitle(), but the title in the
1522 // specifics should be stored in the server legal form. 1591 // specifics should be stored in the server legal form.
1523 EXPECT_EQ(raw_title2, node2.GetTitle()); 1592 EXPECT_EQ(raw_title2, node2.GetTitle());
1524 EXPECT_EQ(title2, node2.GetBookmarkSpecifics().title()); 1593 EXPECT_EQ(title2, node2.GetBookmarkSpecifics().title());
1525 EXPECT_EQ(url2, node2.GetBookmarkSpecifics().url()); 1594 EXPECT_EQ(url2, node2.GetBookmarkSpecifics().url());
1526 } 1595 }
1527 } 1596 }
1528 1597
1598 // Verifies WriteNode::UpdateEntryWithEncryption does not make unnecessary
1599 // changes.
1600 TEST_F(SyncManagerTest, UpdateEntryWithEncryption) {
1601 std::string client_tag = "title";
1602 sync_pb::EntitySpecifics entity_specifics;
1603 entity_specifics.MutableExtension(sync_pb::bookmark)->set_url("url");
1604 entity_specifics.MutableExtension(sync_pb::bookmark)->set_title("title");
1605 MakeServerNode(sync_manager_.GetUserShare(), syncable::BOOKMARKS, client_tag,
1606 BaseNode::GenerateSyncableHash(syncable::BOOKMARKS,
1607 client_tag),
1608 entity_specifics);
1609 // New node shouldn't start off unsynced.
1610 EXPECT_FALSE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag));
1611 // Manually change to the same data. Should not set is_unsynced.
1612 {
1613 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1614 WriteNode node(&trans);
1615 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag));
1616 node.SetEntitySpecifics(entity_specifics);
1617 }
1618 EXPECT_FALSE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag));
1619
1620 // Encrypt the datatatype, should set is_unsynced.
1621 EXPECT_CALL(observer_,
1622 OnEncryptedTypesChanged(GetAllRealModelTypes(), true));
1623 EXPECT_CALL(observer_, OnEncryptionComplete());
1624 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, FULL_ENCRYPTION));
1625 sync_manager_.RefreshEncryption();
1626 {
1627 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1628 ReadNode node(&trans);
1629 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag));
1630 const syncable::Entry* node_entry = node.GetEntry();
1631 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
1632 EXPECT_TRUE(specifics.has_encrypted());
1633 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
1634 Cryptographer* cryptographer = trans.GetCryptographer();
1635 EXPECT_TRUE(cryptographer->is_ready());
1636 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey(
1637 specifics.encrypted()));
1638 }
1639 EXPECT_TRUE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag));
1640
1641 // Set a new passphrase. Should set is_unsynced.
1642 testing::Mock::VerifyAndClearExpectations(&observer_);
1643 EXPECT_CALL(observer_, OnPassphraseAccepted(_));
1644 EXPECT_CALL(observer_, OnEncryptionComplete());
1645 sync_manager_.SetPassphrase("new_passphrase", true);
1646 {
1647 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1648 ReadNode node(&trans);
1649 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag));
1650 const syncable::Entry* node_entry = node.GetEntry();
1651 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
1652 EXPECT_TRUE(specifics.has_encrypted());
1653 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
1654 Cryptographer* cryptographer = trans.GetCryptographer();
1655 EXPECT_TRUE(cryptographer->is_ready());
1656 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey(
1657 specifics.encrypted()));
1658 }
1659 EXPECT_TRUE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag));
1660
1661 // Force a re-encrypt everything. Should not set is_unsynced.
1662 testing::Mock::VerifyAndClearExpectations(&observer_);
1663 EXPECT_CALL(observer_, OnEncryptionComplete());
1664 sync_manager_.RefreshEncryption();
1665 {
1666 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1667 ReadNode node(&trans);
1668 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag));
1669 const syncable::Entry* node_entry = node.GetEntry();
1670 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
1671 EXPECT_TRUE(specifics.has_encrypted());
1672 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
1673 Cryptographer* cryptographer = trans.GetCryptographer();
1674 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey(
1675 specifics.encrypted()));
1676 }
1677 EXPECT_FALSE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag));
1678
1679 // Manually change to the same data. Should not set is_unsynced.
1680 {
1681 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1682 WriteNode node(&trans);
1683 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag));
1684 node.SetEntitySpecifics(entity_specifics);
1685 const syncable::Entry* node_entry = node.GetEntry();
1686 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
1687 EXPECT_TRUE(specifics.has_encrypted());
1688 EXPECT_FALSE(node_entry->Get(IS_UNSYNCED));
1689 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
1690 Cryptographer* cryptographer = trans.GetCryptographer();
1691 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey(
1692 specifics.encrypted()));
1693 }
1694 EXPECT_FALSE(ResetUnsyncedEntry(syncable::BOOKMARKS, client_tag));
1695
1696 // Manually change to different data. Should set is_unsynced.
1697 {
1698 entity_specifics.MutableExtension(sync_pb::bookmark)->set_url("url2");
1699 entity_specifics.MutableExtension(sync_pb::bookmark)->set_title("title2");
1700 WriteTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
1701 WriteNode node(&trans);
1702 EXPECT_TRUE(node.InitByClientTagLookup(syncable::BOOKMARKS, client_tag));
1703 node.SetEntitySpecifics(entity_specifics);
1704 const syncable::Entry* node_entry = node.GetEntry();
1705 const sync_pb::EntitySpecifics& specifics = node_entry->Get(SPECIFICS);
1706 EXPECT_TRUE(specifics.has_encrypted());
1707 EXPECT_TRUE(node_entry->Get(IS_UNSYNCED));
1708 EXPECT_EQ(kEncryptedString, node_entry->Get(NON_UNIQUE_NAME));
1709 Cryptographer* cryptographer = trans.GetCryptographer();
1710 EXPECT_TRUE(cryptographer->CanDecryptUsingDefaultKey(
1711 specifics.encrypted()));
1712 }
1713 }
1714
1529 } // namespace browser_sync 1715 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698