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