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 "components/user_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 "components/user_prefs/tracked/dictionary_hash_store_contents.h"
18 #include "components/user_prefs/tracked/hash_store_contents.h"
19 #include "components/user_prefs/tracked/interceptable_pref_filter.h"
20 #include "components/user_prefs/tracked/pref_hash_store.h"
21 #include "components/user_prefs/tracked/pref_hash_store_impl.h"
22 #include "components/user_prefs/tracked/pref_hash_store_transaction.h"
23 #include "components/user_prefs/tracked/pref_service_hash_store_contents.h"
24 #include "components/user_prefs/tracked/tracked_preferences_migration.h"
25 #include "testing/gtest/include/gtest/gtest.h"
29 // An unprotected pref.
30 const char kUnprotectedPref
[] = "unprotected";
32 const char kProtectedPref
[] = "protected";
33 // A protected pref which is initially stored in the unprotected store.
34 const char kPreviouslyUnprotectedPref
[] = "previously.unprotected";
35 // An unprotected pref which is initially stored in the protected store.
36 const char kPreviouslyProtectedPref
[] = "previously.protected";
38 const char kUnprotectedPrefValue
[] = "unprotected_value";
39 const char kProtectedPrefValue
[] = "protected_value";
40 const char kPreviouslyUnprotectedPrefValue
[] = "previously_unprotected_value";
41 const char kPreviouslyProtectedPrefValue
[] = "previously_protected_value";
43 // A simple InterceptablePrefFilter which doesn't do anything but hand the prefs
44 // back downstream in FinalizeFilterOnLoad.
45 class SimpleInterceptablePrefFilter
: public InterceptablePrefFilter
{
47 // PrefFilter remaining implementation.
48 void FilterUpdate(const std::string
& path
) override
{ ADD_FAILURE(); }
49 void FilterSerializeData(
50 base::DictionaryValue
* pref_store_contents
) override
{
55 // InterceptablePrefFilter implementation.
56 void FinalizeFilterOnLoad(
57 const PostFilterOnLoadCallback
& post_filter_on_load_callback
,
58 scoped_ptr
<base::DictionaryValue
> pref_store_contents
,
59 bool prefs_altered
) override
{
60 post_filter_on_load_callback
.Run(pref_store_contents
.Pass(), prefs_altered
);
64 // A test fixture designed to be used like this:
65 // 1) Set up initial store prefs with PresetStoreValue().
66 // 2) Hand both sets of prefs to the migrator via HandPrefsToMigrator().
67 // 3) Migration completes synchronously when the second store hands its prefs
69 // 4) Verifications can be made via various methods of this fixture.
70 // Call Reset() to perform a second migration.
71 class TrackedPreferencesMigrationTest
: public testing::Test
{
73 enum MockPrefStoreID
{
74 MOCK_UNPROTECTED_PREF_STORE
,
75 MOCK_PROTECTED_PREF_STORE
,
78 TrackedPreferencesMigrationTest()
79 : unprotected_prefs_(new base::DictionaryValue
),
80 protected_prefs_(new base::DictionaryValue
),
81 migration_modified_unprotected_store_(false),
82 migration_modified_protected_store_(false),
83 unprotected_store_migration_complete_(false),
84 protected_store_migration_complete_(false) {}
86 void SetUp() override
{
87 PrefServiceHashStoreContents::RegisterPrefs(local_state_
.registry());
92 std::set
<std::string
> unprotected_pref_names
;
93 std::set
<std::string
> protected_pref_names
;
94 unprotected_pref_names
.insert(kUnprotectedPref
);
95 unprotected_pref_names
.insert(kPreviouslyProtectedPref
);
96 protected_pref_names
.insert(kProtectedPref
);
97 protected_pref_names
.insert(kPreviouslyUnprotectedPref
);
99 migration_modified_unprotected_store_
= false;
100 migration_modified_protected_store_
= false;
101 unprotected_store_migration_complete_
= false;
102 protected_store_migration_complete_
= false;
104 unprotected_store_successful_write_callback_
.Reset();
105 protected_store_successful_write_callback_
.Reset();
107 SetupTrackedPreferencesMigration(
108 unprotected_pref_names
,
109 protected_pref_names
,
110 base::Bind(&TrackedPreferencesMigrationTest::RemovePathFromStore
,
111 base::Unretained(this),
112 MOCK_UNPROTECTED_PREF_STORE
),
113 base::Bind(&TrackedPreferencesMigrationTest::RemovePathFromStore
,
114 base::Unretained(this),
115 MOCK_PROTECTED_PREF_STORE
),
117 &TrackedPreferencesMigrationTest::RegisterSuccessfulWriteClosure
,
118 base::Unretained(this),
119 MOCK_UNPROTECTED_PREF_STORE
),
121 &TrackedPreferencesMigrationTest::RegisterSuccessfulWriteClosure
,
122 base::Unretained(this),
123 MOCK_PROTECTED_PREF_STORE
),
124 scoped_ptr
<PrefHashStore
>(
125 new PrefHashStoreImpl(kSeed
, kDeviceId
, false)),
126 scoped_ptr
<PrefHashStore
>(
127 new PrefHashStoreImpl(kSeed
, kDeviceId
, true)),
128 scoped_ptr
<HashStoreContents
>(
129 new PrefServiceHashStoreContents(kHashStoreId
, &local_state_
)),
131 &mock_unprotected_pref_filter_
,
132 &mock_protected_pref_filter_
);
134 // Verify initial expectations are met.
135 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
136 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
138 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
140 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
144 // Sets |key| to |value| in the test store identified by |store_id| before
145 // migration begins. Also sets the corresponding hash in the same store.
146 void PresetStoreValue(MockPrefStoreID store_id
,
147 const std::string
& key
,
148 const std::string value
) {
149 PresetStoreValueOnly(store_id
, key
, value
);
150 PresetStoreValueHash(store_id
, key
, value
);
153 // Sets |key| to |value| in the test store identified by |store_id| before
154 // migration begins. Stores the value hash in Local State as in M36 and
156 void PresetLegacyStoreValue(MockPrefStoreID store_id
,
157 const std::string
& key
,
158 const std::string value
) {
159 PresetStoreValueOnly(store_id
, key
, value
);
160 PresetLegacyValueHash(key
, value
);
163 // Stores a hash for |key| and |value| in the hash store identified by
164 // |store_id| before migration begins.
165 void PresetStoreValueHash(MockPrefStoreID store_id
,
166 const std::string
& key
,
167 const std::string value
) {
168 base::DictionaryValue
* store
= NULL
;
169 scoped_ptr
<PrefHashStore
> pref_hash_store
;
171 case MOCK_UNPROTECTED_PREF_STORE
:
172 store
= unprotected_prefs_
.get();
173 pref_hash_store
.reset(new PrefHashStoreImpl(kSeed
, kDeviceId
, false));
175 case MOCK_PROTECTED_PREF_STORE
:
176 store
= protected_prefs_
.get();
177 pref_hash_store
.reset(new PrefHashStoreImpl(kSeed
, kDeviceId
, true));
182 base::StringValue
string_value(value
);
183 pref_hash_store
->BeginTransaction(
184 scoped_ptr
<HashStoreContents
>(
185 new DictionaryHashStoreContents(store
)))->StoreHash(
189 // Stores a hash for |key| and |value| in the legacy hash store in
191 void PresetLegacyValueHash(const std::string
& key
,
192 const std::string value
) {
193 scoped_ptr
<PrefHashStore
> pref_hash_store(
194 new PrefHashStoreImpl(kSeed
, kDeviceId
, true));
196 base::StringValue
string_value(value
);
197 PrefHashStoreImpl(kSeed
, kDeviceId
, true)
198 .BeginTransaction(scoped_ptr
<HashStoreContents
>(
199 new PrefServiceHashStoreContents(kHashStoreId
, &local_state_
)))
200 ->StoreHash(key
, &string_value
);
203 // Returns true if the store opposite to |store_id| is observed for its next
205 bool WasOnSuccessfulWriteCallbackRegistered(MockPrefStoreID store_id
) {
207 case MOCK_UNPROTECTED_PREF_STORE
:
208 return !protected_store_successful_write_callback_
.is_null();
209 case MOCK_PROTECTED_PREF_STORE
:
210 return !unprotected_store_successful_write_callback_
.is_null();
216 // Verifies that the (key, value) pairs in |expected_prefs_in_store| are found
217 // in the store identified by |store_id|.
218 void VerifyValuesStored(
219 MockPrefStoreID store_id
,
220 const base::StringPairs
& expected_prefs_in_store
) {
221 base::DictionaryValue
* store
= NULL
;
223 case MOCK_UNPROTECTED_PREF_STORE
:
224 store
= unprotected_prefs_
.get();
226 case MOCK_PROTECTED_PREF_STORE
:
227 store
= protected_prefs_
.get();
232 for (base::StringPairs::const_iterator it
= expected_prefs_in_store
.begin();
233 it
!= expected_prefs_in_store
.end(); ++it
) {
235 EXPECT_TRUE(store
->GetString(it
->first
, &val
));
236 EXPECT_EQ(it
->second
, val
);
240 // Determines whether |expected_pref_in_hash_store| has a hash in the hash
241 // store identified by |store_id|.
242 bool ContainsHash(MockPrefStoreID store_id
,
243 std::string expected_pref_in_hash_store
) {
244 base::DictionaryValue
* store
= NULL
;
246 case MOCK_UNPROTECTED_PREF_STORE
:
247 store
= unprotected_prefs_
.get();
249 case MOCK_PROTECTED_PREF_STORE
:
250 store
= protected_prefs_
.get();
254 const base::DictionaryValue
* hash_store_contents
=
255 DictionaryHashStoreContents(store
).GetContents();
256 return hash_store_contents
&&
257 hash_store_contents
->GetString(expected_pref_in_hash_store
,
258 static_cast<std::string
*>(NULL
));
261 // Determines whether |expected_pref_in_hash_store| has a hash in the Local
263 bool ContainsLegacyHash(std::string expected_pref_in_hash_store
) {
264 const base::DictionaryValue
* hash_store_contents
=
265 PrefServiceHashStoreContents(kHashStoreId
, &local_state_
).GetContents();
266 return hash_store_contents
&&
267 hash_store_contents
->GetString(expected_pref_in_hash_store
,
268 static_cast<std::string
*>(NULL
));
271 // Both stores need to hand their prefs over in order for migration to kick
273 void HandPrefsToMigrator(MockPrefStoreID store_id
) {
275 case MOCK_UNPROTECTED_PREF_STORE
:
276 mock_unprotected_pref_filter_
.FilterOnLoad(
277 base::Bind(&TrackedPreferencesMigrationTest::GetPrefsBack
,
278 base::Unretained(this),
279 MOCK_UNPROTECTED_PREF_STORE
),
280 unprotected_prefs_
.Pass());
282 case MOCK_PROTECTED_PREF_STORE
:
283 mock_protected_pref_filter_
.FilterOnLoad(
284 base::Bind(&TrackedPreferencesMigrationTest::GetPrefsBack
,
285 base::Unretained(this),
286 MOCK_PROTECTED_PREF_STORE
),
287 protected_prefs_
.Pass());
292 bool HasPrefs(MockPrefStoreID store_id
) {
294 case MOCK_UNPROTECTED_PREF_STORE
:
295 return unprotected_prefs_
;
296 case MOCK_PROTECTED_PREF_STORE
:
297 return protected_prefs_
;
303 bool StoreModifiedByMigration(MockPrefStoreID store_id
) {
305 case MOCK_UNPROTECTED_PREF_STORE
:
306 return migration_modified_unprotected_store_
;
307 case MOCK_PROTECTED_PREF_STORE
:
308 return migration_modified_protected_store_
;
314 bool MigrationCompleted() {
315 return unprotected_store_migration_complete_
&&
316 protected_store_migration_complete_
;
319 void SimulateSuccessfulWrite(MockPrefStoreID store_id
) {
321 case MOCK_UNPROTECTED_PREF_STORE
:
322 EXPECT_FALSE(unprotected_store_successful_write_callback_
.is_null());
323 unprotected_store_successful_write_callback_
.Run();
324 unprotected_store_successful_write_callback_
.Reset();
326 case MOCK_PROTECTED_PREF_STORE
:
327 EXPECT_FALSE(protected_store_successful_write_callback_
.is_null());
328 protected_store_successful_write_callback_
.Run();
329 protected_store_successful_write_callback_
.Reset();
335 void RegisterSuccessfulWriteClosure(
336 MockPrefStoreID store_id
,
337 const base::Closure
& successful_write_closure
) {
339 case MOCK_UNPROTECTED_PREF_STORE
:
340 EXPECT_TRUE(unprotected_store_successful_write_callback_
.is_null());
341 unprotected_store_successful_write_callback_
= successful_write_closure
;
343 case MOCK_PROTECTED_PREF_STORE
:
344 EXPECT_TRUE(protected_store_successful_write_callback_
.is_null());
345 protected_store_successful_write_callback_
= successful_write_closure
;
350 // Helper given as an InterceptablePrefFilter::FinalizeFilterOnLoadCallback
351 // to the migrator to be invoked when it's done.
352 void GetPrefsBack(MockPrefStoreID store_id
,
353 scoped_ptr
<base::DictionaryValue
> prefs
,
354 bool prefs_altered
) {
356 case MOCK_UNPROTECTED_PREF_STORE
:
357 EXPECT_FALSE(unprotected_prefs_
);
358 unprotected_prefs_
= prefs
.Pass();
359 migration_modified_unprotected_store_
= prefs_altered
;
360 unprotected_store_migration_complete_
= true;
362 case MOCK_PROTECTED_PREF_STORE
:
363 EXPECT_FALSE(protected_prefs_
);
364 protected_prefs_
= prefs
.Pass();
365 migration_modified_protected_store_
= prefs_altered
;
366 protected_store_migration_complete_
= true;
371 // Helper given as a cleaning callback to the migrator.
372 void RemovePathFromStore(MockPrefStoreID store_id
, const std::string
& key
) {
374 case MOCK_UNPROTECTED_PREF_STORE
:
375 ASSERT_TRUE(unprotected_prefs_
);
376 unprotected_prefs_
->RemovePath(key
, NULL
);
378 case MOCK_PROTECTED_PREF_STORE
:
379 ASSERT_TRUE(protected_prefs_
);
380 protected_prefs_
->RemovePath(key
, NULL
);
385 // Sets |key| to |value| in the test store identified by |store_id| before
386 // migration begins. Does not store a preference hash.
387 void PresetStoreValueOnly(MockPrefStoreID store_id
,
388 const std::string
& key
,
389 const std::string value
) {
390 base::DictionaryValue
* store
= NULL
;
392 case MOCK_UNPROTECTED_PREF_STORE
:
393 store
= unprotected_prefs_
.get();
395 case MOCK_PROTECTED_PREF_STORE
:
396 store
= protected_prefs_
.get();
401 store
->SetString(key
, value
);
404 static const char kHashStoreId
[];
405 static const char kSeed
[];
406 static const char kDeviceId
[];
408 scoped_ptr
<base::DictionaryValue
> unprotected_prefs_
;
409 scoped_ptr
<base::DictionaryValue
> protected_prefs_
;
411 SimpleInterceptablePrefFilter mock_unprotected_pref_filter_
;
412 SimpleInterceptablePrefFilter mock_protected_pref_filter_
;
414 base::Closure unprotected_store_successful_write_callback_
;
415 base::Closure protected_store_successful_write_callback_
;
417 bool migration_modified_unprotected_store_
;
418 bool migration_modified_protected_store_
;
420 bool unprotected_store_migration_complete_
;
421 bool protected_store_migration_complete_
;
423 TestingPrefServiceSimple local_state_
;
425 DISALLOW_COPY_AND_ASSIGN(TrackedPreferencesMigrationTest
);
429 const char TrackedPreferencesMigrationTest::kHashStoreId
[] = "hash-store-id";
432 const char TrackedPreferencesMigrationTest::kSeed
[] = "seed";
435 const char TrackedPreferencesMigrationTest::kDeviceId
[] = "device-id";
439 TEST_F(TrackedPreferencesMigrationTest
, NoMigrationRequired
) {
440 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
,
441 kUnprotectedPrefValue
);
442 PresetStoreValue(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
,
443 kProtectedPrefValue
);
445 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
446 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
448 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
449 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
451 // Hand unprotected prefs to the migrator which should wait for the protected
453 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
454 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
455 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
456 EXPECT_FALSE(MigrationCompleted());
458 // Hand protected prefs to the migrator which should proceed with the
459 // migration synchronously.
460 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
461 EXPECT_TRUE(MigrationCompleted());
463 // Prefs should have been handed back over.
464 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
465 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
467 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
469 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
470 EXPECT_FALSE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE
));
471 EXPECT_FALSE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE
));
473 base::StringPairs expected_unprotected_values
;
474 expected_unprotected_values
.push_back(
475 std::make_pair(kUnprotectedPref
, kUnprotectedPrefValue
));
476 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
, expected_unprotected_values
);
478 base::StringPairs expected_protected_values
;
479 expected_protected_values
.push_back(
480 std::make_pair(kProtectedPref
, kProtectedPrefValue
));
481 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
483 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
484 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
486 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
487 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
490 TEST_F(TrackedPreferencesMigrationTest
, LegacyHashMigrationOnly
) {
491 PresetLegacyStoreValue(
492 MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
, kUnprotectedPrefValue
);
493 PresetLegacyStoreValue(
494 MOCK_PROTECTED_PREF_STORE
, kProtectedPref
, kProtectedPrefValue
);
496 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
497 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
499 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
500 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
502 EXPECT_TRUE(ContainsLegacyHash(kProtectedPref
));
503 EXPECT_TRUE(ContainsLegacyHash(kUnprotectedPref
));
505 // Hand unprotected prefs to the migrator which should wait for the protected
507 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
508 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
509 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
510 EXPECT_FALSE(MigrationCompleted());
512 // Hand protected prefs to the migrator which should proceed with the
513 // migration synchronously.
514 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
515 EXPECT_TRUE(MigrationCompleted());
517 // Prefs should have been handed back over.
518 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
519 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
521 // There is no pending cleanup task for the modern hash stores.
523 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
525 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
527 // Both stores were modified as hashes were moved from Local State.
528 EXPECT_TRUE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE
));
529 EXPECT_TRUE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE
));
531 base::StringPairs expected_unprotected_values
;
532 expected_unprotected_values
.push_back(
533 std::make_pair(kUnprotectedPref
, kUnprotectedPrefValue
));
534 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
, expected_unprotected_values
);
536 base::StringPairs expected_protected_values
;
537 expected_protected_values
.push_back(
538 std::make_pair(kProtectedPref
, kProtectedPrefValue
));
539 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
541 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
542 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
544 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
545 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
547 // The Local State hash store will not be reset until the next run.
548 EXPECT_TRUE(ContainsLegacyHash(kProtectedPref
));
549 EXPECT_TRUE(ContainsLegacyHash(kUnprotectedPref
));
553 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
554 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
555 EXPECT_TRUE(MigrationCompleted());
557 // Neither store was modified.
558 EXPECT_FALSE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE
));
559 EXPECT_FALSE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE
));
561 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
562 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
564 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
565 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
567 EXPECT_FALSE(ContainsLegacyHash(kProtectedPref
));
568 EXPECT_FALSE(ContainsLegacyHash(kUnprotectedPref
));
571 TEST_F(TrackedPreferencesMigrationTest
, FullMigrationWithLegacyHashStore
) {
572 // Store some values with matching MACs in Local State.
573 PresetLegacyStoreValue(
574 MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
, kUnprotectedPrefValue
);
575 PresetLegacyStoreValue(MOCK_UNPROTECTED_PREF_STORE
,
576 kPreviouslyUnprotectedPref
,
577 kPreviouslyUnprotectedPrefValue
);
578 PresetLegacyStoreValue(
579 MOCK_PROTECTED_PREF_STORE
, kProtectedPref
, kProtectedPrefValue
);
580 PresetLegacyStoreValue(MOCK_PROTECTED_PREF_STORE
,
581 kPreviouslyProtectedPref
,
582 kPreviouslyProtectedPrefValue
);
584 // Verify that there are no MACs in Preferences or Secure Preferences.
585 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
587 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
588 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
590 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
592 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
594 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
595 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
597 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
599 // Verify that there are MACs in Local State.
600 EXPECT_TRUE(ContainsLegacyHash(kUnprotectedPref
));
601 EXPECT_TRUE(ContainsLegacyHash(kPreviouslyUnprotectedPref
));
602 EXPECT_TRUE(ContainsLegacyHash(kProtectedPref
));
603 EXPECT_TRUE(ContainsLegacyHash(kPreviouslyProtectedPref
));
605 // Perform a first-pass migration.
606 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
607 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
608 EXPECT_TRUE(MigrationCompleted());
610 // All values should have been moved to their preferred locations, including
612 base::StringPairs expected_unprotected_values
;
613 expected_unprotected_values
.push_back(
614 std::make_pair(kUnprotectedPref
, kUnprotectedPrefValue
));
615 expected_unprotected_values
.push_back(
616 std::make_pair(kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
617 base::StringPairs expected_protected_values
;
618 expected_protected_values
.push_back(
619 std::make_pair(kProtectedPref
, kProtectedPrefValue
));
620 expected_protected_values
.push_back(std::make_pair(
621 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
623 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
, expected_unprotected_values
);
624 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
626 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
628 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
630 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
631 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
633 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
634 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
637 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
639 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
641 // Removing the values from their previous locations is deferred until the new
642 // locations are persisted.
643 EXPECT_TRUE(ContainsLegacyHash(kUnprotectedPref
));
644 EXPECT_TRUE(ContainsLegacyHash(kPreviouslyUnprotectedPref
));
645 EXPECT_TRUE(ContainsLegacyHash(kProtectedPref
));
646 EXPECT_TRUE(ContainsLegacyHash(kPreviouslyProtectedPref
));
649 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
651 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
653 SimulateSuccessfulWrite(MOCK_UNPROTECTED_PREF_STORE
);
654 SimulateSuccessfulWrite(MOCK_PROTECTED_PREF_STORE
);
658 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
659 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
660 EXPECT_TRUE(MigrationCompleted());
662 // In this run the MACs should have been removed from their previous
663 // locations. There is no more pending action.
665 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
667 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
669 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
671 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
672 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
674 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
676 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
678 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
679 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
681 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
683 EXPECT_FALSE(ContainsLegacyHash(kUnprotectedPref
));
684 EXPECT_FALSE(ContainsLegacyHash(kPreviouslyUnprotectedPref
));
685 EXPECT_FALSE(ContainsLegacyHash(kProtectedPref
));
686 EXPECT_FALSE(ContainsLegacyHash(kPreviouslyProtectedPref
));
688 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
, expected_unprotected_values
);
689 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
692 TEST_F(TrackedPreferencesMigrationTest
, FullMigration
) {
694 MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
, kUnprotectedPrefValue
);
695 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE
,
696 kPreviouslyUnprotectedPref
,
697 kPreviouslyUnprotectedPrefValue
);
699 MOCK_PROTECTED_PREF_STORE
, kProtectedPref
, kProtectedPrefValue
);
700 PresetStoreValue(MOCK_PROTECTED_PREF_STORE
,
701 kPreviouslyProtectedPref
,
702 kPreviouslyProtectedPrefValue
);
704 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
706 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
707 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
709 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
711 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
713 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
714 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
716 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
718 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
719 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
720 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
721 EXPECT_FALSE(MigrationCompleted());
723 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
724 EXPECT_TRUE(MigrationCompleted());
726 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
727 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
729 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
731 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
732 EXPECT_TRUE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE
));
733 EXPECT_TRUE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE
));
735 // Values should have been migrated to their store, but migrated values should
736 // still remain in the source store until cleanup tasks are later invoked.
738 base::StringPairs expected_unprotected_values
;
739 expected_unprotected_values
.push_back(std::make_pair(
740 kUnprotectedPref
, kUnprotectedPrefValue
));
741 expected_unprotected_values
.push_back(std::make_pair(
742 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
743 expected_unprotected_values
.push_back(std::make_pair(
744 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
745 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
,
746 expected_unprotected_values
);
748 base::StringPairs expected_protected_values
;
749 expected_protected_values
.push_back(std::make_pair(
750 kProtectedPref
, kProtectedPrefValue
));
751 expected_protected_values
.push_back(std::make_pair(
752 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
753 expected_unprotected_values
.push_back(std::make_pair(
754 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
755 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
757 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
759 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
760 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
762 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
764 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
766 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
767 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
769 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
772 // A successful write of the protected pref store should result in a clean up
773 // of the unprotected store.
774 SimulateSuccessfulWrite(MOCK_PROTECTED_PREF_STORE
);
777 base::StringPairs expected_unprotected_values
;
778 expected_unprotected_values
.push_back(std::make_pair(
779 kUnprotectedPref
, kUnprotectedPrefValue
));
780 expected_unprotected_values
.push_back(std::make_pair(
781 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
782 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
,
783 expected_unprotected_values
);
785 base::StringPairs expected_protected_values
;
786 expected_protected_values
.push_back(std::make_pair(
787 kProtectedPref
, kProtectedPrefValue
));
788 expected_protected_values
.push_back(std::make_pair(
789 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
790 expected_unprotected_values
.push_back(std::make_pair(
791 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
792 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
795 SimulateSuccessfulWrite(MOCK_UNPROTECTED_PREF_STORE
);
798 base::StringPairs expected_unprotected_values
;
799 expected_unprotected_values
.push_back(std::make_pair(
800 kUnprotectedPref
, kUnprotectedPrefValue
));
801 expected_unprotected_values
.push_back(std::make_pair(
802 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
803 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
,
804 expected_unprotected_values
);
806 base::StringPairs expected_protected_values
;
807 expected_protected_values
.push_back(std::make_pair(
808 kProtectedPref
, kProtectedPrefValue
));
809 expected_protected_values
.push_back(std::make_pair(
810 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
811 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
814 // Hashes are not cleaned up yet.
815 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
817 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
818 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
820 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
822 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
824 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
825 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
827 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
831 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
832 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
833 EXPECT_TRUE(MigrationCompleted());
835 // Hashes are cleaned up.
836 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
838 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
839 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
841 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
843 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
845 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
846 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
848 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
851 TEST_F(TrackedPreferencesMigrationTest
, CleanupOnly
) {
852 // Already migrated; only cleanup needed.
854 MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
, kUnprotectedPrefValue
);
855 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE
,
856 kPreviouslyProtectedPref
,
857 kPreviouslyProtectedPrefValue
);
858 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE
,
859 kPreviouslyUnprotectedPref
,
860 kPreviouslyUnprotectedPrefValue
);
862 MOCK_PROTECTED_PREF_STORE
, kProtectedPref
, kProtectedPrefValue
);
863 PresetStoreValue(MOCK_PROTECTED_PREF_STORE
,
864 kPreviouslyProtectedPref
,
865 kPreviouslyProtectedPrefValue
);
866 PresetStoreValue(MOCK_PROTECTED_PREF_STORE
,
867 kPreviouslyUnprotectedPref
,
868 kPreviouslyUnprotectedPrefValue
);
870 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
871 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
872 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
873 EXPECT_FALSE(MigrationCompleted());
875 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
876 EXPECT_TRUE(MigrationCompleted());
878 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
879 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
881 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
883 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
884 EXPECT_FALSE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE
));
885 EXPECT_FALSE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE
));
887 // Cleanup should happen synchronously if the values were already present in
888 // their destination stores.
890 base::StringPairs expected_unprotected_values
;
891 expected_unprotected_values
.push_back(std::make_pair(
892 kUnprotectedPref
, kUnprotectedPrefValue
));
893 expected_unprotected_values
.push_back(std::make_pair(
894 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
895 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
,
896 expected_unprotected_values
);
898 base::StringPairs expected_protected_values
;
899 expected_protected_values
.push_back(std::make_pair(
900 kProtectedPref
, kProtectedPrefValue
));
901 expected_protected_values
.push_back(std::make_pair(
902 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
903 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);