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 "base/memory/scoped_ptr.h" |
7 #include "components/policy/core/common/schema_internal.h" | 8 #include "components/policy/core/common/schema_internal.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
9 | 10 |
10 namespace policy { | 11 namespace policy { |
11 | 12 |
12 namespace { | 13 namespace { |
13 | 14 |
14 const char kTestSchema[] = | 15 const char kTestSchema[] = |
15 "{" | 16 "{" |
16 " \"type\": \"object\"," | 17 " \"type\": \"object\"," |
(...skipping 25 matching lines...) Expand all Loading... |
42 " }" | 43 " }" |
43 " }," | 44 " }," |
44 " \"Object\": {" | 45 " \"Object\": {" |
45 " \"type\": \"object\"," | 46 " \"type\": \"object\"," |
46 " \"properties\": {" | 47 " \"properties\": {" |
47 " \"one\": { \"type\": \"boolean\" }," | 48 " \"one\": { \"type\": \"boolean\" }," |
48 " \"two\": { \"type\": \"integer\" }" | 49 " \"two\": { \"type\": \"integer\" }" |
49 " }," | 50 " }," |
50 " \"additionalProperties\": { \"type\": \"string\" }" | 51 " \"additionalProperties\": { \"type\": \"string\" }" |
51 " }," | 52 " }," |
| 53 " \"ObjectOfObject\": {" |
| 54 " \"type\": \"object\"," |
| 55 " \"properties\": {" |
| 56 " \"Object\": {" |
| 57 " \"type\": \"object\"," |
| 58 " \"properties\": {" |
| 59 " \"one\": { \"type\": \"string\" }," |
| 60 " \"two\": { \"type\": \"integer\" }" |
| 61 " }" |
| 62 " }" |
| 63 " }" |
| 64 " }," |
52 " \"IntegerWithEnums\": {" | 65 " \"IntegerWithEnums\": {" |
53 " \"type\": \"integer\"," | 66 " \"type\": \"integer\"," |
54 " \"enum\": [1, 2, 3]" | 67 " \"enum\": [1, 2, 3]" |
55 " }," | 68 " }," |
56 " \"IntegerWithEnumsGaps\": {" | 69 " \"IntegerWithEnumsGaps\": {" |
57 " \"type\": \"integer\"," | 70 " \"type\": \"integer\"," |
58 " \"enum\": [10, 20, 30]" | 71 " \"enum\": [10, 20, 30]" |
59 " }," | 72 " }," |
60 " \"StringWithEnums\": {" | 73 " \"StringWithEnums\": {" |
61 " \"type\": \"string\"," | 74 " \"type\": \"string\"," |
62 " \"enum\": [\"one\", \"two\", \"three\"]" | 75 " \"enum\": [\"one\", \"two\", \"three\"]" |
63 " }," | 76 " }," |
64 " \"IntegerWithRange\": {" | 77 " \"IntegerWithRange\": {" |
65 " \"type\": \"integer\"," | 78 " \"type\": \"integer\"," |
66 " \"minimum\": 1," | 79 " \"minimum\": 1," |
67 " \"maximum\": 3" | 80 " \"maximum\": 3" |
| 81 " }," |
| 82 " \"ObjectOfArray\": {" |
| 83 " \"type\": \"object\"," |
| 84 " \"properties\": {" |
| 85 " \"List\": {" |
| 86 " \"type\": \"array\"," |
| 87 " \"items\": { \"type\": \"integer\" }" |
| 88 " }" |
| 89 " }" |
| 90 " }," |
| 91 " \"ArrayOfObjectOfArray\": {" |
| 92 " \"type\": \"array\"," |
| 93 " \"items\": {" |
| 94 " \"type\": \"object\"," |
| 95 " \"properties\": {" |
| 96 " \"List\": {" |
| 97 " \"type\": \"array\"," |
| 98 " \"items\": { \"type\": \"string\" }" |
| 99 " }" |
| 100 " }" |
| 101 " }" |
68 " }" | 102 " }" |
69 " }" | 103 " }" |
70 "}"; | 104 "}"; |
71 | 105 |
72 bool ParseFails(const std::string& content) { | 106 bool ParseFails(const std::string& content) { |
73 std::string error; | 107 std::string error; |
74 Schema schema = Schema::Parse(content, &error); | 108 Schema schema = Schema::Parse(content, &error); |
75 if (schema.valid()) | 109 if (schema.valid()) |
76 return false; | 110 return false; |
77 EXPECT_FALSE(error.empty()); | 111 EXPECT_FALSE(error.empty()); |
78 return true; | 112 return true; |
79 } | 113 } |
80 | 114 |
| 115 void TestSchemaValidation(Schema schema, |
| 116 const base::Value& value, |
| 117 SchemaOnErrorStrategy strategy, |
| 118 bool expected_return_value) { |
| 119 std::string error; |
| 120 static const char kNoErrorReturned[] = "No error returned."; |
| 121 |
| 122 // Test that Schema::Validate() works as expected. |
| 123 error = kNoErrorReturned; |
| 124 bool returned = schema.Validate(value, strategy, &error); |
| 125 EXPECT_EQ(returned, expected_return_value) << error; |
| 126 |
| 127 // Test that Schema::Normalize() will return the same value as |
| 128 // Schema::Validate(). |
| 129 error = kNoErrorReturned; |
| 130 scoped_ptr<base::Value> cloned_value(value.DeepCopy()); |
| 131 returned = schema.Normalize(cloned_value.get(), strategy, &error); |
| 132 EXPECT_EQ(returned, expected_return_value) << error; |
| 133 |
| 134 // Test that Schema::Normalize() have actually dropped invalid and unknown |
| 135 // properties. |
| 136 if (expected_return_value) { |
| 137 EXPECT_TRUE(schema.Validate(*cloned_value.get(), SCHEMA_STRICT, &error)); |
| 138 EXPECT_TRUE(schema.Normalize(cloned_value.get(), SCHEMA_STRICT, &error)); |
| 139 } |
| 140 } |
| 141 |
81 std::string SchemaObjectWrapper(const std::string& subschema) { | 142 std::string SchemaObjectWrapper(const std::string& subschema) { |
82 return "{" | 143 return "{" |
83 " \"type\": \"object\"," | 144 " \"type\": \"object\"," |
84 " \"properties\": {" | 145 " \"properties\": {" |
85 " \"SomePropertyName\":" + subschema + | 146 " \"SomePropertyName\":" + subschema + |
86 " }" | 147 " }" |
87 "}"; | 148 "}"; |
88 } | 149 } |
89 | 150 |
90 } // namespace | 151 } // namespace |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 sub = schema.GetProperty("IntegerWithRange"); | 331 sub = schema.GetProperty("IntegerWithRange"); |
271 ASSERT_TRUE(sub.valid()); | 332 ASSERT_TRUE(sub.valid()); |
272 ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type()); | 333 ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type()); |
273 | 334 |
274 struct { | 335 struct { |
275 const char* expected_key; | 336 const char* expected_key; |
276 base::Value::Type expected_type; | 337 base::Value::Type expected_type; |
277 } kExpectedProperties[] = { | 338 } kExpectedProperties[] = { |
278 { "Array", base::Value::TYPE_LIST }, | 339 { "Array", base::Value::TYPE_LIST }, |
279 { "ArrayOfArray", base::Value::TYPE_LIST }, | 340 { "ArrayOfArray", base::Value::TYPE_LIST }, |
| 341 { "ArrayOfObjectOfArray", base::Value::TYPE_LIST }, |
280 { "ArrayOfObjects", base::Value::TYPE_LIST }, | 342 { "ArrayOfObjects", base::Value::TYPE_LIST }, |
281 { "Boolean", base::Value::TYPE_BOOLEAN }, | 343 { "Boolean", base::Value::TYPE_BOOLEAN }, |
282 { "Integer", base::Value::TYPE_INTEGER }, | 344 { "Integer", base::Value::TYPE_INTEGER }, |
283 { "IntegerWithEnums", base::Value::TYPE_INTEGER }, | 345 { "IntegerWithEnums", base::Value::TYPE_INTEGER }, |
284 { "IntegerWithEnumsGaps", base::Value::TYPE_INTEGER }, | 346 { "IntegerWithEnumsGaps", base::Value::TYPE_INTEGER }, |
285 { "IntegerWithRange", base::Value::TYPE_INTEGER }, | 347 { "IntegerWithRange", base::Value::TYPE_INTEGER }, |
286 { "Null", base::Value::TYPE_NULL }, | 348 { "Null", base::Value::TYPE_NULL }, |
287 { "Number", base::Value::TYPE_DOUBLE }, | 349 { "Number", base::Value::TYPE_DOUBLE }, |
288 { "Object", base::Value::TYPE_DICTIONARY }, | 350 { "Object", base::Value::TYPE_DICTIONARY }, |
| 351 { "ObjectOfArray", base::Value::TYPE_DICTIONARY }, |
| 352 { "ObjectOfObject", base::Value::TYPE_DICTIONARY }, |
289 { "String", base::Value::TYPE_STRING }, | 353 { "String", base::Value::TYPE_STRING }, |
290 { "StringWithEnums", base::Value::TYPE_STRING }, | 354 { "StringWithEnums", base::Value::TYPE_STRING }, |
291 }; | 355 }; |
292 Schema::Iterator it = schema.GetPropertiesIterator(); | 356 Schema::Iterator it = schema.GetPropertiesIterator(); |
293 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) { | 357 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) { |
294 ASSERT_FALSE(it.IsAtEnd()); | 358 ASSERT_FALSE(it.IsAtEnd()); |
295 EXPECT_STREQ(kExpectedProperties[i].expected_key, it.key()); | 359 EXPECT_STREQ(kExpectedProperties[i].expected_key, it.key()); |
296 ASSERT_TRUE(it.schema().valid()); | 360 ASSERT_TRUE(it.schema().valid()); |
297 EXPECT_EQ(kExpectedProperties[i].expected_type, it.schema().type()); | 361 EXPECT_EQ(kExpectedProperties[i].expected_type, it.schema().type()); |
298 it.Advance(); | 362 it.Advance(); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 ASSERT_TRUE(subsubsub.valid()); | 524 ASSERT_TRUE(subsubsub.valid()); |
461 ASSERT_EQ(base::Value::TYPE_STRING, subsubsub.type()); | 525 ASSERT_EQ(base::Value::TYPE_STRING, subsubsub.type()); |
462 } | 526 } |
463 | 527 |
464 TEST(SchemaTest, Validate) { | 528 TEST(SchemaTest, Validate) { |
465 std::string error; | 529 std::string error; |
466 Schema schema = Schema::Parse(kTestSchema, &error); | 530 Schema schema = Schema::Parse(kTestSchema, &error); |
467 ASSERT_TRUE(schema.valid()) << error; | 531 ASSERT_TRUE(schema.valid()) << error; |
468 | 532 |
469 base::DictionaryValue bundle; | 533 base::DictionaryValue bundle; |
470 EXPECT_TRUE(schema.Validate(bundle)); | 534 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); |
471 | 535 |
472 // Wrong type, expected integer. | 536 // Wrong type, expected integer. |
473 bundle.SetBoolean("Integer", true); | 537 bundle.SetBoolean("Integer", true); |
474 EXPECT_FALSE(schema.Validate(bundle)); | 538 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
475 | 539 |
476 // Wrong type, expected list of strings. | 540 // Wrong type, expected list of strings. |
477 { | 541 { |
478 bundle.Clear(); | 542 bundle.Clear(); |
479 base::ListValue list; | 543 base::ListValue list; |
480 list.AppendInteger(1); | 544 list.AppendInteger(1); |
481 bundle.Set("Array", list.DeepCopy()); | 545 bundle.Set("Array", list.DeepCopy()); |
482 EXPECT_FALSE(schema.Validate(bundle)); | 546 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
483 } | 547 } |
484 | 548 |
485 // Wrong type in a sub-object. | 549 // Wrong type in a sub-object. |
486 { | 550 { |
487 bundle.Clear(); | 551 bundle.Clear(); |
488 base::DictionaryValue dict; | 552 base::DictionaryValue dict; |
489 dict.SetString("one", "one"); | 553 dict.SetString("one", "one"); |
490 bundle.Set("Object", dict.DeepCopy()); | 554 bundle.Set("Object", dict.DeepCopy()); |
491 EXPECT_FALSE(schema.Validate(bundle)); | 555 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
492 } | 556 } |
493 | 557 |
494 // Unknown name. | 558 // Unknown name. |
495 bundle.Clear(); | 559 bundle.Clear(); |
496 bundle.SetBoolean("Unknown", true); | 560 bundle.SetBoolean("Unknown", true); |
497 EXPECT_FALSE(schema.Validate(bundle)); | 561 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
498 | 562 |
499 // All of these will be valid. | 563 // All of these will be valid. |
500 bundle.Clear(); | 564 bundle.Clear(); |
501 bundle.SetBoolean("Boolean", true); | 565 bundle.SetBoolean("Boolean", true); |
502 bundle.SetInteger("Integer", 123); | 566 bundle.SetInteger("Integer", 123); |
503 bundle.Set("Null", base::Value::CreateNullValue()); | 567 bundle.Set("Null", base::Value::CreateNullValue()); |
504 bundle.Set("Number", base::Value::CreateDoubleValue(3.14)); | 568 bundle.Set("Number", base::Value::CreateDoubleValue(3.14)); |
505 bundle.SetString("String", "omg"); | 569 bundle.SetString("String", "omg"); |
506 | 570 |
507 { | 571 { |
(...skipping 30 matching lines...) Expand all Loading... |
538 dict.SetString("additionally", "a string"); | 602 dict.SetString("additionally", "a string"); |
539 dict.SetString("and also", "another string"); | 603 dict.SetString("and also", "another string"); |
540 bundle.Set("Object", dict.DeepCopy()); | 604 bundle.Set("Object", dict.DeepCopy()); |
541 } | 605 } |
542 | 606 |
543 bundle.SetInteger("IntegerWithEnums", 1); | 607 bundle.SetInteger("IntegerWithEnums", 1); |
544 bundle.SetInteger("IntegerWithEnumsGaps", 20); | 608 bundle.SetInteger("IntegerWithEnumsGaps", 20); |
545 bundle.SetString("StringWithEnums", "two"); | 609 bundle.SetString("StringWithEnums", "two"); |
546 bundle.SetInteger("IntegerWithRange", 3); | 610 bundle.SetInteger("IntegerWithRange", 3); |
547 | 611 |
548 EXPECT_TRUE(schema.Validate(bundle)); | 612 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); |
549 | |
550 bundle.SetString("boom", "bang"); | |
551 EXPECT_FALSE(schema.Validate(bundle)); | |
552 bundle.Remove("boom", NULL); | |
553 | 613 |
554 bundle.SetInteger("IntegerWithEnums", 0); | 614 bundle.SetInteger("IntegerWithEnums", 0); |
555 EXPECT_FALSE(schema.Validate(bundle)); | 615 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
556 bundle.SetInteger("IntegerWithEnums", 1); | 616 bundle.SetInteger("IntegerWithEnums", 1); |
557 | 617 |
558 bundle.SetInteger("IntegerWithEnumsGaps", 0); | 618 bundle.SetInteger("IntegerWithEnumsGaps", 0); |
559 EXPECT_FALSE(schema.Validate(bundle)); | 619 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
560 bundle.SetInteger("IntegerWithEnumsGaps", 9); | 620 bundle.SetInteger("IntegerWithEnumsGaps", 9); |
561 EXPECT_FALSE(schema.Validate(bundle)); | 621 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
562 bundle.SetInteger("IntegerWithEnumsGaps", 10); | 622 bundle.SetInteger("IntegerWithEnumsGaps", 10); |
563 EXPECT_TRUE(schema.Validate(bundle)); | 623 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); |
564 bundle.SetInteger("IntegerWithEnumsGaps", 11); | 624 bundle.SetInteger("IntegerWithEnumsGaps", 11); |
565 EXPECT_FALSE(schema.Validate(bundle)); | 625 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
566 bundle.SetInteger("IntegerWithEnumsGaps", 19); | 626 bundle.SetInteger("IntegerWithEnumsGaps", 19); |
567 EXPECT_FALSE(schema.Validate(bundle)); | 627 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
568 bundle.SetInteger("IntegerWithEnumsGaps", 21); | 628 bundle.SetInteger("IntegerWithEnumsGaps", 21); |
569 EXPECT_FALSE(schema.Validate(bundle)); | 629 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
570 bundle.SetInteger("IntegerWithEnumsGaps", 29); | 630 bundle.SetInteger("IntegerWithEnumsGaps", 29); |
571 EXPECT_FALSE(schema.Validate(bundle)); | 631 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
572 bundle.SetInteger("IntegerWithEnumsGaps", 30); | 632 bundle.SetInteger("IntegerWithEnumsGaps", 30); |
573 EXPECT_TRUE(schema.Validate(bundle)); | 633 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); |
574 bundle.SetInteger("IntegerWithEnumsGaps", 31); | 634 bundle.SetInteger("IntegerWithEnumsGaps", 31); |
575 EXPECT_FALSE(schema.Validate(bundle)); | 635 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
576 bundle.SetInteger("IntegerWithEnumsGaps", 100); | 636 bundle.SetInteger("IntegerWithEnumsGaps", 100); |
577 EXPECT_FALSE(schema.Validate(bundle)); | 637 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
578 bundle.SetInteger("IntegerWithEnumsGaps", 20); | 638 bundle.SetInteger("IntegerWithEnumsGaps", 20); |
579 | 639 |
580 bundle.SetString("StringWithEnums", "FOUR"); | 640 bundle.SetString("StringWithEnums", "FOUR"); |
581 EXPECT_FALSE(schema.Validate(bundle)); | 641 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
582 bundle.SetString("StringWithEnums", "two"); | 642 bundle.SetString("StringWithEnums", "two"); |
583 | 643 |
584 bundle.SetInteger("IntegerWithRange", 4); | 644 bundle.SetInteger("IntegerWithRange", 4); |
585 EXPECT_FALSE(schema.Validate(bundle)); | 645 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
586 bundle.SetInteger("IntegerWithRange", 3); | 646 bundle.SetInteger("IntegerWithRange", 3); |
587 | 647 |
| 648 // Unknown top level property. |
| 649 bundle.SetString("boom", "bang"); |
| 650 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
| 651 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); |
| 652 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN, true); |
| 653 bundle.Remove("boom", NULL); |
| 654 |
| 655 // Invalid top level property. |
| 656 bundle.SetInteger("Boolean", 12345); |
| 657 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); |
| 658 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
| 659 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID, true); |
| 660 bundle.SetBoolean("Boolean", true); |
| 661 |
| 662 // Tests on ObjectOfObject. |
| 663 { |
| 664 Schema subschema = schema.GetProperty("ObjectOfObject"); |
| 665 ASSERT_TRUE(subschema.valid()); |
| 666 base::DictionaryValue root; |
| 667 |
| 668 // Unknown property. |
| 669 root.SetBoolean("Object.three", false); |
| 670 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
| 671 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
| 672 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
| 673 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
| 674 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
| 675 root.Remove("Object.three", NULL); |
| 676 |
| 677 // Invalid property. |
| 678 root.SetInteger("Object.one", 12345); |
| 679 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
| 680 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
| 681 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); |
| 682 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
| 683 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
| 684 root.Remove("Object.one", NULL); |
| 685 } |
| 686 |
| 687 // Tests on ArrayOfObjects. |
| 688 { |
| 689 Schema subschema = schema.GetProperty("ArrayOfObjects"); |
| 690 ASSERT_TRUE(subschema.valid()); |
| 691 base::ListValue root; |
| 692 |
| 693 // Unknown property. |
| 694 base::DictionaryValue* dict_value = new base::DictionaryValue(); |
| 695 dict_value->SetBoolean("three", true); |
| 696 root.Append(dict_value); // Pass ownership to root. |
| 697 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
| 698 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
| 699 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
| 700 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
| 701 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
| 702 root.Remove(root.GetSize() - 1, NULL); |
| 703 |
| 704 // Invalid property. |
| 705 dict_value = new base::DictionaryValue(); |
| 706 dict_value->SetBoolean("two", true); |
| 707 root.Append(dict_value); // Pass ownership to root. |
| 708 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
| 709 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
| 710 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); |
| 711 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
| 712 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
| 713 root.Remove(root.GetSize() - 1, NULL); |
| 714 } |
| 715 |
| 716 // Tests on ObjectOfArray. |
| 717 { |
| 718 Schema subschema = schema.GetProperty("ObjectOfArray"); |
| 719 ASSERT_TRUE(subschema.valid()); |
| 720 base::DictionaryValue root; |
| 721 |
| 722 base::ListValue* list_value = new base::ListValue(); |
| 723 root.Set("List", list_value); // Pass ownership to root. |
| 724 |
| 725 // No errors. |
| 726 list_value->Append(new base::FundamentalValue(12345)); |
| 727 TestSchemaValidation(subschema, root, SCHEMA_STRICT, true); |
| 728 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); |
| 729 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
| 730 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
| 731 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
| 732 |
| 733 // Invalid list item. |
| 734 list_value->Append(new base::StringValue("blabla")); |
| 735 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
| 736 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
| 737 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); |
| 738 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
| 739 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
| 740 } |
| 741 |
| 742 // Tests on ArrayOfObjectOfArray. |
| 743 { |
| 744 Schema subschema = schema.GetProperty("ArrayOfObjectOfArray"); |
| 745 ASSERT_TRUE(subschema.valid()); |
| 746 base::ListValue root; |
| 747 |
| 748 base::ListValue* list_value = new base::ListValue(); |
| 749 base::DictionaryValue* dict_value = new base::DictionaryValue(); |
| 750 dict_value->Set("List", list_value); // Pass ownership to dict_value. |
| 751 root.Append(dict_value); // Pass ownership to root. |
| 752 |
| 753 // No errors. |
| 754 list_value->Append(new base::StringValue("blabla")); |
| 755 TestSchemaValidation(subschema, root, SCHEMA_STRICT, true); |
| 756 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); |
| 757 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); |
| 758 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
| 759 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
| 760 |
| 761 // Invalid list item. |
| 762 list_value->Append(new base::FundamentalValue(12345)); |
| 763 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); |
| 764 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
| 765 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); |
| 766 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
| 767 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); |
| 768 } |
| 769 |
| 770 // Test that integer to double promotion is allowed. |
| 771 bundle.SetInteger("Number", 31415); |
| 772 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); |
588 } | 773 } |
| 774 |
589 TEST(SchemaTest, InvalidReferences) { | 775 TEST(SchemaTest, InvalidReferences) { |
590 // References to undeclared schemas fail. | 776 // References to undeclared schemas fail. |
591 EXPECT_TRUE(ParseFails( | 777 EXPECT_TRUE(ParseFails( |
592 "{" | 778 "{" |
593 " \"type\": \"object\"," | 779 " \"type\": \"object\"," |
594 " \"properties\": {" | 780 " \"properties\": {" |
595 " \"name\": { \"$ref\": \"undeclared\" }" | 781 " \"name\": { \"$ref\": \"undeclared\" }" |
596 " }" | 782 " }" |
597 "}")); | 783 "}")); |
598 | 784 |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
838 | 1024 |
839 EXPECT_FALSE(ParseFails(SchemaObjectWrapper( | 1025 EXPECT_FALSE(ParseFails(SchemaObjectWrapper( |
840 "{" | 1026 "{" |
841 " \"type\": \"integer\"," | 1027 " \"type\": \"integer\"," |
842 " \"minimum\": 10," | 1028 " \"minimum\": 10," |
843 " \"maximum\": 20" | 1029 " \"maximum\": 20" |
844 "}"))); | 1030 "}"))); |
845 } | 1031 } |
846 | 1032 |
847 } // namespace policy | 1033 } // namespace policy |
OLD | NEW |