OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "chrome/common/json_schema_validator_unittest_base.h" | |
6 | |
7 #include <cfloat> | |
8 #include <cmath> | |
9 #include <limits> | |
10 | |
11 #include "base/file_util.h" | |
12 #include "base/json/json_file_value_serializer.h" | |
13 #include "base/logging.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "base/path_service.h" | |
16 #include "base/stringprintf.h" | |
17 #include "base/values.h" | |
18 #include "chrome/common/chrome_paths.h" | |
19 #include "chrome/common/json_schema_constants.h" | |
20 #include "chrome/common/json_schema_validator.h" | |
21 | |
22 namespace schema = json_schema_constants; | |
23 | |
24 namespace { | |
25 | |
26 #define TEST_SOURCE base::StringPrintf("%s:%i", __FILE__, __LINE__) | |
27 | |
28 base::Value* LoadValue(const std::string& filename) { | |
29 base::FilePath path; | |
30 PathService::Get(chrome::DIR_TEST_DATA, &path); | |
31 path = path.AppendASCII("json_schema_validator").AppendASCII(filename); | |
32 EXPECT_TRUE(file_util::PathExists(path)); | |
33 | |
34 std::string error_message; | |
35 JSONFileValueSerializer serializer(path); | |
36 base::Value* result = serializer.Deserialize(NULL, &error_message); | |
37 if (!result) | |
38 ADD_FAILURE() << "Could not parse JSON: " << error_message; | |
39 return result; | |
40 } | |
41 | |
42 base::Value* LoadValue(const std::string& filename, base::Value::Type type) { | |
43 scoped_ptr<base::Value> result(LoadValue(filename)); | |
44 if (!result.get()) | |
45 return NULL; | |
46 if (!result->IsType(type)) { | |
47 ADD_FAILURE() << "Expected type " << type << ", got: " << result->GetType(); | |
48 return NULL; | |
49 } | |
50 return result.release(); | |
51 } | |
52 | |
53 base::ListValue* LoadList(const std::string& filename) { | |
54 return static_cast<base::ListValue*>( | |
55 LoadValue(filename, base::Value::TYPE_LIST)); | |
56 } | |
57 | |
58 base::DictionaryValue* LoadDictionary(const std::string& filename) { | |
59 return static_cast<base::DictionaryValue*>( | |
60 LoadValue(filename, base::Value::TYPE_DICTIONARY)); | |
61 } | |
62 | |
63 } // namespace | |
64 | |
65 | |
66 JSONSchemaValidatorTestBase::JSONSchemaValidatorTestBase( | |
67 JSONSchemaValidatorTestBase::ValidatorType type) | |
68 : type_(type) { | |
69 } | |
70 | |
71 void JSONSchemaValidatorTestBase::RunTests() { | |
72 TestComplex(); | |
73 TestStringPattern(); | |
74 TestEnum(); | |
75 TestChoices(); | |
76 TestExtends(); | |
77 TestObject(); | |
78 TestTypeReference(); | |
79 TestArrayTuple(); | |
80 TestArrayNonTuple(); | |
81 TestString(); | |
82 TestNumber(); | |
83 TestTypeClassifier(); | |
84 TestTypes(); | |
85 } | |
86 | |
87 void JSONSchemaValidatorTestBase::TestComplex() { | |
88 scoped_ptr<base::DictionaryValue> schema( | |
89 LoadDictionary("complex_schema.json")); | |
90 scoped_ptr<base::ListValue> instance(LoadList("complex_instance.json")); | |
91 | |
92 ASSERT_TRUE(schema.get()); | |
93 ASSERT_TRUE(instance.get()); | |
94 | |
95 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
96 instance->Remove(instance->GetSize() - 1, NULL); | |
97 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
98 instance->Append(new base::DictionaryValue()); | |
99 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1", | |
100 JSONSchemaValidator::FormatErrorMessage( | |
101 JSONSchemaValidator::kInvalidType, | |
102 schema::kNumber, | |
103 schema::kObject)); | |
104 instance->Remove(instance->GetSize() - 1, NULL); | |
105 | |
106 base::DictionaryValue* item = NULL; | |
107 ASSERT_TRUE(instance->GetDictionary(0, &item)); | |
108 item->SetString("url", "xxxxxxxxxxx"); | |
109 | |
110 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | |
111 "0.url", | |
112 JSONSchemaValidator::FormatErrorMessage( | |
113 JSONSchemaValidator::kStringMaxLength, "10")); | |
114 } | |
115 | |
116 void JSONSchemaValidatorTestBase::TestStringPattern() { | |
117 // Regex patterns not supported in CPP validator. | |
118 if (type_ == CPP) | |
119 return; | |
120 | |
121 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); | |
122 schema->SetString(schema::kType, schema::kString); | |
123 schema->SetString(schema::kPattern, "foo+"); | |
124 | |
125 ExpectValid(TEST_SOURCE, | |
126 scoped_ptr<base::Value>(new base::StringValue("foo")).get(), | |
127 schema.get(), NULL); | |
128 ExpectValid(TEST_SOURCE, | |
129 scoped_ptr<base::Value>(new base::StringValue("foooooo")).get(), | |
130 schema.get(), NULL); | |
131 ExpectNotValid(TEST_SOURCE, | |
132 scoped_ptr<base::Value>(new base::StringValue("bar")).get(), | |
133 schema.get(), NULL, "", | |
134 JSONSchemaValidator::FormatErrorMessage( | |
135 JSONSchemaValidator::kStringPattern, "foo+")); | |
136 } | |
137 | |
138 void JSONSchemaValidatorTestBase::TestEnum() { | |
139 scoped_ptr<base::DictionaryValue> schema(LoadDictionary("enum_schema.json")); | |
140 | |
141 ExpectValid(TEST_SOURCE, | |
142 scoped_ptr<base::Value>(new base::StringValue("foo")).get(), | |
143 schema.get(), NULL); | |
144 ExpectValid(TEST_SOURCE, | |
145 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get(), | |
146 schema.get(), NULL); | |
147 ExpectValid(TEST_SOURCE, | |
148 scoped_ptr<base::Value>(new base::FundamentalValue(false)).get(), | |
149 schema.get(), NULL); | |
150 | |
151 ExpectNotValid(TEST_SOURCE, | |
152 scoped_ptr<base::Value>(new base::StringValue("42")).get(), | |
153 schema.get(), NULL, "", JSONSchemaValidator::kInvalidEnum); | |
154 ExpectNotValid(TEST_SOURCE, | |
155 scoped_ptr<base::Value>(base::Value::CreateNullValue()).get(), | |
156 schema.get(), NULL, "", JSONSchemaValidator::kInvalidEnum); | |
157 } | |
158 | |
159 void JSONSchemaValidatorTestBase::TestChoices() { | |
160 scoped_ptr<base::DictionaryValue> schema( | |
161 LoadDictionary("choices_schema.json")); | |
162 | |
163 ExpectValid(TEST_SOURCE, | |
164 scoped_ptr<base::Value>(base::Value::CreateNullValue()).get(), | |
165 schema.get(), NULL); | |
166 ExpectValid(TEST_SOURCE, | |
167 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get(), | |
168 schema.get(), NULL); | |
169 | |
170 scoped_ptr<base::DictionaryValue> instance(new base::DictionaryValue()); | |
171 instance->SetString("foo", "bar"); | |
172 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
173 | |
174 ExpectNotValid(TEST_SOURCE, | |
175 scoped_ptr<base::Value>(new base::StringValue("foo")).get(), | |
176 schema.get(), NULL, "", JSONSchemaValidator::kInvalidChoice); | |
177 ExpectNotValid(TEST_SOURCE, | |
178 scoped_ptr<base::Value>(new base::ListValue()).get(), | |
179 schema.get(), NULL, "", JSONSchemaValidator::kInvalidChoice); | |
180 | |
181 instance->SetInteger("foo", 42); | |
182 ExpectNotValid(TEST_SOURCE, instance.get(), | |
183 schema.get(), NULL, "", JSONSchemaValidator::kInvalidChoice); | |
184 } | |
185 | |
186 void JSONSchemaValidatorTestBase::TestExtends() { | |
187 // TODO(aa): JS only | |
188 } | |
189 | |
190 void JSONSchemaValidatorTestBase::TestObject() { | |
191 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); | |
192 schema->SetString(schema::kType, schema::kObject); | |
193 schema->SetString("properties.foo.type", schema::kString); | |
194 schema->SetString("properties.bar.type", schema::kInteger); | |
195 | |
196 scoped_ptr<base::DictionaryValue> instance(new base::DictionaryValue()); | |
197 instance->SetString("foo", "foo"); | |
198 instance->SetInteger("bar", 42); | |
199 | |
200 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
201 | |
202 instance->SetBoolean("extra", true); | |
203 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | |
204 "extra", JSONSchemaValidator::kUnexpectedProperty); | |
205 | |
206 instance->Remove("extra", NULL); | |
207 instance->Remove("bar", NULL); | |
208 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar", | |
209 JSONSchemaValidator::kObjectPropertyIsRequired); | |
210 | |
211 instance->SetString("bar", "42"); | |
212 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar", | |
213 JSONSchemaValidator::FormatErrorMessage( | |
214 JSONSchemaValidator::kInvalidType, | |
215 schema::kInteger, | |
216 schema::kString)); | |
217 | |
218 base::DictionaryValue* additional_properties = new base::DictionaryValue(); | |
219 additional_properties->SetString(schema::kType, schema::kAny); | |
220 schema->Set(schema::kAdditionalProperties, additional_properties); | |
221 | |
222 instance->SetInteger("bar", 42); | |
223 instance->SetBoolean("extra", true); | |
224 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
225 | |
226 instance->SetString("extra", "foo"); | |
227 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
228 | |
229 additional_properties->SetString(schema::kType, schema::kBoolean); | |
230 instance->SetBoolean("extra", true); | |
231 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
232 | |
233 instance->SetString("extra", "foo"); | |
234 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | |
235 "extra", JSONSchemaValidator::FormatErrorMessage( | |
236 JSONSchemaValidator::kInvalidType, | |
237 schema::kBoolean, | |
238 schema::kString)); | |
239 | |
240 base::DictionaryValue* properties = NULL; | |
241 base::DictionaryValue* bar_property = NULL; | |
242 ASSERT_TRUE(schema->GetDictionary(schema::kProperties, &properties)); | |
243 ASSERT_TRUE(properties->GetDictionary("bar", &bar_property)); | |
244 | |
245 bar_property->SetBoolean(schema::kOptional, true); | |
246 instance->Remove("extra", NULL); | |
247 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
248 instance->Remove("bar", NULL); | |
249 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
250 instance->Set("bar", base::Value::CreateNullValue()); | |
251 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | |
252 "bar", JSONSchemaValidator::FormatErrorMessage( | |
253 JSONSchemaValidator::kInvalidType, | |
254 schema::kInteger, | |
255 schema::kNull)); | |
256 instance->SetString("bar", "42"); | |
257 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | |
258 "bar", JSONSchemaValidator::FormatErrorMessage( | |
259 JSONSchemaValidator::kInvalidType, | |
260 schema::kInteger, | |
261 schema::kString)); | |
262 } | |
263 | |
264 void JSONSchemaValidatorTestBase::TestTypeReference() { | |
265 scoped_ptr<base::ListValue> types(LoadList("reference_types.json")); | |
266 ASSERT_TRUE(types.get()); | |
267 | |
268 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); | |
269 schema->SetString(schema::kType, schema::kObject); | |
270 schema->SetString("properties.foo.type", schema::kString); | |
271 schema->SetString("properties.bar.$ref", "Max10Int"); | |
272 schema->SetString("properties.baz.$ref", "MinLengthString"); | |
273 | |
274 scoped_ptr<base::DictionaryValue> schema_inline(new base::DictionaryValue()); | |
275 schema_inline->SetString(schema::kType, schema::kObject); | |
276 schema_inline->SetString("properties.foo.type", schema::kString); | |
277 schema_inline->SetString("properties.bar.id", "NegativeInt"); | |
278 schema_inline->SetString("properties.bar.type", schema::kInteger); | |
279 schema_inline->SetInteger("properties.bar.maximum", 0); | |
280 schema_inline->SetString("properties.baz.$ref", "NegativeInt"); | |
281 | |
282 scoped_ptr<base::DictionaryValue> instance(new base::DictionaryValue()); | |
283 instance->SetString("foo", "foo"); | |
284 instance->SetInteger("bar", 4); | |
285 instance->SetString("baz", "ab"); | |
286 | |
287 scoped_ptr<base::DictionaryValue> instance_inline( | |
288 new base::DictionaryValue()); | |
289 instance_inline->SetString("foo", "foo"); | |
290 instance_inline->SetInteger("bar", -4); | |
291 instance_inline->SetInteger("baz", -2); | |
292 | |
293 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), types.get()); | |
294 ExpectValid(TEST_SOURCE, instance_inline.get(), schema_inline.get(), NULL); | |
295 | |
296 // Validation failure, but successful schema reference. | |
297 instance->SetString("baz", "a"); | |
298 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), types.get(), | |
299 "baz", JSONSchemaValidator::FormatErrorMessage( | |
300 JSONSchemaValidator::kStringMinLength, "2")); | |
301 | |
302 instance_inline->SetInteger("bar", 20); | |
303 ExpectNotValid(TEST_SOURCE, instance_inline.get(), schema_inline.get(), NULL, | |
304 "bar", JSONSchemaValidator::FormatErrorMessage( | |
305 JSONSchemaValidator::kNumberMaximum, "0")); | |
306 | |
307 // Remove MinLengthString type. | |
308 types->Remove(types->GetSize() - 1, NULL); | |
309 instance->SetString("baz", "ab"); | |
310 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), types.get(), | |
311 "bar", JSONSchemaValidator::FormatErrorMessage( | |
312 JSONSchemaValidator::kUnknownTypeReference, | |
313 "Max10Int")); | |
314 | |
315 // Remove internal type "NegativeInt". | |
316 schema_inline->Remove("properties.bar", NULL); | |
317 instance_inline->Remove("bar", NULL); | |
318 ExpectNotValid(TEST_SOURCE, instance_inline.get(), schema_inline.get(), NULL, | |
319 "baz", JSONSchemaValidator::FormatErrorMessage( | |
320 JSONSchemaValidator::kUnknownTypeReference, | |
321 "NegativeInt")); | |
322 } | |
323 | |
324 void JSONSchemaValidatorTestBase::TestArrayTuple() { | |
325 scoped_ptr<base::DictionaryValue> schema( | |
326 LoadDictionary("array_tuple_schema.json")); | |
327 ASSERT_TRUE(schema.get()); | |
328 | |
329 scoped_ptr<base::ListValue> instance(new base::ListValue()); | |
330 instance->Append(new base::StringValue("42")); | |
331 instance->Append(new base::FundamentalValue(42)); | |
332 | |
333 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
334 | |
335 instance->Append(new base::StringValue("anything")); | |
336 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "", | |
337 JSONSchemaValidator::FormatErrorMessage( | |
338 JSONSchemaValidator::kArrayMaxItems, "2")); | |
339 | |
340 instance->Remove(1, NULL); | |
341 instance->Remove(1, NULL); | |
342 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1", | |
343 JSONSchemaValidator::kArrayItemRequired); | |
344 | |
345 instance->Set(0, new base::FundamentalValue(42)); | |
346 instance->Append(new base::FundamentalValue(42)); | |
347 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "0", | |
348 JSONSchemaValidator::FormatErrorMessage( | |
349 JSONSchemaValidator::kInvalidType, | |
350 schema::kString, | |
351 schema::kInteger)); | |
352 | |
353 base::DictionaryValue* additional_properties = new base::DictionaryValue(); | |
354 additional_properties->SetString(schema::kType, schema::kAny); | |
355 schema->Set(schema::kAdditionalProperties, additional_properties); | |
356 instance->Set(0, new base::StringValue("42")); | |
357 instance->Append(new base::StringValue("anything")); | |
358 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
359 instance->Set(2, new base::ListValue()); | |
360 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
361 | |
362 additional_properties->SetString(schema::kType, schema::kBoolean); | |
363 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "2", | |
364 JSONSchemaValidator::FormatErrorMessage( | |
365 JSONSchemaValidator::kInvalidType, | |
366 schema::kBoolean, | |
367 schema::kArray)); | |
368 instance->Set(2, new base::FundamentalValue(false)); | |
369 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
370 | |
371 base::ListValue* items_schema = NULL; | |
372 base::DictionaryValue* item0_schema = NULL; | |
373 ASSERT_TRUE(schema->GetList(schema::kItems, &items_schema)); | |
374 ASSERT_TRUE(items_schema->GetDictionary(0, &item0_schema)); | |
375 item0_schema->SetBoolean(schema::kOptional, true); | |
376 instance->Remove(2, NULL); | |
377 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
378 // TODO(aa): I think this is inconsistent with the handling of NULL+optional | |
379 // for objects. | |
380 instance->Set(0, base::Value::CreateNullValue()); | |
381 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
382 instance->Set(0, new base::FundamentalValue(42)); | |
383 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "0", | |
384 JSONSchemaValidator::FormatErrorMessage( | |
385 JSONSchemaValidator::kInvalidType, | |
386 schema::kString, | |
387 schema::kInteger)); | |
388 } | |
389 | |
390 void JSONSchemaValidatorTestBase::TestArrayNonTuple() { | |
391 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); | |
392 schema->SetString(schema::kType, schema::kArray); | |
393 schema->SetString("items.type", schema::kString); | |
394 schema->SetInteger(schema::kMinItems, 2); | |
395 schema->SetInteger(schema::kMaxItems, 3); | |
396 | |
397 scoped_ptr<base::ListValue> instance(new base::ListValue()); | |
398 instance->Append(new base::StringValue("x")); | |
399 instance->Append(new base::StringValue("x")); | |
400 | |
401 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
402 instance->Append(new base::StringValue("x")); | |
403 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
404 | |
405 instance->Append(new base::StringValue("x")); | |
406 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "", | |
407 JSONSchemaValidator::FormatErrorMessage( | |
408 JSONSchemaValidator::kArrayMaxItems, "3")); | |
409 instance->Remove(1, NULL); | |
410 instance->Remove(1, NULL); | |
411 instance->Remove(1, NULL); | |
412 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "", | |
413 JSONSchemaValidator::FormatErrorMessage( | |
414 JSONSchemaValidator::kArrayMinItems, "2")); | |
415 | |
416 instance->Remove(1, NULL); | |
417 instance->Append(new base::FundamentalValue(42)); | |
418 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1", | |
419 JSONSchemaValidator::FormatErrorMessage( | |
420 JSONSchemaValidator::kInvalidType, | |
421 schema::kString, | |
422 schema::kInteger)); | |
423 } | |
424 | |
425 void JSONSchemaValidatorTestBase::TestString() { | |
426 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); | |
427 schema->SetString(schema::kType, schema::kString); | |
428 schema->SetInteger(schema::kMinLength, 1); | |
429 schema->SetInteger(schema::kMaxLength, 10); | |
430 | |
431 ExpectValid(TEST_SOURCE, | |
432 scoped_ptr<base::Value>(new base::StringValue("x")).get(), | |
433 schema.get(), NULL); | |
434 ExpectValid(TEST_SOURCE, | |
435 scoped_ptr<base::Value>( | |
436 new base::StringValue("xxxxxxxxxx")).get(), | |
437 schema.get(), NULL); | |
438 | |
439 ExpectNotValid(TEST_SOURCE, | |
440 scoped_ptr<base::Value>(new base::StringValue("")).get(), | |
441 schema.get(), NULL, "", | |
442 JSONSchemaValidator::FormatErrorMessage( | |
443 JSONSchemaValidator::kStringMinLength, "1")); | |
444 ExpectNotValid( | |
445 TEST_SOURCE, | |
446 scoped_ptr<base::Value>(new base::StringValue("xxxxxxxxxxx")).get(), | |
447 schema.get(), NULL, "", | |
448 JSONSchemaValidator::FormatErrorMessage( | |
449 JSONSchemaValidator::kStringMaxLength, "10")); | |
450 } | |
451 | |
452 void JSONSchemaValidatorTestBase::TestNumber() { | |
453 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); | |
454 schema->SetString(schema::kType, schema::kNumber); | |
455 schema->SetInteger(schema::kMinimum, 1); | |
456 schema->SetInteger(schema::kMaximum, 100); | |
457 schema->SetInteger("maxDecimal", 2); | |
458 | |
459 ExpectValid(TEST_SOURCE, | |
460 scoped_ptr<base::Value>(new base::FundamentalValue(1)).get(), | |
461 schema.get(), NULL); | |
462 ExpectValid(TEST_SOURCE, | |
463 scoped_ptr<base::Value>(new base::FundamentalValue(50)).get(), | |
464 schema.get(), NULL); | |
465 ExpectValid(TEST_SOURCE, | |
466 scoped_ptr<base::Value>(new base::FundamentalValue(100)).get(), | |
467 schema.get(), NULL); | |
468 ExpectValid(TEST_SOURCE, | |
469 scoped_ptr<base::Value>(new base::FundamentalValue(88.88)).get(), | |
470 schema.get(), NULL); | |
471 | |
472 ExpectNotValid( | |
473 TEST_SOURCE, | |
474 scoped_ptr<base::Value>(new base::FundamentalValue(0.5)).get(), | |
475 schema.get(), NULL, "", | |
476 JSONSchemaValidator::FormatErrorMessage( | |
477 JSONSchemaValidator::kNumberMinimum, "1")); | |
478 ExpectNotValid( | |
479 TEST_SOURCE, | |
480 scoped_ptr<base::Value>(new base::FundamentalValue(100.1)).get(), | |
481 schema.get(), NULL, "", | |
482 JSONSchemaValidator::FormatErrorMessage( | |
483 JSONSchemaValidator::kNumberMaximum, "100")); | |
484 } | |
485 | |
486 void JSONSchemaValidatorTestBase::TestTypeClassifier() { | |
487 EXPECT_EQ(std::string(schema::kBoolean), | |
488 JSONSchemaValidator::GetJSONSchemaType( | |
489 scoped_ptr<base::Value>( | |
490 new base::FundamentalValue(true)).get())); | |
491 EXPECT_EQ(std::string(schema::kBoolean), | |
492 JSONSchemaValidator::GetJSONSchemaType( | |
493 scoped_ptr<base::Value>( | |
494 new base::FundamentalValue(false)).get())); | |
495 | |
496 // It doesn't matter whether the C++ type is 'integer' or 'real'. If the | |
497 // number is integral and within the representable range of integers in | |
498 // double, it's classified as 'integer'. | |
499 EXPECT_EQ(std::string(schema::kInteger), | |
500 JSONSchemaValidator::GetJSONSchemaType( | |
501 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get())); | |
502 EXPECT_EQ(std::string(schema::kInteger), | |
503 JSONSchemaValidator::GetJSONSchemaType( | |
504 scoped_ptr<base::Value>(new base::FundamentalValue(0)).get())); | |
505 EXPECT_EQ(std::string(schema::kInteger), | |
506 JSONSchemaValidator::GetJSONSchemaType( | |
507 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get())); | |
508 EXPECT_EQ(std::string(schema::kInteger), | |
509 JSONSchemaValidator::GetJSONSchemaType(scoped_ptr<base::Value>( | |
510 new base::FundamentalValue(pow(2.0, DBL_MANT_DIG))).get())); | |
511 EXPECT_EQ(std::string(schema::kInteger), | |
512 JSONSchemaValidator::GetJSONSchemaType(scoped_ptr<base::Value>( | |
513 new base::FundamentalValue(pow(-2.0, DBL_MANT_DIG))).get())); | |
514 | |
515 // "number" is only used for non-integral numbers, or numbers beyond what | |
516 // double can accurately represent. | |
517 EXPECT_EQ(std::string(schema::kNumber), | |
518 JSONSchemaValidator::GetJSONSchemaType( | |
519 scoped_ptr<base::Value>( | |
520 new base::FundamentalValue(88.8)).get())); | |
521 EXPECT_EQ(std::string(schema::kNumber), | |
522 JSONSchemaValidator::GetJSONSchemaType(scoped_ptr<base::Value>( | |
523 new base::FundamentalValue(pow(2.0, DBL_MANT_DIG) * 2)).get())); | |
524 EXPECT_EQ(std::string(schema::kNumber), | |
525 JSONSchemaValidator::GetJSONSchemaType(scoped_ptr<base::Value>( | |
526 new base::FundamentalValue( | |
527 pow(-2.0, DBL_MANT_DIG) * 2)).get())); | |
528 | |
529 EXPECT_EQ(std::string(schema::kString), | |
530 JSONSchemaValidator::GetJSONSchemaType( | |
531 scoped_ptr<base::Value>(new base::StringValue("foo")).get())); | |
532 EXPECT_EQ(std::string(schema::kArray), | |
533 JSONSchemaValidator::GetJSONSchemaType( | |
534 scoped_ptr<base::Value>(new base::ListValue()).get())); | |
535 EXPECT_EQ(std::string(schema::kObject), | |
536 JSONSchemaValidator::GetJSONSchemaType( | |
537 scoped_ptr<base::Value>(new base::DictionaryValue()).get())); | |
538 EXPECT_EQ(std::string(schema::kNull), | |
539 JSONSchemaValidator::GetJSONSchemaType( | |
540 scoped_ptr<base::Value>(base::Value::CreateNullValue()).get())); | |
541 } | |
542 | |
543 void JSONSchemaValidatorTestBase::TestTypes() { | |
544 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); | |
545 | |
546 // valid | |
547 schema->SetString(schema::kType, schema::kObject); | |
548 ExpectValid(TEST_SOURCE, | |
549 scoped_ptr<base::Value>(new base::DictionaryValue()).get(), | |
550 schema.get(), NULL); | |
551 | |
552 schema->SetString(schema::kType, schema::kArray); | |
553 ExpectValid(TEST_SOURCE, scoped_ptr<base::Value>(new base::ListValue()).get(), | |
554 schema.get(), NULL); | |
555 | |
556 schema->SetString(schema::kType, schema::kString); | |
557 ExpectValid(TEST_SOURCE, | |
558 scoped_ptr<base::Value>(new base::StringValue("foobar")).get(), | |
559 schema.get(), NULL); | |
560 | |
561 schema->SetString(schema::kType, schema::kNumber); | |
562 ExpectValid(TEST_SOURCE, | |
563 scoped_ptr<base::Value>(new base::FundamentalValue(88.8)).get(), | |
564 schema.get(), NULL); | |
565 ExpectValid(TEST_SOURCE, | |
566 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get(), | |
567 schema.get(), NULL); | |
568 ExpectValid(TEST_SOURCE, | |
569 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get(), | |
570 schema.get(), NULL); | |
571 ExpectValid(TEST_SOURCE, | |
572 scoped_ptr<base::Value>(new base::FundamentalValue(0)).get(), | |
573 schema.get(), NULL); | |
574 | |
575 schema->SetString(schema::kType, schema::kInteger); | |
576 ExpectValid(TEST_SOURCE, | |
577 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get(), | |
578 schema.get(), NULL); | |
579 ExpectValid(TEST_SOURCE, | |
580 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get(), | |
581 schema.get(), NULL); | |
582 ExpectValid(TEST_SOURCE, | |
583 scoped_ptr<base::Value>(new base::FundamentalValue(0)).get(), | |
584 schema.get(), NULL); | |
585 ExpectValid(TEST_SOURCE, | |
586 scoped_ptr<base::Value>( | |
587 new base::FundamentalValue(pow(2.0, DBL_MANT_DIG))).get(), | |
588 schema.get(), NULL); | |
589 ExpectValid(TEST_SOURCE, | |
590 scoped_ptr<base::Value>( | |
591 new base::FundamentalValue(pow(-2.0, DBL_MANT_DIG))).get(), | |
592 schema.get(), NULL); | |
593 | |
594 schema->SetString(schema::kType, schema::kBoolean); | |
595 ExpectValid(TEST_SOURCE, | |
596 scoped_ptr<base::Value>(new base::FundamentalValue(false)).get(), | |
597 schema.get(), NULL); | |
598 ExpectValid(TEST_SOURCE, | |
599 scoped_ptr<base::Value>(new base::FundamentalValue(true)).get(), | |
600 schema.get(), NULL); | |
601 | |
602 schema->SetString(schema::kType, schema::kNull); | |
603 ExpectValid(TEST_SOURCE, | |
604 scoped_ptr<base::Value>(base::Value::CreateNullValue()).get(), | |
605 schema.get(), NULL); | |
606 | |
607 // not valid | |
608 schema->SetString(schema::kType, schema::kObject); | |
609 ExpectNotValid(TEST_SOURCE, | |
610 scoped_ptr<base::Value>(new base::ListValue()).get(), | |
611 schema.get(), NULL, "", | |
612 JSONSchemaValidator::FormatErrorMessage( | |
613 JSONSchemaValidator::kInvalidType, | |
614 schema::kObject, | |
615 schema::kArray)); | |
616 | |
617 schema->SetString(schema::kType, schema::kObject); | |
618 ExpectNotValid(TEST_SOURCE, | |
619 scoped_ptr<base::Value>(base::Value::CreateNullValue()).get(), | |
620 schema.get(), NULL, "", | |
621 JSONSchemaValidator::FormatErrorMessage( | |
622 JSONSchemaValidator::kInvalidType, | |
623 schema::kObject, | |
624 schema::kNull)); | |
625 | |
626 schema->SetString(schema::kType, schema::kArray); | |
627 ExpectNotValid(TEST_SOURCE, | |
628 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get(), | |
629 schema.get(), NULL, "", | |
630 JSONSchemaValidator::FormatErrorMessage( | |
631 JSONSchemaValidator::kInvalidType, | |
632 schema::kArray, | |
633 schema::kInteger)); | |
634 | |
635 schema->SetString(schema::kType, schema::kString); | |
636 ExpectNotValid(TEST_SOURCE, | |
637 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get(), | |
638 schema.get(), NULL, "", | |
639 JSONSchemaValidator::FormatErrorMessage( | |
640 JSONSchemaValidator::kInvalidType, | |
641 schema::kString, | |
642 schema::kInteger)); | |
643 | |
644 schema->SetString(schema::kType, schema::kNumber); | |
645 ExpectNotValid(TEST_SOURCE, | |
646 scoped_ptr<base::Value>(new base::StringValue("42")).get(), | |
647 schema.get(), NULL, "", | |
648 JSONSchemaValidator::FormatErrorMessage( | |
649 JSONSchemaValidator::kInvalidType, | |
650 schema::kNumber, | |
651 schema::kString)); | |
652 | |
653 schema->SetString(schema::kType, schema::kInteger); | |
654 ExpectNotValid(TEST_SOURCE, | |
655 scoped_ptr<base::Value>( | |
656 new base::FundamentalValue(88.8)).get(), | |
657 schema.get(), NULL, "", | |
658 JSONSchemaValidator::kInvalidTypeIntegerNumber); | |
659 | |
660 schema->SetString(schema::kType, schema::kBoolean); | |
661 ExpectNotValid(TEST_SOURCE, | |
662 scoped_ptr<base::Value>(new base::FundamentalValue(1)).get(), | |
663 schema.get(), NULL, "", | |
664 JSONSchemaValidator::FormatErrorMessage( | |
665 JSONSchemaValidator::kInvalidType, | |
666 schema::kBoolean, | |
667 schema::kInteger)); | |
668 | |
669 schema->SetString(schema::kType, schema::kNull); | |
670 ExpectNotValid(TEST_SOURCE, | |
671 scoped_ptr<base::Value>( | |
672 new base::FundamentalValue(false)).get(), | |
673 schema.get(), NULL, "", | |
674 JSONSchemaValidator::FormatErrorMessage( | |
675 JSONSchemaValidator::kInvalidType, | |
676 schema::kNull, | |
677 schema::kBoolean)); | |
678 } | |
OLD | NEW |