1 /*
   2  * Copyright (c) 2010, 2015, 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  * @summary Test socket adapter sendUrgentData method
  26  * @bug 6963907
  27  */
  28 
  29 import java.net.*;
  30 import java.nio.ByteBuffer;
  31 import java.nio.channels.*;
  32 import java.io.IOException;
  33 import java.util.Random;
  34 
  35 public class OutOfBand {
  36 
  37     private static final Random rand = new Random();
  38 
  39     public static void main(String[] args) throws Exception {
  40         ServerSocketChannel ssc = null;
  41         SocketChannel sc1 = null;
  42         SocketChannel sc2 = null;
  43 
  44         try {
  45 
  46             // establish loopback connection
  47             ssc = ServerSocketChannel.open().bind(new InetSocketAddress(0));
  48             InetAddress lh = InetAddress.getLocalHost();
  49             SocketAddress remote =
  50                 new InetSocketAddress(lh, ssc.socket().getLocalPort());
  51             sc1 = SocketChannel.open(remote);
  52             sc2 = ssc.accept();
  53 
  54             // enable SO_OOBLINE on server side
  55             sc2.socket().setOOBInline(true);
  56 
  57             // run tests
  58             test1(sc1, sc2);
  59             test2(sc1, sc2);
  60             test3(sc1, sc2);
  61         } finally {
  62             if (sc1 != null) sc1.close();
  63             if (sc2 != null) sc2.close();
  64             if (ssc != null) ssc.close();
  65         }
  66     }
  67 
  68     /**
  69      * Basic test to check that OOB/TCP urgent byte is received.
  70      */
  71     static void test1(SocketChannel client, SocketChannel server)
  72         throws Exception
  73     {
  74         assert server.socket().getOOBInline();
  75         ByteBuffer bb = ByteBuffer.allocate(100);
  76         for (int i=0; i<1000; i++) {
  77             int b1 = -127 + rand.nextInt(384);
  78             client.socket().sendUrgentData(b1);
  79 
  80             bb.clear();
  81             if (server.read(bb) != 1)
  82                 throw new RuntimeException("One byte expected");
  83             bb.flip();
  84             byte b2 = bb.get();
  85             if ((byte)b1 != b2)
  86                 throw new RuntimeException("Unexpected byte");
  87         }
  88     }
  89 
  90     /**
  91      * Basic test to check that OOB/TCP urgent byte is received, maybe with
  92      * OOB mark changing.
  93      */
  94     static void test2(final SocketChannel client, SocketChannel server)
  95         throws Exception
  96     {
  97         assert server.socket().getOOBInline();
  98         Runnable sender = new Runnable() {
  99             public void run() {
 100                 try {
 101                     for (int i=0; i<256; i++)
 102                         client.socket().sendUrgentData(i);
 103                 } catch (IOException ioe) {
 104                     ioe.printStackTrace();
 105                 }
 106             }
 107         };
 108         Thread thr = new Thread(sender);
 109         thr.start();
 110 
 111         ByteBuffer bb = ByteBuffer.allocate(256);
 112         while (bb.hasRemaining()) {
 113             if (server.read(bb) < 0)
 114                 throw new RuntimeException("Unexpected EOF");
 115         }
 116         bb.flip();
 117         byte expect = 0;
 118         while (bb.hasRemaining()) {
 119             if (bb.get() != expect)
 120                 throw new RuntimeException("Unexpected byte");
 121             expect++;
 122         }
 123 
 124         thr.join();
 125     }
 126 
 127     /**
 128      * Test that is close to some real world examples where an urgent byte is
 129      * used to "cancel" a long running query or transaction on the server.
 130      */
 131     static void test3(SocketChannel client, final SocketChannel server)
 132         throws Exception
 133     {
 134         final int STOP = rand.nextInt(256);
 135 
 136         assert server.socket().getOOBInline();
 137         Runnable reader = new Runnable() {
 138             public void run() {
 139                 ByteBuffer bb = ByteBuffer.allocate(100);
 140                 try {
 141                     int n = server.read(bb);
 142                     if (n != 1) {
 143                         String msg = (n < 0) ? "Unexpected EOF" :
 144                                                "One byte expected";
 145                         throw new RuntimeException(msg);
 146                     }
 147                     bb.flip();
 148                     if (bb.get() != (byte)STOP)
 149                         throw new RuntimeException("Unexpected byte");
 150                     bb.flip();
 151                     server.write(bb);
 152                 } catch (IOException ioe) {
 153                     ioe.printStackTrace();
 154                 }
 155 
 156             }
 157         };
 158 
 159         Thread thr = new Thread(reader);
 160         thr.start();
 161 
 162         // "stop" server
 163         client.socket().sendUrgentData(STOP);
 164 
 165         // wait for server reply
 166         ByteBuffer bb = ByteBuffer.allocate(100);
 167         int n = client.read(bb);
 168         if (n != 1)
 169             throw new RuntimeException("Unexpected number of bytes");
 170         bb.flip();
 171         if (bb.get() != (byte)STOP)
 172             throw new RuntimeException("Unexpected reply");
 173 
 174         thr.join();
 175     }
 176 }