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

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

Issue 2297373002: Refactor PrefHashStore's BeginTransaction to take a rawptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 3 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
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/user_prefs/tracked/pref_hash_store_impl.h" 5 #include "components/user_prefs/tracked/pref_hash_store_impl.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "components/user_prefs/tracked/dictionary_hash_store_contents.h" 11 #include "components/user_prefs/tracked/dictionary_hash_store_contents.h"
12 #include "components/user_prefs/tracked/hash_store_contents.h" 12 #include "components/user_prefs/tracked/hash_store_contents.h"
13 #include "components/user_prefs/tracked/pref_hash_store_impl.h" 13 #include "components/user_prefs/tracked/pref_hash_store_impl.h"
14 #include "components/user_prefs/tracked/pref_hash_store_transaction.h" 14 #include "components/user_prefs/tracked/pref_hash_store_transaction.h"
15 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
16 16
17 class PrefHashStoreImplTest : public testing::Test { 17 class PrefHashStoreImplTest : public testing::Test {
18 protected: 18 public:
19 std::unique_ptr<HashStoreContents> CreateHashStoreContents() { 19 void SetUp() override {
20 return std::unique_ptr<HashStoreContents>( 20 contents_.reset(new DictionaryHashStoreContents(&pref_store_contents_));
gab 2016/09/01 20:31:50 A chromium-dev discussion just concluded in favor
proberge 2016/09/06 19:24:10 Done.
21 new DictionaryHashStoreContents(&pref_store_contents_));
22 } 21 }
23 22
23 protected:
24 HashStoreContents* GetHashStoreContents() { return contents_.get(); }
25
24 private: 26 private:
27 std::unique_ptr<HashStoreContents> contents_;
gab 2016/09/01 20:31:50 Needs to be after |pref_store_contents_| as member
proberge 2016/09/06 19:24:10 Done.
25 base::DictionaryValue pref_store_contents_; 28 base::DictionaryValue pref_store_contents_;
26 }; 29 };
gab 2016/09/01 20:31:50 DISALLOW_COPY_AND_ASSIGN(PrefHashStoreImplTest);
proberge 2016/09/06 19:24:10 Done.
27 30
28 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) { 31 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) {
29 base::StringValue string_1("string1"); 32 base::StringValue string_1("string1");
30 base::StringValue string_2("string2"); 33 base::StringValue string_2("string2");
31 34
32 { 35 {
33 // 32 NULL bytes is the seed that was used to generate the legacy hash. 36 // 32 NULL bytes is the seed that was used to generate the legacy hash.
34 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 37 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
35 std::unique_ptr<PrefHashStoreTransaction> transaction( 38 std::unique_ptr<PrefHashStoreTransaction> transaction(
36 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 39 pref_hash_store.BeginTransaction(GetHashStoreContents()));
37 40
38 // Only NULL should be trusted in the absence of a hash. 41 // Only NULL should be trusted in the absence of a hash.
39 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 42 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
40 transaction->CheckValue("path1", &string_1)); 43 transaction->CheckValue("path1", &string_1));
41 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, 44 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE,
42 transaction->CheckValue("path1", NULL)); 45 transaction->CheckValue("path1", NULL));
43 46
44 transaction->StoreHash("path1", &string_1); 47 transaction->StoreHash("path1", &string_1);
45 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 48 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
46 transaction->CheckValue("path1", &string_1)); 49 transaction->CheckValue("path1", &string_1));
47 EXPECT_EQ(PrefHashStoreTransaction::CLEARED, 50 EXPECT_EQ(PrefHashStoreTransaction::CLEARED,
48 transaction->CheckValue("path1", NULL)); 51 transaction->CheckValue("path1", NULL));
49 transaction->StoreHash("path1", NULL); 52 transaction->StoreHash("path1", NULL);
50 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 53 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
51 transaction->CheckValue("path1", NULL)); 54 transaction->CheckValue("path1", NULL));
52 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, 55 EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
53 transaction->CheckValue("path1", &string_2)); 56 transaction->CheckValue("path1", &string_2));
54 57
55 base::DictionaryValue dict; 58 base::DictionaryValue dict;
56 dict.Set("a", new base::StringValue("foo")); 59 dict.Set("a", new base::StringValue("foo"));
57 dict.Set("d", new base::StringValue("bad")); 60 dict.Set("d", new base::StringValue("bad"));
58 dict.Set("b", new base::StringValue("bar")); 61 dict.Set("b", new base::StringValue("bar"));
59 dict.Set("c", new base::StringValue("baz")); 62 dict.Set("c", new base::StringValue("baz"));
60 63
61 transaction->StoreHash("path1", &dict); 64 transaction->StoreHash("path1", &dict);
62 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 65 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
63 transaction->CheckValue("path1", &dict)); 66 transaction->CheckValue("path1", &dict));
64 } 67 }
65 68
66 ASSERT_FALSE(CreateHashStoreContents()->GetSuperMac().empty()); 69 ASSERT_FALSE(GetHashStoreContents()->GetSuperMac().empty());
67 70
68 { 71 {
69 // |pref_hash_store2| should trust its initial hashes dictionary and thus 72 // |pref_hash_store2| should trust its initial hashes dictionary and thus
70 // trust new unknown values. 73 // trust new unknown values.
71 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); 74 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true);
72 std::unique_ptr<PrefHashStoreTransaction> transaction( 75 std::unique_ptr<PrefHashStoreTransaction> transaction(
73 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); 76 pref_hash_store2.BeginTransaction(GetHashStoreContents()));
74 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 77 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
75 transaction->CheckValue("new_path", &string_1)); 78 transaction->CheckValue("new_path", &string_1));
76 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 79 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
77 transaction->CheckValue("new_path", &string_2)); 80 transaction->CheckValue("new_path", &string_2));
78 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, 81 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE,
79 transaction->CheckValue("new_path", NULL)); 82 transaction->CheckValue("new_path", NULL));
80 } 83 }
81 84
82 // Manually corrupt the super MAC. 85 // Manually corrupt the super MAC.
83 CreateHashStoreContents()->SetSuperMac(std::string(64, 'A')); 86 GetHashStoreContents()->SetSuperMac(std::string(64, 'A'));
84 87
85 { 88 {
86 // |pref_hash_store3| should no longer trust its initial hashes dictionary 89 // |pref_hash_store3| should no longer trust its initial hashes dictionary
87 // and thus shouldn't trust non-NULL unknown values. 90 // and thus shouldn't trust non-NULL unknown values.
88 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true); 91 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true);
89 std::unique_ptr<PrefHashStoreTransaction> transaction( 92 std::unique_ptr<PrefHashStoreTransaction> transaction(
90 pref_hash_store3.BeginTransaction(CreateHashStoreContents())); 93 pref_hash_store3.BeginTransaction(GetHashStoreContents()));
91 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 94 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
92 transaction->CheckValue("new_path", &string_1)); 95 transaction->CheckValue("new_path", &string_1));
93 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 96 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
94 transaction->CheckValue("new_path", &string_2)); 97 transaction->CheckValue("new_path", &string_2));
95 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, 98 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE,
96 transaction->CheckValue("new_path", NULL)); 99 transaction->CheckValue("new_path", NULL));
97 } 100 }
98 } 101 }
99 102
100 TEST_F(PrefHashStoreImplTest, ImportExportOperations) { 103 TEST_F(PrefHashStoreImplTest, ImportExportOperations) {
101 base::StringValue string_1("string1"); 104 base::StringValue string_1("string1");
102 base::StringValue string_2("string2"); 105 base::StringValue string_2("string2");
103 106
104 // Initial state: no super MAC. 107 // Initial state: no super MAC.
105 { 108 {
106 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 109 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
107 std::unique_ptr<PrefHashStoreTransaction> transaction( 110 std::unique_ptr<PrefHashStoreTransaction> transaction(
108 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 111 pref_hash_store.BeginTransaction(GetHashStoreContents()));
109 ASSERT_FALSE(transaction->IsSuperMACValid()); 112 ASSERT_FALSE(transaction->IsSuperMACValid());
110 113
111 ASSERT_FALSE(transaction->HasHash("path1")); 114 ASSERT_FALSE(transaction->HasHash("path1"));
112 115
113 // Storing a hash will stamp the super MAC. 116 // Storing a hash will stamp the super MAC.
114 transaction->StoreHash("path1", &string_1); 117 transaction->StoreHash("path1", &string_1);
115 118
116 ASSERT_TRUE(transaction->HasHash("path1")); 119 ASSERT_TRUE(transaction->HasHash("path1"));
117 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 120 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
118 transaction->CheckValue("path1", &string_1)); 121 transaction->CheckValue("path1", &string_1));
119 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, 122 EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
120 transaction->CheckValue("path1", &string_2)); 123 transaction->CheckValue("path1", &string_2));
121 } 124 }
122 125
123 // Make a copy of the stored hash for future use. 126 // Make a copy of the stored hash for future use.
124 const base::Value* hash = NULL; 127 const base::Value* hash = NULL;
125 ASSERT_TRUE(CreateHashStoreContents()->GetContents()->Get("path1", &hash)); 128 ASSERT_TRUE(GetHashStoreContents()->GetContents()->Get("path1", &hash));
126 std::unique_ptr<base::Value> path_1_string_1_hash_copy(hash->DeepCopy()); 129 std::unique_ptr<base::Value> path_1_string_1_hash_copy(hash->DeepCopy());
127 hash = NULL; 130 hash = NULL;
128 131
129 // Verify that the super MAC was stamped. 132 // Verify that the super MAC was stamped.
130 { 133 {
131 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 134 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
132 std::unique_ptr<PrefHashStoreTransaction> transaction( 135 std::unique_ptr<PrefHashStoreTransaction> transaction(
133 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 136 pref_hash_store.BeginTransaction(GetHashStoreContents()));
134 ASSERT_TRUE(transaction->IsSuperMACValid()); 137 ASSERT_TRUE(transaction->IsSuperMACValid());
135 ASSERT_TRUE(transaction->HasHash("path1")); 138 ASSERT_TRUE(transaction->HasHash("path1"));
136 139
137 // Clearing the hash should preserve validity. 140 // Clearing the hash should preserve validity.
138 transaction->ClearHash("path1"); 141 transaction->ClearHash("path1");
139 142
140 // The effects of the clear should be immediately visible. 143 // The effects of the clear should be immediately visible.
141 ASSERT_FALSE(transaction->HasHash("path1")); 144 ASSERT_FALSE(transaction->HasHash("path1"));
142 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, 145 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE,
143 transaction->CheckValue("path1", NULL)); 146 transaction->CheckValue("path1", NULL));
144 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 147 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
145 transaction->CheckValue("path1", &string_1)); 148 transaction->CheckValue("path1", &string_1));
146 } 149 }
147 150
148 // Verify that validity was preserved and that the clear took effect. 151 // Verify that validity was preserved and that the clear took effect.
149 { 152 {
150 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 153 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
151 std::unique_ptr<PrefHashStoreTransaction> transaction( 154 std::unique_ptr<PrefHashStoreTransaction> transaction(
152 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 155 pref_hash_store.BeginTransaction(GetHashStoreContents()));
153 ASSERT_TRUE(transaction->IsSuperMACValid()); 156 ASSERT_TRUE(transaction->IsSuperMACValid());
154 ASSERT_FALSE(transaction->HasHash("path1")); 157 ASSERT_FALSE(transaction->HasHash("path1"));
155 } 158 }
156 159
157 // Invalidate the super MAC. 160 // Invalidate the super MAC.
158 CreateHashStoreContents()->SetSuperMac(std::string()); 161 GetHashStoreContents()->SetSuperMac(std::string());
159 162
160 { 163 {
161 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 164 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
162 std::unique_ptr<PrefHashStoreTransaction> transaction( 165 std::unique_ptr<PrefHashStoreTransaction> transaction(
163 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 166 pref_hash_store.BeginTransaction(GetHashStoreContents()));
164 ASSERT_FALSE(transaction->IsSuperMACValid()); 167 ASSERT_FALSE(transaction->IsSuperMACValid());
165 ASSERT_FALSE(transaction->HasHash("path1")); 168 ASSERT_FALSE(transaction->HasHash("path1"));
166 169
167 // An import should preserve invalidity. 170 // An import should preserve invalidity.
168 transaction->ImportHash("path1", path_1_string_1_hash_copy.get()); 171 transaction->ImportHash("path1", path_1_string_1_hash_copy.get());
169 172
170 ASSERT_TRUE(transaction->HasHash("path1")); 173 ASSERT_TRUE(transaction->HasHash("path1"));
171 174
172 // The imported hash should be usable for validating the original value. 175 // The imported hash should be usable for validating the original value.
173 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 176 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
174 transaction->CheckValue("path1", &string_1)); 177 transaction->CheckValue("path1", &string_1));
175 } 178 }
176 179
177 // Verify that invalidity was preserved and that the import took effect. 180 // Verify that invalidity was preserved and that the import took effect.
178 { 181 {
179 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 182 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
180 std::unique_ptr<PrefHashStoreTransaction> transaction( 183 std::unique_ptr<PrefHashStoreTransaction> transaction(
181 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 184 pref_hash_store.BeginTransaction(GetHashStoreContents()));
182 ASSERT_FALSE(transaction->IsSuperMACValid()); 185 ASSERT_FALSE(transaction->IsSuperMACValid());
183 ASSERT_TRUE(transaction->HasHash("path1")); 186 ASSERT_TRUE(transaction->HasHash("path1"));
184 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 187 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
185 transaction->CheckValue("path1", &string_1)); 188 transaction->CheckValue("path1", &string_1));
186 189
187 // After clearing the hash, non-null values are UNTRUSTED_UNKNOWN. 190 // After clearing the hash, non-null values are UNTRUSTED_UNKNOWN.
188 transaction->ClearHash("path1"); 191 transaction->ClearHash("path1");
189 192
190 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE, 193 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_NULL_VALUE,
191 transaction->CheckValue("path1", NULL)); 194 transaction->CheckValue("path1", NULL));
192 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 195 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
193 transaction->CheckValue("path1", &string_1)); 196 transaction->CheckValue("path1", &string_1));
194 } 197 }
195 198
196 { 199 {
197 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 200 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
198 std::unique_ptr<PrefHashStoreTransaction> transaction( 201 std::unique_ptr<PrefHashStoreTransaction> transaction(
199 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 202 pref_hash_store.BeginTransaction(GetHashStoreContents()));
200 ASSERT_FALSE(transaction->IsSuperMACValid()); 203 ASSERT_FALSE(transaction->IsSuperMACValid());
201 204
202 // Test StampSuperMac. 205 // Test StampSuperMac.
203 transaction->StampSuperMac(); 206 transaction->StampSuperMac();
204 } 207 }
205 208
206 // Verify that the store is now valid. 209 // Verify that the store is now valid.
207 { 210 {
208 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 211 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
209 std::unique_ptr<PrefHashStoreTransaction> transaction( 212 std::unique_ptr<PrefHashStoreTransaction> transaction(
210 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 213 pref_hash_store.BeginTransaction(GetHashStoreContents()));
211 ASSERT_TRUE(transaction->IsSuperMACValid()); 214 ASSERT_TRUE(transaction->IsSuperMACValid());
212 215
213 // Store the hash of a different value to test an "over-import". 216 // Store the hash of a different value to test an "over-import".
214 transaction->StoreHash("path1", &string_2); 217 transaction->StoreHash("path1", &string_2);
215 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, 218 EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
216 transaction->CheckValue("path1", &string_1)); 219 transaction->CheckValue("path1", &string_1));
217 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 220 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
218 transaction->CheckValue("path1", &string_2)); 221 transaction->CheckValue("path1", &string_2));
219 } 222 }
220 223
221 { 224 {
222 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 225 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
223 std::unique_ptr<PrefHashStoreTransaction> transaction( 226 std::unique_ptr<PrefHashStoreTransaction> transaction(
224 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 227 pref_hash_store.BeginTransaction(GetHashStoreContents()));
225 ASSERT_TRUE(transaction->IsSuperMACValid()); 228 ASSERT_TRUE(transaction->IsSuperMACValid());
226 229
227 // "Over-import". An import should preserve validity. 230 // "Over-import". An import should preserve validity.
228 transaction->ImportHash("path1", path_1_string_1_hash_copy.get()); 231 transaction->ImportHash("path1", path_1_string_1_hash_copy.get());
229 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 232 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
230 transaction->CheckValue("path1", &string_1)); 233 transaction->CheckValue("path1", &string_1));
231 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, 234 EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
232 transaction->CheckValue("path1", &string_2)); 235 transaction->CheckValue("path1", &string_2));
233 } 236 }
234 237
235 // Verify that validity was preserved and the "over-import" took effect. 238 // Verify that validity was preserved and the "over-import" took effect.
236 { 239 {
237 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 240 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
238 std::unique_ptr<PrefHashStoreTransaction> transaction( 241 std::unique_ptr<PrefHashStoreTransaction> transaction(
239 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 242 pref_hash_store.BeginTransaction(GetHashStoreContents()));
240 ASSERT_TRUE(transaction->IsSuperMACValid()); 243 ASSERT_TRUE(transaction->IsSuperMACValid());
241 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 244 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
242 transaction->CheckValue("path1", &string_1)); 245 transaction->CheckValue("path1", &string_1));
243 EXPECT_EQ(PrefHashStoreTransaction::CHANGED, 246 EXPECT_EQ(PrefHashStoreTransaction::CHANGED,
244 transaction->CheckValue("path1", &string_2)); 247 transaction->CheckValue("path1", &string_2));
245 } 248 }
246 } 249 }
247 250
248 TEST_F(PrefHashStoreImplTest, SuperMACDisabled) { 251 TEST_F(PrefHashStoreImplTest, SuperMACDisabled) {
249 base::StringValue string_1("string1"); 252 base::StringValue string_1("string1");
250 base::StringValue string_2("string2"); 253 base::StringValue string_2("string2");
251 254
252 { 255 {
253 // Pass |use_super_mac| => false. 256 // Pass |use_super_mac| => false.
254 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", false); 257 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", false);
255 std::unique_ptr<PrefHashStoreTransaction> transaction( 258 std::unique_ptr<PrefHashStoreTransaction> transaction(
256 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 259 pref_hash_store.BeginTransaction(GetHashStoreContents()));
257 260
258 transaction->StoreHash("path1", &string_2); 261 transaction->StoreHash("path1", &string_2);
259 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 262 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
260 transaction->CheckValue("path1", &string_2)); 263 transaction->CheckValue("path1", &string_2));
261 } 264 }
262 265
263 ASSERT_TRUE(CreateHashStoreContents()->GetSuperMac().empty()); 266 ASSERT_TRUE(GetHashStoreContents()->GetSuperMac().empty());
264 267
265 { 268 {
266 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", false); 269 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", false);
267 std::unique_ptr<PrefHashStoreTransaction> transaction( 270 std::unique_ptr<PrefHashStoreTransaction> transaction(
268 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); 271 pref_hash_store2.BeginTransaction(GetHashStoreContents()));
269 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 272 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
270 transaction->CheckValue("new_path", &string_1)); 273 transaction->CheckValue("new_path", &string_1));
271 } 274 }
272 } 275 }
273 276
274 TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) { 277 TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) {
275 base::DictionaryValue dict; 278 base::DictionaryValue dict;
276 dict.Set("a", new base::StringValue("to be replaced")); 279 dict.Set("a", new base::StringValue("to be replaced"));
277 dict.Set("b", new base::StringValue("same")); 280 dict.Set("b", new base::StringValue("same"));
278 dict.Set("o", new base::StringValue("old")); 281 dict.Set("o", new base::StringValue("old"));
279 282
280 base::DictionaryValue modified_dict; 283 base::DictionaryValue modified_dict;
281 modified_dict.Set("a", new base::StringValue("replaced")); 284 modified_dict.Set("a", new base::StringValue("replaced"));
282 modified_dict.Set("b", new base::StringValue("same")); 285 modified_dict.Set("b", new base::StringValue("same"));
283 modified_dict.Set("c", new base::StringValue("new")); 286 modified_dict.Set("c", new base::StringValue("new"));
284 287
285 base::DictionaryValue empty_dict; 288 base::DictionaryValue empty_dict;
286 289
287 std::vector<std::string> invalid_keys; 290 std::vector<std::string> invalid_keys;
288 291
289 { 292 {
290 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 293 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
291 std::unique_ptr<PrefHashStoreTransaction> transaction( 294 std::unique_ptr<PrefHashStoreTransaction> transaction(
292 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 295 pref_hash_store.BeginTransaction(GetHashStoreContents()));
293 296
294 // No hashes stored yet and hashes dictionary is empty (and thus not 297 // No hashes stored yet and hashes dictionary is empty (and thus not
295 // trusted). 298 // trusted).
296 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 299 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
297 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); 300 transaction->CheckSplitValue("path1", &dict, &invalid_keys));
298 EXPECT_TRUE(invalid_keys.empty()); 301 EXPECT_TRUE(invalid_keys.empty());
299 302
300 transaction->StoreSplitHash("path1", &dict); 303 transaction->StoreSplitHash("path1", &dict);
301 304
302 // Verify match post storage. 305 // Verify match post storage.
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 expected_invalid_keys2.push_back("c"); 355 expected_invalid_keys2.push_back("c");
353 EXPECT_EQ(expected_invalid_keys2, invalid_keys); 356 EXPECT_EQ(expected_invalid_keys2, invalid_keys);
354 invalid_keys.clear(); 357 invalid_keys.clear();
355 } 358 }
356 359
357 { 360 {
358 // |pref_hash_store2| should trust its initial hashes dictionary and thus 361 // |pref_hash_store2| should trust its initial hashes dictionary and thus
359 // trust new unknown values. 362 // trust new unknown values.
360 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); 363 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true);
361 std::unique_ptr<PrefHashStoreTransaction> transaction( 364 std::unique_ptr<PrefHashStoreTransaction> transaction(
362 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); 365 pref_hash_store2.BeginTransaction(GetHashStoreContents()));
363 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 366 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
364 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); 367 transaction->CheckSplitValue("new_path", &dict, &invalid_keys));
365 EXPECT_TRUE(invalid_keys.empty()); 368 EXPECT_TRUE(invalid_keys.empty());
366 } 369 }
367 370
368 // Manually corrupt the super MAC. 371 // Manually corrupt the super MAC.
369 CreateHashStoreContents()->SetSuperMac(std::string(64, 'A')); 372 GetHashStoreContents()->SetSuperMac(std::string(64, 'A'));
370 373
371 { 374 {
372 // |pref_hash_store3| should no longer trust its initial hashes dictionary 375 // |pref_hash_store3| should no longer trust its initial hashes dictionary
373 // and thus shouldn't trust unknown values. 376 // and thus shouldn't trust unknown values.
374 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true); 377 PrefHashStoreImpl pref_hash_store3(std::string(32, 0), "device_id", true);
375 std::unique_ptr<PrefHashStoreTransaction> transaction( 378 std::unique_ptr<PrefHashStoreTransaction> transaction(
376 pref_hash_store3.BeginTransaction(CreateHashStoreContents())); 379 pref_hash_store3.BeginTransaction(GetHashStoreContents()));
377 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 380 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
378 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); 381 transaction->CheckSplitValue("new_path", &dict, &invalid_keys));
379 EXPECT_TRUE(invalid_keys.empty()); 382 EXPECT_TRUE(invalid_keys.empty());
380 } 383 }
381 } 384 }
382 385
383 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) { 386 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) {
384 base::DictionaryValue empty_dict; 387 base::DictionaryValue empty_dict;
385 388
386 std::vector<std::string> invalid_keys; 389 std::vector<std::string> invalid_keys;
387 390
388 { 391 {
389 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 392 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
390 std::unique_ptr<PrefHashStoreTransaction> transaction( 393 std::unique_ptr<PrefHashStoreTransaction> transaction(
391 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 394 pref_hash_store.BeginTransaction(GetHashStoreContents()));
392 395
393 // Store hashes for a random dict to be overwritten below. 396 // Store hashes for a random dict to be overwritten below.
394 base::DictionaryValue initial_dict; 397 base::DictionaryValue initial_dict;
395 initial_dict.Set("a", new base::StringValue("foo")); 398 initial_dict.Set("a", new base::StringValue("foo"));
396 transaction->StoreSplitHash("path1", &initial_dict); 399 transaction->StoreSplitHash("path1", &initial_dict);
397 400
398 // Verify stored empty dictionary matches NULL and empty dictionary back. 401 // Verify stored empty dictionary matches NULL and empty dictionary back.
399 transaction->StoreSplitHash("path1", &empty_dict); 402 transaction->StoreSplitHash("path1", &empty_dict);
400 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 403 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
401 transaction->CheckSplitValue("path1", NULL, &invalid_keys)); 404 transaction->CheckSplitValue("path1", NULL, &invalid_keys));
(...skipping 15 matching lines...) Expand all
417 } 420 }
418 421
419 { 422 {
420 // |pref_hash_store2| should trust its initial hashes dictionary (and thus 423 // |pref_hash_store2| should trust its initial hashes dictionary (and thus
421 // trust new unknown values) even though the last action done was to clear 424 // trust new unknown values) even though the last action done was to clear
422 // the hashes for path1 by setting its value to NULL (this is a regression 425 // the hashes for path1 by setting its value to NULL (this is a regression
423 // test ensuring that the internal action of clearing some hashes does 426 // test ensuring that the internal action of clearing some hashes does
424 // update the stored hash of hashes). 427 // update the stored hash of hashes).
425 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); 428 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true);
426 std::unique_ptr<PrefHashStoreTransaction> transaction( 429 std::unique_ptr<PrefHashStoreTransaction> transaction(
427 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); 430 pref_hash_store2.BeginTransaction(GetHashStoreContents()));
428 431
429 base::DictionaryValue tested_dict; 432 base::DictionaryValue tested_dict;
430 tested_dict.Set("a", new base::StringValue("foo")); 433 tested_dict.Set("a", new base::StringValue("foo"));
431 tested_dict.Set("b", new base::StringValue("bar")); 434 tested_dict.Set("b", new base::StringValue("bar"));
432 EXPECT_EQ( 435 EXPECT_EQ(
433 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 436 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
434 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys)); 437 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys));
435 EXPECT_TRUE(invalid_keys.empty()); 438 EXPECT_TRUE(invalid_keys.empty());
436 } 439 }
437 } 440 }
438 441
439 // Test that the PrefHashStore returns TRUSTED_UNKNOWN_VALUE when checking for 442 // Test that the PrefHashStore returns TRUSTED_UNKNOWN_VALUE when checking for
440 // a split preference even if there is an existing atomic preference's hash 443 // a split preference even if there is an existing atomic preference's hash
441 // stored. There is no point providing a migration path for preferences 444 // stored. There is no point providing a migration path for preferences
442 // switching strategies after their initial release as split preferences are 445 // switching strategies after their initial release as split preferences are
443 // turned into split preferences specifically because the atomic hash isn't 446 // turned into split preferences specifically because the atomic hash isn't
444 // considered useful. 447 // considered useful.
445 TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) { 448 TEST_F(PrefHashStoreImplTest, TrustedUnknownSplitValueFromExistingAtomic) {
446 base::StringValue string("string1"); 449 base::StringValue string("string1");
447 450
448 base::DictionaryValue dict; 451 base::DictionaryValue dict;
449 dict.Set("a", new base::StringValue("foo")); 452 dict.Set("a", new base::StringValue("foo"));
450 dict.Set("d", new base::StringValue("bad")); 453 dict.Set("d", new base::StringValue("bad"));
451 dict.Set("b", new base::StringValue("bar")); 454 dict.Set("b", new base::StringValue("bar"));
452 dict.Set("c", new base::StringValue("baz")); 455 dict.Set("c", new base::StringValue("baz"));
453 456
454 { 457 {
455 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true); 458 PrefHashStoreImpl pref_hash_store(std::string(32, 0), "device_id", true);
456 std::unique_ptr<PrefHashStoreTransaction> transaction( 459 std::unique_ptr<PrefHashStoreTransaction> transaction(
457 pref_hash_store.BeginTransaction(CreateHashStoreContents())); 460 pref_hash_store.BeginTransaction(GetHashStoreContents()));
458 461
459 transaction->StoreHash("path1", &string); 462 transaction->StoreHash("path1", &string);
460 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 463 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
461 transaction->CheckValue("path1", &string)); 464 transaction->CheckValue("path1", &string));
462 } 465 }
463 466
464 { 467 {
465 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted. 468 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted.
466 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true); 469 PrefHashStoreImpl pref_hash_store2(std::string(32, 0), "device_id", true);
467 std::unique_ptr<PrefHashStoreTransaction> transaction( 470 std::unique_ptr<PrefHashStoreTransaction> transaction(
468 pref_hash_store2.BeginTransaction(CreateHashStoreContents())); 471 pref_hash_store2.BeginTransaction(GetHashStoreContents()));
469 std::vector<std::string> invalid_keys; 472 std::vector<std::string> invalid_keys;
470 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 473 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
471 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); 474 transaction->CheckSplitValue("path1", &dict, &invalid_keys));
472 EXPECT_TRUE(invalid_keys.empty()); 475 EXPECT_TRUE(invalid_keys.empty());
473 } 476 }
474 } 477 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698