cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / policy / core / browser / configuration_policy_handler_unittest.cc
blob67b66049ae9f090344caeeefb58c2d1cad61c856
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/bind.h"
6 #include "base/callback.h"
7 #include "base/json/json_reader.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/prefs/pref_value_map.h"
10 #include "base/values.h"
11 #include "components/policy/core/browser/configuration_policy_handler.h"
12 #include "components/policy/core/browser/policy_error_map.h"
13 #include "components/policy/core/common/policy_map.h"
14 #include "components/policy/core/common/schema.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace policy {
19 namespace {
21 void GetIntegerTypeMap(
22 ScopedVector<StringMappingListPolicyHandler::MappingEntry>* result) {
23 result->push_back(new StringMappingListPolicyHandler::MappingEntry(
24 "one", scoped_ptr<base::Value>(new base::FundamentalValue(1))));
25 result->push_back(new StringMappingListPolicyHandler::MappingEntry(
26 "two", scoped_ptr<base::Value>(new base::FundamentalValue(2))));
29 const char kTestPolicy[] = "unit_test.test_policy";
30 const char kTestPref[] = "unit_test.test_pref";
32 class TestSchemaValidatingPolicyHandler : public SchemaValidatingPolicyHandler {
33 public:
34 TestSchemaValidatingPolicyHandler(const Schema& schema,
35 SchemaOnErrorStrategy strategy)
36 : SchemaValidatingPolicyHandler("PolicyForTesting", schema, strategy) {}
37 ~TestSchemaValidatingPolicyHandler() override {}
39 void ApplyPolicySettings(const policy::PolicyMap&, PrefValueMap*) override {}
41 bool CheckAndGetValueForTest(const PolicyMap& policies,
42 scoped_ptr<base::Value>* value) {
43 return SchemaValidatingPolicyHandler::CheckAndGetValue(
44 policies, NULL, value);
48 } // namespace
50 TEST(StringToIntEnumListPolicyHandlerTest, CheckPolicySettings) {
51 base::ListValue list;
52 PolicyMap policy_map;
53 PolicyErrorMap errors;
54 StringMappingListPolicyHandler handler(
55 kTestPolicy,
56 kTestPref,
57 base::Bind(GetIntegerTypeMap));
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,
81 POLICY_LEVEL_MANDATORY,
82 POLICY_SCOPE_USER,
83 new base::StringValue("no list"),
84 NULL);
85 errors.Clear();
86 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
87 EXPECT_FALSE(errors.empty());
88 EXPECT_FALSE(errors.GetErrors(kTestPolicy).empty());
91 TEST(StringMappingListPolicyHandlerTest, ApplyPolicySettings) {
92 base::ListValue list;
93 base::ListValue expected;
94 PolicyMap policy_map;
95 PrefValueMap prefs;
96 base::Value* value;
97 StringMappingListPolicyHandler handler(
98 kTestPolicy,
99 kTestPref,
100 base::Bind(GetIntegerTypeMap));
102 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
103 POLICY_SCOPE_USER, list.DeepCopy(), NULL);
104 handler.ApplyPolicySettings(policy_map, &prefs);
105 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
106 EXPECT_TRUE(base::Value::Equals(&expected, value));
108 list.AppendString("two");
109 expected.AppendInteger(2);
110 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
111 POLICY_SCOPE_USER, list.DeepCopy(), NULL);
112 handler.ApplyPolicySettings(policy_map, &prefs);
113 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
114 EXPECT_TRUE(base::Value::Equals(&expected, value));
116 list.AppendString("invalid");
117 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY,
118 POLICY_SCOPE_USER, list.DeepCopy(), NULL);
119 handler.ApplyPolicySettings(policy_map, &prefs);
120 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
121 EXPECT_TRUE(base::Value::Equals(&expected, value));
124 TEST(IntRangePolicyHandler, CheckPolicySettingsClamp) {
125 PolicyMap policy_map;
126 PolicyErrorMap errors;
128 // This tests needs to modify an int policy. The exact policy used and its
129 // semantics outside the test are irrelevant.
130 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true);
132 // Check that values lying in the accepted range are not rejected.
133 policy_map.Set(kTestPolicy,
134 POLICY_LEVEL_MANDATORY,
135 POLICY_SCOPE_USER,
136 new base::FundamentalValue(0),
137 NULL);
138 errors.Clear();
139 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
140 EXPECT_TRUE(errors.empty());
142 policy_map.Set(kTestPolicy,
143 POLICY_LEVEL_MANDATORY,
144 POLICY_SCOPE_USER,
145 new base::FundamentalValue(5),
146 NULL);
147 errors.Clear();
148 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
149 EXPECT_TRUE(errors.empty());
151 policy_map.Set(kTestPolicy,
152 POLICY_LEVEL_MANDATORY,
153 POLICY_SCOPE_USER,
154 new base::FundamentalValue(10),
155 NULL);
156 errors.Clear();
157 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
158 EXPECT_TRUE(errors.empty());
160 // Check that values lying outside the accepted range are not rejected
161 // (because clamping is enabled) but do yield a warning message.
162 policy_map.Set(kTestPolicy,
163 POLICY_LEVEL_MANDATORY,
164 POLICY_SCOPE_USER,
165 new base::FundamentalValue(-5),
166 NULL);
167 errors.Clear();
168 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
169 EXPECT_FALSE(errors.empty());
171 policy_map.Set(kTestPolicy,
172 POLICY_LEVEL_MANDATORY,
173 POLICY_SCOPE_USER,
174 new base::FundamentalValue(15),
175 NULL);
176 errors.Clear();
177 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
178 EXPECT_FALSE(errors.empty());
180 // Check that an entirely invalid value is rejected and yields an error
181 // message.
182 policy_map.Set(kTestPolicy,
183 POLICY_LEVEL_MANDATORY,
184 POLICY_SCOPE_USER,
185 new base::StringValue("invalid"),
186 NULL);
187 errors.Clear();
188 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
189 EXPECT_FALSE(errors.empty());
192 TEST(IntRangePolicyHandler, CheckPolicySettingsDontClamp) {
193 PolicyMap policy_map;
194 PolicyErrorMap errors;
196 // This tests needs to modify an int policy. The exact policy used and its
197 // semantics outside the test are irrelevant.
198 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, false);
200 // Check that values lying in the accepted range are not rejected.
201 policy_map.Set(kTestPolicy,
202 POLICY_LEVEL_MANDATORY,
203 POLICY_SCOPE_USER,
204 new base::FundamentalValue(0),
205 NULL);
206 errors.Clear();
207 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
208 EXPECT_TRUE(errors.empty());
210 policy_map.Set(kTestPolicy,
211 POLICY_LEVEL_MANDATORY,
212 POLICY_SCOPE_USER,
213 new base::FundamentalValue(5),
214 NULL);
215 errors.Clear();
216 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
217 EXPECT_TRUE(errors.empty());
219 policy_map.Set(kTestPolicy,
220 POLICY_LEVEL_MANDATORY,
221 POLICY_SCOPE_USER,
222 new base::FundamentalValue(10),
223 NULL);
224 errors.Clear();
225 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
226 EXPECT_TRUE(errors.empty());
228 // Check that values lying outside the accepted range are rejected and yield
229 // an error message.
230 policy_map.Set(kTestPolicy,
231 POLICY_LEVEL_MANDATORY,
232 POLICY_SCOPE_USER,
233 new base::FundamentalValue(-5),
234 NULL);
235 errors.Clear();
236 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
237 EXPECT_FALSE(errors.empty());
239 policy_map.Set(kTestPolicy,
240 POLICY_LEVEL_MANDATORY,
241 POLICY_SCOPE_USER,
242 new base::FundamentalValue(15),
243 NULL);
244 errors.Clear();
245 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
246 EXPECT_FALSE(errors.empty());
248 // Check that an entirely invalid value is rejected and yields an error
249 // message.
250 policy_map.Set(kTestPolicy,
251 POLICY_LEVEL_MANDATORY,
252 POLICY_SCOPE_USER,
253 new base::StringValue("invalid"),
254 NULL);
255 errors.Clear();
256 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
257 EXPECT_FALSE(errors.empty());
260 TEST(IntRangePolicyHandler, ApplyPolicySettingsClamp) {
261 PolicyMap policy_map;
262 PrefValueMap prefs;
263 scoped_ptr<base::Value> expected;
264 const base::Value* value;
266 // This tests needs to modify an int policy. The exact policy used and its
267 // semantics outside the test are irrelevant.
268 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true);
270 // Check that values lying in the accepted range are written to the pref.
271 policy_map.Set(kTestPolicy,
272 POLICY_LEVEL_MANDATORY,
273 POLICY_SCOPE_USER,
274 new base::FundamentalValue(0),
275 NULL);
276 prefs.Clear();
277 handler.ApplyPolicySettings(policy_map, &prefs);
278 expected.reset(new base::FundamentalValue(0));
279 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
280 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
282 policy_map.Set(kTestPolicy,
283 POLICY_LEVEL_MANDATORY,
284 POLICY_SCOPE_USER,
285 new base::FundamentalValue(5),
286 NULL);
287 prefs.Clear();
288 handler.ApplyPolicySettings(policy_map, &prefs);
289 expected.reset(new base::FundamentalValue(5));
290 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
291 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
293 policy_map.Set(kTestPolicy,
294 POLICY_LEVEL_MANDATORY,
295 POLICY_SCOPE_USER,
296 new base::FundamentalValue(10),
297 NULL);
298 prefs.Clear();
299 handler.ApplyPolicySettings(policy_map, &prefs);
300 expected.reset(new base::FundamentalValue(10));
301 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
302 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
304 // Check that values lying outside the accepted range are clamped and written
305 // to the pref.
306 policy_map.Set(kTestPolicy,
307 POLICY_LEVEL_MANDATORY,
308 POLICY_SCOPE_USER,
309 new base::FundamentalValue(-5),
310 NULL);
311 prefs.Clear();
312 handler.ApplyPolicySettings(policy_map, &prefs);
313 expected.reset(new base::FundamentalValue(0));
314 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
315 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
317 policy_map.Set(kTestPolicy,
318 POLICY_LEVEL_MANDATORY,
319 POLICY_SCOPE_USER,
320 new base::FundamentalValue(15),
321 NULL);
322 prefs.Clear();
323 handler.ApplyPolicySettings(policy_map, &prefs);
324 expected.reset(new base::FundamentalValue(10));
325 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
326 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
329 TEST(IntRangePolicyHandler, ApplyPolicySettingsDontClamp) {
330 PolicyMap policy_map;
331 PrefValueMap prefs;
332 scoped_ptr<base::Value> expected;
333 const base::Value* value;
335 // This tests needs to modify an int policy. The exact policy used and its
336 // semantics outside the test are irrelevant.
337 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true);
339 // Check that values lying in the accepted range are written to the pref.
340 policy_map.Set(kTestPolicy,
341 POLICY_LEVEL_MANDATORY,
342 POLICY_SCOPE_USER,
343 new base::FundamentalValue(0),
344 NULL);
345 prefs.Clear();
346 handler.ApplyPolicySettings(policy_map, &prefs);
347 expected.reset(new base::FundamentalValue(0));
348 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
349 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
351 policy_map.Set(kTestPolicy,
352 POLICY_LEVEL_MANDATORY,
353 POLICY_SCOPE_USER,
354 new base::FundamentalValue(5),
355 NULL);
356 prefs.Clear();
357 handler.ApplyPolicySettings(policy_map, &prefs);
358 expected.reset(new base::FundamentalValue(5));
359 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
360 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
362 policy_map.Set(kTestPolicy,
363 POLICY_LEVEL_MANDATORY,
364 POLICY_SCOPE_USER,
365 new base::FundamentalValue(10),
366 NULL);
367 prefs.Clear();
368 handler.ApplyPolicySettings(policy_map, &prefs);
369 expected.reset(new base::FundamentalValue(10));
370 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
371 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
374 TEST(IntPercentageToDoublePolicyHandler, CheckPolicySettingsClamp) {
375 PolicyMap policy_map;
376 PolicyErrorMap errors;
378 // This tests needs to modify an int policy. The exact policy used and its
379 // semantics outside the test are irrelevant.
380 IntPercentageToDoublePolicyHandler handler(
381 kTestPolicy, kTestPref, 0, 10, true);
383 // Check that values lying in the accepted range are not rejected.
384 policy_map.Set(kTestPolicy,
385 POLICY_LEVEL_MANDATORY,
386 POLICY_SCOPE_USER,
387 new base::FundamentalValue(0),
388 NULL);
389 errors.Clear();
390 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
391 EXPECT_TRUE(errors.empty());
393 policy_map.Set(kTestPolicy,
394 POLICY_LEVEL_MANDATORY,
395 POLICY_SCOPE_USER,
396 new base::FundamentalValue(5),
397 NULL);
398 errors.Clear();
399 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
400 EXPECT_TRUE(errors.empty());
402 policy_map.Set(kTestPolicy,
403 POLICY_LEVEL_MANDATORY,
404 POLICY_SCOPE_USER,
405 new base::FundamentalValue(10),
406 NULL);
407 errors.Clear();
408 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
409 EXPECT_TRUE(errors.empty());
411 // Check that values lying outside the accepted range are not rejected
412 // (because clamping is enabled) but do yield a warning message.
413 policy_map.Set(kTestPolicy,
414 POLICY_LEVEL_MANDATORY,
415 POLICY_SCOPE_USER,
416 new base::FundamentalValue(-5),
417 NULL);
418 errors.Clear();
419 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
420 EXPECT_FALSE(errors.empty());
422 policy_map.Set(kTestPolicy,
423 POLICY_LEVEL_MANDATORY,
424 POLICY_SCOPE_USER,
425 new base::FundamentalValue(15),
426 NULL);
427 errors.Clear();
428 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
429 EXPECT_FALSE(errors.empty());
431 // Check that an entirely invalid value is rejected and yields an error
432 // message.
433 policy_map.Set(kTestPolicy,
434 POLICY_LEVEL_MANDATORY,
435 POLICY_SCOPE_USER,
436 new base::StringValue("invalid"),
437 NULL);
438 errors.Clear();
439 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
440 EXPECT_FALSE(errors.empty());
443 TEST(IntPercentageToDoublePolicyHandler, CheckPolicySettingsDontClamp) {
444 PolicyMap policy_map;
445 PolicyErrorMap errors;
447 // This tests needs to modify an int policy. The exact policy used and its
448 // semantics outside the test are irrelevant.
449 IntPercentageToDoublePolicyHandler handler(
450 kTestPolicy, kTestPref, 0, 10, false);
452 // Check that values lying in the accepted range are not rejected.
453 policy_map.Set(kTestPolicy,
454 POLICY_LEVEL_MANDATORY,
455 POLICY_SCOPE_USER,
456 new base::FundamentalValue(0),
457 NULL);
458 errors.Clear();
459 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
460 EXPECT_TRUE(errors.empty());
462 policy_map.Set(kTestPolicy,
463 POLICY_LEVEL_MANDATORY,
464 POLICY_SCOPE_USER,
465 new base::FundamentalValue(5),
466 NULL);
467 errors.Clear();
468 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
469 EXPECT_TRUE(errors.empty());
471 policy_map.Set(kTestPolicy,
472 POLICY_LEVEL_MANDATORY,
473 POLICY_SCOPE_USER,
474 new base::FundamentalValue(10),
475 NULL);
476 errors.Clear();
477 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
478 EXPECT_TRUE(errors.empty());
480 // Check that values lying outside the accepted range are rejected and yield
481 // an error message.
482 policy_map.Set(kTestPolicy,
483 POLICY_LEVEL_MANDATORY,
484 POLICY_SCOPE_USER,
485 new base::FundamentalValue(-5),
486 NULL);
487 errors.Clear();
488 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
489 EXPECT_FALSE(errors.empty());
491 policy_map.Set(kTestPolicy,
492 POLICY_LEVEL_MANDATORY,
493 POLICY_SCOPE_USER,
494 new base::FundamentalValue(15),
495 NULL);
496 errors.Clear();
497 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
498 EXPECT_FALSE(errors.empty());
500 // Check that an entirely invalid value is rejected and yields an error
501 // message.
502 policy_map.Set(kTestPolicy,
503 POLICY_LEVEL_MANDATORY,
504 POLICY_SCOPE_USER,
505 new base::StringValue("invalid"),
506 NULL);
507 errors.Clear();
508 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
509 EXPECT_FALSE(errors.empty());
512 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsClamp) {
513 PolicyMap policy_map;
514 PrefValueMap prefs;
515 scoped_ptr<base::Value> expected;
516 const base::Value* value;
518 // This tests needs to modify an int policy. The exact policy used and its
519 // semantics outside the test are irrelevant.
520 IntPercentageToDoublePolicyHandler handler(
521 kTestPolicy, kTestPref, 0, 10, true);
523 // Check that values lying in the accepted range are written to the pref.
524 policy_map.Set(kTestPolicy,
525 POLICY_LEVEL_MANDATORY,
526 POLICY_SCOPE_USER,
527 new base::FundamentalValue(0),
528 NULL);
529 prefs.Clear();
530 handler.ApplyPolicySettings(policy_map, &prefs);
531 expected.reset(new base::FundamentalValue(0.0));
532 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
533 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
535 policy_map.Set(kTestPolicy,
536 POLICY_LEVEL_MANDATORY,
537 POLICY_SCOPE_USER,
538 new base::FundamentalValue(5),
539 NULL);
540 prefs.Clear();
541 handler.ApplyPolicySettings(policy_map, &prefs);
542 expected.reset(new base::FundamentalValue(0.05));
543 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
544 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
546 policy_map.Set(kTestPolicy,
547 POLICY_LEVEL_MANDATORY,
548 POLICY_SCOPE_USER,
549 new base::FundamentalValue(10),
550 NULL);
551 prefs.Clear();
552 handler.ApplyPolicySettings(policy_map, &prefs);
553 expected.reset(new base::FundamentalValue(0.1));
554 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
555 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
557 // Check that values lying outside the accepted range are clamped and written
558 // to the pref.
559 policy_map.Set(kTestPolicy,
560 POLICY_LEVEL_MANDATORY,
561 POLICY_SCOPE_USER,
562 new base::FundamentalValue(-5),
563 NULL);
564 prefs.Clear();
565 handler.ApplyPolicySettings(policy_map, &prefs);
566 expected.reset(new base::FundamentalValue(0.0));
567 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
568 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
570 policy_map.Set(kTestPolicy,
571 POLICY_LEVEL_MANDATORY,
572 POLICY_SCOPE_USER,
573 new base::FundamentalValue(15),
574 NULL);
575 prefs.Clear();
576 handler.ApplyPolicySettings(policy_map, &prefs);
577 expected.reset(new base::FundamentalValue(0.1));
578 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
579 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
582 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsDontClamp) {
583 PolicyMap policy_map;
584 PrefValueMap prefs;
585 scoped_ptr<base::Value> expected;
586 const base::Value* value;
588 // This tests needs to modify an int policy. The exact policy used and its
589 // semantics outside the test are irrelevant.
590 IntPercentageToDoublePolicyHandler handler(
591 kTestPolicy, kTestPref, 0, 10, true);
593 // Check that values lying in the accepted range are written to the pref.
594 policy_map.Set(kTestPolicy,
595 POLICY_LEVEL_MANDATORY,
596 POLICY_SCOPE_USER,
597 new base::FundamentalValue(0),
598 NULL);
599 prefs.Clear();
600 handler.ApplyPolicySettings(policy_map, &prefs);
601 expected.reset(new base::FundamentalValue(0.0));
602 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
603 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
605 policy_map.Set(kTestPolicy,
606 POLICY_LEVEL_MANDATORY,
607 POLICY_SCOPE_USER,
608 new base::FundamentalValue(5),
609 NULL);
610 prefs.Clear();
611 handler.ApplyPolicySettings(policy_map, &prefs);
612 expected.reset(new base::FundamentalValue(0.05));
613 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
614 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
616 policy_map.Set(kTestPolicy,
617 POLICY_LEVEL_MANDATORY,
618 POLICY_SCOPE_USER,
619 new base::FundamentalValue(10),
620 NULL);
621 prefs.Clear();
622 handler.ApplyPolicySettings(policy_map, &prefs);
623 expected.reset(new base::FundamentalValue(0.1));
624 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
625 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
628 TEST(SchemaValidatingPolicyHandlerTest, CheckAndGetValue) {
629 std::string error;
630 static const char kSchemaJson[] =
632 " \"type\": \"object\","
633 " \"properties\": {"
634 " \"OneToThree\": {"
635 " \"type\": \"integer\","
636 " \"minimum\": 1,"
637 " \"maximum\": 3"
638 " },"
639 " \"Colors\": {"
640 " \"type\": \"string\","
641 " \"enum\": [ \"Red\", \"Green\", \"Blue\" ]"
642 " }"
643 " }"
644 "}";
645 Schema schema = Schema::Parse(kSchemaJson, &error);
646 ASSERT_TRUE(schema.valid()) << error;
648 static const char kPolicyMapJson[] =
650 " \"PolicyForTesting\": {"
651 " \"OneToThree\": 2,"
652 " \"Colors\": \"White\""
653 " }"
654 "}";
655 scoped_ptr<base::Value> policy_map_value =
656 base::JSONReader::ReadAndReturnError(kPolicyMapJson, base::JSON_PARSE_RFC,
657 NULL, &error);
658 ASSERT_TRUE(policy_map_value) << error;
660 const base::DictionaryValue* policy_map_dict = NULL;
661 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict));
663 PolicyMap policy_map;
664 policy_map.LoadFrom(
665 policy_map_dict, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER);
667 TestSchemaValidatingPolicyHandler handler(schema, SCHEMA_ALLOW_INVALID);
668 scoped_ptr<base::Value> output_value;
669 ASSERT_TRUE(handler.CheckAndGetValueForTest(policy_map, &output_value));
670 ASSERT_TRUE(output_value);
672 base::DictionaryValue* dict = NULL;
673 ASSERT_TRUE(output_value->GetAsDictionary(&dict));
675 // Test that CheckAndGetValue() actually dropped invalid properties.
676 int int_value = -1;
677 EXPECT_TRUE(dict->GetInteger("OneToThree", &int_value));
678 EXPECT_EQ(2, int_value);
679 EXPECT_FALSE(dict->HasKey("Colors"));
682 TEST(SimpleSchemaValidatingPolicyHandlerTest, CheckAndGetValue) {
683 const char policy_name[] = "PolicyForTesting";
684 static const char kSchemaJson[] =
686 " \"type\": \"object\","
687 " \"properties\": {"
688 " \"PolicyForTesting\": {"
689 " \"type\": \"object\","
690 " \"properties\": {"
691 " \"OneToThree\": {"
692 " \"type\": \"integer\","
693 " \"minimum\": 1,"
694 " \"maximum\": 3"
695 " },"
696 " \"Colors\": {"
697 " \"type\": \"string\","
698 " \"enum\": [ \"Red\", \"Green\", \"Blue\" ]"
699 " }"
700 " }"
701 " }"
702 " }"
703 "}";
704 std::string error;
705 Schema schema = Schema::Parse(kSchemaJson, &error);
706 ASSERT_TRUE(schema.valid()) << error;
708 static const char kPolicyMapJson[] =
710 " \"PolicyForTesting\": {"
711 " \"OneToThree\": 2,"
712 " \"Colors\": \"Green\""
713 " }"
714 "}";
715 scoped_ptr<base::Value> policy_map_value =
716 base::JSONReader::ReadAndReturnError(kPolicyMapJson, base::JSON_PARSE_RFC,
717 NULL, &error);
718 ASSERT_TRUE(policy_map_value) << error;
720 const base::DictionaryValue* policy_map_dict = NULL;
721 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict));
723 PolicyMap policy_map_recommended;
724 policy_map_recommended.LoadFrom(
725 policy_map_dict, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER);
727 PolicyMap policy_map_mandatory;
728 policy_map_mandatory.LoadFrom(
729 policy_map_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
731 SimpleSchemaValidatingPolicyHandler handler_all(
732 policy_name,
733 kTestPref,
734 schema,
735 SCHEMA_STRICT,
736 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_ALLOWED,
737 SimpleSchemaValidatingPolicyHandler::MANDATORY_ALLOWED);
739 SimpleSchemaValidatingPolicyHandler handler_recommended(
740 policy_name,
741 kTestPref,
742 schema,
743 SCHEMA_STRICT,
744 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_ALLOWED,
745 SimpleSchemaValidatingPolicyHandler::MANDATORY_PROHIBITED);
747 SimpleSchemaValidatingPolicyHandler handler_mandatory(
748 policy_name,
749 kTestPref,
750 schema,
751 SCHEMA_STRICT,
752 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_PROHIBITED,
753 SimpleSchemaValidatingPolicyHandler::MANDATORY_ALLOWED);
755 SimpleSchemaValidatingPolicyHandler handler_none(
756 policy_name,
757 kTestPref,
758 schema,
759 SCHEMA_STRICT,
760 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_PROHIBITED,
761 SimpleSchemaValidatingPolicyHandler::MANDATORY_PROHIBITED);
763 const base::Value* value_expected_in_pref;
764 policy_map_dict->Get(policy_name, &value_expected_in_pref);
766 PolicyErrorMap errors;
767 PrefValueMap prefs;
768 base::Value* value_set_in_pref;
770 EXPECT_TRUE(handler_all.CheckPolicySettings(policy_map_mandatory, &errors));
771 EXPECT_TRUE(errors.empty());
772 prefs.Clear();
773 handler_all.ApplyPolicySettings(policy_map_mandatory, &prefs);
774 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref));
775 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref));
777 EXPECT_FALSE(
778 handler_recommended.CheckPolicySettings(policy_map_mandatory, &errors));
779 EXPECT_FALSE(errors.empty());
780 errors.Clear();
782 EXPECT_TRUE(
783 handler_mandatory.CheckPolicySettings(policy_map_mandatory, &errors));
784 EXPECT_TRUE(errors.empty());
785 prefs.Clear();
786 handler_mandatory.ApplyPolicySettings(policy_map_mandatory, &prefs);
787 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref));
788 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref));
790 EXPECT_FALSE(handler_none.CheckPolicySettings(policy_map_mandatory, &errors));
791 EXPECT_FALSE(errors.empty());
792 errors.Clear();
794 EXPECT_TRUE(handler_all.CheckPolicySettings(policy_map_recommended, &errors));
795 EXPECT_TRUE(errors.empty());
796 prefs.Clear();
797 handler_all.ApplyPolicySettings(policy_map_mandatory, &prefs);
798 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref));
799 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref));
801 EXPECT_FALSE(
802 handler_mandatory.CheckPolicySettings(policy_map_recommended, &errors));
803 EXPECT_FALSE(errors.empty());
804 errors.Clear();
806 EXPECT_TRUE(
807 handler_recommended.CheckPolicySettings(policy_map_recommended, &errors));
808 EXPECT_TRUE(errors.empty());
809 prefs.Clear();
810 handler_recommended.ApplyPolicySettings(policy_map_mandatory, &prefs);
811 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref));
812 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref));
814 EXPECT_FALSE(
815 handler_none.CheckPolicySettings(policy_map_recommended, &errors));
816 EXPECT_FALSE(errors.empty());
819 } // namespace policy