jdk/test/java/net/URL/TestIPv6Addresses.java
author amurillo
Wed, 15 Aug 2012 16:49:38 -0700
changeset 13465 d3fc5d192448
parent 7668 d4a77089c587
permissions -rw-r--r--
7191765: make jdk8 the default jprt release for hs24 Reviewed-by: jcoomes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 5506
diff changeset
     2
 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5165
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5165
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5165
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4451522 4460484
5165
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    26
 * @run main/othervm TestIPv6Addresses
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary URI and URL getHost() methods don't comform to RFC 2732
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
5165
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    29
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    30
// Run in othervm because the tests sets a SecurityManager
5693cc1e95c6 6937703: java/net regression test issues with samevm
chegar
parents: 2
diff changeset
    31
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class TestIPv6Addresses {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        // testing InetAddress static constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        InetAddress ia1 = InetAddress.getByName("fe80::a00:20ff:feae:45c9");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        InetAddress ia2 = InetAddress.getByName("[fe80::a00:20ff:feae:45c9]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        System.out.println("InetAddress: "+ia1+" , "+ia2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        if (!ia1.equals(ia2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            throw new RuntimeException("InetAddress.getByName failed for"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
                                       "literal IPv6 addresses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        // testing URL constructor, getHost and getAuthority
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        URL u1 = new URL("http", "fe80::a00:20ff:feae:45c9", 80, "/index.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        URL u2 = new URL("http", "[fe80::a00:20ff:feae:45c9]", 80, "/index.html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        if (!u1.equals(u2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            throw new RuntimeException("URL constructor failed for"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                                       "literal IPv6 addresses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (!u1.getHost().equals(u2.getHost()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            !u1.getHost().equals("[fe80::a00:20ff:feae:45c9]")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            throw new RuntimeException("URL.getHost() failed for"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                                       "literal IPv6 addresses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (!u1.getAuthority().equals("[fe80::a00:20ff:feae:45c9]:80")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            throw new RuntimeException("URL.getAuthority() failed for"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                       "literal IPv6 addresses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        // need to test URI as well
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        // testing SocketPermission to see whether it handles unambiguous cases
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // testing getIP and getHost etc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        SocketPermission sp1 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            new SocketPermission(u1.getHost()+":80-", "resolve");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        SocketPermission sp2 =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            new SocketPermission(ia1.getHostAddress()+":8080", "resolve");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        if (!sp1.implies(sp2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            throw new RuntimeException("SocketPermission implies doesn't work"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                       " for literal IPv6 addresses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new RuntimeException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // bug 4460484
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        SecurityManager sm = new SecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        String strAddr = "::FFFF:127.0.0.1.2";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            InetAddress addr = InetAddress.getByName(strAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            // expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        System.setSecurityManager(sm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            InetAddress addr = InetAddress.getByName(strAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } catch (java.security.AccessControlException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            // expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } catch (UnknownHostException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            // expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
}