Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / components / policy / core / browser / configuration_policy_handler_unittest.cc
blob4947dbf9eeac05fb75a97bedb12341ad713c0d42
1 // Copyright (c) 2014 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 "base/callback.h"
6 #include "base/json/json_reader.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/prefs/pref_value_map.h"
9 #include "base/values.h"
10 #include "components/policy/core/browser/configuration_policy_handler.h"
11 #include "components/policy/core/browser/policy_error_map.h"
12 #include "components/policy/core/common/policy_map.h"
13 #include "components/policy/core/common/schema.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace policy {
18 namespace {
20 StringToIntEnumListPolicyHandler::MappingEntry kTestTypeMap[] = {
21 { "one", 1 },
22 { "two", 2 },
25 const char kTestPolicy[] = "unit_test.test_policy";
26 const char kTestPref[] = "unit_test.test_pref";
28 class SimpleSchemaValidatingPolicyHandler
29 : public SchemaValidatingPolicyHandler {
30 public:
31 SimpleSchemaValidatingPolicyHandler(const Schema& schema,
32 SchemaOnErrorStrategy strategy)
33 : SchemaValidatingPolicyHandler("PolicyForTesting", schema, strategy) {}
34 virtual ~SimpleSchemaValidatingPolicyHandler() {}
36 virtual void ApplyPolicySettings(const policy::PolicyMap&,
37 PrefValueMap*) OVERRIDE {
40 bool CheckAndGetValueForTest(const PolicyMap& policies,
41 scoped_ptr<base::Value>* value) {
42 return SchemaValidatingPolicyHandler::CheckAndGetValue(
43 policies, NULL, value);
47 } // namespace
49 TEST(StringToIntEnumListPolicyHandlerTest, CheckPolicySettings) {
50 base::ListValue list;
51 PolicyMap policy_map;
52 PolicyErrorMap errors;
53 StringToIntEnumListPolicyHandler handler(
54 kTestPolicy,
55 kTestPref,
56 kTestTypeMap,
57 kTestTypeMap + arraysize(kTestTypeMap));
59 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
60 POLICY_SCOPE_USER, list.DeepCopy(), NULL);
61 errors.Clear();
62 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
63 EXPECT_TRUE(errors.empty());
65 list.AppendString("one");
66 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
67 POLICY_SCOPE_USER, list.DeepCopy(), NULL);
68 errors.Clear();
69 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
70 EXPECT_TRUE(errors.empty());
72 list.AppendString("invalid");
73 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
74 POLICY_SCOPE_USER, list.DeepCopy(), NULL);
75 errors.Clear();
76 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
77 EXPECT_FALSE(errors.empty());
78 EXPECT_FALSE(errors.GetErrors(kTestPolicy).empty());
80 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
81 POLICY_SCOPE_USER,
82 base::Value::CreateStringValue("no list"), NULL);
83 errors.Clear();
84 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
85 EXPECT_FALSE(errors.empty());
86 EXPECT_FALSE(errors.GetErrors(kTestPolicy).empty());
89 TEST(StringToIntEnumListPolicyHandlerTest, ApplyPolicySettings) {
90 base::ListValue list;
91 base::ListValue expected;
92 PolicyMap policy_map;
93 PrefValueMap prefs;
94 base::Value* value;
95 StringToIntEnumListPolicyHandler handler(
96 kTestPolicy,
97 kTestPref,
98 kTestTypeMap,
99 kTestTypeMap + arraysize(kTestTypeMap));
101 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
102 POLICY_SCOPE_USER, list.DeepCopy(), NULL);
103 handler.ApplyPolicySettings(policy_map, &prefs);
104 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
105 EXPECT_TRUE(base::Value::Equals(&expected, value));
107 list.AppendString("two");
108 expected.AppendInteger(2);
109 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
110 POLICY_SCOPE_USER, list.DeepCopy(), NULL);
111 handler.ApplyPolicySettings(policy_map, &prefs);
112 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
113 EXPECT_TRUE(base::Value::Equals(&expected, value));
115 list.AppendString("invalid");
116 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
117 POLICY_SCOPE_USER, list.DeepCopy(), NULL);
118 handler.ApplyPolicySettings(policy_map, &prefs);
119 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
120 EXPECT_TRUE(base::Value::Equals(&expected, value));
123 TEST(IntRangePolicyHandler, CheckPolicySettingsClamp) {
124 PolicyMap policy_map;
125 PolicyErrorMap errors;
127 // This tests needs to modify an int policy. The exact policy used and its
128 // semantics outside the test are irrelevant.
129 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true);
131 // Check that values lying in the accepted range are not rejected.
132 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
133 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL);
134 errors.Clear();
135 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
136 EXPECT_TRUE(errors.empty());
138 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
139 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(5), NULL);
140 errors.Clear();
141 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
142 EXPECT_TRUE(errors.empty());
144 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
145 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(10), NULL);
146 errors.Clear();
147 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
148 EXPECT_TRUE(errors.empty());
150 // Check that values lying outside the accepted range are not rejected
151 // (because clamping is enabled) but do yield a warning message.
152 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
153 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(-5), NULL);
154 errors.Clear();
155 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
156 EXPECT_FALSE(errors.empty());
158 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
159 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(15), NULL);
160 errors.Clear();
161 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
162 EXPECT_FALSE(errors.empty());
164 // Check that an entirely invalid value is rejected and yields an error
165 // message.
166 policy_map.Set(kTestPolicy,
167 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
168 base::Value::CreateStringValue("invalid"), NULL);
169 errors.Clear();
170 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
171 EXPECT_FALSE(errors.empty());
174 TEST(IntRangePolicyHandler, CheckPolicySettingsDontClamp) {
175 PolicyMap policy_map;
176 PolicyErrorMap errors;
178 // This tests needs to modify an int policy. The exact policy used and its
179 // semantics outside the test are irrelevant.
180 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, false);
182 // Check that values lying in the accepted range are not rejected.
183 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
184 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL);
185 errors.Clear();
186 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
187 EXPECT_TRUE(errors.empty());
189 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
190 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(5), NULL);
191 errors.Clear();
192 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
193 EXPECT_TRUE(errors.empty());
195 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
196 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(10), NULL);
197 errors.Clear();
198 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
199 EXPECT_TRUE(errors.empty());
201 // Check that values lying outside the accepted range are rejected and yield
202 // an error message.
203 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
204 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(-5), NULL);
205 errors.Clear();
206 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
207 EXPECT_FALSE(errors.empty());
209 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
210 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(15), NULL);
211 errors.Clear();
212 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
213 EXPECT_FALSE(errors.empty());
215 // Check that an entirely invalid value is rejected and yields an error
216 // message.
217 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
218 base::Value::CreateStringValue("invalid"), NULL);
219 errors.Clear();
220 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
221 EXPECT_FALSE(errors.empty());
224 TEST(IntRangePolicyHandler, ApplyPolicySettingsClamp) {
225 PolicyMap policy_map;
226 PrefValueMap prefs;
227 scoped_ptr<base::Value> expected;
228 const base::Value* value;
230 // This tests needs to modify an int policy. The exact policy used and its
231 // semantics outside the test are irrelevant.
232 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true);
234 // Check that values lying in the accepted range are written to the pref.
235 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
236 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL);
237 prefs.Clear();
238 handler.ApplyPolicySettings(policy_map, &prefs);
239 expected.reset(base::Value::CreateIntegerValue(0));
240 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
241 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
243 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
244 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(5), NULL);
245 prefs.Clear();
246 handler.ApplyPolicySettings(policy_map, &prefs);
247 expected.reset(base::Value::CreateIntegerValue(5));
248 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
249 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
251 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
252 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(10), NULL);
253 prefs.Clear();
254 handler.ApplyPolicySettings(policy_map, &prefs);
255 expected.reset(base::Value::CreateIntegerValue(10));
256 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
257 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
259 // Check that values lying outside the accepted range are clamped and written
260 // to the pref.
261 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
262 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(-5), NULL);
263 prefs.Clear();
264 handler.ApplyPolicySettings(policy_map, &prefs);
265 expected.reset(base::Value::CreateIntegerValue(0));
266 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
267 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
269 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
270 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(15), NULL);
271 prefs.Clear();
272 handler.ApplyPolicySettings(policy_map, &prefs);
273 expected.reset(base::Value::CreateIntegerValue(10));
274 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
275 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
278 TEST(IntRangePolicyHandler, ApplyPolicySettingsDontClamp) {
279 PolicyMap policy_map;
280 PrefValueMap prefs;
281 scoped_ptr<base::Value> expected;
282 const base::Value* value;
284 // This tests needs to modify an int policy. The exact policy used and its
285 // semantics outside the test are irrelevant.
286 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true);
288 // Check that values lying in the accepted range are written to the pref.
289 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
290 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL);
291 prefs.Clear();
292 handler.ApplyPolicySettings(policy_map, &prefs);
293 expected.reset(base::Value::CreateIntegerValue(0));
294 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
295 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
297 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
298 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(5), NULL);
299 prefs.Clear();
300 handler.ApplyPolicySettings(policy_map, &prefs);
301 expected.reset(base::Value::CreateIntegerValue(5));
302 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
303 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
305 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
306 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(10), NULL);
307 prefs.Clear();
308 handler.ApplyPolicySettings(policy_map, &prefs);
309 expected.reset(base::Value::CreateIntegerValue(10));
310 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
311 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
314 TEST(IntPercentageToDoublePolicyHandler, CheckPolicySettingsClamp) {
315 PolicyMap policy_map;
316 PolicyErrorMap errors;
318 // This tests needs to modify an int policy. The exact policy used and its
319 // semantics outside the test are irrelevant.
320 IntPercentageToDoublePolicyHandler handler(
321 kTestPolicy, kTestPref, 0, 10, true);
323 // Check that values lying in the accepted range are not rejected.
324 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
325 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL);
326 errors.Clear();
327 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
328 EXPECT_TRUE(errors.empty());
330 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
331 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(5), NULL);
332 errors.Clear();
333 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
334 EXPECT_TRUE(errors.empty());
336 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
337 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(10), NULL);
338 errors.Clear();
339 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
340 EXPECT_TRUE(errors.empty());
342 // Check that values lying outside the accepted range are not rejected
343 // (because clamping is enabled) but do yield a warning message.
344 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
345 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(-5), NULL);
346 errors.Clear();
347 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
348 EXPECT_FALSE(errors.empty());
350 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
351 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(15), NULL);
352 errors.Clear();
353 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
354 EXPECT_FALSE(errors.empty());
356 // Check that an entirely invalid value is rejected and yields an error
357 // message.
358 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
359 base::Value::CreateStringValue("invalid"), NULL);
360 errors.Clear();
361 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
362 EXPECT_FALSE(errors.empty());
365 TEST(IntPercentageToDoublePolicyHandler, CheckPolicySettingsDontClamp) {
366 PolicyMap policy_map;
367 PolicyErrorMap errors;
369 // This tests needs to modify an int policy. The exact policy used and its
370 // semantics outside the test are irrelevant.
371 IntPercentageToDoublePolicyHandler handler(
372 kTestPolicy, kTestPref, 0, 10, false);
374 // Check that values lying in the accepted range are not rejected.
375 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
376 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL);
377 errors.Clear();
378 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
379 EXPECT_TRUE(errors.empty());
381 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
382 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(5), NULL);
383 errors.Clear();
384 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
385 EXPECT_TRUE(errors.empty());
387 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
388 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(10), NULL);
389 errors.Clear();
390 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
391 EXPECT_TRUE(errors.empty());
393 // Check that values lying outside the accepted range are rejected and yield
394 // an error message.
395 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
396 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(-5), NULL);
397 errors.Clear();
398 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
399 EXPECT_FALSE(errors.empty());
401 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
402 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(15), NULL);
403 errors.Clear();
404 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
405 EXPECT_FALSE(errors.empty());
407 // Check that an entirely invalid value is rejected and yields an error
408 // message.
409 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
410 base::Value::CreateStringValue("invalid"), NULL);
411 errors.Clear();
412 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
413 EXPECT_FALSE(errors.empty());
416 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsClamp) {
417 PolicyMap policy_map;
418 PrefValueMap prefs;
419 scoped_ptr<base::Value> expected;
420 const base::Value* value;
422 // This tests needs to modify an int policy. The exact policy used and its
423 // semantics outside the test are irrelevant.
424 IntPercentageToDoublePolicyHandler handler(
425 kTestPolicy, kTestPref, 0, 10, true);
427 // Check that values lying in the accepted range are written to the pref.
428 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
429 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL);
430 prefs.Clear();
431 handler.ApplyPolicySettings(policy_map, &prefs);
432 expected.reset(base::Value::CreateDoubleValue(0.0));
433 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
434 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
436 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
437 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(5), NULL);
438 prefs.Clear();
439 handler.ApplyPolicySettings(policy_map, &prefs);
440 expected.reset(base::Value::CreateDoubleValue(0.05));
441 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
442 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
444 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
445 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(10), NULL);
446 prefs.Clear();
447 handler.ApplyPolicySettings(policy_map, &prefs);
448 expected.reset(base::Value::CreateDoubleValue(0.1));
449 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
450 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
452 // Check that values lying outside the accepted range are clamped and written
453 // to the pref.
454 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
455 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(-5), NULL);
456 prefs.Clear();
457 handler.ApplyPolicySettings(policy_map, &prefs);
458 expected.reset(base::Value::CreateDoubleValue(0.0));
459 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
460 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
462 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
463 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(15), NULL);
464 prefs.Clear();
465 handler.ApplyPolicySettings(policy_map, &prefs);
466 expected.reset(base::Value::CreateDoubleValue(0.1));
467 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
468 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
471 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsDontClamp) {
472 PolicyMap policy_map;
473 PrefValueMap prefs;
474 scoped_ptr<base::Value> expected;
475 const base::Value* value;
477 // This tests needs to modify an int policy. The exact policy used and its
478 // semantics outside the test are irrelevant.
479 IntPercentageToDoublePolicyHandler handler(
480 kTestPolicy, kTestPref, 0, 10, true);
482 // Check that values lying in the accepted range are written to the pref.
483 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
484 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(0), NULL);
485 prefs.Clear();
486 handler.ApplyPolicySettings(policy_map, &prefs);
487 expected.reset(base::Value::CreateDoubleValue(0.0));
488 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
489 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
491 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
492 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(5), NULL);
493 prefs.Clear();
494 handler.ApplyPolicySettings(policy_map, &prefs);
495 expected.reset(base::Value::CreateDoubleValue(0.05));
496 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
497 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
499 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
500 POLICY_SCOPE_USER, base::Value::CreateIntegerValue(10), NULL);
501 prefs.Clear();
502 handler.ApplyPolicySettings(policy_map, &prefs);
503 expected.reset(base::Value::CreateDoubleValue(0.1));
504 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
505 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
508 TEST(SchemaValidatingPolicyHandlerTest, CheckAndGetValue) {
509 std::string error;
510 static const char kSchemaJson[] =
512 " \"type\": \"object\","
513 " \"properties\": {"
514 " \"OneToThree\": {"
515 " \"type\": \"integer\","
516 " \"minimum\": 1,"
517 " \"maximum\": 3"
518 " },"
519 " \"Colors\": {"
520 " \"type\": \"string\","
521 " \"enum\": [ \"Red\", \"Green\", \"Blue\" ]"
522 " }"
523 " }"
524 "}";
525 Schema schema = Schema::Parse(kSchemaJson, &error);
526 ASSERT_TRUE(schema.valid()) << error;
528 static const char kPolicyMapJson[] =
530 " \"PolicyForTesting\": {"
531 " \"OneToThree\": 2,"
532 " \"Colors\": \"White\""
533 " }"
534 "}";
535 scoped_ptr<base::Value> policy_map_value(base::JSONReader::ReadAndReturnError(
536 kPolicyMapJson, base::JSON_PARSE_RFC, NULL, &error));
537 ASSERT_TRUE(policy_map_value) << error;
539 const base::DictionaryValue* policy_map_dict = NULL;
540 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict));
542 PolicyMap policy_map;
543 policy_map.LoadFrom(
544 policy_map_dict, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER);
546 SimpleSchemaValidatingPolicyHandler handler(schema, SCHEMA_ALLOW_INVALID);
547 scoped_ptr<base::Value> output_value;
548 ASSERT_TRUE(handler.CheckAndGetValueForTest(policy_map, &output_value));
549 ASSERT_TRUE(output_value);
551 base::DictionaryValue* dict = NULL;
552 ASSERT_TRUE(output_value->GetAsDictionary(&dict));
554 // Test that CheckAndGetValue() actually dropped invalid properties.
555 int int_value = -1;
556 EXPECT_TRUE(dict->GetInteger("OneToThree", &int_value));
557 EXPECT_EQ(2, int_value);
558 EXPECT_FALSE(dict->HasKey("Colors"));
561 } // namespace policy