Path: blob/master/test/jdk/java/nio/channels/etc/Shadow.java
41153 views
/*1* Copyright (c) 2001, 2019, 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 445537625* @summary Ensure that socket objects obtained from channels26* carry the correct address information27*/2829import java.io.*;30import java.net.*;31import java.nio.*;32import java.nio.channels.*;333435public class Shadow {3637static PrintStream log = System.err;3839private static void dump(ServerSocket s) {40log.println("getInetAddress(): " + s.getInetAddress());41log.println("getLocalPort(): " + s.getLocalPort());42}4344private static void dump(Socket s) {45log.println("getInetAddress(): " + s.getInetAddress());46log.println("getPort(): " + s.getPort());47log.println("getLocalAddress(): " + s.getLocalAddress());48log.println("getLocalPort(): " + s.getLocalPort());49}5051private static int problems = 0;5253private static void problem(String s) {54log.println("FAILURE: " + s);55problems++;56}5758private static void check(Socket s) {59if (s.getPort() == 0)60problem("Socket has no port");61if (s.getLocalPort() == 0)62problem("Socket has no local port");63if (!s.getLocalAddress().equals(s.getInetAddress()))64problem("Socket has wrong local address");65}6667public static void main(String[] args) throws Exception {68boolean useChannels69= ((args.length == 0) || Boolean.valueOf(args[0]).booleanValue());70int port = (args.length > 1 ? Integer.parseInt(args[1]) : -1);7172// open server socket73ServerSocket serverSocket;74if (useChannels) {75ServerSocketChannel serverSocketChannel =76ServerSocketChannel.open();77log.println("opened ServerSocketChannel: " +78serverSocketChannel);79serverSocket = serverSocketChannel.socket();80log.println("associated ServerSocket: " + serverSocket);81} else {82serverSocket = new ServerSocket();83log.println("opened ServerSocket: " + serverSocket);84}8586// bind server socket to port87SocketAddress bindAddr =88new InetSocketAddress((port == -1) ? 0 : port);89serverSocket.bind(bindAddr);90log.println("bound ServerSocket: " + serverSocket);9192log.println();9394// open client socket95Socket socket;96if (useChannels) {97SocketChannel socketChannel = SocketChannel.open();98log.println("opened SocketChannel: " + socketChannel);99100socket = socketChannel.socket();101log.println("associated Socket: " + socket);102} else {103socket = new Socket();104log.println("opened Socket: " + socket);105}106107// connect client socket to port108SocketAddress connectAddr =109new InetSocketAddress(InetAddress.getLoopbackAddress(),110serverSocket.getLocalPort());111socket.connect(connectAddr);112log.println("connected Socket: " + socket);113114log.println();115116// accept connection117Socket acceptedSocket = serverSocket.accept();118log.println("accepted Socket: " + acceptedSocket);119120log.println();121log.println("========================================");122123log.println("*** ServerSocket info: ");124dump(serverSocket);125log.println();126127log.println("*** client Socket info: ");128dump(socket);129check(socket);130log.println();131132log.println("*** accepted Socket info: ");133dump(acceptedSocket);134check(acceptedSocket);135log.println();136137if (problems > 0)138throw new Exception(problems + " tests failed");139}140141}142143144