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