Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/Socket/TestAfterClose.java
38821 views
/*1* Copyright (c) 2007, 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* @summary Socket spec should clarify what getInetAddress/getPort/etc return after the Socket is closed27*/2829import java.net.*;30import java.io.*;3132public class TestAfterClose33{34static int failCount;3536public static void main(String[] args) {37try {38ServerSocket ss = new ServerSocket(0, 0, null);39Socket socket = new Socket("localhost", ss.getLocalPort());40ss.accept();41ss.close();42test(socket);43} catch (IOException ioe) {44ioe.printStackTrace();45}4647if (failCount > 0)48throw new RuntimeException("Failed: failcount = " + failCount);4950}5152static void test(Socket socket) throws IOException {53//Before Close54int socketPort = socket.getPort();55InetAddress socketInetAddress = socket.getInetAddress();56SocketAddress socketRemoteSocketAddress = socket.getRemoteSocketAddress();57int socketLocalPort = socket.getLocalPort();5859//After Close60socket.close();6162if (socketPort != socket.getPort()) {63System.out.println("Socket.getPort failed");64failCount++;65}6667if (!socket.getInetAddress().equals(socketInetAddress)) {68System.out.println("Socket.getInetAddress failed");69failCount++;70}7172if (!socket.getRemoteSocketAddress().equals(socketRemoteSocketAddress)) {73System.out.println("Socket.getRemoteSocketAddresss failed");74failCount++;75}7677if (socketLocalPort != socket.getLocalPort()) {78System.out.println("Socket.getLocalPort failed");79failCount++;80}8182InetAddress anyAddr = null;83try {84anyAddr = InetAddress.getByAddress("",new byte[] {0,0,0,0});85} catch (UnknownHostException uhe) {86}8788if (anyAddr != null && !socket.getLocalAddress().equals(anyAddr)) {89System.out.println("Socket.getLocalAddress failed");90failCount++;91}9293InetSocketAddress addr = new InetSocketAddress(socket.getLocalPort());94if (!socket.getLocalSocketAddress().equals(addr)) {95System.out.println("Socket.getLocalSocketAddress failed");96failCount++;97}9899if (!socket.isConnected()) {100System.out.println("Socket.isConnected failed");101failCount++;102}103104if (!socket.isBound()) {105System.out.println("Socket.isBound failed");106failCount++;107}108}109}110111112