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

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

Issue 138003003: Add additional restriction to policy schema internal (part1) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more fixes Created 6 years, 11 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 unified diff | Download patch
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 {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 54
55 bool ParseFails(const std::string& content) { 55 bool ParseFails(const std::string& content) {
56 std::string error; 56 std::string error;
57 Schema schema = Schema::Parse(content, &error); 57 Schema schema = Schema::Parse(content, &error);
58 if (schema.valid()) 58 if (schema.valid())
59 return false; 59 return false;
60 EXPECT_FALSE(error.empty()); 60 EXPECT_FALSE(error.empty());
61 return true; 61 return true;
62 } 62 }
63 63
64 std::string SchemaObjectWrapper(const std::string& subschema) {
65 return "{"
66 " \"type\": \"object\","
67 " \"properties\": {"
68 " \"SomePropertyName\":" + subschema +
69 " }"
70 "}";
71 }
72
64 } // namespace 73 } // namespace
65 74
66 TEST(SchemaTest, MinimalSchema) { 75 TEST(SchemaTest, MinimalSchema) {
67 EXPECT_FALSE(ParseFails("{ \"type\": \"object\" }")); 76 EXPECT_FALSE(ParseFails("{ \"type\": \"object\" }"));
68 } 77 }
69 78
70 TEST(SchemaTest, InvalidSchemas) { 79 TEST(SchemaTest, InvalidSchemas) {
71 EXPECT_TRUE(ParseFails("")); 80 EXPECT_TRUE(ParseFails(""));
72 EXPECT_TRUE(ParseFails("omg")); 81 EXPECT_TRUE(ParseFails("omg"));
73 EXPECT_TRUE(ParseFails("\"omg\"")); 82 EXPECT_TRUE(ParseFails("\"omg\""));
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 325
317 TEST(SchemaTest, Wrap) { 326 TEST(SchemaTest, Wrap) {
318 const internal::SchemaNode kSchemas[] = { 327 const internal::SchemaNode kSchemas[] = {
319 { base::Value::TYPE_DICTIONARY, 0 }, // 0: root node 328 { base::Value::TYPE_DICTIONARY, 0 }, // 0: root node
320 { base::Value::TYPE_BOOLEAN, -1 }, // 1 329 { base::Value::TYPE_BOOLEAN, -1 }, // 1
321 { base::Value::TYPE_INTEGER, -1 }, // 2 330 { base::Value::TYPE_INTEGER, -1 }, // 2
322 { base::Value::TYPE_DOUBLE, -1 }, // 3 331 { base::Value::TYPE_DOUBLE, -1 }, // 3
323 { base::Value::TYPE_STRING, -1 }, // 4 332 { base::Value::TYPE_STRING, -1 }, // 4
324 { base::Value::TYPE_LIST, 4 }, // 5: list of strings. 333 { base::Value::TYPE_LIST, 4 }, // 5: list of strings.
325 { base::Value::TYPE_LIST, 5 }, // 6: list of lists of strings. 334 { base::Value::TYPE_LIST, 5 }, // 6: list of lists of strings.
335 { base::Value::TYPE_INTEGER, 0 }, // 7: integer enumerations.
336 { base::Value::TYPE_INTEGER, 1 }, // 8: ranged integers.
337 { base::Value::TYPE_STRING, 2 }, // 9: string enumerations.
326 }; 338 };
327 339
328 const internal::PropertyNode kPropertyNodes[] = { 340 const internal::PropertyNode kPropertyNodes[] = {
329 { "Boolean", 1 }, 341 { "Boolean", 1 }, // 0
330 { "Integer", 2 }, 342 { "Integer", 2 }, // 1
331 { "Number", 3 }, 343 { "Number", 3 }, // 2
332 { "String", 4 }, 344 { "String", 4 }, // 3
333 { "List", 5 }, 345 { "List", 5 }, // 4
346 { "IntEnum", 7 }, // 5
347 { "RangedInt", 8 }, // 6
348 { "StrEnum", 9 }, // 7
334 }; 349 };
335 350
336 const internal::PropertiesNode kProperties[] = { 351 const internal::PropertiesNode kProperties[] = {
337 // Properties 0 to 5 (exclusive) are known, from kPropertyNodes. 352 // 0 to 8 (exclusive) are the known properties in kPropertyNodes, and 6 is
338 // SchemaNode offset 6 is for additionalProperties (list of lists). 353 // the addionalProperties node.
339 { 0, 5, 6 }, 354 { 0, 8, 6 },
355 };
356
357 const internal::RestrictionNode kRestriction[] = {
358 {{0, 3}}, // [1, 2, 3]
359 {{5, 1}}, // minimum = 1, maximum = 5
360 {{0, 3}}, // ["one", "two", "three"]
361 };
362
363 const int kIntEnums[] = {1, 2, 3};
364
365 const char* kStringEnums[] = {
366 "one",
367 "two",
368 "three",
340 }; 369 };
341 370
342 const internal::SchemaData kData = { 371 const internal::SchemaData kData = {
343 kSchemas, 372 kSchemas,
344 kPropertyNodes, 373 kPropertyNodes,
345 kProperties, 374 kProperties,
375 kRestriction,
376 kIntEnums,
377 kStringEnums,
346 }; 378 };
347 379
348 Schema schema = Schema::Wrap(&kData); 380 Schema schema = Schema::Wrap(&kData);
349 ASSERT_TRUE(schema.valid()); 381 ASSERT_TRUE(schema.valid());
350 EXPECT_EQ(base::Value::TYPE_DICTIONARY, schema.type()); 382 EXPECT_EQ(base::Value::TYPE_DICTIONARY, schema.type());
351 383
352 struct { 384 struct {
353 const char* key; 385 const char* key;
354 base::Value::Type type; 386 base::Value::Type type;
355 } kExpectedProperties[] = { 387 } kExpectedProperties[] = {
356 { "Boolean", base::Value::TYPE_BOOLEAN }, 388 { "Boolean", base::Value::TYPE_BOOLEAN },
357 { "Integer", base::Value::TYPE_INTEGER }, 389 { "Integer", base::Value::TYPE_INTEGER },
358 { "Number", base::Value::TYPE_DOUBLE }, 390 { "Number", base::Value::TYPE_DOUBLE },
359 { "String", base::Value::TYPE_STRING }, 391 { "String", base::Value::TYPE_STRING },
360 { "List", base::Value::TYPE_LIST }, 392 { "List", base::Value::TYPE_LIST },
393 { "IntEnum", base::Value::TYPE_INTEGER },
394 { "RangedInt", base::Value::TYPE_INTEGER },
395 { "StrEnum", base::Value::TYPE_STRING }
361 }; 396 };
362 397
363 Schema::Iterator it = schema.GetPropertiesIterator(); 398 Schema::Iterator it = schema.GetPropertiesIterator();
364 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) { 399 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExpectedProperties); ++i) {
365 ASSERT_FALSE(it.IsAtEnd()); 400 ASSERT_FALSE(it.IsAtEnd());
366 EXPECT_STREQ(kExpectedProperties[i].key, it.key()); 401 EXPECT_STREQ(kExpectedProperties[i].key, it.key());
367 Schema sub = it.schema(); 402 Schema sub = it.schema();
368 ASSERT_TRUE(sub.valid()); 403 ASSERT_TRUE(sub.valid());
369 EXPECT_EQ(kExpectedProperties[i].type, sub.type()); 404 EXPECT_EQ(kExpectedProperties[i].type, sub.type());
370 405
371 if (sub.type() == base::Value::TYPE_LIST) { 406 if (sub.type() == base::Value::TYPE_LIST) {
372 ASSERT_EQ(base::Value::TYPE_LIST, sub.type());
373 Schema items = sub.GetItems(); 407 Schema items = sub.GetItems();
374 ASSERT_TRUE(items.valid()); 408 ASSERT_TRUE(items.valid());
375 EXPECT_EQ(base::Value::TYPE_STRING, items.type()); 409 EXPECT_EQ(base::Value::TYPE_STRING, items.type());
376 } 410 }
377 411
378 it.Advance(); 412 it.Advance();
379 } 413 }
380 EXPECT_TRUE(it.IsAtEnd()); 414 EXPECT_TRUE(it.IsAtEnd());
381 415
382 Schema sub = schema.GetAdditionalProperties(); 416 Schema sub = schema.GetAdditionalProperties();
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 709
676 Schema list = schema.GetKnownProperty("list"); 710 Schema list = schema.GetKnownProperty("list");
677 ASSERT_TRUE(list.valid()); 711 ASSERT_TRUE(list.valid());
678 ASSERT_EQ(base::Value::TYPE_LIST, list.type()); 712 ASSERT_EQ(base::Value::TYPE_LIST, list.type());
679 713
680 Schema items = list.GetItems(); 714 Schema items = list.GetItems();
681 ASSERT_TRUE(items.valid()); 715 ASSERT_TRUE(items.valid());
682 ASSERT_EQ(base::Value::TYPE_BOOLEAN, items.type()); 716 ASSERT_EQ(base::Value::TYPE_BOOLEAN, items.type());
683 } 717 }
684 718
719 TEST(SchemaTest, EnumerationRestriction) {
720 // Enum attribute is a list.
721 EXPECT_TRUE(ParseFails(SchemaObjectWrapper(
722 "{"
723 " \"type\": \"string\","
724 " \"enum\": 12"
725 "}")));
726
727 // Empty enum attributes is not allowed.
728 EXPECT_TRUE(ParseFails(SchemaObjectWrapper(
729 "{"
730 " \"type\": \"integer\","
731 " \"enum\": []"
732 "}")));
733
734 // Enum elements type should be same as stated.
735 EXPECT_TRUE(ParseFails(SchemaObjectWrapper(
736 "{"
737 " \"type\": \"string\","
738 " \"enum\": [1, 2, 3]"
739 "}")));
740
741 EXPECT_FALSE(ParseFails(SchemaObjectWrapper(
742 "{"
743 " \"type\": \"integer\","
744 " \"enum\": [1, 2, 3]"
745 "}")));
746
747 EXPECT_FALSE(ParseFails(SchemaObjectWrapper(
748 "{"
749 " \"type\": \"string\","
750 " \"enum\": [\"1\", \"2\", \"3\"]"
751 "}")));
752 }
753
754 TEST(SchemaTest, RangedRestriction) {
755 EXPECT_TRUE(ParseFails(SchemaObjectWrapper(
756 "{"
757 " \"type\": \"integer\","
758 " \"minimum\": 10,"
759 " \"maximum\": 5"
760 "}")));
761
762 EXPECT_FALSE(ParseFails(SchemaObjectWrapper(
763 "{"
764 " \"type\": \"integer\","
765 " \"minimum\": 10,"
766 " \"maximum\": 20"
767 "}")));
768 }
685 769
686 } // namespace policy 770 } // namespace policy
OLDNEW
« no previous file with comments | « components/policy/core/common/schema_internal.h ('k') | components/policy/resources/policy_templates.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698