Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1153)

Side by Side Diff: net/proxy/proxy_script_decider_unittest.cc

Issue 23181010: Fast-fail WPAD detection. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Add unit tests Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/strings/string_util.h" 10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h" 12 #include "base/time/time.h"
13 #include "net/base/net_errors.h" 13 #include "net/base/net_errors.h"
14 #include "net/base/net_log.h" 14 #include "net/base/net_log.h"
15 #include "net/base/net_log_unittest.h" 15 #include "net/base/net_log_unittest.h"
16 #include "net/base/test_completion_callback.h" 16 #include "net/base/test_completion_callback.h"
17 #include "net/dns/mock_host_resolver.h"
17 #include "net/proxy/dhcp_proxy_script_fetcher.h" 18 #include "net/proxy/dhcp_proxy_script_fetcher.h"
18 #include "net/proxy/proxy_config.h" 19 #include "net/proxy/proxy_config.h"
19 #include "net/proxy/proxy_resolver.h" 20 #include "net/proxy/proxy_resolver.h"
20 #include "net/proxy/proxy_script_decider.h" 21 #include "net/proxy/proxy_script_decider.h"
21 #include "net/proxy/proxy_script_fetcher.h" 22 #include "net/proxy/proxy_script_fetcher.h"
23 #include "net/url_request/url_request_context.h"
22 #include "testing/gtest/include/gtest/gtest.h" 24 #include "testing/gtest/include/gtest/gtest.h"
23 25
24 namespace net { 26 namespace net {
25 namespace { 27 namespace {
26 28
27 enum Error { 29 enum Error {
28 kFailedDownloading = -100, 30 kFailedDownloading = -100,
29 kFailedParsing = ERR_PAC_SCRIPT_FAILED, 31 kFailedParsing = ERR_PAC_SCRIPT_FAILED,
30 }; 32 };
31 33
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 return rules_[0]; 88 return rules_[0];
87 } 89 }
88 90
89 private: 91 private:
90 typedef std::vector<Rule> RuleList; 92 typedef std::vector<Rule> RuleList;
91 RuleList rules_; 93 RuleList rules_;
92 }; 94 };
93 95
94 class RuleBasedProxyScriptFetcher : public ProxyScriptFetcher { 96 class RuleBasedProxyScriptFetcher : public ProxyScriptFetcher {
95 public: 97 public:
96 explicit RuleBasedProxyScriptFetcher(const Rules* rules) : rules_(rules) {} 98 explicit RuleBasedProxyScriptFetcher(const Rules* rules)
99 : rules_(rules), request_context_(NULL) {}
97 100
98 // ProxyScriptFetcher implementation. 101 // ProxyScriptFetcher implementation.
99 virtual int Fetch(const GURL& url, 102 virtual int Fetch(const GURL& url,
100 base::string16* text, 103 base::string16* text,
101 const CompletionCallback& callback) OVERRIDE { 104 const CompletionCallback& callback) OVERRIDE {
102 const Rules::Rule& rule = rules_->GetRuleByUrl(url); 105 const Rules::Rule& rule = rules_->GetRuleByUrl(url);
103 int rv = rule.fetch_error; 106 int rv = rule.fetch_error;
104 EXPECT_NE(ERR_UNEXPECTED, rv); 107 EXPECT_NE(ERR_UNEXPECTED, rv);
105 if (rv == OK) 108 if (rv == OK)
106 *text = rule.text(); 109 *text = rule.text();
107 return rv; 110 return rv;
108 } 111 }
109 112
110 virtual void Cancel() OVERRIDE {} 113 virtual void Cancel() OVERRIDE {}
111 114
112 virtual URLRequestContext* GetRequestContext() const OVERRIDE { return NULL; } 115 virtual void SetRequestContext(URLRequestContext* context) {
szym 2013/09/11 14:06:22 nit: move this out of here, so that the "ProxyScri
Elly Fong-Jones 2013/09/11 20:20:37 Done.
116 request_context_ = context;
117 }
118
119 virtual URLRequestContext* GetRequestContext() const OVERRIDE {
120 return request_context_;
121 }
113 122
114 private: 123 private:
115 const Rules* rules_; 124 const Rules* rules_;
125 URLRequestContext* request_context_;
116 }; 126 };
117 127
118 // Succeed using custom PAC script. 128 // Succeed using custom PAC script.
119 TEST(ProxyScriptDeciderTest, CustomPacSucceeds) { 129 TEST(ProxyScriptDeciderTest, CustomPacSucceeds) {
120 Rules rules; 130 Rules rules;
121 RuleBasedProxyScriptFetcher fetcher(&rules); 131 RuleBasedProxyScriptFetcher fetcher(&rules);
122 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; 132 DoNothingDhcpProxyScriptFetcher dhcp_fetcher;
123 133
124 ProxyConfig config; 134 ProxyConfig config;
125 config.set_pac_url(GURL("http://custom/proxy.pac")); 135 config.set_pac_url(GURL("http://custom/proxy.pac"));
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 TestCompletionCallback callback; 246 TestCompletionCallback callback;
237 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); 247 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL);
238 EXPECT_EQ(OK, decider.Start( 248 EXPECT_EQ(OK, decider.Start(
239 config, base::TimeDelta(), true, callback.callback())); 249 config, base::TimeDelta(), true, callback.callback()));
240 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); 250 EXPECT_EQ(rule.text(), decider.script_data()->utf16());
241 251
242 EXPECT_TRUE(decider.effective_config().has_pac_url()); 252 EXPECT_TRUE(decider.effective_config().has_pac_url());
243 EXPECT_EQ(rule.url, decider.effective_config().pac_url()); 253 EXPECT_EQ(rule.url, decider.effective_config().pac_url());
244 } 254 }
245 255
256 class ProxyScriptDeciderQuickCheckTest : public ::testing::Test {
257 public:
258 ProxyScriptDeciderQuickCheckTest()
259 : rule_(rules_.AddSuccessRule("http://wpad/wpad.dat")),
260 fetcher_(&rules_) { }
261
262 virtual void SetUp() {
szym 2013/09/11 14:06:22 OVERRIDE
Elly Fong-Jones 2013/09/11 20:20:37 Done.
263 request_context_.set_host_resolver(&resolver_);
264 fetcher_.SetRequestContext(&request_context_);
265 config_.set_auto_detect(true);
266 decider_.reset(new ProxyScriptDecider(&fetcher_, &dhcp_fetcher_, NULL));
267 }
268
269 virtual int StartDecider() {
szym 2013/09/11 14:06:22 why virtual?
Elly Fong-Jones 2013/09/11 20:20:37 Habit. Removed.
270 return decider()->Start(config_, base::TimeDelta(), true,
271 callback_.callback());
272 }
273
274 Rules::Rule* rule() { return &rule_; }
275 ProxyScriptDecider* decider() { return decider_.get(); }
276 MockHostResolver* resolver() { return &resolver_; }
277
278 private:
szym 2013/09/11 14:06:22 I'd suggest making this protected and using direct
Elly Fong-Jones 2013/09/11 20:20:37 Done.
279 URLRequestContext request_context_;
280 MockHostResolver resolver_;
281
282 Rules rules_;
283 Rules::Rule rule_;
284 RuleBasedProxyScriptFetcher fetcher_;
285 DoNothingDhcpProxyScriptFetcher dhcp_fetcher_;
286
287 TestCompletionCallback callback_;
288
289 ProxyConfig config_;
290
291 scoped_ptr<ProxyScriptDecider> decider_;
292 };
293
294 TEST_F(ProxyScriptDeciderQuickCheckTest, SyncSuccess) {
295 resolver()->set_synchronous_mode(true);
296 resolver()->rules()->AddRule("wpad", "1.2.3.4");
297
298 EXPECT_EQ(OK, StartDecider());
299 EXPECT_EQ(rule()->text(), decider()->script_data()->utf16());
300
301 EXPECT_TRUE(decider()->effective_config().has_pac_url());
302 EXPECT_EQ(rule()->url, decider()->effective_config().pac_url());
303 }
304
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 // Let the resolutions complete.
313 base::MessageLoop::current()->RunUntilIdle();
szym 2013/09/11 14:06:22 You should callback_.WaitForResult rather than Run
Elly Fong-Jones 2013/09/11 20:20:37 Oh man, I never knew about WaitForResult! This als
314 EXPECT_FALSE(resolver()->has_pending_requests());
315 EXPECT_EQ(rule()->text(), decider()->script_data()->utf16());
316 EXPECT_TRUE(decider()->effective_config().has_pac_url());
317 EXPECT_EQ(rule()->url, decider()->effective_config().pac_url());
318 }
319
320 TEST_F(ProxyScriptDeciderQuickCheckTest, QuickCheckAsyncFail) {
321 resolver()->set_ondemand_mode(true);
322 resolver()->rules()->AddSimulatedFailure("wpad");
323 EXPECT_EQ(ERR_IO_PENDING, StartDecider());
324 // let the timer run
szym 2013/09/11 14:06:22 I don't understand the comment. There's barely any
Elly Fong-Jones 2013/09/11 20:20:37 Done.
325 ASSERT_TRUE(resolver()->has_pending_requests());
326 resolver()->ResolveAllPending();
327 base::MessageLoop::current()->RunUntilIdle();
328 EXPECT_FALSE(decider()->effective_config().has_pac_url());
329 }
330
szym 2013/09/11 14:06:22 You need a test which exercises the quick check ti
Elly Fong-Jones 2013/09/11 20:20:37 Done.
246 // Fails at WPAD (downloading), but succeeds in choosing the custom PAC. 331 // Fails at WPAD (downloading), but succeeds in choosing the custom PAC.
247 TEST(ProxyScriptDeciderTest, AutodetectFailCustomSuccess1) { 332 TEST(ProxyScriptDeciderTest, AutodetectFailCustomSuccess1) {
248 Rules rules; 333 Rules rules;
249 RuleBasedProxyScriptFetcher fetcher(&rules); 334 RuleBasedProxyScriptFetcher fetcher(&rules);
250 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; 335 DoNothingDhcpProxyScriptFetcher dhcp_fetcher;
251 336
252 ProxyConfig config; 337 ProxyConfig config;
253 config.set_auto_detect(true); 338 config.set_auto_detect(true);
254 config.set_pac_url(GURL("http://custom/proxy.pac")); 339 config.set_pac_url(GURL("http://custom/proxy.pac"));
255 340
(...skipping 334 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 675
591 // Run the message loop to let the DHCP fetch complete and post the results 676 // 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 677 // back. Before the fix linked to above, this would try to invoke on
593 // the callback object provided by ProxyScriptDecider after it was 678 // the callback object provided by ProxyScriptDecider after it was
594 // no longer valid. 679 // no longer valid.
595 base::MessageLoop::current()->RunUntilIdle(); 680 base::MessageLoop::current()->RunUntilIdle();
596 } 681 }
597 682
598 } // namespace 683 } // namespace
599 } // namespace net 684 } // namespace net
OLDNEW
« net/proxy/proxy_script_decider.cc ('K') | « net/proxy/proxy_script_decider.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698