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/json_schema/json_schema_validator_unittest_base.h" | 5 #include "components/json_schema/json_schema_validator_unittest_base.h" |
6 | 6 |
7 #include <cfloat> | 7 #include <cfloat> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <limits> | 9 #include <limits> |
10 #include <memory> | 10 #include <memory> |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 JSONSchemaValidator::FormatErrorMessage( | 116 JSONSchemaValidator::FormatErrorMessage( |
117 JSONSchemaValidator::kStringMaxLength, "10")); | 117 JSONSchemaValidator::kStringMaxLength, "10")); |
118 } | 118 } |
119 | 119 |
120 void JSONSchemaValidatorTestBase::TestStringPattern() { | 120 void JSONSchemaValidatorTestBase::TestStringPattern() { |
121 std::unique_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); | 121 std::unique_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); |
122 schema->SetString(schema::kType, schema::kString); | 122 schema->SetString(schema::kType, schema::kString); |
123 schema->SetString(schema::kPattern, "foo+"); | 123 schema->SetString(schema::kPattern, "foo+"); |
124 | 124 |
125 ExpectValid(TEST_SOURCE, | 125 ExpectValid(TEST_SOURCE, |
126 std::unique_ptr<base::Value>(new base::StringValue("foo")).get(), | 126 std::unique_ptr<base::Value>(new base::Value("foo")).get(), |
127 schema.get(), NULL); | 127 schema.get(), NULL); |
128 ExpectValid( | 128 ExpectValid(TEST_SOURCE, |
129 TEST_SOURCE, | 129 std::unique_ptr<base::Value>(new base::Value("foooooo")).get(), |
130 std::unique_ptr<base::Value>(new base::StringValue("foooooo")).get(), | 130 schema.get(), NULL); |
131 schema.get(), NULL); | 131 ExpectNotValid(TEST_SOURCE, |
132 ExpectNotValid( | 132 std::unique_ptr<base::Value>(new base::Value("bar")).get(), |
133 TEST_SOURCE, | 133 schema.get(), NULL, std::string(), |
134 std::unique_ptr<base::Value>(new base::StringValue("bar")).get(), | 134 JSONSchemaValidator::FormatErrorMessage( |
135 schema.get(), NULL, std::string(), | 135 JSONSchemaValidator::kStringPattern, "foo+")); |
136 JSONSchemaValidator::FormatErrorMessage( | |
137 JSONSchemaValidator::kStringPattern, "foo+")); | |
138 } | 136 } |
139 | 137 |
140 void JSONSchemaValidatorTestBase::TestEnum() { | 138 void JSONSchemaValidatorTestBase::TestEnum() { |
141 std::unique_ptr<base::DictionaryValue> schema( | 139 std::unique_ptr<base::DictionaryValue> schema( |
142 LoadDictionary("enum_schema.json")); | 140 LoadDictionary("enum_schema.json")); |
143 | 141 |
144 ExpectValid(TEST_SOURCE, | 142 ExpectValid(TEST_SOURCE, |
145 std::unique_ptr<base::Value>(new base::StringValue("foo")).get(), | 143 std::unique_ptr<base::Value>(new base::Value("foo")).get(), |
146 schema.get(), NULL); | 144 schema.get(), NULL); |
147 ExpectValid(TEST_SOURCE, | 145 ExpectValid(TEST_SOURCE, |
148 std::unique_ptr<base::Value>(new base::Value(42)).get(), | 146 std::unique_ptr<base::Value>(new base::Value(42)).get(), |
149 schema.get(), NULL); | 147 schema.get(), NULL); |
150 ExpectValid(TEST_SOURCE, | 148 ExpectValid(TEST_SOURCE, |
151 std::unique_ptr<base::Value>(new base::Value(false)).get(), | 149 std::unique_ptr<base::Value>(new base::Value(false)).get(), |
152 schema.get(), NULL); | 150 schema.get(), NULL); |
153 | 151 |
154 ExpectNotValid( | 152 ExpectNotValid( |
155 TEST_SOURCE, | 153 TEST_SOURCE, std::unique_ptr<base::Value>(new base::Value("42")).get(), |
156 std::unique_ptr<base::Value>(new base::StringValue("42")).get(), | |
157 schema.get(), NULL, std::string(), JSONSchemaValidator::kInvalidEnum); | 154 schema.get(), NULL, std::string(), JSONSchemaValidator::kInvalidEnum); |
158 ExpectNotValid(TEST_SOURCE, base::Value::CreateNullValue().get(), | 155 ExpectNotValid(TEST_SOURCE, base::Value::CreateNullValue().get(), |
159 schema.get(), NULL, std::string(), | 156 schema.get(), NULL, std::string(), |
160 JSONSchemaValidator::kInvalidEnum); | 157 JSONSchemaValidator::kInvalidEnum); |
161 } | 158 } |
162 | 159 |
163 void JSONSchemaValidatorTestBase::TestChoices() { | 160 void JSONSchemaValidatorTestBase::TestChoices() { |
164 std::unique_ptr<base::DictionaryValue> schema( | 161 std::unique_ptr<base::DictionaryValue> schema( |
165 LoadDictionary("choices_schema.json")); | 162 LoadDictionary("choices_schema.json")); |
166 | 163 |
167 ExpectValid(TEST_SOURCE, base::Value::CreateNullValue().get(), schema.get(), | 164 ExpectValid(TEST_SOURCE, base::Value::CreateNullValue().get(), schema.get(), |
168 NULL); | 165 NULL); |
169 ExpectValid(TEST_SOURCE, | 166 ExpectValid(TEST_SOURCE, |
170 std::unique_ptr<base::Value>(new base::Value(42)).get(), | 167 std::unique_ptr<base::Value>(new base::Value(42)).get(), |
171 schema.get(), NULL); | 168 schema.get(), NULL); |
172 | 169 |
173 std::unique_ptr<base::DictionaryValue> instance(new base::DictionaryValue()); | 170 std::unique_ptr<base::DictionaryValue> instance(new base::DictionaryValue()); |
174 instance->SetString("foo", "bar"); | 171 instance->SetString("foo", "bar"); |
175 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 172 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
176 | 173 |
177 ExpectNotValid( | 174 ExpectNotValid( |
178 TEST_SOURCE, | 175 TEST_SOURCE, std::unique_ptr<base::Value>(new base::Value("foo")).get(), |
179 std::unique_ptr<base::Value>(new base::StringValue("foo")).get(), | |
180 schema.get(), NULL, std::string(), JSONSchemaValidator::kInvalidChoice); | 176 schema.get(), NULL, std::string(), JSONSchemaValidator::kInvalidChoice); |
181 ExpectNotValid( | 177 ExpectNotValid( |
182 TEST_SOURCE, std::unique_ptr<base::Value>(new base::ListValue()).get(), | 178 TEST_SOURCE, std::unique_ptr<base::Value>(new base::ListValue()).get(), |
183 schema.get(), NULL, std::string(), JSONSchemaValidator::kInvalidChoice); | 179 schema.get(), NULL, std::string(), JSONSchemaValidator::kInvalidChoice); |
184 | 180 |
185 instance->SetInteger("foo", 42); | 181 instance->SetInteger("foo", 42); |
186 ExpectNotValid(TEST_SOURCE, | 182 ExpectNotValid(TEST_SOURCE, |
187 instance.get(), | 183 instance.get(), |
188 schema.get(), | 184 schema.get(), |
189 NULL, | 185 NULL, |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 instance->AppendInteger(42); | 402 instance->AppendInteger(42); |
407 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "0", | 403 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "0", |
408 JSONSchemaValidator::FormatErrorMessage( | 404 JSONSchemaValidator::FormatErrorMessage( |
409 JSONSchemaValidator::kInvalidType, | 405 JSONSchemaValidator::kInvalidType, |
410 schema::kString, | 406 schema::kString, |
411 schema::kInteger)); | 407 schema::kInteger)); |
412 | 408 |
413 base::DictionaryValue* additional_properties = new base::DictionaryValue(); | 409 base::DictionaryValue* additional_properties = new base::DictionaryValue(); |
414 additional_properties->SetString(schema::kType, schema::kAny); | 410 additional_properties->SetString(schema::kType, schema::kAny); |
415 schema->Set(schema::kAdditionalProperties, additional_properties); | 411 schema->Set(schema::kAdditionalProperties, additional_properties); |
416 instance->Set(0, new base::StringValue("42")); | 412 instance->Set(0, new base::Value("42")); |
417 instance->AppendString("anything"); | 413 instance->AppendString("anything"); |
418 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 414 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
419 instance->Set(2, new base::ListValue()); | 415 instance->Set(2, new base::ListValue()); |
420 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 416 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
421 | 417 |
422 additional_properties->SetString(schema::kType, schema::kBoolean); | 418 additional_properties->SetString(schema::kType, schema::kBoolean); |
423 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "2", | 419 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "2", |
424 JSONSchemaValidator::FormatErrorMessage( | 420 JSONSchemaValidator::FormatErrorMessage( |
425 JSONSchemaValidator::kInvalidType, | 421 JSONSchemaValidator::kInvalidType, |
426 schema::kBoolean, | 422 schema::kBoolean, |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 schema::kInteger)); | 486 schema::kInteger)); |
491 } | 487 } |
492 | 488 |
493 void JSONSchemaValidatorTestBase::TestString() { | 489 void JSONSchemaValidatorTestBase::TestString() { |
494 std::unique_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); | 490 std::unique_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); |
495 schema->SetString(schema::kType, schema::kString); | 491 schema->SetString(schema::kType, schema::kString); |
496 schema->SetInteger(schema::kMinLength, 1); | 492 schema->SetInteger(schema::kMinLength, 1); |
497 schema->SetInteger(schema::kMaxLength, 10); | 493 schema->SetInteger(schema::kMaxLength, 10); |
498 | 494 |
499 ExpectValid(TEST_SOURCE, | 495 ExpectValid(TEST_SOURCE, |
500 std::unique_ptr<base::Value>(new base::StringValue("x")).get(), | 496 std::unique_ptr<base::Value>(new base::Value("x")).get(), |
501 schema.get(), NULL); | 497 schema.get(), NULL); |
502 ExpectValid( | 498 ExpectValid(TEST_SOURCE, |
503 TEST_SOURCE, | 499 std::unique_ptr<base::Value>(new base::Value("xxxxxxxxxx")).get(), |
504 std::unique_ptr<base::Value>(new base::StringValue("xxxxxxxxxx")).get(), | 500 schema.get(), NULL); |
505 schema.get(), NULL); | |
506 | 501 |
507 ExpectNotValid( | 502 ExpectNotValid( |
508 TEST_SOURCE, | 503 TEST_SOURCE, |
509 std::unique_ptr<base::Value>(new base::StringValue(std::string())).get(), | 504 std::unique_ptr<base::Value>(new base::Value(std::string())).get(), |
510 schema.get(), NULL, std::string(), | 505 schema.get(), NULL, std::string(), |
511 JSONSchemaValidator::FormatErrorMessage( | 506 JSONSchemaValidator::FormatErrorMessage( |
512 JSONSchemaValidator::kStringMinLength, "1")); | 507 JSONSchemaValidator::kStringMinLength, "1")); |
513 ExpectNotValid( | 508 ExpectNotValid( |
514 TEST_SOURCE, | 509 TEST_SOURCE, |
515 std::unique_ptr<base::Value>(new base::StringValue("xxxxxxxxxxx")).get(), | 510 std::unique_ptr<base::Value>(new base::Value("xxxxxxxxxxx")).get(), |
516 schema.get(), NULL, std::string(), | 511 schema.get(), NULL, std::string(), |
517 JSONSchemaValidator::FormatErrorMessage( | 512 JSONSchemaValidator::FormatErrorMessage( |
518 JSONSchemaValidator::kStringMaxLength, "10")); | 513 JSONSchemaValidator::kStringMaxLength, "10")); |
519 } | 514 } |
520 | 515 |
521 void JSONSchemaValidatorTestBase::TestNumber() { | 516 void JSONSchemaValidatorTestBase::TestNumber() { |
522 std::unique_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); | 517 std::unique_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); |
523 schema->SetString(schema::kType, schema::kNumber); | 518 schema->SetString(schema::kType, schema::kNumber); |
524 schema->SetInteger(schema::kMinimum, 1); | 519 schema->SetInteger(schema::kMinimum, 1); |
525 schema->SetInteger(schema::kMaximum, 100); | 520 schema->SetInteger(schema::kMaximum, 100); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 JSONSchemaValidator::GetJSONSchemaType( | 585 JSONSchemaValidator::GetJSONSchemaType( |
591 std::unique_ptr<base::Value>( | 586 std::unique_ptr<base::Value>( |
592 new base::Value(pow(2.0, DBL_MANT_DIG) * 2)) | 587 new base::Value(pow(2.0, DBL_MANT_DIG) * 2)) |
593 .get())); | 588 .get())); |
594 EXPECT_EQ(std::string(schema::kNumber), | 589 EXPECT_EQ(std::string(schema::kNumber), |
595 JSONSchemaValidator::GetJSONSchemaType( | 590 JSONSchemaValidator::GetJSONSchemaType( |
596 std::unique_ptr<base::Value>( | 591 std::unique_ptr<base::Value>( |
597 new base::Value(pow(-2.0, DBL_MANT_DIG) * 2)) | 592 new base::Value(pow(-2.0, DBL_MANT_DIG) * 2)) |
598 .get())); | 593 .get())); |
599 | 594 |
600 EXPECT_EQ( | 595 EXPECT_EQ(std::string(schema::kString), |
601 std::string(schema::kString), | 596 JSONSchemaValidator::GetJSONSchemaType( |
602 JSONSchemaValidator::GetJSONSchemaType( | 597 std::unique_ptr<base::Value>(new base::Value("foo")).get())); |
603 std::unique_ptr<base::Value>(new base::StringValue("foo")).get())); | |
604 EXPECT_EQ(std::string(schema::kArray), | 598 EXPECT_EQ(std::string(schema::kArray), |
605 JSONSchemaValidator::GetJSONSchemaType( | 599 JSONSchemaValidator::GetJSONSchemaType( |
606 std::unique_ptr<base::Value>(new base::ListValue()).get())); | 600 std::unique_ptr<base::Value>(new base::ListValue()).get())); |
607 EXPECT_EQ( | 601 EXPECT_EQ( |
608 std::string(schema::kObject), | 602 std::string(schema::kObject), |
609 JSONSchemaValidator::GetJSONSchemaType( | 603 JSONSchemaValidator::GetJSONSchemaType( |
610 std::unique_ptr<base::Value>(new base::DictionaryValue()).get())); | 604 std::unique_ptr<base::Value>(new base::DictionaryValue()).get())); |
611 EXPECT_EQ(std::string(schema::kNull), | 605 EXPECT_EQ(std::string(schema::kNull), |
612 JSONSchemaValidator::GetJSONSchemaType( | 606 JSONSchemaValidator::GetJSONSchemaType( |
613 base::Value::CreateNullValue().get())); | 607 base::Value::CreateNullValue().get())); |
614 } | 608 } |
615 | 609 |
616 void JSONSchemaValidatorTestBase::TestTypes() { | 610 void JSONSchemaValidatorTestBase::TestTypes() { |
617 std::unique_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); | 611 std::unique_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); |
618 | 612 |
619 // valid | 613 // valid |
620 schema->SetString(schema::kType, schema::kObject); | 614 schema->SetString(schema::kType, schema::kObject); |
621 ExpectValid(TEST_SOURCE, | 615 ExpectValid(TEST_SOURCE, |
622 std::unique_ptr<base::Value>(new base::DictionaryValue()).get(), | 616 std::unique_ptr<base::Value>(new base::DictionaryValue()).get(), |
623 schema.get(), NULL); | 617 schema.get(), NULL); |
624 | 618 |
625 schema->SetString(schema::kType, schema::kArray); | 619 schema->SetString(schema::kType, schema::kArray); |
626 ExpectValid(TEST_SOURCE, | 620 ExpectValid(TEST_SOURCE, |
627 std::unique_ptr<base::Value>(new base::ListValue()).get(), | 621 std::unique_ptr<base::Value>(new base::ListValue()).get(), |
628 schema.get(), NULL); | 622 schema.get(), NULL); |
629 | 623 |
630 schema->SetString(schema::kType, schema::kString); | 624 schema->SetString(schema::kType, schema::kString); |
631 ExpectValid( | 625 ExpectValid(TEST_SOURCE, |
632 TEST_SOURCE, | 626 std::unique_ptr<base::Value>(new base::Value("foobar")).get(), |
633 std::unique_ptr<base::Value>(new base::StringValue("foobar")).get(), | 627 schema.get(), NULL); |
634 schema.get(), NULL); | |
635 | 628 |
636 schema->SetString(schema::kType, schema::kNumber); | 629 schema->SetString(schema::kType, schema::kNumber); |
637 ExpectValid(TEST_SOURCE, | 630 ExpectValid(TEST_SOURCE, |
638 std::unique_ptr<base::Value>(new base::Value(88.8)).get(), | 631 std::unique_ptr<base::Value>(new base::Value(88.8)).get(), |
639 schema.get(), NULL); | 632 schema.get(), NULL); |
640 ExpectValid(TEST_SOURCE, | 633 ExpectValid(TEST_SOURCE, |
641 std::unique_ptr<base::Value>(new base::Value(42)).get(), | 634 std::unique_ptr<base::Value>(new base::Value(42)).get(), |
642 schema.get(), NULL); | 635 schema.get(), NULL); |
643 ExpectValid(TEST_SOURCE, | 636 ExpectValid(TEST_SOURCE, |
644 std::unique_ptr<base::Value>(new base::Value(42)).get(), | 637 std::unique_ptr<base::Value>(new base::Value(42)).get(), |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
705 schema->SetString(schema::kType, schema::kString); | 698 schema->SetString(schema::kType, schema::kString); |
706 ExpectNotValid(TEST_SOURCE, | 699 ExpectNotValid(TEST_SOURCE, |
707 std::unique_ptr<base::Value>(new base::Value(42)).get(), | 700 std::unique_ptr<base::Value>(new base::Value(42)).get(), |
708 schema.get(), NULL, std::string(), | 701 schema.get(), NULL, std::string(), |
709 JSONSchemaValidator::FormatErrorMessage( | 702 JSONSchemaValidator::FormatErrorMessage( |
710 JSONSchemaValidator::kInvalidType, schema::kString, | 703 JSONSchemaValidator::kInvalidType, schema::kString, |
711 schema::kInteger)); | 704 schema::kInteger)); |
712 | 705 |
713 schema->SetString(schema::kType, schema::kNumber); | 706 schema->SetString(schema::kType, schema::kNumber); |
714 ExpectNotValid( | 707 ExpectNotValid( |
715 TEST_SOURCE, | 708 TEST_SOURCE, std::unique_ptr<base::Value>(new base::Value("42")).get(), |
716 std::unique_ptr<base::Value>(new base::StringValue("42")).get(), | |
717 schema.get(), NULL, std::string(), | 709 schema.get(), NULL, std::string(), |
718 JSONSchemaValidator::FormatErrorMessage( | 710 JSONSchemaValidator::FormatErrorMessage( |
719 JSONSchemaValidator::kInvalidType, schema::kNumber, schema::kString)); | 711 JSONSchemaValidator::kInvalidType, schema::kNumber, schema::kString)); |
720 | 712 |
721 schema->SetString(schema::kType, schema::kInteger); | 713 schema->SetString(schema::kType, schema::kInteger); |
722 ExpectNotValid(TEST_SOURCE, | 714 ExpectNotValid(TEST_SOURCE, |
723 std::unique_ptr<base::Value>(new base::Value(88.8)).get(), | 715 std::unique_ptr<base::Value>(new base::Value(88.8)).get(), |
724 schema.get(), NULL, std::string(), | 716 schema.get(), NULL, std::string(), |
725 JSONSchemaValidator::kInvalidTypeIntegerNumber); | 717 JSONSchemaValidator::kInvalidTypeIntegerNumber); |
726 | 718 |
727 schema->SetString(schema::kType, schema::kBoolean); | 719 schema->SetString(schema::kType, schema::kBoolean); |
728 ExpectNotValid(TEST_SOURCE, | 720 ExpectNotValid(TEST_SOURCE, |
729 std::unique_ptr<base::Value>(new base::Value(1)).get(), | 721 std::unique_ptr<base::Value>(new base::Value(1)).get(), |
730 schema.get(), NULL, std::string(), | 722 schema.get(), NULL, std::string(), |
731 JSONSchemaValidator::FormatErrorMessage( | 723 JSONSchemaValidator::FormatErrorMessage( |
732 JSONSchemaValidator::kInvalidType, schema::kBoolean, | 724 JSONSchemaValidator::kInvalidType, schema::kBoolean, |
733 schema::kInteger)); | 725 schema::kInteger)); |
734 | 726 |
735 schema->SetString(schema::kType, schema::kNull); | 727 schema->SetString(schema::kType, schema::kNull); |
736 ExpectNotValid( | 728 ExpectNotValid( |
737 TEST_SOURCE, std::unique_ptr<base::Value>(new base::Value(false)).get(), | 729 TEST_SOURCE, std::unique_ptr<base::Value>(new base::Value(false)).get(), |
738 schema.get(), NULL, std::string(), | 730 schema.get(), NULL, std::string(), |
739 JSONSchemaValidator::FormatErrorMessage(JSONSchemaValidator::kInvalidType, | 731 JSONSchemaValidator::FormatErrorMessage(JSONSchemaValidator::kInvalidType, |
740 schema::kNull, schema::kBoolean)); | 732 schema::kNull, schema::kBoolean)); |
741 } | 733 } |
OLD | NEW |