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::FundamentalValue int_value(42); | 21 base::Value int_value(42); |
22 base::StringValue string_value("fortytwo"); | 22 base::StringValue 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::FundamentalValue int_value(42); | 49 base::Value int_value(42); |
50 base::StringValue string_value("fortytwo"); | 50 base::StringValue 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::FundamentalValue int_value(42); | 72 base::Value int_value(42); |
73 base::StringValue string_value("fortytwo"); | 73 base::StringValue 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 |
(...skipping 11 matching lines...) Expand all Loading... |
94 | 94 |
95 test_dict.ClearKeys(); | 95 test_dict.ClearKeys(); |
96 EXPECT_FALSE(test_dict.GetKey("one")); | 96 EXPECT_FALSE(test_dict.GetKey("one")); |
97 EXPECT_FALSE(test_dict.GetKey("three")); | 97 EXPECT_FALSE(test_dict.GetKey("three")); |
98 EXPECT_TRUE(test_dict.keys().empty()); | 98 EXPECT_TRUE(test_dict.keys().empty()); |
99 } | 99 } |
100 | 100 |
101 TEST(RegistryDictTest, CaseInsensitiveButPreservingKeyNames) { | 101 TEST(RegistryDictTest, CaseInsensitiveButPreservingKeyNames) { |
102 RegistryDict test_dict; | 102 RegistryDict test_dict; |
103 | 103 |
104 base::FundamentalValue int_value(42); | 104 base::Value int_value(42); |
105 | 105 |
106 test_dict.SetKey("One", base::MakeUnique<RegistryDict>()); | 106 test_dict.SetKey("One", base::MakeUnique<RegistryDict>()); |
107 EXPECT_EQ(1u, test_dict.keys().size()); | 107 EXPECT_EQ(1u, test_dict.keys().size()); |
108 RegistryDict* actual_subdict = test_dict.GetKey("One"); | 108 RegistryDict* actual_subdict = test_dict.GetKey("One"); |
109 ASSERT_TRUE(actual_subdict); | 109 ASSERT_TRUE(actual_subdict); |
110 EXPECT_TRUE(actual_subdict->values().empty()); | 110 EXPECT_TRUE(actual_subdict->values().empty()); |
111 | 111 |
112 RegistryDict::KeyMap::const_iterator entry = test_dict.keys().begin(); | 112 RegistryDict::KeyMap::const_iterator entry = test_dict.keys().begin(); |
113 ASSERT_NE(entry, test_dict.keys().end()); | 113 ASSERT_NE(entry, test_dict.keys().end()); |
114 EXPECT_EQ("One", entry->first); | 114 EXPECT_EQ("One", entry->first); |
(...skipping 11 matching lines...) Expand all Loading... |
126 ASSERT_TRUE(removed_key); | 126 ASSERT_TRUE(removed_key); |
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::FundamentalValue int_value(42); | 136 base::Value int_value(42); |
137 base::StringValue string_value("fortytwo"); | 137 base::StringValue 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()); |
(...skipping 12 matching lines...) Expand all Loading... |
159 actual_subdict = dict_a.GetKey("six"); | 159 actual_subdict = dict_a.GetKey("six"); |
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::FundamentalValue int_value(42); | 169 base::Value int_value(42); |
170 base::StringValue string_value("fortytwo"); | 170 base::StringValue 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::FundamentalValue int_value(42); | 191 base::Value int_value(42); |
192 base::StringValue string_value("fortytwo"); | 192 base::StringValue string_value("fortytwo"); |
193 base::StringValue string_zero("0"); | 193 base::StringValue string_zero("0"); |
194 base::StringValue string_dict("{ \"key\": [ \"value\" ] }"); | 194 base::StringValue 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()); |
(...skipping 29 matching lines...) Expand all Loading... |
231 | 231 |
232 base::DictionaryValue expected; | 232 base::DictionaryValue expected; |
233 expected.Set("one", int_value.CreateDeepCopy()); | 233 expected.Set("one", int_value.CreateDeepCopy()); |
234 std::unique_ptr<base::DictionaryValue> expected_subdict( | 234 std::unique_ptr<base::DictionaryValue> expected_subdict( |
235 new base::DictionaryValue()); | 235 new base::DictionaryValue()); |
236 expected_subdict->Set("two", string_value.CreateDeepCopy()); | 236 expected_subdict->Set("two", string_value.CreateDeepCopy()); |
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::FundamentalValue(true)); | 241 expected.Set("int-to-bool", new base::Value(true)); |
242 expected.Set("int-to-double", new base::FundamentalValue(42.0)); | 242 expected.Set("int-to-double", new base::Value(42.0)); |
243 expected.Set("string-to-bool", new base::FundamentalValue(false)); | 243 expected.Set("string-to-bool", new base::Value(false)); |
244 expected.Set("string-to-double", new base::FundamentalValue(0.0)); | 244 expected.Set("string-to-double", new base::Value(0.0)); |
245 expected.Set("string-to-int", | 245 expected.Set("string-to-int", new base::Value(static_cast<int>(0))); |
246 new base::FundamentalValue(static_cast<int>(0))); | |
247 expected_list.reset(new base::ListValue()); | 246 expected_list.reset(new base::ListValue()); |
248 expected_list->Append(base::MakeUnique<base::StringValue>("value")); | 247 expected_list->Append(base::MakeUnique<base::StringValue>("value")); |
249 expected_subdict.reset(new base::DictionaryValue()); | 248 expected_subdict.reset(new base::DictionaryValue()); |
250 expected_subdict->Set("key", std::move(expected_list)); | 249 expected_subdict->Set("key", std::move(expected_list)); |
251 expected.Set("string-to-dict", std::move(expected_subdict)); | 250 expected.Set("string-to-dict", std::move(expected_subdict)); |
252 | 251 |
253 EXPECT_TRUE(base::Value::Equals(actual.get(), &expected)); | 252 EXPECT_TRUE(base::Value::Equals(actual.get(), &expected)); |
254 } | 253 } |
255 | 254 |
256 TEST(RegistryDictTest, NonSequentialConvertToJSON) { | 255 TEST(RegistryDictTest, NonSequentialConvertToJSON) { |
(...skipping 29 matching lines...) Expand all Loading... |
286 expected_list->Append(base::StringValue("4").CreateDeepCopy()); | 285 expected_list->Append(base::StringValue("4").CreateDeepCopy()); |
287 expected.Set("dict-to-list", std::move(expected_list)); | 286 expected.Set("dict-to-list", std::move(expected_list)); |
288 | 287 |
289 EXPECT_TRUE(base::Value::Equals(actual.get(), &expected)); | 288 EXPECT_TRUE(base::Value::Equals(actual.get(), &expected)); |
290 } | 289 } |
291 #endif | 290 #endif |
292 | 291 |
293 TEST(RegistryDictTest, KeyValueNameClashes) { | 292 TEST(RegistryDictTest, KeyValueNameClashes) { |
294 RegistryDict test_dict; | 293 RegistryDict test_dict; |
295 | 294 |
296 base::FundamentalValue int_value(42); | 295 base::Value int_value(42); |
297 base::StringValue string_value("fortytwo"); | 296 base::StringValue string_value("fortytwo"); |
298 | 297 |
299 test_dict.SetValue("one", int_value.CreateDeepCopy()); | 298 test_dict.SetValue("one", int_value.CreateDeepCopy()); |
300 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); | 299 std::unique_ptr<RegistryDict> subdict(new RegistryDict()); |
301 subdict->SetValue("two", string_value.CreateDeepCopy()); | 300 subdict->SetValue("two", string_value.CreateDeepCopy()); |
302 test_dict.SetKey("one", std::move(subdict)); | 301 test_dict.SetKey("one", std::move(subdict)); |
303 | 302 |
304 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("one"))); | 303 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("one"))); |
305 RegistryDict* actual_subdict = test_dict.GetKey("one"); | 304 RegistryDict* actual_subdict = test_dict.GetKey("one"); |
306 ASSERT_TRUE(actual_subdict); | 305 ASSERT_TRUE(actual_subdict); |
307 EXPECT_TRUE(base::Value::Equals(&string_value, | 306 EXPECT_TRUE(base::Value::Equals(&string_value, |
308 actual_subdict->GetValue("two"))); | 307 actual_subdict->GetValue("two"))); |
309 } | 308 } |
310 | 309 |
311 } // namespace | 310 } // namespace |
312 } // namespace policy | 311 } // namespace policy |
OLD | NEW |