Chromium Code Reviews| Index: components/policy/core/common/schema_unittest.cc |
| diff --git a/components/policy/core/common/schema_unittest.cc b/components/policy/core/common/schema_unittest.cc |
| index 8a3ba90b1bd09876b9f0f98900fb11a9ddd50b6c..3f8483d4337e6f43711ca0f2e22b214ed6d9143f 100644 |
| --- a/components/policy/core/common/schema_unittest.cc |
| +++ b/components/policy/core/common/schema_unittest.cc |
| @@ -4,6 +4,7 @@ |
| #include "components/policy/core/common/schema.h" |
| +#include "base/memory/scoped_ptr.h" |
| #include "components/policy/core/common/schema_internal.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -49,6 +50,16 @@ const char kTestSchema[] = |
| " }," |
| " \"additionalProperties\": { \"type\": \"string\" }" |
| " }," |
| + " \"ObjectOfObject\": {" |
| + " \"type\": \"object\"," |
| + " \"Object\": {" |
| + " \"type\": \"object\"," |
| + " \"properties\": {" |
| + " \"one\": { \"type\": \"string\" }," |
| + " \"two\": { \"type\": \"integer\" }" |
| + " }" |
| + " }" |
| + " }," |
|
Joao da Silva
2014/01/22 10:49:53
Test with a list of dictionaries too. More types t
binjin
2014/01/23 12:01:31
Done. Added tests for ArrayOfObjects, ObjectOfArra
|
| " \"IntegerWithEnums\": {" |
| " \"type\": \"integer\"," |
| " \"enum\": [1, 2, 3]" |
| @@ -78,6 +89,35 @@ bool ParseFails(const std::string& content) { |
| return true; |
| } |
| +void TestSchemaValidation(Schema schema, |
| + const base::Value& value, |
| + SchemaOnErrorStrategy strategy, |
| + bool expected_return_value) { |
| + std::string error; |
| + static const char* no_error_returned = "No error returned."; |
| + |
| + // Test that Schema::Validate() works as expected. |
| + error = no_error_returned; |
| + bool returned = schema.Validate(value, strategy, &error); |
| + EXPECT_EQ(returned, expected_return_value) << error; |
| + |
| + // Test that Schema::Normalize() will return the same value as |
| + // Schema::Validate(). |
| + error = no_error_returned; |
| + scoped_ptr<base::Value> cloned_value(value.DeepCopy()); |
| + returned = schema.Normalize(cloned_value.get(), strategy, &error); |
| + EXPECT_EQ(returned, expected_return_value) << error; |
| + |
| + // Test that Schema::Normalize() have actually dropped invalid and unknown |
| + // properties. |
| + if (expected_return_value) { |
| + EXPECT_TRUE(schema.Validate(*cloned_value.get(), |
| + SCHEMA_VERY_STRICT, &error)); |
| + EXPECT_TRUE(schema.Normalize(cloned_value.get(), |
| + SCHEMA_VERY_STRICT, &error)); |
| + } |
| +} |
| + |
| std::string SchemaObjectWrapper(const std::string& subschema) { |
| return "{" |
| " \"type\": \"object\"," |
| @@ -286,6 +326,7 @@ TEST(SchemaTest, ValidSchema) { |
| { "Null", base::Value::TYPE_NULL }, |
| { "Number", base::Value::TYPE_DOUBLE }, |
| { "Object", base::Value::TYPE_DICTIONARY }, |
| + { "ObjectOfObject", base::Value::TYPE_DICTIONARY }, |
| { "String", base::Value::TYPE_STRING }, |
| { "StringWithEnums", base::Value::TYPE_STRING }, |
| }; |
| @@ -467,11 +508,11 @@ TEST(SchemaTest, Validate) { |
| ASSERT_TRUE(schema.valid()) << error; |
| base::DictionaryValue bundle; |
| - EXPECT_TRUE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, true); |
| // Wrong type, expected integer. |
| bundle.SetBoolean("Integer", true); |
| - EXPECT_FALSE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| // Wrong type, expected list of strings. |
| { |
| @@ -479,7 +520,7 @@ TEST(SchemaTest, Validate) { |
| base::ListValue list; |
| list.AppendInteger(1); |
| bundle.Set("Array", list.DeepCopy()); |
| - EXPECT_FALSE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| } |
| // Wrong type in a sub-object. |
| @@ -488,13 +529,13 @@ TEST(SchemaTest, Validate) { |
| base::DictionaryValue dict; |
| dict.SetString("one", "one"); |
| bundle.Set("Object", dict.DeepCopy()); |
| - EXPECT_FALSE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| } |
| // Unknown name. |
| bundle.Clear(); |
| bundle.SetBoolean("Unknown", true); |
| - EXPECT_FALSE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| // All of these will be valid. |
| bundle.Clear(); |
| @@ -545,47 +586,71 @@ TEST(SchemaTest, Validate) { |
| bundle.SetString("StringWithEnums", "two"); |
| bundle.SetInteger("IntegerWithRange", 3); |
| - EXPECT_TRUE(schema.Validate(bundle)); |
| - |
| - bundle.SetString("boom", "bang"); |
| - EXPECT_FALSE(schema.Validate(bundle)); |
| - bundle.Remove("boom", NULL); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, true); |
| bundle.SetInteger("IntegerWithEnums", 0); |
| - EXPECT_FALSE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| bundle.SetInteger("IntegerWithEnums", 1); |
| bundle.SetInteger("IntegerWithEnumsGaps", 0); |
| - EXPECT_FALSE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| bundle.SetInteger("IntegerWithEnumsGaps", 9); |
| - EXPECT_FALSE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| bundle.SetInteger("IntegerWithEnumsGaps", 10); |
| - EXPECT_TRUE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, true); |
| bundle.SetInteger("IntegerWithEnumsGaps", 11); |
| - EXPECT_FALSE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| bundle.SetInteger("IntegerWithEnumsGaps", 19); |
| - EXPECT_FALSE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| bundle.SetInteger("IntegerWithEnumsGaps", 21); |
| - EXPECT_FALSE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| bundle.SetInteger("IntegerWithEnumsGaps", 29); |
| - EXPECT_FALSE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| bundle.SetInteger("IntegerWithEnumsGaps", 30); |
| - EXPECT_TRUE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, true); |
| bundle.SetInteger("IntegerWithEnumsGaps", 31); |
| - EXPECT_FALSE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| bundle.SetInteger("IntegerWithEnumsGaps", 100); |
| - EXPECT_FALSE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| bundle.SetInteger("IntegerWithEnumsGaps", 20); |
| bundle.SetString("StringWithEnums", "FOUR"); |
| - EXPECT_FALSE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| bundle.SetString("StringWithEnums", "two"); |
| bundle.SetInteger("IntegerWithRange", 4); |
| - EXPECT_FALSE(schema.Validate(bundle)); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| bundle.SetInteger("IntegerWithRange", 3); |
| + // Unknown top level property. |
| + bundle.SetString("boom", "bang"); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| + TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true); |
| + TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN, true); |
| + bundle.Remove("boom", NULL); |
| + |
| + // Invalid top level property. |
| + bundle.SetInteger("Boolean", 12345); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| + TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID_TOPLEVEL, true); |
| + TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID, true); |
| + bundle.SetBoolean("Boolean", true); |
| + |
| + // Unknown property. |
| + bundle.SetBoolean("ObjectOfObject.Object.three", false); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| + TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false); |
| + TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN, true); |
| + bundle.Remove("ObjectOfObject.Object.three", NULL); |
| + |
| + // Invalid property. |
| + bundle.SetInteger("ObjectOfObject.Object.one", 12345); |
| + TestSchemaValidation(schema, bundle, SCHEMA_VERY_STRICT, false); |
| + TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID_TOPLEVEL, false); |
| + TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID, true); |
| + bundle.Remove("ObjectOfObject.Object.one", NULL); |
| } |
| + |
| TEST(SchemaTest, InvalidReferences) { |
| // References to undeclared schemas fail. |
| EXPECT_TRUE(ParseFails( |