0N/A/*
3261N/A * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 6442073
0N/A * @summary Check getXXX methods for local/remote port/address/socketaddress
0N/A * match socket spec for unbound case
0N/A */
0N/Aimport java.net.*;
0N/Aimport java.nio.channels.*;
0N/A
0N/Apublic class UnboundSocketTests {
0N/A
0N/A static int failures = 0;
0N/A
0N/A static void check(String msg, Object actual, Object expected) {
0N/A System.out.format("%s expected: %s, actual: %s", msg, expected, actual);
0N/A if (actual == expected) {
0N/A System.out.println(" [PASS]");
0N/A } else {
0N/A System.out.println(" [FAIL]");
0N/A failures++;
0N/A }
0N/A }
0N/A
0N/A static void checkIsAnyLocalAddress(String msg, InetAddress actual) {
0N/A System.out.format("%s actual: %s", msg, actual);
0N/A if (actual.isAnyLocalAddress()) {
0N/A System.out.println(" [PASS]");
0N/A } else {
0N/A System.out.println(" [FAIL]");
0N/A failures++;
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A System.out.println("\n-- SocketChannel --");
0N/A
0N/A SocketChannel sc = SocketChannel.open();
2546N/A try {
2546N/A check("getLocalPort()", sc.socket().getLocalPort(), -1);
2546N/A checkIsAnyLocalAddress("getLocalAddress()",
2546N/A sc.socket().getLocalAddress());
2546N/A check("getLocalSocketAddress()", sc.socket().getLocalSocketAddress(), null);
0N/A
2546N/A check("getPort()", sc.socket().getPort(), 0);
2546N/A check("getInetAddress()", sc.socket().getInetAddress(), null);
2546N/A check("getRemoteSocketAddress()", sc.socket().getRemoteSocketAddress(), null);
2546N/A } finally {
2546N/A sc.close();
2546N/A }
0N/A
0N/A System.out.println("\n-- ServerSocketChannel --");
0N/A
0N/A ServerSocketChannel ssc = ServerSocketChannel.open();
2546N/A try {
2546N/A check("getLocalPort()", ssc.socket().getLocalPort(), -1);
2546N/A check("getInetAddress()", ssc.socket().getInetAddress(), null);
2546N/A check("getLocalSocketAddress()", ssc.socket().getLocalSocketAddress(), null);
2546N/A } finally {
2546N/A ssc.close();
2546N/A }
0N/A
0N/A System.out.println("\n-- DatagramChannel --");
0N/A
0N/A DatagramChannel dc = DatagramChannel.open();
2546N/A try {
2546N/A // not specified
2546N/A check("getLocalPort()", dc.socket().getLocalPort(), 0);
0N/A
2546N/A checkIsAnyLocalAddress("getLocalAddress()",
2546N/A dc.socket().getLocalAddress());
2546N/A check("getLocalSocketAddress()", dc.socket().getLocalSocketAddress(), null);
0N/A
2546N/A check("getPort()", dc.socket().getPort(), -1);
2546N/A check("getInetAddress()", dc.socket().getInetAddress(), null);
2546N/A check("getRemoteSocketAddress()", dc.socket().getRemoteSocketAddress(), null);
2546N/A } finally {
2546N/A dc.close();
2546N/A }
0N/A
0N/A if (failures > 0) {
0N/A throw new RuntimeException(failures + " sub-tests(s) failed.");
0N/A }
0N/A
0N/A }
0N/A}