blob: 1947ce4acf2510c389d10b4da362d59efe54c13e [file] [log] [blame]
[email protected]5b45aec02009-03-31 01:03:231// 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]ab501a6a2009-05-12 15:07:506#include "net/proxy/proxy_config_service_common_unittest.h"
[email protected]a48bf4a2010-06-14 18:24:537#include "net/proxy/proxy_info.h"
[email protected]5b45aec02009-03-31 01:03:238#include "testing/gtest/include/gtest/gtest.h"
9
[email protected]3dfd29f2009-09-06 00:48:3710namespace net {
[email protected]5b45aec02009-03-31 01:03:2311namespace {
[email protected]3dfd29f2009-09-06 00:48:3712
13void ExpectProxyServerEquals(const char* expectation,
14 const ProxyServer& proxy_server) {
[email protected]5b45aec02009-03-31 01:03:2315 if (expectation == NULL) {
16 EXPECT_FALSE(proxy_server.is_valid());
17 } else {
18 EXPECT_EQ(expectation, proxy_server.ToURI());
19 }
20}
[email protected]5b45aec02009-03-31 01:03:2321
22TEST(ProxyConfigTest, Equals) {
23 // Test |ProxyConfig::auto_detect|.
24
[email protected]3dfd29f2009-09-06 00:48:3725 ProxyConfig config1;
[email protected]ed4ed0f2010-02-24 00:20:4826 config1.set_auto_detect(true);
[email protected]5b45aec02009-03-31 01:03:2327
[email protected]3dfd29f2009-09-06 00:48:3728 ProxyConfig config2;
[email protected]ed4ed0f2010-02-24 00:20:4829 config2.set_auto_detect(false);
[email protected]5b45aec02009-03-31 01:03:2330
31 EXPECT_FALSE(config1.Equals(config2));
32 EXPECT_FALSE(config2.Equals(config1));
33
[email protected]ed4ed0f2010-02-24 00:20:4834 config2.set_auto_detect(true);
[email protected]5b45aec02009-03-31 01:03:2335
36 EXPECT_TRUE(config1.Equals(config2));
37 EXPECT_TRUE(config2.Equals(config1));
38
39 // Test |ProxyConfig::pac_url|.
40
[email protected]ed4ed0f2010-02-24 00:20:4841 config2.set_pac_url(GURL("http://wpad/wpad.dat"));
[email protected]5b45aec02009-03-31 01:03:2342
43 EXPECT_FALSE(config1.Equals(config2));
44 EXPECT_FALSE(config2.Equals(config1));
45
[email protected]ed4ed0f2010-02-24 00:20:4846 config1.set_pac_url(GURL("http://wpad/wpad.dat"));
[email protected]5b45aec02009-03-31 01:03:2347
48 EXPECT_TRUE(config1.Equals(config2));
49 EXPECT_TRUE(config2.Equals(config1));
50
51 // Test |ProxyConfig::proxy_rules|.
52
[email protected]ed4ed0f2010-02-24 00:20:4853 config2.proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
54 config2.proxy_rules().single_proxy =
[email protected]3dfd29f2009-09-06 00:48:3755 ProxyServer::FromURI("myproxy:80", ProxyServer::SCHEME_HTTP);
[email protected]5b45aec02009-03-31 01:03:2356
57 EXPECT_FALSE(config1.Equals(config2));
58 EXPECT_FALSE(config2.Equals(config1));
59
[email protected]ed4ed0f2010-02-24 00:20:4860 config1.proxy_rules().type = ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
61 config1.proxy_rules().single_proxy =
[email protected]3dfd29f2009-09-06 00:48:3762 ProxyServer::FromURI("myproxy:100", ProxyServer::SCHEME_HTTP);
[email protected]5b45aec02009-03-31 01:03:2363
64 EXPECT_FALSE(config1.Equals(config2));
65 EXPECT_FALSE(config2.Equals(config1));
66
[email protected]ed4ed0f2010-02-24 00:20:4867 config1.proxy_rules().single_proxy =
[email protected]3dfd29f2009-09-06 00:48:3768 ProxyServer::FromURI("myproxy", ProxyServer::SCHEME_HTTP);
[email protected]5b45aec02009-03-31 01:03:2369
70 EXPECT_TRUE(config1.Equals(config2));
71 EXPECT_TRUE(config2.Equals(config1));
72
[email protected]7541206c2010-02-19 20:24:0673 // Test |ProxyConfig::bypass_rules|.
[email protected]5b45aec02009-03-31 01:03:2374
[email protected]ed4ed0f2010-02-24 00:20:4875 config2.proxy_rules().bypass_rules.AddRuleFromString("*.google.com");
[email protected]5b45aec02009-03-31 01:03:2376
77 EXPECT_FALSE(config1.Equals(config2));
78 EXPECT_FALSE(config2.Equals(config1));
79
[email protected]ed4ed0f2010-02-24 00:20:4880 config1.proxy_rules().bypass_rules.AddRuleFromString("*.google.com");
[email protected]5b45aec02009-03-31 01:03:2381
82 EXPECT_TRUE(config1.Equals(config2));
83 EXPECT_TRUE(config2.Equals(config1));
[email protected]a48bf4a2010-06-14 18:24:5384
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]5b45aec02009-03-31 01:03:2396}
97
98TEST(ProxyConfigTest, ParseProxyRules) {
99 const struct {
100 const char* proxy_rules;
101
[email protected]3dfd29f2009-09-06 00:48:37102 ProxyConfig::ProxyRules::Type type;
[email protected]5b45aec02009-03-31 01:03:23103 const char* single_proxy;
104 const char* proxy_for_http;
105 const char* proxy_for_https;
106 const char* proxy_for_ftp;
[email protected]2b6ed3dc2010-08-25 06:03:48107 const char* fallback_proxy;
[email protected]5b45aec02009-03-31 01:03:23108 } tests[] = {
109 // One HTTP proxy for all schemes.
110 {
111 "myproxy:80",
112
[email protected]3dfd29f2009-09-06 00:48:37113 ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY,
[email protected]5b45aec02009-03-31 01:03:23114 "myproxy:80",
115 NULL,
116 NULL,
117 NULL,
[email protected]87a102b2009-07-14 05:23:30118 NULL,
[email protected]5b45aec02009-03-31 01:03:23119 },
120
121 // Only specify a proxy server for "http://" urls.
122 {
123 "http=myproxy:80",
124
[email protected]3dfd29f2009-09-06 00:48:37125 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
[email protected]5b45aec02009-03-31 01:03:23126 NULL,
127 "myproxy:80",
128 NULL,
129 NULL,
[email protected]87a102b2009-07-14 05:23:30130 NULL,
[email protected]5b45aec02009-03-31 01:03:23131 },
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]3dfd29f2009-09-06 00:48:37137 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
[email protected]5b45aec02009-03-31 01:03:23138 NULL,
139 NULL,
140 "socks4://foopy:1080",
141 "ftp-proxy:80",
[email protected]87a102b2009-07-14 05:23:30142 NULL,
[email protected]5b45aec02009-03-31 01:03:23143 },
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]3dfd29f2009-09-06 00:48:37151 ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY,
[email protected]5b45aec02009-03-31 01:03:23152 "foopy:80",
153 NULL,
154 NULL,
155 NULL,
[email protected]87a102b2009-07-14 05:23:30156 NULL,
[email protected]5b45aec02009-03-31 01:03:23157 },
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]3dfd29f2009-09-06 00:48:37165 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
[email protected]5b45aec02009-03-31 01:03:23166 NULL,
167 NULL,
168 NULL,
169 "ftp-proxy:80",
[email protected]87a102b2009-07-14 05:23:30170 NULL,
[email protected]5b45aec02009-03-31 01:03:23171 },
172
173 // Include duplicate entries -- last one wins.
174 {
175 "ftp=ftp1 ; ftp=ftp2 ; ftp=ftp3",
176
[email protected]3dfd29f2009-09-06 00:48:37177 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
[email protected]5b45aec02009-03-31 01:03:23178 NULL,
179 NULL,
180 NULL,
181 "ftp3:80",
[email protected]87a102b2009-07-14 05:23:30182 NULL,
[email protected]5b45aec02009-03-31 01:03:23183 },
184
[email protected]87a102b2009-07-14 05:23:30185 // Only SOCKS proxy present, others being blank.
186 {
[email protected]3cd17242009-06-23 02:59:02187 "socks=foopy",
188
[email protected]3dfd29f2009-09-06 00:48:37189 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
[email protected]87a102b2009-07-14 05:23:30190 NULL,
191 NULL,
192 NULL,
193 NULL,
[email protected]3cd17242009-06-23 02:59:02194 "socks4://foopy:1080",
[email protected]87a102b2009-07-14 05:23:30195 },
196
197 // SOCKS proxy present along with other proxies too
198 {
199 "http=httpproxy ; https=httpsproxy ; ftp=ftpproxy ; socks=foopy ",
200
[email protected]3dfd29f2009-09-06 00:48:37201 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
[email protected]3cd17242009-06-23 02:59:02202 NULL,
[email protected]87a102b2009-07-14 05:23:30203 "httpproxy:80",
204 "httpsproxy:80",
205 "ftpproxy:80",
206 "socks4://foopy:1080",
[email protected]3cd17242009-06-23 02:59:02207 },
208
[email protected]87a102b2009-07-14 05:23:30209 // 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]3dfd29f2009-09-06 00:48:37214 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
[email protected]87a102b2009-07-14 05:23:30215 NULL,
216 "httpproxy:80",
217 "httpsproxy:80",
218 NULL,
219 "socks5://foopy:1080",
220 },
221
[email protected]5b45aec02009-03-31 01:03:23222 // Include unsupported schemes -- they are discarded.
223 {
224 "crazy=foopy ; foo=bar ; https=myhttpsproxy",
225
[email protected]3dfd29f2009-09-06 00:48:37226 ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
[email protected]5b45aec02009-03-31 01:03:23227 NULL,
228 NULL,
229 "myhttpsproxy:80",
230 NULL,
[email protected]87a102b2009-07-14 05:23:30231 NULL,
[email protected]5b45aec02009-03-31 01:03:23232 },
233 };
234
[email protected]3dfd29f2009-09-06 00:48:37235 ProxyConfig config;
[email protected]5b45aec02009-03-31 01:03:23236
237 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
[email protected]ed4ed0f2010-02-24 00:20:48238 config.proxy_rules().ParseFromString(tests[i].proxy_rules);
[email protected]5b45aec02009-03-31 01:03:23239
[email protected]ed4ed0f2010-02-24 00:20:48240 EXPECT_EQ(tests[i].type, config.proxy_rules().type);
[email protected]5b45aec02009-03-31 01:03:23241 ExpectProxyServerEquals(tests[i].single_proxy,
[email protected]ed4ed0f2010-02-24 00:20:48242 config.proxy_rules().single_proxy);
[email protected]5b45aec02009-03-31 01:03:23243 ExpectProxyServerEquals(tests[i].proxy_for_http,
[email protected]ed4ed0f2010-02-24 00:20:48244 config.proxy_rules().proxy_for_http);
[email protected]5b45aec02009-03-31 01:03:23245 ExpectProxyServerEquals(tests[i].proxy_for_https,
[email protected]ed4ed0f2010-02-24 00:20:48246 config.proxy_rules().proxy_for_https);
[email protected]5b45aec02009-03-31 01:03:23247 ExpectProxyServerEquals(tests[i].proxy_for_ftp,
[email protected]ed4ed0f2010-02-24 00:20:48248 config.proxy_rules().proxy_for_ftp);
[email protected]2b6ed3dc2010-08-25 06:03:48249 ExpectProxyServerEquals(tests[i].fallback_proxy,
250 config.proxy_rules().fallback_proxy);
[email protected]5b45aec02009-03-31 01:03:23251 }
252}
[email protected]ab501a6a2009-05-12 15:07:50253
[email protected]3dfd29f2009-09-06 00:48:37254} // namespace
255} // namespace net
256