| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/browser/url_blacklist_policy_handler.h" | 5 #include "components/policy/core/browser/url_blacklist_policy_handler.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> |
| 8 | 9 |
| 10 #include "base/memory/ptr_util.h" |
| 9 #include "base/values.h" | 11 #include "base/values.h" |
| 10 #include "components/policy/core/browser/policy_error_map.h" | 12 #include "components/policy/core/browser/policy_error_map.h" |
| 11 #include "components/policy/core/common/policy_map.h" | 13 #include "components/policy/core/common/policy_map.h" |
| 12 #include "components/policy/core/common/policy_pref_names.h" | 14 #include "components/policy/core/common/policy_pref_names.h" |
| 13 #include "components/policy/core/common/policy_types.h" | 15 #include "components/policy/core/common/policy_types.h" |
| 14 #include "components/prefs/pref_value_map.h" | 16 #include "components/prefs/pref_value_map.h" |
| 15 #include "policy/policy_constants.h" | 17 #include "policy/policy_constants.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 19 |
| 18 // Note: this file should move to components/policy/core/browser, but the | 20 // Note: this file should move to components/policy/core/browser, but the |
| 19 // components_unittests runner does not load the ResourceBundle as | 21 // components_unittests runner does not load the ResourceBundle as |
| 20 // ChromeTestSuite::Initialize does, which leads to failures using | 22 // ChromeTestSuite::Initialize does, which leads to failures using |
| 21 // PolicyErrorMap. | 23 // PolicyErrorMap. |
| 22 | 24 |
| 23 namespace policy { | 25 namespace policy { |
| 24 | 26 |
| 25 namespace { | 27 namespace { |
| 26 | 28 |
| 27 const char kTestDisabledScheme[] = "kTestDisabledScheme"; | 29 const char kTestDisabledScheme[] = "kTestDisabledScheme"; |
| 28 const char kTestBlacklistValue[] = "kTestBlacklistValue"; | 30 const char kTestBlacklistValue[] = "kTestBlacklistValue"; |
| 29 | 31 |
| 30 } // namespace | 32 } // namespace |
| 31 | 33 |
| 32 class URLBlacklistPolicyHandlerTest : public testing::Test { | 34 class URLBlacklistPolicyHandlerTest : public testing::Test { |
| 33 protected: | 35 protected: |
| 34 void SetPolicy(const std::string& key, base::Value* value) { | 36 void SetPolicy(const std::string& key, std::unique_ptr<base::Value> value) { |
| 35 policies_.Set(key, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | 37 policies_.Set(key, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, |
| 36 POLICY_SOURCE_CLOUD, value, nullptr); | 38 POLICY_SOURCE_CLOUD, std::move(value), nullptr); |
| 37 } | 39 } |
| 38 bool CheckPolicy(const std::string& key, base::Value* value) { | 40 bool CheckPolicy(const std::string& key, std::unique_ptr<base::Value> value) { |
| 39 SetPolicy(key, value); | 41 SetPolicy(key, std::move(value)); |
| 40 return handler_.CheckPolicySettings(policies_, &errors_); | 42 return handler_.CheckPolicySettings(policies_, &errors_); |
| 41 } | 43 } |
| 42 void ApplyPolicies() { | 44 void ApplyPolicies() { |
| 43 handler_.ApplyPolicySettings(policies_, &prefs_); | 45 handler_.ApplyPolicySettings(policies_, &prefs_); |
| 44 } | 46 } |
| 45 | 47 |
| 46 URLBlacklistPolicyHandler handler_; | 48 URLBlacklistPolicyHandler handler_; |
| 47 PolicyErrorMap errors_; | 49 PolicyErrorMap errors_; |
| 48 PolicyMap policies_; | 50 PolicyMap policies_; |
| 49 PrefValueMap prefs_; | 51 PrefValueMap prefs_; |
| 50 }; | 52 }; |
| 51 | 53 |
| 52 TEST_F(URLBlacklistPolicyHandlerTest, | 54 TEST_F(URLBlacklistPolicyHandlerTest, |
| 53 CheckPolicySettings_DisabledSchemesUnspecified) { | 55 CheckPolicySettings_DisabledSchemesUnspecified) { |
| 54 EXPECT_TRUE(CheckPolicy(key::kURLBlacklist, new base::ListValue)); | 56 EXPECT_TRUE( |
| 57 CheckPolicy(key::kURLBlacklist, base::WrapUnique(new base::ListValue))); |
| 55 EXPECT_EQ(0U, errors_.size()); | 58 EXPECT_EQ(0U, errors_.size()); |
| 56 } | 59 } |
| 57 | 60 |
| 58 TEST_F(URLBlacklistPolicyHandlerTest, | 61 TEST_F(URLBlacklistPolicyHandlerTest, |
| 59 CheckPolicySettings_URLBlacklistUnspecified) { | 62 CheckPolicySettings_URLBlacklistUnspecified) { |
| 60 EXPECT_TRUE(CheckPolicy(key::kDisabledSchemes, new base::ListValue)); | 63 EXPECT_TRUE(CheckPolicy(key::kDisabledSchemes, |
| 64 base::WrapUnique(new base::ListValue))); |
| 61 EXPECT_EQ(0U, errors_.size()); | 65 EXPECT_EQ(0U, errors_.size()); |
| 62 } | 66 } |
| 63 | 67 |
| 64 TEST_F(URLBlacklistPolicyHandlerTest, | 68 TEST_F(URLBlacklistPolicyHandlerTest, |
| 65 CheckPolicySettings_DisabledSchemesWrongType) { | 69 CheckPolicySettings_DisabledSchemesWrongType) { |
| 66 // The policy expects a list. Give it a boolean. | 70 // The policy expects a list. Give it a boolean. |
| 67 EXPECT_TRUE( | 71 EXPECT_TRUE(CheckPolicy(key::kDisabledSchemes, |
| 68 CheckPolicy(key::kDisabledSchemes, new base::FundamentalValue(false))); | 72 base::WrapUnique(new base::FundamentalValue(false)))); |
| 69 EXPECT_EQ(1U, errors_.size()); | 73 EXPECT_EQ(1U, errors_.size()); |
| 70 const std::string expected = key::kDisabledSchemes; | 74 const std::string expected = key::kDisabledSchemes; |
| 71 const std::string actual = errors_.begin()->first; | 75 const std::string actual = errors_.begin()->first; |
| 72 EXPECT_EQ(expected, actual); | 76 EXPECT_EQ(expected, actual); |
| 73 } | 77 } |
| 74 | 78 |
| 75 TEST_F(URLBlacklistPolicyHandlerTest, | 79 TEST_F(URLBlacklistPolicyHandlerTest, |
| 76 CheckPolicySettings_URLBlacklistWrongType) { | 80 CheckPolicySettings_URLBlacklistWrongType) { |
| 77 // The policy expects a list. Give it a boolean. | 81 // The policy expects a list. Give it a boolean. |
| 78 EXPECT_TRUE( | 82 EXPECT_TRUE(CheckPolicy(key::kURLBlacklist, |
| 79 CheckPolicy(key::kURLBlacklist, new base::FundamentalValue(false))); | 83 base::WrapUnique(new base::FundamentalValue(false)))); |
| 80 EXPECT_EQ(1U, errors_.size()); | 84 EXPECT_EQ(1U, errors_.size()); |
| 81 const std::string expected = key::kURLBlacklist; | 85 const std::string expected = key::kURLBlacklist; |
| 82 const std::string actual = errors_.begin()->first; | 86 const std::string actual = errors_.begin()->first; |
| 83 EXPECT_EQ(expected, actual); | 87 EXPECT_EQ(expected, actual); |
| 84 } | 88 } |
| 85 | 89 |
| 86 TEST_F(URLBlacklistPolicyHandlerTest, ApplyPolicySettings_NothingSpecified) { | 90 TEST_F(URLBlacklistPolicyHandlerTest, ApplyPolicySettings_NothingSpecified) { |
| 87 ApplyPolicies(); | 91 ApplyPolicies(); |
| 88 EXPECT_FALSE(prefs_.GetValue(policy_prefs::kUrlBlacklist, NULL)); | 92 EXPECT_FALSE(prefs_.GetValue(policy_prefs::kUrlBlacklist, NULL)); |
| 89 } | 93 } |
| 90 | 94 |
| 91 TEST_F(URLBlacklistPolicyHandlerTest, | 95 TEST_F(URLBlacklistPolicyHandlerTest, |
| 92 ApplyPolicySettings_DisabledSchemesWrongType) { | 96 ApplyPolicySettings_DisabledSchemesWrongType) { |
| 93 // The policy expects a list. Give it a boolean. | 97 // The policy expects a list. Give it a boolean. |
| 94 SetPolicy(key::kDisabledSchemes, new base::FundamentalValue(false)); | 98 SetPolicy(key::kDisabledSchemes, |
| 99 base::WrapUnique(new base::FundamentalValue(false))); |
| 95 ApplyPolicies(); | 100 ApplyPolicies(); |
| 96 EXPECT_FALSE(prefs_.GetValue(policy_prefs::kUrlBlacklist, NULL)); | 101 EXPECT_FALSE(prefs_.GetValue(policy_prefs::kUrlBlacklist, NULL)); |
| 97 } | 102 } |
| 98 | 103 |
| 99 TEST_F(URLBlacklistPolicyHandlerTest, | 104 TEST_F(URLBlacklistPolicyHandlerTest, |
| 100 ApplyPolicySettings_URLBlacklistWrongType) { | 105 ApplyPolicySettings_URLBlacklistWrongType) { |
| 101 // The policy expects a list. Give it a boolean. | 106 // The policy expects a list. Give it a boolean. |
| 102 SetPolicy(key::kURLBlacklist, new base::FundamentalValue(false)); | 107 SetPolicy(key::kURLBlacklist, |
| 108 base::WrapUnique(new base::FundamentalValue(false))); |
| 103 ApplyPolicies(); | 109 ApplyPolicies(); |
| 104 EXPECT_FALSE(prefs_.GetValue(policy_prefs::kUrlBlacklist, NULL)); | 110 EXPECT_FALSE(prefs_.GetValue(policy_prefs::kUrlBlacklist, NULL)); |
| 105 } | 111 } |
| 106 | 112 |
| 107 TEST_F(URLBlacklistPolicyHandlerTest, | 113 TEST_F(URLBlacklistPolicyHandlerTest, |
| 108 ApplyPolicySettings_DisabledSchemesEmpty) { | 114 ApplyPolicySettings_DisabledSchemesEmpty) { |
| 109 SetPolicy(key::kDisabledSchemes, new base::ListValue); | 115 SetPolicy(key::kDisabledSchemes, base::WrapUnique(new base::ListValue)); |
| 110 ApplyPolicies(); | 116 ApplyPolicies(); |
| 111 base::Value* out; | 117 base::Value* out; |
| 112 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); | 118 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); |
| 113 base::ListValue* out_list; | 119 base::ListValue* out_list; |
| 114 EXPECT_TRUE(out->GetAsList(&out_list)); | 120 EXPECT_TRUE(out->GetAsList(&out_list)); |
| 115 EXPECT_EQ(0U, out_list->GetSize()); | 121 EXPECT_EQ(0U, out_list->GetSize()); |
| 116 } | 122 } |
| 117 | 123 |
| 118 TEST_F(URLBlacklistPolicyHandlerTest, | 124 TEST_F(URLBlacklistPolicyHandlerTest, |
| 119 ApplyPolicySettings_URLBlacklistEmpty) { | 125 ApplyPolicySettings_URLBlacklistEmpty) { |
| 120 SetPolicy(key::kURLBlacklist, new base::ListValue); | 126 SetPolicy(key::kURLBlacklist, base::WrapUnique(new base::ListValue)); |
| 121 ApplyPolicies(); | 127 ApplyPolicies(); |
| 122 base::Value* out; | 128 base::Value* out; |
| 123 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); | 129 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); |
| 124 base::ListValue* out_list; | 130 base::ListValue* out_list; |
| 125 EXPECT_TRUE(out->GetAsList(&out_list)); | 131 EXPECT_TRUE(out->GetAsList(&out_list)); |
| 126 EXPECT_EQ(0U, out_list->GetSize()); | 132 EXPECT_EQ(0U, out_list->GetSize()); |
| 127 } | 133 } |
| 128 | 134 |
| 129 TEST_F(URLBlacklistPolicyHandlerTest, | 135 TEST_F(URLBlacklistPolicyHandlerTest, |
| 130 ApplyPolicySettings_DisabledSchemesWrongElementType) { | 136 ApplyPolicySettings_DisabledSchemesWrongElementType) { |
| 131 // The policy expects string-valued elements. Give it booleans. | 137 // The policy expects string-valued elements. Give it booleans. |
| 132 std::unique_ptr<base::ListValue> in(new base::ListValue); | 138 std::unique_ptr<base::ListValue> in(new base::ListValue); |
| 133 in->AppendBoolean(false); | 139 in->AppendBoolean(false); |
| 134 SetPolicy(key::kDisabledSchemes, in.release()); | 140 SetPolicy(key::kDisabledSchemes, std::move(in)); |
| 135 ApplyPolicies(); | 141 ApplyPolicies(); |
| 136 | 142 |
| 137 // The element should be skipped. | 143 // The element should be skipped. |
| 138 base::Value* out; | 144 base::Value* out; |
| 139 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); | 145 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); |
| 140 base::ListValue* out_list; | 146 base::ListValue* out_list; |
| 141 EXPECT_TRUE(out->GetAsList(&out_list)); | 147 EXPECT_TRUE(out->GetAsList(&out_list)); |
| 142 EXPECT_EQ(0U, out_list->GetSize()); | 148 EXPECT_EQ(0U, out_list->GetSize()); |
| 143 } | 149 } |
| 144 | 150 |
| 145 TEST_F(URLBlacklistPolicyHandlerTest, | 151 TEST_F(URLBlacklistPolicyHandlerTest, |
| 146 ApplyPolicySettings_URLBlacklistWrongElementType) { | 152 ApplyPolicySettings_URLBlacklistWrongElementType) { |
| 147 // The policy expects string-valued elements. Give it booleans. | 153 // The policy expects string-valued elements. Give it booleans. |
| 148 std::unique_ptr<base::ListValue> in(new base::ListValue); | 154 std::unique_ptr<base::ListValue> in(new base::ListValue); |
| 149 in->AppendBoolean(false); | 155 in->AppendBoolean(false); |
| 150 SetPolicy(key::kURLBlacklist, in.release()); | 156 SetPolicy(key::kURLBlacklist, std::move(in)); |
| 151 ApplyPolicies(); | 157 ApplyPolicies(); |
| 152 | 158 |
| 153 // The element should be skipped. | 159 // The element should be skipped. |
| 154 base::Value* out; | 160 base::Value* out; |
| 155 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); | 161 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); |
| 156 base::ListValue* out_list; | 162 base::ListValue* out_list; |
| 157 EXPECT_TRUE(out->GetAsList(&out_list)); | 163 EXPECT_TRUE(out->GetAsList(&out_list)); |
| 158 EXPECT_EQ(0U, out_list->GetSize()); | 164 EXPECT_EQ(0U, out_list->GetSize()); |
| 159 } | 165 } |
| 160 | 166 |
| 161 TEST_F(URLBlacklistPolicyHandlerTest, | 167 TEST_F(URLBlacklistPolicyHandlerTest, |
| 162 ApplyPolicySettings_DisabledSchemesSuccessful) { | 168 ApplyPolicySettings_DisabledSchemesSuccessful) { |
| 163 std::unique_ptr<base::ListValue> in_disabled_schemes(new base::ListValue); | 169 std::unique_ptr<base::ListValue> in_disabled_schemes(new base::ListValue); |
| 164 in_disabled_schemes->AppendString(kTestDisabledScheme); | 170 in_disabled_schemes->AppendString(kTestDisabledScheme); |
| 165 SetPolicy(key::kDisabledSchemes, in_disabled_schemes.release()); | 171 SetPolicy(key::kDisabledSchemes, std::move(in_disabled_schemes)); |
| 166 ApplyPolicies(); | 172 ApplyPolicies(); |
| 167 | 173 |
| 168 base::Value* out; | 174 base::Value* out; |
| 169 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); | 175 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); |
| 170 base::ListValue* out_list; | 176 base::ListValue* out_list; |
| 171 EXPECT_TRUE(out->GetAsList(&out_list)); | 177 EXPECT_TRUE(out->GetAsList(&out_list)); |
| 172 EXPECT_EQ(1U, out_list->GetSize()); | 178 EXPECT_EQ(1U, out_list->GetSize()); |
| 173 | 179 |
| 174 std::string out_string; | 180 std::string out_string; |
| 175 EXPECT_TRUE(out_list->GetString(0U, &out_string)); | 181 EXPECT_TRUE(out_list->GetString(0U, &out_string)); |
| 176 EXPECT_EQ(kTestDisabledScheme + std::string("://*"), out_string); | 182 EXPECT_EQ(kTestDisabledScheme + std::string("://*"), out_string); |
| 177 } | 183 } |
| 178 | 184 |
| 179 TEST_F(URLBlacklistPolicyHandlerTest, | 185 TEST_F(URLBlacklistPolicyHandlerTest, |
| 180 ApplyPolicySettings_URLBlacklistSuccessful) { | 186 ApplyPolicySettings_URLBlacklistSuccessful) { |
| 181 std::unique_ptr<base::ListValue> in_url_blacklist(new base::ListValue); | 187 std::unique_ptr<base::ListValue> in_url_blacklist(new base::ListValue); |
| 182 in_url_blacklist->AppendString(kTestBlacklistValue); | 188 in_url_blacklist->AppendString(kTestBlacklistValue); |
| 183 SetPolicy(key::kURLBlacklist, in_url_blacklist.release()); | 189 SetPolicy(key::kURLBlacklist, std::move(in_url_blacklist)); |
| 184 ApplyPolicies(); | 190 ApplyPolicies(); |
| 185 | 191 |
| 186 base::Value* out; | 192 base::Value* out; |
| 187 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); | 193 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); |
| 188 base::ListValue* out_list; | 194 base::ListValue* out_list; |
| 189 EXPECT_TRUE(out->GetAsList(&out_list)); | 195 EXPECT_TRUE(out->GetAsList(&out_list)); |
| 190 EXPECT_EQ(1U, out_list->GetSize()); | 196 EXPECT_EQ(1U, out_list->GetSize()); |
| 191 | 197 |
| 192 std::string out_string; | 198 std::string out_string; |
| 193 EXPECT_TRUE(out_list->GetString(0U, &out_string)); | 199 EXPECT_TRUE(out_list->GetString(0U, &out_string)); |
| 194 EXPECT_EQ(kTestBlacklistValue, out_string); | 200 EXPECT_EQ(kTestBlacklistValue, out_string); |
| 195 } | 201 } |
| 196 | 202 |
| 197 TEST_F(URLBlacklistPolicyHandlerTest, ApplyPolicySettings_MergeSuccessful) { | 203 TEST_F(URLBlacklistPolicyHandlerTest, ApplyPolicySettings_MergeSuccessful) { |
| 198 std::unique_ptr<base::ListValue> in_disabled_schemes(new base::ListValue); | 204 std::unique_ptr<base::ListValue> in_disabled_schemes(new base::ListValue); |
| 199 in_disabled_schemes->AppendString(kTestDisabledScheme); | 205 in_disabled_schemes->AppendString(kTestDisabledScheme); |
| 200 SetPolicy(key::kDisabledSchemes, in_disabled_schemes.release()); | 206 SetPolicy(key::kDisabledSchemes, std::move(in_disabled_schemes)); |
| 201 | 207 |
| 202 std::unique_ptr<base::ListValue> in_url_blacklist(new base::ListValue); | 208 std::unique_ptr<base::ListValue> in_url_blacklist(new base::ListValue); |
| 203 in_url_blacklist->AppendString(kTestBlacklistValue); | 209 in_url_blacklist->AppendString(kTestBlacklistValue); |
| 204 SetPolicy(key::kURLBlacklist, in_url_blacklist.release()); | 210 SetPolicy(key::kURLBlacklist, std::move(in_url_blacklist)); |
| 205 | 211 |
| 206 ApplyPolicies(); | 212 ApplyPolicies(); |
| 207 | 213 |
| 208 base::Value* out; | 214 base::Value* out; |
| 209 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); | 215 EXPECT_TRUE(prefs_.GetValue(policy_prefs::kUrlBlacklist, &out)); |
| 210 base::ListValue* out_list; | 216 base::ListValue* out_list; |
| 211 EXPECT_TRUE(out->GetAsList(&out_list)); | 217 EXPECT_TRUE(out->GetAsList(&out_list)); |
| 212 EXPECT_EQ(2U, out_list->GetSize()); | 218 EXPECT_EQ(2U, out_list->GetSize()); |
| 213 | 219 |
| 214 std::string out1; | 220 std::string out1; |
| 215 EXPECT_TRUE(out_list->GetString(0U, &out1)); | 221 EXPECT_TRUE(out_list->GetString(0U, &out1)); |
| 216 EXPECT_EQ(kTestDisabledScheme + std::string("://*"), out1); | 222 EXPECT_EQ(kTestDisabledScheme + std::string("://*"), out1); |
| 217 | 223 |
| 218 std::string out2; | 224 std::string out2; |
| 219 EXPECT_TRUE(out_list->GetString(1U, &out2)); | 225 EXPECT_TRUE(out_list->GetString(1U, &out2)); |
| 220 EXPECT_EQ(kTestBlacklistValue, out2); | 226 EXPECT_EQ(kTestBlacklistValue, out2); |
| 221 } | 227 } |
| 222 | 228 |
| 223 } // namespace policy | 229 } // namespace policy |
| OLD | NEW |