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