J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 6505016 |
| 27 | * @summary Socket spec should clarify what getInetAddress/getPort/etc return after the Socket is closed |
| 28 | */ |
| 29 | |
| 30 | import java.net.*; |
| 31 | import java.io.*; |
| 32 | |
| 33 | public class TestAfterClose |
| 34 | { |
| 35 | static int failCount; |
| 36 | |
| 37 | public static void main(String[] args) { |
| 38 | try { |
| 39 | ServerSocket ss = new ServerSocket(0, 0, null); |
| 40 | Socket socket = new Socket("localhost", ss.getLocalPort()); |
| 41 | ss.accept(); |
| 42 | ss.close(); |
| 43 | test(socket); |
| 44 | } catch (IOException ioe) { |
| 45 | ioe.printStackTrace(); |
| 46 | } |
| 47 | |
| 48 | if (failCount > 0) |
| 49 | throw new RuntimeException("Failed: failcount = " + failCount); |
| 50 | |
| 51 | } |
| 52 | |
| 53 | static void test(Socket socket) throws IOException { |
| 54 | //Before Close |
| 55 | int socketPort = socket.getPort(); |
| 56 | InetAddress socketInetAddress = socket.getInetAddress(); |
| 57 | SocketAddress socketRemoteSocketAddress = socket.getRemoteSocketAddress(); |
| 58 | int socketLocalPort = socket.getLocalPort(); |
| 59 | |
| 60 | //After Close |
| 61 | socket.close(); |
| 62 | |
| 63 | if (socketPort != socket.getPort()) { |
| 64 | System.out.println("Socket.getPort failed"); |
| 65 | failCount++; |
| 66 | } |
| 67 | |
| 68 | if (!socket.getInetAddress().equals(socketInetAddress)) { |
| 69 | System.out.println("Socket.getInetAddress failed"); |
| 70 | failCount++; |
| 71 | } |
| 72 | |
| 73 | if (!socket.getRemoteSocketAddress().equals(socketRemoteSocketAddress)) { |
| 74 | System.out.println("Socket.getRemoteSocketAddresss failed"); |
| 75 | failCount++; |
| 76 | } |
| 77 | |
| 78 | if (socketLocalPort != socket.getLocalPort()) { |
| 79 | System.out.println("Socket.getLocalPort failed"); |
| 80 | failCount++; |
| 81 | } |
| 82 | |
| 83 | InetAddress anyAddr = null; |
| 84 | try { |
| 85 | anyAddr = InetAddress.getByAddress("",new byte[] {0,0,0,0}); |
| 86 | } catch (UnknownHostException uhe) { |
| 87 | } |
| 88 | |
| 89 | if (anyAddr != null && !socket.getLocalAddress().equals(anyAddr)) { |
| 90 | System.out.println("Socket.getLocalAddress failed"); |
| 91 | failCount++; |
| 92 | } |
| 93 | |
| 94 | InetSocketAddress addr = new InetSocketAddress(socket.getLocalPort()); |
| 95 | if (!socket.getLocalSocketAddress().equals(addr)) { |
| 96 | System.out.println("Socket.getLocalSocketAddress failed"); |
| 97 | failCount++; |
| 98 | } |
| 99 | |
| 100 | if (!socket.isConnected()) { |
| 101 | System.out.println("Socket.isConnected failed"); |
| 102 | failCount++; |
| 103 | } |
| 104 | |
| 105 | if (!socket.isBound()) { |
| 106 | System.out.println("Socket.isBound failed"); |
| 107 | failCount++; |
| 108 | } |
| 109 | } |
| 110 | } |