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/schema.h" | 5 #include "components/policy/core/common/schema.h" |
6 | 6 |
7 #include "components/policy/core/common/schema_internal.h" | 7 #include "components/policy/core/common/schema_internal.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace policy { | 10 namespace policy { |
(...skipping 30 matching lines...) Expand all Loading... |
41 " \"items\": { \"type\": \"string\" }" | 41 " \"items\": { \"type\": \"string\" }" |
42 " }" | 42 " }" |
43 " }," | 43 " }," |
44 " \"Object\": {" | 44 " \"Object\": {" |
45 " \"type\": \"object\"," | 45 " \"type\": \"object\"," |
46 " \"properties\": {" | 46 " \"properties\": {" |
47 " \"one\": { \"type\": \"boolean\" }," | 47 " \"one\": { \"type\": \"boolean\" }," |
48 " \"two\": { \"type\": \"integer\" }" | 48 " \"two\": { \"type\": \"integer\" }" |
49 " }," | 49 " }," |
50 " \"additionalProperties\": { \"type\": \"string\" }" | 50 " \"additionalProperties\": { \"type\": \"string\" }" |
| 51 " }," |
| 52 " \"IntegerWithEnums\": {" |
| 53 " \"type\": \"integer\"," |
| 54 " \"enum\": [1, 2, 3]" |
| 55 " }," |
| 56 " \"IntegerWithEnumsGaps\": {" |
| 57 " \"type\": \"integer\"," |
| 58 " \"enum\": [10, 20, 30]" |
| 59 " }," |
| 60 " \"StringWithEnums\": {" |
| 61 " \"type\": \"string\"," |
| 62 " \"enum\": [\"one\", \"two\", \"three\"]" |
| 63 " }," |
| 64 " \"IntegerWithRange\": {" |
| 65 " \"type\": \"integer\"," |
| 66 " \"minimum\": 1," |
| 67 " \"maximum\": 3" |
51 " }" | 68 " }" |
52 " }" | 69 " }" |
53 "}"; | 70 "}"; |
54 | 71 |
55 bool ParseFails(const std::string& content) { | 72 bool ParseFails(const std::string& content) { |
56 std::string error; | 73 std::string error; |
57 Schema schema = Schema::Parse(content, &error); | 74 Schema schema = Schema::Parse(content, &error); |
58 if (schema.valid()) | 75 if (schema.valid()) |
59 return false; | 76 return false; |
60 EXPECT_FALSE(error.empty()); | 77 EXPECT_FALSE(error.empty()); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 subsub = sub.GetProperty("one"); | 248 subsub = sub.GetProperty("one"); |
232 ASSERT_TRUE(subsub.valid()); | 249 ASSERT_TRUE(subsub.valid()); |
233 EXPECT_EQ(base::Value::TYPE_BOOLEAN, subsub.type()); | 250 EXPECT_EQ(base::Value::TYPE_BOOLEAN, subsub.type()); |
234 subsub = sub.GetProperty("two"); | 251 subsub = sub.GetProperty("two"); |
235 ASSERT_TRUE(subsub.valid()); | 252 ASSERT_TRUE(subsub.valid()); |
236 EXPECT_EQ(base::Value::TYPE_INTEGER, subsub.type()); | 253 EXPECT_EQ(base::Value::TYPE_INTEGER, subsub.type()); |
237 subsub = sub.GetProperty("undeclared"); | 254 subsub = sub.GetProperty("undeclared"); |
238 ASSERT_TRUE(subsub.valid()); | 255 ASSERT_TRUE(subsub.valid()); |
239 EXPECT_EQ(base::Value::TYPE_STRING, subsub.type()); | 256 EXPECT_EQ(base::Value::TYPE_STRING, subsub.type()); |
240 | 257 |
| 258 sub = schema.GetProperty("IntegerWithEnums"); |
| 259 ASSERT_TRUE(sub.valid()); |
| 260 ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type()); |
| 261 |
| 262 sub = schema.GetProperty("IntegerWithEnumsGaps"); |
| 263 ASSERT_TRUE(sub.valid()); |
| 264 ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type()); |
| 265 |
| 266 sub = schema.GetProperty("StringWithEnums"); |
| 267 ASSERT_TRUE(sub.valid()); |
| 268 ASSERT_EQ(base::Value::TYPE_STRING, sub.type()); |
| 269 |
| 270 sub = schema.GetProperty("IntegerWithRange"); |
| 271 ASSERT_TRUE(sub.valid()); |
| 272 ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type()); |
| 273 |
241 struct { | 274 struct { |
242 const char* expected_key; | 275 const char* expected_key; |
243 base::Value::Type expected_type; | 276 base::Value::Type expected_type; |
244 } kExpectedProperties[] = { | 277 } kExpectedProperties[] = { |
245 { "Array", base::Value::TYPE_LIST }, | 278 { "Array", base::Value::TYPE_LIST }, |
246 { "ArrayOfArray", base::Value::TYPE_LIST }, | 279 { "ArrayOfArray", base::Value::TYPE_LIST }, |
247 { "ArrayOfObjects", base::Value::TYPE_LIST }, | 280 { "ArrayOfObjects", base::Value::TYPE_LIST }, |
248 { "Boolean", base::Value::TYPE_BOOLEAN }, | 281 { "Boolean", base::Value::TYPE_BOOLEAN }, |
249 { "Integer", base::Value::TYPE_INTEGER }, | 282 { "Integer", base::Value::TYPE_INTEGER }, |
250 { "Null", base::Value::TYPE_NULL }, | 283 { "IntegerWithEnums", base::Value::TYPE_INTEGER }, |
251 { "Number", base::Value::TYPE_DOUBLE }, | 284 { "IntegerWithEnumsGaps", base::Value::TYPE_INTEGER }, |
252 { "Object", base::Value::TYPE_DICTIONARY }, | 285 { "IntegerWithRange", base::Value::TYPE_INTEGER }, |
253 { "String", base::Value::TYPE_STRING }, | 286 { "Null", base::Value::TYPE_NULL }, |
| 287 { "Number", base::Value::TYPE_DOUBLE }, |
| 288 { "Object", base::Value::TYPE_DICTIONARY }, |
| 289 { "String", base::Value::TYPE_STRING }, |
| 290 { "StringWithEnums", base::Value::TYPE_STRING }, |
254 }; | 291 }; |
255 Schema::Iterator it = schema.GetPropertiesIterator(); | 292 Schema::Iterator it = schema.GetPropertiesIterator(); |
256 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) { | 293 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) { |
257 ASSERT_FALSE(it.IsAtEnd()); | 294 ASSERT_FALSE(it.IsAtEnd()); |
258 EXPECT_STREQ(kExpectedProperties[i].expected_key, it.key()); | 295 EXPECT_STREQ(kExpectedProperties[i].expected_key, it.key()); |
259 ASSERT_TRUE(it.schema().valid()); | 296 ASSERT_TRUE(it.schema().valid()); |
260 EXPECT_EQ(kExpectedProperties[i].expected_type, it.schema().type()); | 297 EXPECT_EQ(kExpectedProperties[i].expected_type, it.schema().type()); |
261 it.Advance(); | 298 it.Advance(); |
262 } | 299 } |
263 EXPECT_TRUE(it.IsAtEnd()); | 300 EXPECT_TRUE(it.IsAtEnd()); |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 | 533 |
497 { | 534 { |
498 base::DictionaryValue dict; | 535 base::DictionaryValue dict; |
499 dict.SetBoolean("one", true); | 536 dict.SetBoolean("one", true); |
500 dict.SetInteger("two", 2); | 537 dict.SetInteger("two", 2); |
501 dict.SetString("additionally", "a string"); | 538 dict.SetString("additionally", "a string"); |
502 dict.SetString("and also", "another string"); | 539 dict.SetString("and also", "another string"); |
503 bundle.Set("Object", dict.DeepCopy()); | 540 bundle.Set("Object", dict.DeepCopy()); |
504 } | 541 } |
505 | 542 |
| 543 bundle.SetInteger("IntegerWithEnums", 1); |
| 544 bundle.SetInteger("IntegerWithEnumsGaps", 20); |
| 545 bundle.SetString("StringWithEnums", "two"); |
| 546 bundle.SetInteger("IntegerWithRange", 3); |
| 547 |
506 EXPECT_TRUE(schema.Validate(bundle)); | 548 EXPECT_TRUE(schema.Validate(bundle)); |
507 | 549 |
508 bundle.SetString("boom", "bang"); | 550 bundle.SetString("boom", "bang"); |
509 EXPECT_FALSE(schema.Validate(bundle)); | 551 EXPECT_FALSE(schema.Validate(bundle)); |
| 552 bundle.Remove("boom", NULL); |
| 553 |
| 554 bundle.SetInteger("IntegerWithEnums", 0); |
| 555 EXPECT_FALSE(schema.Validate(bundle)); |
| 556 bundle.SetInteger("IntegerWithEnums", 1); |
| 557 |
| 558 bundle.SetInteger("IntegerWithEnumsGaps", 0); |
| 559 EXPECT_FALSE(schema.Validate(bundle)); |
| 560 bundle.SetInteger("IntegerWithEnumsGaps", 9); |
| 561 EXPECT_FALSE(schema.Validate(bundle)); |
| 562 bundle.SetInteger("IntegerWithEnumsGaps", 10); |
| 563 EXPECT_TRUE(schema.Validate(bundle)); |
| 564 bundle.SetInteger("IntegerWithEnumsGaps", 11); |
| 565 EXPECT_FALSE(schema.Validate(bundle)); |
| 566 bundle.SetInteger("IntegerWithEnumsGaps", 19); |
| 567 EXPECT_FALSE(schema.Validate(bundle)); |
| 568 bundle.SetInteger("IntegerWithEnumsGaps", 21); |
| 569 EXPECT_FALSE(schema.Validate(bundle)); |
| 570 bundle.SetInteger("IntegerWithEnumsGaps", 29); |
| 571 EXPECT_FALSE(schema.Validate(bundle)); |
| 572 bundle.SetInteger("IntegerWithEnumsGaps", 30); |
| 573 EXPECT_TRUE(schema.Validate(bundle)); |
| 574 bundle.SetInteger("IntegerWithEnumsGaps", 31); |
| 575 EXPECT_FALSE(schema.Validate(bundle)); |
| 576 bundle.SetInteger("IntegerWithEnumsGaps", 100); |
| 577 EXPECT_FALSE(schema.Validate(bundle)); |
| 578 bundle.SetInteger("IntegerWithEnumsGaps", 20); |
| 579 |
| 580 bundle.SetString("StringWithEnums", "FOUR"); |
| 581 EXPECT_FALSE(schema.Validate(bundle)); |
| 582 bundle.SetString("StringWithEnums", "two"); |
| 583 |
| 584 bundle.SetInteger("IntegerWithRange", 4); |
| 585 EXPECT_FALSE(schema.Validate(bundle)); |
| 586 bundle.SetInteger("IntegerWithRange", 3); |
510 | 587 |
511 } | 588 } |
512 TEST(SchemaTest, InvalidReferences) { | 589 TEST(SchemaTest, InvalidReferences) { |
513 // References to undeclared schemas fail. | 590 // References to undeclared schemas fail. |
514 EXPECT_TRUE(ParseFails( | 591 EXPECT_TRUE(ParseFails( |
515 "{" | 592 "{" |
516 " \"type\": \"object\"," | 593 " \"type\": \"object\"," |
517 " \"properties\": {" | 594 " \"properties\": {" |
518 " \"name\": { \"$ref\": \"undeclared\" }" | 595 " \"name\": { \"$ref\": \"undeclared\" }" |
519 " }" | 596 " }" |
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
761 | 838 |
762 EXPECT_FALSE(ParseFails(SchemaObjectWrapper( | 839 EXPECT_FALSE(ParseFails(SchemaObjectWrapper( |
763 "{" | 840 "{" |
764 " \"type\": \"integer\"," | 841 " \"type\": \"integer\"," |
765 " \"minimum\": 10," | 842 " \"minimum\": 10," |
766 " \"maximum\": 20" | 843 " \"maximum\": 20" |
767 "}"))); | 844 "}"))); |
768 } | 845 } |
769 | 846 |
770 } // namespace policy | 847 } // namespace policy |
OLD | NEW |