| 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 13 matching lines...) Expand all Loading... |
| 24 // Returns the current value of |commit_performed| and resets it to false | 24 // Returns the current value of |commit_performed| and resets it to false |
| 25 // immediately after. | 25 // immediately after. |
| 26 bool GetCommitPerformedAndReset() { | 26 bool GetCommitPerformedAndReset() { |
| 27 bool current_commit_performed = commit_performed; | 27 bool current_commit_performed = commit_performed; |
| 28 commit_performed = false; | 28 commit_performed = false; |
| 29 return current_commit_performed; | 29 return current_commit_performed; |
| 30 } | 30 } |
| 31 | 31 |
| 32 scoped_ptr<base::DictionaryValue> contents; | 32 scoped_ptr<base::DictionaryValue> contents; |
| 33 std::string super_mac; | 33 std::string super_mac; |
| 34 scoped_ptr<int> version; | |
| 35 bool commit_performed; | 34 bool commit_performed; |
| 36 }; | 35 }; |
| 37 | 36 |
| 38 explicit MockHashStoreContents(Data* data) : data_(data) {} | 37 explicit MockHashStoreContents(Data* data) : data_(data) {} |
| 39 | 38 |
| 40 // HashStoreContents implementation | 39 // HashStoreContents implementation |
| 41 virtual std::string hash_store_id() const OVERRIDE { return "store_id"; } | 40 virtual std::string hash_store_id() const OVERRIDE { return "store_id"; } |
| 42 | 41 |
| 43 virtual void Reset() OVERRIDE { | 42 virtual void Reset() OVERRIDE { |
| 44 data_->contents.reset(); | 43 data_->contents.reset(); |
| 45 data_->super_mac = ""; | 44 data_->super_mac = ""; |
| 46 data_->version.reset(); | |
| 47 } | 45 } |
| 48 | 46 |
| 49 virtual bool IsInitialized() const OVERRIDE { return data_->contents; } | 47 virtual bool IsInitialized() const OVERRIDE { return data_->contents; } |
| 50 | 48 |
| 51 virtual bool GetVersion(int* version) const OVERRIDE { | |
| 52 if (data_->version) | |
| 53 *version = *data_->version; | |
| 54 return data_->version; | |
| 55 } | |
| 56 | |
| 57 virtual void SetVersion(int version) OVERRIDE { | |
| 58 data_->version.reset(new int(version)); | |
| 59 } | |
| 60 | |
| 61 virtual const base::DictionaryValue* GetContents() const OVERRIDE { | 49 virtual const base::DictionaryValue* GetContents() const OVERRIDE { |
| 62 return data_->contents.get(); | 50 return data_->contents.get(); |
| 63 } | 51 } |
| 64 | 52 |
| 65 virtual scoped_ptr<MutableDictionary> GetMutableContents() OVERRIDE { | 53 virtual scoped_ptr<MutableDictionary> GetMutableContents() OVERRIDE { |
| 66 return scoped_ptr<MutableDictionary>(new MockMutableDictionary(data_)); | 54 return scoped_ptr<MutableDictionary>(new MockMutableDictionary(data_)); |
| 67 } | 55 } |
| 68 | 56 |
| 69 virtual std::string GetSuperMac() const OVERRIDE { return data_->super_mac; } | 57 virtual std::string GetSuperMac() const OVERRIDE { return data_->super_mac; } |
| 70 | 58 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 MockHashStoreContents::Data hash_store_data_; | 97 MockHashStoreContents::Data hash_store_data_; |
| 110 }; | 98 }; |
| 111 | 99 |
| 112 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) { | 100 TEST_F(PrefHashStoreImplTest, AtomicHashStoreAndCheck) { |
| 113 base::StringValue string_1("string1"); | 101 base::StringValue string_1("string1"); |
| 114 base::StringValue string_2("string2"); | 102 base::StringValue string_2("string2"); |
| 115 | 103 |
| 116 { | 104 { |
| 117 // 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. |
| 118 PrefHashStoreImpl pref_hash_store( | 106 PrefHashStoreImpl pref_hash_store( |
| 119 std::string(32, 0), "device_id", CreateHashStoreContents()); | 107 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 120 scoped_ptr<PrefHashStoreTransaction> transaction( | 108 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 121 pref_hash_store.BeginTransaction()); | 109 pref_hash_store.BeginTransaction()); |
| 122 | 110 |
| 123 // Only NULL should be trusted in the absence of a hash. | 111 // Only NULL should be trusted in the absence of a hash. |
| 124 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 112 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| 125 transaction->CheckValue("path1", &string_1)); | 113 transaction->CheckValue("path1", &string_1)); |
| 126 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 114 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 127 transaction->CheckValue("path1", NULL)); | 115 transaction->CheckValue("path1", NULL)); |
| 128 | 116 |
| 129 transaction->StoreHash("path1", &string_1); | 117 transaction->StoreHash("path1", &string_1); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 154 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 142 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| 155 transaction->CheckValue("path1", &dict)); | 143 transaction->CheckValue("path1", &dict)); |
| 156 | 144 |
| 157 // 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 |
| 158 // transaction. | 146 // transaction. |
| 159 transaction.reset(); | 147 transaction.reset(); |
| 160 pref_hash_store.CommitPendingWrite(); | 148 pref_hash_store.CommitPendingWrite(); |
| 161 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); | 149 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); |
| 162 } | 150 } |
| 163 | 151 |
| 152 ASSERT_FALSE(CreateHashStoreContents()->GetSuperMac().empty()); |
| 153 |
| 164 { | 154 { |
| 165 // |pref_hash_store2| should trust its initial hashes dictionary and thus | 155 // |pref_hash_store2| should trust its initial hashes dictionary and thus |
| 166 // trust new unknown values. | 156 // trust new unknown values. |
| 167 PrefHashStoreImpl pref_hash_store2( | 157 PrefHashStoreImpl pref_hash_store2( |
| 168 std::string(32, 0), "device_id", CreateHashStoreContents()); | 158 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 169 scoped_ptr<PrefHashStoreTransaction> transaction( | 159 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 170 pref_hash_store2.BeginTransaction()); | 160 pref_hash_store2.BeginTransaction()); |
| 171 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 161 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 172 transaction->CheckValue("new_path", &string_1)); | 162 transaction->CheckValue("new_path", &string_1)); |
| 173 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 163 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 174 transaction->CheckValue("new_path", &string_2)); | 164 transaction->CheckValue("new_path", &string_2)); |
| 175 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 165 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 176 transaction->CheckValue("new_path", NULL)); | 166 transaction->CheckValue("new_path", NULL)); |
| 177 | 167 |
| 178 // 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 |
| 179 // didn't change. | 169 // didn't change. |
| 180 transaction.reset(); | 170 transaction.reset(); |
| 181 pref_hash_store2.CommitPendingWrite(); | 171 pref_hash_store2.CommitPendingWrite(); |
| 182 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | 172 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); |
| 183 } | 173 } |
| 184 | 174 |
| 185 // Manually corrupt the super MAC. | 175 // Manually corrupt the super MAC. |
| 186 hash_store_data_.super_mac = std::string(64, 'A'); | 176 hash_store_data_.super_mac = std::string(64, 'A'); |
| 187 | 177 |
| 188 { | 178 { |
| 189 // |pref_hash_store3| should no longer trust its initial hashes dictionary | 179 // |pref_hash_store3| should no longer trust its initial hashes dictionary |
| 190 // and thus shouldn't trust non-NULL unknown values. | 180 // and thus shouldn't trust non-NULL unknown values. |
| 191 PrefHashStoreImpl pref_hash_store3( | 181 PrefHashStoreImpl pref_hash_store3( |
| 192 std::string(32, 0), "device_id", CreateHashStoreContents()); | 182 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 193 scoped_ptr<PrefHashStoreTransaction> transaction( | 183 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 194 pref_hash_store3.BeginTransaction()); | 184 pref_hash_store3.BeginTransaction()); |
| 195 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 185 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| 196 transaction->CheckValue("new_path", &string_1)); | 186 transaction->CheckValue("new_path", &string_1)); |
| 197 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 187 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| 198 transaction->CheckValue("new_path", &string_2)); | 188 transaction->CheckValue("new_path", &string_2)); |
| 199 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 189 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 200 transaction->CheckValue("new_path", NULL)); | 190 transaction->CheckValue("new_path", NULL)); |
| 201 | 191 |
| 202 // 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 |
| 203 // didn't change. | 193 // didn't change. |
| 204 transaction.reset(); | 194 transaction.reset(); |
| 205 pref_hash_store3.CommitPendingWrite(); | 195 pref_hash_store3.CommitPendingWrite(); |
| 206 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | 196 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); |
| 207 } | 197 } |
| 208 } | 198 } |
| 209 | 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 |
| 210 TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) { | 228 TEST_F(PrefHashStoreImplTest, SplitHashStoreAndCheck) { |
| 211 base::DictionaryValue dict; | 229 base::DictionaryValue dict; |
| 212 dict.Set("a", new base::StringValue("to be replaced")); | 230 dict.Set("a", new base::StringValue("to be replaced")); |
| 213 dict.Set("b", new base::StringValue("same")); | 231 dict.Set("b", new base::StringValue("same")); |
| 214 dict.Set("o", new base::StringValue("old")); | 232 dict.Set("o", new base::StringValue("old")); |
| 215 | 233 |
| 216 base::DictionaryValue modified_dict; | 234 base::DictionaryValue modified_dict; |
| 217 modified_dict.Set("a", new base::StringValue("replaced")); | 235 modified_dict.Set("a", new base::StringValue("replaced")); |
| 218 modified_dict.Set("b", new base::StringValue("same")); | 236 modified_dict.Set("b", new base::StringValue("same")); |
| 219 modified_dict.Set("c", new base::StringValue("new")); | 237 modified_dict.Set("c", new base::StringValue("new")); |
| 220 | 238 |
| 221 base::DictionaryValue empty_dict; | 239 base::DictionaryValue empty_dict; |
| 222 | 240 |
| 223 std::vector<std::string> invalid_keys; | 241 std::vector<std::string> invalid_keys; |
| 224 | 242 |
| 225 { | 243 { |
| 226 PrefHashStoreImpl pref_hash_store( | 244 PrefHashStoreImpl pref_hash_store( |
| 227 std::string(32, 0), "device_id", CreateHashStoreContents()); | 245 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 228 scoped_ptr<PrefHashStoreTransaction> transaction( | 246 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 229 pref_hash_store.BeginTransaction()); | 247 pref_hash_store.BeginTransaction()); |
| 230 | 248 |
| 231 // 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 |
| 232 // trusted). | 250 // trusted). |
| 233 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 251 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| 234 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); | 252 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); |
| 235 EXPECT_TRUE(invalid_keys.empty()); | 253 EXPECT_TRUE(invalid_keys.empty()); |
| 236 | 254 |
| 237 transaction->StoreSplitHash("path1", &dict); | 255 transaction->StoreSplitHash("path1", &dict); |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 // Test that the |pref_hash_store| flushes its changes on request. | 311 // Test that the |pref_hash_store| flushes its changes on request. |
| 294 transaction.reset(); | 312 transaction.reset(); |
| 295 pref_hash_store.CommitPendingWrite(); | 313 pref_hash_store.CommitPendingWrite(); |
| 296 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); | 314 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); |
| 297 } | 315 } |
| 298 | 316 |
| 299 { | 317 { |
| 300 // |pref_hash_store2| should trust its initial hashes dictionary and thus | 318 // |pref_hash_store2| should trust its initial hashes dictionary and thus |
| 301 // trust new unknown values. | 319 // trust new unknown values. |
| 302 PrefHashStoreImpl pref_hash_store2( | 320 PrefHashStoreImpl pref_hash_store2( |
| 303 std::string(32, 0), "device_id", CreateHashStoreContents()); | 321 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 304 scoped_ptr<PrefHashStoreTransaction> transaction( | 322 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 305 pref_hash_store2.BeginTransaction()); | 323 pref_hash_store2.BeginTransaction()); |
| 306 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 324 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 307 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); | 325 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); |
| 308 EXPECT_TRUE(invalid_keys.empty()); | 326 EXPECT_TRUE(invalid_keys.empty()); |
| 309 | 327 |
| 310 // 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 |
| 311 // didn't change. | 329 // didn't change. |
| 312 transaction.reset(); | 330 transaction.reset(); |
| 313 pref_hash_store2.CommitPendingWrite(); | 331 pref_hash_store2.CommitPendingWrite(); |
| 314 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | 332 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); |
| 315 } | 333 } |
| 316 | 334 |
| 317 // Manually corrupt the super MAC. | 335 // Manually corrupt the super MAC. |
| 318 hash_store_data_.super_mac = std::string(64, 'A'); | 336 hash_store_data_.super_mac = std::string(64, 'A'); |
| 319 | 337 |
| 320 { | 338 { |
| 321 // |pref_hash_store3| should no longer trust its initial hashes dictionary | 339 // |pref_hash_store3| should no longer trust its initial hashes dictionary |
| 322 // and thus shouldn't trust unknown values. | 340 // and thus shouldn't trust unknown values. |
| 323 PrefHashStoreImpl pref_hash_store3( | 341 PrefHashStoreImpl pref_hash_store3( |
| 324 std::string(32, 0), "device_id", CreateHashStoreContents()); | 342 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 325 scoped_ptr<PrefHashStoreTransaction> transaction( | 343 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 326 pref_hash_store3.BeginTransaction()); | 344 pref_hash_store3.BeginTransaction()); |
| 327 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, | 345 EXPECT_EQ(PrefHashStoreTransaction::UNTRUSTED_UNKNOWN_VALUE, |
| 328 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); | 346 transaction->CheckSplitValue("new_path", &dict, &invalid_keys)); |
| 329 EXPECT_TRUE(invalid_keys.empty()); | 347 EXPECT_TRUE(invalid_keys.empty()); |
| 330 | 348 |
| 331 // 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 |
| 332 // didn't change. | 350 // didn't change. |
| 333 transaction.reset(); | 351 transaction.reset(); |
| 334 pref_hash_store3.CommitPendingWrite(); | 352 pref_hash_store3.CommitPendingWrite(); |
| 335 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | 353 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); |
| 336 } | 354 } |
| 337 } | 355 } |
| 338 | 356 |
| 339 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) { | 357 TEST_F(PrefHashStoreImplTest, EmptyAndNULLSplitDict) { |
| 340 base::DictionaryValue empty_dict; | 358 base::DictionaryValue empty_dict; |
| 341 | 359 |
| 342 std::vector<std::string> invalid_keys; | 360 std::vector<std::string> invalid_keys; |
| 343 | 361 |
| 344 { | 362 { |
| 345 PrefHashStoreImpl pref_hash_store( | 363 PrefHashStoreImpl pref_hash_store( |
| 346 std::string(32, 0), "device_id", CreateHashStoreContents()); | 364 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 347 scoped_ptr<PrefHashStoreTransaction> transaction( | 365 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 348 pref_hash_store.BeginTransaction()); | 366 pref_hash_store.BeginTransaction()); |
| 349 | 367 |
| 350 // Store hashes for a random dict to be overwritten below. | 368 // Store hashes for a random dict to be overwritten below. |
| 351 base::DictionaryValue initial_dict; | 369 base::DictionaryValue initial_dict; |
| 352 initial_dict.Set("a", new base::StringValue("foo")); | 370 initial_dict.Set("a", new base::StringValue("foo")); |
| 353 transaction->StoreSplitHash("path1", &initial_dict); | 371 transaction->StoreSplitHash("path1", &initial_dict); |
| 354 | 372 |
| 355 // Verify stored empty dictionary matches NULL and empty dictionary back. | 373 // Verify stored empty dictionary matches NULL and empty dictionary back. |
| 356 transaction->StoreSplitHash("path1", &empty_dict); | 374 transaction->StoreSplitHash("path1", &empty_dict); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 373 EXPECT_TRUE(invalid_keys.empty()); | 391 EXPECT_TRUE(invalid_keys.empty()); |
| 374 } | 392 } |
| 375 | 393 |
| 376 { | 394 { |
| 377 // |pref_hash_store2| should trust its initial hashes dictionary (and thus | 395 // |pref_hash_store2| should trust its initial hashes dictionary (and thus |
| 378 // 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 |
| 379 // 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 |
| 380 // test ensuring that the internal action of clearing some hashes does | 398 // test ensuring that the internal action of clearing some hashes does |
| 381 // update the stored hash of hashes). | 399 // update the stored hash of hashes). |
| 382 PrefHashStoreImpl pref_hash_store2( | 400 PrefHashStoreImpl pref_hash_store2( |
| 383 std::string(32, 0), "device_id", CreateHashStoreContents()); | 401 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 384 scoped_ptr<PrefHashStoreTransaction> transaction( | 402 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 385 pref_hash_store2.BeginTransaction()); | 403 pref_hash_store2.BeginTransaction()); |
| 386 | 404 |
| 387 base::DictionaryValue tested_dict; | 405 base::DictionaryValue tested_dict; |
| 388 tested_dict.Set("a", new base::StringValue("foo")); | 406 tested_dict.Set("a", new base::StringValue("foo")); |
| 389 tested_dict.Set("b", new base::StringValue("bar")); | 407 tested_dict.Set("b", new base::StringValue("bar")); |
| 390 EXPECT_EQ( | 408 EXPECT_EQ( |
| 391 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 409 PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 392 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys)); | 410 transaction->CheckSplitValue("new_path", &tested_dict, &invalid_keys)); |
| 393 EXPECT_TRUE(invalid_keys.empty()); | 411 EXPECT_TRUE(invalid_keys.empty()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 404 base::StringValue string("string1"); | 422 base::StringValue string("string1"); |
| 405 | 423 |
| 406 base::DictionaryValue dict; | 424 base::DictionaryValue dict; |
| 407 dict.Set("a", new base::StringValue("foo")); | 425 dict.Set("a", new base::StringValue("foo")); |
| 408 dict.Set("d", new base::StringValue("bad")); | 426 dict.Set("d", new base::StringValue("bad")); |
| 409 dict.Set("b", new base::StringValue("bar")); | 427 dict.Set("b", new base::StringValue("bar")); |
| 410 dict.Set("c", new base::StringValue("baz")); | 428 dict.Set("c", new base::StringValue("baz")); |
| 411 | 429 |
| 412 { | 430 { |
| 413 PrefHashStoreImpl pref_hash_store( | 431 PrefHashStoreImpl pref_hash_store( |
| 414 std::string(32, 0), "device_id", CreateHashStoreContents()); | 432 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 415 scoped_ptr<PrefHashStoreTransaction> transaction( | 433 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 416 pref_hash_store.BeginTransaction()); | 434 pref_hash_store.BeginTransaction()); |
| 417 | 435 |
| 418 transaction->StoreHash("path1", &string); | 436 transaction->StoreHash("path1", &string); |
| 419 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, | 437 EXPECT_EQ(PrefHashStoreTransaction::UNCHANGED, |
| 420 transaction->CheckValue("path1", &string)); | 438 transaction->CheckValue("path1", &string)); |
| 421 } | 439 } |
| 422 | 440 |
| 423 { | 441 { |
| 424 // 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. |
| 425 PrefHashStoreImpl pref_hash_store2( | 443 PrefHashStoreImpl pref_hash_store2( |
| 426 std::string(32, 0), "device_id", CreateHashStoreContents()); | 444 std::string(32, 0), "device_id", CreateHashStoreContents(), true); |
| 427 scoped_ptr<PrefHashStoreTransaction> transaction( | 445 scoped_ptr<PrefHashStoreTransaction> transaction( |
| 428 pref_hash_store2.BeginTransaction()); | 446 pref_hash_store2.BeginTransaction()); |
| 429 std::vector<std::string> invalid_keys; | 447 std::vector<std::string> invalid_keys; |
| 430 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, | 448 EXPECT_EQ(PrefHashStoreTransaction::TRUSTED_UNKNOWN_VALUE, |
| 431 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); | 449 transaction->CheckSplitValue("path1", &dict, &invalid_keys)); |
| 432 EXPECT_TRUE(invalid_keys.empty()); | 450 EXPECT_TRUE(invalid_keys.empty()); |
| 433 } | 451 } |
| 434 } | 452 } |
| 435 | |
| 436 TEST_F(PrefHashStoreImplTest, GetCurrentVersion) { | |
| 437 COMPILE_ASSERT(PrefHashStoreImpl::VERSION_LATEST == 2, | |
| 438 new_versions_should_be_tested_here); | |
| 439 { | |
| 440 PrefHashStoreImpl pref_hash_store( | |
| 441 std::string(32, 0), "device_id", CreateHashStoreContents()); | |
| 442 | |
| 443 // VERSION_UNINITIALIZED when no hashes are stored. | |
| 444 EXPECT_EQ(PrefHashStoreImpl::VERSION_UNINITIALIZED, | |
| 445 pref_hash_store.GetCurrentVersion()); | |
| 446 | |
| 447 scoped_ptr<PrefHashStoreTransaction> transaction( | |
| 448 pref_hash_store.BeginTransaction()); | |
| 449 base::StringValue string_value("foo"); | |
| 450 transaction->StoreHash("path1", &string_value); | |
| 451 | |
| 452 // Test that |pref_hash_store| flushes its content to disk when it | |
| 453 // initializes its version. | |
| 454 transaction.reset(); | |
| 455 pref_hash_store.CommitPendingWrite(); | |
| 456 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); | |
| 457 } | |
| 458 { | |
| 459 PrefHashStoreImpl pref_hash_store( | |
| 460 std::string(32, 0), "device_id", CreateHashStoreContents()); | |
| 461 | |
| 462 // VERSION_LATEST after storing a hash. | |
| 463 EXPECT_EQ(PrefHashStoreImpl::VERSION_LATEST, | |
| 464 pref_hash_store.GetCurrentVersion()); | |
| 465 | |
| 466 // Test that |pref_hash_store| doesn't flush its contents to disk when it | |
| 467 // didn't change. | |
| 468 pref_hash_store.CommitPendingWrite(); | |
| 469 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | |
| 470 } | |
| 471 | |
| 472 // Manually clear the version number. | |
| 473 hash_store_data_.version.reset(); | |
| 474 | |
| 475 { | |
| 476 PrefHashStoreImpl pref_hash_store( | |
| 477 std::string(32, 0), "device_id", CreateHashStoreContents()); | |
| 478 | |
| 479 // VERSION_PRE_MIGRATION when no version is stored. | |
| 480 EXPECT_EQ(PrefHashStoreImpl::VERSION_PRE_MIGRATION, | |
| 481 pref_hash_store.GetCurrentVersion()); | |
| 482 | |
| 483 scoped_ptr<PrefHashStoreTransaction> transaction( | |
| 484 pref_hash_store.BeginTransaction()); | |
| 485 | |
| 486 // Test that |pref_hash_store| flushes its content to disk when it | |
| 487 // re-initializes its version. | |
| 488 transaction.reset(); | |
| 489 pref_hash_store.CommitPendingWrite(); | |
| 490 EXPECT_TRUE(hash_store_data_.GetCommitPerformedAndReset()); | |
| 491 } | |
| 492 { | |
| 493 PrefHashStoreImpl pref_hash_store( | |
| 494 std::string(32, 0), "device_id", CreateHashStoreContents()); | |
| 495 | |
| 496 // Back to VERSION_LATEST after performing a transaction from | |
| 497 // VERSION_PRE_MIGRATION (the presence of an existing hash should be | |
| 498 // sufficient, no need for the transaction itself to perform any work). | |
| 499 EXPECT_EQ(PrefHashStoreImpl::VERSION_LATEST, | |
| 500 pref_hash_store.GetCurrentVersion()); | |
| 501 | |
| 502 // Test that |pref_hash_store| doesn't flush its contents to disk when it | |
| 503 // didn't change (i.e., its version was already up-to-date and the only | |
| 504 // transaction performed was empty). | |
| 505 scoped_ptr<PrefHashStoreTransaction> transaction( | |
| 506 pref_hash_store.BeginTransaction()); | |
| 507 transaction.reset(); | |
| 508 pref_hash_store.CommitPendingWrite(); | |
| 509 EXPECT_FALSE(hash_store_data_.GetCommitPerformedAndReset()); | |
| 510 } | |
| 511 } | |
| OLD | NEW |