1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/prefs/pref_hash_store_impl.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/prefs/scoped_user_pref_update.h"
11 #include "base/prefs/testing_pref_service.h"
12 #include "base/values.h"
13 #include "chrome/browser/prefs/pref_hash_store.h"
14 #include "chrome/common/pref_names.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 TEST(PrefHashStoreImplTest
, TestCase
) {
18 base::StringValue
string_1("string1");
19 base::StringValue
string_2("string2");
21 TestingPrefServiceSimple local_state
;
22 PrefHashStoreImpl::RegisterPrefs(local_state
.registry());
24 // 32 NULL bytes is the seed that was used to generate the legacy hash.
25 PrefHashStoreImpl
pref_hash_store(
26 "store_id", std::string(32,0), "device_id", &local_state
);
28 // Only NULL should be trusted in the absence of a hash.
29 EXPECT_EQ(PrefHashStore::UNTRUSTED_UNKNOWN_VALUE
,
30 pref_hash_store
.CheckValue("path1", &string_1
));
31 EXPECT_EQ(PrefHashStore::TRUSTED_UNKNOWN_VALUE
,
32 pref_hash_store
.CheckValue("path1", NULL
));
34 pref_hash_store
.StoreHash("path1", &string_1
);
35 EXPECT_EQ(PrefHashStore::UNCHANGED
,
36 pref_hash_store
.CheckValue("path1", &string_1
));
37 EXPECT_EQ(PrefHashStore::CLEARED
, pref_hash_store
.CheckValue("path1", NULL
));
38 pref_hash_store
.StoreHash("path1", NULL
);
39 EXPECT_EQ(PrefHashStore::UNCHANGED
,
40 pref_hash_store
.CheckValue("path1", NULL
));
41 EXPECT_EQ(PrefHashStore::CHANGED
,
42 pref_hash_store
.CheckValue("path1", &string_2
));
44 base::DictionaryValue dict
;
45 dict
.Set("a", new base::StringValue("foo"));
46 dict
.Set("d", new base::StringValue("bad"));
47 dict
.Set("b", new base::StringValue("bar"));
48 dict
.Set("c", new base::StringValue("baz"));
51 // Manually shove in a legacy hash.
52 DictionaryPrefUpdate
update(&local_state
, prefs::kProfilePreferenceHashes
);
53 base::DictionaryValue
* child_dictionary
= NULL
;
54 ASSERT_TRUE(update
->GetDictionary("store_id", &child_dictionary
));
55 child_dictionary
->SetString(
57 "C503FB7C65EEFD5C07185F616A0AA67923C069909933F362022B1F187E73E9A2");
59 EXPECT_EQ(PrefHashStore::MIGRATED
,
60 pref_hash_store
.CheckValue("path1", &dict
));
61 pref_hash_store
.StoreHash("path1", &dict
);
62 EXPECT_EQ(PrefHashStore::UNCHANGED
,
63 pref_hash_store
.CheckValue("path1", &dict
));
65 // |pref_hash_store2| should trust its initial hashes dictionary and thus
66 // trust new unknown values.
67 PrefHashStoreImpl
pref_hash_store2(
68 "store_id", std::string(32, 0), "device_id", &local_state
);
69 EXPECT_EQ(PrefHashStore::TRUSTED_UNKNOWN_VALUE
,
70 pref_hash_store2
.CheckValue("new_path", &string_1
));
71 EXPECT_EQ(PrefHashStore::TRUSTED_UNKNOWN_VALUE
,
72 pref_hash_store2
.CheckValue("new_path", &string_2
));
73 EXPECT_EQ(PrefHashStore::TRUSTED_UNKNOWN_VALUE
,
74 pref_hash_store2
.CheckValue("new_path", NULL
));
77 // Manually corrupt the hash of hashes for "store_id".
78 DictionaryPrefUpdate
update(&local_state
, prefs::kProfilePreferenceHashes
);
79 base::DictionaryValue
* hash_of_hashes_dict
= NULL
;
80 ASSERT_TRUE(update
->GetDictionaryWithoutPathExpansion(
81 internals::kHashOfHashesPref
, &hash_of_hashes_dict
));
82 hash_of_hashes_dict
->SetString("store_id", std::string(64, 'A'));
83 // This shouldn't have increased the number of existing hash of hashes.
84 ASSERT_EQ(1U, hash_of_hashes_dict
->size());
87 // |pref_hash_store3| should no longer trust its initial hashes dictionary and
88 // thus shouldn't trust non-NULL unknown values.
89 PrefHashStoreImpl
pref_hash_store3(
90 "store_id", std::string(32, 0), "device_id", &local_state
);
91 EXPECT_EQ(PrefHashStore::UNTRUSTED_UNKNOWN_VALUE
,
92 pref_hash_store3
.CheckValue("new_path", &string_1
));
93 EXPECT_EQ(PrefHashStore::UNTRUSTED_UNKNOWN_VALUE
,
94 pref_hash_store3
.CheckValue("new_path", &string_2
));
95 EXPECT_EQ(PrefHashStore::TRUSTED_UNKNOWN_VALUE
,
96 pref_hash_store3
.CheckValue("new_path", NULL
));