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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" |
10 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
13 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
14 #include "net/base/net_log.h" | 15 #include "net/base/net_log.h" |
15 #include "net/base/net_log_unittest.h" | 16 #include "net/base/net_log_unittest.h" |
16 #include "net/base/test_completion_callback.h" | 17 #include "net/base/test_completion_callback.h" |
| 18 #include "net/dns/mock_host_resolver.h" |
17 #include "net/proxy/dhcp_proxy_script_fetcher.h" | 19 #include "net/proxy/dhcp_proxy_script_fetcher.h" |
18 #include "net/proxy/proxy_config.h" | 20 #include "net/proxy/proxy_config.h" |
19 #include "net/proxy/proxy_resolver.h" | 21 #include "net/proxy/proxy_resolver.h" |
20 #include "net/proxy/proxy_script_decider.h" | 22 #include "net/proxy/proxy_script_decider.h" |
21 #include "net/proxy/proxy_script_fetcher.h" | 23 #include "net/proxy/proxy_script_fetcher.h" |
| 24 #include "net/url_request/url_request_context.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
23 | 26 |
24 namespace net { | 27 namespace net { |
25 namespace { | 28 namespace { |
26 | 29 |
27 enum Error { | 30 enum Error { |
28 kFailedDownloading = -100, | 31 kFailedDownloading = -100, |
29 kFailedParsing = ERR_PAC_SCRIPT_FAILED, | 32 kFailedParsing = ERR_PAC_SCRIPT_FAILED, |
30 }; | 33 }; |
31 | 34 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 return rules_[0]; | 89 return rules_[0]; |
87 } | 90 } |
88 | 91 |
89 private: | 92 private: |
90 typedef std::vector<Rule> RuleList; | 93 typedef std::vector<Rule> RuleList; |
91 RuleList rules_; | 94 RuleList rules_; |
92 }; | 95 }; |
93 | 96 |
94 class RuleBasedProxyScriptFetcher : public ProxyScriptFetcher { | 97 class RuleBasedProxyScriptFetcher : public ProxyScriptFetcher { |
95 public: | 98 public: |
96 explicit RuleBasedProxyScriptFetcher(const Rules* rules) : rules_(rules) {} | 99 explicit RuleBasedProxyScriptFetcher(const Rules* rules) |
| 100 : rules_(rules), request_context_(NULL) {} |
| 101 |
| 102 virtual void SetRequestContext(URLRequestContext* context) { |
| 103 request_context_ = context; |
| 104 } |
97 | 105 |
98 // ProxyScriptFetcher implementation. | 106 // ProxyScriptFetcher implementation. |
99 virtual int Fetch(const GURL& url, | 107 virtual int Fetch(const GURL& url, |
100 base::string16* text, | 108 base::string16* text, |
101 const CompletionCallback& callback) OVERRIDE { | 109 const CompletionCallback& callback) OVERRIDE { |
102 const Rules::Rule& rule = rules_->GetRuleByUrl(url); | 110 const Rules::Rule& rule = rules_->GetRuleByUrl(url); |
103 int rv = rule.fetch_error; | 111 int rv = rule.fetch_error; |
104 EXPECT_NE(ERR_UNEXPECTED, rv); | 112 EXPECT_NE(ERR_UNEXPECTED, rv); |
105 if (rv == OK) | 113 if (rv == OK) |
106 *text = rule.text(); | 114 *text = rule.text(); |
107 return rv; | 115 return rv; |
108 } | 116 } |
109 | 117 |
110 virtual void Cancel() OVERRIDE {} | 118 virtual void Cancel() OVERRIDE {} |
111 | 119 |
112 virtual URLRequestContext* GetRequestContext() const OVERRIDE { return NULL; } | 120 virtual URLRequestContext* GetRequestContext() const OVERRIDE { |
| 121 return request_context_; |
| 122 } |
113 | 123 |
114 private: | 124 private: |
115 const Rules* rules_; | 125 const Rules* rules_; |
| 126 URLRequestContext* request_context_; |
116 }; | 127 }; |
117 | 128 |
118 // Succeed using custom PAC script. | 129 // Succeed using custom PAC script. |
119 TEST(ProxyScriptDeciderTest, CustomPacSucceeds) { | 130 TEST(ProxyScriptDeciderTest, CustomPacSucceeds) { |
120 Rules rules; | 131 Rules rules; |
121 RuleBasedProxyScriptFetcher fetcher(&rules); | 132 RuleBasedProxyScriptFetcher fetcher(&rules); |
122 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 133 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
123 | 134 |
124 ProxyConfig config; | 135 ProxyConfig config; |
125 config.set_pac_url(GURL("http://custom/proxy.pac")); | 136 config.set_pac_url(GURL("http://custom/proxy.pac")); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 TestCompletionCallback callback; | 247 TestCompletionCallback callback; |
237 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); | 248 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); |
238 EXPECT_EQ(OK, decider.Start( | 249 EXPECT_EQ(OK, decider.Start( |
239 config, base::TimeDelta(), true, callback.callback())); | 250 config, base::TimeDelta(), true, callback.callback())); |
240 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); | 251 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); |
241 | 252 |
242 EXPECT_TRUE(decider.effective_config().has_pac_url()); | 253 EXPECT_TRUE(decider.effective_config().has_pac_url()); |
243 EXPECT_EQ(rule.url, decider.effective_config().pac_url()); | 254 EXPECT_EQ(rule.url, decider.effective_config().pac_url()); |
244 } | 255 } |
245 | 256 |
| 257 class ProxyScriptDeciderQuickCheckTest : public ::testing::Test { |
| 258 public: |
| 259 ProxyScriptDeciderQuickCheckTest() |
| 260 : rule_(rules_.AddSuccessRule("http://wpad/wpad.dat")), |
| 261 fetcher_(&rules_) { } |
| 262 |
| 263 virtual void SetUp() OVERRIDE { |
| 264 request_context_.set_host_resolver(&resolver_); |
| 265 fetcher_.SetRequestContext(&request_context_); |
| 266 config_.set_auto_detect(true); |
| 267 decider_.reset(new ProxyScriptDecider(&fetcher_, &dhcp_fetcher_, NULL)); |
| 268 } |
| 269 |
| 270 int StartDecider() { |
| 271 return decider_->Start(config_, base::TimeDelta(), true, |
| 272 callback_.callback()); |
| 273 } |
| 274 |
| 275 protected: |
| 276 scoped_ptr<ProxyScriptDecider> decider_; |
| 277 MockHostResolver resolver_; |
| 278 Rules rules_; |
| 279 Rules::Rule rule_; |
| 280 TestCompletionCallback callback_; |
| 281 |
| 282 private: |
| 283 URLRequestContext request_context_; |
| 284 |
| 285 RuleBasedProxyScriptFetcher fetcher_; |
| 286 DoNothingDhcpProxyScriptFetcher dhcp_fetcher_; |
| 287 |
| 288 ProxyConfig config_; |
| 289 }; |
| 290 |
| 291 // Fails if a synchronous DNS lookup success for wpad causes QuickCheck to fail. |
| 292 TEST_F(ProxyScriptDeciderQuickCheckTest, SyncSuccess) { |
| 293 resolver_.set_synchronous_mode(true); |
| 294 resolver_.rules()->AddRule("wpad", "1.2.3.4"); |
| 295 |
| 296 EXPECT_EQ(OK, StartDecider()); |
| 297 EXPECT_EQ(rule_.text(), decider_->script_data()->utf16()); |
| 298 |
| 299 EXPECT_TRUE(decider_->effective_config().has_pac_url()); |
| 300 EXPECT_EQ(rule_.url, decider_->effective_config().pac_url()); |
| 301 } |
| 302 |
| 303 // Fails if an asynchronous DNS lookup success for wpad causes QuickCheck to |
| 304 // fail. |
| 305 TEST_F(ProxyScriptDeciderQuickCheckTest, AsyncSuccess) { |
| 306 resolver_.set_ondemand_mode(true); |
| 307 resolver_.rules()->AddRule("wpad", "1.2.3.4"); |
| 308 |
| 309 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); |
| 310 ASSERT_TRUE(resolver_.has_pending_requests()); |
| 311 resolver_.ResolveAllPending(); |
| 312 callback_.WaitForResult(); |
| 313 EXPECT_FALSE(resolver_.has_pending_requests()); |
| 314 EXPECT_EQ(rule_.text(), decider_->script_data()->utf16()); |
| 315 EXPECT_TRUE(decider_->effective_config().has_pac_url()); |
| 316 EXPECT_EQ(rule_.url, decider_->effective_config().pac_url()); |
| 317 } |
| 318 |
| 319 // Fails if an asynchronous DNS lookup failure (i.e. an NXDOMAIN) still causes |
| 320 // ProxyScriptDecider to yield a PAC URL. |
| 321 TEST_F(ProxyScriptDeciderQuickCheckTest, AsyncFail) { |
| 322 resolver_.set_ondemand_mode(true); |
| 323 resolver_.rules()->AddSimulatedFailure("wpad"); |
| 324 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); |
| 325 ASSERT_TRUE(resolver_.has_pending_requests()); |
| 326 resolver_.ResolveAllPending(); |
| 327 callback_.WaitForResult(); |
| 328 EXPECT_FALSE(decider_->effective_config().has_pac_url()); |
| 329 } |
| 330 |
| 331 // Fails if a DNS lookup timeout either causes ProxyScriptDecider to yield a PAC |
| 332 // URL or causes ProxyScriptDecider not to cancel its pending resolution. |
| 333 TEST_F(ProxyScriptDeciderQuickCheckTest, AsyncTimeout) { |
| 334 resolver_.set_ondemand_mode(true); |
| 335 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); |
| 336 ASSERT_TRUE(resolver_.has_pending_requests()); |
| 337 callback_.WaitForResult(); |
| 338 EXPECT_FALSE(resolver_.has_pending_requests()); |
| 339 EXPECT_FALSE(decider_->effective_config().has_pac_url()); |
| 340 } |
| 341 |
246 // Fails at WPAD (downloading), but succeeds in choosing the custom PAC. | 342 // Fails at WPAD (downloading), but succeeds in choosing the custom PAC. |
247 TEST(ProxyScriptDeciderTest, AutodetectFailCustomSuccess1) { | 343 TEST(ProxyScriptDeciderTest, AutodetectFailCustomSuccess1) { |
248 Rules rules; | 344 Rules rules; |
249 RuleBasedProxyScriptFetcher fetcher(&rules); | 345 RuleBasedProxyScriptFetcher fetcher(&rules); |
250 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 346 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
251 | 347 |
252 ProxyConfig config; | 348 ProxyConfig config; |
253 config.set_auto_detect(true); | 349 config.set_auto_detect(true); |
254 config.set_pac_url(GURL("http://custom/proxy.pac")); | 350 config.set_pac_url(GURL("http://custom/proxy.pac")); |
255 | 351 |
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 | 686 |
591 // Run the message loop to let the DHCP fetch complete and post the results | 687 // Run the message loop to let the DHCP fetch complete and post the results |
592 // back. Before the fix linked to above, this would try to invoke on | 688 // back. Before the fix linked to above, this would try to invoke on |
593 // the callback object provided by ProxyScriptDecider after it was | 689 // the callback object provided by ProxyScriptDecider after it was |
594 // no longer valid. | 690 // no longer valid. |
595 base::MessageLoop::current()->RunUntilIdle(); | 691 base::MessageLoop::current()->RunUntilIdle(); |
596 } | 692 } |
597 | 693 |
598 } // namespace | 694 } // namespace |
599 } // namespace net | 695 } // namespace net |
OLD | NEW |