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

Side by Side Diff: components/sync/core_impl/sync_manager_impl_unittest.cc

Issue 2294913009: Fill the un-encrypted metadata field in password specifics. (Closed)
Patch Set: check Created 4 years, 3 months 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
OLDNEW
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
11 #include <stdint.h> 11 #include <stdint.h>
12 12
13 #include <cstddef> 13 #include <cstddef>
14 #include <map> 14 #include <map>
15 #include <memory> 15 #include <memory>
16 #include <utility> 16 #include <utility>
17 17
18 #include "base/callback.h" 18 #include "base/callback.h"
19 #include "base/compiler_specific.h" 19 #include "base/compiler_specific.h"
20 #include "base/files/scoped_temp_dir.h" 20 #include "base/files/scoped_temp_dir.h"
21 #include "base/format_macros.h" 21 #include "base/format_macros.h"
22 #include "base/location.h" 22 #include "base/location.h"
23 #include "base/metrics/field_trial.h"
23 #include "base/run_loop.h" 24 #include "base/run_loop.h"
24 #include "base/strings/string_number_conversions.h" 25 #include "base/strings/string_number_conversions.h"
25 #include "base/strings/stringprintf.h" 26 #include "base/strings/stringprintf.h"
26 #include "base/strings/utf_string_conversions.h" 27 #include "base/strings/utf_string_conversions.h"
27 #include "base/test/values_test_util.h" 28 #include "base/test/values_test_util.h"
28 #include "base/values.h" 29 #include "base/values.h"
29 #include "components/sync/base/attachment_id_proto.h" 30 #include "components/sync/base/attachment_id_proto.h"
30 #include "components/sync/base/cancelation_signal.h" 31 #include "components/sync/base/cancelation_signal.h"
31 #include "components/sync/base/cryptographer.h" 32 #include "components/sync/base/cryptographer.h"
32 #include "components/sync/base/extensions_activity.h" 33 #include "components/sync/base/extensions_activity.h"
33 #include "components/sync/base/fake_encryptor.h" 34 #include "components/sync/base/fake_encryptor.h"
34 #include "components/sync/base/mock_unrecoverable_error_handler.h" 35 #include "components/sync/base/mock_unrecoverable_error_handler.h"
35 #include "components/sync/base/model_type_test_util.h" 36 #include "components/sync/base/model_type_test_util.h"
36 #include "components/sync/base/time.h" 37 #include "components/sync/base/time.h"
37 #include "components/sync/core/change_record.h" 38 #include "components/sync/core/change_record.h"
38 #include "components/sync/core/http_post_provider_factory.h" 39 #include "components/sync/core/http_post_provider_factory.h"
39 #include "components/sync/core/http_post_provider_interface.h" 40 #include "components/sync/core/http_post_provider_interface.h"
41 #include "components/sync/core/password_metadata_filling.h"
40 #include "components/sync/core/read_node.h" 42 #include "components/sync/core/read_node.h"
41 #include "components/sync/core/read_transaction.h" 43 #include "components/sync/core/read_transaction.h"
42 #include "components/sync/core/test/test_entry_factory.h" 44 #include "components/sync/core/test/test_entry_factory.h"
43 #include "components/sync/core/test/test_internal_components_factory.h" 45 #include "components/sync/core/test/test_internal_components_factory.h"
44 #include "components/sync/core/test/test_user_share.h" 46 #include "components/sync/core/test/test_user_share.h"
45 #include "components/sync/core/write_node.h" 47 #include "components/sync/core/write_node.h"
46 #include "components/sync/core/write_transaction.h" 48 #include "components/sync/core/write_transaction.h"
47 #include "components/sync/core_impl/sync_encryption_handler_impl.h" 49 #include "components/sync/core_impl/sync_encryption_handler_impl.h"
48 #include "components/sync/core_impl/syncapi_internal.h" 50 #include "components/sync/core_impl/syncapi_internal.h"
49 #include "components/sync/engine/events/protocol_event.h" 51 #include "components/sync/engine/events/protocol_event.h"
(...skipping 453 matching lines...) Expand 10 before | Expand all | Expand 10 after
503 ReadNode node(&trans); 505 ReadNode node(&trans);
504 EXPECT_EQ(BaseNode::INIT_OK, 506 EXPECT_EQ(BaseNode::INIT_OK,
505 node.InitByClientTagLookup(BOOKMARKS, "testtag")); 507 node.InitByClientTagLookup(BOOKMARKS, "testtag"));
506 EXPECT_EQ(node.GetTitle(), test_title); 508 EXPECT_EQ(node.GetTitle(), test_title);
507 EXPECT_EQ(node.GetModelType(), BOOKMARKS); 509 EXPECT_EQ(node.GetModelType(), BOOKMARKS);
508 } 510 }
509 } 511 }
510 512
511 TEST_F(SyncApiTest, WriteAndReadPassword) { 513 TEST_F(SyncApiTest, WriteAndReadPassword) {
512 KeyParams params = {"localhost", "username", "passphrase"}; 514 KeyParams params = {"localhost", "username", "passphrase"};
515 base::FeatureList::ClearInstanceForTesting();
516 EXPECT_FALSE(base::FeatureList::IsEnabled(kPasswordsMetadataFilling));
517
513 { 518 {
514 ReadTransaction trans(FROM_HERE, user_share()); 519 ReadTransaction trans(FROM_HERE, user_share());
515 trans.GetCryptographer()->AddKey(params); 520 trans.GetCryptographer()->AddKey(params);
516 } 521 }
517 { 522 {
518 WriteTransaction trans(FROM_HERE, user_share()); 523 WriteTransaction trans(FROM_HERE, user_share());
519 ReadNode root_node(&trans); 524 ReadNode root_node(&trans);
520 root_node.InitByRootLookup(); 525 root_node.InitByRootLookup();
521 526
522 WriteNode password_node(&trans); 527 WriteNode password_node(&trans);
523 WriteNode::InitUniqueByCreationResult result = 528 WriteNode::InitUniqueByCreationResult result =
524 password_node.InitUniqueByCreation(PASSWORDS, root_node, kClientTag); 529 password_node.InitUniqueByCreation(PASSWORDS, root_node, kClientTag);
525 EXPECT_EQ(WriteNode::INIT_SUCCESS, result); 530 EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
526 sync_pb::PasswordSpecificsData data; 531 sync_pb::PasswordSpecificsData data;
527 data.set_password_value(kPasswordValue); 532 data.set_password_value(kPasswordValue);
528 password_node.SetPasswordSpecifics(data); 533 password_node.SetPasswordSpecifics(data);
529 } 534 }
530 { 535 {
531 ReadTransaction trans(FROM_HERE, user_share()); 536 ReadTransaction trans(FROM_HERE, user_share());
532 537
533 ReadNode password_node(&trans); 538 ReadNode password_node(&trans);
534 EXPECT_EQ(BaseNode::INIT_OK, 539 EXPECT_EQ(BaseNode::INIT_OK,
535 password_node.InitByClientTagLookup(PASSWORDS, kClientTag)); 540 password_node.InitByClientTagLookup(PASSWORDS, kClientTag));
536 const sync_pb::PasswordSpecificsData& data = 541 const sync_pb::PasswordSpecificsData& data =
537 password_node.GetPasswordSpecifics(); 542 password_node.GetPasswordSpecifics();
538 EXPECT_EQ(kPasswordValue, data.password_value()); 543 EXPECT_EQ(kPasswordValue, data.password_value());
539 // Check that nothing has appeared in the unencrypted field. 544 // Check that when feature is disabled nothing appears in the unencrypted
545 // field.
540 EXPECT_FALSE(password_node.GetEntitySpecifics() 546 EXPECT_FALSE(password_node.GetEntitySpecifics()
541 .password() 547 .password()
542 .has_unencrypted_metadata()); 548 .has_unencrypted_metadata());
543 } 549 }
544 } 550 }
545 551
552 TEST_F(SyncApiTest, WritePasswordAndCheckMetadata) {
553 KeyParams params = {"localhost", "username", "passphrase"};
554 {
555 ReadTransaction trans(FROM_HERE, user_share());
556 trans.GetCryptographer()->AddKey(params);
557 }
558
559 base::FeatureList::ClearInstanceForTesting();
560 base::FieldTrialList field_trial_list(nullptr);
561 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
562 feature_list->InitializeFromCommandLine(kPasswordsMetadataFilling.name,
563 std::string());
564 base::FeatureList::SetInstance(std::move(feature_list));
565
566 EXPECT_TRUE(base::FeatureList::IsEnabled(kPasswordsMetadataFilling));
567 {
568 WriteTransaction trans(FROM_HERE, user_share());
569 ReadNode root_node(&trans);
570 root_node.InitByRootLookup();
571
572 WriteNode password_node(&trans);
573 WriteNode::InitUniqueByCreationResult result =
574 password_node.InitUniqueByCreation(PASSWORDS, root_node, kClientTag);
575 EXPECT_EQ(WriteNode::INIT_SUCCESS, result);
576 sync_pb::PasswordSpecificsData data;
577 data.set_password_value(kPasswordValue);
578 data.set_signon_realm(kUrl);
579 password_node.SetPasswordSpecifics(data);
580 }
581 {
582 ReadTransaction trans(FROM_HERE, user_share());
583
584 ReadNode password_node(&trans);
585 EXPECT_EQ(BaseNode::INIT_OK,
586 password_node.InitByClientTagLookup(PASSWORDS, kClientTag));
587 const sync_pb::PasswordSpecificsData& data =
588 password_node.GetPasswordSpecifics();
589 EXPECT_EQ(kPasswordValue, data.password_value());
590 EXPECT_EQ(kUrl, password_node.GetEntitySpecifics()
591 .password()
592 .unencrypted_metadata()
593 .url());
594 }
595 }
596
546 TEST_F(SyncApiTest, WriteEncryptedTitle) { 597 TEST_F(SyncApiTest, WriteEncryptedTitle) {
547 KeyParams params = {"localhost", "username", "passphrase"}; 598 KeyParams params = {"localhost", "username", "passphrase"};
548 { 599 {
549 ReadTransaction trans(FROM_HERE, user_share()); 600 ReadTransaction trans(FROM_HERE, user_share());
550 trans.GetCryptographer()->AddKey(params); 601 trans.GetCryptographer()->AddKey(params);
551 } 602 }
552 encryption_handler()->EnableEncryptEverything(); 603 encryption_handler()->EnableEncryptEverything();
553 int bookmark_id; 604 int bookmark_id;
554 { 605 {
555 WriteTransaction trans(FROM_HERE, user_share()); 606 WriteTransaction trans(FROM_HERE, user_share());
(...skipping 1536 matching lines...) Expand 10 before | Expand all | Expand 10 after
2092 EXPECT_FALSE(password_node.GetEntitySpecifics() 2143 EXPECT_FALSE(password_node.GetEntitySpecifics()
2093 .password() 2144 .password()
2094 .has_unencrypted_metadata()); 2145 .has_unencrypted_metadata());
2095 } 2146 }
2096 EXPECT_TRUE(ResetUnsyncedEntry(PASSWORDS, kClientTag)); 2147 EXPECT_TRUE(ResetUnsyncedEntry(PASSWORDS, kClientTag));
2097 } 2148 }
2098 2149
2099 // Passwords have their own handling for encryption. Verify it does not result 2150 // Passwords have their own handling for encryption. Verify it does not result
2100 // in unnecessary writes via ReencryptEverything. 2151 // in unnecessary writes via ReencryptEverything.
2101 TEST_F(SyncManagerTest, UpdatePasswordReencryptEverything) { 2152 TEST_F(SyncManagerTest, UpdatePasswordReencryptEverything) {
2102 std::string client_tag = "title"; 2153 base::FeatureList::ClearInstanceForTesting();
2154 EXPECT_FALSE(base::FeatureList::IsEnabled(kPasswordsMetadataFilling));
2155
2103 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); 2156 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
2104 sync_pb::EntitySpecifics entity_specifics; 2157 sync_pb::EntitySpecifics entity_specifics;
2105 { 2158 {
2106 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare()); 2159 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2107 Cryptographer* cryptographer = trans.GetCryptographer(); 2160 Cryptographer* cryptographer = trans.GetCryptographer();
2108 sync_pb::PasswordSpecificsData data; 2161 sync_pb::PasswordSpecificsData data;
2109 data.set_password_value("secret"); 2162 data.set_password_value("secret");
2110 cryptographer->Encrypt( 2163 cryptographer->Encrypt(
2111 data, entity_specifics.mutable_password()->mutable_encrypted()); 2164 data, entity_specifics.mutable_password()->mutable_encrypted());
2112 } 2165 }
2113 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, client_tag, 2166 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, kClientTag,
2114 syncable::GenerateSyncableHash(PASSWORDS, client_tag), 2167 syncable::GenerateSyncableHash(PASSWORDS, kClientTag),
2115 entity_specifics); 2168 entity_specifics);
2116 // New node shouldn't start off unsynced. 2169 // New node shouldn't start off unsynced.
2117 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag)); 2170 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, kClientTag));
2118 2171
2119 // Force a re-encrypt everything. Should not set is_unsynced. 2172 // Force a re-encrypt everything. Should not set is_unsynced.
2120 testing::Mock::VerifyAndClearExpectations(&encryption_observer_); 2173 testing::Mock::VerifyAndClearExpectations(&encryption_observer_);
2121 EXPECT_CALL(encryption_observer_, OnEncryptionComplete()); 2174 EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
2122 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_)); 2175 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
2123 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false)); 2176 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false));
2124 sync_manager_.GetEncryptionHandler()->Init(); 2177 sync_manager_.GetEncryptionHandler()->Init();
2125 PumpLoop(); 2178 PumpLoop();
2126 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, client_tag)); 2179 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, kClientTag));
2180 }
2181
2182 // Metadata filling can happen during ReencryptEverything, check that data is
2183 // written when it's applicable, namely that password specifics entity is marked
2184 // unsynced, when data was written to the unencrypted metadata field.
2185 TEST_F(SyncManagerTest, UpdatePasswordReencryptEverythingFillMetadata) {
2186 base::FeatureList::ClearInstanceForTesting();
2187 base::FieldTrialList field_trial_list(nullptr);
2188 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
2189 feature_list->InitializeFromCommandLine(kPasswordsMetadataFilling.name,
2190 std::string());
2191 base::FeatureList::SetInstance(std::move(feature_list));
2192 EXPECT_TRUE(base::FeatureList::IsEnabled(kPasswordsMetadataFilling));
2193
2194 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
2195 sync_pb::EntitySpecifics entity_specifics;
2196 {
2197 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2198 Cryptographer* cryptographer = trans.GetCryptographer();
2199 sync_pb::PasswordSpecificsData data;
2200 data.set_password_value("secret");
2201 data.set_signon_realm(kUrl);
2202 cryptographer->Encrypt(
2203 data, entity_specifics.mutable_password()->mutable_encrypted());
2204 }
2205 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, kClientTag,
2206 syncable::GenerateSyncableHash(PASSWORDS, kClientTag),
2207 entity_specifics);
2208 // New node shouldn't start off unsynced.
2209 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, kClientTag));
2210
2211 // Force a re-encrypt everything. Should set is_unsynced.
2212 testing::Mock::VerifyAndClearExpectations(&encryption_observer_);
2213 EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
2214 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
2215 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false));
2216 sync_manager_.GetEncryptionHandler()->Init();
2217 PumpLoop();
2218 // Check that unencrypted metadata field was set.
2219 {
2220 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2221 Cryptographer* cryptographer = trans.GetCryptographer();
2222 EXPECT_TRUE(cryptographer->is_ready());
2223 ReadNode password_node(&trans);
2224 EXPECT_EQ(BaseNode::INIT_OK,
2225 password_node.InitByClientTagLookup(PASSWORDS, kClientTag));
2226 EXPECT_EQ(kUrl, password_node.GetEntitySpecifics()
2227 .password()
2228 .unencrypted_metadata()
2229 .url());
2230 }
2231
2232 EXPECT_TRUE(ResetUnsyncedEntry(PASSWORDS, kClientTag));
2233 }
2234
2235 TEST_F(SyncManagerTest,
Nicolas Zea 2016/09/23 23:10:37 nit: To be consistent, would be good to add a comm
melandory 2016/10/04 10:13:12 Done.
2236 UpdatePasswordReencryptEverythingDontMarkUnsyncWhenNotNeeded) {
2237 base::FeatureList::ClearInstanceForTesting();
2238 base::FieldTrialList field_trial_list(nullptr);
2239 std::unique_ptr<base::FeatureList> feature_list(new base::FeatureList);
2240 feature_list->InitializeFromCommandLine(kPasswordsMetadataFilling.name,
2241 std::string());
2242 base::FeatureList::SetInstance(std::move(feature_list));
2243 EXPECT_TRUE(base::FeatureList::IsEnabled(kPasswordsMetadataFilling));
2244
2245 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
2246 sync_pb::EntitySpecifics entity_specifics;
2247 {
2248 ReadTransaction trans(FROM_HERE, sync_manager_.GetUserShare());
2249 Cryptographer* cryptographer = trans.GetCryptographer();
2250 sync_pb::PasswordSpecificsData data;
2251 data.set_password_value("secret");
2252 data.set_signon_realm(kUrl);
2253 cryptographer->Encrypt(
2254 data, entity_specifics.mutable_password()->mutable_encrypted());
2255 }
2256 entity_specifics.mutable_password()->mutable_unencrypted_metadata()->set_url(
2257 kUrl);
2258 MakeServerNode(sync_manager_.GetUserShare(), PASSWORDS, kClientTag,
2259 syncable::GenerateSyncableHash(PASSWORDS, kClientTag),
2260 entity_specifics);
2261 // New node shouldn't start off unsynced.
2262 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, kClientTag));
2263
2264 // Force a re-encrypt everything. Should not set is_unsynced.
2265 testing::Mock::VerifyAndClearExpectations(&encryption_observer_);
2266 EXPECT_CALL(encryption_observer_, OnEncryptionComplete());
2267 EXPECT_CALL(encryption_observer_, OnCryptographerStateChanged(_));
2268 EXPECT_CALL(encryption_observer_, OnEncryptedTypesChanged(_, false));
2269 sync_manager_.GetEncryptionHandler()->Init();
2270 PumpLoop();
2271 EXPECT_FALSE(ResetUnsyncedEntry(PASSWORDS, kClientTag));
2127 } 2272 }
2128 2273
2129 // Test that attempting to start up with corrupted password data triggers 2274 // Test that attempting to start up with corrupted password data triggers
2130 // an unrecoverable error (rather than crashing). 2275 // an unrecoverable error (rather than crashing).
2131 TEST_F(SyncManagerTest, ReencryptEverythingWithUnrecoverableErrorPasswords) { 2276 TEST_F(SyncManagerTest, ReencryptEverythingWithUnrecoverableErrorPasswords) {
2132 const char kClientTag[] = "client_tag"; 2277 const char kClientTag[] = "client_tag";
2133 2278
2134 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION)); 2279 EXPECT_TRUE(SetUpEncryption(WRITE_TO_NIGORI, DEFAULT_ENCRYPTION));
2135 sync_pb::EntitySpecifics entity_specifics; 2280 sync_pb::EntitySpecifics entity_specifics;
2136 { 2281 {
(...skipping 1222 matching lines...) Expand 10 before | Expand all | Expand 10 after
3359 // SyncManagerInitInvalidStorageTest::GetFactory will return 3504 // SyncManagerInitInvalidStorageTest::GetFactory will return
3360 // DirectoryBackingStore that ensures that SyncManagerImpl::OpenDirectory fails. 3505 // DirectoryBackingStore that ensures that SyncManagerImpl::OpenDirectory fails.
3361 // SyncManagerImpl initialization is done in SyncManagerTest::SetUp. This test's 3506 // SyncManagerImpl initialization is done in SyncManagerTest::SetUp. This test's
3362 // task is to ensure that SyncManagerImpl reported initialization failure in 3507 // task is to ensure that SyncManagerImpl reported initialization failure in
3363 // OnInitializationComplete callback. 3508 // OnInitializationComplete callback.
3364 TEST_F(SyncManagerInitInvalidStorageTest, FailToOpenDatabase) { 3509 TEST_F(SyncManagerInitInvalidStorageTest, FailToOpenDatabase) {
3365 EXPECT_FALSE(initialization_succeeded_); 3510 EXPECT_FALSE(initialization_succeeded_);
3366 } 3511 }
3367 3512
3368 } // namespace syncer 3513 } // namespace syncer
OLDNEW
« components/sync/core/write_node.cc ('K') | « components/sync/core/write_node.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698