OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/prefs/pref_hash_calculator.h" |
| 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/values.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 TEST(PrefHashCalculatorTest, TestCurrentAlgorithm) { |
| 13 base::StringValue string_value_1("string value 1"); |
| 14 base::StringValue string_value_2("string value 2"); |
| 15 base::DictionaryValue dictionary_value_1; |
| 16 dictionary_value_1.SetInteger("int value", 1); |
| 17 dictionary_value_1.Set("nested empty map", new DictionaryValue); |
| 18 base::DictionaryValue dictionary_value_1_equivalent; |
| 19 dictionary_value_1_equivalent.SetInteger("int value", 1); |
| 20 base::DictionaryValue dictionary_value_2; |
| 21 dictionary_value_2.SetInteger("int value", 2); |
| 22 |
| 23 PrefHashCalculator calc1("seed1", "deviceid"); |
| 24 PrefHashCalculator calc1_dup("seed1", "deviceid"); |
| 25 PrefHashCalculator calc2("seed2", "deviceid"); |
| 26 PrefHashCalculator calc3("seed1", "deviceid2"); |
| 27 |
| 28 // Two calculators with same seed produce same hash. |
| 29 ASSERT_EQ(calc1.Calculate("pref_path", &string_value_1), |
| 30 calc1_dup.Calculate("pref_path", &string_value_1)); |
| 31 ASSERT_EQ(PrefHashCalculator::VALID, |
| 32 calc1_dup.Validate( |
| 33 "pref_path", |
| 34 &string_value_1, |
| 35 calc1.Calculate("pref_path", &string_value_1))); |
| 36 |
| 37 // Different seeds, different hashes. |
| 38 ASSERT_NE(calc1.Calculate("pref_path", &string_value_1), |
| 39 calc2.Calculate("pref_path", &string_value_1)); |
| 40 ASSERT_EQ(PrefHashCalculator::INVALID, |
| 41 calc2.Validate( |
| 42 "pref_path", |
| 43 &string_value_1, |
| 44 calc1.Calculate("pref_path", &string_value_1))); |
| 45 |
| 46 // Different device IDs, different hashes. |
| 47 ASSERT_NE(calc1.Calculate("pref_path", &string_value_1), |
| 48 calc3.Calculate("pref_path", &string_value_1)); |
| 49 |
| 50 // Different values, different hashes. |
| 51 ASSERT_NE(calc1.Calculate("pref_path", &string_value_1), |
| 52 calc1.Calculate("pref_path", &string_value_2)); |
| 53 |
| 54 // Different paths, different hashes. |
| 55 ASSERT_NE(calc1.Calculate("pref_path", &string_value_1), |
| 56 calc1.Calculate("pref_path_2", &string_value_1)); |
| 57 |
| 58 // Works for dictionaries. |
| 59 ASSERT_EQ(calc1.Calculate("pref_path", &dictionary_value_1), |
| 60 calc1.Calculate("pref_path", &dictionary_value_1)); |
| 61 ASSERT_NE(calc1.Calculate("pref_path", &dictionary_value_1), |
| 62 calc1.Calculate("pref_path", &dictionary_value_2)); |
| 63 |
| 64 // Empty dictionary children are pruned. |
| 65 ASSERT_EQ(calc1.Calculate("pref_path", &dictionary_value_1), |
| 66 calc1.Calculate("pref_path", &dictionary_value_1_equivalent)); |
| 67 |
| 68 // NULL value is supported. |
| 69 ASSERT_EQ(calc1.Calculate("pref_path", NULL), |
| 70 calc1.Calculate("pref_path", NULL)); |
| 71 } |
| 72 |
| 73 TEST(PrefHashCalculatorTest, TestLegacyAlgorithm) { |
| 74 DictionaryValue dict; |
| 75 dict.Set("a", new StringValue("foo")); |
| 76 dict.Set("d", new StringValue("bad")); |
| 77 dict.Set("b", new StringValue("bar")); |
| 78 dict.Set("c", new StringValue("baz")); |
| 79 |
| 80 // 32 NULL bytes is the seed that was used to generate the legacy hash. |
| 81 EXPECT_EQ(PrefHashCalculator::VALID_LEGACY, |
| 82 PrefHashCalculator(std::string(32u, 0), "deviceid").Validate( |
| 83 "pref.path1", |
| 84 &dict, |
| 85 "C503FB7C65EEFD5C07185F616A0AA679" |
| 86 "23C069909933F362022B1F187E73E9A2")); |
| 87 } |
OLD | NEW |