blob: d4d43aabf020b9a18cb551f8117507e57ae66530 [file] [log] [blame]
[email protected]db8ff912012-06-12 23:32:511// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]5b45aec02009-03-31 01:03:232// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Lily Houghton582d4622018-01-22 22:43:405#include "net/proxy_resolution/proxy_config.h"
Daniel Cheng5feb16f2022-02-28 06:52:076
Eric Roman4c0456a2019-09-18 17:04:347#include "base/json/json_writer.h"
Eric Roman4c0456a2019-09-18 17:04:348#include "base/values.h"
Eric Orth5ccc3f02021-09-23 00:01:579#include "net/base/proxy_string_util.h"
Lily Houghton582d4622018-01-22 22:43:4010#include "net/proxy_resolution/proxy_config_service_common_unittest.h"
11#include "net/proxy_resolution/proxy_info.h"
[email protected]5b45aec02009-03-31 01:03:2312#include "testing/gtest/include/gtest/gtest.h"
13
[email protected]3dfd29f2009-09-06 00:48:3714namespace net {
[email protected]5b45aec02009-03-31 01:03:2315namespace {
[email protected]3dfd29f2009-09-06 00:48:3716
17void ExpectProxyServerEquals(const char* expectation,
[email protected]2189e092013-03-16 18:02:0218 const ProxyList& proxy_servers) {
Raul Tambre94493c652019-03-11 17:18:3519 if (expectation == nullptr) {
[email protected]2189e092013-03-16 18:02:0220 EXPECT_TRUE(proxy_servers.IsEmpty());
[email protected]5b45aec02009-03-31 01:03:2321 } else {
[email protected]2189e092013-03-16 18:02:0222 EXPECT_EQ(expectation, proxy_servers.ToPacString());
[email protected]5b45aec02009-03-31 01:03:2323 }
24}
[email protected]5b45aec02009-03-31 01:03:2325
26TEST(ProxyConfigTest, Equals) {
27 // Test |ProxyConfig::auto_detect|.
28
[email protected]3dfd29f2009-09-06 00:48:3729 ProxyConfig config1;
[email protected]ed4ed0f2010-02-24 00:20:4830 config1.set_auto_detect(true);
[email protected]5b45aec02009-03-31 01:03:2331
[email protected]3dfd29f2009-09-06 00:48:3732 ProxyConfig config2;
[email protected]ed4ed0f2010-02-24 00:20:4833 config2.set_auto_detect(false);
[email protected]5b45aec02009-03-31 01:03:2334
35 EXPECT_FALSE(config1.Equals(config2));
36 EXPECT_FALSE(config2.Equals(config1));
37
[email protected]ed4ed0f2010-02-24 00:20:4838 config2.set_auto_detect(true);
[email protected]5b45aec02009-03-31 01:03:2339
40 EXPECT_TRUE(config1.Equals(config2));
41 EXPECT_TRUE(config2.Equals(config1));
42
43 // Test |ProxyConfig::pac_url|.
44
[email protected]ed4ed0f2010-02-24 00:20:4845 config2.set_pac_url(GURL("http://wpad/wpad.dat"));
[email protected]5b45aec02009-03-31 01:03:2346
47 EXPECT_FALSE(config1.Equals(config2));
48 EXPECT_FALSE(config2.Equals(config1));
49
[email protected]ed4ed0f2010-02-24 00:20:4850 config1.set_pac_url(GURL("http://wpad/wpad.dat"));
[email protected]5b45aec02009-03-31 01:03:2351
52 EXPECT_TRUE(config1.Equals(config2));
53 EXPECT_TRUE(config2.Equals(config1));
54
55 // Test |ProxyConfig::proxy_rules|.
56
Lily Houghtone6b617e2018-01-19 20:13:0757 config2.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
[email protected]2189e092013-03-16 18:02:0258 config2.proxy_rules().single_proxies.SetSingleProxyServer(
Eric Orth5ccc3f02021-09-23 00:01:5759 ProxyUriToProxyServer("myproxy:80", ProxyServer::SCHEME_HTTP));
[email protected]5b45aec02009-03-31 01:03:2360
61 EXPECT_FALSE(config1.Equals(config2));
62 EXPECT_FALSE(config2.Equals(config1));
63
Lily Houghtone6b617e2018-01-19 20:13:0764 config1.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST;
[email protected]2189e092013-03-16 18:02:0265 config1.proxy_rules().single_proxies.SetSingleProxyServer(
Eric Orth5ccc3f02021-09-23 00:01:5766 ProxyUriToProxyServer("myproxy:100", ProxyServer::SCHEME_HTTP));
[email protected]5b45aec02009-03-31 01:03:2367
68 EXPECT_FALSE(config1.Equals(config2));
69 EXPECT_FALSE(config2.Equals(config1));
70
[email protected]2189e092013-03-16 18:02:0271 config1.proxy_rules().single_proxies.SetSingleProxyServer(
Eric Orth5ccc3f02021-09-23 00:01:5772 ProxyUriToProxyServer("myproxy", ProxyServer::SCHEME_HTTP));
[email protected]5b45aec02009-03-31 01:03:2373
74 EXPECT_TRUE(config1.Equals(config2));
75 EXPECT_TRUE(config2.Equals(config1));
76
[email protected]7541206c2010-02-19 20:24:0677 // Test |ProxyConfig::bypass_rules|.
[email protected]5b45aec02009-03-31 01:03:2378
[email protected]ed4ed0f2010-02-24 00:20:4879 config2.proxy_rules().bypass_rules.AddRuleFromString("*.google.com");
[email protected]5b45aec02009-03-31 01:03:2380
81 EXPECT_FALSE(config1.Equals(config2));
82 EXPECT_FALSE(config2.Equals(config1));
83
[email protected]ed4ed0f2010-02-24 00:20:4884 config1.proxy_rules().bypass_rules.AddRuleFromString("*.google.com");
[email protected]5b45aec02009-03-31 01:03:2385
86 EXPECT_TRUE(config1.Equals(config2));
87 EXPECT_TRUE(config2.Equals(config1));
[email protected]a48bf4a2010-06-14 18:24:5388
89 // Test |ProxyConfig::proxy_rules.reverse_bypass|.
90
91 config2.proxy_rules().reverse_bypass = true;
92
93 EXPECT_FALSE(config1.Equals(config2));
94 EXPECT_FALSE(config2.Equals(config1));
95
96 config1.proxy_rules().reverse_bypass = true;
97
98 EXPECT_TRUE(config1.Equals(config2));
99 EXPECT_TRUE(config2.Equals(config1));
[email protected]5b45aec02009-03-31 01:03:23100}
101
Eric Roman4c0456a2019-09-18 17:04:34102struct ProxyConfigToValueTestCase {
103 ProxyConfig config;
104 const char* expected_value_json;
105};
106
107class ProxyConfigToValueTest
108 : public ::testing::TestWithParam<ProxyConfigToValueTestCase> {};
109
110TEST_P(ProxyConfigToValueTest, ToValueJSON) {
111 const ProxyConfigToValueTestCase& test_case = GetParam();
112
113 base::Value value = test_case.config.ToValue();
114
115 std::string json_string;
116 ASSERT_TRUE(base::JSONWriter::Write(value, &json_string));
117
118 EXPECT_EQ(std::string(test_case.expected_value_json), json_string);
119}
120
121ProxyConfigToValueTestCase GetTestCaseDirect() {
122 return {ProxyConfig::CreateDirect(), "{}"};
123}
124
125ProxyConfigToValueTestCase GetTestCaseAutoDetect() {
126 return {ProxyConfig::CreateAutoDetect(), "{\"auto_detect\":true}"};
127}
128
129ProxyConfigToValueTestCase GetTestCasePacUrl() {
130 ProxyConfig config;
131 config.set_pac_url(GURL("http://www.example.com/test.pac"));
132
133 return {std::move(config),
134 "{\"pac_url\":\"http://www.example.com/test.pac\"}"};
135}
136
137ProxyConfigToValueTestCase GetTestCasePacUrlMandatory() {
138 ProxyConfig config;
139 config.set_pac_url(GURL("http://www.example.com/test.pac"));
140 config.set_pac_mandatory(true);
141
142 return {std::move(config),
143 "{\"pac_mandatory\":true,\"pac_url\":\"http://www.example.com/"
144 "test.pac\"}"};
145}
146
147ProxyConfigToValueTestCase GetTestCasePacUrlAndAutoDetect() {
148 ProxyConfig config = ProxyConfig::CreateAutoDetect();
149 config.set_pac_url(GURL("http://www.example.com/test.pac"));
150
151 return {
152 std::move(config),
153 "{\"auto_detect\":true,\"pac_url\":\"http://www.example.com/test.pac\"}"};
154}
155
156ProxyConfigToValueTestCase GetTestCaseSingleProxy() {
157 ProxyConfig config;
158 config.proxy_rules().ParseFromString("https://proxy1:8080");
159
160 return {std::move(config), "{\"single_proxy\":[\"https://proxy1:8080\"]}"};
161}
162
163ProxyConfigToValueTestCase GetTestCaseSingleProxyWithBypass() {
164 ProxyConfig config;
165 config.proxy_rules().ParseFromString("https://proxy1:8080");
166 config.proxy_rules().bypass_rules.AddRuleFromString("*.google.com");
167 config.proxy_rules().bypass_rules.AddRuleFromString("192.168.0.1/16");
168
169 return {std::move(config),
170 "{\"bypass_list\":[\"*.google.com\",\"192.168.0.1/"
171 "16\"],\"single_proxy\":[\"https://proxy1:8080\"]}"};
172}
173
174ProxyConfigToValueTestCase GetTestCaseSingleProxyWithReversedBypass() {
175 ProxyConfig config;
176 config.proxy_rules().ParseFromString("https://proxy1:8080");
177 config.proxy_rules().bypass_rules.AddRuleFromString("*.google.com");
178 config.proxy_rules().reverse_bypass = true;
179
180 return {std::move(config),
181 "{\"bypass_list\":[\"*.google.com\"],\"reverse_bypass\":true,"
182 "\"single_proxy\":[\"https://proxy1:8080\"]}"};
183}
184
185ProxyConfigToValueTestCase GetTestCaseProxyPerScheme() {
186 ProxyConfig config;
187 config.proxy_rules().ParseFromString(
188 "http=https://proxy1:8080;https=socks5://proxy2");
189 config.proxy_rules().bypass_rules.AddRuleFromString("*.google.com");
190 config.set_pac_url(GURL("http://wpad/wpad.dat"));
191 config.set_auto_detect(true);
192
193 return {
194 std::move(config),
195 "{\"auto_detect\":true,\"bypass_list\":[\"*.google.com\"],\"pac_url\":"
196 "\"http://wpad/wpad.dat\",\"proxy_per_scheme\":{\"http\":[\"https://"
197 "proxy1:8080\"],\"https\":[\"socks5://proxy2:1080\"]}}"};
198}
199
200ProxyConfigToValueTestCase GetTestCaseSingleProxyList() {
201 ProxyConfig config;
202 config.proxy_rules().ParseFromString(
203 "https://proxy1:8080,http://proxy2,direct://");
204
205 return {std::move(config),
206 "{\"single_proxy\":[\"https://proxy1:8080\",\"proxy2:80\",\"direct://"
207 "\"]}"};
208}
209
210INSTANTIATE_TEST_SUITE_P(
Ilia Samsonov614b3332019-11-20 22:31:54211 All,
Eric Roman4c0456a2019-09-18 17:04:34212 ProxyConfigToValueTest,
213 testing::Values(GetTestCaseDirect(),
214 GetTestCaseAutoDetect(),
215 GetTestCasePacUrl(),
216 GetTestCasePacUrlMandatory(),
217 GetTestCasePacUrlAndAutoDetect(),
218 GetTestCaseSingleProxy(),
219 GetTestCaseSingleProxyWithBypass(),
220 GetTestCaseSingleProxyWithReversedBypass(),
221 GetTestCaseProxyPerScheme(),
222 GetTestCaseSingleProxyList()));
223
[email protected]5b45aec02009-03-31 01:03:23224TEST(ProxyConfigTest, ParseProxyRules) {
225 const struct {
226 const char* proxy_rules;
227
[email protected]3dfd29f2009-09-06 00:48:37228 ProxyConfig::ProxyRules::Type type;
[email protected]2189e092013-03-16 18:02:02229 // These will be PAC-stle strings, eg 'PROXY foo.com'
[email protected]5b45aec02009-03-31 01:03:23230 const char* single_proxy;
231 const char* proxy_for_http;
232 const char* proxy_for_https;
233 const char* proxy_for_ftp;
[email protected]2b6ed3dc2010-08-25 06:03:48234 const char* fallback_proxy;
[email protected]5b45aec02009-03-31 01:03:23235 } tests[] = {
Raul Tambre94493c652019-03-11 17:18:35236 // One HTTP proxy for all schemes.
237 {
238 "myproxy:80",
[email protected]5b45aec02009-03-31 01:03:23239
Raul Tambre94493c652019-03-11 17:18:35240 ProxyConfig::ProxyRules::Type::PROXY_LIST,
241 "PROXY myproxy:80",
242 nullptr,
243 nullptr,
244 nullptr,
245 nullptr,
[email protected]87a102b2009-07-14 05:23:30246 },
247
Raul Tambre94493c652019-03-11 17:18:35248 // Multiple HTTP proxies for all schemes.
249 {
250 "myproxy:80,https://myotherproxy",
[email protected]87a102b2009-07-14 05:23:30251
Raul Tambre94493c652019-03-11 17:18:35252 ProxyConfig::ProxyRules::Type::PROXY_LIST,
253 "PROXY myproxy:80;HTTPS myotherproxy:443",
254 nullptr,
255 nullptr,
256 nullptr,
257 nullptr,
[email protected]87a102b2009-07-14 05:23:30258 },
259
Raul Tambre94493c652019-03-11 17:18:35260 // Only specify a proxy server for "http://" urls.
261 {
262 "http=myproxy:80",
[email protected]5b45aec02009-03-31 01:03:23263
Raul Tambre94493c652019-03-11 17:18:35264 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME,
265 nullptr,
266 "PROXY myproxy:80",
267 nullptr,
268 nullptr,
269 nullptr,
270 },
[email protected]2189e092013-03-16 18:02:02271
Raul Tambre94493c652019-03-11 17:18:35272 // Specify an HTTP proxy for "ftp://" and a SOCKS proxy for "https://"
273 // urls.
274 {
275 "ftp=ftp-proxy ; https=socks4://foopy",
[email protected]2189e092013-03-16 18:02:02276
Raul Tambre94493c652019-03-11 17:18:35277 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME,
278 nullptr,
279 nullptr,
280 "SOCKS foopy:1080",
281 "PROXY ftp-proxy:80",
282 nullptr,
283 },
[email protected]2189e092013-03-16 18:02:02284
Raul Tambre94493c652019-03-11 17:18:35285 // Give a scheme-specific proxy as well as a non-scheme specific.
286 // The first entry "foopy" takes precedance marking this list as
287 // Type::PROXY_LIST.
288 {
289 "foopy ; ftp=ftp-proxy",
[email protected]2189e092013-03-16 18:02:02290
Raul Tambre94493c652019-03-11 17:18:35291 ProxyConfig::ProxyRules::Type::PROXY_LIST,
292 "PROXY foopy:80",
293 nullptr,
294 nullptr,
295 nullptr,
296 nullptr,
297 },
298
299 // Give a scheme-specific proxy as well as a non-scheme specific.
300 // The first entry "ftp=ftp-proxy" takes precedance marking this list as
301 // Type::PROXY_LIST_PER_SCHEME.
302 {
303 "ftp=ftp-proxy ; foopy",
304
305 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME,
306 nullptr,
307 nullptr,
308 nullptr,
309 "PROXY ftp-proxy:80",
310 nullptr,
311 },
312
313 // Include a list of entries for a single scheme.
314 {
315 "ftp=ftp1,ftp2,ftp3",
316
317 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME,
318 nullptr,
319 nullptr,
320 nullptr,
321 "PROXY ftp1:80;PROXY ftp2:80;PROXY ftp3:80",
322 nullptr,
323 },
324
325 // Include multiple entries for the same scheme -- they accumulate.
326 {
327 "http=http1,http2; http=http3",
328
329 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME,
330 nullptr,
331 "PROXY http1:80;PROXY http2:80;PROXY http3:80",
332 nullptr,
333 nullptr,
334 nullptr,
335 },
336
337 // Include lists of entries for multiple schemes.
338 {
339 "ftp=ftp1,ftp2,ftp3 ; http=http1,http2; ",
340
341 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME,
342 nullptr,
343 "PROXY http1:80;PROXY http2:80",
344 nullptr,
345 "PROXY ftp1:80;PROXY ftp2:80;PROXY ftp3:80",
346 nullptr,
347 },
348
349 // Include non-default proxy schemes.
350 {
351 "http=https://secure_proxy; ftp=socks4://socks_proxy; "
352 "https=socks://foo",
353
354 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME,
355 nullptr,
356 "HTTPS secure_proxy:443",
357 "SOCKS5 foo:1080",
358 "SOCKS socks_proxy:1080",
359 nullptr,
360 },
361
362 // Only SOCKS proxy present, others being blank.
363 {
364 "socks=foopy",
365
366 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME,
367 nullptr,
368 nullptr,
369 nullptr,
370 nullptr,
371 "SOCKS foopy:1080",
372 },
373
374 // SOCKS proxy present along with other proxies too
375 {
376 "http=httpproxy ; https=httpsproxy ; ftp=ftpproxy ; socks=foopy ",
377
378 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME,
379 nullptr,
380 "PROXY httpproxy:80",
381 "PROXY httpsproxy:80",
382 "PROXY ftpproxy:80",
383 "SOCKS foopy:1080",
384 },
385
386 // SOCKS proxy (with modifier) present along with some proxies
387 // (FTP being blank)
388 {
389 "http=httpproxy ; https=httpsproxy ; socks=socks5://foopy ",
390
391 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME,
392 nullptr,
393 "PROXY httpproxy:80",
394 "PROXY httpsproxy:80",
395 nullptr,
396 "SOCKS5 foopy:1080",
397 },
398
399 // Include unsupported schemes -- they are discarded.
400 {
401 "crazy=foopy ; foo=bar ; https=myhttpsproxy",
402
403 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME,
404 nullptr,
405 nullptr,
406 "PROXY myhttpsproxy:80",
407 nullptr,
408 nullptr,
409 },
410
411 // direct:// as first option for a scheme.
412 {
413 "http=direct://,myhttpproxy; https=direct://",
414
415 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME,
416 nullptr,
417 "DIRECT;PROXY myhttpproxy:80",
418 "DIRECT",
419 nullptr,
420 nullptr,
421 },
422
423 // direct:// as a second option for a scheme.
424 {
425 "http=myhttpproxy,direct://",
426
427 ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME,
428 nullptr,
429 "PROXY myhttpproxy:80;DIRECT",
430 nullptr,
431 nullptr,
432 nullptr,
433 },
[email protected]2189e092013-03-16 18:02:02434
[email protected]5b45aec02009-03-31 01:03:23435 };
436
[email protected]3dfd29f2009-09-06 00:48:37437 ProxyConfig config;
[email protected]5b45aec02009-03-31 01:03:23438
Daniel Cheng5feb16f2022-02-28 06:52:07439 for (size_t i = 0; i < std::size(tests); ++i) {
[email protected]ed4ed0f2010-02-24 00:20:48440 config.proxy_rules().ParseFromString(tests[i].proxy_rules);
[email protected]5b45aec02009-03-31 01:03:23441
[email protected]ed4ed0f2010-02-24 00:20:48442 EXPECT_EQ(tests[i].type, config.proxy_rules().type);
[email protected]5b45aec02009-03-31 01:03:23443 ExpectProxyServerEquals(tests[i].single_proxy,
[email protected]2189e092013-03-16 18:02:02444 config.proxy_rules().single_proxies);
[email protected]5b45aec02009-03-31 01:03:23445 ExpectProxyServerEquals(tests[i].proxy_for_http,
[email protected]2189e092013-03-16 18:02:02446 config.proxy_rules().proxies_for_http);
[email protected]5b45aec02009-03-31 01:03:23447 ExpectProxyServerEquals(tests[i].proxy_for_https,
[email protected]2189e092013-03-16 18:02:02448 config.proxy_rules().proxies_for_https);
[email protected]5b45aec02009-03-31 01:03:23449 ExpectProxyServerEquals(tests[i].proxy_for_ftp,
[email protected]2189e092013-03-16 18:02:02450 config.proxy_rules().proxies_for_ftp);
[email protected]2b6ed3dc2010-08-25 06:03:48451 ExpectProxyServerEquals(tests[i].fallback_proxy,
[email protected]2189e092013-03-16 18:02:02452 config.proxy_rules().fallback_proxies);
[email protected]5b45aec02009-03-31 01:03:23453 }
454}
[email protected]ab501a6a2009-05-12 15:07:50455
[email protected]db8ff912012-06-12 23:32:51456TEST(ProxyConfigTest, ProxyRulesSetBypassFlag) {
457 // Test whether the did_bypass_proxy() flag is set in proxy info correctly.
458 ProxyConfig::ProxyRules rules;
459 ProxyInfo result;
460
461 rules.ParseFromString("http=httpproxy:80");
462 rules.bypass_rules.AddRuleFromString(".com");
463
464 rules.Apply(GURL("http://example.com"), &result);
465 EXPECT_TRUE(result.is_direct_only());
466 EXPECT_TRUE(result.did_bypass_proxy());
467
468 rules.Apply(GURL("http://example.org"), &result);
469 EXPECT_FALSE(result.is_direct());
470 EXPECT_FALSE(result.did_bypass_proxy());
471
472 // Try with reversed bypass rules.
473 rules.reverse_bypass = true;
474
475 rules.Apply(GURL("http://example.org"), &result);
476 EXPECT_TRUE(result.is_direct_only());
477 EXPECT_TRUE(result.did_bypass_proxy());
478
479 rules.Apply(GURL("http://example.com"), &result);
480 EXPECT_FALSE(result.is_direct());
481 EXPECT_FALSE(result.did_bypass_proxy());
482}
483
riceabec95602014-10-30 05:05:07484static const char kWsUrl[] = "ws://example.com/echo";
485static const char kWssUrl[] = "wss://example.com/echo";
486
487class ProxyConfigWebSocketTest : public ::testing::Test {
488 protected:
489 void ParseFromString(const std::string& rules) {
490 rules_.ParseFromString(rules);
491 }
492 void Apply(const GURL& gurl) { rules_.Apply(gurl, &info_); }
493 std::string ToPacString() const { return info_.ToPacString(); }
494
495 static GURL WsUrl() { return GURL(kWsUrl); }
496 static GURL WssUrl() { return GURL(kWssUrl); }
497
498 ProxyConfig::ProxyRules rules_;
499 ProxyInfo info_;
500};
501
502// If a single proxy is set for all protocols, WebSocket uses it.
503TEST_F(ProxyConfigWebSocketTest, UsesProxy) {
504 ParseFromString("proxy:3128");
505 Apply(WsUrl());
506 EXPECT_EQ("PROXY proxy:3128", ToPacString());
507}
508
Eric Roman875754c2019-10-02 16:51:20509// See RFC6455 Section 4.1. item 3, "_Proxy Usage_". Note that this favors a
510// SOCKSv4 proxy (although technically the spec only notes SOCKSv5).
511TEST_F(ProxyConfigWebSocketTest, PrefersSocksV4) {
riceabec95602014-10-30 05:05:07512 ParseFromString(
513 "http=proxy:3128 ; https=sslproxy:3128 ; socks=socksproxy:1080");
514 Apply(WsUrl());
515 EXPECT_EQ("SOCKS socksproxy:1080", ToPacString());
516}
517
Eric Roman875754c2019-10-02 16:51:20518// See RFC6455 Section 4.1. item 3, "_Proxy Usage_".
519TEST_F(ProxyConfigWebSocketTest, PrefersSocksV5) {
520 ParseFromString(
521 "http=proxy:3128 ; https=sslproxy:3128 ; socks=socks5://socksproxy:1080");
522 Apply(WsUrl());
523 EXPECT_EQ("SOCKS5 socksproxy:1080", ToPacString());
524}
525
riceabec95602014-10-30 05:05:07526TEST_F(ProxyConfigWebSocketTest, PrefersHttpsToHttp) {
527 ParseFromString("http=proxy:3128 ; https=sslproxy:3128");
528 Apply(WssUrl());
529 EXPECT_EQ("PROXY sslproxy:3128", ToPacString());
530}
531
Eric Roman875754c2019-10-02 16:51:20532// Tests when a proxy-per-url-scheme configuration was used, and proxies are
533// specified for http://, https://, and a fallback proxy (non-SOCKS).
534// Even though the fallback proxy is not SOCKS, it is still favored over the
535// proxy for http://* and https://*.
536TEST_F(ProxyConfigWebSocketTest, PrefersNonSocksFallbackOverHttps) {
537 // The notation for "socks=" is abused to set the "fallback proxy".
538 ParseFromString(
539 "http=proxy:3128 ; https=sslproxy:3128; socks=https://httpsproxy");
540 EXPECT_EQ("HTTPS httpsproxy:443", rules_.fallback_proxies.ToPacString());
541 Apply(WssUrl());
542 EXPECT_EQ("HTTPS httpsproxy:443", ToPacString());
543}
544
545// Tests when a proxy-per-url-scheme configuration was used, and the fallback
546// proxy is a non-SOCKS proxy, and no proxy was given for https://* or
547// http://*. The fallback proxy is used.
548TEST_F(ProxyConfigWebSocketTest, UsesNonSocksFallbackProxy) {
549 // The notation for "socks=" is abused to set the "fallback proxy".
550 ParseFromString("ftp=ftpproxy:3128; socks=https://httpsproxy");
551 EXPECT_EQ("HTTPS httpsproxy:443", rules_.fallback_proxies.ToPacString());
552 Apply(WssUrl());
553 EXPECT_EQ("HTTPS httpsproxy:443", ToPacString());
554}
555
riceabec95602014-10-30 05:05:07556TEST_F(ProxyConfigWebSocketTest, PrefersHttpsEvenForWs) {
557 ParseFromString("http=proxy:3128 ; https=sslproxy:3128");
558 Apply(WsUrl());
559 EXPECT_EQ("PROXY sslproxy:3128", ToPacString());
560}
561
562TEST_F(ProxyConfigWebSocketTest, PrefersHttpToDirect) {
563 ParseFromString("http=proxy:3128");
564 Apply(WssUrl());
565 EXPECT_EQ("PROXY proxy:3128", ToPacString());
566}
567
568TEST_F(ProxyConfigWebSocketTest, IgnoresFtpProxy) {
569 ParseFromString("ftp=ftpproxy:3128");
570 Apply(WssUrl());
571 EXPECT_EQ("DIRECT", ToPacString());
572}
573
574TEST_F(ProxyConfigWebSocketTest, ObeysBypassRules) {
575 ParseFromString("http=proxy:3128 ; https=sslproxy:3128");
576 rules_.bypass_rules.AddRuleFromString(".chromium.org");
577 Apply(GURL("wss://codereview.chromium.org/feed"));
578 EXPECT_EQ("DIRECT", ToPacString());
579}
580
581TEST_F(ProxyConfigWebSocketTest, ObeysLocalBypass) {
582 ParseFromString("http=proxy:3128 ; https=sslproxy:3128");
583 rules_.bypass_rules.AddRuleFromString("<local>");
584 Apply(GURL("ws://localhost/feed"));
585 EXPECT_EQ("DIRECT", ToPacString());
586}
587
[email protected]3dfd29f2009-09-06 00:48:37588} // namespace
589} // namespace net