1 // Copyright 2015 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.
5 #include "net/dns/host_resolver_mojo.h"
9 #include "base/memory/scoped_vector.h"
10 #include "net/base/address_list.h"
11 #include "net/base/net_errors.h"
12 #include "net/base/request_priority.h"
13 #include "net/base/test_completion_callback.h"
14 #include "net/dns/mojo_host_type_converters.h"
15 #include "net/test/event_waiter.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
18 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h"
23 void Fail(int result
) {
24 FAIL() << "Unexpected callback called with error " << result
;
27 class MockMojoHostResolverRequest
: public mojo::ErrorHandler
{
29 MockMojoHostResolverRequest(interfaces::HostResolverRequestClientPtr client
,
30 const base::Closure
& error_callback
);
31 void OnConnectionError() override
;
34 interfaces::HostResolverRequestClientPtr client_
;
35 const base::Closure error_callback_
;
38 MockMojoHostResolverRequest::MockMojoHostResolverRequest(
39 interfaces::HostResolverRequestClientPtr client
,
40 const base::Closure
& error_callback
)
41 : client_(client
.Pass()), error_callback_(error_callback
) {
42 client_
.set_error_handler(this);
45 void MockMojoHostResolverRequest::OnConnectionError() {
46 error_callback_
.Run();
49 struct HostResolverAction
{
56 static scoped_ptr
<HostResolverAction
> ReturnError(Error error
) {
57 scoped_ptr
<HostResolverAction
> result(new HostResolverAction
);
58 result
->error
= error
;
62 static scoped_ptr
<HostResolverAction
> ReturnResult(
63 const AddressList
& address_list
) {
64 scoped_ptr
<HostResolverAction
> result(new HostResolverAction
);
65 result
->addresses
= interfaces::AddressList::From(address_list
);
69 static scoped_ptr
<HostResolverAction
> DropRequest() {
70 scoped_ptr
<HostResolverAction
> result(new HostResolverAction
);
71 result
->action
= DROP
;
75 static scoped_ptr
<HostResolverAction
> RetainRequest() {
76 scoped_ptr
<HostResolverAction
> result(new HostResolverAction
);
77 result
->action
= RETAIN
;
81 Action action
= COMPLETE
;
82 interfaces::AddressListPtr addresses
;
86 class MockMojoHostResolver
: public interfaces::HostResolver
,
87 public mojo::ErrorHandler
{
89 MockMojoHostResolver(mojo::InterfaceRequest
<interfaces::HostResolver
> request
,
90 const base::Closure
& resolver_connection_error_callback
,
91 const base::Closure
& request_connection_error_callback
);
92 ~MockMojoHostResolver() override
;
94 void AddAction(scoped_ptr
<HostResolverAction
> action
);
96 const mojo::Array
<interfaces::HostResolverRequestInfoPtr
>& requests() {
97 return requests_received_
;
101 // interfaces::HostResolver override.
102 void Resolve(interfaces::HostResolverRequestInfoPtr request_info
,
103 interfaces::HostResolverRequestClientPtr client
) override
;
105 // mojo::ErrorHandler override.
106 void OnConnectionError() override
;
108 mojo::Binding
<interfaces::HostResolver
> binding_
;
109 ScopedVector
<HostResolverAction
> actions_
;
110 size_t results_returned_
= 0;
111 mojo::Array
<interfaces::HostResolverRequestInfoPtr
> requests_received_
;
112 const base::Closure resolver_connection_error_callback_
;
113 const base::Closure request_connection_error_callback_
;
114 ScopedVector
<MockMojoHostResolverRequest
> requests_
;
117 MockMojoHostResolver::MockMojoHostResolver(
118 mojo::InterfaceRequest
<interfaces::HostResolver
> request
,
119 const base::Closure
& resolver_connection_error_callback
,
120 const base::Closure
& request_connection_error_callback
)
121 : binding_(this, request
.Pass()),
122 resolver_connection_error_callback_(resolver_connection_error_callback
),
123 request_connection_error_callback_(request_connection_error_callback
) {
124 binding_
.set_error_handler(this);
127 MockMojoHostResolver::~MockMojoHostResolver() {
128 EXPECT_EQ(results_returned_
, actions_
.size());
131 void MockMojoHostResolver::OnConnectionError() {
132 resolver_connection_error_callback_
.Run();
135 void MockMojoHostResolver::AddAction(scoped_ptr
<HostResolverAction
> action
) {
136 actions_
.push_back(action
.release());
139 void MockMojoHostResolver::Resolve(
140 interfaces::HostResolverRequestInfoPtr request_info
,
141 interfaces::HostResolverRequestClientPtr client
) {
142 requests_received_
.push_back(request_info
.Pass());
143 ASSERT_LE(results_returned_
, actions_
.size());
144 switch (actions_
[results_returned_
]->action
) {
145 case HostResolverAction::COMPLETE
:
146 client
->ReportResult(actions_
[results_returned_
]->error
,
147 actions_
[results_returned_
]->addresses
.Pass());
149 case HostResolverAction::RETAIN
:
150 requests_
.push_back(new MockMojoHostResolverRequest(
151 client
.Pass(), request_connection_error_callback_
));
153 case HostResolverAction::DROP
:
162 class HostResolverMojoTest
: public testing::Test
{
164 enum class ConnectionErrorSource
{
169 using Waiter
= EventWaiter
<ConnectionErrorSource
>;
171 void SetUp() override
{
172 interfaces::HostResolverPtr resolver_ptr
;
173 mock_resolver_
.reset(new MockMojoHostResolver(
174 mojo::GetProxy(&resolver_ptr
),
175 base::Bind(&Waiter::NotifyEvent
, base::Unretained(&waiter_
),
176 ConnectionErrorSource::RESOLVER
),
177 base::Bind(&Waiter::NotifyEvent
, base::Unretained(&waiter_
),
178 ConnectionErrorSource::REQUEST
)));
179 resolver_
.reset(new HostResolverMojo(
181 base::Bind(&Waiter::NotifyEvent
, base::Unretained(&waiter_
),
182 ConnectionErrorSource::CLIENT
)));
185 int Resolve(const HostResolver::RequestInfo
& request_info
,
186 AddressList
* result
) {
187 HostResolver::RequestHandle request_handle
= nullptr;
188 TestCompletionCallback callback
;
189 return callback
.GetResult(resolver_
->Resolve(
190 request_info
, DEFAULT_PRIORITY
, result
, callback
.callback(),
191 &request_handle
, BoundNetLog()));
194 scoped_ptr
<MockMojoHostResolver
> mock_resolver_
;
196 scoped_ptr
<HostResolverMojo
> resolver_
;
201 TEST_F(HostResolverMojoTest
, Basic
) {
202 AddressList address_list
;
203 IPAddressNumber address_number
;
204 ASSERT_TRUE(ParseIPLiteralToNumber("1.2.3.4", &address_number
));
205 address_list
.push_back(IPEndPoint(address_number
, 12345));
206 address_list
.push_back(
207 IPEndPoint(ConvertIPv4NumberToIPv6Number(address_number
), 12345));
208 mock_resolver_
->AddAction(HostResolverAction::ReturnResult(address_list
));
209 HostResolver::RequestInfo
request_info(
210 HostPortPair::FromString("example.com:12345"));
212 EXPECT_EQ(OK
, Resolve(request_info
, &result
));
213 ASSERT_EQ(2u, result
.size());
214 EXPECT_EQ(address_list
[0], result
[0]);
215 EXPECT_EQ(address_list
[1], result
[1]);
217 ASSERT_EQ(1u, mock_resolver_
->requests().size());
218 interfaces::HostResolverRequestInfo
& request
= *mock_resolver_
->requests()[0];
219 EXPECT_EQ("example.com", request
.host
.To
<std::string
>());
220 EXPECT_EQ(12345, request
.port
);
221 EXPECT_EQ(interfaces::ADDRESS_FAMILY_UNSPECIFIED
, request
.address_family
);
222 EXPECT_FALSE(request
.is_my_ip_address
);
225 TEST_F(HostResolverMojoTest
, Multiple
) {
226 AddressList address_list
;
227 IPAddressNumber address_number
;
228 ASSERT_TRUE(ParseIPLiteralToNumber("1.2.3.4", &address_number
));
229 address_list
.push_back(IPEndPoint(address_number
, 12345));
230 mock_resolver_
->AddAction(HostResolverAction::ReturnResult(address_list
));
231 mock_resolver_
->AddAction(
232 HostResolverAction::ReturnError(ERR_NAME_NOT_RESOLVED
));
233 HostResolver::RequestInfo
request_info1(
234 HostPortPair::FromString("example.com:12345"));
235 request_info1
.set_address_family(ADDRESS_FAMILY_IPV4
);
236 request_info1
.set_is_my_ip_address(true);
237 HostResolver::RequestInfo
request_info2(
238 HostPortPair::FromString("example.org:80"));
239 request_info2
.set_address_family(ADDRESS_FAMILY_IPV6
);
242 HostResolver::RequestHandle request_handle1
= nullptr;
243 HostResolver::RequestHandle request_handle2
= nullptr;
244 TestCompletionCallback callback1
;
245 TestCompletionCallback callback2
;
246 ASSERT_EQ(ERR_IO_PENDING
,
247 resolver_
->Resolve(request_info1
, DEFAULT_PRIORITY
, &result1
,
248 callback1
.callback(), &request_handle1
,
250 ASSERT_EQ(ERR_IO_PENDING
,
251 resolver_
->Resolve(request_info2
, DEFAULT_PRIORITY
, &result2
,
252 callback2
.callback(), &request_handle2
,
254 EXPECT_EQ(OK
, callback1
.GetResult(ERR_IO_PENDING
));
255 EXPECT_EQ(ERR_NAME_NOT_RESOLVED
, callback2
.GetResult(ERR_IO_PENDING
));
256 ASSERT_EQ(1u, result1
.size());
257 EXPECT_EQ(address_list
[0], result1
[0]);
258 ASSERT_EQ(0u, result2
.size());
260 ASSERT_EQ(2u, mock_resolver_
->requests().size());
261 interfaces::HostResolverRequestInfo
& request1
=
262 *mock_resolver_
->requests()[0];
263 EXPECT_EQ("example.com", request1
.host
.To
<std::string
>());
264 EXPECT_EQ(12345, request1
.port
);
265 EXPECT_EQ(interfaces::ADDRESS_FAMILY_IPV4
, request1
.address_family
);
266 EXPECT_TRUE(request1
.is_my_ip_address
);
267 interfaces::HostResolverRequestInfo
& request2
=
268 *mock_resolver_
->requests()[1];
269 EXPECT_EQ("example.org", request2
.host
.To
<std::string
>());
270 EXPECT_EQ(80, request2
.port
);
271 EXPECT_EQ(interfaces::ADDRESS_FAMILY_IPV6
, request2
.address_family
);
272 EXPECT_FALSE(request2
.is_my_ip_address
);
275 TEST_F(HostResolverMojoTest
, Error
) {
276 mock_resolver_
->AddAction(
277 HostResolverAction::ReturnError(ERR_NAME_NOT_RESOLVED
));
278 HostResolver::RequestInfo
request_info(
279 HostPortPair::FromString("example.com:8080"));
280 request_info
.set_address_family(ADDRESS_FAMILY_IPV4
);
282 EXPECT_EQ(ERR_NAME_NOT_RESOLVED
, Resolve(request_info
, &result
));
283 EXPECT_TRUE(result
.empty());
285 ASSERT_EQ(1u, mock_resolver_
->requests().size());
286 interfaces::HostResolverRequestInfo
& request
= *mock_resolver_
->requests()[0];
287 EXPECT_EQ("example.com", request
.host
.To
<std::string
>());
288 EXPECT_EQ(8080, request
.port
);
289 EXPECT_EQ(interfaces::ADDRESS_FAMILY_IPV4
, request
.address_family
);
290 EXPECT_FALSE(request
.is_my_ip_address
);
293 TEST_F(HostResolverMojoTest
, EmptyResult
) {
294 mock_resolver_
->AddAction(HostResolverAction::ReturnError(OK
));
295 HostResolver::RequestInfo
request_info(
296 HostPortPair::FromString("example.com:8080"));
298 EXPECT_EQ(OK
, Resolve(request_info
, &result
));
299 EXPECT_TRUE(result
.empty());
301 ASSERT_EQ(1u, mock_resolver_
->requests().size());
304 TEST_F(HostResolverMojoTest
, Cancel
) {
305 mock_resolver_
->AddAction(HostResolverAction::RetainRequest());
306 HostResolver::RequestInfo
request_info(
307 HostPortPair::FromString("example.com:80"));
308 request_info
.set_address_family(ADDRESS_FAMILY_IPV6
);
310 HostResolver::RequestHandle request_handle
= nullptr;
311 resolver_
->Resolve(request_info
, DEFAULT_PRIORITY
, &result
, base::Bind(&Fail
),
312 &request_handle
, BoundNetLog());
313 resolver_
->CancelRequest(request_handle
);
314 waiter_
.WaitForEvent(ConnectionErrorSource::REQUEST
);
315 EXPECT_TRUE(result
.empty());
317 ASSERT_EQ(1u, mock_resolver_
->requests().size());
318 interfaces::HostResolverRequestInfo
& request
= *mock_resolver_
->requests()[0];
319 EXPECT_EQ("example.com", request
.host
.To
<std::string
>());
320 EXPECT_EQ(80, request
.port
);
321 EXPECT_EQ(interfaces::ADDRESS_FAMILY_IPV6
, request
.address_family
);
322 EXPECT_FALSE(request
.is_my_ip_address
);
325 TEST_F(HostResolverMojoTest
, ImplDropsClientConnection
) {
326 mock_resolver_
->AddAction(HostResolverAction::DropRequest());
327 HostResolver::RequestInfo
request_info(
328 HostPortPair::FromString("example.com:1"));
330 EXPECT_EQ(ERR_FAILED
, Resolve(request_info
, &result
));
331 EXPECT_TRUE(result
.empty());
333 ASSERT_EQ(1u, mock_resolver_
->requests().size());
334 interfaces::HostResolverRequestInfo
& request
= *mock_resolver_
->requests()[0];
335 EXPECT_EQ("example.com", request
.host
.To
<std::string
>());
336 EXPECT_EQ(1, request
.port
);
337 EXPECT_EQ(interfaces::ADDRESS_FAMILY_UNSPECIFIED
, request
.address_family
);
338 EXPECT_FALSE(request
.is_my_ip_address
);
341 TEST_F(HostResolverMojoTest
, DestroyImpl
) {
342 mock_resolver_
.reset();
343 waiter_
.WaitForEvent(ConnectionErrorSource::CLIENT
);
346 TEST_F(HostResolverMojoTest
, DestroyClient
) {
348 waiter_
.WaitForEvent(ConnectionErrorSource::RESOLVER
);
351 TEST_F(HostResolverMojoTest
, ResolveFromCache
) {
352 HostResolver::RequestInfo
request_info(
353 HostPortPair::FromString("example.com:8080"));
355 EXPECT_EQ(ERR_DNS_CACHE_MISS
,
356 resolver_
->ResolveFromCache(request_info
, &result
, BoundNetLog()));
357 EXPECT_TRUE(result
.empty());