0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4868820
0N/A * @summary IPv6 support for Windows XP and 2003 server
0N/A */
0N/A
0N/Aimport java.net.*;
0N/Aimport java.io.*;
0N/A
0N/Apublic class UdpTest extends Tests {
0N/A static DatagramSocket c3, s1, s2, s3;
0N/A static InetAddress s1peer, s2peer;
0N/A
0N/A static InetAddress ia4any;
0N/A static InetAddress ia6any;
0N/A static Inet6Address ia6addr;
0N/A static InetAddress ia6bad; /* a global 6to4 IPv6 address, which cant be connected to */
0N/A static InetAddress ia6rem1;
0N/A static Inet4Address ia4addr;
0N/A
0N/A static {
0N/A try {
0N/A ia4any = InetAddress.getByName ("0.0.0.0");
0N/A ia6any = InetAddress.getByName ("::0");
0N/A try {
0N/A ia6bad = InetAddress.getByName ("2002:819c:dc29:1:1322:33ff:fe44:5566%net0");
0N/A } catch (Exception e) {
0N/A ia6bad = InetAddress.getByName ("2002:819c:dc29:1:1322:33ff:fe44:5566");
0N/A }
0N/A //ia6rem1 = InetAddress.getByName ("fe80::a00:20ff:feed:b08d%eth0");
0N/A //ia6rem1 = InetAddress.getByName ("129.156.220.63");
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
0N/A }
0N/A ia6addr = getFirstLocalIPv6Address ();
0N/A ia4addr = getFirstLocalIPv4Address ();
0N/A }
0N/A
0N/A public static void main (String[] args) throws Exception {
0N/A checkDebug(args);
0N/A if (ia6addr == null) {
0N/A System.out.println ("No local IPv6 addresses: exiting now");
0N/A return;
0N/A }
0N/A dprintln ("Local Addresses");
0N/A dprintln (ia4addr.toString());
0N/A dprintln (ia6addr.toString());
0N/A test1 ();
0N/A test2 ();
0N/A if (!isLinux()) {
0N/A test3 ();
0N/A }
0N/A test4 ();
0N/A }
0N/A
0N/A /* basic UDP connectivity test using IPv6 only and IPv4/IPv6 together */
0N/A
0N/A static void test1 () throws Exception {
0N/A s1 = new DatagramSocket ();
0N/A s2 = new DatagramSocket ();
0N/A simpleDataExchange (s1, ia4addr, s2, ia4addr);
0N/A s1.close (); s2.close ();
0N/A
0N/A /* IPv6 */
0N/A s1 = new DatagramSocket ();
0N/A s2 = new DatagramSocket ();
0N/A simpleDataExchange (s1, ia6addr, s2, ia6addr);
0N/A s1.close (); s2.close ();
0N/A
0N/A /* IPv6 only */
0N/A s1 = new DatagramSocket (0, ia6addr);
0N/A s2 = new DatagramSocket (0, ia6addr);
0N/A simpleDataExchange (s1, ia6addr, s2, ia6addr);
0N/A s1.close (); s2.close ();
0N/A
0N/A /* IPv6 and IPv4 */
0N/A s1 = new DatagramSocket ();
0N/A s2 = new DatagramSocket ();
0N/A simpleDataExchange (s1, ia6addr, s2, ia4addr);
0N/A s1.close (); s2.close ();
0N/A
0N/A /* listen on anyaddr and check receive from IPv4 and IPv6 */
0N/A
0N/A s1 = new DatagramSocket ();
0N/A s2 = new DatagramSocket (0, ia6addr);
0N/A s3 = new DatagramSocket (0, ia4addr);
0N/A datagramEcho (s2, s1, ia6addr);
0N/A datagramEcho (s3, s1, ia4addr);
0N/A s1.close (); s2.close (); s3.close();
0N/A
0N/A System.out.println ("Test1: OK");
0N/A }
0N/A
0N/A /* check timeouts on receive */
0N/A
0N/A static void test2 () throws Exception {
0N/A s1 = new DatagramSocket ();
0N/A s2 = new DatagramSocket ();
0N/A s1.setSoTimeout (4000);
0N/A long t1 = System.currentTimeMillis();
0N/A try {
0N/A s1.receive (new DatagramPacket (new byte [128], 128));
0N/A throw new Exception ("expected receive timeout ");
0N/A } catch (SocketTimeoutException e) {
0N/A }
0N/A checkTime (System.currentTimeMillis() - t1, 4000);
0N/A
0N/A /* check data can be exchanged now */
0N/A
0N/A simpleDataExchange (s1, ia6addr, s2, ia4addr);
0N/A
0N/A /* double check timeout still works */
0N/A t1 = System.currentTimeMillis();
0N/A try {
0N/A s1.receive (new DatagramPacket (new byte [128], 128));
0N/A throw new Exception ("expected receive timeout ");
0N/A } catch (SocketTimeoutException e) {
0N/A }
0N/A checkTime (System.currentTimeMillis() - t1, 4000);
0N/A
0N/A /* check receive works after a delay < timeout */
0N/A
0N/A final DatagramSocket s = s2;
0N/A final InetAddress ia6 = ia6addr;
0N/A final int port = s1.getLocalPort();
0N/A
0N/A runAfter (2000, new Runnable () {
0N/A public void run () {
0N/A try {
0N/A DatagramPacket p = new DatagramPacket ("Hello 123".getBytes(), 0, 8, ia6, port);
0N/A s.send (p);
0N/A } catch (Exception e) {}
0N/A }
0N/A });
0N/A t1 = System.currentTimeMillis();
0N/A s1.receive (new DatagramPacket (new byte [128], 128));
0N/A checkTime (System.currentTimeMillis() - t1, 2000);
0N/A s1.close ();
0N/A s2.close ();
0N/A System.out.println ("Test2: OK");
0N/A }
0N/A
0N/A /* check connected sockets */
0N/A
0N/A static void test3 () throws Exception {
0N/A s1 = new DatagramSocket ();
0N/A s2 = new DatagramSocket ();
0N/A s1.connect (ia6addr, s2.getLocalPort());
0N/A datagramEcho (s1, s2, null);
0N/A s1.close (); s2.close();
0N/A System.out.println ("Test3: OK");
0N/A }
0N/A
0N/A /* check PortUnreachable */
0N/A
0N/A static void test4 () throws Exception {
0N/A s1 = new DatagramSocket ();
0N/A s1.connect (ia6addr, 5000);
0N/A s1.setSoTimeout (3000);
0N/A try {
0N/A DatagramPacket p = new DatagramPacket ("HelloWorld".getBytes(), "HelloWorld".length());
0N/A s1.send (p);
0N/A p = new DatagramPacket (new byte[128], 128);
0N/A s1.receive (p);
0N/A } catch (PortUnreachableException e) {
0N/A System.out.println ("Test4: OK");
0N/A return;
0N/A } catch (SocketTimeoutException e) {
0N/A System.out.println ("Test4: failed. Never mind, it's an OS bug");
0N/A }
0N/A }
0N/A
0N/A}