jdk/test/java/net/DatagramSocket/SendDatagramToBadAddress.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 12047 320a714614e9
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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) 2000, 2001, 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);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        // Check for specific Solaris version from here
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        v = p.getProperty ("os.arch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        if (!v.equalsIgnoreCase ("sparc"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            return (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        v = p.getProperty ("os.name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        if (!v.equalsIgnoreCase ("Solaris") && !v.equalsIgnoreCase ("SunOS"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            return (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        v = p.getProperty ("os.version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (v.equals ("5.8") || v.equals ("8"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            return (false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return (true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static void print (String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            System.out.println (s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    class Server {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        DatagramSocket server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        byte[] buf = new byte [128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        DatagramPacket pack = new DatagramPacket (buf, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        public Server (DatagramSocket s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            server = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        public void receive (int loop, boolean expectError) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            for (int i=0; i<loop; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    server.receive (pack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    if (expectError) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                        print ("Got expected error: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        print ("Got: " + new String (pack.getData()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                        print ("Expected: " + new String (buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        throw new Exception ("Error reading data: Iter " +i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                String s1 = "Hello, server"+i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                byte[] buf      = s1.getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                if (!s1.equals (new String (pack.getData(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                            pack.getOffset(),pack.getLength()))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    print ("Got: " + new String (pack.getData()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    print ("Expected: " + new String (buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    throw new Exception ("Error comparing data: Iter " +i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
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
    public static void main (String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (args.length >=1 && args[0].equals ("-d")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            debug = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        SendDatagramToBadAddress ud = new SendDatagramToBadAddress ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        ud.run ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public void run() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (OSsupportsFeature()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            print ("running on OS that supports ICMP port unreachable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        String host = "127.0.0.1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        InetAddress addr = InetAddress.getByName(host);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        DatagramSocket sock = new DatagramSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        DatagramSocket serversock = new DatagramSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        DatagramPacket p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        byte[] buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int port = serversock.getLocalPort ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        final int loop = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        Server s = new Server (serversock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        print ("Checking send to connected address ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        sock.connect(addr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        for (i = 0; i < loop; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                buf = ("Hello, server"+i).getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if (i % 2 == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    p = new DatagramPacket(buf, buf.length, addr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                    p = new DatagramPacket(buf, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                sock.send(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                print ("Got unexpected exception: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                throw new Exception ("Error sending data: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        s.receive (loop, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        // check disconnect() works
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        print ("Checking send to non-connected address ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        sock.disconnect ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        buf = ("Hello, server"+0).getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        p = new DatagramPacket(buf, buf.length, addr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        sock.send (p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        s.receive (1, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        // check send() to invalid destination followed by a blocking receive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        // returns an error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        print ("Checking send to invalid address ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        sock.connect(addr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        serversock.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            sock.setSoTimeout (4000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            print ("could not set timeout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        boolean goterror = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        for (i = 0; i < loop; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                buf = ("Hello, server"+i).getBytes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                p = new DatagramPacket(buf, buf.length, addr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                sock.send(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                p = new DatagramPacket(buf, buf.length, addr, port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                sock.receive (p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            } catch (InterruptedIOException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                print ("socket timeout");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                print ("Got expected exception: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                goterror = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (!goterror && OSsupportsFeature ()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            print ("Didnt get expected exception: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new Exception ("send did not return expected error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
}