1 /* 2 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 8195160 26 * @summary Socket spec should clarify what getInetAddress/getPort/etc return 27 * after the Socket is closed 28 * @requires (os.family == "linux") 29 * @library .. /test/lib 30 * @build RsocketTest 31 * @run main/othervm -Djava.net.preferIPv4Stack=true TestAfterClose 32 */ 33 import java.net.*; 34 import java.io.*; 35 import jdk.net.Sockets; 36 37 public class TestAfterClose implements Runnable 38 { 39 static int failCount; 40 static ServerSocket ss; 41 static Socket socket; 42 static InetAddress ia; 43 44 public static void main(String[] args) { 45 if (!RsocketTest.isRsocketAvailable()) 46 return; 47 48 try { 49 ia = InetAddress.getLocalHost(); 50 ss = Sockets.openRdmaServerSocket(); 51 ss.bind(new InetSocketAddress(ia, 0)); 52 socket = Sockets.openRdmaSocket(); 53 54 Thread t = new Thread(new TestAfterClose()); 55 t.start(); 56 57 ss.accept(); 58 ss.close(); 59 test(socket); 60 } catch (IOException ioe) { 61 ioe.printStackTrace(); 62 } 63 64 if (failCount > 0) 65 throw new RuntimeException("Failed: failcount = " + failCount); 66 67 } 68 69 public void run() { 70 try { 71 socket.connect(new InetSocketAddress(ia, ss.getLocalPort())); 72 } catch (Exception e) { 73 e.printStackTrace(); 74 throw new RuntimeException("Test Failed"); 75 } 76 } 77 78 static void test(Socket socket) throws IOException { 79 //Before Close 80 int socketPort = socket.getPort(); 81 InetAddress socketInetAddress = socket.getInetAddress(); 82 SocketAddress socketRemoteSocketAddress = socket.getRemoteSocketAddress(); 83 int socketLocalPort = socket.getLocalPort(); 84 85 //After Close 86 socket.close(); 87 88 if (socketPort != socket.getPort()) { 89 System.out.println("Socket.getPort failed"); 90 failCount++; 91 } 92 93 if (!socket.getInetAddress().equals(socketInetAddress)) { 94 System.out.println("Socket.getInetAddress failed"); 95 failCount++; 96 } 97 98 if (!socket.getRemoteSocketAddress().equals(socketRemoteSocketAddress)) { 99 System.out.println("Socket.getRemoteSocketAddresss failed"); 100 failCount++; 101 } 102 103 if (socketLocalPort != socket.getLocalPort()) { 104 System.out.println("Socket.getLocalPort failed"); 105 failCount++; 106 } 107 108 InetAddress anyAddr = null; 109 try { 110 anyAddr = InetAddress.getByAddress("",new byte[] {0,0,0,0}); 111 } catch (UnknownHostException uhe) { 112 } 113 114 if (anyAddr != null && !socket.getLocalAddress().equals(anyAddr)) { 115 System.out.println("Socket.getLocalAddress failed"); 116 failCount++; 117 } 118 119 InetSocketAddress addr = new InetSocketAddress(socket.getLocalPort()); 120 if (!socket.getLocalSocketAddress().equals(addr)) { 121 System.out.println("Socket.getLocalSocketAddress failed"); 122 failCount++; 123 } 124 125 if (!socket.isConnected()) { 126 System.out.println("Socket.isConnected failed"); 127 failCount++; 128 } 129 130 if (!socket.isBound()) { 131 System.out.println("Socket.isBound failed"); 132 failCount++; 133 } 134 } 135 }