Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / proxy / proxy_server_unittest.cc
blob25ad4345c6340d61f2a78a2135b39c4ce2c88135
1 // Copyright (c) 2010 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.
5 #include "base/basictypes.h"
6 #include "net/proxy/proxy_server.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace net {
11 namespace {
13 // Test the creation of ProxyServer using ProxyServer::FromURI, which parses
14 // inputs of the form [<scheme>"://"]<host>[":"<port>]. Verify that each part
15 // was labelled correctly, and the accessors all give the right data.
16 TEST(ProxyServerTest, FromURI) {
17 const struct {
18 const char* const input_uri;
19 const char* const expected_uri;
20 ProxyServer::Scheme expected_scheme;
21 const char* const expected_host;
22 int expected_port;
23 const char* const expected_pac_string;
24 } tests[] = {
25 // HTTP proxy URIs:
26 {"foopy:10", // No scheme.
27 "foopy:10",
28 ProxyServer::SCHEME_HTTP,
29 "foopy",
30 10,
31 "PROXY foopy:10"},
32 {"http://foopy", // No port.
33 "foopy:80",
34 ProxyServer::SCHEME_HTTP,
35 "foopy",
36 80,
37 "PROXY foopy:80"},
38 {"http://foopy:10",
39 "foopy:10",
40 ProxyServer::SCHEME_HTTP,
41 "foopy",
42 10,
43 "PROXY foopy:10"},
45 // IPv6 HTTP proxy URIs:
46 {"[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:10", // No scheme.
47 "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:10",
48 ProxyServer::SCHEME_HTTP,
49 "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210",
50 10,
51 "PROXY [FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:10"},
52 {"http://[3ffe:2a00:100:7031::1]", // No port.
53 "[3ffe:2a00:100:7031::1]:80",
54 ProxyServer::SCHEME_HTTP,
55 "3ffe:2a00:100:7031::1",
56 80,
57 "PROXY [3ffe:2a00:100:7031::1]:80"},
58 {"http://[::192.9.5.5]",
59 "[::192.9.5.5]:80",
60 ProxyServer::SCHEME_HTTP,
61 "::192.9.5.5",
62 80,
63 "PROXY [::192.9.5.5]:80"},
64 {"http://[::FFFF:129.144.52.38]:80",
65 "[::FFFF:129.144.52.38]:80",
66 ProxyServer::SCHEME_HTTP,
67 "::FFFF:129.144.52.38",
68 80,
69 "PROXY [::FFFF:129.144.52.38]:80"},
71 // SOCKS4 proxy URIs:
72 {"socks4://foopy", // No port.
73 "socks4://foopy:1080",
74 ProxyServer::SCHEME_SOCKS4,
75 "foopy",
76 1080,
77 "SOCKS foopy:1080"},
78 {"socks4://foopy:10",
79 "socks4://foopy:10",
80 ProxyServer::SCHEME_SOCKS4,
81 "foopy",
82 10,
83 "SOCKS foopy:10"},
85 // SOCKS5 proxy URIs
86 {"socks5://foopy", // No port.
87 "socks5://foopy:1080",
88 ProxyServer::SCHEME_SOCKS5,
89 "foopy",
90 1080,
91 "SOCKS5 foopy:1080"},
92 {"socks5://foopy:10",
93 "socks5://foopy:10",
94 ProxyServer::SCHEME_SOCKS5,
95 "foopy",
96 10,
97 "SOCKS5 foopy:10"},
99 // SOCKS proxy URIs (should default to SOCKS5)
100 {"socks://foopy", // No port.
101 "socks5://foopy:1080",
102 ProxyServer::SCHEME_SOCKS5,
103 "foopy",
104 1080,
105 "SOCKS5 foopy:1080"},
106 {"socks://foopy:10",
107 "socks5://foopy:10",
108 ProxyServer::SCHEME_SOCKS5,
109 "foopy",
111 "SOCKS5 foopy:10"},
113 // HTTPS proxy URIs:
114 {"https://foopy", // No port
115 "https://foopy:443",
116 ProxyServer::SCHEME_HTTPS,
117 "foopy",
118 443,
119 "HTTPS foopy:443"},
120 {"https://foopy:10", // Non-standard port
121 "https://foopy:10",
122 ProxyServer::SCHEME_HTTPS,
123 "foopy",
125 "HTTPS foopy:10"},
126 {"https://1.2.3.4:10", // IP Address
127 "https://1.2.3.4:10",
128 ProxyServer::SCHEME_HTTPS,
129 "1.2.3.4",
131 "HTTPS 1.2.3.4:10"},
134 for (size_t i = 0; i < arraysize(tests); ++i) {
135 ProxyServer uri =
136 ProxyServer::FromURI(tests[i].input_uri, ProxyServer::SCHEME_HTTP);
137 EXPECT_TRUE(uri.is_valid());
138 EXPECT_FALSE(uri.is_direct());
139 EXPECT_EQ(tests[i].expected_uri, uri.ToURI());
140 EXPECT_EQ(tests[i].expected_scheme, uri.scheme());
141 EXPECT_EQ(tests[i].expected_host, uri.host_port_pair().host());
142 EXPECT_EQ(tests[i].expected_port, uri.host_port_pair().port());
143 EXPECT_EQ(tests[i].expected_pac_string, uri.ToPacString());
147 TEST(ProxyServerTest, DefaultConstructor) {
148 ProxyServer proxy_server;
149 EXPECT_FALSE(proxy_server.is_valid());
152 // Test parsing of the special URI form "direct://". Analagous to the "DIRECT"
153 // entry in a PAC result.
154 TEST(ProxyServerTest, Direct) {
155 ProxyServer uri = ProxyServer::FromURI("direct://", ProxyServer::SCHEME_HTTP);
156 EXPECT_TRUE(uri.is_valid());
157 EXPECT_TRUE(uri.is_direct());
158 EXPECT_EQ("direct://", uri.ToURI());
159 EXPECT_EQ("DIRECT", uri.ToPacString());
162 // Test parsing some invalid inputs.
163 TEST(ProxyServerTest, Invalid) {
164 const char* const tests[] = {
166 " ",
167 "dddf:", // not a valid port
168 "dddd:d", // not a valid port
169 "http://", // not a valid host/port.
170 "direct://xyz", // direct is not allowed a host/port.
171 "http:/", // ambiguous, but will fail because of bad port.
172 "http:", // ambiguous, but will fail because of bad port.
175 for (size_t i = 0; i < arraysize(tests); ++i) {
176 ProxyServer uri = ProxyServer::FromURI(tests[i], ProxyServer::SCHEME_HTTP);
177 EXPECT_FALSE(uri.is_valid());
178 EXPECT_FALSE(uri.is_direct());
179 EXPECT_FALSE(uri.is_http());
180 EXPECT_FALSE(uri.is_socks());
184 // Test that LWS (SP | HT) is disregarded from the ends.
185 TEST(ProxyServerTest, Whitespace) {
186 const char* const tests[] = {
187 " foopy:80",
188 "foopy:80 \t",
189 " \tfoopy:80 ",
192 for (size_t i = 0; i < arraysize(tests); ++i) {
193 ProxyServer uri = ProxyServer::FromURI(tests[i], ProxyServer::SCHEME_HTTP);
194 EXPECT_EQ("foopy:80", uri.ToURI());
198 // Test parsing a ProxyServer from a PAC representation.
199 TEST(ProxyServerTest, FromPACString) {
200 const struct {
201 const char* const input_pac;
202 const char* const expected_uri;
203 } tests[] = {
205 "PROXY foopy:10",
206 "foopy:10",
209 " PROXY foopy:10 ",
210 "foopy:10",
213 "pRoXy foopy:10",
214 "foopy:10",
217 "PROXY foopy", // No port.
218 "foopy:80",
221 "socks foopy",
222 "socks4://foopy:1080",
225 "socks4 foopy",
226 "socks4://foopy:1080",
229 "socks5 foopy",
230 "socks5://foopy:1080",
233 "socks5 foopy:11",
234 "socks5://foopy:11",
237 " direct ",
238 "direct://",
241 "https foopy",
242 "https://foopy:443",
245 "https foopy:10",
246 "https://foopy:10",
250 for (size_t i = 0; i < arraysize(tests); ++i) {
251 ProxyServer uri = ProxyServer::FromPacString(tests[i].input_pac);
252 EXPECT_TRUE(uri.is_valid());
253 EXPECT_EQ(tests[i].expected_uri, uri.ToURI());
257 // Test parsing a ProxyServer from an invalid PAC representation.
258 TEST(ProxyServerTest, FromPACStringInvalid) {
259 const char* const tests[] = {
260 "PROXY", // missing host/port.
261 "HTTPS", // missing host/port.
262 "SOCKS", // missing host/port.
263 "DIRECT foopy:10", // direct cannot have host/port.
266 for (size_t i = 0; i < arraysize(tests); ++i) {
267 ProxyServer uri = ProxyServer::FromPacString(tests[i]);
268 EXPECT_FALSE(uri.is_valid());
272 TEST(ProxyServerTest, ComparatorAndEquality) {
273 struct {
274 // Inputs.
275 const char* const server1;
276 const char* const server2;
278 // Expectation.
279 // -1 means server1 is less than server2
280 // 0 means server1 equals server2
281 // 1 means server1 is greater than server2
282 int expected_comparison;
283 } tests[] = {
284 { // Equal.
285 "foo:11",
286 "http://foo:11",
289 { // Port is different.
290 "foo:333",
291 "foo:444",
294 { // Host is different.
295 "foo:33",
296 "bar:33",
299 { // Scheme is different.
300 "socks4://foo:33",
301 "http://foo:33",
306 for (size_t i = 0; i < arraysize(tests); ++i) {
307 // Parse the expected inputs to ProxyServer instances.
308 const ProxyServer server1 =
309 ProxyServer::FromURI(tests[i].server1, ProxyServer::SCHEME_HTTP);
311 const ProxyServer server2 =
312 ProxyServer::FromURI(tests[i].server2, ProxyServer::SCHEME_HTTP);
314 switch (tests[i].expected_comparison) {
315 case -1:
316 EXPECT_TRUE(server1 < server2);
317 EXPECT_FALSE(server2 < server1);
318 EXPECT_FALSE(server2 == server1);
319 break;
320 case 0:
321 EXPECT_FALSE(server1 < server2);
322 EXPECT_FALSE(server2 < server1);
323 EXPECT_TRUE(server2 == server1);
324 break;
325 case 1:
326 EXPECT_FALSE(server1 < server2);
327 EXPECT_TRUE(server2 < server1);
328 EXPECT_FALSE(server2 == server1);
329 break;
330 default:
331 FAIL() << "Invalid expectation. Can be only -1, 0, 1";
336 } // namespace
338 } // namespace net