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