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