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