0N/A/*
2362N/A * Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A *
0N/A * @bug 4204320
0N/A *
0N/A * @summary DatagramSocket.send should throw exception when connected
0N/A * to an invalid destination (on platforms that support it).
0N/A */
0N/A
0N/Aimport java.net.*;
0N/Aimport java.util.*;
0N/Aimport java.io.InterruptedIOException;
0N/A
0N/Apublic class SendDatagramToBadAddress {
0N/A
0N/A static boolean debug = false;
0N/A
0N/A public static boolean OSsupportsFeature () {
0N/A Properties p = System.getProperties ();
0N/A String v;
0N/A if (p.getProperty ("os.name").equals ("Windows 2000"))
0N/A return (true);
0N/A if (p.getProperty ("os.name").equals ("Linux"))
0N/A return (true);
4652N/A if (p.getProperty ("os.name").startsWith ("Mac OS"))
4652N/A return (true);
0N/A // Check for specific Solaris version from here
0N/A v = p.getProperty ("os.arch");
0N/A if (!v.equalsIgnoreCase ("sparc"))
0N/A return (false);
0N/A v = p.getProperty ("os.name");
0N/A if (!v.equalsIgnoreCase ("Solaris") && !v.equalsIgnoreCase ("SunOS"))
0N/A return (false);
0N/A v = p.getProperty ("os.version");
0N/A if (v.equals ("5.8") || v.equals ("8"))
0N/A return (false);
0N/A return (true);
0N/A }
0N/A
0N/A static void print (String s) {
0N/A if (debug)
0N/A System.out.println (s);
0N/A }
0N/A
0N/A class Server {
0N/A
0N/A DatagramSocket server;
0N/A byte[] buf = new byte [128];
0N/A DatagramPacket pack = new DatagramPacket (buf, buf.length);
0N/A
0N/A public Server (DatagramSocket s) {
0N/A server = s;
0N/A }
0N/A
0N/A public void receive (int loop, boolean expectError) throws Exception {
0N/A for (int i=0; i<loop; i++) {
0N/A try {
0N/A server.receive (pack);
0N/A } catch (Exception e) {
0N/A if (expectError) {
0N/A print ("Got expected error: " + e);
0N/A continue;
0N/A } else {
0N/A print ("Got: " + new String (pack.getData()));
0N/A print ("Expected: " + new String (buf));
0N/A throw new Exception ("Error reading data: Iter " +i);
0N/A }
0N/A }
0N/A String s1 = "Hello, server"+i;
0N/A byte[] buf = s1.getBytes();
0N/A if (!s1.equals (new String (pack.getData(),
0N/A pack.getOffset(),pack.getLength()))) {
0N/A print ("Got: " + new String (pack.getData()));
0N/A print ("Expected: " + new String (buf));
0N/A throw new Exception ("Error comparing data: Iter " +i);
0N/A }
0N/A }
0N/A }
0N/A };
0N/A
0N/A public static void main (String args[]) throws Exception {
0N/A if (args.length >=1 && args[0].equals ("-d")) {
0N/A debug = true;
0N/A }
0N/A SendDatagramToBadAddress ud = new SendDatagramToBadAddress ();
0N/A ud.run ();
0N/A }
0N/A
0N/A public void run() throws Exception {
0N/A
0N/A if (OSsupportsFeature()) {
0N/A print ("running on OS that supports ICMP port unreachable");
0N/A }
0N/A String host = "127.0.0.1";
0N/A InetAddress addr = InetAddress.getByName(host);
0N/A DatagramSocket sock = new DatagramSocket();
0N/A DatagramSocket serversock = new DatagramSocket(0);
0N/A DatagramPacket p;
0N/A byte[] buf;
0N/A int port = serversock.getLocalPort ();
0N/A final int loop = 5;
0N/A Server s = new Server (serversock);
0N/A int i;
0N/A
0N/A print ("Checking send to connected address ...");
0N/A sock.connect(addr, port);
0N/A
0N/A for (i = 0; i < loop; i++) {
0N/A try {
0N/A buf = ("Hello, server"+i).getBytes();
0N/A if (i % 2 == 1)
0N/A p = new DatagramPacket(buf, buf.length, addr, port);
0N/A else
0N/A p = new DatagramPacket(buf, buf.length);
0N/A sock.send(p);
0N/A } catch (Exception ex) {
0N/A print ("Got unexpected exception: " + ex);
0N/A throw new Exception ("Error sending data: ");
0N/A }
0N/A }
0N/A
0N/A s.receive (loop, false);
0N/A
0N/A // check disconnect() works
0N/A
0N/A print ("Checking send to non-connected address ...");
0N/A sock.disconnect ();
0N/A buf = ("Hello, server"+0).getBytes();
0N/A p = new DatagramPacket(buf, buf.length, addr, port);
0N/A sock.send (p);
0N/A s.receive (1, false);
0N/A
0N/A // check send() to invalid destination followed by a blocking receive
0N/A // returns an error
0N/A
0N/A print ("Checking send to invalid address ...");
0N/A sock.connect(addr, port);
0N/A serversock.close ();
0N/A try {
0N/A sock.setSoTimeout (4000);
0N/A } catch (Exception e) {
0N/A print ("could not set timeout");
0N/A throw e;
0N/A }
0N/A
0N/A boolean goterror = false;
0N/A
0N/A for (i = 0; i < loop; i++) {
0N/A try {
0N/A buf = ("Hello, server"+i).getBytes();
0N/A p = new DatagramPacket(buf, buf.length, addr, port);
0N/A sock.send(p);
0N/A p = new DatagramPacket(buf, buf.length, addr, port);
0N/A sock.receive (p);
0N/A } catch (InterruptedIOException ex) {
0N/A print ("socket timeout");
0N/A } catch (Exception ex) {
0N/A print ("Got expected exception: " + ex);
0N/A goterror = true;
0N/A }
0N/A }
0N/A
0N/A if (!goterror && OSsupportsFeature ()) {
0N/A print ("Didnt get expected exception: ");
0N/A throw new Exception ("send did not return expected error");
0N/A }
0N/A }
0N/A}