Path: blob/master/test/jdk/java/nio/channels/etc/NetworkChannelTests.java
41153 views
/*1* Copyright (c) 2007, 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*/2223/* @test24* @bug 464054425* @summary Unit test for channels that implement NetworkChannel26*/2728import java.nio.*;29import java.nio.channels.*;30import java.net.*;31import java.io.IOException;32import java.util.*;3334public class NetworkChannelTests {3536static interface ChannelFactory {37NetworkChannel open() throws IOException;38}3940static class BogusSocketAddress extends SocketAddress {41}4243/**44* Exercise bind method.45*/46static void bindTests(ChannelFactory factory) throws IOException {47NetworkChannel ch;4849// AlreadyBoundException50ch = factory.open().bind(new InetSocketAddress(0));51try {52ch.bind(new InetSocketAddress(0));53throw new RuntimeException("AlreadyBoundException not thrown");54} catch (AlreadyBoundException x) {55}56ch.close();5758// bind(null)59ch = factory.open().bind(null);60if (ch.getLocalAddress() == null)61throw new RuntimeException("socket not found");62ch.close();6364// UnsupportedAddressTypeException65ch = factory.open();66try {67ch.bind(new BogusSocketAddress());68throw new RuntimeException("UnsupportedAddressTypeException not thrown");69} catch (UnsupportedAddressTypeException x) {70}71ch.close();7273// ClosedChannelException74try {75ch.bind(new InetSocketAddress(0));76throw new RuntimeException("ClosedChannelException not thrown");77} catch (ClosedChannelException x) {78}79}8081/**82* Exercise getLocalAddress method.83*/84static void localAddressTests(ChannelFactory factory) throws IOException {85NetworkChannel ch;8687// not bound88ch = factory.open();89if (ch.getLocalAddress() != null) {90throw new RuntimeException("Local address returned when not bound");91}9293// bound94InetSocketAddress local =95(InetSocketAddress)(ch.bind(new InetSocketAddress(0)).getLocalAddress());96if (!local.getAddress().isAnyLocalAddress()) {97if (NetworkInterface.getByInetAddress(local.getAddress()) == null)98throw new RuntimeException("not bound to local address");99}100if (local.getPort() <= 0)101throw new RuntimeException("not bound to local port");102103// closed104ch.close();105try {106ch.getLocalAddress();107throw new RuntimeException("ClosedChannelException expected");108} catch (ClosedChannelException e) { }109}110111/**112* Exercise getRemoteAddress method (SocketChannel only)113*/114static void connectedAddressTests() throws IOException {115ServerSocketChannel ssc = ServerSocketChannel.open()116.bind(new InetSocketAddress(0));117InetSocketAddress local = (InetSocketAddress)(ssc.getLocalAddress());118int port = local.getPort();119InetSocketAddress server = new InetSocketAddress(InetAddress.getLocalHost(), port);120121SocketChannel sc = SocketChannel.open();122123// not connected124if (sc.getRemoteAddress() != null)125throw new RuntimeException("getRemoteAddress returned address when not connected");126127// connected128sc.connect(server);129SocketAddress remote = sc.getRemoteAddress();130if (!remote.equals(server))131throw new RuntimeException("getRemoteAddress returned incorrect address");132133// closed134sc.close();135try {136sc.getRemoteAddress();137throw new RuntimeException("ClosedChannelException expected");138} catch (ClosedChannelException e) { }139140ssc.close();141}142143public static void main(String[] args) throws IOException {144ChannelFactory factory;145146// -- SocketChannel --147148factory = new ChannelFactory() {149public NetworkChannel open() throws IOException {150return SocketChannel.open();151}152};153154bindTests(factory);155localAddressTests(factory);156connectedAddressTests();157158// -- ServerSocketChannel --159160factory = new ChannelFactory() {161public NetworkChannel open() throws IOException {162return ServerSocketChannel.open();163}164};165166bindTests(factory);167localAddressTests(factory);168169// backlog values170ServerSocketChannel.open()171.bind(new InetSocketAddress(0), 100).close();172ServerSocketChannel.open()173.bind(new InetSocketAddress(0), 0).close();174ServerSocketChannel.open()175.bind(new InetSocketAddress(0), -1).close();176177// -- DatagramChannel --178179factory = new ChannelFactory() {180public NetworkChannel open() throws IOException {181return DatagramChannel.open();182}183};184185bindTests(factory);186localAddressTests(factory);187}188189}190191192