1 /*
   2  * Copyright (c) 2018, 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  * @test
  26  * @bug 8195160
  27  * @summary Test for basic functionality for RdmaSocketChannel
  28  *         and RdmaServerSocketChannel 
  29  * @requires (os.family == "linux")
  30  * @library .. /test/lib
  31  * @build RsocketTest
  32  * @run main/othervm -Djava.net.preferIPv4Stack=true BasicSocketChannelTest
  33  */
  34 
  35 import java.io.InputStream;
  36 import java.io.OutputStream;
  37 import java.net.InetAddress;
  38 import java.net.InetSocketAddress;
  39 import java.net.ServerSocket;
  40 import java.net.Socket;
  41 import java.nio.channels.ServerSocketChannel;
  42 import java.nio.channels.SocketChannel;
  43 import java.nio.ByteBuffer;
  44 import java.nio.CharBuffer;
  45 import jdk.net.Sockets;
  46 
  47 public class BasicSocketChannelTest implements Runnable {
  48     static ServerSocketChannel ssc;
  49     static SocketChannel sc1, sc2;
  50     static InetAddress iaddr;
  51     static int port = 0;
  52     static String message = "This is a message!";
  53     static int length = -1;
  54 
  55     public static void main(String args[]) throws Exception {
  56         if (!RsocketTest.isRsocketAvailable())
  57             return;
  58 
  59         iaddr = InetAddress.getLocalHost();
  60         length = message.length();
  61         String result;
  62 
  63         //test SocketChannel and ServerSocketChannel
  64         try {
  65             ssc = Sockets.openRdmaServerSocketChannel();
  66             sc1 = Sockets.openRdmaSocketChannel();
  67 
  68             ssc.bind(new InetSocketAddress(iaddr, port));
  69 
  70             Thread t = new Thread(new BasicSocketChannelTest(), "Channel");
  71             t.start(); 
  72 
  73             sc2 = ssc.accept();
  74             ByteBuffer output = ByteBuffer.allocate(length);
  75             int outputNum = 0;
  76             while (outputNum < length) {
  77                 outputNum += sc2.read(output);
  78             }
  79 
  80             result = new String(output.array());
  81             if(!result.equals(message))
  82                 throw new RuntimeException("Test Failed!");
  83             sc2.shutdownInput();
  84             sc2.shutdownOutput();
  85         } catch (Exception e) {
  86             e.printStackTrace();
  87             throw new RuntimeException("Test Failed!");
  88         } finally {
  89             ssc.close();
  90             sc1.close();
  91             sc2.close();
  92         }
  93 
  94         //test SocketChannel.socket() and ServerSocketChannel.socket()
  95         try {
  96             ssc = Sockets.openRdmaServerSocketChannel();
  97             sc1 = Sockets.openRdmaSocketChannel();
  98 
  99             ssc.socket().bind(new InetSocketAddress(iaddr, port));
 100 
 101             Thread t = new Thread(new BasicSocketChannelTest(), "Socket");
 102             t.start();
 103 
 104             sc2 = ssc.accept();
 105             Socket conn = sc2.socket();
 106             InputStream is = conn.getInputStream();
 107 
 108             int num = 0;
 109             byte[] buf = new byte[length];
 110             while (num < length) {
 111                 int l = is.read(buf);
 112                 num += l;
 113             }
 114 
 115             result = new String(buf);
 116             if(!result.equals(message))
 117                 throw new RuntimeException("Test Failed!");
 118             conn.shutdownInput();
 119             conn.shutdownOutput();
 120             if (!conn.isInputShutdown() || !conn.isOutputShutdown())
 121                 throw new RuntimeException("Test Failed!");
 122         } catch (Exception e) {
 123             e.printStackTrace();
 124             throw new RuntimeException("Test Failed!");
 125         } finally {
 126             ssc.close();
 127             sc1.close();
 128             sc2.close();
 129         }
 130     }
 131 
 132     public void run() {
 133         int port = ssc.socket().getLocalPort();
 134         byte[] arr = new byte[length];
 135         arr = message.getBytes();
 136 
 137         try {
 138             if (Thread.currentThread().getName().startsWith("Channel")) {
 139                 sc1.connect(new InetSocketAddress(iaddr, port));
 140                 if (!sc1.isConnected())
 141                     throw new RuntimeException("Test Failed!");
 142                 ByteBuffer input = ByteBuffer.allocate(length);
 143                 input.put(arr);
 144                 input.flip();
 145                 int inputNum = 0;
 146                 while (inputNum < length) {
 147                     inputNum += sc1.write(input);
 148                 }
 149                 sc1.shutdownInput();
 150                 sc1.shutdownOutput();
 151             } else if (Thread.currentThread().getName().startsWith("Socket")) {
 152                 Socket client = sc1.socket();
 153                 client.connect(new InetSocketAddress(iaddr, port));
 154                 if (!client.isConnected())
 155                     throw new RuntimeException("Test Failed!");
 156                 OutputStream os = client.getOutputStream();
 157                 os.write(arr);
 158                 client.shutdownInput();
 159                 client.shutdownOutput();
 160                 if (!client.isInputShutdown() || !client.isOutputShutdown())
 161                     throw new RuntimeException("Test Failed!");
 162             }
 163         } catch (Exception e) {
 164             e.printStackTrace();
 165             throw new RuntimeException("Test Failed!");
 166         }
 167     }
 168 }