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" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 // Empty dictionary children are pruned. | 64 // Empty dictionary children are pruned. |
65 ASSERT_EQ(calc1.Calculate("pref_path", &dictionary_value_1), | 65 ASSERT_EQ(calc1.Calculate("pref_path", &dictionary_value_1), |
66 calc1.Calculate("pref_path", &dictionary_value_1_equivalent)); | 66 calc1.Calculate("pref_path", &dictionary_value_1_equivalent)); |
67 | 67 |
68 // NULL value is supported. | 68 // NULL value is supported. |
69 ASSERT_FALSE(calc1.Calculate("pref_path", NULL).empty()); | 69 ASSERT_FALSE(calc1.Calculate("pref_path", NULL).empty()); |
70 } | 70 } |
71 | 71 |
72 // Tests the output against a known value to catch unexpected algorithm changes. | 72 // Tests the output against a known value to catch unexpected algorithm changes. |
73 TEST(PrefHashCalculatorTest, CatchHashChanges) { | 73 TEST(PrefHashCalculatorTest, CatchHashChanges) { |
74 const char* kDeviceId = "test_device_id1"; | 74 static const char kSeed[] = "0123456789ABCDEF0123456789ABCDEF"; |
75 static const char kDeviceId[] = "test_device_id1"; | |
75 { | 76 { |
76 static const char kExpectedValue[] = | 77 static const char kExpectedValue[] = |
77 "5CE37D7EBCBC9BE510F0F5E7C326CA92C1673713C3717839610AEA1A217D8BB8"; | 78 "D8137B8E767D3D910DCD3821CAC61D26ABB042E6EC406AEB0E347ED73A3A4EC1"; |
78 | 79 |
79 base::ListValue list; | 80 base::ListValue list; |
80 list.Set(0, new base::FundamentalValue(true)); | 81 list.Set(0, new base::FundamentalValue(true)); |
81 list.Set(1, new base::FundamentalValue(100)); | 82 list.Set(1, new base::FundamentalValue(100)); |
82 list.Set(2, new base::FundamentalValue(1.0)); | 83 list.Set(2, new base::FundamentalValue(1.0)); |
83 | 84 |
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, | 85 EXPECT_EQ(PrefHashCalculator::VALID, |
87 PrefHashCalculator(std::string(32u, 0), kDeviceId).Validate( | 86 PrefHashCalculator(kSeed, kDeviceId).Validate( |
88 "pref.path2", &list, kExpectedValue)); | 87 "pref.path2", &list, kExpectedValue)); |
89 } | 88 } |
90 { | 89 { |
91 static const char kExpectedValue[] = | 90 static const char kExpectedValue[] = |
92 "A50FE7EB31BFBC32B8A27E71730AF15421178A9B5815644ACE174B18966735B9"; | 91 "3F947A044DE9E421A735525385B4C789693682E6F6E3E4CB4741E58724B28F96"; |
93 | 92 |
94 DictionaryValue dict; | 93 DictionaryValue dict; |
95 dict.Set("a", new StringValue("foo")); | 94 dict.Set("a", new StringValue("foo")); |
96 dict.Set("d", new StringValue("bad")); | 95 dict.Set("d", new StringValue("bad")); |
97 dict.Set("b", new StringValue("bar")); | 96 dict.Set("b", new StringValue("bar")); |
98 dict.Set("c", new StringValue("baz")); | 97 dict.Set("c", new StringValue("baz")); |
99 | 98 |
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, | 99 EXPECT_EQ(PrefHashCalculator::VALID, |
103 PrefHashCalculator(std::string(32u, 0), kDeviceId).Validate( | 100 PrefHashCalculator(kSeed, kDeviceId).Validate( |
104 "pref.path1", &dict, kExpectedValue)); | 101 "pref.path1", &dict, kExpectedValue)); |
105 } | 102 } |
106 } | 103 } |
107 | 104 |
105 TEST(PrefHashCalculatorTest, TestCompatibilityWithPrefMetricsService) { | |
gab
2014/01/08 23:46:31
optional: s/TestCompatibilityWithPrefMetricsServic
| |
106 static const char kSeed[] = { | |
107 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
108 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, | |
109 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
110 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, | |
111 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
112 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, | |
113 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | |
114 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F | |
115 }; | |
116 static const char kDeviceId[] = | |
117 "D730D9CBD98C734A4FB097A1922275FE9F7E026A4EA1BE0E84"; | |
118 static const char kExpectedValue[] = | |
119 "845EF34663FF8D32BE6707F40258FBA531C2BFC532E3B014AFB3476115C2A9DE"; | |
120 | |
121 ListValue startup_urls; | |
122 startup_urls.Set(0, new StringValue("http://www.chromium.org/")); | |
123 | |
124 EXPECT_EQ(PrefHashCalculator::VALID, | |
125 PrefHashCalculator(std::string(kSeed, arraysize(kSeed)), kDeviceId). | |
126 Validate("session.startup_urls", &startup_urls, kExpectedValue)); | |
127 } | |
128 | |
108 TEST(PrefHashCalculatorTest, TestLegacyAlgorithm) { | 129 TEST(PrefHashCalculatorTest, TestLegacyAlgorithm) { |
109 const char* kExpectedValue = | 130 static const char kExpectedValue[] = |
110 "C503FB7C65EEFD5C07185F616A0AA67923C069909933F362022B1F187E73E9A2"; | 131 "C503FB7C65EEFD5C07185F616A0AA67923C069909933F362022B1F187E73E9A2"; |
111 const char* kDeviceId = "deviceid"; | 132 static const char kDeviceId[] = "not_used"; |
112 | 133 |
113 DictionaryValue dict; | 134 DictionaryValue dict; |
114 dict.Set("a", new StringValue("foo")); | 135 dict.Set("a", new StringValue("foo")); |
115 dict.Set("d", new StringValue("bad")); | 136 dict.Set("d", new StringValue("bad")); |
116 dict.Set("b", new StringValue("bar")); | 137 dict.Set("b", new StringValue("bar")); |
117 dict.Set("c", new StringValue("baz")); | 138 dict.Set("c", new StringValue("baz")); |
118 | 139 |
119 // 32 NULL bytes is the seed that was used to generate the legacy hash. | 140 // 32 NULL bytes is the seed that was used to generate the legacy hash. |
120 EXPECT_EQ(PrefHashCalculator::VALID_LEGACY, | 141 EXPECT_EQ(PrefHashCalculator::VALID_LEGACY, |
121 PrefHashCalculator(std::string(32u, 0), kDeviceId).Validate( | 142 PrefHashCalculator(std::string(32u, 0), kDeviceId).Validate( |
122 "pref.path1", &dict, kExpectedValue)); | 143 "pref.path1", &dict, kExpectedValue)); |
123 | |
124 } | 144 } |
OLD | NEW |