1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
|
/*
* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @summary Unit test for java.net.URL (Based on the URI tests that is authored by Mark Reinhold)
* @bug 4496251
*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.PrintStream;
import java.net.URL;
import java.net.MalformedURLException;
public class Test {
static PrintStream out = System.out;
static int testCount = 0;
// Properties that we check
static final int PARSEFAIL = 1 << 0;
static final int PROTOCOL = 1 << 1;
static final int USERINFO = 1 << 2;
static final int HOST = 1 << 3;
static final int PORT = 1 << 4;
static final int PATH = 1 << 5;
static final int QUERY = 1 << 6;
static final int REF = 1 << 7;
String input;
URL url = null;
URL originalURL;
URL base = null; // Base for resolution/relativization
String op = null; // Op performed if url != originalURL
int checked = 0; // Mask for checked properties
int failed = 0; // Mask for failed properties
Exception exc = null;
private Test(String s) {
testCount++;
input = s;
try {
url = new URL(s);
} catch (MalformedURLException x) {
exc = x;
}
originalURL = url;
}
static Test test(String s) {
return new Test(s);
}
private Test(String s, boolean xxx) {
testCount++;
try {
url = new URL(s);
} catch (Exception x) {
exc = x;
}
if (url != null)
input = url.toString();
originalURL = url;
}
static Test test(URL base, String spec) {
return new Test(base, spec);
}
private Test(URL base, String spec) {
testCount++;
try {
url = new URL(base, spec);
} catch (Exception x) {
exc = x;
}
if (url != null)
input = url.toString();
originalURL = url;
}
static Test test(String protocol, String host, int port, String file) {
return new Test(protocol, host, port, file);
}
private Test(String protocol, String host, int port, String file) {
testCount++;
try {
url = new URL(protocol, host, port, file);
} catch (Exception x) {
exc = x;
}
if (url != null)
input = url.toString();
originalURL = url;
}
boolean parsed() {
return url != null;
}
boolean resolved() {
return base != null;
}
URL url() {
return url;
}
// Operations on Test instances
//
// These are short so as to make test cases compact.
//
// s Scheme
// u User info
// h Host
// n port Number
// p Path
// q Query
// f Fragment
//
// rslv Resolve against given base
// rtvz Relativize
// psa Parse server Authority
// norm Normalize
//
// x Check that parse failed as expected
// z End -- ensure that unchecked components are null
private boolean check1(int prop) {
checked |= prop;
if (!parsed()) {
failed |= prop;
return false;
}
return true;
}
private void check2(String s, String ans, int prop) {
if (s == null && ans == null)
return;
if ((s == null) || !s.equals(ans))
failed |= prop;
}
Test s(String s) {
if (check1(PROTOCOL)) check2(url.getProtocol(), s, PROTOCOL);
return this;
}
Test u(String s) {
if (check1(USERINFO)) check2(url.getUserInfo(), s, USERINFO);
return this;
}
Test h(String s) {
if (check1(HOST)) check2(url.getHost(), s, HOST);
return this;
}
Test n(int n) {
checked |= PORT;
if (!parsed() || (url.getPort() != n))
failed |= PORT;
return this;
}
Test p(String s) {
if (check1(PATH)) check2(url.getPath(), s, PATH);
return this;
}
Test q(String s) {
if (check1(QUERY)) check2(url.getQuery(), s, QUERY);
return this;
}
Test f(String s) {
if (check1(REF)) check2(url.getRef(), s, REF);
return this;
}
Test x() {
checked |= PARSEFAIL;
if (parsed())
failed |= PARSEFAIL;
return this;
}
private void checkEmpty(String s, int prop) {
if (((checked & prop) == 0) && (s != null))
failed |= prop;
}
// Check that unchecked component properties are not defined,
// and report any failures
Test z() {
if (!parsed()) {
report();
return this;
}
checkEmpty(url.getProtocol(), PROTOCOL);
checkEmpty(url.getUserInfo(), USERINFO);
checkEmpty(url.getHost(), HOST);
if (((checked & PORT) == 0) && (url.getPort() != -1)) failed |= PORT;
checkEmpty(url.getPath(), PATH);
checkEmpty(url.getQuery(), QUERY);
checkEmpty(url.getRef(), REF);
report();
return this;
}
// Summarization and reporting
static void header(String s) {
out.println();
out.println();
out.println("-- " + s + " --");
}
static void show(String prefix, MalformedURLException x) {
out.println(prefix + ": " + x.getMessage());
}
private void summarize() {
out.println();
StringBuffer sb = new StringBuffer();
if (input.length() == 0)
sb.append("\"\"");
else
sb.append(input);
if (base != null) {
sb.append(" ");
sb.append(base);
}
if (!parsed()) {
String s = (((checked & PARSEFAIL) != 0)
? "Correct exception" : "UNEXPECTED EXCEPTION");
if (exc instanceof MalformedURLException)
show(s, (MalformedURLException)exc);
else {
out.println(sb.toString());
out.print(s + ": ");
exc.printStackTrace(out);
}
} else {
if (url != originalURL) {
sb.append(" ");
sb.append(op);
sb.append(" --> ");
sb.append(url);
}
out.println(sb.toString());
}
}
static void show(String n, String v) {
out.println(" " + n + " = ".substring(n.length()) + v);
}
public static void show(URL u) {
show("scheme", u.getProtocol());
show("authority", u.getAuthority());
show("userInfo", u.getUserInfo());
show("host", u.getHost());
show("port", "" + u.getPort());
show("path", u.getPath());
show("query", u.getQuery());
show("ref", u.getRef());
}
private void report() {
summarize();
if (failed == 0) return;
StringBuffer sb = new StringBuffer();
sb.append("FAIL:");
if ((failed & PARSEFAIL) != 0) sb.append(" parsefail");
if ((failed & PROTOCOL) != 0) sb.append(" scheme");
if ((failed & USERINFO) != 0) sb.append(" userinfo");
if ((failed & HOST) != 0) sb.append(" host");
if ((failed & PORT) != 0) sb.append(" port");
if ((failed & PATH) != 0) sb.append(" path");
if ((failed & QUERY) != 0) sb.append(" query");
if ((failed & REF) != 0) sb.append(" fragment");
out.println(sb.toString());
if (url != null) show(url);
throw new RuntimeException("Test failed");
}
private static boolean hasFtp() {
try {
return new java.net.URL("ftp://") != null;
} catch (java.net.MalformedURLException x) {
System.out.println("FTP not supported by this runtime.");
return false;
}
}
// -- Tests --
static void rfc2396() {
header("RFC2396: Basic examples");
if (hasFtp())
test("ftp://ftp.is.co.za/rfc/rfc1808.txt")
.s("ftp").h("ftp.is.co.za").p("/rfc/rfc1808.txt").z();
test("http://www.math.uio.no/faq/compression-faq/part1.html")
.s("http").h("www.math.uio.no").p("/faq/compression-faq/part1.html").z();
test("http://www.w3.org/Addressing/")
.s("http").h("www.w3.org").p("/Addressing/").z();
if (hasFtp())
test("ftp://ds.internic.net/rfc/")
.s("ftp").h("ds.internic.net").p("/rfc/").z();
test("http://www.ics.uci.edu/pub/ietf/url/historical.html#WARNING")
.s("http").h("www.ics.uci.edu").p("/pub/ietf/url/historical.html")
.f("WARNING").z();
test("http://www.ics.uci.edu/pub/ietf/url/#Related")
.s("http").h("www.ics.uci.edu").p("/pub/ietf/url/")
.f("Related").z();
test("file:/home/someone/dir1/dir2/file").s("file").h("").p("/home/someone/dir1/dir2/file").z();
header("RFC2396: Normal relative-URL examples (appendix C)");
URL base = (test("http://a/b/c/d;p?q")
.s("http").h("a").p("/b/c/d;p").q("q").z().url());
// g:h g:h
// test(base, "http:h").s("g").p("h").z();
// g http://a/b/c/g
test(base, "g").s("http").h("a").p("/b/c/g").z();
// ./g http://a/b/c/g
test(base, "./g").s("http").h("a").p("/b/c/g").z();
// g/ http://a/b/c/g/
test(base, "g/").s("http").h("a").p("/b/c/g/").z();
// /g http://a/g
test(base, "/g").s("http").h("a").p("/g").z();
// //g http://g
test(base,"//g").s("http").h("g").p("").z();
// ?y http://a/b/c/?y
test(base, "?y").s("http").h("a").p("/b/c/").q("y").z();
// g?y http://a/b/c/g?y
test(base, "g?y").s("http").h("a").p("/b/c/g").q("y").z();
// #s (current document)#s
// DEVIATION: Lone fragment parses as relative URL with empty path,
// and resolves without removing the last segment of the base path.
// test(base,"#s").s("http").h("a").p("/b/c/d;p").f("s").z();
test(base,"#s").s("http").h("a").p("/b/c/d;p").q("q").f("s").z();
// g#s http://a/b/c/g#s
test(base, "g#s").s("http").h("a").p("/b/c/g").f("s").z();
// g?y#s http://a/b/c/g?y#s
test(base,"g?y#s").s("http").h("a").p("/b/c/g").q("y").f("s").z();
// ;x http://a/b/c/;x
test(base,";x").s("http").h("a").p("/b/c/;x").z();
// g;x http://a/b/c/g;x
test(base,"g;x").s("http").h("a").p("/b/c/g;x").z();
// g;x?y#s http://a/b/c/g;x?y#s
test(base,"g;x?y#s").s("http").h("a").p("/b/c/g;x").q("y").f("s").z();
// . http://a/b/c/
test(base,".").s("http").h("a").p("/b/c/").z();
// ./ http://a/b/c/
test(base,"./").s("http").h("a").p("/b/c/").z();
// .. http://a/b/
test(base,"..").s("http").h("a").p("/b/").z();
// ../ http://a/b/
test(base,"../").s("http").h("a").p("/b/").z();
// ../g http://a/b/g
test(base,"../g").s("http").h("a").p("/b/g").z();
// ../.. http://a/
test(base,"../..").s("http").h("a").p("/").z();
// ../../ http://a/
test(base,"../../").s("http").h("a").p("/").z();
// ../../g http://a/g
test(base,"../../g").s("http").h("a").p("/g").z();
// http://u@s1/p1 http://s2/p2
test(test("http://u:p@s1/p1").url(),"http://s2/p2")
.s("http").h("s2").u(null).p("/p2").z();
header("RFC2396: Abnormal relative-URL examples (appendix C)");
// ../../../g = http://a/../g
test(base,"../../../g").s("http").h("a").p("/../g").z();
// ../../../../g = http://a/../../g
test(base, "../../../../g").s("http").h("a").p("/../../g").z();
// /./g = http://a/./g
test(base,"/./g").s("http").h("a").p("/./g").z();
// /../g = http://a/../g
test(base,"/../g").s("http").h("a").p("/../g").z();
// g. = http://a/b/c/g.
test(base,"g.").s("http").h("a").p("/b/c/g.").z();
// .g = http://a/b/c/.g
test(base,".g").s("http").h("a").p("/b/c/.g").z();
// g.. = http://a/b/c/g..
test(base,"g..").s("http").h("a").p("/b/c/g..").z();
// ..g = http://a/b/c/..g
test(base,"..g").s("http").h("a").p("/b/c/..g").z();
// ./../g = http://a/b/g
test(base,"./../g").s("http").h("a").p("/b/g").z();
// ./g/. = http://a/b/c/g/
test(base,"./g/.").s("http").h("a").p("/b/c/g/").z();
// g/./h = http://a/b/c/g/h
test(base,"g/./h").s("http").h("a").p("/b/c/g/h").z();
// g/../h = http://a/b/c/h
test(base,"g/../h").s("http").h("a").p("/b/c/h").z();
// g;x=1/./y = http://a/b/c/g;x=1/y
test(base,"g;x=1/./y").s("http").h("a").p("/b/c/g;x=1/y").z();
// g;x=1/../y = http://a/b/c/y
test(base,"g;x=1/../y").s("http").h("a").p("/b/c/y").z();
// g?y/./x = http://a/b/c/g?y/./x
test(base,"g?y/./x").s("http").h("a").p("/b/c/g").q("y/./x").z();
// g?y/../x = http://a/b/c/g?y/../x
test(base,"g?y/../x").s("http").h("a").p("/b/c/g").q("y/../x").z();
// g#s/./x = http://a/b/c/g#s/./x
test(base,"g#s/./x").s("http").h("a").p("/b/c/g").f("s/./x").z();
// g#s/../x = http://a/b/c/g#s/../x
test(base,"g#s/../x").s("http").h("a").p("/b/c/g").f("s/../x").z();
// http:g = http:g
// test(base,"http:g").s("http").p("g").z();
}
static void ip() {
header("IP addresses");
test("http://1.2.3.4:5")
.s("http").h("1.2.3.4").n(5).p("").z();
// From RFC2732
test("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html")
.s("http").h("[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]")
.n(80).p("/index.html").z();
test("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210%12]:80/index.html")
.s("http").h("[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210%12]")
.n(80).p("/index.html").z();
test("http://[1080:0:0:0:8:800:200C:417A]/index.html")
.s("http").h("[1080:0:0:0:8:800:200C:417A]").p("/index.html").z();
test("http://[1080:0:0:0:8:800:200C:417A%1]/index.html")
.s("http").h("[1080:0:0:0:8:800:200C:417A%1]").p("/index.html").z();
test("http://[3ffe:2a00:100:7031::1]")
.s("http").h("[3ffe:2a00:100:7031::1]").p("").z();
test("http://[1080::8:800:200C:417A]/foo")
.s("http").h("[1080::8:800:200C:417A]").p("/foo").z();
test("http://[::192.9.5.5]/ipng")
.s("http").h("[::192.9.5.5]").p("/ipng").z();
test("http://[::192.9.5.5%interface]/ipng")
.s("http").h("[::192.9.5.5%interface]").p("/ipng").z();
test("http://[::FFFF:129.144.52.38]:80/index.html")
.s("http").h("[::FFFF:129.144.52.38]").n(80).p("/index.html").z();
test("http://[2010:836B:4179::836B:4179]")
.s("http").h("[2010:836B:4179::836B:4179]").p("").z();
// From RFC2373
test("http://[FF01::101]")
.s("http").h("[FF01::101]").p("").z();
test("http://[::1]")
.s("http").h("[::1]").p("").z();
test("http://[::]")
.s("http").h("[::]").p("").z();
test("http://[::%hme0]")
.s("http").h("[::%hme0]").p("").z();
test("http://[0:0:0:0:0:0:13.1.68.3]")
.s("http").h("[0:0:0:0:0:0:13.1.68.3]").p("").z();
test("http://[0:0:0:0:0:FFFF:129.144.52.38]")
.s("http").h("[0:0:0:0:0:FFFF:129.144.52.38]").p("").z();
test("http://[0:0:0:0:0:FFFF:129.144.52.38%33]")
.s("http").h("[0:0:0:0:0:FFFF:129.144.52.38%33]").p("").z();
test("http://[::13.1.68.3]")
.s("http").h("[::13.1.68.3]").p("").z();
test("http://[::13.1.68.3]:")
.s("http").h("[::13.1.68.3]").p("").z();
// Error cases
test("http://[ff01:234/foo").x().z();
test("http://[ff01:234:zzz]/foo").x().z();
test("http://[foo]").x().z();
test("http://[]").x().z();
test("http://[129.33.44.55]").x().z();
test("http://[ff:ee:dd::cc:bb::aa:9:8]").x().z();
test("http://[1:2:3:4:5:6:7:8%]").x().z();
test("http://[1:2:3:4:5:6:7:8%!/]").x().z();
test("http://[1:2:3:4:5:6:7:8:9]").x().z();
test("http://[::1.2.3.300]").x().z();
test("http://[1.2.3.4:5]").x().z();
// Optional IPv6 brackets in constructors
test("http", "1:2:3:4:5:6:7:8", -1, "")
.s("http").h("[1:2:3:4:5:6:7:8]").p("").z();
test("http", "1:2:3:4:5:6:7:8%hme0", -1, "")
.s("http").h("[1:2:3:4:5:6:7:8%hme0]").p("").z();
test("http", "[1:2:3:4:5:6:7:8]", -1, "")
.s("http").h("[1:2:3:4:5:6:7:8]").p("").z();
}
static void serial() throws IOException, MalformedURLException {
header("Serialization");
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(bo);
URL u = new URL("http://java.sun.com/jdk/1.4?release#beta");
oo.writeObject(u);
oo.close();
ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
ObjectInputStream oi = new ObjectInputStream(bi);
try {
Object o = oi.readObject();
u.equals(o);
} catch (ClassNotFoundException x) {
x.printStackTrace();
throw new RuntimeException(x.toString());
}
}
static void tests() throws IOException, MalformedURLException {
rfc2396();
ip();
serial();
}
// -- Command-line invocation --
static void usage() {
out.println("Usage:");
out.println(" java Test -- Runs all tests in this file");
out.println(" java Test <url> -- Parses url, shows components");
out.println(" java Test <base> <url> -- Parses url and base, then resolves");
out.println(" url against base");
}
public static void main(String[] args) throws Exception {
switch (args.length) {
case 0:
tests();
out.println();
out.println("Test cases: " + testCount);
break;
case 1:
if (args[0].equals("-help")) {
usage();
break;
}
// clargs(null, args[0]);
break;
case 2:
// clargs(args[0], args[1]);
break;
default:
usage();
break;
}
}
}
|