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

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: 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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/memory/scoped_ptr.h" 6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop.h" 7 #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" 8 #include "chrome/browser/policy/mock_cloud_policy_store.h"
11 #include "chrome/browser/policy/mock_configuration_policy_provider.h" 9 #include "chrome/browser/policy/mock_configuration_policy_provider.h"
12 #include "chrome/browser/policy/mock_device_management_service.h" 10 #include "chrome/browser/policy/mock_device_management_service.h"
13 #include "chrome/browser/policy/proto/device_management_backend.pb.h" 11 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
14 #include "chrome/browser/policy/user_cloud_policy_manager.h" 12 #include "chrome/browser/policy/user_cloud_policy_manager.h"
Joao da Silva 2012/09/07 12:44:13 nit: first include
Mattias Nissler (ping if slow) 2012/09/07 13:12:59 Done.
15 #include "chrome/browser/prefs/browser_prefs.h" 13 #include "chrome/browser/prefs/browser_prefs.h"
16 #include "chrome/test/base/testing_pref_service.h" 14 #include "chrome/test/base/testing_pref_service.h"
17 #include "policy/policy_constants.h" 15 #include "policy/policy_constants.h"
18 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
20 18
21 namespace em = enterprise_management; 19 namespace em = enterprise_management;
22 20
23 using testing::AnyNumber; 21 using testing::AnyNumber;
24 using testing::AtLeast; 22 using testing::AtLeast;
25 using testing::Mock; 23 using testing::Mock;
26 using testing::_; 24 using testing::_;
27 25
28 namespace policy { 26 namespace policy {
29 namespace { 27 namespace {
30 28
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 { 29 class UserCloudPolicyManagerTest : public testing::Test {
133 protected: 30 protected:
134 UserCloudPolicyManagerTest() 31 UserCloudPolicyManagerTest()
135 : store_(NULL) {} 32 : store_(NULL) {}
136 33
137 virtual void SetUp() OVERRIDE { 34 virtual void SetUp() OVERRIDE {
138 chrome::RegisterLocalState(&prefs_); 35 chrome::RegisterLocalState(&prefs_);
139 36
140 // Set up a policy map for testing. 37 // Set up a policy map for testing.
141 policy_map_.Set("key", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER, 38 policy_map_.Set("key", POLICY_LEVEL_MANDATORY, POLICY_SCOPE_USER,
142 base::Value::CreateStringValue("value")); 39 base::Value::CreateStringValue("value"));
143 expected_bundle_.Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom( 40 expected_bundle_.Get(POLICY_DOMAIN_CHROME, std::string()).CopyFrom(
144 policy_map_); 41 policy_map_);
145 42
146 // Create a fake policy blob to deliver to the client. 43 // Create a fake policy blob to deliver to the client.
147 em::PolicyData policy_data; 44 em::PolicyData policy_data;
148 em::PolicyFetchResponse* policy_response = 45 em::PolicyFetchResponse* policy_response =
149 policy_blob_.mutable_policy_response()->add_response(); 46 policy_blob_.mutable_policy_response()->add_response();
150 ASSERT_TRUE(policy_data.SerializeToString( 47 ASSERT_TRUE(policy_data.SerializeToString(
151 policy_response->mutable_policy_data())); 48 policy_response->mutable_policy_data()));
152 49
153 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _)) 50 EXPECT_CALL(device_management_service_, StartJob(_, _, _, _, _, _, _))
154 .Times(AnyNumber()); 51 .Times(AnyNumber());
155 } 52 }
156 53
157 void CreateManager(bool wait_for_policy_fetch) { 54 void CreateManager(bool wait_for_policy_fetch) {
158 store_ = new MockCloudPolicyStore(); 55 store_ = new MockCloudPolicyStore();
159 EXPECT_CALL(*store_, Load()); 56 EXPECT_CALL(*store_, Load());
160 manager_.reset( 57 manager_.reset(
161 new UserCloudPolicyManager(scoped_ptr<CloudPolicyStore>(store_).Pass(), 58 new UserCloudPolicyManager(scoped_ptr<CloudPolicyStore>(store_),
162 wait_for_policy_fetch)); 59 wait_for_policy_fetch));
163 registrar_.Init(manager_.get(), &observer_); 60 registrar_.Init(manager_.get(), &observer_);
61 Mock::VerifyAndClearExpectations(store_);
164 } 62 }
165 63
166 void CreateManagerWithPendingFetch() { 64 void CreateManagerWithPendingFetch() {
167 CreateManager(true); 65 CreateManager(true);
168 manager_->Initialize(&prefs_, &device_management_service_, 66 manager_->Initialize(&prefs_, &device_management_service_,
169 USER_AFFILIATION_NONE); 67 USER_AFFILIATION_NONE);
170 EXPECT_FALSE(manager_->IsInitializationComplete()); 68 EXPECT_FALSE(manager_->IsInitializationComplete());
171 69
172 // Finishing the Load() operation shouldn't set the initialized flag. 70 // Finishing the Load() operation shouldn't set the initialized flag.
173 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); 71 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0);
(...skipping 15 matching lines...) Expand all
189 MockConfigurationPolicyObserver observer_; 87 MockConfigurationPolicyObserver observer_;
190 MockDeviceManagementService device_management_service_; 88 MockDeviceManagementService device_management_service_;
191 MockCloudPolicyStore* store_; 89 MockCloudPolicyStore* store_;
192 scoped_ptr<UserCloudPolicyManager> manager_; 90 scoped_ptr<UserCloudPolicyManager> manager_;
193 ConfigurationPolicyObserverRegistrar registrar_; 91 ConfigurationPolicyObserverRegistrar registrar_;
194 92
195 private: 93 private:
196 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest); 94 DISALLOW_COPY_AND_ASSIGN(UserCloudPolicyManagerTest);
197 }; 95 };
198 96
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) { 97 TEST_F(UserCloudPolicyManagerTest, ShutdownAndRemovePolicy) {
213 // Load policy, make sure it goes away when ShutdownAndRemove() is called. 98 // Load policy, make sure it goes away when ShutdownAndRemove() is called.
214 CreateManager(false); 99 CreateManager(false);
215 store_->policy_map_.CopyFrom(policy_map_); 100 store_->policy_map_.CopyFrom(policy_map_);
216 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); 101 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
217 store_->NotifyStoreLoaded(); 102 store_->NotifyStoreLoaded();
218 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies())); 103 EXPECT_TRUE(expected_bundle_.Equals(manager_->policies()));
219 EXPECT_TRUE(manager_->IsInitializationComplete()); 104 EXPECT_TRUE(manager_->IsInitializationComplete());
220 EXPECT_CALL(*store_, Clear()); 105 EXPECT_CALL(*store_, Clear());
221 manager_->ShutdownAndRemovePolicy(); 106 manager_->ShutdownAndRemovePolicy();
222 EXPECT_FALSE(manager_->cloud_policy_service()); 107 EXPECT_FALSE(manager_->cloud_policy_service());
223 } 108 }
224 109
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) { 110 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetch) {
296 CreateManagerWithPendingFetch(); 111 CreateManagerWithPendingFetch();
297 112
298 // Setting the token should trigger the policy fetch. 113 // Setting the token should trigger the policy fetch.
299 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); 114 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0);
300 MockDeviceManagementJob* fetch_request = NULL; 115 MockDeviceManagementJob* fetch_request = NULL;
301 EXPECT_CALL(device_management_service_, 116 EXPECT_CALL(device_management_service_,
302 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) 117 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH))
303 .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request)); 118 .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request));
304 manager_->cloud_policy_service()->client()->SetupRegistration("dm_token", 119 manager_->cloud_policy_client()->SetupRegistration("dm_token", "client_id");
305 "client_id");
306 ASSERT_TRUE(fetch_request); 120 ASSERT_TRUE(fetch_request);
307 EXPECT_FALSE(manager_->IsInitializationComplete()); 121 EXPECT_FALSE(manager_->IsInitializationComplete());
308 Mock::VerifyAndClearExpectations(&observer_); 122 Mock::VerifyAndClearExpectations(&observer_);
309 123
310 // Respond to the policy fetch, which should trigger a write to |store_|. 124 // Respond to the policy fetch, which should trigger a write to |store_|.
311 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); 125 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0);
312 EXPECT_CALL(*store_, Store(_)); 126 EXPECT_CALL(*store_, Store(_));
313 fetch_request->SendResponse(DM_STATUS_SUCCESS, policy_blob_); 127 fetch_request->SendResponse(DM_STATUS_SUCCESS, policy_blob_);
314 EXPECT_FALSE(manager_->IsInitializationComplete()); 128 EXPECT_FALSE(manager_->IsInitializationComplete());
315 Mock::VerifyAndClearExpectations(&observer_); 129 Mock::VerifyAndClearExpectations(&observer_);
316 130
317 // The load notification from |store_| should trigger the policy update and 131 // The load notification from |store_| should trigger the policy update and
318 // flip the initialized bit. 132 // flip the initialized bit.
319 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); 133 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
320 store_->NotifyStoreLoaded(); 134 store_->NotifyStoreLoaded();
321 EXPECT_TRUE(manager_->IsInitializationComplete()); 135 EXPECT_TRUE(manager_->IsInitializationComplete());
322 Mock::VerifyAndClearExpectations(&observer_); 136 Mock::VerifyAndClearExpectations(&observer_);
323 } 137 }
324 138
325 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetchError) { 139 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetchError) {
326 CreateManagerWithPendingFetch(); 140 CreateManagerWithPendingFetch();
327 141
328 // Setting the token should trigger the policy fetch. 142 // Setting the token should trigger the policy fetch.
329 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0); 143 EXPECT_CALL(observer_, OnUpdatePolicy(_)).Times(0);
330 MockDeviceManagementJob* fetch_request = NULL; 144 MockDeviceManagementJob* fetch_request = NULL;
331 EXPECT_CALL(device_management_service_, 145 EXPECT_CALL(device_management_service_,
332 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH)) 146 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH))
333 .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request)); 147 .WillOnce(device_management_service_.CreateAsyncJob(&fetch_request));
334 manager_->cloud_policy_service()->client()->SetupRegistration("dm_token", 148 manager_->cloud_policy_client()->SetupRegistration("dm_token", "client_id");
335 "client_id");
336 ASSERT_TRUE(fetch_request); 149 ASSERT_TRUE(fetch_request);
337 EXPECT_FALSE(manager_->IsInitializationComplete()); 150 EXPECT_FALSE(manager_->IsInitializationComplete());
338 Mock::VerifyAndClearExpectations(&observer_); 151 Mock::VerifyAndClearExpectations(&observer_);
339 152
340 // Make the policy fetch fail, at which point the manager should bail out. 153 // Make the policy fetch fail, at which point the manager should bail out.
341 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())).Times(AtLeast(1)); 154 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())).Times(AtLeast(1));
342 fetch_request->SendResponse(DM_STATUS_REQUEST_FAILED, policy_blob_); 155 fetch_request->SendResponse(DM_STATUS_REQUEST_FAILED, policy_blob_);
343 EXPECT_TRUE(manager_->IsInitializationComplete()); 156 EXPECT_TRUE(manager_->IsInitializationComplete());
344 Mock::VerifyAndClearExpectations(&observer_); 157 Mock::VerifyAndClearExpectations(&observer_);
345 } 158 }
346 159
347 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetchCancel) { 160 TEST_F(UserCloudPolicyManagerTest, WaitForPolicyFetchCancel) {
348 CreateManagerWithPendingFetch(); 161 CreateManagerWithPendingFetch();
349 162
350 // Cancelling the initial fetch should flip the flag. 163 // Cancelling the initial fetch should flip the flag.
351 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get())); 164 EXPECT_CALL(observer_, OnUpdatePolicy(manager_.get()));
352 manager_->CancelWaitForPolicyFetch(); 165 manager_->CancelWaitForPolicyFetch();
353 EXPECT_TRUE(manager_->IsInitializationComplete()); 166 EXPECT_TRUE(manager_->IsInitializationComplete());
354 Mock::VerifyAndClearExpectations(&observer_); 167 Mock::VerifyAndClearExpectations(&observer_);
355 } 168 }
356 169
357 } // namespace 170 } // namespace
358 } // namespace policy 171 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698