test/jdk/java/net/Socket/reset/Test.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 5506 jdk/test/java/net/Socket/reset/Test.java@202f599c92aa
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, 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
 * @bug 4517622
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary SocketException on first read after error; -1 on subsequent reads
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    static int TEST_COMBINATIONS = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    // test server that cycles through each combination of response
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    static class Server extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        public Server() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            System.out.println("Server listening on port: " + getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            int testCombination = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                    Socket s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                    switch (testCombination) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                        case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                            s.setTcpNoDelay(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                            s.getOutputStream().write(new byte[256]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                            s.setSoLinger(true, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                        case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                            s.setTcpNoDelay(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                            s.getOutputStream().write(new byte[256]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                            s.setSoLinger(true, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                        case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                            s.getOutputStream().write("hello".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                            s.setSoLinger(true, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                        case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                            break;      /* EOF test */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                        case 4:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                            s.getOutputStream().write(new byte[256]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    testCombination = (testCombination + 1) % TEST_COMBINATIONS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                if (!ss.isClosed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    System.err.println("Server failed: " + ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        public void shutdown() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            } catch (IOException ioe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    static final int STATE_DATA = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    static final int STATE_EOF = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static final int STATE_IOE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    static void Test(SocketAddress sa) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        System.out.println("-----------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Socket s = new Socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        s.connect(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        byte b[] = new byte[50];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        int state = STATE_DATA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        Random rand = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        for (int i=0; i<200; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            switch (rand.nextInt(4)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        s.getOutputStream().write("data".getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    } catch (IOException ioe) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        int n = s.getInputStream().available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                        // available should never return > 0 if read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        // has already thrown IOE or returned EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        if (n > 0 && state != STATE_DATA) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                            System.out.println("FAILED!! available: " + n +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                " (unexpected as IOE or EOF already received)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                            failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        System.out.println("FAILED!!! available: " + ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        int n = s.getInputStream().read(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                        if (n > 0 && state == STATE_IOE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                            System.out.println("FAILED!! read: " + n +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                                " (unexpected as IOE already thrown)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                            failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        if (n > 0 && state == STATE_EOF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                            System.out.println("FAILED!! read: " + n +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                " (unexpected as EOF already received)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                            failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        if (n < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            if (state == STATE_IOE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                System.out.println("FAILED!! read: EOF " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                    " (unexpected as IOE already thrown)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                            if (state != STATE_EOF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                System.out.println("read: EOF");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                                state = STATE_EOF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        if (state == STATE_EOF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                            System.out.println("FAILED!! read: " + ioe +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                                " (unexpected as EOF already received)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                            failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        if (state != STATE_IOE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                            System.out.println("read: " + ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                            state = STATE_IOE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                        Thread.currentThread().sleep(100);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    } catch (Exception ie) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    static int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        SocketAddress sa = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        Server svr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // server mode only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (args[0].equals("-server")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                svr = new Server();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                svr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        // run standalone or connect to remote server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (args.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            InetAddress rh = InetAddress.getByName(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            int port = Integer.parseInt(args[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            sa = new InetSocketAddress(rh, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            svr = new Server();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            svr.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            InetAddress lh = InetAddress.getLocalHost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            sa = new InetSocketAddress(lh, svr.getPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        for (int i=0; i<10; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            Test(sa);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (svr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            svr.shutdown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (failures > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw new Exception(failures + " sub-test(s) failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
}