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
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
53 base::Value::CreateStringValue(value
), NULL
);
54 policy_map
->Set(kDiffLevelPolicy
, level
, scope
,
55 base::Value::CreateStringValue(value
), NULL
);
57 &bundle
->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension
));
58 policy_map
->Set(kSameLevelPolicy
, POLICY_LEVEL_MANDATORY
,
59 POLICY_SCOPE_USER
, base::Value::CreateStringValue(value
),
61 policy_map
->Set(kDiffLevelPolicy
, level
, scope
,
62 base::Value::CreateStringValue(value
), NULL
);
65 // Observer class that changes the policy in the passed provider when the
66 // callback is invoked.
67 class ChangePolicyObserver
: public PolicyService::Observer
{
69 explicit ChangePolicyObserver(MockConfigurationPolicyProvider
* provider
)
70 : provider_(provider
),
71 observer_invoked_(false) {}
73 virtual void OnPolicyUpdated(const PolicyNamespace
&,
74 const PolicyMap
& previous
,
75 const PolicyMap
& current
) OVERRIDE
{
77 new_policy
.Set("foo", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
78 base::Value::CreateIntegerValue(14), NULL
);
79 provider_
->UpdateChromePolicy(new_policy
);
80 observer_invoked_
= true;
83 bool observer_invoked() const { return observer_invoked_
; }
86 MockConfigurationPolicyProvider
* provider_
;
87 bool observer_invoked_
;
92 class PolicyServiceTest
: public testing::Test
{
94 PolicyServiceTest() {}
95 virtual void SetUp() OVERRIDE
{
96 EXPECT_CALL(provider0_
, IsInitializationComplete(_
))
97 .WillRepeatedly(Return(true));
98 EXPECT_CALL(provider1_
, IsInitializationComplete(_
))
99 .WillRepeatedly(Return(true));
100 EXPECT_CALL(provider2_
, IsInitializationComplete(_
))
101 .WillRepeatedly(Return(true));
107 policy0_
.Set("pre", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
108 base::Value::CreateIntegerValue(13), NULL
);
109 provider0_
.UpdateChromePolicy(policy0_
);
111 PolicyServiceImpl::Providers providers
;
112 providers
.push_back(&provider0_
);
113 providers
.push_back(&provider1_
);
114 providers
.push_back(&provider2_
);
115 policy_service_
.reset(new PolicyServiceImpl(providers
));
118 virtual void TearDown() OVERRIDE
{
119 provider0_
.Shutdown();
120 provider1_
.Shutdown();
121 provider2_
.Shutdown();
124 MOCK_METHOD2(OnPolicyValueUpdated
, void(const base::Value
*,
125 const base::Value
*));
127 MOCK_METHOD0(OnPolicyRefresh
, void());
129 // Returns true if the policies for namespace |ns| match |expected|.
130 bool VerifyPolicies(const PolicyNamespace
& ns
,
131 const PolicyMap
& expected
) {
132 return policy_service_
->GetPolicies(ns
).Equals(expected
);
135 void RunUntilIdle() {
141 base::MessageLoop loop_
;
142 MockConfigurationPolicyProvider provider0_
;
143 MockConfigurationPolicyProvider provider1_
;
144 MockConfigurationPolicyProvider provider2_
;
148 scoped_ptr
<PolicyServiceImpl
> policy_service_
;
151 DISALLOW_COPY_AND_ASSIGN(PolicyServiceTest
);
154 TEST_F(PolicyServiceTest
, LoadsPoliciesBeforeProvidersRefresh
) {
156 expected
.Set("pre", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
157 base::Value::CreateIntegerValue(13), NULL
);
158 EXPECT_TRUE(VerifyPolicies(
159 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()), expected
));
162 TEST_F(PolicyServiceTest
, NotifyObservers
) {
163 MockPolicyServiceObserver observer
;
164 policy_service_
->AddObserver(POLICY_DOMAIN_CHROME
, &observer
);
166 PolicyMap expectedPrevious
;
167 expectedPrevious
.Set("pre", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
168 base::Value::CreateIntegerValue(13), NULL
);
170 PolicyMap expectedCurrent
;
171 expectedCurrent
.CopyFrom(expectedPrevious
);
172 expectedCurrent
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
173 base::Value::CreateIntegerValue(123), NULL
);
174 policy0_
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
175 base::Value::CreateIntegerValue(123), NULL
);
176 EXPECT_CALL(observer
, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME
,
178 PolicyEquals(&expectedPrevious
),
179 PolicyEquals(&expectedCurrent
)));
180 provider0_
.UpdateChromePolicy(policy0_
);
181 Mock::VerifyAndClearExpectations(&observer
);
184 EXPECT_CALL(observer
, OnPolicyUpdated(_
, _
, _
)).Times(0);
185 provider0_
.UpdateChromePolicy(policy0_
);
186 Mock::VerifyAndClearExpectations(&observer
);
187 EXPECT_TRUE(VerifyPolicies(
188 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()), expectedCurrent
));
191 expectedPrevious
.CopyFrom(expectedCurrent
);
192 expectedCurrent
.Set("bbb", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
193 base::Value::CreateIntegerValue(456), NULL
);
194 policy0_
.Set("bbb", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
195 base::Value::CreateIntegerValue(456), NULL
);
196 EXPECT_CALL(observer
, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME
,
198 PolicyEquals(&expectedPrevious
),
199 PolicyEquals(&expectedCurrent
)));
200 provider0_
.UpdateChromePolicy(policy0_
);
201 Mock::VerifyAndClearExpectations(&observer
);
204 expectedPrevious
.CopyFrom(expectedCurrent
);
205 expectedCurrent
.Erase("bbb");
206 policy0_
.Erase("bbb");
207 EXPECT_CALL(observer
, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME
,
209 PolicyEquals(&expectedPrevious
),
210 PolicyEquals(&expectedCurrent
)));
211 provider0_
.UpdateChromePolicy(policy0_
);
212 Mock::VerifyAndClearExpectations(&observer
);
215 expectedPrevious
.CopyFrom(expectedCurrent
);
216 expectedCurrent
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
217 base::Value::CreateIntegerValue(789), NULL
);
218 policy0_
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
219 base::Value::CreateIntegerValue(789), NULL
);
221 EXPECT_CALL(observer
, OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME
,
223 PolicyEquals(&expectedPrevious
),
224 PolicyEquals(&expectedCurrent
)));
225 provider0_
.UpdateChromePolicy(policy0_
);
226 Mock::VerifyAndClearExpectations(&observer
);
229 EXPECT_CALL(observer
, OnPolicyUpdated(_
, _
, _
)).Times(0);
230 provider0_
.UpdateChromePolicy(policy0_
);
231 Mock::VerifyAndClearExpectations(&observer
);
232 EXPECT_TRUE(VerifyPolicies(
233 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()), expectedCurrent
));
235 policy_service_
->RemoveObserver(POLICY_DOMAIN_CHROME
, &observer
);
238 TEST_F(PolicyServiceTest
, NotifyObserversInMultipleNamespaces
) {
239 const std::string
kExtension0("extension-0");
240 const std::string
kExtension1("extension-1");
241 const std::string
kExtension2("extension-2");
242 MockPolicyServiceObserver chrome_observer
;
243 MockPolicyServiceObserver extension_observer
;
244 policy_service_
->AddObserver(POLICY_DOMAIN_CHROME
, &chrome_observer
);
245 policy_service_
->AddObserver(POLICY_DOMAIN_EXTENSIONS
, &extension_observer
);
247 PolicyMap previous_policy_map
;
248 previous_policy_map
.Set("pre", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
249 base::Value::CreateIntegerValue(13), NULL
);
250 PolicyMap policy_map
;
251 policy_map
.CopyFrom(previous_policy_map
);
252 policy_map
.Set("policy", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
253 base::Value::CreateStringValue("value"), NULL
);
255 scoped_ptr
<PolicyBundle
> bundle(new PolicyBundle());
256 // The initial setup includes a policy for chrome that is now changing.
257 bundle
->Get(PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()))
258 .CopyFrom(policy_map
);
259 bundle
->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension0
))
260 .CopyFrom(policy_map
);
261 bundle
->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension1
))
262 .CopyFrom(policy_map
);
264 const PolicyMap kEmptyPolicyMap
;
267 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()),
268 PolicyEquals(&previous_policy_map
),
269 PolicyEquals(&policy_map
)));
272 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension0
),
273 PolicyEquals(&kEmptyPolicyMap
),
274 PolicyEquals(&policy_map
)));
277 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension1
),
278 PolicyEquals(&kEmptyPolicyMap
),
279 PolicyEquals(&policy_map
)));
280 provider0_
.UpdatePolicy(bundle
.Pass());
282 Mock::VerifyAndClearExpectations(&chrome_observer
);
283 Mock::VerifyAndClearExpectations(&extension_observer
);
285 // Chrome policy stays the same, kExtension0 is gone, kExtension1 changes,
286 // and kExtension2 is new.
287 previous_policy_map
.CopyFrom(policy_map
);
288 bundle
.reset(new PolicyBundle());
289 bundle
->Get(PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()))
290 .CopyFrom(policy_map
);
291 policy_map
.Set("policy", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
292 base::Value::CreateStringValue("another value"), NULL
);
293 bundle
->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension1
))
294 .CopyFrom(policy_map
);
295 bundle
->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension2
))
296 .CopyFrom(policy_map
);
298 EXPECT_CALL(chrome_observer
, OnPolicyUpdated(_
, _
, _
)).Times(0);
301 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension0
),
302 PolicyEquals(&previous_policy_map
),
303 PolicyEquals(&kEmptyPolicyMap
)));
306 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension1
),
307 PolicyEquals(&previous_policy_map
),
308 PolicyEquals(&policy_map
)));
311 OnPolicyUpdated(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension2
),
312 PolicyEquals(&kEmptyPolicyMap
),
313 PolicyEquals(&policy_map
)));
314 provider0_
.UpdatePolicy(bundle
.Pass());
316 Mock::VerifyAndClearExpectations(&chrome_observer
);
317 Mock::VerifyAndClearExpectations(&extension_observer
);
319 policy_service_
->RemoveObserver(POLICY_DOMAIN_CHROME
, &chrome_observer
);
320 policy_service_
->RemoveObserver(POLICY_DOMAIN_EXTENSIONS
,
321 &extension_observer
);
324 TEST_F(PolicyServiceTest
, ObserverChangesPolicy
) {
325 ChangePolicyObserver
observer(&provider0_
);
326 policy_service_
->AddObserver(POLICY_DOMAIN_CHROME
, &observer
);
327 policy0_
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
328 base::Value::CreateIntegerValue(123), NULL
);
329 policy0_
.Set("bbb", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
330 base::Value::CreateIntegerValue(1234), NULL
);
332 provider0_
.UpdateChromePolicy(policy0_
);
333 policy_service_
->RemoveObserver(POLICY_DOMAIN_CHROME
, &observer
);
334 EXPECT_TRUE(observer
.observer_invoked());
337 TEST_F(PolicyServiceTest
, Priorities
) {
339 expected
.Set("pre", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
340 base::Value::CreateIntegerValue(13), NULL
);
341 expected
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
342 base::Value::CreateIntegerValue(0), NULL
);
343 policy0_
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
344 base::Value::CreateIntegerValue(0), NULL
);
345 policy1_
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
346 base::Value::CreateIntegerValue(1), NULL
);
347 policy2_
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
348 base::Value::CreateIntegerValue(2), NULL
);
349 provider0_
.UpdateChromePolicy(policy0_
);
350 provider1_
.UpdateChromePolicy(policy1_
);
351 provider2_
.UpdateChromePolicy(policy2_
);
352 EXPECT_TRUE(VerifyPolicies(
353 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()), expected
));
355 expected
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
356 base::Value::CreateIntegerValue(1), NULL
);
357 policy0_
.Erase("aaa");
358 provider0_
.UpdateChromePolicy(policy0_
);
359 EXPECT_TRUE(VerifyPolicies(
360 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()), expected
));
362 expected
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
363 base::Value::CreateIntegerValue(2), NULL
);
364 policy1_
.Set("aaa", POLICY_LEVEL_RECOMMENDED
, POLICY_SCOPE_USER
,
365 base::Value::CreateIntegerValue(1), NULL
);
366 provider1_
.UpdateChromePolicy(policy1_
);
367 EXPECT_TRUE(VerifyPolicies(
368 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()), expected
));
371 TEST_F(PolicyServiceTest
, PolicyChangeRegistrar
) {
372 scoped_ptr
<PolicyChangeRegistrar
> registrar(new PolicyChangeRegistrar(
373 policy_service_
.get(),
374 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string())));
376 // Starting to observe existing policies doesn't trigger a notification.
377 EXPECT_CALL(*this, OnPolicyValueUpdated(_
, _
)).Times(0);
378 registrar
->Observe("pre", base::Bind(
379 &PolicyServiceTest::OnPolicyValueUpdated
,
380 base::Unretained(this)));
381 registrar
->Observe("aaa", base::Bind(
382 &PolicyServiceTest::OnPolicyValueUpdated
,
383 base::Unretained(this)));
385 Mock::VerifyAndClearExpectations(this);
387 // Changing it now triggers a notification.
388 base::FundamentalValue
kValue0(0);
389 EXPECT_CALL(*this, OnPolicyValueUpdated(NULL
, ValueEquals(&kValue0
)));
390 policy0_
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
391 kValue0
.DeepCopy(), NULL
);
392 provider0_
.UpdateChromePolicy(policy0_
);
393 Mock::VerifyAndClearExpectations(this);
395 // Changing other values doesn't trigger a notification.
396 EXPECT_CALL(*this, OnPolicyValueUpdated(_
, _
)).Times(0);
397 policy0_
.Set("bbb", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
398 kValue0
.DeepCopy(), NULL
);
399 provider0_
.UpdateChromePolicy(policy0_
);
400 Mock::VerifyAndClearExpectations(this);
402 // Modifying the value triggers a notification.
403 base::FundamentalValue
kValue1(1);
404 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue0
),
405 ValueEquals(&kValue1
)));
406 policy0_
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
407 kValue1
.DeepCopy(), NULL
);
408 provider0_
.UpdateChromePolicy(policy0_
);
409 Mock::VerifyAndClearExpectations(this);
411 // Removing the value triggers a notification.
412 EXPECT_CALL(*this, OnPolicyValueUpdated(ValueEquals(&kValue1
), NULL
));
413 policy0_
.Erase("aaa");
414 provider0_
.UpdateChromePolicy(policy0_
);
415 Mock::VerifyAndClearExpectations(this);
417 // No more notifications after destroying the registrar.
418 EXPECT_CALL(*this, OnPolicyValueUpdated(_
, _
)).Times(0);
420 policy0_
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
421 kValue1
.DeepCopy(), NULL
);
422 policy0_
.Set("pre", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
423 kValue1
.DeepCopy(), NULL
);
424 provider0_
.UpdateChromePolicy(policy0_
);
425 Mock::VerifyAndClearExpectations(this);
428 TEST_F(PolicyServiceTest
, RefreshPolicies
) {
429 EXPECT_CALL(provider0_
, RefreshPolicies()).Times(AnyNumber());
430 EXPECT_CALL(provider1_
, RefreshPolicies()).Times(AnyNumber());
431 EXPECT_CALL(provider2_
, RefreshPolicies()).Times(AnyNumber());
433 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
434 policy_service_
->RefreshPolicies(base::Bind(
435 &PolicyServiceTest::OnPolicyRefresh
,
436 base::Unretained(this)));
437 // Let any queued observer tasks run.
439 Mock::VerifyAndClearExpectations(this);
441 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
442 base::FundamentalValue
kValue0(0);
443 policy0_
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
444 kValue0
.DeepCopy(), NULL
);
445 provider0_
.UpdateChromePolicy(policy0_
);
446 Mock::VerifyAndClearExpectations(this);
448 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
449 base::FundamentalValue
kValue1(1);
450 policy1_
.Set("aaa", POLICY_LEVEL_RECOMMENDED
, POLICY_SCOPE_USER
,
451 kValue1
.DeepCopy(), NULL
);
452 provider1_
.UpdateChromePolicy(policy1_
);
453 Mock::VerifyAndClearExpectations(this);
455 // A provider can refresh more than once after a RefreshPolicies call, but
456 // OnPolicyRefresh should be triggered only after all providers are
458 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
459 policy1_
.Set("bbb", POLICY_LEVEL_RECOMMENDED
, POLICY_SCOPE_USER
,
460 kValue1
.DeepCopy(), NULL
);
461 provider1_
.UpdateChromePolicy(policy1_
);
462 Mock::VerifyAndClearExpectations(this);
464 // If another RefreshPolicies() call happens while waiting for a previous
465 // one to complete, then all providers must refresh again.
466 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
467 policy_service_
->RefreshPolicies(base::Bind(
468 &PolicyServiceTest::OnPolicyRefresh
,
469 base::Unretained(this)));
471 Mock::VerifyAndClearExpectations(this);
473 EXPECT_CALL(*this, OnPolicyRefresh()).Times(0);
474 policy2_
.Set("bbb", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
475 kValue0
.DeepCopy(), NULL
);
476 provider2_
.UpdateChromePolicy(policy2_
);
477 Mock::VerifyAndClearExpectations(this);
479 // Providers 0 and 1 must reload again.
480 EXPECT_CALL(*this, OnPolicyRefresh()).Times(2);
481 base::FundamentalValue
kValue2(2);
482 policy0_
.Set("aaa", POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
483 kValue2
.DeepCopy(), NULL
);
484 provider0_
.UpdateChromePolicy(policy0_
);
485 provider1_
.UpdateChromePolicy(policy1_
);
486 Mock::VerifyAndClearExpectations(this);
488 const PolicyMap
& policies
= policy_service_
->GetPolicies(
489 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string()));
490 EXPECT_TRUE(base::Value::Equals(&kValue2
, policies
.GetValue("aaa")));
491 EXPECT_TRUE(base::Value::Equals(&kValue0
, policies
.GetValue("bbb")));
494 TEST_F(PolicyServiceTest
, NamespaceMerge
) {
495 scoped_ptr
<PolicyBundle
> bundle0(new PolicyBundle());
496 scoped_ptr
<PolicyBundle
> bundle1(new PolicyBundle());
497 scoped_ptr
<PolicyBundle
> bundle2(new PolicyBundle());
499 AddTestPolicies(bundle0
.get(), "bundle0",
500 POLICY_LEVEL_RECOMMENDED
, POLICY_SCOPE_USER
);
501 AddTestPolicies(bundle1
.get(), "bundle1",
502 POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
);
503 AddTestPolicies(bundle2
.get(), "bundle2",
504 POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_MACHINE
);
506 provider0_
.UpdatePolicy(bundle0
.Pass());
507 provider1_
.UpdatePolicy(bundle1
.Pass());
508 provider2_
.UpdatePolicy(bundle2
.Pass());
512 // For policies of the same level and scope, the first provider takes
513 // precedence, on every namespace.
514 expected
.Set(kSameLevelPolicy
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
515 base::Value::CreateStringValue("bundle0"), NULL
);
516 // For policies with different levels and scopes, the highest priority
517 // level/scope combination takes precedence, on every namespace.
518 expected
.Set(kDiffLevelPolicy
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_MACHINE
,
519 base::Value::CreateStringValue("bundle2"), NULL
);
520 EXPECT_TRUE(policy_service_
->GetPolicies(
521 PolicyNamespace(POLICY_DOMAIN_CHROME
, std::string())).Equals(expected
));
522 EXPECT_TRUE(policy_service_
->GetPolicies(
523 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS
, kExtension
)).Equals(expected
));
526 TEST_F(PolicyServiceTest
, IsInitializationComplete
) {
527 // |provider0| has all domains initialized.
528 Mock::VerifyAndClearExpectations(&provider1_
);
529 Mock::VerifyAndClearExpectations(&provider2_
);
530 EXPECT_CALL(provider1_
, IsInitializationComplete(_
))
531 .WillRepeatedly(Return(false));
532 EXPECT_CALL(provider2_
, IsInitializationComplete(_
))
533 .WillRepeatedly(Return(false));
534 PolicyServiceImpl::Providers providers
;
535 providers
.push_back(&provider0_
);
536 providers
.push_back(&provider1_
);
537 providers
.push_back(&provider2_
);
538 policy_service_
.reset(new PolicyServiceImpl(providers
));
539 EXPECT_FALSE(policy_service_
->IsInitializationComplete(POLICY_DOMAIN_CHROME
));
541 policy_service_
->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
));
543 // |provider2_| still doesn't have POLICY_DOMAIN_CHROME initialized, so
544 // the initialization status of that domain won't change.
545 MockPolicyServiceObserver observer
;
546 policy_service_
->AddObserver(POLICY_DOMAIN_CHROME
, &observer
);
547 policy_service_
->AddObserver(POLICY_DOMAIN_EXTENSIONS
, &observer
);
548 EXPECT_CALL(observer
, OnPolicyServiceInitialized(_
)).Times(0);
549 Mock::VerifyAndClearExpectations(&provider1_
);
550 EXPECT_CALL(provider1_
, IsInitializationComplete(POLICY_DOMAIN_CHROME
))
551 .WillRepeatedly(Return(true));
552 EXPECT_CALL(provider1_
, IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
))
553 .WillRepeatedly(Return(false));
554 const PolicyMap kPolicyMap
;
555 provider1_
.UpdateChromePolicy(kPolicyMap
);
556 Mock::VerifyAndClearExpectations(&observer
);
557 EXPECT_FALSE(policy_service_
->IsInitializationComplete(POLICY_DOMAIN_CHROME
));
559 policy_service_
->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
));
561 // Same if |provider1_| doesn't have POLICY_DOMAIN_EXTENSIONS initialized.
562 EXPECT_CALL(observer
, OnPolicyServiceInitialized(_
)).Times(0);
563 Mock::VerifyAndClearExpectations(&provider2_
);
564 EXPECT_CALL(provider2_
, IsInitializationComplete(POLICY_DOMAIN_CHROME
))
565 .WillRepeatedly(Return(false));
566 EXPECT_CALL(provider2_
, IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
))
567 .WillRepeatedly(Return(true));
568 provider2_
.UpdateChromePolicy(kPolicyMap
);
569 Mock::VerifyAndClearExpectations(&observer
);
570 EXPECT_FALSE(policy_service_
->IsInitializationComplete(POLICY_DOMAIN_CHROME
));
572 policy_service_
->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
));
574 // Now initialize POLICY_DOMAIN_CHROME on all the providers.
575 EXPECT_CALL(observer
, OnPolicyServiceInitialized(POLICY_DOMAIN_CHROME
));
576 Mock::VerifyAndClearExpectations(&provider2_
);
577 EXPECT_CALL(provider2_
, IsInitializationComplete(POLICY_DOMAIN_CHROME
))
578 .WillRepeatedly(Return(true));
579 EXPECT_CALL(provider2_
, IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
))
580 .WillRepeatedly(Return(true));
581 provider2_
.UpdateChromePolicy(kPolicyMap
);
582 Mock::VerifyAndClearExpectations(&observer
);
583 EXPECT_TRUE(policy_service_
->IsInitializationComplete(POLICY_DOMAIN_CHROME
));
584 // Other domains are still not initialized.
586 policy_service_
->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
));
588 // Initialize the remaining domain.
589 EXPECT_CALL(observer
, OnPolicyServiceInitialized(POLICY_DOMAIN_EXTENSIONS
));
590 Mock::VerifyAndClearExpectations(&provider1_
);
591 EXPECT_CALL(provider1_
, IsInitializationComplete(POLICY_DOMAIN_CHROME
))
592 .WillRepeatedly(Return(true));
593 EXPECT_CALL(provider1_
, IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
))
594 .WillRepeatedly(Return(true));
595 provider1_
.UpdateChromePolicy(kPolicyMap
);
596 Mock::VerifyAndClearExpectations(&observer
);
597 EXPECT_TRUE(policy_service_
->IsInitializationComplete(POLICY_DOMAIN_CHROME
));
599 policy_service_
->IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS
));
602 policy_service_
->RemoveObserver(POLICY_DOMAIN_CHROME
, &observer
);
603 policy_service_
->RemoveObserver(POLICY_DOMAIN_EXTENSIONS
, &observer
);
606 TEST_F(PolicyServiceTest
, FixDeprecatedPolicies
) {
607 const PolicyNamespace
chrome_namespace(POLICY_DOMAIN_CHROME
, std::string());
608 const PolicyNamespace
extension_namespace(POLICY_DOMAIN_EXTENSIONS
, "xyz");
610 scoped_ptr
<PolicyBundle
> policy_bundle(new PolicyBundle());
611 PolicyMap
& policy_map
= policy_bundle
->Get(chrome_namespace
);
612 // Individual proxy policy values in the Chrome namespace should be collected
613 // into a dictionary.
614 policy_map
.Set(key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
,
615 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(3), NULL
);
617 // Both these policies should be ignored, since there's a higher priority
619 policy_map
.Set(key::kProxyMode
, POLICY_LEVEL_RECOMMENDED
, POLICY_SCOPE_USER
,
620 base::Value::CreateStringValue("pac_script"), NULL
);
621 policy_map
.Set(key::kProxyPacUrl
, POLICY_LEVEL_RECOMMENDED
, POLICY_SCOPE_USER
,
622 base::Value::CreateStringValue("http://example.com/wpad.dat"),
625 // Add a value to a non-Chrome namespace.
626 policy_bundle
->Get(extension_namespace
)
627 .Set(key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
, POLICY_SCOPE_USER
,
628 base::Value::CreateIntegerValue(3), NULL
);
630 // The resulting Chrome namespace map should have the collected policy.
631 PolicyMap expected_chrome
;
632 scoped_ptr
<base::DictionaryValue
> expected_value(new base::DictionaryValue
);
633 expected_value
->SetInteger(key::kProxyServerMode
, 3);
634 expected_chrome
.Set(key::kProxySettings
, POLICY_LEVEL_MANDATORY
,
635 POLICY_SCOPE_USER
, expected_value
.release(), NULL
);
637 // The resulting Extensions namespace map shouldn't have been modified.
638 PolicyMap expected_extension
;
639 expected_extension
.Set(key::kProxyServerMode
, POLICY_LEVEL_MANDATORY
,
640 POLICY_SCOPE_USER
, base::Value::CreateIntegerValue(3),
643 provider0_
.UpdatePolicy(policy_bundle
.Pass());
646 EXPECT_TRUE(VerifyPolicies(chrome_namespace
, expected_chrome
));
647 EXPECT_TRUE(VerifyPolicies(extension_namespace
, expected_extension
));
650 } // namespace policy