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 RdmaSocket and RdmaServerSocket
  28  * @requires (os.family == "linux")
  29  * @library .. /test/lib
  30  * @build RsocketTest
  31  * @run main/othervm -Djava.net.preferIPv4Stack=true BasicSocketTest
  32  */
  33 
  34 import java.io.InputStream;
  35 import java.io.OutputStream;
  36 import java.net.InetAddress;
  37 import java.net.InetSocketAddress;
  38 import java.net.ServerSocket;
  39 import java.net.Socket;
  40 import java.nio.channels.ServerSocketChannel;
  41 import java.nio.channels.SocketChannel;
  42 import jdk.net.Sockets;
  43 
  44 public class BasicSocketTest implements Runnable {
  45     static ServerSocket ss;
  46     static Socket s1, s2;
  47     static InetAddress iaddr;
  48     static int port = 0;
  49     static ServerSocketChannel ssc;
  50     static SocketChannel sc1, sc2;
  51     static String message = "This is a message!";
  52     static int length = -1;
  53 
  54     public static void main(String args[]) throws Exception {
  55         if (!RsocketTest.isRsocketAvailable())
  56             return;
  57 
  58         iaddr = InetAddress.getLocalHost();
  59 
  60         try {
  61             ss = Sockets.openRdmaServerSocket();
  62             s1 = Sockets.openRdmaSocket();         
  63             ss.bind(new InetSocketAddress(iaddr, port));
  64 
  65             Thread t = new Thread(new BasicSocketTest());
  66             t.start();
  67             s2 = ss.accept();
  68 
  69             InputStream is = s2.getInputStream();
  70             length = message.length();
  71 
  72             int num = 0;
  73             byte[] buf = new byte[length];
  74             while (num < length) {
  75                 int l = is.read(buf);
  76                 num += l;
  77             }
  78 
  79             String result = new String(buf);
  80             if(!result.equals(message))
  81                 throw new RuntimeException("Test Failed!");
  82 
  83         } catch (Exception e) {
  84             e.printStackTrace();
  85             throw new RuntimeException("Test Failed!");
  86         } finally {
  87             ss.close();
  88             s1.close();
  89             s2.close();
  90         }
  91     }
  92 
  93     public void run() {
  94         try {
  95             s1.connect(new InetSocketAddress(iaddr, ss.getLocalPort()));
  96 
  97             OutputStream os = s1.getOutputStream();
  98             os.write(message.getBytes());
  99         } catch (Exception e) {
 100             e.printStackTrace();
 101             throw new RuntimeException("Test Failed!");
 102         }
 103     }
 104 }