duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4868820 |
| 27 | * @summary IPv6 support for Windows XP and 2003 server |
| 28 | */ |
| 29 | |
| 30 | import java.net.*; |
| 31 | import java.io.*; |
| 32 | |
| 33 | public 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 | runAfter (2000, new Runnable () { |
| 151 | public void run () { |
| 152 | try { |
| 153 | DatagramPacket p = new DatagramPacket ("Hello 123".getBytes(), 0, 8, ia6, port); |
| 154 | s.send (p); |
| 155 | } catch (Exception e) {} |
| 156 | } |
| 157 | }); |
| 158 | t1 = System.currentTimeMillis(); |
| 159 | s1.receive (new DatagramPacket (new byte [128], 128)); |
| 160 | checkTime (System.currentTimeMillis() - t1, 2000); |
| 161 | s1.close (); |
| 162 | s2.close (); |
| 163 | System.out.println ("Test2: OK"); |
| 164 | } |
| 165 | |
| 166 | /* check connected sockets */ |
| 167 | |
| 168 | static void test3 () throws Exception { |
| 169 | s1 = new DatagramSocket (); |
| 170 | s2 = new DatagramSocket (); |
| 171 | s1.connect (ia6addr, s2.getLocalPort()); |
| 172 | datagramEcho (s1, s2, null); |
| 173 | s1.close (); s2.close(); |
| 174 | System.out.println ("Test3: OK"); |
| 175 | } |
| 176 | |
| 177 | /* check PortUnreachable */ |
| 178 | |
| 179 | static void test4 () throws Exception { |
| 180 | s1 = new DatagramSocket (); |
| 181 | s1.connect (ia6addr, 5000); |
| 182 | s1.setSoTimeout (3000); |
| 183 | try { |
| 184 | DatagramPacket p = new DatagramPacket ("HelloWorld".getBytes(), "HelloWorld".length()); |
| 185 | s1.send (p); |
| 186 | p = new DatagramPacket (new byte[128], 128); |
| 187 | s1.receive (p); |
| 188 | } catch (PortUnreachableException e) { |
| 189 | System.out.println ("Test4: OK"); |
| 190 | return; |
| 191 | } catch (SocketTimeoutException e) { |
| 192 | System.out.println ("Test4: failed. Never mind, it's an OS bug"); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | } |