OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_impl.h" | 5 #include "net/dns/host_resolver_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <tuple> | 10 #include <tuple> |
(...skipping 2380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2391 &addresses)); | 2391 &addresses)); |
2392 EXPECT_FALSE(ResolveLocalHostname("::1:1", kLocalhostLookupPort, &addresses)); | 2392 EXPECT_FALSE(ResolveLocalHostname("::1:1", kLocalhostLookupPort, &addresses)); |
2393 EXPECT_FALSE(ResolveLocalHostname("0:0:0:0:0:0:0:0:1", kLocalhostLookupPort, | 2393 EXPECT_FALSE(ResolveLocalHostname("0:0:0:0:0:0:0:0:1", kLocalhostLookupPort, |
2394 &addresses)); | 2394 &addresses)); |
2395 EXPECT_FALSE(ResolveLocalHostname("foo.localhost.com", kLocalhostLookupPort, | 2395 EXPECT_FALSE(ResolveLocalHostname("foo.localhost.com", kLocalhostLookupPort, |
2396 &addresses)); | 2396 &addresses)); |
2397 EXPECT_FALSE( | 2397 EXPECT_FALSE( |
2398 ResolveLocalHostname("foo.localhoste", kLocalhostLookupPort, &addresses)); | 2398 ResolveLocalHostname("foo.localhoste", kLocalhostLookupPort, &addresses)); |
2399 } | 2399 } |
2400 | 2400 |
| 2401 void TestCacheHitCallback(int* callback_count, |
| 2402 HostResolver::RequestInfo* last_request_info, |
| 2403 const HostResolver::RequestInfo& request_info) { |
| 2404 ++*callback_count; |
| 2405 *last_request_info = request_info; |
| 2406 } |
| 2407 |
| 2408 TEST_F(HostResolverImplTest, CacheHitCallback) { |
| 2409 proc_->AddRuleForAllFamilies("just.testing", "192.168.1.42"); |
| 2410 proc_->SignalMultiple(5u); |
| 2411 |
| 2412 HostResolver::RequestInfo last_request_info(HostPortPair("unassigned", 80)); |
| 2413 |
| 2414 // Set a cache hit callback. |
| 2415 int count1 = 0; |
| 2416 HostResolver::RequestInfo info_callback1(HostPortPair("just.testing", 80)); |
| 2417 info_callback1.set_cache_hit_callback( |
| 2418 base::Bind(&TestCacheHitCallback, &count1, &last_request_info)); |
| 2419 Request* req = CreateRequest(info_callback1, MEDIUM); |
| 2420 EXPECT_THAT(req->Resolve(), IsError(ERR_IO_PENDING)); |
| 2421 EXPECT_THAT(req->WaitForResult(), IsOk()); |
| 2422 EXPECT_EQ(0, count1); |
| 2423 |
| 2424 // Make sure the cache hit callback is called, and set another one. |
| 2425 // Future requests should call *both* callbacks. |
| 2426 int count2 = 0; |
| 2427 HostResolver::RequestInfo info_callback2(HostPortPair("just.testing", 80)); |
| 2428 info_callback2.set_cache_hit_callback( |
| 2429 base::Bind(&TestCacheHitCallback, &count2, &last_request_info)); |
| 2430 req = CreateRequest(info_callback2, MEDIUM); |
| 2431 EXPECT_THAT(req->Resolve(), IsOk()); |
| 2432 EXPECT_EQ(1, count1); |
| 2433 EXPECT_EQ(0, count2); |
| 2434 |
| 2435 // Make another request to make sure both callbacks are called. |
| 2436 req = CreateRequest("just.testing", 80); |
| 2437 EXPECT_THAT(req->Resolve(), IsOk()); |
| 2438 EXPECT_EQ(2, count1); |
| 2439 EXPECT_EQ(1, count2); |
| 2440 |
| 2441 // Make an uncached request to clear the cache hit callbacks. |
| 2442 // (They should be cleared because the uncached request will write a new |
| 2443 // result into the cache.) |
| 2444 // It should not call the callbacks itself, since it doesn't hit the cache. |
| 2445 HostResolver::RequestInfo info_uncached(HostPortPair("just.testing", 80)); |
| 2446 info_uncached.set_allow_cached_response(false); |
| 2447 req = CreateRequest(info_uncached, MEDIUM); |
| 2448 EXPECT_THAT(req->Resolve(), IsError(ERR_IO_PENDING)); |
| 2449 EXPECT_THAT(req->WaitForResult(), IsOk()); |
| 2450 EXPECT_EQ(2, count1); |
| 2451 EXPECT_EQ(1, count2); |
| 2452 |
| 2453 // Make another request to make sure both callbacks were cleared. |
| 2454 req = CreateRequest("just.testing", 80); |
| 2455 EXPECT_THAT(req->Resolve(), IsOk()); |
| 2456 EXPECT_EQ(2, count1); |
| 2457 EXPECT_EQ(1, count2); |
| 2458 } |
| 2459 |
2401 } // namespace net | 2460 } // namespace net |
OLD | NEW |