This source file includes following definitions.
- TEST
- MakeTempFile
- IsMatchingFileURL
- TEST
- TEST
- TEST
#include <stdlib.h>
#include "base/basictypes.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/net/url_fixer_upper.h"
#include "net/base/net_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/url_parse.h"
namespace {
class URLFixerUpperTest : public testing::Test {
};
};
namespace url_parse {
std::ostream& operator<<(std::ostream& os, const Component& part) {
return os << "(begin=" << part.begin << ", len=" << part.len << ")";
}
}
struct SegmentCase {
const std::string input;
const std::string result;
const url_parse::Component scheme;
const url_parse::Component username;
const url_parse::Component password;
const url_parse::Component host;
const url_parse::Component port;
const url_parse::Component path;
const url_parse::Component query;
const url_parse::Component ref;
};
static const SegmentCase segment_cases[] = {
{ "http://www.google.com/", "http",
url_parse::Component(0, 4),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(7, 14),
url_parse::Component(),
url_parse::Component(21, 1),
url_parse::Component(),
url_parse::Component(),
},
{ "aBoUt:vErSiOn", "about",
url_parse::Component(0, 5),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(6, 7),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
},
{ "about:host/path?query#ref", "about",
url_parse::Component(0, 5),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(6, 4),
url_parse::Component(),
url_parse::Component(10, 5),
url_parse::Component(16, 5),
url_parse::Component(22, 3),
},
{ "about://host/path?query#ref", "about",
url_parse::Component(0, 5),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(8, 4),
url_parse::Component(),
url_parse::Component(12, 5),
url_parse::Component(18, 5),
url_parse::Component(24, 3),
},
{ "chrome:host/path?query#ref", "chrome",
url_parse::Component(0, 6),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(7, 4),
url_parse::Component(),
url_parse::Component(11, 5),
url_parse::Component(17, 5),
url_parse::Component(23, 3),
},
{ "chrome://host/path?query#ref", "chrome",
url_parse::Component(0, 6),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(9, 4),
url_parse::Component(),
url_parse::Component(13, 5),
url_parse::Component(19, 5),
url_parse::Component(25, 3),
},
{ " www.google.com:124?foo#", "http",
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(4, 14),
url_parse::Component(19, 3),
url_parse::Component(),
url_parse::Component(23, 3),
url_parse::Component(27, 0),
},
{ "user@www.google.com", "http",
url_parse::Component(),
url_parse::Component(0, 4),
url_parse::Component(),
url_parse::Component(5, 14),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
},
{ "ftp:/user:P:a$$Wd@..ftp.google.com...::23///pub?foo#bar", "ftp",
url_parse::Component(0, 3),
url_parse::Component(5, 4),
url_parse::Component(10, 7),
url_parse::Component(18, 20),
url_parse::Component(39, 2),
url_parse::Component(41, 6),
url_parse::Component(48, 3),
url_parse::Component(52, 3),
},
{ "[2001:db8::1]/path", "http",
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(0, 13),
url_parse::Component(),
url_parse::Component(13, 5),
url_parse::Component(),
url_parse::Component(),
},
{ "[::1]", "http",
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(0, 5),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
},
{ "[2001:4860:", "http",
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(0, 11),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
},
{ "[2001:4860:/foo", "http",
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(0, 11),
url_parse::Component(),
url_parse::Component(11, 4),
url_parse::Component(),
url_parse::Component(),
},
{ "http://:b005::68]", "http",
url_parse::Component(0, 4),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(7, 10),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
},
{ ":b005::68]", "",
url_parse::Component(0, 0),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
url_parse::Component(),
},
};
TEST(URLFixerUpperTest, SegmentURL) {
std::string result;
url_parse::Parsed parts;
for (size_t i = 0; i < arraysize(segment_cases); ++i) {
SegmentCase value = segment_cases[i];
result = URLFixerUpper::SegmentURL(value.input, &parts);
EXPECT_EQ(value.result, result);
EXPECT_EQ(value.scheme, parts.scheme);
EXPECT_EQ(value.username, parts.username);
EXPECT_EQ(value.password, parts.password);
EXPECT_EQ(value.host, parts.host);
EXPECT_EQ(value.port, parts.port);
EXPECT_EQ(value.path, parts.path);
EXPECT_EQ(value.query, parts.query);
EXPECT_EQ(value.ref, parts.ref);
}
}
static bool MakeTempFile(const base::FilePath& dir,
const base::FilePath& file_name,
base::FilePath* full_path) {
*full_path = dir.Append(file_name);
return base::WriteFile(*full_path, "", 0) == 0;
}
static bool IsMatchingFileURL(const std::string& url,
const base::FilePath& full_file_path) {
if (url.length() <= 8)
return false;
if (std::string("file:///") != url.substr(0, 8))
return false;
if (url.find('\\') != std::string::npos)
return false;
base::FilePath derived_path;
net::FileURLToFilePath(GURL(url), &derived_path);
return base::FilePath::CompareEqualIgnoreCase(derived_path.value(),
full_file_path.value());
}
struct FixupCase {
const std::string input;
const std::string desired_tld;
const std::string output;
} fixup_cases[] = {
{"www.google.com", "", "http://www.google.com/"},
{" www.google.com ", "", "http://www.google.com/"},
{" foo.com/asdf bar", "", "http://foo.com/asdf%20%20bar"},
{"..www.google.com..", "", "http://www.google.com./"},
{"http://......", "", "http://....../"},
{"http://host.com:ninety-two/", "", "http://host.com:ninety-two/"},
{"http://host.com:ninety-two?foo", "", "http://host.com:ninety-two/?foo"},
{"google.com:123", "", "http://google.com:123/"},
{"about:", "", "chrome://version/"},
{"about:foo", "", "chrome://foo/"},
{"about:version", "", "chrome://version/"},
{"about:usr:pwd@hst/pth?qry#ref", "", "chrome://usr:pwd@hst/pth?qry#ref"},
{"about://usr:pwd@hst/pth?qry#ref", "", "chrome://usr:pwd@hst/pth?qry#ref"},
{"chrome:usr:pwd@hst/pth?qry#ref", "", "chrome://usr:pwd@hst/pth?qry#ref"},
{"chrome://usr:pwd@hst/pth?qry#ref", "", "chrome://usr:pwd@hst/pth?qry#ref"},
{"www:123", "", "http://www:123/"},
{" www:123", "", "http://www:123/"},
{"www.google.com?foo", "", "http://www.google.com/?foo"},
{"www.google.com#foo", "", "http://www.google.com/#foo"},
{"www.google.com?", "", "http://www.google.com/?"},
{"www.google.com#", "", "http://www.google.com/#"},
{"www.google.com:123?foo#bar", "", "http://www.google.com:123/?foo#bar"},
{"user@www.google.com", "", "http://user@www.google.com/"},
{"\xE6\xB0\xB4.com" , "", "http://xn--1rw.com/"},
{"user:passwd@www.google.com:8080/", "", "user:passwd@www.google.com:8080/"},
{"ftp.google.com", "", "ftp://ftp.google.com/"},
{" ftp.google.com", "", "ftp://ftp.google.com/"},
{"FTP.GooGle.com", "", "ftp://ftp.google.com/"},
{"ftpblah.google.com", "", "http://ftpblah.google.com/"},
{"ftp", "", "http://ftp/"},
{"google.ftp.com", "", "http://google.ftp.com/"},
{ "http://google.com/search?q=\xd0\x85", "",
"http://google.com/search?q=%D0%85"
},
{ "http://google.com/search?q=\xec\x97\x85", "",
"http://google.com/search?q=%EC%97%85"
},
{ "http://google.com/search?q=\xf0\x90\x80\x85", "",
"http://google.com/search?q=%F0%90%80%85"
},
{ "http://google.com/search?q=\xd0\xa0", "",
"http://google.com/search?q=%D0%A0"
},
{ "http://google.com/search?q=\xec\x97\xa0", "",
"http://google.com/search?q=%EC%97%A0"
},
{ "http://google.com/search?q=\xf0\x90\x80\xa0", "",
"http://google.com/search?q=%F0%90%80%A0"
},
{"[2001:db8::2]", "", "http://[2001:db8::2]/"},
{"[::]:80", "", "http://[::]/"},
{"[::]:80/path", "", "http://[::]/path"},
{"[::]:180/path", "", "http://[::]:180/path"},
{"::1", "", "::1"},
{"http;//www.google.com/", "", "http://www.google.com/"},
{"about;chrome", "", "chrome://chrome/"},
{"whatsup;//fool", "", "whatsup://fool"},
{"http://host/port?query;moar", "", "http://host/port?query;moar"},
{"http;www.google.com/", "", "http://www.google.com/"},
{"http;/www.google.com/", "", "http://www.google.com/"},
{";http://www.google.com/", "", "http://%3Bhttp//www.google.com/"},
};
TEST(URLFixerUpperTest, FixupURL) {
for (size_t i = 0; i < arraysize(fixup_cases); ++i) {
FixupCase value = fixup_cases[i];
EXPECT_EQ(value.output, URLFixerUpper::FixupURL(value.input,
value.desired_tld).possibly_invalid_spec())
<< "input: " << value.input;
}
FixupCase tld_cases[] = {
{"google", "com", "http://www.google.com/"},
{"google.", "com", "http://www.google.com/"},
{"google..", "com", "http://www.google.com/"},
{".google", "com", "http://www.google.com/"},
{"www.google", "com", "http://www.google.com/"},
{"google.com", "com", "http://google.com/"},
{"http://google", "com", "http://www.google.com/"},
{"..google..", "com", "http://www.google.com/"},
{"http://www.google", "com", "http://www.google.com/"},
{"9999999999999999", "com", "http://www.9999999999999999.com/"},
{"google/foo", "com", "http://www.google.com/foo"},
{"google.com/foo", "com", "http://google.com/foo"},
{"google/?foo=.com", "com", "http://www.google.com/?foo=.com"},
{"www.google/?foo=www.", "com", "http://www.google.com/?foo=www."},
{"google.com/?foo=.com", "com", "http://google.com/?foo=.com"},
{"http://www.google.com", "com", "http://www.google.com/"},
{"google:123", "com", "http://www.google.com:123/"},
{"http://google:123", "com", "http://www.google.com:123/"},
};
for (size_t i = 0; i < arraysize(tld_cases); ++i) {
FixupCase value = tld_cases[i];
EXPECT_EQ(value.output, URLFixerUpper::FixupURL(value.input,
value.desired_tld).possibly_invalid_spec());
}
}
TEST(URLFixerUpperTest, FixupFile) {
base::FilePath dir;
base::FilePath original;
ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir));
ASSERT_TRUE(MakeTempFile(
dir,
base::FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")),
&original));
GURL golden(net::FilePathToFileURL(original));
#if defined(OS_WIN)
GURL fixedup(URLFixerUpper::FixupURL(base::WideToUTF8(original.value()),
std::string()));
#elif defined(OS_POSIX)
GURL fixedup(URLFixerUpper::FixupURL(original.value(), std::string()));
#endif
EXPECT_EQ(golden, fixedup);
#if defined(OS_WIN)
std::string cur(base::WideToUTF8(original.value()));
EXPECT_EQ(':', cur[1]);
cur[1] = '|';
EXPECT_EQ(golden, URLFixerUpper::FixupURL(cur, std::string()));
FixupCase file_cases[] = {
{"c:\\This%20is a non-existent file.txt", "",
"file:///C:/This%2520is%20a%20non-existent%20file.txt"},
{"\\\\SomeNonexistentHost\\foo\\bar.txt", "",
"file://somenonexistenthost/foo/bar.txt"},
{"//SomeNonexistentHost\\foo/bar.txt", "",
"http://somenonexistenthost/foo/bar.txt"},
{"file:///C:/foo/bar", "", "file:///C:/foo/bar"},
{"file://C:/foo/bar", "", "file:///C:/foo/bar"},
{"file:c:", "", "file:///C:/"},
{"file:c:WINDOWS", "", "file:///C:/WINDOWS"},
{"file:c|Program Files", "", "file:///C:/Program%20Files"},
{"file:/file", "", "file://file/"},
{"file:////////c:\\foo", "", "file:///C:/foo"},
{"file://server/folder/file", "", "file://server/folder/file"},
};
#elif defined(OS_POSIX)
#if defined(OS_MACOSX)
#define HOME "/Users/"
#else
#define HOME "/home/"
#endif
URLFixerUpper::home_directory_override = "/foo";
FixupCase file_cases[] = {
{"/This%20is a non-existent file.txt", "",
"file:///This%2520is%20a%20non-existent%20file.txt"},
{"/", "",
"file:///"},
{"~", "",
"file:///foo"},
{"~/bar", "",
"file:///foo/bar"},
{"~foo", "",
"file://" HOME "foo"},
{"~x/blah", "",
"file://" HOME "x/blah"},
};
#endif
for (size_t i = 0; i < arraysize(file_cases); i++) {
EXPECT_EQ(file_cases[i].output, URLFixerUpper::FixupURL(file_cases[i].input,
file_cases[i].desired_tld).possibly_invalid_spec());
}
EXPECT_TRUE(base::DeleteFile(original, false));
}
TEST(URLFixerUpperTest, FixupRelativeFile) {
base::FilePath full_path, dir;
base::FilePath file_part(
FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt"));
ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir));
ASSERT_TRUE(MakeTempFile(dir, file_part, &full_path));
full_path = base::MakeAbsoluteFilePath(full_path);
ASSERT_FALSE(full_path.empty());
for (size_t i = 0; i < arraysize(fixup_cases); ++i) {
FixupCase value = fixup_cases[i];
base::FilePath input = base::FilePath::FromUTF8Unsafe(value.input);
EXPECT_EQ(value.output,
URLFixerUpper::FixupRelativeFile(dir, input).possibly_invalid_spec());
}
EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir,
file_part).possibly_invalid_spec(), full_path));
EXPECT_TRUE(base::DeleteFile(full_path, false));
base::FilePath nonexistent_file(
FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt"));
std::string fixedup(URLFixerUpper::FixupRelativeFile(dir,
nonexistent_file).possibly_invalid_spec());
EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8));
EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file));
base::FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir"));
base::FilePath sub_file(
FILE_PATH_LITERAL("url fixer-upper existing file.txt"));
base::FilePath new_dir = dir.Append(sub_dir);
base::CreateDirectory(new_dir);
ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path));
full_path = base::MakeAbsoluteFilePath(full_path);
ASSERT_FALSE(full_path.empty());
base::FilePath relative_file = sub_dir.Append(sub_file);
EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir,
relative_file).possibly_invalid_spec(), full_path));
base::FilePath::StringType relative_file_str = sub_dir.value() +
FILE_PATH_LITERAL("/") + sub_file.value();
ReplaceSubstringsAfterOffset(&relative_file_str, 0,
FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20"));
EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir,
base::FilePath(relative_file_str)).possibly_invalid_spec(), full_path));
relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") +
sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value();
EXPECT_TRUE(IsMatchingFileURL(URLFixerUpper::FixupRelativeFile(dir,
base::FilePath(relative_file_str)).possibly_invalid_spec(), full_path));
EXPECT_TRUE(base::DeleteFile(full_path, false));
EXPECT_TRUE(base::DeleteFile(new_dir, true));
base::FilePath empty_path;
base::FilePath http_url_path(FILE_PATH_LITERAL("http://../"));
EXPECT_TRUE(URLFixerUpper::FixupRelativeFile(
empty_path, http_url_path).SchemeIs("http"));
}