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"
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
) {
18 const char* const input_uri
;
19 const char* const expected_uri
;
20 ProxyServer::Scheme expected_scheme
;
21 const char* const expected_host
;
23 const char* const expected_pac_string
;
26 {"foopy:10", // No scheme.
28 ProxyServer::SCHEME_HTTP
,
32 {"http://foopy", // No port.
34 ProxyServer::SCHEME_HTTP
,
40 ProxyServer::SCHEME_HTTP
,
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",
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",
57 "PROXY [3ffe:2a00:100:7031::1]:80"},
58 {"http://[::192.9.5.5]",
60 ProxyServer::SCHEME_HTTP
,
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",
69 "PROXY [::FFFF:129.144.52.38]:80"},
72 {"socks4://foopy", // No port.
73 "socks4://foopy:1080",
74 ProxyServer::SCHEME_SOCKS4
,
80 ProxyServer::SCHEME_SOCKS4
,
86 {"socks5://foopy", // No port.
87 "socks5://foopy:1080",
88 ProxyServer::SCHEME_SOCKS5
,
94 ProxyServer::SCHEME_SOCKS5
,
99 // SOCKS proxy URIs (should default to SOCKS5)
100 {"socks://foopy", // No port.
101 "socks5://foopy:1080",
102 ProxyServer::SCHEME_SOCKS5
,
105 "SOCKS5 foopy:1080"},
108 ProxyServer::SCHEME_SOCKS5
,
114 {"https://foopy", // No port
116 ProxyServer::SCHEME_HTTPS
,
120 {"https://foopy:10", // Non-standard port
122 ProxyServer::SCHEME_HTTPS
,
126 {"https://1.2.3.4:10", // IP Address
127 "https://1.2.3.4:10",
128 ProxyServer::SCHEME_HTTPS
,
134 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
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
[] = {
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
[] = {
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
) {
201 const char* const input_pac
;
202 const char* const expected_uri
;
217 "PROXY foopy", // No port.
222 "socks4://foopy:1080",
226 "socks4://foopy:1080",
230 "socks5://foopy:1080",
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
) {
275 const char* const server1
;
276 const char* const server2
;
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
;
289 { // Port is different.
294 { // Host is different.
299 { // Scheme is different.
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
) {
316 EXPECT_TRUE(server1
< server2
);
317 EXPECT_FALSE(server2
< server1
);
318 EXPECT_FALSE(server2
== server1
);
321 EXPECT_FALSE(server1
< server2
);
322 EXPECT_FALSE(server2
< server1
);
323 EXPECT_TRUE(server2
== server1
);
326 EXPECT_FALSE(server1
< server2
);
327 EXPECT_TRUE(server2
< server1
);
328 EXPECT_FALSE(server2
== server1
);
331 FAIL() << "Invalid expectation. Can be only -1, 0, 1";