2736N/A/*
2736N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2736N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2736N/A *
2736N/A * This code is free software; you can redistribute it and/or modify it
2736N/A * under the terms of the GNU General Public License version 2 only, as
2736N/A * published by the Free Software Foundation.
2736N/A *
2736N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2736N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2736N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2736N/A * version 2 for more details (a copy is included in the LICENSE file that
2736N/A * accompanied this code).
2736N/A *
2736N/A * You should have received a copy of the GNU General Public License version
2736N/A * 2 along with this work; if not, write to the Free Software Foundation,
2736N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2736N/A *
2736N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2736N/A * or visit www.oracle.com if you need additional information or have any
2736N/A * questions.
2736N/A */
2736N/A
2736N/Aimport com.oracle.net.Sdp;
2736N/A
2736N/Aimport java.net.*;
2736N/Aimport java.io.*;
2736N/Aimport java.nio.channels.*;
2736N/Aimport java.util.*;
2736N/A
2736N/A/**
2736N/A * Exercise com.oracle.net.Sdp with each IP address plumbed to InfiniBand
2736N/A * interfaces listed in a given file.
2736N/A */
2736N/A
2736N/Apublic class Sanity {
2736N/A public static void main(String[] args) throws Exception {
2736N/A // The file is a list of interfaces to test.
2736N/A Scanner s = new Scanner(new File(args[0]));
2736N/A try {
2736N/A while (s.hasNextLine()) {
2736N/A String link = s.nextLine();
2736N/A NetworkInterface ni = NetworkInterface.getByName(link);
2736N/A if (ni != null) {
2736N/A Enumeration<InetAddress> addrs = ni.getInetAddresses();
2736N/A while (addrs.hasMoreElements()) {
2736N/A InetAddress addr = addrs.nextElement();
2736N/A System.out.format("Testing %s: %s\n", link, addr.getHostAddress());
2736N/A test(addr);
2736N/A }
2736N/A }
2736N/A }
2736N/A } finally {
2736N/A s.close();
2736N/A }
2736N/A }
2736N/A
2736N/A static void test(InetAddress addr) throws Exception {
2736N/A // Test SocketChannel and ServerSocketChannel
2736N/A ServerSocketChannel ssc = Sdp.openServerSocketChannel();
2736N/A try {
2736N/A ssc.socket().bind(new InetSocketAddress(addr, 0));
2736N/A int port = ssc.socket().getLocalPort();
2736N/A
2736N/A // SocketChannel.connect (implicit bind)
2736N/A SocketChannel client = Sdp.openSocketChannel();
2736N/A try {
2736N/A client.connect(new InetSocketAddress(addr, port));
2736N/A SocketChannel peer = ssc.accept();
2736N/A try {
2736N/A testConnection(Channels.newOutputStream(client),
2736N/A Channels.newInputStream(peer));
2736N/A } finally {
2736N/A peer.close();
2736N/A }
2736N/A } finally {
2736N/A client.close();
2736N/A }
2736N/A
2736N/A // SocketChannel.connect (explicit bind)
2736N/A client = Sdp.openSocketChannel();
2736N/A try {
2736N/A client.socket().bind(new InetSocketAddress(addr, 0));
2736N/A client.connect(new InetSocketAddress(addr, port));
2736N/A ssc.accept().close();
2736N/A } finally {
2736N/A client.close();
2736N/A }
2736N/A } finally {
2736N/A ssc.close();
2736N/A }
2736N/A
2736N/A // Test Socket and ServerSocket
2736N/A ServerSocket ss = Sdp.openServerSocket();
2736N/A try {
2736N/A ss.bind(new InetSocketAddress(addr, 0));
2736N/A int port = ss.getLocalPort();
2736N/A
2736N/A // Socket.connect (implicit bind)
2736N/A Socket s = Sdp.openSocket();
2736N/A try {
2736N/A s.connect(new InetSocketAddress(addr, port));
2736N/A Socket peer = ss.accept();
2736N/A try {
2736N/A testConnection(s.getOutputStream(), peer.getInputStream());
2736N/A } finally {
2736N/A peer.close();
2736N/A }
2736N/A } finally {
2736N/A s.close();
2736N/A }
2736N/A
2736N/A // Socket.connect (explicit bind)
2736N/A s = Sdp.openSocket();
2736N/A try {
2736N/A s.bind(new InetSocketAddress(addr, 0));
2736N/A s.connect(new InetSocketAddress(addr, port));
2736N/A ss.accept().close();
2736N/A } finally {
2736N/A s.close();
2736N/A }
2736N/A } finally {
2736N/A ss.close();
2736N/A }
2736N/A }
2736N/A
2736N/A static void testConnection(OutputStream out, InputStream in)
2736N/A throws IOException
2736N/A {
2736N/A byte[] msg = "hello".getBytes();
2736N/A out.write(msg);
2736N/A
2736N/A byte[] ba = new byte[100];
2736N/A int nread = 0;
2736N/A while (nread < msg.length) {
2736N/A int n = in.read(ba);
2736N/A if (n < 0)
2736N/A throw new IOException("EOF not expected!");
2736N/A nread += n;
2736N/A }
2736N/A }
2736N/A}