Path: blob/master/test/jdk/java/net/Socket/TestAfterClose.java
41152 views
/*1* Copyright (c) 2007, 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/*24* @test25* @bug 650501626* @library /test/lib27* @summary Socket spec should clarify what getInetAddress/getPort/etc return28* after the Socket is closed29* @run main TestAfterClose30* @run main/othervm -Djava.net.preferIPv4Stack=true TestAfterClose31*/3233import java.net.*;34import java.io.*;35import jdk.test.lib.net.IPSupport;3637public class TestAfterClose38{39static int failCount;4041public static void main(String[] args) {42IPSupport.throwSkippedExceptionIfNonOperational();4344try {45InetAddress loopback = InetAddress.getLoopbackAddress();46ServerSocket ss = new ServerSocket(0, 0, loopback);47Socket socket = new Socket(loopback, ss.getLocalPort());48ss.accept();49ss.close();50test(socket);51} catch (IOException ioe) {52ioe.printStackTrace();53}5455if (failCount > 0)56throw new RuntimeException("Failed: failcount = " + failCount);5758}5960static void test(Socket socket) throws IOException {61//Before Close62int socketPort = socket.getPort();63InetAddress socketInetAddress = socket.getInetAddress();64SocketAddress socketRemoteSocketAddress = socket.getRemoteSocketAddress();65int socketLocalPort = socket.getLocalPort();6667//After Close68socket.close();6970if (socketPort != socket.getPort()) {71System.out.println("Socket.getPort failed");72failCount++;73}7475if (!socket.getInetAddress().equals(socketInetAddress)) {76System.out.println("Socket.getInetAddress failed");77failCount++;78}7980if (!socket.getRemoteSocketAddress().equals(socketRemoteSocketAddress)) {81System.out.println("Socket.getRemoteSocketAddresss failed");82failCount++;83}8485if (socketLocalPort != socket.getLocalPort()) {86System.out.println("Socket.getLocalPort failed");87failCount++;88}8990InetAddress anyAddr = null;91try {92anyAddr = InetAddress.getByAddress("",new byte[] {0,0,0,0});93} catch (UnknownHostException uhe) {94}9596if (anyAddr != null && !socket.getLocalAddress().equals(anyAddr)) {97System.out.println("Socket.getLocalAddress failed");98failCount++;99}100101InetSocketAddress addr = new InetSocketAddress(socket.getLocalPort());102if (!socket.getLocalSocketAddress().equals(addr)) {103System.out.println("Socket.getLocalSocketAddress failed");104failCount++;105}106107if (!socket.isConnected()) {108System.out.println("Socket.isConnected failed");109failCount++;110}111112if (!socket.isBound()) {113System.out.println("Socket.isBound failed");114failCount++;115}116}117}118119120