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

Side by Side Diff: components/user_prefs/tracked/tracked_preferences_migration_unittest.cc

Issue 2782803002: Move tracked prefs into services/preferences/tracked. (Closed)
Patch Set: rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "components/user_prefs/tracked/tracked_preferences_migration.h"
6
7 #include <memory>
8 #include <set>
9 #include <string>
10 #include <utility>
11 #include <vector>
12
13 #include "base/bind.h"
14 #include "base/callback.h"
15 #include "base/macros.h"
16 #include "base/strings/string_split.h"
17 #include "base/values.h"
18 #include "components/prefs/testing_pref_service.h"
19 #include "components/user_prefs/tracked/dictionary_hash_store_contents.h"
20 #include "components/user_prefs/tracked/hash_store_contents.h"
21 #include "components/user_prefs/tracked/interceptable_pref_filter.h"
22 #include "components/user_prefs/tracked/pref_hash_store.h"
23 #include "components/user_prefs/tracked/pref_hash_store_impl.h"
24 #include "components/user_prefs/tracked/pref_hash_store_transaction.h"
25 #include "components/user_prefs/tracked/tracked_preferences_migration.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27
28 namespace {
29
30 // An unprotected pref.
31 const char kUnprotectedPref[] = "unprotected";
32 // A protected pref.
33 const char kProtectedPref[] = "protected";
34 // A protected pref which is initially stored in the unprotected store.
35 const char kPreviouslyUnprotectedPref[] = "previously.unprotected";
36 // An unprotected pref which is initially stored in the protected store.
37 const char kPreviouslyProtectedPref[] = "previously.protected";
38
39 const char kUnprotectedPrefValue[] = "unprotected_value";
40 const char kProtectedPrefValue[] = "protected_value";
41 const char kPreviouslyUnprotectedPrefValue[] = "previously_unprotected_value";
42 const char kPreviouslyProtectedPrefValue[] = "previously_protected_value";
43
44 // A simple InterceptablePrefFilter which doesn't do anything but hand the prefs
45 // back downstream in FinalizeFilterOnLoad.
46 class SimpleInterceptablePrefFilter : public InterceptablePrefFilter {
47 public:
48 // PrefFilter remaining implementation.
49 void FilterUpdate(const std::string& path) override { ADD_FAILURE(); }
50 OnWriteCallbackPair FilterSerializeData(
51 base::DictionaryValue* pref_store_contents) override {
52 ADD_FAILURE();
53 return std::make_pair(base::Closure(),
54 base::Callback<void(bool success)>());
55 }
56
57 private:
58 // InterceptablePrefFilter implementation.
59 void FinalizeFilterOnLoad(
60 const PostFilterOnLoadCallback& post_filter_on_load_callback,
61 std::unique_ptr<base::DictionaryValue> pref_store_contents,
62 bool prefs_altered) override {
63 post_filter_on_load_callback.Run(std::move(pref_store_contents),
64 prefs_altered);
65 }
66 };
67
68 // A test fixture designed to be used like this:
69 // 1) Set up initial store prefs with PresetStoreValue().
70 // 2) Hand both sets of prefs to the migrator via HandPrefsToMigrator().
71 // 3) Migration completes synchronously when the second store hands its prefs
72 // over.
73 // 4) Verifications can be made via various methods of this fixture.
74 // Call Reset() to perform a second migration.
75 class TrackedPreferencesMigrationTest : public testing::Test {
76 public:
77 enum MockPrefStoreID {
78 MOCK_UNPROTECTED_PREF_STORE,
79 MOCK_PROTECTED_PREF_STORE,
80 };
81
82 TrackedPreferencesMigrationTest()
83 : unprotected_prefs_(new base::DictionaryValue),
84 protected_prefs_(new base::DictionaryValue),
85 migration_modified_unprotected_store_(false),
86 migration_modified_protected_store_(false),
87 unprotected_store_migration_complete_(false),
88 protected_store_migration_complete_(false) {}
89
90 void SetUp() override {
91 Reset();
92 }
93
94 void Reset() {
95 std::set<std::string> unprotected_pref_names;
96 std::set<std::string> protected_pref_names;
97 unprotected_pref_names.insert(kUnprotectedPref);
98 unprotected_pref_names.insert(kPreviouslyProtectedPref);
99 protected_pref_names.insert(kProtectedPref);
100 protected_pref_names.insert(kPreviouslyUnprotectedPref);
101
102 migration_modified_unprotected_store_ = false;
103 migration_modified_protected_store_ = false;
104 unprotected_store_migration_complete_ = false;
105 protected_store_migration_complete_ = false;
106
107 unprotected_store_successful_write_callback_.Reset();
108 protected_store_successful_write_callback_.Reset();
109
110 SetupTrackedPreferencesMigration(
111 unprotected_pref_names, protected_pref_names,
112 base::Bind(&TrackedPreferencesMigrationTest::RemovePathFromStore,
113 base::Unretained(this), MOCK_UNPROTECTED_PREF_STORE),
114 base::Bind(&TrackedPreferencesMigrationTest::RemovePathFromStore,
115 base::Unretained(this), MOCK_PROTECTED_PREF_STORE),
116 base::Bind(
117 &TrackedPreferencesMigrationTest::RegisterSuccessfulWriteClosure,
118 base::Unretained(this), MOCK_UNPROTECTED_PREF_STORE),
119 base::Bind(
120 &TrackedPreferencesMigrationTest::RegisterSuccessfulWriteClosure,
121 base::Unretained(this), MOCK_PROTECTED_PREF_STORE),
122 std::unique_ptr<PrefHashStore>(
123 new PrefHashStoreImpl(kSeed, kDeviceId, false)),
124 std::unique_ptr<PrefHashStore>(
125 new PrefHashStoreImpl(kSeed, kDeviceId, true)),
126 &mock_unprotected_pref_filter_, &mock_protected_pref_filter_);
127
128 // Verify initial expectations are met.
129 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE));
130 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE));
131 EXPECT_FALSE(
132 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE));
133 EXPECT_FALSE(
134 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE));
135 }
136
137 protected:
138 // Sets |key| to |value| in the test store identified by |store_id| before
139 // migration begins. Also sets the corresponding hash in the same store.
140 void PresetStoreValue(MockPrefStoreID store_id,
141 const std::string& key,
142 const std::string value) {
143 PresetStoreValueOnly(store_id, key, value);
144 PresetStoreValueHash(store_id, key, value);
145 }
146
147 // Stores a hash for |key| and |value| in the hash store identified by
148 // |store_id| before migration begins.
149 void PresetStoreValueHash(MockPrefStoreID store_id,
150 const std::string& key,
151 const std::string value) {
152 base::DictionaryValue* store = NULL;
153 std::unique_ptr<PrefHashStore> pref_hash_store;
154 switch (store_id) {
155 case MOCK_UNPROTECTED_PREF_STORE:
156 store = unprotected_prefs_.get();
157 pref_hash_store.reset(new PrefHashStoreImpl(kSeed, kDeviceId, false));
158 break;
159 case MOCK_PROTECTED_PREF_STORE:
160 store = protected_prefs_.get();
161 pref_hash_store.reset(new PrefHashStoreImpl(kSeed, kDeviceId, true));
162 break;
163 }
164 DCHECK(store);
165
166 base::Value string_value(value);
167 DictionaryHashStoreContents contents(store);
168 pref_hash_store->BeginTransaction(&contents)->StoreHash(key, &string_value);
169 }
170
171 // Returns true if the store opposite to |store_id| is observed for its next
172 // successful write.
173 bool WasOnSuccessfulWriteCallbackRegistered(MockPrefStoreID store_id) {
174 switch (store_id) {
175 case MOCK_UNPROTECTED_PREF_STORE:
176 return !protected_store_successful_write_callback_.is_null();
177 case MOCK_PROTECTED_PREF_STORE:
178 return !unprotected_store_successful_write_callback_.is_null();
179 }
180 NOTREACHED();
181 return false;
182 }
183
184 // Verifies that the (key, value) pairs in |expected_prefs_in_store| are found
185 // in the store identified by |store_id|.
186 void VerifyValuesStored(
187 MockPrefStoreID store_id,
188 const base::StringPairs& expected_prefs_in_store) {
189 base::DictionaryValue* store = NULL;
190 switch (store_id) {
191 case MOCK_UNPROTECTED_PREF_STORE:
192 store = unprotected_prefs_.get();
193 break;
194 case MOCK_PROTECTED_PREF_STORE:
195 store = protected_prefs_.get();
196 break;
197 }
198 DCHECK(store);
199
200 for (base::StringPairs::const_iterator it = expected_prefs_in_store.begin();
201 it != expected_prefs_in_store.end(); ++it) {
202 std::string val;
203 EXPECT_TRUE(store->GetString(it->first, &val));
204 EXPECT_EQ(it->second, val);
205 }
206 }
207
208 // Determines whether |expected_pref_in_hash_store| has a hash in the hash
209 // store identified by |store_id|.
210 bool ContainsHash(MockPrefStoreID store_id,
211 std::string expected_pref_in_hash_store) {
212 base::DictionaryValue* store = NULL;
213 switch (store_id) {
214 case MOCK_UNPROTECTED_PREF_STORE:
215 store = unprotected_prefs_.get();
216 break;
217 case MOCK_PROTECTED_PREF_STORE:
218 store = protected_prefs_.get();
219 break;
220 }
221 DCHECK(store);
222 const base::DictionaryValue* hash_store_contents =
223 DictionaryHashStoreContents(store).GetContents();
224 return hash_store_contents &&
225 hash_store_contents->GetString(expected_pref_in_hash_store,
226 static_cast<std::string*>(NULL));
227 }
228
229 // Both stores need to hand their prefs over in order for migration to kick
230 // in.
231 void HandPrefsToMigrator(MockPrefStoreID store_id) {
232 switch (store_id) {
233 case MOCK_UNPROTECTED_PREF_STORE:
234 mock_unprotected_pref_filter_.FilterOnLoad(
235 base::Bind(&TrackedPreferencesMigrationTest::GetPrefsBack,
236 base::Unretained(this), MOCK_UNPROTECTED_PREF_STORE),
237 std::move(unprotected_prefs_));
238 break;
239 case MOCK_PROTECTED_PREF_STORE:
240 mock_protected_pref_filter_.FilterOnLoad(
241 base::Bind(&TrackedPreferencesMigrationTest::GetPrefsBack,
242 base::Unretained(this), MOCK_PROTECTED_PREF_STORE),
243 std::move(protected_prefs_));
244 break;
245 }
246 }
247
248 bool HasPrefs(MockPrefStoreID store_id) {
249 switch (store_id) {
250 case MOCK_UNPROTECTED_PREF_STORE:
251 return !!unprotected_prefs_;
252 case MOCK_PROTECTED_PREF_STORE:
253 return !!protected_prefs_;
254 }
255 NOTREACHED();
256 return false;
257 }
258
259 bool StoreModifiedByMigration(MockPrefStoreID store_id) {
260 switch (store_id) {
261 case MOCK_UNPROTECTED_PREF_STORE:
262 return migration_modified_unprotected_store_;
263 case MOCK_PROTECTED_PREF_STORE:
264 return migration_modified_protected_store_;
265 }
266 NOTREACHED();
267 return false;
268 }
269
270 bool MigrationCompleted() {
271 return unprotected_store_migration_complete_ &&
272 protected_store_migration_complete_;
273 }
274
275 void SimulateSuccessfulWrite(MockPrefStoreID store_id) {
276 switch (store_id) {
277 case MOCK_UNPROTECTED_PREF_STORE:
278 EXPECT_FALSE(unprotected_store_successful_write_callback_.is_null());
279 unprotected_store_successful_write_callback_.Run();
280 unprotected_store_successful_write_callback_.Reset();
281 break;
282 case MOCK_PROTECTED_PREF_STORE:
283 EXPECT_FALSE(protected_store_successful_write_callback_.is_null());
284 protected_store_successful_write_callback_.Run();
285 protected_store_successful_write_callback_.Reset();
286 break;
287 }
288 }
289
290 private:
291 void RegisterSuccessfulWriteClosure(
292 MockPrefStoreID store_id,
293 const base::Closure& successful_write_closure) {
294 switch (store_id) {
295 case MOCK_UNPROTECTED_PREF_STORE:
296 EXPECT_TRUE(unprotected_store_successful_write_callback_.is_null());
297 unprotected_store_successful_write_callback_ = successful_write_closure;
298 break;
299 case MOCK_PROTECTED_PREF_STORE:
300 EXPECT_TRUE(protected_store_successful_write_callback_.is_null());
301 protected_store_successful_write_callback_ = successful_write_closure;
302 break;
303 }
304 }
305
306 // Helper given as an InterceptablePrefFilter::FinalizeFilterOnLoadCallback
307 // to the migrator to be invoked when it's done.
308 void GetPrefsBack(MockPrefStoreID store_id,
309 std::unique_ptr<base::DictionaryValue> prefs,
310 bool prefs_altered) {
311 switch (store_id) {
312 case MOCK_UNPROTECTED_PREF_STORE:
313 EXPECT_FALSE(unprotected_prefs_);
314 unprotected_prefs_ = std::move(prefs);
315 migration_modified_unprotected_store_ = prefs_altered;
316 unprotected_store_migration_complete_ = true;
317 break;
318 case MOCK_PROTECTED_PREF_STORE:
319 EXPECT_FALSE(protected_prefs_);
320 protected_prefs_ = std::move(prefs);
321 migration_modified_protected_store_ = prefs_altered;
322 protected_store_migration_complete_ = true;
323 break;
324 }
325 }
326
327 // Helper given as a cleaning callback to the migrator.
328 void RemovePathFromStore(MockPrefStoreID store_id, const std::string& key) {
329 switch (store_id) {
330 case MOCK_UNPROTECTED_PREF_STORE:
331 ASSERT_TRUE(unprotected_prefs_);
332 unprotected_prefs_->RemovePath(key, NULL);
333 break;
334 case MOCK_PROTECTED_PREF_STORE:
335 ASSERT_TRUE(protected_prefs_);
336 protected_prefs_->RemovePath(key, NULL);
337 break;
338 }
339 }
340
341 // Sets |key| to |value| in the test store identified by |store_id| before
342 // migration begins. Does not store a preference hash.
343 void PresetStoreValueOnly(MockPrefStoreID store_id,
344 const std::string& key,
345 const std::string value) {
346 base::DictionaryValue* store = NULL;
347 switch (store_id) {
348 case MOCK_UNPROTECTED_PREF_STORE:
349 store = unprotected_prefs_.get();
350 break;
351 case MOCK_PROTECTED_PREF_STORE:
352 store = protected_prefs_.get();
353 break;
354 }
355 DCHECK(store);
356
357 store->SetString(key, value);
358 }
359
360 static const char kSeed[];
361 static const char kDeviceId[];
362
363 std::unique_ptr<base::DictionaryValue> unprotected_prefs_;
364 std::unique_ptr<base::DictionaryValue> protected_prefs_;
365
366 SimpleInterceptablePrefFilter mock_unprotected_pref_filter_;
367 SimpleInterceptablePrefFilter mock_protected_pref_filter_;
368
369 base::Closure unprotected_store_successful_write_callback_;
370 base::Closure protected_store_successful_write_callback_;
371
372 bool migration_modified_unprotected_store_;
373 bool migration_modified_protected_store_;
374
375 bool unprotected_store_migration_complete_;
376 bool protected_store_migration_complete_;
377
378 TestingPrefServiceSimple local_state_;
379
380 DISALLOW_COPY_AND_ASSIGN(TrackedPreferencesMigrationTest);
381 };
382
383 // static
384 const char TrackedPreferencesMigrationTest::kSeed[] = "seed";
385
386 // static
387 const char TrackedPreferencesMigrationTest::kDeviceId[] = "device-id";
388
389 } // namespace
390
391 TEST_F(TrackedPreferencesMigrationTest, NoMigrationRequired) {
392 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref,
393 kUnprotectedPrefValue);
394 PresetStoreValue(MOCK_PROTECTED_PREF_STORE, kProtectedPref,
395 kProtectedPrefValue);
396
397 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref));
398 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref));
399
400 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref));
401 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref));
402
403 // Hand unprotected prefs to the migrator which should wait for the protected
404 // prefs.
405 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE);
406 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE));
407 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE));
408 EXPECT_FALSE(MigrationCompleted());
409
410 // Hand protected prefs to the migrator which should proceed with the
411 // migration synchronously.
412 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE);
413 EXPECT_TRUE(MigrationCompleted());
414
415 // Prefs should have been handed back over.
416 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE));
417 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE));
418 EXPECT_FALSE(
419 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE));
420 EXPECT_FALSE(
421 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE));
422 EXPECT_FALSE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE));
423 EXPECT_FALSE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE));
424
425 base::StringPairs expected_unprotected_values;
426 expected_unprotected_values.push_back(
427 std::make_pair(kUnprotectedPref, kUnprotectedPrefValue));
428 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE, expected_unprotected_values);
429
430 base::StringPairs expected_protected_values;
431 expected_protected_values.push_back(
432 std::make_pair(kProtectedPref, kProtectedPrefValue));
433 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE, expected_protected_values);
434
435 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref));
436 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref));
437
438 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref));
439 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref));
440 }
441
442 TEST_F(TrackedPreferencesMigrationTest, FullMigration) {
443 PresetStoreValue(
444 MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref, kUnprotectedPrefValue);
445 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE,
446 kPreviouslyUnprotectedPref,
447 kPreviouslyUnprotectedPrefValue);
448 PresetStoreValue(
449 MOCK_PROTECTED_PREF_STORE, kProtectedPref, kProtectedPrefValue);
450 PresetStoreValue(MOCK_PROTECTED_PREF_STORE,
451 kPreviouslyProtectedPref,
452 kPreviouslyProtectedPrefValue);
453
454 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref));
455 EXPECT_TRUE(
456 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
457 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref));
458 EXPECT_FALSE(
459 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyProtectedPref));
460
461 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref));
462 EXPECT_FALSE(
463 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
464 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref));
465 EXPECT_TRUE(
466 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyProtectedPref));
467
468 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE);
469 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE));
470 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE));
471 EXPECT_FALSE(MigrationCompleted());
472
473 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE);
474 EXPECT_TRUE(MigrationCompleted());
475
476 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE));
477 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE));
478 EXPECT_TRUE(
479 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE));
480 EXPECT_TRUE(
481 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE));
482 EXPECT_TRUE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE));
483 EXPECT_TRUE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE));
484
485 // Values should have been migrated to their store, but migrated values should
486 // still remain in the source store until cleanup tasks are later invoked.
487 {
488 base::StringPairs expected_unprotected_values;
489 expected_unprotected_values.push_back(std::make_pair(
490 kUnprotectedPref, kUnprotectedPrefValue));
491 expected_unprotected_values.push_back(std::make_pair(
492 kPreviouslyProtectedPref, kPreviouslyProtectedPrefValue));
493 expected_unprotected_values.push_back(std::make_pair(
494 kPreviouslyUnprotectedPref, kPreviouslyUnprotectedPrefValue));
495 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE,
496 expected_unprotected_values);
497
498 base::StringPairs expected_protected_values;
499 expected_protected_values.push_back(std::make_pair(
500 kProtectedPref, kProtectedPrefValue));
501 expected_protected_values.push_back(std::make_pair(
502 kPreviouslyUnprotectedPref, kPreviouslyUnprotectedPrefValue));
503 expected_unprotected_values.push_back(std::make_pair(
504 kPreviouslyProtectedPref, kPreviouslyProtectedPrefValue));
505 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE, expected_protected_values);
506
507 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref));
508 EXPECT_TRUE(
509 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
510 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref));
511 EXPECT_TRUE(
512 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyProtectedPref));
513
514 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref));
515 EXPECT_TRUE(
516 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
517 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref));
518 EXPECT_TRUE(
519 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyProtectedPref));
520 }
521
522 // A successful write of the protected pref store should result in a clean up
523 // of the unprotected store.
524 SimulateSuccessfulWrite(MOCK_PROTECTED_PREF_STORE);
525
526 {
527 base::StringPairs expected_unprotected_values;
528 expected_unprotected_values.push_back(std::make_pair(
529 kUnprotectedPref, kUnprotectedPrefValue));
530 expected_unprotected_values.push_back(std::make_pair(
531 kPreviouslyProtectedPref, kPreviouslyProtectedPrefValue));
532 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE,
533 expected_unprotected_values);
534
535 base::StringPairs expected_protected_values;
536 expected_protected_values.push_back(std::make_pair(
537 kProtectedPref, kProtectedPrefValue));
538 expected_protected_values.push_back(std::make_pair(
539 kPreviouslyUnprotectedPref, kPreviouslyUnprotectedPrefValue));
540 expected_unprotected_values.push_back(std::make_pair(
541 kPreviouslyProtectedPref, kPreviouslyProtectedPrefValue));
542 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE, expected_protected_values);
543 }
544
545 SimulateSuccessfulWrite(MOCK_UNPROTECTED_PREF_STORE);
546
547 {
548 base::StringPairs expected_unprotected_values;
549 expected_unprotected_values.push_back(std::make_pair(
550 kUnprotectedPref, kUnprotectedPrefValue));
551 expected_unprotected_values.push_back(std::make_pair(
552 kPreviouslyProtectedPref, kPreviouslyProtectedPrefValue));
553 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE,
554 expected_unprotected_values);
555
556 base::StringPairs expected_protected_values;
557 expected_protected_values.push_back(std::make_pair(
558 kProtectedPref, kProtectedPrefValue));
559 expected_protected_values.push_back(std::make_pair(
560 kPreviouslyUnprotectedPref, kPreviouslyUnprotectedPrefValue));
561 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE, expected_protected_values);
562 }
563
564 // Hashes are not cleaned up yet.
565 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref));
566 EXPECT_TRUE(
567 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
568 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref));
569 EXPECT_TRUE(
570 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyProtectedPref));
571
572 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref));
573 EXPECT_TRUE(
574 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
575 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref));
576 EXPECT_TRUE(
577 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyProtectedPref));
578
579 Reset();
580
581 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE);
582 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE);
583 EXPECT_TRUE(MigrationCompleted());
584
585 // Hashes are cleaned up.
586 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref));
587 EXPECT_FALSE(
588 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
589 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref));
590 EXPECT_TRUE(
591 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyProtectedPref));
592
593 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref));
594 EXPECT_TRUE(
595 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyUnprotectedPref));
596 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref));
597 EXPECT_FALSE(
598 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyProtectedPref));
599 }
600
601 TEST_F(TrackedPreferencesMigrationTest, CleanupOnly) {
602 // Already migrated; only cleanup needed.
603 PresetStoreValue(
604 MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref, kUnprotectedPrefValue);
605 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE,
606 kPreviouslyProtectedPref,
607 kPreviouslyProtectedPrefValue);
608 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE,
609 kPreviouslyUnprotectedPref,
610 kPreviouslyUnprotectedPrefValue);
611 PresetStoreValue(
612 MOCK_PROTECTED_PREF_STORE, kProtectedPref, kProtectedPrefValue);
613 PresetStoreValue(MOCK_PROTECTED_PREF_STORE,
614 kPreviouslyProtectedPref,
615 kPreviouslyProtectedPrefValue);
616 PresetStoreValue(MOCK_PROTECTED_PREF_STORE,
617 kPreviouslyUnprotectedPref,
618 kPreviouslyUnprotectedPrefValue);
619
620 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE);
621 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE));
622 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE));
623 EXPECT_FALSE(MigrationCompleted());
624
625 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE);
626 EXPECT_TRUE(MigrationCompleted());
627
628 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE));
629 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE));
630 EXPECT_FALSE(
631 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE));
632 EXPECT_FALSE(
633 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE));
634 EXPECT_FALSE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE));
635 EXPECT_FALSE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE));
636
637 // Cleanup should happen synchronously if the values were already present in
638 // their destination stores.
639 {
640 base::StringPairs expected_unprotected_values;
641 expected_unprotected_values.push_back(std::make_pair(
642 kUnprotectedPref, kUnprotectedPrefValue));
643 expected_unprotected_values.push_back(std::make_pair(
644 kPreviouslyProtectedPref, kPreviouslyProtectedPrefValue));
645 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE,
646 expected_unprotected_values);
647
648 base::StringPairs expected_protected_values;
649 expected_protected_values.push_back(std::make_pair(
650 kProtectedPref, kProtectedPrefValue));
651 expected_protected_values.push_back(std::make_pair(
652 kPreviouslyUnprotectedPref, kPreviouslyUnprotectedPrefValue));
653 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE, expected_protected_values);
654 }
655 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698