cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / user_prefs / tracked / tracked_preferences_migration_unittest.cc
blob7557f36bb1d46d6c6c0dc7d34d272449def89e34
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"
7 #include <set>
8 #include <string>
9 #include <vector>
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"
27 namespace {
29 // An unprotected pref.
30 const char kUnprotectedPref[] = "unprotected";
31 // A protected pref.
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 {
46 public:
47 // PrefFilter remaining implementation.
48 void FilterUpdate(const std::string& path) override { ADD_FAILURE(); }
49 void FilterSerializeData(
50 base::DictionaryValue* pref_store_contents) override {
51 ADD_FAILURE();
54 private:
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
68 // over.
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 {
72 public:
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());
88 Reset();
91 void Reset() {
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),
116 base::Bind(
117 &TrackedPreferencesMigrationTest::RegisterSuccessfulWriteClosure,
118 base::Unretained(this),
119 MOCK_UNPROTECTED_PREF_STORE),
120 base::Bind(
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));
137 EXPECT_FALSE(
138 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE));
139 EXPECT_FALSE(
140 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE));
143 protected:
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
155 // earlier.
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;
170 switch (store_id) {
171 case MOCK_UNPROTECTED_PREF_STORE:
172 store = unprotected_prefs_.get();
173 pref_hash_store.reset(new PrefHashStoreImpl(kSeed, kDeviceId, false));
174 break;
175 case MOCK_PROTECTED_PREF_STORE:
176 store = protected_prefs_.get();
177 pref_hash_store.reset(new PrefHashStoreImpl(kSeed, kDeviceId, true));
178 break;
180 DCHECK(store);
182 base::StringValue string_value(value);
183 pref_hash_store->BeginTransaction(
184 scoped_ptr<HashStoreContents>(
185 new DictionaryHashStoreContents(store)))->StoreHash(
186 key, &string_value);
189 // Stores a hash for |key| and |value| in the legacy hash store in
190 // local_state.
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
204 // successful write.
205 bool WasOnSuccessfulWriteCallbackRegistered(MockPrefStoreID store_id) {
206 switch (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();
212 NOTREACHED();
213 return false;
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;
222 switch (store_id) {
223 case MOCK_UNPROTECTED_PREF_STORE:
224 store = unprotected_prefs_.get();
225 break;
226 case MOCK_PROTECTED_PREF_STORE:
227 store = protected_prefs_.get();
228 break;
230 DCHECK(store);
232 for (base::StringPairs::const_iterator it = expected_prefs_in_store.begin();
233 it != expected_prefs_in_store.end(); ++it) {
234 std::string val;
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;
245 switch (store_id) {
246 case MOCK_UNPROTECTED_PREF_STORE:
247 store = unprotected_prefs_.get();
248 break;
249 case MOCK_PROTECTED_PREF_STORE:
250 store = protected_prefs_.get();
251 break;
253 DCHECK(store);
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
262 // State hash store.
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
272 // in.
273 void HandPrefsToMigrator(MockPrefStoreID store_id) {
274 switch (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());
281 break;
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());
288 break;
292 bool HasPrefs(MockPrefStoreID store_id) {
293 switch (store_id) {
294 case MOCK_UNPROTECTED_PREF_STORE:
295 return unprotected_prefs_;
296 case MOCK_PROTECTED_PREF_STORE:
297 return protected_prefs_;
299 NOTREACHED();
300 return false;
303 bool StoreModifiedByMigration(MockPrefStoreID store_id) {
304 switch (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_;
310 NOTREACHED();
311 return false;
314 bool MigrationCompleted() {
315 return unprotected_store_migration_complete_ &&
316 protected_store_migration_complete_;
319 void SimulateSuccessfulWrite(MockPrefStoreID store_id) {
320 switch (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();
325 break;
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();
330 break;
334 private:
335 void RegisterSuccessfulWriteClosure(
336 MockPrefStoreID store_id,
337 const base::Closure& successful_write_closure) {
338 switch (store_id) {
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;
342 break;
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;
346 break;
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) {
355 switch (store_id) {
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;
361 break;
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;
367 break;
371 // Helper given as a cleaning callback to the migrator.
372 void RemovePathFromStore(MockPrefStoreID store_id, const std::string& key) {
373 switch (store_id) {
374 case MOCK_UNPROTECTED_PREF_STORE:
375 ASSERT_TRUE(unprotected_prefs_);
376 unprotected_prefs_->RemovePath(key, NULL);
377 break;
378 case MOCK_PROTECTED_PREF_STORE:
379 ASSERT_TRUE(protected_prefs_);
380 protected_prefs_->RemovePath(key, NULL);
381 break;
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;
391 switch (store_id) {
392 case MOCK_UNPROTECTED_PREF_STORE:
393 store = unprotected_prefs_.get();
394 break;
395 case MOCK_PROTECTED_PREF_STORE:
396 store = protected_prefs_.get();
397 break;
399 DCHECK(store);
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);
428 // static
429 const char TrackedPreferencesMigrationTest::kHashStoreId[] = "hash-store-id";
431 // static
432 const char TrackedPreferencesMigrationTest::kSeed[] = "seed";
434 // static
435 const char TrackedPreferencesMigrationTest::kDeviceId[] = "device-id";
437 } // namespace
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
452 // prefs.
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));
466 EXPECT_FALSE(
467 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE));
468 EXPECT_FALSE(
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
506 // prefs.
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.
522 EXPECT_FALSE(
523 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE));
524 EXPECT_FALSE(
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));
551 Reset();
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));
586 EXPECT_FALSE(
587 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
588 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref));
589 EXPECT_FALSE(
590 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyProtectedPref));
592 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref));
593 EXPECT_FALSE(
594 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
595 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref));
596 EXPECT_FALSE(
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
611 // MACs.
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));
627 EXPECT_TRUE(
628 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyProtectedPref));
629 EXPECT_TRUE(
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));
636 EXPECT_FALSE(
637 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
638 EXPECT_FALSE(
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));
648 EXPECT_TRUE(
649 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE));
650 EXPECT_TRUE(
651 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE));
653 SimulateSuccessfulWrite(MOCK_UNPROTECTED_PREF_STORE);
654 SimulateSuccessfulWrite(MOCK_PROTECTED_PREF_STORE);
656 Reset();
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.
664 EXPECT_FALSE(
665 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE));
666 EXPECT_FALSE(
667 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE));
669 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref));
670 EXPECT_FALSE(
671 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
672 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref));
673 EXPECT_TRUE(
674 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyProtectedPref));
676 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref));
677 EXPECT_TRUE(
678 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
679 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref));
680 EXPECT_FALSE(
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) {
693 PresetStoreValue(
694 MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref, kUnprotectedPrefValue);
695 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE,
696 kPreviouslyUnprotectedPref,
697 kPreviouslyUnprotectedPrefValue);
698 PresetStoreValue(
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));
705 EXPECT_TRUE(
706 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
707 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref));
708 EXPECT_FALSE(
709 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyProtectedPref));
711 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref));
712 EXPECT_FALSE(
713 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
714 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref));
715 EXPECT_TRUE(
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));
728 EXPECT_TRUE(
729 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE));
730 EXPECT_TRUE(
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));
758 EXPECT_TRUE(
759 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
760 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref));
761 EXPECT_TRUE(
762 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyProtectedPref));
764 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref));
765 EXPECT_TRUE(
766 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
767 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref));
768 EXPECT_TRUE(
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));
816 EXPECT_TRUE(
817 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
818 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref));
819 EXPECT_TRUE(
820 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyProtectedPref));
822 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref));
823 EXPECT_TRUE(
824 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
825 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref));
826 EXPECT_TRUE(
827 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyProtectedPref));
829 Reset();
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));
837 EXPECT_FALSE(
838 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
839 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref));
840 EXPECT_TRUE(
841 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyProtectedPref));
843 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref));
844 EXPECT_TRUE(
845 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
846 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref));
847 EXPECT_FALSE(
848 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyProtectedPref));
851 TEST_F(TrackedPreferencesMigrationTest, CleanupOnly) {
852 // Already migrated; only cleanup needed.
853 PresetStoreValue(
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);
861 PresetStoreValue(
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));
880 EXPECT_FALSE(
881 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE));
882 EXPECT_FALSE(
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);