blob: 44f9d0283c636903a4ee9c2e666b1ef8dba257e6 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
2 * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
3 * 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 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 * @bug 4451522 4460484
chegarf514c562010-03-25 09:38:56 +000026 * @run main/othervm TestIPv6Addresses
duke6e45e102007-12-01 00:00:00 +000027 * @summary URI and URL getHost() methods don't comform to RFC 2732
28 */
chegarf514c562010-03-25 09:38:56 +000029
30// Run in othervm because the tests sets a SecurityManager
31
duke6e45e102007-12-01 00:00:00 +000032import java.net.*;
33
34public class TestIPv6Addresses {
35 public static void main(String[] args) {
36 try {
37 // testing InetAddress static constructors
38 InetAddress ia1 = InetAddress.getByName("fe80::a00:20ff:feae:45c9");
39 InetAddress ia2 = InetAddress.getByName("[fe80::a00:20ff:feae:45c9]");
40 System.out.println("InetAddress: "+ia1+" , "+ia2);
41 if (!ia1.equals(ia2)) {
42 throw new RuntimeException("InetAddress.getByName failed for"+
43 "literal IPv6 addresses");
44 }
45 // testing URL constructor, getHost and getAuthority
46 URL u1 = new URL("http", "fe80::a00:20ff:feae:45c9", 80, "/index.html");
47 URL u2 = new URL("http", "[fe80::a00:20ff:feae:45c9]", 80, "/index.html");
48 if (!u1.equals(u2)) {
49 throw new RuntimeException("URL constructor failed for"+
50 "literal IPv6 addresses");
51 }
52 if (!u1.getHost().equals(u2.getHost()) ||
53 !u1.getHost().equals("[fe80::a00:20ff:feae:45c9]")) {
54 throw new RuntimeException("URL.getHost() failed for"+
55 "literal IPv6 addresses");
56 }
57 if (!u1.getAuthority().equals("[fe80::a00:20ff:feae:45c9]:80")) {
58 throw new RuntimeException("URL.getAuthority() failed for"+
59 "literal IPv6 addresses");
60 }
61
62 // need to test URI as well
63
64 // testing SocketPermission to see whether it handles unambiguous cases
65 // testing getIP and getHost etc
66 SocketPermission sp1 =
67 new SocketPermission(u1.getHost()+":80-", "resolve");
68 SocketPermission sp2 =
69 new SocketPermission(ia1.getHostAddress()+":8080", "resolve");
70
71 if (!sp1.implies(sp2)) {
72 throw new RuntimeException("SocketPermission implies doesn't work"+
73 " for literal IPv6 addresses");
74 }
75 } catch (Exception e) {
76 throw new RuntimeException(e.getMessage());
77 }
78 // bug 4460484
79
80 SecurityManager sm = new SecurityManager();
81 String strAddr = "::FFFF:127.0.0.1.2";
82
83 try {
84 InetAddress addr = InetAddress.getByName(strAddr);
85 } catch (UnknownHostException e) {
86 // expected
87 }
88 System.setSecurityManager(sm);
89
90 try {
91 InetAddress addr = InetAddress.getByName(strAddr);
92 } catch (java.security.AccessControlException e) {
93 // expected
94 } catch (UnknownHostException e) {
95 // expected
96 }
97
98 }
99}