Path: blob/master/test/jdk/java/nio/channels/SocketChannel/UnboundSocketTests.java
41154 views
/*1* Copyright (c) 2006, 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*/2223/* @test24* @bug 644207325* @summary Check getXXX methods for local/remote port/address/socketaddress26* match socket spec for unbound case27*/28import java.net.*;29import java.nio.channels.*;3031public class UnboundSocketTests {3233static int failures = 0;3435static void check(String msg, Object actual, Object expected) {36System.out.format("%s expected: %s, actual: %s", msg, expected, actual);37if (actual == expected) {38System.out.println(" [PASS]");39} else {40System.out.println(" [FAIL]");41failures++;42}43}4445static void checkIsAnyLocalAddress(String msg, InetAddress actual) {46System.out.format("%s actual: %s", msg, actual);47if (actual.isAnyLocalAddress()) {48System.out.println(" [PASS]");49} else {50System.out.println(" [FAIL]");51failures++;52}53}5455public static void main(String[] args) throws Exception {56System.out.println("\n-- SocketChannel --");5758SocketChannel sc = SocketChannel.open();59try {60check("getLocalPort()", sc.socket().getLocalPort(), -1);61checkIsAnyLocalAddress("getLocalAddress()",62sc.socket().getLocalAddress());63check("getLocalSocketAddress()", sc.socket().getLocalSocketAddress(), null);6465check("getPort()", sc.socket().getPort(), 0);66check("getInetAddress()", sc.socket().getInetAddress(), null);67check("getRemoteSocketAddress()", sc.socket().getRemoteSocketAddress(), null);68} finally {69sc.close();70}7172System.out.println("\n-- ServerSocketChannel --");7374ServerSocketChannel ssc = ServerSocketChannel.open();75try {76check("getLocalPort()", ssc.socket().getLocalPort(), -1);77check("getInetAddress()", ssc.socket().getInetAddress(), null);78check("getLocalSocketAddress()", ssc.socket().getLocalSocketAddress(), null);79} finally {80ssc.close();81}8283System.out.println("\n-- DatagramChannel --");8485DatagramChannel dc = DatagramChannel.open();86try {87// not specified88check("getLocalPort()", dc.socket().getLocalPort(), 0);8990checkIsAnyLocalAddress("getLocalAddress()",91dc.socket().getLocalAddress());92check("getLocalSocketAddress()", dc.socket().getLocalSocketAddress(), null);9394check("getPort()", dc.socket().getPort(), -1);95check("getInetAddress()", dc.socket().getInetAddress(), null);96check("getRemoteSocketAddress()", dc.socket().getRemoteSocketAddress(), null);97} finally {98dc.close();99}100101if (failures > 0) {102throw new RuntimeException(failures + " sub-tests(s) failed.");103}104105}106}107108109