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