blob: b89bfa162e0db7558d7e50e072b466d2f1b3d321 [file] [log] [blame]
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/macros.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/third_party/mozilla/url_parse.h"
#include "url/url_canon.h"
#include "url/url_canon_stdstring.h"
#include "url/url_test_utils.h"
#include "url/url_util.h"
namespace url {
TEST(URLUtilTest, FindAndCompareScheme) {
Component found_scheme;
// Simple case where the scheme is found and matches.
const char kStr1[] = "";
kStr1, static_cast<int>(strlen(kStr1)), "http", NULL));
kStr1, static_cast<int>(strlen(kStr1)), "http", &found_scheme));
EXPECT_TRUE(found_scheme == Component(0, 4));
// A case where the scheme is found and doesn't match.
kStr1, static_cast<int>(strlen(kStr1)), "https", &found_scheme));
EXPECT_TRUE(found_scheme == Component(0, 4));
// A case where there is no scheme.
const char kStr2[] = "httpfoobar";
kStr2, static_cast<int>(strlen(kStr2)), "http", &found_scheme));
EXPECT_TRUE(found_scheme == Component());
// When there is an empty scheme, it should match the empty scheme.
const char kStr3[] = "";
kStr3, static_cast<int>(strlen(kStr3)), "", &found_scheme));
EXPECT_TRUE(found_scheme == Component(0, 0));
// But when there is no scheme, it should fail.
EXPECT_FALSE(FindAndCompareScheme("", 0, "", &found_scheme));
EXPECT_TRUE(found_scheme == Component());
// When there is a whitespace char in scheme, it should canonicalize the URL
// before comparison.
const char whtspc_str[] = " \r\n\tjav\ra\nscri\tpt:alert(1)";
"javascript", &found_scheme));
EXPECT_TRUE(found_scheme == Component(1, 10));
// Control characters should be stripped out on the ends, and kept in the
// middle.
const char ctrl_str[] = "\02jav\02scr\03ipt:alert(1)";
"javascript", &found_scheme));
EXPECT_TRUE(found_scheme == Component(1, 11));
TEST(URLUtilTest, IsStandard) {
const char kHTTPScheme[] = "http";
EXPECT_TRUE(IsStandard(kHTTPScheme, Component(0, strlen(kHTTPScheme))));
const char kFooScheme[] = "foo";
EXPECT_FALSE(IsStandard(kFooScheme, Component(0, strlen(kFooScheme))));
TEST(URLUtilTest, GetStandardSchemeType) {
url::SchemeType scheme_type;
const char kHTTPScheme[] = "http";
scheme_type = url::SCHEME_WITHOUT_AUTHORITY;
Component(0, strlen(kHTTPScheme)),
EXPECT_EQ(url::SCHEME_WITH_PORT, scheme_type);
const char kFilesystemScheme[] = "filesystem";
scheme_type = url::SCHEME_WITH_PORT;
Component(0, strlen(kFilesystemScheme)),
const char kFooScheme[] = "foo";
scheme_type = url::SCHEME_WITH_PORT;
Component(0, strlen(kFooScheme)),
TEST(URLUtilTest, ReplaceComponents) {
Parsed parsed;
RawCanonOutputT<char> output;
Parsed new_parsed;
// Check that the following calls do not cause crash
Replacements<char> replacements;
replacements.SetRef("test", Component(0, 4));
ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
replacements.SetHost("test", Component(0, 4));
ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
static std::string CheckReplaceScheme(const char* base_url,
const char* scheme) {
// Make sure the input is canonicalized.
RawCanonOutput<32> original;
Parsed original_parsed;
Canonicalize(base_url, strlen(base_url), true, NULL, &original,
Replacements<char> replacements;
replacements.SetScheme(scheme, Component(0, strlen(scheme)));
std::string output_string;
StdStringCanonOutput output(&output_string);
Parsed output_parsed;
ReplaceComponents(, original.length(), original_parsed,
replacements, NULL, &output, &output_parsed);
return output_string;
TEST(URLUtilTest, ReplaceScheme) {
CheckReplaceScheme("", "https"));
CheckReplaceScheme("", "file"));
CheckReplaceScheme("file:///Home/Build", "http"));
CheckReplaceScheme("about:foo", "javascript"));
CheckReplaceScheme("", ""));
CheckReplaceScheme("", "http"));
EXPECT_EQ("http:", CheckReplaceScheme("", "http"));
#ifdef WIN32
// Magic Windows drive letter behavior when converting to a file URL.
CheckReplaceScheme("http://localhost/e:foo/", "file"));
// This will probably change to "about://" when we fix
// which should also be an acceptable result.
CheckReplaceScheme("", "about"));
EXPECT_EQ(" world",
CheckReplaceScheme(" hello # world ", "http"));
TEST(URLUtilTest, DecodeURLEscapeSequences) {
struct DecodeCase {
const char* input;
const char* output;
} decode_cases[] = {
{"hello, world", "hello, world"},
" !\"#$%&'()*+,-.//"},
// Test un-UTF-8-ization.
{"%e4%bd%a0%e5%a5%bd", "\xe4\xbd\xa0\xe5\xa5\xbd"},
for (size_t i = 0; i < arraysize(decode_cases); i++) {
const char* input = decode_cases[i].input;
RawCanonOutputT<base::char16> output;
DecodeURLEscapeSequences(input, strlen(input), &output);
// Our decode should decode %00
const char zero_input[] = "%00";
RawCanonOutputT<base::char16> zero_output;
DecodeURLEscapeSequences(zero_input, strlen(zero_input), &zero_output);
EXPECT_NE("%00", test_utils::ConvertUTF16ToUTF8(
base::string16(, zero_output.length())));
// Test the error behavior for invalid UTF-8.
const char invalid_input[] = "%e4%a0%e5%a5%bd";
const base::char16 invalid_expected[4] = {0x00e4, 0x00a0, 0x597d, 0};
RawCanonOutputT<base::char16> invalid_output;
DecodeURLEscapeSequences(invalid_input, strlen(invalid_input),
base::string16(, invalid_output.length()));
TEST(URLUtilTest, TestEncodeURIComponent) {
struct EncodeCase {
const char* input;
const char* output;
} encode_cases[] = {
{"hello, world", "hello%2C%20world"},
{" !\"#$%&'()*+,-./",
for (size_t i = 0; i < arraysize(encode_cases); i++) {
const char* input = encode_cases[i].input;
RawCanonOutputT<char> buffer;
EncodeURIComponent(input, strlen(input), &buffer);
std::string output(, buffer.length());
EXPECT_EQ(encode_cases[i].output, output);
TEST(URLUtilTest, TestResolveRelativeWithNonStandardBase) {
// This tests non-standard (in the sense that IsStandard() == false)
// hierarchical schemes.
struct ResolveRelativeCase {
const char* base;
const char* rel;
bool is_valid;
const char* out;
} resolve_non_standard_cases[] = {
// Resolving a relative path against a non-hierarchical URL should fail.
{"scheme:opaque_data", "/path", false, ""},
// Resolving a relative path against a non-standard authority-based base
// URL doesn't alter the authority section.
{"scheme://Authority/", "../path", true, "scheme://Authority/path"},
// A non-standard hierarchical base is resolved with path URL
// canonicalization rules.
{"data:/Blah:Blah/", "file.html", true, "data:/Blah:Blah/file.html"},
{"data:/Path/../part/part2", "file.html", true,
// Path URL canonicalization rules also apply to non-standard authority-
// based URLs.
{"custom://Authority/", "file.html", true,
{"custom://Authority/", "other://Auth/", true, "other://Auth/"},
{"custom://Authority/", "../../file.html", true,
{"custom://Authority/path/", "file.html", true,
{"custom://Authority:NoCanon/path/", "file.html", true,
// It's still possible to get an invalid path URL.
{"custom://Invalid:!#Auth/", "file.html", false, ""},
// A path with an authority section gets canonicalized under standard URL
// rules, even though the base was non-standard.
{"content://content.Provider/", "//other.Provider", true,
// Resolving an absolute URL doesn't cause canonicalization of the
// result.
{"about:blank", "custom://Authority", true, "custom://Authority"},
// Fragment URLs can be resolved against a non-standard base.
{"scheme://Authority/path", "#fragment", true,
{"scheme://Authority/", "#fragment", true, "scheme://Authority/#fragment"},
// Resolving should fail if the base URL is authority-based but is
// missing a path component (the '/' at the end).
{"scheme://Authority", "path", false, ""},
// Test resolving a fragment (only) against any kind of base-URL.
{"about:blank", "#id42", true, "about:blank#id42" },
{"about:blank", " #id42", true, "about:blank#id42" },
{"about:blank#oldfrag", "#newfrag", true, "about:blank#newfrag" },
// A surprising side effect of allowing fragments to resolve against
// any URL scheme is we might break javascript: URLs by doing so...
{"javascript:alert('foo#bar')", "#badfrag", true,
"javascript:alert('foo#badfrag" },
// In this case, the backslashes will not be canonicalized because it's a
// non-standard URL, but they will be treated as a path separators,
// giving the base URL here a path of "\".
// The result here is somewhat arbitrary. One could argue it should be
// either "aaa://a\" or "aaa://a/" since the path is being replaced with
// the "current directory". But in the context of resolving on data URLs,
// adding the requested dot doesn't seem wrong either.
{"aaa://a\\", "aaa:.", true, "aaa://a\\." }
for (size_t i = 0; i < arraysize(resolve_non_standard_cases); i++) {
const ResolveRelativeCase& test_data = resolve_non_standard_cases[i];
Parsed base_parsed;
ParsePathURL(test_data.base, strlen(test_data.base), false, &base_parsed);
std::string resolved;
StdStringCanonOutput output(&resolved);
Parsed resolved_parsed;
bool valid = ResolveRelative(test_data.base, strlen(test_data.base),
base_parsed, test_data.rel,
strlen(test_data.rel), NULL, &output,
EXPECT_EQ(test_data.is_valid, valid) << i;
if (test_data.is_valid && valid)
EXPECT_EQ(test_data.out, resolved) << i;
TEST(URLUtilTest, TestNoRefComponent) {
// The hash-mark must be ignored when mailto: scheme is parsed,
// even if the URL has a base and relative part.
const char* base = "mailto://to/";
const char* rel = "any#body";
Parsed base_parsed;
ParsePathURL(base, strlen(base), false, &base_parsed);
std::string resolved;
StdStringCanonOutput output(&resolved);
Parsed resolved_parsed;
bool valid = ResolveRelative(base, strlen(base),
base_parsed, rel,
strlen(rel), NULL, &output,
} // namespace url