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