Chromium Code Reviews| 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_calculator.h" | 5 #include "chrome/browser/prefs/pref_hash_calculator.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/values.h" | 9 #include "base/values.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 namespace { | |
| 13 const char kSeed[] = "0123456789ABCDEF0123456789ABCDEF"; | |
|
Bernhard Bauer
2014/01/07 19:20:45
The lone namespace here is unnecessary; const vari
| |
| 14 } // namespace | |
| 15 | |
| 12 TEST(PrefHashCalculatorTest, TestCurrentAlgorithm) { | 16 TEST(PrefHashCalculatorTest, TestCurrentAlgorithm) { |
| 13 base::StringValue string_value_1("string value 1"); | 17 base::StringValue string_value_1("string value 1"); |
| 14 base::StringValue string_value_2("string value 2"); | 18 base::StringValue string_value_2("string value 2"); |
| 15 base::DictionaryValue dictionary_value_1; | 19 base::DictionaryValue dictionary_value_1; |
| 16 dictionary_value_1.SetInteger("int value", 1); | 20 dictionary_value_1.SetInteger("int value", 1); |
| 17 dictionary_value_1.Set("nested empty map", new DictionaryValue); | 21 dictionary_value_1.Set("nested empty map", new DictionaryValue); |
| 18 base::DictionaryValue dictionary_value_1_equivalent; | 22 base::DictionaryValue dictionary_value_1_equivalent; |
| 19 dictionary_value_1_equivalent.SetInteger("int value", 1); | 23 dictionary_value_1_equivalent.SetInteger("int value", 1); |
| 20 base::DictionaryValue dictionary_value_2; | 24 base::DictionaryValue dictionary_value_2; |
| 21 dictionary_value_2.SetInteger("int value", 2); | 25 dictionary_value_2.SetInteger("int value", 2); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 67 | 71 |
| 68 // NULL value is supported. | 72 // NULL value is supported. |
| 69 ASSERT_FALSE(calc1.Calculate("pref_path", NULL).empty()); | 73 ASSERT_FALSE(calc1.Calculate("pref_path", NULL).empty()); |
| 70 } | 74 } |
| 71 | 75 |
| 72 // Tests the output against a known value to catch unexpected algorithm changes. | 76 // Tests the output against a known value to catch unexpected algorithm changes. |
| 73 TEST(PrefHashCalculatorTest, CatchHashChanges) { | 77 TEST(PrefHashCalculatorTest, CatchHashChanges) { |
| 74 const char* kDeviceId = "test_device_id1"; | 78 const char* kDeviceId = "test_device_id1"; |
| 75 { | 79 { |
| 76 static const char kExpectedValue[] = | 80 static const char kExpectedValue[] = |
| 77 "5CE37D7EBCBC9BE510F0F5E7C326CA92C1673713C3717839610AEA1A217D8BB8"; | 81 "D8137B8E767D3D910DCD3821CAC61D26ABB042E6EC406AEB0E347ED73A3A4EC1"; |
| 78 | 82 |
| 79 base::ListValue list; | 83 base::ListValue list; |
| 80 list.Set(0, new base::FundamentalValue(true)); | 84 list.Set(0, new base::FundamentalValue(true)); |
| 81 list.Set(1, new base::FundamentalValue(100)); | 85 list.Set(1, new base::FundamentalValue(100)); |
| 82 list.Set(2, new base::FundamentalValue(1.0)); | 86 list.Set(2, new base::FundamentalValue(1.0)); |
| 83 | 87 |
| 84 // 32 NULL bytes is the seed that was used to generate the hash in old | |
| 85 // tests. Use it again to ensure that we haven't altered the algorithm. | |
| 86 EXPECT_EQ(PrefHashCalculator::VALID, | 88 EXPECT_EQ(PrefHashCalculator::VALID, |
| 87 PrefHashCalculator(std::string(32u, 0), kDeviceId).Validate( | 89 PrefHashCalculator(kSeed, kDeviceId).Validate( |
| 88 "pref.path2", &list, kExpectedValue)); | 90 "pref.path2", &list, kExpectedValue)); |
| 89 } | 91 } |
| 90 { | 92 { |
| 91 static const char kExpectedValue[] = | 93 static const char kExpectedValue[] = |
| 92 "A50FE7EB31BFBC32B8A27E71730AF15421178A9B5815644ACE174B18966735B9"; | 94 "3F947A044DE9E421A735525385B4C789693682E6F6E3E4CB4741E58724B28F96"; |
| 93 | 95 |
| 94 DictionaryValue dict; | 96 DictionaryValue dict; |
| 95 dict.Set("a", new StringValue("foo")); | 97 dict.Set("a", new StringValue("foo")); |
| 96 dict.Set("d", new StringValue("bad")); | 98 dict.Set("d", new StringValue("bad")); |
| 97 dict.Set("b", new StringValue("bar")); | 99 dict.Set("b", new StringValue("bar")); |
| 98 dict.Set("c", new StringValue("baz")); | 100 dict.Set("c", new StringValue("baz")); |
| 99 | 101 |
| 100 // 32 NULL bytes is the seed that was used to generate the hash in old | |
| 101 // tests. Use it again to ensure that we haven't altered the algorithm. | |
| 102 EXPECT_EQ(PrefHashCalculator::VALID, | 102 EXPECT_EQ(PrefHashCalculator::VALID, |
| 103 PrefHashCalculator(std::string(32u, 0), kDeviceId).Validate( | 103 PrefHashCalculator(kSeed, kDeviceId).Validate( |
| 104 "pref.path1", &dict, kExpectedValue)); | 104 "pref.path1", &dict, kExpectedValue)); |
| 105 } | 105 } |
| 106 } | 106 } |
| 107 | 107 |
| 108 TEST(PrefHashCalculatorTest, TestLegacyAlgorithm) { | 108 TEST(PrefHashCalculatorTest, TestLegacyAlgorithm) { |
| 109 const char* kExpectedValue = | 109 const char* kExpectedValue = |
| 110 "C503FB7C65EEFD5C07185F616A0AA67923C069909933F362022B1F187E73E9A2"; | 110 "C503FB7C65EEFD5C07185F616A0AA67923C069909933F362022B1F187E73E9A2"; |
| 111 const char* kDeviceId = "deviceid"; | 111 const char* kDeviceId = "not_used"; |
| 112 | 112 |
| 113 DictionaryValue dict; | 113 DictionaryValue dict; |
| 114 dict.Set("a", new StringValue("foo")); | 114 dict.Set("a", new StringValue("foo")); |
| 115 dict.Set("d", new StringValue("bad")); | 115 dict.Set("d", new StringValue("bad")); |
| 116 dict.Set("b", new StringValue("bar")); | 116 dict.Set("b", new StringValue("bar")); |
| 117 dict.Set("c", new StringValue("baz")); | 117 dict.Set("c", new StringValue("baz")); |
| 118 | 118 |
| 119 // 32 NULL bytes is the seed that was used to generate the legacy hash. | 119 // 32 NULL bytes is the seed that was used to generate the legacy hash. |
| 120 EXPECT_EQ(PrefHashCalculator::VALID_LEGACY, | 120 EXPECT_EQ(PrefHashCalculator::VALID_LEGACY, |
| 121 PrefHashCalculator(std::string(32u, 0), kDeviceId).Validate( | 121 PrefHashCalculator(std::string(32u, 0), kDeviceId).Validate( |
| 122 "pref.path1", &dict, kExpectedValue)); | 122 "pref.path1", &dict, kExpectedValue)); |
| 123 | |
| 124 } | 123 } |
| OLD | NEW |