/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* 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.URI
* @bug 4464135 4505046 4503239 4438319 4991359 4866303 7023363 7041800
* @author Mark Reinhold
*/
public class Test {
// Properties that we check
testCount++;
input = s;
try {
} catch (URISyntaxException x) {
exc = x;
}
originalURI = uri;
}
return new Test(s);
}
{
testCount++;
try {
} catch (URISyntaxException x) {
exc = x;
}
originalURI = uri;
}
return new Test(s, u, h, n, p, q, f);
}
{
testCount++;
try {
} catch (URISyntaxException x) {
exc = x;
}
originalURI = uri;
}
return new Test(s, a, p, q, f);
}
testCount++;
try {
} catch (URISyntaxException x) {
exc = x;
}
originalURI = uri;
}
return new Test(s, h, p, f);
}
testCount++;
try {
} catch (URISyntaxException x) {
exc = x;
}
originalURI = uri;
}
}
testCount++;
try {
} catch (IllegalArgumentException x) {
exc = x;
}
originalURI = uri;
}
return new Test(s, false);
}
boolean parsed() {
}
boolean resolved() {
}
return uri;
}
// Operations on Test instances
//
// These are short so as to make test cases compact.
//
// s Scheme
// sp Scheme-specific part
// spd Scheme-specific part, decoded
// o Opaque part (isOpaque() && ssp matches)
// g reGistry (authority matches, and host is not defined)
// gd reGistry, decoded
// u User info
// ud User info, decoded
// h Host
// n port Number
// p Path
// pd Path, decoded
// q Query
// qd Query, decoded
// f Fragment
// fd Fragment, decoded
//
// rslv Resolve against given base
// rtvz Relativize
// psa Parse server Authority
// norm Normalize
// ta ASCII form
//
// x Check that parse failed as expected
// z End -- ensure that unchecked components are null
if (!parsed()) {
return false;
}
return true;
}
}
return this;
}
return this;
}
if (check1(USERINFO_D)) {
}
return this;
}
return this;
}
else
}
return this;
}
if (check1(REGISTRY_D)) {
failed |= REGISTRY_D;
else
}
return this;
}
Test n(int n) {
return this;
}
return this;
}
return this;
}
if (check1(OPAQUEPART)) {
failed |= OPAQUEPART;
else
}
return this;
}
return this;
}
return this;
}
return this;
}
return this;
}
return this;
}
return this;
}
return this;
}
return this;
}
Test x() {
if (parsed())
return this;
}
if (!parsed())
return this;
op = "rslv";
try {
} catch (IllegalArgumentException x) {
exc = x;
}
checked = 0;
failed = 0;
return this;
}
if (!parsed())
return this;
op = "norm";
return this;
}
if (!parsed())
return this;
op = "rtvz";
checked = 0;
failed = 0;
return this;
}
try {
} catch (URISyntaxException x) {
exc = x;
}
checked = 0;
failed = 0;
return this;
}
}
// Check identity for the seven-argument URI constructor
//
void checkURI7() {
// Only works on hierarchical URIs
return;
// Only works with server-based authorities
return;
// Not true if non-US-ASCII chars are encoded unnecessarily
return;
try {
failed |= IDENT_URI7;
} catch (URISyntaxException x) {
failed |= IDENT_URI7;
}
}
// Check identity for the five-argument URI constructor
//
void checkURI5() {
// Only works on hierarchical URIs
return;
try {
failed |= IDENT_URI5;
} catch (URISyntaxException x) {
failed |= IDENT_URI5;
}
}
// Check identity for the three-argument URI constructor
//
void checkURI3() {
try {
uri.getFragment());
failed |= IDENT_URI3;
} catch (URISyntaxException x) {
failed |= IDENT_URI3;
}
}
// Check all identities mentioned in the URI class specification
//
void checkIdentities() {
}
try {
failed |= IDENT_URI1;
} catch (URISyntaxException x) {
failed |= IDENT_URI1;
}
// Remaining identities fail if "//" given but authority is undefined
return;
// Remaining identities fail if ":" given but port is undefined
return;
// Remaining identities fail if non-US-ASCII chars are encoded
// unnecessarily
return;
checkURI3();
checkURI5();
checkURI7();
}
// Check identities, check that unchecked component properties are not
// defined, and report any failures
//
Test z() {
if (!parsed()) {
report();
return this;
}
// Check that unchecked components are undefined
// Report failures
report();
return this;
}
// Summarization and reporting
}
if (x.getIndex() >= 0) {
for (int i = 0; i < x.getIndex(); i++) {
else
}
}
}
private void summarize() {
else
}
if (!parsed()) {
? "Correct exception" : "UNEXPECTED EXCEPTION");
if (exc instanceof URISyntaxException)
else {
}
} else {
if (uri != originalURI) {
}
}
}
return str;
for (int i = 0; i < n; i++) {
if ((c >= ' ') && (c < 0x7f)) {
continue;
}
while (s.length() < 4)
s = "0" + s;
}
}
+ uquote(v));
}
show(n, v);
else {
+ uquote(v)
}
}
}
private void report() {
summarize();
if (failed == 0) return;
throw new RuntimeException("Test failed");
}
// -- Tests --
static void rfc2396() {
header("RFC2396: Basic examples");
test("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles")
.s("gopher").h("spinaltap.micro.umn.edu")
.p("/00/Weather/California/Los%20Angeles").z();
test("mailto:mduerst@ifi.unizh.ch")
.s("mailto").o("mduerst@ifi.unizh.ch").z();
test("news:comp.infosystems.www.servers.unix")
.s("news").o("comp.infosystems.www.servers.unix").z();
test("telnet://melvyl.ucop.edu/")
.s("telnet").h("melvyl.ucop.edu").p("/").z();
.s("http").h("www.w3.org").p("/Addressing/").z();
.s("ftp").h("ds.internic.net").p("/rfc/").z();
.f("WARNING").z();
test("http://www.ics.uci.edu/pub/ietf/uri/#Related")
.f("Related").z();
header("RFC2396: Normal relative-URI examples (appendix C)");
// g:h g:h
test("g:h")
.s("g").o("h").z()
// g http://a/b/c/g
test("g")
.p("g").z()
// ./g http://a/b/c/g
test("./g")
.p("./g").z()
// g/ http://a/b/c/g/
test("g/")
.p("g/").z()
// /g http://a/g
test("/g")
.p("/g").z()
// //g http://g
test("//g")
.h("g").p("").z()
// ?y http://a/b/c/?y
test("?y")
.p("").q("y").z()
// g?y http://a/b/c/g?y
test("g?y")
.p("g").q("y").z()
// #s (current document)#s
// DEVIATION: Lone fragment parses as relative URI with empty path
test("#s")
.p("").f("s").z()
// g#s http://a/b/c/g#s
test("g#s")
.p("g").f("s").z()
// g?y#s http://a/b/c/g?y#s
test("g?y#s")
.p("g").q("y").f("s").z()
// ;x http://a/b/c/;x
test(";x")
.p(";x").z()
// g;x http://a/b/c/g;x
test("g;x")
.p("g;x").z()
// g;x?y#s http://a/b/c/g;x?y#s
test("g;x?y#s")
.p("g;x").q("y").f("s").z()
// . http://a/b/c/
test(".")
.p(".").z()
// ./ http://a/b/c/
test("./")
.p("./").z()
// .. http://a/b/
test("..")
.p("..").z()
// ../ http://a/b/
test("../")
.p("../").z()
// ../g http://a/b/g
test("../g")
.p("../g").z()
// ../.. http://a/
test("../..")
.p("../..").z()
// ../../ http://a/
test("../../")
.p("../../").z()
// ../../g http://a/g
test("../../g")
.p("../../g").z()
header("RFC2396: Abnormal relative-URI examples (appendix C)");
// ../../../g = http://a/../g
test("../../../g")
.p("../../../g").z()
// ../../../../g = http://a/../../g
test("../../../../g")
.p("../../../../g").z()
// /./g = http://a/./g
test("/./g")
.p("/./g").z()
// /../g = http://a/../g
test("/../g")
.p("/../g").z()
// g. = http://a/b/c/g.
test("g.")
.p("g.").z()
// .g = http://a/b/c/.g
test(".g")
.p(".g").z()
// g.. = http://a/b/c/g..
test("g..")
.p("g..").z()
// ..g = http://a/b/c/..g
test("..g")
.p("..g").z()
// ./../g = http://a/b/g
test("./../g")
.p("./../g").z()
// ./g/. = http://a/b/c/g/
test("./g/.")
.p("./g/.").z()
// g/./h = http://a/b/c/g/h
test("g/./h")
.p("g/./h").z()
// g/../h = http://a/b/c/h
test("g/../h")
.p("g/../h").z()
// g;x=1/./y = http://a/b/c/g;x=1/y
test("g;x=1/./y")
.p("g;x=1/./y").z()
// g;x=1/../y = http://a/b/c/y
test("g;x=1/../y")
.p("g;x=1/../y").z()
// g?y/./x = http://a/b/c/g?y/./x
test("g?y/./x")
.p("g").q("y/./x").z()
// g?y/../x = http://a/b/c/g?y/../x
test("g?y/../x")
.p("g").q("y/../x").z()
// g#s/./x = http://a/b/c/g#s/./x
test("g#s/./x")
.p("g").f("s/./x").z()
// g#s/../x = http://a/b/c/g#s/../x
test("g#s/../x")
.p("g").f("s/../x").z()
// http:g = http:g
test("http:g")
.s("http").o("g").z()
}
static void ip() {
header("IP addresses");
.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:10%12]:80/index.html")
.s("http").h("[FEDC:BA98:7654:3210:FEDC:BA98:7654:10%12]")
.n(80).p("/index.html").z();
test("http://[1080:0:0:0:8:800:200C:417A]/index.html")
test("http://[1080:0:0:0:8:800:200C:417A%1]/index.html")
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")
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://[0:0:0:0:0:ffff:1.2.3.4]")
.s("http").h("[0:0:0:0:0:ffff:1.2.3.4]").p("").z();
test("http://[::13.1.68.3]")
.s("http").h("[::13.1.68.3]").p("").z();
// Optional IPv6 brackets in constructors
.s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
.s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
.s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
.s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
.s("s").h("[1:2:3:4:5:6:7:8%hme0]").p("").z();
.s("s").h("[1:2:3:4:5:6:7:8%1]").p("").z();
.s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
.s("s").h("[1:2:3:4:5:6:7:8]").p("").z();
.s("s").g("1:2:3:4:5:6:7:8").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://[fffff::1]").x().z();
test("http://[ff::ee::8]").x().z();
test("http://[1:2:3:4::5:6:7:8]").x().z();
test("http://[1:2]").x().z();
test("http://[1:2:3:4:5:6:7:8:9]").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.300]").x().z();
test("http://[1.2.3.4:5]").x().z();
test("http://[1.2.3.4]/").x().z();
test("http://[1.2.3.4/").x().z();
test("http://[foo]/").x().z();
test("http://[foo/").x().z();
// Test hostnames that might initially look like IPv4 addresses
}
header("Corner cases");
// The empty URI parses as a relative URI with an empty path
// Resolving solo queries and fragments
// Fragment is not part of ssp
test("p#f")
header("Emptiness");
// Components that may be empty
// Components that may not be empty
header("Resolution, normalization, and relativization");
// Resolving relative paths
// Normalization
.norm().p("a/d/f").z();
.s("http").h("a").p("/./b/c/../d").q("q").f("f").z()
norm().p("b");
.norm().p("./b:c").z();
// Normalization of already normalized URI should yield the
// same URI
// Relativization
test("http://a/b/d/e?q#f")
.s("http").h("a").p("/b/d/e").q("q").f("f").z()
.p("d/e").q("q").f("f").z();
// parseServerAuthority
test("s://u@h:1/p")
.psa().x().z();
header("Constructors and factories");
.s("s").u("u").h("h").n(10).p("/p").q("q").f("f").z();
.s("s").g("a:b").p("/p").q("q").f("f").z();
.s("s").h("h").p("/p").f("f").z();
testCreate("s://u@h/p?q#f")
.s("s").u("u").h("h").p("/p").q("q").f("f").z();
}
header("NullPointerException");
try {
throw new RuntimeException("NullPointerException not thrown");
} catch (NullPointerException x) {
}
try {
throw new RuntimeException("NullPointerException not thrown");
} catch (NullPointerException x) {
}
try {
throw new RuntimeException("NullPointerException not thrown");
} catch (NullPointerException x) {
}
testCount += 3;
}
header("Escapes and non-US-ASCII characters");
// Escape pairs
test("%0a%0A%0f%0F%01%09zz")
.p("%0a%0A%0f%0F%01%09zz").z();
test("foo%1").x().z();
test("foo%z").x().z();
test("foo%9z").x().z();
// Escapes not permitted in scheme, host
test("s%20t://a").x().z();
// Escapes permitted in opaque part, userInfo, registry, path,
// query, and fragment
// Non-US-ASCII chars
test("s\u00a7t://a").x().z();
// 4648111 - Escapes quoted by toString after resolution
test("/p%20p")
// 4464135: Forbid unwise characters throughout opaque part
test("foo:x{bar").x().z();
test("foo:{bar").x().z();
// 4438319: Single-argument constructor requires quotation,
// preserves escapes
test("//u%01@h/a/b/%02/c?q%03#f%04")
.h("h")
.z();
test("/a/b c").x().z();
// 4438319: Multi-argument constructors quote illegal chars and
// preserve legal non-ASCII chars
// \uA001-\uA009 are visible characters, \u2000 is a space character
"/p% \uA002\2\u2000",
"q% \uA003\3\u2000",
"f% \uA004\4\u2000")
.u("u\uA001%01").h("h")
"/p% \uA002\2\u2000",
"q% \uA003\3\u2000",
"f% \uA004\4\u2000")
.g("g\uA001%01")
// 4438319: Non-raw accessors decode all escaped octets
test("/%25%20%E2%82%AC%E2%80%80")
// 4438319: toASCIIString
test("/\uCAFE\uBABE")
// 4991359 and 4866303: bad quoting by defineSchemeSpecificPart()
test ("resolve")
// 6773270: java.net.URI fails to escape u0000
.s("s").p("/%00").h("a")
.ta("s://a/%00").z();
}
testCount++;
if (!u.equals(v))
throw new RuntimeException("Hash codes not equal: "
}
throws URISyntaxException
{
int c = u.compareTo(v);
if ((c == 0) != same)
+ " " + c);
}
eq0(u, v);
cmp0(u, v, true);
}
testCount++;
if (u != v)
}
testCount++;
if (u.equals(v))
+ "]");
}
ne0(u, v);
cmp0(u, v, false);
}
ne0(u, v);
int c = u.compareTo(v);
if (c >= 0) {
+ " " + c);
}
}
}
lt(v, u);
}
header("Equality, hashing, and comparison");
eq(o, o);
lt(o, r);
lt(s, o);
lt(s, r);
}
oo.writeObject(u);
try {
eq(u, (Comparable)o);
} catch (ClassNotFoundException x) {
x.printStackTrace();
throw new RuntimeException(x.toString());
}
testCount++;
}
header("Serialization");
}
header("URLs");
boolean caught = false;
try {
} catch (MalformedURLException x) {
throw new RuntimeException(x.toString());
}
try {
} catch (IllegalArgumentException x) {
caught = true;
} catch (MalformedURLException x) {
caught = true;
throw new RuntimeException("Incorrect exception: " + x);
}
if (!caught)
caught = false;
try {
} catch (MalformedURLException x) {
caught = true;
} catch (IllegalArgumentException x) {
caught = true;
throw new RuntimeException("Incorrect exception: " + x);
}
if (!caught)
testCount += 3;
}
rfc2396();
ip();
misc();
chars();
eqHashComp();
serial();
urls();
npes();
bugs();
}
// -- Command-line invocation --
static void usage() {
}
try {
show(b);
}
show(u);
show(r);
}
} catch (URISyntaxException x) {
show("ERROR", x);
x.printStackTrace(out);
}
}
static void bugs() {
// 6339649 - include detail message from nested exception
try {
} catch (IllegalArgumentException e) {
throw new RuntimeException ("No detail message");
}
}
}
case 0:
tests();
break;
case 1:
usage();
break;
}
break;
case 2:
break;
default:
usage();
break;
}
}
}