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 "policy/policy_constants.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using ::testing::AnyNumber
;
21 using ::testing::Mock
;
22 using ::testing::Return
;
29 const char kExtension
[] = "extension-id";
30 const char kSameLevelPolicy
[] = "policy-same-level-and-scope";
31 const char kDiffLevelPolicy
[] = "chrome-diff-level-and-scope";
33 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyUpdated() with
34 // their expected values.
35 MATCHER_P(PolicyEquals
, expected
, "") {
36 return arg
.Equals(*expected
);
39 // Helper to compare the arguments to an EXPECT_CALL of OnPolicyValueUpdated()
40 // with their expected values.
41 MATCHER_P(ValueEquals
, expected
, "") {
42 return base::Value::Equals(arg
, expected
);
45 // Helper that fills |bundle| with test policies.
46 void AddTestPolicies(PolicyBundle
* bundle
,
50 PolicyMap
* policy_map
=
51 &bundle
->Get(PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()));
52 policy_map
->Set(kSameLevelPolicy
,
53 POLICY_LEVEL_MANDATORY
,
55 new base::StringValue(value
),
58 kDiffLevelPolicy
, level
, scope
, new base::StringValue(value
), NULL
);
60 &bundle
->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension
));
61 policy_map
->Set(kSameLevelPolicy
,
62 POLICY_LEVEL_MANDATORY
,
64 new base::StringValue(value
),
67 kDiffLevelPolicy
, level
, scope
, new base::StringValue(value
), NULL
);
70 // Observer class that changes the policy in the passed provider when the
71 // callback is invoked.
72 class ChangePolicyObserver
: public PolicyService::Observer
{
74 explicit ChangePolicyObserver(MockConfigurationPolicyProvider
* provider
)
75 : provider_(provider
),
76 observer_invoked_(false) {}
78 void OnPolicyUpdated(const PolicyNamespace
&,
79 const PolicyMap
& previous
,
80 const PolicyMap
& current
) override
{
83 POLICY_LEVEL_MANDATORY
,
85 new base::FundamentalValue(14),
87 provider_
->UpdateChromePolicy(new_policy
);
88 observer_invoked_
= true;
91 bool observer_invoked() const { return observer_invoked_
; }
94 MockConfigurationPolicyProvider
* provider_
;
95 bool observer_invoked_
;
100 class PolicyServiceTest
: public testing::Test
{
102 PolicyServiceTest() {}
103 void SetUp() override
{
104 EXPECT_CALL(provider0_
, IsInitializationComplete(_
))
105 .WillRepeatedly(Return(true));
106 EXPECT_CALL(provider1_
, IsInitializationComplete(_
))
107 .WillRepeatedly(Return(true));
108 EXPECT_CALL(provider2_
, IsInitializationComplete(_
))
109 .WillRepeatedly(Return(true));
116 POLICY_LEVEL_MANDATORY
,
118 new base::FundamentalValue(13),
120 provider0_
.UpdateChromePolicy(policy0_
);
122 PolicyServiceImpl::Providers providers
;
123 providers
.push_back(&provider0_
);
124 providers
.push_back(&provider1_
);
125 providers
.push_back(&provider2_
);
126 policy_service_
.reset(new PolicyServiceImpl(providers
));
129 void TearDown() override
{
130 provider0_
.Shutdown();
131 provider1_
.Shutdown();
132 provider2_
.Shutdown();
135 MOCK_METHOD2(OnPolicyValueUpdated
, void(const base::Value
*,
136 const base::Value
*));
138 MOCK_METHOD0(OnPolicyRefresh
, void());
140 // Returns true if the policies for namespace |ns| match |expected|.
141 bool VerifyPolicies(const PolicyNamespace
& ns
,
142 const PolicyMap
& expected
) {
143 return policy_service_
->GetPolicies(ns
).Equals(expected
);
146 void RunUntilIdle() {
152 base::MessageLoop loop_
;
153 MockConfigurationPolicyProvider provider0_
;
154 MockConfigurationPolicyProvider provider1_
;
155 MockConfigurationPolicyProvider provider2_
;
159 scoped_ptr
<PolicyServiceImpl
> policy_service_
;
162 DISALLOW_COPY_AND_ASSIGN(PolicyServiceTest
);
165 TEST_F(PolicyServiceTest
, LoadsPoliciesBeforeProvidersRefresh
) {
168 POLICY_LEVEL_MANDATORY
,
170 new base::FundamentalValue(13),
172 EXPECT_TRUE(VerifyPolicies(
173 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()), expected
));
176 TEST_F(PolicyServiceTest
, NotifyObservers
) {
177 MockPolicyServiceObserver observer
;
178 policy_service_
->AddObserver(POLICY_DOMAIN_CHROME
, &observer
);
180 PolicyMap expectedPrevious
;
181 expectedPrevious
.Set("pre",
182 POLICY_LEVEL_MANDATORY
,
184 new base::FundamentalValue(13),
187 PolicyMap expectedCurrent
;
188 expectedCurrent
.CopyFrom(expectedPrevious
);
189 expectedCurrent
.Set("aaa",
190 POLICY_LEVEL_MANDATORY
,
192 new base::FundamentalValue(123),
195 POLICY_LEVEL_MANDATORY
,
197 new base::FundamentalValue(123),
199 EXPECT_CALL(observer
, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME
,
201 PolicyEquals(&expectedPrevious
),
202 PolicyEquals(&expectedCurrent
)));
203 provider0_
.UpdateChromePolicy(policy0_
);
204 Mock::VerifyAndClearExpectations(&observer
);
207 EXPECT_CALL(observer
, OnPolicyUpdated(_
, _
, _
)).Times(0);
208 provider0_
.UpdateChromePolicy(policy0_
);
209 Mock::VerifyAndClearExpectations(&observer
);
210 EXPECT_TRUE(VerifyPolicies(
211 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()), expectedCurrent
));
214 expectedPrevious
.CopyFrom(expectedCurrent
);
215 expectedCurrent
.Set("bbb",
216 POLICY_LEVEL_MANDATORY
,
218 new base::FundamentalValue(456),
221 POLICY_LEVEL_MANDATORY
,
223 new base::FundamentalValue(456),
225 EXPECT_CALL(observer
, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME
,
227 PolicyEquals(&expectedPrevious
),
228 PolicyEquals(&expectedCurrent
)));
229 provider0_
.UpdateChromePolicy(policy0_
);
230 Mock::VerifyAndClearExpectations(&observer
);
233 expectedPrevious
.CopyFrom(expectedCurrent
);
234 expectedCurrent
.Erase("bbb");
235 policy0_
.Erase("bbb");
236 EXPECT_CALL(observer
, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME
,
238 PolicyEquals(&expectedPrevious
),
239 PolicyEquals(&expectedCurrent
)));
240 provider0_
.UpdateChromePolicy(policy0_
);
241 Mock::VerifyAndClearExpectations(&observer
);
244 expectedPrevious
.CopyFrom(expectedCurrent
);
245 expectedCurrent
.Set("aaa",
246 POLICY_LEVEL_MANDATORY
,
248 new base::FundamentalValue(789),
251 POLICY_LEVEL_MANDATORY
,
253 new base::FundamentalValue(789),
256 EXPECT_CALL(observer
, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME
,
258 PolicyEquals(&expectedPrevious
),
259 PolicyEquals(&expectedCurrent
)));
260 provider0_
.UpdateChromePolicy(policy0_
);
261 Mock::VerifyAndClearExpectations(&observer
);
264 EXPECT_CALL(observer
, OnPolicyUpdated(_
, _
, _
)).Times(0);
265 provider0_
.UpdateChromePolicy(policy0_
);
266 Mock::VerifyAndClearExpectations(&observer
);
267 EXPECT_TRUE(VerifyPolicies(
268 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()), expectedCurrent
));
270 policy_service_
->RemoveObserver(POLICY_DOMAIN_CHROME
, &observer
);
273 TEST_F(PolicyServiceTest
, NotifyObserversInMultipleNamespaces
) {
274 const std::string
kExtension0("extension-0");
275 const std::string
kExtension1("extension-1");
276 const std::string
kExtension2("extension-2");
277 MockPolicyServiceObserver chrome_observer
;
278 MockPolicyServiceObserver extension_observer
;
279 policy_service_
->AddObserver(POLICY_DOMAIN_CHROME
, &chrome_observer
);
280 policy_service_
->AddObserver(POLICY_DOMAIN_EXTENSIONS
, &extension_observer
);
282 PolicyMap previous_policy_map
;
283 previous_policy_map
.Set("pre",
284 POLICY_LEVEL_MANDATORY
,
286 new base::FundamentalValue(13),
288 PolicyMap policy_map
;
289 policy_map
.CopyFrom(previous_policy_map
);
290 policy_map
.Set("policy",
291 POLICY_LEVEL_MANDATORY
,
293 new base::StringValue("value"),
296 scoped_ptr
<PolicyBundle
> bundle(new PolicyBundle());
297 // The initial setup includes a policy for chrome that is now changing.
298 bundle
->Get(PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()))
299 .CopyFrom(policy_map
);
300 bundle
->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension0
))
301 .CopyFrom(policy_map
);
302 bundle
->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension1
))
303 .CopyFrom(policy_map
);
305 const PolicyMap kEmptyPolicyMap
;
308 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()),
309 PolicyEquals(&previous_policy_map
),
310 PolicyEquals(&policy_map
)));
313 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension0
),
314 PolicyEquals(&kEmptyPolicyMap
),
315 PolicyEquals(&policy_map
)));
318 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension1
),
319 PolicyEquals(&kEmptyPolicyMap
),
320 PolicyEquals(&policy_map
)));
321 provider0_
.UpdatePolicy(bundle
.Pass());
323 Mock::VerifyAndClearExpectations(&chrome_observer
);
324 Mock::VerifyAndClearExpectations(&extension_observer
);
326 // Chrome policy stays the same, kExtension0 is gone, kExtension1 changes,
327 // and kExtension2 is new.
328 previous_policy_map
.CopyFrom(policy_map
);
329 bundle
.reset(new PolicyBundle());
330 bundle
->Get(PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()))
331 .CopyFrom(policy_map
);
332 policy_map
.Set("policy",
333 POLICY_LEVEL_MANDATORY
,
335 new base::StringValue("another value"),
337 bundle
->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension1
))
338 .CopyFrom(policy_map
);
339 bundle
->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension2
))
340 .CopyFrom(policy_map
);
342 EXPECT_CALL(chrome_observer
, OnPolicyUpdated(_
, _
, _
)).Times(0);
345 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension0
),
346 PolicyEquals(&previous_policy_map
),
347 PolicyEquals(&kEmptyPolicyMap
)));
350 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension1
),
351 PolicyEquals(&previous_policy_map
),
352 PolicyEquals(&policy_map
)));
355 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension2
),
356 PolicyEquals(&kEmptyPolicyMap
),
357 PolicyEquals(&policy_map
)));
358 provider0_
.UpdatePolicy(bundle
.Pass());
360 Mock::VerifyAndClearExpectations(&chrome_observer
);
361 Mock::VerifyAndClearExpectations(&extension_observer
);
363 policy_service_
->RemoveObserver(POLICY_DOMAIN_CHROME
, &chrome_observer
);
364 policy_service_
->RemoveObserver(POLICY_DOMAIN_EXTENSIONS
,
365 &extension_observer
);
368 TEST_F(PolicyServiceTest
, ObserverChangesPolicy
) {
369 ChangePolicyObserver
observer(&provider0_
);
370 policy_service_
->AddObserver(POLICY_DOMAIN_CHROME
, &observer
);
372 POLICY_LEVEL_MANDATORY
,
374 new base::FundamentalValue(123),
377 POLICY_LEVEL_MANDATORY
,
379 new base::FundamentalValue(1234),
382 provider0_
.UpdateChromePolicy(policy0_
);
383 policy_service_
->RemoveObserver(POLICY_DOMAIN_CHROME
, &observer
);
384 EXPECT_TRUE(observer
.observer_invoked());
387 TEST_F(PolicyServiceTest
, Priorities
) {
390 POLICY_LEVEL_MANDATORY
,
392 new base::FundamentalValue(13),
395 POLICY_LEVEL_MANDATORY
,
397 new base::FundamentalValue(0),
400 POLICY_LEVEL_MANDATORY
,
402 new base::FundamentalValue(0),
405 POLICY_LEVEL_MANDATORY
,
407 new base::FundamentalValue(1),
410 POLICY_LEVEL_MANDATORY
,
412 new base::FundamentalValue(2),
414 provider0_
.UpdateChromePolicy(policy0_
);
415 provider1_
.UpdateChromePolicy(policy1_
);
416 provider2_
.UpdateChromePolicy(policy2_
);
417 EXPECT_TRUE(VerifyPolicies(
418 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()), expected
));
421 POLICY_LEVEL_MANDATORY
,
423 new base::FundamentalValue(1),
425 policy0_
.Erase("aaa");
426 provider0_
.UpdateChromePolicy(policy0_
);
427 EXPECT_TRUE(VerifyPolicies(
428 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()), expected
));
431 POLICY_LEVEL_MANDATORY
,
433 new base::FundamentalValue(2),
436 POLICY_LEVEL_RECOMMENDED
,
438 new base::FundamentalValue(1),
440 provider1_
.UpdateChromePolicy(policy1_
);
441 EXPECT_TRUE(VerifyPolicies(
442 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()), expected
));
445 TEST_F(PolicyServiceTest
, PolicyChangeRegistrar
) {
446 scoped_ptr
<PolicyChangeRegistrar
> registrar(new PolicyChangeRegistrar(
447 policy_service_
.get(),
448 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string())));
450 // Starting to observe existing policies doesn't trigger a notification.
451 EXPECT_CALL(*this, OnPolicyValueUpdated(_
, _
)).Times(0);
452 registrar
->Observe("pre", base::Bind(
453 &PolicyServiceTest::OnPolicyValueUpdated
,
454 base::Unretained(this)));
455 registrar
->Observe("aaa", base::Bind(
456 &PolicyServiceTest::OnPolicyValueUpdated
,
457 base::Unretained(this)));
459 Mock::VerifyAndClearExpectations(this);
461 // Changing it now triggers a notification.
462 base::FundamentalValue
kValue0(0);
463 EXPECT_CALL(*this, OnPolicyValueUpdated(NULL
, ValueEquals(&kValue0
)));
464 policy0_
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
465 kValue0
.DeepCopy(), NULL
);
466 provider0_
.UpdateChromePolicy(policy0_
);
467 Mock::VerifyAndClearExpectations(this);
469 // Changing other values doesn't trigger a notification.
470 EXPECT_CALL(*this, OnPolicyValueUpdated(_
, _
)).Times(0);
471 policy0_
.Set("bbb", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
472 kValue0
.DeepCopy(), NULL
);
473 provider0_
.UpdateChromePolicy(policy0_
);
474 Mock::VerifyAndClearExpectations(this);
476 // Modifying the value triggers a notification.
477 base::FundamentalValue
kValue1(1);
478 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue0
),
479 ValueEquals(&kValue1
)));
480 policy0_
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
481 kValue1
.DeepCopy(), NULL
);
482 provider0_
.UpdateChromePolicy(policy0_
);
483 Mock::VerifyAndClearExpectations(this);
485 // Removing the value triggers a notification.
486 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue1
), NULL
));
487 policy0_
.Erase("aaa");
488 provider0_
.UpdateChromePolicy(policy0_
);
489 Mock::VerifyAndClearExpectations(this);
491 // No more notifications after destroying the registrar.
492 EXPECT_CALL(*this, OnPolicyValueUpdated(_
, _
)).Times(0);
494 policy0_
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
495 kValue1
.DeepCopy(), NULL
);
496 policy0_
.Set("pre", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
497 kValue1
.DeepCopy(), NULL
);
498 provider0_
.UpdateChromePolicy(policy0_
);
499 Mock::VerifyAndClearExpectations(this);
502 TEST_F(PolicyServiceTest
, RefreshPolicies
) {
503 EXPECT_CALL(provider0_
, RefreshPolicies()).Times(AnyNumber());
504 EXPECT_CALL(provider1_
, RefreshPolicies()).Times(AnyNumber());
505 EXPECT_CALL(provider2_
, RefreshPolicies()).Times(AnyNumber());
507 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
508 policy_service_
->RefreshPolicies(base::Bind(
509 &PolicyServiceTest::OnPolicyRefresh
,
510 base::Unretained(this)));
511 // Let any queued observer tasks run.
513 Mock::VerifyAndClearExpectations(this);
515 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
516 base::FundamentalValue
kValue0(0);
517 policy0_
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
518 kValue0
.DeepCopy(), NULL
);
519 provider0_
.UpdateChromePolicy(policy0_
);
520 Mock::VerifyAndClearExpectations(this);
522 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
523 base::FundamentalValue
kValue1(1);
524 policy1_
.Set("aaa", POLICY_LEVEL_RECOMMENDED
, POLICY_SCOPE_USER
,
525 kValue1
.DeepCopy(), NULL
);
526 provider1_
.UpdateChromePolicy(policy1_
);
527 Mock::VerifyAndClearExpectations(this);
529 // A provider can refresh more than once after a RefreshPolicies call, but
530 // OnPolicyRefresh should be triggered only after all providers are
532 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
533 policy1_
.Set("bbb", POLICY_LEVEL_RECOMMENDED
, POLICY_SCOPE_USER
,
534 kValue1
.DeepCopy(), NULL
);
535 provider1_
.UpdateChromePolicy(policy1_
);
536 Mock::VerifyAndClearExpectations(this);
538 // If another RefreshPolicies() call happens while waiting for a previous
539 // one to complete, then all providers must refresh again.
540 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
541 policy_service_
->RefreshPolicies(base::Bind(
542 &PolicyServiceTest::OnPolicyRefresh
,
543 base::Unretained(this)));
545 Mock::VerifyAndClearExpectations(this);
547 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
548 policy2_
.Set("bbb", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
549 kValue0
.DeepCopy(), NULL
);
550 provider2_
.UpdateChromePolicy(policy2_
);
551 Mock::VerifyAndClearExpectations(this);
553 // Providers 0 and 1 must reload again.
554 EXPECT_CALL(*this, OnPolicyRefresh()).Times(2);
555 base::FundamentalValue
kValue2(2);
556 policy0_
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
557 kValue2
.DeepCopy(), NULL
);
558 provider0_
.UpdateChromePolicy(policy0_
);
559 provider1_
.UpdateChromePolicy(policy1_
);
560 Mock::VerifyAndClearExpectations(this);
562 const PolicyMap
& policies
= policy_service_
->GetPolicies(
563 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()));
564 EXPECT_TRUE(base::Value::Equals(&kValue2
, policies
.GetValue("aaa")));
565 EXPECT_TRUE(base::Value::Equals(&kValue0
, policies
.GetValue("bbb")));
568 TEST_F(PolicyServiceTest
, NamespaceMerge
) {
569 scoped_ptr
<PolicyBundle
> bundle0(new PolicyBundle());
570 scoped_ptr
<PolicyBundle
> bundle1(new PolicyBundle());
571 scoped_ptr
<PolicyBundle
> bundle2(new PolicyBundle());
573 AddTestPolicies(bundle0
.get(), "bundle0",
574 POLICY_LEVEL_RECOMMENDED
, POLICY_SCOPE_USER
);
575 AddTestPolicies(bundle1
.get(), "bundle1",
576 POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
);
577 AddTestPolicies(bundle2
.get(), "bundle2",
578 POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_MACHINE
);
580 provider0_
.UpdatePolicy(bundle0
.Pass());
581 provider1_
.UpdatePolicy(bundle1
.Pass());
582 provider2_
.UpdatePolicy(bundle2
.Pass());
586 // For policies of the same level and scope, the first provider takes
587 // precedence, on every namespace.
588 expected
.Set(kSameLevelPolicy
,
589 POLICY_LEVEL_MANDATORY
,
591 new base::StringValue("bundle0"),
593 // For policies with different levels and scopes, the highest priority
594 // level/scope combination takes precedence, on every namespace.
595 expected
.Set(kDiffLevelPolicy
,
596 POLICY_LEVEL_MANDATORY
,
597 POLICY_SCOPE_MACHINE
,
598 new base::StringValue("bundle2"),
600 EXPECT_TRUE(policy_service_
->GetPolicies(
601 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string())).Equals(expected
));
602 EXPECT_TRUE(policy_service_
->GetPolicies(
603 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension
)).Equals(expected
));
606 TEST_F(PolicyServiceTest
, IsInitializationComplete
) {
607 // |provider0| has all domains initialized.
608 Mock::VerifyAndClearExpectations(&provider1_
);
609 Mock::VerifyAndClearExpectations(&provider2_
);
610 EXPECT_CALL(provider1_
, IsInitializationComplete(_
))
611 .WillRepeatedly(Return(false));
612 EXPECT_CALL(provider2_
, IsInitializationComplete(_
))
613 .WillRepeatedly(Return(false));
614 PolicyServiceImpl::Providers providers
;
615 providers
.push_back(&provider0_
);
616 providers
.push_back(&provider1_
);
617 providers
.push_back(&provider2_
);
618 policy_service_
.reset(new PolicyServiceImpl(providers
));
619 EXPECT_FALSE(policy_service_
->IsInitializationComplete(POLICY_DOMAIN_CHROME
));
621 policy_service_
->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
));
623 // |provider2_| still doesn't have POLICY_DOMAIN_CHROME initialized, so
624 // the initialization status of that domain won't change.
625 MockPolicyServiceObserver observer
;
626 policy_service_
->AddObserver(POLICY_DOMAIN_CHROME
, &observer
);
627 policy_service_
->AddObserver(POLICY_DOMAIN_EXTENSIONS
, &observer
);
628 EXPECT_CALL(observer
, OnPolicyServiceInitialized(_
)).Times(0);
629 Mock::VerifyAndClearExpectations(&provider1_
);
630 EXPECT_CALL(provider1_
, IsInitializationComplete(POLICY_DOMAIN_CHROME
))
631 .WillRepeatedly(Return(true));
632 EXPECT_CALL(provider1_
, IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
))
633 .WillRepeatedly(Return(false));
634 const PolicyMap kPolicyMap
;
635 provider1_
.UpdateChromePolicy(kPolicyMap
);
636 Mock::VerifyAndClearExpectations(&observer
);
637 EXPECT_FALSE(policy_service_
->IsInitializationComplete(POLICY_DOMAIN_CHROME
));
639 policy_service_
->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
));
641 // Same if |provider1_| doesn't have POLICY_DOMAIN_EXTENSIONS initialized.
642 EXPECT_CALL(observer
, OnPolicyServiceInitialized(_
)).Times(0);
643 Mock::VerifyAndClearExpectations(&provider2_
);
644 EXPECT_CALL(provider2_
, IsInitializationComplete(POLICY_DOMAIN_CHROME
))
645 .WillRepeatedly(Return(false));
646 EXPECT_CALL(provider2_
, IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
))
647 .WillRepeatedly(Return(true));
648 provider2_
.UpdateChromePolicy(kPolicyMap
);
649 Mock::VerifyAndClearExpectations(&observer
);
650 EXPECT_FALSE(policy_service_
->IsInitializationComplete(POLICY_DOMAIN_CHROME
));
652 policy_service_
->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
));
654 // Now initialize POLICY_DOMAIN_CHROME on all the providers.
655 EXPECT_CALL(observer
, OnPolicyServiceInitialized(POLICY_DOMAIN_CHROME
));
656 Mock::VerifyAndClearExpectations(&provider2_
);
657 EXPECT_CALL(provider2_
, IsInitializationComplete(POLICY_DOMAIN_CHROME
))
658 .WillRepeatedly(Return(true));
659 EXPECT_CALL(provider2_
, IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
))
660 .WillRepeatedly(Return(true));
661 provider2_
.UpdateChromePolicy(kPolicyMap
);
662 Mock::VerifyAndClearExpectations(&observer
);
663 EXPECT_TRUE(policy_service_
->IsInitializationComplete(POLICY_DOMAIN_CHROME
));
664 // Other domains are still not initialized.
666 policy_service_
->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
));
668 // Initialize the remaining domain.
669 EXPECT_CALL(observer
, OnPolicyServiceInitialized(POLICY_DOMAIN_EXTENSIONS
));
670 Mock::VerifyAndClearExpectations(&provider1_
);
671 EXPECT_CALL(provider1_
, IsInitializationComplete(POLICY_DOMAIN_CHROME
))
672 .WillRepeatedly(Return(true));
673 EXPECT_CALL(provider1_
, IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
))
674 .WillRepeatedly(Return(true));
675 provider1_
.UpdateChromePolicy(kPolicyMap
);
676 Mock::VerifyAndClearExpectations(&observer
);
677 EXPECT_TRUE(policy_service_
->IsInitializationComplete(POLICY_DOMAIN_CHROME
));
679 policy_service_
->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
));
682 policy_service_
->RemoveObserver(POLICY_DOMAIN_CHROME
, &observer
);
683 policy_service_
->RemoveObserver(POLICY_DOMAIN_EXTENSIONS
, &observer
);
686 TEST_F(PolicyServiceTest
, FixDeprecatedPolicies
) {
687 const PolicyNamespace
chrome_namespace(POLICY_DOMAIN_CHROME
, std::string());
688 const PolicyNamespace
extension_namespace(POLICY_DOMAIN_EXTENSIONS
, "xyz");
690 scoped_ptr
<PolicyBundle
> policy_bundle(new PolicyBundle());
691 PolicyMap
& policy_map
= policy_bundle
->Get(chrome_namespace
);
692 // Individual proxy policy values in the Chrome namespace should be collected
693 // into a dictionary.
694 policy_map
.Set(key::kProxyServerMode
,
695 POLICY_LEVEL_MANDATORY
,
697 new base::FundamentalValue(3),
700 // Both these policies should be ignored, since there's a higher priority
702 policy_map
.Set(key::kProxyMode
,
703 POLICY_LEVEL_RECOMMENDED
,
705 new base::StringValue("pac_script"),
707 policy_map
.Set(key::kProxyPacUrl
,
708 POLICY_LEVEL_RECOMMENDED
,
710 new base::StringValue("http://example.com/wpad.dat"),
713 // Add a value to a non-Chrome namespace.
714 policy_bundle
->Get(extension_namespace
).Set(key::kProxyServerMode
,
715 POLICY_LEVEL_MANDATORY
,
717 new base::FundamentalValue(3),
720 // The resulting Chrome namespace map should have the collected policy.
721 PolicyMap expected_chrome
;
722 scoped_ptr
<base::DictionaryValue
> expected_value(new base::DictionaryValue
);
723 expected_value
->SetInteger(key::kProxyServerMode
, 3);
724 expected_chrome
.Set(key::kProxySettings
, POLICY_LEVEL_MANDATORY
,
725 POLICY_SCOPE_USER
, expected_value
.release(), NULL
);
727 // The resulting Extensions namespace map shouldn't have been modified.
728 PolicyMap expected_extension
;
729 expected_extension
.Set(key::kProxyServerMode
,
730 POLICY_LEVEL_MANDATORY
,
732 new base::FundamentalValue(3),
735 provider0_
.UpdatePolicy(policy_bundle
.Pass());
738 EXPECT_TRUE(VerifyPolicies(chrome_namespace
, expected_chrome
));
739 EXPECT_TRUE(VerifyPolicies(extension_namespace
, expected_extension
));
742 } // namespace policy