Path: blob/master/test/jdk/java/net/URL/Test.java
41149 views
/*1* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @summary Unit test for java.net.URL (Based on the URI tests that is authored by Mark Reinhold)25* @bug 449625126*/2728import java.io.ByteArrayInputStream;29import java.io.ByteArrayOutputStream;30import java.io.IOException;31import java.io.ObjectInputStream;32import java.io.ObjectOutputStream;33import java.io.PrintStream;34import java.net.URL;35import java.net.MalformedURLException;363738public class Test {3940static PrintStream out = System.out;41static int testCount = 0;4243// Properties that we check44static final int PARSEFAIL = 1 << 0;45static final int PROTOCOL = 1 << 1;46static final int USERINFO = 1 << 2;47static final int HOST = 1 << 3;48static final int PORT = 1 << 4;49static final int PATH = 1 << 5;50static final int QUERY = 1 << 6;51static final int REF = 1 << 7;5253String input;54URL url = null;55URL originalURL;56URL base = null; // Base for resolution/relativization57String op = null; // Op performed if url != originalURL58int checked = 0; // Mask for checked properties59int failed = 0; // Mask for failed properties60Exception exc = null;6162private Test(String s) {63testCount++;64input = s;65try {66url = new URL(s);67} catch (MalformedURLException x) {68exc = x;69}70originalURL = url;71}7273static Test test(String s) {74return new Test(s);75}7677private Test(String s, boolean xxx) {78testCount++;79try {80url = new URL(s);81} catch (Exception x) {82exc = x;83}84if (url != null)85input = url.toString();86originalURL = url;87}8889static Test test(URL base, String spec) {90return new Test(base, spec);91}92private Test(URL base, String spec) {93testCount++;94try {95url = new URL(base, spec);96} catch (Exception x) {97exc = x;98}99if (url != null)100input = url.toString();101originalURL = url;102}103104static Test test(String protocol, String host, int port, String file) {105return new Test(protocol, host, port, file);106}107private Test(String protocol, String host, int port, String file) {108testCount++;109try {110url = new URL(protocol, host, port, file);111} catch (Exception x) {112exc = x;113}114if (url != null)115input = url.toString();116originalURL = url;117}118119boolean parsed() {120return url != null;121}122123boolean resolved() {124return base != null;125}126127URL url() {128return url;129}130131// Operations on Test instances132//133// These are short so as to make test cases compact.134//135// s Scheme136// u User info137// h Host138// n port Number139// p Path140// q Query141// f Fragment142//143// rslv Resolve against given base144// rtvz Relativize145// psa Parse server Authority146// norm Normalize147//148// x Check that parse failed as expected149// z End -- ensure that unchecked components are null150151private boolean check1(int prop) {152checked |= prop;153if (!parsed()) {154failed |= prop;155return false;156}157return true;158}159160private void check2(String s, String ans, int prop) {161if (s == null && ans == null)162return;163if ((s == null) || !s.equals(ans))164failed |= prop;165}166167Test s(String s) {168if (check1(PROTOCOL)) check2(url.getProtocol(), s, PROTOCOL);169return this;170}171172Test u(String s) {173if (check1(USERINFO)) check2(url.getUserInfo(), s, USERINFO);174return this;175}176177Test h(String s) {178if (check1(HOST)) check2(url.getHost(), s, HOST);179return this;180}181182Test n(int n) {183checked |= PORT;184if (!parsed() || (url.getPort() != n))185failed |= PORT;186return this;187}188189Test p(String s) {190if (check1(PATH)) check2(url.getPath(), s, PATH);191return this;192}193194Test q(String s) {195if (check1(QUERY)) check2(url.getQuery(), s, QUERY);196return this;197}198199Test f(String s) {200if (check1(REF)) check2(url.getRef(), s, REF);201return this;202}203204Test x() {205checked |= PARSEFAIL;206if (parsed())207failed |= PARSEFAIL;208return this;209}210211private void checkEmpty(String s, int prop) {212if (((checked & prop) == 0) && (s != null))213failed |= prop;214}215216// Check that unchecked component properties are not defined,217// and report any failures218Test z() {219if (!parsed()) {220report();221return this;222}223checkEmpty(url.getProtocol(), PROTOCOL);224checkEmpty(url.getUserInfo(), USERINFO);225checkEmpty(url.getHost(), HOST);226if (((checked & PORT) == 0) && (url.getPort() != -1)) failed |= PORT;227checkEmpty(url.getPath(), PATH);228checkEmpty(url.getQuery(), QUERY);229checkEmpty(url.getRef(), REF);230report();231return this;232}233234235// Summarization and reporting236237static void header(String s) {238out.println();239out.println();240out.println("-- " + s + " --");241}242243static void show(String prefix, MalformedURLException x) {244out.println(prefix + ": " + x.getMessage());245}246247private void summarize() {248out.println();249StringBuffer sb = new StringBuffer();250if (input.length() == 0)251sb.append("\"\"");252else253sb.append(input);254if (base != null) {255sb.append(" ");256sb.append(base);257}258if (!parsed()) {259String s = (((checked & PARSEFAIL) != 0)260? "Correct exception" : "UNEXPECTED EXCEPTION");261if (exc instanceof MalformedURLException)262show(s, (MalformedURLException)exc);263else {264out.println(sb.toString());265out.print(s + ": ");266exc.printStackTrace(out);267}268} else {269if (url != originalURL) {270sb.append(" ");271sb.append(op);272sb.append(" --> ");273sb.append(url);274}275out.println(sb.toString());276}277}278279static void show(String n, String v) {280out.println(" " + n + " = ".substring(n.length()) + v);281}282283public static void show(URL u) {284show("scheme", u.getProtocol());285show("authority", u.getAuthority());286show("userInfo", u.getUserInfo());287show("host", u.getHost());288show("port", "" + u.getPort());289show("path", u.getPath());290show("query", u.getQuery());291show("ref", u.getRef());292}293294private void report() {295summarize();296if (failed == 0) return;297StringBuffer sb = new StringBuffer();298sb.append("FAIL:");299if ((failed & PARSEFAIL) != 0) sb.append(" parsefail");300if ((failed & PROTOCOL) != 0) sb.append(" scheme");301if ((failed & USERINFO) != 0) sb.append(" userinfo");302if ((failed & HOST) != 0) sb.append(" host");303if ((failed & PORT) != 0) sb.append(" port");304if ((failed & PATH) != 0) sb.append(" path");305if ((failed & QUERY) != 0) sb.append(" query");306if ((failed & REF) != 0) sb.append(" fragment");307out.println(sb.toString());308if (url != null) show(url);309throw new RuntimeException("Test failed");310}311312private static boolean hasFtp() {313try {314return new java.net.URL("ftp://") != null;315} catch (java.net.MalformedURLException x) {316System.out.println("FTP not supported by this runtime.");317return false;318}319}320321// -- Tests --322323static void rfc2396() {324325326header("RFC2396: Basic examples");327328if (hasFtp())329test("ftp://ftp.is.co.za/rfc/rfc1808.txt")330.s("ftp").h("ftp.is.co.za").p("/rfc/rfc1808.txt").z();331332test("http://www.math.uio.no/faq/compression-faq/part1.html")333.s("http").h("www.math.uio.no").p("/faq/compression-faq/part1.html").z();334335test("http://www.w3.org/Addressing/")336.s("http").h("www.w3.org").p("/Addressing/").z();337338if (hasFtp())339test("ftp://ds.internic.net/rfc/")340.s("ftp").h("ds.internic.net").p("/rfc/").z();341342test("http://www.ics.uci.edu/pub/ietf/url/historical.html#WARNING")343.s("http").h("www.ics.uci.edu").p("/pub/ietf/url/historical.html")344.f("WARNING").z();345346test("http://www.ics.uci.edu/pub/ietf/url/#Related")347.s("http").h("www.ics.uci.edu").p("/pub/ietf/url/")348.f("Related").z();349350test("file:/home/someone/dir1/dir2/file").s("file").h("").p("/home/someone/dir1/dir2/file").z();351352header("RFC2396: Normal relative-URL examples (appendix C)");353354URL base = (test("http://a/b/c/d;p?q")355.s("http").h("a").p("/b/c/d;p").q("q").z().url());356357// g:h g:h358// test(base, "http:h").s("g").p("h").z();359360// g http://a/b/c/g361test(base, "g").s("http").h("a").p("/b/c/g").z();362363// ./g http://a/b/c/g364test(base, "./g").s("http").h("a").p("/b/c/g").z();365366// g/ http://a/b/c/g/367test(base, "g/").s("http").h("a").p("/b/c/g/").z();368369// /g http://a/g370test(base, "/g").s("http").h("a").p("/g").z();371372// //g http://g373test(base,"//g").s("http").h("g").p("").z();374375// ?y http://a/b/c/?y376test(base, "?y").s("http").h("a").p("/b/c/").q("y").z();377378// g?y http://a/b/c/g?y379test(base, "g?y").s("http").h("a").p("/b/c/g").q("y").z();380381// #s (current document)#s382// DEVIATION: Lone fragment parses as relative URL with empty path,383// and resolves without removing the last segment of the base path.384// test(base,"#s").s("http").h("a").p("/b/c/d;p").f("s").z();385test(base,"#s").s("http").h("a").p("/b/c/d;p").q("q").f("s").z();386387// g#s http://a/b/c/g#s388test(base, "g#s").s("http").h("a").p("/b/c/g").f("s").z();389390// g?y#s http://a/b/c/g?y#s391test(base,"g?y#s").s("http").h("a").p("/b/c/g").q("y").f("s").z();392393// ;x http://a/b/c/;x394test(base,";x").s("http").h("a").p("/b/c/;x").z();395396// g;x http://a/b/c/g;x397test(base,"g;x").s("http").h("a").p("/b/c/g;x").z();398399// g;x?y#s http://a/b/c/g;x?y#s400test(base,"g;x?y#s").s("http").h("a").p("/b/c/g;x").q("y").f("s").z();401402// . http://a/b/c/403test(base,".").s("http").h("a").p("/b/c/").z();404405// ./ http://a/b/c/406test(base,"./").s("http").h("a").p("/b/c/").z();407408// .. http://a/b/409test(base,"..").s("http").h("a").p("/b/").z();410411// ../ http://a/b/412test(base,"../").s("http").h("a").p("/b/").z();413414// ../g http://a/b/g415test(base,"../g").s("http").h("a").p("/b/g").z();416417// ../.. http://a/418test(base,"../..").s("http").h("a").p("/").z();419420// ../../ http://a/421test(base,"../../").s("http").h("a").p("/").z();422423// ../../g http://a/g424test(base,"../../g").s("http").h("a").p("/g").z();425426427// http://u@s1/p1 http://s2/p2428test(test("http://u:p@s1/p1").url(),"http://s2/p2")429.s("http").h("s2").u(null).p("/p2").z();430431header("RFC2396: Abnormal relative-URL examples (appendix C)");432433// ../../../g = http://a/../g434test(base,"../../../g").s("http").h("a").p("/../g").z();435436// ../../../../g = http://a/../../g437test(base, "../../../../g").s("http").h("a").p("/../../g").z();438439440// /./g = http://a/./g441test(base,"/./g").s("http").h("a").p("/./g").z();442443// /../g = http://a/../g444test(base,"/../g").s("http").h("a").p("/../g").z();445446// g. = http://a/b/c/g.447test(base,"g.").s("http").h("a").p("/b/c/g.").z();448449// .g = http://a/b/c/.g450test(base,".g").s("http").h("a").p("/b/c/.g").z();451452// g.. = http://a/b/c/g..453test(base,"g..").s("http").h("a").p("/b/c/g..").z();454455// ..g = http://a/b/c/..g456test(base,"..g").s("http").h("a").p("/b/c/..g").z();457458// ./../g = http://a/b/g459test(base,"./../g").s("http").h("a").p("/b/g").z();460461// ./g/. = http://a/b/c/g/462test(base,"./g/.").s("http").h("a").p("/b/c/g/").z();463464// g/./h = http://a/b/c/g/h465test(base,"g/./h").s("http").h("a").p("/b/c/g/h").z();466467// g/../h = http://a/b/c/h468test(base,"g/../h").s("http").h("a").p("/b/c/h").z();469470// g;x=1/./y = http://a/b/c/g;x=1/y471test(base,"g;x=1/./y").s("http").h("a").p("/b/c/g;x=1/y").z();472473// g;x=1/../y = http://a/b/c/y474test(base,"g;x=1/../y").s("http").h("a").p("/b/c/y").z();475476// g?y/./x = http://a/b/c/g?y/./x477test(base,"g?y/./x").s("http").h("a").p("/b/c/g").q("y/./x").z();478479// g?y/../x = http://a/b/c/g?y/../x480test(base,"g?y/../x").s("http").h("a").p("/b/c/g").q("y/../x").z();481482// g#s/./x = http://a/b/c/g#s/./x483test(base,"g#s/./x").s("http").h("a").p("/b/c/g").f("s/./x").z();484485// g#s/../x = http://a/b/c/g#s/../x486test(base,"g#s/../x").s("http").h("a").p("/b/c/g").f("s/../x").z();487488// http:g = http:g489// test(base,"http:g").s("http").p("g").z();490491}492493494static void ip() {495496header("IP addresses");497498test("http://1.2.3.4:5")499.s("http").h("1.2.3.4").n(5).p("").z();500501// From RFC2732502503test("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html")504.s("http").h("[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]")505.n(80).p("/index.html").z();506507test("http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210%12]:80/index.html")508.s("http").h("[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210%12]")509.n(80).p("/index.html").z();510511test("http://[1080:0:0:0:8:800:200C:417A]/index.html")512.s("http").h("[1080:0:0:0:8:800:200C:417A]").p("/index.html").z();513514test("http://[1080:0:0:0:8:800:200C:417A%1]/index.html")515.s("http").h("[1080:0:0:0:8:800:200C:417A%1]").p("/index.html").z();516517test("http://[3ffe:2a00:100:7031::1]")518.s("http").h("[3ffe:2a00:100:7031::1]").p("").z();519520test("http://[1080::8:800:200C:417A]/foo")521.s("http").h("[1080::8:800:200C:417A]").p("/foo").z();522523test("http://[::192.9.5.5]/ipng")524.s("http").h("[::192.9.5.5]").p("/ipng").z();525526test("http://[::192.9.5.5%interface]/ipng")527.s("http").h("[::192.9.5.5%interface]").p("/ipng").z();528529test("http://[::FFFF:129.144.52.38]:80/index.html")530.s("http").h("[::FFFF:129.144.52.38]").n(80).p("/index.html").z();531532test("http://[2010:836B:4179::836B:4179]")533.s("http").h("[2010:836B:4179::836B:4179]").p("").z();534535// From RFC2373536537test("http://[FF01::101]")538.s("http").h("[FF01::101]").p("").z();539540test("http://[::1]")541.s("http").h("[::1]").p("").z();542543test("http://[::]")544.s("http").h("[::]").p("").z();545546test("http://[::%hme0]")547.s("http").h("[::%hme0]").p("").z();548549test("http://[0:0:0:0:0:0:13.1.68.3]")550.s("http").h("[0:0:0:0:0:0:13.1.68.3]").p("").z();551552test("http://[0:0:0:0:0:FFFF:129.144.52.38]")553.s("http").h("[0:0:0:0:0:FFFF:129.144.52.38]").p("").z();554555test("http://[0:0:0:0:0:FFFF:129.144.52.38%33]")556.s("http").h("[0:0:0:0:0:FFFF:129.144.52.38%33]").p("").z();557558test("http://[::13.1.68.3]")559.s("http").h("[::13.1.68.3]").p("").z();560561test("http://[::13.1.68.3]:")562.s("http").h("[::13.1.68.3]").p("").z();563// Error cases564565test("http://[ff01:234/foo").x().z();566test("http://[ff01:234:zzz]/foo").x().z();567test("http://[foo]").x().z();568test("http://[]").x().z();569test("http://[129.33.44.55]").x().z();570test("http://[ff:ee:dd::cc:bb::aa:9:8]").x().z();571test("http://[1:2:3:4:5:6:7:8%]").x().z();572test("http://[1:2:3:4:5:6:7:8%!/]").x().z();573test("http://[1:2:3:4:5:6:7:8:9]").x().z();574test("http://[::1.2.3.300]").x().z();575test("http://[1.2.3.4:5]").x().z();576577// Optional IPv6 brackets in constructors578test("http", "1:2:3:4:5:6:7:8", -1, "")579.s("http").h("[1:2:3:4:5:6:7:8]").p("").z();580test("http", "1:2:3:4:5:6:7:8%hme0", -1, "")581.s("http").h("[1:2:3:4:5:6:7:8%hme0]").p("").z();582test("http", "[1:2:3:4:5:6:7:8]", -1, "")583.s("http").h("[1:2:3:4:5:6:7:8]").p("").z();584585}586587static void serial() throws IOException, MalformedURLException {588589header("Serialization");590591ByteArrayOutputStream bo = new ByteArrayOutputStream();592ObjectOutputStream oo = new ObjectOutputStream(bo);593URL u = new URL("http://java.sun.com/jdk/1.4?release#beta");594oo.writeObject(u);595oo.close();596597ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());598ObjectInputStream oi = new ObjectInputStream(bi);599try {600Object o = oi.readObject();601u.equals(o);602} catch (ClassNotFoundException x) {603x.printStackTrace();604throw new RuntimeException(x.toString());605}606607}608609static void tests() throws IOException, MalformedURLException {610rfc2396();611ip();612serial();613}614615616// -- Command-line invocation --617618static void usage() {619out.println("Usage:");620out.println(" java Test -- Runs all tests in this file");621out.println(" java Test <url> -- Parses url, shows components");622out.println(" java Test <base> <url> -- Parses url and base, then resolves");623out.println(" url against base");624}625626public static void main(String[] args) throws Exception {627switch (args.length) {628629case 0:630tests();631out.println();632out.println("Test cases: " + testCount);633break;634635case 1:636if (args[0].equals("-help")) {637usage();638break;639}640// clargs(null, args[0]);641break;642643case 2:644// clargs(args[0], args[1]);645break;646647default:648usage();649break;650651}652}653654}655656657