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