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/policy_types.h"
18 #include "components/policy/core/common/schema.h"
19 #include "policy/policy_constants.h"
20 #include "testing/gtest/include/gtest/gtest.h"
26 // Test cases for the screen magnifier type policy setting.
27 class ScreenMagnifierPolicyHandlerTest
: public testing::Test
{
31 ScreenMagnifierPolicyHandler handler_
;
34 class LoginScreenPowerManagementPolicyHandlerTest
: public testing::Test
{
36 LoginScreenPowerManagementPolicyHandlerTest();
38 void SetUp() override
;
40 Schema chrome_schema_
;
43 DISALLOW_COPY_AND_ASSIGN(LoginScreenPowerManagementPolicyHandlerTest
);
46 LoginScreenPowerManagementPolicyHandlerTest::
47 LoginScreenPowerManagementPolicyHandlerTest() {
50 void LoginScreenPowerManagementPolicyHandlerTest::SetUp() {
51 chrome_schema_
= Schema::Wrap(GetChromeSchemaData());
54 TEST_F(ScreenMagnifierPolicyHandlerTest
, Default
) {
55 handler_
.ApplyPolicySettings(policy_
, &prefs_
);
57 prefs_
.GetValue(prefs::kAccessibilityScreenMagnifierEnabled
, NULL
));
58 EXPECT_FALSE(prefs_
.GetValue(prefs::kAccessibilityScreenMagnifierType
, NULL
));
61 TEST_F(ScreenMagnifierPolicyHandlerTest
, Disabled
) {
62 policy_
.Set(key::kScreenMagnifierType
,
63 POLICY_LEVEL_MANDATORY
,
66 new base::FundamentalValue(0),
68 handler_
.ApplyPolicySettings(policy_
, &prefs_
);
70 const base::Value
* enabled
= NULL
;
72 prefs_
.GetValue(prefs::kAccessibilityScreenMagnifierEnabled
, &enabled
));
74 EXPECT_TRUE(base::FundamentalValue(false).Equals(enabled
));
76 const base::Value
* type
= NULL
;
77 EXPECT_TRUE(prefs_
.GetValue(prefs::kAccessibilityScreenMagnifierType
, &type
));
79 EXPECT_TRUE(base::FundamentalValue(0).Equals(type
));
82 TEST_F(ScreenMagnifierPolicyHandlerTest
, Enabled
) {
83 policy_
.Set(key::kScreenMagnifierType
,
84 POLICY_LEVEL_MANDATORY
,
87 new base::FundamentalValue(1),
89 handler_
.ApplyPolicySettings(policy_
, &prefs_
);
91 const base::Value
* enabled
= NULL
;
93 prefs_
.GetValue(prefs::kAccessibilityScreenMagnifierEnabled
, &enabled
));
95 EXPECT_TRUE(base::FundamentalValue(true).Equals(enabled
));
97 const base::Value
* type
= NULL
;
98 EXPECT_TRUE(prefs_
.GetValue(prefs::kAccessibilityScreenMagnifierType
, &type
));
100 EXPECT_TRUE(base::FundamentalValue(1).Equals(type
));
103 TEST(ExternalDataPolicyHandlerTest
, Empty
) {
104 PolicyErrorMap errors
;
105 EXPECT_TRUE(ExternalDataPolicyHandler(key::kUserAvatarImage
)
106 .CheckPolicySettings(PolicyMap(), &errors
));
107 EXPECT_TRUE(errors
.GetErrors(key::kUserAvatarImage
).empty());
110 TEST(ExternalDataPolicyHandlerTest
, WrongType
) {
111 PolicyMap policy_map
;
112 policy_map
.Set(key::kUserAvatarImage
,
113 POLICY_LEVEL_MANDATORY
,
116 new base::FundamentalValue(false),
118 PolicyErrorMap errors
;
119 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage
)
120 .CheckPolicySettings(policy_map
, &errors
));
121 EXPECT_FALSE(errors
.GetErrors(key::kUserAvatarImage
).empty());
124 TEST(ExternalDataPolicyHandlerTest
, MissingURL
) {
125 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue
);
126 dict
->SetString("hash", "1234567890123456789012345678901234567890");
127 PolicyMap policy_map
;
128 policy_map
.Set(key::kUserAvatarImage
,
129 POLICY_LEVEL_MANDATORY
,
134 PolicyErrorMap errors
;
135 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage
)
136 .CheckPolicySettings(policy_map
, &errors
));
137 EXPECT_FALSE(errors
.GetErrors(key::kUserAvatarImage
).empty());
140 TEST(ExternalDataPolicyHandlerTest
, InvalidURL
) {
141 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue
);
142 dict
->SetString("url", "http://");
143 dict
->SetString("hash", "1234567890123456789012345678901234567890");
144 PolicyMap policy_map
;
145 policy_map
.Set(key::kUserAvatarImage
,
146 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
, MissingHash
) {
158 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue
);
159 dict
->SetString("url", "http://localhost/");
160 PolicyMap policy_map
;
161 policy_map
.Set(key::kUserAvatarImage
,
162 POLICY_LEVEL_MANDATORY
,
167 PolicyErrorMap errors
;
168 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage
)
169 .CheckPolicySettings(policy_map
, &errors
));
170 EXPECT_FALSE(errors
.GetErrors(key::kUserAvatarImage
).empty());
173 TEST(ExternalDataPolicyHandlerTest
, InvalidHash
) {
174 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue
);
175 dict
->SetString("url", "http://localhost/");
176 dict
->SetString("hash", "1234");
177 PolicyMap policy_map
;
178 policy_map
.Set(key::kUserAvatarImage
,
179 POLICY_LEVEL_MANDATORY
,
184 PolicyErrorMap errors
;
185 EXPECT_FALSE(ExternalDataPolicyHandler(key::kUserAvatarImage
)
186 .CheckPolicySettings(policy_map
, &errors
));
187 EXPECT_FALSE(errors
.GetErrors(key::kUserAvatarImage
).empty());
190 TEST(ExternalDataPolicyHandlerTest
, Valid
) {
191 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue
);
192 dict
->SetString("url", "http://localhost/");
195 "1234567890123456789012345678901234567890123456789012345678901234");
196 PolicyMap policy_map
;
197 policy_map
.Set(key::kUserAvatarImage
,
198 POLICY_LEVEL_MANDATORY
,
203 PolicyErrorMap errors
;
204 EXPECT_TRUE(ExternalDataPolicyHandler(key::kUserAvatarImage
)
205 .CheckPolicySettings(policy_map
, &errors
));
206 EXPECT_TRUE(errors
.GetErrors(key::kUserAvatarImage
).empty());
209 const char kLoginScreenPowerManagementPolicy
[] =
213 " \"ScreenDim\": 5000,"
214 " \"ScreenOff\": 7000,"
217 " \"IdleAction\": \"DoNothing\""
221 " \"ScreenDim\": 1000,"
222 " \"ScreenOff\": 3000,"
225 " \"IdleAction\": \"DoNothing\""
227 " \"LidCloseAction\": \"DoNothing\","
228 " \"UserActivityScreenDimDelayScale\": 300"
233 TEST(NetworkConfigurationPolicyHandlerTest
, Empty
) {
234 PolicyMap policy_map
;
235 scoped_ptr
<NetworkConfigurationPolicyHandler
> handler(
236 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
237 PolicyErrorMap errors
;
238 EXPECT_TRUE(handler
->CheckPolicySettings(policy_map
, &errors
));
239 EXPECT_TRUE(errors
.GetErrors(key::kOpenNetworkConfiguration
).empty());
242 TEST(NetworkConfigurationPolicyHandlerTest
, ValidONC
) {
243 const std::string
kTestONC(
245 " \"NetworkConfigurations\": [{"
246 " \"GUID\": \"{485d6076-dd44-6b6d-69787465725f5045}\","
247 " \"Type\": \"WiFi\","
248 " \"Name\": \"some name\","
250 " \"Security\": \"WEP-PSK\","
251 " \"SSID\": \"ssid\","
252 " \"Passphrase\": \"pass\","
257 PolicyMap policy_map
;
258 policy_map
.Set(key::kOpenNetworkConfiguration
,
259 POLICY_LEVEL_MANDATORY
,
262 new base::StringValue(kTestONC
),
264 scoped_ptr
<NetworkConfigurationPolicyHandler
> handler(
265 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
266 PolicyErrorMap errors
;
267 EXPECT_TRUE(handler
->CheckPolicySettings(policy_map
, &errors
));
268 EXPECT_TRUE(errors
.GetErrors(key::kOpenNetworkConfiguration
).empty());
271 TEST(NetworkConfigurationPolicyHandlerTest
, WrongType
) {
272 PolicyMap policy_map
;
273 policy_map
.Set(key::kOpenNetworkConfiguration
,
274 POLICY_LEVEL_MANDATORY
,
277 new base::FundamentalValue(false),
279 scoped_ptr
<NetworkConfigurationPolicyHandler
> handler(
280 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
281 PolicyErrorMap errors
;
282 EXPECT_FALSE(handler
->CheckPolicySettings(policy_map
, &errors
));
283 EXPECT_FALSE(errors
.GetErrors(key::kOpenNetworkConfiguration
).empty());
286 TEST(NetworkConfigurationPolicyHandlerTest
, JSONParseError
) {
287 const std::string
kTestONC("I'm not proper JSON!");
288 PolicyMap policy_map
;
289 policy_map
.Set(key::kOpenNetworkConfiguration
,
290 POLICY_LEVEL_MANDATORY
,
293 new base::StringValue(kTestONC
),
295 scoped_ptr
<NetworkConfigurationPolicyHandler
> handler(
296 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
297 PolicyErrorMap errors
;
298 EXPECT_FALSE(handler
->CheckPolicySettings(policy_map
, &errors
));
299 EXPECT_FALSE(errors
.GetErrors(key::kOpenNetworkConfiguration
).empty());
302 TEST(NetworkConfigurationPolicyHandlerTest
, Sanitization
) {
303 const std::string
kTestONC(
305 " \"NetworkConfigurations\": [{"
306 " \"GUID\": \"{485d6076-dd44-6b6d-69787465725f5045}\","
307 " \"Type\": \"WiFi\","
308 " \"Name\": \"some name\","
310 " \"Security\": \"WEP-PSK\","
311 " \"SSID\": \"ssid\","
312 " \"Passphrase\": \"pass\","
317 PolicyMap policy_map
;
318 policy_map
.Set(key::kOpenNetworkConfiguration
,
319 POLICY_LEVEL_MANDATORY
,
322 new base::StringValue(kTestONC
),
324 scoped_ptr
<NetworkConfigurationPolicyHandler
> handler(
325 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
326 PolicyErrorMap errors
;
327 handler
->PrepareForDisplaying(&policy_map
);
328 const base::Value
* sanitized
=
329 policy_map
.GetValue(key::kOpenNetworkConfiguration
);
330 ASSERT_TRUE(sanitized
);
331 std::string sanitized_onc
;
332 EXPECT_TRUE(sanitized
->GetAsString(&sanitized_onc
));
333 EXPECT_FALSE(sanitized_onc
.empty());
334 EXPECT_EQ(std::string::npos
, sanitized_onc
.find("pass"));
337 TEST(PinnedLauncherAppsPolicyHandler
, PrefTranslation
) {
338 base::ListValue list
;
339 PolicyMap policy_map
;
341 base::ListValue expected_pinned_apps
;
342 base::Value
* value
= NULL
;
343 PinnedLauncherAppsPolicyHandler handler
;
345 policy_map
.Set(key::kPinnedLauncherApps
, POLICY_LEVEL_MANDATORY
,
346 POLICY_SCOPE_USER
, POLICY_SOURCE_CLOUD
, list
.DeepCopy(),
348 handler
.ApplyPolicySettings(policy_map
, &prefs
);
349 EXPECT_TRUE(prefs
.GetValue(prefs::kPinnedLauncherApps
, &value
));
350 EXPECT_TRUE(base::Value::Equals(&expected_pinned_apps
, value
));
352 base::StringValue
entry1("abcdefghijklmnopabcdefghijklmnop");
353 base::DictionaryValue
* entry1_dict
= new base::DictionaryValue();
354 entry1_dict
->Set(ash::kPinnedAppsPrefAppIDPath
, entry1
.DeepCopy());
355 expected_pinned_apps
.Append(entry1_dict
);
356 list
.Append(entry1
.DeepCopy());
357 policy_map
.Set(key::kPinnedLauncherApps
, POLICY_LEVEL_MANDATORY
,
358 POLICY_SCOPE_USER
, POLICY_SOURCE_CLOUD
, list
.DeepCopy(),
361 handler
.ApplyPolicySettings(policy_map
, &prefs
);
362 EXPECT_TRUE(prefs
.GetValue(prefs::kPinnedLauncherApps
, &value
));
363 EXPECT_TRUE(base::Value::Equals(&expected_pinned_apps
, value
));
366 TEST_F(LoginScreenPowerManagementPolicyHandlerTest
, Empty
) {
367 PolicyMap policy_map
;
368 LoginScreenPowerManagementPolicyHandler
handler(chrome_schema_
);
369 PolicyErrorMap errors
;
370 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
371 EXPECT_TRUE(errors
.GetErrors(key::kDeviceLoginScreenPowerManagement
).empty());
374 TEST_F(LoginScreenPowerManagementPolicyHandlerTest
, ValidPolicy
) {
375 PolicyMap policy_map
;
377 key::kDeviceLoginScreenPowerManagement
, POLICY_LEVEL_MANDATORY
,
380 base::JSONReader::Read(kLoginScreenPowerManagementPolicy
).release(),
382 LoginScreenPowerManagementPolicyHandler
handler(chrome_schema_
);
383 PolicyErrorMap errors
;
384 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
385 EXPECT_TRUE(errors
.GetErrors(key::kDeviceLoginScreenPowerManagement
).empty());
388 TEST_F(LoginScreenPowerManagementPolicyHandlerTest
, WrongType
) {
389 PolicyMap policy_map
;
390 policy_map
.Set(key::kDeviceLoginScreenPowerManagement
,
391 POLICY_LEVEL_MANDATORY
,
394 new base::FundamentalValue(false),
396 LoginScreenPowerManagementPolicyHandler
handler(chrome_schema_
);
397 PolicyErrorMap errors
;
398 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
400 errors
.GetErrors(key::kDeviceLoginScreenPowerManagement
).empty());
403 } // namespace policy