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

Side by Side Diff: chrome/browser/prefs/pref_hash_store_impl_unittest.cc

Issue 332473002: Make super MAC optional. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add a unit test. Created 6 years, 6 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 | Annotate | Revision Log
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 "chrome/browser/prefs/pref_hash_store_impl.h" 5 #include "chrome/browser/prefs/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"
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 MockHashStoreContents::Data hash_store_data_; 97 MockHashStoreContents::Data hash_store_data_;
98 }; 98 };
99 99
100 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) { 100 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) {
101 base::StringValue string_1("string1"); 101 base::StringValue string_1("string1");
102 base::StringValue string_2("string2"); 102 base::StringValue string_2("string2");
103 103
104 { 104 {
105 // 32 NULL bytes is the seed that was used to generate the legacy hash. 105 // 32 NULL bytes is the seed that was used to generate the legacy hash.
106 PrefHashStoreImpl pref_hash_store( 106 PrefHashStoreImpl pref_hash_store(
107 std::string(32, 0), "device_id", CreateHashStoreContents()); 107 std::string(32, 0), "device_id", CreateHashStoreContents(), true);
108 scoped_ptr<PrefHashStoreTransaction> transaction( 108 scoped_ptr<PrefHashStoreTransaction> transaction(
109 pref_hash_store.BeginTransaction()); 109 pref_hash_store.BeginTransaction());
110 110
111 // Only NULL should be trusted in the absence of a hash. 111 // Only NULL should be trusted in the absence of a hash.
112 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 112 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
113 transaction->CheckValue("path1", &string_1)); 113 transaction->CheckValue("path1", &string_1));
114 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 114 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
115 transaction->CheckValue("path1", NULL)); 115 transaction->CheckValue("path1", NULL));
116 116
117 transaction->StoreHash("path1", &string_1); 117 transaction->StoreHash("path1", &string_1);
(...skipping 24 matching lines...) Expand all
142 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 142 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
143 transaction->CheckValue("path1", &dict)); 143 transaction->CheckValue("path1", &dict));
144 144
145 // Test that the |pref_hash_store| flushes its changes on request post 145 // Test that the |pref_hash_store| flushes its changes on request post
146 // transaction. 146 // transaction.
147 transaction.reset(); 147 transaction.reset();
148 pref_hash_store.CommitPendingWrite(); 148 pref_hash_store.CommitPendingWrite();
149 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); 149 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset());
150 } 150 }
151 151
152 ASSERT_FALSE(CreateHashStoreContents()->GetSuperMac().empty());
153
152 { 154 {
153 // |pref_hash_store2| should trust its initial hashes dictionary and thus 155 // |pref_hash_store2| should trust its initial hashes dictionary and thus
154 // trust new unknown values. 156 // trust new unknown values.
155 PrefHashStoreImpl pref_hash_store2( 157 PrefHashStoreImpl pref_hash_store2(
156 std::string(32, 0), "device_id", CreateHashStoreContents()); 158 std::string(32, 0), "device_id", CreateHashStoreContents(), true);
157 scoped_ptr<PrefHashStoreTransaction> transaction( 159 scoped_ptr<PrefHashStoreTransaction> transaction(
158 pref_hash_store2.BeginTransaction()); 160 pref_hash_store2.BeginTransaction());
159 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 161 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
160 transaction->CheckValue("new_path", &string_1)); 162 transaction->CheckValue("new_path", &string_1));
161 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 163 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
162 transaction->CheckValue("new_path", &string_2)); 164 transaction->CheckValue("new_path", &string_2));
163 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 165 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
164 transaction->CheckValue("new_path", NULL)); 166 transaction->CheckValue("new_path", NULL));
165 167
166 // Test that |pref_hash_store2| doesn't flush its contents to disk when it 168 // Test that |pref_hash_store2| doesn't flush its contents to disk when it
167 // didn't change. 169 // didn't change.
168 transaction.reset(); 170 transaction.reset();
169 pref_hash_store2.CommitPendingWrite(); 171 pref_hash_store2.CommitPendingWrite();
170 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); 172 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset());
171 } 173 }
172 174
173 // Manually corrupt the super MAC. 175 // Manually corrupt the super MAC.
174 hash_store_data_.super_mac = std::string(64, 'A'); 176 hash_store_data_.super_mac = std::string(64, 'A');
175 177
176 { 178 {
177 // |pref_hash_store3| should no longer trust its initial hashes dictionary 179 // |pref_hash_store3| should no longer trust its initial hashes dictionary
178 // and thus shouldn't trust non-NULL unknown values. 180 // and thus shouldn't trust non-NULL unknown values.
179 PrefHashStoreImpl pref_hash_store3( 181 PrefHashStoreImpl pref_hash_store3(
180 std::string(32, 0), "device_id", CreateHashStoreContents()); 182 std::string(32, 0), "device_id", CreateHashStoreContents(), true);
181 scoped_ptr<PrefHashStoreTransaction> transaction( 183 scoped_ptr<PrefHashStoreTransaction> transaction(
182 pref_hash_store3.BeginTransaction()); 184 pref_hash_store3.BeginTransaction());
183 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 185 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
184 transaction->CheckValue("new_path", &string_1)); 186 transaction->CheckValue("new_path", &string_1));
185 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 187 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
186 transaction->CheckValue("new_path", &string_2)); 188 transaction->CheckValue("new_path", &string_2));
187 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 189 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
188 transaction->CheckValue("new_path", NULL)); 190 transaction->CheckValue("new_path", NULL));
189 191
190 // Test that |pref_hash_store3| doesn't flush its contents to disk when it 192 // Test that |pref_hash_store3| doesn't flush its contents to disk when it
191 // didn't change. 193 // didn't change.
192 transaction.reset(); 194 transaction.reset();
193 pref_hash_store3.CommitPendingWrite(); 195 pref_hash_store3.CommitPendingWrite();
194 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); 196 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset());
195 } 197 }
196 } 198 }
197 199
200 TEST_F(PrefHashStoreImplTest, SuperMACDisabled) {
201 base::StringValue string_1("string1");
202 base::StringValue string_2("string2");
203
204 {
205 // Pass |use_super_mac| => false.
206 PrefHashStoreImpl pref_hash_store(
207 std::string(32, 0), "device_id", CreateHashStoreContents(), false);
208 scoped_ptr<PrefHashStoreTransaction> transaction(
209 pref_hash_store.BeginTransaction());
210
211 transaction->StoreHash("path1", &string_2);
212 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
213 transaction->CheckValue("path1", &string_2));
214 }
215
216 ASSERT_TRUE(CreateHashStoreContents()->GetSuperMac().empty());
217
218 {
219 PrefHashStoreImpl pref_hash_store2(
220 std::string(32, 0), "device_id", CreateHashStoreContents(), false);
221 scoped_ptr<PrefHashStoreTransaction> transaction(
222 pref_hash_store2.BeginTransaction());
223 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
224 transaction->CheckValue("new_path", &string_1));
225 }
226 }
227
198 TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) { 228 TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) {
199 base::DictionaryValue dict; 229 base::DictionaryValue dict;
200 dict.Set("a", new base::StringValue("to be replaced")); 230 dict.Set("a", new base::StringValue("to be replaced"));
201 dict.Set("b", new base::StringValue("same")); 231 dict.Set("b", new base::StringValue("same"));
202 dict.Set("o", new base::StringValue("old")); 232 dict.Set("o", new base::StringValue("old"));
203 233
204 base::DictionaryValue modified_dict; 234 base::DictionaryValue modified_dict;
205 modified_dict.Set("a", new base::StringValue("replaced")); 235 modified_dict.Set("a", new base::StringValue("replaced"));
206 modified_dict.Set("b", new base::StringValue("same")); 236 modified_dict.Set("b", new base::StringValue("same"));
207 modified_dict.Set("c", new base::StringValue("new")); 237 modified_dict.Set("c", new base::StringValue("new"));
208 238
209 base::DictionaryValue empty_dict; 239 base::DictionaryValue empty_dict;
210 240
211 std::vector<std::string> invalid_keys; 241 std::vector<std::string> invalid_keys;
212 242
213 { 243 {
214 PrefHashStoreImpl pref_hash_store( 244 PrefHashStoreImpl pref_hash_store(
215 std::string(32, 0), "device_id", CreateHashStoreContents()); 245 std::string(32, 0), "device_id", CreateHashStoreContents(), true);
216 scoped_ptr<PrefHashStoreTransaction> transaction( 246 scoped_ptr<PrefHashStoreTransaction> transaction(
217 pref_hash_store.BeginTransaction()); 247 pref_hash_store.BeginTransaction());
218 248
219 // No hashes stored yet and hashes dictionary is empty (and thus not 249 // No hashes stored yet and hashes dictionary is empty (and thus not
220 // trusted). 250 // trusted).
221 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 251 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
222 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); 252 transaction->CheckSplitValue("path1", &dict, &invalid_keys));
223 EXPECT_TRUE(invalid_keys.empty()); 253 EXPECT_TRUE(invalid_keys.empty());
224 254
225 transaction->StoreSplitHash("path1", &dict); 255 transaction->StoreSplitHash("path1", &dict);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 // Test that the |pref_hash_store| flushes its changes on request. 311 // Test that the |pref_hash_store| flushes its changes on request.
282 transaction.reset(); 312 transaction.reset();
283 pref_hash_store.CommitPendingWrite(); 313 pref_hash_store.CommitPendingWrite();
284 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); 314 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset());
285 } 315 }
286 316
287 { 317 {
288 // |pref_hash_store2| should trust its initial hashes dictionary and thus 318 // |pref_hash_store2| should trust its initial hashes dictionary and thus
289 // trust new unknown values. 319 // trust new unknown values.
290 PrefHashStoreImpl pref_hash_store2( 320 PrefHashStoreImpl pref_hash_store2(
291 std::string(32, 0), "device_id", CreateHashStoreContents()); 321 std::string(32, 0), "device_id", CreateHashStoreContents(), true);
292 scoped_ptr<PrefHashStoreTransaction> transaction( 322 scoped_ptr<PrefHashStoreTransaction> transaction(
293 pref_hash_store2.BeginTransaction()); 323 pref_hash_store2.BeginTransaction());
294 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 324 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
295 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); 325 transaction->CheckSplitValue("new_path", &dict, &invalid_keys));
296 EXPECT_TRUE(invalid_keys.empty()); 326 EXPECT_TRUE(invalid_keys.empty());
297 327
298 // Test that |pref_hash_store2| doesn't flush its contents to disk when it 328 // Test that |pref_hash_store2| doesn't flush its contents to disk when it
299 // didn't change. 329 // didn't change.
300 transaction.reset(); 330 transaction.reset();
301 pref_hash_store2.CommitPendingWrite(); 331 pref_hash_store2.CommitPendingWrite();
302 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); 332 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset());
303 } 333 }
304 334
305 // Manually corrupt the super MAC. 335 // Manually corrupt the super MAC.
306 hash_store_data_.super_mac = std::string(64, 'A'); 336 hash_store_data_.super_mac = std::string(64, 'A');
307 337
308 { 338 {
309 // |pref_hash_store3| should no longer trust its initial hashes dictionary 339 // |pref_hash_store3| should no longer trust its initial hashes dictionary
310 // and thus shouldn't trust unknown values. 340 // and thus shouldn't trust unknown values.
311 PrefHashStoreImpl pref_hash_store3( 341 PrefHashStoreImpl pref_hash_store3(
312 std::string(32, 0), "device_id", CreateHashStoreContents()); 342 std::string(32, 0), "device_id", CreateHashStoreContents(), true);
313 scoped_ptr<PrefHashStoreTransaction> transaction( 343 scoped_ptr<PrefHashStoreTransaction> transaction(
314 pref_hash_store3.BeginTransaction()); 344 pref_hash_store3.BeginTransaction());
315 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, 345 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE,
316 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); 346 transaction->CheckSplitValue("new_path", &dict, &invalid_keys));
317 EXPECT_TRUE(invalid_keys.empty()); 347 EXPECT_TRUE(invalid_keys.empty());
318 348
319 // Test that |pref_hash_store3| doesn't flush its contents to disk when it 349 // Test that |pref_hash_store3| doesn't flush its contents to disk when it
320 // didn't change. 350 // didn't change.
321 transaction.reset(); 351 transaction.reset();
322 pref_hash_store3.CommitPendingWrite(); 352 pref_hash_store3.CommitPendingWrite();
323 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); 353 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset());
324 } 354 }
325 } 355 }
326 356
327 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) { 357 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) {
328 base::DictionaryValue empty_dict; 358 base::DictionaryValue empty_dict;
329 359
330 std::vector<std::string> invalid_keys; 360 std::vector<std::string> invalid_keys;
331 361
332 { 362 {
333 PrefHashStoreImpl pref_hash_store( 363 PrefHashStoreImpl pref_hash_store(
334 std::string(32, 0), "device_id", CreateHashStoreContents()); 364 std::string(32, 0), "device_id", CreateHashStoreContents(), true);
335 scoped_ptr<PrefHashStoreTransaction> transaction( 365 scoped_ptr<PrefHashStoreTransaction> transaction(
336 pref_hash_store.BeginTransaction()); 366 pref_hash_store.BeginTransaction());
337 367
338 // Store hashes for a random dict to be overwritten below. 368 // Store hashes for a random dict to be overwritten below.
339 base::DictionaryValue initial_dict; 369 base::DictionaryValue initial_dict;
340 initial_dict.Set("a", new base::StringValue("foo")); 370 initial_dict.Set("a", new base::StringValue("foo"));
341 transaction->StoreSplitHash("path1", &initial_dict); 371 transaction->StoreSplitHash("path1", &initial_dict);
342 372
343 // Verify stored empty dictionary matches NULL and empty dictionary back. 373 // Verify stored empty dictionary matches NULL and empty dictionary back.
344 transaction->StoreSplitHash("path1", &empty_dict); 374 transaction->StoreSplitHash("path1", &empty_dict);
(...skipping 16 matching lines...) Expand all
361 EXPECT_TRUE(invalid_keys.empty()); 391 EXPECT_TRUE(invalid_keys.empty());
362 } 392 }
363 393
364 { 394 {
365 // |pref_hash_store2| should trust its initial hashes dictionary (and thus 395 // |pref_hash_store2| should trust its initial hashes dictionary (and thus
366 // trust new unknown values) even though the last action done was to clear 396 // trust new unknown values) even though the last action done was to clear
367 // the hashes for path1 by setting its value to NULL (this is a regression 397 // the hashes for path1 by setting its value to NULL (this is a regression
368 // test ensuring that the internal action of clearing some hashes does 398 // test ensuring that the internal action of clearing some hashes does
369 // update the stored hash of hashes). 399 // update the stored hash of hashes).
370 PrefHashStoreImpl pref_hash_store2( 400 PrefHashStoreImpl pref_hash_store2(
371 std::string(32, 0), "device_id", CreateHashStoreContents()); 401 std::string(32, 0), "device_id", CreateHashStoreContents(), true);
372 scoped_ptr<PrefHashStoreTransaction> transaction( 402 scoped_ptr<PrefHashStoreTransaction> transaction(
373 pref_hash_store2.BeginTransaction()); 403 pref_hash_store2.BeginTransaction());
374 404
375 base::DictionaryValue tested_dict; 405 base::DictionaryValue tested_dict;
376 tested_dict.Set("a", new base::StringValue("foo")); 406 tested_dict.Set("a", new base::StringValue("foo"));
377 tested_dict.Set("b", new base::StringValue("bar")); 407 tested_dict.Set("b", new base::StringValue("bar"));
378 EXPECT_EQ( 408 EXPECT_EQ(
379 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 409 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
380 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys)); 410 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys));
381 EXPECT_TRUE(invalid_keys.empty()); 411 EXPECT_TRUE(invalid_keys.empty());
(...skipping 10 matching lines...) Expand all
392 base::StringValue string("string1"); 422 base::StringValue string("string1");
393 423
394 base::DictionaryValue dict; 424 base::DictionaryValue dict;
395 dict.Set("a", new base::StringValue("foo")); 425 dict.Set("a", new base::StringValue("foo"));
396 dict.Set("d", new base::StringValue("bad")); 426 dict.Set("d", new base::StringValue("bad"));
397 dict.Set("b", new base::StringValue("bar")); 427 dict.Set("b", new base::StringValue("bar"));
398 dict.Set("c", new base::StringValue("baz")); 428 dict.Set("c", new base::StringValue("baz"));
399 429
400 { 430 {
401 PrefHashStoreImpl pref_hash_store( 431 PrefHashStoreImpl pref_hash_store(
402 std::string(32, 0), "device_id", CreateHashStoreContents()); 432 std::string(32, 0), "device_id", CreateHashStoreContents(), true);
403 scoped_ptr<PrefHashStoreTransaction> transaction( 433 scoped_ptr<PrefHashStoreTransaction> transaction(
404 pref_hash_store.BeginTransaction()); 434 pref_hash_store.BeginTransaction());
405 435
406 transaction->StoreHash("path1", &string); 436 transaction->StoreHash("path1", &string);
407 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, 437 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED,
408 transaction->CheckValue("path1", &string)); 438 transaction->CheckValue("path1", &string));
409 } 439 }
410 440
411 { 441 {
412 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted. 442 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted.
413 PrefHashStoreImpl pref_hash_store2( 443 PrefHashStoreImpl pref_hash_store2(
414 std::string(32, 0), "device_id", CreateHashStoreContents()); 444 std::string(32, 0), "device_id", CreateHashStoreContents(), true);
415 scoped_ptr<PrefHashStoreTransaction> transaction( 445 scoped_ptr<PrefHashStoreTransaction> transaction(
416 pref_hash_store2.BeginTransaction()); 446 pref_hash_store2.BeginTransaction());
417 std::vector<std::string> invalid_keys; 447 std::vector<std::string> invalid_keys;
418 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, 448 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE,
419 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); 449 transaction->CheckSplitValue("path1", &dict, &invalid_keys));
420 EXPECT_TRUE(invalid_keys.empty()); 450 EXPECT_TRUE(invalid_keys.empty());
421 } 451 }
422 } 452 }
OLDNEW
« no previous file with comments | « chrome/browser/prefs/pref_hash_store_impl.cc ('k') | chrome/browser/prefs/profile_pref_store_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698