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