[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/proxy/proxy_config.h" |
[email protected] | ab501a6a | 2009-05-12 15:07:50 | [diff] [blame] | 6 | #include "net/proxy/proxy_config_service_common_unittest.h" |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 7 | #include "net/proxy/proxy_info.h" |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 8 | #include "testing/gtest/include/gtest/gtest.h" |
| 9 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 10 | namespace net { |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 11 | namespace { |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 12 | |
| 13 | void ExpectProxyServerEquals(const char* expectation, |
| 14 | const ProxyServer& proxy_server) { |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 15 | if (expectation == NULL) { |
| 16 | EXPECT_FALSE(proxy_server.is_valid()); |
| 17 | } else { |
| 18 | EXPECT_EQ(expectation, proxy_server.ToURI()); |
| 19 | } |
| 20 | } |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 21 | |
| 22 | TEST(ProxyConfigTest, Equals) { |
| 23 | // Test |ProxyConfig::auto_detect|. |
| 24 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 25 | ProxyConfig config1; |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 26 | config1.set_auto_detect(true); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 27 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 28 | ProxyConfig config2; |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 29 | config2.set_auto_detect(false); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 30 | |
| 31 | EXPECT_FALSE(config1.Equals(config2)); |
| 32 | EXPECT_FALSE(config2.Equals(config1)); |
| 33 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 34 | config2.set_auto_detect(true); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 35 | |
| 36 | EXPECT_TRUE(config1.Equals(config2)); |
| 37 | EXPECT_TRUE(config2.Equals(config1)); |
| 38 | |
| 39 | // Test |ProxyConfig::pac_url|. |
| 40 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 41 | config2.set_pac_url(GURL("http://wpad/wpad.dat")); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 42 | |
| 43 | EXPECT_FALSE(config1.Equals(config2)); |
| 44 | EXPECT_FALSE(config2.Equals(config1)); |
| 45 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 46 | config1.set_pac_url(GURL("http://wpad/wpad.dat")); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 47 | |
| 48 | EXPECT_TRUE(config1.Equals(config2)); |
| 49 | EXPECT_TRUE(config2.Equals(config1)); |
| 50 | |
| 51 | // Test |ProxyConfig::proxy_rules|. |
| 52 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 53 | config2.proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY; |
| 54 | config2.proxy_rules().single_proxy = |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 55 | ProxyServer::FromURI("myproxy:80", ProxyServer::SCHEME_HTTP); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 56 | |
| 57 | EXPECT_FALSE(config1.Equals(config2)); |
| 58 | EXPECT_FALSE(config2.Equals(config1)); |
| 59 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 60 | config1.proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY; |
| 61 | config1.proxy_rules().single_proxy = |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 62 | ProxyServer::FromURI("myproxy:100", ProxyServer::SCHEME_HTTP); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 63 | |
| 64 | EXPECT_FALSE(config1.Equals(config2)); |
| 65 | EXPECT_FALSE(config2.Equals(config1)); |
| 66 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 67 | config1.proxy_rules().single_proxy = |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 68 | ProxyServer::FromURI("myproxy", ProxyServer::SCHEME_HTTP); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 69 | |
| 70 | EXPECT_TRUE(config1.Equals(config2)); |
| 71 | EXPECT_TRUE(config2.Equals(config1)); |
| 72 | |
[email protected] | 7541206c | 2010-02-19 20:24:06 | [diff] [blame] | 73 | // Test |ProxyConfig::bypass_rules|. |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 74 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 75 | config2.proxy_rules().bypass_rules.AddRuleFromString("*.google.com"); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 76 | |
| 77 | EXPECT_FALSE(config1.Equals(config2)); |
| 78 | EXPECT_FALSE(config2.Equals(config1)); |
| 79 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 80 | config1.proxy_rules().bypass_rules.AddRuleFromString("*.google.com"); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 81 | |
| 82 | EXPECT_TRUE(config1.Equals(config2)); |
| 83 | EXPECT_TRUE(config2.Equals(config1)); |
[email protected] | a48bf4a | 2010-06-14 18:24:53 | [diff] [blame] | 84 | |
| 85 | // Test |ProxyConfig::proxy_rules.reverse_bypass|. |
| 86 | |
| 87 | config2.proxy_rules().reverse_bypass = true; |
| 88 | |
| 89 | EXPECT_FALSE(config1.Equals(config2)); |
| 90 | EXPECT_FALSE(config2.Equals(config1)); |
| 91 | |
| 92 | config1.proxy_rules().reverse_bypass = true; |
| 93 | |
| 94 | EXPECT_TRUE(config1.Equals(config2)); |
| 95 | EXPECT_TRUE(config2.Equals(config1)); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | TEST(ProxyConfigTest, ParseProxyRules) { |
| 99 | const struct { |
| 100 | const char* proxy_rules; |
| 101 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 102 | ProxyConfig::ProxyRules::Type type; |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 103 | const char* single_proxy; |
| 104 | const char* proxy_for_http; |
| 105 | const char* proxy_for_https; |
| 106 | const char* proxy_for_ftp; |
[email protected] | 2b6ed3dc | 2010-08-25 06:03:48 | [diff] [blame] | 107 | const char* fallback_proxy; |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 108 | } tests[] = { |
| 109 | // One HTTP proxy for all schemes. |
| 110 | { |
| 111 | "myproxy:80", |
| 112 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 113 | ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 114 | "myproxy:80", |
| 115 | NULL, |
| 116 | NULL, |
| 117 | NULL, |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 118 | NULL, |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 119 | }, |
| 120 | |
| 121 | // Only specify a proxy server for "http://" urls. |
| 122 | { |
| 123 | "http=myproxy:80", |
| 124 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 125 | ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 126 | NULL, |
| 127 | "myproxy:80", |
| 128 | NULL, |
| 129 | NULL, |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 130 | NULL, |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 131 | }, |
| 132 | |
| 133 | // Specify an HTTP proxy for "ftp://" and a SOCKS proxy for "https://" urls. |
| 134 | { |
| 135 | "ftp=ftp-proxy ; https=socks4://foopy", |
| 136 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 137 | ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 138 | NULL, |
| 139 | NULL, |
| 140 | "socks4://foopy:1080", |
| 141 | "ftp-proxy:80", |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 142 | NULL, |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 143 | }, |
| 144 | |
| 145 | // Give a scheme-specific proxy as well as a non-scheme specific. |
| 146 | // The first entry "foopy" takes precedance marking this list as |
| 147 | // TYPE_SINGLE_PROXY. |
| 148 | { |
| 149 | "foopy ; ftp=ftp-proxy", |
| 150 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 151 | ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY, |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 152 | "foopy:80", |
| 153 | NULL, |
| 154 | NULL, |
| 155 | NULL, |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 156 | NULL, |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 157 | }, |
| 158 | |
| 159 | // 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 |
| 161 | // TYPE_PROXY_PER_SCHEME. |
| 162 | { |
| 163 | "ftp=ftp-proxy ; foopy", |
| 164 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 165 | ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 166 | NULL, |
| 167 | NULL, |
| 168 | NULL, |
| 169 | "ftp-proxy:80", |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 170 | NULL, |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 171 | }, |
| 172 | |
| 173 | // Include duplicate entries -- last one wins. |
| 174 | { |
| 175 | "ftp=ftp1 ; ftp=ftp2 ; ftp=ftp3", |
| 176 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 177 | ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 178 | NULL, |
| 179 | NULL, |
| 180 | NULL, |
| 181 | "ftp3:80", |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 182 | NULL, |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 183 | }, |
| 184 | |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 185 | // Only SOCKS proxy present, others being blank. |
| 186 | { |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 187 | "socks=foopy", |
| 188 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 189 | ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 190 | NULL, |
| 191 | NULL, |
| 192 | NULL, |
| 193 | NULL, |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 194 | "socks4://foopy:1080", |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 195 | }, |
| 196 | |
| 197 | // SOCKS proxy present along with other proxies too |
| 198 | { |
| 199 | "http=httpproxy ; https=httpsproxy ; ftp=ftpproxy ; socks=foopy ", |
| 200 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 201 | ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 202 | NULL, |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 203 | "httpproxy:80", |
| 204 | "httpsproxy:80", |
| 205 | "ftpproxy:80", |
| 206 | "socks4://foopy:1080", |
[email protected] | 3cd1724 | 2009-06-23 02:59:02 | [diff] [blame] | 207 | }, |
| 208 | |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 209 | // SOCKS proxy (with modifier) present along with some proxies |
| 210 | // (FTP being blank) |
| 211 | { |
| 212 | "http=httpproxy ; https=httpsproxy ; socks=socks5://foopy ", |
| 213 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 214 | ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 215 | NULL, |
| 216 | "httpproxy:80", |
| 217 | "httpsproxy:80", |
| 218 | NULL, |
| 219 | "socks5://foopy:1080", |
| 220 | }, |
| 221 | |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 222 | // Include unsupported schemes -- they are discarded. |
| 223 | { |
| 224 | "crazy=foopy ; foo=bar ; https=myhttpsproxy", |
| 225 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 226 | ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME, |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 227 | NULL, |
| 228 | NULL, |
| 229 | "myhttpsproxy:80", |
| 230 | NULL, |
[email protected] | 87a102b | 2009-07-14 05:23:30 | [diff] [blame] | 231 | NULL, |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 232 | }, |
| 233 | }; |
| 234 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 235 | ProxyConfig config; |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 236 | |
| 237 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 238 | config.proxy_rules().ParseFromString(tests[i].proxy_rules); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 239 | |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 240 | EXPECT_EQ(tests[i].type, config.proxy_rules().type); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 241 | ExpectProxyServerEquals(tests[i].single_proxy, |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 242 | config.proxy_rules().single_proxy); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 243 | ExpectProxyServerEquals(tests[i].proxy_for_http, |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 244 | config.proxy_rules().proxy_for_http); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 245 | ExpectProxyServerEquals(tests[i].proxy_for_https, |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 246 | config.proxy_rules().proxy_for_https); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 247 | ExpectProxyServerEquals(tests[i].proxy_for_ftp, |
[email protected] | ed4ed0f | 2010-02-24 00:20:48 | [diff] [blame] | 248 | config.proxy_rules().proxy_for_ftp); |
[email protected] | 2b6ed3dc | 2010-08-25 06:03:48 | [diff] [blame] | 249 | ExpectProxyServerEquals(tests[i].fallback_proxy, |
| 250 | config.proxy_rules().fallback_proxy); |
[email protected] | 5b45aec0 | 2009-03-31 01:03:23 | [diff] [blame] | 251 | } |
| 252 | } |
[email protected] | ab501a6a | 2009-05-12 15:07:50 | [diff] [blame] | 253 | |
[email protected] | 3dfd29f | 2009-09-06 00:48:37 | [diff] [blame] | 254 | } // namespace |
| 255 | } // namespace net |
| 256 | |