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 * Tests that the channel returned by System.inheritedChannel() 28 * is in blocking mode, bound, and in the case of a SocketChannel 29 * connected to a peer. 30 * 31 * The test works by launching a test service (called StateTestService) so 32 * that it inherits each type of channel. The test service checks the 33 * socket state and replies back to this class via an out-of-band 34 * channel. 35 */ 36 import java.io.IOException; 37 import java.net.InetSocketAddress; 38 import java.nio.ByteBuffer; 39 import java.nio.channels.DatagramChannel; 40 import java.nio.channels.SelectionKey; 41 import java.nio.channels.Selector; 42 import java.nio.channels.ServerSocketChannel; 43 import java.nio.channels.SocketChannel; 44 45 public class StateTest { 46 47 private static int failures = 0; 48 49 private static String TEST_SERVICE = "StateTestService"; 50 51 /* 52 * Reads the test result from the "out-of-band" connection to the test service. 53 * 54 * The out-of-band connection is just a TCP connection from the service to 55 * this class. waitForTestResult just waits (with timeout) for the service 56 * to connect. Once connected it waits (with timeout) for the test result. 57 * The test result is examined. 58 */ 59 private static void waitForTestResult(ServerSocketChannel ssc, boolean expectFail) throws IOException { 60 Selector sel = ssc.provider().openSelector(); 61 SelectionKey sk; 62 SocketChannel sc; 63 64 /* 65 * Wait for service to connect 66 */ 67 ssc.configureBlocking(false); 68 sk = ssc.register(sel, SelectionKey.OP_ACCEPT); 69 long to = 15*1000; 70 sc = null; 71 for (;;) { 72 long st = System.currentTimeMillis(); 73 sel.select(to); 74 if (sk.isAcceptable() && ((sc = ssc.accept()) != null)) { 75 // connection established 76 break; 77 } 78 sel.selectedKeys().remove(sk); 79 to -= System.currentTimeMillis() - st; 80 if (to <= 0) { 81 throw new IOException("Timed out waiting for service to report test result"); 82 } 83 } 84 sk.cancel(); 85 ssc.configureBlocking(false); 86 87 /* 88 * Wait for service to report test result 89 */ 90 sc.configureBlocking(false); 91 sk = sc.register(sel, SelectionKey.OP_READ); 92 to = 5000; 93 ByteBuffer bb = ByteBuffer.allocateDirect(20); 94 for (;;) { 95 long st = System.currentTimeMillis(); 96 sel.select(to); 97 if (sk.isReadable()) { 98 int n = sc.read(bb); 99 if (n > 0) { 100 break; 101 } 102 if (n < 0) { 103 throw new IOException("Premature EOF - no test result from service"); 104 } 105 } 106 sel.selectedKeys().remove(sk); 107 to -= System.currentTimeMillis() - st; 108 if (to <= 0) { 109 throw new IOException("Timed out waiting for service to report test result"); 110 } 111 } 112 sk.cancel(); 113 sc.close(); 114 sel.close(); 115 116 /* 117 * Examine the test result 118 */ 119 bb.flip(); 120 byte b = bb.get(); 121 122 if (expectFail && b == 'P') { 123 System.err.println("Test passed - test is expected to fail!!!"); 124 failures++; 125 } 126 if (!expectFail && b != 'P') { 127 System.err.println("Test failed!"); 128 failures++; 129 } 130 } 131 132 public static void main(String args[]) throws IOException { 133 boolean expectFail = false; 134 135 /* 136 * [-expectFail] [options...] 137 */ 138 String options[] = args; 139 if (args.length > 0 && args[0].equals("-expectFail")) { 140 // shift out first arg to create options 141 expectFail = true; 142 options = new String[args.length-1]; 143 if (args.length > 1) { 144 System.arraycopy(args, 1, options, 0, args.length-1); 145 } 146 } 147 148 /* 149 * Create the listener which will be used to read the test result 150 * from the service. 151 */ 152 ServerSocketChannel ssc = ServerSocketChannel.open(); 153 ssc.socket().bind(new InetSocketAddress(0)); 154 155 /* 156 * The port is passed to the service as an argument. 157 */ 158 int port = ssc.socket().getLocalPort(); 159 String arg[] = new String[1]; 160 arg[0] = String.valueOf(port); 161 162 /* 163 * Launch service with a SocketChannel (tcp nowait) 164 */ 165 SocketChannel sc = Launcher.launchWithSocketChannel(TEST_SERVICE, options, arg); 166 waitForTestResult(ssc, expectFail); 167 sc.close(); 168 169 /* 170 * Launch service with a ServerSocketChannel (tcp wait) 171 * launchWithServerSocketChannel establishes a connection to the service 172 * and the returned SocketChannel is connected to the service. 173 */ 174 sc = Launcher.launchWithServerSocketChannel(TEST_SERVICE, options, arg); 175 waitForTestResult(ssc, expectFail); 176 sc.close(); 177 178 /* 179 * Launch service with a DatagramChannel (udp wait) 180 */ 181 DatagramChannel dc = Launcher.launchWithDatagramChannel(TEST_SERVICE, options, arg); 182 waitForTestResult(ssc, expectFail); 183 dc.close(); 184 185 if (failures > 0) { 186 throw new RuntimeException("Test failed - see log for details"); 187 } else { 188 System.out.println("All tests passed."); 189 } 190 } 191 }