Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(398)

Side by Side Diff: components/url_formatter/url_fixer_unittest.cc

Issue 1171333003: Move net::FormatUrl and friends outside of //net and into //components (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase again now that CQ is fixed Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/url_formatter/url_fixer.cc ('k') | components/url_formatter/url_formatter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdlib.h> 5 #include <stdlib.h>
6 6
7 #include "base/base_paths.h" 7 #include "base/base_paths.h"
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h" 10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h" 11 #include "base/files/scoped_temp_dir.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "components/url_fixer/url_fixer.h" 14 #include "components/url_formatter/url_fixer.h"
15 #include "net/base/filename_util.h" 15 #include "net/base/filename_util.h"
16 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/gurl.h" 17 #include "url/gurl.h"
18 #include "url/third_party/mozilla/url_parse.h" 18 #include "url/third_party/mozilla/url_parse.h"
19 19
20 namespace url { 20 namespace url {
21 21
22 std::ostream& operator<<(std::ostream& os, const Component& part) { 22 std::ostream& operator<<(std::ostream& os, const Component& part) {
23 return os << "(begin=" << part.begin << ", len=" << part.len << ")"; 23 return os << "(begin=" << part.begin << ", len=" << part.len << ")";
24 } 24 }
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 }; 194 };
195 195
196 typedef testing::Test URLFixerTest; 196 typedef testing::Test URLFixerTest;
197 197
198 TEST(URLFixerTest, SegmentURL) { 198 TEST(URLFixerTest, SegmentURL) {
199 std::string result; 199 std::string result;
200 url::Parsed parts; 200 url::Parsed parts;
201 201
202 for (size_t i = 0; i < arraysize(segment_cases); ++i) { 202 for (size_t i = 0; i < arraysize(segment_cases); ++i) {
203 SegmentCase value = segment_cases[i]; 203 SegmentCase value = segment_cases[i];
204 result = url_fixer::SegmentURL(value.input, &parts); 204 result = url_formatter::SegmentURL(value.input, &parts);
205 EXPECT_EQ(value.result, result); 205 EXPECT_EQ(value.result, result);
206 EXPECT_EQ(value.scheme, parts.scheme); 206 EXPECT_EQ(value.scheme, parts.scheme);
207 EXPECT_EQ(value.username, parts.username); 207 EXPECT_EQ(value.username, parts.username);
208 EXPECT_EQ(value.password, parts.password); 208 EXPECT_EQ(value.password, parts.password);
209 EXPECT_EQ(value.host, parts.host); 209 EXPECT_EQ(value.host, parts.host);
210 EXPECT_EQ(value.port, parts.port); 210 EXPECT_EQ(value.port, parts.port);
211 EXPECT_EQ(value.path, parts.path); 211 EXPECT_EQ(value.path, parts.path);
212 EXPECT_EQ(value.query, parts.query); 212 EXPECT_EQ(value.query, parts.query);
213 EXPECT_EQ(value.ref, parts.ref); 213 EXPECT_EQ(value.ref, parts.ref);
214 } 214 }
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 {"http;www.google.com/", "http://www.google.com/"}, 308 {"http;www.google.com/", "http://www.google.com/"},
309 {"http;/www.google.com/", "http://www.google.com/"}, 309 {"http;/www.google.com/", "http://www.google.com/"},
310 // Semicolon at start. 310 // Semicolon at start.
311 {";http://www.google.com/", "http://%3Bhttp//www.google.com/"}, 311 {";http://www.google.com/", "http://%3Bhttp//www.google.com/"},
312 }; 312 };
313 313
314 TEST(URLFixerTest, FixupURL) { 314 TEST(URLFixerTest, FixupURL) {
315 for (size_t i = 0; i < arraysize(fixup_cases); ++i) { 315 for (size_t i = 0; i < arraysize(fixup_cases); ++i) {
316 FixupCase value = fixup_cases[i]; 316 FixupCase value = fixup_cases[i];
317 EXPECT_EQ(value.output, 317 EXPECT_EQ(value.output,
318 url_fixer::FixupURL(value.input, "").possibly_invalid_spec()) 318 url_formatter::FixupURL(value.input, "").possibly_invalid_spec())
319 << "input: " << value.input; 319 << "input: " << value.input;
320 } 320 }
321 321
322 // Check the TLD-appending functionality. 322 // Check the TLD-appending functionality.
323 FixupCase tld_cases[] = { 323 FixupCase tld_cases[] = {
324 {"somedomainthatwillnotbeagtld", 324 {"somedomainthatwillnotbeagtld",
325 "http://www.somedomainthatwillnotbeagtld.com/"}, 325 "http://www.somedomainthatwillnotbeagtld.com/"},
326 {"somedomainthatwillnotbeagtld.", 326 {"somedomainthatwillnotbeagtld.",
327 "http://www.somedomainthatwillnotbeagtld.com/"}, 327 "http://www.somedomainthatwillnotbeagtld.com/"},
328 {"somedomainthatwillnotbeagtld..", 328 {"somedomainthatwillnotbeagtld..",
(...skipping 23 matching lines...) Expand all
352 "http://somedomainthatwillnotbeagtld.com/?foo=.com"}, 352 "http://somedomainthatwillnotbeagtld.com/?foo=.com"},
353 {"http://www.somedomainthatwillnotbeagtld.com", 353 {"http://www.somedomainthatwillnotbeagtld.com",
354 "http://www.somedomainthatwillnotbeagtld.com/"}, 354 "http://www.somedomainthatwillnotbeagtld.com/"},
355 {"somedomainthatwillnotbeagtld:123", 355 {"somedomainthatwillnotbeagtld:123",
356 "http://www.somedomainthatwillnotbeagtld.com:123/"}, 356 "http://www.somedomainthatwillnotbeagtld.com:123/"},
357 {"http://somedomainthatwillnotbeagtld:123", 357 {"http://somedomainthatwillnotbeagtld:123",
358 "http://www.somedomainthatwillnotbeagtld.com:123/"}, 358 "http://www.somedomainthatwillnotbeagtld.com:123/"},
359 }; 359 };
360 for (size_t i = 0; i < arraysize(tld_cases); ++i) { 360 for (size_t i = 0; i < arraysize(tld_cases); ++i) {
361 FixupCase value = tld_cases[i]; 361 FixupCase value = tld_cases[i];
362 EXPECT_EQ(value.output, 362 EXPECT_EQ(value.output, url_formatter::FixupURL(value.input, "com")
363 url_fixer::FixupURL(value.input, "com").possibly_invalid_spec()); 363 .possibly_invalid_spec());
364 } 364 }
365 } 365 }
366 366
367 // Test different types of file inputs to URIFixerUpper::FixupURL. This 367 // Test different types of file inputs to URIFixerUpper::FixupURL. This
368 // doesn't go into the nice array of fixups above since the file input 368 // doesn't go into the nice array of fixups above since the file input
369 // has to exist. 369 // has to exist.
370 TEST(URLFixerTest, FixupFile) { 370 TEST(URLFixerTest, FixupFile) {
371 // this "original" filename is the one we tweak to get all the variations 371 // this "original" filename is the one we tweak to get all the variations
372 base::ScopedTempDir temp_dir_; 372 base::ScopedTempDir temp_dir_;
373 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 373 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
374 base::FilePath original; 374 base::FilePath original;
375 ASSERT_TRUE(MakeTempFile( 375 ASSERT_TRUE(MakeTempFile(
376 temp_dir_.path(), 376 temp_dir_.path(),
377 base::FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")), 377 base::FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")),
378 &original)); 378 &original));
379 379
380 // reference path 380 // reference path
381 GURL golden(net::FilePathToFileURL(original)); 381 GURL golden(net::FilePathToFileURL(original));
382 382
383 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic) 383 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic)
384 GURL fixedup(url_fixer::FixupURL(original.AsUTF8Unsafe(), std::string())); 384 GURL fixedup(url_formatter::FixupURL(original.AsUTF8Unsafe(), std::string()));
385 EXPECT_EQ(golden, fixedup); 385 EXPECT_EQ(golden, fixedup);
386 386
387 // TODO(port): Make some equivalent tests for posix. 387 // TODO(port): Make some equivalent tests for posix.
388 #if defined(OS_WIN) 388 #if defined(OS_WIN)
389 // c|/foo\bar.txt -> file:///c:/foo/bar.txt (pipe allowed instead of colon) 389 // c|/foo\bar.txt -> file:///c:/foo/bar.txt (pipe allowed instead of colon)
390 std::string cur(base::WideToUTF8(original.value())); 390 std::string cur(base::WideToUTF8(original.value()));
391 EXPECT_EQ(':', cur[1]); 391 EXPECT_EQ(':', cur[1]);
392 cur[1] = '|'; 392 cur[1] = '|';
393 EXPECT_EQ(golden, url_fixer::FixupURL(cur, std::string())); 393 EXPECT_EQ(golden, url_formatter::FixupURL(cur, std::string()));
394 394
395 FixupCase cases[] = { 395 FixupCase cases[] = {
396 {"c:\\Non-existent%20file.txt", "file:///C:/Non-existent%2520file.txt"}, 396 {"c:\\Non-existent%20file.txt", "file:///C:/Non-existent%2520file.txt"},
397 397
398 // \\foo\bar.txt -> file://foo/bar.txt 398 // \\foo\bar.txt -> file://foo/bar.txt
399 // UNC paths, this file won't exist, but since there are no escapes, it 399 // UNC paths, this file won't exist, but since there are no escapes, it
400 // should be returned just converted to a file: URL. 400 // should be returned just converted to a file: URL.
401 {"\\\\NonexistentHost\\foo\\bar.txt", "file://nonexistenthost/foo/bar.txt"}, 401 {"\\\\NonexistentHost\\foo\\bar.txt", "file://nonexistenthost/foo/bar.txt"},
402 // We do this strictly, like IE8, which only accepts this form using 402 // We do this strictly, like IE8, which only accepts this form using
403 // backslashes and not forward ones. Turning "//foo" into "http" matches 403 // backslashes and not forward ones. Turning "//foo" into "http" matches
(...skipping 15 matching lines...) Expand all
419 // {"file:///foo:/bar", "file://foo/bar"}, 419 // {"file:///foo:/bar", "file://foo/bar"},
420 // {"file:/\\/server\\folder/file", "file://server/folder/file"}, 420 // {"file:/\\/server\\folder/file", "file://server/folder/file"},
421 }; 421 };
422 #elif defined(OS_POSIX) 422 #elif defined(OS_POSIX)
423 423
424 #if defined(OS_MACOSX) 424 #if defined(OS_MACOSX)
425 #define HOME "/Users/" 425 #define HOME "/Users/"
426 #else 426 #else
427 #define HOME "/home/" 427 #define HOME "/home/"
428 #endif 428 #endif
429 url_fixer::home_directory_override = "/foo"; 429 url_formatter::home_directory_override = "/foo";
430 FixupCase cases[] = { 430 FixupCase cases[] = {
431 // File URLs go through GURL, which tries to escape intelligently. 431 // File URLs go through GURL, which tries to escape intelligently.
432 {"/A%20non-existent file.txt", "file:///A%2520non-existent%20file.txt"}, 432 {"/A%20non-existent file.txt", "file:///A%2520non-existent%20file.txt"},
433 // A plain "/" refers to the root. 433 // A plain "/" refers to the root.
434 {"/", "file:///"}, 434 {"/", "file:///"},
435 435
436 // These rely on the above home_directory_override. 436 // These rely on the above home_directory_override.
437 {"~", "file:///foo"}, 437 {"~", "file:///foo"},
438 {"~/bar", "file:///foo/bar"}, 438 {"~/bar", "file:///foo/bar"},
439 439
440 // References to other users' homedirs. 440 // References to other users' homedirs.
441 {"~foo", "file://" HOME "foo"}, 441 {"~foo", "file://" HOME "foo"},
442 {"~x/blah", "file://" HOME "x/blah"}, 442 {"~x/blah", "file://" HOME "x/blah"},
443 }; 443 };
444 #endif 444 #endif
445 445
446 for (size_t i = 0; i < arraysize(cases); i++) { 446 for (size_t i = 0; i < arraysize(cases); i++) {
447 EXPECT_EQ(cases[i].output, 447 EXPECT_EQ(cases[i].output,
448 url_fixer::FixupURL(cases[i].input, "").possibly_invalid_spec()); 448 url_formatter::FixupURL(cases[i].input, std::string())
449 .possibly_invalid_spec());
449 } 450 }
450 451
451 EXPECT_TRUE(base::DeleteFile(original, false)); 452 EXPECT_TRUE(base::DeleteFile(original, false));
452 } 453 }
453 454
454 TEST(URLFixerTest, FixupRelativeFile) { 455 TEST(URLFixerTest, FixupRelativeFile) {
455 base::FilePath full_path; 456 base::FilePath full_path;
456 base::FilePath file_part( 457 base::FilePath file_part(
457 FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt")); 458 FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt"));
458 base::ScopedTempDir temp_dir_; 459 base::ScopedTempDir temp_dir_;
459 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 460 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
460 ASSERT_TRUE(MakeTempFile(temp_dir_.path(), file_part, &full_path)); 461 ASSERT_TRUE(MakeTempFile(temp_dir_.path(), file_part, &full_path));
461 full_path = base::MakeAbsoluteFilePath(full_path); 462 full_path = base::MakeAbsoluteFilePath(full_path);
462 ASSERT_FALSE(full_path.empty()); 463 ASSERT_FALSE(full_path.empty());
463 464
464 // make sure we pass through good URLs 465 // make sure we pass through good URLs
465 for (size_t i = 0; i < arraysize(fixup_cases); ++i) { 466 for (size_t i = 0; i < arraysize(fixup_cases); ++i) {
466 FixupCase value = fixup_cases[i]; 467 FixupCase value = fixup_cases[i];
467 base::FilePath input = base::FilePath::FromUTF8Unsafe(value.input); 468 base::FilePath input = base::FilePath::FromUTF8Unsafe(value.input);
468 EXPECT_EQ(value.output, 469 EXPECT_EQ(value.output,
469 url_fixer::FixupRelativeFile(temp_dir_.path(), 470 url_formatter::FixupRelativeFile(temp_dir_.path(),
470 input).possibly_invalid_spec()); 471 input).possibly_invalid_spec());
471 } 472 }
472 473
473 // make sure the existing file got fixed-up to a file URL, and that there 474 // make sure the existing file got fixed-up to a file URL, and that there
474 // are no backslashes 475 // are no backslashes
475 EXPECT_TRUE(IsMatchingFileURL( 476 EXPECT_TRUE(IsMatchingFileURL(
476 url_fixer::FixupRelativeFile(temp_dir_.path(), 477 url_formatter::FixupRelativeFile(temp_dir_.path(),
477 file_part).possibly_invalid_spec(), full_path)); 478 file_part).possibly_invalid_spec(), full_path));
478 EXPECT_TRUE(base::DeleteFile(full_path, false)); 479 EXPECT_TRUE(base::DeleteFile(full_path, false));
479 480
480 // create a filename we know doesn't exist and make sure it doesn't get 481 // create a filename we know doesn't exist and make sure it doesn't get
481 // fixed up to a file URL 482 // fixed up to a file URL
482 base::FilePath nonexistent_file( 483 base::FilePath nonexistent_file(
483 FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt")); 484 FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt"));
484 std::string fixedup(url_fixer::FixupRelativeFile( 485 std::string fixedup(url_formatter::FixupRelativeFile(
485 temp_dir_.path(), nonexistent_file).possibly_invalid_spec()); 486 temp_dir_.path(), nonexistent_file).possibly_invalid_spec());
486 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8)); 487 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8));
487 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file)); 488 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file));
488 489
489 // make a subdir to make sure relative paths with directories work, also 490 // make a subdir to make sure relative paths with directories work, also
490 // test spaces: 491 // test spaces:
491 // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt" 492 // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt"
492 base::FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir")); 493 base::FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir"));
493 base::FilePath sub_file( 494 base::FilePath sub_file(
494 FILE_PATH_LITERAL("url fixer-upper existing file.txt")); 495 FILE_PATH_LITERAL("url fixer-upper existing file.txt"));
495 base::FilePath new_dir = temp_dir_.path().Append(sub_dir); 496 base::FilePath new_dir = temp_dir_.path().Append(sub_dir);
496 base::CreateDirectory(new_dir); 497 base::CreateDirectory(new_dir);
497 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path)); 498 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path));
498 full_path = base::MakeAbsoluteFilePath(full_path); 499 full_path = base::MakeAbsoluteFilePath(full_path);
499 ASSERT_FALSE(full_path.empty()); 500 ASSERT_FALSE(full_path.empty());
500 501
501 // test file in the subdir 502 // test file in the subdir
502 base::FilePath relative_file = sub_dir.Append(sub_file); 503 base::FilePath relative_file = sub_dir.Append(sub_file);
503 EXPECT_TRUE(IsMatchingFileURL( 504 EXPECT_TRUE(IsMatchingFileURL(
504 url_fixer::FixupRelativeFile(temp_dir_.path(), 505 url_formatter::FixupRelativeFile(temp_dir_.path(),
505 relative_file).possibly_invalid_spec(), full_path)); 506 relative_file).possibly_invalid_spec(), full_path));
506 507
507 // test file in the subdir with different slashes and escaping. 508 // test file in the subdir with different slashes and escaping.
508 base::FilePath::StringType relative_file_str = sub_dir.value() + 509 base::FilePath::StringType relative_file_str = sub_dir.value() +
509 FILE_PATH_LITERAL("/") + sub_file.value(); 510 FILE_PATH_LITERAL("/") + sub_file.value();
510 base::ReplaceSubstringsAfterOffset(&relative_file_str, 0, 511 base::ReplaceSubstringsAfterOffset(&relative_file_str, 0,
511 FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20")); 512 FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20"));
512 EXPECT_TRUE(IsMatchingFileURL( 513 EXPECT_TRUE(IsMatchingFileURL(
513 url_fixer::FixupRelativeFile(temp_dir_.path(), 514 url_formatter::FixupRelativeFile(temp_dir_.path(),
514 base::FilePath(relative_file_str)).possibly_invalid_spec(), 515 base::FilePath(relative_file_str)).possibly_invalid_spec(),
515 full_path)); 516 full_path));
516 517
517 // test relative directories and duplicate slashes 518 // test relative directories and duplicate slashes
518 // (should resolve to the same file as above) 519 // (should resolve to the same file as above)
519 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") + 520 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") +
520 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value(); 521 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value();
521 EXPECT_TRUE(IsMatchingFileURL( 522 EXPECT_TRUE(IsMatchingFileURL(
522 url_fixer::FixupRelativeFile(temp_dir_.path(), 523 url_formatter::FixupRelativeFile(temp_dir_.path(),
523 base::FilePath(relative_file_str)).possibly_invalid_spec(), 524 base::FilePath(relative_file_str)).possibly_invalid_spec(),
524 full_path)); 525 full_path));
525 526
526 // done with the subdir 527 // done with the subdir
527 EXPECT_TRUE(base::DeleteFile(full_path, false)); 528 EXPECT_TRUE(base::DeleteFile(full_path, false));
528 EXPECT_TRUE(base::DeleteFile(new_dir, true)); 529 EXPECT_TRUE(base::DeleteFile(new_dir, true));
529 530
530 // Test that an obvious HTTP URL isn't accidentally treated as an absolute 531 // Test that an obvious HTTP URL isn't accidentally treated as an absolute
531 // file path (on account of system-specific craziness). 532 // file path (on account of system-specific craziness).
532 base::FilePath empty_path; 533 base::FilePath empty_path;
533 base::FilePath http_url_path(FILE_PATH_LITERAL("http://../")); 534 base::FilePath http_url_path(FILE_PATH_LITERAL("http://../"));
534 EXPECT_TRUE( 535 EXPECT_TRUE(url_formatter::FixupRelativeFile(empty_path, http_url_path)
535 url_fixer::FixupRelativeFile(empty_path, http_url_path).SchemeIs("http")); 536 .SchemeIs("http"));
536 } 537 }
OLDNEW
« no previous file with comments | « components/url_formatter/url_fixer.cc ('k') | components/url_formatter/url_formatter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698