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"
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
;
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
,
51 PolicyMap
* policy_map
=
52 &bundle
->Get(PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()));
53 policy_map
->Set(kSameLevelPolicy
,
54 POLICY_LEVEL_MANDATORY
,
56 POLICY_SOURCE_ENTERPRISE_DEFAULT
,
57 new base::StringValue(value
),
60 kDiffLevelPolicy
, level
, scope
, POLICY_SOURCE_PLATFORM
,
61 new base::StringValue(value
), nullptr);
63 &bundle
->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension
));
64 policy_map
->Set(kSameLevelPolicy
,
65 POLICY_LEVEL_MANDATORY
,
67 POLICY_SOURCE_ENTERPRISE_DEFAULT
,
68 new base::StringValue(value
),
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
{
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
{
88 POLICY_LEVEL_MANDATORY
,
91 new base::FundamentalValue(14),
93 provider_
->UpdateChromePolicy(new_policy
);
94 observer_invoked_
= true;
97 bool observer_invoked() const { return observer_invoked_
; }
100 MockConfigurationPolicyProvider
* provider_
;
101 bool observer_invoked_
;
106 class PolicyServiceTest
: public testing::Test
{
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));
122 POLICY_LEVEL_MANDATORY
,
124 POLICY_SOURCE_ENTERPRISE_DEFAULT
,
125 new base::FundamentalValue(13),
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() {
159 base::MessageLoop loop_
;
160 MockConfigurationPolicyProvider provider0_
;
161 MockConfigurationPolicyProvider provider1_
;
162 MockConfigurationPolicyProvider provider2_
;
166 scoped_ptr
<PolicyServiceImpl
> policy_service_
;
169 DISALLOW_COPY_AND_ASSIGN(PolicyServiceTest
);
172 TEST_F(PolicyServiceTest
, LoadsPoliciesBeforeProvidersRefresh
) {
175 POLICY_LEVEL_MANDATORY
,
177 POLICY_SOURCE_ENTERPRISE_DEFAULT
,
178 new base::FundamentalValue(13),
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
,
192 POLICY_SOURCE_ENTERPRISE_DEFAULT
,
193 new base::FundamentalValue(13),
196 PolicyMap expectedCurrent
;
197 expectedCurrent
.CopyFrom(expectedPrevious
);
198 expectedCurrent
.Set("aaa",
199 POLICY_LEVEL_MANDATORY
,
202 new base::FundamentalValue(123),
205 POLICY_LEVEL_MANDATORY
,
208 new base::FundamentalValue(123),
210 EXPECT_CALL(observer
, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME
,
212 PolicyEquals(&expectedPrevious
),
213 PolicyEquals(&expectedCurrent
)));
214 provider0_
.UpdateChromePolicy(policy0_
);
215 Mock::VerifyAndClearExpectations(&observer
);
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
));
225 expectedPrevious
.CopyFrom(expectedCurrent
);
226 expectedCurrent
.Set("bbb",
227 POLICY_LEVEL_MANDATORY
,
230 new base::FundamentalValue(456),
233 POLICY_LEVEL_MANDATORY
,
236 new base::FundamentalValue(456),
238 EXPECT_CALL(observer
, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME
,
240 PolicyEquals(&expectedPrevious
),
241 PolicyEquals(&expectedCurrent
)));
242 provider0_
.UpdateChromePolicy(policy0_
);
243 Mock::VerifyAndClearExpectations(&observer
);
246 expectedPrevious
.CopyFrom(expectedCurrent
);
247 expectedCurrent
.Erase("bbb");
248 policy0_
.Erase("bbb");
249 EXPECT_CALL(observer
, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME
,
251 PolicyEquals(&expectedPrevious
),
252 PolicyEquals(&expectedCurrent
)));
253 provider0_
.UpdateChromePolicy(policy0_
);
254 Mock::VerifyAndClearExpectations(&observer
);
257 expectedPrevious
.CopyFrom(expectedCurrent
);
258 expectedCurrent
.Set("aaa",
259 POLICY_LEVEL_MANDATORY
,
262 new base::FundamentalValue(789),
265 POLICY_LEVEL_MANDATORY
,
268 new base::FundamentalValue(789),
271 EXPECT_CALL(observer
, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME
,
273 PolicyEquals(&expectedPrevious
),
274 PolicyEquals(&expectedCurrent
)));
275 provider0_
.UpdateChromePolicy(policy0_
);
276 Mock::VerifyAndClearExpectations(&observer
);
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
,
301 POLICY_SOURCE_ENTERPRISE_DEFAULT
,
302 new base::FundamentalValue(13),
304 PolicyMap policy_map
;
305 policy_map
.CopyFrom(previous_policy_map
);
306 policy_map
.Set("policy",
307 POLICY_LEVEL_MANDATORY
,
310 new base::StringValue("value"),
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
;
325 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()),
326 PolicyEquals(&previous_policy_map
),
327 PolicyEquals(&policy_map
)));
330 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension0
),
331 PolicyEquals(&kEmptyPolicyMap
),
332 PolicyEquals(&policy_map
)));
335 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension1
),
336 PolicyEquals(&kEmptyPolicyMap
),
337 PolicyEquals(&policy_map
)));
338 provider0_
.UpdatePolicy(bundle
.Pass());
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
,
353 new base::StringValue("another value"),
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);
363 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension0
),
364 PolicyEquals(&previous_policy_map
),
365 PolicyEquals(&kEmptyPolicyMap
)));
368 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension1
),
369 PolicyEquals(&previous_policy_map
),
370 PolicyEquals(&policy_map
)));
373 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension2
),
374 PolicyEquals(&kEmptyPolicyMap
),
375 PolicyEquals(&policy_map
)));
376 provider0_
.UpdatePolicy(bundle
.Pass());
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
);
390 POLICY_LEVEL_MANDATORY
,
393 new base::FundamentalValue(123),
396 POLICY_LEVEL_MANDATORY
,
399 new base::FundamentalValue(1234),
402 provider0_
.UpdateChromePolicy(policy0_
);
403 policy_service_
->RemoveObserver(POLICY_DOMAIN_CHROME
, &observer
);
404 EXPECT_TRUE(observer
.observer_invoked());
407 TEST_F(PolicyServiceTest
, Priorities
) {
410 POLICY_LEVEL_MANDATORY
,
412 POLICY_SOURCE_ENTERPRISE_DEFAULT
,
413 new base::FundamentalValue(13),
416 POLICY_LEVEL_MANDATORY
,
419 new base::FundamentalValue(0),
422 POLICY_LEVEL_MANDATORY
,
425 new base::FundamentalValue(0),
428 POLICY_LEVEL_MANDATORY
,
431 new base::FundamentalValue(1),
434 POLICY_LEVEL_MANDATORY
,
437 new base::FundamentalValue(2),
439 provider0_
.UpdateChromePolicy(policy0_
);
440 provider1_
.UpdateChromePolicy(policy1_
);
441 provider2_
.UpdateChromePolicy(policy2_
);
442 EXPECT_TRUE(VerifyPolicies(
443 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()), expected
));
446 POLICY_LEVEL_MANDATORY
,
449 new base::FundamentalValue(1),
451 policy0_
.Erase("aaa");
452 provider0_
.UpdateChromePolicy(policy0_
);
453 EXPECT_TRUE(VerifyPolicies(
454 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()), expected
));
457 POLICY_LEVEL_MANDATORY
,
460 new base::FundamentalValue(2),
463 POLICY_LEVEL_RECOMMENDED
,
466 new base::FundamentalValue(1),
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)));
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);
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.
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
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)));
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());
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
,
619 POLICY_SOURCE_ENTERPRISE_DEFAULT
,
620 new base::StringValue("bundle0"),
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"),
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
));
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
));
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
));
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.
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
));
709 policy_service_
->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
));
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
,
728 new base::FundamentalValue(3),
731 // Both these policies should be ignored, since there's a higher priority
733 policy_map
.Set(key::kProxyMode
,
734 POLICY_LEVEL_RECOMMENDED
,
737 new base::StringValue("pac_script"),
739 policy_map
.Set(key::kProxyPacUrl
,
740 POLICY_LEVEL_RECOMMENDED
,
743 new base::StringValue("http://example.com/wpad.dat"),
746 // Add a value to a non-Chrome namespace.
747 policy_bundle
->Get(extension_namespace
).Set(key::kProxyServerMode
,
748 POLICY_LEVEL_MANDATORY
,
751 new base::FundamentalValue(3),
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
,
768 new base::FundamentalValue(3),
771 provider0_
.UpdatePolicy(policy_bundle
.Pass());
774 EXPECT_TRUE(VerifyPolicies(chrome_namespace
, expected_chrome
));
775 EXPECT_TRUE(VerifyPolicies(extension_namespace
, expected_extension
));
778 } // namespace policy