0N/A/*
3261N/A * Copyright (c) 2001, 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 4455376
0N/A * @summary Ensure that socket objects obtained from channels
0N/A * carry the correct address information
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.nio.*;
0N/Aimport java.nio.channels.*;
0N/A
0N/A
0N/Apublic class Shadow {
0N/A
0N/A static PrintStream log = System.err;
0N/A
0N/A private static void dump(ServerSocket s) {
0N/A log.println("getInetAddress(): " + s.getInetAddress());
0N/A log.println("getLocalPort(): " + s.getLocalPort());
0N/A }
0N/A
0N/A private static void dump(Socket s) {
0N/A log.println("getInetAddress(): " + s.getInetAddress());
0N/A log.println("getPort(): " + s.getPort());
0N/A log.println("getLocalAddress(): " + s.getLocalAddress());
0N/A log.println("getLocalPort(): " + s.getLocalPort());
0N/A }
0N/A
0N/A private static int problems = 0;
0N/A
0N/A private static void problem(String s) {
0N/A log.println("FAILURE: " + s);
0N/A problems++;
0N/A }
0N/A
0N/A private static void check(Socket s) {
0N/A if (s.getPort() == 0)
0N/A problem("Socket has no port");
0N/A if (s.getLocalPort() == 0)
0N/A problem("Socket has no local port");
0N/A if (!s.getLocalAddress().equals(s.getInetAddress()))
0N/A problem("Socket has wrong local address");
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A boolean useChannels
0N/A = ((args.length == 0) || Boolean.valueOf(args[0]).booleanValue());
2546N/A int port = (args.length > 1 ? Integer.parseInt(args[1]) : -1);
0N/A
0N/A // open server socket
0N/A ServerSocket serverSocket;
0N/A if (useChannels) {
0N/A ServerSocketChannel serverSocketChannel =
0N/A ServerSocketChannel.open();
0N/A log.println("opened ServerSocketChannel: " +
0N/A serverSocketChannel);
0N/A serverSocket = serverSocketChannel.socket();
0N/A log.println("associated ServerSocket: " + serverSocket);
0N/A } else {
0N/A serverSocket = new ServerSocket();
0N/A log.println("opened ServerSocket: " + serverSocket);
0N/A }
0N/A
0N/A // bind server socket to port
2546N/A SocketAddress bindAddr =
2546N/A new InetSocketAddress((port == -1) ? 0 : port);
0N/A serverSocket.bind(bindAddr);
0N/A log.println("bound ServerSocket: " + serverSocket);
0N/A
0N/A log.println();
0N/A
0N/A // open client socket
0N/A Socket socket;
0N/A if (useChannels) {
0N/A SocketChannel socketChannel = SocketChannel.open();
0N/A log.println("opened SocketChannel: " + socketChannel);
0N/A
0N/A socket = socketChannel.socket();
0N/A log.println("associated Socket: " + socket);
0N/A } else {
0N/A socket = new Socket();
0N/A log.println("opened Socket: " + socket);
0N/A }
0N/A
0N/A // connect client socket to port
0N/A SocketAddress connectAddr =
0N/A new InetSocketAddress("127.0.0.1",
0N/A serverSocket.getLocalPort());
0N/A socket.connect(connectAddr);
0N/A log.println("connected Socket: " + socket);
0N/A
0N/A log.println();
0N/A
0N/A // accept connection
0N/A Socket acceptedSocket = serverSocket.accept();
0N/A log.println("accepted Socket: " + acceptedSocket);
0N/A
0N/A log.println();
0N/A log.println("========================================");
0N/A
0N/A log.println("*** ServerSocket info: ");
0N/A dump(serverSocket);
0N/A log.println();
0N/A
0N/A log.println("*** client Socket info: ");
0N/A dump(socket);
0N/A check(socket);
0N/A log.println();
0N/A
0N/A log.println("*** accepted Socket info: ");
0N/A dump(acceptedSocket);
0N/A check(acceptedSocket);
0N/A log.println();
0N/A
0N/A if (problems > 0)
0N/A throw new Exception(problems + " tests failed");
0N/A }
0N/A
0N/A}