This source file includes following definitions.
- ErrorJobCallback
- BadRequestJobCallback
- FileJobCallback
- ValidRequest
- RegisterJobCallback
- io_task_runner_
- MaybeCreateJob
- GetPendingSize
- PushJobCallback
- io_task_runner_
- GetPendingSize
- PushJobCallback
- ErrorJob
- BadRequestJob
- RegisterJob
- FileJob
- PostToIOAndWait
#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 "base/run_loop.h"
#include "base/sequenced_task_runner.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 {
net::URLRequestJob* ErrorJobCallback(int error,
net::URLRequest* request,
net::NetworkDelegate* network_delegate) {
return new net::URLRequestErrorJob(request, network_delegate, 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);
}
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;
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;
}
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);
}
}
class TestRequestInterceptor::Delegate
: public net::URLRequestJobFactory::ProtocolHandler {
public:
Delegate(const std::string& hostname,
scoped_refptr<base::SequencedTaskRunner> io_task_runner);
virtual ~Delegate();
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_;
scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
mutable std::queue<JobCallback> pending_job_callbacks_;
};
TestRequestInterceptor::Delegate::Delegate(
const std::string& hostname,
scoped_refptr<base::SequencedTaskRunner> io_task_runner)
: hostname_(hostname), io_task_runner_(io_task_runner) {}
TestRequestInterceptor::Delegate::~Delegate() {}
net::URLRequestJob* TestRequestInterceptor::Delegate::MaybeCreateJob(
net::URLRequest* request,
net::NetworkDelegate* network_delegate) const {
CHECK(io_task_runner_->RunsTasksOnCurrentThread());
if (request->url().host() != hostname_) {
return ErrorJobCallback(
net::ERR_CONNECTION_REFUSED, request, network_delegate);
}
if (pending_job_callbacks_.empty()) {
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(io_task_runner_->RunsTasksOnCurrentThread());
*pending_size = pending_job_callbacks_.size();
}
void TestRequestInterceptor::Delegate::PushJobCallback(
const JobCallback& callback) {
CHECK(io_task_runner_->RunsTasksOnCurrentThread());
pending_job_callbacks_.push(callback);
}
TestRequestInterceptor::TestRequestInterceptor(const std::string& hostname,
scoped_refptr<base::SequencedTaskRunner> io_task_runner)
: hostname_(hostname),
io_task_runner_(io_task_runner) {
delegate_ = new Delegate(hostname_, io_task_runner_);
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() {
delegate_ = NULL;
PostToIOAndWait(
base::Bind(&net::URLRequestFilter::RemoveHostnameHandler,
base::Unretained(net::URLRequestFilter::GetInstance()),
"http", hostname_));
}
size_t TestRequestInterceptor::GetPendingSize() {
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));
}
TestRequestInterceptor::JobCallback TestRequestInterceptor::ErrorJob(
int error) {
return base::Bind(&ErrorJobCallback, error);
}
TestRequestInterceptor::JobCallback TestRequestInterceptor::BadRequestJob() {
return base::Bind(&BadRequestJobCallback);
}
TestRequestInterceptor::JobCallback TestRequestInterceptor::RegisterJob(
em::DeviceRegisterRequest::Type expected_type,
bool expect_reregister) {
return base::Bind(&RegisterJobCallback, expected_type, expect_reregister);
}
TestRequestInterceptor::JobCallback TestRequestInterceptor::FileJob(
const base::FilePath& file_path) {
return base::Bind(&FileJobCallback, file_path);
}
void TestRequestInterceptor::PostToIOAndWait(const base::Closure& task) {
io_task_runner_->PostTask(FROM_HERE, task);
base::RunLoop run_loop;
io_task_runner_->PostTask(
FROM_HERE,
base::Bind(
base::IgnoreResult(&base::MessageLoopProxy::PostTask),
base::MessageLoopProxy::current(),
FROM_HERE,
run_loop.QuitClosure()));
run_loop.Run();
}
}