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.
6 #include "base/bind_helpers.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/stl_util.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/policy/cloud/test_request_interceptor.h"
12 #include "chrome/browser/policy/test/local_policy_test_server.h"
13 #include "chrome/test/base/in_process_browser_test.h"
14 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
15 #include "components/policy/core/common/cloud/device_management_service.h"
16 #include "components/policy/core/common/cloud/mock_device_management_service.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "net/base/upload_bytes_element_reader.h"
19 #include "net/base/upload_data_stream.h"
20 #include "net/url_request/url_fetcher.h"
21 #include "net/url_request/url_request.h"
22 #include "net/url_request/url_request_context_getter.h"
23 #include "net/url_request/url_request_test_job.h"
24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 using content::BrowserThread
;
29 using testing::Invoke
;
30 using testing::InvokeWithoutArgs
;
33 namespace em
= enterprise_management
;
39 // Parses the DeviceManagementRequest in |request_data| and writes a serialized
40 // DeviceManagementResponse to |response_data|.
41 void ConstructResponse(const char* request_data
,
42 uint64 request_data_length
,
43 std::string
* response_data
) {
44 em::DeviceManagementRequest request
;
45 ASSERT_TRUE(request
.ParseFromArray(request_data
, request_data_length
));
46 em::DeviceManagementResponse response
;
47 if (request
.has_register_request()) {
48 response
.mutable_register_response()->set_device_management_token(
50 } else if (request
.has_service_api_access_request()) {
51 response
.mutable_service_api_access_response()->set_auth_code(
53 } else if (request
.has_unregister_request()) {
54 response
.mutable_unregister_response();
55 } else if (request
.has_policy_request()) {
56 response
.mutable_policy_response()->add_response();
57 } else if (request
.has_auto_enrollment_request()) {
58 response
.mutable_auto_enrollment_response();
60 FAIL() << "Failed to parse request.";
62 ASSERT_TRUE(response
.SerializeToString(response_data
));
65 // JobCallback for the interceptor.
66 net::URLRequestJob
* ResponseJob(
67 net::URLRequest
* request
,
68 net::NetworkDelegate
* network_delegate
) {
69 const net::UploadDataStream
* upload
= request
->get_upload();
71 upload
->GetElementReaders() &&
72 upload
->GetElementReaders()->size() == 1 &&
73 (*upload
->GetElementReaders())[0]->AsBytesReader()) {
74 std::string response_data
;
75 const net::UploadBytesElementReader
* bytes_reader
=
76 (*upload
->GetElementReaders())[0]->AsBytesReader();
77 ConstructResponse(bytes_reader
->bytes(),
78 bytes_reader
->length(),
80 return new net::URLRequestTestJob(
83 net::URLRequestTestJob::test_headers(),
93 class DeviceManagementServiceIntegrationTest
94 : public InProcessBrowserTest
,
95 public testing::WithParamInterface
<
96 std::string (DeviceManagementServiceIntegrationTest::*)(void)> {
98 MOCK_METHOD3(OnJobDone
, void(DeviceManagementStatus
, int,
99 const em::DeviceManagementResponse
&));
101 std::string
InitCannedResponse() {
102 interceptor_
.reset(new TestRequestInterceptor(
104 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
)));
105 return "http://localhost";
108 std::string
InitTestServer() {
110 return test_server_
->GetServiceURL().spec();
113 void RecordAuthCode(DeviceManagementStatus status
,
115 const em::DeviceManagementResponse
& response
) {
116 robot_auth_code_
= response
.service_api_access_response().auth_code();
120 void ExpectRequest() {
122 interceptor_
->PushJobCallback(base::Bind(&ResponseJob
));
125 void PerformRegistration() {
127 EXPECT_CALL(*this, OnJobDone(DM_STATUS_SUCCESS
, _
, _
))
130 &DeviceManagementServiceIntegrationTest::RecordToken
),
131 InvokeWithoutArgs(base::MessageLoop::current(),
132 &base::MessageLoop::Quit
)));
133 scoped_ptr
<DeviceManagementRequestJob
> job(
134 service_
->CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION
,
135 g_browser_process
->system_request_context()));
136 job
->SetGaiaToken("gaia_auth_token");
137 job
->SetOAuthToken("oauth_token");
138 job
->SetClientID("testid");
139 job
->GetRequest()->mutable_register_request();
140 job
->Start(base::Bind(&DeviceManagementServiceIntegrationTest::OnJobDone
,
141 base::Unretained(this)));
142 base::MessageLoop::current()->Run();
145 void SetUpOnMainThread() override
{
146 std::string
service_url((this->*(GetParam()))());
147 service_
.reset(new DeviceManagementService(
148 scoped_ptr
<DeviceManagementService::Configuration
>(
149 new MockDeviceManagementServiceConfiguration(service_url
))));
150 service_
->ScheduleInitialization(0);
153 void TearDownOnMainThread() override
{
155 test_server_
.reset();
156 interceptor_
.reset();
159 void StartTestServer() {
161 new LocalPolicyTestServer("device_management_service_browsertest"));
162 ASSERT_TRUE(test_server_
->Start());
165 void RecordToken(DeviceManagementStatus status
,
167 const em::DeviceManagementResponse
& response
) {
168 token_
= response
.register_response().device_management_token();
172 std::string robot_auth_code_
;
173 scoped_ptr
<DeviceManagementService
> service_
;
174 scoped_ptr
<LocalPolicyTestServer
> test_server_
;
175 scoped_ptr
<TestRequestInterceptor
> interceptor_
;
178 IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest
, Registration
) {
179 PerformRegistration();
180 EXPECT_FALSE(token_
.empty());
183 IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest
,
185 PerformRegistration();
188 EXPECT_CALL(*this, OnJobDone(DM_STATUS_SUCCESS
, _
, _
))
191 &DeviceManagementServiceIntegrationTest::RecordAuthCode
),
192 InvokeWithoutArgs(base::MessageLoop::current(),
193 &base::MessageLoop::Quit
)));
194 scoped_ptr
<DeviceManagementRequestJob
> job(service_
->CreateJob(
195 DeviceManagementRequestJob::TYPE_API_AUTH_CODE_FETCH
,
196 g_browser_process
->system_request_context()));
197 job
->SetDMToken(token_
);
198 job
->SetClientID("testid");
199 em::DeviceServiceApiAccessRequest
* request
=
200 job
->GetRequest()->mutable_service_api_access_request();
201 request
->add_auth_scope("authScope4Test");
202 request
->set_oauth2_client_id("oauth2ClientId4Test");
203 job
->Start(base::Bind(&DeviceManagementServiceIntegrationTest::OnJobDone
,
204 base::Unretained(this)));
205 base::MessageLoop::current()->Run();
206 ASSERT_EQ("fake_auth_code", robot_auth_code_
);
209 IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest
, PolicyFetch
) {
210 PerformRegistration();
213 EXPECT_CALL(*this, OnJobDone(DM_STATUS_SUCCESS
, _
, _
))
214 .WillOnce(InvokeWithoutArgs(base::MessageLoop::current(),
215 &base::MessageLoop::Quit
));
216 scoped_ptr
<DeviceManagementRequestJob
> job(
217 service_
->CreateJob(DeviceManagementRequestJob::TYPE_POLICY_FETCH
,
218 g_browser_process
->system_request_context()));
219 job
->SetDMToken(token_
);
220 job
->SetClientID("testid");
221 em::DevicePolicyRequest
* request
=
222 job
->GetRequest()->mutable_policy_request();
223 request
->add_request()->set_policy_type(dm_protocol::kChromeUserPolicyType
);
224 job
->Start(base::Bind(&DeviceManagementServiceIntegrationTest::OnJobDone
,
225 base::Unretained(this)));
226 base::MessageLoop::current()->Run();
229 IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest
, Unregistration
) {
230 PerformRegistration();
233 EXPECT_CALL(*this, OnJobDone(DM_STATUS_SUCCESS
, _
, _
))
234 .WillOnce(InvokeWithoutArgs(base::MessageLoop::current(),
235 &base::MessageLoop::Quit
));
236 scoped_ptr
<DeviceManagementRequestJob
> job(
237 service_
->CreateJob(DeviceManagementRequestJob::TYPE_UNREGISTRATION
,
238 g_browser_process
->system_request_context()));
239 job
->SetDMToken(token_
);
240 job
->SetClientID("testid");
241 job
->GetRequest()->mutable_unregister_request();
242 job
->Start(base::Bind(&DeviceManagementServiceIntegrationTest::OnJobDone
,
243 base::Unretained(this)));
244 base::MessageLoop::current()->Run();
247 IN_PROC_BROWSER_TEST_P(DeviceManagementServiceIntegrationTest
, AutoEnrollment
) {
249 EXPECT_CALL(*this, OnJobDone(DM_STATUS_SUCCESS
, _
, _
))
250 .WillOnce(InvokeWithoutArgs(base::MessageLoop::current(),
251 &base::MessageLoop::Quit
));
252 scoped_ptr
<DeviceManagementRequestJob
> job(
253 service_
->CreateJob(DeviceManagementRequestJob::TYPE_AUTO_ENROLLMENT
,
254 g_browser_process
->system_request_context()));
255 job
->SetClientID("testid");
256 job
->GetRequest()->mutable_auto_enrollment_request()->set_remainder(0);
257 job
->GetRequest()->mutable_auto_enrollment_request()->set_modulus(1);
258 job
->Start(base::Bind(&DeviceManagementServiceIntegrationTest::OnJobDone
,
259 base::Unretained(this)));
260 base::MessageLoop::current()->Run();
263 INSTANTIATE_TEST_CASE_P(
264 DeviceManagementServiceIntegrationTestInstance
,
265 DeviceManagementServiceIntegrationTest
,
266 testing::Values(&DeviceManagementServiceIntegrationTest::InitCannedResponse
,
267 &DeviceManagementServiceIntegrationTest::InitTestServer
));
269 } // namespace policy