Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / configuration_policy_handler_chromeos_unittest.cc
blob58934065ac0af478d7ce0f4183a5d85c1c7fecf5
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/memory/scoped_ptr.h"
9 #include "base/prefs/pref_value_map.h"
10 #include "base/values.h"
11 #include "chrome/browser/ui/ash/chrome_launcher_prefs.h"
12 #include "chrome/common/pref_names.h"
13 #include "components/policy/core/browser/policy_error_map.h"
14 #include "components/policy/core/common/external_data_fetcher.h"
15 #include "components/policy/core/common/policy_map.h"
16 #include "policy/policy_constants.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 namespace policy {
21 namespace {
23 // Test cases for the screen magnifier type policy setting.
24 class ScreenMagnifierPolicyHandlerTest : public testing::Test {
25 protected:
26 PolicyMap policy_;
27 PrefValueMap prefs_;
28 ScreenMagnifierPolicyHandler handler_;
31 TEST_F(ScreenMagnifierPolicyHandlerTest, Default) {
32 handler_.ApplyPolicySettings(policy_, &prefs_);
33 EXPECT_FALSE(prefs_.GetValue(prefs::kScreenMagnifierEnabled, NULL));
34 EXPECT_FALSE(prefs_.GetValue(prefs::kScreenMagnifierType, NULL));
37 TEST_F(ScreenMagnifierPolicyHandlerTest, Disabled) {
38 policy_.Set(key::kScreenMagnifierType,
39 POLICY_LEVEL_MANDATORY,
40 POLICY_SCOPE_USER,
41 base::Value::CreateIntegerValue(0),
42 NULL);
43 handler_.ApplyPolicySettings(policy_, &prefs_);
45 const base::Value* enabled = NULL;
46 EXPECT_TRUE(prefs_.GetValue(prefs::kScreenMagnifierEnabled, &enabled));
47 ASSERT_TRUE(enabled);
48 EXPECT_TRUE(base::FundamentalValue(false).Equals(enabled));
50 const base::Value* type = NULL;
51 EXPECT_TRUE(prefs_.GetValue(prefs::kScreenMagnifierType, &type));
52 ASSERT_TRUE(type);
53 EXPECT_TRUE(base::FundamentalValue(0).Equals(type));
56 TEST_F(ScreenMagnifierPolicyHandlerTest, Enabled) {
57 policy_.Set(key::kScreenMagnifierType,
58 POLICY_LEVEL_MANDATORY,
59 POLICY_SCOPE_USER,
60 base::Value::CreateIntegerValue(1),
61 NULL);
62 handler_.ApplyPolicySettings(policy_, &prefs_);
64 const base::Value* enabled = NULL;
65 EXPECT_TRUE(prefs_.GetValue(prefs::kScreenMagnifierEnabled, &enabled));
66 ASSERT_TRUE(enabled);
67 EXPECT_TRUE(base::FundamentalValue(true).Equals(enabled));
69 const base::Value* type = NULL;
70 EXPECT_TRUE(prefs_.GetValue(prefs::kScreenMagnifierType, &type));
71 ASSERT_TRUE(type);
72 EXPECT_TRUE(base::FundamentalValue(1).Equals(type));
75 TEST(ExternalDataPolicyHandlerTest, Empty) {
76 PolicyErrorMap errors;
77 EXPECT_TRUE(ExternalDataPolicyHandler(key::kUserAvatarImage)
78 .CheckPolicySettings(PolicyMap(), &errors));
79 EXPECT_TRUE(errors.GetErrors(key::kUserAvatarImage).empty());
82 TEST(ExternalDataPolicyHandlerTest, WrongType) {
83 PolicyMap policy_map;
84 policy_map.Set(key::kUserAvatarImage,
85 POLICY_LEVEL_MANDATORY,
86 POLICY_SCOPE_USER,
87 new base::FundamentalValue(false),
88 NULL);
89 PolicyErrorMap errors;
90 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage)
91 .CheckPolicySettings(policy_map, &errors));
92 EXPECT_FALSE(errors.GetErrors(key::kUserAvatarImage).empty());
95 TEST(ExternalDataPolicyHandlerTest, MissingURL) {
96 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
97 dict->SetString("hash", "1234567890123456789012345678901234567890");
98 PolicyMap policy_map;
99 policy_map.Set(key::kUserAvatarImage,
100 POLICY_LEVEL_MANDATORY,
101 POLICY_SCOPE_USER,
102 dict.release(),
103 NULL);
104 PolicyErrorMap errors;
105 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage)
106 .CheckPolicySettings(policy_map, &errors));
107 EXPECT_FALSE(errors.GetErrors(key::kUserAvatarImage).empty());
110 TEST(ExternalDataPolicyHandlerTest, InvalidURL) {
111 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
112 dict->SetString("url", "http://");
113 dict->SetString("hash", "1234567890123456789012345678901234567890");
114 PolicyMap policy_map;
115 policy_map.Set(key::kUserAvatarImage,
116 POLICY_LEVEL_MANDATORY,
117 POLICY_SCOPE_USER,
118 dict.release(),
119 NULL);
120 PolicyErrorMap errors;
121 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage)
122 .CheckPolicySettings(policy_map, &errors));
123 EXPECT_FALSE(errors.GetErrors(key::kUserAvatarImage).empty());
126 TEST(ExternalDataPolicyHandlerTest, MissingHash) {
127 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
128 dict->SetString("url", "http://localhost/");
129 PolicyMap policy_map;
130 policy_map.Set(key::kUserAvatarImage,
131 POLICY_LEVEL_MANDATORY,
132 POLICY_SCOPE_USER,
133 dict.release(),
134 NULL);
135 PolicyErrorMap errors;
136 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage)
137 .CheckPolicySettings(policy_map, &errors));
138 EXPECT_FALSE(errors.GetErrors(key::kUserAvatarImage).empty());
141 TEST(ExternalDataPolicyHandlerTest, InvalidHash) {
142 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
143 dict->SetString("url", "http://localhost/");
144 dict->SetString("hash", "1234");
145 PolicyMap policy_map;
146 policy_map.Set(key::kUserAvatarImage,
147 POLICY_LEVEL_MANDATORY,
148 POLICY_SCOPE_USER,
149 dict.release(),
150 NULL);
151 PolicyErrorMap errors;
152 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage)
153 .CheckPolicySettings(policy_map, &errors));
154 EXPECT_FALSE(errors.GetErrors(key::kUserAvatarImage).empty());
157 TEST(ExternalDataPolicyHandlerTest, Valid) {
158 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
159 dict->SetString("url", "http://localhost/");
160 dict->SetString("hash", "1234567890123456789012345678901234567890");
161 PolicyMap policy_map;
162 policy_map.Set(key::kUserAvatarImage,
163 POLICY_LEVEL_MANDATORY,
164 POLICY_SCOPE_USER,
165 dict.release(),
166 NULL);
167 PolicyErrorMap errors;
168 EXPECT_TRUE(ExternalDataPolicyHandler(key::kUserAvatarImage)
169 .CheckPolicySettings(policy_map, &errors));
170 EXPECT_TRUE(errors.GetErrors(key::kUserAvatarImage).empty());
173 const char kLoginScreenPowerManagementPolicy[] =
175 " \"AC\": {"
176 " \"Delays\": {"
177 " \"ScreenDim\": 5000,"
178 " \"ScreenOff\": 7000,"
179 " \"Idle\": 9000"
180 " },"
181 " \"IdleAction\": \"DoNothing\""
182 " },"
183 " \"Battery\": {"
184 " \"Delays\": {"
185 " \"ScreenDim\": 1000,"
186 " \"ScreenOff\": 3000,"
187 " \"Idle\": 4000"
188 " },"
189 " \"IdleAction\": \"DoNothing\""
190 " },"
191 " \"LidCloseAction\": \"DoNothing\","
192 " \"UserActivityScreenDimDelayScale\": 300"
193 "}";
195 } // namespace
197 TEST(NetworkConfigurationPolicyHandlerTest, Empty) {
198 PolicyMap policy_map;
199 scoped_ptr<NetworkConfigurationPolicyHandler> handler(
200 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
201 PolicyErrorMap errors;
202 EXPECT_TRUE(handler->CheckPolicySettings(policy_map, &errors));
203 EXPECT_TRUE(errors.GetErrors(key::kOpenNetworkConfiguration).empty());
206 TEST(NetworkConfigurationPolicyHandlerTest, ValidONC) {
207 const std::string kTestONC(
209 " \"NetworkConfigurations\": [{"
210 " \"GUID\": \"{485d6076-dd44-6b6d-69787465725f5045}\","
211 " \"Type\": \"WiFi\","
212 " \"Name\": \"some name\","
213 " \"WiFi\": {"
214 " \"Security\": \"WEP-PSK\","
215 " \"SSID\": \"ssid\","
216 " \"Passphrase\": \"pass\","
217 " }"
218 " }]"
219 "}");
221 PolicyMap policy_map;
222 policy_map.Set(key::kOpenNetworkConfiguration,
223 POLICY_LEVEL_MANDATORY,
224 POLICY_SCOPE_USER,
225 base::Value::CreateStringValue(kTestONC),
226 NULL);
227 scoped_ptr<NetworkConfigurationPolicyHandler> handler(
228 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
229 PolicyErrorMap errors;
230 EXPECT_TRUE(handler->CheckPolicySettings(policy_map, &errors));
231 EXPECT_TRUE(errors.GetErrors(key::kOpenNetworkConfiguration).empty());
234 TEST(NetworkConfigurationPolicyHandlerTest, WrongType) {
235 PolicyMap policy_map;
236 policy_map.Set(key::kOpenNetworkConfiguration,
237 POLICY_LEVEL_MANDATORY,
238 POLICY_SCOPE_USER,
239 base::Value::CreateBooleanValue(false),
240 NULL);
241 scoped_ptr<NetworkConfigurationPolicyHandler> handler(
242 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
243 PolicyErrorMap errors;
244 EXPECT_FALSE(handler->CheckPolicySettings(policy_map, &errors));
245 EXPECT_FALSE(errors.GetErrors(key::kOpenNetworkConfiguration).empty());
248 TEST(NetworkConfigurationPolicyHandlerTest, JSONParseError) {
249 const std::string kTestONC("I'm not proper JSON!");
250 PolicyMap policy_map;
251 policy_map.Set(key::kOpenNetworkConfiguration,
252 POLICY_LEVEL_MANDATORY,
253 POLICY_SCOPE_USER,
254 base::Value::CreateStringValue(kTestONC),
255 NULL);
256 scoped_ptr<NetworkConfigurationPolicyHandler> handler(
257 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
258 PolicyErrorMap errors;
259 EXPECT_FALSE(handler->CheckPolicySettings(policy_map, &errors));
260 EXPECT_FALSE(errors.GetErrors(key::kOpenNetworkConfiguration).empty());
263 TEST(NetworkConfigurationPolicyHandlerTest, Sanitization) {
264 const std::string kTestONC(
266 " \"NetworkConfigurations\": [{"
267 " \"GUID\": \"{485d6076-dd44-6b6d-69787465725f5045}\","
268 " \"Type\": \"WiFi\","
269 " \"Name\": \"some name\","
270 " \"WiFi\": {"
271 " \"Security\": \"WEP-PSK\","
272 " \"SSID\": \"ssid\","
273 " \"Passphrase\": \"pass\","
274 " }"
275 " }]"
276 "}");
278 PolicyMap policy_map;
279 policy_map.Set(key::kOpenNetworkConfiguration,
280 POLICY_LEVEL_MANDATORY,
281 POLICY_SCOPE_USER,
282 base::Value::CreateStringValue(kTestONC),
283 NULL);
284 scoped_ptr<NetworkConfigurationPolicyHandler> handler(
285 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
286 PolicyErrorMap errors;
287 handler->PrepareForDisplaying(&policy_map);
288 const base::Value* sanitized =
289 policy_map.GetValue(key::kOpenNetworkConfiguration);
290 ASSERT_TRUE(sanitized);
291 std::string sanitized_onc;
292 EXPECT_TRUE(sanitized->GetAsString(&sanitized_onc));
293 EXPECT_FALSE(sanitized_onc.empty());
294 EXPECT_EQ(std::string::npos, sanitized_onc.find("pass"));
297 TEST(PinnedLauncherAppsPolicyHandler, PrefTranslation) {
298 base::ListValue list;
299 PolicyMap policy_map;
300 PrefValueMap prefs;
301 base::ListValue expected_pinned_apps;
302 base::Value* value = NULL;
303 PinnedLauncherAppsPolicyHandler handler;
305 policy_map.Set(key::kPinnedLauncherApps, POLICY_LEVEL_MANDATORY,
306 POLICY_SCOPE_USER, list.DeepCopy(), NULL);
307 handler.ApplyPolicySettings(policy_map, &prefs);
308 EXPECT_TRUE(prefs.GetValue(prefs::kPinnedLauncherApps, &value));
309 EXPECT_TRUE(base::Value::Equals(&expected_pinned_apps, value));
311 base::StringValue entry1("abcdefghijklmnopabcdefghijklmnop");
312 base::DictionaryValue* entry1_dict = new base::DictionaryValue();
313 entry1_dict->Set(ash::kPinnedAppsPrefAppIDPath, entry1.DeepCopy());
314 expected_pinned_apps.Append(entry1_dict);
315 list.Append(entry1.DeepCopy());
316 policy_map.Set(key::kPinnedLauncherApps, POLICY_LEVEL_MANDATORY,
317 POLICY_SCOPE_USER, list.DeepCopy(), NULL);
318 prefs.Clear();
319 handler.ApplyPolicySettings(policy_map, &prefs);
320 EXPECT_TRUE(prefs.GetValue(prefs::kPinnedLauncherApps, &value));
321 EXPECT_TRUE(base::Value::Equals(&expected_pinned_apps, value));
324 TEST(LoginScreenPowerManagementPolicyHandlerTest, Empty) {
325 PolicyMap policy_map;
326 LoginScreenPowerManagementPolicyHandler handler;
327 PolicyErrorMap errors;
328 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
329 EXPECT_TRUE(errors.GetErrors(key::kDeviceLoginScreenPowerManagement).empty());
332 TEST(LoginScreenPowerManagementPolicyHandlerTest, ValidPolicy) {
333 PolicyMap policy_map;
334 policy_map.Set(
335 key::kDeviceLoginScreenPowerManagement,
336 POLICY_LEVEL_MANDATORY,
337 POLICY_SCOPE_USER,
338 base::Value::CreateStringValue(kLoginScreenPowerManagementPolicy),
339 NULL);
340 LoginScreenPowerManagementPolicyHandler handler;
341 PolicyErrorMap errors;
342 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
343 EXPECT_TRUE(errors.GetErrors(key::kDeviceLoginScreenPowerManagement).empty());
346 TEST(LoginScreenPowerManagementPolicyHandlerTest, WrongType) {
347 PolicyMap policy_map;
348 policy_map.Set(key::kDeviceLoginScreenPowerManagement,
349 POLICY_LEVEL_MANDATORY,
350 POLICY_SCOPE_USER,
351 base::Value::CreateBooleanValue(false),
352 NULL);
353 LoginScreenPowerManagementPolicyHandler handler;
354 PolicyErrorMap errors;
355 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
356 EXPECT_FALSE(
357 errors.GetErrors(key::kDeviceLoginScreenPowerManagement).empty());
360 TEST(LoginScreenPowerManagementPolicyHandlerTest, JSONParseError) {
361 const std::string policy("I'm not proper JSON!");
362 PolicyMap policy_map;
363 policy_map.Set(key::kDeviceLoginScreenPowerManagement,
364 POLICY_LEVEL_MANDATORY,
365 POLICY_SCOPE_USER,
366 base::Value::CreateStringValue(policy),
367 NULL);
368 LoginScreenPowerManagementPolicyHandler handler;
369 PolicyErrorMap errors;
370 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
371 EXPECT_FALSE(
372 errors.GetErrors(key::kDeviceLoginScreenPowerManagement).empty());
375 } // namespace policy