blob: 44602cc6daa99b5ad96f73520b950ccfdfda9bee [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2001 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 *
27 * @bug 4204320
28 *
29 * @summary DatagramSocket.send should throw exception when connected
30 * to an invalid destination (on platforms that support it).
31 */
32
33import java.net.*;
34import java.util.*;
35import java.io.InterruptedIOException;
36
37public class SendDatagramToBadAddress {
38
39 static boolean debug = false;
40
41 public static boolean OSsupportsFeature () {
42 Properties p = System.getProperties ();
43 String v;
44 if (p.getProperty ("os.name").equals ("Windows 2000"))
45 return (true);
46 if (p.getProperty ("os.name").equals ("Linux"))
47 return (true);
48 // Check for specific Solaris version from here
49 v = p.getProperty ("os.arch");
50 if (!v.equalsIgnoreCase ("sparc"))
51 return (false);
52 v = p.getProperty ("os.name");
53 if (!v.equalsIgnoreCase ("Solaris") && !v.equalsIgnoreCase ("SunOS"))
54 return (false);
55 v = p.getProperty ("os.version");
56 if (v.equals ("5.8") || v.equals ("8"))
57 return (false);
58 return (true);
59 }
60
61 static void print (String s) {
62 if (debug)
63 System.out.println (s);
64 }
65
66 class Server {
67
68 DatagramSocket server;
69 byte[] buf = new byte [128];
70 DatagramPacket pack = new DatagramPacket (buf, buf.length);
71
72 public Server (DatagramSocket s) {
73 server = s;
74 }
75
76 public void receive (int loop, boolean expectError) throws Exception {
77 for (int i=0; i<loop; i++) {
78 try {
79 server.receive (pack);
80 } catch (Exception e) {
81 if (expectError) {
82 print ("Got expected error: " + e);
83 continue;
84 } else {
85 print ("Got: " + new String (pack.getData()));
86 print ("Expected: " + new String (buf));
87 throw new Exception ("Error reading data: Iter " +i);
88 }
89 }
90 String s1 = "Hello, server"+i;
91 byte[] buf = s1.getBytes();
92 if (!s1.equals (new String (pack.getData(),
93 pack.getOffset(),pack.getLength()))) {
94 print ("Got: " + new String (pack.getData()));
95 print ("Expected: " + new String (buf));
96 throw new Exception ("Error comparing data: Iter " +i);
97 }
98 }
99 }
100 };
101
102 public static void main (String args[]) throws Exception {
103 if (args.length >=1 && args[0].equals ("-d")) {
104 debug = true;
105 }
106 SendDatagramToBadAddress ud = new SendDatagramToBadAddress ();
107 ud.run ();
108 }
109
110 public void run() throws Exception {
111
112 if (OSsupportsFeature()) {
113 print ("running on OS that supports ICMP port unreachable");
114 }
115 String host = "127.0.0.1";
116 InetAddress addr = InetAddress.getByName(host);
117 DatagramSocket sock = new DatagramSocket();
118 DatagramSocket serversock = new DatagramSocket(0);
119 DatagramPacket p;
120 byte[] buf;
121 int port = serversock.getLocalPort ();
122 final int loop = 5;
123 Server s = new Server (serversock);
124 int i;
125
126 print ("Checking send to connected address ...");
127 sock.connect(addr, port);
128
129 for (i = 0; i < loop; i++) {
130 try {
131 buf = ("Hello, server"+i).getBytes();
132 if (i % 2 == 1)
133 p = new DatagramPacket(buf, buf.length, addr, port);
134 else
135 p = new DatagramPacket(buf, buf.length);
136 sock.send(p);
137 } catch (Exception ex) {
138 print ("Got unexpected exception: " + ex);
139 throw new Exception ("Error sending data: ");
140 }
141 }
142
143 s.receive (loop, false);
144
145 // check disconnect() works
146
147 print ("Checking send to non-connected address ...");
148 sock.disconnect ();
149 buf = ("Hello, server"+0).getBytes();
150 p = new DatagramPacket(buf, buf.length, addr, port);
151 sock.send (p);
152 s.receive (1, false);
153
154 // check send() to invalid destination followed by a blocking receive
155 // returns an error
156
157 print ("Checking send to invalid address ...");
158 sock.connect(addr, port);
159 serversock.close ();
160 try {
161 sock.setSoTimeout (4000);
162 } catch (Exception e) {
163 print ("could not set timeout");
164 throw e;
165 }
166
167 boolean goterror = false;
168
169 for (i = 0; i < loop; i++) {
170 try {
171 buf = ("Hello, server"+i).getBytes();
172 p = new DatagramPacket(buf, buf.length, addr, port);
173 sock.send(p);
174 p = new DatagramPacket(buf, buf.length, addr, port);
175 sock.receive (p);
176 } catch (InterruptedIOException ex) {
177 print ("socket timeout");
178 } catch (Exception ex) {
179 print ("Got expected exception: " + ex);
180 goterror = true;
181 }
182 }
183
184 if (!goterror && OSsupportsFeature ()) {
185 print ("Didnt get expected exception: ");
186 throw new Exception ("send did not return expected error");
187 }
188 }
189}