1 // Copyright (c) 2011 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 <stdlib.h> 6 7 #include "base/basictypes.h" 8 #include "base/file_util.h" 9 #include "base/path_service.h" 10 #include "base/strings/string_util.h" 11 #include "base/strings/utf_string_conversions.h" 12 #include "chrome/common/chrome_paths.h" 13 #include "chrome/common/net/url_fixer_upper.h" 14 #include "net/base/net_util.h" 15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "url/gurl.h" 17 #include "url/url_parse.h" 18 19 namespace { 20 class URLFixerUpperTest : public testing::Test { 21 }; 22 }; 23 24 namespace url_parse { 25 26 std::ostream& operator<<(std::ostream& os, const Component& part) { 27 return os << "(begin=" << part.begin << ", len=" << part.len << ")"; 28 } 29 30 } // namespace url_parse 31 32 struct SegmentCase { 33 const std::string input; 34 const std::string result; 35 const url_parse::Component scheme; 36 const url_parse::Component username; 37 const url_parse::Component password; 38 const url_parse::Component host; 39 const url_parse::Component port; 40 const url_parse::Component path; 41 const url_parse::Component query; 42 const url_parse::Component ref; 43 }; 44 45 static const SegmentCase segment_cases[] = { 46 { "http://www.google.com/", "http", 47 url_parse::Component(0, 4), // scheme 48 url_parse::Component(), // username 49 url_parse::Component(), // password 50 url_parse::Component(7, 14), // host 51 url_parse::Component(), // port 52 url_parse::Component(21, 1), // path 53 url_parse::Component(), // query 54 url_parse::Component(), // ref 55 }, 56 { "aBoUt:vErSiOn", "about", 57 url_parse::Component(0, 5), // scheme 58 url_parse::Component(), // username 59 url_parse::Component(), // password 60 url_parse::Component(6, 7), // host 61 url_parse::Component(), // port 62 url_parse::Component(), // path 63 url_parse::Component(), // query 64 url_parse::Component(), // ref 65 }, 66 { "about:host/path?query#ref", "about", 67 url_parse::Component(0, 5), // scheme 68 url_parse::Component(), // username 69 url_parse::Component(), // password 70 url_parse::Component(6, 4), // host 71 url_parse::Component(), // port 72 url_parse::Component(10, 5), // path 73 url_parse::Component(16, 5), // query 74 url_parse::Component(22, 3), // ref 75 }, 76 { "about://host/path?query#ref", "about", 77 url_parse::Component(0, 5), // scheme 78 url_parse::Component(), // username 79 url_parse::Component(), // password 80 url_parse::Component(8, 4), // host 81 url_parse::Component(), // port 82 url_parse::Component(12, 5), // path 83 url_parse::Component(18, 5), // query 84 url_parse::Component(24, 3), // ref 85 }, 86 { "chrome:host/path?query#ref", "chrome", 87 url_parse::Component(0, 6), // scheme 88 url_parse::Component(), // username 89 url_parse::Component(), // password 90 url_parse::Component(7, 4), // host 91 url_parse::Component(), // port 92 url_parse::Component(11, 5), // path 93 url_parse::Component(17, 5), // query 94 url_parse::Component(23, 3), // ref 95 }, 96 { "chrome://host/path?query#ref", "chrome", 97 url_parse::Component(0, 6), // scheme 98 url_parse::Component(), // username 99 url_parse::Component(), // password 100 url_parse::Component(9, 4), // host 101 url_parse::Component(), // port 102 url_parse::Component(13, 5), // path 103 url_parse::Component(19, 5), // query 104 url_parse::Component(25, 3), // ref 105 }, 106 { " www.google.com:124?foo#", "http", 107 url_parse::Component(), // scheme 108 url_parse::Component(), // username 109 url_parse::Component(), // password 110 url_parse::Component(4, 14), // host 111 url_parse::Component(19, 3), // port 112 url_parse::Component(), // path 113 url_parse::Component(23, 3), // query 114 url_parse::Component(27, 0), // ref 115 }, 116 { "user (at) www.google.com", "http", 117 url_parse::Component(), // scheme 118 url_parse::Component(0, 4), // username 119 url_parse::Component(), // password 120 url_parse::Component(5, 14), // host 121 url_parse::Component(), // port 122 url_parse::Component(), // path 123 url_parse::Component(), // query 124 url_parse::Component(), // ref 125 }, 126 { "ftp:/user:P:a$$Wd (at) ..ftp.google.com...::23///pub?foo#bar", "ftp", 127 url_parse::Component(0, 3), // scheme 128 url_parse::Component(5, 4), // username 129 url_parse::Component(10, 7), // password 130 url_parse::Component(18, 20), // host 131 url_parse::Component(39, 2), // port 132 url_parse::Component(41, 6), // path 133 url_parse::Component(48, 3), // query 134 url_parse::Component(52, 3), // ref 135 }, 136 { "[2001:db8::1]/path", "http", 137 url_parse::Component(), // scheme 138 url_parse::Component(), // username 139 url_parse::Component(), // password 140 url_parse::Component(0, 13), // host 141 url_parse::Component(), // port 142 url_parse::Component(13, 5), // path 143 url_parse::Component(), // query 144 url_parse::Component(), // ref 145 }, 146 { "[::1]", "http", 147 url_parse::Component(), // scheme 148 url_parse::Component(), // username 149 url_parse::Component(), // password 150 url_parse::Component(0, 5), // host 151 url_parse::Component(), // port 152 url_parse::Component(), // path 153 url_parse::Component(), // query 154 url_parse::Component(), // ref 155 }, 156 // Incomplete IPv6 addresses (will not canonicalize). 157 { "[2001:4860:", "http", 158 url_parse::Component(), // scheme 159 url_parse::Component(), // username 160 url_parse::Component(), // password 161 url_parse::Component(0, 11), // host 162 url_parse::Component(), // port 163 url_parse::Component(), // path 164 url_parse::Component(), // query 165 url_parse::Component(), // ref 166 }, 167 { "[2001:4860:/foo", "http", 168 url_parse::Component(), // scheme 169 url_parse::Component(), // username 170 url_parse::Component(), // password 171 url_parse::Component(0, 11), // host 172 url_parse::Component(), // port 173 url_parse::Component(11, 4), // path 174 url_parse::Component(), // query 175 url_parse::Component(), // ref 176 }, 177 { "http://:b005::68]", "http", 178 url_parse::Component(0, 4), // scheme 179 url_parse::Component(), // username 180 url_parse::Component(), // password 181 url_parse::Component(7, 10), // host 182 url_parse::Component(), // port 183 url_parse::Component(), // path 184 url_parse::Component(), // query 185 url_parse::Component(), // ref 186 }, 187 // Can't do anything useful with this. 188 { ":b005::68]", "", 189 url_parse::Component(0, 0), // scheme 190 url_parse::Component(), // username 191 url_parse::Component(), // password 192 url_parse::Component(), // host 193 url_parse::Component(), // port 194 url_parse::Component(), // path 195 url_parse::Component(), // query 196 url_parse::Component(), // ref 197 }, 198 }; 199 200 TEST(URLFixerUpperTest, SegmentURL) { 201 std::string result; 202 url_parse::Parsed parts; 203 204 for (size_t i = 0; i < arraysize(segment_cases); ++i) { 205 SegmentCase value = segment_cases[i]; 206 result = URLFixerUpper::SegmentURL(value.input, &parts); 207 EXPECT_EQ(value.result, result); 208 EXPECT_EQ(value.scheme, parts.scheme); 209 EXPECT_EQ(value.username, parts.username); 210 EXPECT_EQ(value.password, parts.password); 211 EXPECT_EQ(value.host, parts.host); 212 EXPECT_EQ(value.port, parts.port); 213 EXPECT_EQ(value.path, parts.path); 214 EXPECT_EQ(value.query, parts.query); 215 EXPECT_EQ(value.ref, parts.ref); 216 } 217 } 218 219 // Creates a file and returns its full name as well as the decomposed 220 // version. Example: 221 // full_path = "c:\foo\bar.txt" 222 // dir = "c:\foo" 223 // file_name = "bar.txt" 224 static bool MakeTempFile(const base::FilePath& dir, 225 const base::FilePath& file_name, 226 base::FilePath* full_path) { 227 *full_path = dir.Append(file_name); 228 return file_util::WriteFile(*full_path, "", 0) == 0; 229 } 230 231 // Returns true if the given URL is a file: URL that matches the given file 232 static bool IsMatchingFileURL(const std::string& url, 233 const base::FilePath& full_file_path) { 234 if (url.length() <= 8) 235 return false; 236 if (std::string("file:///") != url.substr(0, 8)) 237 return false; // no file:/// prefix 238 if (url.find('\\') != std::string::npos) 239 return false; // contains backslashes 240 241 base::FilePath derived_path; 242 net::FileURLToFilePath(GURL(url), &derived_path); 243 244 return base::FilePath::CompareEqualIgnoreCase(derived_path.value(), 245 full_file_path.value()); 246 } 247 248 struct FixupCase { 249 const std::string input; 250 const std::string desired_tld; 251 const std::string output; 252 } fixup_cases[] = { 253 {"www.google.com", "", "http://www.google.com/"}, 254 {" www.google.com ", "", "http://www.google.com/"}, 255 {" foo.com/asdf bar", "", "http://foo.com/asdf%20%20bar"}, 256 {"..www.google.com..", "", "http://www.google.com./"}, 257 {"http://......", "", "http://....../"}, 258 {"http://host.com:ninety-two/", "", "http://host.com:ninety-two/"}, 259 {"http://host.com:ninety-two?foo", "", "http://host.com:ninety-two/?foo"}, 260 {"google.com:123", "", "http://google.com:123/"}, 261 {"about:", "", "chrome://version/"}, 262 {"about:foo", "", "chrome://foo/"}, 263 {"about:version", "", "chrome://version/"}, 264 {"about:usr:pwd@hst/pth?qry#ref", "", "chrome://usr:pwd@hst/pth?qry#ref"}, 265 {"about://usr:pwd@hst/pth?qry#ref", "", "chrome://usr:pwd@hst/pth?qry#ref"}, 266 {"chrome:usr:pwd@hst/pth?qry#ref", "", "chrome://usr:pwd@hst/pth?qry#ref"}, 267 {"chrome://usr:pwd@hst/pth?qry#ref", "", "chrome://usr:pwd@hst/pth?qry#ref"}, 268 {"www:123", "", "http://www:123/"}, 269 {" www:123", "", "http://www:123/"}, 270 {"www.google.com?foo", "", "http://www.google.com/?foo"}, 271 {"www.google.com#foo", "", "http://www.google.com/#foo"}, 272 {"www.google.com?", "", "http://www.google.com/?"}, 273 {"www.google.com#", "", "http://www.google.com/#"}, 274 {"www.google.com:123?foo#bar", "", "http://www.google.com:123/?foo#bar"}, 275 {"user (at) www.google.com", "", "http://user@www.google.com/"}, 276 {"\xE6\xB0\xB4.com" , "", "http://xn--1rw.com/"}, 277 // It would be better if this next case got treated as http, but I don't see 278 // a clean way to guess this isn't the new-and-exciting "user" scheme. 279 {"user:passwd (at) www.google.com:8080/", "", "user:passwd (at) www.google.com:8080/"}, 280 // {"file:///c:/foo/bar%20baz.txt", "", "file:///C:/foo/bar%20baz.txt"}, 281 {"ftp.google.com", "", "ftp://ftp.google.com/"}, 282 {" ftp.google.com", "", "ftp://ftp.google.com/"}, 283 {"FTP.GooGle.com", "", "ftp://ftp.google.com/"}, 284 {"ftpblah.google.com", "", "http://ftpblah.google.com/"}, 285 {"ftp", "", "http://ftp/"}, 286 {"google.ftp.com", "", "http://google.ftp.com/"}, 287 // URLs which end with 0x85 (NEL in ISO-8859). 288 { "http://google.com/search?q=\xd0\x85", "", 289 "http://google.com/search?q=%D0%85" 290 }, 291 { "http://google.com/search?q=\xec\x97\x85", "", 292 "http://google.com/search?q=%EC%97%85" 293 }, 294 { "http://google.com/search?q=\xf0\x90\x80\x85", "", 295 "http://google.com/search?q=%F0%90%80%85" 296 }, 297 // URLs which end with 0xA0 (non-break space in ISO-8859). 298 { "http://google.com/search?q=\xd0\xa0", "", 299 "http://google.com/search?q=%D0%A0" 300 }, 301 { "http://google.com/search?q=\xec\x97\xa0", "", 302 "http://google.com/search?q=%EC%97%A0" 303 }, 304 { "http://google.com/search?q=\xf0\x90\x80\xa0", "", 305 "http://google.com/search?q=%F0%90%80%A0" 306 }, 307 // URLs containing IPv6 literals. 308 {"[2001:db8::2]", "", "http://[2001:db8::2]/"}, 309 {"[::]:80", "", "http://[::]/"}, 310 {"[::]:80/path", "", "http://[::]/path"}, 311 {"[::]:180/path", "", "http://[::]:180/path"}, 312 // TODO(pmarks): Maybe we should parse bare IPv6 literals someday. 313 {"::1", "", "::1"}, 314 // Semicolon as scheme separator for standard schemes. 315 {"http;//www.google.com/", "", "http://www.google.com/"}, 316 {"about;chrome", "", "chrome://chrome/"}, 317 // Semicolon left as-is for non-standard schemes. 318 {"whatsup;//fool", "", "whatsup://fool"}, 319 // Semicolon left as-is in URL itself. 320 {"http://host/port?query;moar", "", "http://host/port?query;moar"}, 321 // Fewer slashes than expected. 322 {"http;www.google.com/", "", "http://www.google.com/"}, 323 {"http;/www.google.com/", "", "http://www.google.com/"}, 324 // Semicolon at start. 325 {";http://www.google.com/", "", "http://%3Bhttp//www.google.com/"}, 326 }; 327 328 TEST(URLFixerUpperTest, FixupURL) { 329 for (size_t i = 0; i < arraysize(fixup_cases); ++i) { 330 FixupCase value = fixup_cases[i]; 331 EXPECT_EQ(value.output, URLFixerUpper::FixupURL(value.input, 332 value.desired_tld).possibly_invalid_spec()) 333 << "input: " << value.input; 334 } 335 336 // Check the TLD-appending functionality 337 FixupCase tld_cases[] = { 338 {"google", "com", "http://www.google.com/"}, 339 {"google.", "com", "http://www.google.com/"}, 340 {"google..", "com", "http://www.google.com/"}, 341 {".google", "com", "http://www.google.com/"}, 342 {"www.google", "com", "http://www.google.com/"}, 343 {"google.com", "com", "http://google.com/"}, 344 {"http://google", "com", "http://www.google.com/"}, 345 {"..google..", "com", "http://www.google.com/"}, 346 {"http://www.google", "com", "http://www.google.com/"}, 347 {"9999999999999999", "com", "http://www.9999999999999999.com/"}, 348 {"google/foo", "com", "http://www.google.com/foo"}, 349 {"google.com/foo", "com", "http://google.com/foo"}, 350 {"google/?foo=.com", "com", "http://www.google.com/?foo=.com"}, 351 {"www.google/?foo=www.", "com", "http://www.google.com/?foo=www."}, 352 {"google.com/?foo=.com", "com", "http://google.com/?foo=.com"}, 353 {"http://www.google.com", "com", "http://www.google.com/"}, 354 {"google:123", "com", "http://www.google.com:123/"}, 355 {"http://google:123", "com", "http://www.google.com:123/"}, 356 }; 357 for (size_t i = 0; i < arraysize(tld_cases); ++i) { 358 FixupCase value = tld_cases[i]; 359 EXPECT_EQ(value.output, URLFixerUpper::FixupURL(value.input, 360 value.desired_tld).possibly_invalid_spec()); 361 } 362 } 363 364 // Test different types of file inputs to URIFixerUpper::FixupURL. This 365 // doesn't go into the nice array of fixups above since the file input 366 // has to exist. 367 TEST(URLFixerUpperTest, FixupFile) { 368 // this "original" filename is the one we tweak to get all the variations 369 base::FilePath dir; 370 base::FilePath original; 371 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir)); 372 ASSERT_TRUE(MakeTempFile( 373 dir, 374 base::FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")), 375 &original)); 376 377 // reference path 378 GURL golden(net::FilePathToFileURL(original)); 379 380 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic) 381 #if defined(OS_WIN) 382 GURL fixedup(URLFixerUpper::FixupURL(WideToUTF8(original.value()), 383 std::string())); 384 #elif defined(OS_POSIX) 385 GURL fixedup(URLFixerUpper::FixupURL(original.value(), std::string())); 386 #endif 387 EXPECT_EQ(golden, fixedup); 388 389 // TODO(port): Make some equivalent tests for posix. 390 #if defined(OS_WIN) 391 // c|/foo\bar.txt -> file:///c:/foo/bar.txt (pipe allowed instead of colon) 392 std::string cur(WideToUTF8(original.value())); 393 EXPECT_EQ(':', cur[1]); 394 cur[1] = '|'; 395 EXPECT_EQ(golden, URLFixerUpper::FixupURL(cur, std::string())); 396 397 FixupCase file_cases[] = { 398 {"c:\\This%20is a non-existent file.txt", "", 399 "file:///C:/This%2520is%20a%20non-existent%20file.txt"}, 400 401 // \\foo\bar.txt -> file://foo/bar.txt 402 // UNC paths, this file won't exist, but since there are no escapes, it 403 // should be returned just converted to a file: URL. 404 {"\\\\SomeNonexistentHost\\foo\\bar.txt", "", 405 "file://somenonexistenthost/foo/bar.txt"}, 406 // We do this strictly, like IE8, which only accepts this form using 407 // backslashes and not forward ones. Turning "//foo" into "http" matches 408 // Firefox and IE, silly though it may seem (it falls out of adding "http" 409 // as the default protocol if you haven't entered one). 410 {"//SomeNonexistentHost\\foo/bar.txt", "", 411 "http://somenonexistenthost/foo/bar.txt"}, 412 {"file:///C:/foo/bar", "", "file:///C:/foo/bar"}, 413 414 // Much of the work here comes from GURL's canonicalization stage. 415 {"file://C:/foo/bar", "", "file:///C:/foo/bar"}, 416 {"file:c:", "", "file:///C:/"}, 417 {"file:c:WINDOWS", "", "file:///C:/WINDOWS"}, 418 {"file:c|Program Files", "", "file:///C:/Program%20Files"}, 419 {"file:/file", "", "file://file/"}, 420 {"file:////////c:\\foo", "", "file:///C:/foo"}, 421 {"file://server/folder/file", "", "file://server/folder/file"}, 422 423 // These are fixups we don't do, but could consider: 424 // 425 // {"file:///foo:/bar", "", "file://foo/bar"}, 426 // {"file:/\\/server\\folder/file", "", "file://server/folder/file"}, 427 }; 428 #elif defined(OS_POSIX) 429 430 #if defined(OS_MACOSX) 431 #define HOME "/Users/" 432 #else 433 #define HOME "/home/" 434 #endif 435 URLFixerUpper::home_directory_override = "/foo"; 436 FixupCase file_cases[] = { 437 // File URLs go through GURL, which tries to escape intelligently. 438 {"/This%20is a non-existent file.txt", "", 439 "file:///This%2520is%20a%20non-existent%20file.txt"}, 440 // A plain "/" refers to the root. 441 {"/", "", 442 "file:///"}, 443 444 // These rely on the above home_directory_override. 445 {"~", "", 446 "file:///foo"}, 447 {"~/bar", "", 448 "file:///foo/bar"}, 449 450 // References to other users' homedirs. 451 {"~foo", "", 452 "file://" HOME "foo"}, 453 {"~x/blah", "", 454 "file://" HOME "x/blah"}, 455 }; 456 #endif 457 for (size_t i = 0; i < arraysize(file_cases); i++) { 458 EXPECT_EQ(file_cases[i].output, URLFixerUpper::FixupURL(file_cases[i].input, 459 file_cases[i].desired_tld).possibly_invalid_spec()); 460 } 461 462 EXPECT_TRUE(base::DeleteFile(original, false)); 463 } 464 465 TEST(URLFixerUpperTest, FixupRelativeFile) { 466 base::FilePath full_path, dir; 467 base::FilePath file_part( 468 FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt")); 469 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir)); 470 ASSERT_TRUE(MakeTempFile(dir, file_part, &full_path)); 471 full_path = base::MakeAbsoluteFilePath(full_path); 472 ASSERT_FALSE(full_path.empty()); 473 474 // make sure we pass through good URLs 475 for (size_t i = 0; i < arraysize(fixup_cases); ++i) { 476 FixupCase value = fixup_cases[i]; 477 base::FilePath input = base::FilePath::FromUTF8Unsafe(value.input); 478 EXPECT_EQ(value.output, 479 URLFixerUpper::FixupRelativeFile(dir, input).possibly_invalid_spec()); 480 } 481 482 // make sure the existing file got fixed-up to a file URL, and that there 483 // are no backslashes 484 EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir, 485 file_part).possibly_invalid_spec(), full_path)); 486 EXPECT_TRUE(base::DeleteFile(full_path, false)); 487 488 // create a filename we know doesn't exist and make sure it doesn't get 489 // fixed up to a file URL 490 base::FilePath nonexistent_file( 491 FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt")); 492 std::string fixedup(URLFixerUpper::FixupRelativeFile(dir, 493 nonexistent_file).possibly_invalid_spec()); 494 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8)); 495 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file)); 496 497 // make a subdir to make sure relative paths with directories work, also 498 // test spaces: 499 // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt" 500 base::FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir")); 501 base::FilePath sub_file( 502 FILE_PATH_LITERAL("url fixer-upper existing file.txt")); 503 base::FilePath new_dir = dir.Append(sub_dir); 504 base::CreateDirectory(new_dir); 505 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path)); 506 full_path = base::MakeAbsoluteFilePath(full_path); 507 ASSERT_FALSE(full_path.empty()); 508 509 // test file in the subdir 510 base::FilePath relative_file = sub_dir.Append(sub_file); 511 EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir, 512 relative_file).possibly_invalid_spec(), full_path)); 513 514 // test file in the subdir with different slashes and escaping. 515 base::FilePath::StringType relative_file_str = sub_dir.value() + 516 FILE_PATH_LITERAL("/") + sub_file.value(); 517 ReplaceSubstringsAfterOffset(&relative_file_str, 0, 518 FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20")); 519 EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir, 520 base::FilePath(relative_file_str)).possibly_invalid_spec(), full_path)); 521 522 // test relative directories and duplicate slashes 523 // (should resolve to the same file as above) 524 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") + 525 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value(); 526 EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir, 527 base::FilePath(relative_file_str)).possibly_invalid_spec(), full_path)); 528 529 // done with the subdir 530 EXPECT_TRUE(base::DeleteFile(full_path, false)); 531 EXPECT_TRUE(base::DeleteFile(new_dir, true)); 532 533 // Test that an obvious HTTP URL isn't accidentally treated as an absolute 534 // file path (on account of system-specific craziness). 535 base::FilePath empty_path; 536 base::FilePath http_url_path(FILE_PATH_LITERAL("http://../")); 537 EXPECT_TRUE(URLFixerUpper::FixupRelativeFile( 538 empty_path, http_url_path).SchemeIs("http")); 539 } 540