Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/sdp/Sanity.java
38839 views
/*1* Copyright (c) 2009, 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 java.net.*;24import java.io.*;25import java.nio.channels.*;26import java.util.Enumeration;2728/**29* Sanity check Socket/ServerSocket and each of the stream-oriented channels30* on each IP address plumbed to the network adapters.31*/3233public class Sanity {34public static void main(String[] args) throws Exception {35Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();36while (nifs.hasMoreElements()) {37NetworkInterface ni = nifs.nextElement();38Enumeration<InetAddress> addrs = ni.getInetAddresses();39while (addrs.hasMoreElements()) {40InetAddress addr = addrs.nextElement();41test(addr);42}43}44}4546static void test(InetAddress addr) throws Exception {47System.out.println(addr.getHostAddress());4849// ServerSocketChannel.bind50ServerSocketChannel ssc = ServerSocketChannel.open();51try {52ssc.bind(new InetSocketAddress(addr, 0));53int port = ((InetSocketAddress)(ssc.getLocalAddress())).getPort();5455// SocketChannel.connect (implicit bind)56SocketChannel client = SocketChannel.open();57try {58client.connect(new InetSocketAddress(addr, port));59SocketChannel peer = ssc.accept();60try {61testConnection(Channels.newOutputStream(client),62Channels.newInputStream(peer));63} finally {64peer.close();65}66} finally {67client.close();68}6970// SocketChannel.connect (explicit bind)71client = SocketChannel.open();72try {73client.bind(new InetSocketAddress(addr, 0))74.connect(new InetSocketAddress(addr, port));75ssc.accept().close();76} finally {77client.close();78}79} finally {80ssc.close();81}8283// AsynchronousServerSocketChannel.bind84AsynchronousServerSocketChannel server =85AsynchronousServerSocketChannel.open();86try {87server.bind(new InetSocketAddress(addr, 0));88int port = ((InetSocketAddress)(server.getLocalAddress())).getPort();8990// AsynchronousSocketChannel.connect (implicit bind)91AsynchronousSocketChannel client = AsynchronousSocketChannel.open();92try {93client.connect(new InetSocketAddress(addr, port)).get();94AsynchronousSocketChannel peer = server.accept().get();95try {96testConnection(Channels.newOutputStream(client),97Channels.newInputStream(peer));98} finally {99peer.close();100}101} finally {102client.close();103}104105// AsynchronousSocketChannel.connect (explicit bind)106client = AsynchronousSocketChannel.open();107try {108client.bind(new InetSocketAddress(addr, 0))109.connect(new InetSocketAddress(addr, port)).get();110server.accept().get().close();111} finally {112client.close();113}114} finally {115server.close();116}117118// ServerSocket.bind119ServerSocket ss = new ServerSocket();120try {121ss.bind(new InetSocketAddress(addr, 0));122int port = ss.getLocalPort();123124// Socket.connect (implicit bind)125Socket s = new Socket();126try {127s.connect(new InetSocketAddress(addr, port));128Socket peer = ss.accept();129try {130testConnection(s.getOutputStream(), peer.getInputStream());131} finally {132peer.close();133}134} finally {135s.close();136}137138// Socket.connect (explicit bind)139s = new Socket();140try {141s.bind(new InetSocketAddress(addr, 0));142s.connect(new InetSocketAddress(addr, port));143ss.accept().close();144} finally {145s.close();146}147} finally {148ss.close();149}150}151152static void testConnection(OutputStream out, InputStream in)153throws IOException154{155byte[] msg = "hello".getBytes();156out.write(msg);157158byte[] ba = new byte[100];159int nread = 0;160while (nread < msg.length) {161int n = in.read(ba);162if (n < 0)163throw new IOException("EOF not expected!");164nread += n;165}166}167}168169170