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