Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/ipv6tests/UdpTest.java
38812 views
/*1* Copyright (c) 2003, 2016, 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* @summary IPv6 support for Windows XP and 2003 server27*/2829import java.net.*;30import java.io.*;3132public class UdpTest extends Tests {33static DatagramSocket c3, s1, s2, s3;34static InetAddress s1peer, s2peer;3536static InetAddress ia4any;37static InetAddress ia6any;38static Inet6Address ia6addr;39static InetAddress ia6bad; /* a global 6to4 IPv6 address, which cant be connected to */40static InetAddress ia6rem1;41static Inet4Address ia4addr;4243static {44try {45ia4any = InetAddress.getByName ("0.0.0.0");46ia6any = InetAddress.getByName ("::0");47try {48ia6bad = InetAddress.getByName ("2002:819c:dc29:1:1322:33ff:fe44:5566%net0");49} catch (Exception e) {50ia6bad = InetAddress.getByName ("2002:819c:dc29:1:1322:33ff:fe44:5566");51}52//ia6rem1 = InetAddress.getByName ("fe80::a00:20ff:feed:b08d%eth0");53//ia6rem1 = InetAddress.getByName ("129.156.220.63");54} catch (Exception e) {55e.printStackTrace();56}57ia6addr = getFirstLocalIPv6Address ();58ia4addr = getFirstLocalIPv4Address ();59}6061public static void main (String[] args) throws Exception {62checkDebug(args);63if (ia6addr == null) {64System.out.println ("No local IPv6 addresses: exiting now");65return;66}67dprintln ("Local Addresses");68dprintln (ia4addr.toString());69dprintln (ia6addr.toString());70test1 ();71test2 ();72if (!isLinux()) {73test3 ();74}75test4 ();76}7778/* basic UDP connectivity test using IPv6 only and IPv4/IPv6 together */7980static void test1 () throws Exception {81s1 = new DatagramSocket ();82s2 = new DatagramSocket ();83simpleDataExchange (s1, ia4addr, s2, ia4addr);84s1.close (); s2.close ();8586/* IPv6 */87s1 = new DatagramSocket ();88s2 = new DatagramSocket ();89simpleDataExchange (s1, ia6addr, s2, ia6addr);90s1.close (); s2.close ();9192/* IPv6 only */93s1 = new DatagramSocket (0, ia6addr);94s2 = new DatagramSocket (0, ia6addr);95simpleDataExchange (s1, ia6addr, s2, ia6addr);96s1.close (); s2.close ();9798/* IPv6 and IPv4 */99s1 = new DatagramSocket ();100s2 = new DatagramSocket ();101simpleDataExchange (s1, ia6addr, s2, ia4addr);102s1.close (); s2.close ();103104/* listen on anyaddr and check receive from IPv4 and IPv6 */105106s1 = new DatagramSocket ();107s2 = new DatagramSocket (0, ia6addr);108s3 = new DatagramSocket (0, ia4addr);109datagramEcho (s2, s1, ia6addr);110datagramEcho (s3, s1, ia4addr);111s1.close (); s2.close (); s3.close();112113System.out.println ("Test1: OK");114}115116/* check timeouts on receive */117118static void test2 () throws Exception {119s1 = new DatagramSocket ();120s2 = new DatagramSocket ();121s1.setSoTimeout (4000);122long t1 = System.currentTimeMillis();123try {124s1.receive (new DatagramPacket (new byte [128], 128));125throw new Exception ("expected receive timeout ");126} catch (SocketTimeoutException e) {127}128checkTime (System.currentTimeMillis() - t1, 4000);129130/* check data can be exchanged now */131132simpleDataExchange (s1, ia6addr, s2, ia4addr);133134/* double check timeout still works */135t1 = System.currentTimeMillis();136try {137s1.receive (new DatagramPacket (new byte [128], 128));138throw new Exception ("expected receive timeout ");139} catch (SocketTimeoutException e) {140}141checkTime (System.currentTimeMillis() - t1, 4000);142143/* check receive works after a delay < timeout */144145final DatagramSocket s = s2;146final InetAddress ia6 = ia6addr;147final int port = s1.getLocalPort();148149s1.setSoTimeout(10000);150runAfter (2000, new Runnable () {151public void run () {152try {153DatagramPacket p = new DatagramPacket ("Hello 123".getBytes(), 0, 8, ia6, port);154s.send (p);155} catch (Exception e) {}156}157});158t1 = System.currentTimeMillis();159s1.receive (new DatagramPacket (new byte [128], 128));160checkTime (System.currentTimeMillis() - t1, 2000, 10000);161s1.close ();162s2.close ();163System.out.println ("Test2: OK");164}165166/* check connected sockets */167168static void test3 () throws Exception {169s1 = new DatagramSocket ();170s2 = new DatagramSocket ();171s1.connect (ia6addr, s2.getLocalPort());172datagramEcho (s1, s2, null);173s1.close (); s2.close();174System.out.println ("Test3: OK");175}176177/* check PortUnreachable */178179static void test4 () throws Exception {180s1 = new DatagramSocket ();181s1.connect (ia6addr, 5000);182s1.setSoTimeout (3000);183try {184DatagramPacket p = new DatagramPacket ("HelloWorld".getBytes(), "HelloWorld".length());185s1.send (p);186p = new DatagramPacket (new byte[128], 128);187s1.receive (p);188} catch (PortUnreachableException e) {189System.out.println ("Test4: OK");190return;191} catch (SocketTimeoutException e) {192System.out.println ("Test4: failed. Never mind, it's an OS bug");193}194}195196}197198199