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