Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/oracle/net/Sanity.java
38838 views
/*1* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import com.oracle.net.Sdp;2425import java.net.*;26import java.io.*;27import java.nio.channels.*;28import java.util.*;2930/**31* Exercise com.oracle.net.Sdp with each IP address plumbed to InfiniBand32* interfaces listed in a given file.33*/3435public class Sanity {36public static void main(String[] args) throws Exception {37// The file is a list of interfaces to test.38Scanner s = new Scanner(new File(args[0]));39try {40while (s.hasNextLine()) {41String link = s.nextLine();42NetworkInterface ni = NetworkInterface.getByName(link);43if (ni != null) {44Enumeration<InetAddress> addrs = ni.getInetAddresses();45while (addrs.hasMoreElements()) {46InetAddress addr = addrs.nextElement();47System.out.format("Testing %s: %s\n", link, addr.getHostAddress());48test(addr);49}50}51}52} finally {53s.close();54}55}5657static void test(InetAddress addr) throws Exception {58// Test SocketChannel and ServerSocketChannel59ServerSocketChannel ssc = Sdp.openServerSocketChannel();60try {61ssc.socket().bind(new InetSocketAddress(addr, 0));62int port = ssc.socket().getLocalPort();6364// SocketChannel.connect (implicit bind)65SocketChannel client = Sdp.openSocketChannel();66try {67client.connect(new InetSocketAddress(addr, port));68SocketChannel peer = ssc.accept();69try {70testConnection(Channels.newOutputStream(client),71Channels.newInputStream(peer));72} finally {73peer.close();74}75} finally {76client.close();77}7879// SocketChannel.connect (explicit bind)80client = Sdp.openSocketChannel();81try {82client.socket().bind(new InetSocketAddress(addr, 0));83client.connect(new InetSocketAddress(addr, port));84ssc.accept().close();85} finally {86client.close();87}88} finally {89ssc.close();90}9192// Test Socket and ServerSocket93ServerSocket ss = Sdp.openServerSocket();94try {95ss.bind(new InetSocketAddress(addr, 0));96int port = ss.getLocalPort();9798// Socket.connect (implicit bind)99Socket s = Sdp.openSocket();100try {101s.connect(new InetSocketAddress(addr, port));102Socket peer = ss.accept();103try {104testConnection(s.getOutputStream(), peer.getInputStream());105} finally {106peer.close();107}108} finally {109s.close();110}111112// Socket.connect (explicit bind)113s = Sdp.openSocket();114try {115s.bind(new InetSocketAddress(addr, 0));116s.connect(new InetSocketAddress(addr, port));117ss.accept().close();118} finally {119s.close();120}121} finally {122ss.close();123}124}125126static void testConnection(OutputStream out, InputStream in)127throws IOException128{129byte[] msg = "hello".getBytes();130out.write(msg);131132byte[] ba = new byte[100];133int nread = 0;134while (nread < msg.length) {135int n = in.read(ba);136if (n < 0)137throw new IOException("EOF not expected!");138nread += n;139}140}141}142143144