[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" |
Daniel Cheng | 5feb16f | 2022-02-28 06:52:07 | [diff] [blame] | 6 | |
Eric Roman | 4c0456a | 2019-09-18 17:04:34 | [diff] [blame] | 7 | #include "base/json/json_writer.h" |
Eric Roman | 4c0456a | 2019-09-18 17:04:34 | [diff] [blame] | 8 | #include "base/values.h" |
Eric Orth | 5ccc3f0 | 2021-09-23 00:01:57 | [diff] [blame] | 9 | #include "net/base/proxy_string_util.h" |
Lily Houghton | 582d462 | 2018-01-22 22:43:40 | [diff] [blame] | 10 | #include "net/proxy_resolution/proxy_config_service_common_unittest.h" |
| 11 | #include "net/proxy_resolution/proxy_info.h" |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
| 13 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 14 | namespace net { |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 15 | namespace { |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 16 | |
| 17 | void ExpectProxyServerEquals(const char* expectation, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 18 | const ProxyList& proxy_servers) { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 19 | if (expectation == nullptr) { |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 20 | EXPECT_TRUE(proxy_servers.IsEmpty()); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 21 | } else { |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 22 | EXPECT_EQ(expectation, proxy_servers.ToPacString()); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 23 | } |
| 24 | } |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 25 | |
| 26 | TEST(ProxyConfigTest, Equals) { |
| 27 | // Test |ProxyConfig::auto_detect|. |
| 28 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 29 | ProxyConfig config1; |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 30 | config1.set_auto_detect(true); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 31 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 32 | ProxyConfig config2; |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 33 | config2.set_auto_detect(false); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 34 | |
| 35 | EXPECT_FALSE(config1.Equals(config2)); |
| 36 | EXPECT_FALSE(config2.Equals(config1)); |
| 37 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 38 | config2.set_auto_detect(true); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 39 | |
| 40 | EXPECT_TRUE(config1.Equals(config2)); |
| 41 | EXPECT_TRUE(config2.Equals(config1)); |
| 42 | |
| 43 | // Test |ProxyConfig::pac_url|. |
| 44 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 45 | config2.set_pac_url(GURL("http://wpad/wpad.dat")); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 46 | |
| 47 | EXPECT_FALSE(config1.Equals(config2)); |
| 48 | EXPECT_FALSE(config2.Equals(config1)); |
| 49 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 50 | config1.set_pac_url(GURL("http://wpad/wpad.dat")); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 51 | |
| 52 | EXPECT_TRUE(config1.Equals(config2)); |
| 53 | EXPECT_TRUE(config2.Equals(config1)); |
| 54 | |
| 55 | // Test |ProxyConfig::proxy_rules|. |
| 56 | |
Lily Houghton | e6b617e | 2018-01-19 20:13:07 | [diff] [blame] | 57 | config2.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST; |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 58 | config2.proxy_rules().single_proxies.SetSingleProxyServer( |
Eric Orth | 5ccc3f0 | 2021-09-23 00:01:57 | [diff] [blame] | 59 | ProxyUriToProxyServer("myproxy:80", ProxyServer::SCHEME_HTTP)); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 60 | |
| 61 | EXPECT_FALSE(config1.Equals(config2)); |
| 62 | EXPECT_FALSE(config2.Equals(config1)); |
| 63 | |
Lily Houghton | e6b617e | 2018-01-19 20:13:07 | [diff] [blame] | 64 | config1.proxy_rules().type = ProxyConfig::ProxyRules::Type::PROXY_LIST; |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 65 | config1.proxy_rules().single_proxies.SetSingleProxyServer( |
Eric Orth | 5ccc3f0 | 2021-09-23 00:01:57 | [diff] [blame] | 66 | ProxyUriToProxyServer("myproxy:100", ProxyServer::SCHEME_HTTP)); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 67 | |
| 68 | EXPECT_FALSE(config1.Equals(config2)); |
| 69 | EXPECT_FALSE(config2.Equals(config1)); |
| 70 | |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 71 | config1.proxy_rules().single_proxies.SetSingleProxyServer( |
Eric Orth | 5ccc3f0 | 2021-09-23 00:01:57 | [diff] [blame] | 72 | ProxyUriToProxyServer("myproxy", ProxyServer::SCHEME_HTTP)); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 73 | |
| 74 | EXPECT_TRUE(config1.Equals(config2)); |
| 75 | EXPECT_TRUE(config2.Equals(config1)); |
| 76 | |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 77 | // Test |ProxyConfig::bypass_rules|. |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 78 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 79 | config2.proxy_rules().bypass_rules.AddRuleFromString("*.google.com"); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 80 | |
| 81 | EXPECT_FALSE(config1.Equals(config2)); |
| 82 | EXPECT_FALSE(config2.Equals(config1)); |
| 83 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 84 | config1.proxy_rules().bypass_rules.AddRuleFromString("*.google.com"); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 85 | |
| 86 | EXPECT_TRUE(config1.Equals(config2)); |
| 87 | EXPECT_TRUE(config2.Equals(config1)); |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 88 | |
| 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] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 100 | } |
| 101 | |
Eric Roman | 4c0456a | 2019-09-18 17:04:34 | [diff] [blame] | 102 | struct ProxyConfigToValueTestCase { |
| 103 | ProxyConfig config; |
| 104 | const char* expected_value_json; |
| 105 | }; |
| 106 | |
| 107 | class ProxyConfigToValueTest |
| 108 | : public ::testing::TestWithParam<ProxyConfigToValueTestCase> {}; |
| 109 | |
| 110 | TEST_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 | |
| 121 | ProxyConfigToValueTestCase GetTestCaseDirect() { |
| 122 | return {ProxyConfig::CreateDirect(), "{}"}; |
| 123 | } |
| 124 | |
| 125 | ProxyConfigToValueTestCase GetTestCaseAutoDetect() { |
| 126 | return {ProxyConfig::CreateAutoDetect(), "{\"auto_detect\":true}"}; |
| 127 | } |
| 128 | |
| 129 | ProxyConfigToValueTestCase 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 | |
| 137 | ProxyConfigToValueTestCase 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 | |
| 147 | ProxyConfigToValueTestCase 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 | |
| 156 | ProxyConfigToValueTestCase 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 | |
| 163 | ProxyConfigToValueTestCase 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 | |
| 174 | ProxyConfigToValueTestCase 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 | |
| 185 | ProxyConfigToValueTestCase 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 | |
| 200 | ProxyConfigToValueTestCase 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 | |
| 210 | INSTANTIATE_TEST_SUITE_P( |
Ilia Samsonov | 614b333 | 2019-11-20 22:31:54 | [diff] [blame] | 211 | All, |
Eric Roman | 4c0456a | 2019-09-18 17:04:34 | [diff] [blame] | 212 | 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] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 224 | TEST(ProxyConfigTest, ParseProxyRules) { |
| 225 | const struct { |
| 226 | const char* proxy_rules; |
| 227 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 228 | ProxyConfig::ProxyRules::Type type; |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 229 | // These will be PAC-stle strings, eg 'PROXY foo.com' |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 230 | const char* single_proxy; |
| 231 | const char* proxy_for_http; |
| 232 | const char* proxy_for_https; |
| 233 | const char* proxy_for_ftp; |
[email protected] | 2b6ed3dc | 2010-08-25 06:03:48 | [diff] [blame] | 234 | const char* fallback_proxy; |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 235 | } tests[] = { |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 236 | // One HTTP proxy for all schemes. |
| 237 | { |
| 238 | "myproxy:80", |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 239 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 240 | ProxyConfig::ProxyRules::Type::PROXY_LIST, |
| 241 | "PROXY myproxy:80", |
| 242 | nullptr, |
| 243 | nullptr, |
| 244 | nullptr, |
| 245 | nullptr, |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 246 | }, |
| 247 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 248 | // Multiple HTTP proxies for all schemes. |
| 249 | { |
| 250 | "myproxy:80,https://myotherproxy", |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 251 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 252 | ProxyConfig::ProxyRules::Type::PROXY_LIST, |
| 253 | "PROXY myproxy:80;HTTPS myotherproxy:443", |
| 254 | nullptr, |
| 255 | nullptr, |
| 256 | nullptr, |
| 257 | nullptr, |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 258 | }, |
| 259 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 260 | // Only specify a proxy server for "http://" urls. |
| 261 | { |
| 262 | "http=myproxy:80", |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 263 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 264 | ProxyConfig::ProxyRules::Type::PROXY_LIST_PER_SCHEME, |
| 265 | nullptr, |
| 266 | "PROXY myproxy:80", |
| 267 | nullptr, |
| 268 | nullptr, |
| 269 | nullptr, |
| 270 | }, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 271 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 272 | // Specify an HTTP proxy for "ftp://" and a SOCKS proxy for "https://" |
| 273 | // urls. |
| 274 | { |
| 275 | "ftp=ftp-proxy ; https=socks4://foopy", |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 276 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 277 | 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] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 284 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 285 | // 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] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 290 | |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 291 | 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] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 434 | |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 435 | }; |
| 436 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 437 | ProxyConfig config; |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 438 | |
Daniel Cheng | 5feb16f | 2022-02-28 06:52:07 | [diff] [blame] | 439 | for (size_t i = 0; i < std::size(tests); ++i) { |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 440 | config.proxy_rules().ParseFromString(tests[i].proxy_rules); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 441 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 442 | EXPECT_EQ(tests[i].type, config.proxy_rules().type); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 443 | ExpectProxyServerEquals(tests[i].single_proxy, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 444 | config.proxy_rules().single_proxies); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 445 | ExpectProxyServerEquals(tests[i].proxy_for_http, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 446 | config.proxy_rules().proxies_for_http); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 447 | ExpectProxyServerEquals(tests[i].proxy_for_https, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 448 | config.proxy_rules().proxies_for_https); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 449 | ExpectProxyServerEquals(tests[i].proxy_for_ftp, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 450 | config.proxy_rules().proxies_for_ftp); |
[email protected] | 2b6ed3dc | 2010-08-25 06:03:48 | [diff] [blame] | 451 | ExpectProxyServerEquals(tests[i].fallback_proxy, |
[email protected] | 2189e09 | 2013-03-16 18:02:02 | [diff] [blame] | 452 | config.proxy_rules().fallback_proxies); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 453 | } |
| 454 | } |
[email protected] | ab501a6a | 2009-05-12 15:07:50 | [diff] [blame] | 455 | |
[email protected] | db8ff91 | 2012-06-12 23:32:51 | [diff] [blame] | 456 | TEST(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 | |
ricea | bec9560 | 2014-10-30 05:05:07 | [diff] [blame] | 484 | static const char kWsUrl[] = "ws://example.com/echo"; |
| 485 | static const char kWssUrl[] = "wss://example.com/echo"; |
| 486 | |
| 487 | class 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. |
| 503 | TEST_F(ProxyConfigWebSocketTest, UsesProxy) { |
| 504 | ParseFromString("proxy:3128"); |
| 505 | Apply(WsUrl()); |
| 506 | EXPECT_EQ("PROXY proxy:3128", ToPacString()); |
| 507 | } |
| 508 | |
Eric Roman | 875754c | 2019-10-02 16:51:20 | [diff] [blame] | 509 | // See RFC6455 Section 4.1. item 3, "_Proxy Usage_". Note that this favors a |
| 510 | // SOCKSv4 proxy (although technically the spec only notes SOCKSv5). |
| 511 | TEST_F(ProxyConfigWebSocketTest, PrefersSocksV4) { |
ricea | bec9560 | 2014-10-30 05:05:07 | [diff] [blame] | 512 | ParseFromString( |
| 513 | "http=proxy:3128 ; https=sslproxy:3128 ; socks=socksproxy:1080"); |
| 514 | Apply(WsUrl()); |
| 515 | EXPECT_EQ("SOCKS socksproxy:1080", ToPacString()); |
| 516 | } |
| 517 | |
Eric Roman | 875754c | 2019-10-02 16:51:20 | [diff] [blame] | 518 | // See RFC6455 Section 4.1. item 3, "_Proxy Usage_". |
| 519 | TEST_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 | |
ricea | bec9560 | 2014-10-30 05:05:07 | [diff] [blame] | 526 | TEST_F(ProxyConfigWebSocketTest, PrefersHttpsToHttp) { |
| 527 | ParseFromString("http=proxy:3128 ; https=sslproxy:3128"); |
| 528 | Apply(WssUrl()); |
| 529 | EXPECT_EQ("PROXY sslproxy:3128", ToPacString()); |
| 530 | } |
| 531 | |
Eric Roman | 875754c | 2019-10-02 16:51:20 | [diff] [blame] | 532 | // 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://*. |
| 536 | TEST_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. |
| 548 | TEST_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 | |
ricea | bec9560 | 2014-10-30 05:05:07 | [diff] [blame] | 556 | TEST_F(ProxyConfigWebSocketTest, PrefersHttpsEvenForWs) { |
| 557 | ParseFromString("http=proxy:3128 ; https=sslproxy:3128"); |
| 558 | Apply(WsUrl()); |
| 559 | EXPECT_EQ("PROXY sslproxy:3128", ToPacString()); |
| 560 | } |
| 561 | |
| 562 | TEST_F(ProxyConfigWebSocketTest, PrefersHttpToDirect) { |
| 563 | ParseFromString("http=proxy:3128"); |
| 564 | Apply(WssUrl()); |
| 565 | EXPECT_EQ("PROXY proxy:3128", ToPacString()); |
| 566 | } |
| 567 | |
| 568 | TEST_F(ProxyConfigWebSocketTest, IgnoresFtpProxy) { |
| 569 | ParseFromString("ftp=ftpproxy:3128"); |
| 570 | Apply(WssUrl()); |
| 571 | EXPECT_EQ("DIRECT", ToPacString()); |
| 572 | } |
| 573 | |
| 574 | TEST_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 | |
| 581 | TEST_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] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 588 | } // namespace |
| 589 | } // namespace net |