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.
5 #include "components/policy/core/common/cloud/cloud_policy_client.h"
10 #include "base/bind.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
14 #include "components/policy/core/common/cloud/mock_cloud_policy_client.h"
15 #include "components/policy/core/common/cloud/mock_device_management_service.h"
16 #include "net/url_request/url_request_context_getter.h"
17 #include "net/url_request/url_request_test_util.h"
18 #include "policy/proto/device_management_backend.pb.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
23 using testing::Return;
24 using testing::SaveArg;
25 using testing::StrictMock;
28 namespace em = enterprise_management;
34 const char kClientID[] = "fake-client-id";
35 const char kMachineID[] = "fake-machine-id";
36 const char kMachineModel[] = "fake-machine-model";
37 const char kOAuthToken[] = "fake-oauth-token";
38 const char kDMToken[] = "fake-dm-token";
39 const char kDeviceCertificate[] = "fake-device-certificate";
40 const char kRequisition[] = "fake-requisition";
42 class MockStatusProvider : public CloudPolicyClient::StatusProvider {
44 MockStatusProvider() {}
45 virtual ~MockStatusProvider() {}
47 MOCK_METHOD1(GetDeviceStatus, bool(em::DeviceStatusReportRequest* status));
48 MOCK_METHOD1(GetSessionStatus, bool(em::SessionStatusReportRequest* status));
49 MOCK_METHOD0(OnSubmittedSuccessfully, void(void));
52 DISALLOW_COPY_AND_ASSIGN(MockStatusProvider);
55 MATCHER_P(MatchProto, expected, "matches protobuf") {
56 return arg.SerializePartialAsString() == expected.SerializePartialAsString();
59 // A mock class to allow us to set expectations on upload certificate callbacks.
60 class MockUploadCertificateObserver {
62 MockUploadCertificateObserver() {}
63 virtual ~MockUploadCertificateObserver() {}
65 MOCK_METHOD1(OnUploadComplete, void(bool));
70 class CloudPolicyClientTest : public testing::Test {
72 CloudPolicyClientTest()
73 : client_id_(kClientID),
74 policy_ns_key_(dm_protocol::kChromeUserPolicyType, std::string()) {
75 em::DeviceRegisterRequest* register_request =
76 registration_request_.mutable_register_request();
77 register_request->set_type(em::DeviceRegisterRequest::USER);
78 register_request->set_machine_id(kMachineID);
79 register_request->set_machine_model(kMachineModel);
80 registration_response_.mutable_register_response()->
81 set_device_management_token(kDMToken);
83 em::PolicyFetchRequest* policy_fetch_request =
84 policy_request_.mutable_policy_request()->add_request();
85 policy_fetch_request->set_policy_type(dm_protocol::kChromeUserPolicyType);
86 policy_fetch_request->set_signature_type(em::PolicyFetchRequest::SHA1_RSA);
87 policy_fetch_request->set_verification_key_hash(kPolicyVerificationKeyHash);
88 policy_response_.mutable_policy_response()->add_response()->set_policy_data(
89 CreatePolicyData("fake-policy-data"));
91 unregistration_request_.mutable_unregister_request();
92 unregistration_response_.mutable_unregister_response();
93 upload_certificate_request_.mutable_cert_upload_request()->
94 set_device_certificate(kDeviceCertificate);
95 upload_certificate_response_.mutable_cert_upload_response();
98 virtual void SetUp() OVERRIDE {
99 EXPECT_CALL(status_provider_, GetDeviceStatus(_))
100 .WillRepeatedly(Return(false));
101 EXPECT_CALL(status_provider_, GetSessionStatus(_))
102 .WillRepeatedly(Return(false));
103 CreateClient(USER_AFFILIATION_NONE);
106 virtual void TearDown() OVERRIDE {
107 client_->RemoveObserver(&observer_);
111 EXPECT_CALL(observer_, OnRegistrationStateChanged(_));
112 client_->SetupRegistration(kDMToken, client_id_);
115 void CreateClient(UserAffiliation user_affiliation) {
117 client_->RemoveObserver(&observer_);
119 request_context_ = new net::TestURLRequestContextGetter(
120 loop_.message_loop_proxy());
121 client_.reset(new CloudPolicyClient(kMachineID, kMachineModel,
122 kPolicyVerificationKeyHash,
123 user_affiliation, &status_provider_,
126 client_->AddNamespaceToFetch(policy_ns_key_);
127 client_->AddObserver(&observer_);
130 void ExpectRegistration(const std::string& oauth_token) {
131 EXPECT_CALL(service_,
132 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION,
134 .WillOnce(service_.SucceedJob(registration_response_));
135 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestRegister,
136 "", oauth_token, "", "", _,
137 MatchProto(registration_request_)))
138 .WillOnce(SaveArg<5>(&client_id_));
141 void ExpectPolicyFetch(const std::string& dm_token,
142 const std::string& user_affiliation) {
143 EXPECT_CALL(service_,
144 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH,
146 .WillOnce(service_.SucceedJob(policy_response_));
147 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestPolicy,
148 "", "", dm_token, user_affiliation,
150 MatchProto(policy_request_)));
153 void ExpectUnregistration(const std::string& dm_token) {
154 EXPECT_CALL(service_,
155 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION,
157 .WillOnce(service_.SucceedJob(unregistration_response_));
158 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestUnregister,
159 "", "", dm_token, "", client_id_,
160 MatchProto(unregistration_request_)));
163 void ExpectUploadCertificate() {
164 EXPECT_CALL(service_,
165 CreateJob(DeviceManagementRequestJob::TYPE_UPLOAD_CERTIFICATE,
167 .WillOnce(service_.SucceedJob(upload_certificate_response_));
168 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestUploadCertificate,
169 "", "", kDMToken, "", client_id_,
170 MatchProto(upload_certificate_request_)));
173 void CheckPolicyResponse() {
174 ASSERT_TRUE(client_->GetPolicyFor(policy_ns_key_));
175 EXPECT_THAT(*client_->GetPolicyFor(policy_ns_key_),
176 MatchProto(policy_response_.policy_response().response(0)));
179 std::string CreatePolicyData(const std::string& policy_value) {
180 em::PolicyData policy_data;
181 policy_data.set_policy_type(dm_protocol::kChromeUserPolicyType);
182 policy_data.set_policy_value(policy_value);
183 return policy_data.SerializeAsString();
186 // Request protobufs used as expectations for the client requests.
187 em::DeviceManagementRequest registration_request_;
188 em::DeviceManagementRequest policy_request_;
189 em::DeviceManagementRequest unregistration_request_;
190 em::DeviceManagementRequest upload_certificate_request_;
192 // Protobufs used in successful responses.
193 em::DeviceManagementResponse registration_response_;
194 em::DeviceManagementResponse policy_response_;
195 em::DeviceManagementResponse unregistration_response_;
196 em::DeviceManagementResponse upload_certificate_response_;
198 base::MessageLoop loop_;
199 std::string client_id_;
200 PolicyNamespaceKey policy_ns_key_;
201 MockDeviceManagementService service_;
202 StrictMock<MockStatusProvider> status_provider_;
203 StrictMock<MockCloudPolicyClientObserver> observer_;
204 StrictMock<MockUploadCertificateObserver> upload_certificate_observer_;
205 scoped_ptr<CloudPolicyClient> client_;
206 // Cached weak pointer to the client's request context.
207 net::URLRequestContextGetter* request_context_;
210 TEST_F(CloudPolicyClientTest, Init) {
211 EXPECT_CALL(service_, CreateJob(_, _)).Times(0);
212 EXPECT_FALSE(client_->is_registered());
213 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_));
214 EXPECT_EQ(0, client_->fetched_invalidation_version());
217 TEST_F(CloudPolicyClientTest, SetupRegistrationAndPolicyFetch) {
218 EXPECT_CALL(service_, CreateJob(_, _)).Times(0);
219 EXPECT_CALL(observer_, OnRegistrationStateChanged(_));
220 client_->SetupRegistration(kDMToken, client_id_);
221 EXPECT_TRUE(client_->is_registered());
222 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_));
224 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
225 EXPECT_CALL(observer_, OnPolicyFetched(_));
226 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
227 client_->FetchPolicy();
228 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
229 CheckPolicyResponse();
232 TEST_F(CloudPolicyClientTest, RegistrationAndPolicyFetch) {
233 ExpectRegistration(kOAuthToken);
234 EXPECT_CALL(observer_, OnRegistrationStateChanged(_));
235 client_->Register(em::DeviceRegisterRequest::USER,
236 kOAuthToken, std::string(), false, std::string());
237 EXPECT_TRUE(client_->is_registered());
238 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_));
239 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
241 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
242 EXPECT_CALL(observer_, OnPolicyFetched(_));
243 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
244 client_->FetchPolicy();
245 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
246 CheckPolicyResponse();
249 TEST_F(CloudPolicyClientTest, RegistrationParameters) {
250 registration_request_.mutable_register_request()->set_reregister(true);
251 registration_request_.mutable_register_request()->set_auto_enrolled(true);
252 registration_request_.mutable_register_request()->set_requisition(
254 ExpectRegistration(kOAuthToken);
255 EXPECT_CALL(observer_, OnRegistrationStateChanged(_));
256 client_->Register(em::DeviceRegisterRequest::USER,
257 kOAuthToken, kClientID, true, kRequisition);
258 EXPECT_EQ(kClientID, client_id_);
261 TEST_F(CloudPolicyClientTest, RegistrationNoToken) {
262 registration_response_.mutable_register_response()->
263 clear_device_management_token();
264 ExpectRegistration(kOAuthToken);
265 EXPECT_CALL(observer_, OnClientError(_));
266 client_->Register(em::DeviceRegisterRequest::USER,
267 kOAuthToken, std::string(), false, std::string());
268 EXPECT_FALSE(client_->is_registered());
269 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_));
270 EXPECT_EQ(DM_STATUS_RESPONSE_DECODING_ERROR, client_->status());
273 TEST_F(CloudPolicyClientTest, RegistrationFailure) {
274 EXPECT_CALL(service_,
275 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION,
277 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED));
278 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _));
279 EXPECT_CALL(observer_, OnClientError(_));
280 client_->Register(em::DeviceRegisterRequest::USER,
281 kOAuthToken, std::string(), false, std::string());
282 EXPECT_FALSE(client_->is_registered());
283 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_));
284 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status());
287 TEST_F(CloudPolicyClientTest, RetryRegistration) {
288 // First registration does not set the re-register flag.
290 registration_request_.mutable_register_request()->has_reregister());
291 MockDeviceManagementJob* register_job = NULL;
292 EXPECT_CALL(service_,
293 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION,
295 .WillOnce(service_.CreateAsyncJob(®ister_job));
296 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestRegister,
297 "", kOAuthToken, "", "", _,
298 MatchProto(registration_request_)));
299 client_->Register(em::DeviceRegisterRequest::USER,
300 kOAuthToken, std::string(), false, std::string());
301 EXPECT_FALSE(client_->is_registered());
302 Mock::VerifyAndClearExpectations(&service_);
304 // Simulate a retry callback before proceeding; the re-register flag is set.
305 registration_request_.mutable_register_request()->set_reregister(true);
306 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestRegister,
307 "", kOAuthToken, "", "", _,
308 MatchProto(registration_request_)));
309 register_job->RetryJob();
310 Mock::VerifyAndClearExpectations(&service_);
312 // Subsequent retries keep the flag set.
313 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestRegister,
314 "", kOAuthToken, "", "", _,
315 MatchProto(registration_request_)));
316 register_job->RetryJob();
317 Mock::VerifyAndClearExpectations(&service_);
320 TEST_F(CloudPolicyClientTest, PolicyUpdate) {
323 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
324 EXPECT_CALL(observer_, OnPolicyFetched(_));
325 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
326 client_->FetchPolicy();
327 CheckPolicyResponse();
329 policy_response_.mutable_policy_response()->clear_response();
330 policy_response_.mutable_policy_response()->add_response()->set_policy_data(
331 CreatePolicyData("updated-fake-policy-data"));
332 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
333 EXPECT_CALL(observer_, OnPolicyFetched(_));
334 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
335 client_->FetchPolicy();
336 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
337 CheckPolicyResponse();
340 TEST_F(CloudPolicyClientTest, PolicyFetchWithMetaData) {
343 const base::Time timestamp(
344 base::Time::UnixEpoch() + base::TimeDelta::FromDays(20));
345 client_->set_submit_machine_id(true);
346 client_->set_last_policy_timestamp(timestamp);
347 client_->set_public_key_version(42);
348 em::PolicyFetchRequest* policy_fetch_request =
349 policy_request_.mutable_policy_request()->mutable_request(0);
350 policy_fetch_request->set_machine_id(kMachineID);
351 policy_fetch_request->set_timestamp(
352 (timestamp - base::Time::UnixEpoch()).InMilliseconds());
353 policy_fetch_request->set_public_key_version(42);
355 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
356 EXPECT_CALL(observer_, OnPolicyFetched(_));
357 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
358 client_->FetchPolicy();
359 CheckPolicyResponse();
362 TEST_F(CloudPolicyClientTest, PolicyFetchWithInvalidation) {
365 int64 previous_version = client_->fetched_invalidation_version();
366 client_->SetInvalidationInfo(12345, "12345");
367 EXPECT_EQ(previous_version, client_->fetched_invalidation_version());
368 em::PolicyFetchRequest* policy_fetch_request =
369 policy_request_.mutable_policy_request()->mutable_request(0);
370 policy_fetch_request->set_invalidation_version(12345);
371 policy_fetch_request->set_invalidation_payload("12345");
373 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
374 EXPECT_CALL(observer_, OnPolicyFetched(_));
375 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
376 client_->FetchPolicy();
377 CheckPolicyResponse();
378 EXPECT_EQ(12345, client_->fetched_invalidation_version());
381 TEST_F(CloudPolicyClientTest, PolicyFetchWithInvalidationNoPayload) {
384 int64 previous_version = client_->fetched_invalidation_version();
385 client_->SetInvalidationInfo(-12345, std::string());
386 EXPECT_EQ(previous_version, client_->fetched_invalidation_version());
388 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
389 EXPECT_CALL(observer_, OnPolicyFetched(_));
390 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
391 client_->FetchPolicy();
392 CheckPolicyResponse();
393 EXPECT_EQ(-12345, client_->fetched_invalidation_version());
396 TEST_F(CloudPolicyClientTest, BadPolicyResponse) {
399 policy_response_.clear_policy_response();
400 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
401 EXPECT_CALL(observer_, OnClientError(_));
402 client_->FetchPolicy();
403 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_));
404 EXPECT_EQ(DM_STATUS_RESPONSE_DECODING_ERROR, client_->status());
406 policy_response_.mutable_policy_response()->add_response()->set_policy_data(
407 CreatePolicyData("fake-policy-data"));
408 policy_response_.mutable_policy_response()->add_response()->set_policy_data(
409 CreatePolicyData("excess-fake-policy-data"));
410 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone);
411 EXPECT_CALL(observer_, OnPolicyFetched(_));
412 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
413 client_->FetchPolicy();
414 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
415 CheckPolicyResponse();
418 TEST_F(CloudPolicyClientTest, PolicyRequestFailure) {
421 EXPECT_CALL(service_,
422 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH,
424 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED));
425 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _));
426 EXPECT_CALL(observer_, OnClientError(_));
427 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()).Times(0);
428 client_->FetchPolicy();
429 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status());
430 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_));
433 TEST_F(CloudPolicyClientTest, Unregister) {
436 ExpectUnregistration(kDMToken);
437 EXPECT_CALL(observer_, OnRegistrationStateChanged(_));
438 client_->Unregister();
439 EXPECT_FALSE(client_->is_registered());
440 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
443 TEST_F(CloudPolicyClientTest, UnregisterEmpty) {
446 unregistration_response_.clear_unregister_response();
447 EXPECT_CALL(service_,
448 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION,
450 .WillOnce(service_.SucceedJob(unregistration_response_));
451 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _));
452 EXPECT_CALL(observer_, OnRegistrationStateChanged(_));
453 client_->Unregister();
454 EXPECT_FALSE(client_->is_registered());
455 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
458 TEST_F(CloudPolicyClientTest, UnregisterFailure) {
461 EXPECT_CALL(service_,
462 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION,
464 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED));
465 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _));
466 EXPECT_CALL(observer_, OnClientError(_));
467 client_->Unregister();
468 EXPECT_TRUE(client_->is_registered());
469 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status());
472 TEST_F(CloudPolicyClientTest, PolicyFetchWithExtensionPolicy) {
475 // Setup the |expected_responses| and |policy_response_|.
476 static const char* kExtensions[] = {
477 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
478 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
479 "cccccccccccccccccccccccccccccccc",
481 typedef std::map<PolicyNamespaceKey, em::PolicyFetchResponse> ResponseMap;
482 ResponseMap expected_responses;
483 std::set<PolicyNamespaceKey> expected_namespaces;
484 PolicyNamespaceKey key(dm_protocol::kChromeUserPolicyType, std::string());
485 // Copy the user policy fetch request.
486 expected_responses[key].CopyFrom(
487 policy_response_.policy_response().response(0));
488 expected_namespaces.insert(key);
489 key.first = dm_protocol::kChromeExtensionPolicyType;
490 for (size_t i = 0; i < arraysize(kExtensions); ++i) {
491 key.second = kExtensions[i];
492 em::PolicyData policy_data;
493 policy_data.set_policy_type(key.first);
494 policy_data.set_settings_entity_id(key.second);
495 expected_responses[key].set_policy_data(policy_data.SerializeAsString());
496 policy_response_.mutable_policy_response()->add_response()->CopyFrom(
497 expected_responses[key]);
498 expected_namespaces.insert(key);
501 // Make a policy fetch.
502 EXPECT_CALL(service_,
503 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH,
505 .WillOnce(service_.SucceedJob(policy_response_));
506 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestPolicy, "", "",
508 dm_protocol::kValueUserAffiliationNone,
510 .WillOnce(SaveArg<6>(&policy_request_));
511 EXPECT_CALL(observer_, OnPolicyFetched(_));
512 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully());
513 for (size_t i = 0; i < arraysize(kExtensions); ++i) {
514 client_->AddNamespaceToFetch(PolicyNamespaceKey(
515 dm_protocol::kChromeExtensionPolicyType, kExtensions[i]));
517 client_->FetchPolicy();
519 // Verify that the request includes the expected namespaces.
520 ASSERT_TRUE(policy_request_.has_policy_request());
521 const em::DevicePolicyRequest& policy_request =
522 policy_request_.policy_request();
523 ASSERT_EQ(static_cast<int>(1 + arraysize(kExtensions)),
524 policy_request.request_size());
525 for (int i = 0; i < policy_request.request_size(); ++i) {
526 const em::PolicyFetchRequest& fetch_request = policy_request.request(i);
527 ASSERT_TRUE(fetch_request.has_policy_type());
528 std::string entity_id;
529 if (fetch_request.has_settings_entity_id())
530 entity_id = fetch_request.settings_entity_id();
531 PolicyNamespaceKey key(fetch_request.policy_type(), entity_id);
532 EXPECT_EQ(1u, expected_namespaces.erase(key));
534 EXPECT_TRUE(expected_namespaces.empty());
536 // Verify that the client got all the responses mapped to their namespaces.
537 for (ResponseMap::iterator it = expected_responses.begin();
538 it != expected_responses.end(); ++it) {
539 const em::PolicyFetchResponse* response = client_->GetPolicyFor(it->first);
540 ASSERT_TRUE(response);
541 EXPECT_EQ(it->second.SerializeAsString(), response->SerializeAsString());
545 TEST_F(CloudPolicyClientTest, UploadCertificate) {
548 ExpectUploadCertificate();
549 EXPECT_CALL(upload_certificate_observer_, OnUploadComplete(true)).Times(1);
550 CloudPolicyClient::StatusCallback callback = base::Bind(
551 &MockUploadCertificateObserver::OnUploadComplete,
552 base::Unretained(&upload_certificate_observer_));
553 client_->UploadCertificate(kDeviceCertificate, callback);
554 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
557 TEST_F(CloudPolicyClientTest, UploadCertificateEmpty) {
560 upload_certificate_response_.clear_cert_upload_response();
561 ExpectUploadCertificate();
562 EXPECT_CALL(upload_certificate_observer_, OnUploadComplete(false)).Times(1);
563 CloudPolicyClient::StatusCallback callback = base::Bind(
564 &MockUploadCertificateObserver::OnUploadComplete,
565 base::Unretained(&upload_certificate_observer_));
566 client_->UploadCertificate(kDeviceCertificate, callback);
567 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status());
570 TEST_F(CloudPolicyClientTest, UploadCertificateFailure) {
573 EXPECT_CALL(upload_certificate_observer_, OnUploadComplete(false)).Times(1);
574 EXPECT_CALL(service_,
575 CreateJob(DeviceManagementRequestJob::TYPE_UPLOAD_CERTIFICATE,
577 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED));
578 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _));
579 EXPECT_CALL(observer_, OnClientError(_));
580 CloudPolicyClient::StatusCallback callback = base::Bind(
581 &MockUploadCertificateObserver::OnUploadComplete,
582 base::Unretained(&upload_certificate_observer_));
583 client_->UploadCertificate(kDeviceCertificate, callback);
584 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status());
587 } // namespace policy