1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/prefs/tracked/tracked_preferences_migration.h"
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/prefs/testing_pref_service.h"
15 #include "base/strings/string_split.h"
16 #include "base/values.h"
17 #include "chrome/browser/prefs/profile_pref_store_manager.h"
18 #include "chrome/browser/prefs/tracked/dictionary_hash_store_contents.h"
19 #include "chrome/browser/prefs/tracked/hash_store_contents.h"
20 #include "chrome/browser/prefs/tracked/interceptable_pref_filter.h"
21 #include "chrome/browser/prefs/tracked/pref_hash_store.h"
22 #include "chrome/browser/prefs/tracked/pref_hash_store_impl.h"
23 #include "chrome/browser/prefs/tracked/pref_hash_store_transaction.h"
24 #include "chrome/browser/prefs/tracked/pref_service_hash_store_contents.h"
25 #include "chrome/browser/prefs/tracked/tracked_preferences_migration.h"
26 #include "testing/gtest/include/gtest/gtest.h"
30 // An unprotected pref.
31 const char kUnprotectedPref
[] = "unprotected";
33 const char kProtectedPref
[] = "protected";
34 // A protected pref which is initially stored in the unprotected store.
35 const char kPreviouslyUnprotectedPref
[] = "previously.unprotected";
36 // An unprotected pref which is initially stored in the protected store.
37 const char kPreviouslyProtectedPref
[] = "previously.protected";
39 const char kUnprotectedPrefValue
[] = "unprotected_value";
40 const char kProtectedPrefValue
[] = "protected_value";
41 const char kPreviouslyUnprotectedPrefValue
[] = "previously_unprotected_value";
42 const char kPreviouslyProtectedPrefValue
[] = "previously_protected_value";
44 // A simple InterceptablePrefFilter which doesn't do anything but hand the prefs
45 // back downstream in FinalizeFilterOnLoad.
46 class SimpleInterceptablePrefFilter
: public InterceptablePrefFilter
{
48 // PrefFilter remaining implementation.
49 void FilterUpdate(const std::string
& path
) override
{ ADD_FAILURE(); }
50 void FilterSerializeData(
51 base::DictionaryValue
* pref_store_contents
) override
{
56 // InterceptablePrefFilter implementation.
57 void FinalizeFilterOnLoad(
58 const PostFilterOnLoadCallback
& post_filter_on_load_callback
,
59 scoped_ptr
<base::DictionaryValue
> pref_store_contents
,
60 bool prefs_altered
) override
{
61 post_filter_on_load_callback
.Run(pref_store_contents
.Pass(), prefs_altered
);
65 // A test fixture designed to be used like this:
66 // 1) Set up initial store prefs with PresetStoreValue().
67 // 2) Hand both sets of prefs to the migrator via HandPrefsToMigrator().
68 // 3) Migration completes synchronously when the second store hands its prefs
70 // 4) Verifications can be made via various methods of this fixture.
71 // Call Reset() to perform a second migration.
72 class TrackedPreferencesMigrationTest
: public testing::Test
{
74 enum MockPrefStoreID
{
75 MOCK_UNPROTECTED_PREF_STORE
,
76 MOCK_PROTECTED_PREF_STORE
,
79 TrackedPreferencesMigrationTest()
80 : unprotected_prefs_(new base::DictionaryValue
),
81 protected_prefs_(new base::DictionaryValue
),
82 migration_modified_unprotected_store_(false),
83 migration_modified_protected_store_(false),
84 unprotected_store_migration_complete_(false),
85 protected_store_migration_complete_(false) {}
87 void SetUp() override
{
88 ProfilePrefStoreManager::RegisterPrefs(local_state_
.registry());
93 std::set
<std::string
> unprotected_pref_names
;
94 std::set
<std::string
> protected_pref_names
;
95 unprotected_pref_names
.insert(kUnprotectedPref
);
96 unprotected_pref_names
.insert(kPreviouslyProtectedPref
);
97 protected_pref_names
.insert(kProtectedPref
);
98 protected_pref_names
.insert(kPreviouslyUnprotectedPref
);
100 migration_modified_unprotected_store_
= false;
101 migration_modified_protected_store_
= false;
102 unprotected_store_migration_complete_
= false;
103 protected_store_migration_complete_
= false;
105 unprotected_store_successful_write_callback_
.Reset();
106 protected_store_successful_write_callback_
.Reset();
108 SetupTrackedPreferencesMigration(
109 unprotected_pref_names
,
110 protected_pref_names
,
111 base::Bind(&TrackedPreferencesMigrationTest::RemovePathFromStore
,
112 base::Unretained(this),
113 MOCK_UNPROTECTED_PREF_STORE
),
114 base::Bind(&TrackedPreferencesMigrationTest::RemovePathFromStore
,
115 base::Unretained(this),
116 MOCK_PROTECTED_PREF_STORE
),
118 &TrackedPreferencesMigrationTest::RegisterSuccessfulWriteClosure
,
119 base::Unretained(this),
120 MOCK_UNPROTECTED_PREF_STORE
),
122 &TrackedPreferencesMigrationTest::RegisterSuccessfulWriteClosure
,
123 base::Unretained(this),
124 MOCK_PROTECTED_PREF_STORE
),
125 scoped_ptr
<PrefHashStore
>(
126 new PrefHashStoreImpl(kSeed
, kDeviceId
, false)),
127 scoped_ptr
<PrefHashStore
>(
128 new PrefHashStoreImpl(kSeed
, kDeviceId
, true)),
129 scoped_ptr
<HashStoreContents
>(
130 new PrefServiceHashStoreContents(kHashStoreId
, &local_state_
)),
132 &mock_unprotected_pref_filter_
,
133 &mock_protected_pref_filter_
);
135 // Verify initial expectations are met.
136 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
137 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
139 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
141 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
145 // Sets |key| to |value| in the test store identified by |store_id| before
146 // migration begins. Also sets the corresponding hash in the same store.
147 void PresetStoreValue(MockPrefStoreID store_id
,
148 const std::string
& key
,
149 const std::string value
) {
150 PresetStoreValueOnly(store_id
, key
, value
);
151 PresetStoreValueHash(store_id
, key
, value
);
154 // Sets |key| to |value| in the test store identified by |store_id| before
155 // migration begins. Stores the value hash in Local State as in M36 and
157 void PresetLegacyStoreValue(MockPrefStoreID store_id
,
158 const std::string
& key
,
159 const std::string value
) {
160 PresetStoreValueOnly(store_id
, key
, value
);
161 PresetLegacyValueHash(key
, value
);
164 // Stores a hash for |key| and |value| in the hash store identified by
165 // |store_id| before migration begins.
166 void PresetStoreValueHash(MockPrefStoreID store_id
,
167 const std::string
& key
,
168 const std::string value
) {
169 base::DictionaryValue
* store
= NULL
;
170 scoped_ptr
<PrefHashStore
> pref_hash_store
;
172 case MOCK_UNPROTECTED_PREF_STORE
:
173 store
= unprotected_prefs_
.get();
174 pref_hash_store
.reset(new PrefHashStoreImpl(kSeed
, kDeviceId
, false));
176 case MOCK_PROTECTED_PREF_STORE
:
177 store
= protected_prefs_
.get();
178 pref_hash_store
.reset(new PrefHashStoreImpl(kSeed
, kDeviceId
, true));
183 base::StringValue
string_value(value
);
184 pref_hash_store
->BeginTransaction(
185 scoped_ptr
<HashStoreContents
>(
186 new DictionaryHashStoreContents(store
)))->StoreHash(
190 // Stores a hash for |key| and |value| in the legacy hash store in
192 void PresetLegacyValueHash(const std::string
& key
,
193 const std::string value
) {
194 scoped_ptr
<PrefHashStore
> pref_hash_store(
195 new PrefHashStoreImpl(kSeed
, kDeviceId
, true));
197 base::StringValue
string_value(value
);
198 PrefHashStoreImpl(kSeed
, kDeviceId
, true)
199 .BeginTransaction(scoped_ptr
<HashStoreContents
>(
200 new PrefServiceHashStoreContents(kHashStoreId
, &local_state_
)))
201 ->StoreHash(key
, &string_value
);
204 // Returns true if the store opposite to |store_id| is observed for its next
206 bool WasOnSuccessfulWriteCallbackRegistered(MockPrefStoreID store_id
) {
208 case MOCK_UNPROTECTED_PREF_STORE
:
209 return !protected_store_successful_write_callback_
.is_null();
210 case MOCK_PROTECTED_PREF_STORE
:
211 return !unprotected_store_successful_write_callback_
.is_null();
217 // Verifies that the (key, value) pairs in |expected_prefs_in_store| are found
218 // in the store identified by |store_id|.
219 void VerifyValuesStored(
220 MockPrefStoreID store_id
,
221 const base::StringPairs
& expected_prefs_in_store
) {
222 base::DictionaryValue
* store
= NULL
;
224 case MOCK_UNPROTECTED_PREF_STORE
:
225 store
= unprotected_prefs_
.get();
227 case MOCK_PROTECTED_PREF_STORE
:
228 store
= protected_prefs_
.get();
233 for (base::StringPairs::const_iterator it
= expected_prefs_in_store
.begin();
234 it
!= expected_prefs_in_store
.end(); ++it
) {
236 EXPECT_TRUE(store
->GetString(it
->first
, &val
));
237 EXPECT_EQ(it
->second
, val
);
241 // Determines whether |expected_pref_in_hash_store| has a hash in the hash
242 // store identified by |store_id|.
243 bool ContainsHash(MockPrefStoreID store_id
,
244 std::string expected_pref_in_hash_store
) {
245 base::DictionaryValue
* store
= NULL
;
247 case MOCK_UNPROTECTED_PREF_STORE
:
248 store
= unprotected_prefs_
.get();
250 case MOCK_PROTECTED_PREF_STORE
:
251 store
= protected_prefs_
.get();
255 const base::DictionaryValue
* hash_store_contents
=
256 DictionaryHashStoreContents(store
).GetContents();
257 return hash_store_contents
&&
258 hash_store_contents
->GetString(expected_pref_in_hash_store
,
259 static_cast<std::string
*>(NULL
));
262 // Determines whether |expected_pref_in_hash_store| has a hash in the Local
264 bool ContainsLegacyHash(std::string expected_pref_in_hash_store
) {
265 const base::DictionaryValue
* hash_store_contents
=
266 PrefServiceHashStoreContents(kHashStoreId
, &local_state_
).GetContents();
267 return hash_store_contents
&&
268 hash_store_contents
->GetString(expected_pref_in_hash_store
,
269 static_cast<std::string
*>(NULL
));
272 // Both stores need to hand their prefs over in order for migration to kick
274 void HandPrefsToMigrator(MockPrefStoreID store_id
) {
276 case MOCK_UNPROTECTED_PREF_STORE
:
277 mock_unprotected_pref_filter_
.FilterOnLoad(
278 base::Bind(&TrackedPreferencesMigrationTest::GetPrefsBack
,
279 base::Unretained(this),
280 MOCK_UNPROTECTED_PREF_STORE
),
281 unprotected_prefs_
.Pass());
283 case MOCK_PROTECTED_PREF_STORE
:
284 mock_protected_pref_filter_
.FilterOnLoad(
285 base::Bind(&TrackedPreferencesMigrationTest::GetPrefsBack
,
286 base::Unretained(this),
287 MOCK_PROTECTED_PREF_STORE
),
288 protected_prefs_
.Pass());
293 bool HasPrefs(MockPrefStoreID store_id
) {
295 case MOCK_UNPROTECTED_PREF_STORE
:
296 return unprotected_prefs_
;
297 case MOCK_PROTECTED_PREF_STORE
:
298 return protected_prefs_
;
304 bool StoreModifiedByMigration(MockPrefStoreID store_id
) {
306 case MOCK_UNPROTECTED_PREF_STORE
:
307 return migration_modified_unprotected_store_
;
308 case MOCK_PROTECTED_PREF_STORE
:
309 return migration_modified_protected_store_
;
315 bool MigrationCompleted() {
316 return unprotected_store_migration_complete_
&&
317 protected_store_migration_complete_
;
320 void SimulateSuccessfulWrite(MockPrefStoreID store_id
) {
322 case MOCK_UNPROTECTED_PREF_STORE
:
323 EXPECT_FALSE(unprotected_store_successful_write_callback_
.is_null());
324 unprotected_store_successful_write_callback_
.Run();
325 unprotected_store_successful_write_callback_
.Reset();
327 case MOCK_PROTECTED_PREF_STORE
:
328 EXPECT_FALSE(protected_store_successful_write_callback_
.is_null());
329 protected_store_successful_write_callback_
.Run();
330 protected_store_successful_write_callback_
.Reset();
336 void RegisterSuccessfulWriteClosure(
337 MockPrefStoreID store_id
,
338 const base::Closure
& successful_write_closure
) {
340 case MOCK_UNPROTECTED_PREF_STORE
:
341 EXPECT_TRUE(unprotected_store_successful_write_callback_
.is_null());
342 unprotected_store_successful_write_callback_
= successful_write_closure
;
344 case MOCK_PROTECTED_PREF_STORE
:
345 EXPECT_TRUE(protected_store_successful_write_callback_
.is_null());
346 protected_store_successful_write_callback_
= successful_write_closure
;
351 // Helper given as an InterceptablePrefFilter::FinalizeFilterOnLoadCallback
352 // to the migrator to be invoked when it's done.
353 void GetPrefsBack(MockPrefStoreID store_id
,
354 scoped_ptr
<base::DictionaryValue
> prefs
,
355 bool prefs_altered
) {
357 case MOCK_UNPROTECTED_PREF_STORE
:
358 EXPECT_FALSE(unprotected_prefs_
);
359 unprotected_prefs_
= prefs
.Pass();
360 migration_modified_unprotected_store_
= prefs_altered
;
361 unprotected_store_migration_complete_
= true;
363 case MOCK_PROTECTED_PREF_STORE
:
364 EXPECT_FALSE(protected_prefs_
);
365 protected_prefs_
= prefs
.Pass();
366 migration_modified_protected_store_
= prefs_altered
;
367 protected_store_migration_complete_
= true;
372 // Helper given as a cleaning callback to the migrator.
373 void RemovePathFromStore(MockPrefStoreID store_id
, const std::string
& key
) {
375 case MOCK_UNPROTECTED_PREF_STORE
:
376 ASSERT_TRUE(unprotected_prefs_
);
377 unprotected_prefs_
->RemovePath(key
, NULL
);
379 case MOCK_PROTECTED_PREF_STORE
:
380 ASSERT_TRUE(protected_prefs_
);
381 protected_prefs_
->RemovePath(key
, NULL
);
386 // Sets |key| to |value| in the test store identified by |store_id| before
387 // migration begins. Does not store a preference hash.
388 void PresetStoreValueOnly(MockPrefStoreID store_id
,
389 const std::string
& key
,
390 const std::string value
) {
391 base::DictionaryValue
* store
= NULL
;
393 case MOCK_UNPROTECTED_PREF_STORE
:
394 store
= unprotected_prefs_
.get();
396 case MOCK_PROTECTED_PREF_STORE
:
397 store
= protected_prefs_
.get();
402 store
->SetString(key
, value
);
405 static const char kHashStoreId
[];
406 static const char kSeed
[];
407 static const char kDeviceId
[];
409 scoped_ptr
<base::DictionaryValue
> unprotected_prefs_
;
410 scoped_ptr
<base::DictionaryValue
> protected_prefs_
;
412 SimpleInterceptablePrefFilter mock_unprotected_pref_filter_
;
413 SimpleInterceptablePrefFilter mock_protected_pref_filter_
;
415 base::Closure unprotected_store_successful_write_callback_
;
416 base::Closure protected_store_successful_write_callback_
;
418 bool migration_modified_unprotected_store_
;
419 bool migration_modified_protected_store_
;
421 bool unprotected_store_migration_complete_
;
422 bool protected_store_migration_complete_
;
424 TestingPrefServiceSimple local_state_
;
426 DISALLOW_COPY_AND_ASSIGN(TrackedPreferencesMigrationTest
);
430 const char TrackedPreferencesMigrationTest::kHashStoreId
[] = "hash-store-id";
433 const char TrackedPreferencesMigrationTest::kSeed
[] = "seed";
436 const char TrackedPreferencesMigrationTest::kDeviceId
[] = "device-id";
440 TEST_F(TrackedPreferencesMigrationTest
, NoMigrationRequired
) {
441 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
,
442 kUnprotectedPrefValue
);
443 PresetStoreValue(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
,
444 kProtectedPrefValue
);
446 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
447 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
449 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
450 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
452 // Hand unprotected prefs to the migrator which should wait for the protected
454 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
455 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
456 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
457 EXPECT_FALSE(MigrationCompleted());
459 // Hand protected prefs to the migrator which should proceed with the
460 // migration synchronously.
461 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
462 EXPECT_TRUE(MigrationCompleted());
464 // Prefs should have been handed back over.
465 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
466 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
468 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
470 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
471 EXPECT_FALSE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE
));
472 EXPECT_FALSE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE
));
474 base::StringPairs expected_unprotected_values
;
475 expected_unprotected_values
.push_back(
476 std::make_pair(kUnprotectedPref
, kUnprotectedPrefValue
));
477 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
, expected_unprotected_values
);
479 base::StringPairs expected_protected_values
;
480 expected_protected_values
.push_back(
481 std::make_pair(kProtectedPref
, kProtectedPrefValue
));
482 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
484 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
485 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
487 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
488 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
491 TEST_F(TrackedPreferencesMigrationTest
, LegacyHashMigrationOnly
) {
492 PresetLegacyStoreValue(
493 MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
, kUnprotectedPrefValue
);
494 PresetLegacyStoreValue(
495 MOCK_PROTECTED_PREF_STORE
, kProtectedPref
, kProtectedPrefValue
);
497 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
498 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
500 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
501 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
503 EXPECT_TRUE(ContainsLegacyHash(kProtectedPref
));
504 EXPECT_TRUE(ContainsLegacyHash(kUnprotectedPref
));
506 // Hand unprotected prefs to the migrator which should wait for the protected
508 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
509 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
510 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
511 EXPECT_FALSE(MigrationCompleted());
513 // Hand protected prefs to the migrator which should proceed with the
514 // migration synchronously.
515 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
516 EXPECT_TRUE(MigrationCompleted());
518 // Prefs should have been handed back over.
519 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
520 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
522 // There is no pending cleanup task for the modern hash stores.
524 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
526 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
528 // Both stores were modified as hashes were moved from Local State.
529 EXPECT_TRUE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE
));
530 EXPECT_TRUE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE
));
532 base::StringPairs expected_unprotected_values
;
533 expected_unprotected_values
.push_back(
534 std::make_pair(kUnprotectedPref
, kUnprotectedPrefValue
));
535 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
, expected_unprotected_values
);
537 base::StringPairs expected_protected_values
;
538 expected_protected_values
.push_back(
539 std::make_pair(kProtectedPref
, kProtectedPrefValue
));
540 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
542 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
543 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
545 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
546 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
548 // The Local State hash store will not be reset until the next run.
549 EXPECT_TRUE(ContainsLegacyHash(kProtectedPref
));
550 EXPECT_TRUE(ContainsLegacyHash(kUnprotectedPref
));
554 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
555 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
556 EXPECT_TRUE(MigrationCompleted());
558 // Neither store was modified.
559 EXPECT_FALSE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE
));
560 EXPECT_FALSE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE
));
562 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
563 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
565 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
566 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
568 EXPECT_FALSE(ContainsLegacyHash(kProtectedPref
));
569 EXPECT_FALSE(ContainsLegacyHash(kUnprotectedPref
));
572 TEST_F(TrackedPreferencesMigrationTest
, FullMigrationWithLegacyHashStore
) {
573 // Store some values with matching MACs in Local State.
574 PresetLegacyStoreValue(
575 MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
, kUnprotectedPrefValue
);
576 PresetLegacyStoreValue(MOCK_UNPROTECTED_PREF_STORE
,
577 kPreviouslyUnprotectedPref
,
578 kPreviouslyUnprotectedPrefValue
);
579 PresetLegacyStoreValue(
580 MOCK_PROTECTED_PREF_STORE
, kProtectedPref
, kProtectedPrefValue
);
581 PresetLegacyStoreValue(MOCK_PROTECTED_PREF_STORE
,
582 kPreviouslyProtectedPref
,
583 kPreviouslyProtectedPrefValue
);
585 // Verify that there are no MACs in Preferences or Secure Preferences.
586 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
588 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
589 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
591 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
593 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
595 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
596 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
598 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
600 // Verify that there are MACs in Local State.
601 EXPECT_TRUE(ContainsLegacyHash(kUnprotectedPref
));
602 EXPECT_TRUE(ContainsLegacyHash(kPreviouslyUnprotectedPref
));
603 EXPECT_TRUE(ContainsLegacyHash(kProtectedPref
));
604 EXPECT_TRUE(ContainsLegacyHash(kPreviouslyProtectedPref
));
606 // Perform a first-pass migration.
607 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
608 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
609 EXPECT_TRUE(MigrationCompleted());
611 // All values should have been moved to their preferred locations, including
613 base::StringPairs expected_unprotected_values
;
614 expected_unprotected_values
.push_back(
615 std::make_pair(kUnprotectedPref
, kUnprotectedPrefValue
));
616 expected_unprotected_values
.push_back(
617 std::make_pair(kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
618 base::StringPairs expected_protected_values
;
619 expected_protected_values
.push_back(
620 std::make_pair(kProtectedPref
, kProtectedPrefValue
));
621 expected_protected_values
.push_back(std::make_pair(
622 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
624 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
, expected_unprotected_values
);
625 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
627 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
629 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
631 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
632 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
634 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
635 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
638 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
640 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
642 // Removing the values from their previous locations is deferred until the new
643 // locations are persisted.
644 EXPECT_TRUE(ContainsLegacyHash(kUnprotectedPref
));
645 EXPECT_TRUE(ContainsLegacyHash(kPreviouslyUnprotectedPref
));
646 EXPECT_TRUE(ContainsLegacyHash(kProtectedPref
));
647 EXPECT_TRUE(ContainsLegacyHash(kPreviouslyProtectedPref
));
650 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
652 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
654 SimulateSuccessfulWrite(MOCK_UNPROTECTED_PREF_STORE
);
655 SimulateSuccessfulWrite(MOCK_PROTECTED_PREF_STORE
);
659 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
660 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
661 EXPECT_TRUE(MigrationCompleted());
663 // In this run the MACs should have been removed from their previous
664 // locations. There is no more pending action.
666 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
668 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
670 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
672 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
673 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
675 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
677 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
679 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
680 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
682 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
684 EXPECT_FALSE(ContainsLegacyHash(kUnprotectedPref
));
685 EXPECT_FALSE(ContainsLegacyHash(kPreviouslyUnprotectedPref
));
686 EXPECT_FALSE(ContainsLegacyHash(kProtectedPref
));
687 EXPECT_FALSE(ContainsLegacyHash(kPreviouslyProtectedPref
));
689 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
, expected_unprotected_values
);
690 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
693 TEST_F(TrackedPreferencesMigrationTest
, FullMigration
) {
695 MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
, kUnprotectedPrefValue
);
696 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE
,
697 kPreviouslyUnprotectedPref
,
698 kPreviouslyUnprotectedPrefValue
);
700 MOCK_PROTECTED_PREF_STORE
, kProtectedPref
, kProtectedPrefValue
);
701 PresetStoreValue(MOCK_PROTECTED_PREF_STORE
,
702 kPreviouslyProtectedPref
,
703 kPreviouslyProtectedPrefValue
);
705 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
707 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
708 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
710 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
712 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
714 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
715 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
717 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
719 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
720 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
721 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
722 EXPECT_FALSE(MigrationCompleted());
724 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
725 EXPECT_TRUE(MigrationCompleted());
727 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
728 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
730 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
732 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
733 EXPECT_TRUE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE
));
734 EXPECT_TRUE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE
));
736 // Values should have been migrated to their store, but migrated values should
737 // still remain in the source store until cleanup tasks are later invoked.
739 base::StringPairs expected_unprotected_values
;
740 expected_unprotected_values
.push_back(std::make_pair(
741 kUnprotectedPref
, kUnprotectedPrefValue
));
742 expected_unprotected_values
.push_back(std::make_pair(
743 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
744 expected_unprotected_values
.push_back(std::make_pair(
745 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
746 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
,
747 expected_unprotected_values
);
749 base::StringPairs expected_protected_values
;
750 expected_protected_values
.push_back(std::make_pair(
751 kProtectedPref
, kProtectedPrefValue
));
752 expected_protected_values
.push_back(std::make_pair(
753 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
754 expected_unprotected_values
.push_back(std::make_pair(
755 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
756 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
758 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
760 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
761 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
763 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
765 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
767 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
768 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
770 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
773 // A successful write of the protected pref store should result in a clean up
774 // of the unprotected store.
775 SimulateSuccessfulWrite(MOCK_PROTECTED_PREF_STORE
);
778 base::StringPairs expected_unprotected_values
;
779 expected_unprotected_values
.push_back(std::make_pair(
780 kUnprotectedPref
, kUnprotectedPrefValue
));
781 expected_unprotected_values
.push_back(std::make_pair(
782 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
783 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
,
784 expected_unprotected_values
);
786 base::StringPairs expected_protected_values
;
787 expected_protected_values
.push_back(std::make_pair(
788 kProtectedPref
, kProtectedPrefValue
));
789 expected_protected_values
.push_back(std::make_pair(
790 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
791 expected_unprotected_values
.push_back(std::make_pair(
792 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
793 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
796 SimulateSuccessfulWrite(MOCK_UNPROTECTED_PREF_STORE
);
799 base::StringPairs expected_unprotected_values
;
800 expected_unprotected_values
.push_back(std::make_pair(
801 kUnprotectedPref
, kUnprotectedPrefValue
));
802 expected_unprotected_values
.push_back(std::make_pair(
803 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
804 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
,
805 expected_unprotected_values
);
807 base::StringPairs expected_protected_values
;
808 expected_protected_values
.push_back(std::make_pair(
809 kProtectedPref
, kProtectedPrefValue
));
810 expected_protected_values
.push_back(std::make_pair(
811 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
812 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
815 // Hashes are not cleaned up yet.
816 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
818 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
819 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
821 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
823 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
825 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
826 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
828 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
832 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
833 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
834 EXPECT_TRUE(MigrationCompleted());
836 // Hashes are cleaned up.
837 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
839 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
840 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
842 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
844 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
846 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
847 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
849 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
852 TEST_F(TrackedPreferencesMigrationTest
, CleanupOnly
) {
853 // Already migrated; only cleanup needed.
855 MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
, kUnprotectedPrefValue
);
856 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE
,
857 kPreviouslyProtectedPref
,
858 kPreviouslyProtectedPrefValue
);
859 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE
,
860 kPreviouslyUnprotectedPref
,
861 kPreviouslyUnprotectedPrefValue
);
863 MOCK_PROTECTED_PREF_STORE
, kProtectedPref
, kProtectedPrefValue
);
864 PresetStoreValue(MOCK_PROTECTED_PREF_STORE
,
865 kPreviouslyProtectedPref
,
866 kPreviouslyProtectedPrefValue
);
867 PresetStoreValue(MOCK_PROTECTED_PREF_STORE
,
868 kPreviouslyUnprotectedPref
,
869 kPreviouslyUnprotectedPrefValue
);
871 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
872 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
873 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
874 EXPECT_FALSE(MigrationCompleted());
876 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
877 EXPECT_TRUE(MigrationCompleted());
879 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
880 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
882 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
884 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
885 EXPECT_FALSE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE
));
886 EXPECT_FALSE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE
));
888 // Cleanup should happen synchronously if the values were already present in
889 // their destination stores.
891 base::StringPairs expected_unprotected_values
;
892 expected_unprotected_values
.push_back(std::make_pair(
893 kUnprotectedPref
, kUnprotectedPrefValue
));
894 expected_unprotected_values
.push_back(std::make_pair(
895 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
896 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
,
897 expected_unprotected_values
);
899 base::StringPairs expected_protected_values
;
900 expected_protected_values
.push_back(std::make_pair(
901 kProtectedPref
, kProtectedPrefValue
));
902 expected_protected_values
.push_back(std::make_pair(
903 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
904 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);