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