test/jdk/java/net/ipv6tests/ScopeTests.java
author xiaofeya
Tue, 17 Apr 2018 23:27:41 -0700
changeset 49799 33dcb9c42f55
parent 47216 71c04702a3d5
permissions -rw-r--r--
8194260: Point-to-point interface should be excluded from java/net/ipv6tests/* Reviewed-by: chegar, vtewari
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49799
33dcb9c42f55 8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 2018, 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: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4868820
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary IPv6 support for Windows XP and 2003 server
49799
33dcb9c42f55 8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents: 47216
diff changeset
    28
 * @library /test/lib
33dcb9c42f55 8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents: 47216
diff changeset
    29
 * @build jdk.test.lib.NetworkConfiguration
33dcb9c42f55 8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents: 47216
diff changeset
    30
 *        jdk.test.lib.Platform
33dcb9c42f55 8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents: 47216
diff changeset
    31
 * @run main ScopeTests
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class ScopeTests extends Tests {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        checkDebug (args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        simpleTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        complexTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        System.out.println ("Tests passed: OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static void sassert (boolean condition, String msg) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        if (!condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            throw new Exception (msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    static void checkAddress (String a, int numeric) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        Inet6Address addr = (Inet6Address) InetAddress.getByName (a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (addr.getScopeId () != numeric) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            throw new Exception ("wroing numeric scopeid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    static void checkAddress (String a, String str) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        Inet6Address addr = (Inet6Address) InetAddress.getByName (a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (!addr.getScopedInterface().getName().equals (str)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            throw new Exception ("wroing scoped interface name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /* These tests check generic API functionality that is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * dependent on scoping being implemented in the platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static void simpleTests () throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        checkAddress ("fe80::%1", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        checkAddress ("fe80::1%1", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        checkAddress ("fec0::1:a00:20ff:feed:b08d%0", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        checkAddress ("fec0::1:a00:20ff:feed:b08d%1", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        checkAddress ("fec0::1:a00:20ff:feed:b08d%99", 99);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        checkAddress ("fe80::", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        checkAddress ("fec0::1:a00:20ff:feed:b08d", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        checkAddress ("fec0::1:a00:20ff:feed:b08d", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        checkAddress ("fec0::1:a00:20ff:feed:b08d", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /* These tests check the NetworkInterfaces for the actual scopeids
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * configured on the system and do tests using that information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static void complexTests () throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        dprintln ("ComplexTests");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        Enumeration e = NetworkInterface.getNetworkInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            NetworkInterface nif = (NetworkInterface)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            String name = nif.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            Enumeration addrs = nif.getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            while (addrs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                InetAddress addr = (InetAddress) addrs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                dprintln ("ComplexTests: "+addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                if (addr instanceof Inet6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    Inet6Address ia6 = (Inet6Address) addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    if (ia6.getScopeId() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        System.out.println ("Testing interface: " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                            " and address: " + ia6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                        ctest1 (name, ia6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                        ctest2 (name, ia6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                        System.out.println ("Interface: " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                                            " Address: "+ ia6 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                                            " does not support scope");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
   /* check the scoped name on the Inet6Address is the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    * the interface it is attached to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    static void ctest1 (String ifname, Inet6Address ia6) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        System.out.println ("ctest1:" + ia6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        String s = ia6.getScopedInterface().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        int scope = ia6.getScopeId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        sassert (ifname.equals (s), "ctest1:"+ifname+":"+s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /* create an Inet6Adddress by all three methods using the ifname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * compare the numeric scope id with that of the Inet6Address passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    static void ctest2 (String ifname, Inet6Address ia6) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        System.out.println ("ctest2:" + ia6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        String s = ia6.getScopedInterface().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        int scope = ia6.getScopeId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        String s1 = ia6.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (s1.indexOf('%') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            s1 = s1.substring (0, s1.indexOf ('%'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Inet6Address add = (Inet6Address) InetAddress.getByName (s1+"%"+ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        sassert (add.getScopeId() == scope, "ctest2:1:" +scope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
}