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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
// Copyright 2015 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 "net/proxy/mojo_proxy_resolver_impl.h"
#include <string>
#include <vector>
#include "base/run_loop.h"
#include "net/base/net_errors.h"
#include "net/proxy/mock_proxy_resolver.h"
#include "net/proxy/mojo_proxy_type_converters.h"
#include "net/proxy/proxy_info.h"
#include "net/proxy/proxy_resolver_v8_tracing.h"
#include "net/proxy/proxy_server.h"
#include "net/test/event_waiter.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
namespace net {
namespace {
class TestRequestClient : public interfaces::ProxyResolverRequestClient {
public:
enum Event {
RESULT_RECEIVED,
CONNECTION_ERROR,
};
explicit TestRequestClient(
mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request);
void WaitForResult();
Error error() { return error_; }
const mojo::Array<interfaces::ProxyServerPtr>& results() { return results_; }
EventWaiter<Event>& event_waiter() { return event_waiter_; }
private:
// interfaces::ProxyResolverRequestClient override.
void ReportResult(int32_t error,
mojo::Array<interfaces::ProxyServerPtr> results) override;
void Alert(const mojo::String& message) override;
void OnError(int32_t line_number, const mojo::String& message) override;
void ResolveDns(interfaces::HostResolverRequestInfoPtr request_info,
interfaces::HostResolverRequestClientPtr client) override;
// Mojo error handler.
void OnConnectionError();
bool done_ = false;
Error error_ = ERR_FAILED;
mojo::Array<interfaces::ProxyServerPtr> results_;
mojo::Binding<interfaces::ProxyResolverRequestClient> binding_;
EventWaiter<Event> event_waiter_;
};
TestRequestClient::TestRequestClient(
mojo::InterfaceRequest<interfaces::ProxyResolverRequestClient> request)
: binding_(this, request.Pass()) {
binding_.set_connection_error_handler(base::Bind(
&TestRequestClient::OnConnectionError, base::Unretained(this)));
}
void TestRequestClient::WaitForResult() {
if (done_)
return;
event_waiter_.WaitForEvent(RESULT_RECEIVED);
ASSERT_TRUE(done_);
}
void TestRequestClient::ReportResult(
int32_t error,
mojo::Array<interfaces::ProxyServerPtr> results) {
event_waiter_.NotifyEvent(RESULT_RECEIVED);
ASSERT_FALSE(done_);
error_ = static_cast<Error>(error);
results_ = results.Pass();
done_ = true;
}
void TestRequestClient::Alert(const mojo::String& message) {
}
void TestRequestClient::OnError(int32_t line_number,
const mojo::String& message) {
}
void TestRequestClient::ResolveDns(
interfaces::HostResolverRequestInfoPtr request_info,
interfaces::HostResolverRequestClientPtr client) {
}
void TestRequestClient::OnConnectionError() {
event_waiter_.NotifyEvent(CONNECTION_ERROR);
}
class MockProxyResolverV8Tracing : public ProxyResolverV8Tracing {
public:
struct Request {
GURL url;
ProxyInfo* results;
CompletionCallback callback;
bool cancelled = false;
};
MockProxyResolverV8Tracing() {}
// ProxyResolverV8Tracing overrides.
void GetProxyForURL(const GURL& url,
ProxyInfo* results,
const CompletionCallback& callback,
ProxyResolver::RequestHandle* request,
scoped_ptr<Bindings> bindings) override;
void CancelRequest(ProxyResolver::RequestHandle request_handle) override;
LoadState GetLoadState(ProxyResolver::RequestHandle request) const override;
// Wait until the mock resolver has received a CancelRequest call.
void WaitForCancel();
const std::vector<Request>& pending_requests() { return pending_requests_; }
private:
base::Closure cancel_callback_;
std::vector<Request> pending_requests_;
};
void MockProxyResolverV8Tracing::GetProxyForURL(
const GURL& url,
ProxyInfo* results,
const CompletionCallback& callback,
ProxyResolver::RequestHandle* request,
scoped_ptr<Bindings> bindings) {
pending_requests_.push_back(Request());
auto& pending_request = pending_requests_.back();
pending_request.url = url;
pending_request.results = results;
pending_request.callback = callback;
*request =
reinterpret_cast<ProxyResolver::RequestHandle>(pending_requests_.size());
}
void MockProxyResolverV8Tracing::CancelRequest(
ProxyResolver::RequestHandle request_handle) {
size_t id = reinterpret_cast<size_t>(request_handle) - 1;
pending_requests_[id].cancelled = true;
if (!cancel_callback_.is_null()) {
cancel_callback_.Run();
cancel_callback_.Reset();
}
}
LoadState MockProxyResolverV8Tracing::GetLoadState(
ProxyResolver::RequestHandle request) const {
return LOAD_STATE_RESOLVING_PROXY_FOR_URL;
}
void MockProxyResolverV8Tracing::WaitForCancel() {
while (std::find_if(pending_requests_.begin(), pending_requests_.end(),
[](const Request& request) {
return request.cancelled;
}) != pending_requests_.end()) {
base::RunLoop run_loop;
cancel_callback_ = run_loop.QuitClosure();
run_loop.Run();
}
}
} // namespace
class MojoProxyResolverImplTest : public testing::Test {
protected:
void SetUp() override {
scoped_ptr<MockProxyResolverV8Tracing> mock_resolver(
new MockProxyResolverV8Tracing);
mock_proxy_resolver_ = mock_resolver.get();
resolver_impl_.reset(new MojoProxyResolverImpl(mock_resolver.Pass()));
resolver_ = resolver_impl_.get();
}
MockProxyResolverV8Tracing* mock_proxy_resolver_;
scoped_ptr<MojoProxyResolverImpl> resolver_impl_;
interfaces::ProxyResolver* resolver_;
};
TEST_F(MojoProxyResolverImplTest, GetProxyForUrl) {
interfaces::ProxyResolverRequestClientPtr client_ptr;
TestRequestClient client(mojo::GetProxy(&client_ptr));
resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass());
ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size());
const MockProxyResolverV8Tracing::Request& request =
mock_proxy_resolver_->pending_requests()[0];
EXPECT_EQ(GURL("http://example.com"), request.url);
request.results->UsePacString(
"PROXY proxy.example.com:1; "
"SOCKS4 socks4.example.com:2; "
"SOCKS5 socks5.example.com:3; "
"HTTPS https.example.com:4; "
"QUIC quic.example.com:65000; "
"DIRECT");
request.callback.Run(OK);
client.WaitForResult();
EXPECT_EQ(OK, client.error());
std::vector<ProxyServer> servers =
client.results().To<std::vector<ProxyServer>>();
ASSERT_EQ(6u, servers.size());
EXPECT_EQ(ProxyServer::SCHEME_HTTP, servers[0].scheme());
EXPECT_EQ("proxy.example.com", servers[0].host_port_pair().host());
EXPECT_EQ(1, servers[0].host_port_pair().port());
EXPECT_EQ(ProxyServer::SCHEME_SOCKS4, servers[1].scheme());
EXPECT_EQ("socks4.example.com", servers[1].host_port_pair().host());
EXPECT_EQ(2, servers[1].host_port_pair().port());
EXPECT_EQ(ProxyServer::SCHEME_SOCKS5, servers[2].scheme());
EXPECT_EQ("socks5.example.com", servers[2].host_port_pair().host());
EXPECT_EQ(3, servers[2].host_port_pair().port());
EXPECT_EQ(ProxyServer::SCHEME_HTTPS, servers[3].scheme());
EXPECT_EQ("https.example.com", servers[3].host_port_pair().host());
EXPECT_EQ(4, servers[3].host_port_pair().port());
EXPECT_EQ(ProxyServer::SCHEME_QUIC, servers[4].scheme());
EXPECT_EQ("quic.example.com", servers[4].host_port_pair().host());
EXPECT_EQ(65000, servers[4].host_port_pair().port());
EXPECT_EQ(ProxyServer::SCHEME_DIRECT, servers[5].scheme());
}
TEST_F(MojoProxyResolverImplTest, GetProxyForUrlFailure) {
interfaces::ProxyResolverRequestClientPtr client_ptr;
TestRequestClient client(mojo::GetProxy(&client_ptr));
resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass());
ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size());
const MockProxyResolverV8Tracing::Request& request =
mock_proxy_resolver_->pending_requests()[0];
EXPECT_EQ(GURL("http://example.com"), request.url);
request.callback.Run(ERR_FAILED);
client.WaitForResult();
EXPECT_EQ(ERR_FAILED, client.error());
std::vector<ProxyServer> proxy_servers =
client.results().To<std::vector<ProxyServer>>();
EXPECT_TRUE(proxy_servers.empty());
}
TEST_F(MojoProxyResolverImplTest, GetProxyForUrlMultiple) {
interfaces::ProxyResolverRequestClientPtr client_ptr1;
TestRequestClient client1(mojo::GetProxy(&client_ptr1));
interfaces::ProxyResolverRequestClientPtr client_ptr2;
TestRequestClient client2(mojo::GetProxy(&client_ptr2));
resolver_->GetProxyForUrl("http://example.com", client_ptr1.Pass());
resolver_->GetProxyForUrl("https://example.com", client_ptr2.Pass());
ASSERT_EQ(2u, mock_proxy_resolver_->pending_requests().size());
const MockProxyResolverV8Tracing::Request& request1 =
mock_proxy_resolver_->pending_requests()[0];
EXPECT_EQ(GURL("http://example.com"), request1.url);
const MockProxyResolverV8Tracing::Request& request2 =
mock_proxy_resolver_->pending_requests()[1];
EXPECT_EQ(GURL("https://example.com"), request2.url);
request1.results->UsePacString("HTTPS proxy.example.com:12345");
request1.callback.Run(OK);
request2.results->UsePacString("SOCKS5 another-proxy.example.com:6789");
request2.callback.Run(OK);
client1.WaitForResult();
client2.WaitForResult();
EXPECT_EQ(OK, client1.error());
std::vector<ProxyServer> proxy_servers1 =
client1.results().To<std::vector<ProxyServer>>();
ASSERT_EQ(1u, proxy_servers1.size());
ProxyServer& server1 = proxy_servers1[0];
EXPECT_EQ(ProxyServer::SCHEME_HTTPS, server1.scheme());
EXPECT_EQ("proxy.example.com", server1.host_port_pair().host());
EXPECT_EQ(12345, server1.host_port_pair().port());
EXPECT_EQ(OK, client2.error());
std::vector<ProxyServer> proxy_servers2 =
client2.results().To<std::vector<ProxyServer>>();
ASSERT_EQ(1u, proxy_servers1.size());
ProxyServer& server2 = proxy_servers2[0];
EXPECT_EQ(ProxyServer::SCHEME_SOCKS5, server2.scheme());
EXPECT_EQ("another-proxy.example.com", server2.host_port_pair().host());
EXPECT_EQ(6789, server2.host_port_pair().port());
}
TEST_F(MojoProxyResolverImplTest, DestroyClient) {
interfaces::ProxyResolverRequestClientPtr client_ptr;
scoped_ptr<TestRequestClient> client(
new TestRequestClient(mojo::GetProxy(&client_ptr)));
resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass());
ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size());
const MockProxyResolverV8Tracing::Request& request =
mock_proxy_resolver_->pending_requests()[0];
EXPECT_EQ(GURL("http://example.com"), request.url);
request.results->UsePacString("PROXY proxy.example.com:8080");
client.reset();
mock_proxy_resolver_->WaitForCancel();
}
TEST_F(MojoProxyResolverImplTest, DestroyService) {
interfaces::ProxyResolverRequestClientPtr client_ptr;
TestRequestClient client(mojo::GetProxy(&client_ptr));
resolver_->GetProxyForUrl("http://example.com", client_ptr.Pass());
ASSERT_EQ(1u, mock_proxy_resolver_->pending_requests().size());
resolver_impl_.reset();
client.event_waiter().WaitForEvent(TestRequestClient::CONNECTION_ERROR);
}
} // namespace net
|