|
1 /* |
|
2 * Copyright (c) 2019, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 8233141 |
|
27 * @summary DatagramSocket.send should throw IllegalArgumentException |
|
28 * when the packet address is not correctly set. |
|
29 * @run main AddressNotSet |
|
30 */ |
|
31 |
|
32 import java.net.DatagramPacket; |
|
33 import java.net.DatagramSocket; |
|
34 import java.net.InetAddress; |
|
35 import java.net.MulticastSocket; |
|
36 import java.net.SocketAddress; |
|
37 import java.nio.channels.DatagramChannel; |
|
38 |
|
39 import static java.lang.System.out; |
|
40 |
|
41 public class AddressNotSet { |
|
42 |
|
43 final InetAddress loopbackAddress = InetAddress.getLoopbackAddress(); |
|
44 final DatagramSocket serversock; |
|
45 int i; |
|
46 AddressNotSet() throws Exception { |
|
47 serversock = new DatagramSocket(0, loopbackAddress); |
|
48 } |
|
49 |
|
50 public static void main (String args[]) throws Exception { |
|
51 new AddressNotSet().run(); |
|
52 } |
|
53 |
|
54 public void run() throws Exception { |
|
55 try (var ss = serversock) { |
|
56 try (DatagramSocket sock = new DatagramSocket()) { |
|
57 test(sock); |
|
58 } |
|
59 try (DatagramSocket sock = new MulticastSocket()) { |
|
60 test(sock); |
|
61 } |
|
62 try (DatagramSocket sock = DatagramChannel.open().socket()) { |
|
63 test(sock); |
|
64 } |
|
65 } |
|
66 } |
|
67 |
|
68 private void test(DatagramSocket sock) throws Exception { |
|
69 out.println("Testing with " + sock.getClass()); |
|
70 InetAddress addr = loopbackAddress; |
|
71 byte[] buf; |
|
72 DatagramPacket p; |
|
73 int port = serversock.getLocalPort(); |
|
74 SocketAddress connectedAddress = serversock.getLocalSocketAddress(); |
|
75 |
|
76 out.println("Checking send to non-connected address ..."); |
|
77 try { |
|
78 out.println("Checking send with no packet address"); |
|
79 buf = ("Hello, server"+(++i)).getBytes(); |
|
80 p = new DatagramPacket(buf, buf.length); |
|
81 sock.send(p); |
|
82 throw new AssertionError("Expected IllegalArgumentException not received"); |
|
83 } catch (IllegalArgumentException x) { |
|
84 out.println("Got expected exception: " + x); |
|
85 } |
|
86 |
|
87 out.println("Checking send to valid address"); |
|
88 buf = ("Hello, server"+(++i)).getBytes(); |
|
89 p = new DatagramPacket(buf, buf.length, addr, port); |
|
90 sock.send(p); |
|
91 serversock.receive(p); |
|
92 |
|
93 out.println("Connecting to server address: " + connectedAddress); |
|
94 sock.connect(connectedAddress); |
|
95 |
|
96 try { |
|
97 out.println("Checking send with different address than connected"); |
|
98 buf = ("Hello, server"+(++i)).getBytes(); |
|
99 p = new DatagramPacket(buf, buf.length, addr, port+1); |
|
100 sock.send(p); |
|
101 throw new AssertionError("Expected IllegalArgumentException not received"); |
|
102 } catch (IllegalArgumentException x) { |
|
103 out.println("Got expected exception: " + x); |
|
104 } |
|
105 |
|
106 out.println("Checking send to valid address"); |
|
107 buf = ("Hello, server"+(++i)).getBytes(); |
|
108 p = new DatagramPacket(buf, buf.length, addr, port); |
|
109 sock.send(p); |
|
110 serversock.receive(p); |
|
111 |
|
112 if (sock instanceof MulticastSocket) { |
|
113 sock.disconnect(); |
|
114 testTTL((MulticastSocket)sock); |
|
115 } |
|
116 } |
|
117 |
|
118 private void testTTL(MulticastSocket sock) throws Exception { |
|
119 out.println("Testing deprecated send TTL with " + sock.getClass()); |
|
120 final byte ttl = 100; |
|
121 InetAddress addr = loopbackAddress; |
|
122 byte[] buf; |
|
123 DatagramPacket p; |
|
124 int port = serversock.getLocalPort(); |
|
125 |
|
126 out.println("Checking send to non-connected address ..."); |
|
127 try { |
|
128 out.println("Checking send with no packet address"); |
|
129 buf = ("Hello, server"+(++i)).getBytes(); |
|
130 p = new DatagramPacket(buf, buf.length); |
|
131 sock.send(p,ttl); |
|
132 throw new AssertionError("Expected IllegalArgumentException not received"); |
|
133 } catch (IllegalArgumentException x) { |
|
134 out.println("Got expected exception: " + x); |
|
135 } |
|
136 |
|
137 out.println("Connecting to connected address: " + sock); |
|
138 sock.connect(addr, port); |
|
139 |
|
140 try { |
|
141 out.println("Checking send with different address than connected"); |
|
142 buf = ("Hello, server"+(++i)).getBytes(); |
|
143 p = new DatagramPacket(buf, buf.length, addr, port+1); |
|
144 sock.send(p, ttl); |
|
145 throw new AssertionError("Expected IllegalArgumentException not received"); |
|
146 } catch (IllegalArgumentException x) { |
|
147 out.println("Got expected exception: " + x); |
|
148 } |
|
149 } |
|
150 } |