| Index: net/proxy/init_proxy_resolver_unittest.cc
|
| diff --git a/net/proxy/init_proxy_resolver_unittest.cc b/net/proxy/init_proxy_resolver_unittest.cc
|
| index b0d416d33777f482e3e18b86e2570d7bd41da3e7..c8944d11ee67305abb02c6c34a254653dd4da75b 100644
|
| --- a/net/proxy/init_proxy_resolver_unittest.cc
|
| +++ b/net/proxy/init_proxy_resolver_unittest.cc
|
| @@ -11,6 +11,7 @@
|
| #include "net/base/net_log_unittest.h"
|
| #include "net/base/test_completion_callback.h"
|
| #include "net/proxy/init_proxy_resolver.h"
|
| +#include "net/proxy/dhcp_proxy_script_fetcher.h"
|
| #include "net/proxy/proxy_config.h"
|
| #include "net/proxy/proxy_resolver.h"
|
| #include "net/proxy/proxy_script_fetcher.h"
|
| @@ -89,15 +90,14 @@ class Rules {
|
| RuleList rules_;
|
| };
|
|
|
| -class RuleBasedProxyScriptFetcher : public ProxyScriptFetcher {
|
| +class RuleBasedProxyScriptFetcher : public URLProxyScriptFetcher {
|
| public:
|
| explicit RuleBasedProxyScriptFetcher(const Rules* rules) : rules_(rules) {}
|
|
|
| // ProxyScriptFetcher implementation.
|
| - virtual int Fetch(const GURL& url,
|
| - string16* text,
|
| + virtual int Fetch(string16* text,
|
| CompletionCallback* callback) {
|
| - const Rules::Rule& rule = rules_->GetRuleByUrl(url);
|
| + const Rules::Rule& rule = rules_->GetRuleByUrl(url_);
|
| int rv = rule.fetch_error;
|
| EXPECT_NE(ERR_UNEXPECTED, rv);
|
| if (rv == OK)
|
| @@ -107,10 +107,15 @@ class RuleBasedProxyScriptFetcher : public ProxyScriptFetcher {
|
|
|
| virtual void Cancel() {}
|
|
|
| - virtual URLRequestContext* GetRequestContext() { return NULL; }
|
| + virtual URLRequestContext* GetRequestContext() const { return NULL; }
|
| +
|
| + virtual void SetURL(const GURL& url) {
|
| + url_ = url;
|
| + }
|
|
|
| private:
|
| const Rules* rules_;
|
| + GURL url_;
|
| };
|
|
|
| class RuleBasedProxyResolver : public ProxyResolver {
|
| @@ -182,8 +187,10 @@ TEST(InitProxyResolverTest, CustomPacSucceeds) {
|
|
|
| TestCompletionCallback callback;
|
| CapturingNetLog log(CapturingNetLog::kUnbounded);
|
| + ProxyConfig effective_config;
|
| InitProxyResolver init(&resolver, &fetcher, &log);
|
| - EXPECT_EQ(OK, init.Init(config, base::TimeDelta(), NULL, &callback));
|
| + EXPECT_EQ(OK, init.Init(
|
| + config, base::TimeDelta(), &effective_config, &callback));
|
| EXPECT_EQ(rule.text(), resolver.script_data()->utf16());
|
|
|
| // Check the NetLog was filled correctly.
|
| @@ -203,6 +210,9 @@ TEST(InitProxyResolverTest, CustomPacSucceeds) {
|
| entries, 4, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
|
| EXPECT_TRUE(LogContainsEndEvent(
|
| entries, 5, NetLog::TYPE_INIT_PROXY_RESOLVER));
|
| +
|
| + EXPECT_TRUE(effective_config.has_pac_url());
|
| + EXPECT_EQ(config.pac_url(), effective_config.pac_url());
|
| }
|
|
|
| // Fail downloading the custom PAC script.
|
| @@ -218,9 +228,10 @@ TEST(InitProxyResolverTest, CustomPacFails1) {
|
|
|
| TestCompletionCallback callback;
|
| CapturingNetLog log(CapturingNetLog::kUnbounded);
|
| + ProxyConfig effective_config;
|
| InitProxyResolver init(&resolver, &fetcher, &log);
|
| EXPECT_EQ(kFailedDownloading,
|
| - init.Init(config, base::TimeDelta(), NULL, &callback));
|
| + init.Init(config, base::TimeDelta(), &effective_config, &callback));
|
| EXPECT_EQ(NULL, resolver.script_data());
|
|
|
| // Check the NetLog was filled correctly.
|
| @@ -236,6 +247,8 @@ TEST(InitProxyResolverTest, CustomPacFails1) {
|
| entries, 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
|
| EXPECT_TRUE(LogContainsEndEvent(
|
| entries, 3, NetLog::TYPE_INIT_PROXY_RESOLVER));
|
| +
|
| + EXPECT_FALSE(effective_config.has_pac_url());
|
| }
|
|
|
| // Fail parsing the custom PAC script.
|
| @@ -271,7 +284,21 @@ TEST(InitProxyResolverTest, HasNullProxyScriptFetcher) {
|
| EXPECT_EQ(NULL, resolver.script_data());
|
| }
|
|
|
| -// Succeeds in choosing autodetect (wpad).
|
| +class NoDhcpInitProxyResolver : public InitProxyResolver {
|
| + public:
|
| + NoDhcpInitProxyResolver(ProxyResolver* resolver,
|
| + URLProxyScriptFetcher* proxy_script_fetcher,
|
| + NetLog* net_log)
|
| + : InitProxyResolver(resolver, proxy_script_fetcher, net_log) {
|
| + }
|
| +
|
| + DhcpProxyScriptFetcher* ImplCreateDhcpProxyScriptFetcher(
|
| + URLRequestContext* url_request_context) OVERRIDE {
|
| + return new DoNothingDhcpProxyScriptFetcher();
|
| + }
|
| +};
|
| +
|
| +// Succeeds in choosing autodetect (WPAD DNS).
|
| TEST(InitProxyResolverTest, AutodetectSuccess) {
|
| Rules rules;
|
| RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
|
| @@ -283,9 +310,14 @@ TEST(InitProxyResolverTest, AutodetectSuccess) {
|
| Rules::Rule rule = rules.AddSuccessRule("http://wpad/wpad.dat");
|
|
|
| TestCompletionCallback callback;
|
| - InitProxyResolver init(&resolver, &fetcher, NULL);
|
| - EXPECT_EQ(OK, init.Init(config, base::TimeDelta(), NULL, &callback));
|
| + ProxyConfig effective_config;
|
| + NoDhcpInitProxyResolver init(&resolver, &fetcher, NULL);
|
| + EXPECT_EQ(OK, init.Init(
|
| + config, base::TimeDelta(), &effective_config, &callback));
|
| EXPECT_EQ(rule.text(), resolver.script_data()->utf16());
|
| +
|
| + EXPECT_TRUE(effective_config.has_pac_url());
|
| + EXPECT_EQ(rule.url, effective_config.pac_url());
|
| }
|
|
|
| // Fails at WPAD (downloading), but succeeds in choosing the custom PAC.
|
| @@ -302,12 +334,18 @@ TEST(InitProxyResolverTest, AutodetectFailCustomSuccess1) {
|
| Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");
|
|
|
| TestCompletionCallback callback;
|
| - InitProxyResolver init(&resolver, &fetcher, NULL);
|
| - EXPECT_EQ(OK, init.Init(config, base::TimeDelta(), NULL, &callback));
|
| + ProxyConfig effective_config;
|
| + NoDhcpInitProxyResolver init(&resolver, &fetcher, NULL);
|
| + EXPECT_EQ(OK, init.Init(
|
| + config, base::TimeDelta(), &effective_config, &callback));
|
| EXPECT_EQ(rule.text(), resolver.script_data()->utf16());
|
| +
|
| + EXPECT_TRUE(effective_config.has_pac_url());
|
| + EXPECT_EQ(rule.url, effective_config.pac_url());
|
| }
|
|
|
| -// Fails at WPAD (parsing), but succeeds in choosing the custom PAC.
|
| +// Fails at WPAD (no DHCP config, DNS PAC fails parsing), but succeeds in
|
| +// choosing the custom PAC.
|
| TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2) {
|
| Rules rules;
|
| RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
|
| @@ -325,7 +363,7 @@ TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2) {
|
| CapturingNetLog log(CapturingNetLog::kUnbounded);
|
|
|
| ProxyConfig effective_config;
|
| - InitProxyResolver init(&resolver, &fetcher, &log);
|
| + NoDhcpInitProxyResolver init(&resolver, &fetcher, &log);
|
| EXPECT_EQ(OK, init.Init(config, base::TimeDelta(),
|
| &effective_config, &callback));
|
| EXPECT_EQ(rule.text(), resolver.script_data()->utf16());
|
| @@ -336,36 +374,48 @@ TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2) {
|
| ProxyConfig::CreateFromCustomPacURL(GURL("http://custom/proxy.pac"))));
|
|
|
| // Check the NetLog was filled correctly.
|
| - // (Note that the Fetch and Set states are repeated since both WPAD and custom
|
| + // (Note that various states are repeated since both WPAD and custom
|
| // PAC scripts are tried).
|
| CapturingNetLog::EntryList entries;
|
| log.GetEntries(&entries);
|
|
|
| - EXPECT_EQ(11u, entries.size());
|
| + EXPECT_EQ(14u, entries.size());
|
| EXPECT_TRUE(LogContainsBeginEvent(
|
| entries, 0, NetLog::TYPE_INIT_PROXY_RESOLVER));
|
| + // This is the DHCP phase, which fails fetching rather than parsing, so
|
| + // there is no pair of SET_PAC_SCRIPT events.
|
| EXPECT_TRUE(LogContainsBeginEvent(
|
| entries, 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
|
| EXPECT_TRUE(LogContainsEndEvent(
|
| entries, 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
|
| + EXPECT_TRUE(LogContainsEvent(
|
| + entries, 3,
|
| + NetLog::TYPE_INIT_PROXY_RESOLVER_FALLING_BACK_TO_NEXT_PAC_SOURCE,
|
| + NetLog::PHASE_NONE));
|
| + // This is the DNS phase, which attempts a fetch but fails.
|
| EXPECT_TRUE(LogContainsBeginEvent(
|
| - entries, 3, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
|
| + entries, 4, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
|
| EXPECT_TRUE(LogContainsEndEvent(
|
| - entries, 4, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
|
| + entries, 5, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
|
| + EXPECT_TRUE(LogContainsBeginEvent(
|
| + entries, 6, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
|
| + EXPECT_TRUE(LogContainsEndEvent(
|
| + entries, 7, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
|
| EXPECT_TRUE(LogContainsEvent(
|
| - entries, 5,
|
| - NetLog::TYPE_INIT_PROXY_RESOLVER_FALLING_BACK_TO_NEXT_PAC_URL,
|
| + entries, 8,
|
| + NetLog::TYPE_INIT_PROXY_RESOLVER_FALLING_BACK_TO_NEXT_PAC_SOURCE,
|
| NetLog::PHASE_NONE));
|
| + // Finally, the custom PAC URL phase.
|
| EXPECT_TRUE(LogContainsBeginEvent(
|
| - entries, 6, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
|
| + entries, 9, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
|
| EXPECT_TRUE(LogContainsEndEvent(
|
| - entries, 7, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
|
| + entries, 10, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT));
|
| EXPECT_TRUE(LogContainsBeginEvent(
|
| - entries, 8, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
|
| + entries, 11, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
|
| EXPECT_TRUE(LogContainsEndEvent(
|
| - entries, 9, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
|
| + entries, 12, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT));
|
| EXPECT_TRUE(LogContainsEndEvent(
|
| - entries, 10, NetLog::TYPE_INIT_PROXY_RESOLVER));
|
| + entries, 13, NetLog::TYPE_INIT_PROXY_RESOLVER));
|
| }
|
|
|
| // Fails at WPAD (downloading), and fails at custom PAC (downloading).
|
| @@ -382,7 +432,7 @@ TEST(InitProxyResolverTest, AutodetectFailCustomFails1) {
|
| rules.AddFailDownloadRule("http://custom/proxy.pac");
|
|
|
| TestCompletionCallback callback;
|
| - InitProxyResolver init(&resolver, &fetcher, NULL);
|
| + NoDhcpInitProxyResolver init(&resolver, &fetcher, NULL);
|
| EXPECT_EQ(kFailedDownloading,
|
| init.Init(config, base::TimeDelta(), NULL, &callback));
|
| EXPECT_EQ(NULL, resolver.script_data());
|
| @@ -402,7 +452,7 @@ TEST(InitProxyResolverTest, AutodetectFailCustomFails2) {
|
| rules.AddFailParsingRule("http://custom/proxy.pac");
|
|
|
| TestCompletionCallback callback;
|
| - InitProxyResolver init(&resolver, &fetcher, NULL);
|
| + NoDhcpInitProxyResolver init(&resolver, &fetcher, NULL);
|
| EXPECT_EQ(kFailedParsing,
|
| init.Init(config, base::TimeDelta(), NULL, &callback));
|
| EXPECT_EQ(NULL, resolver.script_data());
|
| @@ -424,7 +474,7 @@ TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2_NoFetch) {
|
| Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac");
|
|
|
| TestCompletionCallback callback;
|
| - InitProxyResolver init(&resolver, &fetcher, NULL);
|
| + NoDhcpInitProxyResolver init(&resolver, &fetcher, NULL);
|
| EXPECT_EQ(OK, init.Init(config, base::TimeDelta(), NULL, &callback));
|
| EXPECT_EQ(rule.url, resolver.script_data()->url());
|
| }
|
| @@ -444,7 +494,7 @@ TEST(InitProxyResolverTest, CustomPacFails1_WithPositiveDelay) {
|
|
|
| TestCompletionCallback callback;
|
| CapturingNetLog log(CapturingNetLog::kUnbounded);
|
| - InitProxyResolver init(&resolver, &fetcher, &log);
|
| + NoDhcpInitProxyResolver init(&resolver, &fetcher, &log);
|
| EXPECT_EQ(ERR_IO_PENDING,
|
| init.Init(config, base::TimeDelta::FromMilliseconds(1),
|
| NULL, &callback));
|
| @@ -486,7 +536,7 @@ TEST(InitProxyResolverTest, CustomPacFails1_WithNegativeDelay) {
|
|
|
| TestCompletionCallback callback;
|
| CapturingNetLog log(CapturingNetLog::kUnbounded);
|
| - InitProxyResolver init(&resolver, &fetcher, &log);
|
| + NoDhcpInitProxyResolver init(&resolver, &fetcher, &log);
|
| EXPECT_EQ(kFailedDownloading,
|
| init.Init(config, base::TimeDelta::FromSeconds(-5),
|
| NULL, &callback));
|
| @@ -507,5 +557,99 @@ TEST(InitProxyResolverTest, CustomPacFails1_WithNegativeDelay) {
|
| entries, 3, NetLog::TYPE_INIT_PROXY_RESOLVER));
|
| }
|
|
|
| +class SynchronousSuccessProxyScriptFetcher : public DhcpProxyScriptFetcher {
|
| +public:
|
| + int Fetch(string16* utf16_text, CompletionCallback* callback) OVERRIDE {
|
| + *utf16_text = expected_text_;
|
| + return OK;
|
| + }
|
| +
|
| + void Cancel() OVERRIDE {
|
| + }
|
| +
|
| + URLRequestContext* GetRequestContext() const OVERRIDE {
|
| + return NULL;
|
| + }
|
| +
|
| + GURL GetPacURL() const OVERRIDE {
|
| + return GURL("http://dhcppac/");
|
| + }
|
| +
|
| + // Set this before your test.
|
| + static string16 expected_text_;
|
| +};
|
| +
|
| +string16 SynchronousSuccessProxyScriptFetcher::expected_text_;
|
| +
|
| +// Returns a DHCP fetcher that returns success immediately.
|
| +class SynchronousDhcpInitProxyResolver : public InitProxyResolver {
|
| + public:
|
| + SynchronousDhcpInitProxyResolver(ProxyResolver* resolver,
|
| + URLProxyScriptFetcher* proxy_script_fetcher,
|
| + NetLog* net_log)
|
| + : InitProxyResolver(resolver, proxy_script_fetcher, net_log) {
|
| + }
|
| +
|
| + DhcpProxyScriptFetcher* ImplCreateDhcpProxyScriptFetcher(
|
| + URLRequestContext* url_request_context) OVERRIDE {
|
| + return new SynchronousSuccessProxyScriptFetcher();
|
| + }
|
| +};
|
| +
|
| +// All of the tests above that use NoDhcpInitProxyResolver have tested
|
| +// failure to fetch a PAC file via DHCP configuration, so we now test
|
| +// success at downloading and parsing, and then success at downloading,
|
| +// failure at parsing.
|
| +
|
| +TEST(InitProxyResolverTest, AutodetectDhcpSuccess) {
|
| + Rules rules;
|
| + RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
|
| + RuleBasedProxyScriptFetcher fetcher(&rules);
|
| +
|
| + ProxyConfig config;
|
| + config.set_auto_detect(true);
|
| +
|
| + rules.AddSuccessRule("http://bingo/");
|
| + rules.AddFailDownloadRule("http://wpad/wpad.dat");
|
| + SynchronousSuccessProxyScriptFetcher::expected_text_ =
|
| + WideToUTF16(L"http://bingo/!valid-script");
|
| +
|
| + TestCompletionCallback callback;
|
| + ProxyConfig effective_config;
|
| + SynchronousDhcpInitProxyResolver init(&resolver, &fetcher, NULL);
|
| + EXPECT_EQ(OK, init.Init(
|
| + config, base::TimeDelta(), &effective_config, &callback));
|
| + EXPECT_EQ(SynchronousSuccessProxyScriptFetcher::expected_text_,
|
| + resolver.script_data()->utf16());
|
| +
|
| + EXPECT_TRUE(effective_config.has_pac_url());
|
| + EXPECT_EQ(GURL("http://dhcppac/"), effective_config.pac_url());
|
| +}
|
| +
|
| +TEST(InitProxyResolverTest, AutodetectDhcpFailParse) {
|
| + Rules rules;
|
| + RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/);
|
| + RuleBasedProxyScriptFetcher fetcher(&rules);
|
| +
|
| + ProxyConfig config;
|
| + config.set_auto_detect(true);
|
| +
|
| + rules.AddFailParsingRule("http://bingo/");
|
| + rules.AddFailDownloadRule("http://wpad/wpad.dat");
|
| + SynchronousSuccessProxyScriptFetcher::expected_text_ =
|
| + WideToUTF16(L"http://bingo/!invalid-script");
|
| +
|
| + TestCompletionCallback callback;
|
| + ProxyConfig effective_config;
|
| + SynchronousDhcpInitProxyResolver init(&resolver, &fetcher, NULL);
|
| + // Since there is fallback to DNS-based WPAD, the final error will be that
|
| + // it failed downloading, not that it failed parsing.
|
| + EXPECT_EQ(kFailedDownloading,
|
| + init.Init(config, base::TimeDelta(), &effective_config, &callback));
|
| + EXPECT_EQ(NULL, resolver.script_data());
|
| +
|
| + EXPECT_FALSE(effective_config.has_pac_url());
|
| +}
|
| +
|
| } // namespace
|
| } // namespace net
|
|
|