OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/policy/cloud_policy_client.h" | 5 #include "chrome/browser/policy/cloud_policy_client.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "chrome/browser/policy/mock_cloud_policy_client.h" |
12 #include "chrome/browser/policy/mock_device_management_service.h" | 13 #include "chrome/browser/policy/mock_device_management_service.h" |
13 #include "chrome/browser/policy/proto/device_management_backend.pb.h" | 14 #include "chrome/browser/policy/proto/device_management_backend.pb.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
| 18 using testing::Mock; |
17 using testing::Return; | 19 using testing::Return; |
18 using testing::SaveArg; | 20 using testing::SaveArg; |
19 using testing::StrictMock; | 21 using testing::StrictMock; |
20 using testing::_; | 22 using testing::_; |
21 | 23 |
22 namespace em = enterprise_management; | 24 namespace em = enterprise_management; |
23 | 25 |
24 namespace policy { | 26 namespace policy { |
25 | 27 |
26 namespace { | 28 namespace { |
27 | 29 |
28 const char kClientID[] = "fake-client-id"; | 30 const char kClientID[] = "fake-client-id"; |
29 const char kMachineID[] = "fake-machine-id"; | 31 const char kMachineID[] = "fake-machine-id"; |
30 const char kMachineModel[] = "fake-machine-model"; | 32 const char kMachineModel[] = "fake-machine-model"; |
31 const char kOAuthToken[] = "fake-oauth-token"; | 33 const char kOAuthToken[] = "fake-oauth-token"; |
32 const char kDMToken[] = "fake-dm-token"; | 34 const char kDMToken[] = "fake-dm-token"; |
33 | 35 |
34 class MockObserver : public CloudPolicyClient::Observer { | |
35 public: | |
36 MockObserver() {} | |
37 virtual ~MockObserver() {} | |
38 | |
39 MOCK_METHOD1(OnPolicyFetched, void(CloudPolicyClient*)); | |
40 MOCK_METHOD1(OnRegistrationStateChanged, void(CloudPolicyClient*)); | |
41 MOCK_METHOD1(OnClientError, void(CloudPolicyClient*)); | |
42 | |
43 private: | |
44 DISALLOW_COPY_AND_ASSIGN(MockObserver); | |
45 }; | |
46 | |
47 class MockStatusProvider : public CloudPolicyClient::StatusProvider { | 36 class MockStatusProvider : public CloudPolicyClient::StatusProvider { |
48 public: | 37 public: |
49 MockStatusProvider() {} | 38 MockStatusProvider() {} |
50 virtual ~MockStatusProvider() {} | 39 virtual ~MockStatusProvider() {} |
51 | 40 |
52 MOCK_METHOD1(GetDeviceStatus, bool(em::DeviceStatusReportRequest* status)); | 41 MOCK_METHOD1(GetDeviceStatus, bool(em::DeviceStatusReportRequest* status)); |
53 MOCK_METHOD1(GetSessionStatus, bool(em::SessionStatusReportRequest* status)); | 42 MOCK_METHOD1(GetSessionStatus, bool(em::SessionStatusReportRequest* status)); |
54 MOCK_METHOD0(OnSubmittedSuccessfully, void(void)); | 43 MOCK_METHOD0(OnSubmittedSuccessfully, void(void)); |
55 | 44 |
56 private: | 45 private: |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
169 | 158 |
170 // Protobufs used in successful responses. | 159 // Protobufs used in successful responses. |
171 em::DeviceManagementResponse registration_response_; | 160 em::DeviceManagementResponse registration_response_; |
172 em::DeviceManagementResponse policy_response_; | 161 em::DeviceManagementResponse policy_response_; |
173 em::DeviceManagementResponse unregistration_response_; | 162 em::DeviceManagementResponse unregistration_response_; |
174 | 163 |
175 std::string client_id_; | 164 std::string client_id_; |
176 PolicyNamespaceKey policy_ns_key_; | 165 PolicyNamespaceKey policy_ns_key_; |
177 MockDeviceManagementService service_; | 166 MockDeviceManagementService service_; |
178 StrictMock<MockStatusProvider> status_provider_; | 167 StrictMock<MockStatusProvider> status_provider_; |
179 StrictMock<MockObserver> observer_; | 168 StrictMock<MockCloudPolicyClientObserver> observer_; |
180 scoped_ptr<CloudPolicyClient> client_; | 169 scoped_ptr<CloudPolicyClient> client_; |
181 }; | 170 }; |
182 | 171 |
183 TEST_F(CloudPolicyClientTest, Init) { | 172 TEST_F(CloudPolicyClientTest, Init) { |
184 EXPECT_CALL(service_, CreateJob(_)).Times(0); | 173 EXPECT_CALL(service_, CreateJob(_)).Times(0); |
185 EXPECT_FALSE(client_->is_registered()); | 174 EXPECT_FALSE(client_->is_registered()); |
186 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); | 175 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); |
187 } | 176 } |
188 | 177 |
189 TEST_F(CloudPolicyClientTest, SetupRegistrationAndPolicyFetch) { | 178 TEST_F(CloudPolicyClientTest, SetupRegistrationAndPolicyFetch) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED)); | 235 .WillOnce(service_.FailJob(DM_STATUS_REQUEST_FAILED)); |
247 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _)); | 236 EXPECT_CALL(service_, StartJob(_, _, _, _, _, _, _)); |
248 EXPECT_CALL(observer_, OnClientError(_)); | 237 EXPECT_CALL(observer_, OnClientError(_)); |
249 client_->Register(em::DeviceRegisterRequest::USER, | 238 client_->Register(em::DeviceRegisterRequest::USER, |
250 kOAuthToken, std::string(), false); | 239 kOAuthToken, std::string(), false); |
251 EXPECT_FALSE(client_->is_registered()); | 240 EXPECT_FALSE(client_->is_registered()); |
252 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); | 241 EXPECT_FALSE(client_->GetPolicyFor(policy_ns_key_)); |
253 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status()); | 242 EXPECT_EQ(DM_STATUS_REQUEST_FAILED, client_->status()); |
254 } | 243 } |
255 | 244 |
| 245 TEST_F(CloudPolicyClientTest, RetryRegistration) { |
| 246 // First registration does not set the re-register flag. |
| 247 EXPECT_FALSE( |
| 248 registration_request_.mutable_register_request()->has_reregister()); |
| 249 MockDeviceManagementJob* register_job = NULL; |
| 250 EXPECT_CALL(service_, |
| 251 CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION)) |
| 252 .WillOnce(service_.CreateAsyncJob(®ister_job)); |
| 253 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestRegister, |
| 254 "", kOAuthToken, "", "", _, |
| 255 MatchProto(registration_request_))); |
| 256 client_->Register(em::DeviceRegisterRequest::USER, |
| 257 kOAuthToken, std::string(), false); |
| 258 EXPECT_FALSE(client_->is_registered()); |
| 259 Mock::VerifyAndClearExpectations(&service_); |
| 260 |
| 261 // Simulate a retry callback before proceeding; the re-register flag is set. |
| 262 registration_request_.mutable_register_request()->set_reregister(true); |
| 263 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestRegister, |
| 264 "", kOAuthToken, "", "", _, |
| 265 MatchProto(registration_request_))); |
| 266 register_job->RetryJob(); |
| 267 Mock::VerifyAndClearExpectations(&service_); |
| 268 |
| 269 // Subsequent retries keep the flag set. |
| 270 EXPECT_CALL(service_, StartJob(dm_protocol::kValueRequestRegister, |
| 271 "", kOAuthToken, "", "", _, |
| 272 MatchProto(registration_request_))); |
| 273 register_job->RetryJob(); |
| 274 Mock::VerifyAndClearExpectations(&service_); |
| 275 } |
| 276 |
256 TEST_F(CloudPolicyClientTest, PolicyUpdate) { | 277 TEST_F(CloudPolicyClientTest, PolicyUpdate) { |
257 Register(); | 278 Register(); |
258 | 279 |
259 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); | 280 ExpectPolicyFetch(kDMToken, dm_protocol::kValueUserAffiliationNone); |
260 EXPECT_CALL(observer_, OnPolicyFetched(_)); | 281 EXPECT_CALL(observer_, OnPolicyFetched(_)); |
261 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); | 282 EXPECT_CALL(status_provider_, OnSubmittedSuccessfully()); |
262 client_->FetchPolicy(); | 283 client_->FetchPolicy(); |
263 CheckPolicyResponse(); | 284 CheckPolicyResponse(); |
264 | 285 |
265 policy_response_.mutable_policy_response()->clear_response(); | 286 policy_response_.mutable_policy_response()->clear_response(); |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
434 // Verify that the client got all the responses mapped to their namespaces. | 455 // Verify that the client got all the responses mapped to their namespaces. |
435 for (ResponseMap::iterator it = expected_responses.begin(); | 456 for (ResponseMap::iterator it = expected_responses.begin(); |
436 it != expected_responses.end(); ++it) { | 457 it != expected_responses.end(); ++it) { |
437 const em::PolicyFetchResponse* response = client_->GetPolicyFor(it->first); | 458 const em::PolicyFetchResponse* response = client_->GetPolicyFor(it->first); |
438 ASSERT_TRUE(response); | 459 ASSERT_TRUE(response); |
439 EXPECT_EQ(it->second.SerializeAsString(), response->SerializeAsString()); | 460 EXPECT_EQ(it->second.SerializeAsString(), response->SerializeAsString()); |
440 } | 461 } |
441 } | 462 } |
442 | 463 |
443 } // namespace policy | 464 } // namespace policy |
OLD | NEW |