This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include "config.h"
#include "platform/weborigin/KURL.h"
#include "wtf/testing/WTFTestHelpers.h"
#include "wtf/text/CString.h"
#include "wtf/text/WTFString.h"
#include <gtest/gtest.h>
namespace {
struct ComponentCase {
const char* url;
const char* protocol;
const char* host;
const int port;
const char* user;
const char* pass;
const char* lastPath;
const char* query;
const char* ref;
};
TEST(KURLTest, SameGetters)
{
struct GetterCase {
const char* url;
const char* protocol;
const char* host;
int port;
const char* user;
const char* pass;
const char* lastPathComponent;
const char* query;
const char* ref;
bool hasRef;
} cases[] = {
{"http://www.google.com/foo/blah?bar=baz#ref", "http", "www.google.com", 0, "", 0, "blah", "bar=baz", "ref", true},
{"http://foo.com:1234/foo/bar/", "http", "foo.com", 1234, "", 0, "bar", 0, 0, false},
{"http://www.google.com?#", "http", "www.google.com", 0, "", 0, 0, "", "", true},
{"https://me:pass@google.com:23#foo", "https", "google.com", 23, "me", "pass", 0, 0, "foo", true},
{"javascript:hello!//world", "javascript", "", 0, "", 0, "world", 0, 0, false},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
WebCore::KURL kurl(WebCore::ParsedURLString, cases[i].url);
EXPECT_EQ(cases[i].protocol, kurl.protocol());
EXPECT_EQ(cases[i].host, kurl.host());
EXPECT_EQ(cases[i].port, kurl.port());
EXPECT_EQ(cases[i].user, kurl.user());
EXPECT_EQ(cases[i].pass, kurl.pass());
EXPECT_EQ(cases[i].lastPathComponent, kurl.lastPathComponent());
EXPECT_EQ(cases[i].query, kurl.query());
EXPECT_EQ(cases[i].ref, kurl.fragmentIdentifier());
EXPECT_EQ(cases[i].hasRef, kurl.hasFragmentIdentifier());
WTF::String utf16(cases[i].url);
kurl = WebCore::KURL(WebCore::ParsedURLString, utf16);
EXPECT_EQ(cases[i].protocol, kurl.protocol());
EXPECT_EQ(cases[i].host, kurl.host());
EXPECT_EQ(cases[i].port, kurl.port());
EXPECT_EQ(cases[i].user, kurl.user());
EXPECT_EQ(cases[i].pass, kurl.pass());
EXPECT_EQ(cases[i].lastPathComponent, kurl.lastPathComponent());
EXPECT_EQ(cases[i].query, kurl.query());
EXPECT_EQ(cases[i].ref, kurl.fragmentIdentifier());
EXPECT_EQ(cases[i].hasRef, kurl.hasFragmentIdentifier());
}
}
TEST(KURLTest, DISABLED_DifferentGetters)
{
ComponentCase cases[] = {
{"javascript:hello!?#/\\world", "javascript", "", 0, "", 0, "world", 0, 0},
{"http://a.com/hello;world", "http", "a.com", 0, "", 0, "hello", 0, 0},
{"http://\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd/", "http", "xn--6qqa088eba", 0, "", 0, 0, 0, 0},
{"http://www.google.com/foo/blah?bar=baz#\xce\xb1\xce\xb2", "http", "www.google.com", 0, "", 0, "blah", "bar=baz", "\xce\xb1\xce\xb2"},
};
for (size_t i = 0; i < arraysize(cases); i++) {
WebCore::KURL kurl(WebCore::ParsedURLString, cases[i].url);
EXPECT_EQ(cases[i].protocol, kurl.protocol());
EXPECT_EQ(cases[i].host, kurl.host());
EXPECT_EQ(cases[i].port, kurl.port());
EXPECT_EQ(cases[i].user, kurl.user());
EXPECT_EQ(cases[i].pass, kurl.pass());
EXPECT_EQ(cases[i].lastPath, kurl.lastPathComponent());
EXPECT_EQ(cases[i].query, kurl.query());
if (cases[i].ref)
EXPECT_EQ(WTF::String::fromUTF8(cases[i].ref), kurl.fragmentIdentifier());
else
EXPECT_TRUE(kurl.fragmentIdentifier().isNull());
}
}
TEST(KURLTest, DISABLED_UTF8)
{
const char asciiURL[] = "http://foo/bar#baz";
WebCore::KURL asciiKURL(WebCore::ParsedURLString, asciiURL);
EXPECT_TRUE(asciiKURL.string() == WTF::String(asciiURL));
EXPECT_TRUE(WTF::equalIgnoringCase(asciiKURL, WTF::String(asciiURL)));
WebCore::KURL mailto(WebCore::ParsedURLString, "mailto:foo@foo.com");
EXPECT_TRUE(WTF::equalIgnoringCase(mailto.protocol(), "mailto"));
const char utf8URL[] = "http://foo/bar#\xe4\xbd\xa0\xe5\xa5\xbd";
WebCore::KURL utf8KURL(WebCore::ParsedURLString, utf8URL);
EXPECT_TRUE(utf8KURL.string() == WTF::String::fromUTF8(utf8URL));
}
TEST(KURLTest, Setters)
{
struct ExpectedComponentCase {
const char* url;
const char* protocol;
const char* host;
const int port;
const char* user;
const char* pass;
const char* path;
const char* query;
const char* expectedProtocol;
const char* expectedHost;
const char* expectedPort;
const char* expectedUser;
const char* expectedPass;
const char* expectedPath;
const char* expectedQuery;
} cases[] = {
{"http://www.google.com/", "https", "news.google.com", 8888, "me", "pass", "/foo", "?q=asdf",
"https://www.google.com/",
"https://news.google.com/",
"https://news.google.com:8888/",
"https://me@news.google.com:8888/",
"https://me:pass@news.google.com:8888/",
"https://me:pass@news.google.com:8888/foo",
"https://me:pass@news.google.com:8888/foo?q=asdf"},
{"https://me:pass@google.com:88/a?f#b", "http", "goo.com", 92, "", "", "/", 0,
"http://me:pass@google.com:88/a?f#b",
"http://me:pass@goo.com:88/a?f#b",
"http://me:pass@goo.com:92/a?f#b",
"http://:pass@goo.com:92/a?f#b",
"http://goo.com:92/a?f#b",
"http://goo.com:92/?f#b",
"http://goo.com:92/#b"},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
WebCore::KURL kurl(WebCore::ParsedURLString, cases[i].url);
kurl.setProtocol(cases[i].protocol);
EXPECT_STREQ(cases[i].expectedProtocol, kurl.string().utf8().data());
kurl.setHost(cases[i].host);
EXPECT_STREQ(cases[i].expectedHost, kurl.string().utf8().data());
kurl.setPort(cases[i].port);
EXPECT_STREQ(cases[i].expectedPort, kurl.string().utf8().data());
kurl.setUser(cases[i].user);
EXPECT_STREQ(cases[i].expectedUser, kurl.string().utf8().data());
kurl.setPass(cases[i].pass);
EXPECT_STREQ(cases[i].expectedPass, kurl.string().utf8().data());
kurl.setPath(cases[i].path);
EXPECT_STREQ(cases[i].expectedPath, kurl.string().utf8().data());
kurl.setQuery(cases[i].query);
EXPECT_STREQ(cases[i].expectedQuery, kurl.string().utf8().data());
}
}
TEST(KURLTest, Decode)
{
struct DecodeCase {
const char* input;
const char* output;
} decodeCases[] = {
{"hello, world", "hello, world"},
{"%01%02%03%04%05%06%07%08%09%0a%0B%0C%0D%0e%0f/", "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0B\x0C\x0D\x0e\x0f/"},
{"%10%11%12%13%14%15%16%17%18%19%1a%1B%1C%1D%1e%1f/", "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1B\x1C\x1D\x1e\x1f/"},
{"%20%21%22%23%24%25%26%27%28%29%2a%2B%2C%2D%2e%2f/", " !\"#$%&'()*+,-.//"},
{"%30%31%32%33%34%35%36%37%38%39%3a%3B%3C%3D%3e%3f/", "0123456789:;<=>?/"},
{"%40%41%42%43%44%45%46%47%48%49%4a%4B%4C%4D%4e%4f/", "@ABCDEFGHIJKLMNO/"},
{"%50%51%52%53%54%55%56%57%58%59%5a%5B%5C%5D%5e%5f/", "PQRSTUVWXYZ[\\]^_/"},
{"%60%61%62%63%64%65%66%67%68%69%6a%6B%6C%6D%6e%6f/", "`abcdefghijklmno/"},
{"%70%71%72%73%74%75%76%77%78%79%7a%7B%7C%7D%7e%7f/", "pqrstuvwxyz{|}~\x7f/"},
{"%e4%bd%a0%e5%a5%bd", "\xe4\xbd\xa0\xe5\xa5\xbd"},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(decodeCases); i++) {
WTF::String input(decodeCases[i].input);
WTF::String str = WebCore::decodeURLEscapeSequences(input);
EXPECT_STREQ(decodeCases[i].output, str.utf8().data());
}
WTF::String zero = WebCore::decodeURLEscapeSequences("%00");
EXPECT_STRNE("%00", zero.utf8().data());
WTF::String invalid = WebCore::decodeURLEscapeSequences(
"%e4%a0%e5%a5%bd");
UChar invalidExpectedHelper[4] = { 0x00e4, 0x00a0, 0x597d, 0 };
WTF::String invalidExpected(
reinterpret_cast<const ::UChar*>(invalidExpectedHelper),
3);
EXPECT_EQ(invalidExpected, invalid);
}
TEST(KURLTest, Encode)
{
struct EncodeCase {
const char* input;
const char* output;
} encode_cases[] = {
{"hello, world", "hello%2C%20world"},
{"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
"%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F"},
{"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
"%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F"},
{" !\"#$%&'()*+,-./", "%20!%22%23%24%25%26%27()*%2B%2C-./"},
{"0123456789:;<=>?",
"0123456789%3A%3B%3C%3D%3E%3F"},
{"@ABCDEFGHIJKLMNO",
"%40ABCDEFGHIJKLMNO"},
{"PQRSTUVWXYZ[\\]^_",
"PQRSTUVWXYZ%5B%5C%5D%5E_"},
{"`abcdefghijklmno",
"%60abcdefghijklmno"},
{"pqrstuvwxyz{|}~\x7f",
"pqrstuvwxyz%7B%7C%7D~%7F"},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(encode_cases); i++) {
WTF::String input(encode_cases[i].input);
WTF::String expectedOutput(encode_cases[i].output);
WTF::String output = WebCore::encodeWithURLEscapeSequences(input);
EXPECT_EQ(expectedOutput, output);
}
WTF::String input("\x00\x01", 2);
WTF::String reference("%00%01");
WTF::String output = WebCore::encodeWithURLEscapeSequences(input);
EXPECT_EQ(reference, output);
UChar wideInputHelper[3] = { 0x4f60, 0x597d, 0 };
WTF::String wideInput(
reinterpret_cast<const ::UChar*>(wideInputHelper), 2);
WTF::String wideReference("%E4%BD%A0%E5%A5%BD");
WTF::String wideOutput =
WebCore::encodeWithURLEscapeSequences(wideInput);
EXPECT_EQ(wideReference, wideOutput);
}
TEST(KURLTest, ResolveEmpty)
{
WebCore::KURL emptyBase;
const char abs[] = "http://www.google.com/";
WebCore::KURL resolveAbs(emptyBase, abs);
EXPECT_TRUE(resolveAbs.isValid());
EXPECT_STREQ(abs, resolveAbs.string().utf8().data());
const char rel[] = "foo.html";
WebCore::KURL resolveErr(emptyBase, rel);
EXPECT_FALSE(resolveErr.isValid());
}
TEST(KURLTest, ReplaceInvalid)
{
WebCore::KURL kurl;
EXPECT_FALSE(kurl.isValid());
EXPECT_TRUE(kurl.isEmpty());
EXPECT_STREQ("", kurl.string().utf8().data());
kurl.setProtocol("http");
EXPECT_FALSE(kurl.isValid());
EXPECT_FALSE(kurl.isEmpty());
EXPECT_STREQ("http:", kurl.string().utf8().data());
kurl.setHost("www.google.com");
EXPECT_TRUE(kurl.isValid());
EXPECT_FALSE(kurl.isEmpty());
EXPECT_STREQ("http://www.google.com/", kurl.string().utf8().data());
kurl.setPort(8000);
EXPECT_TRUE(kurl.isValid());
EXPECT_FALSE(kurl.isEmpty());
EXPECT_STREQ("http://www.google.com:8000/", kurl.string().utf8().data());
kurl.setPath("/favicon.ico");
EXPECT_TRUE(kurl.isValid());
EXPECT_FALSE(kurl.isEmpty());
EXPECT_STREQ("http://www.google.com:8000/favicon.ico", kurl.string().utf8().data());
EXPECT_FALSE(kurl.setProtocol("f/sj#@"));
EXPECT_TRUE(kurl.isValid());
}
TEST(KURLTest, Path)
{
const char initial[] = "http://www.google.com/path/foo";
WebCore::KURL kurl(WebCore::ParsedURLString, initial);
WTF::String nullString;
EXPECT_TRUE(nullString.isNull());
kurl.setPath(nullString);
EXPECT_STREQ("http://www.google.com/", kurl.string().utf8().data());
}
TEST(KURLTest, Query)
{
const char initial[] = "http://www.google.com/search?q=awesome";
WebCore::KURL kurl(WebCore::ParsedURLString, initial);
WTF::String nullString;
EXPECT_TRUE(nullString.isNull());
kurl.setQuery(nullString);
EXPECT_STREQ("http://www.google.com/search", kurl.string().utf8().data());
kurl = WebCore::KURL(WebCore::ParsedURLString, initial);
WTF::String emptyString("");
EXPECT_FALSE(emptyString.isNull());
kurl.setQuery(emptyString);
EXPECT_STREQ("http://www.google.com/search?", kurl.string().utf8().data());
const char question[] = "?foo=bar";
kurl.setQuery(question);
EXPECT_STREQ("http://www.google.com/search?foo=bar",
kurl.string().utf8().data());
const char query[] = "foo=bar";
kurl.setQuery(query);
EXPECT_STREQ("http://www.google.com/search?foo=bar",
kurl.string().utf8().data());
}
TEST(KURLTest, Ref)
{
WebCore::KURL kurl(WebCore::ParsedURLString, "http://foo/bar#baz");
WebCore::KURL cur(WebCore::ParsedURLString, "http://foo/bar");
cur.setFragmentIdentifier("asdf");
EXPECT_STREQ("http://foo/bar#asdf", cur.string().utf8().data());
cur = kurl;
cur.setFragmentIdentifier("asdf");
EXPECT_STREQ("http://foo/bar#asdf", cur.string().utf8().data());
cur = WebCore::KURL(WebCore::ParsedURLString, "http://foo/bar");
cur.setFragmentIdentifier("");
EXPECT_STREQ("http://foo/bar#", cur.string().utf8().data());
cur = kurl;
cur.setFragmentIdentifier("");
EXPECT_STREQ("http://foo/bar#", cur.string().utf8().data());
cur = WebCore::KURL(WebCore::ParsedURLString, "http://foo/bar");
cur.setFragmentIdentifier(WTF::String());
EXPECT_STREQ("http://foo/bar", cur.string().utf8().data());
cur = kurl;
cur.setFragmentIdentifier(WTF::String());
EXPECT_STREQ("http://foo/bar", cur.string().utf8().data());
}
TEST(KURLTest, Empty)
{
WebCore::KURL kurl;
EXPECT_TRUE(kurl.isEmpty());
EXPECT_FALSE(kurl.isValid());
EXPECT_TRUE(kurl.isNull());
EXPECT_TRUE(kurl.string().isNull());
EXPECT_TRUE(kurl.string().isEmpty());
WebCore::KURL kurl2(kurl, "");
EXPECT_TRUE(kurl2.isNull());
EXPECT_TRUE(kurl2.isEmpty());
EXPECT_FALSE(kurl2.isValid());
EXPECT_TRUE(kurl2.string().isNull());
EXPECT_TRUE(kurl2.string().isEmpty());
EXPECT_TRUE(kurl2.string().isNull());
EXPECT_TRUE(kurl2.string().isEmpty());
WebCore::KURL kurl22(kurl, WTF::String());
EXPECT_TRUE(kurl22.isNull());
EXPECT_TRUE(kurl22.isEmpty());
EXPECT_FALSE(kurl22.isValid());
EXPECT_TRUE(kurl22.string().isNull());
EXPECT_TRUE(kurl22.string().isEmpty());
EXPECT_TRUE(kurl22.string().isNull());
EXPECT_TRUE(kurl22.string().isEmpty());
WebCore::KURL kurl3(WebCore::KURL(WebCore::ParsedURLString, "data:foo"),
"something.gif");
EXPECT_TRUE(kurl3.isEmpty());
EXPECT_FALSE(kurl3.isValid());
WebCore::KURL kurl4(WebCore::ParsedURLString, kurl.string());
EXPECT_TRUE(kurl4.isEmpty());
EXPECT_FALSE(kurl4.isValid());
EXPECT_TRUE(kurl4.string().isNull());
EXPECT_TRUE(kurl4.string().isEmpty());
WebCore::KURL kurl5(WebCore::KURL(), "foo.js");
EXPECT_FALSE(kurl5.isValid());
EXPECT_TRUE(kurl5.string().isNull());
WebCore::KURL kurl6(WebCore::ParsedURLString, "");
EXPECT_TRUE(kurl6.isEmpty());
EXPECT_FALSE(kurl6.isValid());
EXPECT_TRUE(kurl6.string().isNull());
EXPECT_TRUE(kurl6.string().isEmpty());
WebCore::KURL kurl7(WebCore::ParsedURLString, "foo.js");
EXPECT_FALSE(kurl7.isValid());
EXPECT_TRUE(kurl7.string().isNull());
}
TEST(KURLTest, UserPass)
{
const char* src = "http://user:pass@google.com/";
WebCore::KURL kurl(WebCore::ParsedURLString, src);
kurl.setUser("");
EXPECT_EQ("http://:pass@google.com/", kurl.string());
kurl = WebCore::KURL(WebCore::ParsedURLString, src);
kurl.setPass("");
EXPECT_EQ("http://user@google.com/", kurl.string());
kurl.setUser("");
EXPECT_EQ("http://google.com/", kurl.string());
}
TEST(KURLTest, Offsets)
{
const char* src1 = "http://user:pass@google.com/foo/bar.html?baz=query#ref";
WebCore::KURL kurl1(WebCore::ParsedURLString, src1);
EXPECT_EQ(17u, kurl1.hostStart());
EXPECT_EQ(27u, kurl1.hostEnd());
EXPECT_EQ(27u, kurl1.pathStart());
EXPECT_EQ(40u, kurl1.pathEnd());
EXPECT_EQ(32u, kurl1.pathAfterLastSlash());
const char* src2 = "http://google.com/foo/";
WebCore::KURL kurl2(WebCore::ParsedURLString, src2);
EXPECT_EQ(7u, kurl2.hostStart());
EXPECT_EQ(17u, kurl2.hostEnd());
EXPECT_EQ(17u, kurl2.pathStart());
EXPECT_EQ(22u, kurl2.pathEnd());
EXPECT_EQ(22u, kurl2.pathAfterLastSlash());
const char* src3 = "javascript:foobar";
WebCore::KURL kurl3(WebCore::ParsedURLString, src3);
EXPECT_EQ(11u, kurl3.hostStart());
EXPECT_EQ(11u, kurl3.hostEnd());
EXPECT_EQ(11u, kurl3.pathStart());
EXPECT_EQ(17u, kurl3.pathEnd());
EXPECT_EQ(11u, kurl3.pathAfterLastSlash());
}
TEST(KURLTest, DeepCopy)
{
const char url[] = "http://www.google.com/";
WebCore::KURL src(WebCore::ParsedURLString, url);
EXPECT_TRUE(src.string() == url);
WebCore::KURL dest = src.copy();
EXPECT_TRUE(dest.string() == url);
EXPECT_NE(dest.string().impl(), src.string().impl());
}
TEST(KURLTest, LastPathComponent)
{
WebCore::KURL url1(WebCore::ParsedURLString, "http://host/path/to/file.txt");
EXPECT_EQ("file.txt", url1.lastPathComponent());
WebCore::KURL invalidUTF8(WebCore::ParsedURLString, "http://a@9%aa%:/path/to/file.txt");
EXPECT_EQ(String(), invalidUTF8.lastPathComponent());
}
TEST(KURLTest, IsHierarchical)
{
WebCore::KURL url1(WebCore::ParsedURLString, "http://host/path/to/file.txt");
EXPECT_TRUE(url1.isHierarchical());
WebCore::KURL invalidUTF8(WebCore::ParsedURLString, "http://a@9%aa%:/path/to/file.txt");
EXPECT_FALSE(invalidUTF8.isHierarchical());
}
TEST(KURLTest, PathAfterLastSlash)
{
WebCore::KURL url1(WebCore::ParsedURLString, "http://host/path/to/file.txt");
EXPECT_EQ(20u, url1.pathAfterLastSlash());
WebCore::KURL invalidUTF8(WebCore::ParsedURLString, "http://a@9%aa%:/path/to/file.txt");
EXPECT_EQ(0u, invalidUTF8.pathAfterLastSlash());
}
TEST(KURLTest, ProtocolIsInHTTPFamily)
{
WebCore::KURL url1(WebCore::ParsedURLString, "http://host/path/to/file.txt");
EXPECT_TRUE(url1.protocolIsInHTTPFamily());
WebCore::KURL invalidUTF8(WebCore::ParsedURLString, "http://a@9%aa%:/path/to/file.txt");
EXPECT_FALSE(invalidUTF8.protocolIsInHTTPFamily());
}
TEST(KURLTest, ProtocolIs)
{
WebCore::KURL url1(WebCore::ParsedURLString, "foo://bar");
EXPECT_TRUE(url1.protocolIs("foo"));
EXPECT_FALSE(url1.protocolIs("foo-bar"));
WebCore::KURL url2(WebCore::ParsedURLString, "foo-bar:");
EXPECT_TRUE(url2.protocolIs("foo-bar"));
EXPECT_FALSE(url2.protocolIs("foo"));
WebCore::KURL invalidUTF8(WebCore::ParsedURLString, "http://a@9%aa%:");
EXPECT_FALSE(invalidUTF8.protocolIs("http"));
EXPECT_TRUE(invalidUTF8.protocolIs(""));
}
}