J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2001 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 | /* @test |
| 25 | * @bug 4455376 |
| 26 | * @summary Ensure that socket objects obtained from channels |
| 27 | * carry the correct address information |
| 28 | */ |
| 29 | |
| 30 | import java.io.*; |
| 31 | import java.net.*; |
| 32 | import java.nio.*; |
| 33 | import java.nio.channels.*; |
| 34 | |
| 35 | |
| 36 | public class Shadow { |
| 37 | |
| 38 | private static final int PORT = 2019; |
| 39 | static PrintStream log = System.err; |
| 40 | |
| 41 | private static void dump(ServerSocket s) { |
| 42 | log.println("getInetAddress(): " + s.getInetAddress()); |
| 43 | log.println("getLocalPort(): " + s.getLocalPort()); |
| 44 | } |
| 45 | |
| 46 | private static void dump(Socket s) { |
| 47 | log.println("getInetAddress(): " + s.getInetAddress()); |
| 48 | log.println("getPort(): " + s.getPort()); |
| 49 | log.println("getLocalAddress(): " + s.getLocalAddress()); |
| 50 | log.println("getLocalPort(): " + s.getLocalPort()); |
| 51 | } |
| 52 | |
| 53 | private static int problems = 0; |
| 54 | |
| 55 | private static void problem(String s) { |
| 56 | log.println("FAILURE: " + s); |
| 57 | problems++; |
| 58 | } |
| 59 | |
| 60 | private static void check(Socket s) { |
| 61 | if (s.getPort() == 0) |
| 62 | problem("Socket has no port"); |
| 63 | if (s.getLocalPort() == 0) |
| 64 | problem("Socket has no local port"); |
| 65 | if (!s.getLocalAddress().equals(s.getInetAddress())) |
| 66 | problem("Socket has wrong local address"); |
| 67 | } |
| 68 | |
| 69 | public static void main(String[] args) throws Exception { |
| 70 | boolean useChannels |
| 71 | = ((args.length == 0) || Boolean.valueOf(args[0]).booleanValue()); |
| 72 | int port = (args.length > 1 ? Integer.parseInt(args[1]) : PORT); |
| 73 | |
| 74 | // open server socket |
| 75 | ServerSocket serverSocket; |
| 76 | if (useChannels) { |
| 77 | ServerSocketChannel serverSocketChannel = |
| 78 | ServerSocketChannel.open(); |
| 79 | log.println("opened ServerSocketChannel: " + |
| 80 | serverSocketChannel); |
| 81 | serverSocket = serverSocketChannel.socket(); |
| 82 | log.println("associated ServerSocket: " + serverSocket); |
| 83 | } else { |
| 84 | serverSocket = new ServerSocket(); |
| 85 | log.println("opened ServerSocket: " + serverSocket); |
| 86 | } |
| 87 | |
| 88 | // bind server socket to port |
| 89 | SocketAddress bindAddr = new InetSocketAddress(port); |
| 90 | serverSocket.bind(bindAddr); |
| 91 | log.println("bound ServerSocket: " + serverSocket); |
| 92 | |
| 93 | log.println(); |
| 94 | |
| 95 | // open client socket |
| 96 | Socket socket; |
| 97 | if (useChannels) { |
| 98 | SocketChannel socketChannel = SocketChannel.open(); |
| 99 | log.println("opened SocketChannel: " + socketChannel); |
| 100 | |
| 101 | socket = socketChannel.socket(); |
| 102 | log.println("associated Socket: " + socket); |
| 103 | } else { |
| 104 | socket = new Socket(); |
| 105 | log.println("opened Socket: " + socket); |
| 106 | } |
| 107 | |
| 108 | // connect client socket to port |
| 109 | SocketAddress connectAddr = |
| 110 | new InetSocketAddress("127.0.0.1", |
| 111 | serverSocket.getLocalPort()); |
| 112 | socket.connect(connectAddr); |
| 113 | log.println("connected Socket: " + socket); |
| 114 | |
| 115 | log.println(); |
| 116 | |
| 117 | // accept connection |
| 118 | Socket acceptedSocket = serverSocket.accept(); |
| 119 | log.println("accepted Socket: " + acceptedSocket); |
| 120 | |
| 121 | log.println(); |
| 122 | log.println("========================================"); |
| 123 | |
| 124 | log.println("*** ServerSocket info: "); |
| 125 | dump(serverSocket); |
| 126 | log.println(); |
| 127 | |
| 128 | log.println("*** client Socket info: "); |
| 129 | dump(socket); |
| 130 | check(socket); |
| 131 | log.println(); |
| 132 | |
| 133 | log.println("*** accepted Socket info: "); |
| 134 | dump(acceptedSocket); |
| 135 | check(acceptedSocket); |
| 136 | log.println(); |
| 137 | |
| 138 | if (problems > 0) |
| 139 | throw new Exception(problems + " tests failed"); |
| 140 | } |
| 141 | |
| 142 | } |