Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / policy / core / browser / configuration_policy_handler_unittest.cc
blob57db6d6fedd76d3e59cc836f2bd940cf9ebe49d9
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/policy_types.h"
15 #include "components/policy/core/common/schema.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace policy {
20 namespace {
22 void GetIntegerTypeMap(
23 ScopedVector<StringMappingListPolicyHandler::MappingEntry>* result) {
24 result->push_back(new StringMappingListPolicyHandler::MappingEntry(
25 "one", scoped_ptr<base::Value>(new base::FundamentalValue(1))));
26 result->push_back(new StringMappingListPolicyHandler::MappingEntry(
27 "two", scoped_ptr<base::Value>(new base::FundamentalValue(2))));
30 const char kTestPolicy[] = "unit_test.test_policy";
31 const char kTestPref[] = "unit_test.test_pref";
33 class TestSchemaValidatingPolicyHandler : public SchemaValidatingPolicyHandler {
34 public:
35 TestSchemaValidatingPolicyHandler(const Schema& schema,
36 SchemaOnErrorStrategy strategy)
37 : SchemaValidatingPolicyHandler("PolicyForTesting", schema, strategy) {}
38 ~TestSchemaValidatingPolicyHandler() override {}
40 void ApplyPolicySettings(const policy::PolicyMap&, PrefValueMap*) override {}
42 bool CheckAndGetValueForTest(const PolicyMap& policies,
43 scoped_ptr<base::Value>* value) {
44 return SchemaValidatingPolicyHandler::CheckAndGetValue(
45 policies, NULL, value);
49 } // namespace
51 TEST(StringToIntEnumListPolicyHandlerTest, CheckPolicySettings) {
52 base::ListValue list;
53 PolicyMap policy_map;
54 PolicyErrorMap errors;
55 StringMappingListPolicyHandler handler(
56 kTestPolicy,
57 kTestPref,
58 base::Bind(GetIntegerTypeMap));
60 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
61 POLICY_SOURCE_CLOUD, list.DeepCopy(), nullptr);
62 errors.Clear();
63 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
64 EXPECT_TRUE(errors.empty());
66 list.AppendString("one");
67 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
68 POLICY_SOURCE_CLOUD, list.DeepCopy(), nullptr);
69 errors.Clear();
70 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
71 EXPECT_TRUE(errors.empty());
73 list.AppendString("invalid");
74 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
75 POLICY_SOURCE_CLOUD, list.DeepCopy(), nullptr);
76 errors.Clear();
77 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
78 EXPECT_FALSE(errors.empty());
79 EXPECT_FALSE(errors.GetErrors(kTestPolicy).empty());
81 policy_map.Set(kTestPolicy,
82 POLICY_LEVEL_MANDATORY,
83 POLICY_SCOPE_USER,
84 POLICY_SOURCE_CLOUD,
85 new base::StringValue("no list"),
86 NULL);
87 errors.Clear();
88 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
89 EXPECT_FALSE(errors.empty());
90 EXPECT_FALSE(errors.GetErrors(kTestPolicy).empty());
93 TEST(StringMappingListPolicyHandlerTest, ApplyPolicySettings) {
94 base::ListValue list;
95 base::ListValue expected;
96 PolicyMap policy_map;
97 PrefValueMap prefs;
98 base::Value* value;
99 StringMappingListPolicyHandler handler(
100 kTestPolicy,
101 kTestPref,
102 base::Bind(GetIntegerTypeMap));
104 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
105 POLICY_SOURCE_CLOUD, list.DeepCopy(), nullptr);
106 handler.ApplyPolicySettings(policy_map, &prefs);
107 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
108 EXPECT_TRUE(base::Value::Equals(&expected, value));
110 list.AppendString("two");
111 expected.AppendInteger(2);
112 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
113 POLICY_SOURCE_CLOUD, list.DeepCopy(), nullptr);
114 handler.ApplyPolicySettings(policy_map, &prefs);
115 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
116 EXPECT_TRUE(base::Value::Equals(&expected, value));
118 list.AppendString("invalid");
119 policy_map.Set(kTestPolicy, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
120 POLICY_SOURCE_CLOUD, list.DeepCopy(), nullptr);
121 handler.ApplyPolicySettings(policy_map, &prefs);
122 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
123 EXPECT_TRUE(base::Value::Equals(&expected, value));
126 TEST(IntRangePolicyHandler, CheckPolicySettingsClamp) {
127 PolicyMap policy_map;
128 PolicyErrorMap errors;
130 // This tests needs to modify an int policy. The exact policy used and its
131 // semantics outside the test are irrelevant.
132 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true);
134 // Check that values lying in the accepted range are not rejected.
135 policy_map.Set(kTestPolicy,
136 POLICY_LEVEL_MANDATORY,
137 POLICY_SCOPE_USER,
138 POLICY_SOURCE_CLOUD,
139 new base::FundamentalValue(0),
140 NULL);
141 errors.Clear();
142 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
143 EXPECT_TRUE(errors.empty());
145 policy_map.Set(kTestPolicy,
146 POLICY_LEVEL_MANDATORY,
147 POLICY_SCOPE_USER,
148 POLICY_SOURCE_CLOUD,
149 new base::FundamentalValue(5),
150 NULL);
151 errors.Clear();
152 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
153 EXPECT_TRUE(errors.empty());
155 policy_map.Set(kTestPolicy,
156 POLICY_LEVEL_MANDATORY,
157 POLICY_SCOPE_USER,
158 POLICY_SOURCE_CLOUD,
159 new base::FundamentalValue(10),
160 NULL);
161 errors.Clear();
162 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
163 EXPECT_TRUE(errors.empty());
165 // Check that values lying outside the accepted range are not rejected
166 // (because clamping is enabled) but do yield a warning message.
167 policy_map.Set(kTestPolicy,
168 POLICY_LEVEL_MANDATORY,
169 POLICY_SCOPE_USER,
170 POLICY_SOURCE_CLOUD,
171 new base::FundamentalValue(-5),
172 NULL);
173 errors.Clear();
174 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
175 EXPECT_FALSE(errors.empty());
177 policy_map.Set(kTestPolicy,
178 POLICY_LEVEL_MANDATORY,
179 POLICY_SCOPE_USER,
180 POLICY_SOURCE_CLOUD,
181 new base::FundamentalValue(15),
182 NULL);
183 errors.Clear();
184 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
185 EXPECT_FALSE(errors.empty());
187 // Check that an entirely invalid value is rejected and yields an error
188 // message.
189 policy_map.Set(kTestPolicy,
190 POLICY_LEVEL_MANDATORY,
191 POLICY_SCOPE_USER,
192 POLICY_SOURCE_CLOUD,
193 new base::StringValue("invalid"),
194 NULL);
195 errors.Clear();
196 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
197 EXPECT_FALSE(errors.empty());
200 TEST(IntRangePolicyHandler, CheckPolicySettingsDontClamp) {
201 PolicyMap policy_map;
202 PolicyErrorMap errors;
204 // This tests needs to modify an int policy. The exact policy used and its
205 // semantics outside the test are irrelevant.
206 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, false);
208 // Check that values lying in the accepted range are not rejected.
209 policy_map.Set(kTestPolicy,
210 POLICY_LEVEL_MANDATORY,
211 POLICY_SCOPE_USER,
212 POLICY_SOURCE_CLOUD,
213 new base::FundamentalValue(0),
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 POLICY_SOURCE_CLOUD,
223 new base::FundamentalValue(5),
224 NULL);
225 errors.Clear();
226 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
227 EXPECT_TRUE(errors.empty());
229 policy_map.Set(kTestPolicy,
230 POLICY_LEVEL_MANDATORY,
231 POLICY_SCOPE_USER,
232 POLICY_SOURCE_CLOUD,
233 new base::FundamentalValue(10),
234 NULL);
235 errors.Clear();
236 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
237 EXPECT_TRUE(errors.empty());
239 // Check that values lying outside the accepted range are rejected and yield
240 // an error message.
241 policy_map.Set(kTestPolicy,
242 POLICY_LEVEL_MANDATORY,
243 POLICY_SCOPE_USER,
244 POLICY_SOURCE_CLOUD,
245 new base::FundamentalValue(-5),
246 NULL);
247 errors.Clear();
248 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
249 EXPECT_FALSE(errors.empty());
251 policy_map.Set(kTestPolicy,
252 POLICY_LEVEL_MANDATORY,
253 POLICY_SCOPE_USER,
254 POLICY_SOURCE_CLOUD,
255 new base::FundamentalValue(15),
256 NULL);
257 errors.Clear();
258 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
259 EXPECT_FALSE(errors.empty());
261 // Check that an entirely invalid value is rejected and yields an error
262 // message.
263 policy_map.Set(kTestPolicy,
264 POLICY_LEVEL_MANDATORY,
265 POLICY_SCOPE_USER,
266 POLICY_SOURCE_CLOUD,
267 new base::StringValue("invalid"),
268 NULL);
269 errors.Clear();
270 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
271 EXPECT_FALSE(errors.empty());
274 TEST(IntRangePolicyHandler, ApplyPolicySettingsClamp) {
275 PolicyMap policy_map;
276 PrefValueMap prefs;
277 scoped_ptr<base::Value> expected;
278 const base::Value* value;
280 // This tests needs to modify an int policy. The exact policy used and its
281 // semantics outside the test are irrelevant.
282 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true);
284 // Check that values lying in the accepted range are written to the pref.
285 policy_map.Set(kTestPolicy,
286 POLICY_LEVEL_MANDATORY,
287 POLICY_SCOPE_USER,
288 POLICY_SOURCE_CLOUD,
289 new base::FundamentalValue(0),
290 NULL);
291 prefs.Clear();
292 handler.ApplyPolicySettings(policy_map, &prefs);
293 expected.reset(new base::FundamentalValue(0));
294 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
295 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
297 policy_map.Set(kTestPolicy,
298 POLICY_LEVEL_MANDATORY,
299 POLICY_SCOPE_USER,
300 POLICY_SOURCE_CLOUD,
301 new base::FundamentalValue(5),
302 NULL);
303 prefs.Clear();
304 handler.ApplyPolicySettings(policy_map, &prefs);
305 expected.reset(new base::FundamentalValue(5));
306 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
307 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
309 policy_map.Set(kTestPolicy,
310 POLICY_LEVEL_MANDATORY,
311 POLICY_SCOPE_USER,
312 POLICY_SOURCE_CLOUD,
313 new base::FundamentalValue(10),
314 NULL);
315 prefs.Clear();
316 handler.ApplyPolicySettings(policy_map, &prefs);
317 expected.reset(new base::FundamentalValue(10));
318 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
319 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
321 // Check that values lying outside the accepted range are clamped and written
322 // to the pref.
323 policy_map.Set(kTestPolicy,
324 POLICY_LEVEL_MANDATORY,
325 POLICY_SCOPE_USER,
326 POLICY_SOURCE_CLOUD,
327 new base::FundamentalValue(-5),
328 NULL);
329 prefs.Clear();
330 handler.ApplyPolicySettings(policy_map, &prefs);
331 expected.reset(new base::FundamentalValue(0));
332 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
333 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
335 policy_map.Set(kTestPolicy,
336 POLICY_LEVEL_MANDATORY,
337 POLICY_SCOPE_USER,
338 POLICY_SOURCE_CLOUD,
339 new base::FundamentalValue(15),
340 NULL);
341 prefs.Clear();
342 handler.ApplyPolicySettings(policy_map, &prefs);
343 expected.reset(new base::FundamentalValue(10));
344 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
345 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
348 TEST(IntRangePolicyHandler, ApplyPolicySettingsDontClamp) {
349 PolicyMap policy_map;
350 PrefValueMap prefs;
351 scoped_ptr<base::Value> expected;
352 const base::Value* value;
354 // This tests needs to modify an int policy. The exact policy used and its
355 // semantics outside the test are irrelevant.
356 IntRangePolicyHandler handler(kTestPolicy, kTestPref, 0, 10, true);
358 // Check that values lying in the accepted range are written to the pref.
359 policy_map.Set(kTestPolicy,
360 POLICY_LEVEL_MANDATORY,
361 POLICY_SCOPE_USER,
362 POLICY_SOURCE_CLOUD,
363 new base::FundamentalValue(0),
364 NULL);
365 prefs.Clear();
366 handler.ApplyPolicySettings(policy_map, &prefs);
367 expected.reset(new base::FundamentalValue(0));
368 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
369 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
371 policy_map.Set(kTestPolicy,
372 POLICY_LEVEL_MANDATORY,
373 POLICY_SCOPE_USER,
374 POLICY_SOURCE_CLOUD,
375 new base::FundamentalValue(5),
376 NULL);
377 prefs.Clear();
378 handler.ApplyPolicySettings(policy_map, &prefs);
379 expected.reset(new base::FundamentalValue(5));
380 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
381 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
383 policy_map.Set(kTestPolicy,
384 POLICY_LEVEL_MANDATORY,
385 POLICY_SCOPE_USER,
386 POLICY_SOURCE_CLOUD,
387 new base::FundamentalValue(10),
388 NULL);
389 prefs.Clear();
390 handler.ApplyPolicySettings(policy_map, &prefs);
391 expected.reset(new base::FundamentalValue(10));
392 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
393 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
396 TEST(IntPercentageToDoublePolicyHandler, CheckPolicySettingsClamp) {
397 PolicyMap policy_map;
398 PolicyErrorMap errors;
400 // This tests needs to modify an int policy. The exact policy used and its
401 // semantics outside the test are irrelevant.
402 IntPercentageToDoublePolicyHandler handler(
403 kTestPolicy, kTestPref, 0, 10, true);
405 // Check that values lying in the accepted range are not rejected.
406 policy_map.Set(kTestPolicy,
407 POLICY_LEVEL_MANDATORY,
408 POLICY_SCOPE_USER,
409 POLICY_SOURCE_CLOUD,
410 new base::FundamentalValue(0),
411 NULL);
412 errors.Clear();
413 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
414 EXPECT_TRUE(errors.empty());
416 policy_map.Set(kTestPolicy,
417 POLICY_LEVEL_MANDATORY,
418 POLICY_SCOPE_USER,
419 POLICY_SOURCE_CLOUD,
420 new base::FundamentalValue(5),
421 NULL);
422 errors.Clear();
423 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
424 EXPECT_TRUE(errors.empty());
426 policy_map.Set(kTestPolicy,
427 POLICY_LEVEL_MANDATORY,
428 POLICY_SCOPE_USER,
429 POLICY_SOURCE_CLOUD,
430 new base::FundamentalValue(10),
431 NULL);
432 errors.Clear();
433 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
434 EXPECT_TRUE(errors.empty());
436 // Check that values lying outside the accepted range are not rejected
437 // (because clamping is enabled) but do yield a warning message.
438 policy_map.Set(kTestPolicy,
439 POLICY_LEVEL_MANDATORY,
440 POLICY_SCOPE_USER,
441 POLICY_SOURCE_CLOUD,
442 new base::FundamentalValue(-5),
443 NULL);
444 errors.Clear();
445 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
446 EXPECT_FALSE(errors.empty());
448 policy_map.Set(kTestPolicy,
449 POLICY_LEVEL_MANDATORY,
450 POLICY_SCOPE_USER,
451 POLICY_SOURCE_CLOUD,
452 new base::FundamentalValue(15),
453 NULL);
454 errors.Clear();
455 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
456 EXPECT_FALSE(errors.empty());
458 // Check that an entirely invalid value is rejected and yields an error
459 // message.
460 policy_map.Set(kTestPolicy,
461 POLICY_LEVEL_MANDATORY,
462 POLICY_SCOPE_USER,
463 POLICY_SOURCE_CLOUD,
464 new base::StringValue("invalid"),
465 NULL);
466 errors.Clear();
467 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
468 EXPECT_FALSE(errors.empty());
471 TEST(IntPercentageToDoublePolicyHandler, CheckPolicySettingsDontClamp) {
472 PolicyMap policy_map;
473 PolicyErrorMap errors;
475 // This tests needs to modify an int policy. The exact policy used and its
476 // semantics outside the test are irrelevant.
477 IntPercentageToDoublePolicyHandler handler(
478 kTestPolicy, kTestPref, 0, 10, false);
480 // Check that values lying in the accepted range are not rejected.
481 policy_map.Set(kTestPolicy,
482 POLICY_LEVEL_MANDATORY,
483 POLICY_SCOPE_USER,
484 POLICY_SOURCE_CLOUD,
485 new base::FundamentalValue(0),
486 NULL);
487 errors.Clear();
488 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
489 EXPECT_TRUE(errors.empty());
491 policy_map.Set(kTestPolicy,
492 POLICY_LEVEL_MANDATORY,
493 POLICY_SCOPE_USER,
494 POLICY_SOURCE_CLOUD,
495 new base::FundamentalValue(5),
496 NULL);
497 errors.Clear();
498 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
499 EXPECT_TRUE(errors.empty());
501 policy_map.Set(kTestPolicy,
502 POLICY_LEVEL_MANDATORY,
503 POLICY_SCOPE_USER,
504 POLICY_SOURCE_CLOUD,
505 new base::FundamentalValue(10),
506 NULL);
507 errors.Clear();
508 EXPECT_TRUE(handler.CheckPolicySettings(policy_map, &errors));
509 EXPECT_TRUE(errors.empty());
511 // Check that values lying outside the accepted range are rejected and yield
512 // an error message.
513 policy_map.Set(kTestPolicy,
514 POLICY_LEVEL_MANDATORY,
515 POLICY_SCOPE_USER,
516 POLICY_SOURCE_CLOUD,
517 new base::FundamentalValue(-5),
518 NULL);
519 errors.Clear();
520 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
521 EXPECT_FALSE(errors.empty());
523 policy_map.Set(kTestPolicy,
524 POLICY_LEVEL_MANDATORY,
525 POLICY_SCOPE_USER,
526 POLICY_SOURCE_CLOUD,
527 new base::FundamentalValue(15),
528 NULL);
529 errors.Clear();
530 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
531 EXPECT_FALSE(errors.empty());
533 // Check that an entirely invalid value is rejected and yields an error
534 // message.
535 policy_map.Set(kTestPolicy,
536 POLICY_LEVEL_MANDATORY,
537 POLICY_SCOPE_USER,
538 POLICY_SOURCE_CLOUD,
539 new base::StringValue("invalid"),
540 NULL);
541 errors.Clear();
542 EXPECT_FALSE(handler.CheckPolicySettings(policy_map, &errors));
543 EXPECT_FALSE(errors.empty());
546 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsClamp) {
547 PolicyMap policy_map;
548 PrefValueMap prefs;
549 scoped_ptr<base::Value> expected;
550 const base::Value* value;
552 // This tests needs to modify an int policy. The exact policy used and its
553 // semantics outside the test are irrelevant.
554 IntPercentageToDoublePolicyHandler handler(
555 kTestPolicy, kTestPref, 0, 10, true);
557 // Check that values lying in the accepted range are written to the pref.
558 policy_map.Set(kTestPolicy,
559 POLICY_LEVEL_MANDATORY,
560 POLICY_SCOPE_USER,
561 POLICY_SOURCE_CLOUD,
562 new base::FundamentalValue(0),
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 POLICY_SOURCE_CLOUD,
574 new base::FundamentalValue(5),
575 NULL);
576 prefs.Clear();
577 handler.ApplyPolicySettings(policy_map, &prefs);
578 expected.reset(new base::FundamentalValue(0.05));
579 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
580 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
582 policy_map.Set(kTestPolicy,
583 POLICY_LEVEL_MANDATORY,
584 POLICY_SCOPE_USER,
585 POLICY_SOURCE_CLOUD,
586 new base::FundamentalValue(10),
587 NULL);
588 prefs.Clear();
589 handler.ApplyPolicySettings(policy_map, &prefs);
590 expected.reset(new base::FundamentalValue(0.1));
591 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
592 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
594 // Check that values lying outside the accepted range are clamped and written
595 // to the pref.
596 policy_map.Set(kTestPolicy,
597 POLICY_LEVEL_MANDATORY,
598 POLICY_SCOPE_USER,
599 POLICY_SOURCE_CLOUD,
600 new base::FundamentalValue(-5),
601 NULL);
602 prefs.Clear();
603 handler.ApplyPolicySettings(policy_map, &prefs);
604 expected.reset(new base::FundamentalValue(0.0));
605 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
606 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
608 policy_map.Set(kTestPolicy,
609 POLICY_LEVEL_MANDATORY,
610 POLICY_SCOPE_USER,
611 POLICY_SOURCE_CLOUD,
612 new base::FundamentalValue(15),
613 NULL);
614 prefs.Clear();
615 handler.ApplyPolicySettings(policy_map, &prefs);
616 expected.reset(new base::FundamentalValue(0.1));
617 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
618 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
621 TEST(IntPercentageToDoublePolicyHandler, ApplyPolicySettingsDontClamp) {
622 PolicyMap policy_map;
623 PrefValueMap prefs;
624 scoped_ptr<base::Value> expected;
625 const base::Value* value;
627 // This tests needs to modify an int policy. The exact policy used and its
628 // semantics outside the test are irrelevant.
629 IntPercentageToDoublePolicyHandler handler(
630 kTestPolicy, kTestPref, 0, 10, true);
632 // Check that values lying in the accepted range are written to the pref.
633 policy_map.Set(kTestPolicy,
634 POLICY_LEVEL_MANDATORY,
635 POLICY_SCOPE_USER,
636 POLICY_SOURCE_CLOUD,
637 new base::FundamentalValue(0),
638 NULL);
639 prefs.Clear();
640 handler.ApplyPolicySettings(policy_map, &prefs);
641 expected.reset(new base::FundamentalValue(0.0));
642 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
643 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
645 policy_map.Set(kTestPolicy,
646 POLICY_LEVEL_MANDATORY,
647 POLICY_SCOPE_USER,
648 POLICY_SOURCE_CLOUD,
649 new base::FundamentalValue(5),
650 NULL);
651 prefs.Clear();
652 handler.ApplyPolicySettings(policy_map, &prefs);
653 expected.reset(new base::FundamentalValue(0.05));
654 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
655 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
657 policy_map.Set(kTestPolicy,
658 POLICY_LEVEL_MANDATORY,
659 POLICY_SCOPE_USER,
660 POLICY_SOURCE_CLOUD,
661 new base::FundamentalValue(10),
662 NULL);
663 prefs.Clear();
664 handler.ApplyPolicySettings(policy_map, &prefs);
665 expected.reset(new base::FundamentalValue(0.1));
666 EXPECT_TRUE(prefs.GetValue(kTestPref, &value));
667 EXPECT_TRUE(base::Value::Equals(expected.get(), value));
670 TEST(SchemaValidatingPolicyHandlerTest, CheckAndGetValue) {
671 std::string error;
672 static const char kSchemaJson[] =
674 " \"type\": \"object\","
675 " \"properties\": {"
676 " \"OneToThree\": {"
677 " \"type\": \"integer\","
678 " \"minimum\": 1,"
679 " \"maximum\": 3"
680 " },"
681 " \"Colors\": {"
682 " \"type\": \"string\","
683 " \"enum\": [ \"Red\", \"Green\", \"Blue\" ]"
684 " }"
685 " }"
686 "}";
687 Schema schema = Schema::Parse(kSchemaJson, &error);
688 ASSERT_TRUE(schema.valid()) << error;
690 static const char kPolicyMapJson[] =
692 " \"PolicyForTesting\": {"
693 " \"OneToThree\": 2,"
694 " \"Colors\": \"White\""
695 " }"
696 "}";
697 scoped_ptr<base::Value> policy_map_value =
698 base::JSONReader::ReadAndReturnError(kPolicyMapJson, base::JSON_PARSE_RFC,
699 NULL, &error);
700 ASSERT_TRUE(policy_map_value) << error;
702 const base::DictionaryValue* policy_map_dict = NULL;
703 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict));
705 PolicyMap policy_map;
706 policy_map.LoadFrom(policy_map_dict, POLICY_LEVEL_RECOMMENDED,
707 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD);
709 TestSchemaValidatingPolicyHandler handler(schema, SCHEMA_ALLOW_INVALID);
710 scoped_ptr<base::Value> output_value;
711 ASSERT_TRUE(handler.CheckAndGetValueForTest(policy_map, &output_value));
712 ASSERT_TRUE(output_value);
714 base::DictionaryValue* dict = NULL;
715 ASSERT_TRUE(output_value->GetAsDictionary(&dict));
717 // Test that CheckAndGetValue() actually dropped invalid properties.
718 int int_value = -1;
719 EXPECT_TRUE(dict->GetInteger("OneToThree", &int_value));
720 EXPECT_EQ(2, int_value);
721 EXPECT_FALSE(dict->HasKey("Colors"));
724 TEST(SimpleSchemaValidatingPolicyHandlerTest, CheckAndGetValue) {
725 const char policy_name[] = "PolicyForTesting";
726 static const char kSchemaJson[] =
728 " \"type\": \"object\","
729 " \"properties\": {"
730 " \"PolicyForTesting\": {"
731 " \"type\": \"object\","
732 " \"properties\": {"
733 " \"OneToThree\": {"
734 " \"type\": \"integer\","
735 " \"minimum\": 1,"
736 " \"maximum\": 3"
737 " },"
738 " \"Colors\": {"
739 " \"type\": \"string\","
740 " \"enum\": [ \"Red\", \"Green\", \"Blue\" ]"
741 " }"
742 " }"
743 " }"
744 " }"
745 "}";
746 std::string error;
747 Schema schema = Schema::Parse(kSchemaJson, &error);
748 ASSERT_TRUE(schema.valid()) << error;
750 static const char kPolicyMapJson[] =
752 " \"PolicyForTesting\": {"
753 " \"OneToThree\": 2,"
754 " \"Colors\": \"Green\""
755 " }"
756 "}";
757 scoped_ptr<base::Value> policy_map_value =
758 base::JSONReader::ReadAndReturnError(kPolicyMapJson, base::JSON_PARSE_RFC,
759 NULL, &error);
760 ASSERT_TRUE(policy_map_value) << error;
762 const base::DictionaryValue* policy_map_dict = NULL;
763 ASSERT_TRUE(policy_map_value->GetAsDictionary(&policy_map_dict));
765 PolicyMap policy_map_recommended;
766 policy_map_recommended.LoadFrom(
767 policy_map_dict, POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER,
768 POLICY_SOURCE_CLOUD);
770 PolicyMap policy_map_mandatory;
771 policy_map_mandatory.LoadFrom(
772 policy_map_dict, POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
773 POLICY_SOURCE_CLOUD);
775 SimpleSchemaValidatingPolicyHandler handler_all(
776 policy_name,
777 kTestPref,
778 schema,
779 SCHEMA_STRICT,
780 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_ALLOWED,
781 SimpleSchemaValidatingPolicyHandler::MANDATORY_ALLOWED);
783 SimpleSchemaValidatingPolicyHandler handler_recommended(
784 policy_name,
785 kTestPref,
786 schema,
787 SCHEMA_STRICT,
788 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_ALLOWED,
789 SimpleSchemaValidatingPolicyHandler::MANDATORY_PROHIBITED);
791 SimpleSchemaValidatingPolicyHandler handler_mandatory(
792 policy_name,
793 kTestPref,
794 schema,
795 SCHEMA_STRICT,
796 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_PROHIBITED,
797 SimpleSchemaValidatingPolicyHandler::MANDATORY_ALLOWED);
799 SimpleSchemaValidatingPolicyHandler handler_none(
800 policy_name,
801 kTestPref,
802 schema,
803 SCHEMA_STRICT,
804 SimpleSchemaValidatingPolicyHandler::RECOMMENDED_PROHIBITED,
805 SimpleSchemaValidatingPolicyHandler::MANDATORY_PROHIBITED);
807 const base::Value* value_expected_in_pref;
808 policy_map_dict->Get(policy_name, &value_expected_in_pref);
810 PolicyErrorMap errors;
811 PrefValueMap prefs;
812 base::Value* value_set_in_pref;
814 EXPECT_TRUE(handler_all.CheckPolicySettings(policy_map_mandatory, &errors));
815 EXPECT_TRUE(errors.empty());
816 prefs.Clear();
817 handler_all.ApplyPolicySettings(policy_map_mandatory, &prefs);
818 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref));
819 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref));
821 EXPECT_FALSE(
822 handler_recommended.CheckPolicySettings(policy_map_mandatory, &errors));
823 EXPECT_FALSE(errors.empty());
824 errors.Clear();
826 EXPECT_TRUE(
827 handler_mandatory.CheckPolicySettings(policy_map_mandatory, &errors));
828 EXPECT_TRUE(errors.empty());
829 prefs.Clear();
830 handler_mandatory.ApplyPolicySettings(policy_map_mandatory, &prefs);
831 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref));
832 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref));
834 EXPECT_FALSE(handler_none.CheckPolicySettings(policy_map_mandatory, &errors));
835 EXPECT_FALSE(errors.empty());
836 errors.Clear();
838 EXPECT_TRUE(handler_all.CheckPolicySettings(policy_map_recommended, &errors));
839 EXPECT_TRUE(errors.empty());
840 prefs.Clear();
841 handler_all.ApplyPolicySettings(policy_map_mandatory, &prefs);
842 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref));
843 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref));
845 EXPECT_FALSE(
846 handler_mandatory.CheckPolicySettings(policy_map_recommended, &errors));
847 EXPECT_FALSE(errors.empty());
848 errors.Clear();
850 EXPECT_TRUE(
851 handler_recommended.CheckPolicySettings(policy_map_recommended, &errors));
852 EXPECT_TRUE(errors.empty());
853 prefs.Clear();
854 handler_recommended.ApplyPolicySettings(policy_map_mandatory, &prefs);
855 EXPECT_TRUE(prefs.GetValue(kTestPref, &value_set_in_pref));
856 EXPECT_TRUE(value_expected_in_pref->Equals(value_set_in_pref));
858 EXPECT_FALSE(
859 handler_none.CheckPolicySettings(policy_map_recommended, &errors));
860 EXPECT_FALSE(errors.empty());
863 } // namespace policy