OLD | NEW |
(Empty) | |
| 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/schema.h" |
| 6 |
| 7 #include "components/policy/core/common/schema_internal.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 |
| 10 namespace policy { |
| 11 |
| 12 namespace { |
| 13 |
| 14 #define OBJECT_TYPE "\"type\":\"object\"" |
| 15 |
| 16 const internal::SchemaNode kTypeBoolean = { base::Value::TYPE_BOOLEAN, NULL, }; |
| 17 const internal::SchemaNode kTypeInteger = { base::Value::TYPE_INTEGER, NULL, }; |
| 18 const internal::SchemaNode kTypeNumber = { base::Value::TYPE_DOUBLE, NULL, }; |
| 19 const internal::SchemaNode kTypeString = { base::Value::TYPE_STRING, NULL, }; |
| 20 |
| 21 bool ParseFails(const std::string& content) { |
| 22 std::string error; |
| 23 scoped_ptr<SchemaOwner> schema = SchemaOwner::Parse(content, &error); |
| 24 if (schema) |
| 25 EXPECT_TRUE(schema->schema().valid()); |
| 26 else |
| 27 EXPECT_FALSE(error.empty()); |
| 28 return !schema; |
| 29 } |
| 30 |
| 31 } // namespace |
| 32 |
| 33 TEST(SchemaTest, MinimalSchema) { |
| 34 EXPECT_FALSE(ParseFails( |
| 35 "{" |
| 36 OBJECT_TYPE |
| 37 "}")); |
| 38 } |
| 39 |
| 40 TEST(SchemaTest, InvalidSchemas) { |
| 41 EXPECT_TRUE(ParseFails("")); |
| 42 EXPECT_TRUE(ParseFails("omg")); |
| 43 EXPECT_TRUE(ParseFails("\"omg\"")); |
| 44 EXPECT_TRUE(ParseFails("123")); |
| 45 EXPECT_TRUE(ParseFails("[]")); |
| 46 EXPECT_TRUE(ParseFails("null")); |
| 47 EXPECT_TRUE(ParseFails("{}")); |
| 48 |
| 49 EXPECT_TRUE(ParseFails( |
| 50 "{" |
| 51 OBJECT_TYPE "," |
| 52 "\"additionalProperties\": { \"type\":\"object\" }" |
| 53 "}")); |
| 54 |
| 55 EXPECT_TRUE(ParseFails( |
| 56 "{" |
| 57 OBJECT_TYPE "," |
| 58 "\"patternProperties\": { \"a+b*\": { \"type\": \"object\" } }" |
| 59 "}")); |
| 60 |
| 61 EXPECT_TRUE(ParseFails( |
| 62 "{" |
| 63 OBJECT_TYPE "," |
| 64 "\"properties\": { \"Policy\": { \"type\": \"bogus\" } }" |
| 65 "}")); |
| 66 |
| 67 EXPECT_TRUE(ParseFails( |
| 68 "{" |
| 69 OBJECT_TYPE "," |
| 70 "\"properties\": { \"Policy\": { \"type\": [\"string\", \"number\"] } }" |
| 71 "}")); |
| 72 |
| 73 EXPECT_TRUE(ParseFails( |
| 74 "{" |
| 75 OBJECT_TYPE "," |
| 76 "\"properties\": { \"Policy\": { \"type\": \"any\" } }" |
| 77 "}")); |
| 78 } |
| 79 |
| 80 TEST(SchemaTest, ValidSchema) { |
| 81 std::string error; |
| 82 scoped_ptr<SchemaOwner> policy_schema = SchemaOwner::Parse( |
| 83 "{" |
| 84 OBJECT_TYPE "," |
| 85 "\"properties\": {" |
| 86 " \"Boolean\": { \"type\": \"boolean\" }," |
| 87 " \"Integer\": { \"type\": \"integer\" }," |
| 88 " \"Null\": { \"type\": \"null\" }," |
| 89 " \"Number\": { \"type\": \"number\" }," |
| 90 " \"String\": { \"type\": \"string\" }," |
| 91 " \"Array\": {" |
| 92 " \"type\": \"array\"," |
| 93 " \"items\": { \"type\": \"string\" }" |
| 94 " }," |
| 95 " \"ArrayOfObjects\": {" |
| 96 " \"type\": \"array\"," |
| 97 " \"items\": {" |
| 98 " \"type\": \"object\"," |
| 99 " \"properties\": {" |
| 100 " \"one\": { \"type\": \"string\" }," |
| 101 " \"two\": { \"type\": \"integer\" }" |
| 102 " }" |
| 103 " }" |
| 104 " }," |
| 105 " \"ArrayOfArray\": {" |
| 106 " \"type\": \"array\"," |
| 107 " \"items\": {" |
| 108 " \"type\": \"array\"," |
| 109 " \"items\": { \"type\": \"string\" }" |
| 110 " }" |
| 111 " }," |
| 112 " \"Object\": {" |
| 113 " \"type\": \"object\"," |
| 114 " \"properties\": {" |
| 115 " \"one\": { \"type\": \"boolean\" }," |
| 116 " \"two\": { \"type\": \"integer\" }" |
| 117 " }," |
| 118 " \"additionalProperties\": { \"type\": \"string\" }" |
| 119 " }" |
| 120 "}" |
| 121 "}", &error); |
| 122 ASSERT_TRUE(policy_schema) << error; |
| 123 ASSERT_TRUE(policy_schema->schema().valid()); |
| 124 |
| 125 Schema schema = policy_schema->schema(); |
| 126 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); |
| 127 EXPECT_FALSE(schema.GetProperty("invalid").valid()); |
| 128 |
| 129 Schema sub = schema.GetProperty("Boolean"); |
| 130 ASSERT_TRUE(sub.valid()); |
| 131 EXPECT_EQ(base::Value::TYPE_BOOLEAN, sub.type()); |
| 132 |
| 133 sub = schema.GetProperty("Integer"); |
| 134 ASSERT_TRUE(sub.valid()); |
| 135 EXPECT_EQ(base::Value::TYPE_INTEGER, sub.type()); |
| 136 |
| 137 sub = schema.GetProperty("Null"); |
| 138 ASSERT_TRUE(sub.valid()); |
| 139 EXPECT_EQ(base::Value::TYPE_NULL, sub.type()); |
| 140 |
| 141 sub = schema.GetProperty("Number"); |
| 142 ASSERT_TRUE(sub.valid()); |
| 143 EXPECT_EQ(base::Value::TYPE_DOUBLE, sub.type()); |
| 144 |
| 145 sub = schema.GetProperty("String"); |
| 146 ASSERT_TRUE(sub.valid()); |
| 147 EXPECT_EQ(base::Value::TYPE_STRING, sub.type()); |
| 148 |
| 149 sub = schema.GetProperty("Array"); |
| 150 ASSERT_TRUE(sub.valid()); |
| 151 ASSERT_EQ(base::Value::TYPE_LIST, sub.type()); |
| 152 sub = sub.GetItems(); |
| 153 ASSERT_TRUE(sub.valid()); |
| 154 EXPECT_EQ(base::Value::TYPE_STRING, sub.type()); |
| 155 |
| 156 sub = schema.GetProperty("ArrayOfObjects"); |
| 157 ASSERT_TRUE(sub.valid()); |
| 158 ASSERT_EQ(base::Value::TYPE_LIST, sub.type()); |
| 159 sub = sub.GetItems(); |
| 160 ASSERT_TRUE(sub.valid()); |
| 161 EXPECT_EQ(base::Value::TYPE_DICTIONARY, sub.type()); |
| 162 Schema subsub = sub.GetProperty("one"); |
| 163 ASSERT_TRUE(subsub.valid()); |
| 164 EXPECT_EQ(base::Value::TYPE_STRING, subsub.type()); |
| 165 subsub = sub.GetProperty("two"); |
| 166 ASSERT_TRUE(subsub.valid()); |
| 167 EXPECT_EQ(base::Value::TYPE_INTEGER, subsub.type()); |
| 168 subsub = sub.GetProperty("invalid"); |
| 169 EXPECT_FALSE(subsub.valid()); |
| 170 |
| 171 sub = schema.GetProperty("ArrayOfArray"); |
| 172 ASSERT_TRUE(sub.valid()); |
| 173 ASSERT_EQ(base::Value::TYPE_LIST, sub.type()); |
| 174 sub = sub.GetItems(); |
| 175 ASSERT_TRUE(sub.valid()); |
| 176 ASSERT_EQ(base::Value::TYPE_LIST, sub.type()); |
| 177 sub = sub.GetItems(); |
| 178 ASSERT_TRUE(sub.valid()); |
| 179 EXPECT_EQ(base::Value::TYPE_STRING, sub.type()); |
| 180 |
| 181 sub = schema.GetProperty("Object"); |
| 182 ASSERT_TRUE(sub.valid()); |
| 183 ASSERT_EQ(base::Value::TYPE_DICTIONARY, sub.type()); |
| 184 subsub = sub.GetProperty("one"); |
| 185 ASSERT_TRUE(subsub.valid()); |
| 186 EXPECT_EQ(base::Value::TYPE_BOOLEAN, subsub.type()); |
| 187 subsub = sub.GetProperty("two"); |
| 188 ASSERT_TRUE(subsub.valid()); |
| 189 EXPECT_EQ(base::Value::TYPE_INTEGER, subsub.type()); |
| 190 subsub = sub.GetProperty("undeclared"); |
| 191 ASSERT_TRUE(subsub.valid()); |
| 192 EXPECT_EQ(base::Value::TYPE_STRING, subsub.type()); |
| 193 |
| 194 struct { |
| 195 const char* expected_key; |
| 196 base::Value::Type expected_type; |
| 197 } kExpectedProperties[] = { |
| 198 { "Array", base::Value::TYPE_LIST }, |
| 199 { "ArrayOfArray", base::Value::TYPE_LIST }, |
| 200 { "ArrayOfObjects", base::Value::TYPE_LIST }, |
| 201 { "Boolean", base::Value::TYPE_BOOLEAN }, |
| 202 { "Integer", base::Value::TYPE_INTEGER }, |
| 203 { "Null", base::Value::TYPE_NULL }, |
| 204 { "Number", base::Value::TYPE_DOUBLE }, |
| 205 { "Object", base::Value::TYPE_DICTIONARY }, |
| 206 { "String", base::Value::TYPE_STRING }, |
| 207 }; |
| 208 Schema::Iterator it = schema.GetPropertiesIterator(); |
| 209 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) { |
| 210 ASSERT_FALSE(it.IsAtEnd()); |
| 211 EXPECT_STREQ(kExpectedProperties[i].expected_key, it.key()); |
| 212 ASSERT_TRUE(it.schema().valid()); |
| 213 EXPECT_EQ(kExpectedProperties[i].expected_type, it.schema().type()); |
| 214 it.Advance(); |
| 215 } |
| 216 EXPECT_TRUE(it.IsAtEnd()); |
| 217 } |
| 218 |
| 219 TEST(SchemaTest, Lookups) { |
| 220 std::string error; |
| 221 scoped_ptr<SchemaOwner> policy_schema = SchemaOwner::Parse( |
| 222 "{" |
| 223 OBJECT_TYPE |
| 224 "}", &error); |
| 225 ASSERT_TRUE(policy_schema) << error; |
| 226 Schema schema = policy_schema->schema(); |
| 227 ASSERT_TRUE(schema.valid()); |
| 228 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); |
| 229 |
| 230 // This empty schema should never find named properties. |
| 231 EXPECT_FALSE(schema.GetKnownProperty("").valid()); |
| 232 EXPECT_FALSE(schema.GetKnownProperty("xyz").valid()); |
| 233 EXPECT_TRUE(schema.GetPropertiesIterator().IsAtEnd()); |
| 234 |
| 235 policy_schema = SchemaOwner::Parse( |
| 236 "{" |
| 237 OBJECT_TYPE "," |
| 238 "\"properties\": {" |
| 239 " \"Boolean\": { \"type\": \"boolean\" }" |
| 240 "}" |
| 241 "}", &error); |
| 242 ASSERT_TRUE(policy_schema) << error; |
| 243 schema = policy_schema->schema(); |
| 244 ASSERT_TRUE(schema.valid()); |
| 245 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); |
| 246 |
| 247 EXPECT_FALSE(schema.GetKnownProperty("").valid()); |
| 248 EXPECT_FALSE(schema.GetKnownProperty("xyz").valid()); |
| 249 EXPECT_TRUE(schema.GetKnownProperty("Boolean").valid()); |
| 250 |
| 251 policy_schema = SchemaOwner::Parse( |
| 252 "{" |
| 253 OBJECT_TYPE "," |
| 254 "\"properties\": {" |
| 255 " \"bb\" : { \"type\": \"null\" }," |
| 256 " \"aa\" : { \"type\": \"boolean\" }," |
| 257 " \"abab\" : { \"type\": \"string\" }," |
| 258 " \"ab\" : { \"type\": \"number\" }," |
| 259 " \"aba\" : { \"type\": \"integer\" }" |
| 260 "}" |
| 261 "}", &error); |
| 262 ASSERT_TRUE(policy_schema) << error; |
| 263 schema = policy_schema->schema(); |
| 264 ASSERT_TRUE(schema.valid()); |
| 265 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); |
| 266 |
| 267 EXPECT_FALSE(schema.GetKnownProperty("").valid()); |
| 268 EXPECT_FALSE(schema.GetKnownProperty("xyz").valid()); |
| 269 |
| 270 struct { |
| 271 const char* expected_key; |
| 272 base::Value::Type expected_type; |
| 273 } kExpectedKeys[] = { |
| 274 { "aa", base::Value::TYPE_BOOLEAN }, |
| 275 { "ab", base::Value::TYPE_DOUBLE }, |
| 276 { "aba", base::Value::TYPE_INTEGER }, |
| 277 { "abab", base::Value::TYPE_STRING }, |
| 278 { "bb", base::Value::TYPE_NULL }, |
| 279 }; |
| 280 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedKeys); ++i) { |
| 281 Schema sub = schema.GetKnownProperty(kExpectedKeys[i].expected_key); |
| 282 ASSERT_TRUE(sub.valid()); |
| 283 EXPECT_EQ(kExpectedKeys[i].expected_type, sub.type()); |
| 284 } |
| 285 } |
| 286 |
| 287 TEST(SchemaTest, WrapSimpleNode) { |
| 288 scoped_ptr<SchemaOwner> policy_schema = SchemaOwner::Wrap(&kTypeString); |
| 289 ASSERT_TRUE(policy_schema); |
| 290 Schema schema = policy_schema->schema(); |
| 291 ASSERT_TRUE(schema.valid()); |
| 292 EXPECT_EQ(base::Value::TYPE_STRING, schema.type()); |
| 293 } |
| 294 |
| 295 TEST(SchemaTest, WrapDictionary) { |
| 296 const internal::SchemaNode kList = { |
| 297 base::Value::TYPE_LIST, |
| 298 &kTypeString, |
| 299 }; |
| 300 |
| 301 const internal::PropertyNode kPropertyNodes[] = { |
| 302 { "Boolean", &kTypeBoolean }, |
| 303 { "Integer", &kTypeInteger }, |
| 304 { "List", &kList }, |
| 305 { "Number", &kTypeNumber }, |
| 306 { "String", &kTypeString }, |
| 307 }; |
| 308 |
| 309 const internal::PropertiesNode kProperties = { |
| 310 kPropertyNodes, |
| 311 kPropertyNodes + arraysize(kPropertyNodes), |
| 312 NULL, |
| 313 }; |
| 314 |
| 315 const internal::SchemaNode root = { |
| 316 base::Value::TYPE_DICTIONARY, |
| 317 &kProperties, |
| 318 }; |
| 319 |
| 320 scoped_ptr<SchemaOwner> policy_schema = SchemaOwner::Wrap(&root); |
| 321 ASSERT_TRUE(policy_schema); |
| 322 Schema schema = policy_schema->schema(); |
| 323 ASSERT_TRUE(schema.valid()); |
| 324 EXPECT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); |
| 325 |
| 326 Schema::Iterator it = schema.GetPropertiesIterator(); |
| 327 for (size_t i = 0; i < arraysize(kPropertyNodes); ++i) { |
| 328 ASSERT_FALSE(it.IsAtEnd()); |
| 329 EXPECT_STREQ(kPropertyNodes[i].key, it.key()); |
| 330 Schema sub = it.schema(); |
| 331 ASSERT_TRUE(sub.valid()); |
| 332 EXPECT_EQ(kPropertyNodes[i].schema->type, sub.type()); |
| 333 it.Advance(); |
| 334 } |
| 335 EXPECT_TRUE(it.IsAtEnd()); |
| 336 } |
| 337 |
| 338 } // namespace policy |
OLD | NEW |