test/jdk/java/net/Socket/ConnectionReset.java
author alanb
Thu, 21 Mar 2019 18:04:01 +0000
branchniosocketimpl-branch
changeset 57277 d2b2a4edbfe7
parent 57210 a67ea4f53e56
parent 54216 f10ca228b22f
child 57281 c08d024d6bf9
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     1
/*
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     4
 *
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     8
 *
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    13
 * accompanied this code).
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    14
 *
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    18
 *
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    21
 * questions.
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    22
 */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    23
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    24
/**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    25
 * @test
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    26
 * @requires os.family != "solaris"
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    27
 * @run testng ConnectionReset
57202
f946ae816b8a Add test for connection reset
alanb
parents:
diff changeset
    28
 * @run testng/othervm -Djdk.net.usePlainSocketImpl ConnectionReset
54216
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    29
 * @summary Test behavior of read and available when a connection is reset
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    30
 */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    31
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    32
import java.io.IOException;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    33
import java.io.InputStream;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    34
import java.net.InetAddress;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    35
import java.net.InetSocketAddress;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    36
import java.net.ServerSocket;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    37
import java.net.Socket;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    38
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    39
import org.testng.annotations.Test;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    40
import static org.testng.Assert.*;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    41
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    42
@Test
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    43
public class ConnectionReset {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    44
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    45
    static final int REPEAT_COUNT = 5;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    46
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    47
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    48
     * Tests available before read when there are no bytes to read
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    49
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    50
    public void testAvailableBeforeRead1() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    51
        System.out.println("testAvailableBeforeRead1");
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    52
        withResetConnection(null, s -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    53
            InputStream in = s.getInputStream();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    54
            for (int i=0; i<REPEAT_COUNT; i++) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    55
                int bytesAvailable = in.available();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    56
                System.out.format("available => %d%n", bytesAvailable);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    57
                assertTrue(bytesAvailable == 0);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    58
                try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    59
                    int bytesRead = in.read();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    60
                    if (bytesRead == -1) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    61
                        System.out.println("read => EOF");
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    62
                    } else {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    63
                        System.out.println("read => 1 byte");
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    64
                    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    65
                    assertTrue(false);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    66
                } catch (IOException ioe) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    67
                    System.out.format("read => %s (expected)%n", ioe);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    68
                }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    69
            }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    70
        });
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    71
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    72
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    73
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    74
     * Tests available before read when there are bytes to read
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    75
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    76
    public void testAvailableBeforeRead2() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    77
        System.out.println("testAvailableBeforeRead2");
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    78
        byte[] data = { 1, 2, 3 };
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    79
        withResetConnection(data, s -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    80
            InputStream in = s.getInputStream();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    81
            int remaining = data.length;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    82
            for (int i=0; i<REPEAT_COUNT; i++) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    83
                int bytesAvailable = in.available();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    84
                System.out.format("available => %d%n", bytesAvailable);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    85
                assertTrue(bytesAvailable <= remaining);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    86
                try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    87
                    int bytesRead = in.read();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    88
                    if (bytesRead == -1) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    89
                        System.out.println("read => EOF");
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    90
                        assertTrue(false);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    91
                    } else {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    92
                        System.out.println("read => 1 byte");
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    93
                        assertTrue(remaining > 0);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    94
                        remaining--;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    95
                    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    96
                } catch (IOException ioe) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    97
                    System.out.format("read => %s%n", ioe);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    98
                    remaining = 0;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
    99
                }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   100
            }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   101
        });
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   102
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   103
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   104
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   105
     * Tests read before available when there are no bytes to read
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   106
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   107
    public void testReadBeforeAvailable1() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   108
        System.out.println("testReadBeforeAvailable1");
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   109
        withResetConnection(null, s -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   110
            InputStream in = s.getInputStream();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   111
            for (int i=0; i<REPEAT_COUNT; i++) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   112
                try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   113
                    int bytesRead = in.read();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   114
                    if (bytesRead == -1) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   115
                        System.out.println("read => EOF");
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   116
                    } else {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   117
                        System.out.println("read => 1 byte");
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   118
                    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   119
                    assertTrue(false);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   120
                } catch (IOException ioe) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   121
                    System.out.format("read => %s (expected)%n", ioe);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   122
                }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   123
                int bytesAvailable = in.available();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   124
                System.out.format("available => %d%n", bytesAvailable);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   125
                assertTrue(bytesAvailable == 0);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   126
            }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   127
        });
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   128
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   129
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   130
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   131
     * Tests read before available when there are bytes to read
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   132
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   133
    public void testReadBeforeAvailable2() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   134
        System.out.println("testReadBeforeAvailable2");
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   135
        byte[] data = { 1, 2, 3 };
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   136
        withResetConnection(data, s -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   137
            InputStream in = s.getInputStream();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   138
            int remaining = data.length;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   139
            for (int i=0; i<REPEAT_COUNT; i++) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   140
                try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   141
                    int bytesRead = in.read();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   142
                    if (bytesRead == -1) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   143
                        System.out.println("read => EOF");
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   144
                        assertTrue(false);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   145
                    } else {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   146
                        System.out.println("read => 1 byte");
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   147
                        assertTrue(remaining > 0);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   148
                        remaining--;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   149
                    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   150
                } catch (IOException ioe) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   151
                    System.out.format("read => %s%n", ioe);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   152
                    remaining = 0;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   153
                }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   154
                int bytesAvailable = in.available();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   155
                System.out.format("available => %d%n", bytesAvailable);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   156
                assertTrue(bytesAvailable <= remaining);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   157
            }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   158
        });
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   159
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   160
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   161
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   162
     * Tests available and read on a socket closed after connection reset
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   163
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   164
    public void testAfterClose() throws IOException {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   165
        System.out.println("testAfterClose");
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   166
        withResetConnection(null, s -> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   167
            InputStream in = s.getInputStream();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   168
            try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   169
                in.read();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   170
                assertTrue(false);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   171
            } catch (IOException ioe) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   172
                // expected
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   173
            }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   174
            s.close();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   175
            try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   176
                int bytesAvailable = in.available();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   177
                System.out.format("available => %d%n", bytesAvailable);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   178
                assertTrue(false);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   179
            } catch (IOException ioe) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   180
                System.out.format("available => %s (expected)%n", ioe);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   181
            }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   182
            try {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   183
                int n = in.read();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   184
                System.out.format("read => %d%n", n);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   185
                assertTrue(false);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   186
            } catch (IOException ioe) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   187
                System.out.format("read => %s (expected)%n", ioe);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   188
            }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   189
        });
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   190
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   191
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   192
    interface ThrowingConsumer<T> {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   193
        void accept(T t) throws IOException;
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   194
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   195
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   196
    /**
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   197
     * Invokes a consumer with a Socket connected to a peer that has closed the
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   198
     * connection with a "connection reset". The peer sends the given data bytes
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   199
     * before closing (when data is not null).
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   200
     */
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   201
    static void withResetConnection(byte[] data, ThrowingConsumer<Socket> consumer)
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   202
        throws IOException
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   203
    {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   204
        var loopback = InetAddress.getLoopbackAddress();
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   205
        try (var listener = new ServerSocket()) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   206
            listener.bind(new InetSocketAddress(loopback, 0));
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   207
            try (var socket = new Socket()) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   208
                socket.connect(listener.getLocalSocketAddress());
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   209
                try (Socket peer = listener.accept()) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   210
                    if (data != null) {
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   211
                        peer.getOutputStream().write(data);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   212
                    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   213
                    peer.setSoLinger(true, 0);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   214
                }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   215
                consumer.accept(socket);
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   216
            }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   217
        }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   218
    }
f10ca228b22f 8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff changeset
   219
}