Revert of Add source column to chrome://policy showing the origins of policies. ...
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / configuration_policy_handler_chromeos_unittest.cc
blobb654188ead171425120fd340481997569bfbd995
1 // Copyright (c) 2012 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 "chrome/browser/chromeos/policy/configuration_policy_handler_chromeos.h"
7 #include "base/callback.h"
8 #include "base/json/json_reader.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/prefs/pref_value_map.h"
11 #include "base/values.h"
12 #include "chrome/browser/ui/ash/chrome_launcher_prefs.h"
13 #include "chrome/common/pref_names.h"
14 #include "components/policy/core/browser/policy_error_map.h"
15 #include "components/policy/core/common/external_data_fetcher.h"
16 #include "components/policy/core/common/policy_map.h"
17 #include "components/policy/core/common/schema.h"
18 #include "policy/policy_constants.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 namespace policy {
23 namespace {
25 // Test cases for the screen magnifier type policy setting.
26 class ScreenMagnifierPolicyHandlerTest : public testing::Test {
27 protected:
28 PolicyMap policy_;
29 PrefValueMap prefs_;
30 ScreenMagnifierPolicyHandler handler_;
33 class LoginScreenPowerManagementPolicyHandlerTest : public testing::Test {
34 protected:
35 LoginScreenPowerManagementPolicyHandlerTest();
37 void SetUp() override;
39 Schema chrome_schema_;
41 private:
42 DISALLOW_COPY_AND_ASSIGN(LoginScreenPowerManagementPolicyHandlerTest);
45 LoginScreenPowerManagementPolicyHandlerTest::
46 LoginScreenPowerManagementPolicyHandlerTest() {
49 void LoginScreenPowerManagementPolicyHandlerTest::SetUp() {
50 chrome_schema_ = Schema::Wrap(GetChromeSchemaData());
53 TEST_F(ScreenMagnifierPolicyHandlerTest, Default) {
54 handler_.ApplyPolicySettings(policy_, &prefs_);
55 EXPECT_FALSE(
56 prefs_.GetValue(prefs::kAccessibilityScreenMagnifierEnabled, NULL));
57 EXPECT_FALSE(prefs_.GetValue(prefs::kAccessibilityScreenMagnifierType, NULL));
60 TEST_F(ScreenMagnifierPolicyHandlerTest, Disabled) {
61 policy_.Set(key::kScreenMagnifierType,
62 POLICY_LEVEL_MANDATORY,
63 POLICY_SCOPE_USER,
64 new base::FundamentalValue(0),
65 NULL);
66 handler_.ApplyPolicySettings(policy_, &prefs_);
68 const base::Value* enabled = NULL;
69 EXPECT_TRUE(
70 prefs_.GetValue(prefs::kAccessibilityScreenMagnifierEnabled, &enabled));
71 ASSERT_TRUE(enabled);
72 EXPECT_TRUE(base::FundamentalValue(false).Equals(enabled));
74 const base::Value* type = NULL;
75 EXPECT_TRUE(prefs_.GetValue(prefs::kAccessibilityScreenMagnifierType, &type));
76 ASSERT_TRUE(type);
77 EXPECT_TRUE(base::FundamentalValue(0).Equals(type));
80 TEST_F(ScreenMagnifierPolicyHandlerTest, Enabled) {
81 policy_.Set(key::kScreenMagnifierType,
82 POLICY_LEVEL_MANDATORY,
83 POLICY_SCOPE_USER,
84 new base::FundamentalValue(1),
85 NULL);
86 handler_.ApplyPolicySettings(policy_, &prefs_);
88 const base::Value* enabled = NULL;
89 EXPECT_TRUE(
90 prefs_.GetValue(prefs::kAccessibilityScreenMagnifierEnabled, &enabled));
91 ASSERT_TRUE(enabled);
92 EXPECT_TRUE(base::FundamentalValue(true).Equals(enabled));
94 const base::Value* type = NULL;
95 EXPECT_TRUE(prefs_.GetValue(prefs::kAccessibilityScreenMagnifierType, &type));
96 ASSERT_TRUE(type);
97 EXPECT_TRUE(base::FundamentalValue(1).Equals(type));
100 TEST(ExternalDataPolicyHandlerTest, Empty) {
101 PolicyErrorMap errors;
102 EXPECT_TRUE(ExternalDataPolicyHandler(key::kUserAvatarImage)
103 .CheckPolicySettings(PolicyMap(), &errors));
104 EXPECT_TRUE(errors.GetErrors(key::kUserAvatarImage).empty());
107 TEST(ExternalDataPolicyHandlerTest, WrongType) {
108 PolicyMap policy_map;
109 policy_map.Set(key::kUserAvatarImage,
110 POLICY_LEVEL_MANDATORY,
111 POLICY_SCOPE_USER,
112 new base::FundamentalValue(false),
113 NULL);
114 PolicyErrorMap errors;
115 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage)
116 .CheckPolicySettings(policy_map, &errors));
117 EXPECT_FALSE(errors.GetErrors(key::kUserAvatarImage).empty());
120 TEST(ExternalDataPolicyHandlerTest, MissingURL) {
121 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
122 dict->SetString("hash", "1234567890123456789012345678901234567890");
123 PolicyMap policy_map;
124 policy_map.Set(key::kUserAvatarImage,
125 POLICY_LEVEL_MANDATORY,
126 POLICY_SCOPE_USER,
127 dict.release(),
128 NULL);
129 PolicyErrorMap errors;
130 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage)
131 .CheckPolicySettings(policy_map, &errors));
132 EXPECT_FALSE(errors.GetErrors(key::kUserAvatarImage).empty());
135 TEST(ExternalDataPolicyHandlerTest, InvalidURL) {
136 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
137 dict->SetString("url", "http://");
138 dict->SetString("hash", "1234567890123456789012345678901234567890");
139 PolicyMap policy_map;
140 policy_map.Set(key::kUserAvatarImage,
141 POLICY_LEVEL_MANDATORY,
142 POLICY_SCOPE_USER,
143 dict.release(),
144 NULL);
145 PolicyErrorMap errors;
146 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage)
147 .CheckPolicySettings(policy_map, &errors));
148 EXPECT_FALSE(errors.GetErrors(key::kUserAvatarImage).empty());
151 TEST(ExternalDataPolicyHandlerTest, MissingHash) {
152 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
153 dict->SetString("url", "http://localhost/");
154 PolicyMap policy_map;
155 policy_map.Set(key::kUserAvatarImage,
156 POLICY_LEVEL_MANDATORY,
157 POLICY_SCOPE_USER,
158 dict.release(),
159 NULL);
160 PolicyErrorMap errors;
161 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage)
162 .CheckPolicySettings(policy_map, &errors));
163 EXPECT_FALSE(errors.GetErrors(key::kUserAvatarImage).empty());
166 TEST(ExternalDataPolicyHandlerTest, InvalidHash) {
167 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
168 dict->SetString("url", "http://localhost/");
169 dict->SetString("hash", "1234");
170 PolicyMap policy_map;
171 policy_map.Set(key::kUserAvatarImage,
172 POLICY_LEVEL_MANDATORY,
173 POLICY_SCOPE_USER,
174 dict.release(),
175 NULL);
176 PolicyErrorMap errors;
177 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage)
178 .CheckPolicySettings(policy_map, &errors));
179 EXPECT_FALSE(errors.GetErrors(key::kUserAvatarImage).empty());
182 TEST(ExternalDataPolicyHandlerTest, Valid) {
183 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
184 dict->SetString("url", "http://localhost/");
185 dict->SetString(
186 "hash",
187 "1234567890123456789012345678901234567890123456789012345678901234");
188 PolicyMap policy_map;
189 policy_map.Set(key::kUserAvatarImage,
190 POLICY_LEVEL_MANDATORY,
191 POLICY_SCOPE_USER,
192 dict.release(),
193 NULL);
194 PolicyErrorMap errors;
195 EXPECT_TRUE(ExternalDataPolicyHandler(key::kUserAvatarImage)
196 .CheckPolicySettings(policy_map, &errors));
197 EXPECT_TRUE(errors.GetErrors(key::kUserAvatarImage).empty());
200 const char kLoginScreenPowerManagementPolicy[] =
202 " \"AC\": {"
203 " \"Delays\": {"
204 " \"ScreenDim\": 5000,"
205 " \"ScreenOff\": 7000,"
206 " \"Idle\": 9000"
207 " },"
208 " \"IdleAction\": \"DoNothing\""
209 " },"
210 " \"Battery\": {"
211 " \"Delays\": {"
212 " \"ScreenDim\": 1000,"
213 " \"ScreenOff\": 3000,"
214 " \"Idle\": 4000"
215 " },"
216 " \"IdleAction\": \"DoNothing\""
217 " },"
218 " \"LidCloseAction\": \"DoNothing\","
219 " \"UserActivityScreenDimDelayScale\": 300"
220 "}";
222 } // namespace
224 TEST(NetworkConfigurationPolicyHandlerTest, Empty) {
225 PolicyMap policy_map;
226 scoped_ptr<NetworkConfigurationPolicyHandler> handler(
227 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
228 PolicyErrorMap errors;
229 EXPECT_TRUE(handler->CheckPolicySettings(policy_map, &errors));
230 EXPECT_TRUE(errors.GetErrors(key::kOpenNetworkConfiguration).empty());
233 TEST(NetworkConfigurationPolicyHandlerTest, ValidONC) {
234 const std::string kTestONC(
236 " \"NetworkConfigurations\": [{"
237 " \"GUID\": \"{485d6076-dd44-6b6d-69787465725f5045}\","
238 " \"Type\": \"WiFi\","
239 " \"Name\": \"some name\","
240 " \"WiFi\": {"
241 " \"Security\": \"WEP-PSK\","
242 " \"SSID\": \"ssid\","
243 " \"Passphrase\": \"pass\","
244 " }"
245 " }]"
246 "}");
248 PolicyMap policy_map;
249 policy_map.Set(key::kOpenNetworkConfiguration,
250 POLICY_LEVEL_MANDATORY,
251 POLICY_SCOPE_USER,
252 new base::StringValue(kTestONC),
253 NULL);
254 scoped_ptr<NetworkConfigurationPolicyHandler> handler(
255 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
256 PolicyErrorMap errors;
257 EXPECT_TRUE(handler->CheckPolicySettings(policy_map, &errors));
258 EXPECT_TRUE(errors.GetErrors(key::kOpenNetworkConfiguration).empty());
261 TEST(NetworkConfigurationPolicyHandlerTest, WrongType) {
262 PolicyMap policy_map;
263 policy_map.Set(key::kOpenNetworkConfiguration,
264 POLICY_LEVEL_MANDATORY,
265 POLICY_SCOPE_USER,
266 new base::FundamentalValue(false),
267 NULL);
268 scoped_ptr<NetworkConfigurationPolicyHandler> handler(
269 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
270 PolicyErrorMap errors;
271 EXPECT_FALSE(handler->CheckPolicySettings(policy_map, &errors));
272 EXPECT_FALSE(errors.GetErrors(key::kOpenNetworkConfiguration).empty());
275 TEST(NetworkConfigurationPolicyHandlerTest, JSONParseError) {
276 const std::string kTestONC("I'm not proper JSON!");
277 PolicyMap policy_map;
278 policy_map.Set(key::kOpenNetworkConfiguration,
279 POLICY_LEVEL_MANDATORY,
280 POLICY_SCOPE_USER,
281 new base::StringValue(kTestONC),
282 NULL);
283 scoped_ptr<NetworkConfigurationPolicyHandler> handler(
284 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
285 PolicyErrorMap errors;
286 EXPECT_FALSE(handler->CheckPolicySettings(policy_map, &errors));
287 EXPECT_FALSE(errors.GetErrors(key::kOpenNetworkConfiguration).empty());
290 TEST(NetworkConfigurationPolicyHandlerTest, Sanitization) {
291 const std::string kTestONC(
293 " \"NetworkConfigurations\": [{"
294 " \"GUID\": \"{485d6076-dd44-6b6d-69787465725f5045}\","
295 " \"Type\": \"WiFi\","
296 " \"Name\": \"some name\","
297 " \"WiFi\": {"
298 " \"Security\": \"WEP-PSK\","
299 " \"SSID\": \"ssid\","
300 " \"Passphrase\": \"pass\","
301 " }"
302 " }]"
303 "}");
305 PolicyMap policy_map;
306 policy_map.Set(key::kOpenNetworkConfiguration,
307 POLICY_LEVEL_MANDATORY,
308 POLICY_SCOPE_USER,
309 new base::StringValue(kTestONC),
310 NULL);
311 scoped_ptr<NetworkConfigurationPolicyHandler> handler(
312 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
313 PolicyErrorMap errors;
314 handler->PrepareForDisplaying(&policy_map);
315 const base::Value* sanitized =
316 policy_map.GetValue(key::kOpenNetworkConfiguration);
317 ASSERT_TRUE(sanitized);
318 std::string sanitized_onc;
319 EXPECT_TRUE(sanitized->GetAsString(&sanitized_onc));
320 EXPECT_FALSE(sanitized_onc.empty());
321 EXPECT_EQ(std::string::npos, sanitized_onc.find("pass"));
324 TEST(PinnedLauncherAppsPolicyHandler, PrefTranslation) {
325 base::ListValue list;
326 PolicyMap policy_map;
327 PrefValueMap prefs;
328 base::ListValue expected_pinned_apps;
329 base::Value* value = NULL;
330 PinnedLauncherAppsPolicyHandler handler;
332 policy_map.Set(key::kPinnedLauncherApps, POLICY_LEVEL_MANDATORY,
333 POLICY_SCOPE_USER, list.DeepCopy(), NULL);
334 handler.ApplyPolicySettings(policy_map, &prefs);
335 EXPECT_TRUE(prefs.GetValue(prefs::kPinnedLauncherApps, &value));
336 EXPECT_TRUE(base::Value::Equals(&expected_pinned_apps, value));
338 base::StringValue entry1("abcdefghijklmnopabcdefghijklmnop");
339 base::DictionaryValue* entry1_dict = new base::DictionaryValue();
340 entry1_dict->Set(ash::kPinnedAppsPrefAppIDPath, entry1.DeepCopy());
341 expected_pinned_apps.Append(entry1_dict);
342 list.Append(entry1.DeepCopy());
343 policy_map.Set(key::kPinnedLauncherApps, POLICY_LEVEL_MANDATORY,
344 POLICY_SCOPE_USER, list.DeepCopy(), NULL);
345 prefs.Clear();
346 handler.ApplyPolicySettings(policy_map, &prefs);
347 EXPECT_TRUE(prefs.GetValue(prefs::kPinnedLauncherApps, &value));
348 EXPECT_TRUE(base::Value::Equals(&expected_pinned_apps, value));
351 TEST_F(LoginScreenPowerManagementPolicyHandlerTest, Empty) {
352 PolicyMap policy_map;
353 LoginScreenPowerManagementPolicyHandler handler(chrome_schema_);
354 PolicyErrorMap errors;
355 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
356 EXPECT_TRUE(errors.GetErrors(key::kDeviceLoginScreenPowerManagement).empty());
359 TEST_F(LoginScreenPowerManagementPolicyHandlerTest, ValidPolicy) {
360 PolicyMap policy_map;
361 policy_map.Set(
362 key::kDeviceLoginScreenPowerManagement, POLICY_LEVEL_MANDATORY,
363 POLICY_SCOPE_USER,
364 base::JSONReader::Read(kLoginScreenPowerManagementPolicy).release(),
365 NULL);
366 LoginScreenPowerManagementPolicyHandler handler(chrome_schema_);
367 PolicyErrorMap errors;
368 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
369 EXPECT_TRUE(errors.GetErrors(key::kDeviceLoginScreenPowerManagement).empty());
372 TEST_F(LoginScreenPowerManagementPolicyHandlerTest, WrongType) {
373 PolicyMap policy_map;
374 policy_map.Set(key::kDeviceLoginScreenPowerManagement,
375 POLICY_LEVEL_MANDATORY,
376 POLICY_SCOPE_USER,
377 new base::FundamentalValue(false),
378 NULL);
379 LoginScreenPowerManagementPolicyHandler handler(chrome_schema_);
380 PolicyErrorMap errors;
381 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
382 EXPECT_FALSE(
383 errors.GetErrors(key::kDeviceLoginScreenPowerManagement).empty());
386 } // namespace policy