test/jdk/java/net/DatagramSocket/SendDatagramToBadAddress.java
author pconcannon
Wed, 27 Nov 2019 16:01:34 +0000
changeset 59299 17d242844fc9
parent 59124 d01fe40e9cd8
permissions -rw-r--r--
8233018: Add a new test to verify that DatagramSocket is not interruptible Summary: Test added to check the interruptability of DatagramSocket, MulticastSocket and DatagramSocketAdaptor. Reviewed-by: chegar, dfuchs
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: 47216
diff changeset
     2
 * Copyright (c) 2000, 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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @bug 4204320
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @summary DatagramSocket.send should throw exception when connected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *  to an invalid destination (on platforms that support it).
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.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.InterruptedIOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class SendDatagramToBadAddress {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    static boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    public static boolean OSsupportsFeature () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        Properties p = System.getProperties ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        String v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        if (p.getProperty ("os.name").equals ("Windows 2000"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            return (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        if (p.getProperty ("os.name").equals ("Linux"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            return (true);
12047
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    48
        if (p.getProperty ("os.name").startsWith ("Mac OS"))
320a714614e9 7113349: Initial changeset for Macosx port to jdk
michaelm
parents: 5506
diff changeset
    49
            return (true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        // Check for specific Solaris version from here
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        v = p.getProperty ("os.arch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (!v.equalsIgnoreCase ("sparc"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            return (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        v = p.getProperty ("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (!v.equalsIgnoreCase ("Solaris") && !v.equalsIgnoreCase ("SunOS"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            return (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        v = p.getProperty ("os.version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if (v.equals ("5.8") || v.equals ("8"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            return (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    static void print (String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            System.out.println (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    class Server {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        DatagramSocket server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        byte[] buf = new byte [128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        DatagramPacket pack = new DatagramPacket (buf, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        public Server (DatagramSocket s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            server = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        public void receive (int loop, boolean expectError) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            for (int i=0; i<loop; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    server.receive (pack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                    if (expectError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        print ("Got expected error: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        print ("Got: " + new String (pack.getData()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                        print ("Expected: " + new String (buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        throw new Exception ("Error reading data: Iter " +i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                String s1 = "Hello, server"+i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                byte[] buf      = s1.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                if (!s1.equals (new String (pack.getData(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                            pack.getOffset(),pack.getLength()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    print ("Got: " + new String (pack.getData()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    print ("Expected: " + new String (buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    throw new Exception ("Error comparing data: Iter " +i);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static void main (String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (args.length >=1 && args[0].equals ("-d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            debug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        SendDatagramToBadAddress ud = new SendDatagramToBadAddress ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        ud.run ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public void run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (OSsupportsFeature()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            print ("running on OS that supports ICMP port unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
59124
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 54086
diff changeset
   116
        try (DatagramSocket sock = new DatagramSocket()) {
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 54086
diff changeset
   117
            test(sock);
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 54086
diff changeset
   118
        }
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 54086
diff changeset
   119
    }
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 54086
diff changeset
   120
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 54086
diff changeset
   121
    private void test(DatagramSocket sock) throws Exception {
d01fe40e9cd8 8233141: DatagramSocket.send doesn't specify exception thrown when no target address
dfuchs
parents: 54086
diff changeset
   122
        print("Testing with " + sock.getClass());
54086
ccb4a50bee06 8220083: Use InetAddress.getLoopbackAddress() in place of 127.0.0.1 for some tests
aeubanks
parents: 47216
diff changeset
   123
        InetAddress addr = InetAddress.getLoopbackAddress();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        DatagramSocket serversock = new DatagramSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        DatagramPacket p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        byte[] buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        int port = serversock.getLocalPort ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        final int loop = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        Server s = new Server (serversock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        print ("Checking send to connected address ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        sock.connect(addr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        for (i = 0; i < loop; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                buf = ("Hello, server"+i).getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (i % 2 == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    p = new DatagramPacket(buf, buf.length, addr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    p = new DatagramPacket(buf, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                sock.send(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                print ("Got unexpected exception: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                throw new Exception ("Error sending data: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        s.receive (loop, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        // check disconnect() works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        print ("Checking send to non-connected address ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        sock.disconnect ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        buf = ("Hello, server"+0).getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        p = new DatagramPacket(buf, buf.length, addr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        sock.send (p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        s.receive (1, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // check send() to invalid destination followed by a blocking receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        // returns an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        print ("Checking send to invalid address ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        sock.connect(addr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        serversock.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            sock.setSoTimeout (4000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            print ("could not set timeout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        boolean goterror = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        for (i = 0; i < loop; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                buf = ("Hello, server"+i).getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                p = new DatagramPacket(buf, buf.length, addr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                sock.send(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                p = new DatagramPacket(buf, buf.length, addr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                sock.receive (p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            } catch (InterruptedIOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                print ("socket timeout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                print ("Got expected exception: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                goterror = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        if (!goterror && OSsupportsFeature ()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            print ("Didnt get expected exception: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new Exception ("send did not return expected error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
}