test/jdk/java/net/Socket/CloseAvailable.java
author aeubanks
Tue, 16 Apr 2019 13:06:23 -0700
changeset 54770 62b6e7587b1f
parent 52209 3b2e68c9e7a6
child 54811 9db7c0f561a6
permissions -rw-r--r--
8220673: Add test library support for determining platform IP support Reviewed-by: dfuchs, chegar Contributed-by: aeubanks@google.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49431
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
     2
 * Copyright (c) 1998, 2018, 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
52104
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    26
 * @bug 4091859 8189366
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 52209
diff changeset
    27
 * @library /test/lib
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Test Socket.available()
49431
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
    29
 * @run main CloseAvailable
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
    30
 * @run main/othervm -Djava.net.preferIPv4Stack=true CloseAvailable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.*;
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 52209
diff changeset
    35
import jdk.test.lib.net.IPSupport;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
52104
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    38
public class CloseAvailable {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    public static void main(String[] args) throws Exception {
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 52209
diff changeset
    41
        IPSupport.skipIfCurrentConfigurationIsInvalid();
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 52209
diff changeset
    42
52104
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    43
        testClose();
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    44
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    45
        testEOF(true);
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    46
        testEOF(false);
52209
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
    47
        testIOEOnClosed(true);
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
    48
        testIOEOnClosed(false);
52104
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    49
    }
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    50
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    51
    static void testClose() throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        boolean error = true;
52104
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    53
        InetAddress addr = InetAddress.getLocalHost();
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    54
        ServerSocket ss = new ServerSocket(0);
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    55
        int port = ss.getLocalPort();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
52104
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    57
        Thread t = new Thread(new Thread("Close-Available-1") {
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    58
            public void run() {
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    59
                try {
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    60
                    Socket s = new Socket(addr, port);
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    61
                    s.close();
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    62
                } catch (Exception e) {
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    63
                    e.printStackTrace();
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    64
                }
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    65
            }
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    66
        });
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    67
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        t.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        Socket  soc = ss.accept();
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
    71
        ss.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        DataInputStream is = new DataInputStream(soc.getInputStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            is.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        catch (IOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            error = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (error)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            throw new RuntimeException("Available() can be called after stream closed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
52104
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    86
    // Verifies consistency of `available` behaviour when EOF reached, both
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    87
    // explicitly and implicitly.
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    88
    static void testEOF(boolean readUntilEOF) throws IOException {
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    89
        System.out.println("testEOF, readUntilEOF: " + readUntilEOF);
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    90
        InetAddress addr = InetAddress.getLoopbackAddress();
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    91
        ServerSocket ss = new ServerSocket();
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    92
        ss.bind(new InetSocketAddress(addr, 0), 0);
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    93
        int port = ss.getLocalPort();
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    94
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    95
        try (Socket s = new Socket(addr, port)) {
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    96
            s.getOutputStream().write(0x42);
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    97
            s.shutdownOutput();
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    98
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
    99
            try (Socket soc = ss.accept()) {
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   100
                ss.close();
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   101
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   102
                InputStream is = soc.getInputStream();
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   103
                int b = is.read();
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   104
                assert b == 0x42;
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   105
                assert !s.isClosed();
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   106
                if (readUntilEOF) {
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   107
                    b = is.read();
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   108
                    assert b == -1;
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   109
                }
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   110
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   111
                int a;
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   112
                for (int i = 0; i < 100; i++) {
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   113
                    a = is.available();
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   114
                    System.out.print(a + ", ");
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   115
                    if (a != 0)
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   116
                        throw new RuntimeException("Unexpected non-zero available: " + a);
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   117
                }
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   118
                assert !s.isClosed();
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   119
                assert is.read() == -1;
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   120
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
52104
331fbd2db6b5 8189366: SocketInputStream.available() should check for eof
vtewari
parents: 49431
diff changeset
   122
        System.out.println("\ncomplete");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
52209
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   124
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   125
    // Verifies IOException thrown by `available`, on a closed input stream
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   126
    // that may, or may not, have reached EOF prior to closure.
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   127
    static void testIOEOnClosed(boolean readUntilEOF) throws IOException {
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   128
        System.out.println("testIOEOnClosed, readUntilEOF: " + readUntilEOF);
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   129
        InetAddress addr = InetAddress.getLoopbackAddress();
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   130
        ServerSocket ss = new ServerSocket();
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   131
        ss.bind(new InetSocketAddress(addr, 0), 0);
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   132
        int port = ss.getLocalPort();
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   133
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   134
        try (Socket s = new Socket(addr, port)) {
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   135
            s.getOutputStream().write(0x43);
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   136
            s.shutdownOutput();
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   137
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   138
            try (Socket soc = ss.accept()) {
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   139
                ss.close();
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   140
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   141
                InputStream is = soc.getInputStream();
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   142
                int b = is.read();
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   143
                assert b == 0x43;
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   144
                assert !s.isClosed();
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   145
                if (readUntilEOF) {
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   146
                    b = is.read();
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   147
                    assert b == -1;
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   148
                }
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   149
                is.close();
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   150
                try {
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   151
                    b = is.available();
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   152
                    throw new RuntimeException("UNEXPECTED successful read: " + b);
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   153
                } catch (IOException expected) {
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   154
                    System.out.println("caught expected IOException:" + expected);
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   155
                }
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   156
            }
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   157
        }
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   158
        System.out.println("\ncomplete");
3b2e68c9e7a6 8212114: Reconsider the affect on closed streams resulting from 8189366
vtewari
parents: 52104
diff changeset
   159
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
}