Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 30 matching lines...) Expand all Loading... | |
| 41 " \"items\": { \"type\": \"string\" }" | 41 " \"items\": { \"type\": \"string\" }" |
| 42 " }" | 42 " }" |
| 43 " }," | 43 " }," |
| 44 " \"Object\": {" | 44 " \"Object\": {" |
| 45 " \"type\": \"object\"," | 45 " \"type\": \"object\"," |
| 46 " \"properties\": {" | 46 " \"properties\": {" |
| 47 " \"one\": { \"type\": \"boolean\" }," | 47 " \"one\": { \"type\": \"boolean\" }," |
| 48 " \"two\": { \"type\": \"integer\" }" | 48 " \"two\": { \"type\": \"integer\" }" |
| 49 " }," | 49 " }," |
| 50 " \"additionalProperties\": { \"type\": \"string\" }" | 50 " \"additionalProperties\": { \"type\": \"string\" }" |
| 51 " }" | 51 " }" |
|
Joao da Silva
2014/01/20 10:49:10
Add integer with enum, integer with range and stri
binjin
2014/01/20 14:09:23
I prefer to leave this to the coming CL as work on
| |
| 52 " }" | 52 " }" |
| 53 "}"; | 53 "}"; |
| 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) { | |
|
Joao da Silva
2014/01/20 10:49:10
SchemaObjectWrapper
Also, put the & next to the t
binjin
2014/01/20 14:09:23
Done.
| |
| 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 Loading... | |
| 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 | |
|
Joao da Silva
2014/01/20 10:49:10
Leave a comma after the last }
binjin
2014/01/20 14:09:23
Done.
| |
| 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 // Properties 0 to 5 (exclusive) are known, from kPropertyNodes. |
| 338 // SchemaNode offset 6 is for additionalProperties (list of lists). | 353 // SchemaNode offset 6 is for additionalProperties (list of lists). |
| 339 { 0, 5, 6 }, | 354 { 0, 8, 6 }, |
|
Joao da Silva
2014/01/20 10:49:10
Update the comment: 0 to 8 (exclusive) are the kno
binjin
2014/01/20 14:09:23
Done.
| |
| 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"] | |
|
Joao da Silva
2014/01/20 10:49:10
Comma after last element
binjin
2014/01/20 14:09:23
Done.
| |
| 361 }; | |
| 362 | |
| 363 const int kIntEnums[] = {1, 2, 3}; | |
| 364 | |
| 365 const char* kStringEnums[] = { | |
| 366 "one", | |
| 367 "two", | |
| 368 "three" | |
|
Joao da Silva
2014/01/20 10:49:10
Comma after last element
binjin
2014/01/20 14:09:23
Done.
| |
| 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 | |
|
Joao da Silva
2014/01/20 10:49:10
Comma after last element
binjin
2014/01/20 14:09:23
Done.
| |
| 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 Loading... | |
| 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 |
| OLD | NEW |