• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 // Basic tests that verify our KURL's interface behaves the same as the
32 // original KURL's.
33 
34 #include "config.h"
35 
36 #include <gtest/gtest.h>
37 
38 #include "KURL.h"
39 
40 namespace WTF {
41 // Output stream operator so gTest's macros work with WebCore strings.
operator <<(std::ostream & out,const String & str)42 std::ostream& operator<<(std::ostream& out, const String& str)
43 {
44     return str.isEmpty() ? out : out << str.utf8().data();
45 }
46 } // namespace WTF
47 
48 namespace {
49 
50 
51 struct ComponentCase {
52     const char* url;
53     const char* protocol;
54     const char* host;
55     const int port;
56     const char* user;
57     const char* pass;
58     const char* path;
59     const char* lastPath;
60     const char* query;
61     const char* ref;
62 };
63 
64 // Test the cases where we should be the same as WebKit's old KURL.
TEST(KURLTest,SameGetters)65 TEST(KURLTest, SameGetters)
66 {
67     struct GetterCase {
68         const char* url;
69         const char* protocol;
70         const char* host;
71         int port;
72         const char* user;
73         const char* pass;
74         const char* lastPathComponent;
75         const char* query;
76         const char* ref;
77         bool hasRef;
78     } cases[] = {
79         {"http://www.google.com/foo/blah?bar=baz#ref", "http", "www.google.com", 0, "", 0, "blah", "bar=baz", "ref", true},
80         {"http://foo.com:1234/foo/bar/", "http", "foo.com", 1234, "", 0, "bar", 0, 0, false},
81         {"http://www.google.com?#", "http", "www.google.com", 0, "", 0, 0, "", "", true},
82         {"https://me:pass@google.com:23#foo", "https", "google.com", 23, "me", "pass", 0, 0, "foo", true},
83         {"javascript:hello!//world", "javascript", "", 0, "", 0, "world", 0, 0, false},
84     };
85 
86     for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
87         // UTF-8
88         WebCore::KURL kurl(WebCore::ParsedURLString, cases[i].url);
89 
90         EXPECT_EQ(cases[i].protocol, kurl.protocol());
91         EXPECT_EQ(cases[i].host, kurl.host());
92         EXPECT_EQ(cases[i].port, kurl.port());
93         EXPECT_EQ(cases[i].user, kurl.user());
94         EXPECT_EQ(cases[i].pass, kurl.pass());
95         EXPECT_EQ(cases[i].lastPathComponent, kurl.lastPathComponent());
96         EXPECT_EQ(cases[i].query, kurl.query());
97         EXPECT_EQ(cases[i].ref, kurl.fragmentIdentifier());
98         EXPECT_EQ(cases[i].hasRef, kurl.hasFragmentIdentifier());
99 
100         // UTF-16
101         WTF::String utf16(cases[i].url);
102         kurl = WebCore::KURL(WebCore::ParsedURLString, utf16);
103 
104         EXPECT_EQ(cases[i].protocol, kurl.protocol());
105         EXPECT_EQ(cases[i].host, kurl.host());
106         EXPECT_EQ(cases[i].port, kurl.port());
107         EXPECT_EQ(cases[i].user, kurl.user());
108         EXPECT_EQ(cases[i].pass, kurl.pass());
109         EXPECT_EQ(cases[i].lastPathComponent, kurl.lastPathComponent());
110         EXPECT_EQ(cases[i].query, kurl.query());
111         EXPECT_EQ(cases[i].ref, kurl.fragmentIdentifier());
112         EXPECT_EQ(cases[i].hasRef, kurl.hasFragmentIdentifier());
113     }
114 }
115 
116 // Test a few cases where we're different just to make sure we give reasonable
117 // output.
TEST(KURLTest,DifferentGetters)118 TEST(KURLTest, DifferentGetters)
119 {
120     ComponentCase cases[] = {
121         // url                                    protocol      host        port  user  pass    path                lastPath  query      ref
122 
123         // Old WebKit allows references and queries in what we call "path" URLs
124         // like javascript, so the path here will only consist of "hello!".
125         {"javascript:hello!?#/\\world",           "javascript", "",         0,    "",   0,      "hello!?#/\\world", "world",  0,         0},
126 
127         // Old WebKit doesn't handle "parameters" in paths, so will
128         // disagree with us about where the path is for this URL.
129         {"http://a.com/hello;world",              "http",       "a.com",    0,    "",   0,      "/hello;world",     "hello",  0,         0},
130 
131         // WebKit doesn't like UTF-8 or UTF-16 input.
132         {"http://\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd/", "http", "xn--6qqa088eba", 0, "", 0, "/",       0,        0,         0},
133 
134         // WebKit %-escapes non-ASCII characters in reference, but we don't.
135         {"http://www.google.com/foo/blah?bar=baz#\xce\xb1\xce\xb2", "http", "www.google.com", 0, "", 0, "/foo/blah/", "blah", "bar=baz", "\xce\xb1\xce\xb2"},
136     };
137 
138     for (size_t i = 0; i < arraysize(cases); i++) {
139         WebCore::KURL kurl(WebCore::ParsedURLString, cases[i].url);
140 
141         EXPECT_EQ(cases[i].protocol, kurl.protocol());
142         EXPECT_EQ(cases[i].host, kurl.host());
143         EXPECT_EQ(cases[i].port, kurl.port());
144         EXPECT_EQ(cases[i].user, kurl.user());
145         EXPECT_EQ(cases[i].pass, kurl.pass());
146         EXPECT_EQ(cases[i].lastPath, kurl.lastPathComponent());
147         EXPECT_EQ(cases[i].query, kurl.query());
148         // Want to compare UCS-16 refs (or to null).
149         if (cases[i].ref)
150             EXPECT_EQ(WTF::String::fromUTF8(cases[i].ref), kurl.fragmentIdentifier());
151         else
152             EXPECT_TRUE(kurl.fragmentIdentifier().isNull());
153     }
154 }
155 
156 // Ensures that both ASCII and UTF-8 canonical URLs are handled properly and we
157 // get the correct string object out.
TEST(KURLTest,UTF8)158 TEST(KURLTest, UTF8)
159 {
160     const char asciiURL[] = "http://foo/bar#baz";
161     WebCore::KURL asciiKURL(WebCore::ParsedURLString, asciiURL);
162     EXPECT_TRUE(asciiKURL.string() == WTF::String(asciiURL));
163 
164     // When the result is ASCII, we should get an ASCII String. Some
165     // code depends on being able to compare the result of the .string()
166     // getter with another String, and the isASCIIness of the two
167     // strings must match for these functions (like equalIgnoringCase).
168     EXPECT_TRUE(WTF::equalIgnoringCase(asciiKURL, WTF::String(asciiURL)));
169 
170     // Reproduce code path in FrameLoader.cpp -- equalIgnoringCase implicitly
171     // expects gkurl.protocol() to have been created as ascii.
172     WebCore::KURL mailto(WebCore::ParsedURLString, "mailto:foo@foo.com");
173     EXPECT_TRUE(WTF::equalIgnoringCase(mailto.protocol(), "mailto"));
174 
175     const char utf8URL[] = "http://foo/bar#\xe4\xbd\xa0\xe5\xa5\xbd";
176     WebCore::KURL utf8KURL(WebCore::ParsedURLString, utf8URL);
177 
178     EXPECT_TRUE(utf8KURL.string() == WTF::String::fromUTF8(utf8URL));
179 }
180 
TEST(KURLTest,Setters)181 TEST(KURLTest, Setters)
182 {
183     // Replace the starting URL with the given components one at a time and
184     // verify that we're always the same as the old KURL.
185     //
186     // Note that old KURL won't canonicalize the default port away, so we
187     // can't set setting the http port to "80" (or even "0").
188     //
189     // We also can't test clearing the query.
190     //
191     // The format is every other row is a test, and the row that follows it is the
192     // expected result.
193     struct ExpectedComponentCase {
194         const char* url;
195         const char* protocol;
196         const char* host;
197         const int port;
198         const char* user;
199         const char* pass;
200         const char* path;
201         const char* query;
202         const char* ref;
203 
204         // The full expected URL with the given "set" applied.
205         const char* expectedProtocol;
206         const char* expectedHost;
207         const char* expectedPort;
208         const char* expectedUser;
209         const char* expectedPass;
210         const char* expectedPath;
211         const char* expectedQuery;
212         const char* expectedRef;
213     } cases[] = {
214          // url                                   protocol      host               port  user  pass    path            query      ref
215         {"http://www.google.com/",                "https",      "news.google.com", 8888, "me", "pass", "/foo",         "?q=asdf", "heehee",
216                                                   "https://www.google.com/",
217                                                                 "https://news.google.com/",
218                                                                                    "https://news.google.com:8888/",
219                                                                                          "https://me@news.google.com:8888/",
220                                                                                                "https://me:pass@news.google.com:8888/",
221                                                                                                        "https://me:pass@news.google.com:8888/foo",
222                                                                                                                        "https://me:pass@news.google.com:8888/foo?q=asdf",
223                                                                                                                                   "https://me:pass@news.google.com:8888/foo?q=asdf#heehee"},
224 
225         {"https://me:pass@google.com:88/a?f#b",   "http",       "goo.com",         92,   "",   "",     "/",            0,      "",
226                                                   "http://me:pass@google.com:88/a?f#b",
227                                                                 "http://me:pass@goo.com:88/a?f#b",
228                                                                                    "http://me:pass@goo.com:92/a?f#b",
229                                                                                          "http://:pass@goo.com:92/a?f#b",
230                                                                                                "http://goo.com:92/a?f#b",
231                                                                                                         "http://goo.com:92/?f#b",
232                                                                                                                        "http://goo.com:92/#b",
233                                                                                                                                   "https://goo.com:92/"},
234     };
235 
236     for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); i++) {
237         WebCore::KURL kurl(WebCore::ParsedURLString, cases[i].url);
238 
239         kurl.setProtocol(cases[i].protocol);
240         EXPECT_STREQ(cases[i].expectedProtocol, kurl.string().utf8().data());
241 
242         kurl.setHost(cases[i].host);
243         EXPECT_STREQ(cases[i].expectedHost, kurl.string().utf8().data());
244 
245         kurl.setPort(cases[i].port);
246         EXPECT_STREQ(cases[i].expectedPort, kurl.string().utf8().data());
247 
248         kurl.setUser(cases[i].user);
249         EXPECT_STREQ(cases[i].expectedUser, kurl.string().utf8().data());
250 
251         kurl.setPass(cases[i].pass);
252         EXPECT_STREQ(cases[i].expectedPass, kurl.string().utf8().data());
253 
254         kurl.setPath(cases[i].path);
255         EXPECT_STREQ(cases[i].expectedPath, kurl.string().utf8().data());
256 
257         kurl.setQuery(cases[i].query);
258         EXPECT_STREQ(cases[i].expectedQuery, kurl.string().utf8().data());
259 
260         // Refs are tested below. On the Safari 3.1 branch, we don't match their
261         // KURL since we integrated a fix from their trunk.
262     }
263 }
264 
265 // Tests that KURL::decodeURLEscapeSequences works as expected
266 #if USE(GOOGLEURL)
TEST(KURLTest,Decode)267 TEST(KURLTest, Decode)
268 {
269     struct DecodeCase {
270         const char* input;
271         const char* output;
272     } decodeCases[] = {
273         {"hello, world", "hello, world"},
274         {"%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/"},
275         {"%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/"},
276         {"%20%21%22%23%24%25%26%27%28%29%2a%2B%2C%2D%2e%2f/", " !\"#$%&'()*+,-.//"},
277         {"%30%31%32%33%34%35%36%37%38%39%3a%3B%3C%3D%3e%3f/", "0123456789:;<=>?/"},
278         {"%40%41%42%43%44%45%46%47%48%49%4a%4B%4C%4D%4e%4f/", "@ABCDEFGHIJKLMNO/"},
279         {"%50%51%52%53%54%55%56%57%58%59%5a%5B%5C%5D%5e%5f/", "PQRSTUVWXYZ[\\]^_/"},
280         {"%60%61%62%63%64%65%66%67%68%69%6a%6B%6C%6D%6e%6f/", "`abcdefghijklmno/"},
281         {"%70%71%72%73%74%75%76%77%78%79%7a%7B%7C%7D%7e%7f/", "pqrstuvwxyz{|}~\x7f/"},
282           // Test un-UTF-8-ization.
283         {"%e4%bd%a0%e5%a5%bd", "\xe4\xbd\xa0\xe5\xa5\xbd"},
284     };
285 
286     for (size_t i = 0; i < ARRAYSIZE_UNSAFE(decodeCases); i++) {
287         WTF::String input(decodeCases[i].input);
288         WTF::String str = WebCore::decodeURLEscapeSequences(input);
289         EXPECT_STREQ(decodeCases[i].output, str.utf8().data());
290     }
291 
292     // Our decode should decode %00
293     WTF::String zero = WebCore::decodeURLEscapeSequences("%00");
294     EXPECT_STRNE("%00", zero.utf8().data());
295 
296     // Test the error behavior for invalid UTF-8 (we differ from WebKit here).
297     WTF::String invalid = WebCore::decodeURLEscapeSequences(
298         "%e4%a0%e5%a5%bd");
299     char16 invalidExpectedHelper[4] = { 0x00e4, 0x00a0, 0x597d, 0 };
300     WTF::String invalidExpected(
301         reinterpret_cast<const ::UChar*>(invalidExpectedHelper),
302         3);
303     EXPECT_EQ(invalidExpected, invalid);
304 }
305 #endif
306 
TEST(KURLTest,Encode)307 TEST(KURLTest, Encode)
308 {
309     // Also test that it gets converted to UTF-8 properly.
310     char16 wideInputHelper[3] = { 0x4f60, 0x597d, 0 };
311     WTF::String wideInput(
312         reinterpret_cast<const ::UChar*>(wideInputHelper), 2);
313     WTF::String wideReference("\xe4\xbd\xa0\xe5\xa5\xbd", 6);
314     WTF::String wideOutput =
315         WebCore::encodeWithURLEscapeSequences(wideInput);
316     EXPECT_EQ(wideReference, wideOutput);
317 
318     // Our encode only escapes NULLs for safety (see the implementation for
319     // more), so we only bother to test a few cases.
320     WTF::String input(
321         "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 16);
322     WTF::String reference(
323         "%00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 18);
324     WTF::String output = WebCore::encodeWithURLEscapeSequences(input);
325     EXPECT_EQ(reference, output);
326 }
327 
TEST(KURLTest,ResolveEmpty)328 TEST(KURLTest, ResolveEmpty)
329 {
330     WebCore::KURL emptyBase;
331 
332     // WebKit likes to be able to resolve absolute input agains empty base URLs,
333     // which would normally be invalid since the base URL is invalid.
334     const char abs[] = "http://www.google.com/";
335     WebCore::KURL resolveAbs(emptyBase, abs);
336     EXPECT_TRUE(resolveAbs.isValid());
337     EXPECT_STREQ(abs, resolveAbs.string().utf8().data());
338 
339     // Resolving a non-relative URL agains the empty one should still error.
340     const char rel[] = "foo.html";
341     WebCore::KURL resolveErr(emptyBase, rel);
342     EXPECT_FALSE(resolveErr.isValid());
343 }
344 
345 // WebKit will make empty URLs and set components on them. kurl doesn't allow
346 // replacements on invalid URLs, but here we do.
TEST(KURLTest,ReplaceInvalid)347 TEST(KURLTest, ReplaceInvalid)
348 {
349     WebCore::KURL kurl;
350 
351     EXPECT_FALSE(kurl.isValid());
352     EXPECT_TRUE(kurl.isEmpty());
353     EXPECT_STREQ("", kurl.string().utf8().data());
354 
355     kurl.setProtocol("http");
356     // GKURL will say that a URL with just a scheme is invalid, KURL will not.
357 #if USE(GOOGLEURL)
358     EXPECT_FALSE(kurl.isValid());
359 #else
360     EXPECT_TRUE(kurl.isValid());
361 #endif
362     EXPECT_FALSE(kurl.isEmpty());
363     // At this point, we do things slightly differently if there is only a scheme.
364     // We check the results here to make it more obvious what is going on, but it
365     // shouldn't be a big deal if these change.
366 #if USE(GOOGLEURL)
367     EXPECT_STREQ("http:", kurl.string().utf8().data());
368 #else
369     EXPECT_STREQ("http:/", kurl.string().utf8().data());
370 #endif
371 
372     kurl.setHost("www.google.com");
373     EXPECT_TRUE(kurl.isValid());
374     EXPECT_FALSE(kurl.isEmpty());
375     EXPECT_STREQ("http://www.google.com/", kurl.string().utf8().data());
376 
377     kurl.setPort(8000);
378     EXPECT_TRUE(kurl.isValid());
379     EXPECT_FALSE(kurl.isEmpty());
380     EXPECT_STREQ("http://www.google.com:8000/", kurl.string().utf8().data());
381 
382     kurl.setPath("/favicon.ico");
383     EXPECT_TRUE(kurl.isValid());
384     EXPECT_FALSE(kurl.isEmpty());
385     EXPECT_STREQ("http://www.google.com:8000/favicon.ico", kurl.string().utf8().data());
386 
387     // Now let's test that giving an invalid replacement fails. Invalid
388     // protocols fail without modifying the URL, which should remain valid.
389 #if USE(GOOGLEURL)
390     EXPECT_FALSE(kurl.setProtocol("f/sj#@"));
391     EXPECT_TRUE(kurl.isValid());
392 #endif
393 }
394 
TEST(KURLTest,Path)395 TEST(KURLTest, Path)
396 {
397     const char initial[] = "http://www.google.com/path/foo";
398     WebCore::KURL kurl(WebCore::ParsedURLString, initial);
399 
400     // Clear by setting a null string.
401     WTF::String nullString;
402     EXPECT_TRUE(nullString.isNull());
403     kurl.setPath(nullString);
404     EXPECT_STREQ("http://www.google.com/", kurl.string().utf8().data());
405 }
406 
407 // Test that setting the query to different things works. Thq query is handled
408 // a littler differently than some of the other components.
TEST(KURLTest,Query)409 TEST(KURLTest, Query)
410 {
411     const char initial[] = "http://www.google.com/search?q=awesome";
412     WebCore::KURL kurl(WebCore::ParsedURLString, initial);
413 
414     // Clear by setting a null string.
415     WTF::String nullString;
416     EXPECT_TRUE(nullString.isNull());
417     kurl.setQuery(nullString);
418     EXPECT_STREQ("http://www.google.com/search", kurl.string().utf8().data());
419 
420     // Clear by setting an empty string.
421     kurl = WebCore::KURL(WebCore::ParsedURLString, initial);
422     WTF::String emptyString("");
423     EXPECT_FALSE(emptyString.isNull());
424     kurl.setQuery(emptyString);
425     EXPECT_STREQ("http://www.google.com/search?", kurl.string().utf8().data());
426 
427     // Set with something that begins in a question mark.
428     const char question[] = "?foo=bar";
429     kurl.setQuery(question);
430     EXPECT_STREQ("http://www.google.com/search?foo=bar",
431                  kurl.string().utf8().data());
432 
433     // Set with something that doesn't begin in a question mark.
434     const char query[] = "foo=bar";
435     kurl.setQuery(query);
436     EXPECT_STREQ("http://www.google.com/search?foo=bar",
437                  kurl.string().utf8().data());
438 }
439 
TEST(KURLTest,Ref)440 TEST(KURLTest, Ref)
441 {
442     WebCore::KURL kurl(WebCore::ParsedURLString, "http://foo/bar#baz");
443 
444     // Basic ref setting.
445     WebCore::KURL cur(WebCore::ParsedURLString, "http://foo/bar");
446     cur.setFragmentIdentifier("asdf");
447     EXPECT_STREQ("http://foo/bar#asdf", cur.string().utf8().data());
448     cur = kurl;
449     cur.setFragmentIdentifier("asdf");
450     EXPECT_STREQ("http://foo/bar#asdf", cur.string().utf8().data());
451 
452     // Setting a ref to the empty string will set it to "#".
453     cur = WebCore::KURL(WebCore::ParsedURLString, "http://foo/bar");
454     cur.setFragmentIdentifier("");
455     EXPECT_STREQ("http://foo/bar#", cur.string().utf8().data());
456     cur = kurl;
457     cur.setFragmentIdentifier("");
458     EXPECT_STREQ("http://foo/bar#", cur.string().utf8().data());
459 
460     // Setting the ref to the null string will clear it altogether.
461     cur = WebCore::KURL(WebCore::ParsedURLString, "http://foo/bar");
462     cur.setFragmentIdentifier(WTF::String());
463     EXPECT_STREQ("http://foo/bar", cur.string().utf8().data());
464     cur = kurl;
465     cur.setFragmentIdentifier(WTF::String());
466     EXPECT_STREQ("http://foo/bar", cur.string().utf8().data());
467 }
468 
TEST(KURLTest,Empty)469 TEST(KURLTest, Empty)
470 {
471     WebCore::KURL kurl;
472 
473     // First test that regular empty URLs are the same.
474     EXPECT_TRUE(kurl.isEmpty());
475     EXPECT_FALSE(kurl.isValid());
476     EXPECT_TRUE(kurl.isNull());
477     EXPECT_TRUE(kurl.string().isNull());
478     EXPECT_TRUE(kurl.string().isEmpty());
479 
480     // Test resolving a null URL on an empty string.
481     WebCore::KURL kurl2(kurl, "");
482     EXPECT_FALSE(kurl2.isNull());
483     EXPECT_TRUE(kurl2.isEmpty());
484     EXPECT_FALSE(kurl2.isValid());
485     EXPECT_FALSE(kurl2.string().isNull());
486     EXPECT_TRUE(kurl2.string().isEmpty());
487     EXPECT_FALSE(kurl2.string().isNull());
488     EXPECT_TRUE(kurl2.string().isEmpty());
489 
490     // Resolve the null URL on a null string.
491     WebCore::KURL kurl22(kurl, WTF::String());
492     EXPECT_FALSE(kurl22.isNull());
493     EXPECT_TRUE(kurl22.isEmpty());
494     EXPECT_FALSE(kurl22.isValid());
495     EXPECT_FALSE(kurl22.string().isNull());
496     EXPECT_TRUE(kurl22.string().isEmpty());
497     EXPECT_FALSE(kurl22.string().isNull());
498     EXPECT_TRUE(kurl22.string().isEmpty());
499 
500     // Test non-hierarchical schemes resolving. The actual URLs will be different.
501     // WebKit's one will set the string to "something.gif" and we'll set it to an
502     // empty string. I think either is OK, so we just check our behavior.
503 #if USE(GOOGLEURL)
504     WebCore::KURL kurl3(WebCore::KURL(WebCore::ParsedURLString, "data:foo"),
505                         "something.gif");
506     EXPECT_TRUE(kurl3.isEmpty());
507     EXPECT_FALSE(kurl3.isValid());
508 #endif
509 
510     // Test for weird isNull string input,
511     // see: http://bugs.webkit.org/show_bug.cgi?id=16487
512     WebCore::KURL kurl4(WebCore::ParsedURLString, kurl.string());
513     EXPECT_TRUE(kurl4.isEmpty());
514     EXPECT_FALSE(kurl4.isValid());
515     EXPECT_TRUE(kurl4.string().isNull());
516     EXPECT_TRUE(kurl4.string().isEmpty());
517 
518     // Resolving an empty URL on an invalid string.
519     WebCore::KURL kurl5(WebCore::KURL(), "foo.js");
520     // We'll be empty in this case, but KURL won't be. Should be OK.
521     // EXPECT_EQ(kurl5.isEmpty(), kurl5.isEmpty());
522     // EXPECT_EQ(kurl5.string().isEmpty(), kurl5.string().isEmpty());
523     EXPECT_FALSE(kurl5.isValid());
524     EXPECT_FALSE(kurl5.string().isNull());
525 
526     // Empty string as input
527     WebCore::KURL kurl6(WebCore::ParsedURLString, "");
528     EXPECT_TRUE(kurl6.isEmpty());
529     EXPECT_FALSE(kurl6.isValid());
530     EXPECT_FALSE(kurl6.string().isNull());
531     EXPECT_TRUE(kurl6.string().isEmpty());
532 
533     // Non-empty but invalid C string as input.
534     WebCore::KURL kurl7(WebCore::ParsedURLString, "foo.js");
535     // WebKit will actually say this URL has the string "foo.js" but is invalid.
536     // We don't do that.
537     // EXPECT_EQ(kurl7.isEmpty(), kurl7.isEmpty());
538     EXPECT_FALSE(kurl7.isValid());
539     EXPECT_FALSE(kurl7.string().isNull());
540 }
541 
TEST(KURLTest,UserPass)542 TEST(KURLTest, UserPass)
543 {
544     const char* src = "http://user:pass@google.com/";
545     WebCore::KURL kurl(WebCore::ParsedURLString, src);
546 
547     // Clear just the username.
548     kurl.setUser("");
549     EXPECT_EQ("http://:pass@google.com/", kurl.string());
550 
551     // Clear just the password.
552     kurl = WebCore::KURL(WebCore::ParsedURLString, src);
553     kurl.setPass("");
554     EXPECT_EQ("http://user@google.com/", kurl.string());
555 
556     // Now clear both.
557     kurl.setUser("");
558     EXPECT_EQ("http://google.com/", kurl.string());
559 }
560 
TEST(KURLTest,Offsets)561 TEST(KURLTest, Offsets)
562 {
563     const char* src1 = "http://user:pass@google.com/foo/bar.html?baz=query#ref";
564     WebCore::KURL kurl1(WebCore::ParsedURLString, src1);
565 
566     EXPECT_EQ(17u, kurl1.hostStart());
567     EXPECT_EQ(27u, kurl1.hostEnd());
568     EXPECT_EQ(27u, kurl1.pathStart());
569     EXPECT_EQ(40u, kurl1.pathEnd());
570     EXPECT_EQ(32u, kurl1.pathAfterLastSlash());
571 
572     const char* src2 = "http://google.com/foo/";
573     WebCore::KURL kurl2(WebCore::ParsedURLString, src2);
574 
575     EXPECT_EQ(7u, kurl2.hostStart());
576     EXPECT_EQ(17u, kurl2.hostEnd());
577     EXPECT_EQ(17u, kurl2.pathStart());
578     EXPECT_EQ(22u, kurl2.pathEnd());
579     EXPECT_EQ(22u, kurl2.pathAfterLastSlash());
580 
581     const char* src3 = "javascript:foobar";
582     WebCore::KURL kurl3(WebCore::ParsedURLString, src3);
583 
584     EXPECT_EQ(11u, kurl3.hostStart());
585     EXPECT_EQ(11u, kurl3.hostEnd());
586     EXPECT_EQ(11u, kurl3.pathStart());
587     EXPECT_EQ(17u, kurl3.pathEnd());
588     EXPECT_EQ(11u, kurl3.pathAfterLastSlash());
589 }
590 
TEST(KURLTest,DeepCopy)591 TEST(KURLTest, DeepCopy)
592 {
593     const char url[] = "http://www.google.com/";
594     WebCore::KURL src(WebCore::ParsedURLString, url);
595     EXPECT_TRUE(src.string() == url); // This really just initializes the cache.
596     WebCore::KURL dest = src.copy();
597     EXPECT_TRUE(dest.string() == url); // This really just initializes the cache.
598 
599     // The pointers should be different for both UTF-8 and UTF-16.
600     EXPECT_NE(dest.string().characters(), src.string().characters());
601     EXPECT_NE(dest.utf8String().data(), src.utf8String().data());
602 }
603 
TEST(KURLTest,ProtocolIs)604 TEST(KURLTest, ProtocolIs)
605 {
606     WebCore::KURL url1(WebCore::ParsedURLString, "foo://bar");
607     EXPECT_TRUE(url1.protocolIs("foo"));
608     EXPECT_FALSE(url1.protocolIs("foo-bar"));
609 
610     WebCore::KURL url2(WebCore::ParsedURLString, "foo-bar:");
611     EXPECT_TRUE(url2.protocolIs("foo-bar"));
612     EXPECT_FALSE(url2.protocolIs("foo"));
613 }
614 
615 } // namespace
616