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/values.h"
16 #include "chrome/browser/prefs/interceptable_pref_filter.h"
17 #include "chrome/browser/prefs/pref_hash_store.h"
18 #include "chrome/browser/prefs/pref_hash_store_impl.h"
19 #include "chrome/browser/prefs/pref_hash_store_transaction.h"
20 #include "chrome/browser/prefs/profile_pref_store_manager.h"
21 #include "chrome/browser/prefs/tracked/dictionary_hash_store_contents.h"
22 #include "chrome/browser/prefs/tracked/hash_store_contents.h"
23 #include "chrome/browser/prefs/tracked/pref_service_hash_store_contents.h"
24 #include "chrome/browser/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 virtual void FilterUpdate(const std::string
& path
) OVERRIDE
{
51 virtual void FilterSerializeData(
52 base::DictionaryValue
* pref_store_contents
) OVERRIDE
{
57 // InterceptablePrefFilter implementation.
58 virtual void FinalizeFilterOnLoad(
59 const PostFilterOnLoadCallback
& post_filter_on_load_callback
,
60 scoped_ptr
<base::DictionaryValue
> pref_store_contents
,
61 bool prefs_altered
) OVERRIDE
{
62 post_filter_on_load_callback
.Run(pref_store_contents
.Pass(), prefs_altered
);
66 // A test fixture designed to be used like this:
67 // 1) Set up initial store prefs with PresetStoreValue().
68 // 2) Hand both sets of prefs to the migrator via HandPrefsToMigrator().
69 // 3) Migration completes synchronously when the second store hands its prefs
71 // 4) Verifications can be made via various methods of this fixture.
72 // Call Reset() to perform a second migration.
73 class TrackedPreferencesMigrationTest
: public testing::Test
{
75 enum MockPrefStoreID
{
76 MOCK_UNPROTECTED_PREF_STORE
,
77 MOCK_PROTECTED_PREF_STORE
,
80 TrackedPreferencesMigrationTest()
81 : unprotected_prefs_(new base::DictionaryValue
),
82 protected_prefs_(new base::DictionaryValue
),
83 migration_modified_unprotected_store_(false),
84 migration_modified_protected_store_(false),
85 unprotected_store_migration_complete_(false),
86 protected_store_migration_complete_(false) {}
88 virtual void SetUp() OVERRIDE
{
89 ProfilePrefStoreManager::RegisterPrefs(local_state_
.registry());
94 std::set
<std::string
> unprotected_pref_names
;
95 std::set
<std::string
> protected_pref_names
;
96 unprotected_pref_names
.insert(kUnprotectedPref
);
97 unprotected_pref_names
.insert(kPreviouslyProtectedPref
);
98 protected_pref_names
.insert(kProtectedPref
);
99 protected_pref_names
.insert(kPreviouslyUnprotectedPref
);
101 migration_modified_unprotected_store_
= false;
102 migration_modified_protected_store_
= false;
103 unprotected_store_migration_complete_
= false;
104 protected_store_migration_complete_
= false;
106 unprotected_store_successful_write_callback_
.Reset();
107 protected_store_successful_write_callback_
.Reset();
109 SetupTrackedPreferencesMigration(
110 unprotected_pref_names
,
111 protected_pref_names
,
112 base::Bind(&TrackedPreferencesMigrationTest::RemovePathFromStore
,
113 base::Unretained(this),
114 MOCK_UNPROTECTED_PREF_STORE
),
115 base::Bind(&TrackedPreferencesMigrationTest::RemovePathFromStore
,
116 base::Unretained(this),
117 MOCK_PROTECTED_PREF_STORE
),
119 &TrackedPreferencesMigrationTest::RegisterSuccessfulWriteClosure
,
120 base::Unretained(this),
121 MOCK_UNPROTECTED_PREF_STORE
),
123 &TrackedPreferencesMigrationTest::RegisterSuccessfulWriteClosure
,
124 base::Unretained(this),
125 MOCK_PROTECTED_PREF_STORE
),
126 scoped_ptr
<PrefHashStore
>(
127 new PrefHashStoreImpl(kSeed
, kDeviceId
, false)),
128 scoped_ptr
<PrefHashStore
>(
129 new PrefHashStoreImpl(kSeed
, kDeviceId
, true)),
130 scoped_ptr
<HashStoreContents
>(
131 new PrefServiceHashStoreContents(kHashStoreId
, &local_state_
)),
133 &mock_unprotected_pref_filter_
,
134 &mock_protected_pref_filter_
);
136 // Verify initial expectations are met.
137 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
138 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
140 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
142 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
146 // Sets |key| to |value| in the test store identified by |store_id| before
147 // migration begins. Also sets the corresponding hash in the same store.
148 void PresetStoreValue(MockPrefStoreID store_id
,
149 const std::string
& key
,
150 const std::string value
) {
151 PresetStoreValueOnly(store_id
, key
, value
);
152 PresetStoreValueHash(store_id
, key
, value
);
155 // Sets |key| to |value| in the test store identified by |store_id| before
156 // migration begins. Stores the value hash in Local State as in M36 and
158 void PresetLegacyStoreValue(MockPrefStoreID store_id
,
159 const std::string
& key
,
160 const std::string value
) {
161 PresetStoreValueOnly(store_id
, key
, value
);
162 PresetLegacyValueHash(key
, value
);
165 // Stores a hash for |key| and |value| in the hash store identified by
166 // |store_id| before migration begins.
167 void PresetStoreValueHash(MockPrefStoreID store_id
,
168 const std::string
& key
,
169 const std::string value
) {
170 base::DictionaryValue
* store
= NULL
;
171 scoped_ptr
<PrefHashStore
> pref_hash_store
;
173 case MOCK_UNPROTECTED_PREF_STORE
:
174 store
= unprotected_prefs_
.get();
175 pref_hash_store
.reset(new PrefHashStoreImpl(kSeed
, kDeviceId
, false));
177 case MOCK_PROTECTED_PREF_STORE
:
178 store
= protected_prefs_
.get();
179 pref_hash_store
.reset(new PrefHashStoreImpl(kSeed
, kDeviceId
, true));
184 base::StringValue
string_value(value
);
185 pref_hash_store
->BeginTransaction(
186 scoped_ptr
<HashStoreContents
>(
187 new DictionaryHashStoreContents(store
)))->StoreHash(
191 // Stores a hash for |key| and |value| in the legacy hash store in
193 void PresetLegacyValueHash(const std::string
& key
,
194 const std::string value
) {
195 scoped_ptr
<PrefHashStore
> pref_hash_store(
196 new PrefHashStoreImpl(kSeed
, kDeviceId
, true));
198 base::StringValue
string_value(value
);
199 PrefHashStoreImpl(kSeed
, kDeviceId
, true)
200 .BeginTransaction(scoped_ptr
<HashStoreContents
>(
201 new PrefServiceHashStoreContents(kHashStoreId
, &local_state_
)))
202 ->StoreHash(key
, &string_value
);
205 // Returns true if the store opposite to |store_id| is observed for its next
207 bool WasOnSuccessfulWriteCallbackRegistered(MockPrefStoreID store_id
) {
209 case MOCK_UNPROTECTED_PREF_STORE
:
210 return !protected_store_successful_write_callback_
.is_null();
211 case MOCK_PROTECTED_PREF_STORE
:
212 return !unprotected_store_successful_write_callback_
.is_null();
218 // Verifies that the (key, value) pairs in |expected_prefs_in_store| are found
219 // in the store identified by |store_id|.
220 void VerifyValuesStored(
221 MockPrefStoreID store_id
,
222 const std::vector
<std::pair
<std::string
, std::string
> >&
223 expected_prefs_in_store
) {
224 base::DictionaryValue
* store
= NULL
;
226 case MOCK_UNPROTECTED_PREF_STORE
:
227 store
= unprotected_prefs_
.get();
229 case MOCK_PROTECTED_PREF_STORE
:
230 store
= protected_prefs_
.get();
235 for (std::vector
<std::pair
<std::string
, std::string
> >::const_iterator it
=
236 expected_prefs_in_store
.begin();
237 it
!= expected_prefs_in_store
.end(); ++it
) {
239 EXPECT_TRUE(store
->GetString(it
->first
, &val
));
240 EXPECT_EQ(it
->second
, val
);
244 // Determines whether |expected_pref_in_hash_store| has a hash in the hash
245 // store identified by |store_id|.
246 bool ContainsHash(MockPrefStoreID store_id
,
247 std::string expected_pref_in_hash_store
) {
248 base::DictionaryValue
* store
= NULL
;
250 case MOCK_UNPROTECTED_PREF_STORE
:
251 store
= unprotected_prefs_
.get();
253 case MOCK_PROTECTED_PREF_STORE
:
254 store
= protected_prefs_
.get();
258 const base::DictionaryValue
* hash_store_contents
=
259 DictionaryHashStoreContents(store
).GetContents();
260 return hash_store_contents
&&
261 hash_store_contents
->GetString(expected_pref_in_hash_store
,
262 static_cast<std::string
*>(NULL
));
265 // Determines whether |expected_pref_in_hash_store| has a hash in the Local
267 bool ContainsLegacyHash(std::string expected_pref_in_hash_store
) {
268 const base::DictionaryValue
* hash_store_contents
=
269 PrefServiceHashStoreContents(kHashStoreId
, &local_state_
).GetContents();
270 return hash_store_contents
&&
271 hash_store_contents
->GetString(expected_pref_in_hash_store
,
272 static_cast<std::string
*>(NULL
));
275 // Both stores need to hand their prefs over in order for migration to kick
277 void HandPrefsToMigrator(MockPrefStoreID store_id
) {
279 case MOCK_UNPROTECTED_PREF_STORE
:
280 mock_unprotected_pref_filter_
.FilterOnLoad(
281 base::Bind(&TrackedPreferencesMigrationTest::GetPrefsBack
,
282 base::Unretained(this),
283 MOCK_UNPROTECTED_PREF_STORE
),
284 unprotected_prefs_
.Pass());
286 case MOCK_PROTECTED_PREF_STORE
:
287 mock_protected_pref_filter_
.FilterOnLoad(
288 base::Bind(&TrackedPreferencesMigrationTest::GetPrefsBack
,
289 base::Unretained(this),
290 MOCK_PROTECTED_PREF_STORE
),
291 protected_prefs_
.Pass());
296 bool HasPrefs(MockPrefStoreID store_id
) {
298 case MOCK_UNPROTECTED_PREF_STORE
:
299 return unprotected_prefs_
;
300 case MOCK_PROTECTED_PREF_STORE
:
301 return protected_prefs_
;
307 bool StoreModifiedByMigration(MockPrefStoreID store_id
) {
309 case MOCK_UNPROTECTED_PREF_STORE
:
310 return migration_modified_unprotected_store_
;
311 case MOCK_PROTECTED_PREF_STORE
:
312 return migration_modified_protected_store_
;
318 bool MigrationCompleted() {
319 return unprotected_store_migration_complete_
&&
320 protected_store_migration_complete_
;
323 void SimulateSuccessfulWrite(MockPrefStoreID store_id
) {
325 case MOCK_UNPROTECTED_PREF_STORE
:
326 EXPECT_FALSE(unprotected_store_successful_write_callback_
.is_null());
327 unprotected_store_successful_write_callback_
.Run();
328 unprotected_store_successful_write_callback_
.Reset();
330 case MOCK_PROTECTED_PREF_STORE
:
331 EXPECT_FALSE(protected_store_successful_write_callback_
.is_null());
332 protected_store_successful_write_callback_
.Run();
333 protected_store_successful_write_callback_
.Reset();
339 void RegisterSuccessfulWriteClosure(
340 MockPrefStoreID store_id
,
341 const base::Closure
& successful_write_closure
) {
343 case MOCK_UNPROTECTED_PREF_STORE
:
344 EXPECT_TRUE(unprotected_store_successful_write_callback_
.is_null());
345 unprotected_store_successful_write_callback_
= successful_write_closure
;
347 case MOCK_PROTECTED_PREF_STORE
:
348 EXPECT_TRUE(protected_store_successful_write_callback_
.is_null());
349 protected_store_successful_write_callback_
= successful_write_closure
;
354 // Helper given as an InterceptablePrefFilter::FinalizeFilterOnLoadCallback
355 // to the migrator to be invoked when it's done.
356 void GetPrefsBack(MockPrefStoreID store_id
,
357 scoped_ptr
<base::DictionaryValue
> prefs
,
358 bool prefs_altered
) {
360 case MOCK_UNPROTECTED_PREF_STORE
:
361 EXPECT_FALSE(unprotected_prefs_
);
362 unprotected_prefs_
= prefs
.Pass();
363 migration_modified_unprotected_store_
= prefs_altered
;
364 unprotected_store_migration_complete_
= true;
366 case MOCK_PROTECTED_PREF_STORE
:
367 EXPECT_FALSE(protected_prefs_
);
368 protected_prefs_
= prefs
.Pass();
369 migration_modified_protected_store_
= prefs_altered
;
370 protected_store_migration_complete_
= true;
375 // Helper given as a cleaning callback to the migrator.
376 void RemovePathFromStore(MockPrefStoreID store_id
, const std::string
& key
) {
378 case MOCK_UNPROTECTED_PREF_STORE
:
379 ASSERT_TRUE(unprotected_prefs_
);
380 unprotected_prefs_
->RemovePath(key
, NULL
);
382 case MOCK_PROTECTED_PREF_STORE
:
383 ASSERT_TRUE(protected_prefs_
);
384 protected_prefs_
->RemovePath(key
, NULL
);
389 // Sets |key| to |value| in the test store identified by |store_id| before
390 // migration begins. Does not store a preference hash.
391 void PresetStoreValueOnly(MockPrefStoreID store_id
,
392 const std::string
& key
,
393 const std::string value
) {
394 base::DictionaryValue
* store
= NULL
;
396 case MOCK_UNPROTECTED_PREF_STORE
:
397 store
= unprotected_prefs_
.get();
399 case MOCK_PROTECTED_PREF_STORE
:
400 store
= protected_prefs_
.get();
405 store
->SetString(key
, value
);
408 static const char kHashStoreId
[];
409 static const char kSeed
[];
410 static const char kDeviceId
[];
412 scoped_ptr
<base::DictionaryValue
> unprotected_prefs_
;
413 scoped_ptr
<base::DictionaryValue
> protected_prefs_
;
415 SimpleInterceptablePrefFilter mock_unprotected_pref_filter_
;
416 SimpleInterceptablePrefFilter mock_protected_pref_filter_
;
418 base::Closure unprotected_store_successful_write_callback_
;
419 base::Closure protected_store_successful_write_callback_
;
421 bool migration_modified_unprotected_store_
;
422 bool migration_modified_protected_store_
;
424 bool unprotected_store_migration_complete_
;
425 bool protected_store_migration_complete_
;
427 TestingPrefServiceSimple local_state_
;
429 DISALLOW_COPY_AND_ASSIGN(TrackedPreferencesMigrationTest
);
433 const char TrackedPreferencesMigrationTest::kHashStoreId
[] = "hash-store-id";
436 const char TrackedPreferencesMigrationTest::kSeed
[] = "seed";
439 const char TrackedPreferencesMigrationTest::kDeviceId
[] = "device-id";
443 TEST_F(TrackedPreferencesMigrationTest
, NoMigrationRequired
) {
444 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
,
445 kUnprotectedPrefValue
);
446 PresetStoreValue(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
,
447 kProtectedPrefValue
);
449 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
450 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
452 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
453 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
455 // Hand unprotected prefs to the migrator which should wait for the protected
457 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
458 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
459 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
460 EXPECT_FALSE(MigrationCompleted());
462 // Hand protected prefs to the migrator which should proceed with the
463 // migration synchronously.
464 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
465 EXPECT_TRUE(MigrationCompleted());
467 // Prefs should have been handed back over.
468 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
469 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
471 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
473 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
474 EXPECT_FALSE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE
));
475 EXPECT_FALSE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE
));
477 std::vector
<std::pair
<std::string
, std::string
> > expected_unprotected_values
;
478 expected_unprotected_values
.push_back(
479 std::make_pair(kUnprotectedPref
, kUnprotectedPrefValue
));
480 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
, expected_unprotected_values
);
482 std::vector
<std::pair
<std::string
, std::string
> > expected_protected_values
;
483 expected_protected_values
.push_back(
484 std::make_pair(kProtectedPref
, kProtectedPrefValue
));
485 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
487 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
488 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
490 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
491 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
494 TEST_F(TrackedPreferencesMigrationTest
, LegacyHashMigrationOnly
) {
495 PresetLegacyStoreValue(
496 MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
, kUnprotectedPrefValue
);
497 PresetLegacyStoreValue(
498 MOCK_PROTECTED_PREF_STORE
, kProtectedPref
, kProtectedPrefValue
);
500 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
501 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
503 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
504 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
506 EXPECT_TRUE(ContainsLegacyHash(kProtectedPref
));
507 EXPECT_TRUE(ContainsLegacyHash(kUnprotectedPref
));
509 // Hand unprotected prefs to the migrator which should wait for the protected
511 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
512 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
513 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
514 EXPECT_FALSE(MigrationCompleted());
516 // Hand protected prefs to the migrator which should proceed with the
517 // migration synchronously.
518 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
519 EXPECT_TRUE(MigrationCompleted());
521 // Prefs should have been handed back over.
522 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
523 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
525 // There is no pending cleanup task for the modern hash stores.
527 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
529 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
531 // Both stores were modified as hashes were moved from Local State.
532 EXPECT_TRUE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE
));
533 EXPECT_TRUE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE
));
535 std::vector
<std::pair
<std::string
, std::string
> > expected_unprotected_values
;
536 expected_unprotected_values
.push_back(
537 std::make_pair(kUnprotectedPref
, kUnprotectedPrefValue
));
538 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
, expected_unprotected_values
);
540 std::vector
<std::pair
<std::string
, std::string
> > expected_protected_values
;
541 expected_protected_values
.push_back(
542 std::make_pair(kProtectedPref
, kProtectedPrefValue
));
543 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
545 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
546 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
548 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
549 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
551 // The Local State hash store will not be reset until the next run.
552 EXPECT_TRUE(ContainsLegacyHash(kProtectedPref
));
553 EXPECT_TRUE(ContainsLegacyHash(kUnprotectedPref
));
557 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
558 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
559 EXPECT_TRUE(MigrationCompleted());
561 // Neither store was modified.
562 EXPECT_FALSE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE
));
563 EXPECT_FALSE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE
));
565 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
566 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
568 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
569 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
571 EXPECT_FALSE(ContainsLegacyHash(kProtectedPref
));
572 EXPECT_FALSE(ContainsLegacyHash(kUnprotectedPref
));
575 TEST_F(TrackedPreferencesMigrationTest
, FullMigrationWithLegacyHashStore
) {
576 // Store some values with matching MACs in Local State.
577 PresetLegacyStoreValue(
578 MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
, kUnprotectedPrefValue
);
579 PresetLegacyStoreValue(MOCK_UNPROTECTED_PREF_STORE
,
580 kPreviouslyUnprotectedPref
,
581 kPreviouslyUnprotectedPrefValue
);
582 PresetLegacyStoreValue(
583 MOCK_PROTECTED_PREF_STORE
, kProtectedPref
, kProtectedPrefValue
);
584 PresetLegacyStoreValue(MOCK_PROTECTED_PREF_STORE
,
585 kPreviouslyProtectedPref
,
586 kPreviouslyProtectedPrefValue
);
588 // Verify that there are no MACs in Preferences or Secure Preferences.
589 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
591 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
592 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
594 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
596 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
598 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
599 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
601 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
603 // Verify that there are MACs in Local State.
604 EXPECT_TRUE(ContainsLegacyHash(kUnprotectedPref
));
605 EXPECT_TRUE(ContainsLegacyHash(kPreviouslyUnprotectedPref
));
606 EXPECT_TRUE(ContainsLegacyHash(kProtectedPref
));
607 EXPECT_TRUE(ContainsLegacyHash(kPreviouslyProtectedPref
));
609 // Perform a first-pass migration.
610 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
611 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
612 EXPECT_TRUE(MigrationCompleted());
614 // All values should have been moved to their preferred locations, including
616 std::vector
<std::pair
<std::string
, std::string
> > expected_unprotected_values
;
617 expected_unprotected_values
.push_back(
618 std::make_pair(kUnprotectedPref
, kUnprotectedPrefValue
));
619 expected_unprotected_values
.push_back(
620 std::make_pair(kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
621 std::vector
<std::pair
<std::string
, std::string
> > expected_protected_values
;
622 expected_protected_values
.push_back(
623 std::make_pair(kProtectedPref
, kProtectedPrefValue
));
624 expected_protected_values
.push_back(std::make_pair(
625 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
627 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
, expected_unprotected_values
);
628 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
630 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
632 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
634 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
635 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
637 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
638 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
641 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
643 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
645 // Removing the values from their previous locations is deferred until the new
646 // locations are persisted.
647 EXPECT_TRUE(ContainsLegacyHash(kUnprotectedPref
));
648 EXPECT_TRUE(ContainsLegacyHash(kPreviouslyUnprotectedPref
));
649 EXPECT_TRUE(ContainsLegacyHash(kProtectedPref
));
650 EXPECT_TRUE(ContainsLegacyHash(kPreviouslyProtectedPref
));
653 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
655 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
657 SimulateSuccessfulWrite(MOCK_UNPROTECTED_PREF_STORE
);
658 SimulateSuccessfulWrite(MOCK_PROTECTED_PREF_STORE
);
662 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
663 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
664 EXPECT_TRUE(MigrationCompleted());
666 // In this run the MACs should have been removed from their previous
667 // locations. There is no more pending action.
669 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
671 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
673 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
675 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
676 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
678 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
680 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
682 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
683 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
685 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
687 EXPECT_FALSE(ContainsLegacyHash(kUnprotectedPref
));
688 EXPECT_FALSE(ContainsLegacyHash(kPreviouslyUnprotectedPref
));
689 EXPECT_FALSE(ContainsLegacyHash(kProtectedPref
));
690 EXPECT_FALSE(ContainsLegacyHash(kPreviouslyProtectedPref
));
692 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
, expected_unprotected_values
);
693 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
696 TEST_F(TrackedPreferencesMigrationTest
, FullMigration
) {
698 MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
, kUnprotectedPrefValue
);
699 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE
,
700 kPreviouslyUnprotectedPref
,
701 kPreviouslyUnprotectedPrefValue
);
703 MOCK_PROTECTED_PREF_STORE
, kProtectedPref
, kProtectedPrefValue
);
704 PresetStoreValue(MOCK_PROTECTED_PREF_STORE
,
705 kPreviouslyProtectedPref
,
706 kPreviouslyProtectedPrefValue
);
708 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
710 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
711 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
713 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
715 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
717 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
718 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
720 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
722 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
723 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
724 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
725 EXPECT_FALSE(MigrationCompleted());
727 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
728 EXPECT_TRUE(MigrationCompleted());
730 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
731 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
733 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
735 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
736 EXPECT_TRUE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE
));
737 EXPECT_TRUE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE
));
739 // Values should have been migrated to their store, but migrated values should
740 // still remain in the source store until cleanup tasks are later invoked.
742 std::vector
<std::pair
<std::string
, std::string
> >
743 expected_unprotected_values
;
744 expected_unprotected_values
.push_back(std::make_pair(
745 kUnprotectedPref
, kUnprotectedPrefValue
));
746 expected_unprotected_values
.push_back(std::make_pair(
747 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
748 expected_unprotected_values
.push_back(std::make_pair(
749 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
750 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
,
751 expected_unprotected_values
);
753 std::vector
<std::pair
<std::string
, std::string
> > expected_protected_values
;
754 expected_protected_values
.push_back(std::make_pair(
755 kProtectedPref
, kProtectedPrefValue
));
756 expected_protected_values
.push_back(std::make_pair(
757 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
758 expected_unprotected_values
.push_back(std::make_pair(
759 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
760 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
762 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
764 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
765 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
767 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
769 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
771 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
772 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
774 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
777 // A successful write of the protected pref store should result in a clean up
778 // of the unprotected store.
779 SimulateSuccessfulWrite(MOCK_PROTECTED_PREF_STORE
);
782 std::vector
<std::pair
<std::string
, std::string
> >
783 expected_unprotected_values
;
784 expected_unprotected_values
.push_back(std::make_pair(
785 kUnprotectedPref
, kUnprotectedPrefValue
));
786 expected_unprotected_values
.push_back(std::make_pair(
787 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
788 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
,
789 expected_unprotected_values
);
791 std::vector
<std::pair
<std::string
, std::string
> > expected_protected_values
;
792 expected_protected_values
.push_back(std::make_pair(
793 kProtectedPref
, kProtectedPrefValue
));
794 expected_protected_values
.push_back(std::make_pair(
795 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
796 expected_unprotected_values
.push_back(std::make_pair(
797 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
798 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
801 SimulateSuccessfulWrite(MOCK_UNPROTECTED_PREF_STORE
);
804 std::vector
<std::pair
<std::string
, std::string
> >
805 expected_unprotected_values
;
806 expected_unprotected_values
.push_back(std::make_pair(
807 kUnprotectedPref
, kUnprotectedPrefValue
));
808 expected_unprotected_values
.push_back(std::make_pair(
809 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
810 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
,
811 expected_unprotected_values
);
813 std::vector
<std::pair
<std::string
, std::string
> > expected_protected_values
;
814 expected_protected_values
.push_back(std::make_pair(
815 kProtectedPref
, kProtectedPrefValue
));
816 expected_protected_values
.push_back(std::make_pair(
817 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
818 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);
821 // Hashes are not cleaned up yet.
822 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
824 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
825 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
827 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
829 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
831 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
832 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
834 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
838 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
839 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
840 EXPECT_TRUE(MigrationCompleted());
842 // Hashes are cleaned up.
843 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
));
845 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
846 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kProtectedPref
));
848 ContainsHash(MOCK_UNPROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
850 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kUnprotectedPref
));
852 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyUnprotectedPref
));
853 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE
, kProtectedPref
));
855 ContainsHash(MOCK_PROTECTED_PREF_STORE
, kPreviouslyProtectedPref
));
858 TEST_F(TrackedPreferencesMigrationTest
, CleanupOnly
) {
859 // Already migrated; only cleanup needed.
861 MOCK_UNPROTECTED_PREF_STORE
, kUnprotectedPref
, kUnprotectedPrefValue
);
862 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE
,
863 kPreviouslyProtectedPref
,
864 kPreviouslyProtectedPrefValue
);
865 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE
,
866 kPreviouslyUnprotectedPref
,
867 kPreviouslyUnprotectedPrefValue
);
869 MOCK_PROTECTED_PREF_STORE
, kProtectedPref
, kProtectedPrefValue
);
870 PresetStoreValue(MOCK_PROTECTED_PREF_STORE
,
871 kPreviouslyProtectedPref
,
872 kPreviouslyProtectedPrefValue
);
873 PresetStoreValue(MOCK_PROTECTED_PREF_STORE
,
874 kPreviouslyUnprotectedPref
,
875 kPreviouslyUnprotectedPrefValue
);
877 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE
);
878 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
879 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
880 EXPECT_FALSE(MigrationCompleted());
882 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE
);
883 EXPECT_TRUE(MigrationCompleted());
885 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE
));
886 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE
));
888 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE
));
890 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE
));
891 EXPECT_FALSE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE
));
892 EXPECT_FALSE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE
));
894 // Cleanup should happen synchronously if the values were already present in
895 // their destination stores.
897 std::vector
<std::pair
<std::string
, std::string
> >
898 expected_unprotected_values
;
899 expected_unprotected_values
.push_back(std::make_pair(
900 kUnprotectedPref
, kUnprotectedPrefValue
));
901 expected_unprotected_values
.push_back(std::make_pair(
902 kPreviouslyProtectedPref
, kPreviouslyProtectedPrefValue
));
903 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE
,
904 expected_unprotected_values
);
906 std::vector
<std::pair
<std::string
, std::string
> > expected_protected_values
;
907 expected_protected_values
.push_back(std::make_pair(
908 kProtectedPref
, kProtectedPrefValue
));
909 expected_protected_values
.push_back(std::make_pair(
910 kPreviouslyUnprotectedPref
, kPreviouslyUnprotectedPrefValue
));
911 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE
, expected_protected_values
);