jdk/test/java/net/ipv6tests/ScopeTests.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class ScopeTests extends Tests {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
        checkDebug (args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        simpleTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        complexTests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        System.out.println ("Tests passed: OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static void sassert (boolean condition, String msg) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        if (!condition) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            throw new Exception (msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static void checkAddress (String a, int numeric) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        Inet6Address addr = (Inet6Address) InetAddress.getByName (a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        if (addr.getScopeId () != numeric) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            throw new Exception ("wroing numeric scopeid");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static void checkAddress (String a, String str) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        Inet6Address addr = (Inet6Address) InetAddress.getByName (a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (!addr.getScopedInterface().getName().equals (str)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            throw new Exception ("wroing scoped interface name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /* These tests check generic API functionality that is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * dependent on scoping being implemented in the platform
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static void simpleTests () throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        checkAddress ("fe80::%1", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        checkAddress ("fe80::1%1", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        checkAddress ("fec0::1:a00:20ff:feed:b08d%0", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        checkAddress ("fec0::1:a00:20ff:feed:b08d%1", 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        checkAddress ("fec0::1:a00:20ff:feed:b08d%99", 99);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        checkAddress ("fe80::", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        checkAddress ("fec0::1:a00:20ff:feed:b08d", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        checkAddress ("fec0::1:a00:20ff:feed:b08d", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        checkAddress ("fec0::1:a00:20ff:feed:b08d", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /* These tests check the NetworkInterfaces for the actual scopeids
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * configured on the system and do tests using that information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    static void complexTests () throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        dprintln ("ComplexTests");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        Enumeration e = NetworkInterface.getNetworkInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            NetworkInterface nif = (NetworkInterface)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            String name = nif.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            Enumeration addrs = nif.getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            while (addrs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                InetAddress addr = (InetAddress) addrs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                dprintln ("ComplexTests: "+addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                if (addr instanceof Inet6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    Inet6Address ia6 = (Inet6Address) addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    if (ia6.getScopeId() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        System.out.println ("Testing interface: " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                            " and address: " + ia6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                        ctest1 (name, ia6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        ctest2 (name, ia6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                        System.out.println ("Interface: " + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                            " Address: "+ ia6 +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                                            " does not support scope");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
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
   /* check the scoped name on the Inet6Address is the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    * the interface it is attached to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static void ctest1 (String ifname, Inet6Address ia6) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        System.out.println ("ctest1:" + ia6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        String s = ia6.getScopedInterface().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        int scope = ia6.getScopeId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        sassert (ifname.equals (s), "ctest1:"+ifname+":"+s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /* create an Inet6Adddress by all three methods using the ifname
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * compare the numeric scope id with that of the Inet6Address passed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    static void ctest2 (String ifname, Inet6Address ia6) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        System.out.println ("ctest2:" + ia6);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        String s = ia6.getScopedInterface().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        int scope = ia6.getScopeId();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        String s1 = ia6.getHostAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (s1.indexOf('%') != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            s1 = s1.substring (0, s1.indexOf ('%'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        Inet6Address add = (Inet6Address) InetAddress.getByName (s1+"%"+ifname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        sassert (add.getScopeId() == scope, "ctest2:1:" +scope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
}