| 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.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) |
| 11 #include <netdb.h> | 11 #include <netdb.h> |
| 12 #endif | 12 #endif |
| 13 | 13 |
| 14 #include <string> | 14 #include <string> |
| 15 | 15 |
| 16 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
| 17 #include "base/message_loop.h" | 17 #include "base/message_loop.h" |
| 18 #include "base/ref_counted.h" | 18 #include "base/ref_counted.h" |
| 19 #include "net/base/address_list.h" | 19 #include "net/base/address_list.h" |
| 20 #include "net/base/completion_callback.h" | 20 #include "net/base/completion_callback.h" |
| 21 #include "net/base/host_resolver_unittest.h" | 21 #include "net/base/mock_host_resolver.h" |
| 22 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
| 23 #include "net/base/test_completion_callback.h" | 23 #include "net/base/test_completion_callback.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 25 |
| 26 using net::RuleBasedHostMapper; | 26 using net::HostResolverImpl; |
| 27 using net::ScopedHostMapper; | 27 using net::RuleBasedHostResolverProc; |
| 28 using net::WaitingHostMapper; | 28 using net::WaitingHostResolverProc; |
| 29 | 29 |
| 30 // TODO(eroman): | 30 // TODO(eroman): |
| 31 // - Test mixing async with sync (in particular how does sync update the | 31 // - Test mixing async with sync (in particular how does sync update the |
| 32 // cache while an async is already pending). | 32 // cache while an async is already pending). |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 static const int kMaxCacheEntries = 100; |
| 36 static const int kMaxCacheAgeMs = 60000; |
| 35 | 37 |
| 36 // A variant of WaitingHostMapper that pushes each host mapped into a list. | 38 // A variant of WaitingHostResolverProc that pushes each host mapped into a |
| 39 // list. |
| 37 // (and uses a manual-reset event rather than auto-reset). | 40 // (and uses a manual-reset event rather than auto-reset). |
| 38 class CapturingHostMapper : public net::HostMapper { | 41 class CapturingHostResolverProc : public net::HostResolverProc { |
| 39 public: | 42 public: |
| 40 CapturingHostMapper() : event_(true, false) { | 43 explicit CapturingHostResolverProc(HostResolverProc* previous) |
| 44 : net::HostResolverProc(previous), event_(true, false) { |
| 41 } | 45 } |
| 42 | 46 |
| 43 void Signal() { | 47 void Signal() { |
| 44 event_.Signal(); | 48 event_.Signal(); |
| 45 } | 49 } |
| 46 | 50 |
| 47 virtual std::string Map(const std::string& host) { | 51 virtual int Resolve(const std::string& host, net::AddressList* addrlist) { |
| 48 event_.Wait(); | 52 event_.Wait(); |
| 49 { | 53 { |
| 50 AutoLock l(lock_); | 54 AutoLock l(lock_); |
| 51 capture_list_.push_back(host); | 55 capture_list_.push_back(host); |
| 52 } | 56 } |
| 53 return MapUsingPrevious(host); | 57 return ResolveUsingPrevious(host, addrlist); |
| 54 } | 58 } |
| 55 | 59 |
| 56 std::vector<std::string> GetCaptureList() const { | 60 std::vector<std::string> GetCaptureList() const { |
| 57 std::vector<std::string> copy; | 61 std::vector<std::string> copy; |
| 58 { | 62 { |
| 59 AutoLock l(lock_); | 63 AutoLock l(lock_); |
| 60 copy = capture_list_; | 64 copy = capture_list_; |
| 61 } | 65 } |
| 62 return copy; | 66 return copy; |
| 63 } | 67 } |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 } | 131 } |
| 128 | 132 |
| 129 private: | 133 private: |
| 130 void OnLookupFinished(int result) { | 134 void OnLookupFinished(int result) { |
| 131 result_ = result; | 135 result_ = result; |
| 132 delegate_->OnCompleted(this); | 136 delegate_->OnCompleted(this); |
| 133 } | 137 } |
| 134 | 138 |
| 135 // The request details. | 139 // The request details. |
| 136 net::HostResolver::RequestInfo info_; | 140 net::HostResolver::RequestInfo info_; |
| 137 net::HostResolver::Request* req_; | 141 net::HostResolver::RequestHandle req_; |
| 138 | 142 |
| 139 // The result of the resolve. | 143 // The result of the resolve. |
| 140 int result_; | 144 int result_; |
| 141 net::AddressList addrlist_; | 145 net::AddressList addrlist_; |
| 142 | 146 |
| 143 // We don't use a scoped_refptr, to simplify deleting shared resolver in | 147 // We don't use a scoped_refptr, to simplify deleting shared resolver in |
| 144 // DeleteWithinCallback test. | 148 // DeleteWithinCallback test. |
| 145 net::HostResolver* resolver_; | 149 net::HostResolver* resolver_; |
| 146 | 150 |
| 147 Delegate* delegate_; | 151 Delegate* delegate_; |
| 148 net::CompletionCallbackImpl<ResolveRequest> callback_; | 152 net::CompletionCallbackImpl<ResolveRequest> callback_; |
| 149 | 153 |
| 150 DISALLOW_COPY_AND_ASSIGN(ResolveRequest); | 154 DISALLOW_COPY_AND_ASSIGN(ResolveRequest); |
| 151 }; | 155 }; |
| 152 | 156 |
| 153 class HostResolverTest : public testing::Test { | 157 class HostResolverImplTest : public testing::Test { |
| 154 public: | 158 public: |
| 155 HostResolverTest() | 159 HostResolverImplTest() |
| 156 : callback_called_(false), | 160 : callback_called_(false), |
| 157 ALLOW_THIS_IN_INITIALIZER_LIST( | 161 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 158 callback_(this, &HostResolverTest::OnLookupFinished)) { | 162 callback_(this, &HostResolverImplTest::OnLookupFinished)) { |
| 159 } | 163 } |
| 160 | 164 |
| 161 protected: | 165 protected: |
| 162 bool callback_called_; | 166 bool callback_called_; |
| 163 int callback_result_; | 167 int callback_result_; |
| 164 net::CompletionCallbackImpl<HostResolverTest> callback_; | 168 net::CompletionCallbackImpl<HostResolverImplTest> callback_; |
| 165 | 169 |
| 166 private: | 170 private: |
| 167 void OnLookupFinished(int result) { | 171 void OnLookupFinished(int result) { |
| 168 callback_called_ = true; | 172 callback_called_ = true; |
| 169 callback_result_ = result; | 173 callback_result_ = result; |
| 170 MessageLoop::current()->Quit(); | 174 MessageLoop::current()->Quit(); |
| 171 } | 175 } |
| 172 }; | 176 }; |
| 173 | 177 |
| 174 TEST_F(HostResolverTest, SynchronousLookup) { | 178 TEST_F(HostResolverImplTest, SynchronousLookup) { |
| 175 scoped_refptr<net::HostResolver> host_resolver(new net::HostResolver); | |
| 176 net::AddressList adrlist; | 179 net::AddressList adrlist; |
| 177 const int kPortnum = 80; | 180 const int kPortnum = 80; |
| 178 | 181 |
| 179 scoped_refptr<RuleBasedHostMapper> mapper = new RuleBasedHostMapper(); | 182 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = |
| 180 mapper->AddRule("just.testing", "192.168.1.42"); | 183 new RuleBasedHostResolverProc(NULL); |
| 181 ScopedHostMapper scoped_mapper(mapper.get()); | 184 resolver_proc->AddRule("just.testing", "192.168.1.42"); |
| 185 |
| 186 scoped_refptr<net::HostResolver> host_resolver( |
| 187 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 182 | 188 |
| 183 net::HostResolver::RequestInfo info("just.testing", kPortnum); | 189 net::HostResolver::RequestInfo info("just.testing", kPortnum); |
| 184 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL); | 190 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL); |
| 185 EXPECT_EQ(net::OK, err); | 191 EXPECT_EQ(net::OK, err); |
| 186 | 192 |
| 187 const struct addrinfo* ainfo = adrlist.head(); | 193 const struct addrinfo* ainfo = adrlist.head(); |
| 188 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); | 194 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); |
| 189 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); | 195 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); |
| 190 | 196 |
| 191 const struct sockaddr* sa = ainfo->ai_addr; | 197 const struct sockaddr* sa = ainfo->ai_addr; |
| 192 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; | 198 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; |
| 193 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); | 199 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); |
| 194 EXPECT_TRUE(htonl(0xc0a8012a) == sa_in->sin_addr.s_addr); | 200 EXPECT_TRUE(htonl(0xc0a8012a) == sa_in->sin_addr.s_addr); |
| 195 } | 201 } |
| 196 | 202 |
| 197 TEST_F(HostResolverTest, AsynchronousLookup) { | 203 TEST_F(HostResolverImplTest, AsynchronousLookup) { |
| 198 scoped_refptr<net::HostResolver> host_resolver(new net::HostResolver); | |
| 199 net::AddressList adrlist; | 204 net::AddressList adrlist; |
| 200 const int kPortnum = 80; | 205 const int kPortnum = 80; |
| 201 | 206 |
| 202 scoped_refptr<RuleBasedHostMapper> mapper = new RuleBasedHostMapper(); | 207 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = |
| 203 mapper->AddRule("just.testing", "192.168.1.42"); | 208 new RuleBasedHostResolverProc(NULL); |
| 204 ScopedHostMapper scoped_mapper(mapper.get()); | 209 resolver_proc->AddRule("just.testing", "192.168.1.42"); |
| 210 |
| 211 scoped_refptr<net::HostResolver> host_resolver( |
| 212 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 205 | 213 |
| 206 net::HostResolver::RequestInfo info("just.testing", kPortnum); | 214 net::HostResolver::RequestInfo info("just.testing", kPortnum); |
| 207 int err = host_resolver->Resolve(info, &adrlist, &callback_, NULL); | 215 int err = host_resolver->Resolve(info, &adrlist, &callback_, NULL); |
| 208 EXPECT_EQ(net::ERR_IO_PENDING, err); | 216 EXPECT_EQ(net::ERR_IO_PENDING, err); |
| 209 | 217 |
| 210 MessageLoop::current()->Run(); | 218 MessageLoop::current()->Run(); |
| 211 | 219 |
| 212 ASSERT_TRUE(callback_called_); | 220 ASSERT_TRUE(callback_called_); |
| 213 ASSERT_EQ(net::OK, callback_result_); | 221 ASSERT_EQ(net::OK, callback_result_); |
| 214 | 222 |
| 215 const struct addrinfo* ainfo = adrlist.head(); | 223 const struct addrinfo* ainfo = adrlist.head(); |
| 216 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); | 224 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); |
| 217 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); | 225 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); |
| 218 | 226 |
| 219 const struct sockaddr* sa = ainfo->ai_addr; | 227 const struct sockaddr* sa = ainfo->ai_addr; |
| 220 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; | 228 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; |
| 221 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); | 229 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); |
| 222 EXPECT_TRUE(htonl(0xc0a8012a) == sa_in->sin_addr.s_addr); | 230 EXPECT_TRUE(htonl(0xc0a8012a) == sa_in->sin_addr.s_addr); |
| 223 } | 231 } |
| 224 | 232 |
| 225 TEST_F(HostResolverTest, CanceledAsynchronousLookup) { | 233 TEST_F(HostResolverImplTest, CanceledAsynchronousLookup) { |
| 226 scoped_refptr<WaitingHostMapper> mapper = new WaitingHostMapper(); | 234 scoped_refptr<WaitingHostResolverProc> resolver_proc = |
| 227 ScopedHostMapper scoped_mapper(mapper.get()); | 235 new WaitingHostResolverProc(NULL); |
| 228 | 236 |
| 229 { | 237 { |
| 230 scoped_refptr<net::HostResolver> host_resolver(new net::HostResolver); | 238 scoped_refptr<net::HostResolver> host_resolver( |
| 239 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 231 net::AddressList adrlist; | 240 net::AddressList adrlist; |
| 232 const int kPortnum = 80; | 241 const int kPortnum = 80; |
| 233 | 242 |
| 234 net::HostResolver::RequestInfo info("just.testing", kPortnum); | 243 net::HostResolver::RequestInfo info("just.testing", kPortnum); |
| 235 int err = host_resolver->Resolve(info, &adrlist, &callback_, NULL); | 244 int err = host_resolver->Resolve(info, &adrlist, &callback_, NULL); |
| 236 EXPECT_EQ(net::ERR_IO_PENDING, err); | 245 EXPECT_EQ(net::ERR_IO_PENDING, err); |
| 237 | 246 |
| 238 // 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. |
| 239 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 248 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 240 new MessageLoop::QuitTask(), | 249 new MessageLoop::QuitTask(), |
| 241 1000); | 250 1000); |
| 242 MessageLoop::current()->Run(); | 251 MessageLoop::current()->Run(); |
| 243 } | 252 } |
| 244 | 253 |
| 245 mapper->Signal(); | 254 resolver_proc->Signal(); |
| 246 | 255 |
| 247 EXPECT_FALSE(callback_called_); | 256 EXPECT_FALSE(callback_called_); |
| 248 } | 257 } |
| 249 | 258 |
| 250 TEST_F(HostResolverTest, NumericIPv4Address) { | 259 TEST_F(HostResolverImplTest, NumericIPv4Address) { |
| 251 // 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. |
| 252 | 261 |
| 253 scoped_refptr<RuleBasedHostMapper> mapper = new RuleBasedHostMapper(); | 262 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = |
| 254 mapper->AllowDirectLookup("*"); | 263 new RuleBasedHostResolverProc(NULL); |
| 255 ScopedHostMapper scoped_mapper(mapper.get()); | 264 resolver_proc->AllowDirectLookup("*"); |
| 256 | 265 |
| 257 scoped_refptr<net::HostResolver> host_resolver(new net::HostResolver); | 266 scoped_refptr<net::HostResolver> host_resolver( |
| 267 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 258 net::AddressList adrlist; | 268 net::AddressList adrlist; |
| 259 const int kPortnum = 5555; | 269 const int kPortnum = 5555; |
| 260 net::HostResolver::RequestInfo info("127.1.2.3", kPortnum); | 270 net::HostResolver::RequestInfo info("127.1.2.3", kPortnum); |
| 261 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL); | 271 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL); |
| 262 EXPECT_EQ(net::OK, err); | 272 EXPECT_EQ(net::OK, err); |
| 263 | 273 |
| 264 const struct addrinfo* ainfo = adrlist.head(); | 274 const struct addrinfo* ainfo = adrlist.head(); |
| 265 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); | 275 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); |
| 266 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); | 276 EXPECT_EQ(sizeof(struct sockaddr_in), ainfo->ai_addrlen); |
| 267 | 277 |
| 268 const struct sockaddr* sa = ainfo->ai_addr; | 278 const struct sockaddr* sa = ainfo->ai_addr; |
| 269 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; | 279 const struct sockaddr_in* sa_in = (const struct sockaddr_in*) sa; |
| 270 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); | 280 EXPECT_TRUE(htons(kPortnum) == sa_in->sin_port); |
| 271 EXPECT_TRUE(htonl(0x7f010203) == sa_in->sin_addr.s_addr); | 281 EXPECT_TRUE(htonl(0x7f010203) == sa_in->sin_addr.s_addr); |
| 272 } | 282 } |
| 273 | 283 |
| 274 TEST_F(HostResolverTest, NumericIPv6Address) { | 284 TEST_F(HostResolverImplTest, NumericIPv6Address) { |
| 275 scoped_refptr<RuleBasedHostMapper> mapper = new RuleBasedHostMapper(); | 285 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = |
| 276 mapper->AllowDirectLookup("*"); | 286 new RuleBasedHostResolverProc(NULL); |
| 277 ScopedHostMapper scoped_mapper(mapper.get()); | 287 resolver_proc->AllowDirectLookup("*"); |
| 278 | 288 |
| 279 // Resolve a plain IPv6 address. Don't worry about [brackets], because | 289 // Resolve a plain IPv6 address. Don't worry about [brackets], because |
| 280 // the caller should have removed them. | 290 // the caller should have removed them. |
| 281 scoped_refptr<net::HostResolver> host_resolver(new net::HostResolver); | 291 scoped_refptr<net::HostResolver> host_resolver( |
| 292 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 282 net::AddressList adrlist; | 293 net::AddressList adrlist; |
| 283 const int kPortnum = 5555; | 294 const int kPortnum = 5555; |
| 284 net::HostResolver::RequestInfo info("2001:db8::1", kPortnum); | 295 net::HostResolver::RequestInfo info("2001:db8::1", kPortnum); |
| 285 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL); | 296 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL); |
| 286 // On computers without IPv6 support, getaddrinfo cannot convert IPv6 | 297 // On computers without IPv6 support, getaddrinfo cannot convert IPv6 |
| 287 // address literals to addresses (getaddrinfo returns EAI_NONAME). So this | 298 // address literals to addresses (getaddrinfo returns EAI_NONAME). So this |
| 288 // test has to allow host_resolver->Resolve to fail. | 299 // test has to allow host_resolver->Resolve to fail. |
| 289 if (err == net::ERR_NAME_NOT_RESOLVED) | 300 if (err == net::ERR_NAME_NOT_RESOLVED) |
| 290 return; | 301 return; |
| 291 EXPECT_EQ(net::OK, err); | 302 EXPECT_EQ(net::OK, err); |
| 292 | 303 |
| 293 const struct addrinfo* ainfo = adrlist.head(); | 304 const struct addrinfo* ainfo = adrlist.head(); |
| 294 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); | 305 EXPECT_EQ(static_cast<addrinfo*>(NULL), ainfo->ai_next); |
| 295 EXPECT_EQ(sizeof(struct sockaddr_in6), ainfo->ai_addrlen); | 306 EXPECT_EQ(sizeof(struct sockaddr_in6), ainfo->ai_addrlen); |
| 296 | 307 |
| 297 const struct sockaddr* sa = ainfo->ai_addr; | 308 const struct sockaddr* sa = ainfo->ai_addr; |
| 298 const struct sockaddr_in6* sa_in6 = (const struct sockaddr_in6*) sa; | 309 const struct sockaddr_in6* sa_in6 = (const struct sockaddr_in6*) sa; |
| 299 EXPECT_TRUE(htons(kPortnum) == sa_in6->sin6_port); | 310 EXPECT_TRUE(htons(kPortnum) == sa_in6->sin6_port); |
| 300 | 311 |
| 301 const uint8 expect_addr[] = { | 312 const uint8 expect_addr[] = { |
| 302 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, | 313 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, |
| 303 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 | 314 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 |
| 304 }; | 315 }; |
| 305 for (int i = 0; i < 16; i++) { | 316 for (int i = 0; i < 16; i++) { |
| 306 EXPECT_EQ(expect_addr[i], sa_in6->sin6_addr.s6_addr[i]); | 317 EXPECT_EQ(expect_addr[i], sa_in6->sin6_addr.s6_addr[i]); |
| 307 } | 318 } |
| 308 } | 319 } |
| 309 | 320 |
| 310 TEST_F(HostResolverTest, EmptyHost) { | 321 TEST_F(HostResolverImplTest, EmptyHost) { |
| 311 scoped_refptr<RuleBasedHostMapper> mapper = new RuleBasedHostMapper(); | 322 scoped_refptr<RuleBasedHostResolverProc> resolver_proc = |
| 312 mapper->AllowDirectLookup("*"); | 323 new RuleBasedHostResolverProc(NULL); |
| 313 ScopedHostMapper scoped_mapper(mapper.get()); | 324 resolver_proc->AllowDirectLookup("*"); |
| 314 | 325 |
| 315 scoped_refptr<net::HostResolver> host_resolver(new net::HostResolver); | 326 scoped_refptr<net::HostResolver> host_resolver( |
| 327 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 316 net::AddressList adrlist; | 328 net::AddressList adrlist; |
| 317 const int kPortnum = 5555; | 329 const int kPortnum = 5555; |
| 318 net::HostResolver::RequestInfo info("", kPortnum); | 330 net::HostResolver::RequestInfo info("", kPortnum); |
| 319 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL); | 331 int err = host_resolver->Resolve(info, &adrlist, NULL, NULL); |
| 320 EXPECT_EQ(net::ERR_NAME_NOT_RESOLVED, err); | 332 EXPECT_EQ(net::ERR_NAME_NOT_RESOLVED, err); |
| 321 } | 333 } |
| 322 | 334 |
| 323 // Helper class used by HostResolverTest.DeDupeRequests. It receives request | 335 // Helper class used by HostResolverImplTest.DeDupeRequests. It receives request |
| 324 // completion notifications for all the resolves, so it can tally up and | 336 // completion notifications for all the resolves, so it can tally up and |
| 325 // determine when we are done. | 337 // determine when we are done. |
| 326 class DeDupeRequestsVerifier : public ResolveRequest::Delegate { | 338 class DeDupeRequestsVerifier : public ResolveRequest::Delegate { |
| 327 public: | 339 public: |
| 328 explicit DeDupeRequestsVerifier(CapturingHostMapper* mapper) | 340 explicit DeDupeRequestsVerifier(CapturingHostResolverProc* resolver_proc) |
| 329 : count_a_(0), count_b_(0), mapper_(mapper) {} | 341 : count_a_(0), count_b_(0), resolver_proc_(resolver_proc) {} |
| 330 | 342 |
| 331 // The test does 5 resolves (which can complete in any order). | 343 // The test does 5 resolves (which can complete in any order). |
| 332 virtual void OnCompleted(ResolveRequest* resolve) { | 344 virtual void OnCompleted(ResolveRequest* resolve) { |
| 333 // Tally up how many requests we have seen. | 345 // Tally up how many requests we have seen. |
| 334 if (resolve->hostname() == "a") { | 346 if (resolve->hostname() == "a") { |
| 335 count_a_++; | 347 count_a_++; |
| 336 } else if (resolve->hostname() == "b") { | 348 } else if (resolve->hostname() == "b") { |
| 337 count_b_++; | 349 count_b_++; |
| 338 } else { | 350 } else { |
| 339 FAIL() << "Unexpected hostname: " << resolve->hostname(); | 351 FAIL() << "Unexpected hostname: " << resolve->hostname(); |
| 340 } | 352 } |
| 341 | 353 |
| 342 // Check that the port was set correctly. | 354 // Check that the port was set correctly. |
| 343 EXPECT_EQ(resolve->port(), resolve->addrlist().GetPort()); | 355 EXPECT_EQ(resolve->port(), resolve->addrlist().GetPort()); |
| 344 | 356 |
| 345 // Check whether all the requests have finished yet. | 357 // Check whether all the requests have finished yet. |
| 346 int total_completions = count_a_ + count_b_; | 358 int total_completions = count_a_ + count_b_; |
| 347 if (total_completions == 5) { | 359 if (total_completions == 5) { |
| 348 EXPECT_EQ(2, count_a_); | 360 EXPECT_EQ(2, count_a_); |
| 349 EXPECT_EQ(3, count_b_); | 361 EXPECT_EQ(3, count_b_); |
| 350 | 362 |
| 351 // The mapper should have been called only twice -- once with "a", once | 363 // The resolver_proc should have been called only twice -- once with "a", |
| 352 // with "b". | 364 // once with "b". |
| 353 std::vector<std::string> capture_list = mapper_->GetCaptureList(); | 365 std::vector<std::string> capture_list = resolver_proc_->GetCaptureList(); |
| 354 EXPECT_EQ(2U, capture_list.size()); | 366 EXPECT_EQ(2U, capture_list.size()); |
| 355 | 367 |
| 356 // End this test, we are done. | 368 // End this test, we are done. |
| 357 MessageLoop::current()->Quit(); | 369 MessageLoop::current()->Quit(); |
| 358 } | 370 } |
| 359 } | 371 } |
| 360 | 372 |
| 361 private: | 373 private: |
| 362 int count_a_; | 374 int count_a_; |
| 363 int count_b_; | 375 int count_b_; |
| 364 CapturingHostMapper* mapper_; | 376 CapturingHostResolverProc* resolver_proc_; |
| 365 | 377 |
| 366 DISALLOW_COPY_AND_ASSIGN(DeDupeRequestsVerifier); | 378 DISALLOW_COPY_AND_ASSIGN(DeDupeRequestsVerifier); |
| 367 }; | 379 }; |
| 368 | 380 |
| 369 TEST_F(HostResolverTest, DeDupeRequests) { | 381 TEST_F(HostResolverImplTest, DeDupeRequests) { |
| 370 // Use a capturing mapper, since the verifier needs to know what calls | 382 // Use a capturing resolver_proc, since the verifier needs to know what calls |
| 371 // reached Map(). Also, the capturing mapper is initially blocked. | 383 // reached Resolve(). Also, the capturing resolver_proc is initially blocked. |
| 372 scoped_refptr<CapturingHostMapper> mapper = new CapturingHostMapper(); | 384 scoped_refptr<CapturingHostResolverProc> resolver_proc = |
| 373 ScopedHostMapper scoped_mapper(mapper.get()); | 385 new CapturingHostResolverProc(NULL); |
| 374 | 386 |
| 375 scoped_refptr<net::HostResolver> host_resolver(new net::HostResolver); | 387 scoped_refptr<net::HostResolver> host_resolver( |
| 388 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 376 | 389 |
| 377 // The class will receive callbacks for when each resolve completes. It | 390 // The class will receive callbacks for when each resolve completes. It |
| 378 // checks that the right things happened. | 391 // checks that the right things happened. |
| 379 DeDupeRequestsVerifier verifier(mapper.get()); | 392 DeDupeRequestsVerifier verifier(resolver_proc.get()); |
| 380 | 393 |
| 381 // Start 5 requests, duplicating hosts "a" and "b". Since the mapper is | 394 // Start 5 requests, duplicating hosts "a" and "b". Since the resolver_proc is |
| 382 // blocked, these should all pile up until we signal it. | 395 // blocked, these should all pile up until we signal it. |
| 383 | 396 |
| 384 ResolveRequest req1(host_resolver, "a", 80, &verifier); | 397 ResolveRequest req1(host_resolver, "a", 80, &verifier); |
| 385 ResolveRequest req2(host_resolver, "b", 80, &verifier); | 398 ResolveRequest req2(host_resolver, "b", 80, &verifier); |
| 386 ResolveRequest req3(host_resolver, "b", 81, &verifier); | 399 ResolveRequest req3(host_resolver, "b", 81, &verifier); |
| 387 ResolveRequest req4(host_resolver, "a", 82, &verifier); | 400 ResolveRequest req4(host_resolver, "a", 82, &verifier); |
| 388 ResolveRequest req5(host_resolver, "b", 83, &verifier); | 401 ResolveRequest req5(host_resolver, "b", 83, &verifier); |
| 389 | 402 |
| 390 // Ready, Set, GO!!! | 403 // Ready, Set, GO!!! |
| 391 mapper->Signal(); | 404 resolver_proc->Signal(); |
| 392 | 405 |
| 393 // |verifier| will send quit message once all the requests have finished. | 406 // |verifier| will send quit message once all the requests have finished. |
| 394 MessageLoop::current()->Run(); | 407 MessageLoop::current()->Run(); |
| 395 } | 408 } |
| 396 | 409 |
| 397 // Helper class used by HostResolverTest.CancelMultipleRequests. | 410 // Helper class used by HostResolverImplTest.CancelMultipleRequests. |
| 398 class CancelMultipleRequestsVerifier : public ResolveRequest::Delegate { | 411 class CancelMultipleRequestsVerifier : public ResolveRequest::Delegate { |
| 399 public: | 412 public: |
| 400 CancelMultipleRequestsVerifier() {} | 413 CancelMultipleRequestsVerifier() {} |
| 401 | 414 |
| 402 // The cancels kill all but one request. | 415 // The cancels kill all but one request. |
| 403 virtual void OnCompleted(ResolveRequest* resolve) { | 416 virtual void OnCompleted(ResolveRequest* resolve) { |
| 404 EXPECT_EQ("a", resolve->hostname()); | 417 EXPECT_EQ("a", resolve->hostname()); |
| 405 EXPECT_EQ(82, resolve->port()); | 418 EXPECT_EQ(82, resolve->port()); |
| 406 | 419 |
| 407 // Check that the port was set correctly. | 420 // Check that the port was set correctly. |
| 408 EXPECT_EQ(resolve->port(), resolve->addrlist().GetPort()); | 421 EXPECT_EQ(resolve->port(), resolve->addrlist().GetPort()); |
| 409 | 422 |
| 410 // End this test, we are done. | 423 // End this test, we are done. |
| 411 MessageLoop::current()->Quit(); | 424 MessageLoop::current()->Quit(); |
| 412 } | 425 } |
| 413 | 426 |
| 414 private: | 427 private: |
| 415 DISALLOW_COPY_AND_ASSIGN(CancelMultipleRequestsVerifier); | 428 DISALLOW_COPY_AND_ASSIGN(CancelMultipleRequestsVerifier); |
| 416 }; | 429 }; |
| 417 | 430 |
| 418 TEST_F(HostResolverTest, CancelMultipleRequests) { | 431 TEST_F(HostResolverImplTest, CancelMultipleRequests) { |
| 419 // Use a capturing mapper, since the verifier needs to know what calls | 432 // Use a capturing resolver_proc, since the verifier needs to know what calls |
| 420 // reached Map(). Also, the capturing mapper is initially blocked. | 433 // reached Resolver(). Also, the capturing resolver_proc is initially |
| 421 scoped_refptr<CapturingHostMapper> mapper = new CapturingHostMapper(); | 434 // blocked. |
| 422 ScopedHostMapper scoped_mapper(mapper.get()); | 435 scoped_refptr<CapturingHostResolverProc> resolver_proc = |
| 436 new CapturingHostResolverProc(NULL); |
| 423 | 437 |
| 424 scoped_refptr<net::HostResolver> host_resolver(new net::HostResolver); | 438 scoped_refptr<net::HostResolver> host_resolver( |
| 439 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 425 | 440 |
| 426 // The class will receive callbacks for when each resolve completes. It | 441 // The class will receive callbacks for when each resolve completes. It |
| 427 // checks that the right things happened. | 442 // checks that the right things happened. |
| 428 CancelMultipleRequestsVerifier verifier; | 443 CancelMultipleRequestsVerifier verifier; |
| 429 | 444 |
| 430 // Start 5 requests, duplicating hosts "a" and "b". Since the mapper is | 445 // Start 5 requests, duplicating hosts "a" and "b". Since the resolver_proc is |
| 431 // blocked, these should all pile up until we signal it. | 446 // blocked, these should all pile up until we signal it. |
| 432 | 447 |
| 433 ResolveRequest req1(host_resolver, "a", 80, &verifier); | 448 ResolveRequest req1(host_resolver, "a", 80, &verifier); |
| 434 ResolveRequest req2(host_resolver, "b", 80, &verifier); | 449 ResolveRequest req2(host_resolver, "b", 80, &verifier); |
| 435 ResolveRequest req3(host_resolver, "b", 81, &verifier); | 450 ResolveRequest req3(host_resolver, "b", 81, &verifier); |
| 436 ResolveRequest req4(host_resolver, "a", 82, &verifier); | 451 ResolveRequest req4(host_resolver, "a", 82, &verifier); |
| 437 ResolveRequest req5(host_resolver, "b", 83, &verifier); | 452 ResolveRequest req5(host_resolver, "b", 83, &verifier); |
| 438 | 453 |
| 439 // Cancel everything except request 4. | 454 // Cancel everything except request 4. |
| 440 req1.Cancel(); | 455 req1.Cancel(); |
| 441 req2.Cancel(); | 456 req2.Cancel(); |
| 442 req3.Cancel(); | 457 req3.Cancel(); |
| 443 req5.Cancel(); | 458 req5.Cancel(); |
| 444 | 459 |
| 445 // Ready, Set, GO!!! | 460 // Ready, Set, GO!!! |
| 446 mapper->Signal(); | 461 resolver_proc->Signal(); |
| 447 | 462 |
| 448 // |verifier| will send quit message once all the requests have finished. | 463 // |verifier| will send quit message once all the requests have finished. |
| 449 MessageLoop::current()->Run(); | 464 MessageLoop::current()->Run(); |
| 450 } | 465 } |
| 451 | 466 |
| 452 // Helper class used by HostResolverTest.CancelWithinCallback. | 467 // Helper class used by HostResolverImplTest.CancelWithinCallback. |
| 453 class CancelWithinCallbackVerifier : public ResolveRequest::Delegate { | 468 class CancelWithinCallbackVerifier : public ResolveRequest::Delegate { |
| 454 public: | 469 public: |
| 455 CancelWithinCallbackVerifier() | 470 CancelWithinCallbackVerifier() |
| 456 : req_to_cancel1_(NULL), req_to_cancel2_(NULL), num_completions_(0) { | 471 : req_to_cancel1_(NULL), req_to_cancel2_(NULL), num_completions_(0) { |
| 457 } | 472 } |
| 458 | 473 |
| 459 virtual void OnCompleted(ResolveRequest* resolve) { | 474 virtual void OnCompleted(ResolveRequest* resolve) { |
| 460 num_completions_++; | 475 num_completions_++; |
| 461 | 476 |
| 462 // Port 80 is the first request that the callback will be invoked for. | 477 // Port 80 is the first request that the callback will be invoked for. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 493 } | 508 } |
| 494 | 509 |
| 495 private: | 510 private: |
| 496 scoped_ptr<ResolveRequest> final_request_; | 511 scoped_ptr<ResolveRequest> final_request_; |
| 497 ResolveRequest* req_to_cancel1_; | 512 ResolveRequest* req_to_cancel1_; |
| 498 ResolveRequest* req_to_cancel2_; | 513 ResolveRequest* req_to_cancel2_; |
| 499 int num_completions_; | 514 int num_completions_; |
| 500 DISALLOW_COPY_AND_ASSIGN(CancelWithinCallbackVerifier); | 515 DISALLOW_COPY_AND_ASSIGN(CancelWithinCallbackVerifier); |
| 501 }; | 516 }; |
| 502 | 517 |
| 503 TEST_F(HostResolverTest, CancelWithinCallback) { | 518 TEST_F(HostResolverImplTest, CancelWithinCallback) { |
| 504 // Use a capturing mapper, since the verifier needs to know what calls | 519 // Use a capturing resolver_proc, since the verifier needs to know what calls |
| 505 // reached Map(). Also, the capturing mapper is initially blocked. | 520 // reached Resolver(). Also, the capturing resolver_proc is initially |
| 506 scoped_refptr<CapturingHostMapper> mapper = new CapturingHostMapper(); | 521 // blocked. |
| 507 ScopedHostMapper scoped_mapper(mapper.get()); | 522 scoped_refptr<CapturingHostResolverProc> resolver_proc = |
| 523 new CapturingHostResolverProc(NULL); |
| 508 | 524 |
| 509 scoped_refptr<net::HostResolver> host_resolver(new net::HostResolver); | 525 scoped_refptr<net::HostResolver> host_resolver( |
| 526 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 510 | 527 |
| 511 // The class will receive callbacks for when each resolve completes. It | 528 // The class will receive callbacks for when each resolve completes. It |
| 512 // checks that the right things happened. | 529 // checks that the right things happened. |
| 513 CancelWithinCallbackVerifier verifier; | 530 CancelWithinCallbackVerifier verifier; |
| 514 | 531 |
| 515 // Start 4 requests, duplicating hosts "a". Since the mapper is | 532 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is |
| 516 // blocked, these should all pile up until we signal it. | 533 // blocked, these should all pile up until we signal it. |
| 517 | 534 |
| 518 ResolveRequest req1(host_resolver, "a", 80, &verifier); | 535 ResolveRequest req1(host_resolver, "a", 80, &verifier); |
| 519 ResolveRequest req2(host_resolver, "a", 81, &verifier); | 536 ResolveRequest req2(host_resolver, "a", 81, &verifier); |
| 520 ResolveRequest req3(host_resolver, "a", 82, &verifier); | 537 ResolveRequest req3(host_resolver, "a", 82, &verifier); |
| 521 ResolveRequest req4(host_resolver, "a", 83, &verifier); | 538 ResolveRequest req4(host_resolver, "a", 83, &verifier); |
| 522 | 539 |
| 523 // Once "a:80" completes, it will cancel "a:81" and "a:82". | 540 // Once "a:80" completes, it will cancel "a:81" and "a:82". |
| 524 verifier.SetRequestsToCancel(&req2, &req3); | 541 verifier.SetRequestsToCancel(&req2, &req3); |
| 525 | 542 |
| 526 // Ready, Set, GO!!! | 543 // Ready, Set, GO!!! |
| 527 mapper->Signal(); | 544 resolver_proc->Signal(); |
| 528 | 545 |
| 529 // |verifier| will send quit message once all the requests have finished. | 546 // |verifier| will send quit message once all the requests have finished. |
| 530 MessageLoop::current()->Run(); | 547 MessageLoop::current()->Run(); |
| 531 } | 548 } |
| 532 | 549 |
| 533 // Helper class used by HostResolverTest.DeleteWithinCallback. | 550 // Helper class used by HostResolverImplTest.DeleteWithinCallback. |
| 534 class DeleteWithinCallbackVerifier : public ResolveRequest::Delegate { | 551 class DeleteWithinCallbackVerifier : public ResolveRequest::Delegate { |
| 535 public: | 552 public: |
| 536 // |host_resolver| is the resolver that the the resolve requests were started | 553 // |host_resolver| is the resolver that the the resolve requests were started |
| 537 // with. | 554 // with. |
| 538 DeleteWithinCallbackVerifier(net::HostResolver* host_resolver) | 555 DeleteWithinCallbackVerifier(net::HostResolver* host_resolver) |
| 539 : host_resolver_(host_resolver) {} | 556 : host_resolver_(host_resolver) {} |
| 540 | 557 |
| 541 virtual void OnCompleted(ResolveRequest* resolve) { | 558 virtual void OnCompleted(ResolveRequest* resolve) { |
| 542 EXPECT_EQ("a", resolve->hostname()); | 559 EXPECT_EQ("a", resolve->hostname()); |
| 543 EXPECT_EQ(80, resolve->port()); | 560 EXPECT_EQ(80, resolve->port()); |
| 544 | 561 |
| 545 // Release the last reference to the host resolver that started the | 562 // Release the last reference to the host resolver that started the |
| 546 // requests. | 563 // requests. |
| 547 host_resolver_ = NULL; | 564 host_resolver_ = NULL; |
| 548 | 565 |
| 549 // Quit after returning from OnCompleted (to give it a chance at | 566 // Quit after returning from OnCompleted (to give it a chance at |
| 550 // incorrectly running the cancelled tasks). | 567 // incorrectly running the cancelled tasks). |
| 551 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 568 MessageLoop::current()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 552 } | 569 } |
| 553 | 570 |
| 554 private: | 571 private: |
| 555 scoped_refptr<net::HostResolver> host_resolver_; | 572 scoped_refptr<net::HostResolver> host_resolver_; |
| 556 DISALLOW_COPY_AND_ASSIGN(DeleteWithinCallbackVerifier); | 573 DISALLOW_COPY_AND_ASSIGN(DeleteWithinCallbackVerifier); |
| 557 }; | 574 }; |
| 558 | 575 |
| 559 TEST_F(HostResolverTest, DeleteWithinCallback) { | 576 TEST_F(HostResolverImplTest, DeleteWithinCallback) { |
| 560 // Use a capturing mapper, since the verifier needs to know what calls | 577 // Use a capturing resolver_proc, since the verifier needs to know what calls |
| 561 // reached Map(). Also, the capturing mapper is initially blocked. | 578 // reached Resolver(). Also, the capturing resolver_proc is initially |
| 562 scoped_refptr<CapturingHostMapper> mapper = new CapturingHostMapper(); | 579 // blocked. |
| 563 ScopedHostMapper scoped_mapper(mapper.get()); | 580 scoped_refptr<CapturingHostResolverProc> resolver_proc = |
| 581 new CapturingHostResolverProc(NULL); |
| 564 | 582 |
| 565 // The class will receive callbacks for when each resolve completes. It | 583 // The class will receive callbacks for when each resolve completes. It |
| 566 // checks that the right things happened. Note that the verifier holds the | 584 // checks that the right things happened. Note that the verifier holds the |
| 567 // only reference to |host_resolver|, so it can delete it within callback. | 585 // only reference to |host_resolver|, so it can delete it within callback. |
| 568 net::HostResolver* host_resolver = new net::HostResolver; | 586 net::HostResolver* host_resolver = |
| 587 new HostResolverImpl(resolver_proc, kMaxCacheEntries, kMaxCacheAgeMs); |
| 569 DeleteWithinCallbackVerifier verifier(host_resolver); | 588 DeleteWithinCallbackVerifier verifier(host_resolver); |
| 570 | 589 |
| 571 // Start 4 requests, duplicating hosts "a". Since the mapper is | 590 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is |
| 572 // blocked, these should all pile up until we signal it. | 591 // blocked, these should all pile up until we signal it. |
| 573 | 592 |
| 574 ResolveRequest req1(host_resolver, "a", 80, &verifier); | 593 ResolveRequest req1(host_resolver, "a", 80, &verifier); |
| 575 ResolveRequest req2(host_resolver, "a", 81, &verifier); | 594 ResolveRequest req2(host_resolver, "a", 81, &verifier); |
| 576 ResolveRequest req3(host_resolver, "a", 82, &verifier); | 595 ResolveRequest req3(host_resolver, "a", 82, &verifier); |
| 577 ResolveRequest req4(host_resolver, "a", 83, &verifier); | 596 ResolveRequest req4(host_resolver, "a", 83, &verifier); |
| 578 | 597 |
| 579 // Ready, Set, GO!!! | 598 // Ready, Set, GO!!! |
| 580 mapper->Signal(); | 599 resolver_proc->Signal(); |
| 581 | 600 |
| 582 // |verifier| will send quit message once all the requests have finished. | 601 // |verifier| will send quit message once all the requests have finished. |
| 583 MessageLoop::current()->Run(); | 602 MessageLoop::current()->Run(); |
| 584 } | 603 } |
| 585 | 604 |
| 586 // Helper class used by HostResolverTest.StartWithinCallback. | 605 // Helper class used by HostResolverImplTest.StartWithinCallback. |
| 587 class StartWithinCallbackVerifier : public ResolveRequest::Delegate { | 606 class StartWithinCallbackVerifier : public ResolveRequest::Delegate { |
| 588 public: | 607 public: |
| 589 StartWithinCallbackVerifier() : num_requests_(0) {} | 608 StartWithinCallbackVerifier() : num_requests_(0) {} |
| 590 | 609 |
| 591 virtual void OnCompleted(ResolveRequest* resolve) { | 610 virtual void OnCompleted(ResolveRequest* resolve) { |
| 592 EXPECT_EQ("a", resolve->hostname()); | 611 EXPECT_EQ("a", resolve->hostname()); |
| 593 | 612 |
| 594 if (80 == resolve->port()) { | 613 if (80 == resolve->port()) { |
| 595 // On completing the first request, start another request for "a". | 614 // On completing the first request, start another request for "a". |
| 596 // Since caching is disabled, this will result in another async request. | 615 // Since caching is disabled, this will result in another async request. |
| 597 final_request_.reset(new ResolveRequest( | 616 final_request_.reset(new ResolveRequest( |
| 598 resolve->resolver(), "a", 70, this)); | 617 resolve->resolver(), "a", 70, this)); |
| 599 } | 618 } |
| 600 if (++num_requests_ == 5) { | 619 if (++num_requests_ == 5) { |
| 601 // Test is done. | 620 // Test is done. |
| 602 MessageLoop::current()->Quit(); | 621 MessageLoop::current()->Quit(); |
| 603 } | 622 } |
| 604 } | 623 } |
| 605 | 624 |
| 606 private: | 625 private: |
| 607 int num_requests_; | 626 int num_requests_; |
| 608 scoped_ptr<ResolveRequest> final_request_; | 627 scoped_ptr<ResolveRequest> final_request_; |
| 609 DISALLOW_COPY_AND_ASSIGN(StartWithinCallbackVerifier); | 628 DISALLOW_COPY_AND_ASSIGN(StartWithinCallbackVerifier); |
| 610 }; | 629 }; |
| 611 | 630 |
| 612 TEST_F(HostResolverTest, StartWithinCallback) { | 631 TEST_F(HostResolverImplTest, StartWithinCallback) { |
| 613 // Use a capturing mapper, since the verifier needs to know what calls | 632 // Use a capturing resolver_proc, since the verifier needs to know what calls |
| 614 // reached Map(). Also, the capturing mapper is initially blocked. | 633 // reached Resolver(). Also, the capturing resolver_proc is initially |
| 615 scoped_refptr<CapturingHostMapper> mapper = new CapturingHostMapper(); | 634 // blocked. |
| 616 ScopedHostMapper scoped_mapper(mapper.get()); | 635 scoped_refptr<CapturingHostResolverProc> resolver_proc = |
| 636 new CapturingHostResolverProc(NULL); |
| 617 | 637 |
| 618 // Turn off caching for this host resolver. | 638 // Turn off caching for this host resolver. |
| 619 scoped_refptr<net::HostResolver> host_resolver(new net::HostResolver(0, 0)); | 639 scoped_refptr<net::HostResolver> host_resolver( |
| 640 new HostResolverImpl(resolver_proc, 0, 0)); |
| 620 | 641 |
| 621 // The class will receive callbacks for when each resolve completes. It | 642 // The class will receive callbacks for when each resolve completes. It |
| 622 // checks that the right things happened. | 643 // checks that the right things happened. |
| 623 StartWithinCallbackVerifier verifier; | 644 StartWithinCallbackVerifier verifier; |
| 624 | 645 |
| 625 // Start 4 requests, duplicating hosts "a". Since the mapper is | 646 // Start 4 requests, duplicating hosts "a". Since the resolver_proc is |
| 626 // blocked, these should all pile up until we signal it. | 647 // blocked, these should all pile up until we signal it. |
| 627 | 648 |
| 628 ResolveRequest req1(host_resolver, "a", 80, &verifier); | 649 ResolveRequest req1(host_resolver, "a", 80, &verifier); |
| 629 ResolveRequest req2(host_resolver, "a", 81, &verifier); | 650 ResolveRequest req2(host_resolver, "a", 81, &verifier); |
| 630 ResolveRequest req3(host_resolver, "a", 82, &verifier); | 651 ResolveRequest req3(host_resolver, "a", 82, &verifier); |
| 631 ResolveRequest req4(host_resolver, "a", 83, &verifier); | 652 ResolveRequest req4(host_resolver, "a", 83, &verifier); |
| 632 | 653 |
| 633 // Ready, Set, GO!!! | 654 // Ready, Set, GO!!! |
| 634 mapper->Signal(); | 655 resolver_proc->Signal(); |
| 635 | 656 |
| 636 // |verifier| will send quit message once all the requests have finished. | 657 // |verifier| will send quit message once all the requests have finished. |
| 637 MessageLoop::current()->Run(); | 658 MessageLoop::current()->Run(); |
| 638 } | 659 } |
| 639 | 660 |
| 640 // Helper class used by HostResolverTest.BypassCache. | 661 // Helper class used by HostResolverImplTest.BypassCache. |
| 641 class BypassCacheVerifier : public ResolveRequest::Delegate { | 662 class BypassCacheVerifier : public ResolveRequest::Delegate { |
| 642 public: | 663 public: |
| 643 BypassCacheVerifier() {} | 664 BypassCacheVerifier() {} |
| 644 | 665 |
| 645 virtual void OnCompleted(ResolveRequest* resolve) { | 666 virtual void OnCompleted(ResolveRequest* resolve) { |
| 646 EXPECT_EQ("a", resolve->hostname()); | 667 EXPECT_EQ("a", resolve->hostname()); |
| 647 net::HostResolver* resolver = resolve->resolver(); | 668 net::HostResolver* resolver = resolve->resolver(); |
| 648 | 669 |
| 649 if (80 == resolve->port()) { | 670 if (80 == resolve->port()) { |
| 650 // On completing the first request, start another request for "a". | 671 // On completing the first request, start another request for "a". |
| (...skipping 21 matching lines...) Expand all Loading... |
| 672 } else { | 693 } else { |
| 673 FAIL() << "Unexpected port number"; | 694 FAIL() << "Unexpected port number"; |
| 674 } | 695 } |
| 675 } | 696 } |
| 676 | 697 |
| 677 private: | 698 private: |
| 678 scoped_ptr<ResolveRequest> final_request_; | 699 scoped_ptr<ResolveRequest> final_request_; |
| 679 DISALLOW_COPY_AND_ASSIGN(BypassCacheVerifier); | 700 DISALLOW_COPY_AND_ASSIGN(BypassCacheVerifier); |
| 680 }; | 701 }; |
| 681 | 702 |
| 682 TEST_F(HostResolverTest, BypassCache) { | 703 TEST_F(HostResolverImplTest, BypassCache) { |
| 683 scoped_refptr<net::HostResolver> host_resolver(new net::HostResolver); | 704 scoped_refptr<net::HostResolver> host_resolver( |
| 705 new HostResolverImpl(NULL, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 684 | 706 |
| 685 // The class will receive callbacks for when each resolve completes. It | 707 // The class will receive callbacks for when each resolve completes. It |
| 686 // checks that the right things happened. | 708 // checks that the right things happened. |
| 687 BypassCacheVerifier verifier; | 709 BypassCacheVerifier verifier; |
| 688 | 710 |
| 689 // Start a request. | 711 // Start a request. |
| 690 ResolveRequest req1(host_resolver, "a", 80, &verifier); | 712 ResolveRequest req1(host_resolver, "a", 80, &verifier); |
| 691 | 713 |
| 692 // |verifier| will send quit message once all the requests have finished. | 714 // |verifier| will send quit message once all the requests have finished. |
| 693 MessageLoop::current()->Run(); | 715 MessageLoop::current()->Run(); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 755 }; | 777 }; |
| 756 | 778 |
| 757 std::vector<StartOrCancelEntry> start_log; | 779 std::vector<StartOrCancelEntry> start_log; |
| 758 std::vector<FinishEntry> finish_log; | 780 std::vector<FinishEntry> finish_log; |
| 759 std::vector<StartOrCancelEntry> cancel_log; | 781 std::vector<StartOrCancelEntry> cancel_log; |
| 760 }; | 782 }; |
| 761 | 783 |
| 762 // Test that registering, unregistering, and notifying of observers works. | 784 // Test that registering, unregistering, and notifying of observers works. |
| 763 // Does not test the cancellation notification since all resolves are | 785 // Does not test the cancellation notification since all resolves are |
| 764 // synchronous. | 786 // synchronous. |
| 765 TEST_F(HostResolverTest, Observers) { | 787 TEST_F(HostResolverImplTest, Observers) { |
| 766 scoped_refptr<net::HostResolver> host_resolver(new net::HostResolver); | 788 scoped_refptr<net::HostResolver> host_resolver( |
| 789 new HostResolverImpl(NULL, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 767 | 790 |
| 768 CapturingObserver observer; | 791 CapturingObserver observer; |
| 769 | 792 |
| 770 host_resolver->AddObserver(&observer); | 793 host_resolver->AddObserver(&observer); |
| 771 | 794 |
| 772 net::AddressList addrlist; | 795 net::AddressList addrlist; |
| 773 | 796 |
| 774 // Resolve "host1". | 797 // Resolve "host1". |
| 775 net::HostResolver::RequestInfo info1("host1", 70); | 798 net::HostResolver::RequestInfo info1("host1", 70); |
| 776 int rv = host_resolver->Resolve(info1, &addrlist, NULL, NULL); | 799 int rv = host_resolver->Resolve(info1, &addrlist, NULL, NULL); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 822 // No effect this time, since observer was removed. | 845 // No effect this time, since observer was removed. |
| 823 EXPECT_EQ(3U, observer.start_log.size()); | 846 EXPECT_EQ(3U, observer.start_log.size()); |
| 824 EXPECT_EQ(3U, observer.finish_log.size()); | 847 EXPECT_EQ(3U, observer.finish_log.size()); |
| 825 EXPECT_EQ(0U, observer.cancel_log.size()); | 848 EXPECT_EQ(0U, observer.cancel_log.size()); |
| 826 } | 849 } |
| 827 | 850 |
| 828 // Tests that observers are sent OnCancelResolution() whenever a request is | 851 // Tests that observers are sent OnCancelResolution() whenever a request is |
| 829 // cancelled. There are two ways to cancel a request: | 852 // cancelled. There are two ways to cancel a request: |
| 830 // (1) Delete the HostResolver while job is outstanding. | 853 // (1) Delete the HostResolver while job is outstanding. |
| 831 // (2) Call HostResolver::CancelRequest() while a request is outstanding. | 854 // (2) Call HostResolver::CancelRequest() while a request is outstanding. |
| 832 TEST_F(HostResolverTest, CancellationObserver) { | 855 TEST_F(HostResolverImplTest, CancellationObserver) { |
| 833 CapturingObserver observer; | 856 CapturingObserver observer; |
| 834 { | 857 { |
| 835 // Create a host resolver and attach an observer. | 858 // Create a host resolver and attach an observer. |
| 836 scoped_refptr<net::HostResolver> host_resolver(new net::HostResolver); | 859 scoped_refptr<net::HostResolver> host_resolver( |
| 860 new HostResolverImpl(NULL, kMaxCacheEntries, kMaxCacheAgeMs)); |
| 837 host_resolver->AddObserver(&observer); | 861 host_resolver->AddObserver(&observer); |
| 838 | 862 |
| 839 TestCompletionCallback callback; | 863 TestCompletionCallback callback; |
| 840 | 864 |
| 841 EXPECT_EQ(0U, observer.start_log.size()); | 865 EXPECT_EQ(0U, observer.start_log.size()); |
| 842 EXPECT_EQ(0U, observer.finish_log.size()); | 866 EXPECT_EQ(0U, observer.finish_log.size()); |
| 843 EXPECT_EQ(0U, observer.cancel_log.size()); | 867 EXPECT_EQ(0U, observer.cancel_log.size()); |
| 844 | 868 |
| 845 // Start an async resolve for (host1:70). | 869 // Start an async resolve for (host1:70). |
| 846 net::HostResolver::RequestInfo info1("host1", 70); | 870 net::HostResolver::RequestInfo info1("host1", 70); |
| 847 net::HostResolver::Request* req = NULL; | 871 net::HostResolver::RequestHandle req = NULL; |
| 848 net::AddressList addrlist; | 872 net::AddressList addrlist; |
| 849 int rv = host_resolver->Resolve(info1, &addrlist, &callback, &req); | 873 int rv = host_resolver->Resolve(info1, &addrlist, &callback, &req); |
| 850 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 874 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 851 EXPECT_TRUE(NULL != req); | 875 EXPECT_TRUE(NULL != req); |
| 852 | 876 |
| 853 EXPECT_EQ(1U, observer.start_log.size()); | 877 EXPECT_EQ(1U, observer.start_log.size()); |
| 854 EXPECT_EQ(0U, observer.finish_log.size()); | 878 EXPECT_EQ(0U, observer.finish_log.size()); |
| 855 EXPECT_EQ(0U, observer.cancel_log.size()); | 879 EXPECT_EQ(0U, observer.cancel_log.size()); |
| 856 | 880 |
| 857 EXPECT_TRUE(observer.start_log[0] == | 881 EXPECT_TRUE(observer.start_log[0] == |
| 858 CapturingObserver::StartOrCancelEntry(0, info1)); | 882 CapturingObserver::StartOrCancelEntry(0, info1)); |
| 859 | 883 |
| 860 // Cancel the request (host mapper is blocked so it cant be finished yet). | 884 // Cancel the request. |
| 861 host_resolver->CancelRequest(req); | 885 host_resolver->CancelRequest(req); |
| 862 | 886 |
| 863 EXPECT_EQ(1U, observer.start_log.size()); | 887 EXPECT_EQ(1U, observer.start_log.size()); |
| 864 EXPECT_EQ(0U, observer.finish_log.size()); | 888 EXPECT_EQ(0U, observer.finish_log.size()); |
| 865 EXPECT_EQ(1U, observer.cancel_log.size()); | 889 EXPECT_EQ(1U, observer.cancel_log.size()); |
| 866 | 890 |
| 867 EXPECT_TRUE(observer.cancel_log[0] == | 891 EXPECT_TRUE(observer.cancel_log[0] == |
| 868 CapturingObserver::StartOrCancelEntry(0, info1)); | 892 CapturingObserver::StartOrCancelEntry(0, info1)); |
| 869 | 893 |
| 870 // Start an async request for (host2:60) | 894 // Start an async request for (host2:60) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 890 EXPECT_EQ(2U, observer.start_log.size()); | 914 EXPECT_EQ(2U, observer.start_log.size()); |
| 891 EXPECT_EQ(0U, observer.finish_log.size()); | 915 EXPECT_EQ(0U, observer.finish_log.size()); |
| 892 EXPECT_EQ(2U, observer.cancel_log.size()); | 916 EXPECT_EQ(2U, observer.cancel_log.size()); |
| 893 | 917 |
| 894 net::HostResolver::RequestInfo info("host2", 60); | 918 net::HostResolver::RequestInfo info("host2", 60); |
| 895 EXPECT_TRUE(observer.cancel_log[1] == | 919 EXPECT_TRUE(observer.cancel_log[1] == |
| 896 CapturingObserver::StartOrCancelEntry(1, info)); | 920 CapturingObserver::StartOrCancelEntry(1, info)); |
| 897 } | 921 } |
| 898 | 922 |
| 899 } // namespace | 923 } // namespace |
| OLD | NEW |