test/jdk/java/net/Socket/ReadAfterReset.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 50425 43b54a307c89
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50425
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
     1
/*
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
     4
 *
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
     8
 *
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    13
 * accompanied this code).
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    14
 *
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    18
 *
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    21
 * questions.
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    22
 */
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    23
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    24
/* @test
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    25
 * @requires (os.family == "linux" | os.family == "mac")
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    26
 * @bug 8203937
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    27
 * @summary Test reading bytes from a socket after the connection has been
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    28
 *          reset by the peer
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    29
 */
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    30
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    31
import java.io.IOException;
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    32
import java.io.PrintStream;
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    33
import java.net.InetAddress;
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    34
import java.net.InetSocketAddress;
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    35
import java.net.ServerSocket;
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    36
import java.net.Socket;
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    37
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    38
/**
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    39
 * This test exercises platform specific and unspecified behavior. It exists
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    40
 * only to ensure that the behavior doesn't change between JDK releases.
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    41
 */
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    42
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    43
public class ReadAfterReset {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    44
    private static final PrintStream out = System.out;
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    45
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    46
    // number of bytes to write before the connection reset
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    47
    private static final int NUM_BYTES_TO_WRITE = 1000;
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    48
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    49
    public static void main(String[] args) throws IOException {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    50
        try (ServerSocket ss = new ServerSocket()) {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    51
            ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    52
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    53
            /**
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    54
             * Connect to the server which will write some bytes and reset the
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    55
             * connection. The client then attempts to read the bytes sent by
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    56
             * the server before it closed the connection.
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    57
             */
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    58
            out.println("Test connection ...");
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    59
            try (Socket s = new Socket()) {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    60
                s.connect(ss.getLocalSocketAddress());
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    61
                int nwrote = acceptAndResetConnection(ss);
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    62
                int nread = readUntilIOException(s);
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    63
                if (nread != nwrote) {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    64
                    throw new RuntimeException("Client read " + nread + ", expected " + nwrote);
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    65
                }
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    66
            }
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    67
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    68
            /**
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    69
             * Connect to the server which will write some bytes and reset the
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    70
             * connection. The client then writes to its end of the connection,
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    71
             * failing as the connection is reset. It then attempts to read the
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    72
             * bytes sent by the server before it closed the connection.
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    73
             */
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    74
            out.println();
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    75
            out.println("Test connection ...");
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    76
            try (Socket s = new Socket()) {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    77
                s.connect(ss.getLocalSocketAddress());
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    78
                int nwrote = acceptAndResetConnection(ss);
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    79
                writeUntilIOException(s);
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    80
                int nread = readUntilIOException(s);
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    81
                if (nread != nwrote) {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    82
                    throw new RuntimeException("Client read " + nread + ", expected " + nwrote);
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    83
                }
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    84
            }
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    85
        }
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    86
    }
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    87
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    88
    /**
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    89
     * Accept a connection, write bytes, and then reset the connection
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    90
     */
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    91
    static int acceptAndResetConnection(ServerSocket ss) throws IOException {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    92
        int count = NUM_BYTES_TO_WRITE;
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    93
        try (Socket peer = ss.accept()) {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    94
            peer.getOutputStream().write(new byte[count]);
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    95
            peer.setSoLinger(true, 0);
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    96
            out.format("Server wrote %d bytes and reset connection%n", count);
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    97
        }
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    98
        return count;
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
    99
    }
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   100
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   101
    /**
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   102
     * Write bytes to a socket until I/O exception is thrown
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   103
     */
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   104
    static void writeUntilIOException(Socket s) {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   105
        try {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   106
            byte[] bytes = new byte[100];
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   107
            while (true) {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   108
                s.getOutputStream().write(bytes);
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   109
                out.format("Client wrote %d bytes%n", bytes.length);
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   110
            }
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   111
        } catch (IOException ioe) {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   112
            out.format("Client write failed: %s (expected)%n", ioe);
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   113
        }
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   114
    }
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   115
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   116
    /**
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   117
     * Read bytes from a socket until I/O exception is thrown.
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   118
     *
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   119
     * @return the number of bytes read before the I/O exception was thrown
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   120
     */
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   121
    static int readUntilIOException(Socket s) {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   122
        int nread = 0;
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   123
        try {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   124
            byte[] bytes = new byte[100];
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   125
            while (true) {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   126
                int n = s.getInputStream().read(bytes);
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   127
                if (n < 0) {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   128
                    out.println("Client read EOF");
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   129
                    break;
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   130
                } else {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   131
                    out.format("Client read %s bytes%n", n);
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   132
                    nread += n;
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   133
                }
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   134
            }
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   135
        } catch (IOException ioe) {
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   136
            out.format("Client read failed: %s (expected)%n", ioe);
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   137
        }
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   138
        return nread;
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   139
    }
43b54a307c89 8203937: Not possible to read data from socket after write detects connection reset
alanb
parents:
diff changeset
   140
}