1 /* |
|
2 * Copyright (c) 2001, 2018, 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 /* @test |
|
25 * @bug 4313882 4981129 8143610 |
|
26 * @summary Unit test for datagram-socket-channel adaptors |
|
27 * @library .. /test/lib |
|
28 * @build jdk.test.lib.Utils TestServers |
|
29 * @run main AdaptDatagramSocket |
|
30 * @key randomness |
|
31 */ |
|
32 |
|
33 import java.net.*; |
|
34 import java.nio.channels.*; |
|
35 import java.util.*; |
|
36 |
|
37 |
|
38 public class AdaptDatagramSocket { |
|
39 |
|
40 static java.io.PrintStream out = System.out; |
|
41 static Random rand = new Random(); |
|
42 |
|
43 static String toString(DatagramPacket dp) { |
|
44 return ("DatagramPacket[off=" + dp.getOffset() |
|
45 + ", len=" + dp.getLength() |
|
46 + "]"); |
|
47 } |
|
48 |
|
49 static void test(DatagramSocket ds, InetSocketAddress dst, |
|
50 boolean shouldTimeout) |
|
51 throws Exception |
|
52 { |
|
53 DatagramPacket op = new DatagramPacket(new byte[100], 13, 42, dst); |
|
54 rand.nextBytes(op.getData()); |
|
55 DatagramPacket ip = new DatagramPacket(new byte[100], 19, 100 - 19); |
|
56 out.println("pre op: " + toString(op) + " ip: " + toString(ip)); |
|
57 |
|
58 long start = System.currentTimeMillis(); |
|
59 ds.send(op); |
|
60 |
|
61 for (;;) { |
|
62 try { |
|
63 ds.receive(ip); |
|
64 if (ip.getLength() == 0) { // ## Not sure why this happens |
|
65 ip.setLength(100 - 19); |
|
66 continue; |
|
67 } |
|
68 } catch (SocketTimeoutException x) { |
|
69 if (shouldTimeout) { |
|
70 out.println("Receive timed out, as expected"); |
|
71 return; |
|
72 } |
|
73 throw x; |
|
74 } |
|
75 break; |
|
76 } |
|
77 |
|
78 out.println("rtt: " + (System.currentTimeMillis() - start)); |
|
79 out.println("post op: " + toString(op) + " ip: " + toString(ip)); |
|
80 |
|
81 for (int i = 0; i < ip.getLength(); i++) { |
|
82 if (ip.getData()[ip.getOffset() + i] |
|
83 != op.getData()[op.getOffset() + i]) |
|
84 throw new Exception("Incorrect data received"); |
|
85 } |
|
86 |
|
87 if (!(ip.getSocketAddress().equals(dst))) { |
|
88 throw new Exception("Incorrect sender address, expected: " + dst |
|
89 + " actual: " + ip.getSocketAddress()); |
|
90 } |
|
91 } |
|
92 |
|
93 static void test(InetSocketAddress dst, |
|
94 int timeout, boolean shouldTimeout, |
|
95 boolean connect) |
|
96 throws Exception |
|
97 { |
|
98 out.println(); |
|
99 out.println("dst: " + dst); |
|
100 |
|
101 DatagramSocket ds; |
|
102 if (false) { |
|
103 // Original |
|
104 ds = new DatagramSocket(); |
|
105 } else { |
|
106 DatagramChannel dc = DatagramChannel.open(); |
|
107 ds = dc.socket(); |
|
108 ds.bind(new InetSocketAddress(0)); |
|
109 } |
|
110 |
|
111 out.println("socket: " + ds); |
|
112 if (connect) { |
|
113 ds.connect(dst); |
|
114 out.println("connect: " + ds); |
|
115 } |
|
116 InetSocketAddress src = new InetSocketAddress(ds.getLocalAddress(), |
|
117 ds.getLocalPort()); |
|
118 out.println("src: " + src); |
|
119 |
|
120 if (timeout > 0) |
|
121 ds.setSoTimeout(timeout); |
|
122 out.println("timeout: " + ds.getSoTimeout()); |
|
123 |
|
124 for (int i = 0; i < 5; i++) { |
|
125 test(ds, dst, shouldTimeout); |
|
126 } |
|
127 |
|
128 // Leave the socket open so that we don't reuse the old src address |
|
129 //ds.close(); |
|
130 |
|
131 } |
|
132 |
|
133 public static void main(String[] args) throws Exception { |
|
134 // need an UDP echo server |
|
135 try (TestServers.UdpEchoServer echoServer |
|
136 = TestServers.UdpEchoServer.startNewServer(100)) { |
|
137 final InetSocketAddress address |
|
138 = new InetSocketAddress(echoServer.getAddress(), |
|
139 echoServer.getPort()); |
|
140 test(address, 0, false, false); |
|
141 test(address, 0, false, true); |
|
142 test(address, Integer.MAX_VALUE, false, false); |
|
143 } |
|
144 try (TestServers.UdpDiscardServer discardServer |
|
145 = TestServers.UdpDiscardServer.startNewServer()) { |
|
146 final InetSocketAddress address |
|
147 = new InetSocketAddress(discardServer.getAddress(), |
|
148 discardServer.getPort()); |
|
149 test(address, 10, true, false); |
|
150 } |
|
151 } |
|
152 |
|
153 } |
|