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 3// found in the LICENSE file. 4 5#include "components/policy/core/common/registry_dict_win.h" 6 7#include <string> 8 9#include "base/values.h" 10#include "components/policy/core/common/schema.h" 11#include "testing/gtest/include/gtest/gtest.h" 12 13namespace policy { 14namespace { 15 16TEST(RegistryDictTest, SetAndGetValue) { 17 RegistryDict test_dict; 18 19 base::FundamentalValue int_value(42); 20 base::StringValue string_value("fortytwo"); 21 22 test_dict.SetValue("one", scoped_ptr<base::Value>(int_value.DeepCopy())); 23 EXPECT_EQ(1, test_dict.values().size()); 24 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("one"))); 25 EXPECT_FALSE(test_dict.GetValue("two")); 26 27 test_dict.SetValue("two", scoped_ptr<base::Value>(string_value.DeepCopy())); 28 EXPECT_EQ(2, test_dict.values().size()); 29 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("one"))); 30 EXPECT_TRUE(base::Value::Equals(&string_value, test_dict.GetValue("two"))); 31 32 scoped_ptr<base::Value> one(test_dict.RemoveValue("one")); 33 EXPECT_EQ(1, test_dict.values().size()); 34 EXPECT_TRUE(base::Value::Equals(&int_value, one.get())); 35 EXPECT_FALSE(test_dict.GetValue("one")); 36 EXPECT_TRUE(base::Value::Equals(&string_value, test_dict.GetValue("two"))); 37 38 test_dict.ClearValues(); 39 EXPECT_FALSE(test_dict.GetValue("one")); 40 EXPECT_FALSE(test_dict.GetValue("two")); 41 EXPECT_TRUE(test_dict.values().empty()); 42} 43 44TEST(RegistryDictTest, CaseInsensitiveButPreservingValueNames) { 45 RegistryDict test_dict; 46 47 base::FundamentalValue int_value(42); 48 base::StringValue string_value("fortytwo"); 49 50 test_dict.SetValue("One", scoped_ptr<base::Value>(int_value.DeepCopy())); 51 EXPECT_EQ(1, test_dict.values().size()); 52 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("oNe"))); 53 54 RegistryDict::ValueMap::const_iterator entry = test_dict.values().begin(); 55 ASSERT_NE(entry, test_dict.values().end()); 56 EXPECT_EQ("One", entry->first); 57 58 test_dict.SetValue("ONE", scoped_ptr<base::Value>(string_value.DeepCopy())); 59 EXPECT_EQ(1, test_dict.values().size()); 60 EXPECT_TRUE(base::Value::Equals(&string_value, test_dict.GetValue("one"))); 61 62 scoped_ptr<base::Value> removed_value(test_dict.RemoveValue("onE")); 63 EXPECT_TRUE(base::Value::Equals(&string_value, removed_value.get())); 64 EXPECT_TRUE(test_dict.values().empty()); 65} 66 67TEST(RegistryDictTest, SetAndGetKeys) { 68 RegistryDict test_dict; 69 70 base::FundamentalValue int_value(42); 71 base::StringValue string_value("fortytwo"); 72 73 scoped_ptr<RegistryDict> subdict(new RegistryDict()); 74 subdict->SetValue("one", scoped_ptr<base::Value>(int_value.DeepCopy())); 75 test_dict.SetKey("two", subdict.Pass()); 76 EXPECT_EQ(1, test_dict.keys().size()); 77 RegistryDict* actual_subdict = test_dict.GetKey("two"); 78 ASSERT_TRUE(actual_subdict); 79 EXPECT_TRUE(base::Value::Equals(&int_value, actual_subdict->GetValue("one"))); 80 81 subdict.reset(new RegistryDict()); 82 subdict->SetValue("three", scoped_ptr<base::Value>(string_value.DeepCopy())); 83 test_dict.SetKey("four", subdict.Pass()); 84 EXPECT_EQ(2, test_dict.keys().size()); 85 actual_subdict = test_dict.GetKey("two"); 86 ASSERT_TRUE(actual_subdict); 87 EXPECT_TRUE(base::Value::Equals(&int_value, actual_subdict->GetValue("one"))); 88 actual_subdict = test_dict.GetKey("four"); 89 ASSERT_TRUE(actual_subdict); 90 EXPECT_TRUE(base::Value::Equals(&string_value, 91 actual_subdict->GetValue("three"))); 92 93 test_dict.ClearKeys(); 94 EXPECT_FALSE(test_dict.GetKey("one")); 95 EXPECT_FALSE(test_dict.GetKey("three")); 96 EXPECT_TRUE(test_dict.keys().empty()); 97} 98 99TEST(RegistryDictTest, CaseInsensitiveButPreservingKeyNames) { 100 RegistryDict test_dict; 101 102 base::FundamentalValue int_value(42); 103 104 test_dict.SetKey("One", make_scoped_ptr(new RegistryDict()).Pass()); 105 EXPECT_EQ(1, test_dict.keys().size()); 106 RegistryDict* actual_subdict = test_dict.GetKey("One"); 107 ASSERT_TRUE(actual_subdict); 108 EXPECT_TRUE(actual_subdict->values().empty()); 109 110 RegistryDict::KeyMap::const_iterator entry = test_dict.keys().begin(); 111 ASSERT_NE(entry, test_dict.keys().end()); 112 EXPECT_EQ("One", entry->first); 113 114 scoped_ptr<RegistryDict> subdict(new RegistryDict()); 115 subdict->SetValue("two", scoped_ptr<base::Value>(int_value.DeepCopy())); 116 test_dict.SetKey("ONE", subdict.Pass()); 117 EXPECT_EQ(1, test_dict.keys().size()); 118 actual_subdict = test_dict.GetKey("One"); 119 ASSERT_TRUE(actual_subdict); 120 EXPECT_TRUE(base::Value::Equals(&int_value, 121 actual_subdict->GetValue("two"))); 122 123 scoped_ptr<RegistryDict> removed_key(test_dict.RemoveKey("one")); 124 ASSERT_TRUE(removed_key); 125 EXPECT_TRUE(base::Value::Equals(&int_value, 126 removed_key->GetValue("two"))); 127 EXPECT_TRUE(test_dict.keys().empty()); 128} 129 130TEST(RegistryDictTest, Merge) { 131 RegistryDict dict_a; 132 RegistryDict dict_b; 133 134 base::FundamentalValue int_value(42); 135 base::StringValue string_value("fortytwo"); 136 137 dict_a.SetValue("one", scoped_ptr<base::Value>(int_value.DeepCopy())); 138 scoped_ptr<RegistryDict> subdict(new RegistryDict()); 139 subdict->SetValue("two", scoped_ptr<base::Value>(string_value.DeepCopy())); 140 dict_a.SetKey("three", subdict.Pass()); 141 142 dict_b.SetValue("four", scoped_ptr<base::Value>(string_value.DeepCopy())); 143 subdict.reset(new RegistryDict()); 144 subdict->SetValue("two", scoped_ptr<base::Value>(int_value.DeepCopy())); 145 dict_b.SetKey("three", subdict.Pass()); 146 subdict.reset(new RegistryDict()); 147 subdict->SetValue("five", scoped_ptr<base::Value>(int_value.DeepCopy())); 148 dict_b.SetKey("six", subdict.Pass()); 149 150 dict_a.Merge(dict_b); 151 152 EXPECT_TRUE(base::Value::Equals(&int_value, dict_a.GetValue("one"))); 153 EXPECT_TRUE(base::Value::Equals(&string_value, dict_b.GetValue("four"))); 154 RegistryDict* actual_subdict = dict_a.GetKey("three"); 155 ASSERT_TRUE(actual_subdict); 156 EXPECT_TRUE(base::Value::Equals(&int_value, actual_subdict->GetValue("two"))); 157 actual_subdict = dict_a.GetKey("six"); 158 ASSERT_TRUE(actual_subdict); 159 EXPECT_TRUE(base::Value::Equals(&int_value, 160 actual_subdict->GetValue("five"))); 161} 162 163TEST(RegistryDictTest, Swap) { 164 RegistryDict dict_a; 165 RegistryDict dict_b; 166 167 base::FundamentalValue int_value(42); 168 base::StringValue string_value("fortytwo"); 169 170 dict_a.SetValue("one", scoped_ptr<base::Value>(int_value.DeepCopy())); 171 dict_a.SetKey("two", make_scoped_ptr(new RegistryDict()).Pass()); 172 dict_b.SetValue("three", scoped_ptr<base::Value>(string_value.DeepCopy())); 173 174 dict_a.Swap(&dict_b); 175 176 EXPECT_TRUE(base::Value::Equals(&int_value, dict_b.GetValue("one"))); 177 EXPECT_TRUE(dict_b.GetKey("two")); 178 EXPECT_FALSE(dict_b.GetValue("two")); 179 180 EXPECT_TRUE(base::Value::Equals(&string_value, dict_a.GetValue("three"))); 181 EXPECT_FALSE(dict_a.GetValue("one")); 182 EXPECT_FALSE(dict_a.GetKey("two")); 183} 184 185TEST(RegistryDictTest, ConvertToJSON) { 186 RegistryDict test_dict; 187 188 base::FundamentalValue int_value(42); 189 base::StringValue string_value("fortytwo"); 190 base::StringValue string_zero("0"); 191 base::StringValue string_dict("{ \"key\": [ \"value\" ] }"); 192 193 test_dict.SetValue("one", scoped_ptr<base::Value>(int_value.DeepCopy())); 194 scoped_ptr<RegistryDict> subdict(new RegistryDict()); 195 subdict->SetValue("two", scoped_ptr<base::Value>(string_value.DeepCopy())); 196 test_dict.SetKey("three", subdict.Pass()); 197 scoped_ptr<RegistryDict> list(new RegistryDict()); 198 list->SetValue("1", scoped_ptr<base::Value>(string_value.DeepCopy())); 199 test_dict.SetKey("dict-to-list", list.Pass()); 200 test_dict.SetValue("int-to-bool", 201 scoped_ptr<base::Value>(int_value.DeepCopy())); 202 test_dict.SetValue("int-to-double", 203 scoped_ptr<base::Value>(int_value.DeepCopy())); 204 test_dict.SetValue("string-to-bool", 205 scoped_ptr<base::Value>(string_zero.DeepCopy())); 206 test_dict.SetValue("string-to-double", 207 scoped_ptr<base::Value>(string_zero.DeepCopy())); 208 test_dict.SetValue("string-to-int", 209 scoped_ptr<base::Value>(string_zero.DeepCopy())); 210 test_dict.SetValue("string-to-dict", 211 scoped_ptr<base::Value>(string_dict.DeepCopy())); 212 213 std::string error; 214 Schema schema = Schema::Parse( 215 "{" 216 " \"type\": \"object\"," 217 " \"properties\": {" 218 " \"dict-to-list\": {" 219 " \"type\": \"array\"," 220 " \"items\": { \"type\": \"string\" }" 221 " }," 222 " \"int-to-bool\": { \"type\": \"boolean\" }," 223 " \"int-to-double\": { \"type\": \"number\" }," 224 " \"string-to-bool\": { \"type\": \"boolean\" }," 225 " \"string-to-double\": { \"type\": \"number\" }," 226 " \"string-to-int\": { \"type\": \"integer\" }," 227 " \"string-to-dict\": { \"type\": \"object\" }" 228 " }" 229 "}", &error); 230 ASSERT_TRUE(schema.valid()) << error; 231 232 scoped_ptr<base::Value> actual(test_dict.ConvertToJSON(schema)); 233 234 base::DictionaryValue expected; 235 expected.Set("one", int_value.DeepCopy()); 236 scoped_ptr<base::DictionaryValue> expected_subdict( 237 new base::DictionaryValue()); 238 expected_subdict->Set("two", string_value.DeepCopy()); 239 expected.Set("three", expected_subdict.release()); 240 scoped_ptr<base::ListValue> expected_list(new base::ListValue()); 241 expected_list->Append(string_value.DeepCopy()); 242 expected.Set("dict-to-list", expected_list.release()); 243 expected.Set("int-to-bool", new base::FundamentalValue(true)); 244 expected.Set("int-to-double", new base::FundamentalValue(42.0)); 245 expected.Set("string-to-bool", new base::FundamentalValue(false)); 246 expected.Set("string-to-double", new base::FundamentalValue(0.0)); 247 expected.Set("string-to-int", new base::FundamentalValue((int) 0)); 248 expected_list.reset(new base::ListValue()); 249 expected_list->Append(new base::StringValue("value")); 250 expected_subdict.reset(new base::DictionaryValue()); 251 expected_subdict->Set("key", expected_list.release()); 252 expected.Set("string-to-dict", expected_subdict.release()); 253 254 EXPECT_TRUE(base::Value::Equals(actual.get(), &expected)); 255} 256 257TEST(RegistryDictTest, KeyValueNameClashes) { 258 RegistryDict test_dict; 259 260 base::FundamentalValue int_value(42); 261 base::StringValue string_value("fortytwo"); 262 263 test_dict.SetValue("one", scoped_ptr<base::Value>(int_value.DeepCopy())); 264 scoped_ptr<RegistryDict> subdict(new RegistryDict()); 265 subdict->SetValue("two", scoped_ptr<base::Value>(string_value.DeepCopy())); 266 test_dict.SetKey("one", subdict.Pass()); 267 268 EXPECT_TRUE(base::Value::Equals(&int_value, test_dict.GetValue("one"))); 269 RegistryDict* actual_subdict = test_dict.GetKey("one"); 270 ASSERT_TRUE(actual_subdict); 271 EXPECT_TRUE(base::Value::Equals(&string_value, 272 actual_subdict->GetValue("two"))); 273} 274 275} // namespace 276} // namespace policy 277