This source file includes following definitions.
- BytesToHexString
- converter
- SetupReplComp
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- ParsedIsEqual
- TEST
- TEST
#include <errno.h>
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/icu/source/common/unicode/ucnv.h"
#include "url/url_canon.h"
#include "url/url_canon_icu.h"
#include "url/url_canon_internal.h"
#include "url/url_canon_stdstring.h"
#include "url/url_parse.h"
#include "url/url_test_utils.h"
#ifndef ARRAYSIZE
#define ARRAYSIZE ARRAYSIZE_UNSAFE
#endif
using url_test_utils::WStringToUTF16;
using url_test_utils::ConvertUTF8ToUTF16;
using url_test_utils::ConvertUTF16ToUTF8;
using url_canon::CanonHostInfo;
namespace {
struct ComponentCase {
const char* input;
const char* expected;
url_parse::Component expected_component;
bool expected_success;
};
struct DualComponentCase {
const char* input8;
const wchar_t* input16;
const char* expected;
url_parse::Component expected_component;
bool expected_success;
};
struct IPAddressCase {
const char* input8;
const wchar_t* input16;
const char* expected;
url_parse::Component expected_component;
CanonHostInfo::Family expected_family;
int expected_num_ipv4_components;
const char* expected_address_hex;
};
std::string BytesToHexString(unsigned char bytes[16], int length) {
EXPECT_TRUE(length == 0 || length == 4 || length == 16)
<< "Bad IP address length: " << length;
std::string result;
for (int i = 0; i < length; ++i) {
result.push_back(url_canon::kHexCharLookup[(bytes[i] >> 4) & 0xf]);
result.push_back(url_canon::kHexCharLookup[bytes[i] & 0xf]);
}
return result;
}
struct ReplaceCase {
const char* base;
const char* scheme;
const char* username;
const char* password;
const char* host;
const char* port;
const char* path;
const char* query;
const char* ref;
const char* expected;
};
class UConvScoper {
public:
explicit UConvScoper(const char* charset_name) {
UErrorCode err = U_ZERO_ERROR;
converter_ = ucnv_open(charset_name, &err);
}
~UConvScoper() {
if (converter_)
ucnv_close(converter_);
}
UConverter* converter() const { return converter_; }
private:
UConverter* converter_;
};
const char kDeleteComp[] = "|";
template<typename CHAR>
void SetupReplComp(
void (url_canon::Replacements<CHAR>::*set)(const CHAR*,
const url_parse::Component&),
void (url_canon::Replacements<CHAR>::*clear)(),
url_canon::Replacements<CHAR>* rep,
const CHAR* str) {
if (str && str[0] == kDeleteComp[0]) {
(rep->*clear)();
} else if (str) {
(rep->*set)(str, url_parse::Component(0, static_cast<int>(strlen(str))));
}
}
}
TEST(URLCanonTest, DoAppendUTF8) {
struct UTF8Case {
unsigned input;
const char* output;
} utf_cases[] = {
{0x24, "\x24"},
{0xA2, "\xC2\xA2"},
{0x20AC, "\xE2\x82\xAC"},
{0x24B62, "\xF0\xA4\xAD\xA2"},
{0x10FFFF, "\xF4\x8F\xBF\xBF"},
};
std::string out_str;
for (size_t i = 0; i < ARRAYSIZE(utf_cases); i++) {
out_str.clear();
url_canon::StdStringCanonOutput output(&out_str);
url_canon::AppendUTF8Value(utf_cases[i].input, &output);
output.Complete();
EXPECT_EQ(utf_cases[i].output, out_str);
}
}
#if defined(GTEST_HAS_DEATH_TEST) && defined(NDEBUG)
TEST(URLCanonTest, DoAppendUTF8Invalid) {
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
ASSERT_DEBUG_DEATH({
url_canon::AppendUTF8Value(0x110000, &output);
output.Complete();
EXPECT_EQ("", out_str);
}, "");
}
#endif
TEST(URLCanonTest, UTF) {
struct UTFCase {
const char* input8;
const wchar_t* input16;
bool expected_success;
const char* output;
} utf_cases[] = {
{"\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d", true, "%E4%BD%A0%E5%A5%BD"},
{"\xF0\x90\x8C\x80", L"\xd800\xdf00", true, "%F0%90%8C%80"},
{"\xf0\x84\xbd\xa0\xe5\xa5\xbd", NULL, false, "%EF%BF%BD%E5%A5%BD"},
{"\xe4\xa0\xe5\xa5\xbd", L"\xd800\x597d", false, "%EF%BF%BD%E5%A5%BD"},
{"\xe4\xbd\xa0\xe5\xa5", L"\x4f60\xd800", false, "%E4%BD%A0%EF%BF%BD"},
{"\xed\xb0\x80", L"\xdc00", false, "%EF%BF%BD"},
{"\xed\xa0\x80", NULL, false, "%EF%BF%BD"},
};
std::string out_str;
for (size_t i = 0; i < ARRAYSIZE(utf_cases); i++) {
if (utf_cases[i].input8) {
out_str.clear();
url_canon::StdStringCanonOutput output(&out_str);
int input_len = static_cast<int>(strlen(utf_cases[i].input8));
bool success = true;
for (int ch = 0; ch < input_len; ch++) {
success &= AppendUTF8EscapedChar(utf_cases[i].input8, &ch, input_len,
&output);
}
output.Complete();
EXPECT_EQ(utf_cases[i].expected_success, success);
EXPECT_EQ(std::string(utf_cases[i].output), out_str);
}
if (utf_cases[i].input16) {
out_str.clear();
url_canon::StdStringCanonOutput output(&out_str);
base::string16 input_str(WStringToUTF16(utf_cases[i].input16));
int input_len = static_cast<int>(input_str.length());
bool success = true;
for (int ch = 0; ch < input_len; ch++) {
success &= AppendUTF8EscapedChar(input_str.c_str(), &ch, input_len,
&output);
}
output.Complete();
EXPECT_EQ(utf_cases[i].expected_success, success);
EXPECT_EQ(std::string(utf_cases[i].output), out_str);
}
if (utf_cases[i].input8 && utf_cases[i].input16 &&
utf_cases[i].expected_success) {
std::string input8_str(utf_cases[i].input8);
base::string16 input16_str(WStringToUTF16(utf_cases[i].input16));
EXPECT_EQ(input8_str, ConvertUTF16ToUTF8(input16_str));
EXPECT_EQ(input16_str, ConvertUTF8ToUTF16(input8_str));
}
}
}
TEST(URLCanonTest, ICUCharsetConverter) {
struct ICUCase {
const wchar_t* input;
const char* encoding;
const char* expected;
} icu_cases[] = {
{L"Hello, world", "utf-8", "Hello, world"},
{L"\x4f60\x597d", "utf-8", "\xe4\xbd\xa0\xe5\xa5\xbd"},
{L"!\xd800\xdf00!", "utf-8", "!\xf0\x90\x8c\x80!"},
{L"\x4f60\x597d", "big5", "\xa7\x41\xa6\x6e"},
{L"hello\x4f60\x06de\x597dworld", "big5", "hello\xa7\x41%26%231758%3B\xa6\x6eworld"},
};
for (size_t i = 0; i < ARRAYSIZE(icu_cases); i++) {
UConvScoper conv(icu_cases[i].encoding);
ASSERT_TRUE(conv.converter() != NULL);
url_canon::ICUCharsetConverter converter(conv.converter());
std::string str;
url_canon::StdStringCanonOutput output(&str);
base::string16 input_str(WStringToUTF16(icu_cases[i].input));
int input_len = static_cast<int>(input_str.length());
converter.ConvertFromUTF16(input_str.c_str(), input_len, &output);
output.Complete();
EXPECT_STREQ(icu_cases[i].expected, str.c_str());
}
const int static_size = 16;
UConvScoper conv("utf-8");
ASSERT_TRUE(conv.converter());
url_canon::ICUCharsetConverter converter(conv.converter());
for (int i = static_size - 2; i <= static_size + 2; i++) {
base::string16 input;
for (int ch = 0; ch < i; ch++)
input.push_back('a');
url_canon::RawCanonOutput<static_size> output;
converter.ConvertFromUTF16(input.c_str(), static_cast<int>(input.length()),
&output);
EXPECT_EQ(input.length(), static_cast<size_t>(output.length()));
}
}
TEST(URLCanonTest, Scheme) {
ComponentCase scheme_cases[] = {
{"http", "http:", url_parse::Component(0, 4), true},
{"HTTP", "http:", url_parse::Component(0, 4), true},
{" HTTP ", "%20http%20:", url_parse::Component(0, 10), false},
{"htt: ", "htt%3A%20:", url_parse::Component(0, 9), false},
{"\xe4\xbd\xa0\xe5\xa5\xbdhttp", "%E4%BD%A0%E5%A5%BDhttp:", url_parse::Component(0, 22), false},
{"ht%3Atp", "ht%3atp:", url_parse::Component(0, 7), false},
};
std::string out_str;
for (size_t i = 0; i < arraysize(scheme_cases); i++) {
int url_len = static_cast<int>(strlen(scheme_cases[i].input));
url_parse::Component in_comp(0, url_len);
url_parse::Component out_comp;
out_str.clear();
url_canon::StdStringCanonOutput output1(&out_str);
bool success = url_canon::CanonicalizeScheme(scheme_cases[i].input,
in_comp, &output1, &out_comp);
output1.Complete();
EXPECT_EQ(scheme_cases[i].expected_success, success);
EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
out_str.clear();
url_canon::StdStringCanonOutput output2(&out_str);
base::string16 wide_input(ConvertUTF8ToUTF16(scheme_cases[i].input));
in_comp.len = static_cast<int>(wide_input.length());
success = url_canon::CanonicalizeScheme(wide_input.c_str(), in_comp,
&output2, &out_comp);
output2.Complete();
EXPECT_EQ(scheme_cases[i].expected_success, success);
EXPECT_EQ(std::string(scheme_cases[i].expected), out_str);
EXPECT_EQ(scheme_cases[i].expected_component.begin, out_comp.begin);
EXPECT_EQ(scheme_cases[i].expected_component.len, out_comp.len);
}
url_parse::Component out_comp;
out_str.clear();
url_canon::StdStringCanonOutput output(&out_str);
EXPECT_TRUE(url_canon::CanonicalizeScheme("", url_parse::Component(0, -1),
&output, &out_comp));
output.Complete();
EXPECT_EQ(std::string(":"), out_str);
EXPECT_EQ(0, out_comp.begin);
EXPECT_EQ(0, out_comp.len);
}
TEST(URLCanonTest, Host) {
IPAddressCase host_cases[] = {
{"GoOgLe.CoM", L"GoOgLe.CoM", "google.com", url_parse::Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
{"Goo%20 goo%7C|.com", L"Goo%20 goo%7C|.com", "goo%20%20goo%7C%7C.com", url_parse::Component(0, 22), CanonHostInfo::NEUTRAL, -1, ""},
{NULL, L"GOO\x00a0\x3000goo.com", "goo%20%20goo.com", url_parse::Component(0, 16), CanonHostInfo::NEUTRAL, -1, ""},
{NULL, L"GOO\x200b\x2060\xfeffgoo.com", "googoo.com", url_parse::Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
{NULL, L"www.foo\x3002" L"bar.com", "www.foo.bar.com", url_parse::Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""},
{"\xef\xb7\x90zyx.com", L"\xfdd0zyx.com", "%EF%BF%BDzyx.com", url_parse::Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
{"%ef%b7%90zyx.com", L"%ef%b7%90zyx.com", "%EF%BF%BDzyx.com", url_parse::Component(0, 16), CanonHostInfo::BROKEN, -1, ""},
{"\xef\xbc\xa7\xef\xbd\x8f.com", L"\xff27\xff4f.com", "go.com", url_parse::Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""},
{"\xef\xbc\x85\xef\xbc\x94\xef\xbc\x91.com", L"\xff05\xff14\xff11.com", "a.com", url_parse::Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
{"%ef%bc%85%ef%bc%94%ef%bc%91.com", L"%ef%bc%85%ef%bc%94%ef%bc%91.com", "a.com", url_parse::Component(0, 5), CanonHostInfo::NEUTRAL, -1, ""},
{"\xef\xbc\x85\xef\xbc\x90\xef\xbc\x90.com", L"\xff05\xff10\xff10.com", "%00.com", url_parse::Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
{"%ef%bc%85%ef%bc%90%ef%bc%90.com", L"%ef%bc%85%ef%bc%90%ef%bc%90.com", "%00.com", url_parse::Component(0, 7), CanonHostInfo::BROKEN, -1, ""},
{"\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", L"\x4f60\x597d\x4f60\x597d", "xn--6qqa088eba", url_parse::Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
{"fu\xc3\x9f" "ball.de", L"fu\x00df" L"ball.de", "fussball.de",
url_parse::Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
{"\xcf\x83\xcf\x8c\xce\xbb\xce\xbf\xcf\x82", L"\x3c3\x3cc\x3bb\x3bf\x3c2",
"xn--wxaikc6b", url_parse::Component(0, 12),
CanonHostInfo::NEUTRAL, -1, ""},
{"a\xe2\x80\x8c" "b\xe2\x80\x8d" "c", L"a\x200c" L"b\x200d" L"c", "abc",
url_parse::Component(0, 3), CanonHostInfo::NEUTRAL, -1, ""},
{"\xe0\xa4\x95\xe0\xa5\x8d\xe2\x80\x8d\xe0\xa4\x9c",
L"\x915\x94d\x200d\x91c", "xn--11bo0m",
url_parse::Component(0, 10), CanonHostInfo::NEUTRAL, -1, ""},
{"wow\xef\xbc\x81", L"wow\xff01", "wow%21",
url_parse::Component(0, 6), CanonHostInfo::NEUTRAL, -1, ""},
{"\xe2\x84\xb2oo", L"\x2132oo", "%E2%84%B2oo",
url_parse::Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
{"\xf0\xaf\xa1\xa8\xe5\xa7\xbb.cn", L"\xd87e\xdc68\x59fb.cn",
"%F0%AF%A1%A8%E5%A7%BB.cn",
url_parse::Component(0, 24), CanonHostInfo::BROKEN, -1, ""},
{"M\xc3\x9cNCHEN", L"M\xdcNCHEN", "xn--mnchen-3ya",
url_parse::Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
{"\xe2\x99\xa5ny.us", L"\x2665ny.us", "xn--ny-s0x.us",
url_parse::Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""},
{"\xf0\x91\x80\x93.com", L"\xd804\xdc13.com", "xn--n00d.com",
url_parse::Component(0, 12), CanonHostInfo::NEUTRAL, -1, ""},
{"\xd8\x82.eg", L"\x602.eg", "%D8%82.eg",
url_parse::Component(0, 9), CanonHostInfo::BROKEN, -1, ""},
{"\xe2\x82\xb7.com", L"\x20b7.com", "xn--wzg.com",
url_parse::Component(0, 11), CanonHostInfo::NEUTRAL, -1, ""},
{"bc\xc8\xba.com", L"bc\x23a.com", "xn--bc-is1a.com",
url_parse::Component(0, 15), CanonHostInfo::NEUTRAL, -1, ""},
{"\xde\x8b\xde\xa8\xde\x88\xde\xac\xde\x80\xde\xa8",
L"\x78b\x7a8\x788\x7ac\x780\x7a8", "xn--hqbpi0jcw",
url_parse::Component(0, 13), CanonHostInfo::NEUTRAL, -1, ""},
{"\xd8\xac\xd8\xa7\xd8\xb1xyz", L"\x62c\x627\x631xyz",
"%D8%AC%D8%A7%D8%B1xyz", url_parse::Component(0, 21),
CanonHostInfo::BROKEN, -1, ""},
{"\xd8\xac\xd8\xa7\xd8\xb1" "2", L"\x62c\x627\x631" L"2",
"xn--2-ymcov", url_parse::Component(0, 11),
CanonHostInfo::NEUTRAL, -1, ""},
{"\xd8\xac\xd8\xa7\xd8\xb1xy2", L"\x62c\x627\x631xy2",
"%D8%AC%D8%A7%D8%B1xy2", url_parse::Component(0, 21),
CanonHostInfo::BROKEN, -1, ""},
{"\xd8\xac\xd8\xa7\xd8\xb1\xd9\xa2", L"\x62c\x627\x631\x662",
"xn--mgbjq0r", url_parse::Component(0, 11),
CanonHostInfo::NEUTRAL, -1, ""},
{"\xd8\xac\xd8\xa7\xd8\xb1xy\xd9\xa2", L"\x62c\x627\x631xy\x662",
"%D8%AC%D8%A7%D8%B1xy%D9%A2", url_parse::Component(0, 26),
CanonHostInfo::BROKEN, -1, ""},
{"\xd8\xac\xd8\xa7\xd8\xb1xy2\xd9\xa2", L"\x62c\x627\x631xy2\x662",
"%D8%AC%D8%A7%D8%B1xy2%D9%A2", url_parse::Component(0, 27),
CanonHostInfo::BROKEN, -1, ""},
{"\xe2\x83\x8f.com", L"\x20cf.com", "%E2%83%8F.com",
url_parse::Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
{"\xc2\x80.com", L"\x80.com", "%C2%80.com",
url_parse::Component(0, 10), CanonHostInfo::BROKEN, -1, ""},
{"%E4%BD%A0%E5%A5%BD\xe4\xbd\xa0\xe5\xa5\xbd",
L"%E4%BD%A0%E5%A5%BD\x4f60\x597d", "xn--6qqa088eba",
url_parse::Component(0, 14), CanonHostInfo::NEUTRAL, -1, ""},
{"%zz%66%a", L"%zz%66%a", "%25zzf%25a", url_parse::Component(0, 10),
CanonHostInfo::BROKEN, -1, ""},
{"%25", L"%25", "%25", url_parse::Component(0, 3),
CanonHostInfo::BROKEN, -1, ""},
{"hello%00", L"hello%00", "hello%00", url_parse::Component(0, 8),
CanonHostInfo::BROKEN, -1, ""},
{"%30%78%63%30%2e%30%32%35%30.01", L"%30%78%63%30%2e%30%32%35%30.01",
"192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 3,
"C0A80001"},
{"%30%78%63%30%2e%30%32%35%30.01%2e", L"%30%78%63%30%2e%30%32%35%30.01%2e",
"192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 3,
"C0A80001"},
{"%3g%78%63%30%2e%30%32%35%30%2E.01", L"%3g%78%63%30%2e%30%32%35%30%2E.01", "%253gxc0.0250..01", url_parse::Component(0, 17), CanonHostInfo::BROKEN, -1, ""},
{"192.168.0.1 hello", L"192.168.0.1 hello", "192.168.0.1%20hello", url_parse::Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""},
{"\xef\xbc\x90%Ef%bc\xb8%ef%Bd%83\xef\xbc\x90%EF%BC%8E\xef\xbc\x90\xef\xbc\x92\xef\xbc\x95\xef\xbc\x90\xef\xbc%8E\xef\xbc\x90\xef\xbc\x91", L"\xff10\xff38\xff43\xff10\xff0e\xff10\xff12\xff15\xff10\xff0e\xff10\xff11", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 3, "C0A80001"},
{"192.168.0.257", L"192.168.0.257", "192.168.0.257", url_parse::Component(0, 13), CanonHostInfo::BROKEN, -1, ""},
{"[google.com]", L"[google.com]", "[google.com]", url_parse::Component(0, 12), CanonHostInfo::BROKEN, -1, ""},
{"\xd1\x82(", L"\x0442(", "xn--%28-7ed", url_parse::Component(0, 11),
CanonHostInfo::NEUTRAL, -1, ""},
{"12345678912345.de", L"12345678912345.de", "12345678912345.de", url_parse::Component(0, 17), CanonHostInfo::NEUTRAL, -1, ""},
{"1.12345678912345.de", L"1.12345678912345.de", "1.12345678912345.de", url_parse::Component(0, 19), CanonHostInfo::NEUTRAL, -1, ""},
{"12345678912345.12345678912345.de", L"12345678912345.12345678912345.de", "12345678912345.12345678912345.de", url_parse::Component(0, 32), CanonHostInfo::NEUTRAL, -1, ""},
{"1.2.0xB3A73CE5B59.de", L"1.2.0xB3A73CE5B59.de", "1.2.0xb3a73ce5b59.de", url_parse::Component(0, 20), CanonHostInfo::NEUTRAL, -1, ""},
{"12345678912345.0xde", L"12345678912345.0xde", "12345678912345.0xde", url_parse::Component(0, 19), CanonHostInfo::BROKEN, -1, ""},
};
std::string out_str;
for (size_t i = 0; i < arraysize(host_cases); i++) {
if (host_cases[i].input8) {
int host_len = static_cast<int>(strlen(host_cases[i].input8));
url_parse::Component in_comp(0, host_len);
url_parse::Component out_comp;
out_str.clear();
url_canon::StdStringCanonOutput output(&out_str);
bool success = url_canon::CanonicalizeHost(host_cases[i].input8, in_comp,
&output, &out_comp);
output.Complete();
EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
success) << "for input: " << host_cases[i].input8;
EXPECT_EQ(std::string(host_cases[i].expected), out_str) <<
"for input: " << host_cases[i].input8;
EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin) <<
"for input: " << host_cases[i].input8;
EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len) <<
"for input: " << host_cases[i].input8;
}
if (host_cases[i].input16) {
base::string16 input16(WStringToUTF16(host_cases[i].input16));
int host_len = static_cast<int>(input16.length());
url_parse::Component in_comp(0, host_len);
url_parse::Component out_comp;
out_str.clear();
url_canon::StdStringCanonOutput output(&out_str);
bool success = url_canon::CanonicalizeHost(input16.c_str(), in_comp,
&output, &out_comp);
output.Complete();
EXPECT_EQ(host_cases[i].expected_family != CanonHostInfo::BROKEN,
success);
EXPECT_EQ(std::string(host_cases[i].expected), out_str);
EXPECT_EQ(host_cases[i].expected_component.begin, out_comp.begin);
EXPECT_EQ(host_cases[i].expected_component.len, out_comp.len);
}
}
for (size_t i = 0; i < arraysize(host_cases); i++) {
if (host_cases[i].input8) {
int host_len = static_cast<int>(strlen(host_cases[i].input8));
url_parse::Component in_comp(0, host_len);
out_str.clear();
url_canon::StdStringCanonOutput output(&out_str);
CanonHostInfo host_info;
url_canon::CanonicalizeHostVerbose(host_cases[i].input8, in_comp,
&output, &host_info);
output.Complete();
EXPECT_EQ(host_cases[i].expected_family, host_info.family);
EXPECT_EQ(std::string(host_cases[i].expected), out_str);
EXPECT_EQ(host_cases[i].expected_component.begin,
host_info.out_host.begin);
EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
BytesToHexString(host_info.address, host_info.AddressLength()));
if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
host_info.num_ipv4_components);
}
}
if (host_cases[i].input16) {
base::string16 input16(WStringToUTF16(host_cases[i].input16));
int host_len = static_cast<int>(input16.length());
url_parse::Component in_comp(0, host_len);
out_str.clear();
url_canon::StdStringCanonOutput output(&out_str);
CanonHostInfo host_info;
url_canon::CanonicalizeHostVerbose(input16.c_str(), in_comp,
&output, &host_info);
output.Complete();
EXPECT_EQ(host_cases[i].expected_family, host_info.family);
EXPECT_EQ(std::string(host_cases[i].expected), out_str);
EXPECT_EQ(host_cases[i].expected_component.begin,
host_info.out_host.begin);
EXPECT_EQ(host_cases[i].expected_component.len, host_info.out_host.len);
EXPECT_EQ(std::string(host_cases[i].expected_address_hex),
BytesToHexString(host_info.address, host_info.AddressLength()));
if (host_cases[i].expected_family == CanonHostInfo::IPV4) {
EXPECT_EQ(host_cases[i].expected_num_ipv4_components,
host_info.num_ipv4_components);
}
}
}
}
TEST(URLCanonTest, IPv4) {
IPAddressCase cases[] = {
{"", L"", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
{".", L".", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
{"192.168.0.1", L"192.168.0.1", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
{"0300.0250.00.01", L"0300.0250.00.01", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
{"0xC0.0Xa8.0x0.0x1", L"0xC0.0Xa8.0x0.0x1", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
{"192.168.9.com", L"192.168.9.com", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
{"19a.168.0.1", L"19a.168.0.1", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
{"0308.0250.00.01", L"0308.0250.00.01", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
{"0xCG.0xA8.0x0.0x1", L"0xCG.0xA8.0x0.0x1", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
{"192", L"192", "0.0.0.192", url_parse::Component(0, 9), CanonHostInfo::IPV4, 1, "000000C0"},
{"0xC0a80001", L"0xC0a80001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
{"030052000001", L"030052000001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
{"000030052000001", L"000030052000001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 1, "C0A80001"},
{"192.168", L"192.168", "192.0.0.168", url_parse::Component(0, 11), CanonHostInfo::IPV4, 2, "C00000A8"},
{"192.0x00A80001", L"192.0x000A80001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
{"0xc0.052000001", L"0xc0.052000001", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 2, "C0A80001"},
{"192.168.1", L"192.168.1", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 3, "C0A80001"},
{"192.168.0.0.1", L"192.168.0.0.1", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
{"192.168.0.1.", L"192.168.0.1.", "192.168.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 4, "C0A80001"},
{"192.168.0.1. hello", L"192.168.0.1. hello", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
{"192.168.0.1..", L"192.168.0.1..", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
{"192.168..1", L"192.168..1", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
{"0x100.0", L"0x100.0", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"0x100.0.0", L"0x100.0.0", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"0x100.0.0.0", L"0x100.0.0.0", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"0.0x100.0.0", L"0.0x100.0.0", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"0.0.0x100.0", L"0.0.0x100.0", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"0.0.0.0x100", L"0.0.0.0x100", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"0.0.0x10000", L"0.0.0x10000", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"0.0x1000000", L"0.0x1000000", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"0x100000000", L"0x100000000", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"0xFF.0", L"0xFF.0", "255.0.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4, 2, "FF000000"},
{"0xFF.0.0", L"0xFF.0.0", "255.0.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4, 3, "FF000000"},
{"0xFF.0.0.0", L"0xFF.0.0.0", "255.0.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4, 4, "FF000000"},
{"0.0xFF.0.0", L"0.0xFF.0.0", "0.255.0.0", url_parse::Component(0, 9), CanonHostInfo::IPV4, 4, "00FF0000"},
{"0.0.0xFF.0", L"0.0.0xFF.0", "0.0.255.0", url_parse::Component(0, 9), CanonHostInfo::IPV4, 4, "0000FF00"},
{"0.0.0.0xFF", L"0.0.0.0xFF", "0.0.0.255", url_parse::Component(0, 9), CanonHostInfo::IPV4, 4, "000000FF"},
{"0.0.0xFFFF", L"0.0.0xFFFF", "0.0.255.255", url_parse::Component(0, 11), CanonHostInfo::IPV4, 3, "0000FFFF"},
{"0.0xFFFFFF", L"0.0xFFFFFF", "0.255.255.255", url_parse::Component(0, 13), CanonHostInfo::IPV4, 2, "00FFFFFF"},
{"0xFFFFFFFF", L"0xFFFFFFFF", "255.255.255.255", url_parse::Component(0, 15), CanonHostInfo::IPV4, 1, "FFFFFFFF"},
{"276.256.0xf1a2.077777", L"276.256.0xf1a2.077777", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"192.168.0.257", L"192.168.0.257", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"192.168.0xa20001", L"192.168.0xa20001", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"192.015052000001", L"192.015052000001", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"0X12C0a80001", L"0X12C0a80001", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"276.1.2", L"276.1.2", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"192.168.0.1 hello", L"192.168.0.1 hello", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
{"0000000000000300.0x00000000000000fF.00000000000000001", L"0000000000000300.0x00000000000000fF.00000000000000001", "192.255.0.1", url_parse::Component(0, 11), CanonHostInfo::IPV4, 3, "C0FF0001"},
{"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", L"0000000000000300.0xffffffffFFFFFFFF.3022415481470977", "", url_parse::Component(0, 11), CanonHostInfo::BROKEN, -1, ""},
{"00000000000000000001", L"00000000000000000001", "0.0.0.1", url_parse::Component(0, 7), CanonHostInfo::IPV4, 1, "00000001"},
{"0000000000000000100000000000000001", L"0000000000000000100000000000000001", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"0.0.0.000000000000000000z", L"0.0.0.000000000000000000z", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
{"0.0.0.100000000000000000z", L"0.0.0.100000000000000000z", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
{"0.00.0x.0x0", L"0.00.0x.0x0", "0.0.0.0", url_parse::Component(0, 7), CanonHostInfo::IPV4, 4, "00000000"},
};
for (size_t i = 0; i < arraysize(cases); i++) {
url_parse::Component component(0,
static_cast<int>(strlen(cases[i].input8)));
std::string out_str1;
url_canon::StdStringCanonOutput output1(&out_str1);
url_canon::CanonHostInfo host_info;
url_canon::CanonicalizeIPAddress(cases[i].input8, component, &output1,
&host_info);
output1.Complete();
EXPECT_EQ(cases[i].expected_family, host_info.family);
EXPECT_EQ(std::string(cases[i].expected_address_hex),
BytesToHexString(host_info.address, host_info.AddressLength()));
if (host_info.family == CanonHostInfo::IPV4) {
EXPECT_STREQ(cases[i].expected, out_str1.c_str());
EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
EXPECT_EQ(cases[i].expected_num_ipv4_components,
host_info.num_ipv4_components);
}
base::string16 input16(WStringToUTF16(cases[i].input16));
component = url_parse::Component(0, static_cast<int>(input16.length()));
std::string out_str2;
url_canon::StdStringCanonOutput output2(&out_str2);
url_canon::CanonicalizeIPAddress(input16.c_str(), component, &output2,
&host_info);
output2.Complete();
EXPECT_EQ(cases[i].expected_family, host_info.family);
EXPECT_EQ(std::string(cases[i].expected_address_hex),
BytesToHexString(host_info.address, host_info.AddressLength()));
if (host_info.family == CanonHostInfo::IPV4) {
EXPECT_STREQ(cases[i].expected, out_str2.c_str());
EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
EXPECT_EQ(cases[i].expected_num_ipv4_components,
host_info.num_ipv4_components);
}
}
}
TEST(URLCanonTest, IPv6) {
IPAddressCase cases[] = {
{"", L"", "", url_parse::Component(), CanonHostInfo::NEUTRAL, -1, ""},
{":", L":", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[", L"[", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[:", L"[:", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"]", L"]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{":]", L":]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[]", L"[]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[:]", L"[:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"2001:db8::1", L"2001:db8::1", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[2001:db8::1", L"[2001:db8::1", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"2001:db8::1]", L"2001:db8::1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[::]", L"[::]", "[::]", url_parse::Component(0,4), CanonHostInfo::IPV6, -1, "00000000000000000000000000000000"},
{"[::1]", L"[::1]", "[::1]", url_parse::Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000001"},
{"[1::]", L"[1::]", "[1::]", url_parse::Component(0,5), CanonHostInfo::IPV6, -1, "00010000000000000000000000000000"},
{"[000:01:02:003:004:5:6:007]", L"[000:01:02:003:004:5:6:007]", "[0:1:2:3:4:5:6:7]", url_parse::Component(0,17), CanonHostInfo::IPV6, -1, "00000001000200030004000500060007"},
{"[A:b:c:DE:fF:0:1:aC]", L"[A:b:c:DE:fF:0:1:aC]", "[a:b:c:de:ff:0:1:ac]", url_parse::Component(0,20), CanonHostInfo::IPV6, -1, "000A000B000C00DE00FF0000000100AC"},
{"[1:0:0:2::3:0]", L"[1:0:0:2::3:0]", "[1::2:0:0:3:0]", url_parse::Component(0,14), CanonHostInfo::IPV6, -1, "00010000000000020000000000030000"},
{"[1::2:0:0:3:0]", L"[1::2:0:0:3:0]", "[1::2:0:0:3:0]", url_parse::Component(0,14), CanonHostInfo::IPV6, -1, "00010000000000020000000000030000"},
{"[::192.168.0.1]", L"[::192.168.0.1]", "[::c0a8:1]", url_parse::Component(0,10), CanonHostInfo::IPV6, -1, "000000000000000000000000C0A80001"},
{"[::ffff:192.168.0.1]", L"[::ffff:192.168.0.1]", "[::ffff:c0a8:1]", url_parse::Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"},
{"[::eeee:192.168.0.1]", L"[::eeee:192.168.0.1]", "[::eeee:c0a8:1]", url_parse::Component(0, 15), CanonHostInfo::IPV6, -1, "00000000000000000000EEEEC0A80001"},
{"[2001::192.168.0.1]", L"[2001::192.168.0.1]", "[2001::c0a8:1]", url_parse::Component(0, 14), CanonHostInfo::IPV6, -1, "200100000000000000000000C0A80001"},
{"[1:2:192.168.0.1:5:6]", L"[1:2:192.168.0.1:5:6]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[::ffff:192.1.2]", L"[::ffff:192.1.2]", "[::ffff:c001:2]", url_parse::Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0010002"},
{"[::ffff:0xC0.0Xa8.0x0.0x1]", L"[::ffff:0xC0.0Xa8.0x0.0x1]", "[::ffff:c0a8:1]", url_parse::Component(0,15), CanonHostInfo::IPV6, -1, "00000000000000000000FFFFC0A80001"},
{"[0:0::0:0:8]", L"[0:0::0:0:8]", "[::8]", url_parse::Component(0,5), CanonHostInfo::IPV6, -1, "00000000000000000000000000000008"},
{"[2001:db8::1]", L"[2001:db8::1]", "[2001:db8::1]", url_parse::Component(0,13), CanonHostInfo::IPV6, -1, "20010DB8000000000000000000000001"},
{"[2001::db8::1]", L"[2001::db8::1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[2001:db8:::1]", L"[2001:db8:::1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[:::]", L"[:::]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[2001::.com]", L"[2001::.com]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[::192.168.0.0.1]", L"[::192.168.0.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[::ffff:192.168.0.0.1]", L"[::ffff:192.168.0.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[1:2:3:4:5:6:7:8:9]", L"[1:2:3:4:5:6:7:8:9]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[0:0:0:0:0:0:0:192.168.0.1]", L"[0:0:0:0:0:0:0:192.168.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[1:2:3:4:5:6::192.168.0.1]", L"[1:2:3:4:5:6::192.168.0.1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[1:2:3:4:5:6::8]", L"[1:2:3:4:5:6::8]", "[1:2:3:4:5:6:0:8]", url_parse::Component(0,17), CanonHostInfo::IPV6, -1, "00010002000300040005000600000008"},
{"[1:2:3:4:5:6:7:8:]", L"[1:2:3:4:5:6:7:8:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[1:2:3:4:5:6:192.168.0.1:]", L"[1:2:3:4:5:6:192.168.0.1:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[-1:2:3:4:5:6:7:8]", L"[-1:2:3:4:5:6:7:8]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[1::%1]", L"[1::%1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[1::%eth0]", L"[1::%eth0]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[1::%]", L"[1::%]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[%]", L"[%]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[::%:]", L"[::%:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[:0:0::0:0:8]", L"[:0:0::0:0:8]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[0:0::0:0:8:]", L"[0:0::0:0:8:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[:0:0::0:0:8:]", L"[:0:0::0:0:8:]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[::192.168..1]", L"[::192.168..1]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
{"[::1 hello]", L"[::1 hello]", "", url_parse::Component(), CanonHostInfo::BROKEN, -1, ""},
};
for (size_t i = 0; i < arraysize(cases); i++) {
url_parse::Component component(0,
static_cast<int>(strlen(cases[i].input8)));
std::string out_str1;
url_canon::StdStringCanonOutput output1(&out_str1);
url_canon::CanonHostInfo host_info;
url_canon::CanonicalizeIPAddress(cases[i].input8, component, &output1,
&host_info);
output1.Complete();
EXPECT_EQ(cases[i].expected_family, host_info.family);
EXPECT_EQ(std::string(cases[i].expected_address_hex),
BytesToHexString(host_info.address, host_info.AddressLength())) << "iter " << i << " host " << cases[i].input8;
if (host_info.family == CanonHostInfo::IPV6) {
EXPECT_STREQ(cases[i].expected, out_str1.c_str());
EXPECT_EQ(cases[i].expected_component.begin,
host_info.out_host.begin);
EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
}
base::string16 input16(WStringToUTF16(cases[i].input16));
component = url_parse::Component(0, static_cast<int>(input16.length()));
std::string out_str2;
url_canon::StdStringCanonOutput output2(&out_str2);
url_canon::CanonicalizeIPAddress(input16.c_str(), component, &output2,
&host_info);
output2.Complete();
EXPECT_EQ(cases[i].expected_family, host_info.family);
EXPECT_EQ(std::string(cases[i].expected_address_hex),
BytesToHexString(host_info.address, host_info.AddressLength()));
if (host_info.family == CanonHostInfo::IPV6) {
EXPECT_STREQ(cases[i].expected, out_str2.c_str());
EXPECT_EQ(cases[i].expected_component.begin, host_info.out_host.begin);
EXPECT_EQ(cases[i].expected_component.len, host_info.out_host.len);
}
}
}
TEST(URLCanonTest, IPEmpty) {
std::string out_str1;
url_canon::StdStringCanonOutput output1(&out_str1);
url_canon::CanonHostInfo host_info;
const char spec[] = "192.168.0.1";
url_canon::CanonicalizeIPAddress(spec, url_parse::Component(),
&output1, &host_info);
EXPECT_FALSE(host_info.IsIPAddress());
url_canon::CanonicalizeIPAddress(spec, url_parse::Component(0, 0),
&output1, &host_info);
EXPECT_FALSE(host_info.IsIPAddress());
}
TEST(URLCanonTest, UserInfo) {
struct UserComponentCase {
const char* input;
const char* expected;
url_parse::Component expected_username;
url_parse::Component expected_password;
bool expected_success;
} user_info_cases[] = {
{"http://user:pass@host.com/", "user:pass@", url_parse::Component(0, 4), url_parse::Component(5, 4), true},
{"http://@host.com/", "", url_parse::Component(0, -1), url_parse::Component(0, -1), true},
{"http://:@host.com/", "", url_parse::Component(0, -1), url_parse::Component(0, -1), true},
{"http://foo:@host.com/", "foo@", url_parse::Component(0, 3), url_parse::Component(0, -1), true},
{"http://:foo@host.com/", ":foo@", url_parse::Component(0, 0), url_parse::Component(1, 3), true},
{"http://^ :$\t@host.com/", "%5E%20:$%09@", url_parse::Component(0, 6), url_parse::Component(7, 4), true},
{"http://user:pass@/", "user:pass@", url_parse::Component(0, 4), url_parse::Component(5, 4), true},
{"http://%2540:bar@domain.com/", "%2540:bar@", url_parse::Component(0, 5), url_parse::Component(6, 3), true },
{"ftp://me\\mydomain:pass@foo.com/", "", url_parse::Component(0, -1), url_parse::Component(0, -1), true},
};
for (size_t i = 0; i < ARRAYSIZE(user_info_cases); i++) {
int url_len = static_cast<int>(strlen(user_info_cases[i].input));
url_parse::Parsed parsed;
url_parse::ParseStandardURL(user_info_cases[i].input, url_len, &parsed);
url_parse::Component out_user, out_pass;
std::string out_str;
url_canon::StdStringCanonOutput output1(&out_str);
bool success = url_canon::CanonicalizeUserInfo(user_info_cases[i].input,
parsed.username,
user_info_cases[i].input,
parsed.password,
&output1, &out_user,
&out_pass);
output1.Complete();
EXPECT_EQ(user_info_cases[i].expected_success, success);
EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
out_str.clear();
url_canon::StdStringCanonOutput output2(&out_str);
base::string16 wide_input(ConvertUTF8ToUTF16(user_info_cases[i].input));
success = url_canon::CanonicalizeUserInfo(wide_input.c_str(),
parsed.username,
wide_input.c_str(),
parsed.password,
&output2, &out_user, &out_pass);
output2.Complete();
EXPECT_EQ(user_info_cases[i].expected_success, success);
EXPECT_EQ(std::string(user_info_cases[i].expected), out_str);
EXPECT_EQ(user_info_cases[i].expected_username.begin, out_user.begin);
EXPECT_EQ(user_info_cases[i].expected_username.len, out_user.len);
EXPECT_EQ(user_info_cases[i].expected_password.begin, out_pass.begin);
EXPECT_EQ(user_info_cases[i].expected_password.len, out_pass.len);
}
}
TEST(URLCanonTest, Port) {
struct PortCase {
const char* input;
int default_port;
const char* expected;
url_parse::Component expected_component;
bool expected_success;
} port_cases[] = {
{"as df", 80, ":as%20df", url_parse::Component(1, 7), false},
{"-2", 80, ":-2", url_parse::Component(1, 2), false},
{"80", 80, "", url_parse::Component(0, -1), true},
{"8080", 80, ":8080", url_parse::Component(1, 4), true},
{"80", url_parse::PORT_UNSPECIFIED, ":80", url_parse::Component(1, 2), true},
};
for (size_t i = 0; i < ARRAYSIZE(port_cases); i++) {
int url_len = static_cast<int>(strlen(port_cases[i].input));
url_parse::Component in_comp(0, url_len);
url_parse::Component out_comp;
std::string out_str;
url_canon::StdStringCanonOutput output1(&out_str);
bool success = url_canon::CanonicalizePort(port_cases[i].input, in_comp,
port_cases[i].default_port,
&output1, &out_comp);
output1.Complete();
EXPECT_EQ(port_cases[i].expected_success, success);
EXPECT_EQ(std::string(port_cases[i].expected), out_str);
EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
out_str.clear();
url_canon::StdStringCanonOutput output2(&out_str);
base::string16 wide_input(ConvertUTF8ToUTF16(port_cases[i].input));
success = url_canon::CanonicalizePort(wide_input.c_str(), in_comp,
port_cases[i].default_port,
&output2, &out_comp);
output2.Complete();
EXPECT_EQ(port_cases[i].expected_success, success);
EXPECT_EQ(std::string(port_cases[i].expected), out_str);
EXPECT_EQ(port_cases[i].expected_component.begin, out_comp.begin);
EXPECT_EQ(port_cases[i].expected_component.len, out_comp.len);
}
}
TEST(URLCanonTest, Path) {
DualComponentCase path_cases[] = {
{"/././foo", L"/././foo", "/foo", url_parse::Component(0, 4), true},
{"/./.foo", L"/./.foo", "/.foo", url_parse::Component(0, 5), true},
{"/foo/.", L"/foo/.", "/foo/", url_parse::Component(0, 5), true},
{"/foo/./", L"/foo/./", "/foo/", url_parse::Component(0, 5), true},
{"/foo/bar/..", L"/foo/bar/..", "/foo/", url_parse::Component(0, 5), true},
{"/foo/bar/../", L"/foo/bar/../", "/foo/", url_parse::Component(0, 5), true},
{"/foo/..bar", L"/foo/..bar", "/foo/..bar", url_parse::Component(0, 10), true},
{"/foo/bar/../ton", L"/foo/bar/../ton", "/foo/ton", url_parse::Component(0, 8), true},
{"/foo/bar/../ton/../../a", L"/foo/bar/../ton/../../a", "/a", url_parse::Component(0, 2), true},
{"/foo/../../..", L"/foo/../../..", "/", url_parse::Component(0, 1), true},
{"/foo/../../../ton", L"/foo/../../../ton", "/ton", url_parse::Component(0, 4), true},
{"/foo/%2e", L"/foo/%2e", "/foo/", url_parse::Component(0, 5), true},
{"/foo/%2e%2", L"/foo/%2e%2", "/foo/.%2", url_parse::Component(0, 8), true},
{"/foo/%2e./%2e%2e/.%2e/%2e.bar", L"/foo/%2e./%2e%2e/.%2e/%2e.bar", "/..bar", url_parse::Component(0, 6), true},
{"////../..", L"////../..", "//", url_parse::Component(0, 2), true},
{"/foo", L"/foo", "/foo", url_parse::Component(0, 4), true},
{"/%20foo", L"/%20foo", "/%20foo", url_parse::Component(0, 7), true},
{"/foo%", L"/foo%", "/foo%", url_parse::Component(0, 5), true},
{"/foo%2", L"/foo%2", "/foo%2", url_parse::Component(0, 6), true},
{"/foo%2zbar", L"/foo%2zbar", "/foo%2zbar", url_parse::Component(0, 10), true},
{"/foo%2\xc2\xa9zbar", NULL, "/foo%2%C2%A9zbar", url_parse::Component(0, 16), true},
{NULL, L"/foo%2\xc2\xa9zbar", "/foo%2%C3%82%C2%A9zbar", url_parse::Component(0, 22), true},
{"/foo%41%7a", L"/foo%41%7a", "/fooAz", url_parse::Component(0, 6), true},
{"/foo\x09\x91%91", NULL, "/foo%09%91%91", url_parse::Component(0, 13), true},
{NULL, L"/foo\x09\x91%91", "/foo%09%C2%91%91", url_parse::Component(0, 16), true},
{"/foo%00%51", L"/foo%00%51", "/foo%00Q", url_parse::Component(0, 8), false},
{"/(%28:%3A%29)", L"/(%28:%3A%29)", "/(%28:%3A%29)", url_parse::Component(0, 13), true},
{"/%3A%3a%3C%3c", L"/%3A%3a%3C%3c", "/%3A%3a%3C%3c", url_parse::Component(0, 13), true},
{"/foo\tbar", L"/foo\tbar", "/foo%09bar", url_parse::Component(0, 10), true},
{"\\foo\\bar", L"\\foo\\bar", "/foo/bar", url_parse::Component(0, 8), true},
{"/foo#bar", L"/foo#bar", "/foo%23bar", url_parse::Component(0, 10), true},
{"/%7Ffp3%3Eju%3Dduvgw%3Dd", L"/%7Ffp3%3Eju%3Dduvgw%3Dd", "/%7Ffp3%3Eju%3Dduvgw%3Dd", url_parse::Component(0, 24), true},
{"/@asdf%40", L"/@asdf%40", "/@asdf%40", url_parse::Component(0, 9), true},
{"/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", L"/\x4f60\x597d\x4f60\x597d", "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD", url_parse::Component(0, 37), true},
{"/\xef\xb7\x90zyx", NULL, "/%EF%B7%90zyx", url_parse::Component(0, 13), true},
{NULL, L"/\xfdd0zyx", "/%EF%BF%BDzyx", url_parse::Component(0, 13), false},
};
for (size_t i = 0; i < arraysize(path_cases); i++) {
if (path_cases[i].input8) {
int len = static_cast<int>(strlen(path_cases[i].input8));
url_parse::Component in_comp(0, len);
url_parse::Component out_comp;
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
bool success = url_canon::CanonicalizePath(path_cases[i].input8, in_comp,
&output, &out_comp);
output.Complete();
EXPECT_EQ(path_cases[i].expected_success, success);
EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
EXPECT_EQ(path_cases[i].expected, out_str);
}
if (path_cases[i].input16) {
base::string16 input16(WStringToUTF16(path_cases[i].input16));
int len = static_cast<int>(input16.length());
url_parse::Component in_comp(0, len);
url_parse::Component out_comp;
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
bool success = url_canon::CanonicalizePath(input16.c_str(), in_comp,
&output, &out_comp);
output.Complete();
EXPECT_EQ(path_cases[i].expected_success, success);
EXPECT_EQ(path_cases[i].expected_component.begin, out_comp.begin);
EXPECT_EQ(path_cases[i].expected_component.len, out_comp.len);
EXPECT_EQ(path_cases[i].expected, out_str);
}
}
const char path_with_null[] = "/ab\0c";
url_parse::Component in_comp(0, 5);
url_parse::Component out_comp;
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
bool success = url_canon::CanonicalizePath(path_with_null, in_comp,
&output, &out_comp);
output.Complete();
EXPECT_FALSE(success);
EXPECT_EQ("/ab%00c", out_str);
}
TEST(URLCanonTest, Query) {
struct QueryCase {
const char* input8;
const wchar_t* input16;
const char* encoding;
const char* expected;
} query_cases[] = {
{"foo=bar", L"foo=bar", NULL, "?foo=bar"},
{"foo=bar", L"foo=bar", "utf-8", "?foo=bar"},
{"foo=bar", L"foo=bar", "shift_jis", "?foo=bar"},
{"foo=bar", L"foo=bar", "gb2312", "?foo=bar"},
{"as?df", L"as?df", NULL, "?as?df"},
{"as#df", L"as#df", NULL, "?as%23df"},
{"\x02hello\x7f bye", L"\x02hello\x7f bye", NULL, "?%02hello%7F%20bye"},
{"%40%41123", L"%40%41123", NULL, "?%40%41123"},
{"q=\xe4\xbd\xa0\xe5\xa5\xbd", L"q=\x4f60\x597d", NULL, "?q=%E4%BD%A0%E5%A5%BD"},
{"q=\xe4\xbd\xa0\xe5\xa5\xbd", L"q=\x4f60\x597d", "gb2312", "?q=%C4%E3%BA%C3"},
{"q=\xe4\xbd\xa0\xe5\xa5\xbd", L"q=\x4f60\x597d", "big5", "?q=%A7A%A6n"},
{"q=Chinese\xef\xbc\xa7", L"q=Chinese\xff27", "iso-8859-1", "?q=Chinese%26%2365319%3B"},
{"q=\xed\xed", L"q=\xd800\xd800", NULL, "?q=%EF%BF%BD%EF%BF%BD"},
{"q=<asdf>", L"q=<asdf>", NULL, "?q=%3Casdf%3E"},
{"q=\"asdf\"", L"q=\"asdf\"", NULL, "?q=%22asdf%22"},
};
for (size_t i = 0; i < ARRAYSIZE(query_cases); i++) {
url_parse::Component out_comp;
UConvScoper conv(query_cases[i].encoding);
ASSERT_TRUE(!query_cases[i].encoding || conv.converter());
url_canon::ICUCharsetConverter converter(conv.converter());
url_canon::ICUCharsetConverter* conv_pointer = &converter;
if (!query_cases[i].encoding)
conv_pointer = NULL;
if (query_cases[i].input8) {
int len = static_cast<int>(strlen(query_cases[i].input8));
url_parse::Component in_comp(0, len);
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
url_canon::CanonicalizeQuery(query_cases[i].input8, in_comp,
conv_pointer, &output, &out_comp);
output.Complete();
EXPECT_EQ(query_cases[i].expected, out_str);
}
if (query_cases[i].input16) {
base::string16 input16(WStringToUTF16(query_cases[i].input16));
int len = static_cast<int>(input16.length());
url_parse::Component in_comp(0, len);
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
url_canon::CanonicalizeQuery(input16.c_str(), in_comp,
conv_pointer, &output, &out_comp);
output.Complete();
EXPECT_EQ(query_cases[i].expected, out_str);
}
}
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
url_parse::Component out_comp;
url_canon::CanonicalizeQuery("a \x00z\x01", url_parse::Component(0, 5), NULL,
&output, &out_comp);
output.Complete();
EXPECT_EQ("?a%20%00z%01", out_str);
}
TEST(URLCanonTest, Ref) {
DualComponentCase ref_cases[] = {
{"hello, world", L"hello, world", "#hello, world", url_parse::Component(1, 12), true},
{"\xc2\xa9", L"\xa9", "#\xc2\xa9", url_parse::Component(1, 2), true},
{"\xF0\x90\x8C\x80ss", L"\xd800\xdf00ss", "#\xF0\x90\x8C\x80ss", url_parse::Component(1, 6), true},
{"%41%a", L"%41%a", "#%41%a", url_parse::Component(1, 5), true},
{"\xc2", NULL, "#\xef\xbf\xbd", url_parse::Component(1, 3), true},
{NULL, L"\xd800\x597d", "#\xef\xbf\xbd\xe5\xa5\xbd", url_parse::Component(1, 6), true},
{"a\xef\xb7\x90", L"a\xfdd0", "#a\xef\xbf\xbd", url_parse::Component(1, 4), true},
{"asdf#qwer", L"asdf#qwer", "#asdf#qwer", url_parse::Component(1, 9), true},
{"#asdf", L"#asdf", "##asdf", url_parse::Component(1, 5), true},
};
for (size_t i = 0; i < arraysize(ref_cases); i++) {
if (ref_cases[i].input8) {
int len = static_cast<int>(strlen(ref_cases[i].input8));
url_parse::Component in_comp(0, len);
url_parse::Component out_comp;
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
url_canon::CanonicalizeRef(ref_cases[i].input8, in_comp,
&output, &out_comp);
output.Complete();
EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
EXPECT_EQ(ref_cases[i].expected, out_str);
}
if (ref_cases[i].input16) {
base::string16 input16(WStringToUTF16(ref_cases[i].input16));
int len = static_cast<int>(input16.length());
url_parse::Component in_comp(0, len);
url_parse::Component out_comp;
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
url_canon::CanonicalizeRef(input16.c_str(), in_comp, &output, &out_comp);
output.Complete();
EXPECT_EQ(ref_cases[i].expected_component.begin, out_comp.begin);
EXPECT_EQ(ref_cases[i].expected_component.len, out_comp.len);
EXPECT_EQ(ref_cases[i].expected, out_str);
}
}
const char null_input[5] = "ab\x00z";
url_parse::Component null_input_component(0, 4);
url_parse::Component out_comp;
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
url_canon::CanonicalizeRef(null_input, null_input_component,
&output, &out_comp);
output.Complete();
EXPECT_EQ(1, out_comp.begin);
EXPECT_EQ(3, out_comp.len);
EXPECT_EQ("#abz", out_str);
}
TEST(URLCanonTest, CanonicalizeStandardURL) {
struct URLCase {
const char* input;
const char* expected;
bool expected_success;
} cases[] = {
{"http://www.google.com/foo?bar=baz#", "http://www.google.com/foo?bar=baz#", true},
{"http://[www.google.com]/", "http://[www.google.com]/", false},
{"ht\ttp:@www.google.com:80/;p?#", "ht%09tp://www.google.com:80/;p?#", false},
{"http:////////user:@google.com:99?foo", "http://user@google.com:99/?foo", true},
{"www.google.com", ":www.google.com/", true},
{"http://192.0x00A80001", "http://192.168.0.1/", true},
{"http://www/foo%2Ehtml", "http://www/foo.html", true},
{"http://user:pass@/", "http://user:pass@/", false},
{"http://%25DOMAIN:foobar@foodomain.com/", "http://%25DOMAIN:foobar@foodomain.com/", true},
{"http:\\\\www.google.com\\foo", "http://www.google.com/foo", true},
{"http://www.google.com/asdf#\xc2", "http://www.google.com/asdf#\xef\xbf\xbd", true},
{"http://foo:80/", "http://foo/", true},
{"http://foo:81/", "http://foo:81/", true},
{"httpa://foo:80/", "httpa://foo:80/", true},
{"http://foo:-80/", "http://foo:-80/", false},
{"https://foo:443/", "https://foo/", true},
{"https://foo:80/", "https://foo:80/", true},
{"ftp://foo:21/", "ftp://foo/", true},
{"ftp://foo:80/", "ftp://foo:80/", true},
{"gopher://foo:70/", "gopher://foo/", true},
{"gopher://foo:443/", "gopher://foo:443/", true},
{"ws://foo:80/", "ws://foo/", true},
{"ws://foo:81/", "ws://foo:81/", true},
{"ws://foo:443/", "ws://foo:443/", true},
{"ws://foo:815/", "ws://foo:815/", true},
{"wss://foo:80/", "wss://foo:80/", true},
{"wss://foo:81/", "wss://foo:81/", true},
{"wss://foo:443/", "wss://foo/", true},
{"wss://foo:815/", "wss://foo:815/", true},
};
for (size_t i = 0; i < ARRAYSIZE(cases); i++) {
int url_len = static_cast<int>(strlen(cases[i].input));
url_parse::Parsed parsed;
url_parse::ParseStandardURL(cases[i].input, url_len, &parsed);
url_parse::Parsed out_parsed;
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
bool success = url_canon::CanonicalizeStandardURL(
cases[i].input, url_len, parsed, NULL, &output, &out_parsed);
output.Complete();
EXPECT_EQ(cases[i].expected_success, success);
EXPECT_EQ(cases[i].expected, out_str);
}
}
TEST(URLCanonTest, ReplaceStandardURL) {
ReplaceCase replace_cases[] = {
{"http://www.google.com/foo?bar=baz#ref", NULL, NULL, NULL, NULL, NULL, "/", kDeleteComp, kDeleteComp, "http://www.google.com/"},
{"http://a:b@google.com:22/foo;bar?baz@cat", "https", "me", "pw", "host.com", "99", "/path", "query", "ref", "https://me:pw@host.com:99/path?query#ref"},
{"http://a:b@google.com:22/foo?baz@cat", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "http://a:b@google.com:22/foo?baz@cat"},
{"http://a:b@google.com:22/foo?baz@cat", "filesystem", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem://a:b@google.com:22/foo?baz@cat"},
};
for (size_t i = 0; i < arraysize(replace_cases); i++) {
const ReplaceCase& cur = replace_cases[i];
int base_len = static_cast<int>(strlen(cur.base));
url_parse::Parsed parsed;
url_parse::ParseStandardURL(cur.base, base_len, &parsed);
url_canon::Replacements<char> r;
typedef url_canon::Replacements<char> R;
SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
url_parse::Parsed out_parsed;
url_canon::ReplaceStandardURL(replace_cases[i].base, parsed,
r, NULL, &output, &out_parsed);
output.Complete();
EXPECT_EQ(replace_cases[i].expected, out_str);
}
{
const char src[] = "http://www.google.com/here_is_the_path";
int src_len = static_cast<int>(strlen(src));
url_parse::Parsed parsed;
url_parse::ParseStandardURL(src, src_len, &parsed);
url_canon::Replacements<char> r;
r.SetPath(reinterpret_cast<char*>(0x00000001), url_parse::Component(0, 0));
std::string out_str1;
url_canon::StdStringCanonOutput output1(&out_str1);
url_parse::Parsed new_parsed;
url_canon::ReplaceStandardURL(src, parsed, r, NULL, &output1, &new_parsed);
output1.Complete();
EXPECT_STREQ("http://www.google.com/", out_str1.c_str());
r.SetPath(reinterpret_cast<char*>(0x00000001), url_parse::Component());
std::string out_str2;
url_canon::StdStringCanonOutput output2(&out_str2);
url_canon::ReplaceStandardURL(src, parsed, r, NULL, &output2, &new_parsed);
output2.Complete();
EXPECT_STREQ("http://www.google.com/", out_str2.c_str());
}
}
TEST(URLCanonTest, ReplaceFileURL) {
ReplaceCase replace_cases[] = {
{"file:///C:/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
{"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
{"file:///C:/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///C:/gaba"},
{"file:///C:/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
{"file:///home/gaba?query#ref", NULL, NULL, NULL, "filer", NULL, "/foo", "b", "c", "file://filer/foo?b#c"},
{"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///home/gaba?query#ref"},
{"file:///home/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "file:///home/gaba"},
{"file:///home/gaba", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "file:///interesting/"},
{"file:///C:/gaba?query#ref", "http", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "file:///C:/gaba?query#ref"},
};
for (size_t i = 0; i < arraysize(replace_cases); i++) {
const ReplaceCase& cur = replace_cases[i];
int base_len = static_cast<int>(strlen(cur.base));
url_parse::Parsed parsed;
url_parse::ParseFileURL(cur.base, base_len, &parsed);
url_canon::Replacements<char> r;
typedef url_canon::Replacements<char> R;
SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
url_parse::Parsed out_parsed;
url_canon::ReplaceFileURL(cur.base, parsed,
r, NULL, &output, &out_parsed);
output.Complete();
EXPECT_EQ(replace_cases[i].expected, out_str);
}
}
TEST(URLCanonTest, ReplaceFileSystemURL) {
ReplaceCase replace_cases[] = {
{"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "/foo", "b", "c", "filesystem:file:///temporary/foo?b#c"},
{"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:file:///temporary/gaba?query#ref"},
{"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, NULL, kDeleteComp, kDeleteComp, "filesystem:file:///temporary/gaba"},
{"filesystem:file:///temporary/gaba?query#ref", NULL, NULL, NULL, NULL, NULL, "interesting/", NULL, NULL, "filesystem:file:///temporary/interesting/?query#ref"},
{"filesystem:http://u:p@bar.com/t/gaba?query#ref", "http", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:http://u:p@bar.com/t/gaba?query#ref"},
{"filesystem:http://u:p@bar.com/t/gaba?query#ref", NULL, "u2", NULL, NULL, NULL, NULL, NULL, NULL, "filesystem:http://u:p@bar.com/t/gaba?query#ref"},
{"filesystem:http://u:p@bar.com/t/gaba?query#ref", NULL, NULL, "pw2", NULL, NULL, NULL, NULL, NULL, "filesystem:http://u:p@bar.com/t/gaba?query#ref"},
{"filesystem:http://u:p@bar.com/t/gaba?query#ref", NULL, NULL, NULL, "foo.com", NULL, NULL, NULL, NULL, "filesystem:http://u:p@bar.com/t/gaba?query#ref"},
{"filesystem:http://u:p@bar.com:40/t/gaba?query#ref", NULL, NULL, NULL, NULL, "41", NULL, NULL, NULL, "filesystem:http://u:p@bar.com:40/t/gaba?query#ref"},
};
for (size_t i = 0; i < arraysize(replace_cases); i++) {
const ReplaceCase& cur = replace_cases[i];
int base_len = static_cast<int>(strlen(cur.base));
url_parse::Parsed parsed;
url_parse::ParseFileSystemURL(cur.base, base_len, &parsed);
url_canon::Replacements<char> r;
typedef url_canon::Replacements<char> R;
SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
url_parse::Parsed out_parsed;
url_canon::ReplaceFileSystemURL(cur.base, parsed, r, NULL,
&output, &out_parsed);
output.Complete();
EXPECT_EQ(replace_cases[i].expected, out_str);
}
}
TEST(URLCanonTest, ReplacePathURL) {
ReplaceCase replace_cases[] = {
{"data:foo", "javascript", NULL, NULL, NULL, NULL, "alert('foo?');", NULL, NULL, "javascript:alert('foo?');"},
{"data:foo", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "data:foo"},
{"data:foo", "javascript", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "javascript:foo"},
{"data:foo", NULL, NULL, NULL, NULL, NULL, "bar", NULL, NULL, "data:bar"},
{"data:foo", NULL, NULL, NULL, NULL, NULL, kDeleteComp, NULL, NULL, "data:"},
};
for (size_t i = 0; i < arraysize(replace_cases); i++) {
const ReplaceCase& cur = replace_cases[i];
int base_len = static_cast<int>(strlen(cur.base));
url_parse::Parsed parsed;
url_parse::ParsePathURL(cur.base, base_len, false, &parsed);
url_canon::Replacements<char> r;
typedef url_canon::Replacements<char> R;
SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
url_parse::Parsed out_parsed;
url_canon::ReplacePathURL(cur.base, parsed,
r, &output, &out_parsed);
output.Complete();
EXPECT_EQ(replace_cases[i].expected, out_str);
}
}
TEST(URLCanonTest, ReplaceMailtoURL) {
ReplaceCase replace_cases[] = {
{"mailto:jon@foo.com?body=sup", "mailto", NULL, NULL, NULL, NULL, "addr1", "to=tony", NULL, "mailto:addr1?to=tony"},
{"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, "mailto:jon@foo.com?body=sup"},
{"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", NULL, NULL, "mailto:jason?body=sup"},
{"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "custom=1", NULL, "mailto:jon@foo.com?custom=1"},
{"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "jason", "custom=1", NULL, "mailto:jason?custom=1"},
{"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "", NULL, "mailto:jon@foo.com?"},
{"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, NULL, "|", NULL, "mailto:jon@foo.com"},
{"mailto:jon@foo.com?body=sup", NULL, NULL, NULL, NULL, NULL, "|", NULL, NULL, "mailto:?body=sup"},
{"mailto:", NULL, NULL, NULL, NULL, NULL, "|", "|", NULL, "mailto:"},
{"mailto:addr1", NULL, NULL, NULL, NULL, NULL, NULL, NULL, "BLAH", "mailto:addr1"},
};
for (size_t i = 0; i < arraysize(replace_cases); i++) {
const ReplaceCase& cur = replace_cases[i];
int base_len = static_cast<int>(strlen(cur.base));
url_parse::Parsed parsed;
url_parse::ParseMailtoURL(cur.base, base_len, &parsed);
url_canon::Replacements<char> r;
typedef url_canon::Replacements<char> R;
SetupReplComp(&R::SetScheme, &R::ClearRef, &r, cur.scheme);
SetupReplComp(&R::SetUsername, &R::ClearUsername, &r, cur.username);
SetupReplComp(&R::SetPassword, &R::ClearPassword, &r, cur.password);
SetupReplComp(&R::SetHost, &R::ClearHost, &r, cur.host);
SetupReplComp(&R::SetPort, &R::ClearPort, &r, cur.port);
SetupReplComp(&R::SetPath, &R::ClearPath, &r, cur.path);
SetupReplComp(&R::SetQuery, &R::ClearQuery, &r, cur.query);
SetupReplComp(&R::SetRef, &R::ClearRef, &r, cur.ref);
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
url_parse::Parsed out_parsed;
url_canon::ReplaceMailtoURL(cur.base, parsed,
r, &output, &out_parsed);
output.Complete();
EXPECT_EQ(replace_cases[i].expected, out_str);
}
}
TEST(URLCanonTest, CanonicalizeFileURL) {
struct URLCase {
const char* input;
const char* expected;
bool expected_success;
url_parse::Component expected_host;
url_parse::Component expected_path;
} cases[] = {
#ifdef _WIN32
{"file:c:\\foo\\bar.html", "file:///C:/foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 16)},
{" File:c|////foo\\bar.html", "file:///C:////foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 19)},
{"file:", "file:///", true, url_parse::Component(), url_parse::Component(7, 1)},
{"file:UNChost/path", "file://unchost/path", true, url_parse::Component(7, 7), url_parse::Component(14, 5)},
{"c:\\foo\\bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
{"C|/foo/bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
{"/C|\\foo\\bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
{"//C|/foo/bar", "file:///C:/foo/bar", true, url_parse::Component(), url_parse::Component(7, 11)},
{"//server/file", "file://server/file", true, url_parse::Component(7, 6), url_parse::Component(13, 5)},
{"\\\\server\\file", "file://server/file", true, url_parse::Component(7, 6), url_parse::Component(13, 5)},
{"/\\server/file", "file://server/file", true, url_parse::Component(7, 6), url_parse::Component(13, 5)},
{"file:c:foo/bar.html", "file:///C:/foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 16)},
{"file:/\\/\\C:\\\\//foo\\bar.html", "file:///C:////foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 19)},
{"file:///foo/bar.txt", "file:///foo/bar.txt", true, url_parse::Component(), url_parse::Component(7, 12)},
{"FILE:/\\/\\7:\\\\//foo\\bar.html", "file://7:////foo/bar.html", false, url_parse::Component(7, 2), url_parse::Component(9, 16)},
{"file:filer/home\\me", "file://filer/home/me", true, url_parse::Component(7, 5), url_parse::Component(12, 8)},
{"file:///C:/foo/../../../bar.html", "file:///C:/bar.html", true, url_parse::Component(), url_parse::Component(7, 12)},
{"file:///C:/asdf#\xc2", "file:///C:/asdf#\xef\xbf\xbd", true, url_parse::Component(), url_parse::Component(7, 8)},
#else
{"file:///home/me", "file:///home/me", true, url_parse::Component(), url_parse::Component(7, 8)},
{"file:c:\\foo\\bar.html", "file:///c:/foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 16)},
{"file:c|//foo\\bar.html", "file:///c%7C//foo/bar.html", true, url_parse::Component(), url_parse::Component(7, 19)},
{"//", "file:///", true, url_parse::Component(), url_parse::Component(7, 1)},
{"///", "file:///", true, url_parse::Component(), url_parse::Component(7, 1)},
{"///test", "file:///test", true, url_parse::Component(), url_parse::Component(7, 5)},
{"file://test", "file://test/", true, url_parse::Component(7, 4), url_parse::Component(11, 1)},
{"file://localhost", "file://localhost/", true, url_parse::Component(7, 9), url_parse::Component(16, 1)},
{"file://localhost/", "file://localhost/", true, url_parse::Component(7, 9), url_parse::Component(16, 1)},
{"file://localhost/test", "file://localhost/test", true, url_parse::Component(7, 9), url_parse::Component(16, 5)},
#endif
};
for (size_t i = 0; i < ARRAYSIZE(cases); i++) {
int url_len = static_cast<int>(strlen(cases[i].input));
url_parse::Parsed parsed;
url_parse::ParseFileURL(cases[i].input, url_len, &parsed);
url_parse::Parsed out_parsed;
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
bool success = url_canon::CanonicalizeFileURL(cases[i].input, url_len,
parsed, NULL, &output,
&out_parsed);
output.Complete();
EXPECT_EQ(cases[i].expected_success, success);
EXPECT_EQ(cases[i].expected, out_str);
EXPECT_EQ(0, out_parsed.scheme.begin);
EXPECT_EQ(4, out_parsed.scheme.len);
EXPECT_EQ(cases[i].expected_host.begin, out_parsed.host.begin);
EXPECT_EQ(cases[i].expected_host.len, out_parsed.host.len);
EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
}
}
TEST(URLCanonTest, CanonicalizeFileSystemURL) {
struct URLCase {
const char* input;
const char* expected;
bool expected_success;
} cases[] = {
{"Filesystem:htTp://www.Foo.com:80/tempoRary", "filesystem:http://www.foo.com/tempoRary/", true},
{"filesystem:httpS://www.foo.com/temporary/", "filesystem:https://www.foo.com/temporary/", true},
{"filesystem:http://www.foo.com//", "filesystem:http://www.foo.com//", false},
{"filesystem:http://www.foo.com/persistent/bob?query#ref", "filesystem:http://www.foo.com/persistent/bob?query#ref", true},
{"filesystem:fIle://\\temporary/", "filesystem:file:///temporary/", true},
{"filesystem:fiLe:///temporary", "filesystem:file:///temporary/", true},
{"filesystem:File:///temporary/Bob?qUery#reF", "filesystem:file:///temporary/Bob?qUery#reF", true},
};
for (size_t i = 0; i < ARRAYSIZE(cases); i++) {
int url_len = static_cast<int>(strlen(cases[i].input));
url_parse::Parsed parsed;
url_parse::ParseFileSystemURL(cases[i].input, url_len, &parsed);
url_parse::Parsed out_parsed;
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
bool success = url_canon::CanonicalizeFileSystemURL(cases[i].input, url_len,
parsed, NULL, &output,
&out_parsed);
output.Complete();
EXPECT_EQ(cases[i].expected_success, success);
EXPECT_EQ(cases[i].expected, out_str);
EXPECT_EQ(0, out_parsed.scheme.begin);
EXPECT_EQ(10, out_parsed.scheme.len);
if (success)
EXPECT_GT(out_parsed.path.len, 0);
}
}
TEST(URLCanonTest, CanonicalizePathURL) {
struct PathCase {
const char* input;
const char* expected;
} path_cases[] = {
{"javascript:", "javascript:"},
{"JavaScript:Foo", "javascript:Foo"},
{":\":This /is interesting;?#", ":\":This /is interesting;?#"},
};
for (size_t i = 0; i < ARRAYSIZE(path_cases); i++) {
int url_len = static_cast<int>(strlen(path_cases[i].input));
url_parse::Parsed parsed;
url_parse::ParsePathURL(path_cases[i].input, url_len, true, &parsed);
url_parse::Parsed out_parsed;
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
bool success = url_canon::CanonicalizePathURL(path_cases[i].input, url_len,
parsed, &output,
&out_parsed);
output.Complete();
EXPECT_TRUE(success);
EXPECT_EQ(path_cases[i].expected, out_str);
EXPECT_EQ(0, out_parsed.host.begin);
EXPECT_EQ(-1, out_parsed.host.len);
if (path_cases[i].input[url_len - 1] == ':') {
EXPECT_EQ(0, out_parsed.GetContent().begin);
EXPECT_EQ(-1, out_parsed.GetContent().len);
}
}
}
TEST(URLCanonTest, CanonicalizeMailtoURL) {
struct URLCase {
const char* input;
const char* expected;
bool expected_success;
url_parse::Component expected_path;
url_parse::Component expected_query;
} cases[] = {
{"mailto:addr1", "mailto:addr1", true, url_parse::Component(7, 5), url_parse::Component()},
{"mailto:addr1@foo.com", "mailto:addr1@foo.com", true, url_parse::Component(7, 13), url_parse::Component()},
{"MaIlTo:addr1 \t ", "mailto:addr1", true, url_parse::Component(7, 5), url_parse::Component()},
{"MaIlTo:addr1?to=jon", "mailto:addr1?to=jon", true, url_parse::Component(7, 5), url_parse::Component(13,6)},
{"mailto:addr1,addr2", "mailto:addr1,addr2", true, url_parse::Component(7, 11), url_parse::Component()},
{"mailto:addr1, addr2", "mailto:addr1, addr2", true, url_parse::Component(7, 12), url_parse::Component()},
{"mailto:addr1%2caddr2", "mailto:addr1%2caddr2", true, url_parse::Component(7, 13), url_parse::Component()},
{"mailto:\xF0\x90\x8C\x80", "mailto:%F0%90%8C%80", true, url_parse::Component(7, 12), url_parse::Component()},
{"mailto:addr1\0addr2?foo", "mailto:addr1%00addr2?foo", true, url_parse::Component(7, 13), url_parse::Component(21, 3)},
{"mailto:\xed\xa0\x80", "mailto:%EF%BF%BD", false, url_parse::Component(7, 9), url_parse::Component()},
{"mailto:addr1?", "mailto:addr1?", true, url_parse::Component(7, 5), url_parse::Component(13, 0)},
};
url_parse::Parsed parsed;
url_parse::Parsed out_parsed;
for (size_t i = 0; i < ARRAYSIZE(cases); i++) {
int url_len = static_cast<int>(strlen(cases[i].input));
if (i == 8) {
url_len = 22;
}
url_parse::ParseMailtoURL(cases[i].input, url_len, &parsed);
std::string out_str;
url_canon::StdStringCanonOutput output(&out_str);
bool success = url_canon::CanonicalizeMailtoURL(cases[i].input, url_len,
parsed, &output,
&out_parsed);
output.Complete();
EXPECT_EQ(cases[i].expected_success, success);
EXPECT_EQ(cases[i].expected, out_str);
EXPECT_EQ(0, out_parsed.scheme.begin);
EXPECT_EQ(6, out_parsed.scheme.len);
EXPECT_EQ(cases[i].expected_path.begin, out_parsed.path.begin);
EXPECT_EQ(cases[i].expected_path.len, out_parsed.path.len);
EXPECT_EQ(cases[i].expected_query.begin, out_parsed.query.begin);
EXPECT_EQ(cases[i].expected_query.len, out_parsed.query.len);
}
}
#ifndef WIN32
TEST(URLCanonTest, _itoa_s) {
char buf[6];
memset(buf, 0xff, sizeof(buf));
EXPECT_EQ(0, url_canon::_itoa_s(12, buf, sizeof(buf) - 1, 10));
EXPECT_STREQ("12", buf);
EXPECT_EQ('\xFF', buf[3]);
memset(buf, 0xff, sizeof(buf));
EXPECT_EQ(0, url_canon::_itoa_s(1234, buf, sizeof(buf) - 1, 10));
EXPECT_STREQ("1234", buf);
EXPECT_EQ('\xFF', buf[5]);
memset(buf, 0xff, sizeof(buf));
EXPECT_EQ(EINVAL, url_canon::_itoa_s(12345, buf, sizeof(buf) - 1, 10));
EXPECT_EQ('\xFF', buf[5]);
memset(buf, 0xff, sizeof(buf));
EXPECT_EQ(0, url_canon::_itoa_s(12, buf, 10));
EXPECT_STREQ("12", buf);
EXPECT_EQ('\xFF', buf[3]);
memset(buf, 0xff, sizeof(buf));
EXPECT_EQ(0, url_canon::_itoa_s(12345, buf, 10));
EXPECT_STREQ("12345", buf);
EXPECT_EQ(EINVAL, url_canon::_itoa_s(123456, buf, 10));
memset(buf, 0xff, sizeof(buf));
EXPECT_EQ(0, url_canon::_itoa_s(1234, buf, sizeof(buf) - 1, 16));
EXPECT_STREQ("4d2", buf);
EXPECT_EQ('\xFF', buf[5]);
}
TEST(URLCanonTest, _itow_s) {
base::char16 buf[6];
const char fill_mem = 0xff;
const base::char16 fill_char = 0xffff;
memset(buf, fill_mem, sizeof(buf));
EXPECT_EQ(0, url_canon::_itow_s(12, buf, sizeof(buf) / 2 - 1, 10));
EXPECT_EQ(WStringToUTF16(L"12"), base::string16(buf));
EXPECT_EQ(fill_char, buf[3]);
EXPECT_EQ(0, url_canon::_itow_s(1234, buf, sizeof(buf) / 2 - 1, 10));
EXPECT_EQ(WStringToUTF16(L"1234"), base::string16(buf));
EXPECT_EQ(fill_char, buf[5]);
memset(buf, fill_mem, sizeof(buf));
EXPECT_EQ(EINVAL, url_canon::_itow_s(12345, buf, sizeof(buf) / 2 - 1, 10));
EXPECT_EQ(fill_char, buf[5]);
memset(buf, fill_mem, sizeof(buf));
EXPECT_EQ(0, url_canon::_itow_s(12, buf, 10));
EXPECT_EQ(WStringToUTF16(L"12"), base::string16(buf));
EXPECT_EQ(fill_char, buf[3]);
memset(buf, fill_mem, sizeof(buf));
EXPECT_EQ(0, url_canon::_itow_s(12345, buf, 10));
EXPECT_EQ(WStringToUTF16(L"12345"), base::string16(buf));
EXPECT_EQ(EINVAL, url_canon::_itow_s(123456, buf, 10));
}
#endif
static bool ParsedIsEqual(const url_parse::Parsed& a,
const url_parse::Parsed& b) {
return a.scheme.begin == b.scheme.begin && a.scheme.len == b.scheme.len &&
a.username.begin == b.username.begin && a.username.len == b.username.len &&
a.password.begin == b.password.begin && a.password.len == b.password.len &&
a.host.begin == b.host.begin && a.host.len == b.host.len &&
a.port.begin == b.port.begin && a.port.len == b.port.len &&
a.path.begin == b.path.begin && a.path.len == b.path.len &&
a.query.begin == b.query.begin && a.query.len == b.query.len &&
a.ref.begin == b.ref.begin && a.ref.len == b.ref.len;
}
TEST(URLCanonTest, ResolveRelativeURL) {
struct RelativeCase {
const char* base;
bool is_base_hier;
bool is_base_file;
const char* test;
bool succeed_relative;
bool is_rel;
bool succeed_resolve;
const char* resolved;
} rel_cases[] = {
{"http://host/a", true, false, "http://another/", true, false, false, NULL},
{"http://host/a", true, false, "http:////another/", true, false, false, NULL},
{"http://foo/bar", true, false, "", true, true, true, "http://foo/bar"},
{"http://foo/bar#ref", true, false, "", true, true, true, "http://foo/bar"},
{"http://foo/bar#", true, false, "", true, true, true, "http://foo/bar"},
{"http://foo/bar", true, false, " another ", true, true, true, "http://foo/another"},
{"http://foo/bar", true, false, " . ", true, true, true, "http://foo/"},
{"http://foo/bar", true, false, " \t ", true, true, true, "http://foo/bar"},
{"http://host/a", true, false, "http:path", true, true, true, "http://host/path"},
{"http://host/a/", true, false, "http:path", true, true, true, "http://host/a/path"},
{"http://host/a", true, false, "http:/path", true, true, true, "http://host/path"},
{"http://host/a", true, false, "HTTP:/path", true, true, true, "http://host/path"},
{"http://host/a", true, false, "https:host2", true, false, false, NULL},
{"http://host/a", true, false, "htto:/host2", true, false, false, NULL},
{"http://host/a", true, false, "/b/c/d", true, true, true, "http://host/b/c/d"},
{"http://host/a", true, false, "\\b\\c\\d", true, true, true, "http://host/b/c/d"},
{"http://host/a", true, false, "/b/../c", true, true, true, "http://host/c"},
{"http://host/a?b#c", true, false, "/b/../c", true, true, true, "http://host/c"},
{"http://host/a", true, false, "\\b/../c?x#y", true, true, true, "http://host/c?x#y"},
{"http://host/a?b#c", true, false, "/b/../c?x#y", true, true, true, "http://host/c?x#y"},
{"http://host/a", true, false, "b", true, true, true, "http://host/b"},
{"http://host/a", true, false, "bc/de", true, true, true, "http://host/bc/de"},
{"http://host/a/", true, false, "bc/de?query#ref", true, true, true, "http://host/a/bc/de?query#ref"},
{"http://host/a/", true, false, ".", true, true, true, "http://host/a/"},
{"http://host/a/", true, false, "..", true, true, true, "http://host/"},
{"http://host/a/", true, false, "./..", true, true, true, "http://host/"},
{"http://host/a/", true, false, "../.", true, true, true, "http://host/"},
{"http://host/a/", true, false, "././.", true, true, true, "http://host/a/"},
{"http://host/a?query#ref", true, false, "../../../foo", true, true, true, "http://host/foo"},
{"http://host/a", true, false, "?foo=bar", true, true, true, "http://host/a?foo=bar"},
{"http://host/a?x=y#z", true, false, "?", true, true, true, "http://host/a?"},
{"http://host/a?x=y#z", true, false, "?foo=bar#com", true, true, true, "http://host/a?foo=bar#com"},
{"http://host/a", true, false, "#ref", true, true, true, "http://host/a#ref"},
{"http://host/a#b", true, false, "#", true, true, true, "http://host/a#"},
{"http://host/a?foo=bar#hello", true, false, "#bye", true, true, true, "http://host/a?foo=bar#bye"},
{"data:foobar", false, false, "baz.html", false, false, false, NULL},
{"data:foobar", false, false, "data:baz", true, false, false, NULL},
{"data:foobar", false, false, "data:/base", true, false, false, NULL},
{"data:foobar", false, false, "http://host/", true, false, false, NULL},
{"data:foobar", false, false, "http:host", true, false, false, NULL},
{"http://foo/bar", true, false, "./asd:fgh", true, true, true, "http://foo/asd:fgh"},
{"http://foo/bar", true, false, ":foo", true, true, true, "http://foo/:foo"},
{"http://foo/bar", true, false, " hello world", true, true, true, "http://foo/hello%20world"},
{"data:asdf", false, false, ":foo", false, false, false, NULL},
{"data:asdf", false, false, "bad(':foo')", false, false, false, NULL},
{"http://host/a", true, false, ";foo", true, true, true, "http://host/;foo"},
{"http://host/a;", true, false, ";foo", true, true, true, "http://host/;foo"},
{"http://host/a", true, false, ";/../bar", true, true, true, "http://host/bar"},
{"http://host/a", true, false, "//another", true, true, true, "http://another/"},
{"http://host/a", true, false, "//another/path?query#ref", true, true, true, "http://another/path?query#ref"},
{"http://host/a", true, false, "///another/path", true, true, true, "http://another/path"},
{"http://host/a", true, false, "//Another\\path", true, true, true, "http://another/path"},
{"http://host/a", true, false, "//", true, true, false, "http:"},
{"http://host/a", true, false, "\\/another/path", true, true, true, "http://another/path"},
{"http://host/a", true, false, "/\\Another\\path", true, true, true, "http://another/path"},
#ifdef WIN32
{"file:///C:/foo", true, true, "http://host/", true, false, false, NULL},
{"file:///C:/foo", true, true, "bar", true, true, true, "file:///C:/bar"},
{"file:///C:/foo", true, true, "../../../bar.html", true, true, true, "file:///C:/bar.html"},
{"file:///C:/foo", true, true, "/../bar.html", true, true, true, "file:///C:/bar.html"},
{"http://host/a", true, false, "\\\\another\\path", true, false, false, NULL},
{"file:///C:/something", true, true, "//c:/foo", true, true, true, "file:///C:/foo"},
{"file:///C:/something", true, true, "//localhost/c:/foo", true, true, true, "file:///C:/foo"},
{"file:///C:/foo", true, true, "c:", true, false, false, NULL},
{"file:///C:/foo", true, true, "c:/foo", true, false, false, NULL},
{"http://host/a", true, false, "c:\\foo", true, false, false, NULL},
{"file:///C:/foo", true, true, "/z:/bar", true, true, true, "file:///Z:/bar"},
{"file:///C:/foo", true, true, "/bar", true, true, true, "file:///C:/bar"},
{"file://localhost/C:/foo", true, true, "/bar", true, true, true, "file://localhost/C:/bar"},
{"file:///C:/foo/com/", true, true, "/bar", true, true, true, "file:///C:/bar"},
{"file:///C:/something", true, true, "//somehost/path", true, true, true, "file://somehost/path"},
{"file:///C:/something", true, true, "/\\//somehost/path", true, true, true, "file://somehost/path"},
#else
{"http://host/a", true, false, "\\\\Another\\path", true, true, true, "http://another/path"},
#endif
{"http://host/a", true, false, "/c:\\foo", true, true, true, "http://host/c:/foo"},
{"http://host/a", true, false, "//c:\\foo", true, true, true, "http://c/foo"},
{"file:///foo.txt", true, true, "//host:80/bar.txt", true, true, false, "file://host:80/bar.txt"},
{"filesystem:http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
{"filesystem:http://host/t/path", true, false, "filesystem:https://host/t/path2", true, false, false, NULL},
{"filesystem:http://host/t/path", true, false, "http://host/t/path2", true, false, false, NULL},
{"http://host/t/path", true, false, "filesystem:http://host/t/path2", true, false, false, NULL},
{"filesystem:http://host/t/path", true, false, "./path2", true, true, true, "filesystem:http://host/t/path2"},
{"filesystem:http://host/t/path/", true, false, "path2", true, true, true, "filesystem:http://host/t/path/path2"},
{"filesystem:http://host/t/path", true, false, "filesystem:http:path2", true, false, false, NULL},
{"about:blank", false, false, "http://X/A", true, false, true, ""},
{"about:blank", false, false, "content://content.Provider/", true, false, true, ""},
};
for (size_t i = 0; i < ARRAYSIZE(rel_cases); i++) {
const RelativeCase& cur_case = rel_cases[i];
url_parse::Parsed parsed;
int base_len = static_cast<int>(strlen(cur_case.base));
if (cur_case.is_base_file)
url_parse::ParseFileURL(cur_case.base, base_len, &parsed);
else if (cur_case.is_base_hier)
url_parse::ParseStandardURL(cur_case.base, base_len, &parsed);
else
url_parse::ParsePathURL(cur_case.base, base_len, false, &parsed);
int test_len = static_cast<int>(strlen(cur_case.test));
bool is_relative;
url_parse::Component relative_component;
bool succeed_is_rel = url_canon::IsRelativeURL(
cur_case.base, parsed, cur_case.test, test_len, cur_case.is_base_hier,
&is_relative, &relative_component);
EXPECT_EQ(cur_case.succeed_relative, succeed_is_rel) <<
"succeed is rel failure on " << cur_case.test;
EXPECT_EQ(cur_case.is_rel, is_relative) <<
"is rel failure on " << cur_case.test;
if (succeed_is_rel && is_relative && cur_case.is_rel) {
std::string resolved;
url_canon::StdStringCanonOutput output(&resolved);
url_parse::Parsed resolved_parsed;
bool succeed_resolve = url_canon::ResolveRelativeURL(
cur_case.base, parsed, cur_case.is_base_file,
cur_case.test, relative_component, NULL, &output, &resolved_parsed);
output.Complete();
EXPECT_EQ(cur_case.succeed_resolve, succeed_resolve);
EXPECT_EQ(cur_case.resolved, resolved) << " on " << cur_case.test;
url_parse::Parsed ref_parsed;
int resolved_len = static_cast<int>(resolved.size());
if (cur_case.is_base_file) {
url_parse::ParseFileURL(resolved.c_str(), resolved_len, &ref_parsed);
} else if (cur_case.is_base_hier) {
url_parse::ParseStandardURL(resolved.c_str(), resolved_len,
&ref_parsed);
} else {
url_parse::ParsePathURL(resolved.c_str(), resolved_len, false,
&ref_parsed);
}
EXPECT_TRUE(ParsedIsEqual(ref_parsed, resolved_parsed));
}
}
}
TEST(URLCanonTest, ReplacementOverflow) {
const char src[] = "file:///C:/foo/bar";
int src_len = static_cast<int>(strlen(src));
url_parse::Parsed parsed;
url_parse::ParseFileURL(src, src_len, &parsed);
url_canon::Replacements<base::char16> repl;
base::string16 new_query;
for (int i = 0; i < 4800; i++)
new_query.push_back('a');
base::string16 new_path(WStringToUTF16(L"/foo"));
repl.SetPath(new_path.c_str(), url_parse::Component(0, 4));
repl.SetQuery(new_query.c_str(),
url_parse::Component(0, static_cast<int>(new_query.length())));
url_parse::Parsed repl_parsed;
std::string repl_str;
url_canon::StdStringCanonOutput repl_output(&repl_str);
url_canon::ReplaceFileURL(src, parsed, repl, NULL, &repl_output, &repl_parsed);
repl_output.Complete();
std::string expected("file:///foo?");
for (size_t i = 0; i < new_query.length(); i++)
expected.push_back('a');
EXPECT_TRUE(expected == repl_str);
}