Shadow.java revision 3261:a06412e13bf7
1/*
2 * Copyright (c) 2001, 2010, 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 4455376
26 * @summary Ensure that socket objects obtained from channels
27 *          carry the correct address information
28 */
29
30import java.io.*;
31import java.net.*;
32import java.nio.*;
33import java.nio.channels.*;
34
35
36public class Shadow {
37
38    static PrintStream log = System.err;
39
40    private static void dump(ServerSocket s) {
41        log.println("getInetAddress(): " + s.getInetAddress());
42        log.println("getLocalPort(): " + s.getLocalPort());
43    }
44
45    private static void dump(Socket s) {
46        log.println("getInetAddress(): " + s.getInetAddress());
47        log.println("getPort(): " + s.getPort());
48        log.println("getLocalAddress(): " + s.getLocalAddress());
49        log.println("getLocalPort(): " + s.getLocalPort());
50    }
51
52    private static int problems = 0;
53
54    private static void problem(String s) {
55        log.println("FAILURE: " + s);
56        problems++;
57    }
58
59    private static void check(Socket s) {
60        if (s.getPort() == 0)
61            problem("Socket has no port");
62        if (s.getLocalPort() == 0)
63            problem("Socket has no local port");
64        if (!s.getLocalAddress().equals(s.getInetAddress()))
65            problem("Socket has wrong local address");
66    }
67
68    public static void main(String[] args) throws Exception {
69        boolean useChannels
70            = ((args.length == 0) || Boolean.valueOf(args[0]).booleanValue());
71        int port = (args.length > 1 ? Integer.parseInt(args[1]) : -1);
72
73        // open server socket
74        ServerSocket serverSocket;
75        if (useChannels) {
76            ServerSocketChannel serverSocketChannel =
77                ServerSocketChannel.open();
78            log.println("opened ServerSocketChannel: " +
79                      serverSocketChannel);
80            serverSocket = serverSocketChannel.socket();
81            log.println("associated ServerSocket: " + serverSocket);
82        } else {
83            serverSocket = new ServerSocket();
84            log.println("opened ServerSocket: " + serverSocket);
85        }
86
87        // bind server socket to port
88        SocketAddress bindAddr =
89            new InetSocketAddress((port == -1) ? 0 : 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}
143