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