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