OLD | NEW |
| (Empty) |
1 // Copyright 2013 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. | |
4 | |
5 #include "chrome/browser/policy/forwarding_policy_provider.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/values.h" | |
9 #include "components/policy/core/common/mock_configuration_policy_provider.h" | |
10 #include "components/policy/core/common/policy_bundle.h" | |
11 #include "components/policy/core/common/policy_map.h" | |
12 #include "components/policy/core/common/policy_types.h" | |
13 #include "components/policy/core/common/schema.h" | |
14 #include "components/policy/core/common/schema_registry.h" | |
15 #include "testing/gmock/include/gmock/gmock.h" | |
16 #include "testing/gtest/include/gtest/gtest.h" | |
17 | |
18 using testing::_; | |
19 using testing::Mock; | |
20 using testing::Return; | |
21 | |
22 namespace policy { | |
23 | |
24 class ForwardingPolicyProviderTest : public testing::Test { | |
25 protected: | |
26 ForwardingPolicyProviderTest() : forwarding_provider_(&mock_provider_) { | |
27 mock_provider_.Init(); | |
28 forwarding_provider_.Init(&schema_registry_); | |
29 forwarding_provider_.AddObserver(&observer_); | |
30 } | |
31 | |
32 virtual ~ForwardingPolicyProviderTest() { | |
33 forwarding_provider_.RemoveObserver(&observer_); | |
34 forwarding_provider_.Shutdown(); | |
35 mock_provider_.Shutdown(); | |
36 } | |
37 | |
38 SchemaRegistry schema_registry_; | |
39 MockConfigurationPolicyObserver observer_; | |
40 MockConfigurationPolicyProvider mock_provider_; | |
41 ForwardingPolicyProvider forwarding_provider_; | |
42 }; | |
43 | |
44 TEST_F(ForwardingPolicyProviderTest, Empty) { | |
45 EXPECT_FALSE(schema_registry_.IsReady()); | |
46 EXPECT_FALSE( | |
47 forwarding_provider_.IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS)); | |
48 | |
49 EXPECT_CALL(mock_provider_, IsInitializationComplete(POLICY_DOMAIN_CHROME)) | |
50 .WillOnce(Return(false)); | |
51 EXPECT_FALSE( | |
52 forwarding_provider_.IsInitializationComplete(POLICY_DOMAIN_CHROME)); | |
53 Mock::VerifyAndClearExpectations(&mock_provider_); | |
54 | |
55 const PolicyBundle empty_bundle; | |
56 EXPECT_TRUE(forwarding_provider_.policies().Equals(empty_bundle)); | |
57 } | |
58 | |
59 TEST_F(ForwardingPolicyProviderTest, ForwardsChromePolicy) { | |
60 PolicyBundle bundle; | |
61 const PolicyNamespace chrome_ns(POLICY_DOMAIN_CHROME, ""); | |
62 bundle.Get(chrome_ns).Set("policy", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
63 base::Value::CreateStringValue("value"), NULL); | |
64 | |
65 EXPECT_CALL(observer_, OnUpdatePolicy(&forwarding_provider_)); | |
66 scoped_ptr<PolicyBundle> delegate_bundle(new PolicyBundle); | |
67 delegate_bundle->CopyFrom(bundle); | |
68 delegate_bundle->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "xyz")) | |
69 .Set("component policy", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
70 base::Value::CreateStringValue("not forwarded"), NULL); | |
71 mock_provider_.UpdatePolicy(delegate_bundle.Pass()); | |
72 Mock::VerifyAndClearExpectations(&observer_); | |
73 | |
74 EXPECT_FALSE( | |
75 forwarding_provider_.IsInitializationComplete(POLICY_DOMAIN_EXTENSIONS)); | |
76 EXPECT_TRUE(forwarding_provider_.policies().Equals(bundle)); | |
77 } | |
78 | |
79 TEST_F(ForwardingPolicyProviderTest, RefreshPolicies) { | |
80 EXPECT_CALL(mock_provider_, RefreshPolicies()); | |
81 forwarding_provider_.RefreshPolicies(); | |
82 Mock::VerifyAndClearExpectations(&mock_provider_); | |
83 } | |
84 | |
85 TEST_F(ForwardingPolicyProviderTest, SchemaReady) { | |
86 EXPECT_CALL(observer_, OnUpdatePolicy(&forwarding_provider_)); | |
87 schema_registry_.SetReady(POLICY_DOMAIN_CHROME); | |
88 schema_registry_.SetReady(POLICY_DOMAIN_EXTENSIONS); | |
89 Mock::VerifyAndClearExpectations(&observer_); | |
90 | |
91 EXPECT_TRUE(forwarding_provider_.IsInitializationComplete( | |
92 policy::POLICY_DOMAIN_EXTENSIONS)); | |
93 } | |
94 | |
95 TEST_F(ForwardingPolicyProviderTest, SchemaReadyWithComponents) { | |
96 PolicyMap policy_map; | |
97 policy_map.Set("foo", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
98 base::Value::CreateStringValue("omg"), NULL); | |
99 scoped_ptr<PolicyBundle> bundle(new PolicyBundle); | |
100 bundle->Get(PolicyNamespace(POLICY_DOMAIN_CHROME, "")).CopyFrom(policy_map); | |
101 bundle->Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "xyz")) | |
102 .CopyFrom(policy_map); | |
103 EXPECT_CALL(observer_, OnUpdatePolicy(&forwarding_provider_)); | |
104 mock_provider_.UpdatePolicy(bundle.Pass()); | |
105 Mock::VerifyAndClearExpectations(&observer_); | |
106 | |
107 EXPECT_CALL(mock_provider_, RefreshPolicies()).Times(0); | |
108 schema_registry_.RegisterComponent( | |
109 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "xyz"), Schema()); | |
110 schema_registry_.SetReady(POLICY_DOMAIN_EXTENSIONS); | |
111 Mock::VerifyAndClearExpectations(&mock_provider_); | |
112 | |
113 EXPECT_CALL(mock_provider_, RefreshPolicies()); | |
114 schema_registry_.SetReady(POLICY_DOMAIN_CHROME); | |
115 Mock::VerifyAndClearExpectations(&mock_provider_); | |
116 | |
117 EXPECT_FALSE(forwarding_provider_.IsInitializationComplete( | |
118 policy::POLICY_DOMAIN_EXTENSIONS)); | |
119 PolicyBundle expected_bundle; | |
120 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_CHROME, "")) | |
121 .CopyFrom(policy_map); | |
122 EXPECT_TRUE(forwarding_provider_.policies().Equals(expected_bundle)); | |
123 | |
124 EXPECT_CALL(observer_, OnUpdatePolicy(&forwarding_provider_)); | |
125 forwarding_provider_.OnUpdatePolicy(&mock_provider_); | |
126 Mock::VerifyAndClearExpectations(&observer_); | |
127 | |
128 EXPECT_TRUE(forwarding_provider_.IsInitializationComplete( | |
129 policy::POLICY_DOMAIN_EXTENSIONS)); | |
130 expected_bundle.Get(PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "xyz")) | |
131 .CopyFrom(policy_map); | |
132 EXPECT_TRUE(forwarding_provider_.policies().Equals(expected_bundle)); | |
133 } | |
134 | |
135 TEST_F(ForwardingPolicyProviderTest, DelegateUpdates) { | |
136 schema_registry_.RegisterComponent( | |
137 PolicyNamespace(POLICY_DOMAIN_EXTENSIONS, "xyz"), Schema()); | |
138 EXPECT_FALSE(schema_registry_.IsReady()); | |
139 EXPECT_FALSE(forwarding_provider_.IsInitializationComplete( | |
140 policy::POLICY_DOMAIN_EXTENSIONS)); | |
141 | |
142 PolicyMap policy_map; | |
143 policy_map.Set("foo", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
144 base::Value::CreateStringValue("omg"), NULL); | |
145 // Chrome policy updates are forwarded even if the components aren't ready. | |
146 EXPECT_CALL(observer_, OnUpdatePolicy(&forwarding_provider_)); | |
147 mock_provider_.UpdateChromePolicy(policy_map); | |
148 Mock::VerifyAndClearExpectations(&observer_); | |
149 | |
150 EXPECT_CALL(mock_provider_, RefreshPolicies()); | |
151 schema_registry_.SetReady(POLICY_DOMAIN_CHROME); | |
152 schema_registry_.SetReady(POLICY_DOMAIN_EXTENSIONS); | |
153 EXPECT_TRUE(schema_registry_.IsReady()); | |
154 Mock::VerifyAndClearExpectations(&mock_provider_); | |
155 EXPECT_FALSE(forwarding_provider_.IsInitializationComplete( | |
156 policy::POLICY_DOMAIN_EXTENSIONS)); | |
157 | |
158 // The forwarding provider becomes ready after this refresh completes, and | |
159 // starts forwarding policy updates after that. | |
160 EXPECT_CALL(observer_, OnUpdatePolicy(_)); | |
161 mock_provider_.UpdateChromePolicy(policy_map); | |
162 Mock::VerifyAndClearExpectations(&observer_); | |
163 | |
164 EXPECT_TRUE(forwarding_provider_.IsInitializationComplete( | |
165 policy::POLICY_DOMAIN_EXTENSIONS)); | |
166 | |
167 // Keeps forwarding. | |
168 EXPECT_CALL(observer_, OnUpdatePolicy(_)); | |
169 mock_provider_.UpdateChromePolicy(policy_map); | |
170 Mock::VerifyAndClearExpectations(&observer_); | |
171 } | |
172 | |
173 TEST_F(ForwardingPolicyProviderTest, RemoveAndAddComponent) { | |
174 EXPECT_CALL(mock_provider_, RefreshPolicies()); | |
175 const PolicyNamespace ns(POLICY_DOMAIN_EXTENSIONS, "xyz"); | |
176 schema_registry_.SetReady(POLICY_DOMAIN_CHROME); | |
177 schema_registry_.RegisterComponent(ns, Schema()); | |
178 schema_registry_.SetReady(POLICY_DOMAIN_EXTENSIONS); | |
179 Mock::VerifyAndClearExpectations(&mock_provider_); | |
180 | |
181 // Serve policy for |ns|. | |
182 PolicyBundle platform_policy; | |
183 platform_policy.Get(ns).Set("foo", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, | |
184 base::Value::CreateStringValue("omg"), NULL); | |
185 scoped_ptr<PolicyBundle> copy(new PolicyBundle); | |
186 copy->CopyFrom(platform_policy); | |
187 EXPECT_CALL(observer_, OnUpdatePolicy(_)); | |
188 mock_provider_.UpdatePolicy(copy.Pass()); | |
189 Mock::VerifyAndClearExpectations(&observer_); | |
190 EXPECT_TRUE(forwarding_provider_.policies().Equals(platform_policy)); | |
191 | |
192 // Now remove that component. | |
193 EXPECT_CALL(observer_, OnUpdatePolicy(_)); | |
194 schema_registry_.UnregisterComponent(ns); | |
195 Mock::VerifyAndClearExpectations(&observer_); | |
196 const PolicyBundle empty; | |
197 EXPECT_TRUE(forwarding_provider_.policies().Equals(empty)); | |
198 | |
199 // Adding it back should serve the current policies again, even though they | |
200 // haven't changed on the platform provider. | |
201 EXPECT_CALL(mock_provider_, RefreshPolicies()); | |
202 schema_registry_.RegisterComponent(ns, Schema()); | |
203 Mock::VerifyAndClearExpectations(&mock_provider_); | |
204 | |
205 EXPECT_CALL(observer_, OnUpdatePolicy(_)); | |
206 copy.reset(new PolicyBundle); | |
207 copy->CopyFrom(platform_policy); | |
208 mock_provider_.UpdatePolicy(copy.Pass()); | |
209 Mock::VerifyAndClearExpectations(&observer_); | |
210 EXPECT_TRUE(forwarding_provider_.policies().Equals(platform_policy)); | |
211 } | |
212 | |
213 } // namespace policy | |
OLD | NEW |