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
3 // found in the LICENSE file.
5 #include "base/values.h"
6 #include "components/json_schema/json_schema_validator.h"
7 #include "components/json_schema/json_schema_validator_unittest_base.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 class JSONSchemaValidatorCPPTest
: public JSONSchemaValidatorTestBase
{
12 JSONSchemaValidatorCPPTest()
13 : JSONSchemaValidatorTestBase(JSONSchemaValidatorTestBase::CPP
) {
17 virtual void ExpectValid(const std::string
& test_source
,
18 base::Value
* instance
,
19 base::DictionaryValue
* schema
,
20 base::ListValue
* types
) OVERRIDE
{
21 JSONSchemaValidator
validator(schema
, types
);
22 if (validator
.Validate(instance
))
25 for (size_t i
= 0; i
< validator
.errors().size(); ++i
) {
26 ADD_FAILURE() << test_source
<< ": "
27 << validator
.errors()[i
].path
<< ": "
28 << validator
.errors()[i
].message
;
32 virtual void ExpectNotValid(
33 const std::string
& test_source
,
34 base::Value
* instance
, base::DictionaryValue
* schema
,
35 base::ListValue
* types
,
36 const std::string
& expected_error_path
,
37 const std::string
& expected_error_message
) OVERRIDE
{
38 JSONSchemaValidator
validator(schema
, types
);
39 if (validator
.Validate(instance
)) {
40 ADD_FAILURE() << test_source
;
44 ASSERT_EQ(1u, validator
.errors().size()) << test_source
;
45 EXPECT_EQ(expected_error_path
, validator
.errors()[0].path
) << test_source
;
46 EXPECT_EQ(expected_error_message
, validator
.errors()[0].message
)
51 TEST_F(JSONSchemaValidatorCPPTest
, Test
) {
55 TEST(JSONSchemaValidator
, IsValidSchema
) {
57 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema("", &error
));
58 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema("\0", &error
));
59 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema("string", &error
));
60 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema("\"string\"", &error
));
61 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema("[]", &error
));
62 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema("{}", &error
));
63 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema(
64 "{ \"type\": 123 }", &error
));
65 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema(
66 "{ \"type\": \"invalid\" }", &error
));
67 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema(
69 " \"type\": \"object\","
70 " \"properties\": []" // Invalid properties type.
72 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema(
74 " \"type\": \"string\","
75 " \"maxLength\": -1" // Must be >= 0.
77 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema(
79 " \"type\": \"string\","
80 " \"enum\": [ {} ]" // "enum" dict values must contain "name".
82 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema(
84 " \"type\": \"string\","
85 " \"enum\": [ { \"name\": {} } ]" // "enum" name must be a simple value.
87 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema(
89 " \"type\": \"array\","
90 " \"items\": [ 123 ]," // "items" must contain a schema or schemas.
92 EXPECT_TRUE(JSONSchemaValidator::IsValidSchema(
93 "{ \"type\": \"object\" }", &error
)) << error
;
94 EXPECT_TRUE(JSONSchemaValidator::IsValidSchema(
95 "{ \"type\": [\"object\", \"array\"] }", &error
)) << error
;
96 EXPECT_TRUE(JSONSchemaValidator::IsValidSchema(
98 " \"type\": [\"object\", \"array\"],"
100 " \"string-property\": {"
101 " \"type\": \"string\","
103 " \"maxLength\": 100,"
104 " \"title\": \"The String Policy\","
105 " \"description\": \"This policy controls the String widget.\""
107 " \"integer-property\": {"
108 " \"type\": \"number\","
109 " \"minimum\": 1000.0,"
110 " \"maximum\": 9999.0"
112 " \"enum-property\": {"
113 " \"type\": \"integer\","
114 " \"enum\": [0, 1, {\"name\": 10}, 100]"
116 " \"items-property\": {"
117 " \"type\": \"array\","
119 " \"type\": \"string\""
122 " \"items-list-property\": {"
123 " \"type\": \"array\","
125 " { \"type\": \"string\" },"
126 " { \"type\": \"integer\" }"
130 " \"additionalProperties\": {"
133 "}", &error
)) << error
;
134 EXPECT_TRUE(JSONSchemaValidator::IsValidSchema(
136 " \"type\": \"object\","
137 " \"unknown attribute\": \"that should just be ignored\""
139 JSONSchemaValidator::OPTIONS_IGNORE_UNKNOWN_ATTRIBUTES
,
141 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema(
143 " \"type\": \"object\","
144 " \"unknown attribute\": \"that will cause a failure\""
145 "}", 0, &error
)) << error
;