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 "chrome/browser/prefs/tracked/tracked_preferences_migration.h" 6 7 #include <set> 8 #include <string> 9 #include <vector> 10 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" 26 27 namespace { 28 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"; 37 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"; 42 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 virtual void FilterUpdate(const std::string& path) OVERRIDE { 49 ADD_FAILURE(); 50 } 51 virtual void FilterSerializeData( 52 base::DictionaryValue* pref_store_contents) OVERRIDE { 53 ADD_FAILURE(); 54 } 55 56 private: 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); 63 } 64 }; 65 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 70 // over. 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 { 74 public: 75 enum MockPrefStoreID { 76 MOCK_UNPROTECTED_PREF_STORE, 77 MOCK_PROTECTED_PREF_STORE, 78 }; 79 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) {} 87 88 virtual void SetUp() OVERRIDE { 89 ProfilePrefStoreManager::RegisterPrefs(local_state_.registry()); 90 Reset(); 91 } 92 93 void Reset() { 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); 100 101 migration_modified_unprotected_store_ = false; 102 migration_modified_protected_store_ = false; 103 unprotected_store_migration_complete_ = false; 104 protected_store_migration_complete_ = false; 105 106 unprotected_store_successful_write_callback_.Reset(); 107 protected_store_successful_write_callback_.Reset(); 108 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), 118 base::Bind( 119 &TrackedPreferencesMigrationTest::RegisterSuccessfulWriteClosure, 120 base::Unretained(this), 121 MOCK_UNPROTECTED_PREF_STORE), 122 base::Bind( 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("hash-store-id", &local_state_)), 132 133 &mock_unprotected_pref_filter_, 134 &mock_protected_pref_filter_); 135 136 // Verify initial expectations are met. 137 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE)); 138 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE)); 139 EXPECT_FALSE( 140 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE)); 141 EXPECT_FALSE( 142 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE)); 143 } 144 145 protected: 146 // Sets |key| to |value| in the test store identified by |store_id| before 147 // migration begins. 148 void PresetStoreValue(MockPrefStoreID store_id, 149 const std::string& key, 150 const std::string value) { 151 base::DictionaryValue* store = NULL; 152 scoped_ptr<PrefHashStore> pref_hash_store; 153 switch (store_id) { 154 case MOCK_UNPROTECTED_PREF_STORE: 155 store = unprotected_prefs_.get(); 156 pref_hash_store.reset(new PrefHashStoreImpl(kSeed, kDeviceId, false)); 157 break; 158 case MOCK_PROTECTED_PREF_STORE: 159 store = protected_prefs_.get(); 160 pref_hash_store.reset(new PrefHashStoreImpl(kSeed, kDeviceId, true)); 161 break; 162 } 163 DCHECK(store); 164 165 store->SetString(key, value); 166 base::StringValue string_value(value); 167 pref_hash_store->BeginTransaction( 168 scoped_ptr<HashStoreContents>( 169 new DictionaryHashStoreContents(store)))->StoreHash( 170 key, &string_value); 171 } 172 173 // Returns true if the store opposite to |store_id| is observed for its next 174 // successful write. 175 bool WasOnSuccessfulWriteCallbackRegistered(MockPrefStoreID store_id) { 176 switch (store_id) { 177 case MOCK_UNPROTECTED_PREF_STORE: 178 return !protected_store_successful_write_callback_.is_null(); 179 case MOCK_PROTECTED_PREF_STORE: 180 return !unprotected_store_successful_write_callback_.is_null(); 181 } 182 NOTREACHED(); 183 return false; 184 } 185 186 // Verifies that the (key, value) pairs in |expected_prefs_in_store| are found 187 // in the store identified by |store_id|. 188 void VerifyValuesStored( 189 MockPrefStoreID store_id, 190 const std::vector<std::pair<std::string, std::string> >& 191 expected_prefs_in_store) { 192 base::DictionaryValue* store = NULL; 193 switch (store_id) { 194 case MOCK_UNPROTECTED_PREF_STORE: 195 store = unprotected_prefs_.get(); 196 break; 197 case MOCK_PROTECTED_PREF_STORE: 198 store = protected_prefs_.get(); 199 break; 200 } 201 DCHECK(store); 202 203 for (std::vector<std::pair<std::string, std::string> >::const_iterator it = 204 expected_prefs_in_store.begin(); 205 it != expected_prefs_in_store.end(); ++it) { 206 std::string val; 207 EXPECT_TRUE(store->GetString(it->first, &val)); 208 EXPECT_EQ(it->second, val); 209 } 210 } 211 212 // Determines whether |expected_pref_in_hash_store| has a hash in the hash 213 // store identified by |store_id|. 214 bool ContainsHash(MockPrefStoreID store_id, 215 std::string expected_pref_in_hash_store) { 216 base::DictionaryValue* store = NULL; 217 switch (store_id) { 218 case MOCK_UNPROTECTED_PREF_STORE: 219 store = unprotected_prefs_.get(); 220 break; 221 case MOCK_PROTECTED_PREF_STORE: 222 store = protected_prefs_.get(); 223 break; 224 } 225 DCHECK(store); 226 return DictionaryHashStoreContents(store).GetContents()->GetString( 227 expected_pref_in_hash_store, static_cast<std::string*>(NULL)); 228 } 229 230 // Both stores need to hand their prefs over in order for migration to kick 231 // in. 232 void HandPrefsToMigrator(MockPrefStoreID store_id) { 233 switch (store_id) { 234 case MOCK_UNPROTECTED_PREF_STORE: 235 mock_unprotected_pref_filter_.FilterOnLoad( 236 base::Bind(&TrackedPreferencesMigrationTest::GetPrefsBack, 237 base::Unretained(this), 238 MOCK_UNPROTECTED_PREF_STORE), 239 unprotected_prefs_.Pass()); 240 break; 241 case MOCK_PROTECTED_PREF_STORE: 242 mock_protected_pref_filter_.FilterOnLoad( 243 base::Bind(&TrackedPreferencesMigrationTest::GetPrefsBack, 244 base::Unretained(this), 245 MOCK_PROTECTED_PREF_STORE), 246 protected_prefs_.Pass()); 247 break; 248 } 249 } 250 251 bool HasPrefs(MockPrefStoreID store_id) { 252 switch (store_id) { 253 case MOCK_UNPROTECTED_PREF_STORE: 254 return unprotected_prefs_; 255 case MOCK_PROTECTED_PREF_STORE: 256 return protected_prefs_; 257 } 258 NOTREACHED(); 259 return false; 260 } 261 262 bool StoreModifiedByMigration(MockPrefStoreID store_id) { 263 switch (store_id) { 264 case MOCK_UNPROTECTED_PREF_STORE: 265 return migration_modified_unprotected_store_; 266 case MOCK_PROTECTED_PREF_STORE: 267 return migration_modified_protected_store_; 268 } 269 NOTREACHED(); 270 return false; 271 } 272 273 bool MigrationCompleted() { 274 return unprotected_store_migration_complete_ && 275 protected_store_migration_complete_; 276 } 277 278 void SimulateSuccessfulWrite(MockPrefStoreID store_id) { 279 switch (store_id) { 280 case MOCK_UNPROTECTED_PREF_STORE: 281 EXPECT_FALSE(unprotected_store_successful_write_callback_.is_null()); 282 unprotected_store_successful_write_callback_.Run(); 283 unprotected_store_successful_write_callback_.Reset(); 284 break; 285 case MOCK_PROTECTED_PREF_STORE: 286 EXPECT_FALSE(protected_store_successful_write_callback_.is_null()); 287 protected_store_successful_write_callback_.Run(); 288 protected_store_successful_write_callback_.Reset(); 289 break; 290 } 291 } 292 293 private: 294 void RegisterSuccessfulWriteClosure( 295 MockPrefStoreID store_id, 296 const base::Closure& successful_write_closure) { 297 switch (store_id) { 298 case MOCK_UNPROTECTED_PREF_STORE: 299 EXPECT_TRUE(unprotected_store_successful_write_callback_.is_null()); 300 unprotected_store_successful_write_callback_ = successful_write_closure; 301 break; 302 case MOCK_PROTECTED_PREF_STORE: 303 EXPECT_TRUE(protected_store_successful_write_callback_.is_null()); 304 protected_store_successful_write_callback_ = successful_write_closure; 305 break; 306 } 307 } 308 309 // Helper given as an InterceptablePrefFilter::FinalizeFilterOnLoadCallback 310 // to the migrator to be invoked when it's done. 311 void GetPrefsBack(MockPrefStoreID store_id, 312 scoped_ptr<base::DictionaryValue> prefs, 313 bool prefs_altered) { 314 switch (store_id) { 315 case MOCK_UNPROTECTED_PREF_STORE: 316 EXPECT_FALSE(unprotected_prefs_); 317 unprotected_prefs_ = prefs.Pass(); 318 migration_modified_unprotected_store_ = prefs_altered; 319 unprotected_store_migration_complete_ = true; 320 break; 321 case MOCK_PROTECTED_PREF_STORE: 322 EXPECT_FALSE(protected_prefs_); 323 protected_prefs_ = prefs.Pass(); 324 migration_modified_protected_store_ = prefs_altered; 325 protected_store_migration_complete_ = true; 326 break; 327 } 328 } 329 330 // Helper given as a cleaning callback to the migrator. 331 void RemovePathFromStore(MockPrefStoreID store_id, const std::string& key) { 332 switch (store_id) { 333 case MOCK_UNPROTECTED_PREF_STORE: 334 ASSERT_TRUE(unprotected_prefs_); 335 unprotected_prefs_->RemovePath(key, NULL); 336 break; 337 case MOCK_PROTECTED_PREF_STORE: 338 ASSERT_TRUE(protected_prefs_); 339 protected_prefs_->RemovePath(key, NULL); 340 break; 341 } 342 } 343 344 static const char kSeed[]; 345 static const char kDeviceId[]; 346 347 scoped_ptr<base::DictionaryValue> unprotected_prefs_; 348 scoped_ptr<base::DictionaryValue> protected_prefs_; 349 350 SimpleInterceptablePrefFilter mock_unprotected_pref_filter_; 351 SimpleInterceptablePrefFilter mock_protected_pref_filter_; 352 353 base::Closure unprotected_store_successful_write_callback_; 354 base::Closure protected_store_successful_write_callback_; 355 356 bool migration_modified_unprotected_store_; 357 bool migration_modified_protected_store_; 358 359 bool unprotected_store_migration_complete_; 360 bool protected_store_migration_complete_; 361 362 TestingPrefServiceSimple local_state_; 363 }; 364 365 // static 366 const char TrackedPreferencesMigrationTest::kSeed[] = "seed"; 367 368 // static 369 const char TrackedPreferencesMigrationTest::kDeviceId[] = "device-id"; 370 371 } // namespace 372 373 TEST_F(TrackedPreferencesMigrationTest, NoMigrationRequired) { 374 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref, 375 kUnprotectedPrefValue); 376 PresetStoreValue(MOCK_PROTECTED_PREF_STORE, kProtectedPref, 377 kProtectedPrefValue); 378 379 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref)); 380 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref)); 381 382 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref)); 383 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref)); 384 385 // Hand unprotected prefs to the migrator which should wait for the protected 386 // prefs. 387 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE); 388 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE)); 389 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE)); 390 EXPECT_FALSE(MigrationCompleted()); 391 392 // Hand protected prefs to the migrator which should proceed with the 393 // migration synchronously. 394 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE); 395 EXPECT_TRUE(MigrationCompleted()); 396 397 // Prefs should have been handed back over. 398 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE)); 399 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE)); 400 EXPECT_FALSE( 401 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE)); 402 EXPECT_FALSE( 403 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE)); 404 EXPECT_FALSE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE)); 405 EXPECT_FALSE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE)); 406 407 std::vector<std::pair<std::string, std::string> > expected_unprotected_values; 408 expected_unprotected_values.push_back( 409 std::make_pair(kUnprotectedPref, kUnprotectedPrefValue)); 410 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE, expected_unprotected_values); 411 412 std::vector<std::pair<std::string, std::string> > expected_protected_values; 413 expected_protected_values.push_back( 414 std::make_pair(kProtectedPref, kProtectedPrefValue)); 415 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE, expected_protected_values); 416 417 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref)); 418 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref)); 419 420 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref)); 421 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref)); 422 } 423 424 TEST_F(TrackedPreferencesMigrationTest, FullMigration) { 425 PresetStoreValue( 426 MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref, kUnprotectedPrefValue); 427 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE, 428 kPreviouslyUnprotectedPref, 429 kPreviouslyUnprotectedPrefValue); 430 PresetStoreValue( 431 MOCK_PROTECTED_PREF_STORE, kProtectedPref, kProtectedPrefValue); 432 PresetStoreValue(MOCK_PROTECTED_PREF_STORE, 433 kPreviouslyProtectedPref, 434 kPreviouslyProtectedPrefValue); 435 436 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref)); 437 EXPECT_TRUE( 438 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyUnprotectedPref)); 439 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref)); 440 EXPECT_FALSE( 441 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyProtectedPref)); 442 443 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref)); 444 EXPECT_FALSE( 445 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyUnprotectedPref)); 446 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref)); 447 EXPECT_TRUE( 448 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyProtectedPref)); 449 450 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE); 451 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE)); 452 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE)); 453 EXPECT_FALSE(MigrationCompleted()); 454 455 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE); 456 EXPECT_TRUE(MigrationCompleted()); 457 458 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE)); 459 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE)); 460 EXPECT_TRUE( 461 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE)); 462 EXPECT_TRUE( 463 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE)); 464 EXPECT_TRUE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE)); 465 EXPECT_TRUE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE)); 466 467 // Values should have been migrated to their store, but migrated values should 468 // still remain in the source store until cleanup tasks are later invoked. 469 { 470 std::vector<std::pair<std::string, std::string> > 471 expected_unprotected_values; 472 expected_unprotected_values.push_back(std::make_pair( 473 kUnprotectedPref, kUnprotectedPrefValue)); 474 expected_unprotected_values.push_back(std::make_pair( 475 kPreviouslyProtectedPref, kPreviouslyProtectedPrefValue)); 476 expected_unprotected_values.push_back(std::make_pair( 477 kPreviouslyUnprotectedPref, kPreviouslyUnprotectedPrefValue)); 478 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE, 479 expected_unprotected_values); 480 481 std::vector<std::pair<std::string, std::string> > expected_protected_values; 482 expected_protected_values.push_back(std::make_pair( 483 kProtectedPref, kProtectedPrefValue)); 484 expected_protected_values.push_back(std::make_pair( 485 kPreviouslyUnprotectedPref, kPreviouslyUnprotectedPrefValue)); 486 expected_unprotected_values.push_back(std::make_pair( 487 kPreviouslyProtectedPref, kPreviouslyProtectedPrefValue)); 488 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE, expected_protected_values); 489 490 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref)); 491 EXPECT_TRUE( 492 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyUnprotectedPref)); 493 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref)); 494 EXPECT_TRUE( 495 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyProtectedPref)); 496 497 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref)); 498 EXPECT_TRUE( 499 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyUnprotectedPref)); 500 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref)); 501 EXPECT_TRUE( 502 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyProtectedPref)); 503 } 504 505 // A successful write of the protected pref store should result in a clean up 506 // of the unprotected store. 507 SimulateSuccessfulWrite(MOCK_PROTECTED_PREF_STORE); 508 509 { 510 std::vector<std::pair<std::string, std::string> > 511 expected_unprotected_values; 512 expected_unprotected_values.push_back(std::make_pair( 513 kUnprotectedPref, kUnprotectedPrefValue)); 514 expected_unprotected_values.push_back(std::make_pair( 515 kPreviouslyProtectedPref, kPreviouslyProtectedPrefValue)); 516 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE, 517 expected_unprotected_values); 518 519 std::vector<std::pair<std::string, std::string> > expected_protected_values; 520 expected_protected_values.push_back(std::make_pair( 521 kProtectedPref, kProtectedPrefValue)); 522 expected_protected_values.push_back(std::make_pair( 523 kPreviouslyUnprotectedPref, kPreviouslyUnprotectedPrefValue)); 524 expected_unprotected_values.push_back(std::make_pair( 525 kPreviouslyProtectedPref, kPreviouslyProtectedPrefValue)); 526 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE, expected_protected_values); 527 } 528 529 SimulateSuccessfulWrite(MOCK_UNPROTECTED_PREF_STORE); 530 531 { 532 std::vector<std::pair<std::string, std::string> > 533 expected_unprotected_values; 534 expected_unprotected_values.push_back(std::make_pair( 535 kUnprotectedPref, kUnprotectedPrefValue)); 536 expected_unprotected_values.push_back(std::make_pair( 537 kPreviouslyProtectedPref, kPreviouslyProtectedPrefValue)); 538 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE, 539 expected_unprotected_values); 540 541 std::vector<std::pair<std::string, std::string> > expected_protected_values; 542 expected_protected_values.push_back(std::make_pair( 543 kProtectedPref, kProtectedPrefValue)); 544 expected_protected_values.push_back(std::make_pair( 545 kPreviouslyUnprotectedPref, kPreviouslyUnprotectedPrefValue)); 546 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE, expected_protected_values); 547 } 548 549 // Hashes are not cleaned up yet. 550 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref)); 551 EXPECT_TRUE( 552 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyUnprotectedPref)); 553 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref)); 554 EXPECT_TRUE( 555 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyProtectedPref)); 556 557 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref)); 558 EXPECT_TRUE( 559 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyUnprotectedPref)); 560 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref)); 561 EXPECT_TRUE( 562 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyProtectedPref)); 563 564 Reset(); 565 566 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE); 567 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE); 568 EXPECT_TRUE(MigrationCompleted()); 569 570 // Hashes are cleaned up. 571 EXPECT_TRUE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref)); 572 EXPECT_FALSE( 573 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyUnprotectedPref)); 574 EXPECT_FALSE(ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kProtectedPref)); 575 EXPECT_TRUE( 576 ContainsHash(MOCK_UNPROTECTED_PREF_STORE, kPreviouslyProtectedPref)); 577 578 EXPECT_FALSE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kUnprotectedPref)); 579 EXPECT_TRUE( 580 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyUnprotectedPref)); 581 EXPECT_TRUE(ContainsHash(MOCK_PROTECTED_PREF_STORE, kProtectedPref)); 582 EXPECT_FALSE( 583 ContainsHash(MOCK_PROTECTED_PREF_STORE, kPreviouslyProtectedPref)); 584 } 585 586 TEST_F(TrackedPreferencesMigrationTest, CleanupOnly) { 587 // Already migrated; only cleanup needed. 588 PresetStoreValue( 589 MOCK_UNPROTECTED_PREF_STORE, kUnprotectedPref, kUnprotectedPrefValue); 590 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE, 591 kPreviouslyProtectedPref, 592 kPreviouslyProtectedPrefValue); 593 PresetStoreValue(MOCK_UNPROTECTED_PREF_STORE, 594 kPreviouslyUnprotectedPref, 595 kPreviouslyUnprotectedPrefValue); 596 PresetStoreValue( 597 MOCK_PROTECTED_PREF_STORE, kProtectedPref, kProtectedPrefValue); 598 PresetStoreValue(MOCK_PROTECTED_PREF_STORE, 599 kPreviouslyProtectedPref, 600 kPreviouslyProtectedPrefValue); 601 PresetStoreValue(MOCK_PROTECTED_PREF_STORE, 602 kPreviouslyUnprotectedPref, 603 kPreviouslyUnprotectedPrefValue); 604 605 HandPrefsToMigrator(MOCK_UNPROTECTED_PREF_STORE); 606 EXPECT_FALSE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE)); 607 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE)); 608 EXPECT_FALSE(MigrationCompleted()); 609 610 HandPrefsToMigrator(MOCK_PROTECTED_PREF_STORE); 611 EXPECT_TRUE(MigrationCompleted()); 612 613 EXPECT_TRUE(HasPrefs(MOCK_UNPROTECTED_PREF_STORE)); 614 EXPECT_TRUE(HasPrefs(MOCK_PROTECTED_PREF_STORE)); 615 EXPECT_FALSE( 616 WasOnSuccessfulWriteCallbackRegistered(MOCK_UNPROTECTED_PREF_STORE)); 617 EXPECT_FALSE( 618 WasOnSuccessfulWriteCallbackRegistered(MOCK_PROTECTED_PREF_STORE)); 619 EXPECT_FALSE(StoreModifiedByMigration(MOCK_UNPROTECTED_PREF_STORE)); 620 EXPECT_FALSE(StoreModifiedByMigration(MOCK_PROTECTED_PREF_STORE)); 621 622 // Cleanup should happen synchronously if the values were already present in 623 // their destination stores. 624 { 625 std::vector<std::pair<std::string, std::string> > 626 expected_unprotected_values; 627 expected_unprotected_values.push_back(std::make_pair( 628 kUnprotectedPref, kUnprotectedPrefValue)); 629 expected_unprotected_values.push_back(std::make_pair( 630 kPreviouslyProtectedPref, kPreviouslyProtectedPrefValue)); 631 VerifyValuesStored(MOCK_UNPROTECTED_PREF_STORE, 632 expected_unprotected_values); 633 634 std::vector<std::pair<std::string, std::string> > expected_protected_values; 635 expected_protected_values.push_back(std::make_pair( 636 kProtectedPref, kProtectedPrefValue)); 637 expected_protected_values.push_back(std::make_pair( 638 kPreviouslyUnprotectedPref, kPreviouslyUnprotectedPrefValue)); 639 VerifyValuesStored(MOCK_PROTECTED_PREF_STORE, expected_protected_values); 640 } 641 } 642