1 // Copyright (c) 2009 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. 4 5 #include "net/base/net_util.h" 6 7 #include "base/file_path.h" 8 #include "base/format_macros.h" 9 #include "base/string_util.h" 10 #include "base/sys_string_conversions.h" 11 #include "base/time.h" 12 #include "googleurl/src/gurl.h" 13 #include "net/base/sys_addrinfo.h" 14 #include "testing/gtest/include/gtest/gtest.h" 15 16 namespace { 17 18 class NetUtilTest : public testing::Test { 19 }; 20 21 struct FileCase { 22 const wchar_t* file; 23 const char* url; 24 }; 25 26 struct HeaderCase { 27 const wchar_t* header_name; 28 const wchar_t* expected; 29 }; 30 31 struct HeaderParamCase { 32 const wchar_t* header_name; 33 const wchar_t* param_name; 34 const wchar_t* expected; 35 }; 36 37 struct FileNameCDCase { 38 const char* header_field; 39 const char* referrer_charset; 40 const wchar_t* expected; 41 }; 42 43 const wchar_t* kLanguages[] = { 44 L"", L"en", L"zh-CN", L"ja", L"ko", 45 L"he", L"ar", L"ru", L"el", L"fr", 46 L"de", L"pt", L"sv", L"th", L"hi", 47 L"de,en", L"el,en", L"zh-TW,en", L"ko,ja", L"he,ru,en", 48 L"zh,ru,en" 49 }; 50 51 struct IDNTestCase { 52 const char* input; 53 const wchar_t* unicode_output; 54 const bool unicode_allowed[arraysize(kLanguages)]; 55 }; 56 57 // TODO(jungshik) This is just a random sample of languages and is far 58 // from exhaustive. We may have to generate all the combinations 59 // of languages (powerset of a set of all the languages). 60 const IDNTestCase idn_cases[] = { 61 // No IDN 62 {"www.google.com", L"www.google.com", 63 {true, true, true, true, true, 64 true, true, true, true, true, 65 true, true, true, true, true, 66 true, true, true, true, true, 67 true}}, 68 {"www.google.com.", L"www.google.com.", 69 {true, true, true, true, true, 70 true, true, true, true, true, 71 true, true, true, true, true, 72 true, true, true, true, true, 73 true}}, 74 {".", L".", 75 {true, true, true, true, true, 76 true, true, true, true, true, 77 true, true, true, true, true, 78 true, true, true, true, true, 79 true}}, 80 {"", L"", 81 {true, true, true, true, true, 82 true, true, true, true, true, 83 true, true, true, true, true, 84 true, true, true, true, true, 85 true}}, 86 // IDN 87 // Hanzi (Traditional Chinese) 88 {"xn--1lq90ic7f1rc.cn", L"\x5317\x4eac\x5927\x5b78.cn", 89 {true, false, true, true, false, 90 false, false, false, false, false, 91 false, false, false, false, false, 92 false, false, true, true, false, 93 true}}, 94 // Hanzi ('video' in Simplified Chinese : will pass only in zh-CN,zh) 95 {"xn--cy2a840a.com", L"\x89c6\x9891.com", 96 {true, false, true, false, false, 97 false, false, false, false, false, 98 false, false, false, false, false, 99 false, false, false, false, false, 100 true}}, 101 // Hanzi + '123' 102 {"www.xn--123-p18d.com", L"www.\x4e00" L"123.com", 103 {true, false, true, true, false, 104 false, false, false, false, false, 105 false, false, false, false, false, 106 false, false, true, true, false, 107 true}}, 108 // Hanzi + Latin : U+56FD is simplified and is regarded 109 // as not supported in zh-TW. 110 {"www.xn--hello-9n1hm04c.com", L"www.hello\x4e2d\x56fd.com", 111 {false, false, true, true, false, 112 false, false, false, false, false, 113 false, false, false, false, false, 114 false, false, false, true, false, 115 true}}, 116 // Kanji + Kana (Japanese) 117 {"xn--l8jvb1ey91xtjb.jp", L"\x671d\x65e5\x3042\x3055\x3072.jp", 118 {true, false, false, true, false, 119 false, false, false, false, false, 120 false, false, false, false, false, 121 false, false, false, true, false, 122 false}}, 123 // Katakana including U+30FC 124 {"xn--tckm4i2e.jp", L"\x30b3\x30de\x30fc\x30b9.jp", 125 {true, false, false, true, false, 126 false, false, false, false, false, 127 false, false, false, false, false, 128 false, false, false, true, false, 129 }}, 130 // Katakana + Latin (Japanese) 131 // TODO(jungshik): Change 'false' in the first element to 'true' 132 // after upgrading to ICU 4.2.1 to use new uspoof_* APIs instead 133 // of our IsIDNComponentInSingleScript(). 134 {"xn--e-efusa1mzf.jp", L"e\x30b3\x30de\x30fc\x30b9.jp", 135 {false, false, false, true, false, 136 false, false, false, false, false, 137 false, false, false, false, false, 138 false, false, false, true, false, 139 }}, 140 // Hangul (Korean) 141 {"www.xn--or3b17p6jjc.kr", L"www.\xc804\xc790\xc815\xbd80.kr", 142 {true, false, false, false, true, 143 false, false, false, false, false, 144 false, false, false, false, false, 145 false, false, false, true, false, 146 false}}, 147 // b<u-umlaut>cher (German) 148 {"xn--bcher-kva.de", L"b\x00fc" L"cher.de", 149 {true, false, false, false, false, 150 false, false, false, false, true, 151 true, false, false, false, false, 152 true, false, false, false, false, 153 false}}, 154 // a with diaeresis 155 {"www.xn--frgbolaget-q5a.se", L"www.f\x00e4rgbolaget.se", 156 {true, false, false, false, false, 157 false, false, false, false, false, 158 true, false, true, false, false, 159 true, false, false, false, false, 160 false}}, 161 // c-cedilla (French) 162 {"www.xn--alliancefranaise-npb.fr", L"www.alliancefran\x00e7" L"aise.fr", 163 {true, false, false, false, false, 164 false, false, false, false, true, 165 false, true, false, false, false, 166 false, false, false, false, false, 167 false}}, 168 // caf'e with acute accent' (French) 169 {"xn--caf-dma.fr", L"caf\x00e9.fr", 170 {true, false, false, false, false, 171 false, false, false, false, true, 172 false, true, true, false, false, 173 false, false, false, false, false, 174 false}}, 175 // c-cedillla and a with tilde (Portuguese) 176 {"xn--poema-9qae5a.com.br", L"p\x00e3oema\x00e7\x00e3.com.br", 177 {true, false, false, false, false, 178 false, false, false, false, false, 179 false, true, false, false, false, 180 false, false, false, false, false, 181 false}}, 182 // s with caron 183 {"xn--achy-f6a.com", L"\x0161" L"achy.com", 184 {true, false, false, false, false, 185 false, false, false, false, false, 186 false, false, false, false, false, 187 false, false, false, false, false, 188 false}}, 189 // TODO(jungshik) : Add examples with Cyrillic letters 190 // only used in some languages written in Cyrillic. 191 // Eutopia (Greek) 192 {"xn--kxae4bafwg.gr", L"\x03bf\x03c5\x03c4\x03bf\x03c0\x03af\x03b1.gr", 193 {true, false, false, false, false, 194 false, false, false, true, false, 195 false, false, false, false, false, 196 false, true, false, false, false, 197 false}}, 198 // Eutopia + 123 (Greek) 199 {"xn---123-pldm0haj2bk.gr", 200 L"\x03bf\x03c5\x03c4\x03bf\x03c0\x03af\x03b1-123.gr", 201 {true, false, false, false, false, 202 false, false, false, true, false, 203 false, false, false, false, false, 204 false, true, false, false, false, 205 false}}, 206 // Cyrillic (Russian) 207 {"xn--n1aeec9b.ru", L"\x0442\x043e\x0440\x0442\x044b.ru", 208 {true, false, false, false, false, 209 false, false, true, false, false, 210 false, false, false, false, false, 211 false, false, false, false, true, 212 true}}, 213 // Cyrillic + 123 (Russian) 214 {"xn---123-45dmmc5f.ru", L"\x0442\x043e\x0440\x0442\x044b-123.ru", 215 {true, false, false, false, false, 216 false, false, true, false, false, 217 false, false, false, false, false, 218 false, false, false, false, true, 219 true}}, 220 // Arabic 221 {"xn--mgba1fmg.ar", L"\x0627\x0641\x0644\x0627\x0645.ar", 222 {true, false, false, false, false, 223 false, true, false, false, false, 224 false, false, false, false, false, 225 false, false, false, false, false, 226 false}}, 227 // Hebrew 228 {"xn--4dbib.he", L"\x05d5\x05d0\x05d4.he", 229 {true, false, false, false, false, 230 true, false, false, false, false, 231 false, false, false, false, false, 232 false, false, false, false, true, 233 false}}, 234 // Thai 235 {"xn--12c2cc4ag3b4ccu.th", 236 L"\x0e2a\x0e32\x0e22\x0e01\x0e32\x0e23\x0e1a\x0e34\x0e19.th", 237 {true, false, false, false, false, 238 false, false, false, false, false, 239 false, false, false, true, false, 240 false, false, false, false, false, 241 false}}, 242 // Devangari (Hindi) 243 {"www.xn--l1b6a9e1b7c.in", L"www.\x0905\x0915\x094b\x0932\x093e.in", 244 {true, false, false, false, false, 245 false, false, false, false, false, 246 false, false, false, false, true, 247 false, false, false, false, false, 248 false}}, 249 // Invalid IDN 250 {"xn--hello?world.com", NULL, 251 {false, false, false, false, false, 252 false, false, false, false, false, 253 false, false, false, false, false, 254 false, false, false, false, false, 255 false}}, 256 // Unsafe IDNs 257 // "payp<alpha>l.com" 258 {"www.xn--paypl-g9d.com", L"payp\x03b1l.com", 259 {false, false, false, false, false, 260 false, false, false, false, false, 261 false, false, false, false, false, 262 false, false, false, false, false, 263 false}}, 264 // google.gr with Greek omicron and epsilon 265 {"xn--ggl-6xc1ca.gr", L"g\x03bf\x03bfgl\x03b5.gr", 266 {false, false, false, false, false, 267 false, false, false, false, false, 268 false, false, false, false, false, 269 false, false, false, false, false, 270 false}}, 271 // google.ru with Cyrillic o 272 {"xn--ggl-tdd6ba.ru", L"g\x043e\x043egl\x0435.ru", 273 {false, false, false, false, false, 274 false, false, false, false, false, 275 false, false, false, false, false, 276 false, false, false, false, false, 277 false}}, 278 // h<e with acute>llo<China in Han>.cn 279 {"xn--hllo-bpa7979ih5m.cn", L"h\x00e9llo\x4e2d\x56fd.cn", 280 {false, false, false, false, false, 281 false, false, false, false, false, 282 false, false, false, false, false, 283 false, false, false, false, false, 284 false}}, 285 // <Greek rho><Cyrillic a><Cyrillic u>.ru 286 {"xn--2xa6t2b.ru", L"\x03c1\x0430\x0443.ru", 287 {false, false, false, false, false, 288 false, false, false, false, false, 289 false, false, false, false, false, 290 false, false, false, false, false, 291 false}}, 292 // One that's really long that will force a buffer realloc 293 {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 294 "aaaaaaa", 295 L"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" 296 L"aaaaaaaa", 297 {true, true, true, true, true, 298 true, true, true, true, true, 299 true, true, true, true, true, 300 true, true, true, true, true, 301 true}}, 302 // Test cases for characters we blacklisted although allowed in IDN. 303 // Embedded spaces will be turned to %20 in the display. 304 // TODO(jungshik): We need to have more cases. This is a typical 305 // data-driven trap. The following test cases need to be separated 306 // and tested only for a couple of languages. 307 {"xn--osd3820f24c.kr", L"\xac00\xb098\x115f.kr", 308 {false, false, false, false, false, 309 false, false, false, false, false, 310 false, false, false, false, false, 311 false, false, false, false, false, 312 false}}, 313 {"www.xn--google-ho0coa.com", L"www.\x2039google\x203a.com", 314 {false, false, false, false, false, 315 false, false, false, false, false, 316 false, false, false, false, false, 317 false, false, false, false, false, 318 }}, 319 {"google.xn--comabc-k8d", L"google.com\x0338" L"abc", 320 {false, false, false, false, false, 321 false, false, false, false, false, 322 false, false, false, false, false, 323 false, false, false, false, false, 324 }}, 325 #if 0 326 // These two cases are special. We need a separate test. 327 // U+3000 and U+3002 are normalized to ASCII space and dot. 328 {"xn-- -kq6ay5z.cn", L"\x4e2d\x56fd\x3000.cn", 329 {false, false, true, false, false, 330 false, false, false, false, false, 331 false, false, false, false, false, 332 false, false, true, false, false, 333 true}}, 334 {"xn--fiqs8s.cn", L"\x4e2d\x56fd\x3002" L"cn", 335 {false, false, true, false, false, 336 false, false, false, false, false, 337 false, false, false, false, false, 338 false, false, true, false, false, 339 true}}, 340 #endif 341 }; 342 343 struct AdjustOffsetCase { 344 size_t input_offset; 345 size_t output_offset; 346 }; 347 348 struct CompliantHostCase { 349 const char* host; 350 bool expected_output; 351 }; 352 353 struct SuggestedFilenameCase { 354 const char* url; 355 const char* content_disp_header; 356 const char* referrer_charset; 357 const wchar_t* default_filename; 358 const wchar_t* expected_filename; 359 }; 360 361 struct UrlTestData { 362 const char* description; 363 const char* input; 364 const std::wstring languages; 365 bool omit; 366 UnescapeRule::Type escape_rules; 367 const std::wstring output; 368 size_t prefix_len; 369 }; 370 371 // Returns an addrinfo for the given 32-bit address (IPv4.) 372 // The result lives in static storage, so don't delete it. 373 // |bytes| should be an array of length 4. 374 const struct addrinfo* GetIPv4Address(const uint8* bytes) { 375 static struct addrinfo static_ai; 376 static struct sockaddr_in static_addr4; 377 378 struct addrinfo* ai = &static_ai; 379 ai->ai_socktype = SOCK_STREAM; 380 memset(ai, 0, sizeof(static_ai)); 381 382 ai->ai_family = AF_INET; 383 ai->ai_addrlen = sizeof(static_addr4); 384 385 struct sockaddr_in* addr4 = &static_addr4; 386 memset(addr4, 0, sizeof(static_addr4)); 387 addr4->sin_port = htons(80); 388 addr4->sin_family = ai->ai_family; 389 memcpy(&addr4->sin_addr, bytes, 4); 390 391 ai->ai_addr = (sockaddr*)addr4; 392 return ai; 393 } 394 395 // Returns a addrinfo for the given 128-bit address (IPv6.) 396 // The result lives in static storage, so don't delete it. 397 // |bytes| should be an array of length 16. 398 const struct addrinfo* GetIPv6Address(const uint8* bytes) { 399 static struct addrinfo static_ai; 400 static struct sockaddr_in6 static_addr6; 401 402 struct addrinfo* ai = &static_ai; 403 ai->ai_socktype = SOCK_STREAM; 404 memset(ai, 0, sizeof(static_ai)); 405 406 ai->ai_family = AF_INET6; 407 ai->ai_addrlen = sizeof(static_addr6); 408 409 struct sockaddr_in6* addr6 = &static_addr6; 410 memset(addr6, 0, sizeof(static_addr6)); 411 addr6->sin6_port = htons(80); 412 addr6->sin6_family = ai->ai_family; 413 memcpy(&addr6->sin6_addr, bytes, 16); 414 415 ai->ai_addr = (sockaddr*)addr6; 416 return ai; 417 } 418 419 420 // A helper for IDN*{Fast,Slow}. 421 // Append "::<language list>" to |expected| and |actual| to make it 422 // easy to tell which sub-case fails without debugging. 423 void AppendLanguagesToOutputs(const wchar_t* languages, 424 std::wstring* expected, 425 std::wstring* actual) { 426 expected->append(L"::"); 427 expected->append(languages); 428 actual->append(L"::"); 429 actual->append(languages); 430 } 431 432 } // anonymous namespace 433 434 TEST(NetUtilTest, FileURLConversion) { 435 // a list of test file names and the corresponding URLs 436 const FileCase round_trip_cases[] = { 437 #if defined(OS_WIN) 438 {L"C:\\foo\\bar.txt", "file:///C:/foo/bar.txt"}, 439 {L"\\\\some computer\\foo\\bar.txt", 440 "file://some%20computer/foo/bar.txt"}, // UNC 441 {L"D:\\Name;with%some symbols*#", 442 "file:///D:/Name%3Bwith%25some%20symbols*%23"}, 443 // issue 14153: To be tested with the OS default codepage other than 1252. 444 {L"D:\\latin1\\caf\x00E9\x00DD.txt", 445 "file:///D:/latin1/caf%C3%A9%C3%9D.txt"}, 446 {L"D:\\otherlatin\\caf\x0119.txt", 447 "file:///D:/otherlatin/caf%C4%99.txt"}, 448 {L"D:\\greek\\\x03B1\x03B2\x03B3.txt", 449 "file:///D:/greek/%CE%B1%CE%B2%CE%B3.txt"}, 450 {L"D:\\Chinese\\\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc", 451 "file:///D:/Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD%91" 452 "%E9%A1%B5.doc"}, 453 {L"D:\\plane1\\\xD835\xDC00\xD835\xDC01.txt", // Math alphabet "AB" 454 "file:///D:/plane1/%F0%9D%90%80%F0%9D%90%81.txt"}, 455 #elif defined(OS_POSIX) 456 {L"/foo/bar.txt", "file:///foo/bar.txt"}, 457 {L"/foo/BAR.txt", "file:///foo/BAR.txt"}, 458 {L"/C:/foo/bar.txt", "file:///C:/foo/bar.txt"}, 459 {L"/some computer/foo/bar.txt", "file:///some%20computer/foo/bar.txt"}, 460 {L"/Name;with%some symbols*#", "file:///Name%3Bwith%25some%20symbols*%23"}, 461 {L"/latin1/caf\x00E9\x00DD.txt", "file:///latin1/caf%C3%A9%C3%9D.txt"}, 462 {L"/otherlatin/caf\x0119.txt", "file:///otherlatin/caf%C4%99.txt"}, 463 {L"/greek/\x03B1\x03B2\x03B3.txt", "file:///greek/%CE%B1%CE%B2%CE%B3.txt"}, 464 {L"/Chinese/\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc", 465 "file:///Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD" 466 "%91%E9%A1%B5.doc"}, 467 {L"/plane1/\x1D400\x1D401.txt", // Math alphabet "AB" 468 "file:///plane1/%F0%9D%90%80%F0%9D%90%81.txt"}, 469 #endif 470 }; 471 472 // First, we'll test that we can round-trip all of the above cases of URLs 473 FilePath output; 474 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(round_trip_cases); i++) { 475 // convert to the file URL 476 GURL file_url(net::FilePathToFileURL( 477 FilePath::FromWStringHack(round_trip_cases[i].file))); 478 EXPECT_EQ(round_trip_cases[i].url, file_url.spec()); 479 480 // Back to the filename. 481 EXPECT_TRUE(net::FileURLToFilePath(file_url, &output)); 482 EXPECT_EQ(round_trip_cases[i].file, output.ToWStringHack()); 483 } 484 485 // Test that various file: URLs get decoded into the correct file type 486 FileCase url_cases[] = { 487 #if defined(OS_WIN) 488 {L"C:\\foo\\bar.txt", "file:c|/foo\\bar.txt"}, 489 {L"C:\\foo\\bar.txt", "file:/c:/foo/bar.txt"}, 490 {L"\\\\foo\\bar.txt", "file://foo\\bar.txt"}, 491 {L"C:\\foo\\bar.txt", "file:///c:/foo/bar.txt"}, 492 {L"\\\\foo\\bar.txt", "file:////foo\\bar.txt"}, 493 {L"\\\\foo\\bar.txt", "file:/foo/bar.txt"}, 494 {L"\\\\foo\\bar.txt", "file://foo\\bar.txt"}, 495 {L"C:\\foo\\bar.txt", "file:\\\\\\c:/foo/bar.txt"}, 496 #elif defined(OS_POSIX) 497 {L"/c:/foo/bar.txt", "file:/c:/foo/bar.txt"}, 498 {L"/c:/foo/bar.txt", "file:///c:/foo/bar.txt"}, 499 {L"/foo/bar.txt", "file:/foo/bar.txt"}, 500 {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"}, 501 {L"/foo/bar.txt", "file:foo/bar.txt"}, 502 {L"/bar.txt", "file://foo/bar.txt"}, 503 {L"/foo/bar.txt", "file:///foo/bar.txt"}, 504 {L"/foo/bar.txt", "file:////foo/bar.txt"}, 505 {L"/foo/bar.txt", "file:////foo//bar.txt"}, 506 {L"/foo/bar.txt", "file:////foo///bar.txt"}, 507 {L"/foo/bar.txt", "file:////foo////bar.txt"}, 508 {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"}, 509 {L"/c:/foo/bar.txt", "file:c:/foo/bar.txt"}, 510 // We get these wrong because GURL turns back slashes into forward 511 // slashes. 512 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, 513 //{L"/c|/foo%5Cbar.txt", "file:c|/foo\\bar.txt"}, 514 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, 515 //{L"/foo%5Cbar.txt", "file:////foo\\bar.txt"}, 516 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, 517 #endif 518 }; 519 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(url_cases); i++) { 520 net::FileURLToFilePath(GURL(url_cases[i].url), &output); 521 EXPECT_EQ(url_cases[i].file, output.ToWStringHack()); 522 } 523 524 // Unfortunately, UTF8ToWide discards invalid UTF8 input. 525 #ifdef BUG_878908_IS_FIXED 526 // Test that no conversion happens if the UTF-8 input is invalid, and that 527 // the input is preserved in UTF-8 528 const char invalid_utf8[] = "file:///d:/Blah/\xff.doc"; 529 const wchar_t invalid_wide[] = L"D:\\Blah\\\xff.doc"; 530 EXPECT_TRUE(net::FileURLToFilePath( 531 GURL(std::string(invalid_utf8)), &output)); 532 EXPECT_EQ(std::wstring(invalid_wide), output); 533 #endif 534 535 // Test that if a file URL is malformed, we get a failure 536 EXPECT_FALSE(net::FileURLToFilePath(GURL("filefoobar"), &output)); 537 } 538 539 TEST(NetUtilTest, GetIdentityFromURL) { 540 struct { 541 const char* input_url; 542 const wchar_t* expected_username; 543 const wchar_t* expected_password; 544 } tests[] = { 545 { 546 "http://username:password@google.com", 547 L"username", 548 L"password", 549 }, 550 { // Test for http://crbug.com/19200 551 "http://username:p@ssword@google.com", 552 L"username", 553 L"p@ssword", 554 }, 555 { // Username contains %20. 556 "http://use rname:password (at) google.com", 557 L"use rname", 558 L"password", 559 }, 560 { // Keep %00 as is. 561 "http://use%00rname:password@google.com", 562 L"use%00rname", 563 L"password", 564 }, 565 { // Use a '+' in the username. 566 "http://use+rname:password@google.com", 567 L"use+rname", 568 L"password", 569 }, 570 { // Use a '&' in the password. 571 "http://username:p&ssword@google.com", 572 L"username", 573 L"p&ssword", 574 }, 575 }; 576 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 577 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]: %s", i, tests[i].input_url)); 578 GURL url(tests[i].input_url); 579 580 std::wstring username, password; 581 net::GetIdentityFromURL(url, &username, &password); 582 583 EXPECT_EQ(tests[i].expected_username, username); 584 EXPECT_EQ(tests[i].expected_password, password); 585 } 586 } 587 588 // Try extracting a username which was encoded with UTF8. 589 TEST(NetUtilTest, GetIdentityFromURL_UTF8) { 590 GURL url(WideToUTF16(L"http://foo:\x4f60\x597d@blah.com")); 591 592 EXPECT_EQ("foo", url.username()); 593 EXPECT_EQ("%E4%BD%A0%E5%A5%BD", url.password()); 594 595 // Extract the unescaped identity. 596 std::wstring username, password; 597 net::GetIdentityFromURL(url, &username, &password); 598 599 // Verify that it was decoded as UTF8. 600 EXPECT_EQ(L"foo", username); 601 EXPECT_EQ(L"\x4f60\x597d", password); 602 } 603 604 // Just a bunch of fake headers. 605 const wchar_t* google_headers = 606 L"HTTP/1.1 200 OK\n" 607 L"Content-TYPE: text/html; charset=utf-8\n" 608 L"Content-disposition: attachment; filename=\"download.pdf\"\n" 609 L"Content-Length: 378557\n" 610 L"X-Google-Google1: 314159265\n" 611 L"X-Google-Google2: aaaa2:7783,bbb21:9441\n" 612 L"X-Google-Google4: home\n" 613 L"Transfer-Encoding: chunked\n" 614 L"Set-Cookie: HEHE_AT=6666x66beef666x6-66xx6666x66; Path=/mail\n" 615 L"Set-Cookie: HEHE_HELP=owned:0;Path=/\n" 616 L"Set-Cookie: S=gmail=Xxx-beefbeefbeef_beefb:gmail_yj=beefbeef000beefbee" 617 L"fbee:gmproxy=bee-fbeefbe; Domain=.google.com; Path=/\n" 618 L"X-Google-Google2: /one/two/three/four/five/six/seven-height/nine:9411\n" 619 L"Server: GFE/1.3\n" 620 L"Transfer-Encoding: chunked\n" 621 L"Date: Mon, 13 Nov 2006 21:38:09 GMT\n" 622 L"Expires: Tue, 14 Nov 2006 19:23:58 GMT\n" 623 L"X-Malformed: bla; arg=test\"\n" 624 L"X-Malformed2: bla; arg=\n" 625 L"X-Test: bla; arg1=val1; arg2=val2"; 626 627 TEST(NetUtilTest, GetSpecificHeader) { 628 const HeaderCase tests[] = { 629 {L"content-type", L"text/html; charset=utf-8"}, 630 {L"CONTENT-LENGTH", L"378557"}, 631 {L"Date", L"Mon, 13 Nov 2006 21:38:09 GMT"}, 632 {L"Bad-Header", L""}, 633 {L"", L""}, 634 }; 635 636 // Test first with google_headers. 637 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 638 std::wstring result = net::GetSpecificHeader(google_headers, 639 tests[i].header_name); 640 EXPECT_EQ(result, tests[i].expected); 641 } 642 643 // Test again with empty headers. 644 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 645 std::wstring result = net::GetSpecificHeader(L"", tests[i].header_name); 646 EXPECT_EQ(result, std::wstring()); 647 } 648 } 649 650 TEST(NetUtilTest, GetHeaderParamValue) { 651 const HeaderParamCase tests[] = { 652 {L"Content-type", L"charset", L"utf-8"}, 653 {L"content-disposition", L"filename", L"download.pdf"}, 654 {L"Content-Type", L"badparam", L""}, 655 {L"X-Malformed", L"arg", L"test\""}, 656 {L"X-Malformed2", L"arg", L""}, 657 {L"X-Test", L"arg1", L"val1"}, 658 {L"X-Test", L"arg2", L"val2"}, 659 {L"Bad-Header", L"badparam", L""}, 660 {L"Bad-Header", L"", L""}, 661 {L"", L"badparam", L""}, 662 {L"", L"", L""}, 663 }; 664 // TODO(mpcomplete): add tests for other formats of headers. 665 666 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 667 std::wstring header_value = 668 net::GetSpecificHeader(google_headers, tests[i].header_name); 669 std::wstring result = 670 net::GetHeaderParamValue(header_value, tests[i].param_name); 671 EXPECT_EQ(result, tests[i].expected); 672 } 673 674 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 675 std::wstring header_value = 676 net::GetSpecificHeader(L"", tests[i].header_name); 677 std::wstring result = 678 net::GetHeaderParamValue(header_value, tests[i].param_name); 679 EXPECT_EQ(result, std::wstring()); 680 } 681 } 682 683 TEST(NetUtilTest, GetFileNameFromCD) { 684 const FileNameCDCase tests[] = { 685 // Test various forms of C-D header fields emitted by web servers. 686 {"content-disposition: inline; filename=\"abcde.pdf\"", "", L"abcde.pdf"}, 687 {"content-disposition: inline; name=\"abcde.pdf\"", "", L"abcde.pdf"}, 688 {"content-disposition: attachment; filename=abcde.pdf", "", L"abcde.pdf"}, 689 {"content-disposition: attachment; name=abcde.pdf", "", L"abcde.pdf"}, 690 {"content-disposition: attachment; filename=abc,de.pdf", "", L"abc,de.pdf"}, 691 {"content-disposition: filename=abcde.pdf", "", L"abcde.pdf"}, 692 {"content-disposition: filename= abcde.pdf", "", L"abcde.pdf"}, 693 {"content-disposition: filename =abcde.pdf", "", L"abcde.pdf"}, 694 {"content-disposition: filename = abcde.pdf", "", L"abcde.pdf"}, 695 {"content-disposition: filename\t=abcde.pdf", "", L"abcde.pdf"}, 696 {"content-disposition: filename \t\t =abcde.pdf", "", L"abcde.pdf"}, 697 {"content-disposition: name=abcde.pdf", "", L"abcde.pdf"}, 698 {"content-disposition: inline; filename=\"abc%20de.pdf\"", "", 699 L"abc de.pdf"}, 700 // Whitespaces are converted to a space. 701 {"content-disposition: inline; filename=\"abc \t\nde.pdf\"", "", 702 L"abc de.pdf"}, 703 // %-escaped UTF-8 704 {"Content-Disposition: attachment; filename=\"%EC%98%88%EC%88%A0%20" 705 "%EC%98%88%EC%88%A0.jpg\"", "", L"\xc608\xc220 \xc608\xc220.jpg"}, 706 {"Content-Disposition: attachment; filename=\"%F0%90%8C%B0%F0%90%8C%B1" 707 "abc.jpg\"", "", L"\U00010330\U00010331abc.jpg"}, 708 {"Content-Disposition: attachment; filename=\"%EC%98%88%EC%88%A0 \n" 709 "%EC%98%88%EC%88%A0.jpg\"", "", L"\xc608\xc220 \xc608\xc220.jpg"}, 710 // RFC 2047 with various charsets and Q/B encodings 711 {"Content-Disposition: attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD=" 712 "D13=2Epng?=\"", "", L"\x82b8\x8853" L"3.png"}, 713 {"Content-Disposition: attachment; filename==?eUc-Kr?b?v7m8+iAzLnBuZw==?=", 714 "", L"\xc608\xc220 3.png"}, 715 {"Content-Disposition: attachment; filename==?utf-8?Q?=E8=8A=B8=E8" 716 "=A1=93_3=2Epng?=", "", L"\x82b8\x8853 3.png"}, 717 {"Content-Disposition: attachment; filename==?utf-8?Q?=F0=90=8C=B0" 718 "_3=2Epng?=", "", L"\U00010330 3.png"}, 719 {"Content-Disposition: inline; filename=\"=?iso88591?Q?caf=e9_=2epng?=\"", 720 "", L"caf\x00e9 .png"}, 721 // Space after an encode word should be removed. 722 {"Content-Disposition: inline; filename=\"=?iso88591?Q?caf=E9_?= .png\"", 723 "", L"caf\x00e9 .png"}, 724 // Two encoded words with different charsets (not very likely to be emitted 725 // by web servers in the wild). Spaces between them are removed. 726 {"Content-Disposition: inline; filename=\"=?euc-kr?b?v7m8+iAz?=" 727 " =?ksc5601?q?=BF=B9=BC=FA=2Epng?=\"", "", 728 L"\xc608\xc220 3\xc608\xc220.png"}, 729 {"Content-Disposition: attachment; filename=\"=?windows-1252?Q?caf=E9?=" 730 " =?iso-8859-7?b?4eI=?= .png\"", "", L"caf\x00e9\x03b1\x03b2.png"}, 731 // Non-ASCII string is passed through and treated as UTF-8 as long as 732 // it's valid as UTF-8 and regardless of |referrer_charset|. 733 {"Content-Disposition: attachment; filename=caf\xc3\xa9.png", 734 "iso-8859-1", L"caf\x00e9.png"}, 735 {"Content-Disposition: attachment; filename=caf\xc3\xa9.png", 736 "", L"caf\x00e9.png"}, 737 // Non-ASCII/Non-UTF-8 string. Fall back to the referrer charset. 738 {"Content-Disposition: attachment; filename=caf\xe5.png", 739 "windows-1253", L"caf\x03b5.png"}, 740 #if 0 741 // Non-ASCII/Non-UTF-8 string. Fall back to the native codepage. 742 // TODO(jungshik): We need to set the OS default codepage 743 // to a specific value before testing. On Windows, we can use 744 // SetThreadLocale(). 745 {"Content-Disposition: attachment; filename=\xb0\xa1\xb0\xa2.png", 746 "", L"\xac00\xac01.png"}, 747 #endif 748 // Failure cases 749 // Invalid hex-digit "G" 750 {"Content-Disposition: attachment; filename==?iiso88591?Q?caf=EG?=", "", 751 L""}, 752 // Incomplete RFC 2047 encoded-word (missing '='' at the end) 753 {"Content-Disposition: attachment; filename==?iso88591?Q?caf=E3?", "", L""}, 754 // Extra character at the end of an encoded word 755 {"Content-Disposition: attachment; filename==?iso88591?Q?caf=E3?==", 756 "", L""}, 757 // Extra token at the end of an encoded word 758 {"Content-Disposition: attachment; filename==?iso88591?Q?caf=E3?=?", 759 "", L""}, 760 {"Content-Disposition: attachment; filename==?iso88591?Q?caf=E3?=?=", 761 "", L""}, 762 // Incomplete hex-escaped chars 763 {"Content-Disposition: attachment; filename==?windows-1252?Q?=63=61=E?=", 764 "", L""}, 765 {"Content-Disposition: attachment; filename=%EC%98%88%EC%88%A", "", L""}, 766 // %-escaped non-UTF-8 encoding is an "error" 767 {"Content-Disposition: attachment; filename=%B7%DD%BD%D1.png", "", L""}, 768 // Two RFC 2047 encoded words in a row without a space is an error. 769 {"Content-Disposition: attachment; filename==?windows-1252?Q?caf=E3?=" 770 "=?iso-8859-7?b?4eIucG5nCg==?=", "", L""}, 771 }; 772 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 773 EXPECT_EQ(tests[i].expected, 774 UTF8ToWide(net::GetFileNameFromCD(tests[i].header_field, 775 tests[i].referrer_charset))); 776 } 777 } 778 779 TEST(NetUtilTest, IDNToUnicodeFast) { 780 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_cases); i++) { 781 for (size_t j = 0; j < arraysize(kLanguages); j++) { 782 // ja || zh-TW,en || ko,ja -> IDNToUnicodeSlow 783 if (j == 3 || j == 17 || j == 18) 784 continue; 785 std::wstring output(net::IDNToUnicode(idn_cases[i].input, 786 strlen(idn_cases[i].input), kLanguages[j], NULL)); 787 std::wstring expected(idn_cases[i].unicode_allowed[j] ? 788 idn_cases[i].unicode_output : ASCIIToWide(idn_cases[i].input)); 789 AppendLanguagesToOutputs(kLanguages[j], &expected, &output); 790 EXPECT_EQ(expected, output); 791 } 792 } 793 } 794 795 TEST(NetUtilTest, IDNToUnicodeSlow) { 796 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_cases); i++) { 797 for (size_t j = 0; j < arraysize(kLanguages); j++) { 798 // !(ja || zh-TW,en || ko,ja) -> IDNToUnicodeFast 799 if (!(j == 3 || j == 17 || j == 18)) 800 continue; 801 std::wstring output(net::IDNToUnicode(idn_cases[i].input, 802 strlen(idn_cases[i].input), kLanguages[j], NULL)); 803 std::wstring expected(idn_cases[i].unicode_allowed[j] ? 804 idn_cases[i].unicode_output : ASCIIToWide(idn_cases[i].input)); 805 AppendLanguagesToOutputs(kLanguages[j], &expected, &output); 806 EXPECT_EQ(expected, output); 807 } 808 } 809 } 810 811 TEST(NetUtilTest, IDNToUnicodeAdjustOffset) { 812 const AdjustOffsetCase adjust_cases[] = { 813 {0, 0}, 814 {2, 2}, 815 {4, 4}, 816 {5, 5}, 817 {6, std::wstring::npos}, 818 {16, std::wstring::npos}, 819 {17, 7}, 820 {18, 8}, 821 {19, std::wstring::npos}, 822 {25, std::wstring::npos}, 823 {34, 12}, 824 {35, 13}, 825 {38, 16}, 826 {39, std::wstring::npos}, 827 {std::wstring::npos, std::wstring::npos}, 828 }; 829 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(adjust_cases); ++i) { 830 size_t offset = adjust_cases[i].input_offset; 831 // "test.\x89c6\x9891.\x5317\x4eac\x5927\x5b78.test" 832 net::IDNToUnicode("test.xn--cy2a840a.xn--1lq90ic7f1rc.test", 39, L"zh-CN", 833 &offset); 834 EXPECT_EQ(adjust_cases[i].output_offset, offset); 835 } 836 } 837 838 TEST(NetUtilTest, CompliantHost) { 839 const CompliantHostCase compliant_host_cases[] = { 840 {"", false}, 841 {"a", true}, 842 {"-", false}, 843 {".", false}, 844 {"a.", true}, 845 {"a.a", true}, 846 {"9.a", true}, 847 {"a.9", false}, 848 {"_9a", false}, 849 {"a.a9", true}, 850 {"a.9a", false}, 851 {"a+9a", false}, 852 {"1-.a-b", false}, 853 {"1-2.a_b", true}, 854 {"a.b.c.d.e", true}, 855 {"1.2.3.4.e", true}, 856 {"a.b.c.d.5", false}, 857 {"1.2.3.4.e.", true}, 858 {"a.b.c.d.5.", false}, 859 }; 860 861 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(compliant_host_cases); ++i) { 862 EXPECT_EQ(compliant_host_cases[i].expected_output, 863 net::IsCanonicalizedHostCompliant(compliant_host_cases[i].host)); 864 } 865 } 866 867 TEST(NetUtilTest, StripWWW) { 868 EXPECT_EQ(L"", net::StripWWW(L"")); 869 EXPECT_EQ(L"", net::StripWWW(L"www.")); 870 EXPECT_EQ(L"blah", net::StripWWW(L"www.blah")); 871 EXPECT_EQ(L"blah", net::StripWWW(L"blah")); 872 } 873 874 TEST(NetUtilTest, GetSuggestedFilename) { 875 const SuggestedFilenameCase test_cases[] = { 876 {"http://www.google.com/", 877 "Content-disposition: attachment; filename=test.html", 878 "", 879 L"", 880 L"test.html"}, 881 {"http://www.google.com/", 882 "Content-disposition: attachment; filename=\"test.html\"", 883 "", 884 L"", 885 L"test.html"}, 886 {"http://www.google.com/path/test.html", 887 "Content-disposition: attachment", 888 "", 889 L"", 890 L"test.html"}, 891 {"http://www.google.com/path/test.html", 892 "Content-disposition: attachment;", 893 "", 894 L"", 895 L"test.html"}, 896 {"http://www.google.com/", 897 "", 898 "", 899 L"", 900 L"www.google.com"}, 901 {"http://www.google.com/test.html", 902 "", 903 "", 904 L"", 905 L"test.html"}, 906 // Now that we use googleurl's ExtractFileName, this case falls back 907 // to the hostname. If this behavior is not desirable, we'd better 908 // change ExtractFileName (in url_parse). 909 {"http://www.google.com/path/", 910 "", 911 "", 912 L"", 913 L"www.google.com"}, 914 {"http://www.google.com/path", 915 "", 916 "", 917 L"", 918 L"path"}, 919 {"file:///", 920 "", 921 "", 922 L"", 923 L"download"}, 924 {"non-standard-scheme:", 925 "", 926 "", 927 L"", 928 L"download"}, 929 {"http://www.google.com/", 930 "Content-disposition: attachment; filename =\"test.html\"", 931 "", 932 L"download", 933 L"test.html"}, 934 {"http://www.google.com/", 935 "", 936 "", 937 L"download", 938 L"download"}, 939 {"http://www.google.com/", 940 "Content-disposition: attachment; filename=\"../test.html\"", 941 "", 942 L"", 943 L"test.html"}, 944 {"http://www.google.com/", 945 "Content-disposition: attachment; filename=\"..\"", 946 "", 947 L"download", 948 L"download"}, 949 {"http://www.google.com/test.html", 950 "Content-disposition: attachment; filename=\"..\"", 951 "", 952 L"download", 953 L"test.html"}, 954 // Below is a small subset of cases taken from GetFileNameFromCD test above. 955 {"http://www.google.com/", 956 "Content-Disposition: attachment; filename=\"%EC%98%88%EC%88%A0%20" 957 "%EC%98%88%EC%88%A0.jpg\"", 958 "", 959 L"", 960 L"\uc608\uc220 \uc608\uc220.jpg"}, 961 {"http://www.google.com/%EC%98%88%EC%88%A0%20%EC%98%88%EC%88%A0.jpg", 962 "", 963 "", 964 L"download", 965 L"\uc608\uc220 \uc608\uc220.jpg"}, 966 {"http://www.google.com/", 967 "Content-disposition: attachment;", 968 "", 969 L"\uB2E4\uC6B4\uB85C\uB4DC", 970 L"\uB2E4\uC6B4\uB85C\uB4DC"}, 971 {"http://www.google.com/", 972 "Content-Disposition: attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD=" 973 "D13=2Epng?=\"", 974 "", 975 L"download", 976 L"\u82b8\u88533.png"}, 977 {"http://www.example.com/images?id=3", 978 "Content-Disposition: attachment; filename=caf\xc3\xa9.png", 979 "iso-8859-1", 980 L"", 981 L"caf\u00e9.png"}, 982 {"http://www.example.com/images?id=3", 983 "Content-Disposition: attachment; filename=caf\xe5.png", 984 "windows-1253", 985 L"", 986 L"caf\u03b5.png"}, 987 {"http://www.example.com/file?id=3", 988 "Content-Disposition: attachment; name=\xcf\xc2\xd4\xd8.zip", 989 "GBK", 990 L"", 991 L"\u4e0b\u8f7d.zip"}, 992 // Invalid C-D header. Extracts filename from url. 993 {"http://www.google.com/test.html", 994 "Content-Disposition: attachment; filename==?iiso88591?Q?caf=EG?=", 995 "", 996 L"", 997 L"test.html"}, 998 // about: and data: URLs 999 {"about:chrome", 1000 "", 1001 "", 1002 L"", 1003 L"download"}, 1004 {"data:,looks/like/a.path", 1005 "", 1006 "", 1007 L"", 1008 L"download"}, 1009 {"data:text/plain;base64,VG8gYmUgb3Igbm90IHRvIGJlLg=", 1010 "", 1011 "", 1012 L"", 1013 L"download"}, 1014 {"data:,looks/like/a.path", 1015 "", 1016 "", 1017 L"default_filename_is_given", 1018 L"default_filename_is_given"}, 1019 {"data:,looks/like/a.path", 1020 "", 1021 "", 1022 L"\u65e5\u672c\u8a9e", // Japanese Kanji. 1023 L"\u65e5\u672c\u8a9e"}, 1024 // Dotfiles. Ensures preceeding period(s) stripped. 1025 {"http://www.google.com/.test.html", 1026 "", 1027 "", 1028 L"", 1029 L"test.html"}, 1030 {"http://www.google.com/.test", 1031 "", 1032 "", 1033 L"", 1034 L"test"}, 1035 {"http://www.google.com/..test", 1036 "", 1037 "", 1038 L"", 1039 L"test"}, 1040 }; 1041 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 1042 #if defined(OS_WIN) 1043 FilePath default_name(test_cases[i].default_filename); 1044 #else 1045 FilePath default_name( 1046 base::SysWideToNativeMB(test_cases[i].default_filename)); 1047 #endif 1048 FilePath filename = net::GetSuggestedFilename( 1049 GURL(test_cases[i].url), test_cases[i].content_disp_header, 1050 test_cases[i].referrer_charset, default_name); 1051 #if defined(OS_WIN) 1052 EXPECT_EQ(std::wstring(test_cases[i].expected_filename), filename.value()) 1053 #else 1054 EXPECT_EQ(base::SysWideToNativeMB(test_cases[i].expected_filename), 1055 filename.value()) 1056 #endif 1057 << "Iteration " << i << ": " << test_cases[i].url; 1058 } 1059 } 1060 1061 // This is currently a windows specific function. 1062 #if defined(OS_WIN) 1063 namespace { 1064 1065 struct GetDirectoryListingEntryCase { 1066 const wchar_t* name; 1067 const char* raw_bytes; 1068 bool is_dir; 1069 int64 filesize; 1070 base::Time time; 1071 const char* expected; 1072 }; 1073 1074 } // namespace 1075 TEST(NetUtilTest, GetDirectoryListingEntry) { 1076 const GetDirectoryListingEntryCase test_cases[] = { 1077 {L"Foo", 1078 "", 1079 false, 1080 10000, 1081 base::Time(), 1082 "<script>addRow(\"Foo\",\"Foo\",0,\"9.8 kB\",\"\");</script>\n"}, 1083 {L"quo\"tes", 1084 "", 1085 false, 1086 10000, 1087 base::Time(), 1088 "<script>addRow(\"quo\\\"tes\",\"quo%22tes\",0,\"9.8 kB\",\"\");</script>" 1089 "\n"}, 1090 {L"quo\"tes", 1091 "quo\"tes", 1092 false, 1093 10000, 1094 base::Time(), 1095 "<script>addRow(\"quo\\\"tes\",\"quo%22tes\",0,\"9.8 kB\",\"\");</script>" 1096 "\n"}, 1097 // U+D55C0 U+AE00. raw_bytes is empty (either a local file with 1098 // UTF-8/UTF-16 encoding or a remote file on an ftp server using UTF-8 1099 {L"\xD55C\xAE00.txt", 1100 "", 1101 false, 1102 10000, 1103 base::Time(), 1104 "<script>addRow(\"\\uD55C\\uAE00.txt\",\"%ED%95%9C%EA%B8%80.txt\"" 1105 ",0,\"9.8 kB\",\"\");</script>\n"}, 1106 // U+D55C0 U+AE00. raw_bytes is the corresponding EUC-KR sequence: 1107 // a local or remote file in EUC-KR. 1108 {L"\xD55C\xAE00.txt", 1109 "\xC7\xD1\xB1\xDB.txt", 1110 false, 1111 10000, 1112 base::Time(), 1113 "<script>addRow(\"\\uD55C\\uAE00.txt\",\"%C7%D1%B1%DB.txt\"" 1114 ",0,\"9.8 kB\",\"\");</script>\n"}, 1115 }; 1116 1117 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { 1118 const std::string results = net::GetDirectoryListingEntry( 1119 WideToUTF16(test_cases[i].name), 1120 test_cases[i].raw_bytes, 1121 test_cases[i].is_dir, 1122 test_cases[i].filesize, 1123 test_cases[i].time); 1124 EXPECT_EQ(test_cases[i].expected, results); 1125 } 1126 } 1127 1128 #endif 1129 1130 TEST(NetUtilTest, ParseHostAndPort) { 1131 const struct { 1132 const char* input; 1133 bool success; 1134 const char* expected_host; 1135 int expected_port; 1136 } tests[] = { 1137 // Valid inputs: 1138 {"foo:10", true, "foo", 10}, 1139 {"foo", true, "foo", -1}, 1140 { 1141 "[1080:0:0:0:8:800:200C:4171]:11", 1142 true, 1143 "[1080:0:0:0:8:800:200C:4171]", 1144 11, 1145 }, 1146 // Invalid inputs: 1147 {"foo:bar", false, "", -1}, 1148 {"foo:", false, "", -1}, 1149 {":", false, "", -1}, 1150 {":80", false, "", -1}, 1151 {"", false, "", -1}, 1152 {"porttoolong:300000", false, "", -1}, 1153 {"usrname@host", false, "", -1}, 1154 {"usrname:password@host", false, "", -1}, 1155 {":password@host", false, "", -1}, 1156 {":password@host:80", false, "", -1}, 1157 {":password@host", false, "", -1}, 1158 {"@host", false, "", -1}, 1159 }; 1160 1161 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 1162 std::string host; 1163 int port; 1164 bool ok = net::ParseHostAndPort(tests[i].input, &host, &port); 1165 1166 EXPECT_EQ(tests[i].success, ok); 1167 1168 if (tests[i].success) { 1169 EXPECT_EQ(tests[i].expected_host, host); 1170 EXPECT_EQ(tests[i].expected_port, port); 1171 } 1172 } 1173 } 1174 1175 TEST(NetUtilTest, GetHostAndPort) { 1176 const struct { 1177 GURL url; 1178 const char* expected_host_and_port; 1179 } tests[] = { 1180 { GURL("http://www.foo.com/x"), "www.foo.com:80"}, 1181 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, 1182 1183 // For IPv6 literals should always include the brackets. 1184 { GURL("http://[1::2]/x"), "[1::2]:80"}, 1185 { GURL("http://[::a]:33/x"), "[::a]:33"}, 1186 }; 1187 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 1188 std::string host_and_port = net::GetHostAndPort(tests[i].url); 1189 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); 1190 } 1191 } 1192 1193 TEST(NetUtilTest, GetHostAndOptionalPort) { 1194 const struct { 1195 GURL url; 1196 const char* expected_host_and_port; 1197 } tests[] = { 1198 { GURL("http://www.foo.com/x"), "www.foo.com"}, 1199 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, 1200 1201 // For IPv6 literals should always include the brackets. 1202 { GURL("http://[1::2]/x"), "[1::2]"}, 1203 { GURL("http://[::a]:33/x"), "[::a]:33"}, 1204 }; 1205 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 1206 std::string host_and_port = net::GetHostAndOptionalPort(tests[i].url); 1207 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); 1208 } 1209 } 1210 1211 1212 TEST(NetUtilTest, NetAddressToString_IPv4) { 1213 const struct { 1214 uint8 addr[4]; 1215 const char* result; 1216 } tests[] = { 1217 {{0, 0, 0, 0}, "0.0.0.0"}, 1218 {{127, 0, 0, 1}, "127.0.0.1"}, 1219 {{192, 168, 0, 1}, "192.168.0.1"}, 1220 }; 1221 1222 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 1223 const addrinfo* ai = GetIPv4Address(tests[i].addr); 1224 std::string result = net::NetAddressToString(ai); 1225 EXPECT_EQ(std::string(tests[i].result), result); 1226 } 1227 } 1228 1229 TEST(NetUtilTest, NetAddressToString_IPv6) { 1230 const struct { 1231 uint8 addr[16]; 1232 const char* result; 1233 } tests[] = { 1234 {{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, 1235 0x98, 0x76, 0x54, 0x32, 0x10}, 1236 "fedc:ba98:7654:3210:fedc:ba98:7654:3210"}, 1237 }; 1238 1239 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 1240 const addrinfo* ai = GetIPv6Address(tests[i].addr); 1241 std::string result = net::NetAddressToString(ai); 1242 // Allow NetAddressToString() to fail, in case the system doesn't 1243 // support IPv6. 1244 if (!result.empty()) 1245 EXPECT_EQ(std::string(tests[i].result), result); 1246 } 1247 } 1248 1249 TEST(NetUtilTest, GetHostName) { 1250 // We can't check the result of GetHostName() directly, since the result 1251 // will differ across machines. Our goal here is to simply exercise the 1252 // code path, and check that things "look about right". 1253 std::string hostname = net::GetHostName(); 1254 EXPECT_FALSE(hostname.empty()); 1255 } 1256 1257 TEST(NetUtilTest, FormatUrl) { 1258 const UrlTestData tests[] = { 1259 {"Empty URL", "", L"", true, UnescapeRule::NORMAL, L"", 0}, 1260 1261 {"Simple URL", 1262 "http://www.google.com/", L"", true, UnescapeRule::NORMAL, 1263 L"http://www.google.com/", 7}, 1264 1265 {"With a port number and a reference", 1266 "http://www.google.com:8080/#\xE3\x82\xB0", L"", true, 1267 UnescapeRule::NORMAL, 1268 L"http://www.google.com:8080/#\x30B0", 7}, 1269 1270 // -------- IDN tests -------- 1271 {"Japanese IDN with ja", 1272 "http://xn--l8jvb1ey91xtjb.jp", L"ja", true, UnescapeRule::NORMAL, 1273 L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, 1274 1275 {"Japanese IDN with en", 1276 "http://xn--l8jvb1ey91xtjb.jp", L"en", true, UnescapeRule::NORMAL, 1277 L"http://xn--l8jvb1ey91xtjb.jp/", 7}, 1278 1279 {"Japanese IDN without any languages", 1280 "http://xn--l8jvb1ey91xtjb.jp", L"", true, UnescapeRule::NORMAL, 1281 // Single script is safe for empty languages. 1282 L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, 1283 1284 {"mailto: with Japanese IDN", 1285 "mailto:foo (at) xn--l8jvb1ey91xtjb.jp", L"ja", true, UnescapeRule::NORMAL, 1286 // GURL doesn't assume an email address's domain part as a host name. 1287 L"mailto:foo (at) xn--l8jvb1ey91xtjb.jp", 7}, 1288 1289 {"file: with Japanese IDN", 1290 "file://xn--l8jvb1ey91xtjb.jp/config.sys", L"ja", true, 1291 UnescapeRule::NORMAL, 1292 L"file://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 7}, 1293 1294 {"ftp: with Japanese IDN", 1295 "ftp://xn--l8jvb1ey91xtjb.jp/config.sys", L"ja", true, 1296 UnescapeRule::NORMAL, 1297 L"ftp://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 6}, 1298 1299 // -------- omit_username_password flag tests -------- 1300 {"With username and password, omit_username_password=false", 1301 "http://user:passwd@example.com/foo", L"", false, UnescapeRule::NORMAL, 1302 L"http://user:passwd@example.com/foo", 19}, 1303 1304 {"With username and password, omit_username_password=true", 1305 "http://user:passwd@example.com/foo", L"", true, UnescapeRule::NORMAL, 1306 L"http://example.com/foo", 7}, 1307 1308 {"With username and no password", 1309 "http://user@example.com/foo", L"", true, UnescapeRule::NORMAL, 1310 L"http://example.com/foo", 7}, 1311 1312 {"Just '@' without username and password", 1313 "http://@example.com/foo", L"", true, UnescapeRule::NORMAL, 1314 L"http://example.com/foo", 7}, 1315 1316 // GURL doesn't think local-part of an email address is username for URL. 1317 {"mailto:, omit_username_password=true", 1318 "mailto:foo (at) example.com", L"", true, UnescapeRule::NORMAL, 1319 L"mailto:foo (at) example.com", 7}, 1320 1321 // -------- unescape flag tests -------- 1322 {"Do not unescape", 1323 "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/" 1324 "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" 1325 "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", L"en", true, 1326 UnescapeRule::NONE, 1327 // GURL parses %-encoded hostnames into Punycode. 1328 L"http://xn--qcka1pmc.jp/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" 1329 L"?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", 7}, 1330 1331 {"Unescape normally", 1332 "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/" 1333 "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" 1334 "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", L"en", true, 1335 UnescapeRule::NORMAL, 1336 L"http://xn--qcka1pmc.jp/\x30B0\x30FC\x30B0\x30EB" 1337 L"?q=\x30B0\x30FC\x30B0\x30EB", 7}, 1338 1339 {"Unescape normally including unescape spaces", 1340 "http://www.google.com/search?q=Hello%20World", L"en", true, 1341 UnescapeRule::SPACES, 1342 L"http://www.google.com/search?q=Hello World", 7}, 1343 1344 /* 1345 {"unescape=true with some special characters", 1346 "http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", L"", false, true, 1347 L"http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", 25}, 1348 */ 1349 // Disabled: the resultant URL becomes "...user%253A:%2540passwd...". 1350 1351 // -------- view-source: -------- 1352 {"view-source", 1353 "view-source:http://xn--qcka1pmc.jp/", L"ja", true, UnescapeRule::NORMAL, 1354 L"view-source:http://\x30B0\x30FC\x30B0\x30EB.jp/", 12 + 7}, 1355 1356 {"view-source of view-source", 1357 "view-source:view-source:http://xn--qcka1pmc.jp/", L"ja", true, 1358 UnescapeRule::NORMAL, 1359 L"view-source:view-source:http://xn--qcka1pmc.jp/", 12}, 1360 }; 1361 1362 for (size_t i = 0; i < arraysize(tests); ++i) { 1363 size_t prefix_len; 1364 std::wstring formatted = net::FormatUrl( 1365 GURL(tests[i].input), tests[i].languages, tests[i].omit, 1366 tests[i].escape_rules, NULL, &prefix_len, NULL); 1367 EXPECT_EQ(tests[i].output, formatted) << tests[i].description; 1368 EXPECT_EQ(tests[i].prefix_len, prefix_len) << tests[i].description; 1369 } 1370 } 1371 1372 TEST(NetUtilTest, FormatUrlParsed) { 1373 // No unescape case. 1374 url_parse::Parsed parsed; 1375 std::wstring formatted = net::FormatUrl( 1376 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" 1377 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), 1378 L"ja", false, UnescapeRule::NONE, &parsed, NULL, NULL); 1379 EXPECT_EQ(L"http://%E3%82%B0:%E3%83%BC@\x30B0\x30FC\x30B0\x30EB.jp:8080" 1380 L"/%E3%82%B0/?q=%E3%82%B0#\x30B0", formatted); 1381 EXPECT_EQ(L"%E3%82%B0", 1382 formatted.substr(parsed.username.begin, parsed.username.len)); 1383 EXPECT_EQ(L"%E3%83%BC", 1384 formatted.substr(parsed.password.begin, parsed.password.len)); 1385 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp", 1386 formatted.substr(parsed.host.begin, parsed.host.len)); 1387 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len)); 1388 EXPECT_EQ(L"/%E3%82%B0/", 1389 formatted.substr(parsed.path.begin, parsed.path.len)); 1390 EXPECT_EQ(L"q=%E3%82%B0", 1391 formatted.substr(parsed.query.begin, parsed.query.len)); 1392 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len)); 1393 1394 // Unescape case. 1395 formatted = net::FormatUrl( 1396 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" 1397 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), 1398 L"ja", false, UnescapeRule::NORMAL, &parsed, NULL, NULL); 1399 EXPECT_EQ(L"http://\x30B0:\x30FC@\x30B0\x30FC\x30B0\x30EB.jp:8080" 1400 L"/\x30B0/?q=\x30B0#\x30B0", formatted); 1401 EXPECT_EQ(L"\x30B0", 1402 formatted.substr(parsed.username.begin, parsed.username.len)); 1403 EXPECT_EQ(L"\x30FC", 1404 formatted.substr(parsed.password.begin, parsed.password.len)); 1405 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp", 1406 formatted.substr(parsed.host.begin, parsed.host.len)); 1407 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len)); 1408 EXPECT_EQ(L"/\x30B0/", formatted.substr(parsed.path.begin, parsed.path.len)); 1409 EXPECT_EQ(L"q=\x30B0", 1410 formatted.substr(parsed.query.begin, parsed.query.len)); 1411 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len)); 1412 1413 // Omit_username_password + unescape case. 1414 formatted = net::FormatUrl( 1415 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" 1416 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), 1417 L"ja", true, UnescapeRule::NORMAL, &parsed, NULL, NULL); 1418 EXPECT_EQ(L"http://\x30B0\x30FC\x30B0\x30EB.jp:8080" 1419 L"/\x30B0/?q=\x30B0#\x30B0", formatted); 1420 EXPECT_FALSE(parsed.username.is_valid()); 1421 EXPECT_FALSE(parsed.password.is_valid()); 1422 EXPECT_EQ(L"\x30B0\x30FC\x30B0\x30EB.jp", 1423 formatted.substr(parsed.host.begin, parsed.host.len)); 1424 EXPECT_EQ(L"8080", formatted.substr(parsed.port.begin, parsed.port.len)); 1425 EXPECT_EQ(L"/\x30B0/", formatted.substr(parsed.path.begin, parsed.path.len)); 1426 EXPECT_EQ(L"q=\x30B0", 1427 formatted.substr(parsed.query.begin, parsed.query.len)); 1428 EXPECT_EQ(L"\x30B0", formatted.substr(parsed.ref.begin, parsed.ref.len)); 1429 1430 // View-source case. 1431 formatted = net::FormatUrl( 1432 GURL("view-source:http://user:passwd@host:81/path?query#ref"), 1433 L"", true, UnescapeRule::NORMAL, &parsed, NULL, NULL); 1434 EXPECT_EQ(L"view-source:http://host:81/path?query#ref", formatted); 1435 EXPECT_EQ(L"view-source:http", 1436 formatted.substr(parsed.scheme.begin, parsed.scheme.len)); 1437 EXPECT_FALSE(parsed.username.is_valid()); 1438 EXPECT_FALSE(parsed.password.is_valid()); 1439 EXPECT_EQ(L"host", formatted.substr(parsed.host.begin, parsed.host.len)); 1440 EXPECT_EQ(L"81", formatted.substr(parsed.port.begin, parsed.port.len)); 1441 EXPECT_EQ(L"/path", formatted.substr(parsed.path.begin, parsed.path.len)); 1442 EXPECT_EQ(L"query", formatted.substr(parsed.query.begin, parsed.query.len)); 1443 EXPECT_EQ(L"ref", formatted.substr(parsed.ref.begin, parsed.ref.len)); 1444 } 1445 1446 TEST(NetUtilTest, FormatUrlAdjustOffset) { 1447 const AdjustOffsetCase basic_cases[] = { 1448 {0, 0}, 1449 {3, 3}, 1450 {5, 5}, 1451 {6, 6}, 1452 {13, 13}, 1453 {21, 21}, 1454 {22, 22}, 1455 {23, 23}, 1456 {25, 25}, 1457 {26, std::wstring::npos}, 1458 {500000, std::wstring::npos}, 1459 {std::wstring::npos, std::wstring::npos}, 1460 }; 1461 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(basic_cases); ++i) { 1462 size_t offset = basic_cases[i].input_offset; 1463 net::FormatUrl(GURL("http://www.google.com/foo/"), L"en", true, 1464 UnescapeRule::NORMAL, NULL, NULL, &offset); 1465 EXPECT_EQ(basic_cases[i].output_offset, offset); 1466 } 1467 1468 const struct { 1469 const char* input_url; 1470 size_t input_offset; 1471 size_t output_offset; 1472 } omit_auth_cases[] = { 1473 {"http://foo:bar@www.google.com/", 6, 6}, 1474 {"http://foo:bar@www.google.com/", 7, 7}, 1475 {"http://foo:bar@www.google.com/", 8, std::wstring::npos}, 1476 {"http://foo:bar@www.google.com/", 10, std::wstring::npos}, 1477 {"http://foo:bar@www.google.com/", 11, std::wstring::npos}, 1478 {"http://foo:bar@www.google.com/", 14, std::wstring::npos}, 1479 {"http://foo:bar@www.google.com/", 15, 7}, 1480 {"http://foo:bar@www.google.com/", 25, 17}, 1481 {"http://foo@www.google.com/", 9, std::wstring::npos}, 1482 {"http://foo@www.google.com/", 11, 7}, 1483 }; 1484 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(omit_auth_cases); ++i) { 1485 size_t offset = omit_auth_cases[i].input_offset; 1486 net::FormatUrl(GURL(omit_auth_cases[i].input_url), L"en", true, 1487 UnescapeRule::NORMAL, NULL, NULL, &offset); 1488 EXPECT_EQ(omit_auth_cases[i].output_offset, offset); 1489 } 1490 1491 const AdjustOffsetCase view_source_cases[] = { 1492 {0, 0}, 1493 {3, 3}, 1494 {11, 11}, 1495 {12, 12}, 1496 {13, 13}, 1497 {19, 19}, 1498 {20, std::wstring::npos}, 1499 {23, 19}, 1500 {26, 22}, 1501 {std::wstring::npos, std::wstring::npos}, 1502 }; 1503 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(view_source_cases); ++i) { 1504 size_t offset = view_source_cases[i].input_offset; 1505 net::FormatUrl(GURL("view-source:http://foo@www.google.com/"), L"en", true, 1506 UnescapeRule::NORMAL, NULL, NULL, &offset); 1507 EXPECT_EQ(view_source_cases[i].output_offset, offset); 1508 } 1509 1510 const AdjustOffsetCase idn_hostname_cases[] = { 1511 {8, std::wstring::npos}, 1512 {16, std::wstring::npos}, 1513 {24, std::wstring::npos}, 1514 {25, 12}, 1515 {30, 17}, 1516 }; 1517 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_hostname_cases); ++i) { 1518 size_t offset = idn_hostname_cases[i].input_offset; 1519 // "http://\x671d\x65e5\x3042\x3055\x3072.jp/foo/" 1520 net::FormatUrl(GURL("http://xn--l8jvb1ey91xtjb.jp/foo/"), L"ja", true, 1521 UnescapeRule::NORMAL, NULL, NULL, &offset); 1522 EXPECT_EQ(idn_hostname_cases[i].output_offset, offset); 1523 } 1524 1525 const AdjustOffsetCase unescape_cases[] = { 1526 {25, 25}, 1527 {26, std::wstring::npos}, 1528 {27, std::wstring::npos}, 1529 {28, 26}, 1530 {35, std::wstring::npos}, 1531 {41, 31}, 1532 {59, 33}, 1533 {60, std::wstring::npos}, 1534 {67, std::wstring::npos}, 1535 {68, std::wstring::npos}, 1536 }; 1537 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(unescape_cases); ++i) { 1538 size_t offset = unescape_cases[i].input_offset; 1539 // "http://www.google.com/foo bar/\x30B0\x30FC\x30B0\x30EB" 1540 net::FormatUrl(GURL( 1541 "http://www.google.com/foo%20bar/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB"), 1542 L"en", true, UnescapeRule::SPACES, NULL, NULL, &offset); 1543 EXPECT_EQ(unescape_cases[i].output_offset, offset); 1544 } 1545 1546 const AdjustOffsetCase ref_cases[] = { 1547 {30, 30}, 1548 {31, 31}, 1549 {32, std::wstring::npos}, 1550 {34, 32}, 1551 {37, 33}, 1552 {38, std::wstring::npos}, 1553 }; 1554 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(ref_cases); ++i) { 1555 size_t offset = ref_cases[i].input_offset; 1556 // "http://www.google.com/foo.html#\x30B0\x30B0z" 1557 net::FormatUrl(GURL( 1558 "http://www.google.com/foo.html#\xE3\x82\xB0\xE3\x82\xB0z"), L"en", 1559 true, UnescapeRule::NORMAL, NULL, NULL, &offset); 1560 EXPECT_EQ(ref_cases[i].output_offset, offset); 1561 } 1562 } 1563 1564 TEST(NetUtilTest, SimplifyUrlForRequest) { 1565 struct { 1566 const char* input_url; 1567 const char* expected_simplified_url; 1568 } tests[] = { 1569 { 1570 // Reference section should be stripped. 1571 "http://www.google.com:78/foobar?query=1#hash", 1572 "http://www.google.com:78/foobar?query=1", 1573 }, 1574 { 1575 // Reference section can itself contain #. 1576 "http://192.168.0.1?query=1#hash#10#11#13#14", 1577 "http://192.168.0.1?query=1", 1578 }, 1579 { // Strip username/password. 1580 "http://user:pass@google.com", 1581 "http://google.com/", 1582 }, 1583 { // Strip both the reference and the username/password. 1584 "http://user:pass@google.com:80/sup?yo#X#X", 1585 "http://google.com/sup?yo", 1586 }, 1587 { // Try an HTTPS URL -- strip both the reference and the username/password. 1588 "https://user:pass@google.com:80/sup?yo#X#X", 1589 "https://google.com:80/sup?yo", 1590 }, 1591 { // Try an FTP URL -- strip both the reference and the username/password. 1592 "ftp://user:pass@google.com:80/sup?yo#X#X", 1593 "ftp://google.com:80/sup?yo", 1594 }, 1595 { // Try an standard URL with unknow scheme. 1596 "foobar://user:pass (at) google.com:80/sup?yo#X#X", 1597 "foobar://google.com:80/sup?yo", 1598 }, 1599 }; 1600 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { 1601 SCOPED_TRACE(StringPrintf("Test[%" PRIuS "]: %s", i, tests[i].input_url)); 1602 GURL input_url(GURL(tests[i].input_url)); 1603 GURL expected_url(GURL(tests[i].expected_simplified_url)); 1604 EXPECT_EQ(expected_url, net::SimplifyUrlForRequest(input_url)); 1605 } 1606 } 1607 1608 TEST(NetUtilTest, SetExplicitlyAllowedPortsTest) { 1609 std::wstring invalid[] = { L"1,2,a", L"'1','2'", L"1, 2, 3", L"1 0,11,12" }; 1610 std::wstring valid[] = { L"", L"1", L"1,2", L"1,2,3", L"10,11,12,13" }; 1611 1612 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(invalid); ++i) { 1613 net::SetExplicitlyAllowedPorts(invalid[i]); 1614 EXPECT_EQ(0, static_cast<int>(net::explicitly_allowed_ports.size())); 1615 } 1616 1617 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid); ++i) { 1618 net::SetExplicitlyAllowedPorts(valid[i]); 1619 EXPECT_EQ(i, net::explicitly_allowed_ports.size()); 1620 } 1621 } 1622