OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "net/proxy/proxy_server.h" | 6 #include "net/proxy/proxy_server.h" |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 // Test the creation of ProxyServer using ProxyServer::FromURI, which parses | 9 // Test the creation of ProxyServer using ProxyServer::FromURI, which parses |
10 // inputs of the form [<scheme>"://"]<host>[":"<port>]. Verify that each part | 10 // inputs of the form [<scheme>"://"]<host>[":"<port>]. Verify that each part |
11 // was labelled correctly, and the accessors all give the right data. | 11 // was labelled correctly, and the accessors all give the right data. |
12 TEST(ProxyServerTest, FromURI) { | 12 TEST(ProxyServerTest, FromURI) { |
13 const struct { | 13 const struct { |
14 const char* input_uri; | 14 const char* const input_uri; |
15 const char* expected_uri; | 15 const char* const expected_uri; |
16 net::ProxyServer::Scheme expected_scheme; | 16 net::ProxyServer::Scheme expected_scheme; |
17 const char* expected_host; | 17 const char* const expected_host; |
18 int expected_port; | 18 int expected_port; |
19 const char* expected_pac_string; | 19 const char* const expected_pac_string; |
20 } tests[] = { | 20 } tests[] = { |
21 // HTTP proxy URIs: | 21 // HTTP proxy URIs: |
22 { | 22 { |
23 "foopy:10", // No scheme. | 23 "foopy:10", // No scheme. |
24 "foopy:10", | 24 "foopy:10", |
25 net::ProxyServer::SCHEME_HTTP, | 25 net::ProxyServer::SCHEME_HTTP, |
26 "foopy", | 26 "foopy", |
27 10, | 27 10, |
28 "PROXY foopy:10" | 28 "PROXY foopy:10" |
29 }, | 29 }, |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 net::ProxyServer uri = | 184 net::ProxyServer uri = |
185 net::ProxyServer::FromURI("direct://", net::ProxyServer::SCHEME_HTTP); | 185 net::ProxyServer::FromURI("direct://", net::ProxyServer::SCHEME_HTTP); |
186 EXPECT_TRUE(uri.is_valid()); | 186 EXPECT_TRUE(uri.is_valid()); |
187 EXPECT_TRUE(uri.is_direct()); | 187 EXPECT_TRUE(uri.is_direct()); |
188 EXPECT_EQ("direct://", uri.ToURI()); | 188 EXPECT_EQ("direct://", uri.ToURI()); |
189 EXPECT_EQ("DIRECT", uri.ToPacString()); | 189 EXPECT_EQ("DIRECT", uri.ToPacString()); |
190 } | 190 } |
191 | 191 |
192 // Test parsing some invalid inputs. | 192 // Test parsing some invalid inputs. |
193 TEST(ProxyServerTest, Invalid) { | 193 TEST(ProxyServerTest, Invalid) { |
194 const char* tests[] = { | 194 const char* const tests[] = { |
195 "", | 195 "", |
196 " ", | 196 " ", |
197 "dddf:", // not a valid port | 197 "dddf:", // not a valid port |
198 "dddd:d", // not a valid port | 198 "dddd:d", // not a valid port |
199 "http://", // not a valid host/port. | 199 "http://", // not a valid host/port. |
200 "direct://xyz", // direct is not allowed a host/port. | 200 "direct://xyz", // direct is not allowed a host/port. |
201 "http:/", // ambiguous, but will fail because of bad port. | 201 "http:/", // ambiguous, but will fail because of bad port. |
202 "http:", // ambiguous, but will fail because of bad port. | 202 "http:", // ambiguous, but will fail because of bad port. |
203 }; | 203 }; |
204 | 204 |
205 for (size_t i = 0; i < arraysize(tests); ++i) { | 205 for (size_t i = 0; i < arraysize(tests); ++i) { |
206 net::ProxyServer uri = | 206 net::ProxyServer uri = |
207 net::ProxyServer::FromURI(tests[i], net::ProxyServer::SCHEME_HTTP); | 207 net::ProxyServer::FromURI(tests[i], net::ProxyServer::SCHEME_HTTP); |
208 EXPECT_FALSE(uri.is_valid()); | 208 EXPECT_FALSE(uri.is_valid()); |
209 EXPECT_FALSE(uri.is_direct()); | 209 EXPECT_FALSE(uri.is_direct()); |
210 EXPECT_FALSE(uri.is_http()); | 210 EXPECT_FALSE(uri.is_http()); |
211 EXPECT_FALSE(uri.is_socks()); | 211 EXPECT_FALSE(uri.is_socks()); |
212 } | 212 } |
213 } | 213 } |
214 | 214 |
215 // Test that LWS (SP | HT) is disregarded from the ends. | 215 // Test that LWS (SP | HT) is disregarded from the ends. |
216 TEST(ProxyServerTest, Whitespace) { | 216 TEST(ProxyServerTest, Whitespace) { |
217 const char* tests[] = { | 217 const char* const tests[] = { |
218 " foopy:80", | 218 " foopy:80", |
219 "foopy:80 \t", | 219 "foopy:80 \t", |
220 " \tfoopy:80 ", | 220 " \tfoopy:80 ", |
221 }; | 221 }; |
222 | 222 |
223 for (size_t i = 0; i < arraysize(tests); ++i) { | 223 for (size_t i = 0; i < arraysize(tests); ++i) { |
224 net::ProxyServer uri = | 224 net::ProxyServer uri = |
225 net::ProxyServer::FromURI(tests[i], net::ProxyServer::SCHEME_HTTP); | 225 net::ProxyServer::FromURI(tests[i], net::ProxyServer::SCHEME_HTTP); |
226 EXPECT_EQ("foopy:80", uri.ToURI()); | 226 EXPECT_EQ("foopy:80", uri.ToURI()); |
227 } | 227 } |
228 } | 228 } |
229 | 229 |
230 // Test parsing a ProxyServer from a PAC representation. | 230 // Test parsing a ProxyServer from a PAC representation. |
231 TEST(ProxyServerTest, FromPACString) { | 231 TEST(ProxyServerTest, FromPACString) { |
232 const struct { | 232 const struct { |
233 const char* input_pac; | 233 const char* const input_pac; |
234 const char* expected_uri; | 234 const char* const expected_uri; |
235 } tests[] = { | 235 } tests[] = { |
236 { | 236 { |
237 "PROXY foopy:10", | 237 "PROXY foopy:10", |
238 "foopy:10", | 238 "foopy:10", |
239 }, | 239 }, |
240 { | 240 { |
241 " PROXY foopy:10 ", | 241 " PROXY foopy:10 ", |
242 "foopy:10", | 242 "foopy:10", |
243 }, | 243 }, |
244 { | 244 { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 | 281 |
282 for (size_t i = 0; i < arraysize(tests); ++i) { | 282 for (size_t i = 0; i < arraysize(tests); ++i) { |
283 net::ProxyServer uri = net::ProxyServer::FromPacString(tests[i].input_pac); | 283 net::ProxyServer uri = net::ProxyServer::FromPacString(tests[i].input_pac); |
284 EXPECT_TRUE(uri.is_valid()); | 284 EXPECT_TRUE(uri.is_valid()); |
285 EXPECT_EQ(tests[i].expected_uri, uri.ToURI()); | 285 EXPECT_EQ(tests[i].expected_uri, uri.ToURI()); |
286 } | 286 } |
287 } | 287 } |
288 | 288 |
289 // Test parsing a ProxyServer from an invalid PAC representation. | 289 // Test parsing a ProxyServer from an invalid PAC representation. |
290 TEST(ProxyServerTest, FromPACStringInvalid) { | 290 TEST(ProxyServerTest, FromPACStringInvalid) { |
291 const char* tests[] = { | 291 const char* const tests[] = { |
292 "PROXY", // missing host/port. | 292 "PROXY", // missing host/port. |
293 "HTTPS", // missing host/port. | 293 "HTTPS", // missing host/port. |
294 "SOCKS", // missing host/port. | 294 "SOCKS", // missing host/port. |
295 "DIRECT foopy:10", // direct cannot have host/port. | 295 "DIRECT foopy:10", // direct cannot have host/port. |
296 }; | 296 }; |
297 | 297 |
298 for (size_t i = 0; i < arraysize(tests); ++i) { | 298 for (size_t i = 0; i < arraysize(tests); ++i) { |
299 net::ProxyServer uri = net::ProxyServer::FromPacString(tests[i]); | 299 net::ProxyServer uri = net::ProxyServer::FromPacString(tests[i]); |
300 EXPECT_FALSE(uri.is_valid()); | 300 EXPECT_FALSE(uri.is_valid()); |
301 } | 301 } |
302 } | 302 } |
303 | 303 |
304 TEST(ProxyServerTest, ComparatorAndEquality) { | 304 TEST(ProxyServerTest, ComparatorAndEquality) { |
305 struct { | 305 struct { |
306 // Inputs. | 306 // Inputs. |
307 const char* server1; | 307 const char* const server1; |
308 const char* server2; | 308 const char* const server2; |
309 | 309 |
310 // Expectation. | 310 // Expectation. |
311 // -1 means server1 is less than server2 | 311 // -1 means server1 is less than server2 |
312 // 0 means server1 equals server2 | 312 // 0 means server1 equals server2 |
313 // 1 means server1 is greater than server2 | 313 // 1 means server1 is greater than server2 |
314 int expected_comparison; | 314 int expected_comparison; |
315 } tests[] = { | 315 } tests[] = { |
316 { // Equal. | 316 { // Equal. |
317 "foo:11", | 317 "foo:11", |
318 "http://foo:11", | 318 "http://foo:11", |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 case 1: | 359 case 1: |
360 EXPECT_FALSE(server1 < server2); | 360 EXPECT_FALSE(server1 < server2); |
361 EXPECT_TRUE(server2 < server1); | 361 EXPECT_TRUE(server2 < server1); |
362 EXPECT_FALSE(server2 == server1); | 362 EXPECT_FALSE(server2 == server1); |
363 break; | 363 break; |
364 default: | 364 default: |
365 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; | 365 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; |
366 } | 366 } |
367 } | 367 } |
368 } | 368 } |
OLD | NEW |