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

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

Issue 205923004: Add regex support in policy schema (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@json-schema-regex
Patch Set: Validate() against multiple properties Created 6 years, 8 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 "base/memory/scoped_ptr.h"
8 #include "base/strings/stringprintf.h"
8 #include "components/policy/core/common/schema_internal.h" 9 #include "components/policy/core/common/schema_internal.h"
9 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
10 11
11 namespace policy { 12 namespace policy {
12 13
13 namespace { 14 namespace {
14 15
16 #define TEST_SOURCE base::StringPrintf("%s:%i", __FILE__, __LINE__)
Joao da Silva 2014/04/02 09:51:11 An easier way to do this: #define TestSchemaVal
binjin 2014/04/03 10:13:37 Done.
17
15 const char kTestSchema[] = 18 const char kTestSchema[] =
16 "{" 19 "{"
17 " \"type\": \"object\"," 20 " \"type\": \"object\","
18 " \"properties\": {" 21 " \"properties\": {"
19 " \"Boolean\": { \"type\": \"boolean\" }," 22 " \"Boolean\": { \"type\": \"boolean\" },"
20 " \"Integer\": { \"type\": \"integer\" }," 23 " \"Integer\": { \"type\": \"integer\" },"
21 " \"Null\": { \"type\": \"null\" }," 24 " \"Null\": { \"type\": \"null\" },"
22 " \"Number\": { \"type\": \"number\" }," 25 " \"Number\": { \"type\": \"number\" },"
23 " \"String\": { \"type\": \"string\" }," 26 " \"String\": { \"type\": \"string\" },"
24 " \"Array\": {" 27 " \"Array\": {"
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 " \"type\": \"array\"," 95 " \"type\": \"array\","
93 " \"items\": {" 96 " \"items\": {"
94 " \"type\": \"object\"," 97 " \"type\": \"object\","
95 " \"properties\": {" 98 " \"properties\": {"
96 " \"List\": {" 99 " \"List\": {"
97 " \"type\": \"array\"," 100 " \"type\": \"array\","
98 " \"items\": { \"type\": \"string\" }" 101 " \"items\": { \"type\": \"string\" }"
99 " }" 102 " }"
100 " }" 103 " }"
101 " }" 104 " }"
105 " },"
106 " \"StringWithPattern\": {"
107 " \"type\": \"string\","
108 " \"pattern\": \"^foo+$\""
109 " },"
110 " \"ObjectWithPatternProperties\": {"
111 " \"type\": \"object\","
112 " \"patternProperties\": {"
113 " \"^foo+$\": { \"type\": \"integer\" },"
114 " \"^bar+$\": {"
115 " \"type\": \"string\","
116 " \"enum\": [\"one\", \"two\"]"
117 " }"
118 " },"
119 " \"properties\": {"
120 " \"bar\": {"
121 " \"type\": \"string\","
122 " \"enum\": [\"one\", \"three\"]"
123 " }"
124 " }"
102 " }" 125 " }"
103 " }" 126 " }"
104 "}"; 127 "}";
105 128
106 bool ParseFails(const std::string& content) { 129 bool ParseFails(const std::string& content) {
107 std::string error; 130 std::string error;
108 Schema schema = Schema::Parse(content, &error); 131 Schema schema = Schema::Parse(content, &error);
109 if (schema.valid()) 132 if (schema.valid())
110 return false; 133 return false;
111 EXPECT_FALSE(error.empty()); 134 EXPECT_FALSE(error.empty());
112 return true; 135 return true;
113 } 136 }
114 137
115 void TestSchemaValidation(Schema schema, 138 void TestSchemaValidation(const std::string& source,
139 Schema schema,
116 const base::Value& value, 140 const base::Value& value,
117 SchemaOnErrorStrategy strategy, 141 SchemaOnErrorStrategy strategy,
118 bool expected_return_value) { 142 bool expected_return_value) {
119 std::string error; 143 std::string error;
120 static const char kNoErrorReturned[] = "No error returned."; 144 static const char kNoErrorReturned[] = "No error returned.";
121 145
122 // Test that Schema::Validate() works as expected. 146 // Test that Schema::Validate() works as expected.
123 error = kNoErrorReturned; 147 error = kNoErrorReturned;
124 bool returned = schema.Validate(value, strategy, NULL, &error); 148 bool returned = schema.Validate(value, strategy, NULL, &error);
125 EXPECT_EQ(returned, expected_return_value) << error; 149 ASSERT_EQ(expected_return_value, returned) << source << ": " << error;
126 150
127 // Test that Schema::Normalize() will return the same value as 151 // Test that Schema::Normalize() will return the same value as
128 // Schema::Validate(). 152 // Schema::Validate().
129 error = kNoErrorReturned; 153 error = kNoErrorReturned;
130 scoped_ptr<base::Value> cloned_value(value.DeepCopy()); 154 scoped_ptr<base::Value> cloned_value(value.DeepCopy());
131 bool touched = false; 155 bool touched = false;
132 returned = 156 returned =
133 schema.Normalize(cloned_value.get(), strategy, NULL, &error, &touched); 157 schema.Normalize(cloned_value.get(), strategy, NULL, &error, &touched);
134 EXPECT_EQ(returned, expected_return_value) << error; 158 EXPECT_EQ(expected_return_value, returned) << source << ": " << error;
135 159
136 bool strictly_valid = schema.Validate(value, SCHEMA_STRICT, NULL, &error); 160 bool strictly_valid = schema.Validate(value, SCHEMA_STRICT, NULL, &error);
137 EXPECT_EQ(!strictly_valid && returned, touched); 161 EXPECT_EQ(touched, !strictly_valid && returned) << source;
138 162
139 // Test that Schema::Normalize() have actually dropped invalid and unknown 163 // Test that Schema::Normalize() have actually dropped invalid and unknown
140 // properties. 164 // properties.
141 if (expected_return_value) { 165 if (expected_return_value) {
142 EXPECT_TRUE( 166 EXPECT_TRUE(
143 schema.Validate(*cloned_value.get(), SCHEMA_STRICT, NULL, &error)); 167 schema.Validate(*cloned_value.get(), SCHEMA_STRICT, NULL, &error))
144 EXPECT_TRUE(schema.Normalize( 168 << source;
145 cloned_value.get(), SCHEMA_STRICT, NULL, &error, NULL)); 169 EXPECT_TRUE(
170 schema.Normalize(cloned_value.get(), SCHEMA_STRICT, NULL, &error, NULL))
171 << source;
146 } 172 }
147 } 173 }
148 174
149 void TestSchemaValidationWithPath(Schema schema, 175 void TestSchemaValidationWithPath(Schema schema,
150 const base::Value& value, 176 const base::Value& value,
151 const std::string& expected_failure_path) { 177 const std::string& expected_failure_path) {
152 std::string error_path = "NOT_SET"; 178 std::string error_path = "NOT_SET";
153 std::string error; 179 std::string error;
154 180
155 bool returned = schema.Validate(value, SCHEMA_STRICT, &error_path, &error); 181 bool returned = schema.Validate(value, SCHEMA_STRICT, &error_path, &error);
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type()); 369 ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type());
344 370
345 sub = schema.GetProperty("StringWithEnums"); 371 sub = schema.GetProperty("StringWithEnums");
346 ASSERT_TRUE(sub.valid()); 372 ASSERT_TRUE(sub.valid());
347 ASSERT_EQ(base::Value::TYPE_STRING, sub.type()); 373 ASSERT_EQ(base::Value::TYPE_STRING, sub.type());
348 374
349 sub = schema.GetProperty("IntegerWithRange"); 375 sub = schema.GetProperty("IntegerWithRange");
350 ASSERT_TRUE(sub.valid()); 376 ASSERT_TRUE(sub.valid());
351 ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type()); 377 ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type());
352 378
379 sub = schema.GetProperty("StringWithPattern");
380 ASSERT_TRUE(sub.valid());
381 ASSERT_EQ(base::Value::TYPE_STRING, sub.type());
382
383 sub = schema.GetProperty("ObjectWithPatternProperties");
384 ASSERT_TRUE(sub.valid());
385 ASSERT_EQ(base::Value::TYPE_DICTIONARY, sub.type());
386
353 struct { 387 struct {
354 const char* expected_key; 388 const char* expected_key;
355 base::Value::Type expected_type; 389 base::Value::Type expected_type;
356 } kExpectedProperties[] = { 390 } kExpectedProperties[] = {
357 { "Array", base::Value::TYPE_LIST }, 391 { "Array", base::Value::TYPE_LIST },
358 { "ArrayOfArray", base::Value::TYPE_LIST }, 392 { "ArrayOfArray", base::Value::TYPE_LIST },
359 { "ArrayOfObjectOfArray", base::Value::TYPE_LIST }, 393 { "ArrayOfObjectOfArray", base::Value::TYPE_LIST },
360 { "ArrayOfObjects", base::Value::TYPE_LIST }, 394 { "ArrayOfObjects", base::Value::TYPE_LIST },
361 { "Boolean", base::Value::TYPE_BOOLEAN }, 395 { "Boolean", base::Value::TYPE_BOOLEAN },
362 { "Integer", base::Value::TYPE_INTEGER }, 396 { "Integer", base::Value::TYPE_INTEGER },
363 { "IntegerWithEnums", base::Value::TYPE_INTEGER }, 397 { "IntegerWithEnums", base::Value::TYPE_INTEGER },
364 { "IntegerWithEnumsGaps", base::Value::TYPE_INTEGER }, 398 { "IntegerWithEnumsGaps", base::Value::TYPE_INTEGER },
365 { "IntegerWithRange", base::Value::TYPE_INTEGER }, 399 { "IntegerWithRange", base::Value::TYPE_INTEGER },
366 { "Null", base::Value::TYPE_NULL }, 400 { "Null", base::Value::TYPE_NULL },
367 { "Number", base::Value::TYPE_DOUBLE }, 401 { "Number", base::Value::TYPE_DOUBLE },
368 { "Object", base::Value::TYPE_DICTIONARY }, 402 { "Object", base::Value::TYPE_DICTIONARY },
369 { "ObjectOfArray", base::Value::TYPE_DICTIONARY }, 403 { "ObjectOfArray", base::Value::TYPE_DICTIONARY },
370 { "ObjectOfObject", base::Value::TYPE_DICTIONARY }, 404 { "ObjectOfObject", base::Value::TYPE_DICTIONARY },
371 { "String", base::Value::TYPE_STRING }, 405 { "ObjectWithPatternProperties", base::Value::TYPE_DICTIONARY },
372 { "StringWithEnums", base::Value::TYPE_STRING }, 406 { "String", base::Value::TYPE_STRING },
407 { "StringWithEnums", base::Value::TYPE_STRING },
408 { "StringWithPattern", base::Value::TYPE_STRING },
373 }; 409 };
374 Schema::Iterator it = schema.GetPropertiesIterator(); 410 Schema::Iterator it = schema.GetPropertiesIterator();
375 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) { 411 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) {
376 ASSERT_FALSE(it.IsAtEnd()); 412 ASSERT_FALSE(it.IsAtEnd());
377 EXPECT_STREQ(kExpectedProperties[i].expected_key, it.key()); 413 EXPECT_STREQ(kExpectedProperties[i].expected_key, it.key());
378 ASSERT_TRUE(it.schema().valid()); 414 ASSERT_TRUE(it.schema().valid());
379 EXPECT_EQ(kExpectedProperties[i].expected_type, it.schema().type()); 415 EXPECT_EQ(kExpectedProperties[i].expected_type, it.schema().type());
380 it.Advance(); 416 it.Advance();
381 } 417 }
382 EXPECT_TRUE(it.IsAtEnd()); 418 EXPECT_TRUE(it.IsAtEnd());
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 }; 473 };
438 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedKeys); ++i) { 474 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedKeys); ++i) {
439 Schema sub = schema.GetKnownProperty(kExpectedKeys[i].expected_key); 475 Schema sub = schema.GetKnownProperty(kExpectedKeys[i].expected_key);
440 ASSERT_TRUE(sub.valid()); 476 ASSERT_TRUE(sub.valid());
441 EXPECT_EQ(kExpectedKeys[i].expected_type, sub.type()); 477 EXPECT_EQ(kExpectedKeys[i].expected_type, sub.type());
442 } 478 }
443 } 479 }
444 480
445 TEST(SchemaTest, Wrap) { 481 TEST(SchemaTest, Wrap) {
446 const internal::SchemaNode kSchemas[] = { 482 const internal::SchemaNode kSchemas[] = {
447 { base::Value::TYPE_DICTIONARY, 0 }, // 0: root node 483 { base::Value::TYPE_DICTIONARY, 0 }, // 0: root node
448 { base::Value::TYPE_BOOLEAN, -1 }, // 1 484 { base::Value::TYPE_BOOLEAN, -1 }, // 1
449 { base::Value::TYPE_INTEGER, -1 }, // 2 485 { base::Value::TYPE_INTEGER, -1 }, // 2
450 { base::Value::TYPE_DOUBLE, -1 }, // 3 486 { base::Value::TYPE_DOUBLE, -1 }, // 3
451 { base::Value::TYPE_STRING, -1 }, // 4 487 { base::Value::TYPE_STRING, -1 }, // 4
452 { base::Value::TYPE_LIST, 4 }, // 5: list of strings. 488 { base::Value::TYPE_LIST, 4 }, // 5: list of strings.
453 { base::Value::TYPE_LIST, 5 }, // 6: list of lists of strings. 489 { base::Value::TYPE_LIST, 5 }, // 6: list of lists of strings.
454 { base::Value::TYPE_INTEGER, 0 }, // 7: integer enumerations. 490 { base::Value::TYPE_INTEGER, 0 }, // 7: integer enumerations.
455 { base::Value::TYPE_INTEGER, 1 }, // 8: ranged integers. 491 { base::Value::TYPE_INTEGER, 1 }, // 8: ranged integers.
456 { base::Value::TYPE_STRING, 2 }, // 9: string enumerations. 492 { base::Value::TYPE_STRING, 2 }, // 9: string enumerations.
493 { base::Value::TYPE_STRING, 3 }, // 10: string with pattern.
457 }; 494 };
458 495
459 const internal::PropertyNode kPropertyNodes[] = { 496 const internal::PropertyNode kPropertyNodes[] = {
460 { "Boolean", 1 }, // 0 497 { "Boolean", 1 }, // 0
461 { "Integer", 2 }, // 1 498 { "Integer", 2 }, // 1
462 { "Number", 3 }, // 2 499 { "Number", 3 }, // 2
463 { "String", 4 }, // 3 500 { "String", 4 }, // 3
464 { "List", 5 }, // 4 501 { "List", 5 }, // 4
465 { "IntEnum", 7 }, // 5 502 { "IntEnum", 7 }, // 5
466 { "RangedInt", 8 }, // 6 503 { "RangedInt", 8 }, // 6
467 { "StrEnum", 9 }, // 7 504 { "StrEnum", 9 }, // 7
505 { "StrPat", 10 }, // 8
506 { "bar+$", 4 }, // 9
Joao da Silva 2014/04/02 09:51:11 nit: remove a space before the "4" to align the re
binjin 2014/04/03 10:13:37 Done.
468 }; 507 };
469 508
470 const internal::PropertiesNode kProperties[] = { 509 const internal::PropertiesNode kProperties[] = {
471 // 0 to 8 (exclusive) are the known properties in kPropertyNodes, and 6 is 510 // 0 to 9 (exclusive) are the known properties in kPropertyNodes, 9 is
472 // the addionalProperties node. 511 // patternProperties and 6 is the addionalProperties node.
Joao da Silva 2014/04/02 09:51:11 additionalProperties
binjin 2014/04/03 10:13:37 Done.
473 { 0, 8, 6 }, 512 { 0, 9, 10, 6 },
474 }; 513 };
475 514
476 const internal::RestrictionNode kRestriction[] = { 515 const internal::RestrictionNode kRestriction[] = {
477 {{0, 3}}, // [1, 2, 3] 516 {{0, 3}}, // 0: [1, 2, 3]
478 {{5, 1}}, // minimum = 1, maximum = 5 517 {{5, 1}}, // 1: minimum = 1, maximum = 5
479 {{0, 3}}, // ["one", "two", "three"] 518 {{0, 3}}, // 2: ["one", "two", "three"]
519 {{3, 3}}, // 3: pattern "foo+"
480 }; 520 };
481 521
482 const int kIntEnums[] = {1, 2, 3}; 522 const int kIntEnums[] = {1, 2, 3};
483 523
484 const char* kStringEnums[] = { 524 const char* kStringEnums[] = {
485 "one", 525 "one", // 0
486 "two", 526 "two", // 1
487 "three", 527 "three", // 2
528 "foo+", // 3
488 }; 529 };
489 530
490 const internal::SchemaData kData = { 531 const internal::SchemaData kData = {
491 kSchemas, 532 kSchemas,
492 kPropertyNodes, 533 kPropertyNodes,
493 kProperties, 534 kProperties,
494 kRestriction, 535 kRestriction,
495 kIntEnums, 536 kIntEnums,
496 kStringEnums, 537 kStringEnums,
497 }; 538 };
498 539
499 Schema schema = Schema::Wrap(&kData); 540 Schema schema = Schema::Wrap(&kData);
500 ASSERT_TRUE(schema.valid()); 541 ASSERT_TRUE(schema.valid());
501 EXPECT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); 542 EXPECT_EQ(base::Value::TYPE_DICTIONARY, schema.type());
502 543
503 struct { 544 struct {
504 const char* key; 545 const char* key;
505 base::Value::Type type; 546 base::Value::Type type;
506 } kExpectedProperties[] = { 547 } kExpectedProperties[] = {
507 { "Boolean", base::Value::TYPE_BOOLEAN }, 548 { "Boolean", base::Value::TYPE_BOOLEAN },
508 { "Integer", base::Value::TYPE_INTEGER }, 549 { "Integer", base::Value::TYPE_INTEGER },
509 { "Number", base::Value::TYPE_DOUBLE }, 550 { "Number", base::Value::TYPE_DOUBLE },
510 { "String", base::Value::TYPE_STRING }, 551 { "String", base::Value::TYPE_STRING },
511 { "List", base::Value::TYPE_LIST }, 552 { "List", base::Value::TYPE_LIST },
512 { "IntEnum", base::Value::TYPE_INTEGER }, 553 { "IntEnum", base::Value::TYPE_INTEGER },
513 { "RangedInt", base::Value::TYPE_INTEGER }, 554 { "RangedInt", base::Value::TYPE_INTEGER },
514 { "StrEnum", base::Value::TYPE_STRING } 555 { "StrEnum", base::Value::TYPE_STRING },
556 { "StrPat", base::Value::TYPE_STRING },
515 }; 557 };
516 558
517 Schema::Iterator it = schema.GetPropertiesIterator(); 559 Schema::Iterator it = schema.GetPropertiesIterator();
518 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) { 560 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) {
519 ASSERT_FALSE(it.IsAtEnd()); 561 ASSERT_FALSE(it.IsAtEnd());
520 EXPECT_STREQ(kExpectedProperties[i].key, it.key()); 562 EXPECT_STREQ(kExpectedProperties[i].key, it.key());
521 Schema sub = it.schema(); 563 Schema sub = it.schema();
522 ASSERT_TRUE(sub.valid()); 564 ASSERT_TRUE(sub.valid());
523 EXPECT_EQ(kExpectedProperties[i].type, sub.type()); 565 EXPECT_EQ(kExpectedProperties[i].type, sub.type());
524 566
525 if (sub.type() == base::Value::TYPE_LIST) { 567 if (sub.type() == base::Value::TYPE_LIST) {
526 Schema items = sub.GetItems(); 568 Schema items = sub.GetItems();
527 ASSERT_TRUE(items.valid()); 569 ASSERT_TRUE(items.valid());
528 EXPECT_EQ(base::Value::TYPE_STRING, items.type()); 570 EXPECT_EQ(base::Value::TYPE_STRING, items.type());
529 } 571 }
530 572
531 it.Advance(); 573 it.Advance();
532 } 574 }
533 EXPECT_TRUE(it.IsAtEnd()); 575 EXPECT_TRUE(it.IsAtEnd());
534 576
535 Schema sub = schema.GetAdditionalProperties(); 577 Schema sub = schema.GetAdditionalProperties();
536 ASSERT_TRUE(sub.valid()); 578 ASSERT_TRUE(sub.valid());
537 ASSERT_EQ(base::Value::TYPE_LIST, sub.type()); 579 ASSERT_EQ(base::Value::TYPE_LIST, sub.type());
538 Schema subsub = sub.GetItems(); 580 Schema subsub = sub.GetItems();
539 ASSERT_TRUE(subsub.valid()); 581 ASSERT_TRUE(subsub.valid());
540 ASSERT_EQ(base::Value::TYPE_LIST, subsub.type()); 582 ASSERT_EQ(base::Value::TYPE_LIST, subsub.type());
541 Schema subsubsub = subsub.GetItems(); 583 Schema subsubsub = subsub.GetItems();
542 ASSERT_TRUE(subsubsub.valid()); 584 ASSERT_TRUE(subsubsub.valid());
543 ASSERT_EQ(base::Value::TYPE_STRING, subsubsub.type()); 585 ASSERT_EQ(base::Value::TYPE_STRING, subsubsub.type());
586
587 Schema::SchemaList schema_list = schema.GetPatternProperties("barr");
588 ASSERT_EQ(static_cast<size_t>(1), schema_list.size());
Joao da Silva 2014/04/02 09:51:11 1u
binjin 2014/04/03 10:13:37 Done.
589 sub = schema_list[0];
590 ASSERT_TRUE(sub.valid());
591 EXPECT_EQ(base::Value::TYPE_STRING, sub.type());
592
593 EXPECT_TRUE(schema.GetPatternProperties("ba").empty());
594 EXPECT_TRUE(schema.GetPatternProperties("bar+$").empty());
544 } 595 }
545 596
546 TEST(SchemaTest, Validate) { 597 TEST(SchemaTest, Validate) {
547 std::string error; 598 std::string error;
548 Schema schema = Schema::Parse(kTestSchema, &error); 599 Schema schema = Schema::Parse(kTestSchema, &error);
549 ASSERT_TRUE(schema.valid()) << error; 600 ASSERT_TRUE(schema.valid()) << error;
550 601
551 base::DictionaryValue bundle; 602 base::DictionaryValue bundle;
552 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); 603 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, true);
553 604
554 // Wrong type, expected integer. 605 // Wrong type, expected integer.
555 bundle.SetBoolean("Integer", true); 606 bundle.SetBoolean("Integer", true);
556 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 607 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
557 608
558 // Wrong type, expected list of strings. 609 // Wrong type, expected list of strings.
559 { 610 {
560 bundle.Clear(); 611 bundle.Clear();
561 base::ListValue list; 612 base::ListValue list;
562 list.AppendInteger(1); 613 list.AppendInteger(1);
563 bundle.Set("Array", list.DeepCopy()); 614 bundle.Set("Array", list.DeepCopy());
564 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 615 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
565 } 616 }
566 617
567 // Wrong type in a sub-object. 618 // Wrong type in a sub-object.
568 { 619 {
569 bundle.Clear(); 620 bundle.Clear();
570 base::DictionaryValue dict; 621 base::DictionaryValue dict;
571 dict.SetString("one", "one"); 622 dict.SetString("one", "one");
572 bundle.Set("Object", dict.DeepCopy()); 623 bundle.Set("Object", dict.DeepCopy());
573 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 624 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
574 } 625 }
575 626
576 // Unknown name. 627 // Unknown name.
577 bundle.Clear(); 628 bundle.Clear();
578 bundle.SetBoolean("Unknown", true); 629 bundle.SetBoolean("Unknown", true);
579 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 630 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
580 631
581 // All of these will be valid. 632 // All of these will be valid.
582 bundle.Clear(); 633 bundle.Clear();
583 bundle.SetBoolean("Boolean", true); 634 bundle.SetBoolean("Boolean", true);
584 bundle.SetInteger("Integer", 123); 635 bundle.SetInteger("Integer", 123);
585 bundle.Set("Null", base::Value::CreateNullValue()); 636 bundle.Set("Null", base::Value::CreateNullValue());
586 bundle.Set("Number", base::Value::CreateDoubleValue(3.14)); 637 bundle.Set("Number", base::Value::CreateDoubleValue(3.14));
587 bundle.SetString("String", "omg"); 638 bundle.SetString("String", "omg");
588 639
589 { 640 {
(...skipping 30 matching lines...) Expand all
620 dict.SetString("additionally", "a string"); 671 dict.SetString("additionally", "a string");
621 dict.SetString("and also", "another string"); 672 dict.SetString("and also", "another string");
622 bundle.Set("Object", dict.DeepCopy()); 673 bundle.Set("Object", dict.DeepCopy());
623 } 674 }
624 675
625 bundle.SetInteger("IntegerWithEnums", 1); 676 bundle.SetInteger("IntegerWithEnums", 1);
626 bundle.SetInteger("IntegerWithEnumsGaps", 20); 677 bundle.SetInteger("IntegerWithEnumsGaps", 20);
627 bundle.SetString("StringWithEnums", "two"); 678 bundle.SetString("StringWithEnums", "two");
628 bundle.SetInteger("IntegerWithRange", 3); 679 bundle.SetInteger("IntegerWithRange", 3);
629 680
630 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); 681 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, true);
631 682
632 bundle.SetInteger("IntegerWithEnums", 0); 683 bundle.SetInteger("IntegerWithEnums", 0);
633 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 684 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
634 bundle.SetInteger("IntegerWithEnums", 1); 685 bundle.SetInteger("IntegerWithEnums", 1);
635 686
636 bundle.SetInteger("IntegerWithEnumsGaps", 0); 687 bundle.SetInteger("IntegerWithEnumsGaps", 0);
637 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 688 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
638 bundle.SetInteger("IntegerWithEnumsGaps", 9); 689 bundle.SetInteger("IntegerWithEnumsGaps", 9);
639 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 690 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
640 bundle.SetInteger("IntegerWithEnumsGaps", 10); 691 bundle.SetInteger("IntegerWithEnumsGaps", 10);
641 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); 692 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, true);
642 bundle.SetInteger("IntegerWithEnumsGaps", 11); 693 bundle.SetInteger("IntegerWithEnumsGaps", 11);
643 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 694 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
644 bundle.SetInteger("IntegerWithEnumsGaps", 19); 695 bundle.SetInteger("IntegerWithEnumsGaps", 19);
645 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 696 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
646 bundle.SetInteger("IntegerWithEnumsGaps", 21); 697 bundle.SetInteger("IntegerWithEnumsGaps", 21);
647 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 698 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
648 bundle.SetInteger("IntegerWithEnumsGaps", 29); 699 bundle.SetInteger("IntegerWithEnumsGaps", 29);
649 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 700 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
650 bundle.SetInteger("IntegerWithEnumsGaps", 30); 701 bundle.SetInteger("IntegerWithEnumsGaps", 30);
651 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); 702 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, true);
652 bundle.SetInteger("IntegerWithEnumsGaps", 31); 703 bundle.SetInteger("IntegerWithEnumsGaps", 31);
653 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 704 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
654 bundle.SetInteger("IntegerWithEnumsGaps", 100); 705 bundle.SetInteger("IntegerWithEnumsGaps", 100);
655 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 706 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
656 bundle.SetInteger("IntegerWithEnumsGaps", 20); 707 bundle.SetInteger("IntegerWithEnumsGaps", 20);
657 708
658 bundle.SetString("StringWithEnums", "FOUR"); 709 bundle.SetString("StringWithEnums", "FOUR");
659 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 710 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
660 bundle.SetString("StringWithEnums", "two"); 711 bundle.SetString("StringWithEnums", "two");
661 712
662 bundle.SetInteger("IntegerWithRange", 4); 713 bundle.SetInteger("IntegerWithRange", 4);
663 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 714 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
664 bundle.SetInteger("IntegerWithRange", 3); 715 bundle.SetInteger("IntegerWithRange", 3);
665 716
666 // Unknown top level property. 717 // Unknown top level property.
667 bundle.SetString("boom", "bang"); 718 bundle.SetString("boom", "bang");
668 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 719 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
669 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); 720 TestSchemaValidation(
670 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN, true); 721 TEST_SOURCE, schema, bundle, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true);
722 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_ALLOW_UNKNOWN, true);
671 TestSchemaValidationWithPath(schema, bundle, ""); 723 TestSchemaValidationWithPath(schema, bundle, "");
672 bundle.Remove("boom", NULL); 724 bundle.Remove("boom", NULL);
673 725
674 // Invalid top level property. 726 // Invalid top level property.
675 bundle.SetInteger("Boolean", 12345); 727 bundle.SetInteger("Boolean", 12345);
676 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false); 728 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
677 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); 729 TestSchemaValidation(
678 TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID, true); 730 TEST_SOURCE, schema, bundle, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
731 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_ALLOW_INVALID, true);
679 TestSchemaValidationWithPath(schema, bundle, "Boolean"); 732 TestSchemaValidationWithPath(schema, bundle, "Boolean");
680 bundle.SetBoolean("Boolean", true); 733 bundle.SetBoolean("Boolean", true);
681 734
682 // Tests on ObjectOfObject. 735 // Tests on ObjectOfObject.
683 { 736 {
684 Schema subschema = schema.GetProperty("ObjectOfObject"); 737 Schema subschema = schema.GetProperty("ObjectOfObject");
685 ASSERT_TRUE(subschema.valid()); 738 ASSERT_TRUE(subschema.valid());
686 base::DictionaryValue root; 739 base::DictionaryValue root;
687 740
688 // Unknown property. 741 // Unknown property.
689 root.SetBoolean("Object.three", false); 742 root.SetBoolean("Object.three", false);
690 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); 743 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
691 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); 744 TestSchemaValidation(
692 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); 745 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
693 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); 746 TestSchemaValidation(
694 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); 747 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
748 TestSchemaValidation(
749 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
750 TestSchemaValidation(
751 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true);
695 TestSchemaValidationWithPath(subschema, root, "Object"); 752 TestSchemaValidationWithPath(subschema, root, "Object");
696 root.Remove("Object.three", NULL); 753 root.Remove("Object.three", NULL);
697 754
698 // Invalid property. 755 // Invalid property.
699 root.SetInteger("Object.one", 12345); 756 root.SetInteger("Object.one", 12345);
700 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); 757 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
701 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); 758 TestSchemaValidation(
702 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); 759 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
703 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); 760 TestSchemaValidation(
704 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); 761 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
762 TestSchemaValidation(
763 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
764 TestSchemaValidation(
765 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true);
705 TestSchemaValidationWithPath(subschema, root, "Object.one"); 766 TestSchemaValidationWithPath(subschema, root, "Object.one");
706 root.Remove("Object.one", NULL); 767 root.Remove("Object.one", NULL);
707 } 768 }
708 769
709 // Tests on ArrayOfObjects. 770 // Tests on ArrayOfObjects.
710 { 771 {
711 Schema subschema = schema.GetProperty("ArrayOfObjects"); 772 Schema subschema = schema.GetProperty("ArrayOfObjects");
712 ASSERT_TRUE(subschema.valid()); 773 ASSERT_TRUE(subschema.valid());
713 base::ListValue root; 774 base::ListValue root;
714 775
715 // Unknown property. 776 // Unknown property.
716 base::DictionaryValue* dict_value = new base::DictionaryValue(); 777 base::DictionaryValue* dict_value = new base::DictionaryValue();
717 dict_value->SetBoolean("three", true); 778 dict_value->SetBoolean("three", true);
718 root.Append(dict_value); // Pass ownership to root. 779 root.Append(dict_value); // Pass ownership to root.
719 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); 780 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
720 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); 781 TestSchemaValidation(
721 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); 782 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
722 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); 783 TestSchemaValidation(
723 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); 784 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
785 TestSchemaValidation(
786 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
787 TestSchemaValidation(
788 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true);
724 TestSchemaValidationWithPath(subschema, root, "items[0]"); 789 TestSchemaValidationWithPath(subschema, root, "items[0]");
725 root.Remove(root.GetSize() - 1, NULL); 790 root.Remove(root.GetSize() - 1, NULL);
726 791
727 // Invalid property. 792 // Invalid property.
728 dict_value = new base::DictionaryValue(); 793 dict_value = new base::DictionaryValue();
729 dict_value->SetBoolean("two", true); 794 dict_value->SetBoolean("two", true);
730 root.Append(dict_value); // Pass ownership to root. 795 root.Append(dict_value); // Pass ownership to root.
731 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); 796 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
732 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); 797 TestSchemaValidation(
733 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); 798 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
734 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); 799 TestSchemaValidation(
735 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); 800 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
801 TestSchemaValidation(
802 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
803 TestSchemaValidation(
804 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true);
736 TestSchemaValidationWithPath(subschema, root, "items[0].two"); 805 TestSchemaValidationWithPath(subschema, root, "items[0].two");
737 root.Remove(root.GetSize() - 1, NULL); 806 root.Remove(root.GetSize() - 1, NULL);
738 } 807 }
739 808
740 // Tests on ObjectOfArray. 809 // Tests on ObjectOfArray.
741 { 810 {
742 Schema subschema = schema.GetProperty("ObjectOfArray"); 811 Schema subschema = schema.GetProperty("ObjectOfArray");
743 ASSERT_TRUE(subschema.valid()); 812 ASSERT_TRUE(subschema.valid());
744 base::DictionaryValue root; 813 base::DictionaryValue root;
745 814
746 base::ListValue* list_value = new base::ListValue(); 815 base::ListValue* list_value = new base::ListValue();
747 root.Set("List", list_value); // Pass ownership to root. 816 root.Set("List", list_value); // Pass ownership to root.
748 817
749 // Test that there are not errors here. 818 // Test that there are not errors here.
750 list_value->Append(new base::FundamentalValue(12345)); 819 list_value->Append(new base::FundamentalValue(12345));
751 TestSchemaValidation(subschema, root, SCHEMA_STRICT, true); 820 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, true);
752 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); 821 TestSchemaValidation(
753 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); 822 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true);
754 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); 823 TestSchemaValidation(
755 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); 824 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
825 TestSchemaValidation(
826 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
827 TestSchemaValidation(
828 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true);
756 829
757 // Invalid list item. 830 // Invalid list item.
758 list_value->Append(new base::StringValue("blabla")); 831 list_value->Append(new base::StringValue("blabla"));
759 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); 832 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
760 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); 833 TestSchemaValidation(
761 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); 834 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
762 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); 835 TestSchemaValidation(
763 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); 836 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
837 TestSchemaValidation(
838 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
839 TestSchemaValidation(
840 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true);
764 TestSchemaValidationWithPath(subschema, root, "List.items[1]"); 841 TestSchemaValidationWithPath(subschema, root, "List.items[1]");
765 } 842 }
766 843
767 // Tests on ArrayOfObjectOfArray. 844 // Tests on ArrayOfObjectOfArray.
768 { 845 {
769 Schema subschema = schema.GetProperty("ArrayOfObjectOfArray"); 846 Schema subschema = schema.GetProperty("ArrayOfObjectOfArray");
770 ASSERT_TRUE(subschema.valid()); 847 ASSERT_TRUE(subschema.valid());
771 base::ListValue root; 848 base::ListValue root;
772 849
773 base::ListValue* list_value = new base::ListValue(); 850 base::ListValue* list_value = new base::ListValue();
774 base::DictionaryValue* dict_value = new base::DictionaryValue(); 851 base::DictionaryValue* dict_value = new base::DictionaryValue();
775 dict_value->Set("List", list_value); // Pass ownership to dict_value. 852 dict_value->Set("List", list_value); // Pass ownership to dict_value.
776 root.Append(dict_value); // Pass ownership to root. 853 root.Append(dict_value); // Pass ownership to root.
777 854
778 // Test that there are not errors here. 855 // Test that there are not errors here.
779 list_value->Append(new base::StringValue("blabla")); 856 list_value->Append(new base::StringValue("blabla"));
780 TestSchemaValidation(subschema, root, SCHEMA_STRICT, true); 857 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, true);
781 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); 858 TestSchemaValidation(
782 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true); 859 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true);
783 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); 860 TestSchemaValidation(
784 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); 861 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
862 TestSchemaValidation(
863 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
864 TestSchemaValidation(
865 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true);
785 866
786 // Invalid list item. 867 // Invalid list item.
787 list_value->Append(new base::FundamentalValue(12345)); 868 list_value->Append(new base::FundamentalValue(12345));
788 TestSchemaValidation(subschema, root, SCHEMA_STRICT, false); 869 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
789 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); 870 TestSchemaValidation(
790 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false); 871 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
791 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); 872 TestSchemaValidation(
792 TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true); 873 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
874 TestSchemaValidation(
875 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
876 TestSchemaValidation(
877 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true);
793 TestSchemaValidationWithPath(subschema, root, "items[0].List.items[1]"); 878 TestSchemaValidationWithPath(subschema, root, "items[0].List.items[1]");
794 } 879 }
795 880
881 // Tests on StringWithPattern.
882 {
883 Schema subschema = schema.GetProperty("StringWithPattern");
884 ASSERT_TRUE(subschema.valid());
885
886 TestSchemaValidation(TEST_SOURCE,
887 subschema,
888 base::StringValue(std::string("foobar")),
Joao da Silva 2014/04/02 09:51:11 You can pass a const char* directly to StringValue
binjin 2014/04/03 10:13:37 Done.
889 SCHEMA_STRICT,
890 false);
891 TestSchemaValidation(TEST_SOURCE,
892 subschema,
893 base::StringValue(std::string("foo")),
894 SCHEMA_STRICT,
895 true);
896 TestSchemaValidation(TEST_SOURCE,
897 subschema,
898 base::StringValue(std::string("fo")),
899 SCHEMA_STRICT,
900 false);
901 TestSchemaValidation(TEST_SOURCE,
902 subschema,
903 base::StringValue(std::string("fooo")),
904 SCHEMA_STRICT,
905 true);
906 TestSchemaValidation(TEST_SOURCE,
907 subschema,
908 base::StringValue(std::string("^foo+$")),
909 SCHEMA_STRICT,
910 false);
911 }
912
913 // Tests on ObjectWithPatternProperties
Joao da Silva 2014/04/02 09:51:11 Terminate sentence with .
binjin 2014/04/03 10:13:37 Done.
914 {
915 Schema subschema = schema.GetProperty("ObjectWithPatternProperties");
916 ASSERT_TRUE(subschema.valid());
917 base::DictionaryValue root;
918
919 ASSERT_EQ(static_cast<size_t>(1),
Joao da Silva 2014/04/02 09:51:11 1u (also below)
binjin 2014/04/03 10:13:37 Done.
920 subschema.GetPatternProperties("fooo").size());
921 ASSERT_EQ(static_cast<size_t>(1),
922 subschema.GetPatternProperties("foo").size());
923 ASSERT_EQ(static_cast<size_t>(1),
924 subschema.GetPatternProperties("barr").size());
925 ASSERT_EQ(static_cast<size_t>(1),
926 subschema.GetPatternProperties("bar").size());
927 ASSERT_EQ(static_cast<size_t>(1),
928 subschema.GetMatchingProperties("fooo").size());
929 ASSERT_EQ(static_cast<size_t>(1),
930 subschema.GetMatchingProperties("foo").size());
931 ASSERT_EQ(static_cast<size_t>(1),
932 subschema.GetMatchingProperties("barr").size());
933 ASSERT_EQ(static_cast<size_t>(2),
934 subschema.GetMatchingProperties("bar").size());
935 ASSERT_TRUE(subschema.GetPatternProperties("foobar").empty());
936
937 root.SetInteger("fooo", 123);
938 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, true);
939 root.SetBoolean("fooo", false);
940 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
941 root.Remove("fooo", NULL);
942
943 root.SetInteger("foo", 123);
944 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, true);
945 root.SetBoolean("foo", false);
946 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
947 root.Remove("foo", NULL);
948
949 root.SetString("barr", "one");
950 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, true);
951 root.SetString("barr", "three");
952 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
953 root.SetBoolean("barr", false);
954 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
955 root.Remove("barr", NULL);
956
957 root.SetString("bar", "one");
958 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, true);
959 root.SetString("bar", "two");
960 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
961 root.SetString("bar", "three");
962 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
963 root.Remove("bar", NULL);
964
965 root.SetInteger("foobar", 123);
966 TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
967 TestSchemaValidation(
968 TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
969 root.Remove("foobar", NULL);
970 }
971
796 // Test that integer to double promotion is allowed. 972 // Test that integer to double promotion is allowed.
797 bundle.SetInteger("Number", 31415); 973 bundle.SetInteger("Number", 31415);
798 TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true); 974 TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, true);
799 } 975 }
800 976
801 TEST(SchemaTest, InvalidReferences) { 977 TEST(SchemaTest, InvalidReferences) {
802 // References to undeclared schemas fail. 978 // References to undeclared schemas fail.
803 EXPECT_TRUE(ParseFails( 979 EXPECT_TRUE(ParseFails(
804 "{" 980 "{"
805 " \"type\": \"object\"," 981 " \"type\": \"object\","
806 " \"properties\": {" 982 " \"properties\": {"
807 " \"name\": { \"$ref\": \"undeclared\" }" 983 " \"name\": { \"$ref\": \"undeclared\" }"
808 " }" 984 " }"
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
1050 1226
1051 EXPECT_FALSE(ParseFails(SchemaObjectWrapper( 1227 EXPECT_FALSE(ParseFails(SchemaObjectWrapper(
1052 "{" 1228 "{"
1053 " \"type\": \"integer\"," 1229 " \"type\": \"integer\","
1054 " \"minimum\": 10," 1230 " \"minimum\": 10,"
1055 " \"maximum\": 20" 1231 " \"maximum\": 20"
1056 "}"))); 1232 "}")));
1057 } 1233 }
1058 1234
1059 } // namespace policy 1235 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698