test/jdk/java/net/ipv6tests/UdpTest.java
author xiaofeya
Tue, 17 Apr 2018 23:27:41 -0700
changeset 49799 33dcb9c42f55
parent 47216 71c04702a3d5
child 54903 5a211ee83bf1
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 UdpTest
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
49799
33dcb9c42f55 8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents: 47216
diff changeset
    34
import java.net.DatagramPacket;
33dcb9c42f55 8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents: 47216
diff changeset
    35
import java.net.DatagramSocket;
33dcb9c42f55 8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents: 47216
diff changeset
    36
import java.net.Inet4Address;
33dcb9c42f55 8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents: 47216
diff changeset
    37
import java.net.Inet6Address;
33dcb9c42f55 8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents: 47216
diff changeset
    38
import java.net.InetAddress;
33dcb9c42f55 8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents: 47216
diff changeset
    39
import java.net.PortUnreachableException;
33dcb9c42f55 8194260: Point-to-point interface should be excluded from java/net/ipv6tests/*
xiaofeya
parents: 47216
diff changeset
    40
import java.net.SocketTimeoutException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public class UdpTest extends Tests {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static DatagramSocket c3, s1, s2, s3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static InetAddress s1peer, s2peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static InetAddress ia4any;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static InetAddress ia6any;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static Inet6Address ia6addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static InetAddress ia6bad; /* a global 6to4 IPv6 address, which cant be connected to */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static InetAddress ia6rem1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static Inet4Address ia4addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            ia4any = InetAddress.getByName ("0.0.0.0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            ia6any = InetAddress.getByName ("::0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                ia6bad = InetAddress.getByName ("2002:819c:dc29:1:1322:33ff:fe44:5566%net0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                ia6bad = InetAddress.getByName ("2002:819c:dc29:1:1322:33ff:fe44:5566");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            //ia6rem1 = InetAddress.getByName ("fe80::a00:20ff:feed:b08d%eth0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            //ia6rem1 = InetAddress.getByName ("129.156.220.63");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        ia6addr = getFirstLocalIPv6Address ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        ia4addr = getFirstLocalIPv4Address ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        checkDebug(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        if (ia6addr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            System.out.println ("No local IPv6 addresses: exiting now");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        dprintln ("Local Addresses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        dprintln (ia4addr.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        dprintln (ia6addr.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        test1 ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        test2 ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (!isLinux()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            test3 ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        test4 ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /* basic UDP connectivity test using IPv6 only and IPv4/IPv6 together */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    static void test1 () throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        s1 = new DatagramSocket ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        s2 = new DatagramSocket ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        simpleDataExchange (s1, ia4addr, s2, ia4addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        s1.close (); s2.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        /* IPv6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        s1 = new DatagramSocket ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        s2 = new DatagramSocket ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        simpleDataExchange (s1, ia6addr, s2, ia6addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        s1.close (); s2.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        /* IPv6 only */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        s1 = new DatagramSocket (0, ia6addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        s2 = new DatagramSocket (0, ia6addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        simpleDataExchange (s1, ia6addr, s2, ia6addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        s1.close (); s2.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        /* IPv6 and IPv4 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        s1 = new DatagramSocket ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        s2 = new DatagramSocket ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        simpleDataExchange (s1, ia6addr, s2, ia4addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        s1.close (); s2.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        /* listen on anyaddr and check receive from IPv4 and IPv6 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        s1 = new DatagramSocket ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        s2 = new DatagramSocket (0, ia6addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        s3 = new DatagramSocket (0, ia4addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        datagramEcho (s2, s1, ia6addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        datagramEcho (s3, s1, ia4addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        s1.close (); s2.close (); s3.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        System.out.println ("Test1: OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /* check timeouts on receive */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    static void test2 () throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        s1 = new DatagramSocket ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        s2 = new DatagramSocket ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        s1.setSoTimeout (4000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        long t1 = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            s1.receive (new DatagramPacket (new byte [128], 128));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throw new Exception ("expected receive timeout ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } catch (SocketTimeoutException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        checkTime (System.currentTimeMillis() - t1, 4000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        /* check data can be exchanged now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        simpleDataExchange (s1, ia6addr, s2, ia4addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        /* double check timeout still works */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        t1 = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            s1.receive (new DatagramPacket (new byte [128], 128));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            throw new Exception ("expected receive timeout ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } catch (SocketTimeoutException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        checkTime (System.currentTimeMillis() - t1, 4000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        /* check receive works after a delay < timeout */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        final DatagramSocket s = s2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        final InetAddress ia6 = ia6addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        final int port = s1.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
22294
1e3a7c70240c 8031666: TEST_BUG: java/net/ipv6tests/UdpTest.java failed because of SocketTimeoutException
tyan
parents: 5506
diff changeset
   159
        s1.setSoTimeout(10000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        runAfter (2000, new Runnable () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            public void run () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    DatagramPacket p = new DatagramPacket ("Hello 123".getBytes(), 0, 8, ia6, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    s.send (p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                } catch (Exception e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        t1 = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        s1.receive (new DatagramPacket (new byte [128], 128));
41887
61839a1a7e62 8143097: java/net/ipv6tests/UdpTest.java fails intermittently with "checkTime failed: got 1998 expected 4000"
amlu
parents: 36238
diff changeset
   170
        checkTime (System.currentTimeMillis() - t1, 2000, 10000);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        s1.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        s2.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        System.out.println ("Test2: OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /* check connected sockets */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    static void test3 () throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        s1 = new DatagramSocket ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        s2 = new DatagramSocket ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        s1.connect (ia6addr, s2.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        datagramEcho (s1, s2, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        s1.close (); s2.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        System.out.println ("Test3: OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /* check PortUnreachable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    static void test4 () throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        s1 = new DatagramSocket ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        s1.connect (ia6addr, 5000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        s1.setSoTimeout (3000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            DatagramPacket p = new DatagramPacket ("HelloWorld".getBytes(), "HelloWorld".length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            s1.send (p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            p = new DatagramPacket (new byte[128], 128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            s1.receive (p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } catch (PortUnreachableException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            System.out.println ("Test4: OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        } catch (SocketTimeoutException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            System.out.println ("Test4: failed. Never mind, it's an OS bug");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}