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"
23 // Test cases for the screen magnifier type policy setting.
24 class ScreenMagnifierPolicyHandlerTest
: public testing::Test
{
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
,
41 base::Value::CreateIntegerValue(0),
43 handler_
.ApplyPolicySettings(policy_
, &prefs_
);
45 const base::Value
* enabled
= NULL
;
46 EXPECT_TRUE(prefs_
.GetValue(prefs::kScreenMagnifierEnabled
, &enabled
));
48 EXPECT_TRUE(base::FundamentalValue(false).Equals(enabled
));
50 const base::Value
* type
= NULL
;
51 EXPECT_TRUE(prefs_
.GetValue(prefs::kScreenMagnifierType
, &type
));
53 EXPECT_TRUE(base::FundamentalValue(0).Equals(type
));
56 TEST_F(ScreenMagnifierPolicyHandlerTest
, Enabled
) {
57 policy_
.Set(key::kScreenMagnifierType
,
58 POLICY_LEVEL_MANDATORY
,
60 base::Value::CreateIntegerValue(1),
62 handler_
.ApplyPolicySettings(policy_
, &prefs_
);
64 const base::Value
* enabled
= NULL
;
65 EXPECT_TRUE(prefs_
.GetValue(prefs::kScreenMagnifierEnabled
, &enabled
));
67 EXPECT_TRUE(base::FundamentalValue(true).Equals(enabled
));
69 const base::Value
* type
= NULL
;
70 EXPECT_TRUE(prefs_
.GetValue(prefs::kScreenMagnifierType
, &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
) {
84 policy_map
.Set(key::kUserAvatarImage
,
85 POLICY_LEVEL_MANDATORY
,
87 new base::FundamentalValue(false),
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");
99 policy_map
.Set(key::kUserAvatarImage
,
100 POLICY_LEVEL_MANDATORY
,
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
,
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
,
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
,
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
,
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
[] =
177 " \"ScreenDim\": 5000,"
178 " \"ScreenOff\": 7000,"
181 " \"IdleAction\": \"DoNothing\""
185 " \"ScreenDim\": 1000,"
186 " \"ScreenOff\": 3000,"
189 " \"IdleAction\": \"DoNothing\""
191 " \"LidCloseAction\": \"DoNothing\","
192 " \"UserActivityScreenDimDelayScale\": 300"
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\","
214 " \"Security\": \"WEP-PSK\","
215 " \"SSID\": \"ssid\","
216 " \"Passphrase\": \"pass\","
221 PolicyMap policy_map
;
222 policy_map
.Set(key::kOpenNetworkConfiguration
,
223 POLICY_LEVEL_MANDATORY
,
225 base::Value::CreateStringValue(kTestONC
),
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
,
239 base::Value::CreateBooleanValue(false),
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
,
254 base::Value::CreateStringValue(kTestONC
),
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\","
271 " \"Security\": \"WEP-PSK\","
272 " \"SSID\": \"ssid\","
273 " \"Passphrase\": \"pass\","
278 PolicyMap policy_map
;
279 policy_map
.Set(key::kOpenNetworkConfiguration
,
280 POLICY_LEVEL_MANDATORY
,
282 base::Value::CreateStringValue(kTestONC
),
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
;
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
);
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
;
335 key::kDeviceLoginScreenPowerManagement
,
336 POLICY_LEVEL_MANDATORY
,
338 base::Value::CreateStringValue(kLoginScreenPowerManagementPolicy
),
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
,
351 base::Value::CreateBooleanValue(false),
353 LoginScreenPowerManagementPolicyHandler handler
;
354 PolicyErrorMap errors
;
355 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
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
,
366 base::Value::CreateStringValue(policy
),
368 LoginScreenPowerManagementPolicyHandler handler
;
369 PolicyErrorMap errors
;
370 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
372 errors
.GetErrors(key::kDeviceLoginScreenPowerManagement
).empty());
375 } // namespace policy