OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/policy/policy_schema.h" |
| 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 |
| 9 namespace policy { |
| 10 |
| 11 namespace { |
| 12 |
| 13 #define SCHEMA_VERSION "\"$schema\":\"http://json-schema.org/draft-03/schema#\"" |
| 14 #define OBJECT_TYPE "\"type\":\"object\"" |
| 15 |
| 16 bool ParseFails(const std::string& content) { |
| 17 std::string error; |
| 18 scoped_ptr<PolicySchema> schema = PolicySchema::Parse(content, &error); |
| 19 EXPECT_TRUE(schema || !error.empty()); |
| 20 return !schema; |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
| 25 TEST(PolicySchemaTest, MinimalSchema) { |
| 26 EXPECT_FALSE(ParseFails( |
| 27 "{" |
| 28 SCHEMA_VERSION "," |
| 29 OBJECT_TYPE |
| 30 "}")); |
| 31 } |
| 32 |
| 33 TEST(PolicySchemaTest, InvalidSchemas) { |
| 34 EXPECT_TRUE(ParseFails("")); |
| 35 EXPECT_TRUE(ParseFails("omg")); |
| 36 EXPECT_TRUE(ParseFails("\"omg\"")); |
| 37 EXPECT_TRUE(ParseFails("123")); |
| 38 EXPECT_TRUE(ParseFails("[]")); |
| 39 EXPECT_TRUE(ParseFails("null")); |
| 40 EXPECT_TRUE(ParseFails("{}")); |
| 41 EXPECT_TRUE(ParseFails("{" SCHEMA_VERSION "}")); |
| 42 EXPECT_TRUE(ParseFails("{" OBJECT_TYPE "}")); |
| 43 |
| 44 EXPECT_TRUE(ParseFails( |
| 45 "{" |
| 46 SCHEMA_VERSION "," |
| 47 OBJECT_TYPE "," |
| 48 "\"additionalProperties\": { \"type\":\"object\" }" |
| 49 "}")); |
| 50 |
| 51 EXPECT_TRUE(ParseFails( |
| 52 "{" |
| 53 SCHEMA_VERSION "," |
| 54 OBJECT_TYPE "," |
| 55 "\"patternProperties\": { \"a+b*\": { \"type\": \"object\" } }" |
| 56 "}")); |
| 57 |
| 58 EXPECT_TRUE(ParseFails( |
| 59 "{" |
| 60 SCHEMA_VERSION "," |
| 61 OBJECT_TYPE "," |
| 62 "\"properties\": { \"Policy\": { \"type\": \"bogus\" } }" |
| 63 "}")); |
| 64 |
| 65 EXPECT_TRUE(ParseFails( |
| 66 "{" |
| 67 SCHEMA_VERSION "," |
| 68 OBJECT_TYPE "," |
| 69 "\"properties\": { \"Policy\": { \"type\": [\"string\", \"number\"] } }" |
| 70 "}")); |
| 71 |
| 72 EXPECT_TRUE(ParseFails( |
| 73 "{" |
| 74 SCHEMA_VERSION "," |
| 75 OBJECT_TYPE "," |
| 76 "\"properties\": { \"Policy\": { \"type\": \"any\" } }" |
| 77 "}")); |
| 78 } |
| 79 |
| 80 TEST(PolicySchemaTest, ValidSchema) { |
| 81 std::string error; |
| 82 scoped_ptr<PolicySchema> schema = PolicySchema::Parse( |
| 83 "{" |
| 84 SCHEMA_VERSION "," |
| 85 OBJECT_TYPE "," |
| 86 "\"properties\": {" |
| 87 " \"Boolean\": { \"type\": \"boolean\" }," |
| 88 " \"Integer\": { \"type\": \"integer\" }," |
| 89 " \"Null\": { \"type\": \"null\" }," |
| 90 " \"Number\": { \"type\": \"number\" }," |
| 91 " \"String\": { \"type\": \"string\" }," |
| 92 " \"Array\": {" |
| 93 " \"type\": \"array\"," |
| 94 " \"items\": { \"type\": \"string\" }" |
| 95 " }," |
| 96 " \"ArrayOfObjects\": {" |
| 97 " \"type\": \"array\"," |
| 98 " \"items\": {" |
| 99 " \"type\": \"object\"," |
| 100 " \"properties\": {" |
| 101 " \"one\": { \"type\": \"string\" }," |
| 102 " \"two\": { \"type\": \"integer\" }" |
| 103 " }" |
| 104 " }" |
| 105 " }," |
| 106 " \"ArrayOfArray\": {" |
| 107 " \"type\": \"array\"," |
| 108 " \"items\": {" |
| 109 " \"type\": \"array\"," |
| 110 " \"items\": { \"type\": \"string\" }" |
| 111 " }" |
| 112 " }," |
| 113 " \"Object\": {" |
| 114 " \"type\": \"object\"," |
| 115 " \"properties\": {" |
| 116 " \"one\": { \"type\": \"boolean\" }," |
| 117 " \"two\": { \"type\": \"integer\" }" |
| 118 " }," |
| 119 " \"additionalProperties\": { \"type\": \"string\" }" |
| 120 " }" |
| 121 "}" |
| 122 "}", &error); |
| 123 ASSERT_TRUE(schema) << error; |
| 124 |
| 125 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema->type()); |
| 126 EXPECT_FALSE(schema->GetSchemaForProperty("invalid")); |
| 127 |
| 128 const PolicySchema* sub = schema->GetSchemaForProperty("Boolean"); |
| 129 ASSERT_TRUE(sub); |
| 130 EXPECT_EQ(base::Value::TYPE_BOOLEAN, sub->type()); |
| 131 |
| 132 sub = schema->GetSchemaForProperty("Integer"); |
| 133 ASSERT_TRUE(sub); |
| 134 EXPECT_EQ(base::Value::TYPE_INTEGER, sub->type()); |
| 135 |
| 136 sub = schema->GetSchemaForProperty("Null"); |
| 137 ASSERT_TRUE(sub); |
| 138 EXPECT_EQ(base::Value::TYPE_NULL, sub->type()); |
| 139 |
| 140 sub = schema->GetSchemaForProperty("Number"); |
| 141 ASSERT_TRUE(sub); |
| 142 EXPECT_EQ(base::Value::TYPE_DOUBLE, sub->type()); |
| 143 sub = schema->GetSchemaForProperty("String"); |
| 144 ASSERT_TRUE(sub); |
| 145 EXPECT_EQ(base::Value::TYPE_STRING, sub->type()); |
| 146 |
| 147 sub = schema->GetSchemaForProperty("Array"); |
| 148 ASSERT_TRUE(sub); |
| 149 ASSERT_EQ(base::Value::TYPE_LIST, sub->type()); |
| 150 sub = sub->GetSchemaForItems(); |
| 151 ASSERT_TRUE(sub); |
| 152 EXPECT_EQ(base::Value::TYPE_STRING, sub->type()); |
| 153 |
| 154 sub = schema->GetSchemaForProperty("ArrayOfObjects"); |
| 155 ASSERT_TRUE(sub); |
| 156 ASSERT_EQ(base::Value::TYPE_LIST, sub->type()); |
| 157 sub = sub->GetSchemaForItems(); |
| 158 ASSERT_TRUE(sub); |
| 159 EXPECT_EQ(base::Value::TYPE_DICTIONARY, sub->type()); |
| 160 const PolicySchema* subsub = sub->GetSchemaForProperty("one"); |
| 161 ASSERT_TRUE(subsub); |
| 162 EXPECT_EQ(base::Value::TYPE_STRING, subsub->type()); |
| 163 subsub = sub->GetSchemaForProperty("two"); |
| 164 ASSERT_TRUE(subsub); |
| 165 EXPECT_EQ(base::Value::TYPE_INTEGER, subsub->type()); |
| 166 subsub = sub->GetSchemaForProperty("invalid"); |
| 167 EXPECT_FALSE(subsub); |
| 168 |
| 169 sub = schema->GetSchemaForProperty("ArrayOfArray"); |
| 170 ASSERT_TRUE(sub); |
| 171 ASSERT_EQ(base::Value::TYPE_LIST, sub->type()); |
| 172 sub = sub->GetSchemaForItems(); |
| 173 ASSERT_TRUE(sub); |
| 174 ASSERT_EQ(base::Value::TYPE_LIST, sub->type()); |
| 175 sub = sub->GetSchemaForItems(); |
| 176 ASSERT_TRUE(sub); |
| 177 EXPECT_EQ(base::Value::TYPE_STRING, sub->type()); |
| 178 |
| 179 sub = schema->GetSchemaForProperty("Object"); |
| 180 ASSERT_TRUE(sub); |
| 181 ASSERT_EQ(base::Value::TYPE_DICTIONARY, sub->type()); |
| 182 subsub = sub->GetSchemaForProperty("one"); |
| 183 ASSERT_TRUE(subsub); |
| 184 EXPECT_EQ(base::Value::TYPE_BOOLEAN, subsub->type()); |
| 185 subsub = sub->GetSchemaForProperty("two"); |
| 186 ASSERT_TRUE(subsub); |
| 187 EXPECT_EQ(base::Value::TYPE_INTEGER, subsub->type()); |
| 188 subsub = sub->GetSchemaForProperty("undeclared"); |
| 189 ASSERT_TRUE(subsub); |
| 190 EXPECT_EQ(base::Value::TYPE_STRING, subsub->type()); |
| 191 } |
| 192 |
| 193 } // namespace policy |
OLD | NEW |