Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Side by Side Diff: components/policy/core/common/schema_unittest.cc

Issue 134153005: Add strictness to Schema::Validate() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@expand-policy-schema-2
Patch Set: fix a bug and enhance tests Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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* no_error_returned = "No error returned.";
Joao da Silva 2014/01/23 15:52:36 static const char kNoErrorReturned[] = "No error r
binjin 2014/01/23 17:20:13 Done.
121
122 // Test that Schema::Validate() works as expected.
123 error = no_error_returned;
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 = no_error_returned;
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(),
138 SCHEMA_STRICT, &error));
139 EXPECT_TRUE(schema.Normalize(cloned_value.get(),
140 SCHEMA_STRICT, &error));
Joao da Silva 2014/01/23 15:52:36 Run clang-format on these 2 lines
binjin 2014/01/23 17:20:13 Done.
141 }
142 }
143
81 std::string SchemaObjectWrapper(const std::string& subschema) { 144 std::string SchemaObjectWrapper(const std::string& subschema) {
82 return "{" 145 return "{"
83 " \"type\": \"object\"," 146 " \"type\": \"object\","
84 " \"properties\": {" 147 " \"properties\": {"
85 " \"SomePropertyName\":" + subschema + 148 " \"SomePropertyName\":" + subschema +
86 " }" 149 " }"
87 "}"; 150 "}";
88 } 151 }
89 152
90 } // namespace 153 } // namespace
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 sub = schema.GetProperty("IntegerWithRange"); 333 sub = schema.GetProperty("IntegerWithRange");
271 ASSERT_TRUE(sub.valid()); 334 ASSERT_TRUE(sub.valid());
272 ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type()); 335 ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type());
273 336
274 struct { 337 struct {
275 const char* expected_key; 338 const char* expected_key;
276 base::Value::Type expected_type; 339 base::Value::Type expected_type;
277 } kExpectedProperties[] = { 340 } kExpectedProperties[] = {
278 { "Array", base::Value::TYPE_LIST }, 341 { "Array", base::Value::TYPE_LIST },
279 { "ArrayOfArray", base::Value::TYPE_LIST }, 342 { "ArrayOfArray", base::Value::TYPE_LIST },
343 { "ArrayOfObjectOfArray", base::Value::TYPE_LIST },
280 { "ArrayOfObjects", base::Value::TYPE_LIST }, 344 { "ArrayOfObjects", base::Value::TYPE_LIST },
281 { "Boolean", base::Value::TYPE_BOOLEAN }, 345 { "Boolean", base::Value::TYPE_BOOLEAN },
282 { "Integer", base::Value::TYPE_INTEGER }, 346 { "Integer", base::Value::TYPE_INTEGER },
283 { "IntegerWithEnums", base::Value::TYPE_INTEGER }, 347 { "IntegerWithEnums", base::Value::TYPE_INTEGER },
284 { "IntegerWithEnumsGaps", base::Value::TYPE_INTEGER }, 348 { "IntegerWithEnumsGaps", base::Value::TYPE_INTEGER },
285 { "IntegerWithRange", base::Value::TYPE_INTEGER }, 349 { "IntegerWithRange", base::Value::TYPE_INTEGER },
286 { "Null", base::Value::TYPE_NULL }, 350 { "Null", base::Value::TYPE_NULL },
287 { "Number", base::Value::TYPE_DOUBLE }, 351 { "Number", base::Value::TYPE_DOUBLE },
288 { "Object", base::Value::TYPE_DICTIONARY }, 352 { "Object", base::Value::TYPE_DICTIONARY },
353 { "ObjectOfArray", base::Value::TYPE_DICTIONARY },
354 { "ObjectOfObject", base::Value::TYPE_DICTIONARY },
289 { "String", base::Value::TYPE_STRING }, 355 { "String", base::Value::TYPE_STRING },
290 { "StringWithEnums", base::Value::TYPE_STRING }, 356 { "StringWithEnums", base::Value::TYPE_STRING },
291 }; 357 };
292 Schema::Iterator it = schema.GetPropertiesIterator(); 358 Schema::Iterator it = schema.GetPropertiesIterator();
293 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) { 359 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) {
294 ASSERT_FALSE(it.IsAtEnd()); 360 ASSERT_FALSE(it.IsAtEnd());
295 EXPECT_STREQ(kExpectedProperties[i].expected_key, it.key()); 361 EXPECT_STREQ(kExpectedProperties[i].expected_key, it.key());
296 ASSERT_TRUE(it.schema().valid()); 362 ASSERT_TRUE(it.schema().valid());
297 EXPECT_EQ(kExpectedProperties[i].expected_type, it.schema().type()); 363 EXPECT_EQ(kExpectedProperties[i].expected_type, it.schema().type());
298 it.Advance(); 364 it.Advance();
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 ASSERT_TRUE(subsubsub.valid()); 526 ASSERT_TRUE(subsubsub.valid());
461 ASSERT_EQ(base::Value::TYPE_STRING, subsubsub.type()); 527 ASSERT_EQ(base::Value::TYPE_STRING, subsubsub.type());
462 } 528 }
463 529
464 TEST(SchemaTest, Validate) { 530 TEST(SchemaTest, Validate) {
465 std::string error; 531 std::string error;
466 Schema schema = Schema::Parse(kTestSchema, &error); 532 Schema schema = Schema::Parse(kTestSchema, &error);
467 ASSERT_TRUE(schema.valid()) << error; 533 ASSERT_TRUE(schema.valid()) << error;
468 534
469 base::DictionaryValue bundle; 535 base::DictionaryValue bundle;
470 EXPECT_TRUE(schema.Validate(bundle)); 536 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true);
471 537
472 // Wrong type, expected integer. 538 // Wrong type, expected integer.
473 bundle.SetBoolean("Integer", true); 539 bundle.SetBoolean("Integer", true);
474 EXPECT_FALSE(schema.Validate(bundle)); 540 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
475 541
476 // Wrong type, expected list of strings. 542 // Wrong type, expected list of strings.
477 { 543 {
478 bundle.Clear(); 544 bundle.Clear();
479 base::ListValue list; 545 base::ListValue list;
480 list.AppendInteger(1); 546 list.AppendInteger(1);
481 bundle.Set("Array", list.DeepCopy()); 547 bundle.Set("Array", list.DeepCopy());
482 EXPECT_FALSE(schema.Validate(bundle)); 548 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
483 } 549 }
484 550
485 // Wrong type in a sub-object. 551 // Wrong type in a sub-object.
486 { 552 {
487 bundle.Clear(); 553 bundle.Clear();
488 base::DictionaryValue dict; 554 base::DictionaryValue dict;
489 dict.SetString("one", "one"); 555 dict.SetString("one", "one");
490 bundle.Set("Object", dict.DeepCopy()); 556 bundle.Set("Object", dict.DeepCopy());
491 EXPECT_FALSE(schema.Validate(bundle)); 557 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
492 } 558 }
493 559
494 // Unknown name. 560 // Unknown name.
495 bundle.Clear(); 561 bundle.Clear();
496 bundle.SetBoolean("Unknown", true); 562 bundle.SetBoolean("Unknown", true);
497 EXPECT_FALSE(schema.Validate(bundle)); 563 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
498 564
499 // All of these will be valid. 565 // All of these will be valid.
500 bundle.Clear(); 566 bundle.Clear();
501 bundle.SetBoolean("Boolean", true); 567 bundle.SetBoolean("Boolean", true);
502 bundle.SetInteger("Integer", 123); 568 bundle.SetInteger("Integer", 123);
503 bundle.Set("Null", base::Value::CreateNullValue()); 569 bundle.Set("Null", base::Value::CreateNullValue());
504 bundle.Set("Number", base::Value::CreateDoubleValue(3.14)); 570 bundle.Set("Number", base::Value::CreateDoubleValue(3.14));
505 bundle.SetString("String", "omg"); 571 bundle.SetString("String", "omg");
506 572
507 { 573 {
(...skipping 30 matching lines...) Expand all
538 dict.SetString("additionally", "a string"); 604 dict.SetString("additionally", "a string");
539 dict.SetString("and also", "another string"); 605 dict.SetString("and also", "another string");
540 bundle.Set("Object", dict.DeepCopy()); 606 bundle.Set("Object", dict.DeepCopy());
541 } 607 }
542 608
543 bundle.SetInteger("IntegerWithEnums", 1); 609 bundle.SetInteger("IntegerWithEnums", 1);
544 bundle.SetInteger("IntegerWithEnumsGaps", 20); 610 bundle.SetInteger("IntegerWithEnumsGaps", 20);
545 bundle.SetString("StringWithEnums", "two"); 611 bundle.SetString("StringWithEnums", "two");
546 bundle.SetInteger("IntegerWithRange", 3); 612 bundle.SetInteger("IntegerWithRange", 3);
547 613
548 EXPECT_TRUE(schema.Validate(bundle)); 614 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true);
549
550 bundle.SetString("boom", "bang");
551 EXPECT_FALSE(schema.Validate(bundle));
552 bundle.Remove("boom", NULL);
553 615
554 bundle.SetInteger("IntegerWithEnums", 0); 616 bundle.SetInteger("IntegerWithEnums", 0);
555 EXPECT_FALSE(schema.Validate(bundle)); 617 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
556 bundle.SetInteger("IntegerWithEnums", 1); 618 bundle.SetInteger("IntegerWithEnums", 1);
557 619
558 bundle.SetInteger("IntegerWithEnumsGaps", 0); 620 bundle.SetInteger("IntegerWithEnumsGaps", 0);
559 EXPECT_FALSE(schema.Validate(bundle)); 621 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
560 bundle.SetInteger("IntegerWithEnumsGaps", 9); 622 bundle.SetInteger("IntegerWithEnumsGaps", 9);
561 EXPECT_FALSE(schema.Validate(bundle)); 623 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
562 bundle.SetInteger("IntegerWithEnumsGaps", 10); 624 bundle.SetInteger("IntegerWithEnumsGaps", 10);
563 EXPECT_TRUE(schema.Validate(bundle)); 625 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true);
564 bundle.SetInteger("IntegerWithEnumsGaps", 11); 626 bundle.SetInteger("IntegerWithEnumsGaps", 11);
565 EXPECT_FALSE(schema.Validate(bundle)); 627 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
566 bundle.SetInteger("IntegerWithEnumsGaps", 19); 628 bundle.SetInteger("IntegerWithEnumsGaps", 19);
567 EXPECT_FALSE(schema.Validate(bundle)); 629 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
568 bundle.SetInteger("IntegerWithEnumsGaps", 21); 630 bundle.SetInteger("IntegerWithEnumsGaps", 21);
569 EXPECT_FALSE(schema.Validate(bundle)); 631 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
570 bundle.SetInteger("IntegerWithEnumsGaps", 29); 632 bundle.SetInteger("IntegerWithEnumsGaps", 29);
571 EXPECT_FALSE(schema.Validate(bundle)); 633 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
572 bundle.SetInteger("IntegerWithEnumsGaps", 30); 634 bundle.SetInteger("IntegerWithEnumsGaps", 30);
573 EXPECT_TRUE(schema.Validate(bundle)); 635 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true);
574 bundle.SetInteger("IntegerWithEnumsGaps", 31); 636 bundle.SetInteger("IntegerWithEnumsGaps", 31);
575 EXPECT_FALSE(schema.Validate(bundle)); 637 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
576 bundle.SetInteger("IntegerWithEnumsGaps", 100); 638 bundle.SetInteger("IntegerWithEnumsGaps", 100);
577 EXPECT_FALSE(schema.Validate(bundle)); 639 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
578 bundle.SetInteger("IntegerWithEnumsGaps", 20); 640 bundle.SetInteger("IntegerWithEnumsGaps", 20);
579 641
580 bundle.SetString("StringWithEnums", "FOUR"); 642 bundle.SetString("StringWithEnums", "FOUR");
581 EXPECT_FALSE(schema.Validate(bundle)); 643 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
582 bundle.SetString("StringWithEnums", "two"); 644 bundle.SetString("StringWithEnums", "two");
583 645
584 bundle.SetInteger("IntegerWithRange", 4); 646 bundle.SetInteger("IntegerWithRange", 4);
585 EXPECT_FALSE(schema.Validate(bundle)); 647 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
586 bundle.SetInteger("IntegerWithRange", 3); 648 bundle.SetInteger("IntegerWithRange", 3);
587 649
650 // Unknown top level property.
651 bundle.SetString("boom", "bang");
652 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
653 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true);
654 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN, true);
655 bundle.Remove("boom", NULL);
656
657 // Invalid top level property.
658 bundle.SetInteger("Boolean", 12345);
659 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
660 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
661 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID, true);
662 bundle.SetBoolean("Boolean", true);
663
664 // Tests on ObjectOfObject.
665 {
666 Schema subschema = schema.GetProperty("ObjectOfObject");
667 ASSERT_TRUE(subschema.valid());
668 base::DictionaryValue root;
669
670 // Unknown property.
671 root.SetBoolean("Object.three", false);
672 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false);
673 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
674 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
675 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
676 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
677 root.Remove("Object.three", NULL);
678
679 // Invalid property.
680 root.SetInteger("Object.one", 12345);
681 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false);
682 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
683 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
684 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
685 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
686 root.Remove("Object.one", NULL);
687 }
688
689 // Tests on ArrayOfObjects.
690 {
691 Schema subschema = schema.GetProperty("ArrayOfObjects");
692 ASSERT_TRUE(subschema.valid());
693 base::ListValue root;
694
695 // Unknown property.
696 base::DictionaryValue* dict_value = new base::DictionaryValue();
697 dict_value->SetBoolean("three", true);
698 root.Append(dict_value); // Pass ownership to root.
699 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false);
700 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
701 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
702 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
703 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
704 root.Remove(root.GetSize() - 1, NULL);
705
706 // Invalid property.
707 dict_value = new base::DictionaryValue();
708 dict_value->SetBoolean("two", true);
709 root.Append(dict_value); // Pass ownership to root.
710 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false);
711 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
712 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
713 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
714 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
715 root.Remove(root.GetSize() - 1, NULL);
716 }
717
718 // Tests on ObjectOfArray.
719 {
720 Schema subschema = schema.GetProperty("ObjectOfArray");
721 ASSERT_TRUE(subschema.valid());
722 base::DictionaryValue root;
723
724 base::ListValue* list_value = new base::ListValue();
725 root.Set("List", list_value); // Pass ownership to root;
Joao da Silva 2014/01/23 15:52:36 // Pass ownership to root.
726
727 // No errors.
728 list_value->Append(new base::FundamentalValue(12345));
729 TestSchemaValidation(subschema, root, SCHEMA_STRICT, true);
730 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true);
731 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
732 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
733 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
734
735 // Invalid list item.
736 list_value->Append(new base::StringValue("blabla"));
737 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false);
738 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
739 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
740 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
741 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
742 }
743
744 // Tests on ArrayOfObjectOfArray.
745 {
746 Schema subschema = schema.GetProperty("ArrayOfObjectOfArray");
747 ASSERT_TRUE(subschema.valid());
748 base::ListValue root;
749
750 base::ListValue* list_value = new base::ListValue();
751 base::DictionaryValue* dict_value = new base::DictionaryValue();
752 dict_value->Set("List", list_value); // Pass ownership to dict_value.
753 root.Append(dict_value); // Pass ownership to root
Joao da Silva 2014/01/23 15:52:36 // Pass ownership to root.
754
755 // No errors
Joao da Silva 2014/01/23 15:52:36 // No errors.
binjin 2014/01/23 17:20:13 Done.
756 list_value->Append(new base::StringValue("blabla"));
757 TestSchemaValidation(subschema, root, SCHEMA_STRICT, true);
758 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true);
759 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
760 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
761 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
762
763 // Invalid list item.
764 list_value->Append(new base::FundamentalValue(12345));
765 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false);
766 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
767 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
768 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
769 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
770 }
588 } 771 }
772
589 TEST(SchemaTest, InvalidReferences) { 773 TEST(SchemaTest, InvalidReferences) {
590 // References to undeclared schemas fail. 774 // References to undeclared schemas fail.
591 EXPECT_TRUE(ParseFails( 775 EXPECT_TRUE(ParseFails(
592 "{" 776 "{"
593 " \"type\": \"object\"," 777 " \"type\": \"object\","
594 " \"properties\": {" 778 " \"properties\": {"
595 " \"name\": { \"$ref\": \"undeclared\" }" 779 " \"name\": { \"$ref\": \"undeclared\" }"
596 " }" 780 " }"
597 "}")); 781 "}"));
598 782
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
838 1022
839 EXPECT_FALSE(ParseFails(SchemaObjectWrapper( 1023 EXPECT_FALSE(ParseFails(SchemaObjectWrapper(
840 "{" 1024 "{"
841 " \"type\": \"integer\"," 1025 " \"type\": \"integer\","
842 " \"minimum\": 10," 1026 " \"minimum\": 10,"
843 " \"maximum\": 20" 1027 " \"maximum\": 20"
844 "}"))); 1028 "}")));
845 } 1029 }
846 1030
847 } // namespace policy 1031 } // namespace policy
OLDNEW
« components/policy/core/common/schema_map.cc ('K') | « components/policy/core/common/schema_map.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698