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

Side by Side Diff: components/policy/core/common/schema_unittest.cc

Issue 47513018: Make the internal storage of policy::Schemas ref counted. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@chrome-policy-schema-4-new-generate
Patch Set: rebase Created 7 years, 1 month 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 unified diff | Download patch
« no previous file with comments | « components/policy/core/common/schema.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/policy/core/common/schema.h" 5 #include "components/policy/core/common/schema.h"
6 6
7 #include "components/policy/core/common/schema_internal.h" 7 #include "components/policy/core/common/schema_internal.h"
8 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
9 9
10 namespace policy { 10 namespace policy {
11 11
12 namespace { 12 namespace {
13 13
14 #define OBJECT_TYPE "\"type\":\"object\"" 14 #define OBJECT_TYPE "\"type\":\"object\""
15 15
16 bool ParseFails(const std::string& content) { 16 bool ParseFails(const std::string& content) {
17 std::string error; 17 std::string error;
18 scoped_ptr<SchemaOwner> schema = SchemaOwner::Parse(content, &error); 18 Schema schema = Schema::Parse(content, &error);
19 if (schema) 19 if (schema.valid())
20 EXPECT_TRUE(schema->schema().valid()); 20 return false;
21 else 21 EXPECT_FALSE(error.empty());
22 EXPECT_FALSE(error.empty()); 22 return true;
23 return !schema;
24 } 23 }
25 24
26 } // namespace 25 } // namespace
27 26
28 TEST(SchemaTest, MinimalSchema) { 27 TEST(SchemaTest, MinimalSchema) {
29 EXPECT_FALSE(ParseFails( 28 EXPECT_FALSE(ParseFails(
30 "{" 29 "{"
31 OBJECT_TYPE 30 OBJECT_TYPE
32 "}")); 31 "}"));
33 } 32 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 "\"properties\": { \"Policy\": { \"type\": [\"string\", \"number\"] } }" 64 "\"properties\": { \"Policy\": { \"type\": [\"string\", \"number\"] } }"
66 "}")); 65 "}"));
67 66
68 EXPECT_TRUE(ParseFails( 67 EXPECT_TRUE(ParseFails(
69 "{" 68 "{"
70 OBJECT_TYPE "," 69 OBJECT_TYPE ","
71 "\"properties\": { \"Policy\": { \"type\": \"any\" } }" 70 "\"properties\": { \"Policy\": { \"type\": \"any\" } }"
72 "}")); 71 "}"));
73 } 72 }
74 73
74 TEST(SchemaTest, Ownership) {
75 std::string error;
76 Schema schema = Schema::Parse(
77 "{"
78 OBJECT_TYPE ","
79 "\"properties\": {"
80 "\"sub\": {"
81 "\"type\": \"object\","
82 "\"properties\": {"
83 "\"subsub\": { \"type\": \"string\" }"
84 "}"
85 "}"
86 "}"
87 "}", &error);
88 ASSERT_TRUE(schema.valid()) << error;
89 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type());
90
91 schema = schema.GetKnownProperty("sub");
92 ASSERT_TRUE(schema.valid());
93 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type());
94
95 {
96 Schema::Iterator it = schema.GetPropertiesIterator();
97 ASSERT_FALSE(it.IsAtEnd());
98 EXPECT_STREQ("subsub", it.key());
99
100 schema = it.schema();
101 it.Advance();
102 EXPECT_TRUE(it.IsAtEnd());
103 }
104
105 ASSERT_TRUE(schema.valid());
106 EXPECT_EQ(base::Value::TYPE_STRING, schema.type());
107
108 // This test shouldn't leak nor use invalid memory.
109 }
110
75 TEST(SchemaTest, ValidSchema) { 111 TEST(SchemaTest, ValidSchema) {
76 std::string error; 112 std::string error;
77 scoped_ptr<SchemaOwner> policy_schema = SchemaOwner::Parse( 113 Schema schema = Schema::Parse(
78 "{" 114 "{"
79 OBJECT_TYPE "," 115 OBJECT_TYPE ","
80 "\"properties\": {" 116 "\"properties\": {"
81 " \"Boolean\": { \"type\": \"boolean\" }," 117 " \"Boolean\": { \"type\": \"boolean\" },"
82 " \"Integer\": { \"type\": \"integer\" }," 118 " \"Integer\": { \"type\": \"integer\" },"
83 " \"Null\": { \"type\": \"null\" }," 119 " \"Null\": { \"type\": \"null\" },"
84 " \"Number\": { \"type\": \"number\" }," 120 " \"Number\": { \"type\": \"number\" },"
85 " \"String\": { \"type\": \"string\" }," 121 " \"String\": { \"type\": \"string\" },"
86 " \"Array\": {" 122 " \"Array\": {"
87 " \"type\": \"array\"," 123 " \"type\": \"array\","
(...skipping 19 matching lines...) Expand all
107 " \"Object\": {" 143 " \"Object\": {"
108 " \"type\": \"object\"," 144 " \"type\": \"object\","
109 " \"properties\": {" 145 " \"properties\": {"
110 " \"one\": { \"type\": \"boolean\" }," 146 " \"one\": { \"type\": \"boolean\" },"
111 " \"two\": { \"type\": \"integer\" }" 147 " \"two\": { \"type\": \"integer\" }"
112 " }," 148 " },"
113 " \"additionalProperties\": { \"type\": \"string\" }" 149 " \"additionalProperties\": { \"type\": \"string\" }"
114 " }" 150 " }"
115 "}" 151 "}"
116 "}", &error); 152 "}", &error);
117 ASSERT_TRUE(policy_schema) << error; 153 ASSERT_TRUE(schema.valid()) << error;
118 ASSERT_TRUE(policy_schema->schema().valid());
119 154
120 Schema schema = policy_schema->schema();
121 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); 155 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type());
122 EXPECT_FALSE(schema.GetProperty("invalid").valid()); 156 EXPECT_FALSE(schema.GetProperty("invalid").valid());
123 157
124 Schema sub = schema.GetProperty("Boolean"); 158 Schema sub = schema.GetProperty("Boolean");
125 ASSERT_TRUE(sub.valid()); 159 ASSERT_TRUE(sub.valid());
126 EXPECT_EQ(base::Value::TYPE_BOOLEAN, sub.type()); 160 EXPECT_EQ(base::Value::TYPE_BOOLEAN, sub.type());
127 161
128 sub = schema.GetProperty("Integer"); 162 sub = schema.GetProperty("Integer");
129 ASSERT_TRUE(sub.valid()); 163 ASSERT_TRUE(sub.valid());
130 EXPECT_EQ(base::Value::TYPE_INTEGER, sub.type()); 164 EXPECT_EQ(base::Value::TYPE_INTEGER, sub.type());
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 EXPECT_STREQ(kExpectedProperties[i].expected_key, it.key()); 240 EXPECT_STREQ(kExpectedProperties[i].expected_key, it.key());
207 ASSERT_TRUE(it.schema().valid()); 241 ASSERT_TRUE(it.schema().valid());
208 EXPECT_EQ(kExpectedProperties[i].expected_type, it.schema().type()); 242 EXPECT_EQ(kExpectedProperties[i].expected_type, it.schema().type());
209 it.Advance(); 243 it.Advance();
210 } 244 }
211 EXPECT_TRUE(it.IsAtEnd()); 245 EXPECT_TRUE(it.IsAtEnd());
212 } 246 }
213 247
214 TEST(SchemaTest, Lookups) { 248 TEST(SchemaTest, Lookups) {
215 std::string error; 249 std::string error;
216 scoped_ptr<SchemaOwner> policy_schema = SchemaOwner::Parse( 250
251 Schema schema = Schema::Parse(
217 "{" 252 "{"
218 OBJECT_TYPE 253 OBJECT_TYPE
219 "}", &error); 254 "}", &error);
220 ASSERT_TRUE(policy_schema) << error; 255 ASSERT_TRUE(schema.valid()) << error;
221 Schema schema = policy_schema->schema();
222 ASSERT_TRUE(schema.valid());
223 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); 256 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type());
224 257
225 // This empty schema should never find named properties. 258 // This empty schema should never find named properties.
226 EXPECT_FALSE(schema.GetKnownProperty("").valid()); 259 EXPECT_FALSE(schema.GetKnownProperty("").valid());
227 EXPECT_FALSE(schema.GetKnownProperty("xyz").valid()); 260 EXPECT_FALSE(schema.GetKnownProperty("xyz").valid());
228 EXPECT_TRUE(schema.GetPropertiesIterator().IsAtEnd()); 261 EXPECT_TRUE(schema.GetPropertiesIterator().IsAtEnd());
229 262
230 policy_schema = SchemaOwner::Parse( 263 schema = Schema::Parse(
231 "{" 264 "{"
232 OBJECT_TYPE "," 265 OBJECT_TYPE ","
233 "\"properties\": {" 266 "\"properties\": {"
234 " \"Boolean\": { \"type\": \"boolean\" }" 267 " \"Boolean\": { \"type\": \"boolean\" }"
235 "}" 268 "}"
236 "}", &error); 269 "}", &error);
237 ASSERT_TRUE(policy_schema) << error; 270 ASSERT_TRUE(schema.valid()) << error;
238 schema = policy_schema->schema();
239 ASSERT_TRUE(schema.valid());
240 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); 271 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type());
241 272
242 EXPECT_FALSE(schema.GetKnownProperty("").valid()); 273 EXPECT_FALSE(schema.GetKnownProperty("").valid());
243 EXPECT_FALSE(schema.GetKnownProperty("xyz").valid()); 274 EXPECT_FALSE(schema.GetKnownProperty("xyz").valid());
244 EXPECT_TRUE(schema.GetKnownProperty("Boolean").valid()); 275 EXPECT_TRUE(schema.GetKnownProperty("Boolean").valid());
245 276
246 policy_schema = SchemaOwner::Parse( 277 schema = Schema::Parse(
247 "{" 278 "{"
248 OBJECT_TYPE "," 279 OBJECT_TYPE ","
249 "\"properties\": {" 280 "\"properties\": {"
250 " \"bb\" : { \"type\": \"null\" }," 281 " \"bb\" : { \"type\": \"null\" },"
251 " \"aa\" : { \"type\": \"boolean\" }," 282 " \"aa\" : { \"type\": \"boolean\" },"
252 " \"abab\" : { \"type\": \"string\" }," 283 " \"abab\" : { \"type\": \"string\" },"
253 " \"ab\" : { \"type\": \"number\" }," 284 " \"ab\" : { \"type\": \"number\" },"
254 " \"aba\" : { \"type\": \"integer\" }" 285 " \"aba\" : { \"type\": \"integer\" }"
255 "}" 286 "}"
256 "}", &error); 287 "}", &error);
257 ASSERT_TRUE(policy_schema) << error; 288 ASSERT_TRUE(schema.valid()) << error;
258 schema = policy_schema->schema();
259 ASSERT_TRUE(schema.valid());
260 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); 289 ASSERT_EQ(base::Value::TYPE_DICTIONARY, schema.type());
261 290
262 EXPECT_FALSE(schema.GetKnownProperty("").valid()); 291 EXPECT_FALSE(schema.GetKnownProperty("").valid());
263 EXPECT_FALSE(schema.GetKnownProperty("xyz").valid()); 292 EXPECT_FALSE(schema.GetKnownProperty("xyz").valid());
264 293
265 struct { 294 struct {
266 const char* expected_key; 295 const char* expected_key;
267 base::Value::Type expected_type; 296 base::Value::Type expected_type;
268 } kExpectedKeys[] = { 297 } kExpectedKeys[] = {
269 { "aa", base::Value::TYPE_BOOLEAN }, 298 { "aa", base::Value::TYPE_BOOLEAN },
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 // SchemaNode offset 6 is for additionalProperties (list of lists). 332 // SchemaNode offset 6 is for additionalProperties (list of lists).
304 { 0, 5, 6 }, 333 { 0, 5, 6 },
305 }; 334 };
306 335
307 const internal::SchemaData kData = { 336 const internal::SchemaData kData = {
308 kSchemas, 337 kSchemas,
309 kPropertyNodes, 338 kPropertyNodes,
310 kProperties, 339 kProperties,
311 }; 340 };
312 341
313 scoped_ptr<SchemaOwner> policy_schema = SchemaOwner::Wrap(&kData); 342 Schema schema = Schema::Wrap(&kData);
314 ASSERT_TRUE(policy_schema);
315 Schema schema = policy_schema->schema();
316 ASSERT_TRUE(schema.valid()); 343 ASSERT_TRUE(schema.valid());
317 EXPECT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); 344 EXPECT_EQ(base::Value::TYPE_DICTIONARY, schema.type());
318 345
319 struct { 346 struct {
320 const char* key; 347 const char* key;
321 base::Value::Type type; 348 base::Value::Type type;
322 } kExpectedProperties[] = { 349 } kExpectedProperties[] = {
323 { "Boolean", base::Value::TYPE_BOOLEAN }, 350 { "Boolean", base::Value::TYPE_BOOLEAN },
324 { "Integer", base::Value::TYPE_INTEGER }, 351 { "Integer", base::Value::TYPE_INTEGER },
325 { "Number", base::Value::TYPE_DOUBLE }, 352 { "Number", base::Value::TYPE_DOUBLE },
(...skipping 25 matching lines...) Expand all
351 ASSERT_EQ(base::Value::TYPE_LIST, sub.type()); 378 ASSERT_EQ(base::Value::TYPE_LIST, sub.type());
352 Schema subsub = sub.GetItems(); 379 Schema subsub = sub.GetItems();
353 ASSERT_TRUE(subsub.valid()); 380 ASSERT_TRUE(subsub.valid());
354 ASSERT_EQ(base::Value::TYPE_LIST, subsub.type()); 381 ASSERT_EQ(base::Value::TYPE_LIST, subsub.type());
355 Schema subsubsub = subsub.GetItems(); 382 Schema subsubsub = subsub.GetItems();
356 ASSERT_TRUE(subsubsub.valid()); 383 ASSERT_TRUE(subsubsub.valid());
357 ASSERT_EQ(base::Value::TYPE_STRING, subsubsub.type()); 384 ASSERT_EQ(base::Value::TYPE_STRING, subsubsub.type());
358 } 385 }
359 386
360 } // namespace policy 387 } // namespace policy
OLDNEW
« no previous file with comments | « components/policy/core/common/schema.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698