| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 kFailedDownloading = -100, | 32 kFailedDownloading = -100, |
| 33 kFailedParsing = ERR_PAC_SCRIPT_FAILED, | 33 kFailedParsing = ERR_PAC_SCRIPT_FAILED, |
| 34 }; | 34 }; |
| 35 | 35 |
| 36 class Rules { | 36 class Rules { |
| 37 public: | 37 public: |
| 38 struct Rule { | 38 struct Rule { |
| 39 Rule(const GURL& url, int fetch_error, bool is_valid_script) | 39 Rule(const GURL& url, int fetch_error, bool is_valid_script) |
| 40 : url(url), | 40 : url(url), |
| 41 fetch_error(fetch_error), | 41 fetch_error(fetch_error), |
| 42 is_valid_script(is_valid_script) { | 42 is_valid_script(is_valid_script) {} |
| 43 } | |
| 44 | 43 |
| 45 base::string16 text() const { | 44 base::string16 text() const { |
| 46 if (is_valid_script) | 45 if (is_valid_script) |
| 47 return base::UTF8ToUTF16(url.spec() + "!FindProxyForURL"); | 46 return base::UTF8ToUTF16(url.spec() + "!FindProxyForURL"); |
| 48 if (fetch_error == OK) | 47 if (fetch_error == OK) |
| 49 return base::UTF8ToUTF16(url.spec() + "!invalid-script"); | 48 return base::UTF8ToUTF16(url.spec() + "!invalid-script"); |
| 50 return base::string16(); | 49 return base::string16(); |
| 51 } | 50 } |
| 52 | 51 |
| 53 GURL url; | 52 GURL url; |
| 54 int fetch_error; | 53 int fetch_error; |
| 55 bool is_valid_script; | 54 bool is_valid_script; |
| 56 }; | 55 }; |
| 57 | 56 |
| 58 Rule AddSuccessRule(const char* url) { | 57 Rule AddSuccessRule(const char* url) { |
| 59 Rule rule(GURL(url), OK /*fetch_error*/, true); | 58 Rule rule(GURL(url), OK /*fetch_error*/, true); |
| 60 rules_.push_back(rule); | 59 rules_.push_back(rule); |
| 61 return rule; | 60 return rule; |
| 62 } | 61 } |
| 63 | 62 |
| 64 void AddFailDownloadRule(const char* url) { | 63 void AddFailDownloadRule(const char* url) { |
| 65 rules_.push_back(Rule(GURL(url), kFailedDownloading /*fetch_error*/, | 64 rules_.push_back( |
| 66 false)); | 65 Rule(GURL(url), kFailedDownloading /*fetch_error*/, false)); |
| 67 } | 66 } |
| 68 | 67 |
| 69 void AddFailParsingRule(const char* url) { | 68 void AddFailParsingRule(const char* url) { |
| 70 rules_.push_back(Rule(GURL(url), OK /*fetch_error*/, false)); | 69 rules_.push_back(Rule(GURL(url), OK /*fetch_error*/, false)); |
| 71 } | 70 } |
| 72 | 71 |
| 73 const Rule& GetRuleByUrl(const GURL& url) const { | 72 const Rule& GetRuleByUrl(const GURL& url) const { |
| 74 for (RuleList::const_iterator it = rules_.begin(); it != rules_.end(); | 73 for (RuleList::const_iterator it = rules_.begin(); it != rules_.end(); |
| 75 ++it) { | 74 ++it) { |
| 76 if (it->url == url) | 75 if (it->url == url) |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 | 141 |
| 143 virtual void CompleteRequests(int result, const base::string16& script); | 142 virtual void CompleteRequests(int result, const base::string16& script); |
| 144 | 143 |
| 145 private: | 144 private: |
| 146 CompletionCallback callback_; | 145 CompletionCallback callback_; |
| 147 base::string16* utf16_text_; | 146 base::string16* utf16_text_; |
| 148 GURL gurl_; | 147 GURL gurl_; |
| 149 DISALLOW_COPY_AND_ASSIGN(MockDhcpProxyScriptFetcher); | 148 DISALLOW_COPY_AND_ASSIGN(MockDhcpProxyScriptFetcher); |
| 150 }; | 149 }; |
| 151 | 150 |
| 152 MockDhcpProxyScriptFetcher::MockDhcpProxyScriptFetcher() { } | 151 MockDhcpProxyScriptFetcher::MockDhcpProxyScriptFetcher() { |
| 152 } |
| 153 | 153 |
| 154 MockDhcpProxyScriptFetcher::~MockDhcpProxyScriptFetcher() { } | 154 MockDhcpProxyScriptFetcher::~MockDhcpProxyScriptFetcher() { |
| 155 } |
| 155 | 156 |
| 156 int MockDhcpProxyScriptFetcher::Fetch(base::string16* utf16_text, | 157 int MockDhcpProxyScriptFetcher::Fetch(base::string16* utf16_text, |
| 157 const CompletionCallback& callback) { | 158 const CompletionCallback& callback) { |
| 158 utf16_text_ = utf16_text; | 159 utf16_text_ = utf16_text; |
| 159 callback_ = callback; | 160 callback_ = callback; |
| 160 return ERR_IO_PENDING; | 161 return ERR_IO_PENDING; |
| 161 } | 162 } |
| 162 | 163 |
| 163 void MockDhcpProxyScriptFetcher::Cancel() { } | 164 void MockDhcpProxyScriptFetcher::Cancel() { |
| 165 } |
| 164 | 166 |
| 165 const GURL& MockDhcpProxyScriptFetcher::GetPacURL() const { | 167 const GURL& MockDhcpProxyScriptFetcher::GetPacURL() const { |
| 166 return gurl_; | 168 return gurl_; |
| 167 } | 169 } |
| 168 | 170 |
| 169 void MockDhcpProxyScriptFetcher::SetPacURL(const GURL& url) { | 171 void MockDhcpProxyScriptFetcher::SetPacURL(const GURL& url) { |
| 170 gurl_ = url; | 172 gurl_ = url; |
| 171 } | 173 } |
| 172 | 174 |
| 173 void MockDhcpProxyScriptFetcher::CompleteRequests( | 175 void MockDhcpProxyScriptFetcher::CompleteRequests( |
| 174 int result, const base::string16& script) { | 176 int result, |
| 177 const base::string16& script) { |
| 175 *utf16_text_ = script; | 178 *utf16_text_ = script; |
| 176 callback_.Run(result); | 179 callback_.Run(result); |
| 177 } | 180 } |
| 178 | 181 |
| 179 // Succeed using custom PAC script. | 182 // Succeed using custom PAC script. |
| 180 TEST(ProxyScriptDeciderTest, CustomPacSucceeds) { | 183 TEST(ProxyScriptDeciderTest, CustomPacSucceeds) { |
| 181 Rules rules; | 184 Rules rules; |
| 182 RuleBasedProxyScriptFetcher fetcher(&rules); | 185 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 183 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 186 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 184 | 187 |
| 185 ProxyConfig config; | 188 ProxyConfig config; |
| 186 config.set_pac_url(GURL("http://custom/proxy.pac")); | 189 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 187 | 190 |
| 188 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); | 191 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); |
| 189 | 192 |
| 190 TestCompletionCallback callback; | 193 TestCompletionCallback callback; |
| 191 CapturingNetLog log; | 194 CapturingNetLog log; |
| 192 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); | 195 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); |
| 193 EXPECT_EQ(OK, decider.Start( | 196 EXPECT_EQ( |
| 194 config, base::TimeDelta(), true, callback.callback())); | 197 OK, decider.Start(config, base::TimeDelta(), true, callback.callback())); |
| 195 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); | 198 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); |
| 196 | 199 |
| 197 // Check the NetLog was filled correctly. | 200 // Check the NetLog was filled correctly. |
| 198 CapturingNetLog::CapturedEntryList entries; | 201 CapturingNetLog::CapturedEntryList entries; |
| 199 log.GetEntries(&entries); | 202 log.GetEntries(&entries); |
| 200 | 203 |
| 201 EXPECT_EQ(4u, entries.size()); | 204 EXPECT_EQ(4u, entries.size()); |
| 202 EXPECT_TRUE(LogContainsBeginEvent( | 205 EXPECT_TRUE( |
| 203 entries, 0, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); | 206 LogContainsBeginEvent(entries, 0, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); |
| 204 EXPECT_TRUE(LogContainsBeginEvent( | 207 EXPECT_TRUE(LogContainsBeginEvent( |
| 205 entries, 1, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); | 208 entries, 1, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); |
| 206 EXPECT_TRUE(LogContainsEndEvent( | 209 EXPECT_TRUE(LogContainsEndEvent( |
| 207 entries, 2, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); | 210 entries, 2, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); |
| 208 EXPECT_TRUE(LogContainsEndEvent( | 211 EXPECT_TRUE( |
| 209 entries, 3, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); | 212 LogContainsEndEvent(entries, 3, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); |
| 210 | 213 |
| 211 EXPECT_TRUE(decider.effective_config().has_pac_url()); | 214 EXPECT_TRUE(decider.effective_config().has_pac_url()); |
| 212 EXPECT_EQ(config.pac_url(), decider.effective_config().pac_url()); | 215 EXPECT_EQ(config.pac_url(), decider.effective_config().pac_url()); |
| 213 } | 216 } |
| 214 | 217 |
| 215 // Fail downloading the custom PAC script. | 218 // Fail downloading the custom PAC script. |
| 216 TEST(ProxyScriptDeciderTest, CustomPacFails1) { | 219 TEST(ProxyScriptDeciderTest, CustomPacFails1) { |
| 217 Rules rules; | 220 Rules rules; |
| 218 RuleBasedProxyScriptFetcher fetcher(&rules); | 221 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 219 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 222 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 220 | 223 |
| 221 ProxyConfig config; | 224 ProxyConfig config; |
| 222 config.set_pac_url(GURL("http://custom/proxy.pac")); | 225 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 223 | 226 |
| 224 rules.AddFailDownloadRule("http://custom/proxy.pac"); | 227 rules.AddFailDownloadRule("http://custom/proxy.pac"); |
| 225 | 228 |
| 226 TestCompletionCallback callback; | 229 TestCompletionCallback callback; |
| 227 CapturingNetLog log; | 230 CapturingNetLog log; |
| 228 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); | 231 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); |
| 229 EXPECT_EQ(kFailedDownloading, | 232 EXPECT_EQ( |
| 230 decider.Start(config, base::TimeDelta(), true, | 233 kFailedDownloading, |
| 231 callback.callback())); | 234 decider.Start(config, base::TimeDelta(), true, callback.callback())); |
| 232 EXPECT_EQ(NULL, decider.script_data()); | 235 EXPECT_EQ(NULL, decider.script_data()); |
| 233 | 236 |
| 234 // Check the NetLog was filled correctly. | 237 // Check the NetLog was filled correctly. |
| 235 CapturingNetLog::CapturedEntryList entries; | 238 CapturingNetLog::CapturedEntryList entries; |
| 236 log.GetEntries(&entries); | 239 log.GetEntries(&entries); |
| 237 | 240 |
| 238 EXPECT_EQ(4u, entries.size()); | 241 EXPECT_EQ(4u, entries.size()); |
| 239 EXPECT_TRUE(LogContainsBeginEvent( | 242 EXPECT_TRUE( |
| 240 entries, 0, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); | 243 LogContainsBeginEvent(entries, 0, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); |
| 241 EXPECT_TRUE(LogContainsBeginEvent( | 244 EXPECT_TRUE(LogContainsBeginEvent( |
| 242 entries, 1, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); | 245 entries, 1, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); |
| 243 EXPECT_TRUE(LogContainsEndEvent( | 246 EXPECT_TRUE(LogContainsEndEvent( |
| 244 entries, 2, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); | 247 entries, 2, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); |
| 245 EXPECT_TRUE(LogContainsEndEvent( | 248 EXPECT_TRUE( |
| 246 entries, 3, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); | 249 LogContainsEndEvent(entries, 3, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); |
| 247 | 250 |
| 248 EXPECT_FALSE(decider.effective_config().has_pac_url()); | 251 EXPECT_FALSE(decider.effective_config().has_pac_url()); |
| 249 } | 252 } |
| 250 | 253 |
| 251 // Fail parsing the custom PAC script. | 254 // Fail parsing the custom PAC script. |
| 252 TEST(ProxyScriptDeciderTest, CustomPacFails2) { | 255 TEST(ProxyScriptDeciderTest, CustomPacFails2) { |
| 253 Rules rules; | 256 Rules rules; |
| 254 RuleBasedProxyScriptFetcher fetcher(&rules); | 257 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 255 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 258 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 256 | 259 |
| 257 ProxyConfig config; | 260 ProxyConfig config; |
| 258 config.set_pac_url(GURL("http://custom/proxy.pac")); | 261 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 259 | 262 |
| 260 rules.AddFailParsingRule("http://custom/proxy.pac"); | 263 rules.AddFailParsingRule("http://custom/proxy.pac"); |
| 261 | 264 |
| 262 TestCompletionCallback callback; | 265 TestCompletionCallback callback; |
| 263 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); | 266 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); |
| 264 EXPECT_EQ(kFailedParsing, | 267 EXPECT_EQ( |
| 265 decider.Start(config, base::TimeDelta(), true, | 268 kFailedParsing, |
| 266 callback.callback())); | 269 decider.Start(config, base::TimeDelta(), true, callback.callback())); |
| 267 EXPECT_EQ(NULL, decider.script_data()); | 270 EXPECT_EQ(NULL, decider.script_data()); |
| 268 } | 271 } |
| 269 | 272 |
| 270 // Fail downloading the custom PAC script, because the fetcher was NULL. | 273 // Fail downloading the custom PAC script, because the fetcher was NULL. |
| 271 TEST(ProxyScriptDeciderTest, HasNullProxyScriptFetcher) { | 274 TEST(ProxyScriptDeciderTest, HasNullProxyScriptFetcher) { |
| 272 Rules rules; | 275 Rules rules; |
| 273 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 276 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 274 | 277 |
| 275 ProxyConfig config; | 278 ProxyConfig config; |
| 276 config.set_pac_url(GURL("http://custom/proxy.pac")); | 279 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 277 | 280 |
| 278 TestCompletionCallback callback; | 281 TestCompletionCallback callback; |
| 279 ProxyScriptDecider decider(NULL, &dhcp_fetcher, NULL); | 282 ProxyScriptDecider decider(NULL, &dhcp_fetcher, NULL); |
| 280 EXPECT_EQ(ERR_UNEXPECTED, | 283 EXPECT_EQ( |
| 281 decider.Start(config, base::TimeDelta(), true, | 284 ERR_UNEXPECTED, |
| 282 callback.callback())); | 285 decider.Start(config, base::TimeDelta(), true, callback.callback())); |
| 283 EXPECT_EQ(NULL, decider.script_data()); | 286 EXPECT_EQ(NULL, decider.script_data()); |
| 284 } | 287 } |
| 285 | 288 |
| 286 // Succeeds in choosing autodetect (WPAD DNS). | 289 // Succeeds in choosing autodetect (WPAD DNS). |
| 287 TEST(ProxyScriptDeciderTest, AutodetectSuccess) { | 290 TEST(ProxyScriptDeciderTest, AutodetectSuccess) { |
| 288 Rules rules; | 291 Rules rules; |
| 289 RuleBasedProxyScriptFetcher fetcher(&rules); | 292 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 290 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 293 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 291 | 294 |
| 292 ProxyConfig config; | 295 ProxyConfig config; |
| 293 config.set_auto_detect(true); | 296 config.set_auto_detect(true); |
| 294 | 297 |
| 295 Rules::Rule rule = rules.AddSuccessRule("http://wpad/wpad.dat"); | 298 Rules::Rule rule = rules.AddSuccessRule("http://wpad/wpad.dat"); |
| 296 | 299 |
| 297 TestCompletionCallback callback; | 300 TestCompletionCallback callback; |
| 298 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); | 301 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); |
| 299 EXPECT_EQ(OK, decider.Start( | 302 EXPECT_EQ( |
| 300 config, base::TimeDelta(), true, callback.callback())); | 303 OK, decider.Start(config, base::TimeDelta(), true, callback.callback())); |
| 301 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); | 304 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); |
| 302 | 305 |
| 303 EXPECT_TRUE(decider.effective_config().has_pac_url()); | 306 EXPECT_TRUE(decider.effective_config().has_pac_url()); |
| 304 EXPECT_EQ(rule.url, decider.effective_config().pac_url()); | 307 EXPECT_EQ(rule.url, decider.effective_config().pac_url()); |
| 305 } | 308 } |
| 306 | 309 |
| 307 class ProxyScriptDeciderQuickCheckTest : public ::testing::Test { | 310 class ProxyScriptDeciderQuickCheckTest : public ::testing::Test { |
| 308 public: | 311 public: |
| 309 ProxyScriptDeciderQuickCheckTest() | 312 ProxyScriptDeciderQuickCheckTest() |
| 310 : rule_(rules_.AddSuccessRule("http://wpad/wpad.dat")), | 313 : rule_(rules_.AddSuccessRule("http://wpad/wpad.dat")), |
| 311 fetcher_(&rules_) { } | 314 fetcher_(&rules_) {} |
| 312 | 315 |
| 313 virtual void SetUp() OVERRIDE { | 316 virtual void SetUp() OVERRIDE { |
| 314 request_context_.set_host_resolver(&resolver_); | 317 request_context_.set_host_resolver(&resolver_); |
| 315 fetcher_.SetRequestContext(&request_context_); | 318 fetcher_.SetRequestContext(&request_context_); |
| 316 config_.set_auto_detect(true); | 319 config_.set_auto_detect(true); |
| 317 decider_.reset(new ProxyScriptDecider(&fetcher_, &dhcp_fetcher_, NULL)); | 320 decider_.reset(new ProxyScriptDecider(&fetcher_, &dhcp_fetcher_, NULL)); |
| 318 } | 321 } |
| 319 | 322 |
| 320 int StartDecider() { | 323 int StartDecider() { |
| 321 return decider_->Start(config_, base::TimeDelta(), true, | 324 return decider_->Start( |
| 322 callback_.callback()); | 325 config_, base::TimeDelta(), true, callback_.callback()); |
| 323 } | 326 } |
| 324 | 327 |
| 325 protected: | 328 protected: |
| 326 scoped_ptr<ProxyScriptDecider> decider_; | 329 scoped_ptr<ProxyScriptDecider> decider_; |
| 327 MockHostResolver resolver_; | 330 MockHostResolver resolver_; |
| 328 Rules rules_; | 331 Rules rules_; |
| 329 Rules::Rule rule_; | 332 Rules::Rule rule_; |
| 330 TestCompletionCallback callback_; | 333 TestCompletionCallback callback_; |
| 331 RuleBasedProxyScriptFetcher fetcher_; | 334 RuleBasedProxyScriptFetcher fetcher_; |
| 332 ProxyConfig config_; | 335 ProxyConfig config_; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); | 386 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); |
| 384 ASSERT_TRUE(resolver_.has_pending_requests()); | 387 ASSERT_TRUE(resolver_.has_pending_requests()); |
| 385 callback_.WaitForResult(); | 388 callback_.WaitForResult(); |
| 386 EXPECT_FALSE(resolver_.has_pending_requests()); | 389 EXPECT_FALSE(resolver_.has_pending_requests()); |
| 387 EXPECT_FALSE(decider_->effective_config().has_pac_url()); | 390 EXPECT_FALSE(decider_->effective_config().has_pac_url()); |
| 388 } | 391 } |
| 389 | 392 |
| 390 // Fails if DHCP check doesn't take place before QuickCheck. | 393 // Fails if DHCP check doesn't take place before QuickCheck. |
| 391 TEST_F(ProxyScriptDeciderQuickCheckTest, QuickCheckInhibitsDhcp) { | 394 TEST_F(ProxyScriptDeciderQuickCheckTest, QuickCheckInhibitsDhcp) { |
| 392 MockDhcpProxyScriptFetcher dhcp_fetcher; | 395 MockDhcpProxyScriptFetcher dhcp_fetcher; |
| 393 const char *kPac = "function FindProxyForURL(u,h) { return \"DIRECT\"; }"; | 396 const char* kPac = "function FindProxyForURL(u,h) { return \"DIRECT\"; }"; |
| 394 base::string16 pac_contents = base::UTF8ToUTF16(kPac); | 397 base::string16 pac_contents = base::UTF8ToUTF16(kPac); |
| 395 GURL url("http://foobar/baz"); | 398 GURL url("http://foobar/baz"); |
| 396 dhcp_fetcher.SetPacURL(url); | 399 dhcp_fetcher.SetPacURL(url); |
| 397 decider_.reset(new ProxyScriptDecider(&fetcher_, &dhcp_fetcher, NULL)); | 400 decider_.reset(new ProxyScriptDecider(&fetcher_, &dhcp_fetcher, NULL)); |
| 398 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); | 401 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); |
| 399 dhcp_fetcher.CompleteRequests(OK, pac_contents); | 402 dhcp_fetcher.CompleteRequests(OK, pac_contents); |
| 400 EXPECT_TRUE(decider_->effective_config().has_pac_url()); | 403 EXPECT_TRUE(decider_->effective_config().has_pac_url()); |
| 401 EXPECT_EQ(decider_->effective_config().pac_url(), url); | 404 EXPECT_EQ(decider_->effective_config().pac_url(), url); |
| 402 } | 405 } |
| 403 | 406 |
| 404 // Fails if QuickCheck still happens when disabled. To ensure QuickCheck is not | 407 // Fails if QuickCheck still happens when disabled. To ensure QuickCheck is not |
| 405 // happening, we add a synchronous failing resolver, which would ordinarily | 408 // happening, we add a synchronous failing resolver, which would ordinarily |
| 406 // mean a QuickCheck failure, then ensure that our ProxyScriptFetcher is still | 409 // mean a QuickCheck failure, then ensure that our ProxyScriptFetcher is still |
| 407 // asked to fetch. | 410 // asked to fetch. |
| 408 TEST_F(ProxyScriptDeciderQuickCheckTest, QuickCheckDisabled) { | 411 TEST_F(ProxyScriptDeciderQuickCheckTest, QuickCheckDisabled) { |
| 409 const char *kPac = "function FindProxyForURL(u,h) { return \"DIRECT\"; }"; | 412 const char* kPac = "function FindProxyForURL(u,h) { return \"DIRECT\"; }"; |
| 410 resolver_.set_synchronous_mode(true); | 413 resolver_.set_synchronous_mode(true); |
| 411 resolver_.rules()->AddSimulatedFailure("wpad"); | 414 resolver_.rules()->AddSimulatedFailure("wpad"); |
| 412 MockProxyScriptFetcher fetcher; | 415 MockProxyScriptFetcher fetcher; |
| 413 decider_.reset(new ProxyScriptDecider(&fetcher, &dhcp_fetcher_, NULL)); | 416 decider_.reset(new ProxyScriptDecider(&fetcher, &dhcp_fetcher_, NULL)); |
| 414 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); | 417 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); |
| 415 EXPECT_TRUE(fetcher.has_pending_request()); | 418 EXPECT_TRUE(fetcher.has_pending_request()); |
| 416 fetcher.NotifyFetchCompletion(OK, kPac); | 419 fetcher.NotifyFetchCompletion(OK, kPac); |
| 417 } | 420 } |
| 418 | 421 |
| 419 TEST_F(ProxyScriptDeciderQuickCheckTest, ExplicitPacUrl) { | 422 TEST_F(ProxyScriptDeciderQuickCheckTest, ExplicitPacUrl) { |
| 420 const char *kCustomUrl = "http://custom/proxy.pac"; | 423 const char* kCustomUrl = "http://custom/proxy.pac"; |
| 421 config_.set_pac_url(GURL(kCustomUrl)); | 424 config_.set_pac_url(GURL(kCustomUrl)); |
| 422 Rules::Rule rule = rules_.AddSuccessRule(kCustomUrl); | 425 Rules::Rule rule = rules_.AddSuccessRule(kCustomUrl); |
| 423 resolver_.rules()->AddSimulatedFailure("wpad"); | 426 resolver_.rules()->AddSimulatedFailure("wpad"); |
| 424 resolver_.rules()->AddRule("custom", "1.2.3.4"); | 427 resolver_.rules()->AddRule("custom", "1.2.3.4"); |
| 425 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); | 428 EXPECT_EQ(ERR_IO_PENDING, StartDecider()); |
| 426 callback_.WaitForResult(); | 429 callback_.WaitForResult(); |
| 427 EXPECT_TRUE(decider_->effective_config().has_pac_url()); | 430 EXPECT_TRUE(decider_->effective_config().has_pac_url()); |
| 428 EXPECT_EQ(rule.url, decider_->effective_config().pac_url()); | 431 EXPECT_EQ(rule.url, decider_->effective_config().pac_url()); |
| 429 } | 432 } |
| 430 | 433 |
| 431 // Fails at WPAD (downloading), but succeeds in choosing the custom PAC. | 434 // Fails at WPAD (downloading), but succeeds in choosing the custom PAC. |
| 432 TEST(ProxyScriptDeciderTest, AutodetectFailCustomSuccess1) { | 435 TEST(ProxyScriptDeciderTest, AutodetectFailCustomSuccess1) { |
| 433 Rules rules; | 436 Rules rules; |
| 434 RuleBasedProxyScriptFetcher fetcher(&rules); | 437 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 435 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 438 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 436 | 439 |
| 437 ProxyConfig config; | 440 ProxyConfig config; |
| 438 config.set_auto_detect(true); | 441 config.set_auto_detect(true); |
| 439 config.set_pac_url(GURL("http://custom/proxy.pac")); | 442 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 440 | 443 |
| 441 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 444 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 442 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); | 445 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); |
| 443 | 446 |
| 444 TestCompletionCallback callback; | 447 TestCompletionCallback callback; |
| 445 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); | 448 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); |
| 446 EXPECT_EQ(OK, decider.Start( | 449 EXPECT_EQ( |
| 447 config, base::TimeDelta(), true, callback.callback())); | 450 OK, decider.Start(config, base::TimeDelta(), true, callback.callback())); |
| 448 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); | 451 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); |
| 449 | 452 |
| 450 EXPECT_TRUE(decider.effective_config().has_pac_url()); | 453 EXPECT_TRUE(decider.effective_config().has_pac_url()); |
| 451 EXPECT_EQ(rule.url, decider.effective_config().pac_url()); | 454 EXPECT_EQ(rule.url, decider.effective_config().pac_url()); |
| 452 } | 455 } |
| 453 | 456 |
| 454 // Fails at WPAD (no DHCP config, DNS PAC fails parsing), but succeeds in | 457 // Fails at WPAD (no DHCP config, DNS PAC fails parsing), but succeeds in |
| 455 // choosing the custom PAC. | 458 // choosing the custom PAC. |
| 456 TEST(ProxyScriptDeciderTest, AutodetectFailCustomSuccess2) { | 459 TEST(ProxyScriptDeciderTest, AutodetectFailCustomSuccess2) { |
| 457 Rules rules; | 460 Rules rules; |
| 458 RuleBasedProxyScriptFetcher fetcher(&rules); | 461 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 459 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 462 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 460 | 463 |
| 461 ProxyConfig config; | 464 ProxyConfig config; |
| 462 config.set_auto_detect(true); | 465 config.set_auto_detect(true); |
| 463 config.set_pac_url(GURL("http://custom/proxy.pac")); | 466 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 464 config.proxy_rules().ParseFromString("unused-manual-proxy:99"); | 467 config.proxy_rules().ParseFromString("unused-manual-proxy:99"); |
| 465 | 468 |
| 466 rules.AddFailParsingRule("http://wpad/wpad.dat"); | 469 rules.AddFailParsingRule("http://wpad/wpad.dat"); |
| 467 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); | 470 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); |
| 468 | 471 |
| 469 TestCompletionCallback callback; | 472 TestCompletionCallback callback; |
| 470 CapturingNetLog log; | 473 CapturingNetLog log; |
| 471 | 474 |
| 472 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); | 475 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); |
| 473 EXPECT_EQ(OK, decider.Start(config, base::TimeDelta(), | 476 EXPECT_EQ( |
| 474 true, callback.callback())); | 477 OK, decider.Start(config, base::TimeDelta(), true, callback.callback())); |
| 475 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); | 478 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); |
| 476 | 479 |
| 477 // Verify that the effective configuration no longer contains auto detect or | 480 // Verify that the effective configuration no longer contains auto detect or |
| 478 // any of the manual settings. | 481 // any of the manual settings. |
| 479 EXPECT_TRUE(decider.effective_config().Equals( | 482 EXPECT_TRUE(decider.effective_config().Equals( |
| 480 ProxyConfig::CreateFromCustomPacURL(GURL("http://custom/proxy.pac")))); | 483 ProxyConfig::CreateFromCustomPacURL(GURL("http://custom/proxy.pac")))); |
| 481 | 484 |
| 482 // Check the NetLog was filled correctly. | 485 // Check the NetLog was filled correctly. |
| 483 // (Note that various states are repeated since both WPAD and custom | 486 // (Note that various states are repeated since both WPAD and custom |
| 484 // PAC scripts are tried). | 487 // PAC scripts are tried). |
| 485 CapturingNetLog::CapturedEntryList entries; | 488 CapturingNetLog::CapturedEntryList entries; |
| 486 log.GetEntries(&entries); | 489 log.GetEntries(&entries); |
| 487 | 490 |
| 488 EXPECT_EQ(10u, entries.size()); | 491 EXPECT_EQ(10u, entries.size()); |
| 489 EXPECT_TRUE(LogContainsBeginEvent( | 492 EXPECT_TRUE( |
| 490 entries, 0, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); | 493 LogContainsBeginEvent(entries, 0, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); |
| 491 // This is the DHCP phase, which fails fetching rather than parsing, so | 494 // This is the DHCP phase, which fails fetching rather than parsing, so |
| 492 // there is no pair of SET_PAC_SCRIPT events. | 495 // there is no pair of SET_PAC_SCRIPT events. |
| 493 EXPECT_TRUE(LogContainsBeginEvent( | 496 EXPECT_TRUE(LogContainsBeginEvent( |
| 494 entries, 1, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); | 497 entries, 1, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); |
| 495 EXPECT_TRUE(LogContainsEndEvent( | 498 EXPECT_TRUE(LogContainsEndEvent( |
| 496 entries, 2, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); | 499 entries, 2, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); |
| 497 EXPECT_TRUE(LogContainsEvent( | 500 EXPECT_TRUE(LogContainsEvent( |
| 498 entries, 3, | 501 entries, |
| 502 3, |
| 499 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FALLING_BACK_TO_NEXT_PAC_SOURCE, | 503 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FALLING_BACK_TO_NEXT_PAC_SOURCE, |
| 500 NetLog::PHASE_NONE)); | 504 NetLog::PHASE_NONE)); |
| 501 // This is the DNS phase, which attempts a fetch but fails. | 505 // This is the DNS phase, which attempts a fetch but fails. |
| 502 EXPECT_TRUE(LogContainsBeginEvent( | 506 EXPECT_TRUE(LogContainsBeginEvent( |
| 503 entries, 4, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); | 507 entries, 4, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); |
| 504 EXPECT_TRUE(LogContainsEndEvent( | 508 EXPECT_TRUE(LogContainsEndEvent( |
| 505 entries, 5, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); | 509 entries, 5, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); |
| 506 EXPECT_TRUE(LogContainsEvent( | 510 EXPECT_TRUE(LogContainsEvent( |
| 507 entries, 6, | 511 entries, |
| 512 6, |
| 508 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FALLING_BACK_TO_NEXT_PAC_SOURCE, | 513 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FALLING_BACK_TO_NEXT_PAC_SOURCE, |
| 509 NetLog::PHASE_NONE)); | 514 NetLog::PHASE_NONE)); |
| 510 // Finally, the custom PAC URL phase. | 515 // Finally, the custom PAC URL phase. |
| 511 EXPECT_TRUE(LogContainsBeginEvent( | 516 EXPECT_TRUE(LogContainsBeginEvent( |
| 512 entries, 7, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); | 517 entries, 7, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); |
| 513 EXPECT_TRUE(LogContainsEndEvent( | 518 EXPECT_TRUE(LogContainsEndEvent( |
| 514 entries, 8, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); | 519 entries, 8, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); |
| 515 EXPECT_TRUE(LogContainsEndEvent( | 520 EXPECT_TRUE( |
| 516 entries, 9, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); | 521 LogContainsEndEvent(entries, 9, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); |
| 517 } | 522 } |
| 518 | 523 |
| 519 // Fails at WPAD (downloading), and fails at custom PAC (downloading). | 524 // Fails at WPAD (downloading), and fails at custom PAC (downloading). |
| 520 TEST(ProxyScriptDeciderTest, AutodetectFailCustomFails1) { | 525 TEST(ProxyScriptDeciderTest, AutodetectFailCustomFails1) { |
| 521 Rules rules; | 526 Rules rules; |
| 522 RuleBasedProxyScriptFetcher fetcher(&rules); | 527 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 523 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 528 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 524 | 529 |
| 525 ProxyConfig config; | 530 ProxyConfig config; |
| 526 config.set_auto_detect(true); | 531 config.set_auto_detect(true); |
| 527 config.set_pac_url(GURL("http://custom/proxy.pac")); | 532 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 528 | 533 |
| 529 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 534 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 530 rules.AddFailDownloadRule("http://custom/proxy.pac"); | 535 rules.AddFailDownloadRule("http://custom/proxy.pac"); |
| 531 | 536 |
| 532 TestCompletionCallback callback; | 537 TestCompletionCallback callback; |
| 533 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); | 538 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); |
| 534 EXPECT_EQ(kFailedDownloading, | 539 EXPECT_EQ( |
| 535 decider.Start(config, base::TimeDelta(), true, | 540 kFailedDownloading, |
| 536 callback.callback())); | 541 decider.Start(config, base::TimeDelta(), true, callback.callback())); |
| 537 EXPECT_EQ(NULL, decider.script_data()); | 542 EXPECT_EQ(NULL, decider.script_data()); |
| 538 } | 543 } |
| 539 | 544 |
| 540 // Fails at WPAD (downloading), and fails at custom PAC (parsing). | 545 // Fails at WPAD (downloading), and fails at custom PAC (parsing). |
| 541 TEST(ProxyScriptDeciderTest, AutodetectFailCustomFails2) { | 546 TEST(ProxyScriptDeciderTest, AutodetectFailCustomFails2) { |
| 542 Rules rules; | 547 Rules rules; |
| 543 RuleBasedProxyScriptFetcher fetcher(&rules); | 548 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 544 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 549 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 545 | 550 |
| 546 ProxyConfig config; | 551 ProxyConfig config; |
| 547 config.set_auto_detect(true); | 552 config.set_auto_detect(true); |
| 548 config.set_pac_url(GURL("http://custom/proxy.pac")); | 553 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 549 | 554 |
| 550 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 555 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 551 rules.AddFailParsingRule("http://custom/proxy.pac"); | 556 rules.AddFailParsingRule("http://custom/proxy.pac"); |
| 552 | 557 |
| 553 TestCompletionCallback callback; | 558 TestCompletionCallback callback; |
| 554 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); | 559 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); |
| 555 EXPECT_EQ(kFailedParsing, | 560 EXPECT_EQ( |
| 556 decider.Start(config, base::TimeDelta(), true, | 561 kFailedParsing, |
| 557 callback.callback())); | 562 decider.Start(config, base::TimeDelta(), true, callback.callback())); |
| 558 EXPECT_EQ(NULL, decider.script_data()); | 563 EXPECT_EQ(NULL, decider.script_data()); |
| 559 } | 564 } |
| 560 | 565 |
| 561 // This is a copy-paste of CustomPacFails1, with the exception that we give it | 566 // This is a copy-paste of CustomPacFails1, with the exception that we give it |
| 562 // a 1 millisecond delay. This means it will now complete asynchronously. | 567 // a 1 millisecond delay. This means it will now complete asynchronously. |
| 563 // Moreover, we test the NetLog to make sure it logged the pause. | 568 // Moreover, we test the NetLog to make sure it logged the pause. |
| 564 TEST(ProxyScriptDeciderTest, CustomPacFails1_WithPositiveDelay) { | 569 TEST(ProxyScriptDeciderTest, CustomPacFails1_WithPositiveDelay) { |
| 565 Rules rules; | 570 Rules rules; |
| 566 RuleBasedProxyScriptFetcher fetcher(&rules); | 571 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 567 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 572 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 568 | 573 |
| 569 ProxyConfig config; | 574 ProxyConfig config; |
| 570 config.set_pac_url(GURL("http://custom/proxy.pac")); | 575 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 571 | 576 |
| 572 rules.AddFailDownloadRule("http://custom/proxy.pac"); | 577 rules.AddFailDownloadRule("http://custom/proxy.pac"); |
| 573 | 578 |
| 574 TestCompletionCallback callback; | 579 TestCompletionCallback callback; |
| 575 CapturingNetLog log; | 580 CapturingNetLog log; |
| 576 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); | 581 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); |
| 577 EXPECT_EQ(ERR_IO_PENDING, | 582 EXPECT_EQ(ERR_IO_PENDING, |
| 578 decider.Start(config, base::TimeDelta::FromMilliseconds(1), | 583 decider.Start(config, |
| 579 true, callback.callback())); | 584 base::TimeDelta::FromMilliseconds(1), |
| 585 true, |
| 586 callback.callback())); |
| 580 | 587 |
| 581 EXPECT_EQ(kFailedDownloading, callback.WaitForResult()); | 588 EXPECT_EQ(kFailedDownloading, callback.WaitForResult()); |
| 582 EXPECT_EQ(NULL, decider.script_data()); | 589 EXPECT_EQ(NULL, decider.script_data()); |
| 583 | 590 |
| 584 // Check the NetLog was filled correctly. | 591 // Check the NetLog was filled correctly. |
| 585 CapturingNetLog::CapturedEntryList entries; | 592 CapturingNetLog::CapturedEntryList entries; |
| 586 log.GetEntries(&entries); | 593 log.GetEntries(&entries); |
| 587 | 594 |
| 588 EXPECT_EQ(6u, entries.size()); | 595 EXPECT_EQ(6u, entries.size()); |
| 589 EXPECT_TRUE(LogContainsBeginEvent( | 596 EXPECT_TRUE( |
| 590 entries, 0, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); | 597 LogContainsBeginEvent(entries, 0, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); |
| 591 EXPECT_TRUE(LogContainsBeginEvent( | 598 EXPECT_TRUE(LogContainsBeginEvent( |
| 592 entries, 1, NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT)); | 599 entries, 1, NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT)); |
| 593 EXPECT_TRUE(LogContainsEndEvent( | 600 EXPECT_TRUE( |
| 594 entries, 2, NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT)); | 601 LogContainsEndEvent(entries, 2, NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT)); |
| 595 EXPECT_TRUE(LogContainsBeginEvent( | 602 EXPECT_TRUE(LogContainsBeginEvent( |
| 596 entries, 3, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); | 603 entries, 3, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); |
| 597 EXPECT_TRUE(LogContainsEndEvent( | 604 EXPECT_TRUE(LogContainsEndEvent( |
| 598 entries, 4, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); | 605 entries, 4, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); |
| 599 EXPECT_TRUE(LogContainsEndEvent( | 606 EXPECT_TRUE( |
| 600 entries, 5, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); | 607 LogContainsEndEvent(entries, 5, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); |
| 601 } | 608 } |
| 602 | 609 |
| 603 // This is a copy-paste of CustomPacFails1, with the exception that we give it | 610 // This is a copy-paste of CustomPacFails1, with the exception that we give it |
| 604 // a -5 second delay instead of a 0 ms delay. This change should have no effect | 611 // a -5 second delay instead of a 0 ms delay. This change should have no effect |
| 605 // so the rest of the test is unchanged. | 612 // so the rest of the test is unchanged. |
| 606 TEST(ProxyScriptDeciderTest, CustomPacFails1_WithNegativeDelay) { | 613 TEST(ProxyScriptDeciderTest, CustomPacFails1_WithNegativeDelay) { |
| 607 Rules rules; | 614 Rules rules; |
| 608 RuleBasedProxyScriptFetcher fetcher(&rules); | 615 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 609 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 616 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 610 | 617 |
| 611 ProxyConfig config; | 618 ProxyConfig config; |
| 612 config.set_pac_url(GURL("http://custom/proxy.pac")); | 619 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 613 | 620 |
| 614 rules.AddFailDownloadRule("http://custom/proxy.pac"); | 621 rules.AddFailDownloadRule("http://custom/proxy.pac"); |
| 615 | 622 |
| 616 TestCompletionCallback callback; | 623 TestCompletionCallback callback; |
| 617 CapturingNetLog log; | 624 CapturingNetLog log; |
| 618 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); | 625 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); |
| 619 EXPECT_EQ(kFailedDownloading, | 626 EXPECT_EQ( |
| 620 decider.Start(config, base::TimeDelta::FromSeconds(-5), | 627 kFailedDownloading, |
| 621 true, callback.callback())); | 628 decider.Start( |
| 629 config, base::TimeDelta::FromSeconds(-5), true, callback.callback())); |
| 622 EXPECT_EQ(NULL, decider.script_data()); | 630 EXPECT_EQ(NULL, decider.script_data()); |
| 623 | 631 |
| 624 // Check the NetLog was filled correctly. | 632 // Check the NetLog was filled correctly. |
| 625 CapturingNetLog::CapturedEntryList entries; | 633 CapturingNetLog::CapturedEntryList entries; |
| 626 log.GetEntries(&entries); | 634 log.GetEntries(&entries); |
| 627 | 635 |
| 628 EXPECT_EQ(4u, entries.size()); | 636 EXPECT_EQ(4u, entries.size()); |
| 629 EXPECT_TRUE(LogContainsBeginEvent( | 637 EXPECT_TRUE( |
| 630 entries, 0, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); | 638 LogContainsBeginEvent(entries, 0, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); |
| 631 EXPECT_TRUE(LogContainsBeginEvent( | 639 EXPECT_TRUE(LogContainsBeginEvent( |
| 632 entries, 1, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); | 640 entries, 1, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); |
| 633 EXPECT_TRUE(LogContainsEndEvent( | 641 EXPECT_TRUE(LogContainsEndEvent( |
| 634 entries, 2, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); | 642 entries, 2, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); |
| 635 EXPECT_TRUE(LogContainsEndEvent( | 643 EXPECT_TRUE( |
| 636 entries, 3, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); | 644 LogContainsEndEvent(entries, 3, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); |
| 637 } | 645 } |
| 638 | 646 |
| 639 class SynchronousSuccessDhcpFetcher : public DhcpProxyScriptFetcher { | 647 class SynchronousSuccessDhcpFetcher : public DhcpProxyScriptFetcher { |
| 640 public: | 648 public: |
| 641 explicit SynchronousSuccessDhcpFetcher(const base::string16& expected_text) | 649 explicit SynchronousSuccessDhcpFetcher(const base::string16& expected_text) |
| 642 : gurl_("http://dhcppac/"), expected_text_(expected_text) { | 650 : gurl_("http://dhcppac/"), expected_text_(expected_text) {} |
| 643 } | |
| 644 | 651 |
| 645 virtual int Fetch(base::string16* utf16_text, | 652 virtual int Fetch(base::string16* utf16_text, |
| 646 const CompletionCallback& callback) OVERRIDE { | 653 const CompletionCallback& callback) OVERRIDE { |
| 647 *utf16_text = expected_text_; | 654 *utf16_text = expected_text_; |
| 648 return OK; | 655 return OK; |
| 649 } | 656 } |
| 650 | 657 |
| 651 virtual void Cancel() OVERRIDE { | 658 virtual void Cancel() OVERRIDE {} |
| 652 } | |
| 653 | 659 |
| 654 virtual const GURL& GetPacURL() const OVERRIDE { | 660 virtual const GURL& GetPacURL() const OVERRIDE { return gurl_; } |
| 655 return gurl_; | |
| 656 } | |
| 657 | 661 |
| 658 const base::string16& expected_text() const { | 662 const base::string16& expected_text() const { return expected_text_; } |
| 659 return expected_text_; | |
| 660 } | |
| 661 | 663 |
| 662 private: | 664 private: |
| 663 GURL gurl_; | 665 GURL gurl_; |
| 664 base::string16 expected_text_; | 666 base::string16 expected_text_; |
| 665 | 667 |
| 666 DISALLOW_COPY_AND_ASSIGN(SynchronousSuccessDhcpFetcher); | 668 DISALLOW_COPY_AND_ASSIGN(SynchronousSuccessDhcpFetcher); |
| 667 }; | 669 }; |
| 668 | 670 |
| 669 // All of the tests above that use ProxyScriptDecider have tested | 671 // All of the tests above that use ProxyScriptDecider have tested |
| 670 // failure to fetch a PAC file via DHCP configuration, so we now test | 672 // failure to fetch a PAC file via DHCP configuration, so we now test |
| 671 // success at downloading and parsing, and then success at downloading, | 673 // success at downloading and parsing, and then success at downloading, |
| 672 // failure at parsing. | 674 // failure at parsing. |
| 673 | 675 |
| 674 TEST(ProxyScriptDeciderTest, AutodetectDhcpSuccess) { | 676 TEST(ProxyScriptDeciderTest, AutodetectDhcpSuccess) { |
| 675 Rules rules; | 677 Rules rules; |
| 676 RuleBasedProxyScriptFetcher fetcher(&rules); | 678 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 677 SynchronousSuccessDhcpFetcher dhcp_fetcher( | 679 SynchronousSuccessDhcpFetcher dhcp_fetcher( |
| 678 base::WideToUTF16(L"http://bingo/!FindProxyForURL")); | 680 base::WideToUTF16(L"http://bingo/!FindProxyForURL")); |
| 679 | 681 |
| 680 ProxyConfig config; | 682 ProxyConfig config; |
| 681 config.set_auto_detect(true); | 683 config.set_auto_detect(true); |
| 682 | 684 |
| 683 rules.AddSuccessRule("http://bingo/"); | 685 rules.AddSuccessRule("http://bingo/"); |
| 684 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 686 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 685 | 687 |
| 686 TestCompletionCallback callback; | 688 TestCompletionCallback callback; |
| 687 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); | 689 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); |
| 688 EXPECT_EQ(OK, decider.Start( | 690 EXPECT_EQ( |
| 689 config, base::TimeDelta(), true, callback.callback())); | 691 OK, decider.Start(config, base::TimeDelta(), true, callback.callback())); |
| 690 EXPECT_EQ(dhcp_fetcher.expected_text(), | 692 EXPECT_EQ(dhcp_fetcher.expected_text(), decider.script_data()->utf16()); |
| 691 decider.script_data()->utf16()); | |
| 692 | 693 |
| 693 EXPECT_TRUE(decider.effective_config().has_pac_url()); | 694 EXPECT_TRUE(decider.effective_config().has_pac_url()); |
| 694 EXPECT_EQ(GURL("http://dhcppac/"), decider.effective_config().pac_url()); | 695 EXPECT_EQ(GURL("http://dhcppac/"), decider.effective_config().pac_url()); |
| 695 } | 696 } |
| 696 | 697 |
| 697 TEST(ProxyScriptDeciderTest, AutodetectDhcpFailParse) { | 698 TEST(ProxyScriptDeciderTest, AutodetectDhcpFailParse) { |
| 698 Rules rules; | 699 Rules rules; |
| 699 RuleBasedProxyScriptFetcher fetcher(&rules); | 700 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 700 SynchronousSuccessDhcpFetcher dhcp_fetcher( | 701 SynchronousSuccessDhcpFetcher dhcp_fetcher( |
| 701 base::WideToUTF16(L"http://bingo/!invalid-script")); | 702 base::WideToUTF16(L"http://bingo/!invalid-script")); |
| 702 | 703 |
| 703 ProxyConfig config; | 704 ProxyConfig config; |
| 704 config.set_auto_detect(true); | 705 config.set_auto_detect(true); |
| 705 | 706 |
| 706 rules.AddFailParsingRule("http://bingo/"); | 707 rules.AddFailParsingRule("http://bingo/"); |
| 707 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 708 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 708 | 709 |
| 709 TestCompletionCallback callback; | 710 TestCompletionCallback callback; |
| 710 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); | 711 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); |
| 711 // Since there is fallback to DNS-based WPAD, the final error will be that | 712 // Since there is fallback to DNS-based WPAD, the final error will be that |
| 712 // it failed downloading, not that it failed parsing. | 713 // it failed downloading, not that it failed parsing. |
| 713 EXPECT_EQ(kFailedDownloading, | 714 EXPECT_EQ( |
| 715 kFailedDownloading, |
| 714 decider.Start(config, base::TimeDelta(), true, callback.callback())); | 716 decider.Start(config, base::TimeDelta(), true, callback.callback())); |
| 715 EXPECT_EQ(NULL, decider.script_data()); | 717 EXPECT_EQ(NULL, decider.script_data()); |
| 716 | 718 |
| 717 EXPECT_FALSE(decider.effective_config().has_pac_url()); | 719 EXPECT_FALSE(decider.effective_config().has_pac_url()); |
| 718 } | 720 } |
| 719 | 721 |
| 720 class AsyncFailDhcpFetcher | 722 class AsyncFailDhcpFetcher |
| 721 : public DhcpProxyScriptFetcher, | 723 : public DhcpProxyScriptFetcher, |
| 722 public base::SupportsWeakPtr<AsyncFailDhcpFetcher> { | 724 public base::SupportsWeakPtr<AsyncFailDhcpFetcher> { |
| 723 public: | 725 public: |
| 724 AsyncFailDhcpFetcher() {} | 726 AsyncFailDhcpFetcher() {} |
| 725 virtual ~AsyncFailDhcpFetcher() {} | 727 virtual ~AsyncFailDhcpFetcher() {} |
| 726 | 728 |
| 727 virtual int Fetch(base::string16* utf16_text, | 729 virtual int Fetch(base::string16* utf16_text, |
| 728 const CompletionCallback& callback) OVERRIDE { | 730 const CompletionCallback& callback) OVERRIDE { |
| 729 callback_ = callback; | 731 callback_ = callback; |
| 730 base::MessageLoop::current()->PostTask( | 732 base::MessageLoop::current()->PostTask( |
| 731 FROM_HERE, | 733 FROM_HERE, |
| 732 base::Bind(&AsyncFailDhcpFetcher::CallbackWithFailure, AsWeakPtr())); | 734 base::Bind(&AsyncFailDhcpFetcher::CallbackWithFailure, AsWeakPtr())); |
| 733 return ERR_IO_PENDING; | 735 return ERR_IO_PENDING; |
| 734 } | 736 } |
| 735 | 737 |
| 736 virtual void Cancel() OVERRIDE { | 738 virtual void Cancel() OVERRIDE { callback_.Reset(); } |
| 737 callback_.Reset(); | |
| 738 } | |
| 739 | 739 |
| 740 virtual const GURL& GetPacURL() const OVERRIDE { | 740 virtual const GURL& GetPacURL() const OVERRIDE { return dummy_gurl_; } |
| 741 return dummy_gurl_; | |
| 742 } | |
| 743 | 741 |
| 744 void CallbackWithFailure() { | 742 void CallbackWithFailure() { |
| 745 if (!callback_.is_null()) | 743 if (!callback_.is_null()) |
| 746 callback_.Run(ERR_PAC_NOT_IN_DHCP); | 744 callback_.Run(ERR_PAC_NOT_IN_DHCP); |
| 747 } | 745 } |
| 748 | 746 |
| 749 private: | 747 private: |
| 750 GURL dummy_gurl_; | 748 GURL dummy_gurl_; |
| 751 CompletionCallback callback_; | 749 CompletionCallback callback_; |
| 752 }; | 750 }; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 775 | 773 |
| 776 // Run the message loop to let the DHCP fetch complete and post the results | 774 // Run the message loop to let the DHCP fetch complete and post the results |
| 777 // back. Before the fix linked to above, this would try to invoke on | 775 // back. Before the fix linked to above, this would try to invoke on |
| 778 // the callback object provided by ProxyScriptDecider after it was | 776 // the callback object provided by ProxyScriptDecider after it was |
| 779 // no longer valid. | 777 // no longer valid. |
| 780 base::MessageLoop::current()->RunUntilIdle(); | 778 base::MessageLoop::current()->RunUntilIdle(); |
| 781 } | 779 } |
| 782 | 780 |
| 783 } // namespace | 781 } // namespace |
| 784 } // namespace net | 782 } // namespace net |
| OLD | NEW |