Path: blob/master/test/jdk/java/net/Socket/TestClose.java
41149 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*/22/*23* @test24* @bug 440875525* @library /test/lib26* @summary This tests whether it's possible to get some informations27* out of a closed socket. This is for backward compatibility28* purposes.29* @run main TestClose30* @run main/othervm -Djava.net.preferIPv4Stack=true TestClose31*/3233import java.net.*;34import jdk.test.lib.net.IPSupport;3536public class TestClose {3738public static void main(String[] args) throws Exception {39IPSupport.throwSkippedExceptionIfNonOperational();4041ServerSocket ss;42Socket s;43InetAddress ad1, ad2;44int port1, port2, serverport;4546InetAddress loopback = InetAddress.getLoopbackAddress();47ss = new ServerSocket();48ss.bind(new InetSocketAddress(loopback, 0));49serverport = ss.getLocalPort();50s = new Socket(loopback, serverport);51s.close();52ss.close();53ad1 = ss.getInetAddress();54if (ad1 == null)55throw new RuntimeException("ServerSocket.getInetAddress() returned null");56port1 = ss.getLocalPort();57if (port1 != serverport)58throw new RuntimeException("ServerSocket.getLocalPort() returned the wrong value");59ad2 = s.getInetAddress();60if (ad2 == null)61throw new RuntimeException("Socket.getInetAddress() returned null");62port2 = s.getPort();63if (port2 != serverport)64throw new RuntimeException("Socket.getPort() returned wrong value");65ad2 = s.getLocalAddress();66if (ad2 == null)67throw new RuntimeException("Socket.getLocalAddress() returned null");68port2 = s.getLocalPort();69if (port2 == -1)70throw new RuntimeException("Socket.getLocalPort returned -1");71}72}737475