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::Value string_value_1("string value 1"); |
18 base::StringValue string_value_2("string value 2"); | 18 base::Value 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", "legacydeviceid"); | 27 PrefHashCalculator calc1("seed1", "deviceid", "legacydeviceid"); |
28 PrefHashCalculator calc1_dup("seed1", "deviceid", "legacydeviceid"); | 28 PrefHashCalculator calc1_dup("seed1", "deviceid", "legacydeviceid"); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 // must always be able to generate data that will produce these exact hashes. | 74 // must always be able to generate data that will produce these exact hashes. |
75 TEST(PrefHashCalculatorTest, CatchHashChanges) { | 75 TEST(PrefHashCalculatorTest, CatchHashChanges) { |
76 static const char kSeed[] = "0123456789ABCDEF0123456789ABCDEF"; | 76 static const char kSeed[] = "0123456789ABCDEF0123456789ABCDEF"; |
77 static const char kDeviceId[] = "test_device_id1"; | 77 static const char kDeviceId[] = "test_device_id1"; |
78 | 78 |
79 std::unique_ptr<base::Value> null_value = base::Value::CreateNullValue(); | 79 std::unique_ptr<base::Value> null_value = base::Value::CreateNullValue(); |
80 std::unique_ptr<base::Value> bool_value(new base::Value(false)); | 80 std::unique_ptr<base::Value> bool_value(new base::Value(false)); |
81 std::unique_ptr<base::Value> int_value(new base::Value(1234567890)); | 81 std::unique_ptr<base::Value> int_value(new base::Value(1234567890)); |
82 std::unique_ptr<base::Value> double_value(new base::Value(123.0987654321)); | 82 std::unique_ptr<base::Value> double_value(new base::Value(123.0987654321)); |
83 std::unique_ptr<base::Value> string_value( | 83 std::unique_ptr<base::Value> string_value( |
84 new base::StringValue("testing with special chars:\n<>{}:^^@#$\\/")); | 84 new base::Value("testing with special chars:\n<>{}:^^@#$\\/")); |
85 | 85 |
86 // For legacy reasons, we have to support pruning of empty lists/dictionaries | 86 // For legacy reasons, we have to support pruning of empty lists/dictionaries |
87 // and nested empty ists/dicts in the hash generation algorithm. | 87 // and nested empty ists/dicts in the hash generation algorithm. |
88 std::unique_ptr<base::DictionaryValue> nested_empty_dict( | 88 std::unique_ptr<base::DictionaryValue> nested_empty_dict( |
89 new base::DictionaryValue); | 89 new base::DictionaryValue); |
90 nested_empty_dict->Set("a", new base::DictionaryValue); | 90 nested_empty_dict->Set("a", new base::DictionaryValue); |
91 nested_empty_dict->Set("b", new base::ListValue); | 91 nested_empty_dict->Set("b", new base::ListValue); |
92 std::unique_ptr<base::ListValue> nested_empty_list(new base::ListValue); | 92 std::unique_ptr<base::ListValue> nested_empty_list(new base::ListValue); |
93 nested_empty_list->Append(base::MakeUnique<base::DictionaryValue>()); | 93 nested_empty_list->Append(base::MakeUnique<base::DictionaryValue>()); |
94 nested_empty_list->Append(base::MakeUnique<base::ListValue>()); | 94 nested_empty_list->Append(base::MakeUnique<base::ListValue>()); |
95 nested_empty_list->Append(nested_empty_dict->CreateDeepCopy()); | 95 nested_empty_list->Append(nested_empty_dict->CreateDeepCopy()); |
96 | 96 |
97 // A dictionary with an empty dictionary, an empty list, and nested empty | 97 // A dictionary with an empty dictionary, an empty list, and nested empty |
98 // dictionaries/lists in it. | 98 // dictionaries/lists in it. |
99 std::unique_ptr<base::DictionaryValue> dict_value(new base::DictionaryValue); | 99 std::unique_ptr<base::DictionaryValue> dict_value(new base::DictionaryValue); |
100 dict_value->Set("a", new base::StringValue("foo")); | 100 dict_value->Set("a", new base::Value("foo")); |
101 dict_value->Set("d", new base::ListValue); | 101 dict_value->Set("d", new base::ListValue); |
102 dict_value->Set("b", new base::DictionaryValue); | 102 dict_value->Set("b", new base::DictionaryValue); |
103 dict_value->Set("c", new base::StringValue("baz")); | 103 dict_value->Set("c", new base::Value("baz")); |
104 dict_value->Set("e", nested_empty_dict.release()); | 104 dict_value->Set("e", nested_empty_dict.release()); |
105 dict_value->Set("f", nested_empty_list.release()); | 105 dict_value->Set("f", nested_empty_list.release()); |
106 | 106 |
107 std::unique_ptr<base::ListValue> list_value(new base::ListValue); | 107 std::unique_ptr<base::ListValue> list_value(new base::ListValue); |
108 list_value->AppendBoolean(true); | 108 list_value->AppendBoolean(true); |
109 list_value->AppendInteger(100); | 109 list_value->AppendInteger(100); |
110 list_value->AppendDouble(1.0); | 110 list_value->AppendDouble(1.0); |
111 | 111 |
112 ASSERT_EQ(base::Value::Type::NONE, null_value->GetType()); | 112 ASSERT_EQ(base::Value::Type::NONE, null_value->GetType()); |
113 ASSERT_EQ(base::Value::Type::BOOLEAN, bool_value->GetType()); | 113 ASSERT_EQ(base::Value::Type::BOOLEAN, bool_value->GetType()); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 PrefHashCalculator(kSeed, kDeviceId, "legacydeviceid") | 179 PrefHashCalculator(kSeed, kDeviceId, "legacydeviceid") |
180 .Validate("pref.path", &everything, kExpectedEverythingValue)); | 180 .Validate("pref.path", &everything, kExpectedEverythingValue)); |
181 } | 181 } |
182 | 182 |
183 TEST(PrefHashCalculatorTest, TestCompatibilityWithLegacyDeviceId) { | 183 TEST(PrefHashCalculatorTest, TestCompatibilityWithLegacyDeviceId) { |
184 static const char kSeed[] = "0123456789ABCDEF0123456789ABCDEF"; | 184 static const char kSeed[] = "0123456789ABCDEF0123456789ABCDEF"; |
185 static const char kNewDeviceId[] = "new_test_device_id1"; | 185 static const char kNewDeviceId[] = "new_test_device_id1"; |
186 static const char kLegacyDeviceId[] = "test_device_id1"; | 186 static const char kLegacyDeviceId[] = "test_device_id1"; |
187 | 187 |
188 // As in PrefHashCalculatorTest.CatchHashChanges. | 188 // As in PrefHashCalculatorTest.CatchHashChanges. |
189 const base::StringValue string_value( | 189 const base::Value string_value("testing with special chars:\n<>{}:^^@#$\\/"); |
190 "testing with special chars:\n<>{}:^^@#$\\/"); | |
191 static const char kExpectedValue[] = | 190 static const char kExpectedValue[] = |
192 "05ACCBD3B05C45C36CD06190F63EC577112311929D8380E26E5F13182EB68318"; | 191 "05ACCBD3B05C45C36CD06190F63EC577112311929D8380E26E5F13182EB68318"; |
193 | 192 |
194 EXPECT_EQ(PrefHashCalculator::VALID_SECURE_LEGACY, | 193 EXPECT_EQ(PrefHashCalculator::VALID_SECURE_LEGACY, |
195 PrefHashCalculator(kSeed, kNewDeviceId, kLegacyDeviceId) | 194 PrefHashCalculator(kSeed, kNewDeviceId, kLegacyDeviceId) |
196 .Validate("pref.path", &string_value, kExpectedValue)); | 195 .Validate("pref.path", &string_value, kExpectedValue)); |
197 } | 196 } |
OLD | NEW |