| OLD | NEW |
| (Empty) |
| 1 // Copyright 2015 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 "base/json/json_writer.h" | |
| 6 #include "base/values.h" | |
| 7 #include "chrome/browser/android/policy/policy_manager.h" | |
| 8 #include "components/policy/core/common/schema.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 using base::DictionaryValue; | |
| 12 using base::FundamentalValue; | |
| 13 using base::ListValue; | |
| 14 using base::StringValue; | |
| 15 using base::Value; | |
| 16 | |
| 17 class PolicyManagerTest : public testing::Test { | |
| 18 public: | |
| 19 void SetUp() override { | |
| 20 const char kSchemaTemplate[] = | |
| 21 "{" | |
| 22 " \"type\": \"object\"," | |
| 23 " \"properties\": {" | |
| 24 " \"null\": { \"type\": \"null\" }," | |
| 25 " \"bool\": { \"type\": \"boolean\" }," | |
| 26 " \"int\": { \"type\": \"integer\" }," | |
| 27 " \"double\": { \"type\": \"number\" }," | |
| 28 " \"string\": { \"type\": \"string\" }," | |
| 29 " \"list\": {" | |
| 30 " \"type\": \"array\"," | |
| 31 " \"items\": { \"type\": \"string\"}" | |
| 32 " }," | |
| 33 " \"dict\": { \"type\": \"object\" }" | |
| 34 " }" | |
| 35 "}"; | |
| 36 | |
| 37 std::string error; | |
| 38 schema_ = policy::Schema::Parse(kSchemaTemplate, &error); | |
| 39 ASSERT_TRUE(schema_.valid()) << error; | |
| 40 } | |
| 41 | |
| 42 protected: | |
| 43 // Converts the passed in value to the passed in schema, and serializes the | |
| 44 // result to JSON, to make it easier to compare with EXPECT_EQ. | |
| 45 std::string Convert(Value* value, const policy::Schema& value_schema) { | |
| 46 scoped_ptr<Value> converted_value( | |
| 47 PolicyManager::ConvertValueToSchema(value, value_schema)); | |
| 48 | |
| 49 std::string json_string; | |
| 50 EXPECT_TRUE( | |
| 51 base::JSONWriter::Write(*(converted_value.get()), &json_string)); | |
| 52 return json_string; | |
| 53 } | |
| 54 | |
| 55 policy::Schema schema_; | |
| 56 }; | |
| 57 | |
| 58 TEST_F(PolicyManagerTest, ConvertToNullValue) { | |
| 59 policy::Schema null_schema = schema_.GetKnownProperty("null"); | |
| 60 ASSERT_TRUE(null_schema.valid()); | |
| 61 | |
| 62 EXPECT_EQ("null", Convert(new StringValue("foo"), null_schema)); | |
| 63 } | |
| 64 | |
| 65 TEST_F(PolicyManagerTest, ConvertToBoolValue) { | |
| 66 policy::Schema bool_schema = schema_.GetKnownProperty("bool"); | |
| 67 ASSERT_TRUE(bool_schema.valid()); | |
| 68 | |
| 69 EXPECT_EQ("true", Convert(new FundamentalValue(true), bool_schema)); | |
| 70 EXPECT_EQ("false", Convert(new FundamentalValue(false), bool_schema)); | |
| 71 EXPECT_EQ("true", Convert(new StringValue("true"), bool_schema)); | |
| 72 EXPECT_EQ("false", Convert(new StringValue("false"), bool_schema)); | |
| 73 EXPECT_EQ("\"narf\"", Convert(new StringValue("narf"), bool_schema)); | |
| 74 EXPECT_EQ("false", Convert(new FundamentalValue(0), bool_schema)); | |
| 75 EXPECT_EQ("true", Convert(new FundamentalValue(1), bool_schema)); | |
| 76 EXPECT_EQ("true", Convert(new FundamentalValue(42), bool_schema)); | |
| 77 EXPECT_EQ("true", Convert(new FundamentalValue(-1), bool_schema)); | |
| 78 EXPECT_EQ("\"1\"", Convert(new StringValue("1"), bool_schema)); | |
| 79 EXPECT_EQ("{}", Convert(new DictionaryValue(), bool_schema)); | |
| 80 } | |
| 81 | |
| 82 TEST_F(PolicyManagerTest, ConvertToIntValue) { | |
| 83 policy::Schema int_schema = schema_.GetKnownProperty("int"); | |
| 84 ASSERT_TRUE(int_schema.valid()); | |
| 85 | |
| 86 EXPECT_EQ("23", Convert(new FundamentalValue(23), int_schema)); | |
| 87 EXPECT_EQ("42", Convert(new StringValue("42"), int_schema)); | |
| 88 EXPECT_EQ("-1", Convert(new StringValue("-1"), int_schema)); | |
| 89 EXPECT_EQ("\"poit\"", Convert(new StringValue("poit"), int_schema)); | |
| 90 EXPECT_EQ("false", Convert(new FundamentalValue(false), int_schema)); | |
| 91 } | |
| 92 | |
| 93 TEST_F(PolicyManagerTest, ConvertToDoubleValue) { | |
| 94 policy::Schema double_schema = schema_.GetKnownProperty("double"); | |
| 95 ASSERT_TRUE(double_schema.valid()); | |
| 96 | |
| 97 EXPECT_EQ("3", Convert(new FundamentalValue(3), double_schema)); | |
| 98 EXPECT_EQ("3.14", Convert(new FundamentalValue(3.14), double_schema)); | |
| 99 EXPECT_EQ("2.71", Convert(new StringValue("2.71"), double_schema)); | |
| 100 EXPECT_EQ("\"zort\"", Convert(new StringValue("zort"), double_schema)); | |
| 101 EXPECT_EQ("true", Convert(new FundamentalValue(true), double_schema)); | |
| 102 } | |
| 103 | |
| 104 TEST_F(PolicyManagerTest, ConvertToStringValue) { | |
| 105 policy::Schema string_schema = schema_.GetKnownProperty("string"); | |
| 106 ASSERT_TRUE(string_schema.valid()); | |
| 107 | |
| 108 EXPECT_EQ("\"troz\"", Convert(new StringValue("troz"), string_schema)); | |
| 109 EXPECT_EQ("4711", Convert(new FundamentalValue(4711), string_schema)); | |
| 110 } | |
| 111 | |
| 112 TEST_F(PolicyManagerTest, ConvertToListValue) { | |
| 113 policy::Schema list_schema = schema_.GetKnownProperty("list"); | |
| 114 ASSERT_TRUE(list_schema.valid()); | |
| 115 | |
| 116 ListValue* list = new ListValue; | |
| 117 list->AppendString("foo"); | |
| 118 list->AppendString("bar"); | |
| 119 EXPECT_EQ("[\"foo\",\"bar\"]", Convert(list, list_schema)); | |
| 120 EXPECT_EQ("[\"baz\",\"blurp\"]", | |
| 121 Convert(new StringValue("[\"baz\", \"blurp\"]"), list_schema)); | |
| 122 EXPECT_EQ("\"hurz\"", Convert(new StringValue("hurz"), list_schema)); | |
| 123 EXPECT_EQ("19", Convert(new FundamentalValue(19), list_schema)); | |
| 124 } | |
| 125 | |
| 126 TEST_F(PolicyManagerTest, ConvertToDictionaryValue) { | |
| 127 policy::Schema dict_schema = schema_.GetKnownProperty("dict"); | |
| 128 ASSERT_TRUE(dict_schema.valid()); | |
| 129 | |
| 130 DictionaryValue* dict = new DictionaryValue; | |
| 131 dict->SetInteger("thx", 1138); | |
| 132 EXPECT_EQ("{\"thx\":1138}", Convert(dict, dict_schema)); | |
| 133 EXPECT_EQ("{\"moose\":true}", | |
| 134 Convert(new StringValue("{\"moose\": true}"), dict_schema)); | |
| 135 EXPECT_EQ("\"fnord\"", Convert(new StringValue("fnord"), dict_schema)); | |
| 136 EXPECT_EQ("1729", Convert(new FundamentalValue(1729), dict_schema)); | |
| 137 } | |
| OLD | NEW |