1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/policy/cloud/test_request_interceptor.h"
#include <limits>
#include <queue>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/memory/scoped_ptr.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_utils.h"
#include "content/test/net/url_request_mock_http_job.h"
#include "net/base/net_errors.h"
#include "net/base/upload_bytes_element_reader.h"
#include "net/base/upload_data_stream.h"
#include "net/base/upload_element_reader.h"
#include "net/url_request/url_request_error_job.h"
#include "net/url_request/url_request_filter.h"
#include "net/url_request/url_request_job_factory.h"
#include "net/url_request/url_request_test_job.h"
#include "url/gurl.h"
namespace em = enterprise_management;
namespace policy {
namespace {
// Helper to execute a |task| on IO, and return only after it has completed.
void PostToIOAndWait(const base::Closure& task) {
content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, task);
content::RunAllPendingInMessageLoop(content::BrowserThread::IO);
}
// Helper callback for jobs that should fail with a network |error|.
net::URLRequestJob* ErrorJobCallback(int error,
net::URLRequest* request,
net::NetworkDelegate* network_delegate) {
return new net::URLRequestErrorJob(request, network_delegate, error);
}
// Helper callback for jobs that should fail with a 400 HTTP error.
net::URLRequestJob* BadRequestJobCallback(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) {
static const char kBadHeaders[] =
"HTTP/1.1 400 Bad request\0"
"Content-type: application/protobuf\0"
"\0";
std::string headers(kBadHeaders, arraysize(kBadHeaders));
return new net::URLRequestTestJob(
request, network_delegate, headers, std::string(), true);
}
net::URLRequestJob* FileJobCallback(const base::FilePath& file_path,
net::URLRequest* request,
net::NetworkDelegate* network_delegate) {
return new content::URLRequestMockHTTPJob(
request,
network_delegate,
file_path);
}
// Parses the upload data in |request| into |request_msg|, and validates the
// request. The query string in the URL must contain the |expected_type| for
// the "request" parameter. Returns true if all checks succeeded, and the
// request data has been parsed into |request_msg|.
bool ValidRequest(net::URLRequest* request,
const std::string& expected_type,
em::DeviceManagementRequest* request_msg) {
if (request->method() != "POST")
return false;
std::string spec = request->url().spec();
if (spec.find("request=" + expected_type) == std::string::npos)
return false;
// This assumes that the payload data was set from a single string. In that
// case the UploadDataStream has a single UploadBytesElementReader with the
// data in memory.
const net::UploadDataStream* stream = request->get_upload();
if (!stream)
return false;
const ScopedVector<net::UploadElementReader>& readers =
stream->element_readers();
if (readers.size() != 1u)
return false;
const net::UploadBytesElementReader* reader = readers[0]->AsBytesReader();
if (!reader)
return false;
std::string data(reader->bytes(), reader->length());
if (!request_msg->ParseFromString(data))
return false;
return true;
}
// Helper callback for register jobs that should suceed. Validates the request
// parameters and returns an appropriate response job. If |expect_reregister|
// is true then the reregister flag must be set in the DeviceRegisterRequest
// protobuf.
net::URLRequestJob* RegisterJobCallback(
em::DeviceRegisterRequest::Type expected_type,
bool expect_reregister,
net::URLRequest* request,
net::NetworkDelegate* network_delegate) {
em::DeviceManagementRequest request_msg;
if (!ValidRequest(request, "register", &request_msg))
return BadRequestJobCallback(request, network_delegate);
if (!request_msg.has_register_request() ||
request_msg.has_unregister_request() ||
request_msg.has_policy_request() ||
request_msg.has_device_status_report_request() ||
request_msg.has_session_status_report_request() ||
request_msg.has_auto_enrollment_request()) {
return BadRequestJobCallback(request, network_delegate);
}
const em::DeviceRegisterRequest& register_request =
request_msg.register_request();
if (expect_reregister &&
(!register_request.has_reregister() || !register_request.reregister())) {
return BadRequestJobCallback(request, network_delegate);
} else if (!expect_reregister &&
register_request.has_reregister() &&
register_request.reregister()) {
return BadRequestJobCallback(request, network_delegate);
}
if (!register_request.has_type() || register_request.type() != expected_type)
return BadRequestJobCallback(request, network_delegate);
em::DeviceManagementResponse response;
em::DeviceRegisterResponse* register_response =
response.mutable_register_response();
register_response->set_device_management_token("s3cr3t70k3n");
std::string data;
response.SerializeToString(&data);
static const char kGoodHeaders[] =
"HTTP/1.1 200 OK\0"
"Content-type: application/protobuf\0"
"\0";
std::string headers(kGoodHeaders, arraysize(kGoodHeaders));
return new net::URLRequestTestJob(
request, network_delegate, headers, data, true);
}
} // namespace
class TestRequestInterceptor::Delegate
: public net::URLRequestJobFactory::ProtocolHandler {
public:
explicit Delegate(const std::string& hostname);
virtual ~Delegate();
// ProtocolHandler implementation:
virtual net::URLRequestJob* MaybeCreateJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const OVERRIDE;
void GetPendingSize(size_t* pending_size) const;
void PushJobCallback(const JobCallback& callback);
private:
const std::string hostname_;
// The queue of pending callbacks. 'mutable' because MaybeCreateJob() is a
// const method; it can't reenter though, because it runs exclusively on
// the IO thread.
mutable std::queue<JobCallback> pending_job_callbacks_;
};
TestRequestInterceptor::Delegate::Delegate(const std::string& hostname)
: hostname_(hostname) {}
TestRequestInterceptor::Delegate::~Delegate() {}
net::URLRequestJob* TestRequestInterceptor::Delegate::MaybeCreateJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const {
CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
if (request->url().host() != hostname_) {
// Reject requests to other servers.
return ErrorJobCallback(
net::ERR_CONNECTION_REFUSED, request, network_delegate);
}
if (pending_job_callbacks_.empty()) {
// Reject dmserver requests by default.
return BadRequestJobCallback(request, network_delegate);
}
JobCallback callback = pending_job_callbacks_.front();
pending_job_callbacks_.pop();
return callback.Run(request, network_delegate);
}
void TestRequestInterceptor::Delegate::GetPendingSize(
size_t* pending_size) const {
CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
*pending_size = pending_job_callbacks_.size();
}
void TestRequestInterceptor::Delegate::PushJobCallback(
const JobCallback& callback) {
CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
pending_job_callbacks_.push(callback);
}
TestRequestInterceptor::TestRequestInterceptor(const std::string& hostname)
: hostname_(hostname) {
delegate_ = new Delegate(hostname_);
scoped_ptr<net::URLRequestJobFactory::ProtocolHandler> handler(delegate_);
PostToIOAndWait(
base::Bind(&net::URLRequestFilter::AddHostnameProtocolHandler,
base::Unretained(net::URLRequestFilter::GetInstance()),
"http", hostname_, base::Passed(&handler)));
}
TestRequestInterceptor::~TestRequestInterceptor() {
// RemoveHostnameHandler() destroys the |delegate_|, which is owned by
// the URLRequestFilter.
delegate_ = NULL;
PostToIOAndWait(
base::Bind(&net::URLRequestFilter::RemoveHostnameHandler,
base::Unretained(net::URLRequestFilter::GetInstance()),
"http", hostname_));
}
size_t TestRequestInterceptor::GetPendingSize() const {
size_t pending_size = std::numeric_limits<size_t>::max();
PostToIOAndWait(base::Bind(&Delegate::GetPendingSize,
base::Unretained(delegate_),
&pending_size));
return pending_size;
}
void TestRequestInterceptor::PushJobCallback(const JobCallback& callback) {
PostToIOAndWait(base::Bind(&Delegate::PushJobCallback,
base::Unretained(delegate_),
callback));
}
// static
TestRequestInterceptor::JobCallback TestRequestInterceptor::ErrorJob(
int error) {
return base::Bind(&ErrorJobCallback, error);
}
// static
TestRequestInterceptor::JobCallback TestRequestInterceptor::BadRequestJob() {
return base::Bind(&BadRequestJobCallback);
}
// static
TestRequestInterceptor::JobCallback TestRequestInterceptor::RegisterJob(
em::DeviceRegisterRequest::Type expected_type,
bool expect_reregister) {
return base::Bind(&RegisterJobCallback, expected_type, expect_reregister);
}
// static
TestRequestInterceptor::JobCallback TestRequestInterceptor::FileJob(
const base::FilePath& file_path) {
return base::Bind(&FileJobCallback, file_path);
}
} // namespace policy
|