test/jdk/java/net/Socket/UrgentDataTest.java
author aeubanks
Tue, 16 Apr 2019 13:06:23 -0700
changeset 54770 62b6e7587b1f
parent 54681 edd709e64ea1
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
/*
54086
ccb4a50bee06 8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents: 49431
diff changeset
     2
 * Copyright (c) 2001, 2019, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4092038
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 54681
diff changeset
    27
 * @library /test/lib
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary TCP Urgent data support
49431
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
    29
 * @run main UrgentDataTest
5812849b5027 8198358: Align organization of TwoStacksPlainSocketImp with DualStackPlainSocketImpl [win]
igerasim
parents: 47216
diff changeset
    30
 * @run main/othervm -Djava.net.preferIPv4Stack=true UrgentDataTest
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: 54681
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
public class UrgentDataTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    Object opref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    boolean isClient, isServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    String  clHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    ServerSocket listener;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    Socket client, server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    int  clPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    InputStream clis, sis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    OutputStream clos, sos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static void usage () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        System.out.println ("   usage: java UrgentDataTest <runs client and server together>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        System.out.println ("   usage: java UrgentDataTest -server <runs server alone>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        System.out.println ("   usage: java UrgentDataTest -client host port <runs client and connects to"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            " specified server>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public static void main (String args[]) {
54770
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 54681
diff changeset
    56
        IPSupport.skipIfCurrentConfigurationIsInvalid();
62b6e7587b1f 8220673: Add test library support for determining platform IP support
aeubanks
parents: 54681
diff changeset
    57
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            UrgentDataTest test = new UrgentDataTest ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (args.length == 0) {
54681
edd709e64ea1 8223145: Replace wildcard address with loopback or local host in tests - part 1
dfuchs
parents: 54086
diff changeset
    61
                InetAddress loopback = InetAddress.getLoopbackAddress();
edd709e64ea1 8223145: Replace wildcard address with loopback or local host in tests - part 1
dfuchs
parents: 54086
diff changeset
    62
                test.listener = new ServerSocket ();
edd709e64ea1 8223145: Replace wildcard address with loopback or local host in tests - part 1
dfuchs
parents: 54086
diff changeset
    63
                test.listener.bind(new InetSocketAddress(loopback, 0));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                test.isClient = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                test.isServer = true;
54681
edd709e64ea1 8223145: Replace wildcard address with loopback or local host in tests - part 1
dfuchs
parents: 54086
diff changeset
    66
                test.clHost = loopback.getHostAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                test.clPort = test.listener.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                test.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            } else if (args[0].equals ("-server")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                test.listener = new ServerSocket (0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                System.out.println ("Server listening on port " + test.listener.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                test.isClient = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                test.isServer = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                test.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                System.out.println ("Server: Completed OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            } else if (args[0].equals ("-client")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                test.isClient = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                test.isServer = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                test.clHost = args [1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                test.clPort = Integer.parseInt (args[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                test.run();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                System.out.println ("Client: Completed OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                usage ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        catch (ArrayIndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            e.printStackTrace ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            throw new RuntimeException ("Exception caught");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public void run () throws Exception {
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   100
        try {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   101
            if (isClient) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   102
                client = new Socket (clHost, clPort);
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   103
                clis = client.getInputStream();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   104
                clos = client.getOutputStream();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   105
                client.setOOBInline (true);
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   106
                if (client.getOOBInline() != true) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   107
                    throw new RuntimeException ("Setting OOBINLINE failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   110
            if (isServer) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   111
                server = listener.accept ();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   112
                sis = server.getInputStream();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   113
                sos = server.getOutputStream();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   114
            }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   115
            if (isClient) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   116
                clos.write ("Hello".getBytes ());
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   117
                client.sendUrgentData (100);
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   118
                clos.write ("world".getBytes ());
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   119
            }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   120
            // read Hello world from server (during which oob byte must have been dropped)
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   121
            String s = "Helloworld";
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   122
            if (isServer) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   123
                for (int y=0; y<s.length(); y++) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   124
                    int c = sis.read ();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   125
                    if (c != (int)s.charAt (y)) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   126
                        throw new RuntimeException ("Unexpected character read");
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   127
                    }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   128
                }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   129
                // Do the same from server to client
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   130
                sos.write ("Hello".getBytes ());
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   131
                server.sendUrgentData (101);
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   132
                sos.write ("World".getBytes ());
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   133
            }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   134
            if (isClient) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   135
                // read Hello world from client (during which oob byte must have been read)
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   136
                s="Hello";
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   137
                for (int y=0; y<s.length(); y++) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   138
                    int c = clis.read ();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   139
                    if (c != (int)s.charAt (y)) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   140
                        throw new RuntimeException ("Unexpected character read");
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   141
                    }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   142
                }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   143
                if (clis.read() != 101) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   144
                    throw new RuntimeException ("OOB byte not received");
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   145
                }
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   146
                s="World";
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   147
                for (int y=0; y<s.length(); y++) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   148
                    int c = clis.read ();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   149
                    if (c != (int)s.charAt (y)) {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   150
                        throw new RuntimeException ("Unexpected character read");
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   151
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
6115
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   154
        } finally {
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   155
            if (listener != null) listener.close();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   156
            if (client != null) client.close ();
7c523cf2bc8a 6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents: 5506
diff changeset
   157
            if (server != null) server.close ();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
}