1 /* 2 * Copyright (c) 2001, 2013, 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 /* @test 25 * @bug 4313882 4981129 26 * @summary Unit test for datagram-socket-channel adaptors 27 * @library .. 28 * @key randomness 29 */ 30 31 import java.net.*; 32 import java.nio.channels.*; 33 import java.util.*; 34 35 36 public class AdaptDatagramSocket { 37 38 static java.io.PrintStream out = System.out; 39 static Random rand = new Random(); 40 41 static String toString(DatagramPacket dp) { 42 return ("DatagramPacket[off=" + dp.getOffset() 43 + ", len=" + dp.getLength() 44 + "]"); 45 } 46 47 static void test(DatagramSocket ds, InetSocketAddress dst, 48 boolean shouldTimeout) 49 throws Exception 50 { 51 DatagramPacket op = new DatagramPacket(new byte[100], 13, 42, dst); 52 rand.nextBytes(op.getData()); 53 DatagramPacket ip = new DatagramPacket(new byte[100], 19, 100 - 19); 54 out.println("pre op: " + toString(op) + " ip: " + toString(ip)); 55 56 long start = System.currentTimeMillis(); 57 ds.send(op); 58 59 for (;;) { 60 try { 61 ds.receive(ip); 62 if (ip.getLength() == 0) { // ## Not sure why this happens 63 ip.setLength(100 - 19); 64 continue; 65 } 66 } catch (SocketTimeoutException x) { 67 if (shouldTimeout) { 68 out.println("Receive timed out, as expected"); 69 return; 70 } 71 throw x; 72 } 73 break; 74 } 75 76 out.println("rtt: " + (System.currentTimeMillis() - start)); 77 out.println("post op: " + toString(op) + " ip: " + toString(ip)); 78 79 for (int i = 0; i < ip.getLength(); i++) { 80 if (ip.getData()[ip.getOffset() + i] 81 != op.getData()[op.getOffset() + i]) 82 throw new Exception("Incorrect data received"); 83 } 84 85 if (!(ip.getSocketAddress().equals(dst))) { 86 throw new Exception("Incorrect sender address, expected: " + dst 87 + " actual: " + ip.getSocketAddress()); 88 } 89 } 90 91 static void test(InetSocketAddress dst, 92 int timeout, boolean shouldTimeout, 93 boolean connect) 94 throws Exception 95 { 96 out.println(); 97 out.println("dst: " + dst); 98 99 DatagramSocket ds; 100 if (false) { 101 // Original 102 ds = new DatagramSocket(); 103 } else { 104 DatagramChannel dc = DatagramChannel.open(); 105 ds = dc.socket(); 106 ds.bind(new InetSocketAddress(0)); 107 } 108 109 out.println("socket: " + ds); 110 if (connect) { 111 ds.connect(dst); 112 out.println("connect: " + ds); 113 } 114 InetSocketAddress src = new InetSocketAddress(ds.getLocalAddress(), 115 ds.getLocalPort()); 116 out.println("src: " + src); 117 118 if (timeout > 0) 119 ds.setSoTimeout(timeout); 120 out.println("timeout: " + ds.getSoTimeout()); 121 122 for (int i = 0; i < 5; i++) { 123 test(ds, dst, shouldTimeout); 124 } 125 126 // Leave the socket open so that we don't reuse the old src address 127 //ds.close(); 128 129 } 130 131 public static void main(String[] args) throws Exception { 132 // need an UDP echo server 133 try (TestServers.UdpEchoServer echoServer 134 = TestServers.UdpEchoServer.startNewServer(100)) { 135 final InetSocketAddress address 136 = new InetSocketAddress(echoServer.getAddress(), 137 echoServer.getPort()); 138 test(address, 0, false, false); 139 test(address, 0, false, true); 140 test(address, 15000, false, false); 141 } 142 try (TestServers.UdpDiscardServer discardServer 143 = TestServers.UdpDiscardServer.startNewServer()) { 144 final InetSocketAddress address 145 = new InetSocketAddress(discardServer.getAddress(), 146 discardServer.getPort()); 147 test(address, 10, true, false); 148 } 149 } 150 151 }