Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / policy / core / common / policy_service_impl_unittest.cc
blobd9431f12160b569ed06d52fb8fa7bf4d87bc7ffd
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 "components/policy/core/common/policy_service_impl.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "base/values.h"
13 #include "components/policy/core/common/external_data_fetcher.h"
14 #include "components/policy/core/common/mock_configuration_policy_provider.h"
15 #include "components/policy/core/common/mock_policy_service.h"
16 #include "components/policy/core/common/policy_types.h"
17 #include "policy/policy_constants.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 using ::testing::AnyNumber;
22 using ::testing::Mock;
23 using ::testing::Return;
24 using ::testing::_;
26 namespace policy {
28 namespace {
30 const char kExtension[] = "extension-id";
31 const char kSameLevelPolicy[] = "policy-same-level-and-scope";
32 const char kDiffLevelPolicy[] = "chrome-diff-level-and-scope";
34 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyUpdated() with
35 // their expected values.
36 MATCHER_P(PolicyEquals, expected, "") {
37 return arg.Equals(*expected);
40 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyValueUpdated()
41 // with their expected values.
42 MATCHER_P(ValueEquals, expected, "") {
43 return base::Value::Equals(arg, expected);
46 // Helper that fills |bundle| with test policies.
47 void AddTestPolicies(PolicyBundle* bundle,
48 const char* value,
49 PolicyLevel level,
50 PolicyScope scope) {
51 PolicyMap* policy_map =
52 &bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()));
53 policy_map->Set(kSameLevelPolicy,
54 POLICY_LEVEL_MANDATORY,
55 POLICY_SCOPE_USER,
56 POLICY_SOURCE_ENTERPRISE_DEFAULT,
57 new base::StringValue(value),
58 NULL);
59 policy_map->Set(
60 kDiffLevelPolicy, level, scope, POLICY_SOURCE_PLATFORM,
61 new base::StringValue(value), nullptr);
62 policy_map =
63 &bundle->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension));
64 policy_map->Set(kSameLevelPolicy,
65 POLICY_LEVEL_MANDATORY,
66 POLICY_SCOPE_USER,
67 POLICY_SOURCE_ENTERPRISE_DEFAULT,
68 new base::StringValue(value),
69 NULL);
70 policy_map->Set(
71 kDiffLevelPolicy, level, scope, POLICY_SOURCE_PLATFORM,
72 new base::StringValue(value), nullptr);
75 // Observer class that changes the policy in the passed provider when the
76 // callback is invoked.
77 class ChangePolicyObserver : public PolicyService::Observer {
78 public:
79 explicit ChangePolicyObserver(MockConfigurationPolicyProvider* provider)
80 : provider_(provider),
81 observer_invoked_(false) {}
83 void OnPolicyUpdated(const PolicyNamespace&,
84 const PolicyMap& previous,
85 const PolicyMap& current) override {
86 PolicyMap new_policy;
87 new_policy.Set("foo",
88 POLICY_LEVEL_MANDATORY,
89 POLICY_SCOPE_USER,
90 POLICY_SOURCE_CLOUD,
91 new base::FundamentalValue(14),
92 NULL);
93 provider_->UpdateChromePolicy(new_policy);
94 observer_invoked_ = true;
97 bool observer_invoked() const { return observer_invoked_; }
99 private:
100 MockConfigurationPolicyProvider* provider_;
101 bool observer_invoked_;
104 } // namespace
106 class PolicyServiceTest : public testing::Test {
107 public:
108 PolicyServiceTest() {}
109 void SetUp() override {
110 EXPECT_CALL(provider0_, IsInitializationComplete(_))
111 .WillRepeatedly(Return(true));
112 EXPECT_CALL(provider1_, IsInitializationComplete(_))
113 .WillRepeatedly(Return(true));
114 EXPECT_CALL(provider2_, IsInitializationComplete(_))
115 .WillRepeatedly(Return(true));
117 provider0_.Init();
118 provider1_.Init();
119 provider2_.Init();
121 policy0_.Set("pre",
122 POLICY_LEVEL_MANDATORY,
123 POLICY_SCOPE_USER,
124 POLICY_SOURCE_ENTERPRISE_DEFAULT,
125 new base::FundamentalValue(13),
126 NULL);
127 provider0_.UpdateChromePolicy(policy0_);
129 PolicyServiceImpl::Providers providers;
130 providers.push_back(&provider0_);
131 providers.push_back(&provider1_);
132 providers.push_back(&provider2_);
133 policy_service_.reset(new PolicyServiceImpl(providers));
136 void TearDown() override {
137 provider0_.Shutdown();
138 provider1_.Shutdown();
139 provider2_.Shutdown();
142 MOCK_METHOD2(OnPolicyValueUpdated, void(const base::Value*,
143 const base::Value*));
145 MOCK_METHOD0(OnPolicyRefresh, void());
147 // Returns true if the policies for namespace |ns| match |expected|.
148 bool VerifyPolicies(const PolicyNamespace& ns,
149 const PolicyMap& expected) {
150 return policy_service_->GetPolicies(ns).Equals(expected);
153 void RunUntilIdle() {
154 base::RunLoop loop;
155 loop.RunUntilIdle();
158 protected:
159 base::MessageLoop loop_;
160 MockConfigurationPolicyProvider provider0_;
161 MockConfigurationPolicyProvider provider1_;
162 MockConfigurationPolicyProvider provider2_;
163 PolicyMap policy0_;
164 PolicyMap policy1_;
165 PolicyMap policy2_;
166 scoped_ptr<PolicyServiceImpl> policy_service_;
168 private:
169 DISALLOW_COPY_AND_ASSIGN(PolicyServiceTest);
172 TEST_F(PolicyServiceTest, LoadsPoliciesBeforeProvidersRefresh) {
173 PolicyMap expected;
174 expected.Set("pre",
175 POLICY_LEVEL_MANDATORY,
176 POLICY_SCOPE_USER,
177 POLICY_SOURCE_ENTERPRISE_DEFAULT,
178 new base::FundamentalValue(13),
179 NULL);
180 EXPECT_TRUE(VerifyPolicies(
181 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()), expected));
184 TEST_F(PolicyServiceTest, NotifyObservers) {
185 MockPolicyServiceObserver observer;
186 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, &observer);
188 PolicyMap expectedPrevious;
189 expectedPrevious.Set("pre",
190 POLICY_LEVEL_MANDATORY,
191 POLICY_SCOPE_USER,
192 POLICY_SOURCE_ENTERPRISE_DEFAULT,
193 new base::FundamentalValue(13),
194 NULL);
196 PolicyMap expectedCurrent;
197 expectedCurrent.CopyFrom(expectedPrevious);
198 expectedCurrent.Set("aaa",
199 POLICY_LEVEL_MANDATORY,
200 POLICY_SCOPE_USER,
201 POLICY_SOURCE_CLOUD,
202 new base::FundamentalValue(123),
203 NULL);
204 policy0_.Set("aaa",
205 POLICY_LEVEL_MANDATORY,
206 POLICY_SCOPE_USER,
207 POLICY_SOURCE_CLOUD,
208 new base::FundamentalValue(123),
209 NULL);
210 EXPECT_CALL(observer, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME,
211 std::string()),
212 PolicyEquals(&expectedPrevious),
213 PolicyEquals(&expectedCurrent)));
214 provider0_.UpdateChromePolicy(policy0_);
215 Mock::VerifyAndClearExpectations(&observer);
217 // No changes.
218 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _)).Times(0);
219 provider0_.UpdateChromePolicy(policy0_);
220 Mock::VerifyAndClearExpectations(&observer);
221 EXPECT_TRUE(VerifyPolicies(
222 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()), expectedCurrent));
224 // New policy.
225 expectedPrevious.CopyFrom(expectedCurrent);
226 expectedCurrent.Set("bbb",
227 POLICY_LEVEL_MANDATORY,
228 POLICY_SCOPE_USER,
229 POLICY_SOURCE_CLOUD,
230 new base::FundamentalValue(456),
231 NULL);
232 policy0_.Set("bbb",
233 POLICY_LEVEL_MANDATORY,
234 POLICY_SCOPE_USER,
235 POLICY_SOURCE_CLOUD,
236 new base::FundamentalValue(456),
237 NULL);
238 EXPECT_CALL(observer, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME,
239 std::string()),
240 PolicyEquals(&expectedPrevious),
241 PolicyEquals(&expectedCurrent)));
242 provider0_.UpdateChromePolicy(policy0_);
243 Mock::VerifyAndClearExpectations(&observer);
245 // Removed policy.
246 expectedPrevious.CopyFrom(expectedCurrent);
247 expectedCurrent.Erase("bbb");
248 policy0_.Erase("bbb");
249 EXPECT_CALL(observer, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME,
250 std::string()),
251 PolicyEquals(&expectedPrevious),
252 PolicyEquals(&expectedCurrent)));
253 provider0_.UpdateChromePolicy(policy0_);
254 Mock::VerifyAndClearExpectations(&observer);
256 // Changed policy.
257 expectedPrevious.CopyFrom(expectedCurrent);
258 expectedCurrent.Set("aaa",
259 POLICY_LEVEL_MANDATORY,
260 POLICY_SCOPE_USER,
261 POLICY_SOURCE_CLOUD,
262 new base::FundamentalValue(789),
263 NULL);
264 policy0_.Set("aaa",
265 POLICY_LEVEL_MANDATORY,
266 POLICY_SCOPE_USER,
267 POLICY_SOURCE_CLOUD,
268 new base::FundamentalValue(789),
269 NULL);
271 EXPECT_CALL(observer, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME,
272 std::string()),
273 PolicyEquals(&expectedPrevious),
274 PolicyEquals(&expectedCurrent)));
275 provider0_.UpdateChromePolicy(policy0_);
276 Mock::VerifyAndClearExpectations(&observer);
278 // No changes again.
279 EXPECT_CALL(observer, OnPolicyUpdated(_, _, _)).Times(0);
280 provider0_.UpdateChromePolicy(policy0_);
281 Mock::VerifyAndClearExpectations(&observer);
282 EXPECT_TRUE(VerifyPolicies(
283 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()), expectedCurrent));
285 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &observer);
288 TEST_F(PolicyServiceTest, NotifyObserversInMultipleNamespaces) {
289 const std::string kExtension0("extension-0");
290 const std::string kExtension1("extension-1");
291 const std::string kExtension2("extension-2");
292 MockPolicyServiceObserver chrome_observer;
293 MockPolicyServiceObserver extension_observer;
294 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, &chrome_observer);
295 policy_service_->AddObserver(POLICY_DOMAIN_EXTENSIONS, &extension_observer);
297 PolicyMap previous_policy_map;
298 previous_policy_map.Set("pre",
299 POLICY_LEVEL_MANDATORY,
300 POLICY_SCOPE_USER,
301 POLICY_SOURCE_ENTERPRISE_DEFAULT,
302 new base::FundamentalValue(13),
303 NULL);
304 PolicyMap policy_map;
305 policy_map.CopyFrom(previous_policy_map);
306 policy_map.Set("policy",
307 POLICY_LEVEL_MANDATORY,
308 POLICY_SCOPE_USER,
309 POLICY_SOURCE_CLOUD,
310 new base::StringValue("value"),
311 NULL);
313 scoped_ptr<PolicyBundle> bundle(new PolicyBundle());
314 // The initial setup includes a policy for chrome that is now changing.
315 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
316 .CopyFrom(policy_map);
317 bundle->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension0))
318 .CopyFrom(policy_map);
319 bundle->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension1))
320 .CopyFrom(policy_map);
322 const PolicyMap kEmptyPolicyMap;
323 EXPECT_CALL(
324 chrome_observer,
325 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()),
326 PolicyEquals(&previous_policy_map),
327 PolicyEquals(&policy_map)));
328 EXPECT_CALL(
329 extension_observer,
330 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension0),
331 PolicyEquals(&kEmptyPolicyMap),
332 PolicyEquals(&policy_map)));
333 EXPECT_CALL(
334 extension_observer,
335 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension1),
336 PolicyEquals(&kEmptyPolicyMap),
337 PolicyEquals(&policy_map)));
338 provider0_.UpdatePolicy(bundle.Pass());
339 RunUntilIdle();
340 Mock::VerifyAndClearExpectations(&chrome_observer);
341 Mock::VerifyAndClearExpectations(&extension_observer);
343 // Chrome policy stays the same, kExtension0 is gone, kExtension1 changes,
344 // and kExtension2 is new.
345 previous_policy_map.CopyFrom(policy_map);
346 bundle.reset(new PolicyBundle());
347 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()))
348 .CopyFrom(policy_map);
349 policy_map.Set("policy",
350 POLICY_LEVEL_MANDATORY,
351 POLICY_SCOPE_USER,
352 POLICY_SOURCE_CLOUD,
353 new base::StringValue("another value"),
354 NULL);
355 bundle->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension1))
356 .CopyFrom(policy_map);
357 bundle->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension2))
358 .CopyFrom(policy_map);
360 EXPECT_CALL(chrome_observer, OnPolicyUpdated(_, _, _)).Times(0);
361 EXPECT_CALL(
362 extension_observer,
363 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension0),
364 PolicyEquals(&previous_policy_map),
365 PolicyEquals(&kEmptyPolicyMap)));
366 EXPECT_CALL(
367 extension_observer,
368 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension1),
369 PolicyEquals(&previous_policy_map),
370 PolicyEquals(&policy_map)));
371 EXPECT_CALL(
372 extension_observer,
373 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension2),
374 PolicyEquals(&kEmptyPolicyMap),
375 PolicyEquals(&policy_map)));
376 provider0_.UpdatePolicy(bundle.Pass());
377 RunUntilIdle();
378 Mock::VerifyAndClearExpectations(&chrome_observer);
379 Mock::VerifyAndClearExpectations(&extension_observer);
381 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &chrome_observer);
382 policy_service_->RemoveObserver(POLICY_DOMAIN_EXTENSIONS,
383 &extension_observer);
386 TEST_F(PolicyServiceTest, ObserverChangesPolicy) {
387 ChangePolicyObserver observer(&provider0_);
388 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, &observer);
389 policy0_.Set("aaa",
390 POLICY_LEVEL_MANDATORY,
391 POLICY_SCOPE_USER,
392 POLICY_SOURCE_CLOUD,
393 new base::FundamentalValue(123),
394 NULL);
395 policy0_.Set("bbb",
396 POLICY_LEVEL_MANDATORY,
397 POLICY_SCOPE_USER,
398 POLICY_SOURCE_CLOUD,
399 new base::FundamentalValue(1234),
400 NULL);
401 // Should not crash.
402 provider0_.UpdateChromePolicy(policy0_);
403 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &observer);
404 EXPECT_TRUE(observer.observer_invoked());
407 TEST_F(PolicyServiceTest, Priorities) {
408 PolicyMap expected;
409 expected.Set("pre",
410 POLICY_LEVEL_MANDATORY,
411 POLICY_SCOPE_USER,
412 POLICY_SOURCE_ENTERPRISE_DEFAULT,
413 new base::FundamentalValue(13),
414 NULL);
415 expected.Set("aaa",
416 POLICY_LEVEL_MANDATORY,
417 POLICY_SCOPE_USER,
418 POLICY_SOURCE_CLOUD,
419 new base::FundamentalValue(0),
420 NULL);
421 policy0_.Set("aaa",
422 POLICY_LEVEL_MANDATORY,
423 POLICY_SCOPE_USER,
424 POLICY_SOURCE_CLOUD,
425 new base::FundamentalValue(0),
426 NULL);
427 policy1_.Set("aaa",
428 POLICY_LEVEL_MANDATORY,
429 POLICY_SCOPE_USER,
430 POLICY_SOURCE_CLOUD,
431 new base::FundamentalValue(1),
432 NULL);
433 policy2_.Set("aaa",
434 POLICY_LEVEL_MANDATORY,
435 POLICY_SCOPE_USER,
436 POLICY_SOURCE_CLOUD,
437 new base::FundamentalValue(2),
438 NULL);
439 provider0_.UpdateChromePolicy(policy0_);
440 provider1_.UpdateChromePolicy(policy1_);
441 provider2_.UpdateChromePolicy(policy2_);
442 EXPECT_TRUE(VerifyPolicies(
443 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()), expected));
445 expected.Set("aaa",
446 POLICY_LEVEL_MANDATORY,
447 POLICY_SCOPE_USER,
448 POLICY_SOURCE_CLOUD,
449 new base::FundamentalValue(1),
450 NULL);
451 policy0_.Erase("aaa");
452 provider0_.UpdateChromePolicy(policy0_);
453 EXPECT_TRUE(VerifyPolicies(
454 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()), expected));
456 expected.Set("aaa",
457 POLICY_LEVEL_MANDATORY,
458 POLICY_SCOPE_USER,
459 POLICY_SOURCE_CLOUD,
460 new base::FundamentalValue(2),
461 NULL);
462 policy1_.Set("aaa",
463 POLICY_LEVEL_RECOMMENDED,
464 POLICY_SCOPE_USER,
465 POLICY_SOURCE_CLOUD,
466 new base::FundamentalValue(1),
467 NULL);
468 provider1_.UpdateChromePolicy(policy1_);
469 EXPECT_TRUE(VerifyPolicies(
470 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()), expected));
473 TEST_F(PolicyServiceTest, PolicyChangeRegistrar) {
474 scoped_ptr<PolicyChangeRegistrar> registrar(new PolicyChangeRegistrar(
475 policy_service_.get(),
476 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())));
478 // Starting to observe existing policies doesn't trigger a notification.
479 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0);
480 registrar->Observe("pre", base::Bind(
481 &PolicyServiceTest::OnPolicyValueUpdated,
482 base::Unretained(this)));
483 registrar->Observe("aaa", base::Bind(
484 &PolicyServiceTest::OnPolicyValueUpdated,
485 base::Unretained(this)));
486 RunUntilIdle();
487 Mock::VerifyAndClearExpectations(this);
489 // Changing it now triggers a notification.
490 base::FundamentalValue kValue0(0);
491 EXPECT_CALL(*this, OnPolicyValueUpdated(NULL, ValueEquals(&kValue0)));
492 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
493 POLICY_SOURCE_CLOUD, kValue0.DeepCopy(), nullptr);
494 provider0_.UpdateChromePolicy(policy0_);
495 Mock::VerifyAndClearExpectations(this);
497 // Changing other values doesn't trigger a notification.
498 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0);
499 policy0_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
500 POLICY_SOURCE_CLOUD, kValue0.DeepCopy(), nullptr);
501 provider0_.UpdateChromePolicy(policy0_);
502 Mock::VerifyAndClearExpectations(this);
504 // Modifying the value triggers a notification.
505 base::FundamentalValue kValue1(1);
506 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue0),
507 ValueEquals(&kValue1)));
508 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
509 POLICY_SOURCE_CLOUD, kValue1.DeepCopy(), nullptr);
510 provider0_.UpdateChromePolicy(policy0_);
511 Mock::VerifyAndClearExpectations(this);
513 // Removing the value triggers a notification.
514 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue1), NULL));
515 policy0_.Erase("aaa");
516 provider0_.UpdateChromePolicy(policy0_);
517 Mock::VerifyAndClearExpectations(this);
519 // No more notifications after destroying the registrar.
520 EXPECT_CALL(*this, OnPolicyValueUpdated(_, _)).Times(0);
521 registrar.reset();
522 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
523 POLICY_SOURCE_CLOUD, kValue1.DeepCopy(), nullptr);
524 policy0_.Set("pre", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
525 POLICY_SOURCE_ENTERPRISE_DEFAULT, kValue1.DeepCopy(), nullptr);
526 provider0_.UpdateChromePolicy(policy0_);
527 Mock::VerifyAndClearExpectations(this);
530 TEST_F(PolicyServiceTest, RefreshPolicies) {
531 EXPECT_CALL(provider0_, RefreshPolicies()).Times(AnyNumber());
532 EXPECT_CALL(provider1_, RefreshPolicies()).Times(AnyNumber());
533 EXPECT_CALL(provider2_, RefreshPolicies()).Times(AnyNumber());
535 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
536 policy_service_->RefreshPolicies(base::Bind(
537 &PolicyServiceTest::OnPolicyRefresh,
538 base::Unretained(this)));
539 // Let any queued observer tasks run.
540 RunUntilIdle();
541 Mock::VerifyAndClearExpectations(this);
543 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
544 base::FundamentalValue kValue0(0);
545 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
546 POLICY_SOURCE_CLOUD, kValue0.DeepCopy(), nullptr);
547 provider0_.UpdateChromePolicy(policy0_);
548 Mock::VerifyAndClearExpectations(this);
550 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
551 base::FundamentalValue kValue1(1);
552 policy1_.Set("aaa", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER,
553 POLICY_SOURCE_CLOUD, kValue1.DeepCopy(), nullptr);
554 provider1_.UpdateChromePolicy(policy1_);
555 Mock::VerifyAndClearExpectations(this);
557 // A provider can refresh more than once after a RefreshPolicies call, but
558 // OnPolicyRefresh should be triggered only after all providers are
559 // refreshed.
560 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
561 policy1_.Set("bbb", POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER,
562 POLICY_SOURCE_CLOUD, kValue1.DeepCopy(), nullptr);
563 provider1_.UpdateChromePolicy(policy1_);
564 Mock::VerifyAndClearExpectations(this);
566 // If another RefreshPolicies() call happens while waiting for a previous
567 // one to complete, then all providers must refresh again.
568 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
569 policy_service_->RefreshPolicies(base::Bind(
570 &PolicyServiceTest::OnPolicyRefresh,
571 base::Unretained(this)));
572 RunUntilIdle();
573 Mock::VerifyAndClearExpectations(this);
575 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
576 policy2_.Set("bbb", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
577 POLICY_SOURCE_CLOUD, kValue0.DeepCopy(), nullptr);
578 provider2_.UpdateChromePolicy(policy2_);
579 Mock::VerifyAndClearExpectations(this);
581 // Providers 0 and 1 must reload again.
582 EXPECT_CALL(*this, OnPolicyRefresh()).Times(2);
583 base::FundamentalValue kValue2(2);
584 policy0_.Set("aaa", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
585 POLICY_SOURCE_CLOUD, kValue2.DeepCopy(), nullptr);
586 provider0_.UpdateChromePolicy(policy0_);
587 provider1_.UpdateChromePolicy(policy1_);
588 Mock::VerifyAndClearExpectations(this);
590 const PolicyMap& policies = policy_service_->GetPolicies(
591 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string()));
592 EXPECT_TRUE(base::Value::Equals(&kValue2, policies.GetValue("aaa")));
593 EXPECT_TRUE(base::Value::Equals(&kValue0, policies.GetValue("bbb")));
596 TEST_F(PolicyServiceTest, NamespaceMerge) {
597 scoped_ptr<PolicyBundle> bundle0(new PolicyBundle());
598 scoped_ptr<PolicyBundle> bundle1(new PolicyBundle());
599 scoped_ptr<PolicyBundle> bundle2(new PolicyBundle());
601 AddTestPolicies(bundle0.get(), "bundle0",
602 POLICY_LEVEL_RECOMMENDED, POLICY_SCOPE_USER);
603 AddTestPolicies(bundle1.get(), "bundle1",
604 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER);
605 AddTestPolicies(bundle2.get(), "bundle2",
606 POLICY_LEVEL_MANDATORY, POLICY_SCOPE_MACHINE);
608 provider0_.UpdatePolicy(bundle0.Pass());
609 provider1_.UpdatePolicy(bundle1.Pass());
610 provider2_.UpdatePolicy(bundle2.Pass());
611 RunUntilIdle();
613 PolicyMap expected;
614 // For policies of the same level and scope, the first provider takes
615 // precedence, on every namespace.
616 expected.Set(kSameLevelPolicy,
617 POLICY_LEVEL_MANDATORY,
618 POLICY_SCOPE_USER,
619 POLICY_SOURCE_ENTERPRISE_DEFAULT,
620 new base::StringValue("bundle0"),
621 NULL);
622 // For policies with different levels and scopes, the highest priority
623 // level/scope combination takes precedence, on every namespace.
624 expected.Set(kDiffLevelPolicy,
625 POLICY_LEVEL_MANDATORY,
626 POLICY_SCOPE_MACHINE,
627 POLICY_SOURCE_PLATFORM,
628 new base::StringValue("bundle2"),
629 NULL);
630 EXPECT_TRUE(policy_service_->GetPolicies(
631 PolicyNamespace(POLICY_DOMAIN_CHROME, std::string())).Equals(expected));
632 EXPECT_TRUE(policy_service_->GetPolicies(
633 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, kExtension)).Equals(expected));
636 TEST_F(PolicyServiceTest, IsInitializationComplete) {
637 // |provider0| has all domains initialized.
638 Mock::VerifyAndClearExpectations(&provider1_);
639 Mock::VerifyAndClearExpectations(&provider2_);
640 EXPECT_CALL(provider1_, IsInitializationComplete(_))
641 .WillRepeatedly(Return(false));
642 EXPECT_CALL(provider2_, IsInitializationComplete(_))
643 .WillRepeatedly(Return(false));
644 PolicyServiceImpl::Providers providers;
645 providers.push_back(&provider0_);
646 providers.push_back(&provider1_);
647 providers.push_back(&provider2_);
648 policy_service_.reset(new PolicyServiceImpl(providers));
649 EXPECT_FALSE(policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
650 EXPECT_FALSE(
651 policy_service_->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS));
653 // |provider2_| still doesn't have POLICY_DOMAIN_CHROME initialized, so
654 // the initialization status of that domain won't change.
655 MockPolicyServiceObserver observer;
656 policy_service_->AddObserver(POLICY_DOMAIN_CHROME, &observer);
657 policy_service_->AddObserver(POLICY_DOMAIN_EXTENSIONS, &observer);
658 EXPECT_CALL(observer, OnPolicyServiceInitialized(_)).Times(0);
659 Mock::VerifyAndClearExpectations(&provider1_);
660 EXPECT_CALL(provider1_, IsInitializationComplete(POLICY_DOMAIN_CHROME))
661 .WillRepeatedly(Return(true));
662 EXPECT_CALL(provider1_, IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS))
663 .WillRepeatedly(Return(false));
664 const PolicyMap kPolicyMap;
665 provider1_.UpdateChromePolicy(kPolicyMap);
666 Mock::VerifyAndClearExpectations(&observer);
667 EXPECT_FALSE(policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
668 EXPECT_FALSE(
669 policy_service_->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS));
671 // Same if |provider1_| doesn't have POLICY_DOMAIN_EXTENSIONS initialized.
672 EXPECT_CALL(observer, OnPolicyServiceInitialized(_)).Times(0);
673 Mock::VerifyAndClearExpectations(&provider2_);
674 EXPECT_CALL(provider2_, IsInitializationComplete(POLICY_DOMAIN_CHROME))
675 .WillRepeatedly(Return(false));
676 EXPECT_CALL(provider2_, IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS))
677 .WillRepeatedly(Return(true));
678 provider2_.UpdateChromePolicy(kPolicyMap);
679 Mock::VerifyAndClearExpectations(&observer);
680 EXPECT_FALSE(policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
681 EXPECT_FALSE(
682 policy_service_->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS));
684 // Now initialize POLICY_DOMAIN_CHROME on all the providers.
685 EXPECT_CALL(observer, OnPolicyServiceInitialized(POLICY_DOMAIN_CHROME));
686 Mock::VerifyAndClearExpectations(&provider2_);
687 EXPECT_CALL(provider2_, IsInitializationComplete(POLICY_DOMAIN_CHROME))
688 .WillRepeatedly(Return(true));
689 EXPECT_CALL(provider2_, IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS))
690 .WillRepeatedly(Return(true));
691 provider2_.UpdateChromePolicy(kPolicyMap);
692 Mock::VerifyAndClearExpectations(&observer);
693 EXPECT_TRUE(policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
694 // Other domains are still not initialized.
695 EXPECT_FALSE(
696 policy_service_->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS));
698 // Initialize the remaining domain.
699 EXPECT_CALL(observer, OnPolicyServiceInitialized(POLICY_DOMAIN_EXTENSIONS));
700 Mock::VerifyAndClearExpectations(&provider1_);
701 EXPECT_CALL(provider1_, IsInitializationComplete(POLICY_DOMAIN_CHROME))
702 .WillRepeatedly(Return(true));
703 EXPECT_CALL(provider1_, IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS))
704 .WillRepeatedly(Return(true));
705 provider1_.UpdateChromePolicy(kPolicyMap);
706 Mock::VerifyAndClearExpectations(&observer);
707 EXPECT_TRUE(policy_service_->IsInitializationComplete(POLICY_DOMAIN_CHROME));
708 EXPECT_TRUE(
709 policy_service_->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS));
711 // Cleanup.
712 policy_service_->RemoveObserver(POLICY_DOMAIN_CHROME, &observer);
713 policy_service_->RemoveObserver(POLICY_DOMAIN_EXTENSIONS, &observer);
716 TEST_F(PolicyServiceTest, FixDeprecatedPolicies) {
717 const PolicyNamespace chrome_namespace(POLICY_DOMAIN_CHROME, std::string());
718 const PolicyNamespace extension_namespace(POLICY_DOMAIN_EXTENSIONS, "xyz");
720 scoped_ptr<PolicyBundle> policy_bundle(new PolicyBundle());
721 PolicyMap& policy_map = policy_bundle->Get(chrome_namespace);
722 // Individual proxy policy values in the Chrome namespace should be collected
723 // into a dictionary.
724 policy_map.Set(key::kProxyServerMode,
725 POLICY_LEVEL_MANDATORY,
726 POLICY_SCOPE_USER,
727 POLICY_SOURCE_CLOUD,
728 new base::FundamentalValue(3),
729 NULL);
731 // Both these policies should be ignored, since there's a higher priority
732 // policy available.
733 policy_map.Set(key::kProxyMode,
734 POLICY_LEVEL_RECOMMENDED,
735 POLICY_SCOPE_USER,
736 POLICY_SOURCE_CLOUD,
737 new base::StringValue("pac_script"),
738 NULL);
739 policy_map.Set(key::kProxyPacUrl,
740 POLICY_LEVEL_RECOMMENDED,
741 POLICY_SCOPE_USER,
742 POLICY_SOURCE_CLOUD,
743 new base::StringValue("http://example.com/wpad.dat"),
744 NULL);
746 // Add a value to a non-Chrome namespace.
747 policy_bundle->Get(extension_namespace).Set(key::kProxyServerMode,
748 POLICY_LEVEL_MANDATORY,
749 POLICY_SCOPE_USER,
750 POLICY_SOURCE_CLOUD,
751 new base::FundamentalValue(3),
752 NULL);
754 // The resulting Chrome namespace map should have the collected policy.
755 PolicyMap expected_chrome;
756 scoped_ptr<base::DictionaryValue> expected_value(new base::DictionaryValue);
757 expected_value->SetInteger(key::kProxyServerMode, 3);
758 expected_chrome.Set(key::kProxySettings, POLICY_LEVEL_MANDATORY,
759 POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
760 expected_value.release(), nullptr);
762 // The resulting Extensions namespace map shouldn't have been modified.
763 PolicyMap expected_extension;
764 expected_extension.Set(key::kProxyServerMode,
765 POLICY_LEVEL_MANDATORY,
766 POLICY_SCOPE_USER,
767 POLICY_SOURCE_CLOUD,
768 new base::FundamentalValue(3),
769 NULL);
771 provider0_.UpdatePolicy(policy_bundle.Pass());
772 RunUntilIdle();
774 EXPECT_TRUE(VerifyPolicies(chrome_namespace, expected_chrome));
775 EXPECT_TRUE(VerifyPolicies(extension_namespace, expected_extension));
778 } // namespace policy