OLD | NEW |
| (Empty) |
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. | |
4 | |
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" | |
14 | |
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 | |
18 // PolicyErrorMap. | |
19 | |
20 namespace policy { | |
21 | |
22 namespace { | |
23 | |
24 const char kTestDisabledScheme[] = "kTestDisabledScheme"; | |
25 const char kTestBlacklistValue[] = "kTestBlacklistValue"; | |
26 | |
27 } // namespace | |
28 | |
29 class URLBlacklistPolicyHandlerTest : public testing::Test { | |
30 protected: | |
31 void SetPolicy(const std::string& key, base::Value* value) { | |
32 policies_.Set(key, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, value, NULL); | |
33 } | |
34 bool CheckPolicy(const std::string& key, base::Value* value) { | |
35 SetPolicy(key, value); | |
36 return handler_.CheckPolicySettings(policies_, &errors_); | |
37 } | |
38 void ApplyPolicies() { | |
39 handler_.ApplyPolicySettings(policies_, &prefs_); | |
40 } | |
41 | |
42 URLBlacklistPolicyHandler handler_; | |
43 PolicyErrorMap errors_; | |
44 PolicyMap policies_; | |
45 PrefValueMap prefs_; | |
46 }; | |
47 | |
48 TEST_F(URLBlacklistPolicyHandlerTest, | |
49 CheckPolicySettings_DisabledSchemesUnspecified) { | |
50 EXPECT_TRUE(CheckPolicy(key::kURLBlacklist, new base::ListValue)); | |
51 EXPECT_EQ(0U, errors_.size()); | |
52 } | |
53 | |
54 TEST_F(URLBlacklistPolicyHandlerTest, | |
55 CheckPolicySettings_URLBlacklistUnspecified) { | |
56 EXPECT_TRUE(CheckPolicy(key::kDisabledSchemes, new base::ListValue)); | |
57 EXPECT_EQ(0U, errors_.size()); | |
58 } | |
59 | |
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); | |
69 } | |
70 | |
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); | |
80 } | |
81 | |
82 TEST_F(URLBlacklistPolicyHandlerTest, ApplyPolicySettings_NothingSpecified) { | |
83 ApplyPolicies(); | |
84 EXPECT_FALSE(prefs_.GetValue(policy_prefs::kUrlBlacklist, NULL)); | |
85 } | |
86 | |
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)); | |
91 ApplyPolicies(); | |
92 EXPECT_FALSE(prefs_.GetValue(policy_prefs::kUrlBlacklist, NULL)); | |
93 } | |
94 | |
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)); | |
99 ApplyPolicies(); | |
100 EXPECT_FALSE(prefs_.GetValue(policy_prefs::kUrlBlacklist, NULL)); | |
101 } | |
102 | |
103 TEST_F(URLBlacklistPolicyHandlerTest, | |
104 ApplyPolicySettings_DisabledSchemesEmpty) { | |
105 SetPolicy(key::kDisabledSchemes, new base::ListValue); | |
106 ApplyPolicies(); | |
107 base::Value* out; | |
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()); | |
112 } | |
113 | |
114 TEST_F(URLBlacklistPolicyHandlerTest, | |
115 ApplyPolicySettings_URLBlacklistEmpty) { | |
116 SetPolicy(key::kURLBlacklist, new base::ListValue); | |
117 ApplyPolicies(); | |
118 base::Value* out; | |
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()); | |
123 } | |
124 | |
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()); | |
131 ApplyPolicies(); | |
132 | |
133 // The element should be skipped. | |
134 base::Value* out; | |
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()); | |
139 } | |
140 | |
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()); | |
147 ApplyPolicies(); | |
148 | |
149 // The element should be skipped. | |
150 base::Value* out; | |
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()); | |
155 } | |
156 | |
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()); | |
162 ApplyPolicies(); | |
163 | |
164 base::Value* out; | |
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()); | |
169 | |
170 std::string out_string; | |
171 EXPECT_TRUE(out_list->GetString(0U, &out_string)); | |
172 EXPECT_EQ(kTestDisabledScheme + std::string("://*"), out_string); | |
173 } | |
174 | |
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()); | |
180 ApplyPolicies(); | |
181 | |
182 base::Value* out; | |
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()); | |
187 | |
188 std::string out_string; | |
189 EXPECT_TRUE(out_list->GetString(0U, &out_string)); | |
190 EXPECT_EQ(kTestBlacklistValue, out_string); | |
191 } | |
192 | |
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()); | |
197 | |
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()); | |
201 | |
202 ApplyPolicies(); | |
203 | |
204 base::Value* out; | |
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()); | |
209 | |
210 std::string out1; | |
211 EXPECT_TRUE(out_list->GetString(0U, &out1)); | |
212 EXPECT_EQ(kTestDisabledScheme + std::string("://*"), out1); | |
213 | |
214 std::string out2; | |
215 EXPECT_TRUE(out_list->GetString(1U, &out2)); | |
216 EXPECT_EQ(kTestBlacklistValue, out2); | |
217 } | |
218 | |
219 } // namespace policy | |
OLD | NEW |