57115
|
1 |
/*
|
57160
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
57115
|
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 BasicSocketChannelTest
|
|
33 |
*/
|
|
34 |
|
|
35 |
import java.io.IOException;
|
|
36 |
import java.io.InputStream;
|
|
37 |
import java.io.OutputStream;
|
|
38 |
import java.io.UncheckedIOException;
|
|
39 |
import java.net.*;
|
|
40 |
import java.nio.channels.ClosedChannelException;
|
|
41 |
import java.nio.channels.ServerSocketChannel;
|
|
42 |
import java.nio.channels.SocketChannel;
|
|
43 |
import java.nio.ByteBuffer;
|
|
44 |
import jdk.net.RdmaSockets;
|
|
45 |
import jtreg.SkippedException;
|
|
46 |
import static java.lang.String.format;
|
|
47 |
import static java.lang.System.out;
|
|
48 |
import static java.net.StandardProtocolFamily.INET;
|
|
49 |
import static java.net.StandardProtocolFamily.INET6;
|
|
50 |
import static java.nio.charset.StandardCharsets.UTF_8;
|
|
51 |
|
|
52 |
public class BasicSocketChannelTest {
|
|
53 |
|
|
54 |
static final String MESSAGE = "This is a MESSAGE!";
|
|
55 |
static final int MESSAGE_LENGTH = MESSAGE.length();
|
|
56 |
|
|
57 |
public static void main(String args[]) throws Exception {
|
|
58 |
if (!RsocketTest.isRsocketAvailable())
|
|
59 |
throw new SkippedException("rsocket is not available");
|
|
60 |
|
|
61 |
InetAddress iaddr = InetAddress.getLocalHost();
|
|
62 |
ProtocolFamily family = iaddr instanceof Inet6Address ? INET6 : INET;
|
|
63 |
out.printf("local address: %s%n", iaddr);
|
|
64 |
|
|
65 |
testChannel(iaddr, family);
|
|
66 |
testSocket(iaddr, family);
|
|
67 |
}
|
|
68 |
|
|
69 |
// Tests SocketChannel and ServerSocketChannel
|
|
70 |
static void testChannel(InetAddress iaddr, ProtocolFamily family) throws Exception {
|
|
71 |
out.printf("--- testChannel iaddr:%s, family:%s%n", iaddr, family);
|
|
72 |
|
|
73 |
try (ServerSocketChannel ssc = RdmaSockets.openServerSocketChannel(family)) {
|
|
74 |
ssc.bind(new InetSocketAddress(iaddr, 0));
|
|
75 |
final int port = ssc.socket().getLocalPort();
|
|
76 |
|
|
77 |
Thread t = new Thread("Channel") {
|
|
78 |
public void run() {
|
|
79 |
try (SocketChannel sc1 = RdmaSockets.openSocketChannel(family)) {
|
|
80 |
sc1.connect(new InetSocketAddress(iaddr, port));
|
|
81 |
if (!sc1.isConnected())
|
|
82 |
throw new RuntimeException("Unconnected channel:" + sc1);
|
|
83 |
ByteBuffer input = ByteBuffer.allocate(MESSAGE_LENGTH);
|
|
84 |
input.put(MESSAGE.getBytes(UTF_8));
|
|
85 |
input.flip();
|
|
86 |
int inputNum = 0;
|
|
87 |
while (inputNum < MESSAGE_LENGTH) {
|
|
88 |
inputNum += sc1.write(input);
|
|
89 |
}
|
|
90 |
sc1.shutdownInput();
|
|
91 |
assertInputShutdown(sc1);
|
|
92 |
sc1.shutdownOutput();
|
|
93 |
assertOutputShutdown(sc1);
|
|
94 |
} catch (IOException e) {
|
|
95 |
throw new UncheckedIOException(e);
|
|
96 |
}
|
|
97 |
}
|
|
98 |
};
|
|
99 |
t.start();
|
|
100 |
|
|
101 |
try (SocketChannel sc2 = ssc.accept()) {
|
|
102 |
ByteBuffer output = ByteBuffer.allocate(MESSAGE_LENGTH + 1);
|
|
103 |
while (sc2.read(output) != -1);
|
|
104 |
output.flip();
|
|
105 |
|
|
106 |
String result = UTF_8.decode(output).toString();
|
|
107 |
if (!result.equals(MESSAGE)) {
|
|
108 |
String msg = format("Expected [%s], received [%s]", MESSAGE, result);
|
|
109 |
throw new RuntimeException("Test Failed! " + msg);
|
|
110 |
}
|
|
111 |
sc2.shutdownInput();
|
|
112 |
assertInputShutdown(sc2);
|
|
113 |
sc2.shutdownOutput();
|
|
114 |
assertOutputShutdown(sc2);
|
|
115 |
}
|
|
116 |
t.join();
|
|
117 |
out.printf("passed%n");
|
|
118 |
}
|
|
119 |
}
|
|
120 |
|
|
121 |
// Tests SocketChannel.socket() and ServerSocketChannel.socket()
|
|
122 |
static void testSocket(InetAddress iaddr, ProtocolFamily family) throws Exception {
|
|
123 |
out.printf("--- testSocket iaddr:%s, family:%s%n", iaddr, family);
|
|
124 |
|
|
125 |
try (ServerSocketChannel ssc = RdmaSockets.openServerSocketChannel(family)) {
|
|
126 |
ssc.socket().bind(new InetSocketAddress(iaddr, 0));
|
|
127 |
final int port = ssc.socket().getLocalPort();
|
|
128 |
|
|
129 |
Thread t = new Thread("Socket") {
|
|
130 |
public void run() {
|
|
131 |
try (SocketChannel sc1 = RdmaSockets.openSocketChannel(family);
|
|
132 |
Socket client = sc1.socket()) {
|
|
133 |
client.connect(new InetSocketAddress(iaddr, port));
|
|
134 |
if (!client.isConnected())
|
|
135 |
throw new RuntimeException("Test Failed!");
|
|
136 |
InputStream is = client.getInputStream();
|
|
137 |
OutputStream os = client.getOutputStream();
|
|
138 |
os.write(MESSAGE.getBytes(UTF_8));
|
|
139 |
|
|
140 |
client.shutdownInput();
|
|
141 |
assertInputShutdown(sc1);
|
|
142 |
assertInputShutdown(client, is);
|
|
143 |
client.shutdownOutput();
|
|
144 |
assertOutputShutdown(sc1);
|
|
145 |
assertOutputShutdown(client, os);
|
|
146 |
} catch (IOException e) {
|
|
147 |
throw new UncheckedIOException(e);
|
|
148 |
}
|
|
149 |
}
|
|
150 |
};
|
|
151 |
t.start();
|
|
152 |
|
|
153 |
try (Socket conn = ssc.socket().accept()) {
|
|
154 |
InputStream is = conn.getInputStream();
|
|
155 |
OutputStream os = conn.getOutputStream();
|
|
156 |
byte[] buf = is.readAllBytes();
|
|
157 |
|
|
158 |
String result = new String(buf, UTF_8);
|
|
159 |
if (!result.equals(MESSAGE)) {
|
|
160 |
String msg = format("Expected [%s], received [%s]", MESSAGE, result);
|
|
161 |
throw new RuntimeException("Test Failed! " + msg);
|
|
162 |
}
|
|
163 |
conn.shutdownInput();
|
|
164 |
assertInputShutdown(conn, is);
|
|
165 |
conn.shutdownOutput();
|
|
166 |
assertOutputShutdown(conn, os);
|
|
167 |
}
|
|
168 |
t.join();
|
|
169 |
out.printf("passed%n");
|
|
170 |
}
|
|
171 |
}
|
|
172 |
|
|
173 |
static void assertInputShutdown(SocketChannel sc) throws IOException {
|
|
174 |
ByteBuffer bb = ByteBuffer.allocate(1);
|
|
175 |
int r = sc.read(bb);
|
|
176 |
if (r != -1)
|
|
177 |
throw new RuntimeException(format("Unexpected read of %d bytes", r));
|
|
178 |
|
|
179 |
try {
|
|
180 |
sc.socket().getInputStream();
|
|
181 |
} catch (IOException expected) {
|
|
182 |
String msg = expected.getMessage();
|
|
183 |
if (!msg.contains("input"))
|
|
184 |
throw new RuntimeException("Expected to find \"input\" in " + expected);
|
|
185 |
if (!msg.contains("shutdown"))
|
|
186 |
throw new RuntimeException("Expected to find \"shutdown\" in " + expected);
|
|
187 |
}
|
|
188 |
}
|
|
189 |
|
|
190 |
static void assertOutputShutdown(SocketChannel sc) throws IOException {
|
|
191 |
ByteBuffer bb = ByteBuffer.allocate(1);
|
|
192 |
bb.put((byte)0x05);
|
|
193 |
bb.flip();
|
|
194 |
try {
|
|
195 |
sc.write(bb);
|
|
196 |
throw new RuntimeException("Unexpected write of bytes");
|
|
197 |
} catch (ClosedChannelException expected) { }
|
|
198 |
|
|
199 |
try {
|
|
200 |
sc.socket().getOutputStream();
|
|
201 |
} catch (IOException expected) {
|
|
202 |
String msg = expected.getMessage();
|
|
203 |
if (!msg.contains("output"))
|
|
204 |
throw new RuntimeException("Expected to find \"output\" in " + expected);
|
|
205 |
if (!msg.contains("shutdown"))
|
|
206 |
throw new RuntimeException("Expected to find \"shutdown\" in " + expected);
|
|
207 |
}
|
|
208 |
}
|
|
209 |
|
|
210 |
static void assertInputShutdown(Socket s, InputStream is) throws IOException {
|
|
211 |
if (!s.isInputShutdown()) {
|
|
212 |
throw new RuntimeException("Unexpected open input: " + s);
|
|
213 |
}
|
|
214 |
int r;
|
|
215 |
if ((r = is.read()) != -1)
|
|
216 |
throw new RuntimeException(format("Unexpected read of %d", r));
|
|
217 |
|
|
218 |
try {
|
|
219 |
s.getInputStream();
|
|
220 |
} catch (IOException expected) {
|
|
221 |
String msg = expected.getMessage();
|
|
222 |
if (!msg.contains("input"))
|
|
223 |
throw new RuntimeException("Expected to find \"input\" in " + expected);
|
|
224 |
if (!msg.contains("shutdown"))
|
|
225 |
throw new RuntimeException("Expected to find \"shutdown\" in " + expected);
|
|
226 |
}
|
|
227 |
}
|
|
228 |
|
|
229 |
static void assertOutputShutdown(Socket s, OutputStream os) throws IOException {
|
|
230 |
if (!s.isOutputShutdown()) {
|
|
231 |
throw new RuntimeException("Unexpected open output: " + s);
|
|
232 |
}
|
|
233 |
try {
|
|
234 |
os.write((byte)0x07);
|
|
235 |
throw new RuntimeException("Unexpected write of bytes");
|
|
236 |
} catch (ClosedChannelException expected) { }
|
|
237 |
|
|
238 |
try {
|
|
239 |
s.getOutputStream();
|
|
240 |
} catch (IOException expected) {
|
|
241 |
String msg = expected.getMessage();
|
|
242 |
if (!msg.contains("output"))
|
|
243 |
throw new RuntimeException("Expected to find \"output\" in " + expected);
|
|
244 |
if (!msg.contains("shutdown"))
|
|
245 |
throw new RuntimeException("Expected to find \"shutdown\" in " + expected);
|
|
246 |
}
|
|
247 |
}
|
|
248 |
}
|