summaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
authorbinjin@chromium.org <binjin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-01-22 15:02:52 +0000
committerbinjin@chromium.org <binjin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-01-22 15:02:52 +0000
commit161d096b6a56e9cd8c8be00e8932cd207ce5e14f (patch)
tree54f3ea6231da4e2f0c1198a346db39880f8efe13 /components
parentad6842c2aa5f6b96ad0a21b18fa6406dd52dddae (diff)
downloadchromium_src-161d096b6a56e9cd8c8be00e8932cd207ce5e14f.zip
chromium_src-161d096b6a56e9cd8c8be00e8932cd207ce5e14f.tar.gz
chromium_src-161d096b6a56e9cd8c8be00e8932cd207ce5e14f.tar.bz2
Add additional restriction to policy schema internal, including modification to
C++ verifier. BUG=258339 Review URL: https://codereview.chromium.org/143413002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@246328 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components')
-rw-r--r--components/policy/core/common/schema.cc34
-rw-r--r--components/policy/core/common/schema.h3
-rw-r--r--components/policy/core/common/schema_unittest.cc95
3 files changed, 123 insertions, 9 deletions
diff --git a/components/policy/core/common/schema.cc b/components/policy/core/common/schema.cc
index 7123322..38627df 100644
--- a/components/policy/core/common/schema.cc
+++ b/components/policy/core/common/schema.cc
@@ -620,6 +620,8 @@ bool Schema::Validate(const base::Value& value) const {
const base::DictionaryValue* dict = NULL;
const base::ListValue* list = NULL;
+ int int_value;
+ std::string str_value;
if (value.GetAsDictionary(&dict)) {
for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd();
it.Advance()) {
@@ -632,6 +634,12 @@ bool Schema::Validate(const base::Value& value) const {
if (!*it || !GetItems().Validate(**it))
return false;
}
+ } else if (value.GetAsInteger(&int_value)) {
+ return node_->extra == kInvalid ||
+ ValidateIntegerRestriction(node_->extra, int_value);
+ } else if (value.GetAsString(&str_value)) {
+ return node_->extra == kInvalid ||
+ ValidateStringRestriction(node_->extra, str_value.c_str());
}
return true;
@@ -723,4 +731,30 @@ Schema Schema::GetItems() const {
return Schema(storage_, storage_->schema(node_->extra));
}
+bool Schema::ValidateIntegerRestriction(int index, int value) const {
+ const RestrictionNode* rnode = storage_->restriction(index);
+ if (rnode->ranged_restriction.min_value <=
+ rnode->ranged_restriction.max_value) {
+ return rnode->ranged_restriction.min_value <= value &&
+ rnode->ranged_restriction.max_value >= value;
+ } else {
+ for (int i = rnode->enumeration_restriction.offset_begin;
+ i < rnode->enumeration_restriction.offset_end; i++) {
+ if (*storage_->int_enums(i) == value)
+ return true;
+ }
+ return false;
+ }
+}
+
+bool Schema::ValidateStringRestriction(int index, const char *str) const {
+ const RestrictionNode* rnode = storage_->restriction(index);
+ for (int i = rnode->enumeration_restriction.offset_begin;
+ i < rnode->enumeration_restriction.offset_end; i++) {
+ if (strcmp(*storage_->string_enums(i), str) == 0)
+ return true;
+ }
+ return false;
+}
+
} // namespace policy
diff --git a/components/policy/core/common/schema.h b/components/policy/core/common/schema.h
index 91dc850..e4190fb 100644
--- a/components/policy/core/common/schema.h
+++ b/components/policy/core/common/schema.h
@@ -118,6 +118,9 @@ class POLICY_EXPORT Schema {
Schema(const scoped_refptr<const InternalStorage>& storage,
const internal::SchemaNode* node);
+ bool ValidateIntegerRestriction(int index, int value) const;
+ bool ValidateStringRestriction(int index, const char *str) const;
+
scoped_refptr<const InternalStorage> storage_;
const internal::SchemaNode* node_;
};
diff --git a/components/policy/core/common/schema_unittest.cc b/components/policy/core/common/schema_unittest.cc
index 3e0fd26..8a3ba90 100644
--- a/components/policy/core/common/schema_unittest.cc
+++ b/components/policy/core/common/schema_unittest.cc
@@ -48,6 +48,23 @@ const char kTestSchema[] =
" \"two\": { \"type\": \"integer\" }"
" },"
" \"additionalProperties\": { \"type\": \"string\" }"
+ " },"
+ " \"IntegerWithEnums\": {"
+ " \"type\": \"integer\","
+ " \"enum\": [1, 2, 3]"
+ " },"
+ " \"IntegerWithEnumsGaps\": {"
+ " \"type\": \"integer\","
+ " \"enum\": [10, 20, 30]"
+ " },"
+ " \"StringWithEnums\": {"
+ " \"type\": \"string\","
+ " \"enum\": [\"one\", \"two\", \"three\"]"
+ " },"
+ " \"IntegerWithRange\": {"
+ " \"type\": \"integer\","
+ " \"minimum\": 1,"
+ " \"maximum\": 3"
" }"
" }"
"}";
@@ -238,19 +255,39 @@ TEST(SchemaTest, ValidSchema) {
ASSERT_TRUE(subsub.valid());
EXPECT_EQ(base::Value::TYPE_STRING, subsub.type());
+ sub = schema.GetProperty("IntegerWithEnums");
+ ASSERT_TRUE(sub.valid());
+ ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type());
+
+ sub = schema.GetProperty("IntegerWithEnumsGaps");
+ ASSERT_TRUE(sub.valid());
+ ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type());
+
+ sub = schema.GetProperty("StringWithEnums");
+ ASSERT_TRUE(sub.valid());
+ ASSERT_EQ(base::Value::TYPE_STRING, sub.type());
+
+ sub = schema.GetProperty("IntegerWithRange");
+ ASSERT_TRUE(sub.valid());
+ ASSERT_EQ(base::Value::TYPE_INTEGER, sub.type());
+
struct {
const char* expected_key;
base::Value::Type expected_type;
} kExpectedProperties[] = {
- { "Array", base::Value::TYPE_LIST },
- { "ArrayOfArray", base::Value::TYPE_LIST },
- { "ArrayOfObjects", base::Value::TYPE_LIST },
- { "Boolean", base::Value::TYPE_BOOLEAN },
- { "Integer", base::Value::TYPE_INTEGER },
- { "Null", base::Value::TYPE_NULL },
- { "Number", base::Value::TYPE_DOUBLE },
- { "Object", base::Value::TYPE_DICTIONARY },
- { "String", base::Value::TYPE_STRING },
+ { "Array", base::Value::TYPE_LIST },
+ { "ArrayOfArray", 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 },
+ { "String", base::Value::TYPE_STRING },
+ { "StringWithEnums", base::Value::TYPE_STRING },
};
Schema::Iterator it = schema.GetPropertiesIterator();
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) {
@@ -503,10 +540,50 @@ TEST(SchemaTest, Validate) {
bundle.Set("Object", dict.DeepCopy());
}
+ bundle.SetInteger("IntegerWithEnums", 1);
+ bundle.SetInteger("IntegerWithEnumsGaps", 20);
+ 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);
+
+ bundle.SetInteger("IntegerWithEnums", 0);
+ EXPECT_FALSE(schema.Validate(bundle));
+ bundle.SetInteger("IntegerWithEnums", 1);
+
+ bundle.SetInteger("IntegerWithEnumsGaps", 0);
+ EXPECT_FALSE(schema.Validate(bundle));
+ bundle.SetInteger("IntegerWithEnumsGaps", 9);
+ EXPECT_FALSE(schema.Validate(bundle));
+ bundle.SetInteger("IntegerWithEnumsGaps", 10);
+ EXPECT_TRUE(schema.Validate(bundle));
+ bundle.SetInteger("IntegerWithEnumsGaps", 11);
+ EXPECT_FALSE(schema.Validate(bundle));
+ bundle.SetInteger("IntegerWithEnumsGaps", 19);
+ EXPECT_FALSE(schema.Validate(bundle));
+ bundle.SetInteger("IntegerWithEnumsGaps", 21);
+ EXPECT_FALSE(schema.Validate(bundle));
+ bundle.SetInteger("IntegerWithEnumsGaps", 29);
+ EXPECT_FALSE(schema.Validate(bundle));
+ bundle.SetInteger("IntegerWithEnumsGaps", 30);
+ EXPECT_TRUE(schema.Validate(bundle));
+ bundle.SetInteger("IntegerWithEnumsGaps", 31);
+ EXPECT_FALSE(schema.Validate(bundle));
+ bundle.SetInteger("IntegerWithEnumsGaps", 100);
+ EXPECT_FALSE(schema.Validate(bundle));
+ bundle.SetInteger("IntegerWithEnumsGaps", 20);
+
+ bundle.SetString("StringWithEnums", "FOUR");
+ EXPECT_FALSE(schema.Validate(bundle));
+ bundle.SetString("StringWithEnums", "two");
+
+ bundle.SetInteger("IntegerWithRange", 4);
+ EXPECT_FALSE(schema.Validate(bundle));
+ bundle.SetInteger("IntegerWithRange", 3);
}
TEST(SchemaTest, InvalidReferences) {