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/prefs/pref_value_map.h"
9 #include "chrome/browser/policy/external_data_fetcher.h"
10 #include "chrome/browser/policy/policy_error_map.h"
11 #include "chrome/browser/policy/policy_map.h"
12 #include "chrome/browser/ui/ash/chrome_launcher_prefs.h"
13 #include "chrome/common/pref_names.h"
14 #include "policy/policy_constants.h"
15 #include "testing/gtest/include/gtest/gtest.h"
21 const char kLoginScreenPowerManagementPolicy
[] =
25 " \"ScreenDim\": 5000,"
26 " \"ScreenOff\": 7000,"
29 " \"IdleAction\": \"DoNothing\""
33 " \"ScreenDim\": 1000,"
34 " \"ScreenOff\": 3000,"
37 " \"IdleAction\": \"DoNothing\""
39 " \"LidCloseAction\": \"DoNothing\","
40 " \"UserActivityScreenDimDelayScale\": 300"
45 TEST(NetworkConfigurationPolicyHandlerTest
, Empty
) {
47 scoped_ptr
<NetworkConfigurationPolicyHandler
> handler(
48 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
49 PolicyErrorMap errors
;
50 EXPECT_TRUE(handler
->CheckPolicySettings(policy_map
, &errors
));
51 EXPECT_TRUE(errors
.GetErrors(key::kOpenNetworkConfiguration
).empty());
54 TEST(NetworkConfigurationPolicyHandlerTest
, ValidONC
) {
55 const std::string
kTestONC(
57 " \"NetworkConfigurations\": [{"
58 " \"GUID\": \"{485d6076-dd44-6b6d-69787465725f5045}\","
59 " \"Type\": \"WiFi\","
60 " \"Name\": \"some name\","
62 " \"Security\": \"WEP-PSK\","
63 " \"SSID\": \"ssid\","
64 " \"Passphrase\": \"pass\","
70 policy_map
.Set(key::kOpenNetworkConfiguration
,
71 POLICY_LEVEL_MANDATORY
,
73 Value::CreateStringValue(kTestONC
),
75 scoped_ptr
<NetworkConfigurationPolicyHandler
> handler(
76 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
77 PolicyErrorMap errors
;
78 EXPECT_TRUE(handler
->CheckPolicySettings(policy_map
, &errors
));
79 EXPECT_TRUE(errors
.GetErrors(key::kOpenNetworkConfiguration
).empty());
82 TEST(NetworkConfigurationPolicyHandlerTest
, WrongType
) {
84 policy_map
.Set(key::kOpenNetworkConfiguration
,
85 POLICY_LEVEL_MANDATORY
,
87 Value::CreateBooleanValue(false),
89 scoped_ptr
<NetworkConfigurationPolicyHandler
> handler(
90 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
91 PolicyErrorMap errors
;
92 EXPECT_FALSE(handler
->CheckPolicySettings(policy_map
, &errors
));
93 EXPECT_FALSE(errors
.GetErrors(key::kOpenNetworkConfiguration
).empty());
96 TEST(NetworkConfigurationPolicyHandlerTest
, JSONParseError
) {
97 const std::string
kTestONC("I'm not proper JSON!");
99 policy_map
.Set(key::kOpenNetworkConfiguration
,
100 POLICY_LEVEL_MANDATORY
,
102 Value::CreateStringValue(kTestONC
),
104 scoped_ptr
<NetworkConfigurationPolicyHandler
> handler(
105 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
106 PolicyErrorMap errors
;
107 EXPECT_FALSE(handler
->CheckPolicySettings(policy_map
, &errors
));
108 EXPECT_FALSE(errors
.GetErrors(key::kOpenNetworkConfiguration
).empty());
111 TEST(NetworkConfigurationPolicyHandlerTest
, Sanitization
) {
112 const std::string
kTestONC(
114 " \"NetworkConfigurations\": [{"
115 " \"GUID\": \"{485d6076-dd44-6b6d-69787465725f5045}\","
116 " \"Type\": \"WiFi\","
117 " \"Name\": \"some name\","
119 " \"Security\": \"WEP-PSK\","
120 " \"SSID\": \"ssid\","
121 " \"Passphrase\": \"pass\","
126 PolicyMap policy_map
;
127 policy_map
.Set(key::kOpenNetworkConfiguration
,
128 POLICY_LEVEL_MANDATORY
,
130 Value::CreateStringValue(kTestONC
),
132 scoped_ptr
<NetworkConfigurationPolicyHandler
> handler(
133 NetworkConfigurationPolicyHandler::CreateForUserPolicy());
134 PolicyErrorMap errors
;
135 handler
->PrepareForDisplaying(&policy_map
);
136 const Value
* sanitized
= policy_map
.GetValue(key::kOpenNetworkConfiguration
);
137 ASSERT_TRUE(sanitized
);
138 std::string sanitized_onc
;
139 EXPECT_TRUE(sanitized
->GetAsString(&sanitized_onc
));
140 EXPECT_FALSE(sanitized_onc
.empty());
141 EXPECT_EQ(std::string::npos
, sanitized_onc
.find("pass"));
144 TEST(PinnedLauncherAppsPolicyHandler
, PrefTranslation
) {
145 base::ListValue list
;
146 PolicyMap policy_map
;
148 base::ListValue expected_pinned_apps
;
149 base::Value
* value
= NULL
;
150 PinnedLauncherAppsPolicyHandler handler
;
152 policy_map
.Set(key::kPinnedLauncherApps
, POLICY_LEVEL_MANDATORY
,
153 POLICY_SCOPE_USER
, list
.DeepCopy(), NULL
);
154 handler
.ApplyPolicySettings(policy_map
, &prefs
);
155 EXPECT_TRUE(prefs
.GetValue(prefs::kPinnedLauncherApps
, &value
));
156 EXPECT_TRUE(base::Value::Equals(&expected_pinned_apps
, value
));
158 base::StringValue
entry1("abcdefghijklmnopabcdefghijklmnop");
159 base::DictionaryValue
* entry1_dict
= new base::DictionaryValue();
160 entry1_dict
->Set(ash::kPinnedAppsPrefAppIDPath
, entry1
.DeepCopy());
161 expected_pinned_apps
.Append(entry1_dict
);
162 list
.Append(entry1
.DeepCopy());
163 policy_map
.Set(key::kPinnedLauncherApps
, POLICY_LEVEL_MANDATORY
,
164 POLICY_SCOPE_USER
, list
.DeepCopy(), NULL
);
166 handler
.ApplyPolicySettings(policy_map
, &prefs
);
167 EXPECT_TRUE(prefs
.GetValue(prefs::kPinnedLauncherApps
, &value
));
168 EXPECT_TRUE(base::Value::Equals(&expected_pinned_apps
, value
));
171 TEST(LoginScreenPowerManagementPolicyHandlerTest
, Empty
) {
172 PolicyMap policy_map
;
173 LoginScreenPowerManagementPolicyHandler handler
;
174 PolicyErrorMap errors
;
175 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
176 EXPECT_TRUE(errors
.GetErrors(key::kDeviceLoginScreenPowerManagement
).empty());
179 TEST(LoginScreenPowerManagementPolicyHandlerTest
, ValidPolicy
) {
180 PolicyMap policy_map
;
181 policy_map
.Set(key::kDeviceLoginScreenPowerManagement
,
182 POLICY_LEVEL_MANDATORY
,
184 Value::CreateStringValue(kLoginScreenPowerManagementPolicy
),
186 LoginScreenPowerManagementPolicyHandler handler
;
187 PolicyErrorMap errors
;
188 EXPECT_TRUE(handler
.CheckPolicySettings(policy_map
, &errors
));
189 EXPECT_TRUE(errors
.GetErrors(key::kDeviceLoginScreenPowerManagement
).empty());
192 TEST(LoginScreenPowerManagementPolicyHandlerTest
, WrongType
) {
193 PolicyMap policy_map
;
194 policy_map
.Set(key::kDeviceLoginScreenPowerManagement
,
195 POLICY_LEVEL_MANDATORY
,
197 Value::CreateBooleanValue(false),
199 LoginScreenPowerManagementPolicyHandler handler
;
200 PolicyErrorMap errors
;
201 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
203 errors
.GetErrors(key::kDeviceLoginScreenPowerManagement
).empty());
206 TEST(LoginScreenPowerManagementPolicyHandlerTest
, JSONParseError
) {
207 const std::string
policy("I'm not proper JSON!");
208 PolicyMap policy_map
;
209 policy_map
.Set(key::kDeviceLoginScreenPowerManagement
,
210 POLICY_LEVEL_MANDATORY
,
212 Value::CreateStringValue(policy
),
214 LoginScreenPowerManagementPolicyHandler handler
;
215 PolicyErrorMap errors
;
216 EXPECT_FALSE(handler
.CheckPolicySettings(policy_map
, &errors
));
218 errors
.GetErrors(key::kDeviceLoginScreenPowerManagement
).empty());
221 } // namespace policy