Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(825)

Side by Side Diff: chrome/browser/policy/user_cloud_policy_manager_unittest.cc

Issue 10919131: Break out base functionality of UserCloudPolicyManager. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase, address comments, fix compilation. Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/policy/user_cloud_policy_manager.h"
6
5 #include "base/basictypes.h" 7 #include "base/basictypes.h"
6 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop.h" 9 #include "base/message_loop.h"
8 #include "chrome/browser/policy/cloud_policy_service.h"
9 #include "chrome/browser/policy/configuration_policy_provider_test.h"
10 #include "chrome/browser/policy/mock_cloud_policy_store.h" 10 #include "chrome/browser/policy/mock_cloud_policy_store.h"
11 #include "chrome/browser/policy/mock_configuration_policy_provider.h" 11 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
12 #include "chrome/browser/policy/mock_device_management_service.h" 12 #include "chrome/browser/policy/mock_device_management_service.h"
13 #include "chrome/browser/policy/proto/device_management_backend.pb.h" 13 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
14 #include "chrome/browser/policy/user_cloud_policy_manager.h"
15 #include "chrome/browser/prefs/browser_prefs.h" 14 #include "chrome/browser/prefs/browser_prefs.h"
16 #include "chrome/test/base/testing_pref_service.h" 15 #include "chrome/test/base/testing_pref_service.h"
17 #include "policy/policy_constants.h" 16 #include "policy/policy_constants.h"
18 #include "testing/gmock/include/gmock/gmock.h" 17 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
20 19
21 namespace em = enterprise_management; 20 namespace em = enterprise_management;
22 21
23 using testing::AnyNumber; 22 using testing::AnyNumber;
24 using testing::AtLeast; 23 using testing::AtLeast;
25 using testing::Mock; 24 using testing::Mock;
26 using testing::_; 25 using testing::_;
27 26
28 namespace policy { 27 namespace policy {
29 namespace { 28 namespace {
30 29
31 class TestHarness : public PolicyProviderTestHarness {
32 public:
33 explicit TestHarness(PolicyLevel level);
34 virtual ~TestHarness();
35
36 virtual void SetUp() OVERRIDE;
37
38 virtual ConfigurationPolicyProvider* CreateProvider(
39 const PolicyDefinitionList* policy_definition_list) OVERRIDE;
40
41 virtual void InstallEmptyPolicy() OVERRIDE;
42 virtual void InstallStringPolicy(const std::string& policy_name,
43 const std::string& policy_value) OVERRIDE;
44 virtual void InstallIntegerPolicy(const std::string& policy_name,
45 int policy_value) OVERRIDE;
46 virtual void InstallBooleanPolicy(const std::string& policy_name,
47 bool policy_value) OVERRIDE;
48 virtual void InstallStringListPolicy(
49 const std::string& policy_name,
50 const base::ListValue* policy_value) OVERRIDE;
51 virtual void InstallDictionaryPolicy(
52 const std::string& policy_name,
53 const base::DictionaryValue* policy_value) OVERRIDE;
54
55 // Creates harnesses for mandatory and recommended levels, respectively.
56 static PolicyProviderTestHarness* CreateMandatory();
57 static PolicyProviderTestHarness* CreateRecommended();
58
59 private:
60 MockCloudPolicyStore* store_;
61
62 DISALLOW_COPY_AND_ASSIGN(TestHarness);
63 };
64
65 TestHarness::TestHarness(PolicyLevel level)
66 : PolicyProviderTestHarness(level, POLICY_SCOPE_USER) {}
67
68 TestHarness::~TestHarness() {}
69
70 void TestHarness::SetUp() {}
71
72 ConfigurationPolicyProvider* TestHarness::CreateProvider(
73 const PolicyDefinitionList* policy_definition_list) {
74 // Create and initialize the store.
75 store_ = new MockCloudPolicyStore();
76 store_->NotifyStoreLoaded();
77 EXPECT_CALL(*store_, Load());
78 return new UserCloudPolicyManager(scoped_ptr<CloudPolicyStore>(store_).Pass(),
79 false);
80 }
81
82 void TestHarness::InstallEmptyPolicy() {}
83
84 void TestHarness::InstallStringPolicy(const std::string& policy_name,
85 const std::string& policy_value) {
86 store_->policy_map_.Set(policy_name, policy_level(), policy_scope(),
87 base::Value::CreateStringValue(policy_value));
88 }
89
90 void TestHarness::InstallIntegerPolicy(const std::string& policy_name,
91 int policy_value) {
92 store_->policy_map_.Set(policy_name, policy_level(), policy_scope(),
93 base::Value::CreateIntegerValue(policy_value));
94 }
95
96 void TestHarness::InstallBooleanPolicy(const std::string& policy_name,
97 bool policy_value) {
98 store_->policy_map_.Set(policy_name, policy_level(), policy_scope(),
99 base::Value::CreateBooleanValue(policy_value));
100 }
101
102 void TestHarness::InstallStringListPolicy(const std::string& policy_name,
103 const base::ListValue* policy_value) {
104 store_->policy_map_.Set(policy_name, policy_level(), policy_scope(),
105 policy_value->DeepCopy());
106 }
107
108 void TestHarness::InstallDictionaryPolicy(
109 const std::string& policy_name,
110 const base::DictionaryValue* policy_value) {
111 store_->policy_map_.Set(policy_name, policy_level(), policy_scope(),
112 policy_value->DeepCopy());
113 }
114
115 // static
116 PolicyProviderTestHarness* TestHarness::CreateMandatory() {
117 return new TestHarness(POLICY_LEVEL_MANDATORY);
118 }
119
120 // static
121 PolicyProviderTestHarness* TestHarness::CreateRecommended() {
122 return new TestHarness(POLICY_LEVEL_RECOMMENDED);
123 }
124
125 // Instantiate abstract test case for basic policy reading tests.
126 INSTANTIATE_TEST_CASE_P(
127 UserCloudPolicyManagerProviderTest,
128 ConfigurationPolicyProviderTest,
129 testing::Values(TestHarness::CreateMandatory,
130 TestHarness::CreateRecommended));
131
132 class UserCloudPolicyManagerTest : public testing::Test { 30 class UserCloudPolicyManagerTest : public testing::Test {
133 protected: 31 protected:
134 UserCloudPolicyManagerTest() 32 UserCloudPolicyManagerTest()
135 : store_(NULL) {} 33 : store_(NULL) {}
136 34
137 virtual void SetUp() OVERRIDE { 35 virtual void SetUp() OVERRIDE {
138 chrome::RegisterLocalState(&prefs_); 36 chrome::RegisterLocalState(&prefs_);
139 37
140 // Set up a policy map for testing. 38 // Set up a policy map for testing.
141 policy_map_.Set("key", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 39 policy_map_.Set("key", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
142 base::Value::CreateStringValue("value")); 40 base::Value::CreateStringValue("value"));
143 expected_bundle_.Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom( 41 expected_bundle_.Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom(
144 policy_map_); 42 policy_map_);
145 43
146 // Create a fake policy blob to deliver to the client. 44 // Create a fake policy blob to deliver to the client.
147 em::PolicyData policy_data; 45 em::PolicyData policy_data;
148 em::PolicyFetchResponse* policy_response = 46 em::PolicyFetchResponse* policy_response =
149 policy_blob_.mutable_policy_response()->add_response(); 47 policy_blob_.mutable_policy_response()->add_response();
150 ASSERT_TRUE(policy_data.SerializeToString( 48 ASSERT_TRUE(policy_data.SerializeToString(
151 policy_response->mutable_policy_data())); 49 policy_response->mutable_policy_data()));
152 50
153 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)) 51 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _))
154 .Times(AnyNumber()); 52 .Times(AnyNumber());
155 } 53 }
156 54
157 void CreateManager(bool wait_for_policy_fetch) { 55 void CreateManager(bool wait_for_policy_fetch) {
158 store_ = new MockCloudPolicyStore(); 56 store_ = new MockCloudPolicyStore();
159 EXPECT_CALL(*store_, Load()); 57 EXPECT_CALL(*store_, Load());
160 manager_.reset( 58 manager_.reset(
161 new UserCloudPolicyManager(scoped_ptr<CloudPolicyStore>(store_).Pass(), 59 new UserCloudPolicyManager(scoped_ptr<CloudPolicyStore>(store_),
162 wait_for_policy_fetch)); 60 wait_for_policy_fetch));
163 registrar_.Init(manager_.get(), &observer_); 61 registrar_.Init(manager_.get(), &observer_);
62 Mock::VerifyAndClearExpectations(store_);
164 } 63 }
165 64
166 void CreateManagerWithPendingFetch() { 65 void CreateManagerWithPendingFetch() {
167 CreateManager(true); 66 CreateManager(true);
168 manager_->Initialize(&prefs_, &device_management_service_, 67 manager_->Initialize(&prefs_, &device_management_service_,
169 USER_AFFILIATION_NONE); 68 USER_AFFILIATION_NONE);
170 EXPECT_FALSE(manager_->IsInitializationComplete()); 69 EXPECT_FALSE(manager_->IsInitializationComplete());
171 70
172 // Finishing the Load() operation shouldn't set the initialized flag. 71 // Finishing the Load() operation shouldn't set the initialized flag.
173 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); 72 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0);
(...skipping 15 matching lines...) Expand all
189 MockConfigurationPolicyObserver observer_; 88 MockConfigurationPolicyObserver observer_;
190 MockDeviceManagementService device_management_service_; 89 MockDeviceManagementService device_management_service_;
191 MockCloudPolicyStore* store_; 90 MockCloudPolicyStore* store_;
192 scoped_ptr<UserCloudPolicyManager> manager_; 91 scoped_ptr<UserCloudPolicyManager> manager_;
193 ConfigurationPolicyObserverRegistrar registrar_; 92 ConfigurationPolicyObserverRegistrar registrar_;
194 93
195 private: 94 private:
196 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest); 95 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest);
197 }; 96 };
198 97
199 TEST_F(UserCloudPolicyManagerTest, Init) {
200 CreateManager(false);
201 PolicyBundle empty_bundle;
202 EXPECT_TRUE(empty_bundle.Equals(manager_->policies()));
203 EXPECT_FALSE(manager_->IsInitializationComplete());
204
205 store_->policy_map_.CopyFrom(policy_map_);
206 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
207 store_->NotifyStoreLoaded();
208 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies()));
209 EXPECT_TRUE(manager_->IsInitializationComplete());
210 }
211
212 TEST_F(UserCloudPolicyManagerTest, ShutdownAndRemovePolicy) { 98 TEST_F(UserCloudPolicyManagerTest, ShutdownAndRemovePolicy) {
213 // Load policy, make sure it goes away when ShutdownAndRemove() is called. 99 // Load policy, make sure it goes away when ShutdownAndRemove() is called.
214 CreateManager(false); 100 CreateManager(false);
215 store_->policy_map_.CopyFrom(policy_map_); 101 store_->policy_map_.CopyFrom(policy_map_);
216 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); 102 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
217 store_->NotifyStoreLoaded(); 103 store_->NotifyStoreLoaded();
218 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); 104 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies()));
219 EXPECT_TRUE(manager_->IsInitializationComplete()); 105 EXPECT_TRUE(manager_->IsInitializationComplete());
220 EXPECT_CALL(*store_, Clear()); 106 EXPECT_CALL(*store_, Clear());
221 manager_->ShutdownAndRemovePolicy(); 107 manager_->ShutdownAndRemovePolicy();
222 EXPECT_FALSE(manager_->cloud_policy_service()); 108 EXPECT_FALSE(manager_->cloud_policy_service());
223 } 109 }
224 110
225 TEST_F(UserCloudPolicyManagerTest, Update) {
226 CreateManager(false);
227 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
228 store_->NotifyStoreLoaded();
229 EXPECT_TRUE(manager_->IsInitializationComplete());
230 PolicyBundle empty_bundle;
231 EXPECT_TRUE(empty_bundle.Equals(manager_->policies()));
232
233 store_->policy_map_.CopyFrom(policy_map_);
234 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
235 store_->NotifyStoreLoaded();
236 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies()));
237 EXPECT_TRUE(manager_->IsInitializationComplete());
238 }
239
240 TEST_F(UserCloudPolicyManagerTest, RefreshNotRegistered) {
241 CreateManager(false);
242 manager_->Initialize(&prefs_, &device_management_service_,
243 USER_AFFILIATION_NONE);
244
245 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
246 store_->NotifyStoreLoaded();
247 Mock::VerifyAndClearExpectations(&observer_);
248
249 // A refresh on a non-registered store should not block.
250 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
251 manager_->RefreshPolicies();
252 Mock::VerifyAndClearExpectations(&observer_);
253 }
254
255 TEST_F(UserCloudPolicyManagerTest, RefreshSuccessful) {
256 CreateManager(false);
257 manager_->Initialize(&prefs_, &device_management_service_,
258 USER_AFFILIATION_NONE);
259 manager_->cloud_policy_service()->client()->SetupRegistration("dm_token",
260 "client_id");
261
262 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
263 store_->NotifyStoreLoaded();
264 Mock::VerifyAndClearExpectations(&observer_);
265
266 // Start a refresh.
267 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0);
268 MockDeviceManagementJob* request_job;
269 EXPECT_CALL(device_management_service_,
270 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH))
271 .WillOnce(device_management_service_.CreateAsyncJob(&request_job));
272 manager_->RefreshPolicies();
273 ASSERT_TRUE(request_job);
274 store_->policy_map_.CopyFrom(policy_map_);
275 Mock::VerifyAndClearExpectations(&observer_);
276
277 // A stray reload should be suppressed until the refresh completes.
278 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0);
279 store_->NotifyStoreLoaded();
280 Mock::VerifyAndClearExpectations(&observer_);
281
282 // Respond to the policy fetch, which should trigger a write to |store_|.
283 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0);
284 EXPECT_CALL(*store_, Store(_));
285 request_job->SendResponse(DM_STATUS_SUCCESS, policy_blob_);
286 Mock::VerifyAndClearExpectations(&observer_);
287
288 // The load notification from |store_| should trigger the policy update.
289 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
290 store_->NotifyStoreLoaded();
291 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies()));
292 Mock::VerifyAndClearExpectations(&observer_);
293 }
294
295 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetch) { 111 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetch) {
296 CreateManagerWithPendingFetch(); 112 CreateManagerWithPendingFetch();
297 113
298 // Setting the token should trigger the policy fetch. 114 // Setting the token should trigger the policy fetch.
299 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); 115 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0);
300 MockDeviceManagementJob* fetch_request = NULL; 116 MockDeviceManagementJob* fetch_request = NULL;
301 EXPECT_CALL(device_management_service_, 117 EXPECT_CALL(device_management_service_,
302 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) 118 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH))
303 .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request)); 119 .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request));
304 manager_->cloud_policy_service()->client()->SetupRegistration("dm_token", 120 manager_->cloud_policy_client()->SetupRegistration("dm_token", "client_id");
305 "client_id");
306 ASSERT_TRUE(fetch_request); 121 ASSERT_TRUE(fetch_request);
307 EXPECT_FALSE(manager_->IsInitializationComplete()); 122 EXPECT_FALSE(manager_->IsInitializationComplete());
308 Mock::VerifyAndClearExpectations(&observer_); 123 Mock::VerifyAndClearExpectations(&observer_);
309 124
310 // Respond to the policy fetch, which should trigger a write to |store_|. 125 // Respond to the policy fetch, which should trigger a write to |store_|.
311 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); 126 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0);
312 EXPECT_CALL(*store_, Store(_)); 127 EXPECT_CALL(*store_, Store(_));
313 fetch_request->SendResponse(DM_STATUS_SUCCESS, policy_blob_); 128 fetch_request->SendResponse(DM_STATUS_SUCCESS, policy_blob_);
314 EXPECT_FALSE(manager_->IsInitializationComplete()); 129 EXPECT_FALSE(manager_->IsInitializationComplete());
315 Mock::VerifyAndClearExpectations(&observer_); 130 Mock::VerifyAndClearExpectations(&observer_);
316 131
317 // The load notification from |store_| should trigger the policy update and 132 // The load notification from |store_| should trigger the policy update and
318 // flip the initialized bit. 133 // flip the initialized bit.
319 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); 134 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
320 store_->NotifyStoreLoaded(); 135 store_->NotifyStoreLoaded();
321 EXPECT_TRUE(manager_->IsInitializationComplete()); 136 EXPECT_TRUE(manager_->IsInitializationComplete());
322 Mock::VerifyAndClearExpectations(&observer_); 137 Mock::VerifyAndClearExpectations(&observer_);
323 } 138 }
324 139
325 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetchError) { 140 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetchError) {
326 CreateManagerWithPendingFetch(); 141 CreateManagerWithPendingFetch();
327 142
328 // Setting the token should trigger the policy fetch. 143 // Setting the token should trigger the policy fetch.
329 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); 144 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0);
330 MockDeviceManagementJob* fetch_request = NULL; 145 MockDeviceManagementJob* fetch_request = NULL;
331 EXPECT_CALL(device_management_service_, 146 EXPECT_CALL(device_management_service_,
332 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) 147 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH))
333 .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request)); 148 .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request));
334 manager_->cloud_policy_service()->client()->SetupRegistration("dm_token", 149 manager_->cloud_policy_client()->SetupRegistration("dm_token", "client_id");
335 "client_id");
336 ASSERT_TRUE(fetch_request); 150 ASSERT_TRUE(fetch_request);
337 EXPECT_FALSE(manager_->IsInitializationComplete()); 151 EXPECT_FALSE(manager_->IsInitializationComplete());
338 Mock::VerifyAndClearExpectations(&observer_); 152 Mock::VerifyAndClearExpectations(&observer_);
339 153
340 // Make the policy fetch fail, at which point the manager should bail out. 154 // Make the policy fetch fail, at which point the manager should bail out.
341 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())).Times(AtLeast(1)); 155 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())).Times(AtLeast(1));
342 fetch_request->SendResponse(DM_STATUS_REQUEST_FAILED, policy_blob_); 156 fetch_request->SendResponse(DM_STATUS_REQUEST_FAILED, policy_blob_);
343 EXPECT_TRUE(manager_->IsInitializationComplete()); 157 EXPECT_TRUE(manager_->IsInitializationComplete());
344 Mock::VerifyAndClearExpectations(&observer_); 158 Mock::VerifyAndClearExpectations(&observer_);
345 } 159 }
346 160
347 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetchCancel) { 161 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetchCancel) {
348 CreateManagerWithPendingFetch(); 162 CreateManagerWithPendingFetch();
349 163
350 // Cancelling the initial fetch should flip the flag. 164 // Cancelling the initial fetch should flip the flag.
351 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); 165 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
352 manager_->CancelWaitForPolicyFetch(); 166 manager_->CancelWaitForPolicyFetch();
353 EXPECT_TRUE(manager_->IsInitializationComplete()); 167 EXPECT_TRUE(manager_->IsInitializationComplete());
354 Mock::VerifyAndClearExpectations(&observer_); 168 Mock::VerifyAndClearExpectations(&observer_);
355 } 169 }
356 170
357 } // namespace 171 } // namespace
358 } // namespace policy 172 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698