blob: 9e50e3b4c5b77f8015134903e610f1d6969d05fb [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"
6#include "testing/gtest/include/gtest/gtest.h"
7
8namespace {
9static void ExpectProxyServerEquals(const char* expectation,
10 const net::ProxyServer& proxy_server) {
11 if (expectation == NULL) {
12 EXPECT_FALSE(proxy_server.is_valid());
13 } else {
14 EXPECT_EQ(expectation, proxy_server.ToURI());
15 }
16}
17}
18
19TEST(ProxyConfigTest, Equals) {
20 // Test |ProxyConfig::auto_detect|.
21
22 net::ProxyConfig config1;
23 config1.auto_detect = true;
24
25 net::ProxyConfig config2;
26 config2.auto_detect = false;
27
28 EXPECT_FALSE(config1.Equals(config2));
29 EXPECT_FALSE(config2.Equals(config1));
30
31 config2.auto_detect = true;
32
33 EXPECT_TRUE(config1.Equals(config2));
34 EXPECT_TRUE(config2.Equals(config1));
35
36 // Test |ProxyConfig::pac_url|.
37
38 config2.pac_url = GURL("http://wpad/wpad.dat");
39
40 EXPECT_FALSE(config1.Equals(config2));
41 EXPECT_FALSE(config2.Equals(config1));
42
43 config1.pac_url = GURL("http://wpad/wpad.dat");
44
45 EXPECT_TRUE(config1.Equals(config2));
46 EXPECT_TRUE(config2.Equals(config1));
47
48 // Test |ProxyConfig::proxy_rules|.
49
50 config2.proxy_rules.type = net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
51 config2.proxy_rules.single_proxy = net::ProxyServer::FromURI("myproxy:80");
52
53 EXPECT_FALSE(config1.Equals(config2));
54 EXPECT_FALSE(config2.Equals(config1));
55
56 config1.proxy_rules.type = net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY;
57 config1.proxy_rules.single_proxy = net::ProxyServer::FromURI("myproxy:100");
58
59 EXPECT_FALSE(config1.Equals(config2));
60 EXPECT_FALSE(config2.Equals(config1));
61
62 config1.proxy_rules.single_proxy = net::ProxyServer::FromURI("myproxy");
63
64 EXPECT_TRUE(config1.Equals(config2));
65 EXPECT_TRUE(config2.Equals(config1));
66
67 // Test |ProxyConfig::proxy_bypass|.
68
69 config2.proxy_bypass.push_back("*.google.com");
70
71 EXPECT_FALSE(config1.Equals(config2));
72 EXPECT_FALSE(config2.Equals(config1));
73
74 config1.proxy_bypass.push_back("*.google.com");
75
76 EXPECT_TRUE(config1.Equals(config2));
77 EXPECT_TRUE(config2.Equals(config1));
78
79 // Test |ProxyConfig::proxy_bypass_local_names|.
80
81 config1.proxy_bypass_local_names = true;
82
83 EXPECT_FALSE(config1.Equals(config2));
84 EXPECT_FALSE(config2.Equals(config1));
85
86 config2.proxy_bypass_local_names = true;
87
88 EXPECT_TRUE(config1.Equals(config2));
89 EXPECT_TRUE(config2.Equals(config1));
90}
91
92TEST(ProxyConfigTest, ParseProxyRules) {
93 const struct {
94 const char* proxy_rules;
95
96 net::ProxyConfig::ProxyRules::Type type;
97 const char* single_proxy;
98 const char* proxy_for_http;
99 const char* proxy_for_https;
100 const char* proxy_for_ftp;
101 } tests[] = {
102 // One HTTP proxy for all schemes.
103 {
104 "myproxy:80",
105
106 net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY,
107 "myproxy:80",
108 NULL,
109 NULL,
110 NULL,
111 },
112
113 // Only specify a proxy server for "http://" urls.
114 {
115 "http=myproxy:80",
116
117 net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
118 NULL,
119 "myproxy:80",
120 NULL,
121 NULL,
122 },
123
124 // Specify an HTTP proxy for "ftp://" and a SOCKS proxy for "https://" urls.
125 {
126 "ftp=ftp-proxy ; https=socks4://foopy",
127
128 net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
129 NULL,
130 NULL,
131 "socks4://foopy:1080",
132 "ftp-proxy:80",
133 },
134
135 // Give a scheme-specific proxy as well as a non-scheme specific.
136 // The first entry "foopy" takes precedance marking this list as
137 // TYPE_SINGLE_PROXY.
138 {
139 "foopy ; ftp=ftp-proxy",
140
141 net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY,
142 "foopy:80",
143 NULL,
144 NULL,
145 NULL,
146 },
147
148 // Give a scheme-specific proxy as well as a non-scheme specific.
149 // The first entry "ftp=ftp-proxy" takes precedance marking this list as
150 // TYPE_PROXY_PER_SCHEME.
151 {
152 "ftp=ftp-proxy ; foopy",
153
154 net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
155 NULL,
156 NULL,
157 NULL,
158 "ftp-proxy:80",
159 },
160
161 // Include duplicate entries -- last one wins.
162 {
163 "ftp=ftp1 ; ftp=ftp2 ; ftp=ftp3",
164
165 net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
166 NULL,
167 NULL,
168 NULL,
169 "ftp3:80",
170 },
171
172 // Include unsupported schemes -- they are discarded.
173 {
174 "crazy=foopy ; foo=bar ; https=myhttpsproxy",
175
176 net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME,
177 NULL,
178 NULL,
179 "myhttpsproxy:80",
180 NULL,
181 },
182 };
183
184 net::ProxyConfig config;
185
186 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
187 config.proxy_rules.ParseFromString(tests[i].proxy_rules);
188
189 EXPECT_EQ(tests[i].type, config.proxy_rules.type);
190 ExpectProxyServerEquals(tests[i].single_proxy,
191 config.proxy_rules.single_proxy);
192 ExpectProxyServerEquals(tests[i].proxy_for_http,
193 config.proxy_rules.proxy_for_http);
194 ExpectProxyServerEquals(tests[i].proxy_for_https,
195 config.proxy_rules.proxy_for_https);
196 ExpectProxyServerEquals(tests[i].proxy_for_ftp,
197 config.proxy_rules.proxy_for_ftp);
198 }
199}