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 "services/preferences/tracked/pref_hash_calculator.h" | 5 #include "services/preferences/tracked/pref_hash_calculator.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
| 9 #include <utility> |
9 | 10 |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
13 #include "base/values.h" | 14 #include "base/values.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
15 | 16 |
16 TEST(PrefHashCalculatorTest, TestCurrentAlgorithm) { | 17 TEST(PrefHashCalculatorTest, TestCurrentAlgorithm) { |
17 base::Value string_value_1("string value 1"); | 18 base::Value string_value_1("string value 1"); |
18 base::Value string_value_2("string value 2"); | 19 base::Value string_value_2("string value 2"); |
19 base::DictionaryValue dictionary_value_1; | 20 base::DictionaryValue dictionary_value_1; |
20 dictionary_value_1.SetInteger("int value", 1); | 21 dictionary_value_1.SetInteger("int value", 1); |
21 dictionary_value_1.Set("nested empty map", new base::DictionaryValue); | 22 dictionary_value_1.Set("nested empty map", |
| 23 base::MakeUnique<base::DictionaryValue>()); |
22 base::DictionaryValue dictionary_value_1_equivalent; | 24 base::DictionaryValue dictionary_value_1_equivalent; |
23 dictionary_value_1_equivalent.SetInteger("int value", 1); | 25 dictionary_value_1_equivalent.SetInteger("int value", 1); |
24 base::DictionaryValue dictionary_value_2; | 26 base::DictionaryValue dictionary_value_2; |
25 dictionary_value_2.SetInteger("int value", 2); | 27 dictionary_value_2.SetInteger("int value", 2); |
26 | 28 |
27 PrefHashCalculator calc1("seed1", "deviceid", "legacydeviceid"); | 29 PrefHashCalculator calc1("seed1", "deviceid", "legacydeviceid"); |
28 PrefHashCalculator calc1_dup("seed1", "deviceid", "legacydeviceid"); | 30 PrefHashCalculator calc1_dup("seed1", "deviceid", "legacydeviceid"); |
29 PrefHashCalculator calc2("seed2", "deviceid", "legacydeviceid"); | 31 PrefHashCalculator calc2("seed2", "deviceid", "legacydeviceid"); |
30 PrefHashCalculator calc3("seed1", "deviceid2", "legacydeviceid"); | 32 PrefHashCalculator calc3("seed1", "deviceid2", "legacydeviceid"); |
31 | 33 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 } | 72 } |
71 | 73 |
72 // Tests the output against a known value to catch unexpected algorithm changes. | 74 // Tests the output against a known value to catch unexpected algorithm changes. |
73 // The test hashes below must NEVER be updated, the serialization algorithm used | 75 // The test hashes below must NEVER be updated, the serialization algorithm used |
74 // must always be able to generate data that will produce these exact hashes. | 76 // must always be able to generate data that will produce these exact hashes. |
75 TEST(PrefHashCalculatorTest, CatchHashChanges) { | 77 TEST(PrefHashCalculatorTest, CatchHashChanges) { |
76 static const char kSeed[] = "0123456789ABCDEF0123456789ABCDEF"; | 78 static const char kSeed[] = "0123456789ABCDEF0123456789ABCDEF"; |
77 static const char kDeviceId[] = "test_device_id1"; | 79 static const char kDeviceId[] = "test_device_id1"; |
78 | 80 |
79 auto null_value = base::MakeUnique<base::Value>(); | 81 auto null_value = base::MakeUnique<base::Value>(); |
80 std::unique_ptr<base::Value> bool_value(new base::Value(false)); | 82 auto bool_value = base::MakeUnique<base::Value>(false); |
81 std::unique_ptr<base::Value> int_value(new base::Value(1234567890)); | 83 auto int_value = base::MakeUnique<base::Value>(1234567890); |
82 std::unique_ptr<base::Value> double_value(new base::Value(123.0987654321)); | 84 auto double_value = base::MakeUnique<base::Value>(123.0987654321); |
83 std::unique_ptr<base::Value> string_value( | 85 auto string_value = base::MakeUnique<base::Value>( |
84 new base::Value("testing with special chars:\n<>{}:^^@#$\\/")); | 86 "testing with special chars:\n<>{}:^^@#$\\/"); |
85 | 87 |
86 // For legacy reasons, we have to support pruning of empty lists/dictionaries | 88 // For legacy reasons, we have to support pruning of empty lists/dictionaries |
87 // and nested empty ists/dicts in the hash generation algorithm. | 89 // and nested empty ists/dicts in the hash generation algorithm. |
88 std::unique_ptr<base::DictionaryValue> nested_empty_dict( | 90 auto nested_empty_dict = base::MakeUnique<base::DictionaryValue>(); |
89 new base::DictionaryValue); | 91 nested_empty_dict->Set("a", base::MakeUnique<base::DictionaryValue>()); |
90 nested_empty_dict->Set("a", new base::DictionaryValue); | 92 nested_empty_dict->Set("b", base::MakeUnique<base::ListValue>()); |
91 nested_empty_dict->Set("b", new base::ListValue); | 93 auto nested_empty_list = base::MakeUnique<base::ListValue>(); |
92 std::unique_ptr<base::ListValue> nested_empty_list(new base::ListValue); | |
93 nested_empty_list->Append(base::MakeUnique<base::DictionaryValue>()); | 94 nested_empty_list->Append(base::MakeUnique<base::DictionaryValue>()); |
94 nested_empty_list->Append(base::MakeUnique<base::ListValue>()); | 95 nested_empty_list->Append(base::MakeUnique<base::ListValue>()); |
95 nested_empty_list->Append(nested_empty_dict->CreateDeepCopy()); | 96 nested_empty_list->Append(base::MakeUnique<base::Value>(*nested_empty_dict)); |
96 | 97 |
97 // A dictionary with an empty dictionary, an empty list, and nested empty | 98 // A dictionary with an empty dictionary, an empty list, and nested empty |
98 // dictionaries/lists in it. | 99 // dictionaries/lists in it. |
99 std::unique_ptr<base::DictionaryValue> dict_value(new base::DictionaryValue); | 100 auto dict_value = base::MakeUnique<base::DictionaryValue>(); |
100 dict_value->Set("a", new base::Value("foo")); | 101 dict_value->SetString("a", "foo"); |
101 dict_value->Set("d", new base::ListValue); | 102 dict_value->Set("d", base::MakeUnique<base::ListValue>()); |
102 dict_value->Set("b", new base::DictionaryValue); | 103 dict_value->Set("b", base::MakeUnique<base::DictionaryValue>()); |
103 dict_value->Set("c", new base::Value("baz")); | 104 dict_value->SetString("c", "baz"); |
104 dict_value->Set("e", nested_empty_dict.release()); | 105 dict_value->Set("e", std::move(nested_empty_dict)); |
105 dict_value->Set("f", nested_empty_list.release()); | 106 dict_value->Set("f", std::move(nested_empty_list)); |
106 | 107 |
107 std::unique_ptr<base::ListValue> list_value(new base::ListValue); | 108 auto list_value = base::MakeUnique<base::ListValue>(); |
108 list_value->AppendBoolean(true); | 109 list_value->AppendBoolean(true); |
109 list_value->AppendInteger(100); | 110 list_value->AppendInteger(100); |
110 list_value->AppendDouble(1.0); | 111 list_value->AppendDouble(1.0); |
111 | 112 |
112 ASSERT_EQ(base::Value::Type::NONE, null_value->GetType()); | 113 ASSERT_EQ(base::Value::Type::NONE, null_value->GetType()); |
113 ASSERT_EQ(base::Value::Type::BOOLEAN, bool_value->GetType()); | 114 ASSERT_EQ(base::Value::Type::BOOLEAN, bool_value->GetType()); |
114 ASSERT_EQ(base::Value::Type::INTEGER, int_value->GetType()); | 115 ASSERT_EQ(base::Value::Type::INTEGER, int_value->GetType()); |
115 ASSERT_EQ(base::Value::Type::DOUBLE, double_value->GetType()); | 116 ASSERT_EQ(base::Value::Type::DOUBLE, double_value->GetType()); |
116 ASSERT_EQ(base::Value::Type::STRING, string_value->GetType()); | 117 ASSERT_EQ(base::Value::Type::STRING, string_value->GetType()); |
117 ASSERT_EQ(base::Value::Type::DICTIONARY, dict_value->GetType()); | 118 ASSERT_EQ(base::Value::Type::DICTIONARY, dict_value->GetType()); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 .Validate("pref.path", dict_value.get(), kExpectedDictValue)); | 160 .Validate("pref.path", dict_value.get(), kExpectedDictValue)); |
160 | 161 |
161 static const char kExpectedListValue[] = | 162 static const char kExpectedListValue[] = |
162 "8D5A25972DF5AE20D041C780E7CA54E40F614AD53513A0724EE8D62D4F992740"; | 163 "8D5A25972DF5AE20D041C780E7CA54E40F614AD53513A0724EE8D62D4F992740"; |
163 EXPECT_EQ(PrefHashCalculator::VALID, | 164 EXPECT_EQ(PrefHashCalculator::VALID, |
164 PrefHashCalculator(kSeed, kDeviceId, "legacydeviceid") | 165 PrefHashCalculator(kSeed, kDeviceId, "legacydeviceid") |
165 .Validate("pref.path", list_value.get(), kExpectedListValue)); | 166 .Validate("pref.path", list_value.get(), kExpectedListValue)); |
166 | 167 |
167 // Also test every value type together in the same dictionary. | 168 // Also test every value type together in the same dictionary. |
168 base::DictionaryValue everything; | 169 base::DictionaryValue everything; |
169 everything.Set("null", null_value.release()); | 170 everything.Set("null", std::move(null_value)); |
170 everything.Set("bool", bool_value.release()); | 171 everything.Set("bool", std::move(bool_value)); |
171 everything.Set("int", int_value.release()); | 172 everything.Set("int", std::move(int_value)); |
172 everything.Set("double", double_value.release()); | 173 everything.Set("double", std::move(double_value)); |
173 everything.Set("string", string_value.release()); | 174 everything.Set("string", std::move(string_value)); |
174 everything.Set("list", list_value.release()); | 175 everything.Set("list", std::move(list_value)); |
175 everything.Set("dict", dict_value.release()); | 176 everything.Set("dict", std::move(dict_value)); |
176 static const char kExpectedEverythingValue[] = | 177 static const char kExpectedEverythingValue[] = |
177 "B97D09BE7005693574DCBDD03D8D9E44FB51F4008B73FB56A49A9FA671A1999B"; | 178 "B97D09BE7005693574DCBDD03D8D9E44FB51F4008B73FB56A49A9FA671A1999B"; |
178 EXPECT_EQ(PrefHashCalculator::VALID, | 179 EXPECT_EQ(PrefHashCalculator::VALID, |
179 PrefHashCalculator(kSeed, kDeviceId, "legacydeviceid") | 180 PrefHashCalculator(kSeed, kDeviceId, "legacydeviceid") |
180 .Validate("pref.path", &everything, kExpectedEverythingValue)); | 181 .Validate("pref.path", &everything, kExpectedEverythingValue)); |
181 } | 182 } |
182 | 183 |
183 TEST(PrefHashCalculatorTest, TestCompatibilityWithLegacyDeviceId) { | 184 TEST(PrefHashCalculatorTest, TestCompatibilityWithLegacyDeviceId) { |
184 static const char kSeed[] = "0123456789ABCDEF0123456789ABCDEF"; | 185 static const char kSeed[] = "0123456789ABCDEF0123456789ABCDEF"; |
185 static const char kNewDeviceId[] = "new_test_device_id1"; | 186 static const char kNewDeviceId[] = "new_test_device_id1"; |
186 static const char kLegacyDeviceId[] = "test_device_id1"; | 187 static const char kLegacyDeviceId[] = "test_device_id1"; |
187 | 188 |
188 // As in PrefHashCalculatorTest.CatchHashChanges. | 189 // As in PrefHashCalculatorTest.CatchHashChanges. |
189 const base::Value string_value("testing with special chars:\n<>{}:^^@#$\\/"); | 190 const base::Value string_value("testing with special chars:\n<>{}:^^@#$\\/"); |
190 static const char kExpectedValue[] = | 191 static const char kExpectedValue[] = |
191 "05ACCBD3B05C45C36CD06190F63EC577112311929D8380E26E5F13182EB68318"; | 192 "05ACCBD3B05C45C36CD06190F63EC577112311929D8380E26E5F13182EB68318"; |
192 | 193 |
193 EXPECT_EQ(PrefHashCalculator::VALID_SECURE_LEGACY, | 194 EXPECT_EQ(PrefHashCalculator::VALID_SECURE_LEGACY, |
194 PrefHashCalculator(kSeed, kNewDeviceId, kLegacyDeviceId) | 195 PrefHashCalculator(kSeed, kNewDeviceId, kLegacyDeviceId) |
195 .Validate("pref.path", &string_value, kExpectedValue)); | 196 .Validate("pref.path", &string_value, kExpectedValue)); |
196 } | 197 } |
OLD | NEW |