OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 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 "components/sync/core_impl/sync_manager_impl.h" | 9 #include "components/sync/core_impl/sync_manager_impl.h" |
10 | 10 |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 return entry.GetMetahandle(); | 214 return entry.GetMetahandle(); |
215 } | 215 } |
216 | 216 |
217 int GetTotalNodeCount(UserShare* share, int64_t root) { | 217 int GetTotalNodeCount(UserShare* share, int64_t root) { |
218 ReadTransaction trans(FROM_HERE, share); | 218 ReadTransaction trans(FROM_HERE, share); |
219 ReadNode node(&trans); | 219 ReadNode node(&trans); |
220 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(root)); | 220 EXPECT_EQ(BaseNode::INIT_OK, node.InitByIdLookup(root)); |
221 return node.GetTotalNodeCount(); | 221 return node.GetTotalNodeCount(); |
222 } | 222 } |
223 | 223 |
| 224 const char kUrl[] = "example.com"; |
| 225 const char kPasswordValue[] = "secret"; |
| 226 const char kClientTag[] = "tag"; |
| 227 |
224 } // namespace | 228 } // namespace |
225 | 229 |
226 class SyncApiTest : public testing::Test { | 230 class SyncApiTest : public testing::Test { |
227 public: | 231 public: |
228 void SetUp() override { test_user_share_.SetUp(); } | 232 void SetUp() override { test_user_share_.SetUp(); } |
229 | 233 |
230 void TearDown() override { test_user_share_.TearDown(); } | 234 void TearDown() override { test_user_share_.TearDown(); } |
231 | 235 |
232 protected: | 236 protected: |
233 // Create an entry with the given |model_type|, |client_tag| and | 237 // Create an entry with the given |model_type|, |client_tag| and |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
510 ReadTransaction trans(FROM_HERE, user_share()); | 514 ReadTransaction trans(FROM_HERE, user_share()); |
511 trans.GetCryptographer()->AddKey(params); | 515 trans.GetCryptographer()->AddKey(params); |
512 } | 516 } |
513 { | 517 { |
514 WriteTransaction trans(FROM_HERE, user_share()); | 518 WriteTransaction trans(FROM_HERE, user_share()); |
515 ReadNode root_node(&trans); | 519 ReadNode root_node(&trans); |
516 root_node.InitByRootLookup(); | 520 root_node.InitByRootLookup(); |
517 | 521 |
518 WriteNode password_node(&trans); | 522 WriteNode password_node(&trans); |
519 WriteNode::InitUniqueByCreationResult result = | 523 WriteNode::InitUniqueByCreationResult result = |
520 password_node.InitUniqueByCreation(PASSWORDS, root_node, "foo"); | 524 password_node.InitUniqueByCreation(PASSWORDS, root_node, kClientTag); |
521 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); | 525 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); |
522 sync_pb::PasswordSpecificsData data; | 526 sync_pb::PasswordSpecificsData data; |
523 data.set_password_value("secret"); | 527 data.set_password_value(kPasswordValue); |
524 password_node.SetPasswordSpecifics(data); | 528 password_node.SetPasswordSpecifics(data); |
525 } | 529 } |
526 { | 530 { |
527 ReadTransaction trans(FROM_HERE, user_share()); | 531 ReadTransaction trans(FROM_HERE, user_share()); |
528 | 532 |
529 ReadNode password_node(&trans); | 533 ReadNode password_node(&trans); |
530 EXPECT_EQ(BaseNode::INIT_OK, | 534 EXPECT_EQ(BaseNode::INIT_OK, |
531 password_node.InitByClientTagLookup(PASSWORDS, "foo")); | 535 password_node.InitByClientTagLookup(PASSWORDS, kClientTag)); |
532 const sync_pb::PasswordSpecificsData& data = | 536 const sync_pb::PasswordSpecificsData& data = |
533 password_node.GetPasswordSpecifics(); | 537 password_node.GetPasswordSpecifics(); |
534 EXPECT_EQ("secret", data.password_value()); | 538 EXPECT_EQ(kPasswordValue, data.password_value()); |
| 539 // Check that nothing has appeared in the unencrypted field. |
| 540 EXPECT_FALSE(password_node.GetEntitySpecifics() |
| 541 .password() |
| 542 .has_unencrypted_metadata()); |
535 } | 543 } |
536 } | 544 } |
537 | 545 |
538 TEST_F(SyncApiTest, WriteEncryptedTitle) { | 546 TEST_F(SyncApiTest, WriteEncryptedTitle) { |
539 KeyParams params = {"localhost", "username", "passphrase"}; | 547 KeyParams params = {"localhost", "username", "passphrase"}; |
540 { | 548 { |
541 ReadTransaction trans(FROM_HERE, user_share()); | 549 ReadTransaction trans(FROM_HERE, user_share()); |
542 trans.GetCryptographer()->AddKey(params); | 550 trans.GetCryptographer()->AddKey(params); |
543 } | 551 } |
544 encryption_handler()->EnableEncryptEverything(); | 552 encryption_handler()->EnableEncryptEverything(); |
(...skipping 1491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2036 data.set_password_value("secret2"); | 2044 data.set_password_value("secret2"); |
2037 cryptographer->Encrypt( | 2045 cryptographer->Encrypt( |
2038 data, entity_specifics.mutable_password()->mutable_encrypted()); | 2046 data, entity_specifics.mutable_password()->mutable_encrypted()); |
2039 node.SetPasswordSpecifics(data); | 2047 node.SetPasswordSpecifics(data); |
2040 const syncable::Entry* node_entry = node.GetEntry(); | 2048 const syncable::Entry* node_entry = node.GetEntry(); |
2041 EXPECT_TRUE(node_entry->GetIsUnsynced()); | 2049 EXPECT_TRUE(node_entry->GetIsUnsynced()); |
2042 } | 2050 } |
2043 } | 2051 } |
2044 | 2052 |
2045 // Passwords have their own handling for encryption. Verify setting a new | 2053 // Passwords have their own handling for encryption. Verify setting a new |
2046 // passphrase updates the data. | 2054 // passphrase updates the data and clears the unencrypted metadta for passwords. |
2047 TEST_F(SyncManagerTest, UpdatePasswordNewPassphrase) { | 2055 TEST_F(SyncManagerTest, UpdatePasswordNewPassphrase) { |
2048 std::string client_tag = "title"; | |
2049 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | 2056 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); |
2050 sync_pb::EntitySpecifics entity_specifics; | 2057 sync_pb::EntitySpecifics entity_specifics; |
2051 { | 2058 { |
2052 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | 2059 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); |
2053 Cryptographer* cryptographer = trans.GetCryptographer(); | 2060 Cryptographer* cryptographer = trans.GetCryptographer(); |
2054 sync_pb::PasswordSpecificsData data; | 2061 sync_pb::PasswordSpecificsData data; |
2055 data.set_password_value("secret"); | 2062 data.set_password_value(kPasswordValue); |
| 2063 entity_specifics.mutable_password() |
| 2064 ->mutable_unencrypted_metadata() |
| 2065 ->set_url(kUrl); |
2056 cryptographer->Encrypt( | 2066 cryptographer->Encrypt( |
2057 data, entity_specifics.mutable_password()->mutable_encrypted()); | 2067 data, entity_specifics.mutable_password()->mutable_encrypted()); |
2058 } | 2068 } |
2059 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, client_tag, | 2069 EXPECT_TRUE(entity_specifics.password().has_unencrypted_metadata()); |
2060 syncable::GenerateSyncableHash(PASSWORDS, client_tag), | 2070 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, kClientTag, |
| 2071 syncable::GenerateSyncableHash(PASSWORDS, kClientTag), |
2061 entity_specifics); | 2072 entity_specifics); |
2062 // New node shouldn't start off unsynced. | 2073 // New node shouldn't start off unsynced. |
2063 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag)); | 2074 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, kClientTag)); |
2064 | 2075 |
2065 // Set a new passphrase. Should set is_unsynced. | 2076 // Set a new passphrase. Should set is_unsynced. |
2066 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); | 2077 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); |
2067 EXPECT_CALL(encryption_observer_, | 2078 EXPECT_CALL(encryption_observer_, |
2068 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN)); | 2079 OnBootstrapTokenUpdated(_, PASSPHRASE_BOOTSTRAP_TOKEN)); |
2069 ExpectPassphraseAcceptance(); | 2080 ExpectPassphraseAcceptance(); |
2070 SetCustomPassphraseAndCheck("new_passphrase"); | 2081 SetCustomPassphraseAndCheck("new_passphrase"); |
2071 EXPECT_TRUE(ResetUnsyncedEntry(PASSWORDS, client_tag)); | 2082 { |
| 2083 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); |
| 2084 Cryptographer* cryptographer = trans.GetCryptographer(); |
| 2085 EXPECT_TRUE(cryptographer->is_ready()); |
| 2086 ReadNode password_node(&trans); |
| 2087 EXPECT_EQ(BaseNode::INIT_OK, |
| 2088 password_node.InitByClientTagLookup(PASSWORDS, kClientTag)); |
| 2089 const sync_pb::PasswordSpecificsData& data = |
| 2090 password_node.GetPasswordSpecifics(); |
| 2091 EXPECT_EQ(kPasswordValue, data.password_value()); |
| 2092 EXPECT_FALSE(password_node.GetEntitySpecifics() |
| 2093 .password() |
| 2094 .has_unencrypted_metadata()); |
| 2095 } |
| 2096 EXPECT_TRUE(ResetUnsyncedEntry(PASSWORDS, kClientTag)); |
2072 } | 2097 } |
2073 | 2098 |
2074 // Passwords have their own handling for encryption. Verify it does not result | 2099 // Passwords have their own handling for encryption. Verify it does not result |
2075 // in unnecessary writes via ReencryptEverything. | 2100 // in unnecessary writes via ReencryptEverything. |
2076 TEST_F(SyncManagerTest, UpdatePasswordReencryptEverything) { | 2101 TEST_F(SyncManagerTest, UpdatePasswordReencryptEverything) { |
2077 std::string client_tag = "title"; | 2102 std::string client_tag = "title"; |
2078 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); | 2103 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); |
2079 sync_pb::EntitySpecifics entity_specifics; | 2104 sync_pb::EntitySpecifics entity_specifics; |
2080 { | 2105 { |
2081 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); | 2106 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); |
(...skipping 1252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3334 // SyncManagerInitInvalidStorageTest::GetFactory will return | 3359 // SyncManagerInitInvalidStorageTest::GetFactory will return |
3335 // DirectoryBackingStore that ensures that SyncManagerImpl::OpenDirectory fails. | 3360 // DirectoryBackingStore that ensures that SyncManagerImpl::OpenDirectory fails. |
3336 // SyncManagerImpl initialization is done in SyncManagerTest::SetUp. This test's | 3361 // SyncManagerImpl initialization is done in SyncManagerTest::SetUp. This test's |
3337 // task is to ensure that SyncManagerImpl reported initialization failure in | 3362 // task is to ensure that SyncManagerImpl reported initialization failure in |
3338 // OnInitializationComplete callback. | 3363 // OnInitializationComplete callback. |
3339 TEST_F(SyncManagerInitInvalidStorageTest, FailToOpenDatabase) { | 3364 TEST_F(SyncManagerInitInvalidStorageTest, FailToOpenDatabase) { |
3340 EXPECT_FALSE(initialization_succeeded_); | 3365 EXPECT_FALSE(initialization_succeeded_); |
3341 } | 3366 } |
3342 | 3367 |
3343 } // namespace syncer | 3368 } // namespace syncer |
OLD | NEW |