| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/base/host_resolver_impl.h" | 5 #include "net/base/host_resolver_impl.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <ws2tcpip.h> | 8 #include <ws2tcpip.h> |
| 9 #include <wspiapi.h> | 9 #include <wspiapi.h> |
| 10 #elif defined(OS_POSIX) | 10 #elif defined(OS_POSIX) |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 }; | 84 }; |
| 85 | 85 |
| 86 ResolveRequest(net::HostResolver* resolver, | 86 ResolveRequest(net::HostResolver* resolver, |
| 87 const std::string& hostname, | 87 const std::string& hostname, |
| 88 int port, | 88 int port, |
| 89 Delegate* delegate) | 89 Delegate* delegate) |
| 90 : info_(hostname, port), resolver_(resolver), delegate_(delegate), | 90 : info_(hostname, port), resolver_(resolver), delegate_(delegate), |
| 91 ALLOW_THIS_IN_INITIALIZER_LIST( | 91 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 92 callback_(this, &ResolveRequest::OnLookupFinished)) { | 92 callback_(this, &ResolveRequest::OnLookupFinished)) { |
| 93 // Start the request. | 93 // Start the request. |
| 94 int err = resolver->Resolve(info_, &addrlist_, &callback_, &req_); | 94 int err = resolver->Resolve(NULL, info_, &addrlist_, &callback_, &req_); |
| 95 EXPECT_EQ(net::ERR_IO_PENDING, err); | 95 EXPECT_EQ(net::ERR_IO_PENDING, err); |
| 96 } | 96 } |
| 97 | 97 |
| 98 ResolveRequest(net::HostResolver* resolver, | 98 ResolveRequest(net::HostResolver* resolver, |
| 99 const net::HostResolver::RequestInfo& info, | 99 const net::HostResolver::RequestInfo& info, |
| 100 Delegate* delegate) | 100 Delegate* delegate) |
| 101 : info_(info), resolver_(resolver), delegate_(delegate), | 101 : info_(info), resolver_(resolver), delegate_(delegate), |
| 102 ALLOW_THIS_IN_INITIALIZER_LIST( | 102 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 103 callback_(this, &ResolveRequest::OnLookupFinished)) { | 103 callback_(this, &ResolveRequest::OnLookupFinished)) { |
| 104 // Start the request. | 104 // Start the request. |
| 105 int err = resolver->Resolve(info, &addrlist_, &callback_, &req_); | 105 int err = resolver->Resolve(NULL, info, &addrlist_, &callback_, &req_); |
| 106 EXPECT_EQ(net::ERR_IO_PENDING, err); | 106 EXPECT_EQ(net::ERR_IO_PENDING, err); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void Cancel() { | 109 void Cancel() { |
| 110 resolver_->CancelRequest(req_); | 110 resolver_->CancelRequest(req_); |
| 111 } | 111 } |
| 112 | 112 |
| 113 const std::string& hostname() const { | 113 const std::string& hostname() const { |
| 114 return info_.hostname(); | 114 return info_.hostname(); |
| 115 } | 115 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 const int kPortnum = 80; | 180 const int kPortnum = 80; |
| 181 | 181 |
| 182 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = | 182 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = |
| 183 new RuleBasedHostResolverProc(NULL); | 183 new RuleBasedHostResolverProc(NULL); |
| 184 resolver_proc->AddRule("just.testing", "192.168.1.42"); | 184 resolver_proc->AddRule("just.testing", "192.168.1.42"); |
| 185 | 185 |
| 186 scoped_refptr<net::HostResolver> host_resolver( | 186 scoped_refptr<net::HostResolver> host_resolver( |
| 187 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); | 187 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 188 | 188 |
| 189 net::HostResolver::RequestInfo info("just.testing", kPortnum); | 189 net::HostResolver::RequestInfo info("just.testing", kPortnum); |
| 190 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL); | 190 int err = host_resolver->Resolve(NULL, info, &adrlist, NULL, NULL); |
| 191 EXPECT_EQ(net::OK, err); | 191 EXPECT_EQ(net::OK, err); |
| 192 | 192 |
| 193 const struct addrinfo* ainfo = adrlist.head(); | 193 const struct addrinfo* ainfo = adrlist.head(); |
| 194 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); | 194 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); |
| 195 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); | 195 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); |
| 196 | 196 |
| 197 const struct sockaddr* sa = ainfo->ai_addr; | 197 const struct sockaddr* sa = ainfo->ai_addr; |
| 198 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; | 198 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; |
| 199 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); | 199 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); |
| 200 EXPECT_TRUE(htonl(0xc0a8012a) == sa_in->sin_addr.s_addr); | 200 EXPECT_TRUE(htonl(0xc0a8012a) == sa_in->sin_addr.s_addr); |
| 201 } | 201 } |
| 202 | 202 |
| 203 TEST_F(HostResolverImplTest, AsynchronousLookup) { | 203 TEST_F(HostResolverImplTest, AsynchronousLookup) { |
| 204 net::AddressList adrlist; | 204 net::AddressList adrlist; |
| 205 const int kPortnum = 80; | 205 const int kPortnum = 80; |
| 206 | 206 |
| 207 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = | 207 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = |
| 208 new RuleBasedHostResolverProc(NULL); | 208 new RuleBasedHostResolverProc(NULL); |
| 209 resolver_proc->AddRule("just.testing", "192.168.1.42"); | 209 resolver_proc->AddRule("just.testing", "192.168.1.42"); |
| 210 | 210 |
| 211 scoped_refptr<net::HostResolver> host_resolver( | 211 scoped_refptr<net::HostResolver> host_resolver( |
| 212 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); | 212 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 213 | 213 |
| 214 net::HostResolver::RequestInfo info("just.testing", kPortnum); | 214 net::HostResolver::RequestInfo info("just.testing", kPortnum); |
| 215 int err = host_resolver->Resolve(info, &adrlist, &callback_, NULL); | 215 int err = host_resolver->Resolve(NULL, info, &adrlist, &callback_, NULL); |
| 216 EXPECT_EQ(net::ERR_IO_PENDING, err); | 216 EXPECT_EQ(net::ERR_IO_PENDING, err); |
| 217 | 217 |
| 218 MessageLoop::current()->Run(); | 218 MessageLoop::current()->Run(); |
| 219 | 219 |
| 220 ASSERT_TRUE(callback_called_); | 220 ASSERT_TRUE(callback_called_); |
| 221 ASSERT_EQ(net::OK, callback_result_); | 221 ASSERT_EQ(net::OK, callback_result_); |
| 222 | 222 |
| 223 const struct addrinfo* ainfo = adrlist.head(); | 223 const struct addrinfo* ainfo = adrlist.head(); |
| 224 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); | 224 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); |
| 225 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); | 225 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); |
| 226 | 226 |
| 227 const struct sockaddr* sa = ainfo->ai_addr; | 227 const struct sockaddr* sa = ainfo->ai_addr; |
| 228 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; | 228 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; |
| 229 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); | 229 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); |
| 230 EXPECT_TRUE(htonl(0xc0a8012a) == sa_in->sin_addr.s_addr); | 230 EXPECT_TRUE(htonl(0xc0a8012a) == sa_in->sin_addr.s_addr); |
| 231 } | 231 } |
| 232 | 232 |
| 233 TEST_F(HostResolverImplTest, CanceledAsynchronousLookup) { | 233 TEST_F(HostResolverImplTest, CanceledAsynchronousLookup) { |
| 234 scoped_refptr<WaitingHostResolverProc> resolver_proc = | 234 scoped_refptr<WaitingHostResolverProc> resolver_proc = |
| 235 new WaitingHostResolverProc(NULL); | 235 new WaitingHostResolverProc(NULL); |
| 236 | 236 |
| 237 { | 237 { |
| 238 scoped_refptr<net::HostResolver> host_resolver( | 238 scoped_refptr<net::HostResolver> host_resolver( |
| 239 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); | 239 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 240 net::AddressList adrlist; | 240 net::AddressList adrlist; |
| 241 const int kPortnum = 80; | 241 const int kPortnum = 80; |
| 242 | 242 |
| 243 net::HostResolver::RequestInfo info("just.testing", kPortnum); | 243 net::HostResolver::RequestInfo info("just.testing", kPortnum); |
| 244 int err = host_resolver->Resolve(info, &adrlist, &callback_, NULL); | 244 int err = host_resolver->Resolve(NULL, info, &adrlist, &callback_, NULL); |
| 245 EXPECT_EQ(net::ERR_IO_PENDING, err); | 245 EXPECT_EQ(net::ERR_IO_PENDING, err); |
| 246 | 246 |
| 247 // Make sure we will exit the queue even when callback is not called. | 247 // Make sure we will exit the queue even when callback is not called. |
| 248 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 248 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 249 new MessageLoop::QuitTask(), | 249 new MessageLoop::QuitTask(), |
| 250 1000); | 250 1000); |
| 251 MessageLoop::current()->Run(); | 251 MessageLoop::current()->Run(); |
| 252 } | 252 } |
| 253 | 253 |
| 254 resolver_proc->Signal(); | 254 resolver_proc->Signal(); |
| 255 | 255 |
| 256 EXPECT_FALSE(callback_called_); | 256 EXPECT_FALSE(callback_called_); |
| 257 } | 257 } |
| 258 | 258 |
| 259 TEST_F(HostResolverImplTest, NumericIPv4Address) { | 259 TEST_F(HostResolverImplTest, NumericIPv4Address) { |
| 260 // Stevens says dotted quads with AI_UNSPEC resolve to a single sockaddr_in. | 260 // Stevens says dotted quads with AI_UNSPEC resolve to a single sockaddr_in. |
| 261 | 261 |
| 262 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = | 262 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = |
| 263 new RuleBasedHostResolverProc(NULL); | 263 new RuleBasedHostResolverProc(NULL); |
| 264 resolver_proc->AllowDirectLookup("*"); | 264 resolver_proc->AllowDirectLookup("*"); |
| 265 | 265 |
| 266 scoped_refptr<net::HostResolver> host_resolver( | 266 scoped_refptr<net::HostResolver> host_resolver( |
| 267 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); | 267 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 268 net::AddressList adrlist; | 268 net::AddressList adrlist; |
| 269 const int kPortnum = 5555; | 269 const int kPortnum = 5555; |
| 270 net::HostResolver::RequestInfo info("127.1.2.3", kPortnum); | 270 net::HostResolver::RequestInfo info("127.1.2.3", kPortnum); |
| 271 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL); | 271 int err = host_resolver->Resolve(NULL, info, &adrlist, NULL, NULL); |
| 272 EXPECT_EQ(net::OK, err); | 272 EXPECT_EQ(net::OK, err); |
| 273 | 273 |
| 274 const struct addrinfo* ainfo = adrlist.head(); | 274 const struct addrinfo* ainfo = adrlist.head(); |
| 275 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); | 275 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); |
| 276 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); | 276 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); |
| 277 | 277 |
| 278 const struct sockaddr* sa = ainfo->ai_addr; | 278 const struct sockaddr* sa = ainfo->ai_addr; |
| 279 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; | 279 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; |
| 280 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); | 280 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); |
| 281 EXPECT_TRUE(htonl(0x7f010203) == sa_in->sin_addr.s_addr); | 281 EXPECT_TRUE(htonl(0x7f010203) == sa_in->sin_addr.s_addr); |
| 282 } | 282 } |
| 283 | 283 |
| 284 TEST_F(HostResolverImplTest, NumericIPv6Address) { | 284 TEST_F(HostResolverImplTest, NumericIPv6Address) { |
| 285 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = | 285 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = |
| 286 new RuleBasedHostResolverProc(NULL); | 286 new RuleBasedHostResolverProc(NULL); |
| 287 resolver_proc->AllowDirectLookup("*"); | 287 resolver_proc->AllowDirectLookup("*"); |
| 288 | 288 |
| 289 // Resolve a plain IPv6 address. Don't worry about [brackets], because | 289 // Resolve a plain IPv6 address. Don't worry about [brackets], because |
| 290 // the caller should have removed them. | 290 // the caller should have removed them. |
| 291 scoped_refptr<net::HostResolver> host_resolver( | 291 scoped_refptr<net::HostResolver> host_resolver( |
| 292 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); | 292 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 293 net::AddressList adrlist; | 293 net::AddressList adrlist; |
| 294 const int kPortnum = 5555; | 294 const int kPortnum = 5555; |
| 295 net::HostResolver::RequestInfo info("2001:db8::1", kPortnum); | 295 net::HostResolver::RequestInfo info("2001:db8::1", kPortnum); |
| 296 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL); | 296 int err = host_resolver->Resolve(NULL, info, &adrlist, NULL, NULL); |
| 297 // On computers without IPv6 support, getaddrinfo cannot convert IPv6 | 297 // On computers without IPv6 support, getaddrinfo cannot convert IPv6 |
| 298 // address literals to addresses (getaddrinfo returns EAI_NONAME). So this | 298 // address literals to addresses (getaddrinfo returns EAI_NONAME). So this |
| 299 // test has to allow host_resolver->Resolve to fail. | 299 // test has to allow host_resolver->Resolve to fail. |
| 300 if (err == net::ERR_NAME_NOT_RESOLVED) | 300 if (err == net::ERR_NAME_NOT_RESOLVED) |
| 301 return; | 301 return; |
| 302 EXPECT_EQ(net::OK, err); | 302 EXPECT_EQ(net::OK, err); |
| 303 | 303 |
| 304 const struct addrinfo* ainfo = adrlist.head(); | 304 const struct addrinfo* ainfo = adrlist.head(); |
| 305 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); | 305 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); |
| 306 EXPECT_EQ(sizeof(struct sockaddr_in6), ainfo->ai_addrlen); | 306 EXPECT_EQ(sizeof(struct sockaddr_in6), ainfo->ai_addrlen); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 326 TEST_F(HostResolverImplTest, DISABLED_EmptyHost) { | 326 TEST_F(HostResolverImplTest, DISABLED_EmptyHost) { |
| 327 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = | 327 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = |
| 328 new RuleBasedHostResolverProc(NULL); | 328 new RuleBasedHostResolverProc(NULL); |
| 329 resolver_proc->AllowDirectLookup("*"); | 329 resolver_proc->AllowDirectLookup("*"); |
| 330 | 330 |
| 331 scoped_refptr<net::HostResolver> host_resolver( | 331 scoped_refptr<net::HostResolver> host_resolver( |
| 332 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); | 332 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 333 net::AddressList adrlist; | 333 net::AddressList adrlist; |
| 334 const int kPortnum = 5555; | 334 const int kPortnum = 5555; |
| 335 net::HostResolver::RequestInfo info("", kPortnum); | 335 net::HostResolver::RequestInfo info("", kPortnum); |
| 336 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL); | 336 int err = host_resolver->Resolve(NULL, info, &adrlist, NULL, NULL); |
| 337 EXPECT_EQ(net::ERR_NAME_NOT_RESOLVED, err); | 337 EXPECT_EQ(net::ERR_NAME_NOT_RESOLVED, err); |
| 338 } | 338 } |
| 339 | 339 |
| 340 // Helper class used by HostResolverImplTest.DeDupeRequests. It receives request | 340 // Helper class used by HostResolverImplTest.DeDupeRequests. It receives request |
| 341 // completion notifications for all the resolves, so it can tally up and | 341 // completion notifications for all the resolves, so it can tally up and |
| 342 // determine when we are done. | 342 // determine when we are done. |
| 343 class DeDupeRequestsVerifier : public ResolveRequest::Delegate { | 343 class DeDupeRequestsVerifier : public ResolveRequest::Delegate { |
| 344 public: | 344 public: |
| 345 explicit DeDupeRequestsVerifier(CapturingHostResolverProc* resolver_proc) | 345 explicit DeDupeRequestsVerifier(CapturingHostResolverProc* resolver_proc) |
| 346 : count_a_(0), count_b_(0), resolver_proc_(resolver_proc) {} | 346 : count_a_(0), count_b_(0), resolver_proc_(resolver_proc) {} |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 677 // Since caching is enabled, this should complete synchronously. | 677 // Since caching is enabled, this should complete synchronously. |
| 678 | 678 |
| 679 // Note that |junk_callback| shouldn't be used since we are going to | 679 // Note that |junk_callback| shouldn't be used since we are going to |
| 680 // complete synchronously. We can't specify NULL though since that would | 680 // complete synchronously. We can't specify NULL though since that would |
| 681 // mean synchronous mode so we give it a value of 1. | 681 // mean synchronous mode so we give it a value of 1. |
| 682 net::CompletionCallback* junk_callback = | 682 net::CompletionCallback* junk_callback = |
| 683 reinterpret_cast<net::CompletionCallback*> (1); | 683 reinterpret_cast<net::CompletionCallback*> (1); |
| 684 net::AddressList addrlist; | 684 net::AddressList addrlist; |
| 685 | 685 |
| 686 net::HostResolver::RequestInfo info("a", 70); | 686 net::HostResolver::RequestInfo info("a", 70); |
| 687 int error = resolver->Resolve(info, &addrlist, junk_callback, NULL); | 687 int error = resolver->Resolve(NULL, info, &addrlist, junk_callback, NULL); |
| 688 EXPECT_EQ(net::OK, error); | 688 EXPECT_EQ(net::OK, error); |
| 689 | 689 |
| 690 // Ok good. Now make sure that if we ask to bypass the cache, it can no | 690 // Ok good. Now make sure that if we ask to bypass the cache, it can no |
| 691 // longer service the request synchronously. | 691 // longer service the request synchronously. |
| 692 info = net::HostResolver::RequestInfo("a", 71); | 692 info = net::HostResolver::RequestInfo("a", 71); |
| 693 info.set_allow_cached_response(false); | 693 info.set_allow_cached_response(false); |
| 694 final_request_.reset(new ResolveRequest(resolver, info, this)); | 694 final_request_.reset(new ResolveRequest(resolver, info, this)); |
| 695 } else if (71 == resolve->port()) { | 695 } else if (71 == resolve->port()) { |
| 696 // Test is done. | 696 // Test is done. |
| 697 MessageLoop::current()->Quit(); | 697 MessageLoop::current()->Quit(); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 794 new HostResolverImpl(NULL, kMaxCacheEntries, kMaxCacheAgeMs)); | 794 new HostResolverImpl(NULL, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 795 | 795 |
| 796 CapturingObserver observer; | 796 CapturingObserver observer; |
| 797 | 797 |
| 798 host_resolver->AddObserver(&observer); | 798 host_resolver->AddObserver(&observer); |
| 799 | 799 |
| 800 net::AddressList addrlist; | 800 net::AddressList addrlist; |
| 801 | 801 |
| 802 // Resolve "host1". | 802 // Resolve "host1". |
| 803 net::HostResolver::RequestInfo info1("host1", 70); | 803 net::HostResolver::RequestInfo info1("host1", 70); |
| 804 int rv = host_resolver->Resolve(info1, &addrlist, NULL, NULL); | 804 int rv = host_resolver->Resolve(NULL, info1, &addrlist, NULL, NULL); |
| 805 EXPECT_EQ(net::OK, rv); | 805 EXPECT_EQ(net::OK, rv); |
| 806 | 806 |
| 807 EXPECT_EQ(1U, observer.start_log.size()); | 807 EXPECT_EQ(1U, observer.start_log.size()); |
| 808 EXPECT_EQ(1U, observer.finish_log.size()); | 808 EXPECT_EQ(1U, observer.finish_log.size()); |
| 809 EXPECT_EQ(0U, observer.cancel_log.size()); | 809 EXPECT_EQ(0U, observer.cancel_log.size()); |
| 810 EXPECT_TRUE(observer.start_log[0] == | 810 EXPECT_TRUE(observer.start_log[0] == |
| 811 CapturingObserver::StartOrCancelEntry(0, info1)); | 811 CapturingObserver::StartOrCancelEntry(0, info1)); |
| 812 EXPECT_TRUE(observer.finish_log[0] == | 812 EXPECT_TRUE(observer.finish_log[0] == |
| 813 CapturingObserver::FinishEntry(0, true, info1)); | 813 CapturingObserver::FinishEntry(0, true, info1)); |
| 814 | 814 |
| 815 // Resolve "host1" again -- this time it will be served from cache, but it | 815 // Resolve "host1" again -- this time it will be served from cache, but it |
| 816 // should still notify of completion. | 816 // should still notify of completion. |
| 817 TestCompletionCallback callback; | 817 TestCompletionCallback callback; |
| 818 rv = host_resolver->Resolve(info1, &addrlist, &callback, NULL); | 818 rv = host_resolver->Resolve(NULL, info1, &addrlist, &callback, NULL); |
| 819 ASSERT_EQ(net::OK, rv); // Should complete synchronously. | 819 ASSERT_EQ(net::OK, rv); // Should complete synchronously. |
| 820 | 820 |
| 821 EXPECT_EQ(2U, observer.start_log.size()); | 821 EXPECT_EQ(2U, observer.start_log.size()); |
| 822 EXPECT_EQ(2U, observer.finish_log.size()); | 822 EXPECT_EQ(2U, observer.finish_log.size()); |
| 823 EXPECT_EQ(0U, observer.cancel_log.size()); | 823 EXPECT_EQ(0U, observer.cancel_log.size()); |
| 824 EXPECT_TRUE(observer.start_log[1] == | 824 EXPECT_TRUE(observer.start_log[1] == |
| 825 CapturingObserver::StartOrCancelEntry(1, info1)); | 825 CapturingObserver::StartOrCancelEntry(1, info1)); |
| 826 EXPECT_TRUE(observer.finish_log[1] == | 826 EXPECT_TRUE(observer.finish_log[1] == |
| 827 CapturingObserver::FinishEntry(1, true, info1)); | 827 CapturingObserver::FinishEntry(1, true, info1)); |
| 828 | 828 |
| 829 // Resolve "host2", setting referrer to "http://foobar.com" | 829 // Resolve "host2", setting referrer to "http://foobar.com" |
| 830 net::HostResolver::RequestInfo info2("host2", 70); | 830 net::HostResolver::RequestInfo info2("host2", 70); |
| 831 info2.set_referrer(GURL("http://foobar.com")); | 831 info2.set_referrer(GURL("http://foobar.com")); |
| 832 rv = host_resolver->Resolve(info2, &addrlist, NULL, NULL); | 832 rv = host_resolver->Resolve(NULL, info2, &addrlist, NULL, NULL); |
| 833 EXPECT_EQ(net::OK, rv); | 833 EXPECT_EQ(net::OK, rv); |
| 834 | 834 |
| 835 EXPECT_EQ(3U, observer.start_log.size()); | 835 EXPECT_EQ(3U, observer.start_log.size()); |
| 836 EXPECT_EQ(3U, observer.finish_log.size()); | 836 EXPECT_EQ(3U, observer.finish_log.size()); |
| 837 EXPECT_EQ(0U, observer.cancel_log.size()); | 837 EXPECT_EQ(0U, observer.cancel_log.size()); |
| 838 EXPECT_TRUE(observer.start_log[2] == | 838 EXPECT_TRUE(observer.start_log[2] == |
| 839 CapturingObserver::StartOrCancelEntry(2, info2)); | 839 CapturingObserver::StartOrCancelEntry(2, info2)); |
| 840 EXPECT_TRUE(observer.finish_log[2] == | 840 EXPECT_TRUE(observer.finish_log[2] == |
| 841 CapturingObserver::FinishEntry(2, true, info2)); | 841 CapturingObserver::FinishEntry(2, true, info2)); |
| 842 | 842 |
| 843 // Unregister the observer. | 843 // Unregister the observer. |
| 844 host_resolver->RemoveObserver(&observer); | 844 host_resolver->RemoveObserver(&observer); |
| 845 | 845 |
| 846 // Resolve "host3" | 846 // Resolve "host3" |
| 847 net::HostResolver::RequestInfo info3("host3", 70); | 847 net::HostResolver::RequestInfo info3("host3", 70); |
| 848 host_resolver->Resolve(info3, &addrlist, NULL, NULL); | 848 host_resolver->Resolve(NULL, info3, &addrlist, NULL, NULL); |
| 849 | 849 |
| 850 // No effect this time, since observer was removed. | 850 // No effect this time, since observer was removed. |
| 851 EXPECT_EQ(3U, observer.start_log.size()); | 851 EXPECT_EQ(3U, observer.start_log.size()); |
| 852 EXPECT_EQ(3U, observer.finish_log.size()); | 852 EXPECT_EQ(3U, observer.finish_log.size()); |
| 853 EXPECT_EQ(0U, observer.cancel_log.size()); | 853 EXPECT_EQ(0U, observer.cancel_log.size()); |
| 854 } | 854 } |
| 855 | 855 |
| 856 // Tests that observers are sent OnCancelResolution() whenever a request is | 856 // Tests that observers are sent OnCancelResolution() whenever a request is |
| 857 // cancelled. There are two ways to cancel a request: | 857 // cancelled. There are two ways to cancel a request: |
| 858 // (1) Delete the HostResolver while job is outstanding. | 858 // (1) Delete the HostResolver while job is outstanding. |
| 859 // (2) Call HostResolver::CancelRequest() while a request is outstanding. | 859 // (2) Call HostResolver::CancelRequest() while a request is outstanding. |
| 860 TEST_F(HostResolverImplTest, CancellationObserver) { | 860 TEST_F(HostResolverImplTest, CancellationObserver) { |
| 861 CapturingObserver observer; | 861 CapturingObserver observer; |
| 862 { | 862 { |
| 863 // Create a host resolver and attach an observer. | 863 // Create a host resolver and attach an observer. |
| 864 scoped_refptr<net::HostResolver> host_resolver( | 864 scoped_refptr<net::HostResolver> host_resolver( |
| 865 new HostResolverImpl(NULL, kMaxCacheEntries, kMaxCacheAgeMs)); | 865 new HostResolverImpl(NULL, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 866 host_resolver->AddObserver(&observer); | 866 host_resolver->AddObserver(&observer); |
| 867 | 867 |
| 868 TestCompletionCallback callback; | 868 TestCompletionCallback callback; |
| 869 | 869 |
| 870 EXPECT_EQ(0U, observer.start_log.size()); | 870 EXPECT_EQ(0U, observer.start_log.size()); |
| 871 EXPECT_EQ(0U, observer.finish_log.size()); | 871 EXPECT_EQ(0U, observer.finish_log.size()); |
| 872 EXPECT_EQ(0U, observer.cancel_log.size()); | 872 EXPECT_EQ(0U, observer.cancel_log.size()); |
| 873 | 873 |
| 874 // Start an async resolve for (host1:70). | 874 // Start an async resolve for (host1:70). |
| 875 net::HostResolver::RequestInfo info1("host1", 70); | 875 net::HostResolver::RequestInfo info1("host1", 70); |
| 876 net::HostResolver::RequestHandle req = NULL; | 876 net::HostResolver::RequestHandle req = NULL; |
| 877 net::AddressList addrlist; | 877 net::AddressList addrlist; |
| 878 int rv = host_resolver->Resolve(info1, &addrlist, &callback, &req); | 878 int rv = host_resolver->Resolve(NULL, info1, &addrlist, &callback, &req); |
| 879 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 879 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 880 EXPECT_TRUE(NULL != req); | 880 EXPECT_TRUE(NULL != req); |
| 881 | 881 |
| 882 EXPECT_EQ(1U, observer.start_log.size()); | 882 EXPECT_EQ(1U, observer.start_log.size()); |
| 883 EXPECT_EQ(0U, observer.finish_log.size()); | 883 EXPECT_EQ(0U, observer.finish_log.size()); |
| 884 EXPECT_EQ(0U, observer.cancel_log.size()); | 884 EXPECT_EQ(0U, observer.cancel_log.size()); |
| 885 | 885 |
| 886 EXPECT_TRUE(observer.start_log[0] == | 886 EXPECT_TRUE(observer.start_log[0] == |
| 887 CapturingObserver::StartOrCancelEntry(0, info1)); | 887 CapturingObserver::StartOrCancelEntry(0, info1)); |
| 888 | 888 |
| 889 // Cancel the request. | 889 // Cancel the request. |
| 890 host_resolver->CancelRequest(req); | 890 host_resolver->CancelRequest(req); |
| 891 | 891 |
| 892 EXPECT_EQ(1U, observer.start_log.size()); | 892 EXPECT_EQ(1U, observer.start_log.size()); |
| 893 EXPECT_EQ(0U, observer.finish_log.size()); | 893 EXPECT_EQ(0U, observer.finish_log.size()); |
| 894 EXPECT_EQ(1U, observer.cancel_log.size()); | 894 EXPECT_EQ(1U, observer.cancel_log.size()); |
| 895 | 895 |
| 896 EXPECT_TRUE(observer.cancel_log[0] == | 896 EXPECT_TRUE(observer.cancel_log[0] == |
| 897 CapturingObserver::StartOrCancelEntry(0, info1)); | 897 CapturingObserver::StartOrCancelEntry(0, info1)); |
| 898 | 898 |
| 899 // Start an async request for (host2:60) | 899 // Start an async request for (host2:60) |
| 900 net::HostResolver::RequestInfo info2("host2", 60); | 900 net::HostResolver::RequestInfo info2("host2", 60); |
| 901 rv = host_resolver->Resolve(info2, &addrlist, &callback, NULL); | 901 rv = host_resolver->Resolve(NULL, info2, &addrlist, &callback, NULL); |
| 902 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 902 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 903 EXPECT_TRUE(NULL != req); | 903 EXPECT_TRUE(NULL != req); |
| 904 | 904 |
| 905 EXPECT_EQ(2U, observer.start_log.size()); | 905 EXPECT_EQ(2U, observer.start_log.size()); |
| 906 EXPECT_EQ(0U, observer.finish_log.size()); | 906 EXPECT_EQ(0U, observer.finish_log.size()); |
| 907 EXPECT_EQ(1U, observer.cancel_log.size()); | 907 EXPECT_EQ(1U, observer.cancel_log.size()); |
| 908 | 908 |
| 909 EXPECT_TRUE(observer.start_log[1] == | 909 EXPECT_TRUE(observer.start_log[1] == |
| 910 CapturingObserver::StartOrCancelEntry(1, info2)); | 910 CapturingObserver::StartOrCancelEntry(1, info2)); |
| 911 | 911 |
| 912 // Upon exiting this scope, HostResolver is destroyed, so all requests are | 912 // Upon exiting this scope, HostResolver is destroyed, so all requests are |
| 913 // implicitly cancelled. | 913 // implicitly cancelled. |
| 914 } | 914 } |
| 915 | 915 |
| 916 // Check that destroying the HostResolver sent a notification for | 916 // Check that destroying the HostResolver sent a notification for |
| 917 // cancellation of host2:60 request. | 917 // cancellation of host2:60 request. |
| 918 | 918 |
| 919 EXPECT_EQ(2U, observer.start_log.size()); | 919 EXPECT_EQ(2U, observer.start_log.size()); |
| 920 EXPECT_EQ(0U, observer.finish_log.size()); | 920 EXPECT_EQ(0U, observer.finish_log.size()); |
| 921 EXPECT_EQ(2U, observer.cancel_log.size()); | 921 EXPECT_EQ(2U, observer.cancel_log.size()); |
| 922 | 922 |
| 923 net::HostResolver::RequestInfo info("host2", 60); | 923 net::HostResolver::RequestInfo info("host2", 60); |
| 924 EXPECT_TRUE(observer.cancel_log[1] == | 924 EXPECT_TRUE(observer.cancel_log[1] == |
| 925 CapturingObserver::StartOrCancelEntry(1, info)); | 925 CapturingObserver::StartOrCancelEntry(1, info)); |
| 926 } | 926 } |
| 927 | 927 |
| 928 } // namespace | 928 } // namespace |
| OLD | NEW |