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 // Test the creation of ProxyServer using ProxyServer::FromURI, which parses
10 // inputs of the form [<scheme>"://"]<host>[":"<port>]. Verify that each part
11 // was labelled correctly, and the accessors all give the right data.
12 TEST(ProxyServerTest
, FromURI
) {
14 const char* const input_uri
;
15 const char* const expected_uri
;
16 net::ProxyServer::Scheme expected_scheme
;
17 const char* const expected_host
;
19 const char* const expected_pac_string
;
23 "foopy:10", // No scheme.
25 net::ProxyServer::SCHEME_HTTP
,
31 "http://foopy", // No port.
33 net::ProxyServer::SCHEME_HTTP
,
41 net::ProxyServer::SCHEME_HTTP
,
47 // IPv6 HTTP proxy URIs:
49 "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:10", // No scheme.
50 "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:10",
51 net::ProxyServer::SCHEME_HTTP
,
52 "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210",
54 "PROXY [FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:10"
57 "http://[3ffe:2a00:100:7031::1]", // No port.
58 "[3ffe:2a00:100:7031::1]:80",
59 net::ProxyServer::SCHEME_HTTP
,
60 "3ffe:2a00:100:7031::1",
62 "PROXY [3ffe:2a00:100:7031::1]:80"
65 "http://[::192.9.5.5]",
67 net::ProxyServer::SCHEME_HTTP
,
70 "PROXY [::192.9.5.5]:80"
73 "http://[::FFFF:129.144.52.38]:80",
74 "[::FFFF:129.144.52.38]:80",
75 net::ProxyServer::SCHEME_HTTP
,
76 "::FFFF:129.144.52.38",
78 "PROXY [::FFFF:129.144.52.38]:80"
83 "socks4://foopy", // No port.
84 "socks4://foopy:1080",
85 net::ProxyServer::SCHEME_SOCKS4
,
93 net::ProxyServer::SCHEME_SOCKS4
,
101 "socks5://foopy", // No port.
102 "socks5://foopy:1080",
103 net::ProxyServer::SCHEME_SOCKS5
,
111 net::ProxyServer::SCHEME_SOCKS5
,
117 // SOCKS proxy URIs (should default to SOCKS5)
119 "socks://foopy", // No port.
120 "socks5://foopy:1080",
121 net::ProxyServer::SCHEME_SOCKS5
,
129 net::ProxyServer::SCHEME_SOCKS5
,
137 "https://foopy", // No port
139 net::ProxyServer::SCHEME_HTTPS
,
145 "https://foopy:10", // Non-standard port
147 net::ProxyServer::SCHEME_HTTPS
,
153 "https://1.2.3.4:10", // IP Address
154 "https://1.2.3.4:10",
155 net::ProxyServer::SCHEME_HTTPS
,
162 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
163 net::ProxyServer uri
=
164 net::ProxyServer::FromURI(tests
[i
].input_uri
,
165 net::ProxyServer::SCHEME_HTTP
);
166 EXPECT_TRUE(uri
.is_valid());
167 EXPECT_FALSE(uri
.is_direct());
168 EXPECT_EQ(tests
[i
].expected_uri
, uri
.ToURI());
169 EXPECT_EQ(tests
[i
].expected_scheme
, uri
.scheme());
170 EXPECT_EQ(tests
[i
].expected_host
, uri
.host_port_pair().host());
171 EXPECT_EQ(tests
[i
].expected_port
, uri
.host_port_pair().port());
172 EXPECT_EQ(tests
[i
].expected_pac_string
, uri
.ToPacString());
176 TEST(ProxyServerTest
, DefaultConstructor
) {
177 net::ProxyServer proxy_server
;
178 EXPECT_FALSE(proxy_server
.is_valid());
181 // Test parsing of the special URI form "direct://". Analagous to the "DIRECT"
182 // entry in a PAC result.
183 TEST(ProxyServerTest
, Direct
) {
184 net::ProxyServer uri
=
185 net::ProxyServer::FromURI("direct://", net::ProxyServer::SCHEME_HTTP
);
186 EXPECT_TRUE(uri
.is_valid());
187 EXPECT_TRUE(uri
.is_direct());
188 EXPECT_EQ("direct://", uri
.ToURI());
189 EXPECT_EQ("DIRECT", uri
.ToPacString());
192 // Test parsing some invalid inputs.
193 TEST(ProxyServerTest
, Invalid
) {
194 const char* const tests
[] = {
197 "dddf:", // not a valid port
198 "dddd:d", // not a valid port
199 "http://", // not a valid host/port.
200 "direct://xyz", // direct is not allowed a host/port.
201 "http:/", // ambiguous, but will fail because of bad port.
202 "http:", // ambiguous, but will fail because of bad port.
205 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
206 net::ProxyServer uri
=
207 net::ProxyServer::FromURI(tests
[i
], net::ProxyServer::SCHEME_HTTP
);
208 EXPECT_FALSE(uri
.is_valid());
209 EXPECT_FALSE(uri
.is_direct());
210 EXPECT_FALSE(uri
.is_http());
211 EXPECT_FALSE(uri
.is_socks());
215 // Test that LWS (SP | HT) is disregarded from the ends.
216 TEST(ProxyServerTest
, Whitespace
) {
217 const char* const tests
[] = {
223 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
224 net::ProxyServer uri
=
225 net::ProxyServer::FromURI(tests
[i
], net::ProxyServer::SCHEME_HTTP
);
226 EXPECT_EQ("foopy:80", uri
.ToURI());
230 // Test parsing a ProxyServer from a PAC representation.
231 TEST(ProxyServerTest
, FromPACString
) {
233 const char* const input_pac
;
234 const char* const expected_uri
;
249 "PROXY foopy", // No port.
254 "socks4://foopy:1080",
258 "socks4://foopy:1080",
262 "socks5://foopy:1080",
282 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
283 net::ProxyServer uri
= net::ProxyServer::FromPacString(tests
[i
].input_pac
);
284 EXPECT_TRUE(uri
.is_valid());
285 EXPECT_EQ(tests
[i
].expected_uri
, uri
.ToURI());
289 // Test parsing a ProxyServer from an invalid PAC representation.
290 TEST(ProxyServerTest
, FromPACStringInvalid
) {
291 const char* const tests
[] = {
292 "PROXY", // missing host/port.
293 "HTTPS", // missing host/port.
294 "SOCKS", // missing host/port.
295 "DIRECT foopy:10", // direct cannot have host/port.
298 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
299 net::ProxyServer uri
= net::ProxyServer::FromPacString(tests
[i
]);
300 EXPECT_FALSE(uri
.is_valid());
304 TEST(ProxyServerTest
, ComparatorAndEquality
) {
307 const char* const server1
;
308 const char* const server2
;
311 // -1 means server1 is less than server2
312 // 0 means server1 equals server2
313 // 1 means server1 is greater than server2
314 int expected_comparison
;
321 { // Port is different.
326 { // Host is different.
331 { // Scheme is different.
338 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
339 // Parse the expected inputs to ProxyServer instances.
340 const net::ProxyServer server1
=
341 net::ProxyServer::FromURI(
342 tests
[i
].server1
, net::ProxyServer::SCHEME_HTTP
);
344 const net::ProxyServer server2
=
345 net::ProxyServer::FromURI(
346 tests
[i
].server2
, net::ProxyServer::SCHEME_HTTP
);
348 switch (tests
[i
].expected_comparison
) {
350 EXPECT_TRUE(server1
< server2
);
351 EXPECT_FALSE(server2
< server1
);
352 EXPECT_FALSE(server2
== server1
);
355 EXPECT_FALSE(server1
< server2
);
356 EXPECT_FALSE(server2
< server1
);
357 EXPECT_TRUE(server2
== server1
);
360 EXPECT_FALSE(server1
< server2
);
361 EXPECT_TRUE(server2
< server1
);
362 EXPECT_FALSE(server2
== server1
);
365 FAIL() << "Invalid expectation. Can be only -1, 0, 1";