blob: 0c7e8a19cc6d7b8809304136f868a13eb10969b5 [file] [log] [blame]
[email protected]620f5712009-08-04 22:43:121// Copyright (c) 2009 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
[email protected]9b9ae9552010-07-01 22:20:507#include "base/string_util.h"
8#include "base/utf_string_conversions.h"
[email protected]620f5712009-08-04 22:43:129#include "net/base/net_errors.h"
[email protected]9e743cd2010-03-16 07:03:5310#include "net/base/net_log.h"
11#include "net/base/net_log_unittest.h"
[email protected]620f5712009-08-04 22:43:1212#include "net/base/test_completion_callback.h"
13#include "net/proxy/init_proxy_resolver.h"
14#include "net/proxy/proxy_config.h"
15#include "net/proxy/proxy_resolver.h"
16#include "net/proxy/proxy_script_fetcher.h"
17#include "testing/gtest/include/gtest/gtest.h"
18
19namespace net {
20namespace {
21
22enum Error {
23 kFailedDownloading = -100,
24 kFailedParsing = -200,
25};
26
27class Rules {
28 public:
29 struct Rule {
30 Rule(const GURL& url,
31 int fetch_error,
32 int set_pac_error)
33 : url(url),
34 fetch_error(fetch_error),
35 set_pac_error(set_pac_error) {
36 }
37
[email protected]9b9ae9552010-07-01 22:20:5038 string16 text() const {
[email protected]620f5712009-08-04 22:43:1239 if (set_pac_error == OK)
[email protected]9b9ae9552010-07-01 22:20:5040 return UTF8ToUTF16(url.spec() + "!valid-script");
[email protected]620f5712009-08-04 22:43:1241 if (fetch_error == OK)
[email protected]9b9ae9552010-07-01 22:20:5042 return UTF8ToUTF16(url.spec() + "!invalid-script");
43 return string16();
[email protected]620f5712009-08-04 22:43:1244 }
45
46 GURL url;
47 int fetch_error;
48 int set_pac_error;
49 };
50
51 Rule AddSuccessRule(const char* url) {
52 Rule rule(GURL(url), OK /*fetch_error*/, OK /*set_pac_error*/);
53 rules_.push_back(rule);
54 return rule;
55 }
56
57 void AddFailDownloadRule(const char* url) {
58 rules_.push_back(Rule(GURL(url), kFailedDownloading /*fetch_error*/,
59 ERR_UNEXPECTED /*set_pac_error*/));
60 }
61
62 void AddFailParsingRule(const char* url) {
63 rules_.push_back(Rule(GURL(url), OK /*fetch_error*/,
64 kFailedParsing /*set_pac_error*/));
65 }
66
67 const Rule& GetRuleByUrl(const GURL& url) const {
68 for (RuleList::const_iterator it = rules_.begin(); it != rules_.end();
69 ++it) {
70 if (it->url == url)
71 return *it;
72 }
[email protected]8e6b2c1c2010-05-05 00:43:2273 LOG(FATAL) << "Rule not found for " << url;
[email protected]620f5712009-08-04 22:43:1274 return rules_[0];
75 }
76
[email protected]9b9ae9552010-07-01 22:20:5077 const Rule& GetRuleByText(const string16& text) const {
[email protected]620f5712009-08-04 22:43:1278 for (RuleList::const_iterator it = rules_.begin(); it != rules_.end();
79 ++it) {
[email protected]9b9ae9552010-07-01 22:20:5080 if (it->text() == text)
[email protected]620f5712009-08-04 22:43:1281 return *it;
82 }
[email protected]9b9ae9552010-07-01 22:20:5083 LOG(FATAL) << "Rule not found for " << text;
[email protected]620f5712009-08-04 22:43:1284 return rules_[0];
85 }
86
87 private:
88 typedef std::vector<Rule> RuleList;
89 RuleList rules_;
90};
91
92class RuleBasedProxyScriptFetcher : public ProxyScriptFetcher {
93 public:
94 explicit RuleBasedProxyScriptFetcher(const Rules* rules) : rules_(rules) {}
95
96 // ProxyScriptFetcher implementation.
97 virtual int Fetch(const GURL& url,
[email protected]9b9ae9552010-07-01 22:20:5098 string16* text,
[email protected]620f5712009-08-04 22:43:1299 CompletionCallback* callback) {
100 const Rules::Rule& rule = rules_->GetRuleByUrl(url);
101 int rv = rule.fetch_error;
102 EXPECT_NE(ERR_UNEXPECTED, rv);
103 if (rv == OK)
[email protected]9b9ae9552010-07-01 22:20:50104 *text = rule.text();
[email protected]620f5712009-08-04 22:43:12105 return rv;
106 }
107
108 virtual void Cancel() {}
109
110 private:
111 const Rules* rules_;
112};
113
114class RuleBasedProxyResolver : public ProxyResolver {
115 public:
116 RuleBasedProxyResolver(const Rules* rules, bool expects_pac_bytes)
117 : ProxyResolver(expects_pac_bytes), rules_(rules) {}
118
119 // ProxyResolver implementation:
120 virtual int GetProxyForURL(const GURL& /*url*/,
121 ProxyInfo* /*results*/,
122 CompletionCallback* /*callback*/,
[email protected]52ae80c2009-09-10 21:27:00123 RequestHandle* /*request_handle*/,
[email protected]9e743cd2010-03-16 07:03:53124 const BoundNetLog& /*net_log*/) {
[email protected]620f5712009-08-04 22:43:12125 NOTREACHED();
126 return ERR_UNEXPECTED;
127 }
128
129 virtual void CancelRequest(RequestHandle request_handle) {
130 NOTREACHED();
131 }
132
[email protected]24476402010-07-20 20:55:17133 virtual int SetPacScript(
134 const scoped_refptr<ProxyResolverScriptData>& script_data,
135 CompletionCallback* callback) {
136
137 const GURL url =
138 script_data->type() == ProxyResolverScriptData::TYPE_SCRIPT_URL ?
139 script_data->url() : GURL();
140
[email protected]620f5712009-08-04 22:43:12141 const Rules::Rule& rule = expects_pac_bytes() ?
[email protected]24476402010-07-20 20:55:17142 rules_->GetRuleByText(script_data->utf16()) :
143 rules_->GetRuleByUrl(url);
[email protected]620f5712009-08-04 22:43:12144
145 int rv = rule.set_pac_error;
146 EXPECT_NE(ERR_UNEXPECTED, rv);
147
[email protected]24476402010-07-20 20:55:17148 if (expects_pac_bytes()) {
149 EXPECT_EQ(rule.text(), script_data->utf16());
150 } else {
151 EXPECT_EQ(rule.url, url);
[email protected]620f5712009-08-04 22:43:12152 }
[email protected]24476402010-07-20 20:55:17153
154 if (rv == OK)
155 script_data_ = script_data;
[email protected]620f5712009-08-04 22:43:12156 return rv;
157 }
158
[email protected]24476402010-07-20 20:55:17159 const ProxyResolverScriptData* script_data() const { return script_data_; }
[email protected]620f5712009-08-04 22:43:12160
161 private:
162 const Rules* rules_;
[email protected]24476402010-07-20 20:55:17163 scoped_refptr<ProxyResolverScriptData> script_data_;
[email protected]620f5712009-08-04 22:43:12164};
165
166// Succeed using custom PAC script.
167TEST(InitProxyResolverTest, CustomPacSucceeds) {
168 Rules rules;
169 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
170 RuleBasedProxyScriptFetcher fetcher(&rules);
171
172 ProxyConfig config;
[email protected]ed4ed0f2010-02-24 00:20:48173 config.set_pac_url(GURL("http://custom/proxy.pac"));
[email protected]620f5712009-08-04 22:43:12174
175 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");
176
177 TestCompletionCallback callback;
[email protected]5a1d7ca2010-04-28 20:12:27178 CapturingNetLog log(CapturingNetLog::kUnbounded);
179 InitProxyResolver init(&resolver, &fetcher, &log);
[email protected]d747ef32010-08-28 01:17:56180 EXPECT_EQ(OK, init.Init(config, base::TimeDelta(), NULL, &callback));
[email protected]24476402010-07-20 20:55:17181 EXPECT_EQ(rule.text(), resolver.script_data()->utf16());
[email protected]6bd32db12009-08-17 20:28:24182
[email protected]9e743cd2010-03-16 07:03:53183 // Check the NetLog was filled correctly.
[email protected]d13f51b2010-04-27 23:20:45184 EXPECT_EQ(6u, log.entries().size());
[email protected]e9002a92010-01-29 07:10:46185 EXPECT_TRUE(LogContainsBeginEvent(
[email protected]9e743cd2010-03-16 07:03:53186 log.entries(), 0, NetLog::TYPE_INIT_PROXY_RESOLVER));
[email protected]e9002a92010-01-29 07:10:46187 EXPECT_TRUE(LogContainsBeginEvent(
[email protected]9e743cd2010-03-16 07:03:53188 log.entries(), 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
[email protected]e9002a92010-01-29 07:10:46189 EXPECT_TRUE(LogContainsEndEvent(
[email protected]d13f51b2010-04-27 23:20:45190 log.entries(), 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
[email protected]e9002a92010-01-29 07:10:46191 EXPECT_TRUE(LogContainsBeginEvent(
[email protected]d13f51b2010-04-27 23:20:45192 log.entries(), 3, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
[email protected]e9002a92010-01-29 07:10:46193 EXPECT_TRUE(LogContainsEndEvent(
[email protected]d13f51b2010-04-27 23:20:45194 log.entries(), 4, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
[email protected]9e743cd2010-03-16 07:03:53195 EXPECT_TRUE(LogContainsEndEvent(
[email protected]d13f51b2010-04-27 23:20:45196 log.entries(), 5, NetLog::TYPE_INIT_PROXY_RESOLVER));
[email protected]620f5712009-08-04 22:43:12197}
198
199// Fail downloading the custom PAC script.
200TEST(InitProxyResolverTest, CustomPacFails1) {
201 Rules rules;
202 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
203 RuleBasedProxyScriptFetcher fetcher(&rules);
204
205 ProxyConfig config;
[email protected]ed4ed0f2010-02-24 00:20:48206 config.set_pac_url(GURL("http://custom/proxy.pac"));
[email protected]620f5712009-08-04 22:43:12207
208 rules.AddFailDownloadRule("http://custom/proxy.pac");
209
210 TestCompletionCallback callback;
[email protected]5a1d7ca2010-04-28 20:12:27211 CapturingNetLog log(CapturingNetLog::kUnbounded);
212 InitProxyResolver init(&resolver, &fetcher, &log);
[email protected]1b8740f2010-08-26 05:12:47213 EXPECT_EQ(kFailedDownloading,
[email protected]d747ef32010-08-28 01:17:56214 init.Init(config, base::TimeDelta(), NULL, &callback));
[email protected]24476402010-07-20 20:55:17215 EXPECT_EQ(NULL, resolver.script_data());
[email protected]6bd32db12009-08-17 20:28:24216
[email protected]9e743cd2010-03-16 07:03:53217 // Check the NetLog was filled correctly.
[email protected]d13f51b2010-04-27 23:20:45218 EXPECT_EQ(4u, log.entries().size());
[email protected]e9002a92010-01-29 07:10:46219 EXPECT_TRUE(LogContainsBeginEvent(
[email protected]9e743cd2010-03-16 07:03:53220 log.entries(), 0, NetLog::TYPE_INIT_PROXY_RESOLVER));
[email protected]e9002a92010-01-29 07:10:46221 EXPECT_TRUE(LogContainsBeginEvent(
[email protected]9e743cd2010-03-16 07:03:53222 log.entries(), 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
[email protected]e9002a92010-01-29 07:10:46223 EXPECT_TRUE(LogContainsEndEvent(
[email protected]d13f51b2010-04-27 23:20:45224 log.entries(), 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
[email protected]9e743cd2010-03-16 07:03:53225 EXPECT_TRUE(LogContainsEndEvent(
[email protected]d13f51b2010-04-27 23:20:45226 log.entries(), 3, NetLog::TYPE_INIT_PROXY_RESOLVER));
[email protected]620f5712009-08-04 22:43:12227}
228
229// Fail parsing the custom PAC script.
230TEST(InitProxyResolverTest, CustomPacFails2) {
231 Rules rules;
232 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
233 RuleBasedProxyScriptFetcher fetcher(&rules);
234
235 ProxyConfig config;
[email protected]ed4ed0f2010-02-24 00:20:48236 config.set_pac_url(GURL("http://custom/proxy.pac"));
[email protected]620f5712009-08-04 22:43:12237
238 rules.AddFailParsingRule("http://custom/proxy.pac");
239
240 TestCompletionCallback callback;
[email protected]5a1d7ca2010-04-28 20:12:27241 InitProxyResolver init(&resolver, &fetcher, NULL);
[email protected]d747ef32010-08-28 01:17:56242 EXPECT_EQ(kFailedParsing,
243 init.Init(config, base::TimeDelta(), NULL, &callback));
[email protected]24476402010-07-20 20:55:17244 EXPECT_EQ(NULL, resolver.script_data());
[email protected]620f5712009-08-04 22:43:12245}
246
[email protected]20d296ddc2009-11-18 23:07:08247// Fail downloading the custom PAC script, because the fetcher was NULL.
248TEST(InitProxyResolverTest, HasNullProxyScriptFetcher) {
249 Rules rules;
250 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
251
252 ProxyConfig config;
[email protected]ed4ed0f2010-02-24 00:20:48253 config.set_pac_url(GURL("http://custom/proxy.pac"));
[email protected]20d296ddc2009-11-18 23:07:08254
255 TestCompletionCallback callback;
[email protected]5a1d7ca2010-04-28 20:12:27256 InitProxyResolver init(&resolver, NULL, NULL);
[email protected]d747ef32010-08-28 01:17:56257 EXPECT_EQ(ERR_UNEXPECTED,
258 init.Init(config, base::TimeDelta(), NULL, &callback));
[email protected]24476402010-07-20 20:55:17259 EXPECT_EQ(NULL, resolver.script_data());
[email protected]20d296ddc2009-11-18 23:07:08260}
261
[email protected]620f5712009-08-04 22:43:12262// Succeeds in choosing autodetect (wpad).
263TEST(InitProxyResolverTest, AutodetectSuccess) {
264 Rules rules;
265 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
266 RuleBasedProxyScriptFetcher fetcher(&rules);
267
268 ProxyConfig config;
[email protected]ed4ed0f2010-02-24 00:20:48269 config.set_auto_detect(true);
[email protected]620f5712009-08-04 22:43:12270
271 Rules::Rule rule = rules.AddSuccessRule("http://wpad/wpad.dat");
272
273 TestCompletionCallback callback;
[email protected]5a1d7ca2010-04-28 20:12:27274 InitProxyResolver init(&resolver, &fetcher, NULL);
[email protected]d747ef32010-08-28 01:17:56275 EXPECT_EQ(OK, init.Init(config, base::TimeDelta(), NULL, &callback));
[email protected]24476402010-07-20 20:55:17276 EXPECT_EQ(rule.text(), resolver.script_data()->utf16());
[email protected]620f5712009-08-04 22:43:12277}
278
279// Fails at WPAD (downloading), but succeeds in choosing the custom PAC.
280TEST(InitProxyResolverTest, AutodetectFailCustomSuccess1) {
281 Rules rules;
282 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
283 RuleBasedProxyScriptFetcher fetcher(&rules);
284
285 ProxyConfig config;
[email protected]ed4ed0f2010-02-24 00:20:48286 config.set_auto_detect(true);
287 config.set_pac_url(GURL("http://custom/proxy.pac"));
[email protected]620f5712009-08-04 22:43:12288
289 rules.AddFailDownloadRule("http://wpad/wpad.dat");
290 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");
291
292 TestCompletionCallback callback;
[email protected]5a1d7ca2010-04-28 20:12:27293 InitProxyResolver init(&resolver, &fetcher, NULL);
[email protected]d747ef32010-08-28 01:17:56294 EXPECT_EQ(OK, init.Init(config, base::TimeDelta(), NULL, &callback));
[email protected]24476402010-07-20 20:55:17295 EXPECT_EQ(rule.text(), resolver.script_data()->utf16());
[email protected]620f5712009-08-04 22:43:12296}
297
298// Fails at WPAD (parsing), but succeeds in choosing the custom PAC.
299TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2) {
300 Rules rules;
301 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
302 RuleBasedProxyScriptFetcher fetcher(&rules);
303
304 ProxyConfig config;
[email protected]ed4ed0f2010-02-24 00:20:48305 config.set_auto_detect(true);
306 config.set_pac_url(GURL("http://custom/proxy.pac"));
[email protected]d747ef32010-08-28 01:17:56307 config.proxy_rules().ParseFromString("unused-manual-proxy:99");
[email protected]620f5712009-08-04 22:43:12308
309 rules.AddFailParsingRule("http://wpad/wpad.dat");
310 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");
311
312 TestCompletionCallback callback;
[email protected]5a1d7ca2010-04-28 20:12:27313 CapturingNetLog log(CapturingNetLog::kUnbounded);
[email protected]d747ef32010-08-28 01:17:56314
315 ProxyConfig effective_config;
[email protected]5a1d7ca2010-04-28 20:12:27316 InitProxyResolver init(&resolver, &fetcher, &log);
[email protected]d747ef32010-08-28 01:17:56317 EXPECT_EQ(OK, init.Init(config, base::TimeDelta(),
318 &effective_config, &callback));
[email protected]24476402010-07-20 20:55:17319 EXPECT_EQ(rule.text(), resolver.script_data()->utf16());
[email protected]6bd32db12009-08-17 20:28:24320
[email protected]d747ef32010-08-28 01:17:56321 // Verify that the effective configuration no longer contains auto detect or
322 // any of the manual settings.
323 EXPECT_TRUE(effective_config.Equals(
324 ProxyConfig::CreateFromCustomPacURL(GURL("http://custom/proxy.pac"))));
325
[email protected]9e743cd2010-03-16 07:03:53326 // Check the NetLog was filled correctly.
[email protected]6bd32db12009-08-17 20:28:24327 // (Note that the Fetch and Set states are repeated since both WPAD and custom
328 // PAC scripts are tried).
[email protected]d13f51b2010-04-27 23:20:45329 EXPECT_EQ(11u, log.entries().size());
[email protected]e9002a92010-01-29 07:10:46330 EXPECT_TRUE(LogContainsBeginEvent(
[email protected]9e743cd2010-03-16 07:03:53331 log.entries(), 0, NetLog::TYPE_INIT_PROXY_RESOLVER));
[email protected]e9002a92010-01-29 07:10:46332 EXPECT_TRUE(LogContainsBeginEvent(
[email protected]9e743cd2010-03-16 07:03:53333 log.entries(), 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
[email protected]e9002a92010-01-29 07:10:46334 EXPECT_TRUE(LogContainsEndEvent(
[email protected]d13f51b2010-04-27 23:20:45335 log.entries(), 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
[email protected]e9002a92010-01-29 07:10:46336 EXPECT_TRUE(LogContainsBeginEvent(
[email protected]d13f51b2010-04-27 23:20:45337 log.entries(), 3, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
[email protected]e9002a92010-01-29 07:10:46338 EXPECT_TRUE(LogContainsEndEvent(
[email protected]d13f51b2010-04-27 23:20:45339 log.entries(), 4, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
340 EXPECT_TRUE(LogContainsEvent(
341 log.entries(), 5,
342 NetLog::TYPE_INIT_PROXY_RESOLVER_FALLING_BACK_TO_NEXT_PAC_URL,
343 NetLog::PHASE_NONE));
[email protected]e9002a92010-01-29 07:10:46344 EXPECT_TRUE(LogContainsBeginEvent(
[email protected]d13f51b2010-04-27 23:20:45345 log.entries(), 6, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
[email protected]e9002a92010-01-29 07:10:46346 EXPECT_TRUE(LogContainsEndEvent(
[email protected]d13f51b2010-04-27 23:20:45347 log.entries(), 7, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
[email protected]e9002a92010-01-29 07:10:46348 EXPECT_TRUE(LogContainsBeginEvent(
[email protected]d13f51b2010-04-27 23:20:45349 log.entries(), 8, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
[email protected]e9002a92010-01-29 07:10:46350 EXPECT_TRUE(LogContainsEndEvent(
[email protected]d13f51b2010-04-27 23:20:45351 log.entries(), 9, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
[email protected]9e743cd2010-03-16 07:03:53352 EXPECT_TRUE(LogContainsEndEvent(
[email protected]d13f51b2010-04-27 23:20:45353 log.entries(), 10, NetLog::TYPE_INIT_PROXY_RESOLVER));
[email protected]620f5712009-08-04 22:43:12354}
355
356// Fails at WPAD (downloading), and fails at custom PAC (downloading).
357TEST(InitProxyResolverTest, AutodetectFailCustomFails1) {
358 Rules rules;
359 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
360 RuleBasedProxyScriptFetcher fetcher(&rules);
361
362 ProxyConfig config;
[email protected]ed4ed0f2010-02-24 00:20:48363 config.set_auto_detect(true);
364 config.set_pac_url(GURL("http://custom/proxy.pac"));
[email protected]620f5712009-08-04 22:43:12365
366 rules.AddFailDownloadRule("http://wpad/wpad.dat");
367 rules.AddFailDownloadRule("http://custom/proxy.pac");
368
369 TestCompletionCallback callback;
[email protected]5a1d7ca2010-04-28 20:12:27370 InitProxyResolver init(&resolver, &fetcher, NULL);
[email protected]1b8740f2010-08-26 05:12:47371 EXPECT_EQ(kFailedDownloading,
[email protected]d747ef32010-08-28 01:17:56372 init.Init(config, base::TimeDelta(), NULL, &callback));
[email protected]24476402010-07-20 20:55:17373 EXPECT_EQ(NULL, resolver.script_data());
[email protected]620f5712009-08-04 22:43:12374}
375
376// Fails at WPAD (downloading), and fails at custom PAC (parsing).
377TEST(InitProxyResolverTest, AutodetectFailCustomFails2) {
378 Rules rules;
379 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
380 RuleBasedProxyScriptFetcher fetcher(&rules);
381
382 ProxyConfig config;
[email protected]ed4ed0f2010-02-24 00:20:48383 config.set_auto_detect(true);
384 config.set_pac_url(GURL("http://custom/proxy.pac"));
[email protected]620f5712009-08-04 22:43:12385
386 rules.AddFailDownloadRule("http://wpad/wpad.dat");
387 rules.AddFailParsingRule("http://custom/proxy.pac");
388
389 TestCompletionCallback callback;
[email protected]5a1d7ca2010-04-28 20:12:27390 InitProxyResolver init(&resolver, &fetcher, NULL);
[email protected]d747ef32010-08-28 01:17:56391 EXPECT_EQ(kFailedParsing,
392 init.Init(config, base::TimeDelta(), NULL, &callback));
[email protected]24476402010-07-20 20:55:17393 EXPECT_EQ(NULL, resolver.script_data());
[email protected]620f5712009-08-04 22:43:12394}
395
396// Fails at WPAD (parsing), but succeeds in choosing the custom PAC.
397// This is the same as AutodetectFailCustomSuccess2, but using a ProxyResolver
398// that doesn't |expects_pac_bytes|.
399TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2_NoFetch) {
400 Rules rules;
401 RuleBasedProxyResolver resolver(&rules, false /*expects_pac_bytes*/);
402 RuleBasedProxyScriptFetcher fetcher(&rules);
403
404 ProxyConfig config;
[email protected]ed4ed0f2010-02-24 00:20:48405 config.set_auto_detect(true);
406 config.set_pac_url(GURL("http://custom/proxy.pac"));
[email protected]620f5712009-08-04 22:43:12407
408 rules.AddFailParsingRule(""); // Autodetect.
409 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");
410
411 TestCompletionCallback callback;
[email protected]5a1d7ca2010-04-28 20:12:27412 InitProxyResolver init(&resolver, &fetcher, NULL);
[email protected]d747ef32010-08-28 01:17:56413 EXPECT_EQ(OK, init.Init(config, base::TimeDelta(), NULL, &callback));
[email protected]24476402010-07-20 20:55:17414 EXPECT_EQ(rule.url, resolver.script_data()->url());
[email protected]620f5712009-08-04 22:43:12415}
416
[email protected]1b8740f2010-08-26 05:12:47417// This is a copy-paste of CustomPacFails1, with the exception that we give it
418// a 1 millisecond delay. This means it will now complete asynchronously.
419// Moreover, we test the NetLog to make sure it logged the pause.
420TEST(InitProxyResolverTest, CustomPacFails1_WithPositiveDelay) {
421 Rules rules;
422 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
423 RuleBasedProxyScriptFetcher fetcher(&rules);
424
425 ProxyConfig config;
426 config.set_pac_url(GURL("http://custom/proxy.pac"));
427
428 rules.AddFailDownloadRule("http://custom/proxy.pac");
429
430 TestCompletionCallback callback;
431 CapturingNetLog log(CapturingNetLog::kUnbounded);
432 InitProxyResolver init(&resolver, &fetcher, &log);
433 EXPECT_EQ(ERR_IO_PENDING,
434 init.Init(config, base::TimeDelta::FromMilliseconds(1),
[email protected]d747ef32010-08-28 01:17:56435 NULL, &callback));
[email protected]1b8740f2010-08-26 05:12:47436
437 EXPECT_EQ(kFailedDownloading, callback.WaitForResult());
438 EXPECT_EQ(NULL, resolver.script_data());
439
440 // Check the NetLog was filled correctly.
441 EXPECT_EQ(6u, log.entries().size());
442 EXPECT_TRUE(LogContainsBeginEvent(
443 log.entries(), 0, NetLog::TYPE_INIT_PROXY_RESOLVER));
444 EXPECT_TRUE(LogContainsBeginEvent(
445 log.entries(), 1, NetLog::TYPE_INIT_PROXY_RESOLVER_WAIT));
446 EXPECT_TRUE(LogContainsEndEvent(
447 log.entries(), 2, NetLog::TYPE_INIT_PROXY_RESOLVER_WAIT));
448 EXPECT_TRUE(LogContainsBeginEvent(
449 log.entries(), 3, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
450 EXPECT_TRUE(LogContainsEndEvent(
451 log.entries(), 4, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
452 EXPECT_TRUE(LogContainsEndEvent(
453 log.entries(), 5, NetLog::TYPE_INIT_PROXY_RESOLVER));
454}
455
456// This is a copy-paste of CustomPacFails1, with the exception that we give it
457// a -5 second delay instead of a 0 ms delay. This change should have no effect
458// so the rest of the test is unchanged.
459TEST(InitProxyResolverTest, CustomPacFails1_WithNegativeDelay) {
460 Rules rules;
461 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
462 RuleBasedProxyScriptFetcher fetcher(&rules);
463
464 ProxyConfig config;
465 config.set_pac_url(GURL("http://custom/proxy.pac"));
466
467 rules.AddFailDownloadRule("http://custom/proxy.pac");
468
469 TestCompletionCallback callback;
470 CapturingNetLog log(CapturingNetLog::kUnbounded);
471 InitProxyResolver init(&resolver, &fetcher, &log);
472 EXPECT_EQ(kFailedDownloading,
[email protected]d747ef32010-08-28 01:17:56473 init.Init(config, base::TimeDelta::FromSeconds(-5),
474 NULL, &callback));
[email protected]1b8740f2010-08-26 05:12:47475 EXPECT_EQ(NULL, resolver.script_data());
476
477 // Check the NetLog was filled correctly.
478 EXPECT_EQ(4u, log.entries().size());
479 EXPECT_TRUE(LogContainsBeginEvent(
480 log.entries(), 0, NetLog::TYPE_INIT_PROXY_RESOLVER));
481 EXPECT_TRUE(LogContainsBeginEvent(
482 log.entries(), 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
483 EXPECT_TRUE(LogContainsEndEvent(
484 log.entries(), 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
485 EXPECT_TRUE(LogContainsEndEvent(
486 log.entries(), 3, NetLog::TYPE_INIT_PROXY_RESOLVER));
487}
488
[email protected]620f5712009-08-04 22:43:12489} // namespace
490} // namespace net