| OLD | NEW |
| (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/cloud_policy_client.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <set> | |
| 9 | |
| 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 "chrome/browser/policy/cloud/mock_cloud_policy_client.h" | |
| 15 #include "chrome/browser/policy/cloud/mock_device_management_service.h" | |
| 16 #include "chrome/browser/policy/proto/cloud/device_management_backend.pb.h" | |
| 17 #include "net/url_request/url_request_context_getter.h" | |
| 18 #include "net/url_request/url_request_test_util.h" | |
| 19 #include "testing/gmock/include/gmock/gmock.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 using testing::Mock; | |
| 23 using testing::Return; | |
| 24 using testing::SaveArg; | |
| 25 using testing::StrictMock; | |
| 26 using testing::_; | |
| 27 | |
| 28 namespace em = enterprise_management; | |
| 29 | |
| 30 namespace policy { | |
| 31 | |
| 32 namespace { | |
| 33 | |
| 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"; | |
| 41 | |
| 42 class MockStatusProvider : public CloudPolicyClient::StatusProvider { | |
| 43 public: | |
| 44 MockStatusProvider() {} | |
| 45 virtual ~MockStatusProvider() {} | |
| 46 | |
| 47 MOCK_METHOD1(GetDeviceStatus, bool(em::DeviceStatusReportRequest* status)); | |
| 48 MOCK_METHOD1(GetSessionStatus, bool(em::SessionStatusReportRequest* status)); | |
| 49 MOCK_METHOD0(OnSubmittedSuccessfully, void(void)); | |
| 50 | |
| 51 private: | |
| 52 DISALLOW_COPY_AND_ASSIGN(MockStatusProvider); | |
| 53 }; | |
| 54 | |
| 55 MATCHER_P(MatchProto, expected, "matches protobuf") { | |
| 56 return arg.SerializePartialAsString() == expected.SerializePartialAsString(); | |
| 57 } | |
| 58 | |
| 59 // A mock class to allow us to set expectations on upload certificate callbacks. | |
| 60 class MockUploadCertificateObserver { | |
| 61 public: | |
| 62 MockUploadCertificateObserver() {} | |
| 63 virtual ~MockUploadCertificateObserver() {} | |
| 64 | |
| 65 MOCK_METHOD1(OnUploadComplete, void(bool)); | |
| 66 }; | |
| 67 | |
| 68 } // namespace | |
| 69 | |
| 70 class CloudPolicyClientTest : public testing::Test { | |
| 71 protected: | |
| 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); | |
| 82 | |
| 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 #if defined(OS_CHROMEOS) | |
| 87 policy_fetch_request->set_signature_type(em::PolicyFetchRequest::SHA1_RSA); | |
| 88 #else | |
| 89 policy_fetch_request->set_signature_type(em::PolicyFetchRequest::NONE); | |
| 90 #endif | |
| 91 policy_response_.mutable_policy_response()->add_response()->set_policy_data( | |
| 92 CreatePolicyData("fake-policy-data")); | |
| 93 | |
| 94 unregistration_request_.mutable_unregister_request(); | |
| 95 unregistration_response_.mutable_unregister_response(); | |
| 96 upload_certificate_request_.mutable_cert_upload_request()-> | |
| 97 set_device_certificate(kDeviceCertificate); | |
| 98 upload_certificate_response_.mutable_cert_upload_response(); | |
| 99 } | |
| 100 | |
| 101 virtual void SetUp() OVERRIDE { | |
| 102 EXPECT_CALL(status_provider_, GetDeviceStatus(_)) | |
| 103 .WillRepeatedly(Return(false)); | |
| 104 EXPECT_CALL(status_provider_, GetSessionStatus(_)) | |
| 105 .WillRepeatedly(Return(false)); | |
| 106 CreateClient(USER_AFFILIATION_NONE); | |
| 107 } | |
| 108 | |
| 109 virtual void TearDown() OVERRIDE { | |
| 110 client_->RemoveObserver(&observer_); | |
| 111 } | |
| 112 | |
| 113 void Register() { | |
| 114 EXPECT_CALL(observer_, OnRegistrationStateChanged(_)); | |
| 115 client_->SetupRegistration(kDMToken, client_id_); | |
| 116 } | |
| 117 | |
| 118 void CreateClient(UserAffiliation user_affiliation) { | |
| 119 if (client_.get()) | |
| 120 client_->RemoveObserver(&observer_); | |
| 121 | |
| 122 request_context_ = new net::TestURLRequestContextGetter( | |
| 123 loop_.message_loop_proxy()); | |
| 124 client_.reset(new CloudPolicyClient(kMachineID, kMachineModel, | |
| 125 user_affiliation, &status_provider_, | |
| 126 &service_, | |
| 127 request_context_)); | |
| 128 client_->AddNamespaceToFetch(policy_ns_key_); | |
| 129 client_->AddObserver(&observer_); | |
| 130 } | |
| 131 | |
| 132 void ExpectRegistration(const std::string& oauth_token) { | |
| 133 EXPECT_CALL(service_, | |
| 134 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION, | |
| 135 request_context_)) | |
| 136 .WillOnce(service_.SucceedJob(registration_response_)); | |
| 137 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestRegister, | |
| 138 "", oauth_token, "", "", _, | |
| 139 MatchProto(registration_request_))) | |
| 140 .WillOnce(SaveArg<5>(&client_id_)); | |
| 141 } | |
| 142 | |
| 143 void ExpectPolicyFetch(const std::string& dm_token, | |
| 144 const std::string& user_affiliation) { | |
| 145 EXPECT_CALL(service_, | |
| 146 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH, | |
| 147 request_context_)) | |
| 148 .WillOnce(service_.SucceedJob(policy_response_)); | |
| 149 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestPolicy, | |
| 150 "", "", dm_token, user_affiliation, | |
| 151 client_id_, | |
| 152 MatchProto(policy_request_))); | |
| 153 } | |
| 154 | |
| 155 void ExpectUnregistration(const std::string& dm_token) { | |
| 156 EXPECT_CALL(service_, | |
| 157 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION, | |
| 158 request_context_)) | |
| 159 .WillOnce(service_.SucceedJob(unregistration_response_)); | |
| 160 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestUnregister, | |
| 161 "", "", dm_token, "", client_id_, | |
| 162 MatchProto(unregistration_request_))); | |
| 163 } | |
| 164 | |
| 165 void ExpectUploadCertificate() { | |
| 166 EXPECT_CALL(service_, | |
| 167 CreateJob(DeviceManagementRequestJob::TYPE_UPLOAD_CERTIFICATE, | |
| 168 request_context_)) | |
| 169 .WillOnce(service_.SucceedJob(upload_certificate_response_)); | |
| 170 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestUploadCertificate, | |
| 171 "", "", kDMToken, "", client_id_, | |
| 172 MatchProto(upload_certificate_request_))); | |
| 173 } | |
| 174 | |
| 175 void CheckPolicyResponse() { | |
| 176 ASSERT_TRUE(client_->GetPolicyFor(policy_ns_key_)); | |
| 177 EXPECT_THAT(*client_->GetPolicyFor(policy_ns_key_), | |
| 178 MatchProto(policy_response_.policy_response().response(0))); | |
| 179 } | |
| 180 | |
| 181 std::string CreatePolicyData(const std::string& policy_value) { | |
| 182 em::PolicyData policy_data; | |
| 183 policy_data.set_policy_type(dm_protocol::kChromeUserPolicyType); | |
| 184 policy_data.set_policy_value(policy_value); | |
| 185 return policy_data.SerializeAsString(); | |
| 186 } | |
| 187 | |
| 188 // Request protobufs used as expectations for the client requests. | |
| 189 em::DeviceManagementRequest registration_request_; | |
| 190 em::DeviceManagementRequest policy_request_; | |
| 191 em::DeviceManagementRequest unregistration_request_; | |
| 192 em::DeviceManagementRequest upload_certificate_request_; | |
| 193 | |
| 194 // Protobufs used in successful responses. | |
| 195 em::DeviceManagementResponse registration_response_; | |
| 196 em::DeviceManagementResponse policy_response_; | |
| 197 em::DeviceManagementResponse unregistration_response_; | |
| 198 em::DeviceManagementResponse upload_certificate_response_; | |
| 199 | |
| 200 base::MessageLoop loop_; | |
| 201 std::string client_id_; | |
| 202 PolicyNamespaceKey policy_ns_key_; | |
| 203 MockDeviceManagementService service_; | |
| 204 StrictMock<MockStatusProvider> status_provider_; | |
| 205 StrictMock<MockCloudPolicyClientObserver> observer_; | |
| 206 StrictMock<MockUploadCertificateObserver> upload_certificate_observer_; | |
| 207 scoped_ptr<CloudPolicyClient> client_; | |
| 208 // Cached weak pointer to the client's request context. | |
| 209 net::URLRequestContextGetter* request_context_; | |
| 210 }; | |
| 211 | |
| 212 TEST_F(CloudPolicyClientTest, Init) { | |
| 213 EXPECT_CALL(service_, CreateJob(_, _)).Times(0); | |
| 214 EXPECT_FALSE(client_->is_registered()); | |
| 215 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); | |
| 216 EXPECT_EQ(0, client_->fetched_invalidation_version()); | |
| 217 } | |
| 218 | |
| 219 TEST_F(CloudPolicyClientTest, SetupRegistrationAndPolicyFetch) { | |
| 220 EXPECT_CALL(service_, CreateJob(_, _)).Times(0); | |
| 221 EXPECT_CALL(observer_, OnRegistrationStateChanged(_)); | |
| 222 client_->SetupRegistration(kDMToken, client_id_); | |
| 223 EXPECT_TRUE(client_->is_registered()); | |
| 224 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); | |
| 225 | |
| 226 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | |
| 227 EXPECT_CALL(observer_, OnPolicyFetched(_)); | |
| 228 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | |
| 229 client_->FetchPolicy(); | |
| 230 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); | |
| 231 CheckPolicyResponse(); | |
| 232 } | |
| 233 | |
| 234 TEST_F(CloudPolicyClientTest, RegistrationAndPolicyFetch) { | |
| 235 ExpectRegistration(kOAuthToken); | |
| 236 EXPECT_CALL(observer_, OnRegistrationStateChanged(_)); | |
| 237 client_->Register(em::DeviceRegisterRequest::USER, | |
| 238 kOAuthToken, std::string(), false, std::string()); | |
| 239 EXPECT_TRUE(client_->is_registered()); | |
| 240 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); | |
| 241 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); | |
| 242 | |
| 243 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | |
| 244 EXPECT_CALL(observer_, OnPolicyFetched(_)); | |
| 245 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | |
| 246 client_->FetchPolicy(); | |
| 247 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); | |
| 248 CheckPolicyResponse(); | |
| 249 } | |
| 250 | |
| 251 TEST_F(CloudPolicyClientTest, RegistrationParameters) { | |
| 252 registration_request_.mutable_register_request()->set_reregister(true); | |
| 253 registration_request_.mutable_register_request()->set_auto_enrolled(true); | |
| 254 registration_request_.mutable_register_request()->set_requisition( | |
| 255 kRequisition); | |
| 256 ExpectRegistration(kOAuthToken); | |
| 257 EXPECT_CALL(observer_, OnRegistrationStateChanged(_)); | |
| 258 client_->Register(em::DeviceRegisterRequest::USER, | |
| 259 kOAuthToken, kClientID, true, kRequisition); | |
| 260 EXPECT_EQ(kClientID, client_id_); | |
| 261 } | |
| 262 | |
| 263 TEST_F(CloudPolicyClientTest, RegistrationNoToken) { | |
| 264 registration_response_.mutable_register_response()-> | |
| 265 clear_device_management_token(); | |
| 266 ExpectRegistration(kOAuthToken); | |
| 267 EXPECT_CALL(observer_, OnClientError(_)); | |
| 268 client_->Register(em::DeviceRegisterRequest::USER, | |
| 269 kOAuthToken, std::string(), false, std::string()); | |
| 270 EXPECT_FALSE(client_->is_registered()); | |
| 271 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); | |
| 272 EXPECT_EQ(DM_STATUS_RESPONSE_DECODING_ERROR, client_->status()); | |
| 273 } | |
| 274 | |
| 275 TEST_F(CloudPolicyClientTest, RegistrationFailure) { | |
| 276 EXPECT_CALL(service_, | |
| 277 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION, | |
| 278 request_context_)) | |
| 279 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED)); | |
| 280 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _)); | |
| 281 EXPECT_CALL(observer_, OnClientError(_)); | |
| 282 client_->Register(em::DeviceRegisterRequest::USER, | |
| 283 kOAuthToken, std::string(), false, std::string()); | |
| 284 EXPECT_FALSE(client_->is_registered()); | |
| 285 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); | |
| 286 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status()); | |
| 287 } | |
| 288 | |
| 289 TEST_F(CloudPolicyClientTest, RetryRegistration) { | |
| 290 // First registration does not set the re-register flag. | |
| 291 EXPECT_FALSE( | |
| 292 registration_request_.mutable_register_request()->has_reregister()); | |
| 293 MockDeviceManagementJob* register_job = NULL; | |
| 294 EXPECT_CALL(service_, | |
| 295 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION, | |
| 296 request_context_)) | |
| 297 .WillOnce(service_.CreateAsyncJob(®ister_job)); | |
| 298 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestRegister, | |
| 299 "", kOAuthToken, "", "", _, | |
| 300 MatchProto(registration_request_))); | |
| 301 client_->Register(em::DeviceRegisterRequest::USER, | |
| 302 kOAuthToken, std::string(), false, std::string()); | |
| 303 EXPECT_FALSE(client_->is_registered()); | |
| 304 Mock::VerifyAndClearExpectations(&service_); | |
| 305 | |
| 306 // Simulate a retry callback before proceeding; the re-register flag is set. | |
| 307 registration_request_.mutable_register_request()->set_reregister(true); | |
| 308 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestRegister, | |
| 309 "", kOAuthToken, "", "", _, | |
| 310 MatchProto(registration_request_))); | |
| 311 register_job->RetryJob(); | |
| 312 Mock::VerifyAndClearExpectations(&service_); | |
| 313 | |
| 314 // Subsequent retries keep the flag set. | |
| 315 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestRegister, | |
| 316 "", kOAuthToken, "", "", _, | |
| 317 MatchProto(registration_request_))); | |
| 318 register_job->RetryJob(); | |
| 319 Mock::VerifyAndClearExpectations(&service_); | |
| 320 } | |
| 321 | |
| 322 TEST_F(CloudPolicyClientTest, PolicyUpdate) { | |
| 323 Register(); | |
| 324 | |
| 325 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | |
| 326 EXPECT_CALL(observer_, OnPolicyFetched(_)); | |
| 327 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | |
| 328 client_->FetchPolicy(); | |
| 329 CheckPolicyResponse(); | |
| 330 | |
| 331 policy_response_.mutable_policy_response()->clear_response(); | |
| 332 policy_response_.mutable_policy_response()->add_response()->set_policy_data( | |
| 333 CreatePolicyData("updated-fake-policy-data")); | |
| 334 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | |
| 335 EXPECT_CALL(observer_, OnPolicyFetched(_)); | |
| 336 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | |
| 337 client_->FetchPolicy(); | |
| 338 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); | |
| 339 CheckPolicyResponse(); | |
| 340 } | |
| 341 | |
| 342 TEST_F(CloudPolicyClientTest, PolicyFetchWithMetaData) { | |
| 343 Register(); | |
| 344 | |
| 345 const base::Time timestamp( | |
| 346 base::Time::UnixEpoch() + base::TimeDelta::FromDays(20)); | |
| 347 client_->set_submit_machine_id(true); | |
| 348 client_->set_last_policy_timestamp(timestamp); | |
| 349 client_->set_public_key_version(42); | |
| 350 em::PolicyFetchRequest* policy_fetch_request = | |
| 351 policy_request_.mutable_policy_request()->mutable_request(0); | |
| 352 policy_fetch_request->set_machine_id(kMachineID); | |
| 353 policy_fetch_request->set_timestamp( | |
| 354 (timestamp - base::Time::UnixEpoch()).InMilliseconds()); | |
| 355 policy_fetch_request->set_public_key_version(42); | |
| 356 | |
| 357 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | |
| 358 EXPECT_CALL(observer_, OnPolicyFetched(_)); | |
| 359 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | |
| 360 client_->FetchPolicy(); | |
| 361 CheckPolicyResponse(); | |
| 362 } | |
| 363 | |
| 364 TEST_F(CloudPolicyClientTest, PolicyFetchWithInvalidation) { | |
| 365 Register(); | |
| 366 | |
| 367 int64 previous_version = client_->fetched_invalidation_version(); | |
| 368 client_->SetInvalidationInfo(12345, "12345"); | |
| 369 EXPECT_EQ(previous_version, client_->fetched_invalidation_version()); | |
| 370 em::PolicyFetchRequest* policy_fetch_request = | |
| 371 policy_request_.mutable_policy_request()->mutable_request(0); | |
| 372 policy_fetch_request->set_invalidation_version(12345); | |
| 373 policy_fetch_request->set_invalidation_payload("12345"); | |
| 374 | |
| 375 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | |
| 376 EXPECT_CALL(observer_, OnPolicyFetched(_)); | |
| 377 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | |
| 378 client_->FetchPolicy(); | |
| 379 CheckPolicyResponse(); | |
| 380 EXPECT_EQ(12345, client_->fetched_invalidation_version()); | |
| 381 } | |
| 382 | |
| 383 TEST_F(CloudPolicyClientTest, PolicyFetchWithInvalidationNoPayload) { | |
| 384 Register(); | |
| 385 | |
| 386 int64 previous_version = client_->fetched_invalidation_version(); | |
| 387 client_->SetInvalidationInfo(-12345, std::string()); | |
| 388 EXPECT_EQ(previous_version, client_->fetched_invalidation_version()); | |
| 389 | |
| 390 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | |
| 391 EXPECT_CALL(observer_, OnPolicyFetched(_)); | |
| 392 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | |
| 393 client_->FetchPolicy(); | |
| 394 CheckPolicyResponse(); | |
| 395 EXPECT_EQ(-12345, client_->fetched_invalidation_version()); | |
| 396 } | |
| 397 | |
| 398 TEST_F(CloudPolicyClientTest, BadPolicyResponse) { | |
| 399 Register(); | |
| 400 | |
| 401 policy_response_.clear_policy_response(); | |
| 402 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | |
| 403 EXPECT_CALL(observer_, OnClientError(_)); | |
| 404 client_->FetchPolicy(); | |
| 405 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); | |
| 406 EXPECT_EQ(DM_STATUS_RESPONSE_DECODING_ERROR, client_->status()); | |
| 407 | |
| 408 policy_response_.mutable_policy_response()->add_response()->set_policy_data( | |
| 409 CreatePolicyData("fake-policy-data")); | |
| 410 policy_response_.mutable_policy_response()->add_response()->set_policy_data( | |
| 411 CreatePolicyData("excess-fake-policy-data")); | |
| 412 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | |
| 413 EXPECT_CALL(observer_, OnPolicyFetched(_)); | |
| 414 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | |
| 415 client_->FetchPolicy(); | |
| 416 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); | |
| 417 CheckPolicyResponse(); | |
| 418 } | |
| 419 | |
| 420 TEST_F(CloudPolicyClientTest, PolicyRequestFailure) { | |
| 421 Register(); | |
| 422 | |
| 423 EXPECT_CALL(service_, | |
| 424 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH, | |
| 425 request_context_)) | |
| 426 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED)); | |
| 427 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _)); | |
| 428 EXPECT_CALL(observer_, OnClientError(_)); | |
| 429 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()).Times(0); | |
| 430 client_->FetchPolicy(); | |
| 431 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status()); | |
| 432 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); | |
| 433 } | |
| 434 | |
| 435 TEST_F(CloudPolicyClientTest, Unregister) { | |
| 436 Register(); | |
| 437 | |
| 438 ExpectUnregistration(kDMToken); | |
| 439 EXPECT_CALL(observer_, OnRegistrationStateChanged(_)); | |
| 440 client_->Unregister(); | |
| 441 EXPECT_FALSE(client_->is_registered()); | |
| 442 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); | |
| 443 } | |
| 444 | |
| 445 TEST_F(CloudPolicyClientTest, UnregisterEmpty) { | |
| 446 Register(); | |
| 447 | |
| 448 unregistration_response_.clear_unregister_response(); | |
| 449 EXPECT_CALL(service_, | |
| 450 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION, | |
| 451 request_context_)) | |
| 452 .WillOnce(service_.SucceedJob(unregistration_response_)); | |
| 453 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _)); | |
| 454 EXPECT_CALL(observer_, OnRegistrationStateChanged(_)); | |
| 455 client_->Unregister(); | |
| 456 EXPECT_FALSE(client_->is_registered()); | |
| 457 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); | |
| 458 } | |
| 459 | |
| 460 TEST_F(CloudPolicyClientTest, UnregisterFailure) { | |
| 461 Register(); | |
| 462 | |
| 463 EXPECT_CALL(service_, | |
| 464 CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION, | |
| 465 request_context_)) | |
| 466 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED)); | |
| 467 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _)); | |
| 468 EXPECT_CALL(observer_, OnClientError(_)); | |
| 469 client_->Unregister(); | |
| 470 EXPECT_TRUE(client_->is_registered()); | |
| 471 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status()); | |
| 472 } | |
| 473 | |
| 474 TEST_F(CloudPolicyClientTest, PolicyFetchWithExtensionPolicy) { | |
| 475 Register(); | |
| 476 | |
| 477 // Setup the |expected_responses| and |policy_response_|. | |
| 478 static const char* kExtensions[] = { | |
| 479 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", | |
| 480 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", | |
| 481 "cccccccccccccccccccccccccccccccc", | |
| 482 }; | |
| 483 typedef std::map<PolicyNamespaceKey, em::PolicyFetchResponse> ResponseMap; | |
| 484 ResponseMap expected_responses; | |
| 485 std::set<PolicyNamespaceKey> expected_namespaces; | |
| 486 PolicyNamespaceKey key(dm_protocol::kChromeUserPolicyType, std::string()); | |
| 487 // Copy the user policy fetch request. | |
| 488 expected_responses[key].CopyFrom( | |
| 489 policy_response_.policy_response().response(0)); | |
| 490 expected_namespaces.insert(key); | |
| 491 key.first = dm_protocol::kChromeExtensionPolicyType; | |
| 492 for (size_t i = 0; i < arraysize(kExtensions); ++i) { | |
| 493 key.second = kExtensions[i]; | |
| 494 em::PolicyData policy_data; | |
| 495 policy_data.set_policy_type(key.first); | |
| 496 policy_data.set_settings_entity_id(key.second); | |
| 497 expected_responses[key].set_policy_data(policy_data.SerializeAsString()); | |
| 498 policy_response_.mutable_policy_response()->add_response()->CopyFrom( | |
| 499 expected_responses[key]); | |
| 500 expected_namespaces.insert(key); | |
| 501 } | |
| 502 | |
| 503 // Make a policy fetch. | |
| 504 EXPECT_CALL(service_, | |
| 505 CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH, | |
| 506 request_context_)) | |
| 507 .WillOnce(service_.SucceedJob(policy_response_)); | |
| 508 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestPolicy, "", "", | |
| 509 kDMToken, | |
| 510 dm_protocol::kValueUserAffiliationNone, | |
| 511 client_id_, _)) | |
| 512 .WillOnce(SaveArg<6>(&policy_request_)); | |
| 513 EXPECT_CALL(observer_, OnPolicyFetched(_)); | |
| 514 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | |
| 515 for (size_t i = 0; i < arraysize(kExtensions); ++i) { | |
| 516 client_->AddNamespaceToFetch(PolicyNamespaceKey( | |
| 517 dm_protocol::kChromeExtensionPolicyType, kExtensions[i])); | |
| 518 } | |
| 519 client_->FetchPolicy(); | |
| 520 | |
| 521 // Verify that the request includes the expected namespaces. | |
| 522 ASSERT_TRUE(policy_request_.has_policy_request()); | |
| 523 const em::DevicePolicyRequest& policy_request = | |
| 524 policy_request_.policy_request(); | |
| 525 ASSERT_EQ(static_cast<int>(1 + arraysize(kExtensions)), | |
| 526 policy_request.request_size()); | |
| 527 for (int i = 0; i < policy_request.request_size(); ++i) { | |
| 528 const em::PolicyFetchRequest& fetch_request = policy_request.request(i); | |
| 529 ASSERT_TRUE(fetch_request.has_policy_type()); | |
| 530 std::string entity_id; | |
| 531 if (fetch_request.has_settings_entity_id()) | |
| 532 entity_id = fetch_request.settings_entity_id(); | |
| 533 PolicyNamespaceKey key(fetch_request.policy_type(), entity_id); | |
| 534 EXPECT_EQ(1u, expected_namespaces.erase(key)); | |
| 535 } | |
| 536 EXPECT_TRUE(expected_namespaces.empty()); | |
| 537 | |
| 538 // Verify that the client got all the responses mapped to their namespaces. | |
| 539 for (ResponseMap::iterator it = expected_responses.begin(); | |
| 540 it != expected_responses.end(); ++it) { | |
| 541 const em::PolicyFetchResponse* response = client_->GetPolicyFor(it->first); | |
| 542 ASSERT_TRUE(response); | |
| 543 EXPECT_EQ(it->second.SerializeAsString(), response->SerializeAsString()); | |
| 544 } | |
| 545 } | |
| 546 | |
| 547 TEST_F(CloudPolicyClientTest, UploadCertificate) { | |
| 548 Register(); | |
| 549 | |
| 550 ExpectUploadCertificate(); | |
| 551 EXPECT_CALL(upload_certificate_observer_, OnUploadComplete(true)).Times(1); | |
| 552 CloudPolicyClient::StatusCallback callback = base::Bind( | |
| 553 &MockUploadCertificateObserver::OnUploadComplete, | |
| 554 base::Unretained(&upload_certificate_observer_)); | |
| 555 client_->UploadCertificate(kDeviceCertificate, callback); | |
| 556 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); | |
| 557 } | |
| 558 | |
| 559 TEST_F(CloudPolicyClientTest, UploadCertificateEmpty) { | |
| 560 Register(); | |
| 561 | |
| 562 upload_certificate_response_.clear_cert_upload_response(); | |
| 563 ExpectUploadCertificate(); | |
| 564 EXPECT_CALL(upload_certificate_observer_, OnUploadComplete(false)).Times(1); | |
| 565 CloudPolicyClient::StatusCallback callback = base::Bind( | |
| 566 &MockUploadCertificateObserver::OnUploadComplete, | |
| 567 base::Unretained(&upload_certificate_observer_)); | |
| 568 client_->UploadCertificate(kDeviceCertificate, callback); | |
| 569 EXPECT_EQ(DM_STATUS_SUCCESS, client_->status()); | |
| 570 } | |
| 571 | |
| 572 TEST_F(CloudPolicyClientTest, UploadCertificateFailure) { | |
| 573 Register(); | |
| 574 | |
| 575 EXPECT_CALL(upload_certificate_observer_, OnUploadComplete(false)).Times(1); | |
| 576 EXPECT_CALL(service_, | |
| 577 CreateJob(DeviceManagementRequestJob::TYPE_UPLOAD_CERTIFICATE, | |
| 578 request_context_)) | |
| 579 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED)); | |
| 580 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _)); | |
| 581 EXPECT_CALL(observer_, OnClientError(_)); | |
| 582 CloudPolicyClient::StatusCallback callback = base::Bind( | |
| 583 &MockUploadCertificateObserver::OnUploadComplete, | |
| 584 base::Unretained(&upload_certificate_observer_)); | |
| 585 client_->UploadCertificate(kDeviceCertificate, callback); | |
| 586 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status()); | |
| 587 } | |
| 588 | |
| 589 } // namespace policy | |
| OLD | NEW |