| 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/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 #include "net/base/net_log.h" | 10 #include "net/base/net_log.h" |
| 11 #include "net/base/net_log_unittest.h" | 11 #include "net/base/net_log_unittest.h" |
| 12 #include "net/base/test_completion_callback.h" | 12 #include "net/base/test_completion_callback.h" |
| 13 #include "net/proxy/init_proxy_resolver.h" | 13 #include "net/proxy/init_proxy_resolver.h" |
| 14 #include "net/proxy/dhcp_proxy_script_fetcher.h" |
| 14 #include "net/proxy/proxy_config.h" | 15 #include "net/proxy/proxy_config.h" |
| 15 #include "net/proxy/proxy_resolver.h" | 16 #include "net/proxy/proxy_resolver.h" |
| 16 #include "net/proxy/proxy_script_fetcher.h" | 17 #include "net/proxy/proxy_script_fetcher.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace net { | 20 namespace net { |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 enum Error { | 23 enum Error { |
| 23 kFailedDownloading = -100, | 24 kFailedDownloading = -100, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 } | 83 } |
| 83 LOG(FATAL) << "Rule not found for " << text; | 84 LOG(FATAL) << "Rule not found for " << text; |
| 84 return rules_[0]; | 85 return rules_[0]; |
| 85 } | 86 } |
| 86 | 87 |
| 87 private: | 88 private: |
| 88 typedef std::vector<Rule> RuleList; | 89 typedef std::vector<Rule> RuleList; |
| 89 RuleList rules_; | 90 RuleList rules_; |
| 90 }; | 91 }; |
| 91 | 92 |
| 92 class RuleBasedProxyScriptFetcher : public ProxyScriptFetcher { | 93 class RuleBasedProxyScriptFetcher : public URLProxyScriptFetcher { |
| 93 public: | 94 public: |
| 94 explicit RuleBasedProxyScriptFetcher(const Rules* rules) : rules_(rules) {} | 95 explicit RuleBasedProxyScriptFetcher(const Rules* rules) : rules_(rules) {} |
| 95 | 96 |
| 96 // ProxyScriptFetcher implementation. | 97 // ProxyScriptFetcher implementation. |
| 97 virtual int Fetch(const GURL& url, | 98 virtual int Fetch(string16* text, |
| 98 string16* text, | |
| 99 CompletionCallback* callback) { | 99 CompletionCallback* callback) { |
| 100 const Rules::Rule& rule = rules_->GetRuleByUrl(url); | 100 const Rules::Rule& rule = rules_->GetRuleByUrl(url_); |
| 101 int rv = rule.fetch_error; | 101 int rv = rule.fetch_error; |
| 102 EXPECT_NE(ERR_UNEXPECTED, rv); | 102 EXPECT_NE(ERR_UNEXPECTED, rv); |
| 103 if (rv == OK) | 103 if (rv == OK) |
| 104 *text = rule.text(); | 104 *text = rule.text(); |
| 105 return rv; | 105 return rv; |
| 106 } | 106 } |
| 107 | 107 |
| 108 virtual void Cancel() {} | 108 virtual void Cancel() {} |
| 109 | 109 |
| 110 virtual URLRequestContext* GetRequestContext() { return NULL; } | 110 virtual URLRequestContext* GetRequestContext() const { return NULL; } |
| 111 |
| 112 virtual void SetURL(const GURL& url) { |
| 113 url_ = url; |
| 114 } |
| 111 | 115 |
| 112 private: | 116 private: |
| 113 const Rules* rules_; | 117 const Rules* rules_; |
| 118 GURL url_; |
| 114 }; | 119 }; |
| 115 | 120 |
| 116 class RuleBasedProxyResolver : public ProxyResolver { | 121 class RuleBasedProxyResolver : public ProxyResolver { |
| 117 public: | 122 public: |
| 118 RuleBasedProxyResolver(const Rules* rules, bool expects_pac_bytes) | 123 RuleBasedProxyResolver(const Rules* rules, bool expects_pac_bytes) |
| 119 : ProxyResolver(expects_pac_bytes), rules_(rules) {} | 124 : ProxyResolver(expects_pac_bytes), rules_(rules) {} |
| 120 | 125 |
| 121 // ProxyResolver implementation: | 126 // ProxyResolver implementation: |
| 122 virtual int GetProxyForURL(const GURL& /*url*/, | 127 virtual int GetProxyForURL(const GURL& /*url*/, |
| 123 ProxyInfo* /*results*/, | 128 ProxyInfo* /*results*/, |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 180 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 176 RuleBasedProxyScriptFetcher fetcher(&rules); | 181 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 177 | 182 |
| 178 ProxyConfig config; | 183 ProxyConfig config; |
| 179 config.set_pac_url(GURL("http://custom/proxy.pac")); | 184 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 180 | 185 |
| 181 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); | 186 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); |
| 182 | 187 |
| 183 TestCompletionCallback callback; | 188 TestCompletionCallback callback; |
| 184 CapturingNetLog log(CapturingNetLog::kUnbounded); | 189 CapturingNetLog log(CapturingNetLog::kUnbounded); |
| 190 ProxyConfig effective_config; |
| 185 InitProxyResolver init(&resolver, &fetcher, &log); | 191 InitProxyResolver init(&resolver, &fetcher, &log); |
| 186 EXPECT_EQ(OK, init.Init(config, base::TimeDelta(), NULL, &callback)); | 192 EXPECT_EQ(OK, init.Init( |
| 193 config, base::TimeDelta(), &effective_config, &callback)); |
| 187 EXPECT_EQ(rule.text(), resolver.script_data()->utf16()); | 194 EXPECT_EQ(rule.text(), resolver.script_data()->utf16()); |
| 188 | 195 |
| 189 // Check the NetLog was filled correctly. | 196 // Check the NetLog was filled correctly. |
| 190 CapturingNetLog::EntryList entries; | 197 CapturingNetLog::EntryList entries; |
| 191 log.GetEntries(&entries); | 198 log.GetEntries(&entries); |
| 192 | 199 |
| 193 EXPECT_EQ(6u, entries.size()); | 200 EXPECT_EQ(6u, entries.size()); |
| 194 EXPECT_TRUE(LogContainsBeginEvent( | 201 EXPECT_TRUE(LogContainsBeginEvent( |
| 195 entries, 0, NetLog::TYPE_INIT_PROXY_RESOLVER)); | 202 entries, 0, NetLog::TYPE_INIT_PROXY_RESOLVER)); |
| 196 EXPECT_TRUE(LogContainsBeginEvent( | 203 EXPECT_TRUE(LogContainsBeginEvent( |
| 197 entries, 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); | 204 entries, 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 198 EXPECT_TRUE(LogContainsEndEvent( | 205 EXPECT_TRUE(LogContainsEndEvent( |
| 199 entries, 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); | 206 entries, 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 200 EXPECT_TRUE(LogContainsBeginEvent( | 207 EXPECT_TRUE(LogContainsBeginEvent( |
| 201 entries, 3, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT)); | 208 entries, 3, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT)); |
| 202 EXPECT_TRUE(LogContainsEndEvent( | 209 EXPECT_TRUE(LogContainsEndEvent( |
| 203 entries, 4, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT)); | 210 entries, 4, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT)); |
| 204 EXPECT_TRUE(LogContainsEndEvent( | 211 EXPECT_TRUE(LogContainsEndEvent( |
| 205 entries, 5, NetLog::TYPE_INIT_PROXY_RESOLVER)); | 212 entries, 5, NetLog::TYPE_INIT_PROXY_RESOLVER)); |
| 213 |
| 214 EXPECT_TRUE(effective_config.has_pac_url()); |
| 215 EXPECT_EQ(config.pac_url(), effective_config.pac_url()); |
| 206 } | 216 } |
| 207 | 217 |
| 208 // Fail downloading the custom PAC script. | 218 // Fail downloading the custom PAC script. |
| 209 TEST(InitProxyResolverTest, CustomPacFails1) { | 219 TEST(InitProxyResolverTest, CustomPacFails1) { |
| 210 Rules rules; | 220 Rules rules; |
| 211 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 221 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 212 RuleBasedProxyScriptFetcher fetcher(&rules); | 222 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 213 | 223 |
| 214 ProxyConfig config; | 224 ProxyConfig config; |
| 215 config.set_pac_url(GURL("http://custom/proxy.pac")); | 225 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 216 | 226 |
| 217 rules.AddFailDownloadRule("http://custom/proxy.pac"); | 227 rules.AddFailDownloadRule("http://custom/proxy.pac"); |
| 218 | 228 |
| 219 TestCompletionCallback callback; | 229 TestCompletionCallback callback; |
| 220 CapturingNetLog log(CapturingNetLog::kUnbounded); | 230 CapturingNetLog log(CapturingNetLog::kUnbounded); |
| 231 ProxyConfig effective_config; |
| 221 InitProxyResolver init(&resolver, &fetcher, &log); | 232 InitProxyResolver init(&resolver, &fetcher, &log); |
| 222 EXPECT_EQ(kFailedDownloading, | 233 EXPECT_EQ(kFailedDownloading, |
| 223 init.Init(config, base::TimeDelta(), NULL, &callback)); | 234 init.Init(config, base::TimeDelta(), &effective_config, &callback)); |
| 224 EXPECT_EQ(NULL, resolver.script_data()); | 235 EXPECT_EQ(NULL, resolver.script_data()); |
| 225 | 236 |
| 226 // Check the NetLog was filled correctly. | 237 // Check the NetLog was filled correctly. |
| 227 CapturingNetLog::EntryList entries; | 238 CapturingNetLog::EntryList entries; |
| 228 log.GetEntries(&entries); | 239 log.GetEntries(&entries); |
| 229 | 240 |
| 230 EXPECT_EQ(4u, entries.size()); | 241 EXPECT_EQ(4u, entries.size()); |
| 231 EXPECT_TRUE(LogContainsBeginEvent( | 242 EXPECT_TRUE(LogContainsBeginEvent( |
| 232 entries, 0, NetLog::TYPE_INIT_PROXY_RESOLVER)); | 243 entries, 0, NetLog::TYPE_INIT_PROXY_RESOLVER)); |
| 233 EXPECT_TRUE(LogContainsBeginEvent( | 244 EXPECT_TRUE(LogContainsBeginEvent( |
| 234 entries, 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); | 245 entries, 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 235 EXPECT_TRUE(LogContainsEndEvent( | 246 EXPECT_TRUE(LogContainsEndEvent( |
| 236 entries, 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); | 247 entries, 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 237 EXPECT_TRUE(LogContainsEndEvent( | 248 EXPECT_TRUE(LogContainsEndEvent( |
| 238 entries, 3, NetLog::TYPE_INIT_PROXY_RESOLVER)); | 249 entries, 3, NetLog::TYPE_INIT_PROXY_RESOLVER)); |
| 250 |
| 251 EXPECT_FALSE(effective_config.has_pac_url()); |
| 239 } | 252 } |
| 240 | 253 |
| 241 // Fail parsing the custom PAC script. | 254 // Fail parsing the custom PAC script. |
| 242 TEST(InitProxyResolverTest, CustomPacFails2) { | 255 TEST(InitProxyResolverTest, CustomPacFails2) { |
| 243 Rules rules; | 256 Rules rules; |
| 244 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 257 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 245 RuleBasedProxyScriptFetcher fetcher(&rules); | 258 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 246 | 259 |
| 247 ProxyConfig config; | 260 ProxyConfig config; |
| 248 config.set_pac_url(GURL("http://custom/proxy.pac")); | 261 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 264 ProxyConfig config; | 277 ProxyConfig config; |
| 265 config.set_pac_url(GURL("http://custom/proxy.pac")); | 278 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 266 | 279 |
| 267 TestCompletionCallback callback; | 280 TestCompletionCallback callback; |
| 268 InitProxyResolver init(&resolver, NULL, NULL); | 281 InitProxyResolver init(&resolver, NULL, NULL); |
| 269 EXPECT_EQ(ERR_UNEXPECTED, | 282 EXPECT_EQ(ERR_UNEXPECTED, |
| 270 init.Init(config, base::TimeDelta(), NULL, &callback)); | 283 init.Init(config, base::TimeDelta(), NULL, &callback)); |
| 271 EXPECT_EQ(NULL, resolver.script_data()); | 284 EXPECT_EQ(NULL, resolver.script_data()); |
| 272 } | 285 } |
| 273 | 286 |
| 274 // Succeeds in choosing autodetect (wpad). | 287 class NoDhcpInitProxyResolver : public InitProxyResolver { |
| 288 public: |
| 289 NoDhcpInitProxyResolver(ProxyResolver* resolver, |
| 290 URLProxyScriptFetcher* proxy_script_fetcher, |
| 291 NetLog* net_log) |
| 292 : InitProxyResolver(resolver, proxy_script_fetcher, net_log) { |
| 293 } |
| 294 |
| 295 DhcpProxyScriptFetcher* ImplCreateDhcpProxyScriptFetcher( |
| 296 URLRequestContext* url_request_context) OVERRIDE { |
| 297 return new DoNothingDhcpProxyScriptFetcher(); |
| 298 } |
| 299 }; |
| 300 |
| 301 // Succeeds in choosing autodetect (WPAD DNS). |
| 275 TEST(InitProxyResolverTest, AutodetectSuccess) { | 302 TEST(InitProxyResolverTest, AutodetectSuccess) { |
| 276 Rules rules; | 303 Rules rules; |
| 277 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 304 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 278 RuleBasedProxyScriptFetcher fetcher(&rules); | 305 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 279 | 306 |
| 280 ProxyConfig config; | 307 ProxyConfig config; |
| 281 config.set_auto_detect(true); | 308 config.set_auto_detect(true); |
| 282 | 309 |
| 283 Rules::Rule rule = rules.AddSuccessRule("http://wpad/wpad.dat"); | 310 Rules::Rule rule = rules.AddSuccessRule("http://wpad/wpad.dat"); |
| 284 | 311 |
| 285 TestCompletionCallback callback; | 312 TestCompletionCallback callback; |
| 286 InitProxyResolver init(&resolver, &fetcher, NULL); | 313 ProxyConfig effective_config; |
| 287 EXPECT_EQ(OK, init.Init(config, base::TimeDelta(), NULL, &callback)); | 314 NoDhcpInitProxyResolver init(&resolver, &fetcher, NULL); |
| 315 EXPECT_EQ(OK, init.Init( |
| 316 config, base::TimeDelta(), &effective_config, &callback)); |
| 288 EXPECT_EQ(rule.text(), resolver.script_data()->utf16()); | 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()); |
| 289 } | 321 } |
| 290 | 322 |
| 291 // Fails at WPAD (downloading), but succeeds in choosing the custom PAC. | 323 // Fails at WPAD (downloading), but succeeds in choosing the custom PAC. |
| 292 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess1) { | 324 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess1) { |
| 293 Rules rules; | 325 Rules rules; |
| 294 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 326 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 295 RuleBasedProxyScriptFetcher fetcher(&rules); | 327 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 296 | 328 |
| 297 ProxyConfig config; | 329 ProxyConfig config; |
| 298 config.set_auto_detect(true); | 330 config.set_auto_detect(true); |
| 299 config.set_pac_url(GURL("http://custom/proxy.pac")); | 331 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 300 | 332 |
| 301 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 333 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 302 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); | 334 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); |
| 303 | 335 |
| 304 TestCompletionCallback callback; | 336 TestCompletionCallback callback; |
| 305 InitProxyResolver init(&resolver, &fetcher, NULL); | 337 ProxyConfig effective_config; |
| 306 EXPECT_EQ(OK, init.Init(config, base::TimeDelta(), NULL, &callback)); | 338 NoDhcpInitProxyResolver init(&resolver, &fetcher, NULL); |
| 339 EXPECT_EQ(OK, init.Init( |
| 340 config, base::TimeDelta(), &effective_config, &callback)); |
| 307 EXPECT_EQ(rule.text(), resolver.script_data()->utf16()); | 341 EXPECT_EQ(rule.text(), resolver.script_data()->utf16()); |
| 342 |
| 343 EXPECT_TRUE(effective_config.has_pac_url()); |
| 344 EXPECT_EQ(rule.url, effective_config.pac_url()); |
| 308 } | 345 } |
| 309 | 346 |
| 310 // Fails at WPAD (parsing), but succeeds in choosing the custom PAC. | 347 // Fails at WPAD (no DHCP config, DNS PAC fails parsing), but succeeds in |
| 348 // choosing the custom PAC. |
| 311 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2) { | 349 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2) { |
| 312 Rules rules; | 350 Rules rules; |
| 313 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 351 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 314 RuleBasedProxyScriptFetcher fetcher(&rules); | 352 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 315 | 353 |
| 316 ProxyConfig config; | 354 ProxyConfig config; |
| 317 config.set_auto_detect(true); | 355 config.set_auto_detect(true); |
| 318 config.set_pac_url(GURL("http://custom/proxy.pac")); | 356 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 319 config.proxy_rules().ParseFromString("unused-manual-proxy:99"); | 357 config.proxy_rules().ParseFromString("unused-manual-proxy:99"); |
| 320 | 358 |
| 321 rules.AddFailParsingRule("http://wpad/wpad.dat"); | 359 rules.AddFailParsingRule("http://wpad/wpad.dat"); |
| 322 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); | 360 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); |
| 323 | 361 |
| 324 TestCompletionCallback callback; | 362 TestCompletionCallback callback; |
| 325 CapturingNetLog log(CapturingNetLog::kUnbounded); | 363 CapturingNetLog log(CapturingNetLog::kUnbounded); |
| 326 | 364 |
| 327 ProxyConfig effective_config; | 365 ProxyConfig effective_config; |
| 328 InitProxyResolver init(&resolver, &fetcher, &log); | 366 NoDhcpInitProxyResolver init(&resolver, &fetcher, &log); |
| 329 EXPECT_EQ(OK, init.Init(config, base::TimeDelta(), | 367 EXPECT_EQ(OK, init.Init(config, base::TimeDelta(), |
| 330 &effective_config, &callback)); | 368 &effective_config, &callback)); |
| 331 EXPECT_EQ(rule.text(), resolver.script_data()->utf16()); | 369 EXPECT_EQ(rule.text(), resolver.script_data()->utf16()); |
| 332 | 370 |
| 333 // Verify that the effective configuration no longer contains auto detect or | 371 // Verify that the effective configuration no longer contains auto detect or |
| 334 // any of the manual settings. | 372 // any of the manual settings. |
| 335 EXPECT_TRUE(effective_config.Equals( | 373 EXPECT_TRUE(effective_config.Equals( |
| 336 ProxyConfig::CreateFromCustomPacURL(GURL("http://custom/proxy.pac")))); | 374 ProxyConfig::CreateFromCustomPacURL(GURL("http://custom/proxy.pac")))); |
| 337 | 375 |
| 338 // Check the NetLog was filled correctly. | 376 // Check the NetLog was filled correctly. |
| 339 // (Note that the Fetch and Set states are repeated since both WPAD and custom | 377 // (Note that various states are repeated since both WPAD and custom |
| 340 // PAC scripts are tried). | 378 // PAC scripts are tried). |
| 341 CapturingNetLog::EntryList entries; | 379 CapturingNetLog::EntryList entries; |
| 342 log.GetEntries(&entries); | 380 log.GetEntries(&entries); |
| 343 | 381 |
| 344 EXPECT_EQ(11u, entries.size()); | 382 EXPECT_EQ(14u, entries.size()); |
| 345 EXPECT_TRUE(LogContainsBeginEvent( | 383 EXPECT_TRUE(LogContainsBeginEvent( |
| 346 entries, 0, NetLog::TYPE_INIT_PROXY_RESOLVER)); | 384 entries, 0, NetLog::TYPE_INIT_PROXY_RESOLVER)); |
| 385 // This is the DHCP phase, which fails fetching rather than parsing, so |
| 386 // there is no pair of SET_PAC_SCRIPT events. |
| 347 EXPECT_TRUE(LogContainsBeginEvent( | 387 EXPECT_TRUE(LogContainsBeginEvent( |
| 348 entries, 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); | 388 entries, 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 349 EXPECT_TRUE(LogContainsEndEvent( | 389 EXPECT_TRUE(LogContainsEndEvent( |
| 350 entries, 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); | 390 entries, 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 391 EXPECT_TRUE(LogContainsEvent( |
| 392 entries, 3, |
| 393 NetLog::TYPE_INIT_PROXY_RESOLVER_FALLING_BACK_TO_NEXT_PAC_SOURCE, |
| 394 NetLog::PHASE_NONE)); |
| 395 // This is the DNS phase, which attempts a fetch but fails. |
| 351 EXPECT_TRUE(LogContainsBeginEvent( | 396 EXPECT_TRUE(LogContainsBeginEvent( |
| 352 entries, 3, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT)); | 397 entries, 4, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 353 EXPECT_TRUE(LogContainsEndEvent( | 398 EXPECT_TRUE(LogContainsEndEvent( |
| 354 entries, 4, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT)); | 399 entries, 5, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 400 EXPECT_TRUE(LogContainsBeginEvent( |
| 401 entries, 6, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT)); |
| 402 EXPECT_TRUE(LogContainsEndEvent( |
| 403 entries, 7, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT)); |
| 355 EXPECT_TRUE(LogContainsEvent( | 404 EXPECT_TRUE(LogContainsEvent( |
| 356 entries, 5, | 405 entries, 8, |
| 357 NetLog::TYPE_INIT_PROXY_RESOLVER_FALLING_BACK_TO_NEXT_PAC_URL, | 406 NetLog::TYPE_INIT_PROXY_RESOLVER_FALLING_BACK_TO_NEXT_PAC_SOURCE, |
| 358 NetLog::PHASE_NONE)); | 407 NetLog::PHASE_NONE)); |
| 408 // Finally, the custom PAC URL phase. |
| 359 EXPECT_TRUE(LogContainsBeginEvent( | 409 EXPECT_TRUE(LogContainsBeginEvent( |
| 360 entries, 6, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); | 410 entries, 9, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 361 EXPECT_TRUE(LogContainsEndEvent( | 411 EXPECT_TRUE(LogContainsEndEvent( |
| 362 entries, 7, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); | 412 entries, 10, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 363 EXPECT_TRUE(LogContainsBeginEvent( | 413 EXPECT_TRUE(LogContainsBeginEvent( |
| 364 entries, 8, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT)); | 414 entries, 11, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT)); |
| 365 EXPECT_TRUE(LogContainsEndEvent( | 415 EXPECT_TRUE(LogContainsEndEvent( |
| 366 entries, 9, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT)); | 416 entries, 12, NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT)); |
| 367 EXPECT_TRUE(LogContainsEndEvent( | 417 EXPECT_TRUE(LogContainsEndEvent( |
| 368 entries, 10, NetLog::TYPE_INIT_PROXY_RESOLVER)); | 418 entries, 13, NetLog::TYPE_INIT_PROXY_RESOLVER)); |
| 369 } | 419 } |
| 370 | 420 |
| 371 // Fails at WPAD (downloading), and fails at custom PAC (downloading). | 421 // Fails at WPAD (downloading), and fails at custom PAC (downloading). |
| 372 TEST(InitProxyResolverTest, AutodetectFailCustomFails1) { | 422 TEST(InitProxyResolverTest, AutodetectFailCustomFails1) { |
| 373 Rules rules; | 423 Rules rules; |
| 374 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 424 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 375 RuleBasedProxyScriptFetcher fetcher(&rules); | 425 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 376 | 426 |
| 377 ProxyConfig config; | 427 ProxyConfig config; |
| 378 config.set_auto_detect(true); | 428 config.set_auto_detect(true); |
| 379 config.set_pac_url(GURL("http://custom/proxy.pac")); | 429 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 380 | 430 |
| 381 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 431 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 382 rules.AddFailDownloadRule("http://custom/proxy.pac"); | 432 rules.AddFailDownloadRule("http://custom/proxy.pac"); |
| 383 | 433 |
| 384 TestCompletionCallback callback; | 434 TestCompletionCallback callback; |
| 385 InitProxyResolver init(&resolver, &fetcher, NULL); | 435 NoDhcpInitProxyResolver init(&resolver, &fetcher, NULL); |
| 386 EXPECT_EQ(kFailedDownloading, | 436 EXPECT_EQ(kFailedDownloading, |
| 387 init.Init(config, base::TimeDelta(), NULL, &callback)); | 437 init.Init(config, base::TimeDelta(), NULL, &callback)); |
| 388 EXPECT_EQ(NULL, resolver.script_data()); | 438 EXPECT_EQ(NULL, resolver.script_data()); |
| 389 } | 439 } |
| 390 | 440 |
| 391 // Fails at WPAD (downloading), and fails at custom PAC (parsing). | 441 // Fails at WPAD (downloading), and fails at custom PAC (parsing). |
| 392 TEST(InitProxyResolverTest, AutodetectFailCustomFails2) { | 442 TEST(InitProxyResolverTest, AutodetectFailCustomFails2) { |
| 393 Rules rules; | 443 Rules rules; |
| 394 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 444 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 395 RuleBasedProxyScriptFetcher fetcher(&rules); | 445 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 396 | 446 |
| 397 ProxyConfig config; | 447 ProxyConfig config; |
| 398 config.set_auto_detect(true); | 448 config.set_auto_detect(true); |
| 399 config.set_pac_url(GURL("http://custom/proxy.pac")); | 449 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 400 | 450 |
| 401 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 451 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 402 rules.AddFailParsingRule("http://custom/proxy.pac"); | 452 rules.AddFailParsingRule("http://custom/proxy.pac"); |
| 403 | 453 |
| 404 TestCompletionCallback callback; | 454 TestCompletionCallback callback; |
| 405 InitProxyResolver init(&resolver, &fetcher, NULL); | 455 NoDhcpInitProxyResolver init(&resolver, &fetcher, NULL); |
| 406 EXPECT_EQ(kFailedParsing, | 456 EXPECT_EQ(kFailedParsing, |
| 407 init.Init(config, base::TimeDelta(), NULL, &callback)); | 457 init.Init(config, base::TimeDelta(), NULL, &callback)); |
| 408 EXPECT_EQ(NULL, resolver.script_data()); | 458 EXPECT_EQ(NULL, resolver.script_data()); |
| 409 } | 459 } |
| 410 | 460 |
| 411 // Fails at WPAD (parsing), but succeeds in choosing the custom PAC. | 461 // Fails at WPAD (parsing), but succeeds in choosing the custom PAC. |
| 412 // This is the same as AutodetectFailCustomSuccess2, but using a ProxyResolver | 462 // This is the same as AutodetectFailCustomSuccess2, but using a ProxyResolver |
| 413 // that doesn't |expects_pac_bytes|. | 463 // that doesn't |expects_pac_bytes|. |
| 414 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2_NoFetch) { | 464 TEST(InitProxyResolverTest, AutodetectFailCustomSuccess2_NoFetch) { |
| 415 Rules rules; | 465 Rules rules; |
| 416 RuleBasedProxyResolver resolver(&rules, false /*expects_pac_bytes*/); | 466 RuleBasedProxyResolver resolver(&rules, false /*expects_pac_bytes*/); |
| 417 RuleBasedProxyScriptFetcher fetcher(&rules); | 467 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 418 | 468 |
| 419 ProxyConfig config; | 469 ProxyConfig config; |
| 420 config.set_auto_detect(true); | 470 config.set_auto_detect(true); |
| 421 config.set_pac_url(GURL("http://custom/proxy.pac")); | 471 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 422 | 472 |
| 423 rules.AddFailParsingRule(""); // Autodetect. | 473 rules.AddFailParsingRule(""); // Autodetect. |
| 424 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); | 474 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); |
| 425 | 475 |
| 426 TestCompletionCallback callback; | 476 TestCompletionCallback callback; |
| 427 InitProxyResolver init(&resolver, &fetcher, NULL); | 477 NoDhcpInitProxyResolver init(&resolver, &fetcher, NULL); |
| 428 EXPECT_EQ(OK, init.Init(config, base::TimeDelta(), NULL, &callback)); | 478 EXPECT_EQ(OK, init.Init(config, base::TimeDelta(), NULL, &callback)); |
| 429 EXPECT_EQ(rule.url, resolver.script_data()->url()); | 479 EXPECT_EQ(rule.url, resolver.script_data()->url()); |
| 430 } | 480 } |
| 431 | 481 |
| 432 // This is a copy-paste of CustomPacFails1, with the exception that we give it | 482 // This is a copy-paste of CustomPacFails1, with the exception that we give it |
| 433 // a 1 millisecond delay. This means it will now complete asynchronously. | 483 // a 1 millisecond delay. This means it will now complete asynchronously. |
| 434 // Moreover, we test the NetLog to make sure it logged the pause. | 484 // Moreover, we test the NetLog to make sure it logged the pause. |
| 435 TEST(InitProxyResolverTest, CustomPacFails1_WithPositiveDelay) { | 485 TEST(InitProxyResolverTest, CustomPacFails1_WithPositiveDelay) { |
| 436 Rules rules; | 486 Rules rules; |
| 437 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 487 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 438 RuleBasedProxyScriptFetcher fetcher(&rules); | 488 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 439 | 489 |
| 440 ProxyConfig config; | 490 ProxyConfig config; |
| 441 config.set_pac_url(GURL("http://custom/proxy.pac")); | 491 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 442 | 492 |
| 443 rules.AddFailDownloadRule("http://custom/proxy.pac"); | 493 rules.AddFailDownloadRule("http://custom/proxy.pac"); |
| 444 | 494 |
| 445 TestCompletionCallback callback; | 495 TestCompletionCallback callback; |
| 446 CapturingNetLog log(CapturingNetLog::kUnbounded); | 496 CapturingNetLog log(CapturingNetLog::kUnbounded); |
| 447 InitProxyResolver init(&resolver, &fetcher, &log); | 497 NoDhcpInitProxyResolver init(&resolver, &fetcher, &log); |
| 448 EXPECT_EQ(ERR_IO_PENDING, | 498 EXPECT_EQ(ERR_IO_PENDING, |
| 449 init.Init(config, base::TimeDelta::FromMilliseconds(1), | 499 init.Init(config, base::TimeDelta::FromMilliseconds(1), |
| 450 NULL, &callback)); | 500 NULL, &callback)); |
| 451 | 501 |
| 452 EXPECT_EQ(kFailedDownloading, callback.WaitForResult()); | 502 EXPECT_EQ(kFailedDownloading, callback.WaitForResult()); |
| 453 EXPECT_EQ(NULL, resolver.script_data()); | 503 EXPECT_EQ(NULL, resolver.script_data()); |
| 454 | 504 |
| 455 // Check the NetLog was filled correctly. | 505 // Check the NetLog was filled correctly. |
| 456 CapturingNetLog::EntryList entries; | 506 CapturingNetLog::EntryList entries; |
| 457 log.GetEntries(&entries); | 507 log.GetEntries(&entries); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 479 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); | 529 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 480 RuleBasedProxyScriptFetcher fetcher(&rules); | 530 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 481 | 531 |
| 482 ProxyConfig config; | 532 ProxyConfig config; |
| 483 config.set_pac_url(GURL("http://custom/proxy.pac")); | 533 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 484 | 534 |
| 485 rules.AddFailDownloadRule("http://custom/proxy.pac"); | 535 rules.AddFailDownloadRule("http://custom/proxy.pac"); |
| 486 | 536 |
| 487 TestCompletionCallback callback; | 537 TestCompletionCallback callback; |
| 488 CapturingNetLog log(CapturingNetLog::kUnbounded); | 538 CapturingNetLog log(CapturingNetLog::kUnbounded); |
| 489 InitProxyResolver init(&resolver, &fetcher, &log); | 539 NoDhcpInitProxyResolver init(&resolver, &fetcher, &log); |
| 490 EXPECT_EQ(kFailedDownloading, | 540 EXPECT_EQ(kFailedDownloading, |
| 491 init.Init(config, base::TimeDelta::FromSeconds(-5), | 541 init.Init(config, base::TimeDelta::FromSeconds(-5), |
| 492 NULL, &callback)); | 542 NULL, &callback)); |
| 493 EXPECT_EQ(NULL, resolver.script_data()); | 543 EXPECT_EQ(NULL, resolver.script_data()); |
| 494 | 544 |
| 495 // Check the NetLog was filled correctly. | 545 // Check the NetLog was filled correctly. |
| 496 CapturingNetLog::EntryList entries; | 546 CapturingNetLog::EntryList entries; |
| 497 log.GetEntries(&entries); | 547 log.GetEntries(&entries); |
| 498 | 548 |
| 499 EXPECT_EQ(4u, entries.size()); | 549 EXPECT_EQ(4u, entries.size()); |
| 500 EXPECT_TRUE(LogContainsBeginEvent( | 550 EXPECT_TRUE(LogContainsBeginEvent( |
| 501 entries, 0, NetLog::TYPE_INIT_PROXY_RESOLVER)); | 551 entries, 0, NetLog::TYPE_INIT_PROXY_RESOLVER)); |
| 502 EXPECT_TRUE(LogContainsBeginEvent( | 552 EXPECT_TRUE(LogContainsBeginEvent( |
| 503 entries, 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); | 553 entries, 1, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 504 EXPECT_TRUE(LogContainsEndEvent( | 554 EXPECT_TRUE(LogContainsEndEvent( |
| 505 entries, 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); | 555 entries, 2, NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT)); |
| 506 EXPECT_TRUE(LogContainsEndEvent( | 556 EXPECT_TRUE(LogContainsEndEvent( |
| 507 entries, 3, NetLog::TYPE_INIT_PROXY_RESOLVER)); | 557 entries, 3, NetLog::TYPE_INIT_PROXY_RESOLVER)); |
| 508 } | 558 } |
| 509 | 559 |
| 560 class SynchronousSuccessProxyScriptFetcher : public DhcpProxyScriptFetcher { |
| 561 public: |
| 562 int Fetch(string16* utf16_text, CompletionCallback* callback) OVERRIDE { |
| 563 *utf16_text = expected_text_; |
| 564 return OK; |
| 565 } |
| 566 |
| 567 void Cancel() OVERRIDE { |
| 568 } |
| 569 |
| 570 URLRequestContext* GetRequestContext() const OVERRIDE { |
| 571 return NULL; |
| 572 } |
| 573 |
| 574 GURL GetPacURL() const OVERRIDE { |
| 575 return GURL("http://dhcppac/"); |
| 576 } |
| 577 |
| 578 // Set this before your test. |
| 579 static string16 expected_text_; |
| 580 }; |
| 581 |
| 582 string16 SynchronousSuccessProxyScriptFetcher::expected_text_; |
| 583 |
| 584 // Returns a DHCP fetcher that returns success immediately. |
| 585 class SynchronousDhcpInitProxyResolver : public InitProxyResolver { |
| 586 public: |
| 587 SynchronousDhcpInitProxyResolver(ProxyResolver* resolver, |
| 588 URLProxyScriptFetcher* proxy_script_fetcher, |
| 589 NetLog* net_log) |
| 590 : InitProxyResolver(resolver, proxy_script_fetcher, net_log) { |
| 591 } |
| 592 |
| 593 DhcpProxyScriptFetcher* ImplCreateDhcpProxyScriptFetcher( |
| 594 URLRequestContext* url_request_context) OVERRIDE { |
| 595 return new SynchronousSuccessProxyScriptFetcher(); |
| 596 } |
| 597 }; |
| 598 |
| 599 // All of the tests above that use NoDhcpInitProxyResolver have tested |
| 600 // failure to fetch a PAC file via DHCP configuration, so we now test |
| 601 // success at downloading and parsing, and then success at downloading, |
| 602 // failure at parsing. |
| 603 |
| 604 TEST(InitProxyResolverTest, AutodetectDhcpSuccess) { |
| 605 Rules rules; |
| 606 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 607 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 608 |
| 609 ProxyConfig config; |
| 610 config.set_auto_detect(true); |
| 611 |
| 612 rules.AddSuccessRule("http://bingo/"); |
| 613 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 614 SynchronousSuccessProxyScriptFetcher::expected_text_ = |
| 615 WideToUTF16(L"http://bingo/!valid-script"); |
| 616 |
| 617 TestCompletionCallback callback; |
| 618 ProxyConfig effective_config; |
| 619 SynchronousDhcpInitProxyResolver init(&resolver, &fetcher, NULL); |
| 620 EXPECT_EQ(OK, init.Init( |
| 621 config, base::TimeDelta(), &effective_config, &callback)); |
| 622 EXPECT_EQ(SynchronousSuccessProxyScriptFetcher::expected_text_, |
| 623 resolver.script_data()->utf16()); |
| 624 |
| 625 EXPECT_TRUE(effective_config.has_pac_url()); |
| 626 EXPECT_EQ(GURL("http://dhcppac/"), effective_config.pac_url()); |
| 627 } |
| 628 |
| 629 TEST(InitProxyResolverTest, AutodetectDhcpFailParse) { |
| 630 Rules rules; |
| 631 RuleBasedProxyResolver resolver(&rules, true /*expects_pac_bytes*/); |
| 632 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 633 |
| 634 ProxyConfig config; |
| 635 config.set_auto_detect(true); |
| 636 |
| 637 rules.AddFailParsingRule("http://bingo/"); |
| 638 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 639 SynchronousSuccessProxyScriptFetcher::expected_text_ = |
| 640 WideToUTF16(L"http://bingo/!invalid-script"); |
| 641 |
| 642 TestCompletionCallback callback; |
| 643 ProxyConfig effective_config; |
| 644 SynchronousDhcpInitProxyResolver init(&resolver, &fetcher, NULL); |
| 645 // Since there is fallback to DNS-based WPAD, the final error will be that |
| 646 // it failed downloading, not that it failed parsing. |
| 647 EXPECT_EQ(kFailedDownloading, |
| 648 init.Init(config, base::TimeDelta(), &effective_config, &callback)); |
| 649 EXPECT_EQ(NULL, resolver.script_data()); |
| 650 |
| 651 EXPECT_FALSE(effective_config.has_pac_url()); |
| 652 } |
| 653 |
| 510 } // namespace | 654 } // namespace |
| 511 } // namespace net | 655 } // namespace net |
| OLD | NEW |