| OLD | NEW |
| 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 Loading... |
| 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 28 matching lines...) Expand all Loading... |
| 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 { | 152 { |
| 153 // |pref_hash_store2| should trust its initial hashes dictionary and thus | 153 // |pref_hash_store2| should trust its initial hashes dictionary and thus |
| 154 // trust new unknown values. | 154 // trust new unknown values. |
| 155 PrefHashStoreImpl pref_hash_store2( | 155 PrefHashStoreImpl pref_hash_store2( |
| 156 std::string(32, 0), "device_id", CreateHashStoreContents()); | 156 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 157 scoped_ptr<PrefHashStoreTransaction> transaction( | 157 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 158 pref_hash_store2.BeginTransaction()); | 158 pref_hash_store2.BeginTransaction()); |
| 159 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 159 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 160 transaction->CheckValue("new_path", &string_1)); | 160 transaction->CheckValue("new_path", &string_1)); |
| 161 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 161 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 162 transaction->CheckValue("new_path", &string_2)); | 162 transaction->CheckValue("new_path", &string_2)); |
| 163 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 163 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 164 transaction->CheckValue("new_path", NULL)); | 164 transaction->CheckValue("new_path", NULL)); |
| 165 | 165 |
| 166 // Test that |pref_hash_store2| doesn't flush its contents to disk when it | 166 // Test that |pref_hash_store2| doesn't flush its contents to disk when it |
| 167 // didn't change. | 167 // didn't change. |
| 168 transaction.reset(); | 168 transaction.reset(); |
| 169 pref_hash_store2.CommitPendingWrite(); | 169 pref_hash_store2.CommitPendingWrite(); |
| 170 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | 170 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); |
| 171 } | 171 } |
| 172 | 172 |
| 173 // Manually corrupt the super MAC. | 173 // Manually corrupt the super MAC. |
| 174 hash_store_data_.super_mac = std::string(64, 'A'); | 174 hash_store_data_.super_mac = std::string(64, 'A'); |
| 175 | 175 |
| 176 { | 176 { |
| 177 // |pref_hash_store3| should no longer trust its initial hashes dictionary | 177 // |pref_hash_store3| should no longer trust its initial hashes dictionary |
| 178 // and thus shouldn't trust non-NULL unknown values. | 178 // and thus shouldn't trust non-NULL unknown values. |
| 179 PrefHashStoreImpl pref_hash_store3( | 179 PrefHashStoreImpl pref_hash_store3( |
| 180 std::string(32, 0), "device_id", CreateHashStoreContents()); | 180 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 181 scoped_ptr<PrefHashStoreTransaction> transaction( | 181 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 182 pref_hash_store3.BeginTransaction()); | 182 pref_hash_store3.BeginTransaction()); |
| 183 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 183 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| 184 transaction->CheckValue("new_path", &string_1)); | 184 transaction->CheckValue("new_path", &string_1)); |
| 185 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 185 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| 186 transaction->CheckValue("new_path", &string_2)); | 186 transaction->CheckValue("new_path", &string_2)); |
| 187 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 187 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 188 transaction->CheckValue("new_path", NULL)); | 188 transaction->CheckValue("new_path", NULL)); |
| 189 | 189 |
| 190 // Test that |pref_hash_store3| doesn't flush its contents to disk when it | 190 // Test that |pref_hash_store3| doesn't flush its contents to disk when it |
| (...skipping 14 matching lines...) Expand all Loading... |
| 205 modified_dict.Set("a", new base::StringValue("replaced")); | 205 modified_dict.Set("a", new base::StringValue("replaced")); |
| 206 modified_dict.Set("b", new base::StringValue("same")); | 206 modified_dict.Set("b", new base::StringValue("same")); |
| 207 modified_dict.Set("c", new base::StringValue("new")); | 207 modified_dict.Set("c", new base::StringValue("new")); |
| 208 | 208 |
| 209 base::DictionaryValue empty_dict; | 209 base::DictionaryValue empty_dict; |
| 210 | 210 |
| 211 std::vector<std::string> invalid_keys; | 211 std::vector<std::string> invalid_keys; |
| 212 | 212 |
| 213 { | 213 { |
| 214 PrefHashStoreImpl pref_hash_store( | 214 PrefHashStoreImpl pref_hash_store( |
| 215 std::string(32, 0), "device_id", CreateHashStoreContents()); | 215 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 216 scoped_ptr<PrefHashStoreTransaction> transaction( | 216 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 217 pref_hash_store.BeginTransaction()); | 217 pref_hash_store.BeginTransaction()); |
| 218 | 218 |
| 219 // No hashes stored yet and hashes dictionary is empty (and thus not | 219 // No hashes stored yet and hashes dictionary is empty (and thus not |
| 220 // trusted). | 220 // trusted). |
| 221 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 221 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| 222 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); | 222 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); |
| 223 EXPECT_TRUE(invalid_keys.empty()); | 223 EXPECT_TRUE(invalid_keys.empty()); |
| 224 | 224 |
| 225 transaction->StoreSplitHash("path1", &dict); | 225 transaction->StoreSplitHash("path1", &dict); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 // Test that the |pref_hash_store| flushes its changes on request. | 281 // Test that the |pref_hash_store| flushes its changes on request. |
| 282 transaction.reset(); | 282 transaction.reset(); |
| 283 pref_hash_store.CommitPendingWrite(); | 283 pref_hash_store.CommitPendingWrite(); |
| 284 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); | 284 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); |
| 285 } | 285 } |
| 286 | 286 |
| 287 { | 287 { |
| 288 // |pref_hash_store2| should trust its initial hashes dictionary and thus | 288 // |pref_hash_store2| should trust its initial hashes dictionary and thus |
| 289 // trust new unknown values. | 289 // trust new unknown values. |
| 290 PrefHashStoreImpl pref_hash_store2( | 290 PrefHashStoreImpl pref_hash_store2( |
| 291 std::string(32, 0), "device_id", CreateHashStoreContents()); | 291 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 292 scoped_ptr<PrefHashStoreTransaction> transaction( | 292 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 293 pref_hash_store2.BeginTransaction()); | 293 pref_hash_store2.BeginTransaction()); |
| 294 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 294 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 295 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); | 295 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); |
| 296 EXPECT_TRUE(invalid_keys.empty()); | 296 EXPECT_TRUE(invalid_keys.empty()); |
| 297 | 297 |
| 298 // Test that |pref_hash_store2| doesn't flush its contents to disk when it | 298 // Test that |pref_hash_store2| doesn't flush its contents to disk when it |
| 299 // didn't change. | 299 // didn't change. |
| 300 transaction.reset(); | 300 transaction.reset(); |
| 301 pref_hash_store2.CommitPendingWrite(); | 301 pref_hash_store2.CommitPendingWrite(); |
| 302 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | 302 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); |
| 303 } | 303 } |
| 304 | 304 |
| 305 // Manually corrupt the super MAC. | 305 // Manually corrupt the super MAC. |
| 306 hash_store_data_.super_mac = std::string(64, 'A'); | 306 hash_store_data_.super_mac = std::string(64, 'A'); |
| 307 | 307 |
| 308 { | 308 { |
| 309 // |pref_hash_store3| should no longer trust its initial hashes dictionary | 309 // |pref_hash_store3| should no longer trust its initial hashes dictionary |
| 310 // and thus shouldn't trust unknown values. | 310 // and thus shouldn't trust unknown values. |
| 311 PrefHashStoreImpl pref_hash_store3( | 311 PrefHashStoreImpl pref_hash_store3( |
| 312 std::string(32, 0), "device_id", CreateHashStoreContents()); | 312 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 313 scoped_ptr<PrefHashStoreTransaction> transaction( | 313 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 314 pref_hash_store3.BeginTransaction()); | 314 pref_hash_store3.BeginTransaction()); |
| 315 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 315 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| 316 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); | 316 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); |
| 317 EXPECT_TRUE(invalid_keys.empty()); | 317 EXPECT_TRUE(invalid_keys.empty()); |
| 318 | 318 |
| 319 // Test that |pref_hash_store3| doesn't flush its contents to disk when it | 319 // Test that |pref_hash_store3| doesn't flush its contents to disk when it |
| 320 // didn't change. | 320 // didn't change. |
| 321 transaction.reset(); | 321 transaction.reset(); |
| 322 pref_hash_store3.CommitPendingWrite(); | 322 pref_hash_store3.CommitPendingWrite(); |
| 323 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | 323 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); |
| 324 } | 324 } |
| 325 } | 325 } |
| 326 | 326 |
| 327 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) { | 327 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) { |
| 328 base::DictionaryValue empty_dict; | 328 base::DictionaryValue empty_dict; |
| 329 | 329 |
| 330 std::vector<std::string> invalid_keys; | 330 std::vector<std::string> invalid_keys; |
| 331 | 331 |
| 332 { | 332 { |
| 333 PrefHashStoreImpl pref_hash_store( | 333 PrefHashStoreImpl pref_hash_store( |
| 334 std::string(32, 0), "device_id", CreateHashStoreContents()); | 334 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 335 scoped_ptr<PrefHashStoreTransaction> transaction( | 335 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 336 pref_hash_store.BeginTransaction()); | 336 pref_hash_store.BeginTransaction()); |
| 337 | 337 |
| 338 // Store hashes for a random dict to be overwritten below. | 338 // Store hashes for a random dict to be overwritten below. |
| 339 base::DictionaryValue initial_dict; | 339 base::DictionaryValue initial_dict; |
| 340 initial_dict.Set("a", new base::StringValue("foo")); | 340 initial_dict.Set("a", new base::StringValue("foo")); |
| 341 transaction->StoreSplitHash("path1", &initial_dict); | 341 transaction->StoreSplitHash("path1", &initial_dict); |
| 342 | 342 |
| 343 // Verify stored empty dictionary matches NULL and empty dictionary back. | 343 // Verify stored empty dictionary matches NULL and empty dictionary back. |
| 344 transaction->StoreSplitHash("path1", &empty_dict); | 344 transaction->StoreSplitHash("path1", &empty_dict); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 361 EXPECT_TRUE(invalid_keys.empty()); | 361 EXPECT_TRUE(invalid_keys.empty()); |
| 362 } | 362 } |
| 363 | 363 |
| 364 { | 364 { |
| 365 // |pref_hash_store2| should trust its initial hashes dictionary (and thus | 365 // |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 | 366 // 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 | 367 // 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 | 368 // test ensuring that the internal action of clearing some hashes does |
| 369 // update the stored hash of hashes). | 369 // update the stored hash of hashes). |
| 370 PrefHashStoreImpl pref_hash_store2( | 370 PrefHashStoreImpl pref_hash_store2( |
| 371 std::string(32, 0), "device_id", CreateHashStoreContents()); | 371 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 372 scoped_ptr<PrefHashStoreTransaction> transaction( | 372 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 373 pref_hash_store2.BeginTransaction()); | 373 pref_hash_store2.BeginTransaction()); |
| 374 | 374 |
| 375 base::DictionaryValue tested_dict; | 375 base::DictionaryValue tested_dict; |
| 376 tested_dict.Set("a", new base::StringValue("foo")); | 376 tested_dict.Set("a", new base::StringValue("foo")); |
| 377 tested_dict.Set("b", new base::StringValue("bar")); | 377 tested_dict.Set("b", new base::StringValue("bar")); |
| 378 EXPECT_EQ( | 378 EXPECT_EQ( |
| 379 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 379 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 380 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys)); | 380 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys)); |
| 381 EXPECT_TRUE(invalid_keys.empty()); | 381 EXPECT_TRUE(invalid_keys.empty()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 392 base::StringValue string("string1"); | 392 base::StringValue string("string1"); |
| 393 | 393 |
| 394 base::DictionaryValue dict; | 394 base::DictionaryValue dict; |
| 395 dict.Set("a", new base::StringValue("foo")); | 395 dict.Set("a", new base::StringValue("foo")); |
| 396 dict.Set("d", new base::StringValue("bad")); | 396 dict.Set("d", new base::StringValue("bad")); |
| 397 dict.Set("b", new base::StringValue("bar")); | 397 dict.Set("b", new base::StringValue("bar")); |
| 398 dict.Set("c", new base::StringValue("baz")); | 398 dict.Set("c", new base::StringValue("baz")); |
| 399 | 399 |
| 400 { | 400 { |
| 401 PrefHashStoreImpl pref_hash_store( | 401 PrefHashStoreImpl pref_hash_store( |
| 402 std::string(32, 0), "device_id", CreateHashStoreContents()); | 402 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 403 scoped_ptr<PrefHashStoreTransaction> transaction( | 403 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 404 pref_hash_store.BeginTransaction()); | 404 pref_hash_store.BeginTransaction()); |
| 405 | 405 |
| 406 transaction->StoreHash("path1", &string); | 406 transaction->StoreHash("path1", &string); |
| 407 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 407 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| 408 transaction->CheckValue("path1", &string)); | 408 transaction->CheckValue("path1", &string)); |
| 409 } | 409 } |
| 410 | 410 |
| 411 { | 411 { |
| 412 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted. | 412 // Load a new |pref_hash_store2| in which the hashes dictionary is trusted. |
| 413 PrefHashStoreImpl pref_hash_store2( | 413 PrefHashStoreImpl pref_hash_store2( |
| 414 std::string(32, 0), "device_id", CreateHashStoreContents()); | 414 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 415 scoped_ptr<PrefHashStoreTransaction> transaction( | 415 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 416 pref_hash_store2.BeginTransaction()); | 416 pref_hash_store2.BeginTransaction()); |
| 417 std::vector<std::string> invalid_keys; | 417 std::vector<std::string> invalid_keys; |
| 418 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 418 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 419 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); | 419 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); |
| 420 EXPECT_TRUE(invalid_keys.empty()); | 420 EXPECT_TRUE(invalid_keys.empty()); |
| 421 } | 421 } |
| 422 } | 422 } |
| OLD | NEW |