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/memory/scoped_ptr.h" | |
9 #include "base/values.h" | 10 #include "base/values.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 TEST(PrefHashCalculatorTest, TestCurrentAlgorithm) { | 13 TEST(PrefHashCalculatorTest, TestCurrentAlgorithm) { |
13 base::StringValue string_value_1("string value 1"); | 14 base::StringValue string_value_1("string value 1"); |
14 base::StringValue string_value_2("string value 2"); | 15 base::StringValue string_value_2("string value 2"); |
15 base::DictionaryValue dictionary_value_1; | 16 base::DictionaryValue dictionary_value_1; |
16 dictionary_value_1.SetInteger("int value", 1); | 17 dictionary_value_1.SetInteger("int value", 1); |
17 dictionary_value_1.Set("nested empty map", new base::DictionaryValue); | 18 dictionary_value_1.Set("nested empty map", new base::DictionaryValue); |
18 base::DictionaryValue dictionary_value_1_equivalent; | 19 base::DictionaryValue dictionary_value_1_equivalent; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 | 64 |
64 // Empty dictionary children are pruned. | 65 // Empty dictionary children are pruned. |
65 ASSERT_EQ(calc1.Calculate("pref_path", &dictionary_value_1), | 66 ASSERT_EQ(calc1.Calculate("pref_path", &dictionary_value_1), |
66 calc1.Calculate("pref_path", &dictionary_value_1_equivalent)); | 67 calc1.Calculate("pref_path", &dictionary_value_1_equivalent)); |
67 | 68 |
68 // NULL value is supported. | 69 // NULL value is supported. |
69 ASSERT_FALSE(calc1.Calculate("pref_path", NULL).empty()); | 70 ASSERT_FALSE(calc1.Calculate("pref_path", NULL).empty()); |
70 } | 71 } |
71 | 72 |
72 // Tests the output against a known value to catch unexpected algorithm changes. | 73 // Tests the output against a known value to catch unexpected algorithm changes. |
74 // The test hashes below must NEVER be updated, the serialization algorithm used | |
75 // must always be able to generate data that will produce these exact hashes. | |
73 TEST(PrefHashCalculatorTest, CatchHashChanges) { | 76 TEST(PrefHashCalculatorTest, CatchHashChanges) { |
74 static const char kSeed[] = "0123456789ABCDEF0123456789ABCDEF"; | 77 static const char kSeed[] = "0123456789ABCDEF0123456789ABCDEF"; |
75 static const char kDeviceId[] = "test_device_id1"; | 78 static const char kDeviceId[] = "test_device_id1"; |
79 | |
80 scoped_ptr<base::Value> null_value(base::Value::CreateNullValue()); | |
81 scoped_ptr<base::Value> bool_value(base::Value::CreateBooleanValue(false)); | |
82 scoped_ptr<base::Value> int_value(base::Value::CreateIntegerValue( | |
Bernhard Bauer
2014/01/17 12:23:05
Nit: I probably would break after int_value(.
gab
2014/01/17 16:22:15
Done.
| |
83 1234567890)); | |
84 scoped_ptr<base::Value> double_value(base::Value::CreateDoubleValue( | |
85 123.0987654321)); | |
86 scoped_ptr<base::Value> string_value(base::Value::CreateStringValue( | |
87 "testing with special chars:\n<>{}:^^@#$\\/")); | |
88 | |
89 scoped_ptr<base::DictionaryValue> dict_value(new base::DictionaryValue); | |
90 dict_value->Set("a", new base::StringValue("foo")); | |
91 dict_value->Set("d", new base::StringValue("bad")); | |
92 dict_value->Set("b", new base::StringValue("bar")); | |
93 dict_value->Set("c", new base::StringValue("baz")); | |
94 | |
95 scoped_ptr<base::ListValue> list_value(new base::ListValue); | |
96 list_value->AppendBoolean(true); | |
97 list_value->AppendInteger(100); | |
98 list_value->AppendDouble(1.0); | |
99 | |
100 ASSERT_EQ(base::Value::TYPE_NULL, null_value->GetType()); | |
101 ASSERT_EQ(base::Value::TYPE_BOOLEAN, bool_value->GetType()); | |
102 ASSERT_EQ(base::Value::TYPE_INTEGER, int_value->GetType()); | |
103 ASSERT_EQ(base::Value::TYPE_DOUBLE, double_value->GetType()); | |
104 ASSERT_EQ(base::Value::TYPE_STRING, string_value->GetType()); | |
105 ASSERT_EQ(base::Value::TYPE_DICTIONARY, dict_value->GetType()); | |
106 ASSERT_EQ(base::Value::TYPE_LIST, list_value->GetType()); | |
107 | |
108 // Test every value type independently. Intentionally omits TYPE_BINARY which | |
109 // isn't even allowed in JSONWriter's input. | |
76 { | 110 { |
77 static const char kExpectedValue[] = | 111 static const char kExpectedNullValue[] = |
78 "D8137B8E767D3D910DCD3821CAC61D26ABB042E6EC406AEB0E347ED73A3A4EC1"; | 112 "C2871D0AC76176E39948C50A9A562B863E610FDA90C675A6A8AD16B4DC4F53DC"; |
79 | |
80 base::ListValue list; | |
81 list.Set(0, new base::FundamentalValue(true)); | |
82 list.Set(1, new base::FundamentalValue(100)); | |
83 list.Set(2, new base::FundamentalValue(1.0)); | |
84 | |
85 EXPECT_EQ(PrefHashCalculator::VALID, | 113 EXPECT_EQ(PrefHashCalculator::VALID, |
86 PrefHashCalculator(kSeed, kDeviceId).Validate( | 114 PrefHashCalculator(kSeed, kDeviceId).Validate( |
87 "pref.path2", &list, kExpectedValue)); | 115 "pref.path", null_value.get(), kExpectedNullValue)); |
88 } | 116 } |
Bernhard Bauer
2014/01/17 12:23:05
Nit: I don't think you need these separate scopes.
gab
2014/01/17 16:22:15
Done.
| |
89 { | 117 { |
90 static const char kExpectedValue[] = | 118 static const char kExpectedBooleanValue[] = |
91 "3F947A044DE9E421A735525385B4C789693682E6F6E3E4CB4741E58724B28F96"; | 119 "A326E2F405CFE05D08289CDADD9DB4F529592F0945A8CE204289E4C930D8AA43"; |
92 | |
93 base::DictionaryValue dict; | |
94 dict.Set("a", new base::StringValue("foo")); | |
95 dict.Set("d", new base::StringValue("bad")); | |
96 dict.Set("b", new base::StringValue("bar")); | |
97 dict.Set("c", new base::StringValue("baz")); | |
98 | |
99 EXPECT_EQ(PrefHashCalculator::VALID, | 120 EXPECT_EQ(PrefHashCalculator::VALID, |
100 PrefHashCalculator(kSeed, kDeviceId).Validate( | 121 PrefHashCalculator(kSeed, kDeviceId).Validate( |
101 "pref.path1", &dict, kExpectedValue)); | 122 "pref.path", bool_value.get(), kExpectedBooleanValue)); |
123 } | |
124 { | |
125 static const char kExpectedIntegerValue[] = | |
126 "4B69938F802A2A26D69467F3E1E4A474F6323C64EFC54DBDB4A5708A7D005042"; | |
127 EXPECT_EQ(PrefHashCalculator::VALID, | |
128 PrefHashCalculator(kSeed, kDeviceId).Validate( | |
129 "pref.path", int_value.get(), kExpectedIntegerValue)); | |
130 } | |
131 { | |
132 static const char kExpectedDoubleValue[] = | |
133 "1734C9C745B9C92D896B9A710994BF1B56D55BFB0F00C207EC995152AF02F08F"; | |
134 EXPECT_EQ(PrefHashCalculator::VALID, | |
135 PrefHashCalculator(kSeed, kDeviceId).Validate( | |
136 "pref.path", double_value.get(), kExpectedDoubleValue)); | |
137 } | |
138 { | |
139 static const char kExpectedStringValue[] = | |
140 "154D15522C856AA944BFA5A9E3FFB46925BF2B95A10199564651CA1C13E98433"; | |
141 EXPECT_EQ(PrefHashCalculator::VALID, | |
142 PrefHashCalculator(kSeed, kDeviceId).Validate( | |
143 "pref.path", string_value.get(), kExpectedStringValue)); | |
144 } | |
145 { | |
146 static const char kExpectedDictValue[] = | |
147 "3F947A044DE9E421A735525385B4C789693682E6F6E3E4CB4741E58724B28F96"; | |
148 EXPECT_EQ(PrefHashCalculator::VALID, | |
149 PrefHashCalculator(kSeed, kDeviceId).Validate( | |
150 "pref.path1", dict_value.get(), kExpectedDictValue)); | |
151 } | |
152 { | |
153 static const char kExpectedListValue[] = | |
154 "D8137B8E767D3D910DCD3821CAC61D26ABB042E6EC406AEB0E347ED73A3A4EC1"; | |
155 EXPECT_EQ(PrefHashCalculator::VALID, | |
156 PrefHashCalculator(kSeed, kDeviceId).Validate( | |
157 "pref.path2", list_value.get(), kExpectedListValue)); | |
158 } | |
159 | |
160 // Also test every value type together in the same dictionary. | |
161 base::DictionaryValue everything; | |
162 everything.Set("null", null_value.release()); | |
163 everything.Set("bool", bool_value.release()); | |
164 everything.Set("int", int_value.release()); | |
165 everything.Set("double", double_value.release()); | |
166 everything.Set("string", string_value.release()); | |
167 everything.Set("list", list_value.release()); | |
168 everything.Set("dict", dict_value.release()); | |
169 { | |
170 static const char kExpectedEverythingValue[] = | |
171 "0A546480C7AB7699779B2FCFA326D65E0AE1446EA62398AE1D338119C6913943"; | |
172 EXPECT_EQ(PrefHashCalculator::VALID, | |
173 PrefHashCalculator(kSeed, kDeviceId).Validate( | |
174 "pref.path1", &everything, kExpectedEverythingValue)); | |
102 } | 175 } |
103 } | 176 } |
104 | 177 |
105 TEST(PrefHashCalculatorTest, TestCompatibilityWithPrefMetricsService) { | 178 TEST(PrefHashCalculatorTest, TestCompatibilityWithPrefMetricsService) { |
106 static const char kSeed[] = { | 179 static const char kSeed[] = { |
107 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 180 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
108 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, | 181 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
109 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 182 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
110 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, | 183 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, |
111 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, | 184 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
(...skipping 23 matching lines...) Expand all Loading... | |
135 dict.Set("a", new base::StringValue("foo")); | 208 dict.Set("a", new base::StringValue("foo")); |
136 dict.Set("d", new base::StringValue("bad")); | 209 dict.Set("d", new base::StringValue("bad")); |
137 dict.Set("b", new base::StringValue("bar")); | 210 dict.Set("b", new base::StringValue("bar")); |
138 dict.Set("c", new base::StringValue("baz")); | 211 dict.Set("c", new base::StringValue("baz")); |
139 | 212 |
140 // 32 NULL bytes is the seed that was used to generate the legacy hash. | 213 // 32 NULL bytes is the seed that was used to generate the legacy hash. |
141 EXPECT_EQ(PrefHashCalculator::VALID_LEGACY, | 214 EXPECT_EQ(PrefHashCalculator::VALID_LEGACY, |
142 PrefHashCalculator(std::string(32u, 0), kDeviceId).Validate( | 215 PrefHashCalculator(std::string(32u, 0), kDeviceId).Validate( |
143 "pref.path1", &dict, kExpectedValue)); | 216 "pref.path1", &dict, kExpectedValue)); |
144 } | 217 } |
OLD | NEW |