test/jdk/java/net/Socket/ShutdownInput.java
author prr
Fri, 25 May 2018 12:12:24 -0700
changeset 50347 b2f046ae8eb6
parent 49431 5812849b5027
child 54770 62b6e7587b1f
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10421
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
     1
/*
49431
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
     2
 * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
10421
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
     4
 *
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
     8
 *
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    13
 * accompanied this code).
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    14
 *
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    18
 *
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    21
 * questions.
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    22
 */
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    23
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    24
/*
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    25
 * @test
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    26
 * @bug 7014860
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    27
 * @summary Socket.getInputStream().available() not clear for
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    28
 *          case that connection is shutdown for reading
49431
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
    29
 * @run main ShutdownInput
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
    30
 * @run main/othervm -Djava.net.preferIPv4Stack=true ShutdownInput
10421
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    31
 */
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    32
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    33
import java.io.InputStream;
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    34
import java.io.OutputStream;
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    35
import java.net.InetAddress;
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    36
import java.net.InetSocketAddress;
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    37
import java.net.ServerSocket;
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    38
import java.net.Socket;
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    39
import java.nio.channels.ServerSocketChannel;
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    40
import java.nio.channels.SocketChannel;
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    41
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    42
public class ShutdownInput {
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    43
    static boolean failed = false;
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    44
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    45
    public static void main(String args[]) throws Exception {
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    46
        InetAddress iaddr = InetAddress.getLocalHost();
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    47
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    48
        try ( ServerSocket ss = new ServerSocket(0);
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    49
              Socket s1 = new Socket(iaddr, ss.getLocalPort());
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    50
              Socket s2 = ss.accept() ) {
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    51
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    52
            test(s1, s2, "Testing NET");
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    53
        }
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    54
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    55
        // check the NIO socket adapter
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    56
        try (ServerSocketChannel sc = ServerSocketChannel.open().bind(null);
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    57
             SocketChannel s1 = SocketChannel.open(
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    58
                     new InetSocketAddress(iaddr, sc.socket().getLocalPort()));
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    59
             SocketChannel s2 = sc.accept() ) {
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    60
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    61
            test(s1.socket(), s2.socket(), "Testing NIO");
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    62
        }
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    63
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    64
        if (failed) {
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    65
            throw new RuntimeException("Failed: check output");
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    66
        }
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    67
    }
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    68
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    69
    public static void test(Socket s1, Socket s2, String mesg) throws Exception {
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    70
        OutputStream os = s1.getOutputStream();
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    71
        os.write("This is a message".getBytes("US-ASCII"));
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    72
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    73
        InputStream in = s2.getInputStream();
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    74
        s2.shutdownInput();
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    75
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    76
        if (in.available() != 0) {
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    77
            failed = true;
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    78
            System.out.println(mesg + ":" + s2 + " in.available() should be 0, " +
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    79
                               "but returns "+ in.available());
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    80
        }
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    81
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    82
        byte[] ba = new byte[2];
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    83
        if (in.read() != -1 ||
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    84
            in.read(ba) != -1 ||
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    85
            in.read(ba, 0, ba.length) != -1) {
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    86
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    87
            failed = true;
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    88
            System.out.append(mesg + ":" + s2 + " in.read() should be -1");
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    89
        }
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    90
    }
2ee16a0f6df5 7014860: Socket.getInputStream().available() not clear for shutdown input
chegar
parents:
diff changeset
    91
}