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