| 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" |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 | 159 |
| 160 void SetUp() override { | 160 void SetUp() override { |
| 161 mock_resolver_.reset(new MockMojoHostResolver( | 161 mock_resolver_.reset(new MockMojoHostResolver( |
| 162 base::Bind(&Waiter::NotifyEvent, base::Unretained(&waiter_), | 162 base::Bind(&Waiter::NotifyEvent, base::Unretained(&waiter_), |
| 163 ConnectionErrorSource::REQUEST))); | 163 ConnectionErrorSource::REQUEST))); |
| 164 resolver_.reset(new HostResolverMojo(mock_resolver_.get())); | 164 resolver_.reset(new HostResolverMojo(mock_resolver_.get())); |
| 165 } | 165 } |
| 166 | 166 |
| 167 int Resolve(const HostResolver::RequestInfo& request_info, | 167 int Resolve(const HostResolver::RequestInfo& request_info, |
| 168 AddressList* result) { | 168 AddressList* result) { |
| 169 HostResolver::RequestHandle request_handle = nullptr; | |
| 170 TestCompletionCallback callback; | 169 TestCompletionCallback callback; |
| 171 return callback.GetResult(resolver_->Resolve( | 170 return callback.GetResult(resolver_->Resolve(request_info, DEFAULT_PRIORITY, |
| 172 request_info, DEFAULT_PRIORITY, result, callback.callback(), | 171 result, callback.callback(), |
| 173 &request_handle, BoundNetLog())); | 172 &request_, BoundNetLog())); |
| 174 } | 173 } |
| 175 | 174 |
| 176 std::unique_ptr<MockMojoHostResolver> mock_resolver_; | 175 std::unique_ptr<MockMojoHostResolver> mock_resolver_; |
| 177 | 176 |
| 178 std::unique_ptr<HostResolverMojo> resolver_; | 177 std::unique_ptr<HostResolverMojo> resolver_; |
| 179 | 178 |
| 179 std::unique_ptr<HostResolver::Request> request_; |
| 180 |
| 180 Waiter waiter_; | 181 Waiter waiter_; |
| 181 }; | 182 }; |
| 182 | 183 |
| 183 TEST_F(HostResolverMojoTest, Basic) { | 184 TEST_F(HostResolverMojoTest, Basic) { |
| 184 AddressList address_list; | 185 AddressList address_list; |
| 185 IPAddress address(1, 2, 3, 4); | 186 IPAddress address(1, 2, 3, 4); |
| 186 address_list.push_back(IPEndPoint(address, 12345)); | 187 address_list.push_back(IPEndPoint(address, 12345)); |
| 187 address_list.push_back( | 188 address_list.push_back( |
| 188 IPEndPoint(ConvertIPv4ToIPv4MappedIPv6(address), 12345)); | 189 IPEndPoint(ConvertIPv4ToIPv4MappedIPv6(address), 12345)); |
| 189 mock_resolver_->AddAction(HostResolverAction::ReturnResult(address_list)); | 190 mock_resolver_->AddAction(HostResolverAction::ReturnResult(address_list)); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 HostResolverAction::ReturnError(ERR_NAME_NOT_RESOLVED)); | 248 HostResolverAction::ReturnError(ERR_NAME_NOT_RESOLVED)); |
| 248 HostResolver::RequestInfo request_info1( | 249 HostResolver::RequestInfo request_info1( |
| 249 HostPortPair::FromString("example.com:12345")); | 250 HostPortPair::FromString("example.com:12345")); |
| 250 request_info1.set_address_family(ADDRESS_FAMILY_IPV4); | 251 request_info1.set_address_family(ADDRESS_FAMILY_IPV4); |
| 251 request_info1.set_is_my_ip_address(true); | 252 request_info1.set_is_my_ip_address(true); |
| 252 HostResolver::RequestInfo request_info2( | 253 HostResolver::RequestInfo request_info2( |
| 253 HostPortPair::FromString("example.org:80")); | 254 HostPortPair::FromString("example.org:80")); |
| 254 request_info2.set_address_family(ADDRESS_FAMILY_IPV6); | 255 request_info2.set_address_family(ADDRESS_FAMILY_IPV6); |
| 255 AddressList result1; | 256 AddressList result1; |
| 256 AddressList result2; | 257 AddressList result2; |
| 257 HostResolver::RequestHandle request_handle1 = nullptr; | 258 std::unique_ptr<HostResolver::Request> request1; |
| 258 HostResolver::RequestHandle request_handle2 = nullptr; | 259 std::unique_ptr<HostResolver::Request> request2; |
| 259 TestCompletionCallback callback1; | 260 TestCompletionCallback callback1; |
| 260 TestCompletionCallback callback2; | 261 TestCompletionCallback callback2; |
| 261 ASSERT_EQ(ERR_IO_PENDING, | 262 ASSERT_EQ(ERR_IO_PENDING, |
| 262 resolver_->Resolve(request_info1, DEFAULT_PRIORITY, &result1, | 263 resolver_->Resolve(request_info1, DEFAULT_PRIORITY, &result1, |
| 263 callback1.callback(), &request_handle1, | 264 callback1.callback(), &request1, BoundNetLog())); |
| 264 BoundNetLog())); | |
| 265 ASSERT_EQ(ERR_IO_PENDING, | 265 ASSERT_EQ(ERR_IO_PENDING, |
| 266 resolver_->Resolve(request_info2, DEFAULT_PRIORITY, &result2, | 266 resolver_->Resolve(request_info2, DEFAULT_PRIORITY, &result2, |
| 267 callback2.callback(), &request_handle2, | 267 callback2.callback(), &request2, BoundNetLog())); |
| 268 BoundNetLog())); | |
| 269 EXPECT_THAT(callback1.GetResult(ERR_IO_PENDING), IsOk()); | 268 EXPECT_THAT(callback1.GetResult(ERR_IO_PENDING), IsOk()); |
| 270 EXPECT_THAT(callback2.GetResult(ERR_IO_PENDING), | 269 EXPECT_THAT(callback2.GetResult(ERR_IO_PENDING), |
| 271 IsError(ERR_NAME_NOT_RESOLVED)); | 270 IsError(ERR_NAME_NOT_RESOLVED)); |
| 272 ASSERT_EQ(1u, result1.size()); | 271 ASSERT_EQ(1u, result1.size()); |
| 273 EXPECT_EQ(address_list[0], result1[0]); | 272 EXPECT_EQ(address_list[0], result1[0]); |
| 274 ASSERT_EQ(0u, result2.size()); | 273 ASSERT_EQ(0u, result2.size()); |
| 275 | 274 |
| 276 ASSERT_EQ(2u, mock_resolver_->requests().size()); | 275 ASSERT_EQ(2u, mock_resolver_->requests().size()); |
| 277 interfaces::HostResolverRequestInfo& request1 = | 276 interfaces::HostResolverRequestInfo& info1 = *mock_resolver_->requests()[0]; |
| 278 *mock_resolver_->requests()[0]; | 277 EXPECT_EQ("example.com", info1.host.To<std::string>()); |
| 279 EXPECT_EQ("example.com", request1.host.To<std::string>()); | 278 EXPECT_EQ(12345, info1.port); |
| 280 EXPECT_EQ(12345, request1.port); | 279 EXPECT_EQ(interfaces::AddressFamily::IPV4, info1.address_family); |
| 281 EXPECT_EQ(interfaces::AddressFamily::IPV4, request1.address_family); | 280 EXPECT_TRUE(info1.is_my_ip_address); |
| 282 EXPECT_TRUE(request1.is_my_ip_address); | 281 interfaces::HostResolverRequestInfo& info2 = *mock_resolver_->requests()[1]; |
| 283 interfaces::HostResolverRequestInfo& request2 = | 282 EXPECT_EQ("example.org", info2.host.To<std::string>()); |
| 284 *mock_resolver_->requests()[1]; | 283 EXPECT_EQ(80, info2.port); |
| 285 EXPECT_EQ("example.org", request2.host.To<std::string>()); | 284 EXPECT_EQ(interfaces::AddressFamily::IPV6, info2.address_family); |
| 286 EXPECT_EQ(80, request2.port); | 285 EXPECT_FALSE(info2.is_my_ip_address); |
| 287 EXPECT_EQ(interfaces::AddressFamily::IPV6, request2.address_family); | |
| 288 EXPECT_FALSE(request2.is_my_ip_address); | |
| 289 } | 286 } |
| 290 | 287 |
| 291 TEST_F(HostResolverMojoTest, Error) { | 288 TEST_F(HostResolverMojoTest, Error) { |
| 292 mock_resolver_->AddAction( | 289 mock_resolver_->AddAction( |
| 293 HostResolverAction::ReturnError(ERR_NAME_NOT_RESOLVED)); | 290 HostResolverAction::ReturnError(ERR_NAME_NOT_RESOLVED)); |
| 294 HostResolver::RequestInfo request_info( | 291 HostResolver::RequestInfo request_info( |
| 295 HostPortPair::FromString("example.com:8080")); | 292 HostPortPair::FromString("example.com:8080")); |
| 296 request_info.set_address_family(ADDRESS_FAMILY_IPV4); | 293 request_info.set_address_family(ADDRESS_FAMILY_IPV4); |
| 297 AddressList result; | 294 AddressList result; |
| 298 EXPECT_THAT(Resolve(request_info, &result), IsError(ERR_NAME_NOT_RESOLVED)); | 295 EXPECT_THAT(Resolve(request_info, &result), IsError(ERR_NAME_NOT_RESOLVED)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 316 | 313 |
| 317 ASSERT_EQ(1u, mock_resolver_->requests().size()); | 314 ASSERT_EQ(1u, mock_resolver_->requests().size()); |
| 318 } | 315 } |
| 319 | 316 |
| 320 TEST_F(HostResolverMojoTest, Cancel) { | 317 TEST_F(HostResolverMojoTest, Cancel) { |
| 321 mock_resolver_->AddAction(HostResolverAction::RetainRequest()); | 318 mock_resolver_->AddAction(HostResolverAction::RetainRequest()); |
| 322 HostResolver::RequestInfo request_info( | 319 HostResolver::RequestInfo request_info( |
| 323 HostPortPair::FromString("example.com:80")); | 320 HostPortPair::FromString("example.com:80")); |
| 324 request_info.set_address_family(ADDRESS_FAMILY_IPV6); | 321 request_info.set_address_family(ADDRESS_FAMILY_IPV6); |
| 325 AddressList result; | 322 AddressList result; |
| 326 HostResolver::RequestHandle request_handle = nullptr; | 323 std::unique_ptr<HostResolver::Request> request; |
| 327 resolver_->Resolve(request_info, DEFAULT_PRIORITY, &result, base::Bind(&Fail), | 324 resolver_->Resolve(request_info, DEFAULT_PRIORITY, &result, base::Bind(&Fail), |
| 328 &request_handle, BoundNetLog()); | 325 &request, BoundNetLog()); |
| 329 resolver_->CancelRequest(request_handle); | 326 request.reset(); |
| 330 waiter_.WaitForEvent(ConnectionErrorSource::REQUEST); | 327 waiter_.WaitForEvent(ConnectionErrorSource::REQUEST); |
| 331 EXPECT_TRUE(result.empty()); | 328 EXPECT_TRUE(result.empty()); |
| 332 | 329 |
| 333 ASSERT_EQ(1u, mock_resolver_->requests().size()); | 330 ASSERT_EQ(1u, mock_resolver_->requests().size()); |
| 334 interfaces::HostResolverRequestInfo& request = *mock_resolver_->requests()[0]; | 331 interfaces::HostResolverRequestInfo& info1 = *mock_resolver_->requests()[0]; |
| 335 EXPECT_EQ("example.com", request.host.To<std::string>()); | 332 EXPECT_EQ("example.com", info1.host.To<std::string>()); |
| 336 EXPECT_EQ(80, request.port); | 333 EXPECT_EQ(80, info1.port); |
| 337 EXPECT_EQ(interfaces::AddressFamily::IPV6, request.address_family); | 334 EXPECT_EQ(interfaces::AddressFamily::IPV6, info1.address_family); |
| 338 EXPECT_FALSE(request.is_my_ip_address); | 335 EXPECT_FALSE(info1.is_my_ip_address); |
| 339 } | 336 } |
| 340 | 337 |
| 341 TEST_F(HostResolverMojoTest, ImplDropsClientConnection) { | 338 TEST_F(HostResolverMojoTest, ImplDropsClientConnection) { |
| 342 mock_resolver_->AddAction(HostResolverAction::DropRequest()); | 339 mock_resolver_->AddAction(HostResolverAction::DropRequest()); |
| 343 HostResolver::RequestInfo request_info( | 340 HostResolver::RequestInfo request_info( |
| 344 HostPortPair::FromString("example.com:1")); | 341 HostPortPair::FromString("example.com:1")); |
| 345 AddressList result; | 342 AddressList result; |
| 346 EXPECT_THAT(Resolve(request_info, &result), IsError(ERR_FAILED)); | 343 EXPECT_THAT(Resolve(request_info, &result), IsError(ERR_FAILED)); |
| 347 EXPECT_TRUE(result.empty()); | 344 EXPECT_TRUE(result.empty()); |
| 348 | 345 |
| 349 ASSERT_EQ(1u, mock_resolver_->requests().size()); | 346 ASSERT_EQ(1u, mock_resolver_->requests().size()); |
| 350 interfaces::HostResolverRequestInfo& request = *mock_resolver_->requests()[0]; | 347 interfaces::HostResolverRequestInfo& info2 = *mock_resolver_->requests()[0]; |
| 351 EXPECT_EQ("example.com", request.host.To<std::string>()); | 348 EXPECT_EQ("example.com", info2.host.To<std::string>()); |
| 352 EXPECT_EQ(1, request.port); | 349 EXPECT_EQ(1, info2.port); |
| 353 EXPECT_EQ(interfaces::AddressFamily::UNSPECIFIED, request.address_family); | 350 EXPECT_EQ(interfaces::AddressFamily::UNSPECIFIED, info2.address_family); |
| 354 EXPECT_FALSE(request.is_my_ip_address); | 351 EXPECT_FALSE(info2.is_my_ip_address); |
| 355 } | 352 } |
| 356 | 353 |
| 357 TEST_F(HostResolverMojoTest, ResolveFromCache_Miss) { | 354 TEST_F(HostResolverMojoTest, ResolveFromCache_Miss) { |
| 358 HostResolver::RequestInfo request_info( | 355 HostResolver::RequestInfo request_info( |
| 359 HostPortPair::FromString("example.com:8080")); | 356 HostPortPair::FromString("example.com:8080")); |
| 360 AddressList result; | 357 AddressList result; |
| 361 EXPECT_EQ(ERR_DNS_CACHE_MISS, | 358 EXPECT_EQ(ERR_DNS_CACHE_MISS, |
| 362 resolver_->ResolveFromCache(request_info, &result, BoundNetLog())); | 359 resolver_->ResolveFromCache(request_info, &result, BoundNetLog())); |
| 363 EXPECT_TRUE(result.empty()); | 360 EXPECT_TRUE(result.empty()); |
| 364 } | 361 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 EXPECT_EQ(ERR_DNS_CACHE_MISS, | 400 EXPECT_EQ(ERR_DNS_CACHE_MISS, |
| 404 resolver_->ResolveFromCache(request_info, &result, BoundNetLog())); | 401 resolver_->ResolveFromCache(request_info, &result, BoundNetLog())); |
| 405 EXPECT_TRUE(result.empty()); | 402 EXPECT_TRUE(result.empty()); |
| 406 } | 403 } |
| 407 | 404 |
| 408 TEST_F(HostResolverMojoTest, GetHostCache) { | 405 TEST_F(HostResolverMojoTest, GetHostCache) { |
| 409 EXPECT_TRUE(resolver_->GetHostCache()); | 406 EXPECT_TRUE(resolver_->GetHostCache()); |
| 410 } | 407 } |
| 411 | 408 |
| 412 } // namespace net | 409 } // namespace net |
| OLD | NEW |