test/jdk/java/nio/channels/etc/Shadow.java
author jbhateja
Tue, 26 Nov 2019 16:09:25 +0300
changeset 59278 8375560db76b
parent 54086 ccb4a50bee06
permissions -rw-r--r--
8234394: C2: Dynamic register class support in ADLC Reviewed-by: vlivanov, sviswanathan, thartmann, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54086
ccb4a50bee06 8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4455376
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Ensure that socket objects obtained from channels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *          carry the correct address information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class Shadow {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static PrintStream log = System.err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private static void dump(ServerSocket s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        log.println("getInetAddress(): " + s.getInetAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        log.println("getLocalPort(): " + s.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static void dump(Socket s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        log.println("getInetAddress(): " + s.getInetAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        log.println("getPort(): " + s.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        log.println("getLocalAddress(): " + s.getLocalAddress());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        log.println("getLocalPort(): " + s.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static int problems = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static void problem(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        log.println("FAILURE: " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        problems++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static void check(Socket s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (s.getPort() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            problem("Socket has no port");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (s.getLocalPort() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            problem("Socket has no local port");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (!s.getLocalAddress().equals(s.getInetAddress()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            problem("Socket has wrong local address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        boolean useChannels
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            = ((args.length == 0) || Boolean.valueOf(args[0]).booleanValue());
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    71
        int port = (args.length > 1 ? Integer.parseInt(args[1]) : -1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        // open server socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        ServerSocket serverSocket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (useChannels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            ServerSocketChannel serverSocketChannel =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                ServerSocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            log.println("opened ServerSocketChannel: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                      serverSocketChannel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            serverSocket = serverSocketChannel.socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            log.println("associated ServerSocket: " + serverSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            serverSocket = new ServerSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            log.println("opened ServerSocket: " + serverSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        // bind server socket to port
5970
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    88
        SocketAddress bindAddr =
d4e98bbfb0be 6963027: TEST_BUG: channels and buffer tests need to run in samevm mode
alanb
parents: 5506
diff changeset
    89
            new InetSocketAddress((port == -1) ? 0 : port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        serverSocket.bind(bindAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        log.println("bound ServerSocket: " + serverSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // open client socket
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        Socket socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (useChannels) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            SocketChannel socketChannel = SocketChannel.open();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            log.println("opened SocketChannel: " + socketChannel);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            socket = socketChannel.socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            log.println("associated Socket: " + socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            socket = new Socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            log.println("opened Socket: " + socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // connect client socket to port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        SocketAddress connectAddr =
54086
ccb4a50bee06 8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents: 47216
diff changeset
   110
            new InetSocketAddress(InetAddress.getLoopbackAddress(),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                  serverSocket.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        socket.connect(connectAddr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        log.println("connected Socket: " + socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        // accept connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        Socket acceptedSocket = serverSocket.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        log.println("accepted Socket: " + acceptedSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        log.println("========================================");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        log.println("*** ServerSocket info: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        dump(serverSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        log.println("*** client Socket info: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        dump(socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        check(socket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        log.println("*** accepted Socket info: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        dump(acceptedSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        check(acceptedSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        log.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (problems > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new Exception(problems + " tests failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}