OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/dns/host_resolver_mojo.h" | 5 #include "net/dns/host_resolver_mojo.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "mojo/public/cpp/bindings/binding.h" | 11 #include "mojo/public/cpp/bindings/binding.h" |
12 #include "net/base/address_list.h" | 12 #include "net/base/address_list.h" |
13 #include "net/base/ip_address.h" | 13 #include "net/base/ip_address.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 #include "net/base/request_priority.h" | 15 #include "net/base/request_priority.h" |
16 #include "net/base/test_completion_callback.h" | 16 #include "net/base/test_completion_callback.h" |
17 #include "net/dns/mojo_host_type_converters.h" | |
18 #include "net/log/net_log_with_source.h" | 17 #include "net/log/net_log_with_source.h" |
19 #include "net/test/event_waiter.h" | 18 #include "net/test/event_waiter.h" |
20 #include "net/test/gtest_util.h" | 19 #include "net/test/gtest_util.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
23 | 22 |
24 using net::test::IsError; | 23 using net::test::IsError; |
25 using net::test::IsOk; | 24 using net::test::IsOk; |
26 | 25 |
27 namespace net { | 26 namespace net { |
(...skipping 26 matching lines...) Expand all Loading... |
54 error_callback_.Run(); | 53 error_callback_.Run(); |
55 } | 54 } |
56 | 55 |
57 struct HostResolverAction { | 56 struct HostResolverAction { |
58 enum Action { | 57 enum Action { |
59 COMPLETE, | 58 COMPLETE, |
60 DROP, | 59 DROP, |
61 RETAIN, | 60 RETAIN, |
62 }; | 61 }; |
63 | 62 |
64 static std::unique_ptr<HostResolverAction> ReturnError(Error error) { | 63 static HostResolverAction ReturnError(Error error) { |
65 std::unique_ptr<HostResolverAction> result(new HostResolverAction); | 64 HostResolverAction result; |
66 result->error = error; | 65 result.error = error; |
67 return result; | 66 return result; |
68 } | 67 } |
69 | 68 |
70 static std::unique_ptr<HostResolverAction> ReturnResult( | 69 static HostResolverAction ReturnResult(const AddressList& address_list) { |
71 const AddressList& address_list) { | 70 HostResolverAction result; |
72 std::unique_ptr<HostResolverAction> result(new HostResolverAction); | 71 result.addresses = address_list; |
73 result->addresses = interfaces::AddressList::From(address_list); | |
74 return result; | 72 return result; |
75 } | 73 } |
76 | 74 |
77 static std::unique_ptr<HostResolverAction> DropRequest() { | 75 static HostResolverAction DropRequest() { |
78 std::unique_ptr<HostResolverAction> result(new HostResolverAction); | 76 HostResolverAction result; |
79 result->action = DROP; | 77 result.action = DROP; |
80 return result; | 78 return result; |
81 } | 79 } |
82 | 80 |
83 static std::unique_ptr<HostResolverAction> RetainRequest() { | 81 static HostResolverAction RetainRequest() { |
84 std::unique_ptr<HostResolverAction> result(new HostResolverAction); | 82 HostResolverAction result; |
85 result->action = RETAIN; | 83 result.action = RETAIN; |
86 return result; | 84 return result; |
87 } | 85 } |
88 | 86 |
89 Action action = COMPLETE; | 87 Action action = COMPLETE; |
90 interfaces::AddressListPtr addresses; | 88 AddressList addresses; |
91 Error error = OK; | 89 Error error = OK; |
92 }; | 90 }; |
93 | 91 |
94 class MockMojoHostResolver : public HostResolverMojo::Impl { | 92 class MockMojoHostResolver : public HostResolverMojo::Impl { |
95 public: | 93 public: |
96 explicit MockMojoHostResolver( | 94 explicit MockMojoHostResolver( |
97 const base::Closure& request_connection_error_callback); | 95 const base::Closure& request_connection_error_callback); |
98 ~MockMojoHostResolver() override; | 96 ~MockMojoHostResolver() override; |
99 | 97 |
100 void AddAction(std::unique_ptr<HostResolverAction> action); | 98 void AddAction(HostResolverAction action); |
101 | 99 |
102 const mojo::Array<interfaces::HostResolverRequestInfoPtr>& requests() { | 100 const std::vector<HostResolver::RequestInfo>& requests() { |
103 return requests_received_; | 101 return requests_received_; |
104 } | 102 } |
105 | 103 |
106 void ResolveDns(interfaces::HostResolverRequestInfoPtr request_info, | 104 void ResolveDns(std::unique_ptr<HostResolver::RequestInfo> request_info, |
107 interfaces::HostResolverRequestClientPtr client) override; | 105 interfaces::HostResolverRequestClientPtr client) override; |
108 | 106 |
109 private: | 107 private: |
110 std::vector<std::unique_ptr<HostResolverAction>> actions_; | 108 std::vector<HostResolverAction> actions_; |
111 size_t results_returned_ = 0; | 109 size_t results_returned_ = 0; |
112 mojo::Array<interfaces::HostResolverRequestInfoPtr> requests_received_; | 110 std::vector<HostResolver::RequestInfo> requests_received_; |
113 const base::Closure request_connection_error_callback_; | 111 const base::Closure request_connection_error_callback_; |
114 std::vector<std::unique_ptr<MockMojoHostResolverRequest>> requests_; | 112 std::vector<std::unique_ptr<MockMojoHostResolverRequest>> requests_; |
115 }; | 113 }; |
116 | 114 |
117 MockMojoHostResolver::MockMojoHostResolver( | 115 MockMojoHostResolver::MockMojoHostResolver( |
118 const base::Closure& request_connection_error_callback) | 116 const base::Closure& request_connection_error_callback) |
119 : request_connection_error_callback_(request_connection_error_callback) { | 117 : request_connection_error_callback_(request_connection_error_callback) { |
120 } | 118 } |
121 | 119 |
122 MockMojoHostResolver::~MockMojoHostResolver() { | 120 MockMojoHostResolver::~MockMojoHostResolver() { |
123 EXPECT_EQ(results_returned_, actions_.size()); | 121 EXPECT_EQ(results_returned_, actions_.size()); |
124 } | 122 } |
125 | 123 |
126 void MockMojoHostResolver::AddAction( | 124 void MockMojoHostResolver::AddAction(HostResolverAction action) { |
127 std::unique_ptr<HostResolverAction> action) { | |
128 actions_.push_back(std::move(action)); | 125 actions_.push_back(std::move(action)); |
129 } | 126 } |
130 | 127 |
131 void MockMojoHostResolver::ResolveDns( | 128 void MockMojoHostResolver::ResolveDns( |
132 interfaces::HostResolverRequestInfoPtr request_info, | 129 std::unique_ptr<HostResolver::RequestInfo> request_info, |
133 interfaces::HostResolverRequestClientPtr client) { | 130 interfaces::HostResolverRequestClientPtr client) { |
134 requests_received_.push_back(std::move(request_info)); | 131 requests_received_.push_back(std::move(*request_info)); |
135 ASSERT_LE(results_returned_, actions_.size()); | 132 ASSERT_LE(results_returned_, actions_.size()); |
136 switch (actions_[results_returned_]->action) { | 133 switch (actions_[results_returned_].action) { |
137 case HostResolverAction::COMPLETE: | 134 case HostResolverAction::COMPLETE: |
138 client->ReportResult(actions_[results_returned_]->error, | 135 client->ReportResult(actions_[results_returned_].error, |
139 std::move(actions_[results_returned_]->addresses)); | 136 std::move(actions_[results_returned_].addresses)); |
140 break; | 137 break; |
141 case HostResolverAction::RETAIN: | 138 case HostResolverAction::RETAIN: |
142 requests_.push_back(base::WrapUnique(new MockMojoHostResolverRequest( | 139 requests_.push_back(base::WrapUnique(new MockMojoHostResolverRequest( |
143 std::move(client), request_connection_error_callback_))); | 140 std::move(client), request_connection_error_callback_))); |
144 break; | 141 break; |
145 case HostResolverAction::DROP: | 142 case HostResolverAction::DROP: |
146 client.reset(); | 143 client.reset(); |
147 break; | 144 break; |
148 } | 145 } |
149 results_returned_++; | 146 results_returned_++; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 mock_resolver_->AddAction(HostResolverAction::ReturnResult(address_list)); | 188 mock_resolver_->AddAction(HostResolverAction::ReturnResult(address_list)); |
192 HostResolver::RequestInfo request_info( | 189 HostResolver::RequestInfo request_info( |
193 HostPortPair::FromString("example.com:12345")); | 190 HostPortPair::FromString("example.com:12345")); |
194 AddressList result; | 191 AddressList result; |
195 EXPECT_THAT(Resolve(request_info, &result), IsOk()); | 192 EXPECT_THAT(Resolve(request_info, &result), IsOk()); |
196 ASSERT_EQ(2u, result.size()); | 193 ASSERT_EQ(2u, result.size()); |
197 EXPECT_EQ(address_list[0], result[0]); | 194 EXPECT_EQ(address_list[0], result[0]); |
198 EXPECT_EQ(address_list[1], result[1]); | 195 EXPECT_EQ(address_list[1], result[1]); |
199 | 196 |
200 ASSERT_EQ(1u, mock_resolver_->requests().size()); | 197 ASSERT_EQ(1u, mock_resolver_->requests().size()); |
201 interfaces::HostResolverRequestInfo& request = *mock_resolver_->requests()[0]; | 198 const HostResolver::RequestInfo& request = mock_resolver_->requests()[0]; |
202 EXPECT_EQ("example.com", request.host.To<std::string>()); | 199 EXPECT_EQ("example.com", request.hostname()); |
203 EXPECT_EQ(12345, request.port); | 200 EXPECT_EQ(12345, request.port()); |
204 EXPECT_EQ(interfaces::AddressFamily::UNSPECIFIED, request.address_family); | 201 EXPECT_EQ(ADDRESS_FAMILY_UNSPECIFIED, request.address_family()); |
205 EXPECT_FALSE(request.is_my_ip_address); | 202 EXPECT_FALSE(request.is_my_ip_address()); |
206 } | 203 } |
207 | 204 |
208 TEST_F(HostResolverMojoTest, ResolveCachedResult) { | 205 TEST_F(HostResolverMojoTest, ResolveCachedResult) { |
209 AddressList address_list; | 206 AddressList address_list; |
210 IPAddress address(1, 2, 3, 4); | 207 IPAddress address(1, 2, 3, 4); |
211 address_list.push_back(IPEndPoint(address, 12345)); | 208 address_list.push_back(IPEndPoint(address, 12345)); |
212 address_list.push_back( | 209 address_list.push_back( |
213 IPEndPoint(ConvertIPv4ToIPv4MappedIPv6(address), 12345)); | 210 IPEndPoint(ConvertIPv4ToIPv4MappedIPv6(address), 12345)); |
214 mock_resolver_->AddAction(HostResolverAction::ReturnResult(address_list)); | 211 mock_resolver_->AddAction(HostResolverAction::ReturnResult(address_list)); |
215 HostResolver::RequestInfo request_info( | 212 HostResolver::RequestInfo request_info( |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 &result2, callback2.callback(), | 264 &result2, callback2.callback(), |
268 &request2, NetLogWithSource())); | 265 &request2, NetLogWithSource())); |
269 EXPECT_THAT(callback1.GetResult(ERR_IO_PENDING), IsOk()); | 266 EXPECT_THAT(callback1.GetResult(ERR_IO_PENDING), IsOk()); |
270 EXPECT_THAT(callback2.GetResult(ERR_IO_PENDING), | 267 EXPECT_THAT(callback2.GetResult(ERR_IO_PENDING), |
271 IsError(ERR_NAME_NOT_RESOLVED)); | 268 IsError(ERR_NAME_NOT_RESOLVED)); |
272 ASSERT_EQ(1u, result1.size()); | 269 ASSERT_EQ(1u, result1.size()); |
273 EXPECT_EQ(address_list[0], result1[0]); | 270 EXPECT_EQ(address_list[0], result1[0]); |
274 ASSERT_EQ(0u, result2.size()); | 271 ASSERT_EQ(0u, result2.size()); |
275 | 272 |
276 ASSERT_EQ(2u, mock_resolver_->requests().size()); | 273 ASSERT_EQ(2u, mock_resolver_->requests().size()); |
277 interfaces::HostResolverRequestInfo& info1 = *mock_resolver_->requests()[0]; | 274 const HostResolver::RequestInfo& info1 = mock_resolver_->requests()[0]; |
278 EXPECT_EQ("example.com", info1.host.To<std::string>()); | 275 EXPECT_EQ("example.com", info1.hostname()); |
279 EXPECT_EQ(12345, info1.port); | 276 EXPECT_EQ(12345, info1.port()); |
280 EXPECT_EQ(interfaces::AddressFamily::IPV4, info1.address_family); | 277 EXPECT_EQ(ADDRESS_FAMILY_IPV4, info1.address_family()); |
281 EXPECT_TRUE(info1.is_my_ip_address); | 278 EXPECT_TRUE(info1.is_my_ip_address()); |
282 interfaces::HostResolverRequestInfo& info2 = *mock_resolver_->requests()[1]; | 279 const HostResolver::RequestInfo& info2 = mock_resolver_->requests()[1]; |
283 EXPECT_EQ("example.org", info2.host.To<std::string>()); | 280 EXPECT_EQ("example.org", info2.hostname()); |
284 EXPECT_EQ(80, info2.port); | 281 EXPECT_EQ(80, info2.port()); |
285 EXPECT_EQ(interfaces::AddressFamily::IPV6, info2.address_family); | 282 EXPECT_EQ(ADDRESS_FAMILY_IPV6, info2.address_family()); |
286 EXPECT_FALSE(info2.is_my_ip_address); | 283 EXPECT_FALSE(info2.is_my_ip_address()); |
287 } | 284 } |
288 | 285 |
289 TEST_F(HostResolverMojoTest, Error) { | 286 TEST_F(HostResolverMojoTest, Error) { |
290 mock_resolver_->AddAction( | 287 mock_resolver_->AddAction( |
291 HostResolverAction::ReturnError(ERR_NAME_NOT_RESOLVED)); | 288 HostResolverAction::ReturnError(ERR_NAME_NOT_RESOLVED)); |
292 HostResolver::RequestInfo request_info( | 289 HostResolver::RequestInfo request_info( |
293 HostPortPair::FromString("example.com:8080")); | 290 HostPortPair::FromString("example.com:8080")); |
294 request_info.set_address_family(ADDRESS_FAMILY_IPV4); | 291 request_info.set_address_family(ADDRESS_FAMILY_IPV4); |
295 AddressList result; | 292 AddressList result; |
296 EXPECT_THAT(Resolve(request_info, &result), IsError(ERR_NAME_NOT_RESOLVED)); | 293 EXPECT_THAT(Resolve(request_info, &result), IsError(ERR_NAME_NOT_RESOLVED)); |
297 EXPECT_TRUE(result.empty()); | 294 EXPECT_TRUE(result.empty()); |
298 | 295 |
299 ASSERT_EQ(1u, mock_resolver_->requests().size()); | 296 ASSERT_EQ(1u, mock_resolver_->requests().size()); |
300 interfaces::HostResolverRequestInfo& request = *mock_resolver_->requests()[0]; | 297 const HostResolver::RequestInfo& request = mock_resolver_->requests()[0]; |
301 EXPECT_EQ("example.com", request.host.To<std::string>()); | 298 EXPECT_EQ("example.com", request.hostname()); |
302 EXPECT_EQ(8080, request.port); | 299 EXPECT_EQ(8080, request.port()); |
303 EXPECT_EQ(interfaces::AddressFamily::IPV4, request.address_family); | 300 EXPECT_EQ(ADDRESS_FAMILY_IPV4, request.address_family()); |
304 EXPECT_FALSE(request.is_my_ip_address); | 301 EXPECT_FALSE(request.is_my_ip_address()); |
305 } | 302 } |
306 | 303 |
307 TEST_F(HostResolverMojoTest, EmptyResult) { | 304 TEST_F(HostResolverMojoTest, EmptyResult) { |
308 mock_resolver_->AddAction(HostResolverAction::ReturnError(OK)); | 305 mock_resolver_->AddAction(HostResolverAction::ReturnError(OK)); |
309 HostResolver::RequestInfo request_info( | 306 HostResolver::RequestInfo request_info( |
310 HostPortPair::FromString("example.com:8080")); | 307 HostPortPair::FromString("example.com:8080")); |
311 AddressList result; | 308 AddressList result; |
312 EXPECT_THAT(Resolve(request_info, &result), IsOk()); | 309 EXPECT_THAT(Resolve(request_info, &result), IsOk()); |
313 EXPECT_TRUE(result.empty()); | 310 EXPECT_TRUE(result.empty()); |
314 | 311 |
315 ASSERT_EQ(1u, mock_resolver_->requests().size()); | 312 ASSERT_EQ(1u, mock_resolver_->requests().size()); |
316 } | 313 } |
317 | 314 |
318 TEST_F(HostResolverMojoTest, Cancel) { | 315 TEST_F(HostResolverMojoTest, Cancel) { |
319 mock_resolver_->AddAction(HostResolverAction::RetainRequest()); | 316 mock_resolver_->AddAction(HostResolverAction::RetainRequest()); |
320 HostResolver::RequestInfo request_info( | 317 HostResolver::RequestInfo request_info( |
321 HostPortPair::FromString("example.com:80")); | 318 HostPortPair::FromString("example.com:80")); |
322 request_info.set_address_family(ADDRESS_FAMILY_IPV6); | 319 request_info.set_address_family(ADDRESS_FAMILY_IPV6); |
323 AddressList result; | 320 AddressList result; |
324 std::unique_ptr<HostResolver::Request> request; | 321 std::unique_ptr<HostResolver::Request> request; |
325 resolver_->Resolve(request_info, DEFAULT_PRIORITY, &result, base::Bind(&Fail), | 322 resolver_->Resolve(request_info, DEFAULT_PRIORITY, &result, base::Bind(&Fail), |
326 &request, NetLogWithSource()); | 323 &request, NetLogWithSource()); |
327 request.reset(); | 324 request.reset(); |
328 waiter_.WaitForEvent(ConnectionErrorSource::REQUEST); | 325 waiter_.WaitForEvent(ConnectionErrorSource::REQUEST); |
329 EXPECT_TRUE(result.empty()); | 326 EXPECT_TRUE(result.empty()); |
330 | 327 |
331 ASSERT_EQ(1u, mock_resolver_->requests().size()); | 328 ASSERT_EQ(1u, mock_resolver_->requests().size()); |
332 interfaces::HostResolverRequestInfo& info1 = *mock_resolver_->requests()[0]; | 329 const HostResolver::RequestInfo& info1 = mock_resolver_->requests()[0]; |
333 EXPECT_EQ("example.com", info1.host.To<std::string>()); | 330 EXPECT_EQ("example.com", info1.hostname()); |
334 EXPECT_EQ(80, info1.port); | 331 EXPECT_EQ(80, info1.port()); |
335 EXPECT_EQ(interfaces::AddressFamily::IPV6, info1.address_family); | 332 EXPECT_EQ(ADDRESS_FAMILY_IPV6, info1.address_family()); |
336 EXPECT_FALSE(info1.is_my_ip_address); | 333 EXPECT_FALSE(info1.is_my_ip_address()); |
337 } | 334 } |
338 | 335 |
339 TEST_F(HostResolverMojoTest, ImplDropsClientConnection) { | 336 TEST_F(HostResolverMojoTest, ImplDropsClientConnection) { |
340 mock_resolver_->AddAction(HostResolverAction::DropRequest()); | 337 mock_resolver_->AddAction(HostResolverAction::DropRequest()); |
341 HostResolver::RequestInfo request_info( | 338 HostResolver::RequestInfo request_info( |
342 HostPortPair::FromString("example.com:1")); | 339 HostPortPair::FromString("example.com:1")); |
343 AddressList result; | 340 AddressList result; |
344 EXPECT_THAT(Resolve(request_info, &result), IsError(ERR_FAILED)); | 341 EXPECT_THAT(Resolve(request_info, &result), IsError(ERR_FAILED)); |
345 EXPECT_TRUE(result.empty()); | 342 EXPECT_TRUE(result.empty()); |
346 | 343 |
347 ASSERT_EQ(1u, mock_resolver_->requests().size()); | 344 ASSERT_EQ(1u, mock_resolver_->requests().size()); |
348 interfaces::HostResolverRequestInfo& info2 = *mock_resolver_->requests()[0]; | 345 const HostResolver::RequestInfo& info2 = mock_resolver_->requests()[0]; |
349 EXPECT_EQ("example.com", info2.host.To<std::string>()); | 346 EXPECT_EQ("example.com", info2.hostname()); |
350 EXPECT_EQ(1, info2.port); | 347 EXPECT_EQ(1, info2.port()); |
351 EXPECT_EQ(interfaces::AddressFamily::UNSPECIFIED, info2.address_family); | 348 EXPECT_EQ(ADDRESS_FAMILY_UNSPECIFIED, info2.address_family()); |
352 EXPECT_FALSE(info2.is_my_ip_address); | 349 EXPECT_FALSE(info2.is_my_ip_address()); |
353 } | 350 } |
354 | 351 |
355 TEST_F(HostResolverMojoTest, ResolveFromCache_Miss) { | 352 TEST_F(HostResolverMojoTest, ResolveFromCache_Miss) { |
356 HostResolver::RequestInfo request_info( | 353 HostResolver::RequestInfo request_info( |
357 HostPortPair::FromString("example.com:8080")); | 354 HostPortPair::FromString("example.com:8080")); |
358 AddressList result; | 355 AddressList result; |
359 EXPECT_EQ(ERR_DNS_CACHE_MISS, resolver_->ResolveFromCache( | 356 EXPECT_EQ(ERR_DNS_CACHE_MISS, resolver_->ResolveFromCache( |
360 request_info, &result, NetLogWithSource())); | 357 request_info, &result, NetLogWithSource())); |
361 EXPECT_TRUE(result.empty()); | 358 EXPECT_TRUE(result.empty()); |
362 } | 359 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 EXPECT_EQ(ERR_DNS_CACHE_MISS, resolver_->ResolveFromCache( | 398 EXPECT_EQ(ERR_DNS_CACHE_MISS, resolver_->ResolveFromCache( |
402 request_info, &result, NetLogWithSource())); | 399 request_info, &result, NetLogWithSource())); |
403 EXPECT_TRUE(result.empty()); | 400 EXPECT_TRUE(result.empty()); |
404 } | 401 } |
405 | 402 |
406 TEST_F(HostResolverMojoTest, GetHostCache) { | 403 TEST_F(HostResolverMojoTest, GetHostCache) { |
407 EXPECT_TRUE(resolver_->GetHostCache()); | 404 EXPECT_TRUE(resolver_->GetHostCache()); |
408 } | 405 } |
409 | 406 |
410 } // namespace net | 407 } // namespace net |
OLD | NEW |