initial import
[vuplus_webkit] / Source / WebKit / chromium / tests / KURLTest.cpp
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.
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.
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.
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.
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
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)
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
307 TEST(KURLTest, Encode)
308 {
309     struct EncodeCase {
310         const char* input;
311         const char* output;
312     } encode_cases[] = {
313         {"hello, world", "hello%2C%20world"},
314         {"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
315           "%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F"},
316         {"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
317           "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F"},
318         {" !\"#$%&'()*+,-./",
319           "%20!%22%23%24%25%26'()*%2B%2C-.%2F"},
320         {"0123456789:;<=>?",
321           "0123456789%3A%3B%3C%3D%3E%3F"},
322         {"@ABCDEFGHIJKLMNO",
323           "%40ABCDEFGHIJKLMNO"},
324         {"PQRSTUVWXYZ[\\]^_",
325           "PQRSTUVWXYZ%5B%5C%5D%5E_"},
326         {"`abcdefghijklmno",
327           "%60abcdefghijklmno"},
328         {"pqrstuvwxyz{|}~\x7f",
329           "pqrstuvwxyz%7B%7C%7D~%7F"},
330     };
331
332     for (size_t i = 0; i < ARRAYSIZE_UNSAFE(encode_cases); i++) {
333         WTF::String input(encode_cases[i].input);
334         WTF::String expectedOutput(encode_cases[i].output);
335         WTF::String output = WebCore::encodeWithURLEscapeSequences(input);
336         EXPECT_EQ(expectedOutput, output);
337     }
338
339     // Our encode escapes NULLs for safety, so we need to check that too.
340     WTF::String input("\x00\x01", 2);
341     WTF::String reference("%00%01");
342
343     WTF::String output = WebCore::encodeWithURLEscapeSequences(input);
344     EXPECT_EQ(reference, output);
345
346     // Also test that it gets converted to UTF-8 properly.
347     char16 wideInputHelper[3] = { 0x4f60, 0x597d, 0 };
348     WTF::String wideInput(
349         reinterpret_cast<const ::UChar*>(wideInputHelper), 2);
350     WTF::String wideReference("%E4%BD%A0%E5%A5%BD");
351     WTF::String wideOutput =
352         WebCore::encodeWithURLEscapeSequences(wideInput);
353     EXPECT_EQ(wideReference, wideOutput);
354 }
355
356 TEST(KURLTest, ResolveEmpty)
357 {
358     WebCore::KURL emptyBase;
359
360     // WebKit likes to be able to resolve absolute input agains empty base URLs,
361     // which would normally be invalid since the base URL is invalid.
362     const char abs[] = "http://www.google.com/";
363     WebCore::KURL resolveAbs(emptyBase, abs);
364     EXPECT_TRUE(resolveAbs.isValid());
365     EXPECT_STREQ(abs, resolveAbs.string().utf8().data());
366
367     // Resolving a non-relative URL agains the empty one should still error.
368     const char rel[] = "foo.html";
369     WebCore::KURL resolveErr(emptyBase, rel);
370     EXPECT_FALSE(resolveErr.isValid());
371 }
372
373 // WebKit will make empty URLs and set components on them. kurl doesn't allow
374 // replacements on invalid URLs, but here we do.
375 TEST(KURLTest, ReplaceInvalid)
376 {
377     WebCore::KURL kurl;
378
379     EXPECT_FALSE(kurl.isValid());
380     EXPECT_TRUE(kurl.isEmpty());
381     EXPECT_STREQ("", kurl.string().utf8().data());
382
383     kurl.setProtocol("http");
384     // GKURL will say that a URL with just a scheme is invalid, KURL will not.
385 #if USE(GOOGLEURL)
386     EXPECT_FALSE(kurl.isValid());
387 #else
388     EXPECT_TRUE(kurl.isValid());
389 #endif
390     EXPECT_FALSE(kurl.isEmpty());
391     // At this point, we do things slightly differently if there is only a scheme.
392     // We check the results here to make it more obvious what is going on, but it
393     // shouldn't be a big deal if these change.
394 #if USE(GOOGLEURL)
395     EXPECT_STREQ("http:", kurl.string().utf8().data());
396 #else
397     EXPECT_STREQ("http:/", kurl.string().utf8().data());
398 #endif
399
400     kurl.setHost("www.google.com");
401     EXPECT_TRUE(kurl.isValid());
402     EXPECT_FALSE(kurl.isEmpty());
403     EXPECT_STREQ("http://www.google.com/", kurl.string().utf8().data());
404
405     kurl.setPort(8000);
406     EXPECT_TRUE(kurl.isValid());
407     EXPECT_FALSE(kurl.isEmpty());
408     EXPECT_STREQ("http://www.google.com:8000/", kurl.string().utf8().data());
409
410     kurl.setPath("/favicon.ico");
411     EXPECT_TRUE(kurl.isValid());
412     EXPECT_FALSE(kurl.isEmpty());
413     EXPECT_STREQ("http://www.google.com:8000/favicon.ico", kurl.string().utf8().data());
414
415     // Now let's test that giving an invalid replacement fails. Invalid
416     // protocols fail without modifying the URL, which should remain valid.
417 #if USE(GOOGLEURL)
418     EXPECT_FALSE(kurl.setProtocol("f/sj#@"));
419     EXPECT_TRUE(kurl.isValid());
420 #endif
421 }
422
423 TEST(KURLTest, Path)
424 {
425     const char initial[] = "http://www.google.com/path/foo";
426     WebCore::KURL kurl(WebCore::ParsedURLString, initial);
427
428     // Clear by setting a null string.
429     WTF::String nullString;
430     EXPECT_TRUE(nullString.isNull());
431     kurl.setPath(nullString);
432     EXPECT_STREQ("http://www.google.com/", kurl.string().utf8().data());
433 }
434
435 // Test that setting the query to different things works. Thq query is handled
436 // a littler differently than some of the other components.
437 TEST(KURLTest, Query)
438 {
439     const char initial[] = "http://www.google.com/search?q=awesome";
440     WebCore::KURL kurl(WebCore::ParsedURLString, initial);
441
442     // Clear by setting a null string.
443     WTF::String nullString;
444     EXPECT_TRUE(nullString.isNull());
445     kurl.setQuery(nullString);
446     EXPECT_STREQ("http://www.google.com/search", kurl.string().utf8().data());
447
448     // Clear by setting an empty string.
449     kurl = WebCore::KURL(WebCore::ParsedURLString, initial);
450     WTF::String emptyString("");
451     EXPECT_FALSE(emptyString.isNull());
452     kurl.setQuery(emptyString);
453     EXPECT_STREQ("http://www.google.com/search?", kurl.string().utf8().data());
454
455     // Set with something that begins in a question mark.
456     const char question[] = "?foo=bar";
457     kurl.setQuery(question);
458     EXPECT_STREQ("http://www.google.com/search?foo=bar",
459                  kurl.string().utf8().data());
460
461     // Set with something that doesn't begin in a question mark.
462     const char query[] = "foo=bar";
463     kurl.setQuery(query);
464     EXPECT_STREQ("http://www.google.com/search?foo=bar",
465                  kurl.string().utf8().data());
466 }
467
468 TEST(KURLTest, Ref)
469 {
470     WebCore::KURL kurl(WebCore::ParsedURLString, "http://foo/bar#baz");
471
472     // Basic ref setting.
473     WebCore::KURL cur(WebCore::ParsedURLString, "http://foo/bar");
474     cur.setFragmentIdentifier("asdf");
475     EXPECT_STREQ("http://foo/bar#asdf", cur.string().utf8().data());
476     cur = kurl;
477     cur.setFragmentIdentifier("asdf");
478     EXPECT_STREQ("http://foo/bar#asdf", cur.string().utf8().data());
479
480     // Setting a ref to the empty string will set it to "#".
481     cur = WebCore::KURL(WebCore::ParsedURLString, "http://foo/bar");
482     cur.setFragmentIdentifier("");
483     EXPECT_STREQ("http://foo/bar#", cur.string().utf8().data());
484     cur = kurl;
485     cur.setFragmentIdentifier("");
486     EXPECT_STREQ("http://foo/bar#", cur.string().utf8().data());
487
488     // Setting the ref to the null string will clear it altogether.
489     cur = WebCore::KURL(WebCore::ParsedURLString, "http://foo/bar");
490     cur.setFragmentIdentifier(WTF::String());
491     EXPECT_STREQ("http://foo/bar", cur.string().utf8().data());
492     cur = kurl;
493     cur.setFragmentIdentifier(WTF::String());
494     EXPECT_STREQ("http://foo/bar", cur.string().utf8().data());
495 }
496
497 TEST(KURLTest, Empty)
498 {
499     WebCore::KURL kurl;
500
501     // First test that regular empty URLs are the same.
502     EXPECT_TRUE(kurl.isEmpty());
503     EXPECT_FALSE(kurl.isValid());
504     EXPECT_TRUE(kurl.isNull());
505     EXPECT_TRUE(kurl.string().isNull());
506     EXPECT_TRUE(kurl.string().isEmpty());
507
508     // Test resolving a null URL on an empty string.
509     WebCore::KURL kurl2(kurl, "");
510     EXPECT_FALSE(kurl2.isNull());
511     EXPECT_TRUE(kurl2.isEmpty());
512     EXPECT_FALSE(kurl2.isValid());
513     EXPECT_FALSE(kurl2.string().isNull());
514     EXPECT_TRUE(kurl2.string().isEmpty());
515     EXPECT_FALSE(kurl2.string().isNull());
516     EXPECT_TRUE(kurl2.string().isEmpty());
517
518     // Resolve the null URL on a null string.
519     WebCore::KURL kurl22(kurl, WTF::String());
520     EXPECT_FALSE(kurl22.isNull());
521     EXPECT_TRUE(kurl22.isEmpty());
522     EXPECT_FALSE(kurl22.isValid());
523     EXPECT_FALSE(kurl22.string().isNull());
524     EXPECT_TRUE(kurl22.string().isEmpty());
525     EXPECT_FALSE(kurl22.string().isNull());
526     EXPECT_TRUE(kurl22.string().isEmpty());
527
528     // Test non-hierarchical schemes resolving. The actual URLs will be different.
529     // WebKit's one will set the string to "something.gif" and we'll set it to an
530     // empty string. I think either is OK, so we just check our behavior.
531 #if USE(GOOGLEURL)
532     WebCore::KURL kurl3(WebCore::KURL(WebCore::ParsedURLString, "data:foo"),
533                         "something.gif");
534     EXPECT_TRUE(kurl3.isEmpty());
535     EXPECT_FALSE(kurl3.isValid());
536 #endif
537
538     // Test for weird isNull string input,
539     // see: http://bugs.webkit.org/show_bug.cgi?id=16487
540     WebCore::KURL kurl4(WebCore::ParsedURLString, kurl.string());
541     EXPECT_TRUE(kurl4.isEmpty());
542     EXPECT_FALSE(kurl4.isValid());
543     EXPECT_TRUE(kurl4.string().isNull());
544     EXPECT_TRUE(kurl4.string().isEmpty());
545
546     // Resolving an empty URL on an invalid string.
547     WebCore::KURL kurl5(WebCore::KURL(), "foo.js");
548     // We'll be empty in this case, but KURL won't be. Should be OK.
549     // EXPECT_EQ(kurl5.isEmpty(), kurl5.isEmpty());
550     // EXPECT_EQ(kurl5.string().isEmpty(), kurl5.string().isEmpty());
551     EXPECT_FALSE(kurl5.isValid());
552     EXPECT_FALSE(kurl5.string().isNull());
553
554     // Empty string as input
555     WebCore::KURL kurl6(WebCore::ParsedURLString, "");
556     EXPECT_TRUE(kurl6.isEmpty());
557     EXPECT_FALSE(kurl6.isValid());
558     EXPECT_FALSE(kurl6.string().isNull());
559     EXPECT_TRUE(kurl6.string().isEmpty());
560
561     // Non-empty but invalid C string as input.
562     WebCore::KURL kurl7(WebCore::ParsedURLString, "foo.js");
563     // WebKit will actually say this URL has the string "foo.js" but is invalid.
564     // We don't do that.
565     // EXPECT_EQ(kurl7.isEmpty(), kurl7.isEmpty());
566     EXPECT_FALSE(kurl7.isValid());
567     EXPECT_FALSE(kurl7.string().isNull());
568 }
569
570 TEST(KURLTest, UserPass)
571 {
572     const char* src = "http://user:pass@google.com/";
573     WebCore::KURL kurl(WebCore::ParsedURLString, src);
574
575     // Clear just the username.
576     kurl.setUser("");
577     EXPECT_EQ("http://:pass@google.com/", kurl.string());
578
579     // Clear just the password.
580     kurl = WebCore::KURL(WebCore::ParsedURLString, src);
581     kurl.setPass("");
582     EXPECT_EQ("http://user@google.com/", kurl.string());
583
584     // Now clear both.
585     kurl.setUser("");
586     EXPECT_EQ("http://google.com/", kurl.string());
587 }
588
589 TEST(KURLTest, Offsets)
590 {
591     const char* src1 = "http://user:pass@google.com/foo/bar.html?baz=query#ref";
592     WebCore::KURL kurl1(WebCore::ParsedURLString, src1);
593
594     EXPECT_EQ(17u, kurl1.hostStart());
595     EXPECT_EQ(27u, kurl1.hostEnd());
596     EXPECT_EQ(27u, kurl1.pathStart());
597     EXPECT_EQ(40u, kurl1.pathEnd());
598     EXPECT_EQ(32u, kurl1.pathAfterLastSlash());
599
600     const char* src2 = "http://google.com/foo/";
601     WebCore::KURL kurl2(WebCore::ParsedURLString, src2);
602
603     EXPECT_EQ(7u, kurl2.hostStart());
604     EXPECT_EQ(17u, kurl2.hostEnd());
605     EXPECT_EQ(17u, kurl2.pathStart());
606     EXPECT_EQ(22u, kurl2.pathEnd());
607     EXPECT_EQ(22u, kurl2.pathAfterLastSlash());
608
609     const char* src3 = "javascript:foobar";
610     WebCore::KURL kurl3(WebCore::ParsedURLString, src3);
611
612     EXPECT_EQ(11u, kurl3.hostStart());
613     EXPECT_EQ(11u, kurl3.hostEnd());
614     EXPECT_EQ(11u, kurl3.pathStart());
615     EXPECT_EQ(17u, kurl3.pathEnd());
616     EXPECT_EQ(11u, kurl3.pathAfterLastSlash());
617 }
618
619 TEST(KURLTest, DeepCopy)
620 {
621     const char url[] = "http://www.google.com/";
622     WebCore::KURL src(WebCore::ParsedURLString, url);
623     EXPECT_TRUE(src.string() == url); // This really just initializes the cache.
624     WebCore::KURL dest = src.copy();
625     EXPECT_TRUE(dest.string() == url); // This really just initializes the cache.
626
627     // The pointers should be different for both UTF-8 and UTF-16.
628     EXPECT_NE(dest.string().characters(), src.string().characters());
629     EXPECT_NE(dest.utf8String().data(), src.utf8String().data());
630 }
631
632 TEST(KURLTest, ProtocolIs)
633 {
634     WebCore::KURL url1(WebCore::ParsedURLString, "foo://bar");
635     EXPECT_TRUE(url1.protocolIs("foo"));
636     EXPECT_FALSE(url1.protocolIs("foo-bar"));
637
638     WebCore::KURL url2(WebCore::ParsedURLString, "foo-bar:");
639     EXPECT_TRUE(url2.protocolIs("foo-bar"));
640     EXPECT_FALSE(url2.protocolIs("foo"));
641 }
642
643 } // namespace