Debian Codegraph @

unittest/gunit/xplugin/xpl/meta_schema_validator_t.cc

raw symbols SCIP
1 /*
2 * Copyright (c) 2020, 2026, Oracle and/or its affiliates.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2.0,
6 * as published by the Free Software Foundation.
7 *
8 * This program is designed to work with certain software (including
9 * but not limited to OpenSSL) that is licensed under separate terms,
10 * as designated in a particular file or component or in included license
11 * documentation. The authors of MySQL hereby grant you an additional
12 * permission to link the program and your derivative works with the
13 * separately licensed software that they have either included with
14 * the program or referenced in the documentation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License, version 2.0, for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include <gmock/gmock.h>
27 #include <gtest/gtest.h>
28
29 #include "plugin/x/src/meta_schema_validator.h"
30 #include "unittest/gunit/xplugin/xpl/assert_error_code.h"
31
32 namespace xpl {
33 namespace test {
34
35 using ::testing::HasSubstr;
36
37 class Meta_schema_validator_test : public ::testing::Test {
38 public:
39 Meta_schema_validator validator{5};
40 };
41
42 TEST_F(Meta_schema_validator_test, empty_schema) {
43 const auto error = validator.validate("");
44 ASSERT_ERROR_CODE(ER_X_INVALID_VALIDATION_SCHEMA, error);
45 ASSERT_THAT(error.message, HasSubstr("is not a valid JSON document"));
46 }
47
48 TEST_F(Meta_schema_validator_test, empty_array_schema) {
49 const auto error = validator.validate("[]");
50 ASSERT_ERROR_CODE(ER_X_INVALID_VALIDATION_SCHEMA, error);
51 ASSERT_THAT(error.message, HasSubstr("is not a valid JSON object"));
52 }
53
54 TEST_F(Meta_schema_validator_test, empty_object_schema) {
55 const auto error = validator.validate("{}");
56 ASSERT_ERROR_CODE(ER_X_SUCCESS, error);
57 }
58
59 TEST_F(Meta_schema_validator_test, simple_schema) {
60 const auto error = validator.validate(R"({
61 "type": "object"
62 })");
63 ASSERT_ERROR_CODE(ER_X_SUCCESS, error);
64 }
65
66 TEST_F(Meta_schema_validator_test, typo_schema) {
67 const auto error = validator.validate(R"({
68 "typeE": "object"
69 })");
70 ASSERT_ERROR_CODE(ER_X_INVALID_VALIDATION_SCHEMA, error);
71 ASSERT_THAT(error.message,
72 HasSubstr("#/typeE failed requirement: 'additionalProperties'"));
73 }
74
75 TEST_F(Meta_schema_validator_test, valid_reference_schema) {
76 const auto error = validator.validate(R"({
77 "definitions": {
78 "veggie": {
79 "type": "object",
80 "properties": {
81 "name": { "type": "string" },
82 "like": { "type": "boolean" }
83 }
84 }
85 },
86 "properties": {
87 "vegetables": {
88 "type": "array",
89 "items": { "$ref": "#/definitions/veggie" }
90 }
91 }
92 })");
93 ASSERT_ERROR_CODE(ER_X_SUCCESS, error);
94 }
95
96 TEST_F(Meta_schema_validator_test, not_valid_reference_schema) {
97 const auto error = validator.validate(R"({
98 "definitions": {
99 "veggie": {
100 "type": "object",
101 "properties": {
102 "name": { "type": "string" },
103 "like": { "type": "boolean" }
104 }
105 }
106 },
107 "properties": {
108 "vegetables": {
109 "type": "array",
110 "items": { "$ref": "#/definitions/veggieE" }
111 }
112 }
113 })");
114 ASSERT_ERROR_CODE(ER_X_INVALID_VALIDATION_SCHEMA, error);
115 ASSERT_THAT(
116 error.message,
117 HasSubstr("reference '#/properties/vegetables/items' is not valid"));
118 }
119
120 TEST_F(Meta_schema_validator_test, reference_to_reference_schema) {
121 const auto error = validator.validate(R"({
122 "definitions": {
123 "veggie": {
124 "type": "object",
125 "properties": {
126 "name": { "type": "string" },
127 "like": { "type": "boolean" }
128 }
129 },
130 "veggie_array": {
131 "type": "array",
132 "items": { "$ref": "#/definitions/veggie" }
133 }
134 },
135 "properties": {
136 "vegetables": { "$ref": "#/definitions/veggie_array" }
137 }
138 })");
139 ASSERT_ERROR_CODE(ER_X_SUCCESS, error);
140 }
141
142 TEST_F(Meta_schema_validator_test, self_reference_schema) {
143 const auto error = validator.validate(R"({
144 "definitions": {
145 "veggie": {
146 "type": "object",
147 "properties": {
148 "veggie": { "$ref": "#/definitions/veggie" }
149 }
150 }
151 },
152 "properties": {
153 "vegetables": { "$ref": "#/definitions/veggie" }
154 }
155 })");
156 ASSERT_ERROR_CODE(ER_X_SUCCESS, error);
157 }
158
159 TEST_F(Meta_schema_validator_test, quick_self_reference_schema) {
160 const auto error = validator.validate(R"({
161 "properties": {
162 "vegetables": { "$ref": "#" }
163 }
164 })");
165 ASSERT_ERROR_CODE(ER_X_SUCCESS, error);
166 }
167
168 TEST_F(Meta_schema_validator_test, malformed_reference_schema) {
169 const auto error = validator.validate(R"({
170 "properties": {
171 "vegetables": { "$ref": "#bad" }
172 }
173 })");
174 ASSERT_ERROR_CODE(ER_X_INVALID_VALIDATION_SCHEMA, error);
175 ASSERT_THAT(error.message,
176 HasSubstr("reference '#/properties/vegetables' is not valid"));
177 }
178
179 TEST_F(Meta_schema_validator_test, circular_reference_schema) {
180 const auto error = validator.validate(R"({
181 "definitions": {
182 "veggie": {
183 "type": "object",
184 "properties": {
185 "fruit": { "$ref": "#/definitions/fruttie" }
186 }
187 },
188 "fruttie": {
189 "type": "object",
190 "properties": {
191 "vegetable": { "$ref": "#/definitions/veggie" }
192 }
193 }
194 },
195 "properties": {
196 "fruit": { "$ref": "#/definitions/fruttie" }
197 }
198 })");
199 ASSERT_ERROR_CODE(ER_X_SUCCESS, error);
200 }
201
202 TEST_F(Meta_schema_validator_test, too_deep_schema) {
203 const auto error = validator.validate(R"({
204 "properties": {
205 "veggie": {
206 "type": "object",
207 "properties": {
208 "name": {
209 "type": "object",
210 "properties": {
211 "native": { "type": "string" },
212 "translation": { "type": "string" }
213 }
214 }
215 }
216 }
217 }
218 })");
219 ASSERT_ERROR_CODE(ER_X_INVALID_VALIDATION_SCHEMA, error);
220 ASSERT_THAT(
221 error.message,
222 HasSubstr("exceeds the maximum depth on"
223 " #/properties/veggie/properties/name/properties/native"));
224 }
225
226 TEST_F(Meta_schema_validator_test, meta_schema) {
227 const auto error =
228 validator.validate(Meta_schema_validator::k_reference_schema);
229 ASSERT_ERROR_CODE(ER_X_SUCCESS, error);
230 }
231
232 } // namespace test
233 } // namespace xpl