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

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

Issue 9109009: Introduce CloudPolicyClient (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Restore DCHECKS. Created 8 years, 7 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
(Empty)
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.
4
5 #include "chrome/browser/policy/cloud_policy_client.h"
6
7 #include "base/compiler_specific.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "chrome/browser/policy/mock_device_management_service.h"
10 #include "chrome/browser/policy/proto/device_management_backend.pb.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 using testing::Return;
15 using testing::SaveArg;
16 using testing::StrictMock;
17 using testing::_;
18
19 namespace em = enterprise_management;
20
21 namespace policy {
22
23 namespace {
24
25 const char kMachineID[] = "fake-machine-id";
26 const char kMachineModel[] = "fake-machine-model";
27 const char kOAuthToken[] = "fake-oauth-token";
28 const char kDMToken[] = "fake-dm-token";
29
30 class MockObserver : public CloudPolicyClient::Observer {
31 public:
32 MockObserver() {}
33 virtual ~MockObserver() {}
34
35 MOCK_METHOD1(OnPolicyFetched, void(CloudPolicyClient*));
36 MOCK_METHOD1(OnRegistrationStateChanged, void(CloudPolicyClient*));
37 MOCK_METHOD1(OnClientError, void(CloudPolicyClient*));
38
39 private:
40 DISALLOW_COPY_AND_ASSIGN(MockObserver);
41 };
42
43 class MockStatusProvider : public CloudPolicyClient::StatusProvider {
44 public:
45 MockStatusProvider() {}
46 virtual ~MockStatusProvider() {}
47
48 MOCK_METHOD1(GetDeviceStatus, bool(em::DeviceStatusReportRequest* status));
49 MOCK_METHOD1(GetSessionStatus, bool(em::SessionStatusReportRequest* status));
50 MOCK_METHOD0(OnSubmittedSuccessfully, void(void));
51
52 private:
53 DISALLOW_COPY_AND_ASSIGN(MockStatusProvider);
54 };
55
56 MATCHER_P(MatchProto, expected, "matches protobuf") {
57 return arg.SerializePartialAsString() == expected.SerializePartialAsString();
58 }
59
60 } // namespace
61
62 class CloudPolicyClientTest : public testing::Test {
63 protected:
64 CloudPolicyClientTest()
65 : client_id_("fake-client-id") {
66 em::DeviceRegisterRequest* register_request =
67 registration_request_.mutable_register_request();
68 register_request->set_type(em::DeviceRegisterRequest::USER);
69 register_request->set_machine_id(kMachineID);
70 register_request->set_machine_model(kMachineModel);
71 registration_response_.mutable_register_response()->
72 set_device_management_token(kDMToken);
73
74 em::PolicyFetchRequest* policy_fetch_request =
75 policy_request_.mutable_policy_request()->add_request();
76 policy_fetch_request->set_policy_type(dm_protocol::kChromeUserPolicyType);
77 policy_fetch_request->set_signature_type(em::PolicyFetchRequest::SHA1_RSA);
78 policy_response_.mutable_policy_response()->add_response()->set_policy_data(
79 "fake-policy-data");
80
81 unregistration_request_.mutable_unregister_request();
82 unregistration_response_.mutable_unregister_response();
83 }
84
85 virtual void SetUp() OVERRIDE {
86 EXPECT_CALL(status_provider_, GetDeviceStatus(_))
87 .WillRepeatedly(Return(false));
88 EXPECT_CALL(status_provider_, GetSessionStatus(_))
89 .WillRepeatedly(Return(false));
90 CreateClient(USER_AFFILIATION_NONE, POLICY_SCOPE_USER);
91 }
92
93 virtual void TearDown() OVERRIDE {
94 client_->RemoveObserver(&observer_);
95 }
96
97 void Register() {
98 EXPECT_CALL(observer_, OnRegistrationStateChanged(_));
99 client_->SetupRegistration(kDMToken, client_id_);
100 }
101
102 void CreateClient(UserAffiliation user_affiliation, PolicyScope scope) {
103 if (client_.get())
104 client_->RemoveObserver(&observer_);
105
106 client_.reset(new CloudPolicyClient(kMachineID, kMachineModel,
107 user_affiliation, scope,
108 &status_provider_, &service_));
109 client_->AddObserver(&observer_);
110 }
111
112 void ExpectRegistration(const std::string& oauth_token) {
113 EXPECT_CALL(service_,
114 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
115 .WillOnce(service_.SucceedJob(registration_response_));
116 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestRegister,
117 "", oauth_token, "", "", _,
118 MatchProto(registration_request_)))
119 .WillOnce(SaveArg<5>(&client_id_));
120 }
121
122 void ExpectPolicyFetch(const std::string& dm_token,
123 const std::string& user_affiliation) {
124 EXPECT_CALL(service_,
125 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH))
126 .WillOnce(service_.SucceedJob(policy_response_));
127 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestPolicy,
128 "", "", dm_token, user_affiliation,
129 client_id_,
130 MatchProto(policy_request_)));
131 }
132
133 void ExpectUnregistration(const std::string& dm_token) {
134 EXPECT_CALL(service_,
135 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION))
136 .WillOnce(service_.SucceedJob(unregistration_response_));
137 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestUnregister,
138 "", "", dm_token, "", client_id_,
139 MatchProto(unregistration_request_)));
140 }
141
142 void CheckPolicyResponse() {
143 ASSERT_TRUE(client_->policy());
144 EXPECT_THAT(*client_->policy(),
145 MatchProto(policy_response_.policy_response().response(0)));
146 }
147
148 // Request protobufs used as expectations for the client requests.
149 em::DeviceManagementRequest registration_request_;
150 em::DeviceManagementRequest policy_request_;
151 em::DeviceManagementRequest unregistration_request_;
152
153 // Protobufs used in successful responses.
154 em::DeviceManagementResponse registration_response_;
155 em::DeviceManagementResponse policy_response_;
156 em::DeviceManagementResponse unregistration_response_;
157
158 std::string client_id_;
159 MockDeviceManagementService service_;
160 StrictMock<MockStatusProvider> status_provider_;
161 StrictMock<MockObserver> observer_;
162 scoped_ptr<CloudPolicyClient> client_;
163 };
164
165 TEST_F(CloudPolicyClientTest, Init) {
166 EXPECT_CALL(service_, CreateJob(_)).Times(0);
167 EXPECT_FALSE(client_->is_registered());
168 EXPECT_FALSE(client_->policy());
169 }
170
171 TEST_F(CloudPolicyClientTest, SetupRegistrationAndPolicyFetch) {
172 EXPECT_CALL(service_, CreateJob(_)).Times(0);
173 EXPECT_CALL(observer_, OnRegistrationStateChanged(_));
174 client_->SetupRegistration(kDMToken, client_id_);
175 EXPECT_TRUE(client_->is_registered());
176 EXPECT_FALSE(client_->policy());
177
178 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
179 EXPECT_CALL(observer_, OnPolicyFetched(_));
180 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
181 client_->FetchPolicy();
182 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
183 CheckPolicyResponse();
184 }
185
186 TEST_F(CloudPolicyClientTest, RegistrationAndPolicyFetch) {
187 ExpectRegistration(kOAuthToken);
188 EXPECT_CALL(observer_, OnRegistrationStateChanged(_));
189 client_->Register(kOAuthToken);
190 EXPECT_TRUE(client_->is_registered());
191 EXPECT_FALSE(client_->policy());
192 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
193
194 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
195 EXPECT_CALL(observer_, OnPolicyFetched(_));
196 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
197 client_->FetchPolicy();
198 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
199 CheckPolicyResponse();
200 }
201
202 TEST_F(CloudPolicyClientTest, RegistrationNoToken) {
203 registration_response_.mutable_register_response()->
204 clear_device_management_token();
205 ExpectRegistration(kOAuthToken);
206 EXPECT_CALL(observer_, OnClientError(_));
207 client_->Register(kOAuthToken);
208 EXPECT_FALSE(client_->is_registered());
209 EXPECT_FALSE(client_->policy());
210 EXPECT_EQ(DM_STATUS_RESPONSE_DECODING_ERROR, client_->status());
211 }
212
213 TEST_F(CloudPolicyClientTest, RegistrationFailure) {
214 EXPECT_CALL(service_,
215 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION))
216 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED));
217 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _));
218 EXPECT_CALL(observer_, OnClientError(_));
219 client_->Register(kOAuthToken);
220 EXPECT_FALSE(client_->is_registered());
221 EXPECT_FALSE(client_->policy());
222 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status());
223 }
224
225 TEST_F(CloudPolicyClientTest, PolicyUpdate) {
226 Register();
227
228 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
229 EXPECT_CALL(observer_, OnPolicyFetched(_));
230 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
231 client_->FetchPolicy();
232 CheckPolicyResponse();
233
234 policy_response_.mutable_policy_response()->clear_response();
235 policy_response_.mutable_policy_response()->add_response()->set_policy_data(
236 "updated-fake-policy-data");
237 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
238 EXPECT_CALL(observer_, OnPolicyFetched(_));
239 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
240 client_->FetchPolicy();
241 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
242 CheckPolicyResponse();
243 }
244
245 TEST_F(CloudPolicyClientTest, PolicyFetchWithMetaData) {
246 Register();
247
248 const base::Time timestamp(
249 base::Time::UnixEpoch() + base::TimeDelta::FromDays(20));
250 client_->set_submit_machine_id(true);
251 client_->set_last_policy_timestamp(timestamp);
252 client_->set_public_key_version(42);
253 em::PolicyFetchRequest* policy_fetch_request =
254 policy_request_.mutable_policy_request()->mutable_request(0);
255 policy_fetch_request->set_machine_id(kMachineID);
256 policy_fetch_request->set_timestamp(
257 (timestamp - base::Time::UnixEpoch()).InMilliseconds());
258 policy_fetch_request->set_public_key_version(42);
259
260 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
261 EXPECT_CALL(observer_, OnPolicyFetched(_));
262 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
263 client_->FetchPolicy();
264 CheckPolicyResponse();
265 }
266
267 TEST_F(CloudPolicyClientTest, BadPolicyResponse) {
268 Register();
269
270 policy_response_.clear_policy_response();
271 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
272 EXPECT_CALL(observer_, OnClientError(_));
273 client_->FetchPolicy();
274 EXPECT_FALSE(client_->policy());
275 EXPECT_EQ(DM_STATUS_RESPONSE_DECODING_ERROR, client_->status());
276
277 policy_response_.mutable_policy_response()->add_response()->set_policy_data(
278 "fake-policy-data");
279 policy_response_.mutable_policy_response()->add_response()->set_policy_data(
280 "excess-fake-policy-data");
281 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
282 EXPECT_CALL(observer_, OnPolicyFetched(_));
283 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
284 client_->FetchPolicy();
285 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
286 CheckPolicyResponse();
287 }
288
289 TEST_F(CloudPolicyClientTest, PolicyRequestFailure) {
290 Register();
291
292 EXPECT_CALL(service_,
293 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH))
294 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED));
295 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _));
296 EXPECT_CALL(observer_, OnClientError(_));
297 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()).Times(0);
298 client_->FetchPolicy();
299 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status());
300 EXPECT_FALSE(client_->policy());
301 }
302
303 TEST_F(CloudPolicyClientTest, Unregister) {
304 Register();
305
306 ExpectUnregistration(kDMToken);
307 EXPECT_CALL(observer_, OnRegistrationStateChanged(_));
308 client_->Unregister();
309 EXPECT_FALSE(client_->is_registered());
310 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
311 }
312
313 TEST_F(CloudPolicyClientTest, UnregisterEmpty) {
314 Register();
315
316 unregistration_response_.clear_unregister_response();
317 EXPECT_CALL(service_,
318 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION))
319 .WillOnce(service_.SucceedJob(unregistration_response_));
320 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _));
321 EXPECT_CALL(observer_, OnRegistrationStateChanged(_));
322 client_->Unregister();
323 EXPECT_FALSE(client_->is_registered());
324 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
325 }
326
327 TEST_F(CloudPolicyClientTest, UnregisterFailure) {
328 Register();
329
330 EXPECT_CALL(service_,
331 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION))
332 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED));
333 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _));
334 EXPECT_CALL(observer_, OnClientError(_));
335 client_->Unregister();
336 EXPECT_TRUE(client_->is_registered());
337 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status());
338 }
339
340 } // namespace policy
OLDNEW
« no previous file with comments | « chrome/browser/policy/cloud_policy_client.cc ('k') | chrome/browser/policy/cloud_policy_controller_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698