Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(512)

Unified Diff: components/policy/core/common/schema_unittest.cc

Issue 205923004: Add regex support in policy schema (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@json-schema-regex
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 8effd896aabab93aaf9ec32abf1f6e6491f4fff1..d8c267f33934984aa855c65585bf44b2e35ac75c 100644
--- a/components/policy/core/common/schema_unittest.cc
+++ b/components/policy/core/common/schema_unittest.cc
@@ -5,6 +5,7 @@
#include "components/policy/core/common/schema.h"
#include "base/memory/scoped_ptr.h"
+#include "base/strings/stringprintf.h"
#include "components/policy/core/common/schema_internal.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -12,6 +13,8 @@ namespace policy {
namespace {
+#define TEST_SOURCE base::StringPrintf("%s:%i", __FILE__, __LINE__)
+
const char kTestSchema[] =
"{"
" \"type\": \"object\","
@@ -99,6 +102,19 @@ const char kTestSchema[] =
" }"
" }"
" }"
+ " },"
+ " \"StringWithPattern\": {"
+ " \"type\": \"string\","
+ " \"pattern\": \"^foo+$\""
+ " },"
+ " \"ObjectWithPatternProperties\": {"
+ " \"type\": \"object\","
+ " \"patternProperties\": {"
+ " \"^foo+$\": { \"type\": \"integer\" }"
+ " },"
+ " \"properties\": {"
+ " \"bar\": { \"type\": \"string\" }"
+ " }"
" }"
" }"
"}";
@@ -112,7 +128,8 @@ bool ParseFails(const std::string& content) {
return true;
}
-void TestSchemaValidation(Schema schema,
+void TestSchemaValidation(const std::string& source,
+ Schema schema,
const base::Value& value,
SchemaOnErrorStrategy strategy,
bool expected_return_value) {
@@ -122,7 +139,7 @@ void TestSchemaValidation(Schema schema,
// Test that Schema::Validate() works as expected.
error = kNoErrorReturned;
bool returned = schema.Validate(value, strategy, NULL, &error);
- EXPECT_EQ(returned, expected_return_value) << error;
+ ASSERT_EQ(expected_return_value, returned) << source << ": " << error;
// Test that Schema::Normalize() will return the same value as
// Schema::Validate().
@@ -131,18 +148,20 @@ void TestSchemaValidation(Schema schema,
bool touched = false;
returned =
schema.Normalize(cloned_value.get(), strategy, NULL, &error, &touched);
- EXPECT_EQ(returned, expected_return_value) << error;
+ EXPECT_EQ(expected_return_value, returned) << source << ": " << error;
bool strictly_valid = schema.Validate(value, SCHEMA_STRICT, NULL, &error);
- EXPECT_EQ(!strictly_valid && returned, touched);
+ EXPECT_EQ(touched, !strictly_valid && returned) << source;
// Test that Schema::Normalize() have actually dropped invalid and unknown
// properties.
if (expected_return_value) {
EXPECT_TRUE(
- schema.Validate(*cloned_value.get(), SCHEMA_STRICT, NULL, &error));
- EXPECT_TRUE(schema.Normalize(
- cloned_value.get(), SCHEMA_STRICT, NULL, &error, NULL));
+ schema.Validate(*cloned_value.get(), SCHEMA_STRICT, NULL, &error))
+ << source;
+ EXPECT_TRUE(
+ schema.Normalize(cloned_value.get(), SCHEMA_STRICT, NULL, &error, NULL))
+ << source;
}
}
@@ -350,26 +369,36 @@ TEST(SchemaTest, ValidSchema) {
ASSERT_TRUE(sub.valid());
ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type());
+ sub = schema.GetProperty("StringWithPattern");
+ ASSERT_TRUE(sub.valid());
+ ASSERT_EQ(base::Value::TYPE_STRING, sub.type());
+
+ sub = schema.GetProperty("ObjectWithPatternProperties");
+ ASSERT_TRUE(sub.valid());
+ ASSERT_EQ(base::Value::TYPE_DICTIONARY, sub.type());
+
struct {
const char* expected_key;
base::Value::Type expected_type;
} kExpectedProperties[] = {
- { "Array", base::Value::TYPE_LIST },
- { "ArrayOfArray", base::Value::TYPE_LIST },
- { "ArrayOfObjectOfArray", base::Value::TYPE_LIST },
- { "ArrayOfObjects", base::Value::TYPE_LIST },
- { "Boolean", base::Value::TYPE_BOOLEAN },
- { "Integer", base::Value::TYPE_INTEGER },
- { "IntegerWithEnums", base::Value::TYPE_INTEGER },
- { "IntegerWithEnumsGaps", base::Value::TYPE_INTEGER },
- { "IntegerWithRange", base::Value::TYPE_INTEGER },
- { "Null", base::Value::TYPE_NULL },
- { "Number", base::Value::TYPE_DOUBLE },
- { "Object", base::Value::TYPE_DICTIONARY },
- { "ObjectOfArray", base::Value::TYPE_DICTIONARY },
- { "ObjectOfObject", base::Value::TYPE_DICTIONARY },
- { "String", base::Value::TYPE_STRING },
- { "StringWithEnums", base::Value::TYPE_STRING },
+ { "Array", base::Value::TYPE_LIST },
+ { "ArrayOfArray", base::Value::TYPE_LIST },
+ { "ArrayOfObjectOfArray", base::Value::TYPE_LIST },
+ { "ArrayOfObjects", base::Value::TYPE_LIST },
+ { "Boolean", base::Value::TYPE_BOOLEAN },
+ { "Integer", base::Value::TYPE_INTEGER },
+ { "IntegerWithEnums", base::Value::TYPE_INTEGER },
+ { "IntegerWithEnumsGaps", base::Value::TYPE_INTEGER },
+ { "IntegerWithRange", base::Value::TYPE_INTEGER },
+ { "Null", base::Value::TYPE_NULL },
+ { "Number", base::Value::TYPE_DOUBLE },
+ { "Object", base::Value::TYPE_DICTIONARY },
+ { "ObjectOfArray", base::Value::TYPE_DICTIONARY },
+ { "ObjectOfObject", base::Value::TYPE_DICTIONARY },
+ { "ObjectWithPatternProperties", base::Value::TYPE_DICTIONARY },
+ { "String", base::Value::TYPE_STRING },
+ { "StringWithEnums", base::Value::TYPE_STRING },
+ { "StringWithPattern", base::Value::TYPE_STRING },
};
Schema::Iterator it = schema.GetPropertiesIterator();
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) {
@@ -444,16 +473,17 @@ TEST(SchemaTest, Lookups) {
TEST(SchemaTest, Wrap) {
const internal::SchemaNode kSchemas[] = {
- { base::Value::TYPE_DICTIONARY, 0 }, // 0: root node
- { base::Value::TYPE_BOOLEAN, -1 }, // 1
- { base::Value::TYPE_INTEGER, -1 }, // 2
- { base::Value::TYPE_DOUBLE, -1 }, // 3
- { base::Value::TYPE_STRING, -1 }, // 4
- { base::Value::TYPE_LIST, 4 }, // 5: list of strings.
- { base::Value::TYPE_LIST, 5 }, // 6: list of lists of strings.
- { base::Value::TYPE_INTEGER, 0 }, // 7: integer enumerations.
- { base::Value::TYPE_INTEGER, 1 }, // 8: ranged integers.
- { base::Value::TYPE_STRING, 2 }, // 9: string enumerations.
+ { base::Value::TYPE_DICTIONARY, 0 }, // 0: root node
+ { base::Value::TYPE_BOOLEAN, -1 }, // 1
+ { base::Value::TYPE_INTEGER, -1 }, // 2
+ { base::Value::TYPE_DOUBLE, -1 }, // 3
+ { base::Value::TYPE_STRING, -1 }, // 4
+ { base::Value::TYPE_LIST, 4 }, // 5: list of strings.
+ { base::Value::TYPE_LIST, 5 }, // 6: list of lists of strings.
+ { base::Value::TYPE_INTEGER, 0 }, // 7: integer enumerations.
+ { base::Value::TYPE_INTEGER, 1 }, // 8: ranged integers.
+ { base::Value::TYPE_STRING, 2 }, // 9: string enumerations.
+ { base::Value::TYPE_STRING, 3 }, // 10: string with pattern.
};
const internal::PropertyNode kPropertyNodes[] = {
@@ -465,26 +495,30 @@ TEST(SchemaTest, Wrap) {
{ "IntEnum", 7 }, // 5
{ "RangedInt", 8 }, // 6
{ "StrEnum", 9 }, // 7
+ { "StrPat", 10 }, // 8
+ { "bar+", 4 }, // 9
};
const internal::PropertiesNode kProperties[] = {
- // 0 to 8 (exclusive) are the known properties in kPropertyNodes, and 6 is
- // the addionalProperties node.
- { 0, 8, 6 },
+ // 0 to 9 (exclusive) are the known properties in kPropertyNodes, 9 is
+ // patternProperties and 6 is the addionalProperties node.
+ { 0, 9, 10, 6 },
};
const internal::RestrictionNode kRestriction[] = {
- {{0, 3}}, // [1, 2, 3]
- {{5, 1}}, // minimum = 1, maximum = 5
- {{0, 3}}, // ["one", "two", "three"]
+ {{0, 3}}, // 0: [1, 2, 3]
+ {{5, 1}}, // 1: minimum = 1, maximum = 5
+ {{0, 3}}, // 2: ["one", "two", "three"]
+ {{3, 3}}, // 3: pattern "foo+"
};
const int kIntEnums[] = {1, 2, 3};
const char* kStringEnums[] = {
- "one",
- "two",
- "three",
+ "one", // 0
+ "two", // 1
+ "three", // 2
+ "foo+", // 3
};
const internal::SchemaData kData = {
@@ -511,7 +545,8 @@ TEST(SchemaTest, Wrap) {
{ "List", base::Value::TYPE_LIST },
{ "IntEnum", base::Value::TYPE_INTEGER },
{ "RangedInt", base::Value::TYPE_INTEGER },
- { "StrEnum", base::Value::TYPE_STRING }
+ { "StrEnum", base::Value::TYPE_STRING },
+ { "StrPat", base::Value::TYPE_STRING },
};
Schema::Iterator it = schema.GetPropertiesIterator();
@@ -541,6 +576,13 @@ TEST(SchemaTest, Wrap) {
Schema subsubsub = subsub.GetItems();
ASSERT_TRUE(subsubsub.valid());
ASSERT_EQ(base::Value::TYPE_STRING, subsubsub.type());
+
+ sub = schema.GetPatternProperty("barr");
+ ASSERT_TRUE(sub.valid());
+ ASSERT_EQ(base::Value::TYPE_STRING, sub.type());
+
+ sub = schema.GetPatternProperty("ba");
+ ASSERT_FALSE(sub.valid());
}
TEST(SchemaTest, Validate) {
@@ -549,11 +591,11 @@ TEST(SchemaTest, Validate) {
ASSERT_TRUE(schema.valid()) << error;
base::DictionaryValue bundle;
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, true);
// Wrong type, expected integer.
bundle.SetBoolean("Integer", true);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
// Wrong type, expected list of strings.
{
@@ -561,7 +603,7 @@ TEST(SchemaTest, Validate) {
base::ListValue list;
list.AppendInteger(1);
bundle.Set("Array", list.DeepCopy());
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
}
// Wrong type in a sub-object.
@@ -570,13 +612,13 @@ TEST(SchemaTest, Validate) {
base::DictionaryValue dict;
dict.SetString("one", "one");
bundle.Set("Object", dict.DeepCopy());
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
}
// Unknown name.
bundle.Clear();
bundle.SetBoolean("Unknown", true);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
// All of these will be valid.
bundle.Clear();
@@ -627,55 +669,57 @@ TEST(SchemaTest, Validate) {
bundle.SetString("StringWithEnums", "two");
bundle.SetInteger("IntegerWithRange", 3);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, true);
bundle.SetInteger("IntegerWithEnums", 0);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
bundle.SetInteger("IntegerWithEnums", 1);
bundle.SetInteger("IntegerWithEnumsGaps", 0);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
bundle.SetInteger("IntegerWithEnumsGaps", 9);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
bundle.SetInteger("IntegerWithEnumsGaps", 10);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, true);
bundle.SetInteger("IntegerWithEnumsGaps", 11);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
bundle.SetInteger("IntegerWithEnumsGaps", 19);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
bundle.SetInteger("IntegerWithEnumsGaps", 21);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
bundle.SetInteger("IntegerWithEnumsGaps", 29);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
bundle.SetInteger("IntegerWithEnumsGaps", 30);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, true);
bundle.SetInteger("IntegerWithEnumsGaps", 31);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
bundle.SetInteger("IntegerWithEnumsGaps", 100);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
bundle.SetInteger("IntegerWithEnumsGaps", 20);
bundle.SetString("StringWithEnums", "FOUR");
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
bundle.SetString("StringWithEnums", "two");
bundle.SetInteger("IntegerWithRange", 4);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
bundle.SetInteger("IntegerWithRange", 3);
// Unknown top level property.
bundle.SetString("boom", "bang");
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
- TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true);
- TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_UNKNOWN, true);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(
+ TEST_SOURCE, schema, bundle, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_ALLOW_UNKNOWN, true);
TestSchemaValidationWithPath(schema, bundle, "");
bundle.Remove("boom", NULL);
// Invalid top level property.
bundle.SetInteger("Boolean", 12345);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, false);
- TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
- TestSchemaValidation(schema, bundle, SCHEMA_ALLOW_INVALID, true);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, false);
+ TestSchemaValidation(
+ TEST_SOURCE, schema, bundle, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_ALLOW_INVALID, true);
TestSchemaValidationWithPath(schema, bundle, "Boolean");
bundle.SetBoolean("Boolean", true);
@@ -687,21 +731,29 @@ TEST(SchemaTest, Validate) {
// Unknown property.
root.SetBoolean("Object.three", false);
- TestSchemaValidation(subschema, root, SCHEMA_STRICT, false);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true);
TestSchemaValidationWithPath(subschema, root, "Object");
root.Remove("Object.three", NULL);
// Invalid property.
root.SetInteger("Object.one", 12345);
- TestSchemaValidation(subschema, root, SCHEMA_STRICT, false);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true);
TestSchemaValidationWithPath(subschema, root, "Object.one");
root.Remove("Object.one", NULL);
}
@@ -716,11 +768,15 @@ TEST(SchemaTest, Validate) {
base::DictionaryValue* dict_value = new base::DictionaryValue();
dict_value->SetBoolean("three", true);
root.Append(dict_value); // Pass ownership to root.
- TestSchemaValidation(subschema, root, SCHEMA_STRICT, false);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true);
TestSchemaValidationWithPath(subschema, root, "items[0]");
root.Remove(root.GetSize() - 1, NULL);
@@ -728,11 +784,15 @@ TEST(SchemaTest, Validate) {
dict_value = new base::DictionaryValue();
dict_value->SetBoolean("two", true);
root.Append(dict_value); // Pass ownership to root.
- TestSchemaValidation(subschema, root, SCHEMA_STRICT, false);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true);
TestSchemaValidationWithPath(subschema, root, "items[0].two");
root.Remove(root.GetSize() - 1, NULL);
}
@@ -748,19 +808,27 @@ TEST(SchemaTest, Validate) {
// Test that there are not errors here.
list_value->Append(new base::FundamentalValue(12345));
- TestSchemaValidation(subschema, root, SCHEMA_STRICT, true);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, true);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true);
// Invalid list item.
list_value->Append(new base::StringValue("blabla"));
- TestSchemaValidation(subschema, root, SCHEMA_STRICT, false);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true);
TestSchemaValidationWithPath(subschema, root, "List.items[1]");
}
@@ -777,25 +845,90 @@ TEST(SchemaTest, Validate) {
// Test that there are not errors here.
list_value->Append(new base::StringValue("blabla"));
- TestSchemaValidation(subschema, root, SCHEMA_STRICT, true);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, true);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, true);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true);
// Invalid list item.
list_value->Append(new base::FundamentalValue(12345));
- TestSchemaValidation(subschema, root, SCHEMA_STRICT, false);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
- TestSchemaValidation(subschema, root, SCHEMA_ALLOW_INVALID, true);
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN_TOPLEVEL, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID_TOPLEVEL, true);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_INVALID, true);
TestSchemaValidationWithPath(subschema, root, "items[0].List.items[1]");
}
+ // Tests on StringWithPattern.
+ {
+ Schema subschema = schema.GetProperty("StringWithPattern");
+ ASSERT_TRUE(subschema.valid());
+
+ TestSchemaValidation(TEST_SOURCE,
+ subschema,
+ base::StringValue(std::string("foobar")),
+ SCHEMA_STRICT,
+ false);
+ TestSchemaValidation(TEST_SOURCE,
+ subschema,
+ base::StringValue(std::string("foo")),
+ SCHEMA_STRICT,
+ true);
+ TestSchemaValidation(TEST_SOURCE,
+ subschema,
+ base::StringValue(std::string("fo")),
+ SCHEMA_STRICT,
+ false);
+ TestSchemaValidation(TEST_SOURCE,
+ subschema,
+ base::StringValue(std::string("fooo")),
+ SCHEMA_STRICT,
+ true);
+ TestSchemaValidation(TEST_SOURCE,
+ subschema,
+ base::StringValue(std::string("^foo+$")),
+ SCHEMA_STRICT,
+ false);
+ }
+
+ // Tests on ObjectWithPatternProperties
+ {
+ Schema subschema = schema.GetProperty("ObjectWithPatternProperties");
+ ASSERT_TRUE(subschema.valid());
+ base::DictionaryValue root;
+
+ root.SetInteger("fooo", 123);
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, true);
+ root.SetBoolean("fooo", false);
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
+ root.Remove("fooo", NULL);
+
+ root.SetInteger("foo", 123);
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, true);
+ root.SetBoolean("foo", false);
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
+ root.Remove("foo", NULL);
+
+ root.SetInteger("foobar", 123);
+ TestSchemaValidation(TEST_SOURCE, subschema, root, SCHEMA_STRICT, false);
+ TestSchemaValidation(
+ TEST_SOURCE, subschema, root, SCHEMA_ALLOW_UNKNOWN, true);
+ root.Remove("foobar", NULL);
+ }
+
// Test that integer to double promotion is allowed.
bundle.SetInteger("Number", 31415);
- TestSchemaValidation(schema, bundle, SCHEMA_STRICT, true);
+ TestSchemaValidation(TEST_SOURCE, schema, bundle, SCHEMA_STRICT, true);
}
TEST(SchemaTest, InvalidReferences) {

Powered by Google App Engine
This is Rietveld 408576698