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
3 // found in the LICENSE file.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/prefs/pref_value_map.h"
7 #include "base/values.h"
8 #include "components/policy/core/browser/policy_error_map.h"
9 #include "components/policy/core/browser/url_blacklist_policy_handler.h"
10 #include "components/policy/core/common/policy_map.h"
11 #include "components/policy/core/common/policy_pref_names.h"
12 #include "policy/policy_constants.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 // Note: this file should move to components/policy/core/browser, but the
16 // components_unittests runner does not load the ResourceBundle as
17 // ChromeTestSuite::Initialize does, which leads to failures using
24 const char kTestDisabledScheme
[] = "kTestDisabledScheme";
25 const char kTestBlacklistValue
[] = "kTestBlacklistValue";
29 class URLBlacklistPolicyHandlerTest
: public testing::Test
{
31 void SetPolicy(const std::string
& key
, base::Value
* value
) {
32 policies_
.Set(key
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
, value
, NULL
);
34 bool CheckPolicy(const std::string
& key
, base::Value
* value
) {
35 SetPolicy(key
, value
);
36 return handler_
.CheckPolicySettings(policies_
, &errors_
);
38 void ApplyPolicies() {
39 handler_
.ApplyPolicySettings(policies_
, &prefs_
);
42 URLBlacklistPolicyHandler handler_
;
43 PolicyErrorMap errors_
;
48 TEST_F(URLBlacklistPolicyHandlerTest
,
49 CheckPolicySettings_DisabledSchemesUnspecified
) {
50 EXPECT_TRUE(CheckPolicy(key::kURLBlacklist
, new base::ListValue
));
51 EXPECT_EQ(0U, errors_
.size());
54 TEST_F(URLBlacklistPolicyHandlerTest
,
55 CheckPolicySettings_URLBlacklistUnspecified
) {
56 EXPECT_TRUE(CheckPolicy(key::kDisabledSchemes
, new base::ListValue
));
57 EXPECT_EQ(0U, errors_
.size());
60 TEST_F(URLBlacklistPolicyHandlerTest
,
61 CheckPolicySettings_DisabledSchemesWrongType
) {
62 // The policy expects a list. Give it a boolean.
63 EXPECT_TRUE(CheckPolicy(key::kDisabledSchemes
,
64 base::Value::CreateBooleanValue(false)));
65 EXPECT_EQ(1U, errors_
.size());
66 const std::string expected
= key::kDisabledSchemes
;
67 const std::string actual
= errors_
.begin()->first
;
68 EXPECT_EQ(expected
, actual
);
71 TEST_F(URLBlacklistPolicyHandlerTest
,
72 CheckPolicySettings_URLBlacklistWrongType
) {
73 // The policy expects a list. Give it a boolean.
74 EXPECT_TRUE(CheckPolicy(key::kURLBlacklist
,
75 base::Value::CreateBooleanValue(false)));
76 EXPECT_EQ(1U, errors_
.size());
77 const std::string expected
= key::kURLBlacklist
;
78 const std::string actual
= errors_
.begin()->first
;
79 EXPECT_EQ(expected
, actual
);
82 TEST_F(URLBlacklistPolicyHandlerTest
, ApplyPolicySettings_NothingSpecified
) {
84 EXPECT_FALSE(prefs_
.GetValue(policy_prefs::kUrlBlacklist
, NULL
));
87 TEST_F(URLBlacklistPolicyHandlerTest
,
88 ApplyPolicySettings_DisabledSchemesWrongType
) {
89 // The policy expects a list. Give it a boolean.
90 SetPolicy(key::kDisabledSchemes
, base::Value::CreateBooleanValue(false));
92 EXPECT_FALSE(prefs_
.GetValue(policy_prefs::kUrlBlacklist
, NULL
));
95 TEST_F(URLBlacklistPolicyHandlerTest
,
96 ApplyPolicySettings_URLBlacklistWrongType
) {
97 // The policy expects a list. Give it a boolean.
98 SetPolicy(key::kURLBlacklist
, base::Value::CreateBooleanValue(false));
100 EXPECT_FALSE(prefs_
.GetValue(policy_prefs::kUrlBlacklist
, NULL
));
103 TEST_F(URLBlacklistPolicyHandlerTest
,
104 ApplyPolicySettings_DisabledSchemesEmpty
) {
105 SetPolicy(key::kDisabledSchemes
, new base::ListValue
);
108 EXPECT_TRUE(prefs_
.GetValue(policy_prefs::kUrlBlacklist
, &out
));
109 base::ListValue
* out_list
;
110 EXPECT_TRUE(out
->GetAsList(&out_list
));
111 EXPECT_EQ(0U, out_list
->GetSize());
114 TEST_F(URLBlacklistPolicyHandlerTest
,
115 ApplyPolicySettings_URLBlacklistEmpty
) {
116 SetPolicy(key::kURLBlacklist
, new base::ListValue
);
119 EXPECT_TRUE(prefs_
.GetValue(policy_prefs::kUrlBlacklist
, &out
));
120 base::ListValue
* out_list
;
121 EXPECT_TRUE(out
->GetAsList(&out_list
));
122 EXPECT_EQ(0U, out_list
->GetSize());
125 TEST_F(URLBlacklistPolicyHandlerTest
,
126 ApplyPolicySettings_DisabledSchemesWrongElementType
) {
127 // The policy expects string-valued elements. Give it booleans.
128 scoped_ptr
<base::ListValue
> in(new base::ListValue
);
129 in
->AppendBoolean(false);
130 SetPolicy(key::kDisabledSchemes
, in
.release());
133 // The element should be skipped.
135 EXPECT_TRUE(prefs_
.GetValue(policy_prefs::kUrlBlacklist
, &out
));
136 base::ListValue
* out_list
;
137 EXPECT_TRUE(out
->GetAsList(&out_list
));
138 EXPECT_EQ(0U, out_list
->GetSize());
141 TEST_F(URLBlacklistPolicyHandlerTest
,
142 ApplyPolicySettings_URLBlacklistWrongElementType
) {
143 // The policy expects string-valued elements. Give it booleans.
144 scoped_ptr
<base::ListValue
> in(new base::ListValue
);
145 in
->AppendBoolean(false);
146 SetPolicy(key::kURLBlacklist
, in
.release());
149 // The element should be skipped.
151 EXPECT_TRUE(prefs_
.GetValue(policy_prefs::kUrlBlacklist
, &out
));
152 base::ListValue
* out_list
;
153 EXPECT_TRUE(out
->GetAsList(&out_list
));
154 EXPECT_EQ(0U, out_list
->GetSize());
157 TEST_F(URLBlacklistPolicyHandlerTest
,
158 ApplyPolicySettings_DisabledSchemesSuccessful
) {
159 scoped_ptr
<base::ListValue
> in_disabled_schemes(new base::ListValue
);
160 in_disabled_schemes
->AppendString(kTestDisabledScheme
);
161 SetPolicy(key::kDisabledSchemes
, in_disabled_schemes
.release());
165 EXPECT_TRUE(prefs_
.GetValue(policy_prefs::kUrlBlacklist
, &out
));
166 base::ListValue
* out_list
;
167 EXPECT_TRUE(out
->GetAsList(&out_list
));
168 EXPECT_EQ(1U, out_list
->GetSize());
170 std::string out_string
;
171 EXPECT_TRUE(out_list
->GetString(0U, &out_string
));
172 EXPECT_EQ(kTestDisabledScheme
+ std::string("://*"), out_string
);
175 TEST_F(URLBlacklistPolicyHandlerTest
,
176 ApplyPolicySettings_URLBlacklistSuccessful
) {
177 scoped_ptr
<base::ListValue
> in_url_blacklist(new base::ListValue
);
178 in_url_blacklist
->AppendString(kTestBlacklistValue
);
179 SetPolicy(key::kURLBlacklist
, in_url_blacklist
.release());
183 EXPECT_TRUE(prefs_
.GetValue(policy_prefs::kUrlBlacklist
, &out
));
184 base::ListValue
* out_list
;
185 EXPECT_TRUE(out
->GetAsList(&out_list
));
186 EXPECT_EQ(1U, out_list
->GetSize());
188 std::string out_string
;
189 EXPECT_TRUE(out_list
->GetString(0U, &out_string
));
190 EXPECT_EQ(kTestBlacklistValue
, out_string
);
193 TEST_F(URLBlacklistPolicyHandlerTest
, ApplyPolicySettings_MergeSuccessful
) {
194 scoped_ptr
<base::ListValue
> in_disabled_schemes(new base::ListValue
);
195 in_disabled_schemes
->AppendString(kTestDisabledScheme
);
196 SetPolicy(key::kDisabledSchemes
, in_disabled_schemes
.release());
198 scoped_ptr
<base::ListValue
> in_url_blacklist(new base::ListValue
);
199 in_url_blacklist
->AppendString(kTestBlacklistValue
);
200 SetPolicy(key::kURLBlacklist
, in_url_blacklist
.release());
205 EXPECT_TRUE(prefs_
.GetValue(policy_prefs::kUrlBlacklist
, &out
));
206 base::ListValue
* out_list
;
207 EXPECT_TRUE(out
->GetAsList(&out_list
));
208 EXPECT_EQ(2U, out_list
->GetSize());
211 EXPECT_TRUE(out_list
->GetString(0U, &out1
));
212 EXPECT_EQ(kTestDisabledScheme
+ std::string("://*"), out1
);
215 EXPECT_TRUE(out_list
->GetString(1U, &out2
));
216 EXPECT_EQ(kTestBlacklistValue
, out2
);
219 } // namespace policy