| OLD | NEW |
| 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> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| 16 #include "base/scoped_temp_dir.h" | 16 #include "base/scoped_temp_dir.h" |
| 17 #include "base/string_number_conversions.h" | 17 #include "base/string_number_conversions.h" |
| 18 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
| 19 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
| 20 #include "base/values.h" | 20 #include "base/values.h" |
| 21 #include "chrome/browser/password_manager/encryptor.h" |
| 21 #include "chrome/browser/sync/engine/http_post_provider_factory.h" | 22 #include "chrome/browser/sync/engine/http_post_provider_factory.h" |
| 22 #include "chrome/browser/sync/engine/http_post_provider_interface.h" | 23 #include "chrome/browser/sync/engine/http_post_provider_interface.h" |
| 23 #include "chrome/browser/sync/engine/model_safe_worker.h" | 24 #include "chrome/browser/sync/engine/model_safe_worker.h" |
| 24 #include "chrome/browser/sync/engine/syncapi.h" | 25 #include "chrome/browser/sync/engine/syncapi.h" |
| 25 #include "chrome/browser/sync/js_arg_list.h" | 26 #include "chrome/browser/sync/js_arg_list.h" |
| 26 #include "chrome/browser/sync/js_backend.h" | 27 #include "chrome/browser/sync/js_backend.h" |
| 27 #include "chrome/browser/sync/js_event_handler.h" | 28 #include "chrome/browser/sync/js_event_handler.h" |
| 28 #include "chrome/browser/sync/js_event_router.h" | 29 #include "chrome/browser/sync/js_event_router.h" |
| 29 #include "chrome/browser/sync/js_test_util.h" | 30 #include "chrome/browser/sync/js_test_util.h" |
| 30 #include "chrome/browser/sync/notifier/sync_notifier.h" | 31 #include "chrome/browser/sync/notifier/sync_notifier.h" |
| (...skipping 721 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 752 virtual void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) { | 753 virtual void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) { |
| 753 (*out)[syncable::NIGORI] = browser_sync::GROUP_PASSIVE; | 754 (*out)[syncable::NIGORI] = browser_sync::GROUP_PASSIVE; |
| 754 (*out)[syncable::BOOKMARKS] = browser_sync::GROUP_PASSIVE; | 755 (*out)[syncable::BOOKMARKS] = browser_sync::GROUP_PASSIVE; |
| 755 (*out)[syncable::THEMES] = browser_sync::GROUP_PASSIVE; | 756 (*out)[syncable::THEMES] = browser_sync::GROUP_PASSIVE; |
| 756 (*out)[syncable::SESSIONS] = browser_sync::GROUP_PASSIVE; | 757 (*out)[syncable::SESSIONS] = browser_sync::GROUP_PASSIVE; |
| 757 (*out)[syncable::PASSWORDS] = browser_sync::GROUP_PASSIVE; | 758 (*out)[syncable::PASSWORDS] = browser_sync::GROUP_PASSIVE; |
| 758 } | 759 } |
| 759 | 760 |
| 760 // Helper methods. | 761 // Helper methods. |
| 761 bool SetUpEncryption() { | 762 bool SetUpEncryption() { |
| 763 // Mock the Mac Keychain service. The real Keychain can block on user input. |
| 764 #if defined(OS_MACOSX) |
| 765 Encryptor::UseMockKeychain(true); |
| 766 #endif |
| 767 |
| 762 // We need to create the nigori node as if it were an applied server update. | 768 // We need to create the nigori node as if it were an applied server update. |
| 763 UserShare* share = sync_manager_.GetUserShare(); | 769 UserShare* share = sync_manager_.GetUserShare(); |
| 764 int64 nigori_id = GetIdForDataType(syncable::NIGORI); | 770 int64 nigori_id = GetIdForDataType(syncable::NIGORI); |
| 765 if (nigori_id == kInvalidId) | 771 if (nigori_id == kInvalidId) |
| 766 return false; | 772 return false; |
| 767 | 773 |
| 768 // Set the nigori cryptographer information. | 774 // Set the nigori cryptographer information. |
| 769 WriteTransaction trans(share); | 775 WriteTransaction trans(share); |
| 770 Cryptographer* cryptographer = trans.GetCryptographer(); | 776 Cryptographer* cryptographer = trans.GetCryptographer(); |
| 771 if (!cryptographer) | 777 if (!cryptographer) |
| (...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1233 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); | 1239 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); |
| 1234 | 1240 |
| 1235 js_backend->SetParentJsEventRouter(&event_router); | 1241 js_backend->SetParentJsEventRouter(&event_router); |
| 1236 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); | 1242 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); |
| 1237 js_backend->RemoveParentJsEventRouter(); | 1243 js_backend->RemoveParentJsEventRouter(); |
| 1238 | 1244 |
| 1239 sync_manager_.TriggerOnIncomingNotificationForTest(empty_model_types); | 1245 sync_manager_.TriggerOnIncomingNotificationForTest(empty_model_types); |
| 1240 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); | 1246 sync_manager_.TriggerOnIncomingNotificationForTest(model_types); |
| 1241 } | 1247 } |
| 1242 | 1248 |
| 1249 TEST_F(SyncManagerTest, RefreshEncryptionReady) { |
| 1250 EXPECT_TRUE(SetUpEncryption()); |
| 1251 sync_manager_.RefreshEncryption(); |
| 1252 syncable::ModelTypeSet encrypted_types = |
| 1253 sync_manager_.GetEncryptedDataTypes(); |
| 1254 EXPECT_EQ(1U, encrypted_types.count(syncable::PASSWORDS)); |
| 1255 } |
| 1256 |
| 1257 // Attempt to refresh encryption when nigori not downloaded. |
| 1258 TEST_F(SyncManagerTest, RefreshEncryptionNotReady) { |
| 1259 // Don't set up encryption (no nigori node created). |
| 1260 sync_manager_.RefreshEncryption(); // Should fail. |
| 1261 syncable::ModelTypeSet encrypted_types = |
| 1262 sync_manager_.GetEncryptedDataTypes(); |
| 1263 EXPECT_EQ(1U, encrypted_types.count(syncable::PASSWORDS)); // Hardcoded. |
| 1264 } |
| 1265 |
| 1243 TEST_F(SyncManagerTest, EncryptDataTypesWithNoData) { | 1266 TEST_F(SyncManagerTest, EncryptDataTypesWithNoData) { |
| 1244 EXPECT_TRUE(SetUpEncryption()); | 1267 EXPECT_TRUE(SetUpEncryption()); |
| 1245 ModelTypeSet encrypted_types; | 1268 ModelTypeSet encrypted_types; |
| 1246 encrypted_types.insert(syncable::BOOKMARKS); | 1269 encrypted_types.insert(syncable::BOOKMARKS); |
| 1247 // Even though Passwords isn't marked for encryption, it's enabled, so it | 1270 // Even though Passwords isn't marked for encryption, it's enabled, so it |
| 1248 // should automatically be added to the response of OnEncryptionComplete. | 1271 // should automatically be added to the response of OnEncryptionComplete. |
| 1249 ModelTypeSet expected_types = encrypted_types; | 1272 ModelTypeSet expected_types = encrypted_types; |
| 1250 expected_types.insert(syncable::PASSWORDS); | 1273 expected_types.insert(syncable::PASSWORDS); |
| 1251 EXPECT_CALL(observer_, OnEncryptionComplete(expected_types)); | 1274 EXPECT_CALL(observer_, OnEncryptionComplete(expected_types)); |
| 1252 sync_manager_.EncryptDataTypes(encrypted_types); | 1275 sync_manager_.EncryptDataTypes(encrypted_types); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1314 true /* is encrypted */)); | 1337 true /* is encrypted */)); |
| 1315 EXPECT_TRUE(syncable::VerifyDataTypeEncryption(trans.GetWrappedTrans(), | 1338 EXPECT_TRUE(syncable::VerifyDataTypeEncryption(trans.GetWrappedTrans(), |
| 1316 syncable::SESSIONS, | 1339 syncable::SESSIONS, |
| 1317 true /* is encrypted */)); | 1340 true /* is encrypted */)); |
| 1318 EXPECT_TRUE(syncable::VerifyDataTypeEncryption(trans.GetWrappedTrans(), | 1341 EXPECT_TRUE(syncable::VerifyDataTypeEncryption(trans.GetWrappedTrans(), |
| 1319 syncable::THEMES, | 1342 syncable::THEMES, |
| 1320 false /* not encrypted */)); | 1343 false /* not encrypted */)); |
| 1321 } | 1344 } |
| 1322 } | 1345 } |
| 1323 | 1346 |
| 1347 TEST_F(SyncManagerTest, SetPassphraseWithPassword) { |
| 1348 EXPECT_TRUE(SetUpEncryption()); |
| 1349 { |
| 1350 WriteTransaction trans(sync_manager_.GetUserShare()); |
| 1351 ReadNode root_node(&trans); |
| 1352 root_node.InitByRootLookup(); |
| 1353 |
| 1354 WriteNode password_node(&trans); |
| 1355 EXPECT_TRUE(password_node.InitUniqueByCreation(syncable::PASSWORDS, |
| 1356 root_node, "foo")); |
| 1357 sync_pb::PasswordSpecificsData data; |
| 1358 data.set_password_value("secret"); |
| 1359 password_node.SetPasswordSpecifics(data); |
| 1360 } |
| 1361 EXPECT_CALL(observer_, OnPassphraseAccepted(_)); |
| 1362 EXPECT_CALL(observer_, OnEncryptionComplete(_)); |
| 1363 sync_manager_.SetPassphrase("new_passphrase", true); |
| 1364 { |
| 1365 ReadTransaction trans(sync_manager_.GetUserShare()); |
| 1366 ReadNode root_node(&trans); |
| 1367 root_node.InitByRootLookup(); |
| 1368 |
| 1369 ReadNode password_node(&trans); |
| 1370 EXPECT_TRUE(password_node.InitByClientTagLookup(syncable::PASSWORDS, |
| 1371 "foo")); |
| 1372 const sync_pb::PasswordSpecificsData& data = |
| 1373 password_node.GetPasswordSpecifics(); |
| 1374 EXPECT_EQ("secret", data.password_value()); |
| 1375 } |
| 1376 } |
| 1377 |
| 1378 TEST_F(SyncManagerTest, SetPassphraseWithEmptyPasswordNode) { |
| 1379 EXPECT_TRUE(SetUpEncryption()); |
| 1380 int64 node_id = 0; |
| 1381 std::string tag = "foo"; |
| 1382 { |
| 1383 WriteTransaction trans(sync_manager_.GetUserShare()); |
| 1384 ReadNode root_node(&trans); |
| 1385 root_node.InitByRootLookup(); |
| 1386 |
| 1387 WriteNode password_node(&trans); |
| 1388 EXPECT_TRUE(password_node.InitUniqueByCreation(syncable::PASSWORDS, |
| 1389 root_node, tag)); |
| 1390 node_id = password_node.GetId(); |
| 1391 } |
| 1392 EXPECT_CALL(observer_, OnPassphraseAccepted(_)); |
| 1393 EXPECT_CALL(observer_, OnEncryptionComplete(_)); |
| 1394 sync_manager_.SetPassphrase("new_passphrase", true); |
| 1395 { |
| 1396 ReadTransaction trans(sync_manager_.GetUserShare()); |
| 1397 ReadNode root_node(&trans); |
| 1398 root_node.InitByRootLookup(); |
| 1399 |
| 1400 ReadNode password_node(&trans); |
| 1401 EXPECT_FALSE(password_node.InitByClientTagLookup(syncable::PASSWORDS, |
| 1402 tag)); |
| 1403 } |
| 1404 { |
| 1405 ReadTransaction trans(sync_manager_.GetUserShare()); |
| 1406 ReadNode root_node(&trans); |
| 1407 root_node.InitByRootLookup(); |
| 1408 |
| 1409 ReadNode password_node(&trans); |
| 1410 EXPECT_FALSE(password_node.InitByIdLookup(node_id)); |
| 1411 } |
| 1412 } |
| 1413 |
| 1324 } // namespace | 1414 } // namespace |
| 1325 | 1415 |
| 1326 } // namespace browser_sync | 1416 } // namespace browser_sync |
| OLD | NEW |