Path: blob/master/test/jdk/java/net/ipv6tests/UdpTest.java
41152 views
/*1* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 486882026* @key intermittent27* @summary IPv6 support for Windows XP and 2003 server.28* This test requires binding to the wildcard address and as such29* may fail intermittently on some platforms.30* @library /test/lib31* @build jdk.test.lib.NetworkConfiguration32* jdk.test.lib.Platform33* @run main UdpTest -d34*/3536import java.net.DatagramPacket;37import java.net.DatagramSocket;38import java.net.Inet4Address;39import java.net.Inet6Address;40import java.net.InetAddress;41import java.net.PortUnreachableException;42import java.net.SocketTimeoutException;4344public class UdpTest extends Tests {45static DatagramSocket c3, s1, s2, s3;46static InetAddress s1peer, s2peer;4748static InetAddress ia4any;49static InetAddress ia6any;50static Inet6Address ia6addr;51static InetAddress ia6bad; /* a global 6to4 IPv6 address, which cant be connected to */52static InetAddress ia6rem1;53static Inet4Address ia4addr;5455static {56try {57ia4any = InetAddress.getByName ("0.0.0.0");58ia6any = InetAddress.getByName ("::0");59try {60ia6bad = InetAddress.getByName ("2002:819c:dc29:1:1322:33ff:fe44:5566%net0");61} catch (Exception e) {62ia6bad = InetAddress.getByName ("2002:819c:dc29:1:1322:33ff:fe44:5566");63}64//ia6rem1 = InetAddress.getByName ("fe80::a00:20ff:feed:b08d%eth0");65//ia6rem1 = InetAddress.getByName ("129.156.220.63");66} catch (Exception e) {67e.printStackTrace();68}69ia6addr = getFirstLocalIPv6Address ();70ia4addr = getFirstLocalIPv4Address ();71}7273public static void main (String[] args) throws Exception {74checkDebug(args);75if (ia4addr == null) {76System.out.println ("No local IPv4 addresses: exiting now");77return;78}79if (ia6addr == null) {80System.out.println ("No local IPv6 addresses: exiting now");81return;82}83dprintln ("Local Addresses");84dprintln (ia4addr.toString());85dprintln (ia6addr.toString());86test1 ();87test2 ();88if (!isLinux()) {89test3 ();90}91test4 ();92}9394/* basic UDP connectivity test using IPv6 only and IPv4/IPv6 together */9596static void test1 () throws Exception {97System.out.println("Test1 starting");98s1 = new DatagramSocket ();99s2 = new DatagramSocket ();100simpleDataExchange (s1, ia4addr, s2, ia4addr);101s1.close (); s2.close ();102103/* IPv6 */104s1 = new DatagramSocket ();105s2 = new DatagramSocket ();106simpleDataExchange (s1, ia6addr, s2, ia6addr);107s1.close (); s2.close ();108109/* IPv6 only */110s1 = new DatagramSocket (0, ia6addr);111s2 = new DatagramSocket (0, ia6addr);112simpleDataExchange (s1, ia6addr, s2, ia6addr);113s1.close (); s2.close ();114115/* IPv6 and IPv4 */116s1 = new DatagramSocket ();117s2 = new DatagramSocket ();118simpleDataExchange (s1, ia6addr, s2, ia4addr);119s1.close (); s2.close ();120121/* listen on anyaddr and check receive from IPv4 and IPv6 */122123s1 = new DatagramSocket ();124s2 = new DatagramSocket (0, ia6addr);125s3 = new DatagramSocket (0, ia4addr);126datagramEcho (s2, s1, ia6addr);127datagramEcho (s3, s1, ia4addr);128s1.close (); s2.close (); s3.close();129130System.out.println ("Test1: OK");131}132133/* check timeouts on receive */134135static void test2 () throws Exception {136System.out.println("Test2 starting");137s1 = new DatagramSocket ();138s2 = new DatagramSocket ();139s1.setSoTimeout (4000);140long t1 = System.currentTimeMillis();141try {142s1.receive (new DatagramPacket (new byte [128], 128));143throw new Exception ("expected receive timeout ");144} catch (SocketTimeoutException e) {145}146checkTime (System.currentTimeMillis() - t1, 4000);147148/* check data can be exchanged now */149150simpleDataExchange (s1, ia6addr, s2, ia4addr);151152/* double check timeout still works */153t1 = System.currentTimeMillis();154try {155s1.receive (new DatagramPacket (new byte [128], 128));156throw new Exception ("expected receive timeout ");157} catch (SocketTimeoutException e) {158}159checkTime (System.currentTimeMillis() - t1, 4000);160161/* check receive works after a delay < timeout */162163final DatagramSocket s = s2;164final InetAddress ia6 = ia6addr;165final int port = s1.getLocalPort();166167s1.setSoTimeout(10000);168runAfter (2000, new Runnable () {169public void run () {170try {171DatagramPacket p = new DatagramPacket ("Hello 123".getBytes(), 0, 8, ia6, port);172s.send (p);173} catch (Exception e) {}174}175});176t1 = System.currentTimeMillis();177s1.receive (new DatagramPacket (new byte [128], 128));178checkTime (System.currentTimeMillis() - t1, 2000, 10000);179s1.close ();180s2.close ();181System.out.println ("Test2: OK");182}183184/* check connected sockets */185186static void test3 () throws Exception {187System.out.println("Test3 starting");188s1 = new DatagramSocket ();189s2 = new DatagramSocket ();190s1.connect (ia6addr, s2.getLocalPort());191datagramEcho (s1, s2, null);192s1.close (); s2.close();193System.out.println ("Test3: OK");194}195196/* check PortUnreachable */197198static void test4 () throws Exception {199System.out.println("Test4 starting");200s1 = new DatagramSocket ();201s1.connect (ia6addr, 5000);202s1.setSoTimeout (3000);203try {204DatagramPacket p = new DatagramPacket ("HelloWorld".getBytes(), "HelloWorld".length());205s1.send (p);206p = new DatagramPacket (new byte[128], 128);207s1.receive (p);208} catch (PortUnreachableException e) {209System.out.println ("Test4: OK");210return;211} catch (SocketTimeoutException e) {212System.out.println ("Test4: failed. Never mind, it's an OS bug");213}214}215216}217218219