[email protected] | db8ff91 | 2012-06-12 23:32:51 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 5 | #include "net/proxy_resolution/proxy_config.h" |
Eric Roman | 4c0456a | 2019-09-18 17:04:34 | [diff] [blame] | 6 | #include "base/json/json_writer.h" |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 7 | #include "base/stl_util.h" |
Eric Roman | 4c0456a | 2019-09-18 17:04:34 | [diff] [blame] | 8 | #include "base/values.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 9 | #include "net/proxy_resolution/proxy_config_service_common_unittest.h" |
| 10 | #include "net/proxy_resolution/proxy_info.h" |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 13 | namespace net { |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 14 | namespace { |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 15 | |
| 16 | void ExpectProxyServerEquals(const char* expectation, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 17 | const ProxyList& proxy_servers) { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 18 | if (expectation == nullptr) { |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 19 | EXPECT_TRUE(proxy_servers.IsEmpty()); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 20 | } else { |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 21 | EXPECT_EQ(expectation, proxy_servers.ToPacString()); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 22 | } |
| 23 | } |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 24 | |
| 25 | TEST(ProxyConfigTest, Equals) { |
| 26 | // Test |ProxyConfig::auto_detect|. |
| 27 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 28 | ProxyConfig config1; |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 29 | config1.set_auto_detect(true); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 30 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 31 | ProxyConfig config2; |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 32 | config2.set_auto_detect(false); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 33 | |
| 34 | EXPECT_FALSE(config1.Equals(config2)); |
| 35 | EXPECT_FALSE(config2.Equals(config1)); |
| 36 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 37 | config2.set_auto_detect(true); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 38 | |
| 39 | EXPECT_TRUE(config1.Equals(config2)); |
| 40 | EXPECT_TRUE(config2.Equals(config1)); |
| 41 | |
| 42 | // Test |ProxyConfig::pac_url|. |
| 43 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 44 | config2.set_pac_url(GURL("http://wpad/wpad.dat")); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 45 | |
| 46 | EXPECT_FALSE(config1.Equals(config2)); |
| 47 | EXPECT_FALSE(config2.Equals(config1)); |
| 48 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 49 | config1.set_pac_url(GURL("http://wpad/wpad.dat")); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 50 | |
| 51 | EXPECT_TRUE(config1.Equals(config2)); |
| 52 | EXPECT_TRUE(config2.Equals(config1)); |
| 53 | |
| 54 | // Test |ProxyConfig::proxy_rules|. |
| 55 | |
Lily Houghton | e6b617e | 2018-01-19 20:13:07 | [diff] [blame] | 56 | config2.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST; |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 57 | config2.proxy_rules().single_proxies.SetSingleProxyServer( |
| 58 | ProxyServer::FromURI("myproxy:80", ProxyServer::SCHEME_HTTP)); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 59 | |
| 60 | EXPECT_FALSE(config1.Equals(config2)); |
| 61 | EXPECT_FALSE(config2.Equals(config1)); |
| 62 | |
Lily Houghton | e6b617e | 2018-01-19 20:13:07 | [diff] [blame] | 63 | config1.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST; |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 64 | config1.proxy_rules().single_proxies.SetSingleProxyServer( |
| 65 | ProxyServer::FromURI("myproxy:100", ProxyServer::SCHEME_HTTP)); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 66 | |
| 67 | EXPECT_FALSE(config1.Equals(config2)); |
| 68 | EXPECT_FALSE(config2.Equals(config1)); |
| 69 | |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 70 | config1.proxy_rules().single_proxies.SetSingleProxyServer( |
| 71 | ProxyServer::FromURI("myproxy", ProxyServer::SCHEME_HTTP)); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 72 | |
| 73 | EXPECT_TRUE(config1.Equals(config2)); |
| 74 | EXPECT_TRUE(config2.Equals(config1)); |
| 75 | |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 76 | // Test |ProxyConfig::bypass_rules|. |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 77 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 78 | config2.proxy_rules().bypass_rules.AddRuleFromString("*.google.com"); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 79 | |
| 80 | EXPECT_FALSE(config1.Equals(config2)); |
| 81 | EXPECT_FALSE(config2.Equals(config1)); |
| 82 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 83 | config1.proxy_rules().bypass_rules.AddRuleFromString("*.google.com"); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 84 | |
| 85 | EXPECT_TRUE(config1.Equals(config2)); |
| 86 | EXPECT_TRUE(config2.Equals(config1)); |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 87 | |
| 88 | // Test |ProxyConfig::proxy_rules.reverse_bypass|. |
| 89 | |
| 90 | config2.proxy_rules().reverse_bypass = true; |
| 91 | |
| 92 | EXPECT_FALSE(config1.Equals(config2)); |
| 93 | EXPECT_FALSE(config2.Equals(config1)); |
| 94 | |
| 95 | config1.proxy_rules().reverse_bypass = true; |
| 96 | |
| 97 | EXPECT_TRUE(config1.Equals(config2)); |
| 98 | EXPECT_TRUE(config2.Equals(config1)); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 99 | } |
| 100 | |
Eric Roman | 4c0456a | 2019-09-18 17:04:34 | [diff] [blame] | 101 | struct ProxyConfigToValueTestCase { |
| 102 | ProxyConfig config; |
| 103 | const char* expected_value_json; |
| 104 | }; |
| 105 | |
| 106 | class ProxyConfigToValueTest |
| 107 | : public ::testing::TestWithParam<ProxyConfigToValueTestCase> {}; |
| 108 | |
| 109 | TEST_P(ProxyConfigToValueTest, ToValueJSON) { |
| 110 | const ProxyConfigToValueTestCase& test_case = GetParam(); |
| 111 | |
| 112 | base::Value value = test_case.config.ToValue(); |
| 113 | |
| 114 | std::string json_string; |
| 115 | ASSERT_TRUE(base::JSONWriter::Write(value, &json_string)); |
| 116 | |
| 117 | EXPECT_EQ(std::string(test_case.expected_value_json), json_string); |
| 118 | } |
| 119 | |
| 120 | ProxyConfigToValueTestCase GetTestCaseDirect() { |
| 121 | return {ProxyConfig::CreateDirect(), "{}"}; |
| 122 | } |
| 123 | |
| 124 | ProxyConfigToValueTestCase GetTestCaseAutoDetect() { |
| 125 | return {ProxyConfig::CreateAutoDetect(), "{\"auto_detect\":true}"}; |
| 126 | } |
| 127 | |
| 128 | ProxyConfigToValueTestCase GetTestCasePacUrl() { |
| 129 | ProxyConfig config; |
| 130 | config.set_pac_url(GURL("http://www.example.com/test.pac")); |
| 131 | |
| 132 | return {std::move(config), |
| 133 | "{\"pac_url\":\"http://www.example.com/test.pac\"}"}; |
| 134 | } |
| 135 | |
| 136 | ProxyConfigToValueTestCase GetTestCasePacUrlMandatory() { |
| 137 | ProxyConfig config; |
| 138 | config.set_pac_url(GURL("http://www.example.com/test.pac")); |
| 139 | config.set_pac_mandatory(true); |
| 140 | |
| 141 | return {std::move(config), |
| 142 | "{\"pac_mandatory\":true,\"pac_url\":\"http://www.example.com/" |
| 143 | "test.pac\"}"}; |
| 144 | } |
| 145 | |
| 146 | ProxyConfigToValueTestCase GetTestCasePacUrlAndAutoDetect() { |
| 147 | ProxyConfig config = ProxyConfig::CreateAutoDetect(); |
| 148 | config.set_pac_url(GURL("http://www.example.com/test.pac")); |
| 149 | |
| 150 | return { |
| 151 | std::move(config), |
| 152 | "{\"auto_detect\":true,\"pac_url\":\"http://www.example.com/test.pac\"}"}; |
| 153 | } |
| 154 | |
| 155 | ProxyConfigToValueTestCase GetTestCaseSingleProxy() { |
| 156 | ProxyConfig config; |
| 157 | config.proxy_rules().ParseFromString("https://proxy1:8080"); |
| 158 | |
| 159 | return {std::move(config), "{\"single_proxy\":[\"https://proxy1:8080\"]}"}; |
| 160 | } |
| 161 | |
| 162 | ProxyConfigToValueTestCase GetTestCaseSingleProxyWithBypass() { |
| 163 | ProxyConfig config; |
| 164 | config.proxy_rules().ParseFromString("https://proxy1:8080"); |
| 165 | config.proxy_rules().bypass_rules.AddRuleFromString("*.google.com"); |
| 166 | config.proxy_rules().bypass_rules.AddRuleFromString("192.168.0.1/16"); |
| 167 | |
| 168 | return {std::move(config), |
| 169 | "{\"bypass_list\":[\"*.google.com\",\"192.168.0.1/" |
| 170 | "16\"],\"single_proxy\":[\"https://proxy1:8080\"]}"}; |
| 171 | } |
| 172 | |
| 173 | ProxyConfigToValueTestCase GetTestCaseSingleProxyWithReversedBypass() { |
| 174 | ProxyConfig config; |
| 175 | config.proxy_rules().ParseFromString("https://proxy1:8080"); |
| 176 | config.proxy_rules().bypass_rules.AddRuleFromString("*.google.com"); |
| 177 | config.proxy_rules().reverse_bypass = true; |
| 178 | |
| 179 | return {std::move(config), |
| 180 | "{\"bypass_list\":[\"*.google.com\"],\"reverse_bypass\":true," |
| 181 | "\"single_proxy\":[\"https://proxy1:8080\"]}"}; |
| 182 | } |
| 183 | |
| 184 | ProxyConfigToValueTestCase GetTestCaseProxyPerScheme() { |
| 185 | ProxyConfig config; |
| 186 | config.proxy_rules().ParseFromString( |
| 187 | "http=https://proxy1:8080;https=socks5://proxy2"); |
| 188 | config.proxy_rules().bypass_rules.AddRuleFromString("*.google.com"); |
| 189 | config.set_pac_url(GURL("http://wpad/wpad.dat")); |
| 190 | config.set_auto_detect(true); |
| 191 | |
| 192 | return { |
| 193 | std::move(config), |
| 194 | "{\"auto_detect\":true,\"bypass_list\":[\"*.google.com\"],\"pac_url\":" |
| 195 | "\"http://wpad/wpad.dat\",\"proxy_per_scheme\":{\"http\":[\"https://" |
| 196 | "proxy1:8080\"],\"https\":[\"socks5://proxy2:1080\"]}}"}; |
| 197 | } |
| 198 | |
| 199 | ProxyConfigToValueTestCase GetTestCaseSingleProxyList() { |
| 200 | ProxyConfig config; |
| 201 | config.proxy_rules().ParseFromString( |
| 202 | "https://proxy1:8080,http://proxy2,direct://"); |
| 203 | |
| 204 | return {std::move(config), |
| 205 | "{\"single_proxy\":[\"https://proxy1:8080\",\"proxy2:80\",\"direct://" |
| 206 | "\"]}"}; |
| 207 | } |
| 208 | |
| 209 | INSTANTIATE_TEST_SUITE_P( |
| 210 | , |
| 211 | ProxyConfigToValueTest, |
| 212 | testing::Values(GetTestCaseDirect(), |
| 213 | GetTestCaseAutoDetect(), |
| 214 | GetTestCasePacUrl(), |
| 215 | GetTestCasePacUrlMandatory(), |
| 216 | GetTestCasePacUrlAndAutoDetect(), |
| 217 | GetTestCaseSingleProxy(), |
| 218 | GetTestCaseSingleProxyWithBypass(), |
| 219 | GetTestCaseSingleProxyWithReversedBypass(), |
| 220 | GetTestCaseProxyPerScheme(), |
| 221 | GetTestCaseSingleProxyList())); |
| 222 | |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 223 | TEST(ProxyConfigTest, ParseProxyRules) { |
| 224 | const struct { |
| 225 | const char* proxy_rules; |
| 226 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 227 | ProxyConfig::ProxyRules::Type type; |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 228 | // These will be PAC-stle strings, eg 'PROXY foo.com' |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 229 | const char* single_proxy; |
| 230 | const char* proxy_for_http; |
| 231 | const char* proxy_for_https; |
| 232 | const char* proxy_for_ftp; |
[email protected] | 2b6ed3dc | 2010-08-25 06:03:48 | [diff] [blame] | 233 | const char* fallback_proxy; |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 234 | } tests[] = { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 235 | // One HTTP proxy for all schemes. |
| 236 | { |
| 237 | "myproxy:80", |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 238 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 239 | ProxyConfig::ProxyRules::Type::PROXY_LIST, |
| 240 | "PROXY myproxy:80", |
| 241 | nullptr, |
| 242 | nullptr, |
| 243 | nullptr, |
| 244 | nullptr, |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 245 | }, |
| 246 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 247 | // Multiple HTTP proxies for all schemes. |
| 248 | { |
| 249 | "myproxy:80,https://myotherproxy", |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 250 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 251 | ProxyConfig::ProxyRules::Type::PROXY_LIST, |
| 252 | "PROXY myproxy:80;HTTPS myotherproxy:443", |
| 253 | nullptr, |
| 254 | nullptr, |
| 255 | nullptr, |
| 256 | nullptr, |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 257 | }, |
| 258 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 259 | // Only specify a proxy server for "http://" urls. |
| 260 | { |
| 261 | "http=myproxy:80", |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 262 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 263 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 264 | nullptr, |
| 265 | "PROXY myproxy:80", |
| 266 | nullptr, |
| 267 | nullptr, |
| 268 | nullptr, |
| 269 | }, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 270 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 271 | // Specify an HTTP proxy for "ftp://" and a SOCKS proxy for "https://" |
| 272 | // urls. |
| 273 | { |
| 274 | "ftp=ftp-proxy ; https=socks4://foopy", |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 275 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 276 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 277 | nullptr, |
| 278 | nullptr, |
| 279 | "SOCKS foopy:1080", |
| 280 | "PROXY ftp-proxy:80", |
| 281 | nullptr, |
| 282 | }, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 283 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 284 | // Give a scheme-specific proxy as well as a non-scheme specific. |
| 285 | // The first entry "foopy" takes precedance marking this list as |
| 286 | // Type::PROXY_LIST. |
| 287 | { |
| 288 | "foopy ; ftp=ftp-proxy", |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 289 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 290 | ProxyConfig::ProxyRules::Type::PROXY_LIST, |
| 291 | "PROXY foopy:80", |
| 292 | nullptr, |
| 293 | nullptr, |
| 294 | nullptr, |
| 295 | nullptr, |
| 296 | }, |
| 297 | |
| 298 | // Give a scheme-specific proxy as well as a non-scheme specific. |
| 299 | // The first entry "ftp=ftp-proxy" takes precedance marking this list as |
| 300 | // Type::PROXY_LIST_PER_SCHEME. |
| 301 | { |
| 302 | "ftp=ftp-proxy ; foopy", |
| 303 | |
| 304 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 305 | nullptr, |
| 306 | nullptr, |
| 307 | nullptr, |
| 308 | "PROXY ftp-proxy:80", |
| 309 | nullptr, |
| 310 | }, |
| 311 | |
| 312 | // Include a list of entries for a single scheme. |
| 313 | { |
| 314 | "ftp=ftp1,ftp2,ftp3", |
| 315 | |
| 316 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 317 | nullptr, |
| 318 | nullptr, |
| 319 | nullptr, |
| 320 | "PROXY ftp1:80;PROXY ftp2:80;PROXY ftp3:80", |
| 321 | nullptr, |
| 322 | }, |
| 323 | |
| 324 | // Include multiple entries for the same scheme -- they accumulate. |
| 325 | { |
| 326 | "http=http1,http2; http=http3", |
| 327 | |
| 328 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 329 | nullptr, |
| 330 | "PROXY http1:80;PROXY http2:80;PROXY http3:80", |
| 331 | nullptr, |
| 332 | nullptr, |
| 333 | nullptr, |
| 334 | }, |
| 335 | |
| 336 | // Include lists of entries for multiple schemes. |
| 337 | { |
| 338 | "ftp=ftp1,ftp2,ftp3 ; http=http1,http2; ", |
| 339 | |
| 340 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 341 | nullptr, |
| 342 | "PROXY http1:80;PROXY http2:80", |
| 343 | nullptr, |
| 344 | "PROXY ftp1:80;PROXY ftp2:80;PROXY ftp3:80", |
| 345 | nullptr, |
| 346 | }, |
| 347 | |
| 348 | // Include non-default proxy schemes. |
| 349 | { |
| 350 | "http=https://secure_proxy; ftp=socks4://socks_proxy; " |
| 351 | "https=socks://foo", |
| 352 | |
| 353 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 354 | nullptr, |
| 355 | "HTTPS secure_proxy:443", |
| 356 | "SOCKS5 foo:1080", |
| 357 | "SOCKS socks_proxy:1080", |
| 358 | nullptr, |
| 359 | }, |
| 360 | |
| 361 | // Only SOCKS proxy present, others being blank. |
| 362 | { |
| 363 | "socks=foopy", |
| 364 | |
| 365 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 366 | nullptr, |
| 367 | nullptr, |
| 368 | nullptr, |
| 369 | nullptr, |
| 370 | "SOCKS foopy:1080", |
| 371 | }, |
| 372 | |
| 373 | // SOCKS proxy present along with other proxies too |
| 374 | { |
| 375 | "http=httpproxy ; https=httpsproxy ; ftp=ftpproxy ; socks=foopy ", |
| 376 | |
| 377 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 378 | nullptr, |
| 379 | "PROXY httpproxy:80", |
| 380 | "PROXY httpsproxy:80", |
| 381 | "PROXY ftpproxy:80", |
| 382 | "SOCKS foopy:1080", |
| 383 | }, |
| 384 | |
| 385 | // SOCKS proxy (with modifier) present along with some proxies |
| 386 | // (FTP being blank) |
| 387 | { |
| 388 | "http=httpproxy ; https=httpsproxy ; socks=socks5://foopy ", |
| 389 | |
| 390 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 391 | nullptr, |
| 392 | "PROXY httpproxy:80", |
| 393 | "PROXY httpsproxy:80", |
| 394 | nullptr, |
| 395 | "SOCKS5 foopy:1080", |
| 396 | }, |
| 397 | |
| 398 | // Include unsupported schemes -- they are discarded. |
| 399 | { |
| 400 | "crazy=foopy ; foo=bar ; https=myhttpsproxy", |
| 401 | |
| 402 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 403 | nullptr, |
| 404 | nullptr, |
| 405 | "PROXY myhttpsproxy:80", |
| 406 | nullptr, |
| 407 | nullptr, |
| 408 | }, |
| 409 | |
| 410 | // direct:// as first option for a scheme. |
| 411 | { |
| 412 | "http=direct://,myhttpproxy; https=direct://", |
| 413 | |
| 414 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 415 | nullptr, |
| 416 | "DIRECT;PROXY myhttpproxy:80", |
| 417 | "DIRECT", |
| 418 | nullptr, |
| 419 | nullptr, |
| 420 | }, |
| 421 | |
| 422 | // direct:// as a second option for a scheme. |
| 423 | { |
| 424 | "http=myhttpproxy,direct://", |
| 425 | |
| 426 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 427 | nullptr, |
| 428 | "PROXY myhttpproxy:80;DIRECT", |
| 429 | nullptr, |
| 430 | nullptr, |
| 431 | nullptr, |
| 432 | }, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 433 | |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 434 | }; |
| 435 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 436 | ProxyConfig config; |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 437 | |
Avi Drissman | 4365a478 | 2018-12-28 19:26:24 | [diff] [blame] | 438 | for (size_t i = 0; i < base::size(tests); ++i) { |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 439 | config.proxy_rules().ParseFromString(tests[i].proxy_rules); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 440 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 441 | EXPECT_EQ(tests[i].type, config.proxy_rules().type); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 442 | ExpectProxyServerEquals(tests[i].single_proxy, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 443 | config.proxy_rules().single_proxies); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 444 | ExpectProxyServerEquals(tests[i].proxy_for_http, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 445 | config.proxy_rules().proxies_for_http); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 446 | ExpectProxyServerEquals(tests[i].proxy_for_https, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 447 | config.proxy_rules().proxies_for_https); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 448 | ExpectProxyServerEquals(tests[i].proxy_for_ftp, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 449 | config.proxy_rules().proxies_for_ftp); |
[email protected] | 2b6ed3dc | 2010-08-25 06:03:48 | [diff] [blame] | 450 | ExpectProxyServerEquals(tests[i].fallback_proxy, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 451 | config.proxy_rules().fallback_proxies); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 452 | } |
| 453 | } |
[email protected] | ab501a6a | 2009-05-12 15:07:50 | [diff] [blame] | 454 | |
[email protected] | db8ff91 | 2012-06-12 23:32:51 | [diff] [blame] | 455 | TEST(ProxyConfigTest, ProxyRulesSetBypassFlag) { |
| 456 | // Test whether the did_bypass_proxy() flag is set in proxy info correctly. |
| 457 | ProxyConfig::ProxyRules rules; |
| 458 | ProxyInfo result; |
| 459 | |
| 460 | rules.ParseFromString("http=httpproxy:80"); |
| 461 | rules.bypass_rules.AddRuleFromString(".com"); |
| 462 | |
| 463 | rules.Apply(GURL("http://example.com"), &result); |
| 464 | EXPECT_TRUE(result.is_direct_only()); |
| 465 | EXPECT_TRUE(result.did_bypass_proxy()); |
| 466 | |
| 467 | rules.Apply(GURL("http://example.org"), &result); |
| 468 | EXPECT_FALSE(result.is_direct()); |
| 469 | EXPECT_FALSE(result.did_bypass_proxy()); |
| 470 | |
| 471 | // Try with reversed bypass rules. |
| 472 | rules.reverse_bypass = true; |
| 473 | |
| 474 | rules.Apply(GURL("http://example.org"), &result); |
| 475 | EXPECT_TRUE(result.is_direct_only()); |
| 476 | EXPECT_TRUE(result.did_bypass_proxy()); |
| 477 | |
| 478 | rules.Apply(GURL("http://example.com"), &result); |
| 479 | EXPECT_FALSE(result.is_direct()); |
| 480 | EXPECT_FALSE(result.did_bypass_proxy()); |
| 481 | } |
| 482 | |
ricea | bec9560 | 2014-10-30 05:05:07 | [diff] [blame] | 483 | static const char kWsUrl[] = "ws://example.com/echo"; |
| 484 | static const char kWssUrl[] = "wss://example.com/echo"; |
| 485 | |
| 486 | class ProxyConfigWebSocketTest : public ::testing::Test { |
| 487 | protected: |
| 488 | void ParseFromString(const std::string& rules) { |
| 489 | rules_.ParseFromString(rules); |
| 490 | } |
| 491 | void Apply(const GURL& gurl) { rules_.Apply(gurl, &info_); } |
| 492 | std::string ToPacString() const { return info_.ToPacString(); } |
| 493 | |
| 494 | static GURL WsUrl() { return GURL(kWsUrl); } |
| 495 | static GURL WssUrl() { return GURL(kWssUrl); } |
| 496 | |
| 497 | ProxyConfig::ProxyRules rules_; |
| 498 | ProxyInfo info_; |
| 499 | }; |
| 500 | |
| 501 | // If a single proxy is set for all protocols, WebSocket uses it. |
| 502 | TEST_F(ProxyConfigWebSocketTest, UsesProxy) { |
| 503 | ParseFromString("proxy:3128"); |
| 504 | Apply(WsUrl()); |
| 505 | EXPECT_EQ("PROXY proxy:3128", ToPacString()); |
| 506 | } |
| 507 | |
Eric Roman | 875754c | 2019-10-02 16:51:20 | [diff] [blame^] | 508 | // See RFC6455 Section 4.1. item 3, "_Proxy Usage_". Note that this favors a |
| 509 | // SOCKSv4 proxy (although technically the spec only notes SOCKSv5). |
| 510 | TEST_F(ProxyConfigWebSocketTest, PrefersSocksV4) { |
ricea | bec9560 | 2014-10-30 05:05:07 | [diff] [blame] | 511 | ParseFromString( |
| 512 | "http=proxy:3128 ; https=sslproxy:3128 ; socks=socksproxy:1080"); |
| 513 | Apply(WsUrl()); |
| 514 | EXPECT_EQ("SOCKS socksproxy:1080", ToPacString()); |
| 515 | } |
| 516 | |
Eric Roman | 875754c | 2019-10-02 16:51:20 | [diff] [blame^] | 517 | // See RFC6455 Section 4.1. item 3, "_Proxy Usage_". |
| 518 | TEST_F(ProxyConfigWebSocketTest, PrefersSocksV5) { |
| 519 | ParseFromString( |
| 520 | "http=proxy:3128 ; https=sslproxy:3128 ; socks=socks5://socksproxy:1080"); |
| 521 | Apply(WsUrl()); |
| 522 | EXPECT_EQ("SOCKS5 socksproxy:1080", ToPacString()); |
| 523 | } |
| 524 | |
ricea | bec9560 | 2014-10-30 05:05:07 | [diff] [blame] | 525 | TEST_F(ProxyConfigWebSocketTest, PrefersHttpsToHttp) { |
| 526 | ParseFromString("http=proxy:3128 ; https=sslproxy:3128"); |
| 527 | Apply(WssUrl()); |
| 528 | EXPECT_EQ("PROXY sslproxy:3128", ToPacString()); |
| 529 | } |
| 530 | |
Eric Roman | 875754c | 2019-10-02 16:51:20 | [diff] [blame^] | 531 | // Tests when a proxy-per-url-scheme configuration was used, and proxies are |
| 532 | // specified for http://, https://, and a fallback proxy (non-SOCKS). |
| 533 | // Even though the fallback proxy is not SOCKS, it is still favored over the |
| 534 | // proxy for http://* and https://*. |
| 535 | TEST_F(ProxyConfigWebSocketTest, PrefersNonSocksFallbackOverHttps) { |
| 536 | // The notation for "socks=" is abused to set the "fallback proxy". |
| 537 | ParseFromString( |
| 538 | "http=proxy:3128 ; https=sslproxy:3128; socks=https://httpsproxy"); |
| 539 | EXPECT_EQ("HTTPS httpsproxy:443", rules_.fallback_proxies.ToPacString()); |
| 540 | Apply(WssUrl()); |
| 541 | EXPECT_EQ("HTTPS httpsproxy:443", ToPacString()); |
| 542 | } |
| 543 | |
| 544 | // Tests when a proxy-per-url-scheme configuration was used, and the fallback |
| 545 | // proxy is a non-SOCKS proxy, and no proxy was given for https://* or |
| 546 | // http://*. The fallback proxy is used. |
| 547 | TEST_F(ProxyConfigWebSocketTest, UsesNonSocksFallbackProxy) { |
| 548 | // The notation for "socks=" is abused to set the "fallback proxy". |
| 549 | ParseFromString("ftp=ftpproxy:3128; socks=https://httpsproxy"); |
| 550 | EXPECT_EQ("HTTPS httpsproxy:443", rules_.fallback_proxies.ToPacString()); |
| 551 | Apply(WssUrl()); |
| 552 | EXPECT_EQ("HTTPS httpsproxy:443", ToPacString()); |
| 553 | } |
| 554 | |
ricea | bec9560 | 2014-10-30 05:05:07 | [diff] [blame] | 555 | TEST_F(ProxyConfigWebSocketTest, PrefersHttpsEvenForWs) { |
| 556 | ParseFromString("http=proxy:3128 ; https=sslproxy:3128"); |
| 557 | Apply(WsUrl()); |
| 558 | EXPECT_EQ("PROXY sslproxy:3128", ToPacString()); |
| 559 | } |
| 560 | |
| 561 | TEST_F(ProxyConfigWebSocketTest, PrefersHttpToDirect) { |
| 562 | ParseFromString("http=proxy:3128"); |
| 563 | Apply(WssUrl()); |
| 564 | EXPECT_EQ("PROXY proxy:3128", ToPacString()); |
| 565 | } |
| 566 | |
| 567 | TEST_F(ProxyConfigWebSocketTest, IgnoresFtpProxy) { |
| 568 | ParseFromString("ftp=ftpproxy:3128"); |
| 569 | Apply(WssUrl()); |
| 570 | EXPECT_EQ("DIRECT", ToPacString()); |
| 571 | } |
| 572 | |
| 573 | TEST_F(ProxyConfigWebSocketTest, ObeysBypassRules) { |
| 574 | ParseFromString("http=proxy:3128 ; https=sslproxy:3128"); |
| 575 | rules_.bypass_rules.AddRuleFromString(".chromium.org"); |
| 576 | Apply(GURL("wss://codereview.chromium.org/feed")); |
| 577 | EXPECT_EQ("DIRECT", ToPacString()); |
| 578 | } |
| 579 | |
| 580 | TEST_F(ProxyConfigWebSocketTest, ObeysLocalBypass) { |
| 581 | ParseFromString("http=proxy:3128 ; https=sslproxy:3128"); |
| 582 | rules_.bypass_rules.AddRuleFromString("<local>"); |
| 583 | Apply(GURL("ws://localhost/feed")); |
| 584 | EXPECT_EQ("DIRECT", ToPacString()); |
| 585 | } |
| 586 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 587 | } // namespace |
| 588 | } // namespace net |