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