• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Chromium Authors
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/base/proxy_string_util.h"
6 
7 #include "net/base/proxy_chain.h"
8 #include "net/base/proxy_server.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 
11 namespace net {
12 namespace {
13 
14 // Test the creation of ProxyServer using ProxyUriToProxyServer, which parses
15 // inputs of the form [<scheme>"://"]<host>[":"<port>]. Verify that each part
16 // was labelled correctly, and the accessors all give the right data.
TEST(ProxySpecificationUtilTest,ProxyUriToProxyServer)17 TEST(ProxySpecificationUtilTest, ProxyUriToProxyServer) {
18   const struct {
19     const char* const input_uri;
20     const char* const expected_uri;
21     ProxyServer::Scheme expected_scheme;
22     const char* const expected_host;
23     int expected_port;
24     const char* const expected_pac_string;
25   } tests[] = {
26       // HTTP proxy URIs:
27       {"foopy:10",  // No scheme.
28        "foopy:10", ProxyServer::SCHEME_HTTP, "foopy", 10, "PROXY foopy:10"},
29       {"http://foopy",  // No port.
30        "foopy:80", ProxyServer::SCHEME_HTTP, "foopy", 80, "PROXY foopy:80"},
31       {"http://foopy:10", "foopy:10", ProxyServer::SCHEME_HTTP, "foopy", 10,
32        "PROXY foopy:10"},
33 
34       // IPv6 HTTP proxy URIs:
35       {"[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:10",  // No scheme.
36        "[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:10", ProxyServer::SCHEME_HTTP,
37        "fedc:ba98:7654:3210:fedc:ba98:7654:3210", 10,
38        "PROXY [fedc:ba98:7654:3210:fedc:ba98:7654:3210]:10"},
39       {"http://[3ffe:2a00:100:7031::1]",  // No port.
40        "[3ffe:2a00:100:7031::1]:80", ProxyServer::SCHEME_HTTP,
41        "3ffe:2a00:100:7031::1", 80, "PROXY [3ffe:2a00:100:7031::1]:80"},
42 
43       // SOCKS4 proxy URIs:
44       {"socks4://foopy",  // No port.
45        "socks4://foopy:1080", ProxyServer::SCHEME_SOCKS4, "foopy", 1080,
46        "SOCKS foopy:1080"},
47       {"socks4://foopy:10", "socks4://foopy:10", ProxyServer::SCHEME_SOCKS4,
48        "foopy", 10, "SOCKS foopy:10"},
49 
50       // SOCKS5 proxy URIs:
51       {"socks5://foopy",  // No port.
52        "socks5://foopy:1080", ProxyServer::SCHEME_SOCKS5, "foopy", 1080,
53        "SOCKS5 foopy:1080"},
54       {"socks5://foopy:10", "socks5://foopy:10", ProxyServer::SCHEME_SOCKS5,
55        "foopy", 10, "SOCKS5 foopy:10"},
56 
57       // SOCKS proxy URIs (should default to SOCKS5)
58       {"socks://foopy",  // No port.
59        "socks5://foopy:1080", ProxyServer::SCHEME_SOCKS5, "foopy", 1080,
60        "SOCKS5 foopy:1080"},
61       {"socks://foopy:10", "socks5://foopy:10", ProxyServer::SCHEME_SOCKS5,
62        "foopy", 10, "SOCKS5 foopy:10"},
63 
64       // HTTPS proxy URIs:
65       {"https://foopy",  // No port
66        "https://foopy:443", ProxyServer::SCHEME_HTTPS, "foopy", 443,
67        "HTTPS foopy:443"},
68       {"https://foopy:10",  // Non-standard port
69        "https://foopy:10", ProxyServer::SCHEME_HTTPS, "foopy", 10,
70        "HTTPS foopy:10"},
71       {"https://1.2.3.4:10",  // IP Address
72        "https://1.2.3.4:10", ProxyServer::SCHEME_HTTPS, "1.2.3.4", 10,
73        "HTTPS 1.2.3.4:10"},
74 
75       // Hostname canonicalization:
76       {"[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:10",  // No scheme.
77        "[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:10", ProxyServer::SCHEME_HTTP,
78        "fedc:ba98:7654:3210:fedc:ba98:7654:3210", 10,
79        "PROXY [fedc:ba98:7654:3210:fedc:ba98:7654:3210]:10"},
80       {"http://[::192.9.5.5]", "[::c009:505]:80", ProxyServer::SCHEME_HTTP,
81        "::c009:505", 80, "PROXY [::c009:505]:80"},
82       {"http://[::FFFF:129.144.52.38]:80", "[::ffff:8190:3426]:80",
83        ProxyServer::SCHEME_HTTP, "::ffff:8190:3426", 80,
84        "PROXY [::ffff:8190:3426]:80"},
85       {"http://f\u00fcpy:85", "xn--fpy-hoa:85", ProxyServer::SCHEME_HTTP,
86        "xn--fpy-hoa", 85, "PROXY xn--fpy-hoa:85"},
87       {"https://0xA.020.3.4:443", "https://10.16.3.4:443",
88        ProxyServer::SCHEME_HTTPS, "10.16.3.4", 443, "HTTPS 10.16.3.4:443"},
89       {"http://FoO.tEsT:80", "foo.test:80", ProxyServer::SCHEME_HTTP,
90        "foo.test", 80, "PROXY foo.test:80"},
91   };
92 
93   for (const auto& test : tests) {
94     ProxyServer uri =
95         ProxyUriToProxyServer(test.input_uri, ProxyServer::SCHEME_HTTP);
96     EXPECT_TRUE(uri.is_valid());
97     EXPECT_FALSE(uri.is_direct());
98     EXPECT_EQ(test.expected_uri, ProxyServerToProxyUri(uri));
99     EXPECT_EQ(test.expected_scheme, uri.scheme());
100     EXPECT_EQ(test.expected_host, uri.host_port_pair().host());
101     EXPECT_EQ(test.expected_port, uri.host_port_pair().port());
102     EXPECT_EQ(test.expected_pac_string, ProxyServerToPacResultElement(uri));
103   }
104 }
105 
106 // Test parsing of the special URI form "direct://". analogous to the "DIRECT"
107 // element in a PAC result.
TEST(ProxySpecificationUtilTest,DirectProxyUriToProxyServer)108 TEST(ProxySpecificationUtilTest, DirectProxyUriToProxyServer) {
109   ProxyServer uri =
110       ProxyUriToProxyServer("direct://", ProxyServer::SCHEME_HTTP);
111   EXPECT_TRUE(uri.is_valid());
112   EXPECT_TRUE(uri.is_direct());
113   EXPECT_EQ("direct://", ProxyServerToProxyUri(uri));
114   EXPECT_EQ("DIRECT", ProxyServerToPacResultElement(uri));
115 }
116 
117 // Test parsing some invalid inputs.
TEST(ProxySpecificationUtilTest,InvalidProxyUriToProxyServer)118 TEST(ProxySpecificationUtilTest, InvalidProxyUriToProxyServer) {
119   const char* const tests[] = {
120       "",
121       "   ",
122       "dddf:",         // not a valid port
123       "dddd:d",        // not a valid port
124       "http://",       // not a valid host/port.
125       "direct://xyz",  // direct is not allowed a host/port.
126       "http:/",        // ambiguous, but will fail because of bad port.
127       "http:",         // ambiguous, but will fail because of bad port.
128       "foopy.111",     // Interpreted as invalid IPv4 address.
129       "foo.test/"      // Paths disallowed.
130       "foo.test:123/"  // Paths disallowed.
131       "foo.test/foo"   // Paths disallowed.
132   };
133 
134   for (const char* test : tests) {
135     SCOPED_TRACE(test);
136     ProxyServer uri = ProxyUriToProxyServer(test, ProxyServer::SCHEME_HTTP);
137     EXPECT_FALSE(uri.is_valid());
138     EXPECT_FALSE(uri.is_direct());
139     EXPECT_FALSE(uri.is_http());
140     EXPECT_FALSE(uri.is_socks());
141   }
142 }
143 
144 // Test that LWS (SP | HT) is disregarded from the ends.
TEST(ProxySpecificationUtilTest,WhitespaceProxyUriToProxyServer)145 TEST(ProxySpecificationUtilTest, WhitespaceProxyUriToProxyServer) {
146   const char* const tests[] = {
147       "  foopy:80",
148       "foopy:80   \t",
149       "  \tfoopy:80  ",
150   };
151 
152   for (const char* test : tests) {
153     ProxyServer uri = ProxyUriToProxyServer(test, ProxyServer::SCHEME_HTTP);
154     EXPECT_EQ("foopy:80", ProxyServerToProxyUri(uri));
155   }
156 }
157 
158 // Test parsing a ProxyServer from a PAC representation.
TEST(ProxySpecificationUtilTest,PacResultElementToProxyServer)159 TEST(ProxySpecificationUtilTest, PacResultElementToProxyServer) {
160   const struct {
161     const char* const input_pac;
162     const char* const expected_uri;
163   } tests[] = {
164       {
165           "PROXY foopy:10",
166           "foopy:10",
167       },
168       {
169           "   PROXY    foopy:10   ",
170           "foopy:10",
171       },
172       {
173           "pRoXy foopy:10",
174           "foopy:10",
175       },
176       {
177           "PROXY foopy",  // No port.
178           "foopy:80",
179       },
180       {
181           "socks foopy",
182           "socks4://foopy:1080",
183       },
184       {
185           "socks4 foopy",
186           "socks4://foopy:1080",
187       },
188       {
189           "socks5 foopy",
190           "socks5://foopy:1080",
191       },
192       {
193           "socks5 foopy:11",
194           "socks5://foopy:11",
195       },
196       {
197           " direct  ",
198           "direct://",
199       },
200       {
201           "https foopy",
202           "https://foopy:443",
203       },
204       {
205           "https foopy:10",
206           "https://foopy:10",
207       },
208       {"PROXY [FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:10",
209        "[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:10"},
210       {"PROXY f\u00fcpy:85", "xn--fpy-hoa:85"},
211   };
212 
213   for (const auto& test : tests) {
214     SCOPED_TRACE(test.input_pac);
215     ProxyServer server = PacResultElementToProxyServer(test.input_pac);
216     EXPECT_TRUE(server.is_valid());
217     EXPECT_EQ(test.expected_uri, ProxyServerToProxyUri(server));
218 
219     ProxyChain chain = PacResultElementToProxyChain(test.input_pac);
220     EXPECT_TRUE(chain.IsValid());
221     if (!chain.is_direct()) {
222       EXPECT_EQ(test.expected_uri,
223                 ProxyServerToProxyUri(chain.GetProxyServer(/*chain_index=*/0)));
224     }
225   }
226 }
227 
228 // Test parsing a ProxyServer from an invalid PAC representation.
TEST(ProxySpecificationUtilTest,InvalidPacResultElementToProxyServer)229 TEST(ProxySpecificationUtilTest, InvalidPacResultElementToProxyServer) {
230   const char* const tests[] = {
231       "PROXY",                   // missing host/port.
232       "HTTPS",                   // missing host/port.
233       "SOCKS",                   // missing host/port.
234       "DIRECT foopy:10",         // direct cannot have host/port.
235       "INVALIDSCHEME",           // unrecognized scheme.
236       "INVALIDSCHEME foopy:10",  // unrecognized scheme.
237       "HTTP foopy:10",           // http scheme should be "PROXY"
238   };
239 
240   for (const char* test : tests) {
241     SCOPED_TRACE(test);
242     ProxyServer server = PacResultElementToProxyServer(test);
243     EXPECT_FALSE(server.is_valid());
244 
245     ProxyChain chain = PacResultElementToProxyChain(test);
246     EXPECT_FALSE(chain.IsValid());
247   }
248 }
249 
250 }  // namespace
251 }  // namespace net
252