cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / json_schema / json_schema_validator_unittest_base.cc
blob38f111c66264156a4583523337eef8971e7180c3
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 "components/json_schema/json_schema_validator_unittest_base.h"
7 #include <cfloat>
8 #include <cmath>
9 #include <limits>
11 #include "base/base_paths.h"
12 #include "base/files/file_util.h"
13 #include "base/json/json_file_value_serializer.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/path_service.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/values.h"
19 #include "components/json_schema/json_schema_constants.h"
20 #include "components/json_schema/json_schema_validator.h"
22 namespace schema = json_schema_constants;
24 namespace {
26 #define TEST_SOURCE base::StringPrintf("%s:%i", __FILE__, __LINE__)
28 base::Value* LoadValue(const std::string& filename) {
29 base::FilePath path;
30 PathService::Get(base::DIR_SOURCE_ROOT, &path);
31 path = path.AppendASCII("components")
32 .AppendASCII("test")
33 .AppendASCII("data")
34 .AppendASCII("json_schema")
35 .AppendASCII(filename);
36 EXPECT_TRUE(base::PathExists(path));
38 std::string error_message;
39 JSONFileValueDeserializer deserializer(path);
40 base::Value* result = deserializer.Deserialize(NULL, &error_message);
41 if (!result)
42 ADD_FAILURE() << "Could not parse JSON: " << error_message;
43 return result;
46 base::Value* LoadValue(const std::string& filename, base::Value::Type type) {
47 scoped_ptr<base::Value> result(LoadValue(filename));
48 if (!result.get())
49 return NULL;
50 if (!result->IsType(type)) {
51 ADD_FAILURE() << "Expected type " << type << ", got: " << result->GetType();
52 return NULL;
54 return result.release();
57 base::ListValue* LoadList(const std::string& filename) {
58 return static_cast<base::ListValue*>(
59 LoadValue(filename, base::Value::TYPE_LIST));
62 base::DictionaryValue* LoadDictionary(const std::string& filename) {
63 return static_cast<base::DictionaryValue*>(
64 LoadValue(filename, base::Value::TYPE_DICTIONARY));
67 } // namespace
70 JSONSchemaValidatorTestBase::JSONSchemaValidatorTestBase() {
73 void JSONSchemaValidatorTestBase::RunTests() {
74 TestComplex();
75 TestStringPattern();
76 TestEnum();
77 TestChoices();
78 TestExtends();
79 TestObject();
80 TestTypeReference();
81 TestArrayTuple();
82 TestArrayNonTuple();
83 TestString();
84 TestNumber();
85 TestTypeClassifier();
86 TestTypes();
89 void JSONSchemaValidatorTestBase::TestComplex() {
90 scoped_ptr<base::DictionaryValue> schema(
91 LoadDictionary("complex_schema.json"));
92 scoped_ptr<base::ListValue> instance(LoadList("complex_instance.json"));
94 ASSERT_TRUE(schema.get());
95 ASSERT_TRUE(instance.get());
97 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
98 instance->Remove(instance->GetSize() - 1, NULL);
99 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
100 instance->Append(new base::DictionaryValue());
101 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1",
102 JSONSchemaValidator::FormatErrorMessage(
103 JSONSchemaValidator::kInvalidType,
104 schema::kNumber,
105 schema::kObject));
106 instance->Remove(instance->GetSize() - 1, NULL);
108 base::DictionaryValue* item = NULL;
109 ASSERT_TRUE(instance->GetDictionary(0, &item));
110 item->SetString("url", "xxxxxxxxxxx");
112 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
113 "0.url",
114 JSONSchemaValidator::FormatErrorMessage(
115 JSONSchemaValidator::kStringMaxLength, "10"));
118 void JSONSchemaValidatorTestBase::TestStringPattern() {
119 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue());
120 schema->SetString(schema::kType, schema::kString);
121 schema->SetString(schema::kPattern, "foo+");
123 ExpectValid(TEST_SOURCE,
124 scoped_ptr<base::Value>(new base::StringValue("foo")).get(),
125 schema.get(), NULL);
126 ExpectValid(TEST_SOURCE,
127 scoped_ptr<base::Value>(new base::StringValue("foooooo")).get(),
128 schema.get(), NULL);
129 ExpectNotValid(TEST_SOURCE,
130 scoped_ptr<base::Value>(new base::StringValue("bar")).get(),
131 schema.get(),
132 NULL,
133 std::string(),
134 JSONSchemaValidator::FormatErrorMessage(
135 JSONSchemaValidator::kStringPattern, "foo+"));
138 void JSONSchemaValidatorTestBase::TestEnum() {
139 scoped_ptr<base::DictionaryValue> schema(LoadDictionary("enum_schema.json"));
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);
151 ExpectNotValid(TEST_SOURCE,
152 scoped_ptr<base::Value>(new base::StringValue("42")).get(),
153 schema.get(),
154 NULL,
155 std::string(),
156 JSONSchemaValidator::kInvalidEnum);
157 ExpectNotValid(TEST_SOURCE, base::Value::CreateNullValue().get(),
158 schema.get(), NULL, std::string(),
159 JSONSchemaValidator::kInvalidEnum);
162 void JSONSchemaValidatorTestBase::TestChoices() {
163 scoped_ptr<base::DictionaryValue> schema(
164 LoadDictionary("choices_schema.json"));
166 ExpectValid(TEST_SOURCE, base::Value::CreateNullValue().get(), schema.get(),
167 NULL);
168 ExpectValid(TEST_SOURCE,
169 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get(),
170 schema.get(), NULL);
172 scoped_ptr<base::DictionaryValue> instance(new base::DictionaryValue());
173 instance->SetString("foo", "bar");
174 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
176 ExpectNotValid(TEST_SOURCE,
177 scoped_ptr<base::Value>(new base::StringValue("foo")).get(),
178 schema.get(),
179 NULL,
180 std::string(),
181 JSONSchemaValidator::kInvalidChoice);
182 ExpectNotValid(TEST_SOURCE,
183 scoped_ptr<base::Value>(new base::ListValue()).get(),
184 schema.get(),
185 NULL,
186 std::string(),
187 JSONSchemaValidator::kInvalidChoice);
189 instance->SetInteger("foo", 42);
190 ExpectNotValid(TEST_SOURCE,
191 instance.get(),
192 schema.get(),
193 NULL,
194 std::string(),
195 JSONSchemaValidator::kInvalidChoice);
198 void JSONSchemaValidatorTestBase::TestExtends() {
199 // TODO(aa): JS only
202 void JSONSchemaValidatorTestBase::TestObject() {
203 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue());
204 schema->SetString(schema::kType, schema::kObject);
205 schema->SetString("properties.foo.type", schema::kString);
206 schema->SetString("properties.bar.type", schema::kInteger);
208 scoped_ptr<base::DictionaryValue> instance(new base::DictionaryValue());
209 instance->SetString("foo", "foo");
210 instance->SetInteger("bar", 42);
212 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
214 instance->SetBoolean("extra", true);
215 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
216 "extra", JSONSchemaValidator::kUnexpectedProperty);
217 instance->Remove("extra", NULL);
219 instance->Remove("bar", NULL);
220 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar",
221 JSONSchemaValidator::kObjectPropertyIsRequired);
223 instance->SetString("bar", "42");
224 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar",
225 JSONSchemaValidator::FormatErrorMessage(
226 JSONSchemaValidator::kInvalidType,
227 schema::kInteger,
228 schema::kString));
229 instance->SetInteger("bar", 42);
231 // Test "patternProperties".
232 instance->SetInteger("extra", 42);
233 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
234 "extra", JSONSchemaValidator::kUnexpectedProperty);
235 schema->SetString("patternProperties.extra+.type",
236 schema::kInteger);
237 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
238 instance->Remove("extra", NULL);
239 instance->SetInteger("extraaa", 42);
240 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
241 instance->Remove("extraaa", NULL);
242 instance->SetInteger("extr", 42);
243 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
244 "extr", JSONSchemaValidator::kUnexpectedProperty);
245 instance->Remove("extr", NULL);
246 schema->Remove(schema::kPatternProperties, NULL);
248 // Test "patternProperties" and "properties" schemas are both checked if
249 // applicable.
250 schema->SetString("patternProperties.fo+.type", schema::kInteger);
251 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "foo",
252 JSONSchemaValidator::FormatErrorMessage(
253 JSONSchemaValidator::kInvalidType,
254 schema::kInteger,
255 schema::kString));
256 instance->SetInteger("foo", 123);
257 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "foo",
258 JSONSchemaValidator::FormatErrorMessage(
259 JSONSchemaValidator::kInvalidType,
260 schema::kString,
261 schema::kInteger));
262 instance->SetString("foo", "foo");
263 schema->Remove(schema::kPatternProperties, NULL);
265 // Test additional properties.
266 base::DictionaryValue* additional_properties = new base::DictionaryValue();
267 additional_properties->SetString(schema::kType, schema::kAny);
268 schema->Set(schema::kAdditionalProperties, additional_properties);
270 instance->SetBoolean("extra", true);
271 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
273 instance->SetString("extra", "foo");
274 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
276 additional_properties->SetString(schema::kType, schema::kBoolean);
277 instance->SetBoolean("extra", true);
278 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
280 instance->SetString("extra", "foo");
281 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
282 "extra", JSONSchemaValidator::FormatErrorMessage(
283 JSONSchemaValidator::kInvalidType,
284 schema::kBoolean,
285 schema::kString));
286 instance->Remove("extra", NULL);
288 base::DictionaryValue* properties = NULL;
289 base::DictionaryValue* bar_property = NULL;
290 ASSERT_TRUE(schema->GetDictionary(schema::kProperties, &properties));
291 ASSERT_TRUE(properties->GetDictionary("bar", &bar_property));
293 bar_property->SetBoolean(schema::kOptional, true);
294 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
295 instance->Remove("bar", NULL);
296 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
297 instance->Set("bar", base::Value::CreateNullValue());
298 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
299 "bar", JSONSchemaValidator::FormatErrorMessage(
300 JSONSchemaValidator::kInvalidType,
301 schema::kInteger,
302 schema::kNull));
303 instance->SetString("bar", "42");
304 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
305 "bar", JSONSchemaValidator::FormatErrorMessage(
306 JSONSchemaValidator::kInvalidType,
307 schema::kInteger,
308 schema::kString));
310 // Verify that JSON parser handles dot in "patternProperties" well.
311 schema.reset(LoadDictionary("pattern_properties_dot.json"));
312 ASSERT_TRUE(schema->GetDictionary(schema::kPatternProperties, &properties));
313 ASSERT_TRUE(properties->HasKey("^.$"));
315 instance.reset(new base::DictionaryValue());
316 instance->SetString("a", "whatever");
317 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
318 instance->SetString("foo", "bar");
319 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
320 "foo", JSONSchemaValidator::kUnexpectedProperty);
323 void JSONSchemaValidatorTestBase::TestTypeReference() {
324 scoped_ptr<base::ListValue> types(LoadList("reference_types.json"));
325 ASSERT_TRUE(types.get());
327 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue());
328 schema->SetString(schema::kType, schema::kObject);
329 schema->SetString("properties.foo.type", schema::kString);
330 schema->SetString("properties.bar.$ref", "Max10Int");
331 schema->SetString("properties.baz.$ref", "MinLengthString");
333 scoped_ptr<base::DictionaryValue> schema_inline(new base::DictionaryValue());
334 schema_inline->SetString(schema::kType, schema::kObject);
335 schema_inline->SetString("properties.foo.type", schema::kString);
336 schema_inline->SetString("properties.bar.id", "NegativeInt");
337 schema_inline->SetString("properties.bar.type", schema::kInteger);
338 schema_inline->SetInteger("properties.bar.maximum", 0);
339 schema_inline->SetString("properties.baz.$ref", "NegativeInt");
341 scoped_ptr<base::DictionaryValue> instance(new base::DictionaryValue());
342 instance->SetString("foo", "foo");
343 instance->SetInteger("bar", 4);
344 instance->SetString("baz", "ab");
346 scoped_ptr<base::DictionaryValue> instance_inline(
347 new base::DictionaryValue());
348 instance_inline->SetString("foo", "foo");
349 instance_inline->SetInteger("bar", -4);
350 instance_inline->SetInteger("baz", -2);
352 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), types.get());
353 ExpectValid(TEST_SOURCE, instance_inline.get(), schema_inline.get(), NULL);
355 // Validation failure, but successful schema reference.
356 instance->SetString("baz", "a");
357 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), types.get(),
358 "baz", JSONSchemaValidator::FormatErrorMessage(
359 JSONSchemaValidator::kStringMinLength, "2"));
361 instance_inline->SetInteger("bar", 20);
362 ExpectNotValid(TEST_SOURCE, instance_inline.get(), schema_inline.get(), NULL,
363 "bar", JSONSchemaValidator::FormatErrorMessage(
364 JSONSchemaValidator::kNumberMaximum, "0"));
366 // Remove MinLengthString type.
367 types->Remove(types->GetSize() - 1, NULL);
368 instance->SetString("baz", "ab");
369 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), types.get(),
370 "bar", JSONSchemaValidator::FormatErrorMessage(
371 JSONSchemaValidator::kUnknownTypeReference,
372 "Max10Int"));
374 // Remove internal type "NegativeInt".
375 schema_inline->Remove("properties.bar", NULL);
376 instance_inline->Remove("bar", NULL);
377 ExpectNotValid(TEST_SOURCE, instance_inline.get(), schema_inline.get(), NULL,
378 "baz", JSONSchemaValidator::FormatErrorMessage(
379 JSONSchemaValidator::kUnknownTypeReference,
380 "NegativeInt"));
383 void JSONSchemaValidatorTestBase::TestArrayTuple() {
384 scoped_ptr<base::DictionaryValue> schema(
385 LoadDictionary("array_tuple_schema.json"));
386 ASSERT_TRUE(schema.get());
388 scoped_ptr<base::ListValue> instance(new base::ListValue());
389 instance->Append(new base::StringValue("42"));
390 instance->Append(new base::FundamentalValue(42));
392 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
394 instance->Append(new base::StringValue("anything"));
395 ExpectNotValid(TEST_SOURCE,
396 instance.get(),
397 schema.get(),
398 NULL,
399 std::string(),
400 JSONSchemaValidator::FormatErrorMessage(
401 JSONSchemaValidator::kArrayMaxItems, "2"));
403 instance->Remove(1, NULL);
404 instance->Remove(1, NULL);
405 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1",
406 JSONSchemaValidator::kArrayItemRequired);
408 instance->Set(0, new base::FundamentalValue(42));
409 instance->Append(new base::FundamentalValue(42));
410 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "0",
411 JSONSchemaValidator::FormatErrorMessage(
412 JSONSchemaValidator::kInvalidType,
413 schema::kString,
414 schema::kInteger));
416 base::DictionaryValue* additional_properties = new base::DictionaryValue();
417 additional_properties->SetString(schema::kType, schema::kAny);
418 schema->Set(schema::kAdditionalProperties, additional_properties);
419 instance->Set(0, new base::StringValue("42"));
420 instance->Append(new base::StringValue("anything"));
421 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
422 instance->Set(2, new base::ListValue());
423 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
425 additional_properties->SetString(schema::kType, schema::kBoolean);
426 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "2",
427 JSONSchemaValidator::FormatErrorMessage(
428 JSONSchemaValidator::kInvalidType,
429 schema::kBoolean,
430 schema::kArray));
431 instance->Set(2, new base::FundamentalValue(false));
432 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
434 base::ListValue* items_schema = NULL;
435 base::DictionaryValue* item0_schema = NULL;
436 ASSERT_TRUE(schema->GetList(schema::kItems, &items_schema));
437 ASSERT_TRUE(items_schema->GetDictionary(0, &item0_schema));
438 item0_schema->SetBoolean(schema::kOptional, true);
439 instance->Remove(2, NULL);
440 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
441 // TODO(aa): I think this is inconsistent with the handling of NULL+optional
442 // for objects.
443 instance->Set(0, base::Value::CreateNullValue());
444 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
445 instance->Set(0, new base::FundamentalValue(42));
446 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "0",
447 JSONSchemaValidator::FormatErrorMessage(
448 JSONSchemaValidator::kInvalidType,
449 schema::kString,
450 schema::kInteger));
453 void JSONSchemaValidatorTestBase::TestArrayNonTuple() {
454 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue());
455 schema->SetString(schema::kType, schema::kArray);
456 schema->SetString("items.type", schema::kString);
457 schema->SetInteger(schema::kMinItems, 2);
458 schema->SetInteger(schema::kMaxItems, 3);
460 scoped_ptr<base::ListValue> instance(new base::ListValue());
461 instance->Append(new base::StringValue("x"));
462 instance->Append(new base::StringValue("x"));
464 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
465 instance->Append(new base::StringValue("x"));
466 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
468 instance->Append(new base::StringValue("x"));
469 ExpectNotValid(TEST_SOURCE,
470 instance.get(),
471 schema.get(),
472 NULL,
473 std::string(),
474 JSONSchemaValidator::FormatErrorMessage(
475 JSONSchemaValidator::kArrayMaxItems, "3"));
476 instance->Remove(1, NULL);
477 instance->Remove(1, NULL);
478 instance->Remove(1, NULL);
479 ExpectNotValid(TEST_SOURCE,
480 instance.get(),
481 schema.get(),
482 NULL,
483 std::string(),
484 JSONSchemaValidator::FormatErrorMessage(
485 JSONSchemaValidator::kArrayMinItems, "2"));
487 instance->Remove(1, NULL);
488 instance->Append(new base::FundamentalValue(42));
489 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1",
490 JSONSchemaValidator::FormatErrorMessage(
491 JSONSchemaValidator::kInvalidType,
492 schema::kString,
493 schema::kInteger));
496 void JSONSchemaValidatorTestBase::TestString() {
497 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue());
498 schema->SetString(schema::kType, schema::kString);
499 schema->SetInteger(schema::kMinLength, 1);
500 schema->SetInteger(schema::kMaxLength, 10);
502 ExpectValid(TEST_SOURCE,
503 scoped_ptr<base::Value>(new base::StringValue("x")).get(),
504 schema.get(), NULL);
505 ExpectValid(TEST_SOURCE,
506 scoped_ptr<base::Value>(
507 new base::StringValue("xxxxxxxxxx")).get(),
508 schema.get(), NULL);
510 ExpectNotValid(
511 TEST_SOURCE,
512 scoped_ptr<base::Value>(new base::StringValue(std::string())).get(),
513 schema.get(),
514 NULL,
515 std::string(),
516 JSONSchemaValidator::FormatErrorMessage(
517 JSONSchemaValidator::kStringMinLength, "1"));
518 ExpectNotValid(
519 TEST_SOURCE,
520 scoped_ptr<base::Value>(new base::StringValue("xxxxxxxxxxx")).get(),
521 schema.get(),
522 NULL,
523 std::string(),
524 JSONSchemaValidator::FormatErrorMessage(
525 JSONSchemaValidator::kStringMaxLength, "10"));
528 void JSONSchemaValidatorTestBase::TestNumber() {
529 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue());
530 schema->SetString(schema::kType, schema::kNumber);
531 schema->SetInteger(schema::kMinimum, 1);
532 schema->SetInteger(schema::kMaximum, 100);
533 schema->SetInteger("maxDecimal", 2);
535 ExpectValid(TEST_SOURCE,
536 scoped_ptr<base::Value>(new base::FundamentalValue(1)).get(),
537 schema.get(), NULL);
538 ExpectValid(TEST_SOURCE,
539 scoped_ptr<base::Value>(new base::FundamentalValue(50)).get(),
540 schema.get(), NULL);
541 ExpectValid(TEST_SOURCE,
542 scoped_ptr<base::Value>(new base::FundamentalValue(100)).get(),
543 schema.get(), NULL);
544 ExpectValid(TEST_SOURCE,
545 scoped_ptr<base::Value>(new base::FundamentalValue(88.88)).get(),
546 schema.get(), NULL);
548 ExpectNotValid(TEST_SOURCE,
549 scoped_ptr<base::Value>(new base::FundamentalValue(0.5)).get(),
550 schema.get(),
551 NULL,
552 std::string(),
553 JSONSchemaValidator::FormatErrorMessage(
554 JSONSchemaValidator::kNumberMinimum, "1"));
555 ExpectNotValid(
556 TEST_SOURCE,
557 scoped_ptr<base::Value>(new base::FundamentalValue(100.1)).get(),
558 schema.get(),
559 NULL,
560 std::string(),
561 JSONSchemaValidator::FormatErrorMessage(
562 JSONSchemaValidator::kNumberMaximum, "100"));
565 void JSONSchemaValidatorTestBase::TestTypeClassifier() {
566 EXPECT_EQ(std::string(schema::kBoolean),
567 JSONSchemaValidator::GetJSONSchemaType(
568 scoped_ptr<base::Value>(
569 new base::FundamentalValue(true)).get()));
570 EXPECT_EQ(std::string(schema::kBoolean),
571 JSONSchemaValidator::GetJSONSchemaType(
572 scoped_ptr<base::Value>(
573 new base::FundamentalValue(false)).get()));
575 // It doesn't matter whether the C++ type is 'integer' or 'real'. If the
576 // number is integral and within the representable range of integers in
577 // double, it's classified as 'integer'.
578 EXPECT_EQ(std::string(schema::kInteger),
579 JSONSchemaValidator::GetJSONSchemaType(
580 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get()));
581 EXPECT_EQ(std::string(schema::kInteger),
582 JSONSchemaValidator::GetJSONSchemaType(
583 scoped_ptr<base::Value>(new base::FundamentalValue(0)).get()));
584 EXPECT_EQ(std::string(schema::kInteger),
585 JSONSchemaValidator::GetJSONSchemaType(
586 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get()));
587 EXPECT_EQ(std::string(schema::kInteger),
588 JSONSchemaValidator::GetJSONSchemaType(scoped_ptr<base::Value>(
589 new base::FundamentalValue(pow(2.0, DBL_MANT_DIG))).get()));
590 EXPECT_EQ(std::string(schema::kInteger),
591 JSONSchemaValidator::GetJSONSchemaType(scoped_ptr<base::Value>(
592 new base::FundamentalValue(pow(-2.0, DBL_MANT_DIG))).get()));
594 // "number" is only used for non-integral numbers, or numbers beyond what
595 // double can accurately represent.
596 EXPECT_EQ(std::string(schema::kNumber),
597 JSONSchemaValidator::GetJSONSchemaType(
598 scoped_ptr<base::Value>(
599 new base::FundamentalValue(88.8)).get()));
600 EXPECT_EQ(std::string(schema::kNumber),
601 JSONSchemaValidator::GetJSONSchemaType(scoped_ptr<base::Value>(
602 new base::FundamentalValue(pow(2.0, DBL_MANT_DIG) * 2)).get()));
603 EXPECT_EQ(std::string(schema::kNumber),
604 JSONSchemaValidator::GetJSONSchemaType(scoped_ptr<base::Value>(
605 new base::FundamentalValue(
606 pow(-2.0, DBL_MANT_DIG) * 2)).get()));
608 EXPECT_EQ(std::string(schema::kString),
609 JSONSchemaValidator::GetJSONSchemaType(
610 scoped_ptr<base::Value>(new base::StringValue("foo")).get()));
611 EXPECT_EQ(std::string(schema::kArray),
612 JSONSchemaValidator::GetJSONSchemaType(
613 scoped_ptr<base::Value>(new base::ListValue()).get()));
614 EXPECT_EQ(std::string(schema::kObject),
615 JSONSchemaValidator::GetJSONSchemaType(
616 scoped_ptr<base::Value>(new base::DictionaryValue()).get()));
617 EXPECT_EQ(std::string(schema::kNull),
618 JSONSchemaValidator::GetJSONSchemaType(
619 base::Value::CreateNullValue().get()));
622 void JSONSchemaValidatorTestBase::TestTypes() {
623 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue());
625 // valid
626 schema->SetString(schema::kType, schema::kObject);
627 ExpectValid(TEST_SOURCE,
628 scoped_ptr<base::Value>(new base::DictionaryValue()).get(),
629 schema.get(), NULL);
631 schema->SetString(schema::kType, schema::kArray);
632 ExpectValid(TEST_SOURCE, scoped_ptr<base::Value>(new base::ListValue()).get(),
633 schema.get(), NULL);
635 schema->SetString(schema::kType, schema::kString);
636 ExpectValid(TEST_SOURCE,
637 scoped_ptr<base::Value>(new base::StringValue("foobar")).get(),
638 schema.get(), NULL);
640 schema->SetString(schema::kType, schema::kNumber);
641 ExpectValid(TEST_SOURCE,
642 scoped_ptr<base::Value>(new base::FundamentalValue(88.8)).get(),
643 schema.get(), NULL);
644 ExpectValid(TEST_SOURCE,
645 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get(),
646 schema.get(), NULL);
647 ExpectValid(TEST_SOURCE,
648 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get(),
649 schema.get(), NULL);
650 ExpectValid(TEST_SOURCE,
651 scoped_ptr<base::Value>(new base::FundamentalValue(0)).get(),
652 schema.get(), NULL);
654 schema->SetString(schema::kType, schema::kInteger);
655 ExpectValid(TEST_SOURCE,
656 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get(),
657 schema.get(), NULL);
658 ExpectValid(TEST_SOURCE,
659 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get(),
660 schema.get(), NULL);
661 ExpectValid(TEST_SOURCE,
662 scoped_ptr<base::Value>(new base::FundamentalValue(0)).get(),
663 schema.get(), NULL);
664 ExpectValid(TEST_SOURCE,
665 scoped_ptr<base::Value>(
666 new base::FundamentalValue(pow(2.0, DBL_MANT_DIG))).get(),
667 schema.get(), NULL);
668 ExpectValid(TEST_SOURCE,
669 scoped_ptr<base::Value>(
670 new base::FundamentalValue(pow(-2.0, DBL_MANT_DIG))).get(),
671 schema.get(), NULL);
673 schema->SetString(schema::kType, schema::kBoolean);
674 ExpectValid(TEST_SOURCE,
675 scoped_ptr<base::Value>(new base::FundamentalValue(false)).get(),
676 schema.get(), NULL);
677 ExpectValid(TEST_SOURCE,
678 scoped_ptr<base::Value>(new base::FundamentalValue(true)).get(),
679 schema.get(), NULL);
681 schema->SetString(schema::kType, schema::kNull);
682 ExpectValid(TEST_SOURCE, base::Value::CreateNullValue().get(), schema.get(),
683 NULL);
685 // not valid
686 schema->SetString(schema::kType, schema::kObject);
687 ExpectNotValid(
688 TEST_SOURCE,
689 scoped_ptr<base::Value>(new base::ListValue()).get(),
690 schema.get(),
691 NULL,
692 std::string(),
693 JSONSchemaValidator::FormatErrorMessage(
694 JSONSchemaValidator::kInvalidType, schema::kObject, schema::kArray));
696 schema->SetString(schema::kType, schema::kObject);
697 ExpectNotValid(
698 TEST_SOURCE, base::Value::CreateNullValue().get(), schema.get(), NULL,
699 std::string(),
700 JSONSchemaValidator::FormatErrorMessage(JSONSchemaValidator::kInvalidType,
701 schema::kObject, schema::kNull));
703 schema->SetString(schema::kType, schema::kArray);
704 ExpectNotValid(
705 TEST_SOURCE,
706 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get(),
707 schema.get(),
708 NULL,
709 std::string(),
710 JSONSchemaValidator::FormatErrorMessage(
711 JSONSchemaValidator::kInvalidType, schema::kArray, schema::kInteger));
713 schema->SetString(schema::kType, schema::kString);
714 ExpectNotValid(
715 TEST_SOURCE,
716 scoped_ptr<base::Value>(new base::FundamentalValue(42)).get(),
717 schema.get(),
718 NULL,
719 std::string(),
720 JSONSchemaValidator::FormatErrorMessage(JSONSchemaValidator::kInvalidType,
721 schema::kString,
722 schema::kInteger));
724 schema->SetString(schema::kType, schema::kNumber);
725 ExpectNotValid(
726 TEST_SOURCE,
727 scoped_ptr<base::Value>(new base::StringValue("42")).get(),
728 schema.get(),
729 NULL,
730 std::string(),
731 JSONSchemaValidator::FormatErrorMessage(
732 JSONSchemaValidator::kInvalidType, schema::kNumber, schema::kString));
734 schema->SetString(schema::kType, schema::kInteger);
735 ExpectNotValid(
736 TEST_SOURCE,
737 scoped_ptr<base::Value>(new base::FundamentalValue(88.8)).get(),
738 schema.get(),
739 NULL,
740 std::string(),
741 JSONSchemaValidator::kInvalidTypeIntegerNumber);
743 schema->SetString(schema::kType, schema::kBoolean);
744 ExpectNotValid(
745 TEST_SOURCE,
746 scoped_ptr<base::Value>(new base::FundamentalValue(1)).get(),
747 schema.get(),
748 NULL,
749 std::string(),
750 JSONSchemaValidator::FormatErrorMessage(JSONSchemaValidator::kInvalidType,
751 schema::kBoolean,
752 schema::kInteger));
754 schema->SetString(schema::kType, schema::kNull);
755 ExpectNotValid(
756 TEST_SOURCE,
757 scoped_ptr<base::Value>(new base::FundamentalValue(false)).get(),
758 schema.get(),
759 NULL,
760 std::string(),
761 JSONSchemaValidator::FormatErrorMessage(
762 JSONSchemaValidator::kInvalidType, schema::kNull, schema::kBoolean));