tracked_preferences_migration_unittest.cc 26 KB

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