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"
25 // Test cases for the screen magnifier type policy setting.
26 class ScreenMagnifierPolicyHandlerTest
: public testing::Test
{
30 ScreenMagnifierPolicyHandler handler_
;
33 class LoginScreenPowerManagementPolicyHandlerTest
: public testing::Test
{
35 LoginScreenPowerManagementPolicyHandlerTest();
37 void SetUp() override
;
39 Schema chrome_schema_
;
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_
);
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
,
64 new base::FundamentalValue(0),
66 handler_
.ApplyPolicySettings(policy_
, &prefs_
);
68 const base::Value
* enabled
= NULL
;
70 prefs_
.GetValue(prefs::kAccessibilityScreenMagnifierEnabled
, &enabled
));
72 EXPECT_TRUE(base::FundamentalValue(false).Equals(enabled
));
74 const base::Value
* type
= NULL
;
75 EXPECT_TRUE(prefs_
.GetValue(prefs::kAccessibilityScreenMagnifierType
, &type
));
77 EXPECT_TRUE(base::FundamentalValue(0).Equals(type
));
80 TEST_F(ScreenMagnifierPolicyHandlerTest
, Enabled
) {
81 policy_
.Set(key::kScreenMagnifierType
,
82 POLICY_LEVEL_MANDATORY
,
84 new base::FundamentalValue(1),
86 handler_
.ApplyPolicySettings(policy_
, &prefs_
);
88 const base::Value
* enabled
= NULL
;
90 prefs_
.GetValue(prefs::kAccessibilityScreenMagnifierEnabled
, &enabled
));
92 EXPECT_TRUE(base::FundamentalValue(true).Equals(enabled
));
94 const base::Value
* type
= NULL
;
95 EXPECT_TRUE(prefs_
.GetValue(prefs::kAccessibilityScreenMagnifierType
, &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
,
112 new base::FundamentalValue(false),
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
,
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
,
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
,
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
,
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/");
187 "1234567890123456789012345678901234567890123456789012345678901234");
188 PolicyMap policy_map
;
189 policy_map
.Set(key::kUserAvatarImage
,
190 POLICY_LEVEL_MANDATORY
,
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
[] =
204 " \"ScreenDim\": 5000,"
205 " \"ScreenOff\": 7000,"
208 " \"IdleAction\": \"DoNothing\""
212 " \"ScreenDim\": 1000,"
213 " \"ScreenOff\": 3000,"
216 " \"IdleAction\": \"DoNothing\""
218 " \"LidCloseAction\": \"DoNothing\","
219 " \"UserActivityScreenDimDelayScale\": 300"
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\","
241 " \"Security\": \"WEP-PSK\","
242 " \"SSID\": \"ssid\","
243 " \"Passphrase\": \"pass\","
248 PolicyMap policy_map
;
249 policy_map
.Set(key::kOpenNetworkConfiguration
,
250 POLICY_LEVEL_MANDATORY
,
252 new base::StringValue(kTestONC
),
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
,
266 new base::FundamentalValue(false),
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
,
281 new base::StringValue(kTestONC
),
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\","
298 " \"Security\": \"WEP-PSK\","
299 " \"SSID\": \"ssid\","
300 " \"Passphrase\": \"pass\","
305 PolicyMap policy_map
;
306 policy_map
.Set(key::kOpenNetworkConfiguration
,
307 POLICY_LEVEL_MANDATORY
,
309 new base::StringValue(kTestONC
),
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
;
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
);
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
;
362 key::kDeviceLoginScreenPowerManagement
, POLICY_LEVEL_MANDATORY
,
364 base::JSONReader::Read(kLoginScreenPowerManagementPolicy
).release(),
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
,
377 new base::FundamentalValue(false),
379 LoginScreenPowerManagementPolicyHandler
handler(chrome_schema_
);
380 PolicyErrorMap errors
;
381 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
383 errors
.GetErrors(key::kDeviceLoginScreenPowerManagement
).empty());
386 } // namespace policy