1/* 2 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 4868820 27 * @summary IPv6 support for Windows XP and 2003 server 28 */ 29 30import java.net.*; 31import java.io.*; 32 33public class UdpTest extends Tests { 34 static DatagramSocket c3, s1, s2, s3; 35 static InetAddress s1peer, s2peer; 36 37 static InetAddress ia4any; 38 static InetAddress ia6any; 39 static Inet6Address ia6addr; 40 static InetAddress ia6bad; /* a global 6to4 IPv6 address, which cant be connected to */ 41 static InetAddress ia6rem1; 42 static Inet4Address ia4addr; 43 44 static { 45 try { 46 ia4any = InetAddress.getByName ("0.0.0.0"); 47 ia6any = InetAddress.getByName ("::0"); 48 try { 49 ia6bad = InetAddress.getByName ("2002:819c:dc29:1:1322:33ff:fe44:5566%net0"); 50 } catch (Exception e) { 51 ia6bad = InetAddress.getByName ("2002:819c:dc29:1:1322:33ff:fe44:5566"); 52 } 53 //ia6rem1 = InetAddress.getByName ("fe80::a00:20ff:feed:b08d%eth0"); 54 //ia6rem1 = InetAddress.getByName ("129.156.220.63"); 55 } catch (Exception e) { 56 e.printStackTrace(); 57 } 58 ia6addr = getFirstLocalIPv6Address (); 59 ia4addr = getFirstLocalIPv4Address (); 60 } 61 62 public static void main (String[] args) throws Exception { 63 checkDebug(args); 64 if (ia6addr == null) { 65 System.out.println ("No local IPv6 addresses: exiting now"); 66 return; 67 } 68 dprintln ("Local Addresses"); 69 dprintln (ia4addr.toString()); 70 dprintln (ia6addr.toString()); 71 test1 (); 72 test2 (); 73 if (!isLinux()) { 74 test3 (); 75 } 76 test4 (); 77 } 78 79 /* basic UDP connectivity test using IPv6 only and IPv4/IPv6 together */ 80 81 static void test1 () throws Exception { 82 s1 = new DatagramSocket (); 83 s2 = new DatagramSocket (); 84 simpleDataExchange (s1, ia4addr, s2, ia4addr); 85 s1.close (); s2.close (); 86 87 /* IPv6 */ 88 s1 = new DatagramSocket (); 89 s2 = new DatagramSocket (); 90 simpleDataExchange (s1, ia6addr, s2, ia6addr); 91 s1.close (); s2.close (); 92 93 /* IPv6 only */ 94 s1 = new DatagramSocket (0, ia6addr); 95 s2 = new DatagramSocket (0, ia6addr); 96 simpleDataExchange (s1, ia6addr, s2, ia6addr); 97 s1.close (); s2.close (); 98 99 /* IPv6 and IPv4 */ 100 s1 = new DatagramSocket (); 101 s2 = new DatagramSocket (); 102 simpleDataExchange (s1, ia6addr, s2, ia4addr); 103 s1.close (); s2.close (); 104 105 /* listen on anyaddr and check receive from IPv4 and IPv6 */ 106 107 s1 = new DatagramSocket (); 108 s2 = new DatagramSocket (0, ia6addr); 109 s3 = new DatagramSocket (0, ia4addr); 110 datagramEcho (s2, s1, ia6addr); 111 datagramEcho (s3, s1, ia4addr); 112 s1.close (); s2.close (); s3.close(); 113 114 System.out.println ("Test1: OK"); 115 } 116 117 /* check timeouts on receive */ 118 119 static void test2 () throws Exception { 120 s1 = new DatagramSocket (); 121 s2 = new DatagramSocket (); 122 s1.setSoTimeout (4000); 123 long t1 = System.currentTimeMillis(); 124 try { 125 s1.receive (new DatagramPacket (new byte [128], 128)); 126 throw new Exception ("expected receive timeout "); 127 } catch (SocketTimeoutException e) { 128 } 129 checkTime (System.currentTimeMillis() - t1, 4000); 130 131 /* check data can be exchanged now */ 132 133 simpleDataExchange (s1, ia6addr, s2, ia4addr); 134 135 /* double check timeout still works */ 136 t1 = System.currentTimeMillis(); 137 try { 138 s1.receive (new DatagramPacket (new byte [128], 128)); 139 throw new Exception ("expected receive timeout "); 140 } catch (SocketTimeoutException e) { 141 } 142 checkTime (System.currentTimeMillis() - t1, 4000); 143 144 /* check receive works after a delay < timeout */ 145 146 final DatagramSocket s = s2; 147 final InetAddress ia6 = ia6addr; 148 final int port = s1.getLocalPort(); 149 150 s1.setSoTimeout(10000); 151 runAfter (2000, new Runnable () { 152 public void run () { 153 try { 154 DatagramPacket p = new DatagramPacket ("Hello 123".getBytes(), 0, 8, ia6, port); 155 s.send (p); 156 } catch (Exception e) {} 157 } 158 }); 159 t1 = System.currentTimeMillis(); 160 s1.receive (new DatagramPacket (new byte [128], 128)); 161 checkTime (System.currentTimeMillis() - t1, 2000, 10000); 162 s1.close (); 163 s2.close (); 164 System.out.println ("Test2: OK"); 165 } 166 167 /* check connected sockets */ 168 169 static void test3 () throws Exception { 170 s1 = new DatagramSocket (); 171 s2 = new DatagramSocket (); 172 s1.connect (ia6addr, s2.getLocalPort()); 173 datagramEcho (s1, s2, null); 174 s1.close (); s2.close(); 175 System.out.println ("Test3: OK"); 176 } 177 178 /* check PortUnreachable */ 179 180 static void test4 () throws Exception { 181 s1 = new DatagramSocket (); 182 s1.connect (ia6addr, 5000); 183 s1.setSoTimeout (3000); 184 try { 185 DatagramPacket p = new DatagramPacket ("HelloWorld".getBytes(), "HelloWorld".length()); 186 s1.send (p); 187 p = new DatagramPacket (new byte[128], 128); 188 s1.receive (p); 189 } catch (PortUnreachableException e) { 190 System.out.println ("Test4: OK"); 191 return; 192 } catch (SocketTimeoutException e) { 193 System.out.println ("Test4: failed. Never mind, it's an OS bug"); 194 } 195 } 196 197} 198