OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/scoped_ptr.h" | 5 #include "base/scoped_ptr.h" |
6 #include "net/base/address_list.h" | 6 #include "net/base/address_list.h" |
7 #include "net/base/mock_host_resolver.h" | 7 #include "net/base/mock_host_resolver.h" |
8 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
9 #include "net/base/net_log.h" | 9 #include "net/base/net_log.h" |
10 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
11 #include "net/base/sys_addrinfo.h" | 11 #include "net/base/sys_addrinfo.h" |
12 #include "net/proxy/proxy_resolver_js_bindings.h" | 12 #include "net/proxy/proxy_resolver_js_bindings.h" |
| 13 #include "net/proxy/proxy_resolver_request_context.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 namespace net { | 16 namespace net { |
16 namespace { | 17 namespace { |
17 | 18 |
18 // This is a HostResolver that synchronously resolves all hosts to the | 19 // This is a HostResolver that synchronously resolves all hosts to the |
19 // following address list of length 3: | 20 // following address list of length 3: |
20 // 192.168.1.1 | 21 // 192.168.1.1 |
21 // 172.22.34.1 | 22 // 172.22.34.1 |
22 // 200.100.1.2 | 23 // 200.100.1.2 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 AddressList concatenated; | 72 AddressList concatenated; |
72 concatenated.Copy(result.head(), true); | 73 concatenated.Copy(result.head(), true); |
73 | 74 |
74 // Restore |result| (so it is freed properly). | 75 // Restore |result| (so it is freed properly). |
75 result_head->ai_next = NULL; | 76 result_head->ai_next = NULL; |
76 | 77 |
77 return concatenated; | 78 return concatenated; |
78 } | 79 } |
79 }; | 80 }; |
80 | 81 |
| 82 class MockFailingHostResolver : public HostResolver { |
| 83 public: |
| 84 MockFailingHostResolver() : count_(0) {} |
| 85 |
| 86 // HostResolver methods: |
| 87 virtual int Resolve(const RequestInfo& info, |
| 88 AddressList* addresses, |
| 89 CompletionCallback* callback, |
| 90 RequestHandle* out_req, |
| 91 const BoundNetLog& net_log) { |
| 92 count_++; |
| 93 return ERR_NAME_NOT_RESOLVED; |
| 94 } |
| 95 |
| 96 virtual void CancelRequest(RequestHandle req) {} |
| 97 virtual void AddObserver(Observer* observer) {} |
| 98 virtual void RemoveObserver(Observer* observer) {} |
| 99 virtual void Shutdown() {} |
| 100 |
| 101 // Returns the number of times Resolve() has been called. |
| 102 int count() const { return count_; } |
| 103 void ResetCount() { count_ = 0; } |
| 104 |
| 105 private: |
| 106 int count_; |
| 107 }; |
| 108 |
81 TEST(ProxyResolverJSBindingsTest, DnsResolve) { | 109 TEST(ProxyResolverJSBindingsTest, DnsResolve) { |
82 scoped_refptr<MockHostResolver> host_resolver(new MockHostResolver); | 110 scoped_refptr<MockHostResolver> host_resolver(new MockHostResolver); |
83 | 111 |
84 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). | 112 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). |
85 scoped_ptr<ProxyResolverJSBindings> bindings( | 113 scoped_ptr<ProxyResolverJSBindings> bindings( |
86 ProxyResolverJSBindings::CreateDefault(host_resolver)); | 114 ProxyResolverJSBindings::CreateDefault(host_resolver)); |
87 | 115 |
88 // Empty string is not considered a valid host (even though on some systems | 116 // Empty string is not considered a valid host (even though on some systems |
89 // requesting this will resolve to localhost). | 117 // requesting this will resolve to localhost). |
90 host_resolver->rules()->AddSimulatedFailure(""); | 118 host_resolver->rules()->AddSimulatedFailure(""); |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 scoped_ptr<ProxyResolverJSBindings> bindings( | 207 scoped_ptr<ProxyResolverJSBindings> bindings( |
180 ProxyResolverJSBindings::CreateDefault(host_resolver)); | 208 ProxyResolverJSBindings::CreateDefault(host_resolver)); |
181 | 209 |
182 EXPECT_EQ("192.168.1.1;172.22.34.1;200.100.1.2", | 210 EXPECT_EQ("192.168.1.1;172.22.34.1;200.100.1.2", |
183 bindings->MyIpAddressEx()); | 211 bindings->MyIpAddressEx()); |
184 | 212 |
185 EXPECT_EQ("192.168.1.1;172.22.34.1;200.100.1.2", | 213 EXPECT_EQ("192.168.1.1;172.22.34.1;200.100.1.2", |
186 bindings->DnsResolveEx("FOO")); | 214 bindings->DnsResolveEx("FOO")); |
187 } | 215 } |
188 | 216 |
| 217 TEST(ProxyResolverJSBindingsTest, PerRequestDNSCache) { |
| 218 scoped_refptr<MockFailingHostResolver> host_resolver( |
| 219 new MockFailingHostResolver); |
| 220 |
| 221 // Get a hold of a DefaultJSBindings* (it is a hidden impl class). |
| 222 scoped_ptr<ProxyResolverJSBindings> bindings( |
| 223 ProxyResolverJSBindings::CreateDefault(host_resolver)); |
| 224 |
| 225 // Call DnsResolve() 4 times for the same hostname -- this should issue |
| 226 // 4 separate calls to the underlying host resolver, since there is no |
| 227 // current request context. |
| 228 EXPECT_EQ("", bindings->DnsResolve("foo")); |
| 229 EXPECT_EQ("", bindings->DnsResolve("foo")); |
| 230 EXPECT_EQ("", bindings->DnsResolve("foo")); |
| 231 EXPECT_EQ("", bindings->DnsResolve("foo")); |
| 232 EXPECT_EQ(4, host_resolver->count()); |
| 233 |
| 234 host_resolver->ResetCount(); |
| 235 |
| 236 // Now setup a per-request context, and try the same experiment -- we |
| 237 // expect the underlying host resolver to receive only 1 request this time, |
| 238 // since it will service the others from the per-request DNS cache. |
| 239 HostCache cache(50, |
| 240 base::TimeDelta::FromMinutes(10), |
| 241 base::TimeDelta::FromMinutes(10)); |
| 242 ProxyResolverRequestContext context(NULL, NULL, &cache); |
| 243 bindings->set_current_request_context(&context); |
| 244 |
| 245 EXPECT_EQ("", bindings->DnsResolve("foo")); |
| 246 EXPECT_EQ("", bindings->DnsResolve("foo")); |
| 247 EXPECT_EQ("", bindings->DnsResolve("foo")); |
| 248 EXPECT_EQ("", bindings->DnsResolve("foo")); |
| 249 EXPECT_EQ(1, host_resolver->count()); |
| 250 |
| 251 host_resolver->ResetCount(); |
| 252 |
| 253 // The "Ex" version shares this same cache, however since the flags |
| 254 // are different it won't reuse this particular entry. |
| 255 EXPECT_EQ("", bindings->DnsResolveEx("foo")); |
| 256 EXPECT_EQ(1, host_resolver->count()); |
| 257 EXPECT_EQ("", bindings->DnsResolveEx("foo")); |
| 258 EXPECT_EQ("", bindings->DnsResolveEx("foo")); |
| 259 EXPECT_EQ(1, host_resolver->count()); |
| 260 |
| 261 bindings->set_current_request_context(NULL); |
| 262 } |
| 263 |
189 } // namespace | 264 } // namespace |
190 } // namespace net | 265 } // namespace net |
OLD | NEW |