jdk/test/java/net/ipv6tests/Tests.java
author mikael
Fri, 04 Dec 2015 15:08:49 -0800
changeset 35090 1f5b6aa795d0
parent 25975 80a1b7aa96d8
child 41887 61839a1a7e62
permissions -rw-r--r--
8144748: Move assembler/macroAssembler inline function definitions to corresponding inline.hpp files Reviewed-by: kvn, coleenp
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: 6115
diff changeset
     2
 * Copyright (c) 2003, 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: 5151
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5151
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5151
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
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
public class Tests {
25975
80a1b7aa96d8 8054118: java/net/ipv6tests/UdpTest.java failed intermittently
msheppar
parents: 7668
diff changeset
    29
80a1b7aa96d8 8054118: java/net/ipv6tests/UdpTest.java failed intermittently
msheppar
parents: 7668
diff changeset
    30
    static boolean isWindows = System.getProperty("os.name").startsWith("Windows");
80a1b7aa96d8 8054118: java/net/ipv6tests/UdpTest.java failed intermittently
msheppar
parents: 7668
diff changeset
    31
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
     * performs a simple exchange of data between the two sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
     * and throws an exception if there is any problem.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public static void simpleDataExchange (Socket s1, Socket s2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        InputStream i1 = s1.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        InputStream i2 = s2.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        OutputStream o1 = s1.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        OutputStream o2 = s2.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
5151
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
    44
        startSimpleWriter("SimpleWriter-1", o1, 100);
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
    45
        startSimpleWriter("SimpleWriter-2", o2, 200);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        simpleRead (i2, 100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        simpleRead (i1, 200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
5151
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
    50
    static void startSimpleWriter(String threadName, final OutputStream os, final int start) {
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
    51
        (new Thread(new Runnable() {
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
    52
            public void run() {
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
    53
                try { simpleWrite(os, start); }
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
    54
                catch (Exception e) {unexpected(e); }
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
    55
            }}, threadName)).start();
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
    56
    }
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
    57
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
    58
    static void unexpected(Exception e ) {
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
    59
        System.out.println("Unexcepted Exception: " + e);
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
    60
        e.printStackTrace();
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
    61
    }
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
    62
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Send a packet from s1 to s2 (ia2/s2.localPort) and check it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Send a packet from s2 to s1 (ia1/s1.localPort) and check it
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static void simpleDataExchange (DatagramSocket s1, InetAddress ia1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                           DatagramSocket s2, InetAddress ia2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        SocketAddress dest1 = new InetSocketAddress (ia1, s1.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        dprintln ("dest1 = " + dest1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        SocketAddress dest2 = new InetSocketAddress (ia2, s2.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        dprintln ("dest2 = " + dest2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        byte[] ba = "Hello world".getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        byte[] bb = "HELLO WORLD1".getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        DatagramPacket p1 = new DatagramPacket (ba, ba.length, dest1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        DatagramPacket p2 = new DatagramPacket (ba, ba.length, dest2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        DatagramPacket r1 = new DatagramPacket (new byte[256], 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        DatagramPacket r2 = new DatagramPacket (new byte[256], 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        s2.send (p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        s1.send (p2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        s1.receive (r1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        s2.receive (r2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        comparePackets (p1, r1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        comparePackets (p2, r2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Send a packet from s1 to s2 (ia2/s2.localPort) and send same packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * back from s2 to sender. Check s1 receives original packet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static void datagramEcho (DatagramSocket s1, DatagramSocket s2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                     InetAddress ia2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        byte[] ba = "Hello world".getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        DatagramPacket p1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        SocketAddress dest2 = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (ia2 != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            dest2 = new InetSocketAddress (ia2, s2.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            p1 = new DatagramPacket (ba, ba.length, dest2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            p1 = new DatagramPacket (ba, ba.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        dprintln ("dest2 = " + dest2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        DatagramPacket r1 = new DatagramPacket (new byte[256], 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        DatagramPacket r2 = new DatagramPacket (new byte[256], 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        s1.send (p1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        s2.receive (r1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        s2.send (r1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        s1.receive (r2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        comparePackets (p1, r1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        comparePackets (p1, r2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public static void comparePackets (DatagramPacket p1, DatagramPacket p2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        byte[] b1 = p1.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        byte[] b2 = p2.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        int len = p1.getLength () > p2.getLength() ? p2.getLength()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                                   : p1.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        for (int i=0; i<len; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (b1[i] != b2[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                throw new Exception ("packets not the same");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5981
diff changeset
   140
    /* check the time got is within 50% of the time expected */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public static void checkTime (long got, long expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        dprintln ("checkTime: got " + got + " expected " + expected);
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5981
diff changeset
   143
        long upper = expected + (expected / 2);
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5981
diff changeset
   144
        long lower = expected - (expected / 2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (got > upper || got < lower) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throw new RuntimeException ("checkTime failed: got " + got + " expected " + expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public static void checkDebug (String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        debug = args.length > 0 && args[0].equals("-d");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    public static void dprint (String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            System.out.print (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public static void dprintln (String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            System.out.println (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    static int numberInterfaces () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            Enumeration ifs = NetworkInterface.getNetworkInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            int nifs=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            while (ifs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                nifs++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                ifs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            return nifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        } catch (SocketException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public static Enumeration ipv4Addresses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return new AddrEnum (Inet4Address.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public static Inet4Address getFirstLocalIPv4Address () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        Enumeration e = ipv4Addresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (!e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        return (Inet4Address)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    public static Inet6Address getFirstLocalIPv6Address () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        Enumeration e = ipv6Addresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (!e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        return (Inet6Address)e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public static Enumeration ipv6Addresses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return new AddrEnum (Inet6Address.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /* enumerates the Inet4Addresses or Inet6Addresses on the system */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    private static class AddrEnum implements Enumeration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        Enumeration ifs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        NetworkInterface currIf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        InetAddress nextAddr=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        Enumeration addrs=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        Class filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        static final byte[] fe80_loopback = new byte [] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            (byte)0xfe,(byte)0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        AddrEnum (Class filter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            this.filter = filter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                ifs = NetworkInterface.getNetworkInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            } catch (SocketException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        public boolean hasMoreElements () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if (nextAddr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                nextAddr = getNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return (nextAddr != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        public Object nextElement () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            if (!hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                throw new NoSuchElementException ("no more addresses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            Object next = nextAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            nextAddr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            return next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        private InetAddress getNext() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                if (currIf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                    currIf = getNextIf();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    if (currIf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    addrs = currIf.getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                while (addrs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    InetAddress addr = (InetAddress) addrs.nextElement();
5981
2d5303114f44 6961029: java/net/BindException/Test.java should not use wildcard address
chegar
parents: 5506
diff changeset
   254
                    if (filter.isInstance (addr) && !addr.isLoopbackAddress()
2d5303114f44 6961029: java/net/BindException/Test.java should not use wildcard address
chegar
parents: 5506
diff changeset
   255
                            && !addr.isAnyLocalAddress()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                        if (Arrays.equals (addr.getAddress(), fe80_loopback)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                        return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                currIf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        private NetworkInterface getNextIf () {
5151
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
   267
            if (ifs != null) {
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
   268
                while (ifs.hasMoreElements()) {
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
   269
                    NetworkInterface nic = (NetworkInterface)ifs.nextElement();
25975
80a1b7aa96d8 8054118: java/net/ipv6tests/UdpTest.java failed intermittently
msheppar
parents: 7668
diff changeset
   270
                    // Skip (Windows)Teredo Tunneling Pseudo-Interface
80a1b7aa96d8 8054118: java/net/ipv6tests/UdpTest.java failed intermittently
msheppar
parents: 7668
diff changeset
   271
                    if (isWindows) {
80a1b7aa96d8 8054118: java/net/ipv6tests/UdpTest.java failed intermittently
msheppar
parents: 7668
diff changeset
   272
                        String dName = nic.getDisplayName();
80a1b7aa96d8 8054118: java/net/ipv6tests/UdpTest.java failed intermittently
msheppar
parents: 7668
diff changeset
   273
                        if (dName != null && dName.contains("Teredo"))
80a1b7aa96d8 8054118: java/net/ipv6tests/UdpTest.java failed intermittently
msheppar
parents: 7668
diff changeset
   274
                            continue;
80a1b7aa96d8 8054118: java/net/ipv6tests/UdpTest.java failed intermittently
msheppar
parents: 7668
diff changeset
   275
                    }
5151
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
   276
                    try {
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
   277
                        if (nic.isUp() && !nic.isLoopback())
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
   278
                            return nic;
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
   279
                    } catch (SocketException e) {
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
   280
                        // ignore
e62028f44cf6 6934923: test/java/net/ipv6tests/TcpTest.java hangs on Solaris 10
chegar
parents: 2
diff changeset
   281
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * Throws a RuntimeException if the boolean condition is false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public static void t_assert (boolean assertion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (assertion) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        Throwable t = new Throwable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        StackTraceElement[] strace = t.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        String msg = "Assertion failed at: " + strace[1].toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        throw new RuntimeException (msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    private static void simpleRead (InputStream is, int start) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        byte b[] = new byte [2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        for (int i=start; i<start+100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            int x = is.read (b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            if (x == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                x += is.read (b,1,1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            if (x!=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                throw new Exception ("read error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            int r = bytes (b[0], b[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            if (r != i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                throw new Exception ("read " + r + " expected " +i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /* convert signed bytes to unisigned int */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private static int bytes (byte b1, byte b2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        int i1 = (int)b1 & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        int i2 = (int)b2 & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return i1 * 256 + i2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    static void simpleWrite (OutputStream os, int start) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        byte b[] = new byte [2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        for (int i=start; i<start+100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            b[0] = (byte) (i / 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            b[1] = (byte) (i % 256);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            os.write (b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private static class Runner extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        Runnable runnee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        long delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        Runner (Runnable runnee, long delay) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            this.runnee = runnee;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            this.delay = delay;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                Thread.sleep (delay);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                runnee.run ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Take the given Runnable and run it in a spawned thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * after the given time has elapsed. runAfter() returns immediately
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    public static void runAfter (long millis, Runnable runnee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        Runner runner = new Runner (runnee, millis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        runner.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    static String osname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        osname = System.getProperty ("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    static boolean isLinux () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return osname.equals ("Linux");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    static boolean isWindows () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        return osname.startsWith ("Windows");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
}