| 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/bind.h" |
| 7 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 8 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 9 #include "base/time.h" | 10 #include "base/time.h" |
| 10 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 11 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 12 #include "net/base/net_log.h" | 13 #include "net/base/net_log.h" |
| 13 #include "net/base/net_log_unittest.h" | 14 #include "net/base/net_log_unittest.h" |
| 14 #include "net/base/test_completion_callback.h" | 15 #include "net/base/test_completion_callback.h" |
| 15 #include "net/proxy/dhcp_proxy_script_fetcher.h" | 16 #include "net/proxy/dhcp_proxy_script_fetcher.h" |
| 16 #include "net/proxy/proxy_config.h" | 17 #include "net/proxy/proxy_config.h" |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 RuleList rules_; | 90 RuleList rules_; |
| 90 }; | 91 }; |
| 91 | 92 |
| 92 class RuleBasedProxyScriptFetcher : public ProxyScriptFetcher { | 93 class RuleBasedProxyScriptFetcher : public ProxyScriptFetcher { |
| 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(const GURL& url, |
| 98 string16* text, | 99 string16* text, |
| 99 OldCompletionCallback* callback) { | 100 const CompletionCallback& callback) { |
| 100 const Rules::Rule& rule = rules_->GetRuleByUrl(url); | 101 const Rules::Rule& rule = rules_->GetRuleByUrl(url); |
| 101 int rv = rule.fetch_error; | 102 int rv = rule.fetch_error; |
| 102 EXPECT_NE(ERR_UNEXPECTED, rv); | 103 EXPECT_NE(ERR_UNEXPECTED, rv); |
| 103 if (rv == OK) | 104 if (rv == OK) |
| 104 *text = rule.text(); | 105 *text = rule.text(); |
| 105 return rv; | 106 return rv; |
| 106 } | 107 } |
| 107 | 108 |
| 108 virtual void Cancel() {} | 109 virtual void Cancel() {} |
| 109 | 110 |
| 110 virtual URLRequestContext* GetRequestContext() const { return NULL; } | 111 virtual URLRequestContext* GetRequestContext() const { return NULL; } |
| 111 | 112 |
| 112 private: | 113 private: |
| 113 const Rules* rules_; | 114 const Rules* rules_; |
| 114 }; | 115 }; |
| 115 | 116 |
| 116 // Succeed using custom PAC script. | 117 // Succeed using custom PAC script. |
| 117 TEST(ProxyScriptDeciderTest, CustomPacSucceeds) { | 118 TEST(ProxyScriptDeciderTest, CustomPacSucceeds) { |
| 118 Rules rules; | 119 Rules rules; |
| 119 RuleBasedProxyScriptFetcher fetcher(&rules); | 120 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 120 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 121 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 121 | 122 |
| 122 ProxyConfig config; | 123 ProxyConfig config; |
| 123 config.set_pac_url(GURL("http://custom/proxy.pac")); | 124 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 124 | 125 |
| 125 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); | 126 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); |
| 126 | 127 |
| 127 TestOldCompletionCallback callback; | 128 TestCompletionCallback callback; |
| 128 CapturingNetLog log(CapturingNetLog::kUnbounded); | 129 CapturingNetLog log(CapturingNetLog::kUnbounded); |
| 129 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); | 130 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); |
| 130 EXPECT_EQ(OK, decider.Start( | 131 EXPECT_EQ(OK, decider.Start( |
| 131 config, base::TimeDelta(), true, &callback)); | 132 config, base::TimeDelta(), true, callback.callback())); |
| 132 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); | 133 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); |
| 133 | 134 |
| 134 // Check the NetLog was filled correctly. | 135 // Check the NetLog was filled correctly. |
| 135 CapturingNetLog::EntryList entries; | 136 CapturingNetLog::EntryList entries; |
| 136 log.GetEntries(&entries); | 137 log.GetEntries(&entries); |
| 137 | 138 |
| 138 EXPECT_EQ(4u, entries.size()); | 139 EXPECT_EQ(4u, entries.size()); |
| 139 EXPECT_TRUE(LogContainsBeginEvent( | 140 EXPECT_TRUE(LogContainsBeginEvent( |
| 140 entries, 0, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); | 141 entries, 0, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); |
| 141 EXPECT_TRUE(LogContainsBeginEvent( | 142 EXPECT_TRUE(LogContainsBeginEvent( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 153 TEST(ProxyScriptDeciderTest, CustomPacFails1) { | 154 TEST(ProxyScriptDeciderTest, CustomPacFails1) { |
| 154 Rules rules; | 155 Rules rules; |
| 155 RuleBasedProxyScriptFetcher fetcher(&rules); | 156 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 156 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 157 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 157 | 158 |
| 158 ProxyConfig config; | 159 ProxyConfig config; |
| 159 config.set_pac_url(GURL("http://custom/proxy.pac")); | 160 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 160 | 161 |
| 161 rules.AddFailDownloadRule("http://custom/proxy.pac"); | 162 rules.AddFailDownloadRule("http://custom/proxy.pac"); |
| 162 | 163 |
| 163 TestOldCompletionCallback callback; | 164 TestCompletionCallback callback; |
| 164 CapturingNetLog log(CapturingNetLog::kUnbounded); | 165 CapturingNetLog log(CapturingNetLog::kUnbounded); |
| 165 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); | 166 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); |
| 166 EXPECT_EQ(kFailedDownloading, | 167 EXPECT_EQ(kFailedDownloading, |
| 167 decider.Start(config, base::TimeDelta(), true, &callback)); | 168 decider.Start(config, base::TimeDelta(), true, |
| 169 callback.callback())); |
| 168 EXPECT_EQ(NULL, decider.script_data()); | 170 EXPECT_EQ(NULL, decider.script_data()); |
| 169 | 171 |
| 170 // Check the NetLog was filled correctly. | 172 // Check the NetLog was filled correctly. |
| 171 CapturingNetLog::EntryList entries; | 173 CapturingNetLog::EntryList entries; |
| 172 log.GetEntries(&entries); | 174 log.GetEntries(&entries); |
| 173 | 175 |
| 174 EXPECT_EQ(4u, entries.size()); | 176 EXPECT_EQ(4u, entries.size()); |
| 175 EXPECT_TRUE(LogContainsBeginEvent( | 177 EXPECT_TRUE(LogContainsBeginEvent( |
| 176 entries, 0, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); | 178 entries, 0, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); |
| 177 EXPECT_TRUE(LogContainsBeginEvent( | 179 EXPECT_TRUE(LogContainsBeginEvent( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 188 TEST(ProxyScriptDeciderTest, CustomPacFails2) { | 190 TEST(ProxyScriptDeciderTest, CustomPacFails2) { |
| 189 Rules rules; | 191 Rules rules; |
| 190 RuleBasedProxyScriptFetcher fetcher(&rules); | 192 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 191 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 193 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 192 | 194 |
| 193 ProxyConfig config; | 195 ProxyConfig config; |
| 194 config.set_pac_url(GURL("http://custom/proxy.pac")); | 196 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 195 | 197 |
| 196 rules.AddFailParsingRule("http://custom/proxy.pac"); | 198 rules.AddFailParsingRule("http://custom/proxy.pac"); |
| 197 | 199 |
| 198 TestOldCompletionCallback callback; | 200 TestCompletionCallback callback; |
| 199 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); | 201 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); |
| 200 EXPECT_EQ(kFailedParsing, | 202 EXPECT_EQ(kFailedParsing, |
| 201 decider.Start(config, base::TimeDelta(), true, &callback)); | 203 decider.Start(config, base::TimeDelta(), true, |
| 204 callback.callback())); |
| 202 EXPECT_EQ(NULL, decider.script_data()); | 205 EXPECT_EQ(NULL, decider.script_data()); |
| 203 } | 206 } |
| 204 | 207 |
| 205 // Fail downloading the custom PAC script, because the fetcher was NULL. | 208 // Fail downloading the custom PAC script, because the fetcher was NULL. |
| 206 TEST(ProxyScriptDeciderTest, HasNullProxyScriptFetcher) { | 209 TEST(ProxyScriptDeciderTest, HasNullProxyScriptFetcher) { |
| 207 Rules rules; | 210 Rules rules; |
| 208 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 211 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 209 | 212 |
| 210 ProxyConfig config; | 213 ProxyConfig config; |
| 211 config.set_pac_url(GURL("http://custom/proxy.pac")); | 214 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 212 | 215 |
| 213 TestOldCompletionCallback callback; | 216 TestCompletionCallback callback; |
| 214 ProxyScriptDecider decider(NULL, &dhcp_fetcher, NULL); | 217 ProxyScriptDecider decider(NULL, &dhcp_fetcher, NULL); |
| 215 EXPECT_EQ(ERR_UNEXPECTED, | 218 EXPECT_EQ(ERR_UNEXPECTED, |
| 216 decider.Start(config, base::TimeDelta(), true, &callback)); | 219 decider.Start(config, base::TimeDelta(), true, |
| 220 callback.callback())); |
| 217 EXPECT_EQ(NULL, decider.script_data()); | 221 EXPECT_EQ(NULL, decider.script_data()); |
| 218 } | 222 } |
| 219 | 223 |
| 220 // Succeeds in choosing autodetect (WPAD DNS). | 224 // Succeeds in choosing autodetect (WPAD DNS). |
| 221 TEST(ProxyScriptDeciderTest, AutodetectSuccess) { | 225 TEST(ProxyScriptDeciderTest, AutodetectSuccess) { |
| 222 Rules rules; | 226 Rules rules; |
| 223 RuleBasedProxyScriptFetcher fetcher(&rules); | 227 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 224 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 228 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 225 | 229 |
| 226 ProxyConfig config; | 230 ProxyConfig config; |
| 227 config.set_auto_detect(true); | 231 config.set_auto_detect(true); |
| 228 | 232 |
| 229 Rules::Rule rule = rules.AddSuccessRule("http://wpad/wpad.dat"); | 233 Rules::Rule rule = rules.AddSuccessRule("http://wpad/wpad.dat"); |
| 230 | 234 |
| 231 TestOldCompletionCallback callback; | 235 TestCompletionCallback callback; |
| 232 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); | 236 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); |
| 233 EXPECT_EQ(OK, decider.Start( | 237 EXPECT_EQ(OK, decider.Start( |
| 234 config, base::TimeDelta(), true, &callback)); | 238 config, base::TimeDelta(), true, callback.callback())); |
| 235 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); | 239 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); |
| 236 | 240 |
| 237 EXPECT_TRUE(decider.effective_config().has_pac_url()); | 241 EXPECT_TRUE(decider.effective_config().has_pac_url()); |
| 238 EXPECT_EQ(rule.url, decider.effective_config().pac_url()); | 242 EXPECT_EQ(rule.url, decider.effective_config().pac_url()); |
| 239 } | 243 } |
| 240 | 244 |
| 241 // Fails at WPAD (downloading), but succeeds in choosing the custom PAC. | 245 // Fails at WPAD (downloading), but succeeds in choosing the custom PAC. |
| 242 TEST(ProxyScriptDeciderTest, AutodetectFailCustomSuccess1) { | 246 TEST(ProxyScriptDeciderTest, AutodetectFailCustomSuccess1) { |
| 243 Rules rules; | 247 Rules rules; |
| 244 RuleBasedProxyScriptFetcher fetcher(&rules); | 248 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 245 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 249 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 246 | 250 |
| 247 ProxyConfig config; | 251 ProxyConfig config; |
| 248 config.set_auto_detect(true); | 252 config.set_auto_detect(true); |
| 249 config.set_pac_url(GURL("http://custom/proxy.pac")); | 253 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 250 | 254 |
| 251 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 255 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 252 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); | 256 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); |
| 253 | 257 |
| 254 TestOldCompletionCallback callback; | 258 TestCompletionCallback callback; |
| 255 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); | 259 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); |
| 256 EXPECT_EQ(OK, decider.Start( | 260 EXPECT_EQ(OK, decider.Start( |
| 257 config, base::TimeDelta(), true, &callback)); | 261 config, base::TimeDelta(), true, callback.callback())); |
| 258 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); | 262 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); |
| 259 | 263 |
| 260 EXPECT_TRUE(decider.effective_config().has_pac_url()); | 264 EXPECT_TRUE(decider.effective_config().has_pac_url()); |
| 261 EXPECT_EQ(rule.url, decider.effective_config().pac_url()); | 265 EXPECT_EQ(rule.url, decider.effective_config().pac_url()); |
| 262 } | 266 } |
| 263 | 267 |
| 264 // Fails at WPAD (no DHCP config, DNS PAC fails parsing), but succeeds in | 268 // Fails at WPAD (no DHCP config, DNS PAC fails parsing), but succeeds in |
| 265 // choosing the custom PAC. | 269 // choosing the custom PAC. |
| 266 TEST(ProxyScriptDeciderTest, AutodetectFailCustomSuccess2) { | 270 TEST(ProxyScriptDeciderTest, AutodetectFailCustomSuccess2) { |
| 267 Rules rules; | 271 Rules rules; |
| 268 RuleBasedProxyScriptFetcher fetcher(&rules); | 272 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 269 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 273 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 270 | 274 |
| 271 ProxyConfig config; | 275 ProxyConfig config; |
| 272 config.set_auto_detect(true); | 276 config.set_auto_detect(true); |
| 273 config.set_pac_url(GURL("http://custom/proxy.pac")); | 277 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 274 config.proxy_rules().ParseFromString("unused-manual-proxy:99"); | 278 config.proxy_rules().ParseFromString("unused-manual-proxy:99"); |
| 275 | 279 |
| 276 rules.AddFailParsingRule("http://wpad/wpad.dat"); | 280 rules.AddFailParsingRule("http://wpad/wpad.dat"); |
| 277 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); | 281 Rules::Rule rule = rules.AddSuccessRule("http://custom/proxy.pac"); |
| 278 | 282 |
| 279 TestOldCompletionCallback callback; | 283 TestCompletionCallback callback; |
| 280 CapturingNetLog log(CapturingNetLog::kUnbounded); | 284 CapturingNetLog log(CapturingNetLog::kUnbounded); |
| 281 | 285 |
| 282 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); | 286 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); |
| 283 EXPECT_EQ(OK, decider.Start(config, base::TimeDelta(), | 287 EXPECT_EQ(OK, decider.Start(config, base::TimeDelta(), |
| 284 true, &callback)); | 288 true, callback.callback())); |
| 285 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); | 289 EXPECT_EQ(rule.text(), decider.script_data()->utf16()); |
| 286 | 290 |
| 287 // Verify that the effective configuration no longer contains auto detect or | 291 // Verify that the effective configuration no longer contains auto detect or |
| 288 // any of the manual settings. | 292 // any of the manual settings. |
| 289 EXPECT_TRUE(decider.effective_config().Equals( | 293 EXPECT_TRUE(decider.effective_config().Equals( |
| 290 ProxyConfig::CreateFromCustomPacURL(GURL("http://custom/proxy.pac")))); | 294 ProxyConfig::CreateFromCustomPacURL(GURL("http://custom/proxy.pac")))); |
| 291 | 295 |
| 292 // Check the NetLog was filled correctly. | 296 // Check the NetLog was filled correctly. |
| 293 // (Note that various states are repeated since both WPAD and custom | 297 // (Note that various states are repeated since both WPAD and custom |
| 294 // PAC scripts are tried). | 298 // PAC scripts are tried). |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 RuleBasedProxyScriptFetcher fetcher(&rules); | 336 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 333 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 337 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 334 | 338 |
| 335 ProxyConfig config; | 339 ProxyConfig config; |
| 336 config.set_auto_detect(true); | 340 config.set_auto_detect(true); |
| 337 config.set_pac_url(GURL("http://custom/proxy.pac")); | 341 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 338 | 342 |
| 339 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 343 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 340 rules.AddFailDownloadRule("http://custom/proxy.pac"); | 344 rules.AddFailDownloadRule("http://custom/proxy.pac"); |
| 341 | 345 |
| 342 TestOldCompletionCallback callback; | 346 TestCompletionCallback callback; |
| 343 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); | 347 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); |
| 344 EXPECT_EQ(kFailedDownloading, | 348 EXPECT_EQ(kFailedDownloading, |
| 345 decider.Start(config, base::TimeDelta(), true, &callback)); | 349 decider.Start(config, base::TimeDelta(), true, |
| 350 callback.callback())); |
| 346 EXPECT_EQ(NULL, decider.script_data()); | 351 EXPECT_EQ(NULL, decider.script_data()); |
| 347 } | 352 } |
| 348 | 353 |
| 349 // Fails at WPAD (downloading), and fails at custom PAC (parsing). | 354 // Fails at WPAD (downloading), and fails at custom PAC (parsing). |
| 350 TEST(ProxyScriptDeciderTest, AutodetectFailCustomFails2) { | 355 TEST(ProxyScriptDeciderTest, AutodetectFailCustomFails2) { |
| 351 Rules rules; | 356 Rules rules; |
| 352 RuleBasedProxyScriptFetcher fetcher(&rules); | 357 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 353 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 358 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 354 | 359 |
| 355 ProxyConfig config; | 360 ProxyConfig config; |
| 356 config.set_auto_detect(true); | 361 config.set_auto_detect(true); |
| 357 config.set_pac_url(GURL("http://custom/proxy.pac")); | 362 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 358 | 363 |
| 359 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 364 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 360 rules.AddFailParsingRule("http://custom/proxy.pac"); | 365 rules.AddFailParsingRule("http://custom/proxy.pac"); |
| 361 | 366 |
| 362 TestOldCompletionCallback callback; | 367 TestCompletionCallback callback; |
| 363 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); | 368 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); |
| 364 EXPECT_EQ(kFailedParsing, | 369 EXPECT_EQ(kFailedParsing, |
| 365 decider.Start(config, base::TimeDelta(), true, &callback)); | 370 decider.Start(config, base::TimeDelta(), true, |
| 371 callback.callback())); |
| 366 EXPECT_EQ(NULL, decider.script_data()); | 372 EXPECT_EQ(NULL, decider.script_data()); |
| 367 } | 373 } |
| 368 | 374 |
| 369 // This is a copy-paste of CustomPacFails1, with the exception that we give it | 375 // This is a copy-paste of CustomPacFails1, with the exception that we give it |
| 370 // a 1 millisecond delay. This means it will now complete asynchronously. | 376 // a 1 millisecond delay. This means it will now complete asynchronously. |
| 371 // Moreover, we test the NetLog to make sure it logged the pause. | 377 // Moreover, we test the NetLog to make sure it logged the pause. |
| 372 TEST(ProxyScriptDeciderTest, CustomPacFails1_WithPositiveDelay) { | 378 TEST(ProxyScriptDeciderTest, CustomPacFails1_WithPositiveDelay) { |
| 373 Rules rules; | 379 Rules rules; |
| 374 RuleBasedProxyScriptFetcher fetcher(&rules); | 380 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 375 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 381 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 376 | 382 |
| 377 ProxyConfig config; | 383 ProxyConfig config; |
| 378 config.set_pac_url(GURL("http://custom/proxy.pac")); | 384 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 379 | 385 |
| 380 rules.AddFailDownloadRule("http://custom/proxy.pac"); | 386 rules.AddFailDownloadRule("http://custom/proxy.pac"); |
| 381 | 387 |
| 382 TestOldCompletionCallback callback; | 388 TestCompletionCallback callback; |
| 383 CapturingNetLog log(CapturingNetLog::kUnbounded); | 389 CapturingNetLog log(CapturingNetLog::kUnbounded); |
| 384 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); | 390 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); |
| 385 EXPECT_EQ(ERR_IO_PENDING, | 391 EXPECT_EQ(ERR_IO_PENDING, |
| 386 decider.Start(config, base::TimeDelta::FromMilliseconds(1), | 392 decider.Start(config, base::TimeDelta::FromMilliseconds(1), |
| 387 true, &callback)); | 393 true, callback.callback())); |
| 388 | 394 |
| 389 EXPECT_EQ(kFailedDownloading, callback.WaitForResult()); | 395 EXPECT_EQ(kFailedDownloading, callback.WaitForResult()); |
| 390 EXPECT_EQ(NULL, decider.script_data()); | 396 EXPECT_EQ(NULL, decider.script_data()); |
| 391 | 397 |
| 392 // Check the NetLog was filled correctly. | 398 // Check the NetLog was filled correctly. |
| 393 CapturingNetLog::EntryList entries; | 399 CapturingNetLog::EntryList entries; |
| 394 log.GetEntries(&entries); | 400 log.GetEntries(&entries); |
| 395 | 401 |
| 396 EXPECT_EQ(6u, entries.size()); | 402 EXPECT_EQ(6u, entries.size()); |
| 397 EXPECT_TRUE(LogContainsBeginEvent( | 403 EXPECT_TRUE(LogContainsBeginEvent( |
| (...skipping 16 matching lines...) Expand all Loading... |
| 414 TEST(ProxyScriptDeciderTest, CustomPacFails1_WithNegativeDelay) { | 420 TEST(ProxyScriptDeciderTest, CustomPacFails1_WithNegativeDelay) { |
| 415 Rules rules; | 421 Rules rules; |
| 416 RuleBasedProxyScriptFetcher fetcher(&rules); | 422 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 417 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; | 423 DoNothingDhcpProxyScriptFetcher dhcp_fetcher; |
| 418 | 424 |
| 419 ProxyConfig config; | 425 ProxyConfig config; |
| 420 config.set_pac_url(GURL("http://custom/proxy.pac")); | 426 config.set_pac_url(GURL("http://custom/proxy.pac")); |
| 421 | 427 |
| 422 rules.AddFailDownloadRule("http://custom/proxy.pac"); | 428 rules.AddFailDownloadRule("http://custom/proxy.pac"); |
| 423 | 429 |
| 424 TestOldCompletionCallback callback; | 430 TestCompletionCallback callback; |
| 425 CapturingNetLog log(CapturingNetLog::kUnbounded); | 431 CapturingNetLog log(CapturingNetLog::kUnbounded); |
| 426 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); | 432 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, &log); |
| 427 EXPECT_EQ(kFailedDownloading, | 433 EXPECT_EQ(kFailedDownloading, |
| 428 decider.Start(config, base::TimeDelta::FromSeconds(-5), | 434 decider.Start(config, base::TimeDelta::FromSeconds(-5), |
| 429 true, &callback)); | 435 true, callback.callback())); |
| 430 EXPECT_EQ(NULL, decider.script_data()); | 436 EXPECT_EQ(NULL, decider.script_data()); |
| 431 | 437 |
| 432 // Check the NetLog was filled correctly. | 438 // Check the NetLog was filled correctly. |
| 433 CapturingNetLog::EntryList entries; | 439 CapturingNetLog::EntryList entries; |
| 434 log.GetEntries(&entries); | 440 log.GetEntries(&entries); |
| 435 | 441 |
| 436 EXPECT_EQ(4u, entries.size()); | 442 EXPECT_EQ(4u, entries.size()); |
| 437 EXPECT_TRUE(LogContainsBeginEvent( | 443 EXPECT_TRUE(LogContainsBeginEvent( |
| 438 entries, 0, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); | 444 entries, 0, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); |
| 439 EXPECT_TRUE(LogContainsBeginEvent( | 445 EXPECT_TRUE(LogContainsBeginEvent( |
| 440 entries, 1, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); | 446 entries, 1, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); |
| 441 EXPECT_TRUE(LogContainsEndEvent( | 447 EXPECT_TRUE(LogContainsEndEvent( |
| 442 entries, 2, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); | 448 entries, 2, NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT)); |
| 443 EXPECT_TRUE(LogContainsEndEvent( | 449 EXPECT_TRUE(LogContainsEndEvent( |
| 444 entries, 3, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); | 450 entries, 3, NetLog::TYPE_PROXY_SCRIPT_DECIDER)); |
| 445 } | 451 } |
| 446 | 452 |
| 447 class SynchronousSuccessDhcpFetcher : public DhcpProxyScriptFetcher { | 453 class SynchronousSuccessDhcpFetcher : public DhcpProxyScriptFetcher { |
| 448 public: | 454 public: |
| 449 explicit SynchronousSuccessDhcpFetcher(const string16& expected_text) | 455 explicit SynchronousSuccessDhcpFetcher(const string16& expected_text) |
| 450 : gurl_("http://dhcppac/"), expected_text_(expected_text) { | 456 : gurl_("http://dhcppac/"), expected_text_(expected_text) { |
| 451 } | 457 } |
| 452 | 458 |
| 453 int Fetch(string16* utf16_text, OldCompletionCallback* callback) OVERRIDE { | 459 int Fetch(string16* utf16_text, const CompletionCallback& callback) OVERRIDE { |
| 454 *utf16_text = expected_text_; | 460 *utf16_text = expected_text_; |
| 455 return OK; | 461 return OK; |
| 456 } | 462 } |
| 457 | 463 |
| 458 void Cancel() OVERRIDE { | 464 void Cancel() OVERRIDE { |
| 459 } | 465 } |
| 460 | 466 |
| 461 const GURL& GetPacURL() const OVERRIDE { | 467 const GURL& GetPacURL() const OVERRIDE { |
| 462 return gurl_; | 468 return gurl_; |
| 463 } | 469 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 483 RuleBasedProxyScriptFetcher fetcher(&rules); | 489 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 484 SynchronousSuccessDhcpFetcher dhcp_fetcher( | 490 SynchronousSuccessDhcpFetcher dhcp_fetcher( |
| 485 WideToUTF16(L"http://bingo/!FindProxyForURL")); | 491 WideToUTF16(L"http://bingo/!FindProxyForURL")); |
| 486 | 492 |
| 487 ProxyConfig config; | 493 ProxyConfig config; |
| 488 config.set_auto_detect(true); | 494 config.set_auto_detect(true); |
| 489 | 495 |
| 490 rules.AddSuccessRule("http://bingo/"); | 496 rules.AddSuccessRule("http://bingo/"); |
| 491 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 497 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 492 | 498 |
| 493 TestOldCompletionCallback callback; | 499 TestCompletionCallback callback; |
| 494 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); | 500 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); |
| 495 EXPECT_EQ(OK, decider.Start( | 501 EXPECT_EQ(OK, decider.Start( |
| 496 config, base::TimeDelta(), true, &callback)); | 502 config, base::TimeDelta(), true, callback.callback())); |
| 497 EXPECT_EQ(dhcp_fetcher.expected_text(), | 503 EXPECT_EQ(dhcp_fetcher.expected_text(), |
| 498 decider.script_data()->utf16()); | 504 decider.script_data()->utf16()); |
| 499 | 505 |
| 500 EXPECT_TRUE(decider.effective_config().has_pac_url()); | 506 EXPECT_TRUE(decider.effective_config().has_pac_url()); |
| 501 EXPECT_EQ(GURL("http://dhcppac/"), decider.effective_config().pac_url()); | 507 EXPECT_EQ(GURL("http://dhcppac/"), decider.effective_config().pac_url()); |
| 502 } | 508 } |
| 503 | 509 |
| 504 TEST(ProxyScriptDeciderTest, AutodetectDhcpFailParse) { | 510 TEST(ProxyScriptDeciderTest, AutodetectDhcpFailParse) { |
| 505 Rules rules; | 511 Rules rules; |
| 506 RuleBasedProxyScriptFetcher fetcher(&rules); | 512 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 507 SynchronousSuccessDhcpFetcher dhcp_fetcher( | 513 SynchronousSuccessDhcpFetcher dhcp_fetcher( |
| 508 WideToUTF16(L"http://bingo/!invalid-script")); | 514 WideToUTF16(L"http://bingo/!invalid-script")); |
| 509 | 515 |
| 510 ProxyConfig config; | 516 ProxyConfig config; |
| 511 config.set_auto_detect(true); | 517 config.set_auto_detect(true); |
| 512 | 518 |
| 513 rules.AddFailParsingRule("http://bingo/"); | 519 rules.AddFailParsingRule("http://bingo/"); |
| 514 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 520 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 515 | 521 |
| 516 TestOldCompletionCallback callback; | 522 TestCompletionCallback callback; |
| 517 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); | 523 ProxyScriptDecider decider(&fetcher, &dhcp_fetcher, NULL); |
| 518 // Since there is fallback to DNS-based WPAD, the final error will be that | 524 // Since there is fallback to DNS-based WPAD, the final error will be that |
| 519 // it failed downloading, not that it failed parsing. | 525 // it failed downloading, not that it failed parsing. |
| 520 EXPECT_EQ(kFailedDownloading, | 526 EXPECT_EQ(kFailedDownloading, |
| 521 decider.Start(config, base::TimeDelta(), true, &callback)); | 527 decider.Start(config, base::TimeDelta(), true, callback.callback())); |
| 522 EXPECT_EQ(NULL, decider.script_data()); | 528 EXPECT_EQ(NULL, decider.script_data()); |
| 523 | 529 |
| 524 EXPECT_FALSE(decider.effective_config().has_pac_url()); | 530 EXPECT_FALSE(decider.effective_config().has_pac_url()); |
| 525 } | 531 } |
| 526 | 532 |
| 527 class AsyncFailDhcpFetcher | 533 class AsyncFailDhcpFetcher |
| 528 : public DhcpProxyScriptFetcher, | 534 : public DhcpProxyScriptFetcher, |
| 529 public base::RefCountedThreadSafe<AsyncFailDhcpFetcher> { | 535 public base::RefCountedThreadSafe<AsyncFailDhcpFetcher> { |
| 530 public: | 536 public: |
| 531 AsyncFailDhcpFetcher() : callback_(NULL) { | 537 AsyncFailDhcpFetcher() {} |
| 532 } | |
| 533 | 538 |
| 534 int Fetch(string16* utf16_text, OldCompletionCallback* callback) OVERRIDE { | 539 int Fetch(string16* utf16_text, const CompletionCallback& callback) OVERRIDE { |
| 535 callback_ = callback; | 540 callback_ = callback; |
| 536 MessageLoop::current()->PostTask( | 541 MessageLoop::current()->PostTask( |
| 537 FROM_HERE, | 542 FROM_HERE, |
| 538 NewRunnableMethod(this, &AsyncFailDhcpFetcher::CallbackWithFailure)); | 543 base::Bind(&AsyncFailDhcpFetcher::CallbackWithFailure, this)); |
| 539 return ERR_IO_PENDING; | 544 return ERR_IO_PENDING; |
| 540 } | 545 } |
| 541 | 546 |
| 542 void Cancel() OVERRIDE { | 547 void Cancel() OVERRIDE { |
| 543 callback_ = NULL; | 548 callback_.Reset(); |
| 544 } | 549 } |
| 545 | 550 |
| 546 const GURL& GetPacURL() const OVERRIDE { | 551 const GURL& GetPacURL() const OVERRIDE { |
| 547 return dummy_gurl_; | 552 return dummy_gurl_; |
| 548 } | 553 } |
| 549 | 554 |
| 550 void CallbackWithFailure() { | 555 void CallbackWithFailure() { |
| 551 if (callback_) | 556 if (!callback_.is_null()) |
| 552 callback_->Run(ERR_PAC_NOT_IN_DHCP); | 557 callback_.Run(ERR_PAC_NOT_IN_DHCP); |
| 553 } | 558 } |
| 554 | 559 |
| 555 private: | 560 private: |
| 556 GURL dummy_gurl_; | 561 GURL dummy_gurl_; |
| 557 OldCompletionCallback* callback_; | 562 CompletionCallback callback_; |
| 558 }; | 563 }; |
| 559 | 564 |
| 560 TEST(ProxyScriptDeciderTest, DhcpCancelledByDestructor) { | 565 TEST(ProxyScriptDeciderTest, DhcpCancelledByDestructor) { |
| 561 // This regression test would crash before | 566 // This regression test would crash before |
| 562 // http://codereview.chromium.org/7044058/ | 567 // http://codereview.chromium.org/7044058/ |
| 563 // Thus, we don't care much about actual results (hence no EXPECT or ASSERT | 568 // Thus, we don't care much about actual results (hence no EXPECT or ASSERT |
| 564 // macros below), just that it doesn't crash. | 569 // macros below), just that it doesn't crash. |
| 565 Rules rules; | 570 Rules rules; |
| 566 RuleBasedProxyScriptFetcher fetcher(&rules); | 571 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 567 | 572 |
| 568 scoped_refptr<AsyncFailDhcpFetcher> dhcp_fetcher(new AsyncFailDhcpFetcher()); | 573 scoped_refptr<AsyncFailDhcpFetcher> dhcp_fetcher(new AsyncFailDhcpFetcher()); |
| 569 | 574 |
| 570 ProxyConfig config; | 575 ProxyConfig config; |
| 571 config.set_auto_detect(true); | 576 config.set_auto_detect(true); |
| 572 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 577 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 573 | 578 |
| 574 TestOldCompletionCallback callback; | 579 TestCompletionCallback callback; |
| 575 | 580 |
| 576 // Scope so ProxyScriptDecider gets destroyed early. | 581 // Scope so ProxyScriptDecider gets destroyed early. |
| 577 { | 582 { |
| 578 ProxyScriptDecider decider(&fetcher, dhcp_fetcher.get(), NULL); | 583 ProxyScriptDecider decider(&fetcher, dhcp_fetcher.get(), NULL); |
| 579 decider.Start(config, base::TimeDelta(), true, &callback); | 584 decider.Start(config, base::TimeDelta(), true, callback.callback()); |
| 580 } | 585 } |
| 581 | 586 |
| 582 // Run the message loop to let the DHCP fetch complete and post the results | 587 // Run the message loop to let the DHCP fetch complete and post the results |
| 583 // back. Before the fix linked to above, this would try to invoke on | 588 // back. Before the fix linked to above, this would try to invoke on |
| 584 // the callback object provided by ProxyScriptDecider after it was | 589 // the callback object provided by ProxyScriptDecider after it was |
| 585 // no longer valid. | 590 // no longer valid. |
| 586 MessageLoop::current()->RunAllPending(); | 591 MessageLoop::current()->RunAllPending(); |
| 587 } | 592 } |
| 588 | 593 |
| 589 } // namespace | 594 } // namespace |
| 590 } // namespace net | 595 } // namespace net |
| OLD | NEW |