Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/json_schema/json_schema_validator_unittest_base.h" | 5 #include "components/json_schema/json_schema_validator_unittest_base.h" |
| 6 | 6 |
| 7 #include <cfloat> | 7 #include <cfloat> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 ASSERT_TRUE(instance->GetDictionary(0, &item)); | 111 ASSERT_TRUE(instance->GetDictionary(0, &item)); |
| 112 item->SetString("url", "xxxxxxxxxxx"); | 112 item->SetString("url", "xxxxxxxxxxx"); |
| 113 | 113 |
| 114 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | 114 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
| 115 "0.url", | 115 "0.url", |
| 116 JSONSchemaValidator::FormatErrorMessage( | 116 JSONSchemaValidator::FormatErrorMessage( |
| 117 JSONSchemaValidator::kStringMaxLength, "10")); | 117 JSONSchemaValidator::kStringMaxLength, "10")); |
| 118 } | 118 } |
| 119 | 119 |
| 120 void JSONSchemaValidatorTestBase::TestStringPattern() { | 120 void JSONSchemaValidatorTestBase::TestStringPattern() { |
| 121 // Regex patterns not supported in CPP validator. | |
| 122 if (type_ == CPP) | |
| 123 return; | |
| 124 | |
| 125 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); | 121 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); |
| 126 schema->SetString(schema::kType, schema::kString); | 122 schema->SetString(schema::kType, schema::kString); |
| 127 schema->SetString(schema::kPattern, "foo+"); | 123 schema->SetString(schema::kPattern, "foo+"); |
| 128 | 124 |
| 129 ExpectValid(TEST_SOURCE, | 125 ExpectValid(TEST_SOURCE, |
| 130 scoped_ptr<base::Value>(new base::StringValue("foo")).get(), | 126 scoped_ptr<base::Value>(new base::StringValue("foo")).get(), |
| 131 schema.get(), NULL); | 127 schema.get(), NULL); |
| 132 ExpectValid(TEST_SOURCE, | 128 ExpectValid(TEST_SOURCE, |
| 133 scoped_ptr<base::Value>(new base::StringValue("foooooo")).get(), | 129 scoped_ptr<base::Value>(new base::StringValue("foooooo")).get(), |
| 134 schema.get(), NULL); | 130 schema.get(), NULL); |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 | 213 |
| 218 scoped_ptr<base::DictionaryValue> instance(new base::DictionaryValue()); | 214 scoped_ptr<base::DictionaryValue> instance(new base::DictionaryValue()); |
| 219 instance->SetString("foo", "foo"); | 215 instance->SetString("foo", "foo"); |
| 220 instance->SetInteger("bar", 42); | 216 instance->SetInteger("bar", 42); |
| 221 | 217 |
| 222 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 218 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
| 223 | 219 |
| 224 instance->SetBoolean("extra", true); | 220 instance->SetBoolean("extra", true); |
| 225 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | 221 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
| 226 "extra", JSONSchemaValidator::kUnexpectedProperty); | 222 "extra", JSONSchemaValidator::kUnexpectedProperty); |
| 223 instance->Remove("extra", NULL); | |
| 227 | 224 |
| 228 instance->Remove("extra", NULL); | |
| 229 instance->Remove("bar", NULL); | 225 instance->Remove("bar", NULL); |
| 230 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar", | 226 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar", |
| 231 JSONSchemaValidator::kObjectPropertyIsRequired); | 227 JSONSchemaValidator::kObjectPropertyIsRequired); |
| 232 | 228 |
| 233 instance->SetString("bar", "42"); | 229 instance->SetString("bar", "42"); |
| 234 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar", | 230 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar", |
| 235 JSONSchemaValidator::FormatErrorMessage( | 231 JSONSchemaValidator::FormatErrorMessage( |
| 236 JSONSchemaValidator::kInvalidType, | 232 JSONSchemaValidator::kInvalidType, |
| 237 schema::kInteger, | 233 schema::kInteger, |
| 238 schema::kString)); | 234 schema::kString)); |
| 235 instance->SetInteger("bar", 42); | |
| 239 | 236 |
| 237 // Test "patternProperties". | |
| 238 instance->SetInteger("extra", 42); | |
| 239 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | |
| 240 "extra", JSONSchemaValidator::kUnexpectedProperty); | |
| 241 schema->SetString("patternProperties.extra+.type", | |
| 242 schema::kInteger); | |
| 243 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
| 244 instance->SetInteger("extra", 42); | |
|
Joao da Silva
2014/03/13 15:10:52
Isn't this already done in line 238 and tested in
binjin
2014/03/20 15:14:30
Done.
| |
| 245 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
| 246 instance->Remove("extra", NULL); | |
| 247 instance->SetInteger("extraaa", 42); | |
| 248 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
| 249 instance->Remove("extraaa", NULL); | |
| 250 instance->SetInteger("extr", 42); | |
| 251 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | |
| 252 "extr", JSONSchemaValidator::kUnexpectedProperty); | |
| 253 instance->Remove("extr", NULL); | |
| 254 schema->Remove(schema::kPatternProperties, NULL); | |
| 255 | |
| 256 // Test "patternProperties" and "properties" schemas are both checked if | |
| 257 // applicable. | |
| 258 schema->SetString("patternProperties.fo+.type", schema::kInteger); | |
| 259 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "foo", | |
| 260 JSONSchemaValidator::FormatErrorMessage( | |
| 261 JSONSchemaValidator::kInvalidType, | |
| 262 schema::kInteger, | |
| 263 schema::kString)); | |
| 264 instance->SetInteger("foo", 123); | |
| 265 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "foo", | |
| 266 JSONSchemaValidator::FormatErrorMessage( | |
| 267 JSONSchemaValidator::kInvalidType, | |
| 268 schema::kString, | |
| 269 schema::kInteger)); | |
| 270 instance->SetString("foo", "foo"); | |
| 271 schema->Remove(schema::kPatternProperties, NULL); | |
| 272 | |
| 273 // Test additional properties. | |
| 240 base::DictionaryValue* additional_properties = new base::DictionaryValue(); | 274 base::DictionaryValue* additional_properties = new base::DictionaryValue(); |
| 241 additional_properties->SetString(schema::kType, schema::kAny); | 275 additional_properties->SetString(schema::kType, schema::kAny); |
| 242 schema->Set(schema::kAdditionalProperties, additional_properties); | 276 schema->Set(schema::kAdditionalProperties, additional_properties); |
| 243 | 277 |
| 244 instance->SetInteger("bar", 42); | |
| 245 instance->SetBoolean("extra", true); | 278 instance->SetBoolean("extra", true); |
| 246 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 279 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
| 247 | 280 |
| 248 instance->SetString("extra", "foo"); | 281 instance->SetString("extra", "foo"); |
| 249 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 282 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
| 250 | 283 |
| 251 additional_properties->SetString(schema::kType, schema::kBoolean); | 284 additional_properties->SetString(schema::kType, schema::kBoolean); |
| 252 instance->SetBoolean("extra", true); | 285 instance->SetBoolean("extra", true); |
| 253 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 286 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
| 254 | 287 |
| 255 instance->SetString("extra", "foo"); | 288 instance->SetString("extra", "foo"); |
| 256 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | 289 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
| 257 "extra", JSONSchemaValidator::FormatErrorMessage( | 290 "extra", JSONSchemaValidator::FormatErrorMessage( |
| 258 JSONSchemaValidator::kInvalidType, | 291 JSONSchemaValidator::kInvalidType, |
| 259 schema::kBoolean, | 292 schema::kBoolean, |
| 260 schema::kString)); | 293 schema::kString)); |
| 294 instance->Remove("extra", NULL); | |
| 261 | 295 |
| 262 base::DictionaryValue* properties = NULL; | 296 base::DictionaryValue* properties = NULL; |
| 263 base::DictionaryValue* bar_property = NULL; | 297 base::DictionaryValue* bar_property = NULL; |
| 264 ASSERT_TRUE(schema->GetDictionary(schema::kProperties, &properties)); | 298 ASSERT_TRUE(schema->GetDictionary(schema::kProperties, &properties)); |
| 265 ASSERT_TRUE(properties->GetDictionary("bar", &bar_property)); | 299 ASSERT_TRUE(properties->GetDictionary("bar", &bar_property)); |
| 266 | 300 |
| 267 bar_property->SetBoolean(schema::kOptional, true); | 301 bar_property->SetBoolean(schema::kOptional, true); |
| 268 instance->Remove("extra", NULL); | |
| 269 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 302 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
| 270 instance->Remove("bar", NULL); | 303 instance->Remove("bar", NULL); |
| 271 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 304 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
| 272 instance->Set("bar", base::Value::CreateNullValue()); | 305 instance->Set("bar", base::Value::CreateNullValue()); |
| 273 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | 306 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
| 274 "bar", JSONSchemaValidator::FormatErrorMessage( | 307 "bar", JSONSchemaValidator::FormatErrorMessage( |
| 275 JSONSchemaValidator::kInvalidType, | 308 JSONSchemaValidator::kInvalidType, |
| 276 schema::kInteger, | 309 schema::kInteger, |
| 277 schema::kNull)); | 310 schema::kNull)); |
| 278 instance->SetString("bar", "42"); | 311 instance->SetString("bar", "42"); |
| 279 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | 312 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
| 280 "bar", JSONSchemaValidator::FormatErrorMessage( | 313 "bar", JSONSchemaValidator::FormatErrorMessage( |
| 281 JSONSchemaValidator::kInvalidType, | 314 JSONSchemaValidator::kInvalidType, |
| 282 schema::kInteger, | 315 schema::kInteger, |
| 283 schema::kString)); | 316 schema::kString)); |
| 317 | |
| 318 // Verify that JSON parser handles dot in "patternProperties" well. | |
| 319 schema.reset(LoadDictionary("pattern_properties_dot.json")); | |
| 320 ASSERT_TRUE(schema->GetDictionary(schema::kPatternProperties, &properties)); | |
| 321 ASSERT_TRUE(properties->HasKey("^.$")); | |
| 322 | |
| 323 instance.reset(new base::DictionaryValue()); | |
| 324 instance->SetString("a", "whatever"); | |
| 325 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | |
| 326 instance->SetString("foo", "bar"); | |
| 327 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | |
| 328 "foo", JSONSchemaValidator::kUnexpectedProperty); | |
| 284 } | 329 } |
| 285 | 330 |
| 286 void JSONSchemaValidatorTestBase::TestTypeReference() { | 331 void JSONSchemaValidatorTestBase::TestTypeReference() { |
| 287 scoped_ptr<base::ListValue> types(LoadList("reference_types.json")); | 332 scoped_ptr<base::ListValue> types(LoadList("reference_types.json")); |
| 288 ASSERT_TRUE(types.get()); | 333 ASSERT_TRUE(types.get()); |
| 289 | 334 |
| 290 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); | 335 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); |
| 291 schema->SetString(schema::kType, schema::kObject); | 336 schema->SetString(schema::kType, schema::kObject); |
| 292 schema->SetString("properties.foo.type", schema::kString); | 337 schema->SetString("properties.foo.type", schema::kString); |
| 293 schema->SetString("properties.bar.$ref", "Max10Int"); | 338 schema->SetString("properties.bar.$ref", "Max10Int"); |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 721 schema->SetString(schema::kType, schema::kNull); | 766 schema->SetString(schema::kType, schema::kNull); |
| 722 ExpectNotValid( | 767 ExpectNotValid( |
| 723 TEST_SOURCE, | 768 TEST_SOURCE, |
| 724 scoped_ptr<base::Value>(new base::FundamentalValue(false)).get(), | 769 scoped_ptr<base::Value>(new base::FundamentalValue(false)).get(), |
| 725 schema.get(), | 770 schema.get(), |
| 726 NULL, | 771 NULL, |
| 727 std::string(), | 772 std::string(), |
| 728 JSONSchemaValidator::FormatErrorMessage( | 773 JSONSchemaValidator::FormatErrorMessage( |
| 729 JSONSchemaValidator::kInvalidType, schema::kNull, schema::kBoolean)); | 774 JSONSchemaValidator::kInvalidType, schema::kNull, schema::kBoolean)); |
| 730 } | 775 } |
| OLD | NEW |