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 "components/user_prefs/tracked/pref_hash_calculator.h" | 5 #include "components/user_prefs/tracked/pref_hash_calculator.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
16 TEST(PrefHashCalculatorTest, TestCurrentAlgorithm) { | 16 TEST(PrefHashCalculatorTest, TestCurrentAlgorithm) { |
17 base::StringValue string_value_1("string value 1"); | 17 base::StringValue string_value_1("string value 1"); |
18 base::StringValue string_value_2("string value 2"); | 18 base::StringValue string_value_2("string value 2"); |
19 base::DictionaryValue dictionary_value_1; | 19 base::DictionaryValue dictionary_value_1; |
20 dictionary_value_1.SetInteger("int value", 1); | 20 dictionary_value_1.SetInteger("int value", 1); |
21 dictionary_value_1.Set("nested empty map", new base::DictionaryValue); | 21 dictionary_value_1.Set("nested empty map", new base::DictionaryValue); |
22 base::DictionaryValue dictionary_value_1_equivalent; | 22 base::DictionaryValue dictionary_value_1_equivalent; |
23 dictionary_value_1_equivalent.SetInteger("int value", 1); | 23 dictionary_value_1_equivalent.SetInteger("int value", 1); |
24 base::DictionaryValue dictionary_value_2; | 24 base::DictionaryValue dictionary_value_2; |
25 dictionary_value_2.SetInteger("int value", 2); | 25 dictionary_value_2.SetInteger("int value", 2); |
26 | 26 |
27 PrefHashCalculator calc1("seed1", "deviceid"); | 27 PrefHashCalculator calc1("seed1", "deviceid", "legacydeviceid"); |
28 PrefHashCalculator calc1_dup("seed1", "deviceid"); | 28 PrefHashCalculator calc1_dup("seed1", "deviceid", "legacydeviceid"); |
29 PrefHashCalculator calc2("seed2", "deviceid"); | 29 PrefHashCalculator calc2("seed2", "deviceid", "legacydeviceid"); |
30 PrefHashCalculator calc3("seed1", "deviceid2"); | 30 PrefHashCalculator calc3("seed1", "deviceid2", "legacydeviceid"); |
31 | 31 |
32 // Two calculators with same seed produce same hash. | 32 // Two calculators with same seed produce same hash. |
33 ASSERT_EQ(calc1.Calculate("pref_path", &string_value_1), | 33 ASSERT_EQ(calc1.Calculate("pref_path", &string_value_1), |
34 calc1_dup.Calculate("pref_path", &string_value_1)); | 34 calc1_dup.Calculate("pref_path", &string_value_1)); |
35 ASSERT_EQ(PrefHashCalculator::VALID, | 35 ASSERT_EQ(PrefHashCalculator::VALID, |
36 calc1_dup.Validate("pref_path", &string_value_1, | 36 calc1_dup.Validate("pref_path", &string_value_1, |
37 calc1.Calculate("pref_path", &string_value_1))); | 37 calc1.Calculate("pref_path", &string_value_1))); |
38 | 38 |
39 // Different seeds, different hashes. | 39 // Different seeds, different hashes. |
40 ASSERT_NE(calc1.Calculate("pref_path", &string_value_1), | 40 ASSERT_NE(calc1.Calculate("pref_path", &string_value_1), |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 ASSERT_EQ(base::Value::Type::DOUBLE, double_value->GetType()); | 117 ASSERT_EQ(base::Value::Type::DOUBLE, double_value->GetType()); |
118 ASSERT_EQ(base::Value::Type::STRING, string_value->GetType()); | 118 ASSERT_EQ(base::Value::Type::STRING, string_value->GetType()); |
119 ASSERT_EQ(base::Value::Type::DICTIONARY, dict_value->GetType()); | 119 ASSERT_EQ(base::Value::Type::DICTIONARY, dict_value->GetType()); |
120 ASSERT_EQ(base::Value::Type::LIST, list_value->GetType()); | 120 ASSERT_EQ(base::Value::Type::LIST, list_value->GetType()); |
121 | 121 |
122 // Test every value type independently. Intentionally omits Type::BINARY which | 122 // Test every value type independently. Intentionally omits Type::BINARY which |
123 // isn't even allowed in JSONWriter's input. | 123 // isn't even allowed in JSONWriter's input. |
124 static const char kExpectedNullValue[] = | 124 static const char kExpectedNullValue[] = |
125 "82A9F3BBC7F9FF84C76B033C854E79EEB162783FA7B3E99FF9372FA8E12C44F7"; | 125 "82A9F3BBC7F9FF84C76B033C854E79EEB162783FA7B3E99FF9372FA8E12C44F7"; |
126 EXPECT_EQ(PrefHashCalculator::VALID, | 126 EXPECT_EQ(PrefHashCalculator::VALID, |
127 PrefHashCalculator(kSeed, kDeviceId) | 127 PrefHashCalculator(kSeed, kDeviceId, "legacydeviceid") |
128 .Validate("pref.path", null_value.get(), kExpectedNullValue)); | 128 .Validate("pref.path", null_value.get(), kExpectedNullValue)); |
129 | 129 |
130 static const char kExpectedBooleanValue[] = | 130 static const char kExpectedBooleanValue[] = |
131 "A520D8F43EA307B0063736DC9358C330539D0A29417580514C8B9862632C4CCC"; | 131 "A520D8F43EA307B0063736DC9358C330539D0A29417580514C8B9862632C4CCC"; |
132 EXPECT_EQ( | 132 EXPECT_EQ( |
133 PrefHashCalculator::VALID, | 133 PrefHashCalculator::VALID, |
134 PrefHashCalculator(kSeed, kDeviceId) | 134 PrefHashCalculator(kSeed, kDeviceId, "legacydeviceid") |
135 .Validate("pref.path", bool_value.get(), kExpectedBooleanValue)); | 135 .Validate("pref.path", bool_value.get(), kExpectedBooleanValue)); |
136 | 136 |
137 static const char kExpectedIntegerValue[] = | 137 static const char kExpectedIntegerValue[] = |
138 "8D60DA1F10BF5AA29819D2D66D7CCEF9AABC5DA93C11A0D2BD21078D63D83682"; | 138 "8D60DA1F10BF5AA29819D2D66D7CCEF9AABC5DA93C11A0D2BD21078D63D83682"; |
139 EXPECT_EQ(PrefHashCalculator::VALID, | 139 EXPECT_EQ(PrefHashCalculator::VALID, |
140 PrefHashCalculator(kSeed, kDeviceId) | 140 PrefHashCalculator(kSeed, kDeviceId, "legacydeviceid") |
141 .Validate("pref.path", int_value.get(), kExpectedIntegerValue)); | 141 .Validate("pref.path", int_value.get(), kExpectedIntegerValue)); |
142 | 142 |
143 static const char kExpectedDoubleValue[] = | 143 static const char kExpectedDoubleValue[] = |
144 "C9D94772516125BEEDAE68C109D44BC529E719EE020614E894CC7FB4098C545D"; | 144 "C9D94772516125BEEDAE68C109D44BC529E719EE020614E894CC7FB4098C545D"; |
145 EXPECT_EQ( | 145 EXPECT_EQ( |
146 PrefHashCalculator::VALID, | 146 PrefHashCalculator::VALID, |
147 PrefHashCalculator(kSeed, kDeviceId) | 147 PrefHashCalculator(kSeed, kDeviceId, "legacydeviceid") |
148 .Validate("pref.path", double_value.get(), kExpectedDoubleValue)); | 148 .Validate("pref.path", double_value.get(), kExpectedDoubleValue)); |
149 | 149 |
150 static const char kExpectedStringValue[] = | 150 static const char kExpectedStringValue[] = |
151 "05ACCBD3B05C45C36CD06190F63EC577112311929D8380E26E5F13182EB68318"; | 151 "05ACCBD3B05C45C36CD06190F63EC577112311929D8380E26E5F13182EB68318"; |
152 EXPECT_EQ( | 152 EXPECT_EQ( |
153 PrefHashCalculator::VALID, | 153 PrefHashCalculator::VALID, |
154 PrefHashCalculator(kSeed, kDeviceId) | 154 PrefHashCalculator(kSeed, kDeviceId, "legacydeviceid") |
155 .Validate("pref.path", string_value.get(), kExpectedStringValue)); | 155 .Validate("pref.path", string_value.get(), kExpectedStringValue)); |
156 | 156 |
157 static const char kExpectedDictValue[] = | 157 static const char kExpectedDictValue[] = |
158 "7A84DCC710D796C771F789A4DA82C952095AA956B6F1667EE42D0A19ECAA3C4A"; | 158 "7A84DCC710D796C771F789A4DA82C952095AA956B6F1667EE42D0A19ECAA3C4A"; |
159 EXPECT_EQ(PrefHashCalculator::VALID, | 159 EXPECT_EQ(PrefHashCalculator::VALID, |
160 PrefHashCalculator(kSeed, kDeviceId) | 160 PrefHashCalculator(kSeed, kDeviceId, "legacydeviceid") |
161 .Validate("pref.path", dict_value.get(), kExpectedDictValue)); | 161 .Validate("pref.path", dict_value.get(), kExpectedDictValue)); |
162 | 162 |
163 static const char kExpectedListValue[] = | 163 static const char kExpectedListValue[] = |
164 "8D5A25972DF5AE20D041C780E7CA54E40F614AD53513A0724EE8D62D4F992740"; | 164 "8D5A25972DF5AE20D041C780E7CA54E40F614AD53513A0724EE8D62D4F992740"; |
165 EXPECT_EQ(PrefHashCalculator::VALID, | 165 EXPECT_EQ(PrefHashCalculator::VALID, |
166 PrefHashCalculator(kSeed, kDeviceId) | 166 PrefHashCalculator(kSeed, kDeviceId, "legacydeviceid") |
167 .Validate("pref.path", list_value.get(), kExpectedListValue)); | 167 .Validate("pref.path", list_value.get(), kExpectedListValue)); |
168 | 168 |
169 // Also test every value type together in the same dictionary. | 169 // Also test every value type together in the same dictionary. |
170 base::DictionaryValue everything; | 170 base::DictionaryValue everything; |
171 everything.Set("null", null_value.release()); | 171 everything.Set("null", null_value.release()); |
172 everything.Set("bool", bool_value.release()); | 172 everything.Set("bool", bool_value.release()); |
173 everything.Set("int", int_value.release()); | 173 everything.Set("int", int_value.release()); |
174 everything.Set("double", double_value.release()); | 174 everything.Set("double", double_value.release()); |
175 everything.Set("string", string_value.release()); | 175 everything.Set("string", string_value.release()); |
176 everything.Set("list", list_value.release()); | 176 everything.Set("list", list_value.release()); |
177 everything.Set("dict", dict_value.release()); | 177 everything.Set("dict", dict_value.release()); |
178 static const char kExpectedEverythingValue[] = | 178 static const char kExpectedEverythingValue[] = |
179 "B97D09BE7005693574DCBDD03D8D9E44FB51F4008B73FB56A49A9FA671A1999B"; | 179 "B97D09BE7005693574DCBDD03D8D9E44FB51F4008B73FB56A49A9FA671A1999B"; |
180 EXPECT_EQ(PrefHashCalculator::VALID, | 180 EXPECT_EQ(PrefHashCalculator::VALID, |
181 PrefHashCalculator(kSeed, kDeviceId) | 181 PrefHashCalculator(kSeed, kDeviceId, "legacydeviceid") |
182 .Validate("pref.path", &everything, kExpectedEverythingValue)); | 182 .Validate("pref.path", &everything, kExpectedEverythingValue)); |
183 } | 183 } |
184 | 184 |
185 TEST(PrefHashCalculatorTest, TestCompatibilityWithLegacyPrefMetricsServiceId) { | 185 TEST(PrefHashCalculatorTest, TestCompatibilityWithLegacyDeviceId) { |
186 static const char kSeed[] = { | 186 static const char kSeed[] = "0123456789ABCDEF0123456789ABCDEF"; |
187 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, | 187 static const char kNewDeviceId[] = "new_test_device_id1"; |
188 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, | 188 static const char kLegacyDeviceId[] = "test_device_id1"; |
189 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00, | 189 |
190 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, | 190 // As in PrefHashCalculatorTest.CatchHashChanges. |
191 0x0C, 0x0D, 0x0E, 0x0F, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, | 191 const base::StringValue string_value( |
192 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; | 192 "testing with special chars:\n<>{}:^^@#$\\/"); |
193 static const char kDeviceId[] = | |
194 "D730D9CBD98C734A4FB097A1922275FE9F7E026A4EA1BE0E84"; | |
195 static const char kExpectedValue[] = | 193 static const char kExpectedValue[] = |
196 "845EF34663FF8D32BE6707F40258FBA531C2BFC532E3B014AFB3476115C2A9DE"; | 194 "05ACCBD3B05C45C36CD06190F63EC577112311929D8380E26E5F13182EB68318"; |
197 | 195 |
198 base::ListValue startup_urls; | 196 EXPECT_EQ(PrefHashCalculator::VALID_SECURE_LEGACY, |
199 startup_urls.Set(0, new base::StringValue("http://www.chromium.org/")); | 197 PrefHashCalculator(kSeed, kNewDeviceId, kLegacyDeviceId) |
200 | 198 .Validate("pref.path", &string_value, kExpectedValue)); |
201 EXPECT_EQ( | |
202 PrefHashCalculator::VALID_SECURE_LEGACY, | |
203 PrefHashCalculator(std::string(kSeed, arraysize(kSeed)), kDeviceId) | |
204 .Validate("session.startup_urls", &startup_urls, kExpectedValue)); | |
205 } | 199 } |
OLD | NEW |