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

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

Issue 8896019: Refactor: Extract "InitProxyResolver" to "ProxyScriptDecider". (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: do another sync since commitbot failed... Created 9 years 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 | Annotate | Revision Log
« no previous file with comments | « net/proxy/init_proxy_resolver.cc ('k') | net/proxy/proxy_script_decider.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <vector>
6
7 #include "base/message_loop.h"
8 #include "base/string_util.h"
9 #include "base/time.h"
10 #include "base/utf_string_conversions.h"
11 #include "net/base/net_errors.h"
12 #include "net/base/net_log.h"
13 #include "net/base/net_log_unittest.h"
14 #include "net/base/test_completion_callback.h"
15 #include "net/proxy/init_proxy_resolver.h"
16 #include "net/proxy/dhcp_proxy_script_fetcher.h"
17 #include "net/proxy/proxy_config.h"
18 #include "net/proxy/proxy_resolver.h"
19 #include "net/proxy/proxy_script_fetcher.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace net {
23 namespace {
24
25 enum Error {
26 kFailedDownloading = -100,
27 kFailedParsing = -200,
28 };
29
30 class Rules {
31 public:
32 struct Rule {
33 Rule(const GURL& url,
34 int fetch_error,
35 int set_pac_error)
36 : url(url),
37 fetch_error(fetch_error),
38 set_pac_error(set_pac_error) {
39 }
40
41 string16 text() const {
42 if (set_pac_error == OK)
43 return UTF8ToUTF16(url.spec() + "!valid-script");
44 if (fetch_error == OK)
45 return UTF8ToUTF16(url.spec() + "!invalid-script");
46 return string16();
47 }
48
49 GURL url;
50 int fetch_error;
51 int set_pac_error;
52 };
53
54 Rule AddSuccessRule(const char* url) {
55 Rule rule(GURL(url), OK /*fetch_error*/, OK /*set_pac_error*/);
56 rules_.push_back(rule);
57 return rule;
58 }
59
60 void AddFailDownloadRule(const char* url) {
61 rules_.push_back(Rule(GURL(url), kFailedDownloading /*fetch_error*/,
62 ERR_UNEXPECTED /*set_pac_error*/));
63 }
64
65 void AddFailParsingRule(const char* url) {
66 rules_.push_back(Rule(GURL(url), OK /*fetch_error*/,
67 kFailedParsing /*set_pac_error*/));
68 }
69
70 const Rule& GetRuleByUrl(const GURL& url) const {
71 for (RuleList::const_iterator it = rules_.begin(); it != rules_.end();
72 ++it) {
73 if (it->url == url)
74 return *it;
75 }
76 LOG(FATAL) << "Rule not found for " << url;
77 return rules_[0];
78 }
79
80 const Rule& GetRuleByText(const string16& text) const {
81 for (RuleList::const_iterator it = rules_.begin(); it != rules_.end();
82 ++it) {
83 if (it->text() == text)
84 return *it;
85 }
86 LOG(FATAL) << "Rule not found for " << text;
87 return rules_[0];
88 }
89
90 private:
91 typedef std::vector<Rule> RuleList;
92 RuleList rules_;
93 };
94
95 class RuleBasedProxyScriptFetcher : public ProxyScriptFetcher {
96 public:
97 explicit RuleBasedProxyScriptFetcher(const Rules* rules) : rules_(rules) {}
98
99 // ProxyScriptFetcher implementation.
100 virtual int Fetch(const GURL& url,
101 string16* text,
102 OldCompletionCallback* callback) {
103 const Rules::Rule& rule = rules_->GetRuleByUrl(url);
104 int rv = rule.fetch_error;
105 EXPECT_NE(ERR_UNEXPECTED, rv);
106 if (rv == OK)
107 *text = rule.text();
108 return rv;
109 }
110
111 virtual void Cancel() {}
112
113 virtual URLRequestContext* GetRequestContext() const { return NULL; }
114
115 private:
116 const Rules* rules_;
117 };
118
119 class RuleBasedProxyResolver : public ProxyResolver {
120 public:
121 RuleBasedProxyResolver(const Rules* rules, bool expects_pac_bytes)
122 : ProxyResolver(expects_pac_bytes), rules_(rules) {}
123
124 // ProxyResolver implementation:
125 virtual int GetProxyForURL(const GURL& /*url*/,
126 ProxyInfo* /*results*/,
127 OldCompletionCallback* /*callback*/,
128 RequestHandle* /*request_handle*/,
129 const BoundNetLog& /*net_log*/) {
130 NOTREACHED();
131 return ERR_UNEXPECTED;
132 }
133
134 virtual void CancelRequest(RequestHandle request_handle) {
135 NOTREACHED();
136 }
137
138 virtual LoadState GetLoadState(RequestHandle request_handle) const {
139 NOTREACHED();
140 return LOAD_STATE_IDLE;
141 }
142
143 virtual LoadState GetLoadStateThreadSafe(
144 RequestHandle request) const OVERRIDE {
145 NOTREACHED();
146 return LOAD_STATE_IDLE;
147 }
148
149 virtual void CancelSetPacScript() {
150 NOTREACHED();
151 }
152
153 virtual int SetPacScript(
154 const scoped_refptr<ProxyResolverScriptData>& script_data,
155 OldCompletionCallback* callback) {
156
157 const GURL url =
158 script_data->type() == ProxyResolverScriptData::TYPE_SCRIPT_URL ?
159 script_data->url() : GURL();
160
161 const Rules::Rule& rule = expects_pac_bytes() ?
162 rules_->GetRuleByText(script_data->utf16()) :
163 rules_->GetRuleByUrl(url);
164
165 int rv = rule.set_pac_error;
166 EXPECT_NE(ERR_UNEXPECTED, rv);
167
168 if (expects_pac_bytes()) {
169 EXPECT_EQ(rule.text(), script_data->utf16());
170 } else {
171 EXPECT_EQ(rule.url, url);
172 }
173
174 if (rv == OK)
175 script_data_ = script_data;
176 return rv;
177 }
178
179 const ProxyResolverScriptData* script_data() const { return script_data_; }
180
181 private:
182 const Rules* rules_;
183 scoped_refptr<ProxyResolverScriptData> script_data_;
184 };
185
186 // Succeed using custom PAC script.
187 TEST(InitProxyResolverTest, CustomPacSucceeds) {
188 Rules rules;
189 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
190 RuleBasedProxyScriptFetcher fetcher(&rules);
191 DoNothingDhcpProxyScriptFetcher dhcp_fetcher;
192
193 ProxyConfig config;
194 config.set_pac_url(GURL("http://custom/proxy.pac"));
195
196 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");
197
198 TestOldCompletionCallback callback;
199 CapturingNetLog log(CapturingNetLog::kUnbounded);
200 ProxyConfig effective_config;
201 InitProxyResolver init(&resolver, &fetcher, &dhcp_fetcher, &log);
202 EXPECT_EQ(OK, init.Init(
203 config, base::TimeDelta(), &effective_config, &callback));
204 EXPECT_EQ(rule.text(), resolver.script_data()->utf16());
205
206 // Check the NetLog was filled correctly.
207 CapturingNetLog::EntryList entries;
208 log.GetEntries(&entries);
209
210 EXPECT_EQ(6u, entries.size());
211 EXPECT_TRUE(LogContainsBeginEvent(
212 entries, 0, NetLog::TYPE_INIT_PROXY_RESOLVER));
213 EXPECT_TRUE(LogContainsBeginEvent(
214 entries, 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
215 EXPECT_TRUE(LogContainsEndEvent(
216 entries, 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
217 EXPECT_TRUE(LogContainsBeginEvent(
218 entries, 3, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
219 EXPECT_TRUE(LogContainsEndEvent(
220 entries, 4, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
221 EXPECT_TRUE(LogContainsEndEvent(
222 entries, 5, NetLog::TYPE_INIT_PROXY_RESOLVER));
223
224 EXPECT_TRUE(effective_config.has_pac_url());
225 EXPECT_EQ(config.pac_url(), effective_config.pac_url());
226 }
227
228 // Fail downloading the custom PAC script.
229 TEST(InitProxyResolverTest, CustomPacFails1) {
230 Rules rules;
231 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
232 RuleBasedProxyScriptFetcher fetcher(&rules);
233 DoNothingDhcpProxyScriptFetcher dhcp_fetcher;
234
235 ProxyConfig config;
236 config.set_pac_url(GURL("http://custom/proxy.pac"));
237
238 rules.AddFailDownloadRule("http://custom/proxy.pac");
239
240 TestOldCompletionCallback callback;
241 CapturingNetLog log(CapturingNetLog::kUnbounded);
242 ProxyConfig effective_config;
243 InitProxyResolver init(&resolver, &fetcher, &dhcp_fetcher, &log);
244 EXPECT_EQ(kFailedDownloading,
245 init.Init(config, base::TimeDelta(), &effective_config, &callback));
246 EXPECT_EQ(NULL, resolver.script_data());
247
248 // Check the NetLog was filled correctly.
249 CapturingNetLog::EntryList entries;
250 log.GetEntries(&entries);
251
252 EXPECT_EQ(4u, entries.size());
253 EXPECT_TRUE(LogContainsBeginEvent(
254 entries, 0, NetLog::TYPE_INIT_PROXY_RESOLVER));
255 EXPECT_TRUE(LogContainsBeginEvent(
256 entries, 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
257 EXPECT_TRUE(LogContainsEndEvent(
258 entries, 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
259 EXPECT_TRUE(LogContainsEndEvent(
260 entries, 3, NetLog::TYPE_INIT_PROXY_RESOLVER));
261
262 EXPECT_FALSE(effective_config.has_pac_url());
263 }
264
265 // Fail parsing the custom PAC script.
266 TEST(InitProxyResolverTest, CustomPacFails2) {
267 Rules rules;
268 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
269 RuleBasedProxyScriptFetcher fetcher(&rules);
270 DoNothingDhcpProxyScriptFetcher dhcp_fetcher;
271
272 ProxyConfig config;
273 config.set_pac_url(GURL("http://custom/proxy.pac"));
274
275 rules.AddFailParsingRule("http://custom/proxy.pac");
276
277 TestOldCompletionCallback callback;
278 InitProxyResolver init(&resolver, &fetcher, &dhcp_fetcher, NULL);
279 EXPECT_EQ(kFailedParsing,
280 init.Init(config, base::TimeDelta(), NULL, &callback));
281 EXPECT_EQ(NULL, resolver.script_data());
282 }
283
284 // Fail downloading the custom PAC script, because the fetcher was NULL.
285 TEST(InitProxyResolverTest, HasNullProxyScriptFetcher) {
286 Rules rules;
287 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
288 DoNothingDhcpProxyScriptFetcher dhcp_fetcher;
289
290 ProxyConfig config;
291 config.set_pac_url(GURL("http://custom/proxy.pac"));
292
293 TestOldCompletionCallback callback;
294 InitProxyResolver init(&resolver, NULL, &dhcp_fetcher, NULL);
295 EXPECT_EQ(ERR_UNEXPECTED,
296 init.Init(config, base::TimeDelta(), NULL, &callback));
297 EXPECT_EQ(NULL, resolver.script_data());
298 }
299
300 // Succeeds in choosing autodetect (WPAD DNS).
301 TEST(InitProxyResolverTest, AutodetectSuccess) {
302 Rules rules;
303 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
304 RuleBasedProxyScriptFetcher fetcher(&rules);
305 DoNothingDhcpProxyScriptFetcher dhcp_fetcher;
306
307 ProxyConfig config;
308 config.set_auto_detect(true);
309
310 Rules::Rule rule = rules.AddSuccessRule("http://wpad/wpad.dat");
311
312 TestOldCompletionCallback callback;
313 ProxyConfig effective_config;
314 InitProxyResolver init(&resolver, &fetcher, &dhcp_fetcher, NULL);
315 EXPECT_EQ(OK, init.Init(
316 config, base::TimeDelta(), &effective_config, &callback));
317 EXPECT_EQ(rule.text(), resolver.script_data()->utf16());
318
319 EXPECT_TRUE(effective_config.has_pac_url());
320 EXPECT_EQ(rule.url, effective_config.pac_url());
321 }
322
323 // Fails at WPAD (downloading), but succeeds in choosing the custom PAC.
324 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess1) {
325 Rules rules;
326 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
327 RuleBasedProxyScriptFetcher fetcher(&rules);
328 DoNothingDhcpProxyScriptFetcher dhcp_fetcher;
329
330 ProxyConfig config;
331 config.set_auto_detect(true);
332 config.set_pac_url(GURL("http://custom/proxy.pac"));
333
334 rules.AddFailDownloadRule("http://wpad/wpad.dat");
335 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");
336
337 TestOldCompletionCallback callback;
338 ProxyConfig effective_config;
339 InitProxyResolver init(&resolver, &fetcher, &dhcp_fetcher, NULL);
340 EXPECT_EQ(OK, init.Init(
341 config, base::TimeDelta(), &effective_config, &callback));
342 EXPECT_EQ(rule.text(), resolver.script_data()->utf16());
343
344 EXPECT_TRUE(effective_config.has_pac_url());
345 EXPECT_EQ(rule.url, effective_config.pac_url());
346 }
347
348 // Fails at WPAD (no DHCP config, DNS PAC fails parsing), but succeeds in
349 // choosing the custom PAC.
350 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2) {
351 Rules rules;
352 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
353 RuleBasedProxyScriptFetcher fetcher(&rules);
354 DoNothingDhcpProxyScriptFetcher dhcp_fetcher;
355
356 ProxyConfig config;
357 config.set_auto_detect(true);
358 config.set_pac_url(GURL("http://custom/proxy.pac"));
359 config.proxy_rules().ParseFromString("unused-manual-proxy:99");
360
361 rules.AddFailParsingRule("http://wpad/wpad.dat");
362 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");
363
364 TestOldCompletionCallback callback;
365 CapturingNetLog log(CapturingNetLog::kUnbounded);
366
367 ProxyConfig effective_config;
368 InitProxyResolver init(&resolver, &fetcher, &dhcp_fetcher, &log);
369 EXPECT_EQ(OK, init.Init(config, base::TimeDelta(),
370 &effective_config, &callback));
371 EXPECT_EQ(rule.text(), resolver.script_data()->utf16());
372
373 // Verify that the effective configuration no longer contains auto detect or
374 // any of the manual settings.
375 EXPECT_TRUE(effective_config.Equals(
376 ProxyConfig::CreateFromCustomPacURL(GURL("http://custom/proxy.pac"))));
377
378 // Check the NetLog was filled correctly.
379 // (Note that various states are repeated since both WPAD and custom
380 // PAC scripts are tried).
381 CapturingNetLog::EntryList entries;
382 log.GetEntries(&entries);
383
384 EXPECT_EQ(14u, entries.size());
385 EXPECT_TRUE(LogContainsBeginEvent(
386 entries, 0, NetLog::TYPE_INIT_PROXY_RESOLVER));
387 // This is the DHCP phase, which fails fetching rather than parsing, so
388 // there is no pair of SET_PAC_SCRIPT events.
389 EXPECT_TRUE(LogContainsBeginEvent(
390 entries, 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
391 EXPECT_TRUE(LogContainsEndEvent(
392 entries, 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
393 EXPECT_TRUE(LogContainsEvent(
394 entries, 3,
395 NetLog::TYPE_INIT_PROXY_RESOLVER_FALLING_BACK_TO_NEXT_PAC_SOURCE,
396 NetLog::PHASE_NONE));
397 // This is the DNS phase, which attempts a fetch but fails.
398 EXPECT_TRUE(LogContainsBeginEvent(
399 entries, 4, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
400 EXPECT_TRUE(LogContainsEndEvent(
401 entries, 5, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
402 EXPECT_TRUE(LogContainsBeginEvent(
403 entries, 6, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
404 EXPECT_TRUE(LogContainsEndEvent(
405 entries, 7, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
406 EXPECT_TRUE(LogContainsEvent(
407 entries, 8,
408 NetLog::TYPE_INIT_PROXY_RESOLVER_FALLING_BACK_TO_NEXT_PAC_SOURCE,
409 NetLog::PHASE_NONE));
410 // Finally, the custom PAC URL phase.
411 EXPECT_TRUE(LogContainsBeginEvent(
412 entries, 9, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
413 EXPECT_TRUE(LogContainsEndEvent(
414 entries, 10, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
415 EXPECT_TRUE(LogContainsBeginEvent(
416 entries, 11, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
417 EXPECT_TRUE(LogContainsEndEvent(
418 entries, 12, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
419 EXPECT_TRUE(LogContainsEndEvent(
420 entries, 13, NetLog::TYPE_INIT_PROXY_RESOLVER));
421 }
422
423 // Fails at WPAD (downloading), and fails at custom PAC (downloading).
424 TEST(InitProxyResolverTest, AutodetectFailCustomFails1) {
425 Rules rules;
426 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
427 RuleBasedProxyScriptFetcher fetcher(&rules);
428 DoNothingDhcpProxyScriptFetcher dhcp_fetcher;
429
430 ProxyConfig config;
431 config.set_auto_detect(true);
432 config.set_pac_url(GURL("http://custom/proxy.pac"));
433
434 rules.AddFailDownloadRule("http://wpad/wpad.dat");
435 rules.AddFailDownloadRule("http://custom/proxy.pac");
436
437 TestOldCompletionCallback callback;
438 InitProxyResolver init(&resolver, &fetcher, &dhcp_fetcher, NULL);
439 EXPECT_EQ(kFailedDownloading,
440 init.Init(config, base::TimeDelta(), NULL, &callback));
441 EXPECT_EQ(NULL, resolver.script_data());
442 }
443
444 // Fails at WPAD (downloading), and fails at custom PAC (parsing).
445 TEST(InitProxyResolverTest, AutodetectFailCustomFails2) {
446 Rules rules;
447 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
448 RuleBasedProxyScriptFetcher fetcher(&rules);
449 DoNothingDhcpProxyScriptFetcher dhcp_fetcher;
450
451 ProxyConfig config;
452 config.set_auto_detect(true);
453 config.set_pac_url(GURL("http://custom/proxy.pac"));
454
455 rules.AddFailDownloadRule("http://wpad/wpad.dat");
456 rules.AddFailParsingRule("http://custom/proxy.pac");
457
458 TestOldCompletionCallback callback;
459 InitProxyResolver init(&resolver, &fetcher, &dhcp_fetcher, NULL);
460 EXPECT_EQ(kFailedParsing,
461 init.Init(config, base::TimeDelta(), NULL, &callback));
462 EXPECT_EQ(NULL, resolver.script_data());
463 }
464
465 // Fails at WPAD (parsing), but succeeds in choosing the custom PAC.
466 // This is the same as AutodetectFailCustomSuccess2, but using a ProxyResolver
467 // that doesn't |expects_pac_bytes|.
468 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2_NoFetch) {
469 Rules rules;
470 RuleBasedProxyResolver resolver(&rules, false /*expects_pac_bytes*/);
471 RuleBasedProxyScriptFetcher fetcher(&rules);
472 DoNothingDhcpProxyScriptFetcher dhcp_fetcher;
473
474 ProxyConfig config;
475 config.set_auto_detect(true);
476 config.set_pac_url(GURL("http://custom/proxy.pac"));
477
478 rules.AddFailParsingRule(""); // Autodetect.
479 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");
480
481 TestOldCompletionCallback callback;
482 InitProxyResolver init(&resolver, &fetcher, &dhcp_fetcher, NULL);
483 EXPECT_EQ(OK, init.Init(config, base::TimeDelta(), NULL, &callback));
484 EXPECT_EQ(rule.url, resolver.script_data()->url());
485 }
486
487 // This is a copy-paste of CustomPacFails1, with the exception that we give it
488 // a 1 millisecond delay. This means it will now complete asynchronously.
489 // Moreover, we test the NetLog to make sure it logged the pause.
490 TEST(InitProxyResolverTest, CustomPacFails1_WithPositiveDelay) {
491 Rules rules;
492 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
493 RuleBasedProxyScriptFetcher fetcher(&rules);
494 DoNothingDhcpProxyScriptFetcher dhcp_fetcher;
495
496 ProxyConfig config;
497 config.set_pac_url(GURL("http://custom/proxy.pac"));
498
499 rules.AddFailDownloadRule("http://custom/proxy.pac");
500
501 TestOldCompletionCallback callback;
502 CapturingNetLog log(CapturingNetLog::kUnbounded);
503 InitProxyResolver init(&resolver, &fetcher, &dhcp_fetcher, &log);
504 EXPECT_EQ(ERR_IO_PENDING,
505 init.Init(config, base::TimeDelta::FromMilliseconds(1),
506 NULL, &callback));
507
508 EXPECT_EQ(kFailedDownloading, callback.WaitForResult());
509 EXPECT_EQ(NULL, resolver.script_data());
510
511 // Check the NetLog was filled correctly.
512 CapturingNetLog::EntryList entries;
513 log.GetEntries(&entries);
514
515 EXPECT_EQ(6u, entries.size());
516 EXPECT_TRUE(LogContainsBeginEvent(
517 entries, 0, NetLog::TYPE_INIT_PROXY_RESOLVER));
518 EXPECT_TRUE(LogContainsBeginEvent(
519 entries, 1, NetLog::TYPE_INIT_PROXY_RESOLVER_WAIT));
520 EXPECT_TRUE(LogContainsEndEvent(
521 entries, 2, NetLog::TYPE_INIT_PROXY_RESOLVER_WAIT));
522 EXPECT_TRUE(LogContainsBeginEvent(
523 entries, 3, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
524 EXPECT_TRUE(LogContainsEndEvent(
525 entries, 4, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
526 EXPECT_TRUE(LogContainsEndEvent(
527 entries, 5, NetLog::TYPE_INIT_PROXY_RESOLVER));
528 }
529
530 // This is a copy-paste of CustomPacFails1, with the exception that we give it
531 // a -5 second delay instead of a 0 ms delay. This change should have no effect
532 // so the rest of the test is unchanged.
533 TEST(InitProxyResolverTest, CustomPacFails1_WithNegativeDelay) {
534 Rules rules;
535 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
536 RuleBasedProxyScriptFetcher fetcher(&rules);
537 DoNothingDhcpProxyScriptFetcher dhcp_fetcher;
538
539 ProxyConfig config;
540 config.set_pac_url(GURL("http://custom/proxy.pac"));
541
542 rules.AddFailDownloadRule("http://custom/proxy.pac");
543
544 TestOldCompletionCallback callback;
545 CapturingNetLog log(CapturingNetLog::kUnbounded);
546 InitProxyResolver init(&resolver, &fetcher, &dhcp_fetcher, &log);
547 EXPECT_EQ(kFailedDownloading,
548 init.Init(config, base::TimeDelta::FromSeconds(-5),
549 NULL, &callback));
550 EXPECT_EQ(NULL, resolver.script_data());
551
552 // Check the NetLog was filled correctly.
553 CapturingNetLog::EntryList entries;
554 log.GetEntries(&entries);
555
556 EXPECT_EQ(4u, entries.size());
557 EXPECT_TRUE(LogContainsBeginEvent(
558 entries, 0, NetLog::TYPE_INIT_PROXY_RESOLVER));
559 EXPECT_TRUE(LogContainsBeginEvent(
560 entries, 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
561 EXPECT_TRUE(LogContainsEndEvent(
562 entries, 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
563 EXPECT_TRUE(LogContainsEndEvent(
564 entries, 3, NetLog::TYPE_INIT_PROXY_RESOLVER));
565 }
566
567 class SynchronousSuccessDhcpFetcher : public DhcpProxyScriptFetcher {
568 public:
569 explicit SynchronousSuccessDhcpFetcher(const string16& expected_text)
570 : gurl_("http://dhcppac/"), expected_text_(expected_text) {
571 }
572
573 int Fetch(string16* utf16_text, OldCompletionCallback* callback) OVERRIDE {
574 *utf16_text = expected_text_;
575 return OK;
576 }
577
578 void Cancel() OVERRIDE {
579 }
580
581 const GURL& GetPacURL() const OVERRIDE {
582 return gurl_;
583 }
584
585 const string16& expected_text() const {
586 return expected_text_;
587 }
588
589 private:
590 GURL gurl_;
591 string16 expected_text_;
592
593 DISALLOW_COPY_AND_ASSIGN(SynchronousSuccessDhcpFetcher);
594 };
595
596 // All of the tests above that use InitProxyResolver have tested
597 // failure to fetch a PAC file via DHCP configuration, so we now test
598 // success at downloading and parsing, and then success at downloading,
599 // failure at parsing.
600
601 TEST(InitProxyResolverTest, AutodetectDhcpSuccess) {
602 Rules rules;
603 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
604 RuleBasedProxyScriptFetcher fetcher(&rules);
605 SynchronousSuccessDhcpFetcher dhcp_fetcher(
606 WideToUTF16(L"http://bingo/!valid-script"));
607
608 ProxyConfig config;
609 config.set_auto_detect(true);
610
611 rules.AddSuccessRule("http://bingo/");
612 rules.AddFailDownloadRule("http://wpad/wpad.dat");
613
614 TestOldCompletionCallback callback;
615 ProxyConfig effective_config;
616 InitProxyResolver init(&resolver, &fetcher, &dhcp_fetcher, NULL);
617 EXPECT_EQ(OK, init.Init(
618 config, base::TimeDelta(), &effective_config, &callback));
619 EXPECT_EQ(dhcp_fetcher.expected_text(),
620 resolver.script_data()->utf16());
621
622 EXPECT_TRUE(effective_config.has_pac_url());
623 EXPECT_EQ(GURL("http://dhcppac/"), effective_config.pac_url());
624 }
625
626 TEST(InitProxyResolverTest, AutodetectDhcpFailParse) {
627 Rules rules;
628 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
629 RuleBasedProxyScriptFetcher fetcher(&rules);
630 SynchronousSuccessDhcpFetcher dhcp_fetcher(
631 WideToUTF16(L"http://bingo/!invalid-script"));
632
633 ProxyConfig config;
634 config.set_auto_detect(true);
635
636 rules.AddFailParsingRule("http://bingo/");
637 rules.AddFailDownloadRule("http://wpad/wpad.dat");
638
639 TestOldCompletionCallback callback;
640 ProxyConfig effective_config;
641 InitProxyResolver init(&resolver, &fetcher, &dhcp_fetcher, NULL);
642 // Since there is fallback to DNS-based WPAD, the final error will be that
643 // it failed downloading, not that it failed parsing.
644 EXPECT_EQ(kFailedDownloading,
645 init.Init(config, base::TimeDelta(), &effective_config, &callback));
646 EXPECT_EQ(NULL, resolver.script_data());
647
648 EXPECT_FALSE(effective_config.has_pac_url());
649 }
650
651 class AsyncFailDhcpFetcher
652 : public DhcpProxyScriptFetcher,
653 public base::RefCountedThreadSafe<AsyncFailDhcpFetcher> {
654 public:
655 AsyncFailDhcpFetcher() : callback_(NULL) {
656 }
657
658 int Fetch(string16* utf16_text, OldCompletionCallback* callback) OVERRIDE {
659 callback_ = callback;
660 MessageLoop::current()->PostTask(
661 FROM_HERE,
662 NewRunnableMethod(this, &AsyncFailDhcpFetcher::CallbackWithFailure));
663 return ERR_IO_PENDING;
664 }
665
666 void Cancel() OVERRIDE {
667 callback_ = NULL;
668 }
669
670 const GURL& GetPacURL() const OVERRIDE {
671 return dummy_gurl_;
672 }
673
674 void CallbackWithFailure() {
675 if (callback_)
676 callback_->Run(ERR_PAC_NOT_IN_DHCP);
677 }
678
679 private:
680 GURL dummy_gurl_;
681 OldCompletionCallback* callback_;
682 };
683
684 TEST(InitProxyResolverTest, DhcpCancelledByDestructor) {
685 // This regression test would crash before
686 // http://codereview.chromium.org/7044058/
687 // Thus, we don't care much about actual results (hence no EXPECT or ASSERT
688 // macros below), just that it doesn't crash.
689 Rules rules;
690 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
691 RuleBasedProxyScriptFetcher fetcher(&rules);
692
693 scoped_refptr<AsyncFailDhcpFetcher> dhcp_fetcher(new AsyncFailDhcpFetcher());
694
695 ProxyConfig config;
696 config.set_auto_detect(true);
697 rules.AddFailDownloadRule("http://wpad/wpad.dat");
698
699 TestOldCompletionCallback callback;
700
701 // Scope so InitProxyResolver gets destroyed early.
702 {
703 InitProxyResolver init(&resolver, &fetcher, dhcp_fetcher.get(), NULL);
704 init.Init(config, base::TimeDelta(), NULL, &callback);
705 }
706
707 // Run the message loop to let the DHCP fetch complete and post the results
708 // back. Before the fix linked to above, this would try to invoke on
709 // the callback object provided by InitProxyResolver after it was
710 // no longer valid.
711 MessageLoop::current()->RunAllPending();
712 }
713
714 } // namespace
715 } // namespace net
OLDNEW
« no previous file with comments | « net/proxy/init_proxy_resolver.cc ('k') | net/proxy/proxy_script_decider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698