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