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

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

Issue 320253004: Componentize URLFixerUpper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Win64 fix Created 6 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « components/url_fixer/url_fixer.cc ('k') | net/base/net_util_icu.cc » ('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/basictypes.h" 8 #include "base/basictypes.h"
8 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/files/file_path.h"
9 #include "base/path_service.h" 11 #include "base/path_service.h"
10 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/common/chrome_paths.h" 14 #include "components/url_fixer/url_fixer.h"
13 #include "chrome/common/net/url_fixer_upper.h"
14 #include "net/base/filename_util.h" 15 #include "net/base/filename_util.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 #include "url/gurl.h" 17 #include "url/gurl.h"
17 #include "url/url_parse.h" 18 #include "url/url_parse.h"
18 19
19 namespace url { 20 namespace url {
20 21
21 std::ostream& operator<<(std::ostream& os, const Component& part) { 22 std::ostream& operator<<(std::ostream& os, const Component& part) {
22 return os << "(begin=" << part.begin << ", len=" << part.len << ")"; 23 return os << "(begin=" << part.begin << ", len=" << part.len << ")";
23 } 24 }
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 url::Component(), // username 186 url::Component(), // username
186 url::Component(), // password 187 url::Component(), // password
187 url::Component(), // host 188 url::Component(), // host
188 url::Component(), // port 189 url::Component(), // port
189 url::Component(), // path 190 url::Component(), // path
190 url::Component(), // query 191 url::Component(), // query
191 url::Component(), // ref 192 url::Component(), // ref
192 }, 193 },
193 }; 194 };
194 195
195 typedef testing::Test URLFixerUpperTest; 196 typedef testing::Test URLFixerTest;
196 197
197 TEST(URLFixerUpperTest, SegmentURL) { 198 TEST(URLFixerTest, SegmentURL) {
198 std::string result; 199 std::string result;
199 url::Parsed parts; 200 url::Parsed parts;
200 201
201 for (size_t i = 0; i < arraysize(segment_cases); ++i) { 202 for (size_t i = 0; i < arraysize(segment_cases); ++i) {
202 SegmentCase value = segment_cases[i]; 203 SegmentCase value = segment_cases[i];
203 result = URLFixerUpper::SegmentURL(value.input, &parts); 204 result = url_fixer::SegmentURL(value.input, &parts);
204 EXPECT_EQ(value.result, result); 205 EXPECT_EQ(value.result, result);
205 EXPECT_EQ(value.scheme, parts.scheme); 206 EXPECT_EQ(value.scheme, parts.scheme);
206 EXPECT_EQ(value.username, parts.username); 207 EXPECT_EQ(value.username, parts.username);
207 EXPECT_EQ(value.password, parts.password); 208 EXPECT_EQ(value.password, parts.password);
208 EXPECT_EQ(value.host, parts.host); 209 EXPECT_EQ(value.host, parts.host);
209 EXPECT_EQ(value.port, parts.port); 210 EXPECT_EQ(value.port, parts.port);
210 EXPECT_EQ(value.path, parts.path); 211 EXPECT_EQ(value.path, parts.path);
211 EXPECT_EQ(value.query, parts.query); 212 EXPECT_EQ(value.query, parts.query);
212 EXPECT_EQ(value.ref, parts.ref); 213 EXPECT_EQ(value.ref, parts.ref);
213 } 214 }
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 {"whatsup;//fool", "whatsup://fool"}, 304 {"whatsup;//fool", "whatsup://fool"},
304 // Semicolon left as-is in URL itself. 305 // Semicolon left as-is in URL itself.
305 {"http://host/port?query;moar", "http://host/port?query;moar"}, 306 {"http://host/port?query;moar", "http://host/port?query;moar"},
306 // Fewer slashes than expected. 307 // Fewer slashes than expected.
307 {"http;www.google.com/", "http://www.google.com/"}, 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 // Semicolon at start. 310 // Semicolon at start.
310 {";http://www.google.com/", "http://%3Bhttp//www.google.com/"}, 311 {";http://www.google.com/", "http://%3Bhttp//www.google.com/"},
311 }; 312 };
312 313
313 TEST(URLFixerUpperTest, FixupURL) { 314 TEST(URLFixerTest, FixupURL) {
314 for (size_t i = 0; i < arraysize(fixup_cases); ++i) { 315 for (size_t i = 0; i < arraysize(fixup_cases); ++i) {
315 FixupCase value = fixup_cases[i]; 316 FixupCase value = fixup_cases[i];
316 EXPECT_EQ(value.output, 317 EXPECT_EQ(value.output,
317 URLFixerUpper::FixupURL(value.input, "").possibly_invalid_spec()) 318 url_fixer::FixupURL(value.input, "").possibly_invalid_spec())
318 << "input: " << value.input; 319 << "input: " << value.input;
319 } 320 }
320 321
321 // Check the TLD-appending functionality. 322 // Check the TLD-appending functionality.
322 FixupCase tld_cases[] = { 323 FixupCase tld_cases[] = {
323 {"google", "http://www.google.com/"}, 324 {"google", "http://www.google.com/"},
324 {"google.", "http://www.google.com/"}, 325 {"google.", "http://www.google.com/"},
325 {"google..", "http://www.google.com/"}, 326 {"google..", "http://www.google.com/"},
326 {".google", "http://www.google.com/"}, 327 {".google", "http://www.google.com/"},
327 {"www.google", "http://www.google.com/"}, 328 {"www.google", "http://www.google.com/"},
328 {"google.com", "http://google.com/"}, 329 {"google.com", "http://google.com/"},
329 {"http://google", "http://www.google.com/"}, 330 {"http://google", "http://www.google.com/"},
330 {"..google..", "http://www.google.com/"}, 331 {"..google..", "http://www.google.com/"},
331 {"http://www.google", "http://www.google.com/"}, 332 {"http://www.google", "http://www.google.com/"},
332 {"9999999999999999", "http://www.9999999999999999.com/"}, 333 {"9999999999999999", "http://www.9999999999999999.com/"},
333 {"google/foo", "http://www.google.com/foo"}, 334 {"google/foo", "http://www.google.com/foo"},
334 {"google.com/foo", "http://google.com/foo"}, 335 {"google.com/foo", "http://google.com/foo"},
335 {"google/?foo=.com", "http://www.google.com/?foo=.com"}, 336 {"google/?foo=.com", "http://www.google.com/?foo=.com"},
336 {"www.google/?foo=www.", "http://www.google.com/?foo=www."}, 337 {"www.google/?foo=www.", "http://www.google.com/?foo=www."},
337 {"google.com/?foo=.com", "http://google.com/?foo=.com"}, 338 {"google.com/?foo=.com", "http://google.com/?foo=.com"},
338 {"http://www.google.com", "http://www.google.com/"}, 339 {"http://www.google.com", "http://www.google.com/"},
339 {"google:123", "http://www.google.com:123/"}, 340 {"google:123", "http://www.google.com:123/"},
340 {"http://google:123", "http://www.google.com:123/"}, 341 {"http://google:123", "http://www.google.com:123/"},
341 }; 342 };
342 for (size_t i = 0; i < arraysize(tld_cases); ++i) { 343 for (size_t i = 0; i < arraysize(tld_cases); ++i) {
343 FixupCase value = tld_cases[i]; 344 FixupCase value = tld_cases[i];
344 EXPECT_EQ(value.output, 345 EXPECT_EQ(value.output,
345 URLFixerUpper::FixupURL(value.input, "com").possibly_invalid_spec()); 346 url_fixer::FixupURL(value.input, "com").possibly_invalid_spec());
346 } 347 }
347 } 348 }
348 349
349 // Test different types of file inputs to URIFixerUpper::FixupURL. This 350 // Test different types of file inputs to URIFixerUpper::FixupURL. This
350 // doesn't go into the nice array of fixups above since the file input 351 // doesn't go into the nice array of fixups above since the file input
351 // has to exist. 352 // has to exist.
352 TEST(URLFixerUpperTest, FixupFile) { 353 TEST(URLFixerTest, FixupFile) {
353 // this "original" filename is the one we tweak to get all the variations 354 // this "original" filename is the one we tweak to get all the variations
354 base::FilePath dir; 355 base::FilePath dir;
355 base::FilePath original; 356 base::FilePath original;
356 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir)); 357 ASSERT_TRUE(PathService::Get(base::DIR_MODULE, &dir));
357 ASSERT_TRUE(MakeTempFile( 358 ASSERT_TRUE(MakeTempFile(
358 dir, 359 dir,
359 base::FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")), 360 base::FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")),
360 &original)); 361 &original));
361 362
362 // reference path 363 // reference path
363 GURL golden(net::FilePathToFileURL(original)); 364 GURL golden(net::FilePathToFileURL(original));
364 365
365 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic) 366 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic)
366 GURL fixedup(URLFixerUpper::FixupURL(original.AsUTF8Unsafe(), std::string())); 367 GURL fixedup(url_fixer::FixupURL(original.AsUTF8Unsafe(), std::string()));
367 EXPECT_EQ(golden, fixedup); 368 EXPECT_EQ(golden, fixedup);
368 369
369 // TODO(port): Make some equivalent tests for posix. 370 // TODO(port): Make some equivalent tests for posix.
370 #if defined(OS_WIN) 371 #if defined(OS_WIN)
371 // c|/foo\bar.txt -> file:///c:/foo/bar.txt (pipe allowed instead of colon) 372 // c|/foo\bar.txt -> file:///c:/foo/bar.txt (pipe allowed instead of colon)
372 std::string cur(base::WideToUTF8(original.value())); 373 std::string cur(base::WideToUTF8(original.value()));
373 EXPECT_EQ(':', cur[1]); 374 EXPECT_EQ(':', cur[1]);
374 cur[1] = '|'; 375 cur[1] = '|';
375 EXPECT_EQ(golden, URLFixerUpper::FixupURL(cur, std::string())); 376 EXPECT_EQ(golden, url_fixer::FixupURL(cur, std::string()));
376 377
377 FixupCase cases[] = { 378 FixupCase cases[] = {
378 {"c:\\Non-existent%20file.txt", "file:///C:/Non-existent%2520file.txt"}, 379 {"c:\\Non-existent%20file.txt", "file:///C:/Non-existent%2520file.txt"},
379 380
380 // \\foo\bar.txt -> file://foo/bar.txt 381 // \\foo\bar.txt -> file://foo/bar.txt
381 // UNC paths, this file won't exist, but since there are no escapes, it 382 // UNC paths, this file won't exist, but since there are no escapes, it
382 // should be returned just converted to a file: URL. 383 // should be returned just converted to a file: URL.
383 {"\\\\NonexistentHost\\foo\\bar.txt", "file://nonexistenthost/foo/bar.txt"}, 384 {"\\\\NonexistentHost\\foo\\bar.txt", "file://nonexistenthost/foo/bar.txt"},
384 // We do this strictly, like IE8, which only accepts this form using 385 // We do this strictly, like IE8, which only accepts this form using
385 // backslashes and not forward ones. Turning "//foo" into "http" matches 386 // backslashes and not forward ones. Turning "//foo" into "http" matches
(...skipping 15 matching lines...) Expand all
401 // {"file:///foo:/bar", "file://foo/bar"}, 402 // {"file:///foo:/bar", "file://foo/bar"},
402 // {"file:/\\/server\\folder/file", "file://server/folder/file"}, 403 // {"file:/\\/server\\folder/file", "file://server/folder/file"},
403 }; 404 };
404 #elif defined(OS_POSIX) 405 #elif defined(OS_POSIX)
405 406
406 #if defined(OS_MACOSX) 407 #if defined(OS_MACOSX)
407 #define HOME "/Users/" 408 #define HOME "/Users/"
408 #else 409 #else
409 #define HOME "/home/" 410 #define HOME "/home/"
410 #endif 411 #endif
411 URLFixerUpper::home_directory_override = "/foo"; 412 url_fixer::home_directory_override = "/foo";
412 FixupCase cases[] = { 413 FixupCase cases[] = {
413 // File URLs go through GURL, which tries to escape intelligently. 414 // File URLs go through GURL, which tries to escape intelligently.
414 {"/A%20non-existent file.txt", "file:///A%2520non-existent%20file.txt"}, 415 {"/A%20non-existent file.txt", "file:///A%2520non-existent%20file.txt"},
415 // A plain "/" refers to the root. 416 // A plain "/" refers to the root.
416 {"/", "file:///"}, 417 {"/", "file:///"},
417 418
418 // These rely on the above home_directory_override. 419 // These rely on the above home_directory_override.
419 {"~", "file:///foo"}, 420 {"~", "file:///foo"},
420 {"~/bar", "file:///foo/bar"}, 421 {"~/bar", "file:///foo/bar"},
421 422
422 // References to other users' homedirs. 423 // References to other users' homedirs.
423 {"~foo", "file://" HOME "foo"}, 424 {"~foo", "file://" HOME "foo"},
424 {"~x/blah", "file://" HOME "x/blah"}, 425 {"~x/blah", "file://" HOME "x/blah"},
425 }; 426 };
426 #endif 427 #endif
427 428
428 for (size_t i = 0; i < arraysize(cases); i++) { 429 for (size_t i = 0; i < arraysize(cases); i++) {
429 EXPECT_EQ(cases[i].output, 430 EXPECT_EQ(cases[i].output,
430 URLFixerUpper::FixupURL(cases[i].input, "").possibly_invalid_spec()); 431 url_fixer::FixupURL(cases[i].input, "").possibly_invalid_spec());
431 } 432 }
432 433
433 EXPECT_TRUE(base::DeleteFile(original, false)); 434 EXPECT_TRUE(base::DeleteFile(original, false));
434 } 435 }
435 436
436 TEST(URLFixerUpperTest, FixupRelativeFile) { 437 TEST(URLFixerTest, FixupRelativeFile) {
437 base::FilePath full_path, dir; 438 base::FilePath full_path, dir;
438 base::FilePath file_part( 439 base::FilePath file_part(
439 FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt")); 440 FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt"));
440 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir)); 441 ASSERT_TRUE(PathService::Get(base::DIR_MODULE, &dir));
441 ASSERT_TRUE(MakeTempFile(dir, file_part, &full_path)); 442 ASSERT_TRUE(MakeTempFile(dir, file_part, &full_path));
442 full_path = base::MakeAbsoluteFilePath(full_path); 443 full_path = base::MakeAbsoluteFilePath(full_path);
443 ASSERT_FALSE(full_path.empty()); 444 ASSERT_FALSE(full_path.empty());
444 445
445 // make sure we pass through good URLs 446 // make sure we pass through good URLs
446 for (size_t i = 0; i < arraysize(fixup_cases); ++i) { 447 for (size_t i = 0; i < arraysize(fixup_cases); ++i) {
447 FixupCase value = fixup_cases[i]; 448 FixupCase value = fixup_cases[i];
448 base::FilePath input = base::FilePath::FromUTF8Unsafe(value.input); 449 base::FilePath input = base::FilePath::FromUTF8Unsafe(value.input);
449 EXPECT_EQ(value.output, 450 EXPECT_EQ(value.output,
450 URLFixerUpper::FixupRelativeFile(dir, input).possibly_invalid_spec()); 451 url_fixer::FixupRelativeFile(dir, input).possibly_invalid_spec());
451 } 452 }
452 453
453 // make sure the existing file got fixed-up to a file URL, and that there 454 // make sure the existing file got fixed-up to a file URL, and that there
454 // are no backslashes 455 // are no backslashes
455 EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir, 456 EXPECT_TRUE(IsMatchingFileURL(
456 file_part).possibly_invalid_spec(), full_path)); 457 url_fixer::FixupRelativeFile(dir, file_part).possibly_invalid_spec(),
458 full_path));
457 EXPECT_TRUE(base::DeleteFile(full_path, false)); 459 EXPECT_TRUE(base::DeleteFile(full_path, false));
458 460
459 // create a filename we know doesn't exist and make sure it doesn't get 461 // create a filename we know doesn't exist and make sure it doesn't get
460 // fixed up to a file URL 462 // fixed up to a file URL
461 base::FilePath nonexistent_file( 463 base::FilePath nonexistent_file(
462 FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt")); 464 FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt"));
463 std::string fixedup(URLFixerUpper::FixupRelativeFile(dir, 465 std::string fixedup(url_fixer::FixupRelativeFile(dir, nonexistent_file)
464 nonexistent_file).possibly_invalid_spec()); 466 .possibly_invalid_spec());
465 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8)); 467 EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8));
466 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file)); 468 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file));
467 469
468 // make a subdir to make sure relative paths with directories work, also 470 // make a subdir to make sure relative paths with directories work, also
469 // test spaces: 471 // test spaces:
470 // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt" 472 // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt"
471 base::FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir")); 473 base::FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir"));
472 base::FilePath sub_file( 474 base::FilePath sub_file(
473 FILE_PATH_LITERAL("url fixer-upper existing file.txt")); 475 FILE_PATH_LITERAL("url fixer-upper existing file.txt"));
474 base::FilePath new_dir = dir.Append(sub_dir); 476 base::FilePath new_dir = dir.Append(sub_dir);
475 base::CreateDirectory(new_dir); 477 base::CreateDirectory(new_dir);
476 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path)); 478 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path));
477 full_path = base::MakeAbsoluteFilePath(full_path); 479 full_path = base::MakeAbsoluteFilePath(full_path);
478 ASSERT_FALSE(full_path.empty()); 480 ASSERT_FALSE(full_path.empty());
479 481
480 // test file in the subdir 482 // test file in the subdir
481 base::FilePath relative_file = sub_dir.Append(sub_file); 483 base::FilePath relative_file = sub_dir.Append(sub_file);
482 EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir, 484 EXPECT_TRUE(IsMatchingFileURL(
483 relative_file).possibly_invalid_spec(), full_path)); 485 url_fixer::FixupRelativeFile(dir, relative_file).possibly_invalid_spec(),
486 full_path));
484 487
485 // test file in the subdir with different slashes and escaping. 488 // test file in the subdir with different slashes and escaping.
486 base::FilePath::StringType relative_file_str = sub_dir.value() + 489 base::FilePath::StringType relative_file_str = sub_dir.value() +
487 FILE_PATH_LITERAL("/") + sub_file.value(); 490 FILE_PATH_LITERAL("/") + sub_file.value();
488 ReplaceSubstringsAfterOffset(&relative_file_str, 0, 491 ReplaceSubstringsAfterOffset(&relative_file_str, 0,
489 FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20")); 492 FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20"));
490 EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir, 493 EXPECT_TRUE(IsMatchingFileURL(
491 base::FilePath(relative_file_str)).possibly_invalid_spec(), full_path)); 494 url_fixer::FixupRelativeFile(dir, base::FilePath(relative_file_str))
495 .possibly_invalid_spec(),
496 full_path));
492 497
493 // test relative directories and duplicate slashes 498 // test relative directories and duplicate slashes
494 // (should resolve to the same file as above) 499 // (should resolve to the same file as above)
495 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") + 500 relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") +
496 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value(); 501 sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value();
497 EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir, 502 EXPECT_TRUE(IsMatchingFileURL(
498 base::FilePath(relative_file_str)).possibly_invalid_spec(), full_path)); 503 url_fixer::FixupRelativeFile(dir, base::FilePath(relative_file_str))
504 .possibly_invalid_spec(),
505 full_path));
499 506
500 // done with the subdir 507 // done with the subdir
501 EXPECT_TRUE(base::DeleteFile(full_path, false)); 508 EXPECT_TRUE(base::DeleteFile(full_path, false));
502 EXPECT_TRUE(base::DeleteFile(new_dir, true)); 509 EXPECT_TRUE(base::DeleteFile(new_dir, true));
503 510
504 // Test that an obvious HTTP URL isn't accidentally treated as an absolute 511 // Test that an obvious HTTP URL isn't accidentally treated as an absolute
505 // file path (on account of system-specific craziness). 512 // file path (on account of system-specific craziness).
506 base::FilePath empty_path; 513 base::FilePath empty_path;
507 base::FilePath http_url_path(FILE_PATH_LITERAL("http://../")); 514 base::FilePath http_url_path(FILE_PATH_LITERAL("http://../"));
508 EXPECT_TRUE(URLFixerUpper::FixupRelativeFile( 515 EXPECT_TRUE(
509 empty_path, http_url_path).SchemeIs("http")); 516 url_fixer::FixupRelativeFile(empty_path, http_url_path).SchemeIs("http"));
510 } 517 }
OLDNEW
« no previous file with comments | « components/url_fixer/url_fixer.cc ('k') | net/base/net_util_icu.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698