cryptohome: Move stateless wrapper functions out of CryptohomeLibrary
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / configuration_policy_handler_chromeos_unittest.cc
blob761c82f230f11074c85f3369d8190c3fd8aaccdd
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"
17 namespace policy {
19 namespace {
21 const char kLoginScreenPowerManagementPolicy[] =
22 "{"
23 " \"AC\": {"
24 " \"Delays\": {"
25 " \"ScreenDim\": 5000,"
26 " \"ScreenOff\": 7000,"
27 " \"Idle\": 9000"
28 " },"
29 " \"IdleAction\": \"DoNothing\""
30 " },"
31 " \"Battery\": {"
32 " \"Delays\": {"
33 " \"ScreenDim\": 1000,"
34 " \"ScreenOff\": 3000,"
35 " \"Idle\": 4000"
36 " },"
37 " \"IdleAction\": \"DoNothing\""
38 " },"
39 " \"LidCloseAction\": \"DoNothing\","
40 " \"UserActivityScreenDimDelayScale\": 300"
41 "}";
43 } // namespace
45 TEST(NetworkConfigurationPolicyHandlerTest, Empty) {
46 PolicyMap policy_map;
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(
56 "{"
57 " \"NetworkConfigurations\": [{"
58 " \"GUID\": \"{485d6076-dd44-6b6d-69787465725f5045}\","
59 " \"Type\": \"WiFi\","
60 " \"Name\": \"some name\","
61 " \"WiFi\": {"
62 " \"Security\": \"WEP-PSK\","
63 " \"SSID\": \"ssid\","
64 " \"Passphrase\": \"pass\","
65 " }"
66 " }]"
67 "}");
69 PolicyMap policy_map;
70 policy_map.Set(key::kOpenNetworkConfiguration,
71 POLICY_LEVEL_MANDATORY,
72 POLICY_SCOPE_USER,
73 Value::CreateStringValue(kTestONC),
74 NULL);
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) {
83 PolicyMap policy_map;
84 policy_map.Set(key::kOpenNetworkConfiguration,
85 POLICY_LEVEL_MANDATORY,
86 POLICY_SCOPE_USER,
87 Value::CreateBooleanValue(false),
88 NULL);
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!");
98 PolicyMap policy_map;
99 policy_map.Set(key::kOpenNetworkConfiguration,
100 POLICY_LEVEL_MANDATORY,
101 POLICY_SCOPE_USER,
102 Value::CreateStringValue(kTestONC),
103 NULL);
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\","
118 " \"WiFi\": {"
119 " \"Security\": \"WEP-PSK\","
120 " \"SSID\": \"ssid\","
121 " \"Passphrase\": \"pass\","
122 " }"
123 " }]"
124 "}");
126 PolicyMap policy_map;
127 policy_map.Set(key::kOpenNetworkConfiguration,
128 POLICY_LEVEL_MANDATORY,
129 POLICY_SCOPE_USER,
130 Value::CreateStringValue(kTestONC),
131 NULL);
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;
147 PrefValueMap prefs;
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);
165 prefs.Clear();
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,
183 POLICY_SCOPE_USER,
184 Value::CreateStringValue(kLoginScreenPowerManagementPolicy),
185 NULL);
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,
196 POLICY_SCOPE_USER,
197 Value::CreateBooleanValue(false),
198 NULL);
199 LoginScreenPowerManagementPolicyHandler handler;
200 PolicyErrorMap errors;
201 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
202 EXPECT_FALSE(
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,
211 POLICY_SCOPE_USER,
212 Value::CreateStringValue(policy),
213 NULL);
214 LoginScreenPowerManagementPolicyHandler handler;
215 PolicyErrorMap errors;
216 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
217 EXPECT_FALSE(
218 errors.GetErrors(key::kDeviceLoginScreenPowerManagement).empty());
221 } // namespace policy