OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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. | |
4 | |
5 #include "base/values.h" | |
6 #include "chrome/common/json_schema/json_schema_validator.h" | |
7 #include "chrome/common/json_schema/json_schema_validator_unittest_base.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 | |
10 class JSONSchemaValidatorCPPTest : public JSONSchemaValidatorTestBase { | |
11 public: | |
12 JSONSchemaValidatorCPPTest() | |
13 : JSONSchemaValidatorTestBase(JSONSchemaValidatorTestBase::CPP) { | |
14 } | |
15 | |
16 protected: | |
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)) | |
23 return; | |
24 | |
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; | |
29 } | |
30 } | |
31 | |
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; | |
41 return; | |
42 } | |
43 | |
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) | |
47 << test_source; | |
48 } | |
49 }; | |
50 | |
51 TEST_F(JSONSchemaValidatorCPPTest, Test) { | |
52 RunTests(); | |
53 } | |
54 | |
55 TEST(JSONSchemaValidator, IsValidSchema) { | |
56 std::string error; | |
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( | |
68 "{" | |
69 " \"type\": \"object\"," | |
70 " \"properties\": []" // Invalid properties type. | |
71 "}", &error)); | |
72 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema( | |
73 "{" | |
74 " \"type\": \"string\"," | |
75 " \"maxLength\": -1" // Must be >= 0. | |
76 "}", &error)); | |
77 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema( | |
78 "{" | |
79 " \"type\": \"string\"," | |
80 " \"enum\": [ {} ]," // "enum" must contain simple values. | |
81 "}", &error)); | |
82 EXPECT_FALSE(JSONSchemaValidator::IsValidSchema( | |
83 "{" | |
84 " \"type\": \"array\"," | |
85 " \"items\": [ 123 ]," // "items" must contain a schema or schemas. | |
86 "}", &error)); | |
87 EXPECT_TRUE(JSONSchemaValidator::IsValidSchema( | |
88 "{ \"type\": \"object\" }", &error)) << error; | |
89 EXPECT_TRUE(JSONSchemaValidator::IsValidSchema( | |
90 "{ \"type\": [\"object\", \"array\"] }", &error)) << error; | |
91 EXPECT_TRUE(JSONSchemaValidator::IsValidSchema( | |
92 "{" | |
93 " \"type\": [\"object\", \"array\"]," | |
94 " \"properties\": {" | |
95 " \"string-property\": {" | |
96 " \"type\": \"string\"," | |
97 " \"minLength\": 1," | |
98 " \"maxLength\": 100," | |
99 " \"title\": \"The String Policy\"," | |
100 " \"description\": \"This policy controls the String widget.\"" | |
101 " }," | |
102 " \"integer-property\": {" | |
103 " \"type\": \"number\"," | |
104 " \"minimum\": 1000.0," | |
105 " \"maximum\": 9999.0" | |
106 " }," | |
107 " \"enum-property\": {" | |
108 " \"type\": \"integer\"," | |
109 " \"enum\": [0, 1, 10, 100]" | |
110 " }," | |
111 " \"items-property\": {" | |
112 " \"type\": \"array\"," | |
113 " \"items\": {" | |
114 " \"type\": \"string\"" | |
115 " }" | |
116 " }," | |
117 " \"items-list-property\": {" | |
118 " \"type\": \"array\"," | |
119 " \"items\": [" | |
120 " { \"type\": \"string\" }," | |
121 " { \"type\": \"integer\" }" | |
122 " ]" | |
123 " }" | |
124 " }," | |
125 " \"additionalProperties\": {" | |
126 " \"type\": \"any\"" | |
127 " }" | |
128 "}", &error)) << error; | |
129 } | |
OLD | NEW |