Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/proxy/proxy_config.h" | 5 #include "net/proxy/proxy_config.h" |
| 6 #include "net/proxy/proxy_config_service_common_unittest.h" | 6 #include "net/proxy/proxy_config_service_common_unittest.h" |
| 7 #include "net/proxy/proxy_info.h" | 7 #include "net/proxy/proxy_info.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 namespace net { | 10 namespace net { |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 void ExpectProxyServerEquals(const char* expectation, | 13 void ExpectProxyServerEquals(const char* expectation, |
| 14 const ProxyServer& proxy_server) { | 14 const ProxyList& proxy_servers) { |
| 15 if (expectation == NULL) { | 15 if (expectation == NULL) { |
| 16 EXPECT_FALSE(proxy_server.is_valid()); | 16 EXPECT_TRUE(proxy_servers.IsEmpty()); |
| 17 } else { | 17 } else { |
| 18 EXPECT_EQ(expectation, proxy_server.ToURI()); | 18 EXPECT_EQ(expectation, proxy_servers.ToPacString()); |
| 19 } | 19 } |
| 20 } | 20 } |
| 21 | 21 |
| 22 TEST(ProxyConfigTest, Equals) { | 22 TEST(ProxyConfigTest, Equals) { |
| 23 // Test |ProxyConfig::auto_detect|. | 23 // Test |ProxyConfig::auto_detect|. |
| 24 | 24 |
| 25 ProxyConfig config1; | 25 ProxyConfig config1; |
| 26 config1.set_auto_detect(true); | 26 config1.set_auto_detect(true); |
| 27 | 27 |
| 28 ProxyConfig config2; | 28 ProxyConfig config2; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 44 EXPECT_FALSE(config2.Equals(config1)); | 44 EXPECT_FALSE(config2.Equals(config1)); |
| 45 | 45 |
| 46 config1.set_pac_url(GURL("http://wpad/wpad.dat")); | 46 config1.set_pac_url(GURL("http://wpad/wpad.dat")); |
| 47 | 47 |
| 48 EXPECT_TRUE(config1.Equals(config2)); | 48 EXPECT_TRUE(config1.Equals(config2)); |
| 49 EXPECT_TRUE(config2.Equals(config1)); | 49 EXPECT_TRUE(config2.Equals(config1)); |
| 50 | 50 |
| 51 // Test |ProxyConfig::proxy_rules|. | 51 // Test |ProxyConfig::proxy_rules|. |
| 52 | 52 |
| 53 config2.proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY; | 53 config2.proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY; |
| 54 config2.proxy_rules().single_proxy = | 54 config2.proxy_rules().single_proxies.SetSingleProxyServer( |
| 55 ProxyServer::FromURI("myproxy:80", ProxyServer::SCHEME_HTTP); | 55 ProxyServer::FromURI("myproxy:80", ProxyServer::SCHEME_HTTP)); |
| 56 | 56 |
| 57 EXPECT_FALSE(config1.Equals(config2)); | 57 EXPECT_FALSE(config1.Equals(config2)); |
| 58 EXPECT_FALSE(config2.Equals(config1)); | 58 EXPECT_FALSE(config2.Equals(config1)); |
| 59 | 59 |
| 60 config1.proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY; | 60 config1.proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY; |
| 61 config1.proxy_rules().single_proxy = | 61 config1.proxy_rules().single_proxies.SetSingleProxyServer( |
| 62 ProxyServer::FromURI("myproxy:100", ProxyServer::SCHEME_HTTP); | 62 ProxyServer::FromURI("myproxy:100", ProxyServer::SCHEME_HTTP)); |
| 63 | 63 |
| 64 EXPECT_FALSE(config1.Equals(config2)); | 64 EXPECT_FALSE(config1.Equals(config2)); |
| 65 EXPECT_FALSE(config2.Equals(config1)); | 65 EXPECT_FALSE(config2.Equals(config1)); |
| 66 | 66 |
| 67 config1.proxy_rules().single_proxy = | 67 config1.proxy_rules().single_proxies.SetSingleProxyServer( |
| 68 ProxyServer::FromURI("myproxy", ProxyServer::SCHEME_HTTP); | 68 ProxyServer::FromURI("myproxy", ProxyServer::SCHEME_HTTP)); |
| 69 | 69 |
| 70 EXPECT_TRUE(config1.Equals(config2)); | 70 EXPECT_TRUE(config1.Equals(config2)); |
| 71 EXPECT_TRUE(config2.Equals(config1)); | 71 EXPECT_TRUE(config2.Equals(config1)); |
| 72 | 72 |
| 73 // Test |ProxyConfig::bypass_rules|. | 73 // Test |ProxyConfig::bypass_rules|. |
| 74 | 74 |
| 75 config2.proxy_rules().bypass_rules.AddRuleFromString("*.google.com"); | 75 config2.proxy_rules().bypass_rules.AddRuleFromString("*.google.com"); |
| 76 | 76 |
| 77 EXPECT_FALSE(config1.Equals(config2)); | 77 EXPECT_FALSE(config1.Equals(config2)); |
| 78 EXPECT_FALSE(config2.Equals(config1)); | 78 EXPECT_FALSE(config2.Equals(config1)); |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 93 | 93 |
| 94 EXPECT_TRUE(config1.Equals(config2)); | 94 EXPECT_TRUE(config1.Equals(config2)); |
| 95 EXPECT_TRUE(config2.Equals(config1)); | 95 EXPECT_TRUE(config2.Equals(config1)); |
| 96 } | 96 } |
| 97 | 97 |
| 98 TEST(ProxyConfigTest, ParseProxyRules) { | 98 TEST(ProxyConfigTest, ParseProxyRules) { |
| 99 const struct { | 99 const struct { |
| 100 const char* proxy_rules; | 100 const char* proxy_rules; |
| 101 | 101 |
| 102 ProxyConfig::ProxyRules::Type type; | 102 ProxyConfig::ProxyRules::Type type; |
| 103 // These will be PAC-stle strings, eg 'PROXY foo.com' | |
| 103 const char* single_proxy; | 104 const char* single_proxy; |
| 104 const char* proxy_for_http; | 105 const char* proxy_for_http; |
| 105 const char* proxy_for_https; | 106 const char* proxy_for_https; |
| 106 const char* proxy_for_ftp; | 107 const char* proxy_for_ftp; |
| 107 const char* fallback_proxy; | 108 const char* fallback_proxy; |
| 108 } tests[] = { | 109 } tests[] = { |
| 109 // One HTTP proxy for all schemes. | 110 // One HTTP proxy for all schemes. |
| 110 { | 111 { |
| 111 "myproxy:80", | 112 "myproxy:80", |
| 112 | 113 |
| 113 ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, | 114 ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, |
| 114 "myproxy:80", | 115 "PROXY myproxy:80", |
| 115 NULL, | 116 NULL, |
| 116 NULL, | 117 NULL, |
| 117 NULL, | 118 NULL, |
| 118 NULL, | 119 NULL, |
| 119 }, | 120 }, |
| 120 | 121 |
| 122 // Multiple HTTP proxies for all schemes. | |
| 123 { | |
| 124 "myproxy:80,https://myotherproxy", | |
| 125 | |
| 126 ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, | |
| 127 "PROXY myproxy:80;HTTPS myotherproxy:443", | |
| 128 NULL, | |
| 129 NULL, | |
| 130 NULL, | |
| 131 NULL, | |
| 132 }, | |
| 133 | |
|
eroman
2013/03/06 23:26:32
nit: remove empty line
marq_use_my_chromium_address
2013/03/07 00:34:29
Done.
| |
| 134 | |
| 121 // Only specify a proxy server for "http://" urls. | 135 // Only specify a proxy server for "http://" urls. |
| 122 { | 136 { |
| 123 "http=myproxy:80", | 137 "http=myproxy:80", |
| 124 | 138 |
| 125 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, | 139 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
| 126 NULL, | 140 NULL, |
| 127 "myproxy:80", | 141 "PROXY myproxy:80", |
| 128 NULL, | 142 NULL, |
| 129 NULL, | 143 NULL, |
| 130 NULL, | 144 NULL, |
| 131 }, | 145 }, |
| 132 | 146 |
| 133 // Specify an HTTP proxy for "ftp://" and a SOCKS proxy for "https://" urls. | 147 // Specify an HTTP proxy for "ftp://" and a SOCKS proxy for "https://" urls. |
| 134 { | 148 { |
| 135 "ftp=ftp-proxy ; https=socks4://foopy", | 149 "ftp=ftp-proxy ; https=socks4://foopy", |
| 136 | 150 |
| 137 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, | 151 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
| 138 NULL, | 152 NULL, |
| 139 NULL, | 153 NULL, |
| 140 "socks4://foopy:1080", | 154 "SOCKS foopy:1080", |
| 141 "ftp-proxy:80", | 155 "PROXY ftp-proxy:80", |
| 142 NULL, | 156 NULL, |
| 143 }, | 157 }, |
| 144 | 158 |
| 145 // Give a scheme-specific proxy as well as a non-scheme specific. | 159 // Give a scheme-specific proxy as well as a non-scheme specific. |
| 146 // The first entry "foopy" takes precedance marking this list as | 160 // The first entry "foopy" takes precedance marking this list as |
| 147 // TYPE_SINGLE_PROXY. | 161 // TYPE_SINGLE_PROXY. |
| 148 { | 162 { |
| 149 "foopy ; ftp=ftp-proxy", | 163 "foopy ; ftp=ftp-proxy", |
| 150 | 164 |
| 151 ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, | 165 ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, |
| 152 "foopy:80", | 166 "PROXY foopy:80", |
| 153 NULL, | 167 NULL, |
| 154 NULL, | 168 NULL, |
| 155 NULL, | 169 NULL, |
| 156 NULL, | 170 NULL, |
| 157 }, | 171 }, |
| 158 | 172 |
| 159 // Give a scheme-specific proxy as well as a non-scheme specific. | 173 // Give a scheme-specific proxy as well as a non-scheme specific. |
| 160 // The first entry "ftp=ftp-proxy" takes precedance marking this list as | 174 // The first entry "ftp=ftp-proxy" takes precedance marking this list as |
| 161 // TYPE_PROXY_PER_SCHEME. | 175 // TYPE_PROXY_PER_SCHEME. |
| 162 { | 176 { |
| 163 "ftp=ftp-proxy ; foopy", | 177 "ftp=ftp-proxy ; foopy", |
| 164 | 178 |
| 165 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, | 179 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
| 166 NULL, | 180 NULL, |
| 167 NULL, | 181 NULL, |
| 168 NULL, | 182 NULL, |
| 169 "ftp-proxy:80", | 183 "PROXY ftp-proxy:80", |
| 170 NULL, | 184 NULL, |
| 171 }, | 185 }, |
| 172 | 186 |
| 173 // Include duplicate entries -- last one wins. | 187 // Include a list of entries for a single scheme. |
| 174 { | 188 { |
| 175 "ftp=ftp1 ; ftp=ftp2 ; ftp=ftp3", | 189 "ftp=ftp1,ftp2,ftp3", |
| 176 | 190 |
| 177 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, | 191 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
| 178 NULL, | 192 NULL, |
| 179 NULL, | 193 NULL, |
| 180 NULL, | 194 NULL, |
| 181 "ftp3:80", | 195 "PROXY ftp1:80;PROXY ftp2:80;PROXY ftp3:80", |
| 182 NULL, | 196 NULL, |
| 183 }, | 197 }, |
| 184 | 198 |
| 199 // Include multiple entries for the same scheme -- they accumulate. | |
| 200 { | |
| 201 "http=http1,http2; http=http3", | |
| 202 | |
| 203 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, | |
| 204 NULL, | |
| 205 "PROXY http1:80;PROXY http2:80;PROXY http3:80", | |
| 206 NULL, | |
| 207 NULL, | |
| 208 NULL, | |
| 209 }, | |
| 210 | |
| 211 // Include lists of entries for multiple schemes. | |
| 212 { | |
| 213 "ftp=ftp1,ftp2,ftp3 ; http=http1,http2; ", | |
| 214 | |
| 215 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, | |
| 216 NULL, | |
| 217 "PROXY http1:80;PROXY http2:80", | |
| 218 NULL, | |
| 219 "PROXY ftp1:80;PROXY ftp2:80;PROXY ftp3:80", | |
| 220 NULL, | |
| 221 }, | |
| 222 | |
| 223 // Include non-default proxy schemes. | |
| 224 { | |
| 225 "http=https://secure_proxy; ftp=socks4://socks_proxy; https=socks://foo", | |
| 226 | |
| 227 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, | |
| 228 NULL, | |
| 229 "HTTPS secure_proxy:443", | |
| 230 "SOCKS5 foo:1080", | |
| 231 "SOCKS socks_proxy:1080", | |
| 232 NULL, | |
| 233 }, | |
| 234 | |
| 185 // Only SOCKS proxy present, others being blank. | 235 // Only SOCKS proxy present, others being blank. |
| 186 { | 236 { |
| 187 "socks=foopy", | 237 "socks=foopy", |
| 188 | 238 |
| 189 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, | 239 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
| 190 NULL, | 240 NULL, |
| 191 NULL, | 241 NULL, |
| 192 NULL, | 242 NULL, |
| 193 NULL, | 243 NULL, |
| 194 "socks4://foopy:1080", | 244 "SOCKS foopy:1080", |
| 195 }, | 245 }, |
| 196 | 246 |
| 197 // SOCKS proxy present along with other proxies too | 247 // SOCKS proxy present along with other proxies too |
| 198 { | 248 { |
| 199 "http=httpproxy ; https=httpsproxy ; ftp=ftpproxy ; socks=foopy ", | 249 "http=httpproxy ; https=httpsproxy ; ftp=ftpproxy ; socks=foopy ", |
| 200 | 250 |
| 201 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, | 251 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
| 202 NULL, | 252 NULL, |
| 203 "httpproxy:80", | 253 "PROXY httpproxy:80", |
| 204 "httpsproxy:80", | 254 "PROXY httpsproxy:80", |
| 205 "ftpproxy:80", | 255 "PROXY ftpproxy:80", |
| 206 "socks4://foopy:1080", | 256 "SOCKS foopy:1080", |
| 207 }, | 257 }, |
| 208 | 258 |
| 209 // SOCKS proxy (with modifier) present along with some proxies | 259 // SOCKS proxy (with modifier) present along with some proxies |
| 210 // (FTP being blank) | 260 // (FTP being blank) |
| 211 { | 261 { |
| 212 "http=httpproxy ; https=httpsproxy ; socks=socks5://foopy ", | 262 "http=httpproxy ; https=httpsproxy ; socks=socks5://foopy ", |
| 213 | 263 |
| 214 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, | 264 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
| 215 NULL, | 265 NULL, |
| 216 "httpproxy:80", | 266 "PROXY httpproxy:80", |
| 217 "httpsproxy:80", | 267 "PROXY httpsproxy:80", |
| 218 NULL, | 268 NULL, |
| 219 "socks5://foopy:1080", | 269 "SOCKS5 foopy:1080", |
| 220 }, | 270 }, |
| 221 | 271 |
| 222 // Include unsupported schemes -- they are discarded. | 272 // Include unsupported schemes -- they are discarded. |
| 223 { | 273 { |
| 224 "crazy=foopy ; foo=bar ; https=myhttpsproxy", | 274 "crazy=foopy ; foo=bar ; https=myhttpsproxy", |
| 225 | 275 |
| 226 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, | 276 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
| 227 NULL, | 277 NULL, |
| 228 NULL, | 278 NULL, |
| 229 "myhttpsproxy:80", | 279 "PROXY myhttpsproxy:80", |
| 230 NULL, | 280 NULL, |
| 231 NULL, | 281 NULL, |
| 232 }, | 282 }, |
| 283 | |
| 284 // direct:// as first option for a scheme. | |
| 285 { | |
| 286 "http=direct://,myhttpproxy; https=direct://", | |
| 287 | |
| 288 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, | |
| 289 NULL, | |
| 290 "DIRECT;PROXY myhttpproxy:80", | |
| 291 "DIRECT", | |
| 292 NULL, | |
| 293 NULL, | |
| 294 }, | |
| 295 | |
| 296 // direct:// as a second option for a scheme. | |
| 297 { | |
| 298 "http=myhttpproxy,direct://", | |
| 299 | |
| 300 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, | |
| 301 NULL, | |
| 302 "PROXY myhttpproxy:80;DIRECT", | |
| 303 NULL, | |
| 304 NULL, | |
| 305 NULL, | |
| 306 }, | |
| 307 | |
| 233 }; | 308 }; |
| 234 | 309 |
| 235 ProxyConfig config; | 310 ProxyConfig config; |
| 236 | 311 |
| 237 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 312 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 238 config.proxy_rules().ParseFromString(tests[i].proxy_rules); | 313 config.proxy_rules().ParseFromString(tests[i].proxy_rules); |
| 239 | 314 |
| 240 EXPECT_EQ(tests[i].type, config.proxy_rules().type); | 315 EXPECT_EQ(tests[i].type, config.proxy_rules().type); |
| 241 ExpectProxyServerEquals(tests[i].single_proxy, | 316 ExpectProxyServerEquals(tests[i].single_proxy, |
| 242 config.proxy_rules().single_proxy); | 317 config.proxy_rules().single_proxies); |
| 243 ExpectProxyServerEquals(tests[i].proxy_for_http, | 318 ExpectProxyServerEquals(tests[i].proxy_for_http, |
| 244 config.proxy_rules().proxy_for_http); | 319 config.proxy_rules().proxies_for_http); |
| 245 ExpectProxyServerEquals(tests[i].proxy_for_https, | 320 ExpectProxyServerEquals(tests[i].proxy_for_https, |
| 246 config.proxy_rules().proxy_for_https); | 321 config.proxy_rules().proxies_for_https); |
| 247 ExpectProxyServerEquals(tests[i].proxy_for_ftp, | 322 ExpectProxyServerEquals(tests[i].proxy_for_ftp, |
| 248 config.proxy_rules().proxy_for_ftp); | 323 config.proxy_rules().proxies_for_ftp); |
| 249 ExpectProxyServerEquals(tests[i].fallback_proxy, | 324 ExpectProxyServerEquals(tests[i].fallback_proxy, |
| 250 config.proxy_rules().fallback_proxy); | 325 config.proxy_rules().fallback_proxies); |
| 251 } | 326 } |
| 252 } | 327 } |
| 253 | 328 |
| 254 TEST(ProxyConfigTest, ProxyRulesSetBypassFlag) { | 329 TEST(ProxyConfigTest, ProxyRulesSetBypassFlag) { |
| 255 // Test whether the did_bypass_proxy() flag is set in proxy info correctly. | 330 // Test whether the did_bypass_proxy() flag is set in proxy info correctly. |
| 256 ProxyConfig::ProxyRules rules; | 331 ProxyConfig::ProxyRules rules; |
| 257 ProxyInfo result; | 332 ProxyInfo result; |
| 258 | 333 |
| 259 rules.ParseFromString("http=httpproxy:80"); | 334 rules.ParseFromString("http=httpproxy:80"); |
| 260 rules.bypass_rules.AddRuleFromString(".com"); | 335 rules.bypass_rules.AddRuleFromString(".com"); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 274 EXPECT_TRUE(result.is_direct_only()); | 349 EXPECT_TRUE(result.is_direct_only()); |
| 275 EXPECT_TRUE(result.did_bypass_proxy()); | 350 EXPECT_TRUE(result.did_bypass_proxy()); |
| 276 | 351 |
| 277 rules.Apply(GURL("http://example.com"), &result); | 352 rules.Apply(GURL("http://example.com"), &result); |
| 278 EXPECT_FALSE(result.is_direct()); | 353 EXPECT_FALSE(result.is_direct()); |
| 279 EXPECT_FALSE(result.did_bypass_proxy()); | 354 EXPECT_FALSE(result.did_bypass_proxy()); |
| 280 } | 355 } |
| 281 | 356 |
| 282 } // namespace | 357 } // namespace |
| 283 } // namespace net | 358 } // namespace net |
| OLD | NEW |