OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/base/net_util.h" | 5 #include "net/base/net_util.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <ostream> | 9 #include <ostream> |
10 | 10 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 "http://use+rname:password@google.com", | 111 "http://use+rname:password@google.com", |
112 "use+rname", | 112 "use+rname", |
113 "password", | 113 "password", |
114 }, | 114 }, |
115 { // Use a '&' in the password. | 115 { // Use a '&' in the password. |
116 "http://username:p&ssword@google.com", | 116 "http://username:p&ssword@google.com", |
117 "username", | 117 "username", |
118 "p&ssword", | 118 "p&ssword", |
119 }, | 119 }, |
120 }; | 120 }; |
121 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 121 for (size_t i = 0; i < arraysize(tests); ++i) { |
122 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, | 122 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, |
123 tests[i].input_url)); | 123 tests[i].input_url)); |
124 GURL url(tests[i].input_url); | 124 GURL url(tests[i].input_url); |
125 | 125 |
126 base::string16 username, password; | 126 base::string16 username, password; |
127 GetIdentityFromURL(url, &username, &password); | 127 GetIdentityFromURL(url, &username, &password); |
128 | 128 |
129 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_username), username); | 129 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_username), username); |
130 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_password), password); | 130 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_password), password); |
131 } | 131 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 TEST(NetUtilTest, GetSpecificHeader) { | 173 TEST(NetUtilTest, GetSpecificHeader) { |
174 const HeaderCase tests[] = { | 174 const HeaderCase tests[] = { |
175 {"content-type", "text/html; charset=utf-8"}, | 175 {"content-type", "text/html; charset=utf-8"}, |
176 {"CONTENT-LENGTH", "378557"}, | 176 {"CONTENT-LENGTH", "378557"}, |
177 {"Date", "Mon, 13 Nov 2006 21:38:09 GMT"}, | 177 {"Date", "Mon, 13 Nov 2006 21:38:09 GMT"}, |
178 {"Bad-Header", ""}, | 178 {"Bad-Header", ""}, |
179 {"", ""}, | 179 {"", ""}, |
180 }; | 180 }; |
181 | 181 |
182 // Test first with google_headers. | 182 // Test first with google_headers. |
183 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 183 for (size_t i = 0; i < arraysize(tests); ++i) { |
184 std::string result = | 184 std::string result = |
185 GetSpecificHeader(google_headers, tests[i].header_name); | 185 GetSpecificHeader(google_headers, tests[i].header_name); |
186 EXPECT_EQ(result, tests[i].expected); | 186 EXPECT_EQ(result, tests[i].expected); |
187 } | 187 } |
188 | 188 |
189 // Test again with empty headers. | 189 // Test again with empty headers. |
190 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 190 for (size_t i = 0; i < arraysize(tests); ++i) { |
191 std::string result = GetSpecificHeader(std::string(), tests[i].header_name); | 191 std::string result = GetSpecificHeader(std::string(), tests[i].header_name); |
192 EXPECT_EQ(result, std::string()); | 192 EXPECT_EQ(result, std::string()); |
193 } | 193 } |
194 } | 194 } |
195 | 195 |
196 TEST(NetUtilTest, CompliantHost) { | 196 TEST(NetUtilTest, CompliantHost) { |
197 struct CompliantHostCase { | 197 struct CompliantHostCase { |
198 const char* host; | 198 const char* host; |
199 bool expected_output; | 199 bool expected_output; |
200 }; | 200 }; |
(...skipping 16 matching lines...) Expand all Loading... |
217 {"a+9a", false}, | 217 {"a+9a", false}, |
218 {"-a.a9", true}, | 218 {"-a.a9", true}, |
219 {"1-.a-b", true}, | 219 {"1-.a-b", true}, |
220 {"1_.a-b", false}, | 220 {"1_.a-b", false}, |
221 {"1-2.a_b", true}, | 221 {"1-2.a_b", true}, |
222 {"a.b.c.d.e", true}, | 222 {"a.b.c.d.e", true}, |
223 {"1.2.3.4.5", true}, | 223 {"1.2.3.4.5", true}, |
224 {"1.2.3.4.5.", true}, | 224 {"1.2.3.4.5.", true}, |
225 }; | 225 }; |
226 | 226 |
227 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(compliant_host_cases); ++i) { | 227 for (size_t i = 0; i < arraysize(compliant_host_cases); ++i) { |
228 EXPECT_EQ(compliant_host_cases[i].expected_output, | 228 EXPECT_EQ(compliant_host_cases[i].expected_output, |
229 IsCanonicalizedHostCompliant(compliant_host_cases[i].host)); | 229 IsCanonicalizedHostCompliant(compliant_host_cases[i].host)); |
230 } | 230 } |
231 } | 231 } |
232 | 232 |
233 TEST(NetUtilTest, ParseHostAndPort) { | 233 TEST(NetUtilTest, ParseHostAndPort) { |
234 const struct { | 234 const struct { |
235 const char* input; | 235 const char* input; |
236 bool success; | 236 bool success; |
237 const char* expected_host; | 237 const char* expected_host; |
(...skipping 29 matching lines...) Expand all Loading... |
267 {"usrname@host", false, "", -1}, | 267 {"usrname@host", false, "", -1}, |
268 {"usrname:password@host", false, "", -1}, | 268 {"usrname:password@host", false, "", -1}, |
269 {":password@host", false, "", -1}, | 269 {":password@host", false, "", -1}, |
270 {":password@host:80", false, "", -1}, | 270 {":password@host:80", false, "", -1}, |
271 {":password@host", false, "", -1}, | 271 {":password@host", false, "", -1}, |
272 {"@host", false, "", -1}, | 272 {"@host", false, "", -1}, |
273 {"[", false, "", -1}, | 273 {"[", false, "", -1}, |
274 {"[]", false, "", -1}, | 274 {"[]", false, "", -1}, |
275 }; | 275 }; |
276 | 276 |
277 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 277 for (size_t i = 0; i < arraysize(tests); ++i) { |
278 std::string host; | 278 std::string host; |
279 int port; | 279 int port; |
280 bool ok = ParseHostAndPort(tests[i].input, &host, &port); | 280 bool ok = ParseHostAndPort(tests[i].input, &host, &port); |
281 | 281 |
282 EXPECT_EQ(tests[i].success, ok); | 282 EXPECT_EQ(tests[i].success, ok); |
283 | 283 |
284 if (tests[i].success) { | 284 if (tests[i].success) { |
285 EXPECT_EQ(tests[i].expected_host, host); | 285 EXPECT_EQ(tests[i].expected_host, host); |
286 EXPECT_EQ(tests[i].expected_port, port); | 286 EXPECT_EQ(tests[i].expected_port, port); |
287 } | 287 } |
288 } | 288 } |
289 } | 289 } |
290 | 290 |
291 TEST(NetUtilTest, GetHostAndPort) { | 291 TEST(NetUtilTest, GetHostAndPort) { |
292 const struct { | 292 const struct { |
293 GURL url; | 293 GURL url; |
294 const char* expected_host_and_port; | 294 const char* expected_host_and_port; |
295 } tests[] = { | 295 } tests[] = { |
296 { GURL("http://www.foo.com/x"), "www.foo.com:80"}, | 296 { GURL("http://www.foo.com/x"), "www.foo.com:80"}, |
297 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, | 297 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, |
298 | 298 |
299 // For IPv6 literals should always include the brackets. | 299 // For IPv6 literals should always include the brackets. |
300 { GURL("http://[1::2]/x"), "[1::2]:80"}, | 300 { GURL("http://[1::2]/x"), "[1::2]:80"}, |
301 { GURL("http://[::a]:33/x"), "[::a]:33"}, | 301 { GURL("http://[::a]:33/x"), "[::a]:33"}, |
302 }; | 302 }; |
303 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 303 for (size_t i = 0; i < arraysize(tests); ++i) { |
304 std::string host_and_port = GetHostAndPort(tests[i].url); | 304 std::string host_and_port = GetHostAndPort(tests[i].url); |
305 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); | 305 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); |
306 } | 306 } |
307 } | 307 } |
308 | 308 |
309 TEST(NetUtilTest, GetHostAndOptionalPort) { | 309 TEST(NetUtilTest, GetHostAndOptionalPort) { |
310 const struct { | 310 const struct { |
311 GURL url; | 311 GURL url; |
312 const char* expected_host_and_port; | 312 const char* expected_host_and_port; |
313 } tests[] = { | 313 } tests[] = { |
314 { GURL("http://www.foo.com/x"), "www.foo.com"}, | 314 { GURL("http://www.foo.com/x"), "www.foo.com"}, |
315 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, | 315 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, |
316 | 316 |
317 // For IPv6 literals should always include the brackets. | 317 // For IPv6 literals should always include the brackets. |
318 { GURL("http://[1::2]/x"), "[1::2]"}, | 318 { GURL("http://[1::2]/x"), "[1::2]"}, |
319 { GURL("http://[::a]:33/x"), "[::a]:33"}, | 319 { GURL("http://[::a]:33/x"), "[::a]:33"}, |
320 }; | 320 }; |
321 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 321 for (size_t i = 0; i < arraysize(tests); ++i) { |
322 std::string host_and_port = GetHostAndOptionalPort(tests[i].url); | 322 std::string host_and_port = GetHostAndOptionalPort(tests[i].url); |
323 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); | 323 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); |
324 } | 324 } |
325 } | 325 } |
326 | 326 |
327 TEST(NetUtilTest, IPAddressToString) { | 327 TEST(NetUtilTest, IPAddressToString) { |
328 uint8 addr1[4] = {0, 0, 0, 0}; | 328 uint8 addr1[4] = {0, 0, 0, 0}; |
329 EXPECT_EQ("0.0.0.0", IPAddressToString(addr1, sizeof(addr1))); | 329 EXPECT_EQ("0.0.0.0", IPAddressToString(addr1, sizeof(addr1))); |
330 | 330 |
331 uint8 addr2[4] = {192, 168, 0, 1}; | 331 uint8 addr2[4] = {192, 168, 0, 1}; |
(...skipping 19 matching lines...) Expand all Loading... |
351 TEST(NetUtilTest, NetAddressToString_IPv4) { | 351 TEST(NetUtilTest, NetAddressToString_IPv4) { |
352 const struct { | 352 const struct { |
353 uint8 addr[4]; | 353 uint8 addr[4]; |
354 const char* result; | 354 const char* result; |
355 } tests[] = { | 355 } tests[] = { |
356 {{0, 0, 0, 0}, "0.0.0.0"}, | 356 {{0, 0, 0, 0}, "0.0.0.0"}, |
357 {{127, 0, 0, 1}, "127.0.0.1"}, | 357 {{127, 0, 0, 1}, "127.0.0.1"}, |
358 {{192, 168, 0, 1}, "192.168.0.1"}, | 358 {{192, 168, 0, 1}, "192.168.0.1"}, |
359 }; | 359 }; |
360 | 360 |
361 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 361 for (size_t i = 0; i < arraysize(tests); ++i) { |
362 SockaddrStorage storage; | 362 SockaddrStorage storage; |
363 MakeIPv4Address(tests[i].addr, 80, &storage); | 363 MakeIPv4Address(tests[i].addr, 80, &storage); |
364 std::string result = NetAddressToString(storage.addr, storage.addr_len); | 364 std::string result = NetAddressToString(storage.addr, storage.addr_len); |
365 EXPECT_EQ(std::string(tests[i].result), result); | 365 EXPECT_EQ(std::string(tests[i].result), result); |
366 } | 366 } |
367 } | 367 } |
368 | 368 |
369 TEST(NetUtilTest, NetAddressToString_IPv6) { | 369 TEST(NetUtilTest, NetAddressToString_IPv6) { |
370 const struct { | 370 const struct { |
371 uint8 addr[16]; | 371 uint8 addr[16]; |
372 const char* result; | 372 const char* result; |
373 } tests[] = { | 373 } tests[] = { |
374 {{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, | 374 {{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, |
375 0x98, 0x76, 0x54, 0x32, 0x10}, | 375 0x98, 0x76, 0x54, 0x32, 0x10}, |
376 "fedc:ba98:7654:3210:fedc:ba98:7654:3210"}, | 376 "fedc:ba98:7654:3210:fedc:ba98:7654:3210"}, |
377 }; | 377 }; |
378 | 378 |
379 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 379 for (size_t i = 0; i < arraysize(tests); ++i) { |
380 SockaddrStorage storage; | 380 SockaddrStorage storage; |
381 MakeIPv6Address(tests[i].addr, 80, &storage); | 381 MakeIPv6Address(tests[i].addr, 80, &storage); |
382 EXPECT_EQ(std::string(tests[i].result), | 382 EXPECT_EQ(std::string(tests[i].result), |
383 NetAddressToString(storage.addr, storage.addr_len)); | 383 NetAddressToString(storage.addr, storage.addr_len)); |
384 } | 384 } |
385 } | 385 } |
386 | 386 |
387 TEST(NetUtilTest, NetAddressToStringWithPort_IPv4) { | 387 TEST(NetUtilTest, NetAddressToStringWithPort_IPv4) { |
388 uint8 addr[] = {127, 0, 0, 1}; | 388 uint8 addr[] = {127, 0, 0, 1}; |
389 SockaddrStorage storage; | 389 SockaddrStorage storage; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 }, | 445 }, |
446 { // Try an FTP URL -- strip both the reference and the username/password. | 446 { // Try an FTP URL -- strip both the reference and the username/password. |
447 "ftp://user:pass@google.com:80/sup?yo#X#X", | 447 "ftp://user:pass@google.com:80/sup?yo#X#X", |
448 "ftp://google.com:80/sup?yo", | 448 "ftp://google.com:80/sup?yo", |
449 }, | 449 }, |
450 { // Try a nonstandard URL | 450 { // Try a nonstandard URL |
451 "foobar://user:pass@google.com:80/sup?yo#X#X", | 451 "foobar://user:pass@google.com:80/sup?yo#X#X", |
452 "foobar://user:pass@google.com:80/sup?yo", | 452 "foobar://user:pass@google.com:80/sup?yo", |
453 }, | 453 }, |
454 }; | 454 }; |
455 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 455 for (size_t i = 0; i < arraysize(tests); ++i) { |
456 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, | 456 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, |
457 tests[i].input_url)); | 457 tests[i].input_url)); |
458 GURL input_url(GURL(tests[i].input_url)); | 458 GURL input_url(GURL(tests[i].input_url)); |
459 GURL expected_url(GURL(tests[i].expected_simplified_url)); | 459 GURL expected_url(GURL(tests[i].expected_simplified_url)); |
460 EXPECT_EQ(expected_url, SimplifyUrlForRequest(input_url)); | 460 EXPECT_EQ(expected_url, SimplifyUrlForRequest(input_url)); |
461 } | 461 } |
462 } | 462 } |
463 | 463 |
464 TEST(NetUtilTest, SetExplicitlyAllowedPortsTest) { | 464 TEST(NetUtilTest, SetExplicitlyAllowedPortsTest) { |
465 std::string invalid[] = { "1,2,a", "'1','2'", "1, 2, 3", "1 0,11,12" }; | 465 std::string invalid[] = { "1,2,a", "'1','2'", "1, 2, 3", "1 0,11,12" }; |
466 std::string valid[] = { "", "1", "1,2", "1,2,3", "10,11,12,13" }; | 466 std::string valid[] = { "", "1", "1,2", "1,2,3", "10,11,12,13" }; |
467 | 467 |
468 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(invalid); ++i) { | 468 for (size_t i = 0; i < arraysize(invalid); ++i) { |
469 SetExplicitlyAllowedPorts(invalid[i]); | 469 SetExplicitlyAllowedPorts(invalid[i]); |
470 EXPECT_EQ(0, static_cast<int>(GetCountOfExplicitlyAllowedPorts())); | 470 EXPECT_EQ(0, static_cast<int>(GetCountOfExplicitlyAllowedPorts())); |
471 } | 471 } |
472 | 472 |
473 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid); ++i) { | 473 for (size_t i = 0; i < arraysize(valid); ++i) { |
474 SetExplicitlyAllowedPorts(valid[i]); | 474 SetExplicitlyAllowedPorts(valid[i]); |
475 EXPECT_EQ(i, GetCountOfExplicitlyAllowedPorts()); | 475 EXPECT_EQ(i, GetCountOfExplicitlyAllowedPorts()); |
476 } | 476 } |
477 } | 477 } |
478 | 478 |
479 TEST(NetUtilTest, GetHostOrSpecFromURL) { | 479 TEST(NetUtilTest, GetHostOrSpecFromURL) { |
480 EXPECT_EQ("example.com", | 480 EXPECT_EQ("example.com", |
481 GetHostOrSpecFromURL(GURL("http://example.com/test"))); | 481 GetHostOrSpecFromURL(GURL("http://example.com/test"))); |
482 EXPECT_EQ("example.com", | 482 EXPECT_EQ("example.com", |
483 GetHostOrSpecFromURL(GURL("http://example.com./test"))); | 483 GetHostOrSpecFromURL(GURL("http://example.com./test"))); |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
676 "10.11.33.44/16", | 676 "10.11.33.44/16", |
677 "::ffff:0a0b:89", | 677 "::ffff:0a0b:89", |
678 true | 678 true |
679 }, | 679 }, |
680 { | 680 { |
681 "10.11.33.44/16", | 681 "10.11.33.44/16", |
682 "::ffff:10.12.33.44", | 682 "::ffff:10.12.33.44", |
683 false | 683 false |
684 }, | 684 }, |
685 }; | 685 }; |
686 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 686 for (size_t i = 0; i < arraysize(tests); ++i) { |
687 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s, %s", i, | 687 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s, %s", i, |
688 tests[i].cidr_literal, | 688 tests[i].cidr_literal, |
689 tests[i].ip_literal)); | 689 tests[i].ip_literal)); |
690 | 690 |
691 IPAddressNumber ip_number; | 691 IPAddressNumber ip_number; |
692 EXPECT_TRUE(ParseIPLiteralToNumber(tests[i].ip_literal, &ip_number)); | 692 EXPECT_TRUE(ParseIPLiteralToNumber(tests[i].ip_literal, &ip_number)); |
693 | 693 |
694 IPAddressNumber ip_prefix; | 694 IPAddressNumber ip_prefix; |
695 size_t prefix_length_in_bits; | 695 size_t prefix_length_in_bits; |
696 | 696 |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
984 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) { | 984 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) { |
985 const NonUniqueNameTestData& test_data = GetParam(); | 985 const NonUniqueNameTestData& test_data = GetParam(); |
986 | 986 |
987 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); | 987 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); |
988 } | 988 } |
989 | 989 |
990 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest, | 990 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest, |
991 testing::ValuesIn(kNonUniqueNameTestData)); | 991 testing::ValuesIn(kNonUniqueNameTestData)); |
992 | 992 |
993 } // namespace net | 993 } // namespace net |
OLD | NEW |