author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 58899 | 5573a7098439 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
2 |
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
25 |
* @bug 4313882 4981129 8143610 8232673 |
2 | 26 |
* @summary Unit test for datagram-socket-channel adaptors |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
27 |
* @modules java.base/java.net:+open |
49255
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
28 |
* @library .. /test/lib |
acdb8531cc8b
8199215: Re-examine getFreePort method in test infrastructure library
mli
parents:
47216
diff
changeset
|
29 |
* @build jdk.test.lib.Utils TestServers |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
30 |
* @run main AdaptorBasic |
30046 | 31 |
* @key randomness |
2 | 32 |
*/ |
33 |
||
34 |
import java.net.*; |
|
35 |
import java.nio.channels.*; |
|
36 |
import java.util.*; |
|
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
37 |
import java.lang.reflect.Field; |
2 | 38 |
|
39 |
||
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
40 |
public class AdaptorBasic { |
2 | 41 |
|
42 |
static java.io.PrintStream out = System.out; |
|
43 |
static Random rand = new Random(); |
|
44 |
||
45 |
static String toString(DatagramPacket dp) { |
|
46 |
return ("DatagramPacket[off=" + dp.getOffset() |
|
47 |
+ ", len=" + dp.getLength() |
|
48 |
+ "]"); |
|
49 |
} |
|
50 |
||
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
51 |
static int getBufLength(DatagramPacket p) throws Exception { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
52 |
Field f = DatagramPacket.class.getDeclaredField("bufLength"); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
53 |
f.setAccessible(true); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
54 |
return (int) f.get(p); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
55 |
} |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
56 |
|
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
57 |
static void test(DatagramSocket ds, InetSocketAddress dst, boolean shouldTimeout) |
2 | 58 |
throws Exception |
59 |
{ |
|
60 |
DatagramPacket op = new DatagramPacket(new byte[100], 13, 42, dst); |
|
61 |
rand.nextBytes(op.getData()); |
|
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
62 |
int bufLength = 100 - 19; |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
63 |
DatagramPacket ip = new DatagramPacket(new byte[100], 19, bufLength); |
2 | 64 |
out.println("pre op: " + toString(op) + " ip: " + toString(ip)); |
65 |
||
66 |
long start = System.currentTimeMillis(); |
|
67 |
ds.send(op); |
|
68 |
||
69 |
for (;;) { |
|
70 |
try { |
|
71 |
ds.receive(ip); |
|
72 |
} catch (SocketTimeoutException x) { |
|
73 |
if (shouldTimeout) { |
|
74 |
out.println("Receive timed out, as expected"); |
|
75 |
return; |
|
76 |
} |
|
77 |
throw x; |
|
78 |
} |
|
79 |
break; |
|
80 |
} |
|
81 |
||
82 |
out.println("rtt: " + (System.currentTimeMillis() - start)); |
|
83 |
out.println("post op: " + toString(op) + " ip: " + toString(ip)); |
|
84 |
||
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
85 |
for (int i = 0; i < ip.getLength(); i++) { |
2 | 86 |
if (ip.getData()[ip.getOffset() + i] |
87 |
!= op.getData()[op.getOffset() + i]) |
|
88 |
throw new Exception("Incorrect data received"); |
|
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
89 |
} |
5807
d34ed576e234
4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents:
5506
diff
changeset
|
90 |
|
d34ed576e234
4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents:
5506
diff
changeset
|
91 |
if (!(ip.getSocketAddress().equals(dst))) { |
d34ed576e234
4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents:
5506
diff
changeset
|
92 |
throw new Exception("Incorrect sender address, expected: " + dst |
d34ed576e234
4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents:
5506
diff
changeset
|
93 |
+ " actual: " + ip.getSocketAddress()); |
d34ed576e234
4981129: (dc) DatagramSocket created by DatagramChannel does not provide sender info
alanb
parents:
5506
diff
changeset
|
94 |
} |
58899
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
95 |
|
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
96 |
if (getBufLength(ip) != bufLength) { |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
97 |
throw new Exception("DatagramPacket bufLength changed by receive!!!"); |
5573a7098439
8232673: (dc) DatagramChannel socket adaptor issues
alanb
parents:
49255
diff
changeset
|
98 |
} |
2 | 99 |
} |
100 |
||
101 |
static void test(InetSocketAddress dst, |
|
102 |
int timeout, boolean shouldTimeout, |
|
103 |
boolean connect) |
|
104 |
throws Exception |
|
105 |
{ |
|
106 |
out.println(); |
|
107 |
out.println("dst: " + dst); |
|
108 |
||
109 |
DatagramSocket ds; |
|
110 |
if (false) { |
|
111 |
// Original |
|
112 |
ds = new DatagramSocket(); |
|
113 |
} else { |
|
114 |
DatagramChannel dc = DatagramChannel.open(); |
|
115 |
ds = dc.socket(); |
|
116 |
ds.bind(new InetSocketAddress(0)); |
|
117 |
} |
|
118 |
||
119 |
out.println("socket: " + ds); |
|
120 |
if (connect) { |
|
121 |
ds.connect(dst); |
|
122 |
out.println("connect: " + ds); |
|
123 |
} |
|
124 |
InetSocketAddress src = new InetSocketAddress(ds.getLocalAddress(), |
|
125 |
ds.getLocalPort()); |
|
126 |
out.println("src: " + src); |
|
127 |
||
128 |
if (timeout > 0) |
|
129 |
ds.setSoTimeout(timeout); |
|
130 |
out.println("timeout: " + ds.getSoTimeout()); |
|
131 |
||
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
132 |
for (int i = 0; i < 5; i++) { |
2 | 133 |
test(ds, dst, shouldTimeout); |
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
134 |
} |
2 | 135 |
|
136 |
// Leave the socket open so that we don't reuse the old src address |
|
137 |
//ds.close(); |
|
138 |
||
139 |
} |
|
140 |
||
141 |
public static void main(String[] args) throws Exception { |
|
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
142 |
// need an UDP echo server |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
143 |
try (TestServers.UdpEchoServer echoServer |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
144 |
= TestServers.UdpEchoServer.startNewServer(100)) { |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
145 |
final InetSocketAddress address |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
146 |
= new InetSocketAddress(echoServer.getAddress(), |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
147 |
echoServer.getPort()); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
148 |
test(address, 0, false, false); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
149 |
test(address, 0, false, true); |
36410
c2ef247d0b6f
8143610: (dc) java/nio/channels/DatagramChannel/AdaptDatagramSocket.java failed intermittently due to SocketTimeoutException
mli
parents:
30046
diff
changeset
|
150 |
test(address, Integer.MAX_VALUE, false, false); |
14415
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
151 |
} |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
152 |
try (TestServers.UdpDiscardServer discardServer |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
153 |
= TestServers.UdpDiscardServer.startNewServer()) { |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
154 |
final InetSocketAddress address |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
155 |
= new InetSocketAddress(discardServer.getAddress(), |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
156 |
discardServer.getPort()); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
157 |
test(address, 10, true, false); |
7a31b0e0cfaf
6720349: (ch) Channels tests depending on hosts inside Sun
dfuchs
parents:
7668
diff
changeset
|
158 |
} |
2 | 159 |
} |
160 |
||
161 |
} |