EchoTest.java revision 2362:00cd9dc3c2b5
1/* 2 * Copyright (c) 2003, 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 * 26 * 27 * Used in conjunction to EchoService to test System.inheritedChannel(). 28 * 29 * The first test is the TCP echo test. A service is launched with a TCP 30 * socket and a TCP message is sent to the service. The test checks that 31 * the message is correctly echoed. 32 * 33 * The second test is a UDP echo test. A service is launched with a UDP 34 * socket and a UDP packet is sent to the service. The test checks that 35 * the packet is correctly echoed. 36 * 37 */ 38import java.net.*; 39import java.io.*; 40import java.nio.ByteBuffer; 41import java.nio.channels.*; 42import java.util.Random; 43 44public class EchoTest { 45 46 private static int failures = 0; 47 48 private static String ECHO_SERVICE = "EchoService"; 49 50 /* 51 * Sends a message with random bytes to the service, and then waits for 52 * a reply (with timeout). Once the reply is received it is checked to ensure 53 * that it matches the original message. 54 */ 55 private static void TCPEchoTest() throws IOException { 56 SocketChannel sc = Launcher.launchWithSocketChannel(ECHO_SERVICE, null); 57 58 String msg = "Where's that damn torpedo?"; 59 int repeat = 100; 60 int size = msg.length() * repeat; 61 62 // generate bytes into a buffer and send it to the service 63 64 ByteBuffer bb1 = ByteBuffer.allocate(size); 65 Random gen = new Random(); 66 for (int i=0; i<repeat; i++) { 67 bb1.put(msg.getBytes("UTF-8")); 68 } 69 bb1.flip(); 70 sc.write(bb1); 71 72 // now we put the channel into non-blocking mode and we read the 73 // reply from the service into a second buffer. 74 75 ByteBuffer bb2 = ByteBuffer.allocate(size+100); 76 sc.configureBlocking(false); 77 Selector sel = sc.provider().openSelector(); 78 SelectionKey sk = sc.register(sel, SelectionKey.OP_READ); 79 int nread = 0; 80 long to = 5000; 81 while (nread < size) { 82 long st = System.currentTimeMillis(); 83 sel.select(to); 84 if (sk.isReadable()) { 85 int n = sc.read(bb2); 86 if (n > 0) { 87 nread += n; 88 } 89 if (n < 0) { 90 break; // EOF 91 } 92 } 93 sel.selectedKeys().remove(sk); 94 to -= System.currentTimeMillis() - st; 95 if (to <= 0) { 96 break; 97 } 98 } 99 sc.close(); 100 101 // and compare the response 102 103 boolean err = false; 104 105 if (nread != size) { 106 err = true; 107 } else { 108 bb1.flip(); 109 bb2.flip(); 110 while (bb1.hasRemaining()) { 111 if (bb1.get() != bb2.get()) { 112 err = true; 113 } 114 } 115 } 116 117 // if error print out the response from the service (could be a stack trace) 118 if (err) { 119 System.err.println("Bad response or premature EOF, bytes read: "); 120 bb2.flip(); 121 while (bb2.hasRemaining()) { 122 char c = (char)bb2.get(); 123 System.out.print(c); 124 } 125 throw new RuntimeException("Bad response or premature EOF from service"); 126 } 127 } 128 129 /* 130 * Send a UDP packet to the service, wait for a reply (with timeout). Finally 131 * check that the packet is the same length as the original. 132 */ 133 private static void UDPEchoTest() throws IOException { 134 DatagramChannel dc = Launcher.launchWithDatagramChannel(ECHO_SERVICE, null); 135 136 String msg = "I was out saving the galaxy when your grandfather was in diapers"; 137 138 ByteBuffer bb = ByteBuffer.wrap(msg.getBytes("UTF-8")); 139 dc.write(bb); 140 141 // and receive the echo 142 byte b[] = new byte[msg.length() + 100]; 143 DatagramPacket pkt2 = new DatagramPacket(b, b.length); 144 dc.socket().setSoTimeout(2000); 145 dc.socket().receive(pkt2); 146 147 if (pkt2.getLength() != msg.length()) { 148 throw new RuntimeException("Received packet of incorrect length"); 149 } 150 151 dc.close(); 152 } 153 154 public static void main(String args[]) throws IOException { 155 156 // TCP echo 157 try { 158 TCPEchoTest(); 159 System.out.println("TCP echo test passed."); 160 } catch (Exception x) { 161 System.err.println(x); 162 failures++; 163 } 164 165 // UDP echo 166 try { 167 UDPEchoTest(); 168 System.out.println("UDP echo test passed."); 169 } catch (Exception x) { 170 x.printStackTrace(); 171 System.err.println(x); 172 failures++; 173 } 174 175 if (failures > 0) { 176 throw new RuntimeException("Test failed - see log for details"); 177 } 178 } 179 180} 181