| 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); |
| 236 |
| 237 instance->SetInteger("extra", 42); |
| 238 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
| 239 "extra", JSONSchemaValidator::kUnexpectedProperty); |
| 240 schema->SetString("patternProperties.extra+.type", |
| 241 schema::kInteger); |
| 242 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
| 243 instance->SetInteger("extra", 42); |
| 244 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
| 245 instance->Remove("extra", NULL); |
| 246 instance->SetInteger("extraaa", 42); |
| 247 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
| 248 instance->Remove("extraaa", NULL); |
| 249 instance->SetInteger("extr", 42); |
| 250 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
| 251 "extr", JSONSchemaValidator::kUnexpectedProperty); |
| 252 instance->Remove("extr", NULL); |
| 253 schema->Remove(schema::kPatternProperties, NULL); |
| 239 | 254 |
| 240 base::DictionaryValue* additional_properties = new base::DictionaryValue(); | 255 base::DictionaryValue* additional_properties = new base::DictionaryValue(); |
| 241 additional_properties->SetString(schema::kType, schema::kAny); | 256 additional_properties->SetString(schema::kType, schema::kAny); |
| 242 schema->Set(schema::kAdditionalProperties, additional_properties); | 257 schema->Set(schema::kAdditionalProperties, additional_properties); |
| 243 | 258 |
| 244 instance->SetInteger("bar", 42); | |
| 245 instance->SetBoolean("extra", true); | 259 instance->SetBoolean("extra", true); |
| 246 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 260 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
| 247 | 261 |
| 248 instance->SetString("extra", "foo"); | 262 instance->SetString("extra", "foo"); |
| 249 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 263 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
| 250 | 264 |
| 251 additional_properties->SetString(schema::kType, schema::kBoolean); | 265 additional_properties->SetString(schema::kType, schema::kBoolean); |
| 252 instance->SetBoolean("extra", true); | 266 instance->SetBoolean("extra", true); |
| 253 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 267 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
| 254 | 268 |
| 255 instance->SetString("extra", "foo"); | 269 instance->SetString("extra", "foo"); |
| 256 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | 270 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
| 257 "extra", JSONSchemaValidator::FormatErrorMessage( | 271 "extra", JSONSchemaValidator::FormatErrorMessage( |
| 258 JSONSchemaValidator::kInvalidType, | 272 JSONSchemaValidator::kInvalidType, |
| 259 schema::kBoolean, | 273 schema::kBoolean, |
| 260 schema::kString)); | 274 schema::kString)); |
| 275 instance->Remove("extra", NULL); |
| 261 | 276 |
| 262 base::DictionaryValue* properties = NULL; | 277 base::DictionaryValue* properties = NULL; |
| 263 base::DictionaryValue* bar_property = NULL; | 278 base::DictionaryValue* bar_property = NULL; |
| 264 ASSERT_TRUE(schema->GetDictionary(schema::kProperties, &properties)); | 279 ASSERT_TRUE(schema->GetDictionary(schema::kProperties, &properties)); |
| 265 ASSERT_TRUE(properties->GetDictionary("bar", &bar_property)); | 280 ASSERT_TRUE(properties->GetDictionary("bar", &bar_property)); |
| 266 | 281 |
| 267 bar_property->SetBoolean(schema::kOptional, true); | 282 bar_property->SetBoolean(schema::kOptional, true); |
| 268 instance->Remove("extra", NULL); | |
| 269 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 283 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
| 270 instance->Remove("bar", NULL); | 284 instance->Remove("bar", NULL); |
| 271 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); | 285 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
| 272 instance->Set("bar", base::Value::CreateNullValue()); | 286 instance->Set("bar", base::Value::CreateNullValue()); |
| 273 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | 287 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
| 274 "bar", JSONSchemaValidator::FormatErrorMessage( | 288 "bar", JSONSchemaValidator::FormatErrorMessage( |
| 275 JSONSchemaValidator::kInvalidType, | 289 JSONSchemaValidator::kInvalidType, |
| 276 schema::kInteger, | 290 schema::kInteger, |
| 277 schema::kNull)); | 291 schema::kNull)); |
| 278 instance->SetString("bar", "42"); | 292 instance->SetString("bar", "42"); |
| 279 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, | 293 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
| 280 "bar", JSONSchemaValidator::FormatErrorMessage( | 294 "bar", JSONSchemaValidator::FormatErrorMessage( |
| 281 JSONSchemaValidator::kInvalidType, | 295 JSONSchemaValidator::kInvalidType, |
| 282 schema::kInteger, | 296 schema::kInteger, |
| 283 schema::kString)); | 297 schema::kString)); |
| 298 |
| 299 // Verify that JSON parser handles dot in "patternProperties" well. |
| 300 schema.reset(LoadDictionary("pattern_properties_dot.json")); |
| 301 ASSERT_TRUE(schema->GetDictionary(schema::kPatternProperties, &properties)); |
| 302 ASSERT_TRUE(properties->HasKey("^.$")); |
| 303 |
| 304 instance.reset(new base::DictionaryValue()); |
| 305 instance->SetString("a", "whatever"); |
| 306 ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL); |
| 307 instance->SetString("foo", "bar"); |
| 308 ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, |
| 309 "foo", JSONSchemaValidator::kUnexpectedProperty); |
| 284 } | 310 } |
| 285 | 311 |
| 286 void JSONSchemaValidatorTestBase::TestTypeReference() { | 312 void JSONSchemaValidatorTestBase::TestTypeReference() { |
| 287 scoped_ptr<base::ListValue> types(LoadList("reference_types.json")); | 313 scoped_ptr<base::ListValue> types(LoadList("reference_types.json")); |
| 288 ASSERT_TRUE(types.get()); | 314 ASSERT_TRUE(types.get()); |
| 289 | 315 |
| 290 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); | 316 scoped_ptr<base::DictionaryValue> schema(new base::DictionaryValue()); |
| 291 schema->SetString(schema::kType, schema::kObject); | 317 schema->SetString(schema::kType, schema::kObject); |
| 292 schema->SetString("properties.foo.type", schema::kString); | 318 schema->SetString("properties.foo.type", schema::kString); |
| 293 schema->SetString("properties.bar.$ref", "Max10Int"); | 319 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); | 747 schema->SetString(schema::kType, schema::kNull); |
| 722 ExpectNotValid( | 748 ExpectNotValid( |
| 723 TEST_SOURCE, | 749 TEST_SOURCE, |
| 724 scoped_ptr<base::Value>(new base::FundamentalValue(false)).get(), | 750 scoped_ptr<base::Value>(new base::FundamentalValue(false)).get(), |
| 725 schema.get(), | 751 schema.get(), |
| 726 NULL, | 752 NULL, |
| 727 std::string(), | 753 std::string(), |
| 728 JSONSchemaValidator::FormatErrorMessage( | 754 JSONSchemaValidator::FormatErrorMessage( |
| 729 JSONSchemaValidator::kInvalidType, schema::kNull, schema::kBoolean)); | 755 JSONSchemaValidator::kInvalidType, schema::kNull, schema::kBoolean)); |
| 730 } | 756 } |
| OLD | NEW |