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/policy/core/common/registry_dict.h" | 5 #include "components/policy/core/common/registry_dict.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "components/policy/core/common/schema.h" | 12 #include "components/policy/core/common/schema.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
14 | 14 |
15 namespace policy { | 15 namespace policy { |
16 namespace { | 16 namespace { |
17 | 17 |
18 TEST(RegistryDictTest, SetAndGetValue) { | 18 TEST(RegistryDictTest, SetAndGetValue) { |
19 RegistryDict test_dict; | 19 RegistryDict test_dict; |
20 | 20 |
21 base::Value int_value(42); | 21 base::Value int_value(42); |
22 base::StringValue string_value("fortytwo"); | 22 base::Value string_value("fortytwo"); |
23 | 23 |
24 test_dict.SetValue("one", int_value.CreateDeepCopy()); | 24 test_dict.SetValue("one", int_value.CreateDeepCopy()); |
25 EXPECT_EQ(1u, test_dict.values().size()); | 25 EXPECT_EQ(1u, test_dict.values().size()); |
26 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("one"))); | 26 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("one"))); |
27 EXPECT_FALSE(test_dict.GetValue("two")); | 27 EXPECT_FALSE(test_dict.GetValue("two")); |
28 | 28 |
29 test_dict.SetValue("two", string_value.CreateDeepCopy()); | 29 test_dict.SetValue("two", string_value.CreateDeepCopy()); |
30 EXPECT_EQ(2u, test_dict.values().size()); | 30 EXPECT_EQ(2u, test_dict.values().size()); |
31 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("one"))); | 31 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("one"))); |
32 EXPECT_TRUE(base::Value::Equals(&string_value, test_dict.GetValue("two"))); | 32 EXPECT_TRUE(base::Value::Equals(&string_value, test_dict.GetValue("two"))); |
33 | 33 |
34 std::unique_ptr<base::Value> one(test_dict.RemoveValue("one")); | 34 std::unique_ptr<base::Value> one(test_dict.RemoveValue("one")); |
35 EXPECT_EQ(1u, test_dict.values().size()); | 35 EXPECT_EQ(1u, test_dict.values().size()); |
36 EXPECT_TRUE(base::Value::Equals(&int_value, one.get())); | 36 EXPECT_TRUE(base::Value::Equals(&int_value, one.get())); |
37 EXPECT_FALSE(test_dict.GetValue("one")); | 37 EXPECT_FALSE(test_dict.GetValue("one")); |
38 EXPECT_TRUE(base::Value::Equals(&string_value, test_dict.GetValue("two"))); | 38 EXPECT_TRUE(base::Value::Equals(&string_value, test_dict.GetValue("two"))); |
39 | 39 |
40 test_dict.ClearValues(); | 40 test_dict.ClearValues(); |
41 EXPECT_FALSE(test_dict.GetValue("one")); | 41 EXPECT_FALSE(test_dict.GetValue("one")); |
42 EXPECT_FALSE(test_dict.GetValue("two")); | 42 EXPECT_FALSE(test_dict.GetValue("two")); |
43 EXPECT_TRUE(test_dict.values().empty()); | 43 EXPECT_TRUE(test_dict.values().empty()); |
44 } | 44 } |
45 | 45 |
46 TEST(RegistryDictTest, CaseInsensitiveButPreservingValueNames) { | 46 TEST(RegistryDictTest, CaseInsensitiveButPreservingValueNames) { |
47 RegistryDict test_dict; | 47 RegistryDict test_dict; |
48 | 48 |
49 base::Value int_value(42); | 49 base::Value int_value(42); |
50 base::StringValue string_value("fortytwo"); | 50 base::Value string_value("fortytwo"); |
51 | 51 |
52 test_dict.SetValue("One", int_value.CreateDeepCopy()); | 52 test_dict.SetValue("One", int_value.CreateDeepCopy()); |
53 EXPECT_EQ(1u, test_dict.values().size()); | 53 EXPECT_EQ(1u, test_dict.values().size()); |
54 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("oNe"))); | 54 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("oNe"))); |
55 | 55 |
56 RegistryDict::ValueMap::const_iterator entry = test_dict.values().begin(); | 56 RegistryDict::ValueMap::const_iterator entry = test_dict.values().begin(); |
57 ASSERT_NE(entry, test_dict.values().end()); | 57 ASSERT_NE(entry, test_dict.values().end()); |
58 EXPECT_EQ("One", entry->first); | 58 EXPECT_EQ("One", entry->first); |
59 | 59 |
60 test_dict.SetValue("ONE", string_value.CreateDeepCopy()); | 60 test_dict.SetValue("ONE", string_value.CreateDeepCopy()); |
61 EXPECT_EQ(1u, test_dict.values().size()); | 61 EXPECT_EQ(1u, test_dict.values().size()); |
62 EXPECT_TRUE(base::Value::Equals(&string_value, test_dict.GetValue("one"))); | 62 EXPECT_TRUE(base::Value::Equals(&string_value, test_dict.GetValue("one"))); |
63 | 63 |
64 std::unique_ptr<base::Value> removed_value(test_dict.RemoveValue("onE")); | 64 std::unique_ptr<base::Value> removed_value(test_dict.RemoveValue("onE")); |
65 EXPECT_TRUE(base::Value::Equals(&string_value, removed_value.get())); | 65 EXPECT_TRUE(base::Value::Equals(&string_value, removed_value.get())); |
66 EXPECT_TRUE(test_dict.values().empty()); | 66 EXPECT_TRUE(test_dict.values().empty()); |
67 } | 67 } |
68 | 68 |
69 TEST(RegistryDictTest, SetAndGetKeys) { | 69 TEST(RegistryDictTest, SetAndGetKeys) { |
70 RegistryDict test_dict; | 70 RegistryDict test_dict; |
71 | 71 |
72 base::Value int_value(42); | 72 base::Value int_value(42); |
73 base::StringValue string_value("fortytwo"); | 73 base::Value string_value("fortytwo"); |
74 | 74 |
75 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); | 75 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); |
76 subdict->SetValue("one", int_value.CreateDeepCopy()); | 76 subdict->SetValue("one", int_value.CreateDeepCopy()); |
77 test_dict.SetKey("two", std::move(subdict)); | 77 test_dict.SetKey("two", std::move(subdict)); |
78 EXPECT_EQ(1u, test_dict.keys().size()); | 78 EXPECT_EQ(1u, test_dict.keys().size()); |
79 RegistryDict* actual_subdict = test_dict.GetKey("two"); | 79 RegistryDict* actual_subdict = test_dict.GetKey("two"); |
80 ASSERT_TRUE(actual_subdict); | 80 ASSERT_TRUE(actual_subdict); |
81 EXPECT_TRUE(base::Value::Equals(&int_value, actual_subdict->GetValue("one"))); | 81 EXPECT_TRUE(base::Value::Equals(&int_value, actual_subdict->GetValue("one"))); |
82 | 82 |
83 subdict.reset(new RegistryDict()); | 83 subdict.reset(new RegistryDict()); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 EXPECT_TRUE(base::Value::Equals(&int_value, | 127 EXPECT_TRUE(base::Value::Equals(&int_value, |
128 removed_key->GetValue("two"))); | 128 removed_key->GetValue("two"))); |
129 EXPECT_TRUE(test_dict.keys().empty()); | 129 EXPECT_TRUE(test_dict.keys().empty()); |
130 } | 130 } |
131 | 131 |
132 TEST(RegistryDictTest, Merge) { | 132 TEST(RegistryDictTest, Merge) { |
133 RegistryDict dict_a; | 133 RegistryDict dict_a; |
134 RegistryDict dict_b; | 134 RegistryDict dict_b; |
135 | 135 |
136 base::Value int_value(42); | 136 base::Value int_value(42); |
137 base::StringValue string_value("fortytwo"); | 137 base::Value string_value("fortytwo"); |
138 | 138 |
139 dict_a.SetValue("one", int_value.CreateDeepCopy()); | 139 dict_a.SetValue("one", int_value.CreateDeepCopy()); |
140 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); | 140 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); |
141 subdict->SetValue("two", string_value.CreateDeepCopy()); | 141 subdict->SetValue("two", string_value.CreateDeepCopy()); |
142 dict_a.SetKey("three", std::move(subdict)); | 142 dict_a.SetKey("three", std::move(subdict)); |
143 | 143 |
144 dict_b.SetValue("four", string_value.CreateDeepCopy()); | 144 dict_b.SetValue("four", string_value.CreateDeepCopy()); |
145 subdict.reset(new RegistryDict()); | 145 subdict.reset(new RegistryDict()); |
146 subdict->SetValue("two", int_value.CreateDeepCopy()); | 146 subdict->SetValue("two", int_value.CreateDeepCopy()); |
147 dict_b.SetKey("three", std::move(subdict)); | 147 dict_b.SetKey("three", std::move(subdict)); |
(...skipping 12 matching lines...) Expand all Loading... |
160 ASSERT_TRUE(actual_subdict); | 160 ASSERT_TRUE(actual_subdict); |
161 EXPECT_TRUE(base::Value::Equals(&int_value, | 161 EXPECT_TRUE(base::Value::Equals(&int_value, |
162 actual_subdict->GetValue("five"))); | 162 actual_subdict->GetValue("five"))); |
163 } | 163 } |
164 | 164 |
165 TEST(RegistryDictTest, Swap) { | 165 TEST(RegistryDictTest, Swap) { |
166 RegistryDict dict_a; | 166 RegistryDict dict_a; |
167 RegistryDict dict_b; | 167 RegistryDict dict_b; |
168 | 168 |
169 base::Value int_value(42); | 169 base::Value int_value(42); |
170 base::StringValue string_value("fortytwo"); | 170 base::Value string_value("fortytwo"); |
171 | 171 |
172 dict_a.SetValue("one", int_value.CreateDeepCopy()); | 172 dict_a.SetValue("one", int_value.CreateDeepCopy()); |
173 dict_a.SetKey("two", base::MakeUnique<RegistryDict>()); | 173 dict_a.SetKey("two", base::MakeUnique<RegistryDict>()); |
174 dict_b.SetValue("three", string_value.CreateDeepCopy()); | 174 dict_b.SetValue("three", string_value.CreateDeepCopy()); |
175 | 175 |
176 dict_a.Swap(&dict_b); | 176 dict_a.Swap(&dict_b); |
177 | 177 |
178 EXPECT_TRUE(base::Value::Equals(&int_value, dict_b.GetValue("one"))); | 178 EXPECT_TRUE(base::Value::Equals(&int_value, dict_b.GetValue("one"))); |
179 EXPECT_TRUE(dict_b.GetKey("two")); | 179 EXPECT_TRUE(dict_b.GetKey("two")); |
180 EXPECT_FALSE(dict_b.GetValue("two")); | 180 EXPECT_FALSE(dict_b.GetValue("two")); |
181 | 181 |
182 EXPECT_TRUE(base::Value::Equals(&string_value, dict_a.GetValue("three"))); | 182 EXPECT_TRUE(base::Value::Equals(&string_value, dict_a.GetValue("three"))); |
183 EXPECT_FALSE(dict_a.GetValue("one")); | 183 EXPECT_FALSE(dict_a.GetValue("one")); |
184 EXPECT_FALSE(dict_a.GetKey("two")); | 184 EXPECT_FALSE(dict_a.GetKey("two")); |
185 } | 185 } |
186 | 186 |
187 #if defined(OS_WIN) | 187 #if defined(OS_WIN) |
188 TEST(RegistryDictTest, ConvertToJSON) { | 188 TEST(RegistryDictTest, ConvertToJSON) { |
189 RegistryDict test_dict; | 189 RegistryDict test_dict; |
190 | 190 |
191 base::Value int_value(42); | 191 base::Value int_value(42); |
192 base::StringValue string_value("fortytwo"); | 192 base::Value string_value("fortytwo"); |
193 base::StringValue string_zero("0"); | 193 base::Value string_zero("0"); |
194 base::StringValue string_dict("{ \"key\": [ \"value\" ] }"); | 194 base::Value string_dict("{ \"key\": [ \"value\" ] }"); |
195 | 195 |
196 test_dict.SetValue("one", int_value.CreateDeepCopy()); | 196 test_dict.SetValue("one", int_value.CreateDeepCopy()); |
197 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); | 197 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); |
198 subdict->SetValue("two", string_value.CreateDeepCopy()); | 198 subdict->SetValue("two", string_value.CreateDeepCopy()); |
199 test_dict.SetKey("three", std::move(subdict)); | 199 test_dict.SetKey("three", std::move(subdict)); |
200 std::unique_ptr<RegistryDict> list(new RegistryDict()); | 200 std::unique_ptr<RegistryDict> list(new RegistryDict()); |
201 list->SetValue("1", string_value.CreateDeepCopy()); | 201 list->SetValue("1", string_value.CreateDeepCopy()); |
202 test_dict.SetKey("dict-to-list", std::move(list)); | 202 test_dict.SetKey("dict-to-list", std::move(list)); |
203 test_dict.SetValue("int-to-bool", int_value.CreateDeepCopy()); | 203 test_dict.SetValue("int-to-bool", int_value.CreateDeepCopy()); |
204 test_dict.SetValue("int-to-double", int_value.CreateDeepCopy()); | 204 test_dict.SetValue("int-to-double", int_value.CreateDeepCopy()); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 expected.Set("three", std::move(expected_subdict)); | 237 expected.Set("three", std::move(expected_subdict)); |
238 std::unique_ptr<base::ListValue> expected_list(new base::ListValue()); | 238 std::unique_ptr<base::ListValue> expected_list(new base::ListValue()); |
239 expected_list->Append(string_value.CreateDeepCopy()); | 239 expected_list->Append(string_value.CreateDeepCopy()); |
240 expected.Set("dict-to-list", std::move(expected_list)); | 240 expected.Set("dict-to-list", std::move(expected_list)); |
241 expected.Set("int-to-bool", new base::Value(true)); | 241 expected.Set("int-to-bool", new base::Value(true)); |
242 expected.Set("int-to-double", new base::Value(42.0)); | 242 expected.Set("int-to-double", new base::Value(42.0)); |
243 expected.Set("string-to-bool", new base::Value(false)); | 243 expected.Set("string-to-bool", new base::Value(false)); |
244 expected.Set("string-to-double", new base::Value(0.0)); | 244 expected.Set("string-to-double", new base::Value(0.0)); |
245 expected.Set("string-to-int", new base::Value(static_cast<int>(0))); | 245 expected.Set("string-to-int", new base::Value(static_cast<int>(0))); |
246 expected_list.reset(new base::ListValue()); | 246 expected_list.reset(new base::ListValue()); |
247 expected_list->Append(base::MakeUnique<base::StringValue>("value")); | 247 expected_list->Append(base::MakeUnique<base::Value>("value")); |
248 expected_subdict.reset(new base::DictionaryValue()); | 248 expected_subdict.reset(new base::DictionaryValue()); |
249 expected_subdict->Set("key", std::move(expected_list)); | 249 expected_subdict->Set("key", std::move(expected_list)); |
250 expected.Set("string-to-dict", std::move(expected_subdict)); | 250 expected.Set("string-to-dict", std::move(expected_subdict)); |
251 | 251 |
252 EXPECT_TRUE(base::Value::Equals(actual.get(), &expected)); | 252 EXPECT_TRUE(base::Value::Equals(actual.get(), &expected)); |
253 } | 253 } |
254 | 254 |
255 TEST(RegistryDictTest, NonSequentialConvertToJSON) { | 255 TEST(RegistryDictTest, NonSequentialConvertToJSON) { |
256 RegistryDict test_dict; | 256 RegistryDict test_dict; |
257 | 257 |
258 std::unique_ptr<RegistryDict> list(new RegistryDict()); | 258 std::unique_ptr<RegistryDict> list(new RegistryDict()); |
259 list->SetValue("1", base::StringValue("1").CreateDeepCopy()); | 259 list->SetValue("1", base::Value("1").CreateDeepCopy()); |
260 list->SetValue("2", base::StringValue("2").CreateDeepCopy()); | 260 list->SetValue("2", base::Value("2").CreateDeepCopy()); |
261 list->SetValue("THREE", base::StringValue("3").CreateDeepCopy()); | 261 list->SetValue("THREE", base::Value("3").CreateDeepCopy()); |
262 list->SetValue("4", base::StringValue("4").CreateDeepCopy()); | 262 list->SetValue("4", base::Value("4").CreateDeepCopy()); |
263 test_dict.SetKey("dict-to-list", std::move(list)); | 263 test_dict.SetKey("dict-to-list", std::move(list)); |
264 | 264 |
265 std::string error; | 265 std::string error; |
266 Schema schema = Schema::Parse( | 266 Schema schema = Schema::Parse( |
267 "{" | 267 "{" |
268 " \"type\": \"object\"," | 268 " \"type\": \"object\"," |
269 " \"properties\": {" | 269 " \"properties\": {" |
270 " \"dict-to-list\": {" | 270 " \"dict-to-list\": {" |
271 " \"type\": \"array\"," | 271 " \"type\": \"array\"," |
272 " \"items\": { \"type\": \"string\" }" | 272 " \"items\": { \"type\": \"string\" }" |
273 " }" | 273 " }" |
274 " }" | 274 " }" |
275 "}", | 275 "}", |
276 &error); | 276 &error); |
277 ASSERT_TRUE(schema.valid()) << error; | 277 ASSERT_TRUE(schema.valid()) << error; |
278 | 278 |
279 std::unique_ptr<base::Value> actual(test_dict.ConvertToJSON(schema)); | 279 std::unique_ptr<base::Value> actual(test_dict.ConvertToJSON(schema)); |
280 | 280 |
281 base::DictionaryValue expected; | 281 base::DictionaryValue expected; |
282 std::unique_ptr<base::ListValue> expected_list(new base::ListValue()); | 282 std::unique_ptr<base::ListValue> expected_list(new base::ListValue()); |
283 expected_list->Append(base::StringValue("1").CreateDeepCopy()); | 283 expected_list->Append(base::Value("1").CreateDeepCopy()); |
284 expected_list->Append(base::StringValue("2").CreateDeepCopy()); | 284 expected_list->Append(base::Value("2").CreateDeepCopy()); |
285 expected_list->Append(base::StringValue("4").CreateDeepCopy()); | 285 expected_list->Append(base::Value("4").CreateDeepCopy()); |
286 expected.Set("dict-to-list", std::move(expected_list)); | 286 expected.Set("dict-to-list", std::move(expected_list)); |
287 | 287 |
288 EXPECT_TRUE(base::Value::Equals(actual.get(), &expected)); | 288 EXPECT_TRUE(base::Value::Equals(actual.get(), &expected)); |
289 } | 289 } |
290 #endif | 290 #endif |
291 | 291 |
292 TEST(RegistryDictTest, KeyValueNameClashes) { | 292 TEST(RegistryDictTest, KeyValueNameClashes) { |
293 RegistryDict test_dict; | 293 RegistryDict test_dict; |
294 | 294 |
295 base::Value int_value(42); | 295 base::Value int_value(42); |
296 base::StringValue string_value("fortytwo"); | 296 base::Value string_value("fortytwo"); |
297 | 297 |
298 test_dict.SetValue("one", int_value.CreateDeepCopy()); | 298 test_dict.SetValue("one", int_value.CreateDeepCopy()); |
299 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); | 299 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); |
300 subdict->SetValue("two", string_value.CreateDeepCopy()); | 300 subdict->SetValue("two", string_value.CreateDeepCopy()); |
301 test_dict.SetKey("one", std::move(subdict)); | 301 test_dict.SetKey("one", std::move(subdict)); |
302 | 302 |
303 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("one"))); | 303 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("one"))); |
304 RegistryDict* actual_subdict = test_dict.GetKey("one"); | 304 RegistryDict* actual_subdict = test_dict.GetKey("one"); |
305 ASSERT_TRUE(actual_subdict); | 305 ASSERT_TRUE(actual_subdict); |
306 EXPECT_TRUE(base::Value::Equals(&string_value, | 306 EXPECT_TRUE(base::Value::Equals(&string_value, |
307 actual_subdict->GetValue("two"))); | 307 actual_subdict->GetValue("two"))); |
308 } | 308 } |
309 | 309 |
310 } // namespace | 310 } // namespace |
311 } // namespace policy | 311 } // namespace policy |
OLD | NEW |