author | alanb |
Tue, 09 Apr 2019 12:02:23 +0100 | |
branch | niosocketimpl-branch |
changeset 57313 | 14b02c7b27b8 |
parent 57309 | dbd1beb994ab |
child 57341 | 733e9746d615 |
permissions | -rw-r--r-- |
54216
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
1 |
/* |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
4 |
* |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
8 |
* |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
13 |
* accompanied this code). |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
14 |
* |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
18 |
* |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
21 |
* questions. |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
22 |
*/ |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
23 |
|
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
24 |
/** |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
25 |
* @test |
57308
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
26 |
* @run testng/othervm -Dsun.net.maxDatagramSockets=32 UdpSocket |
54216
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
27 |
* @summary Basic test for a Socket to a UDP socket |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
28 |
*/ |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
29 |
|
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
30 |
import java.io.IOException; |
57309 | 31 |
import java.lang.ref.WeakReference; |
54216
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
32 |
import java.net.InetAddress; |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
33 |
import java.net.InetSocketAddress; |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
34 |
import java.net.Socket; |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
35 |
import java.net.SocketAddress; |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
36 |
import java.nio.ByteBuffer; |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
37 |
import java.nio.channels.DatagramChannel; |
57308
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
38 |
import java.security.Permission; |
54216
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
39 |
import java.util.Arrays; |
57308
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
40 |
import java.util.ArrayList; |
57309 | 41 |
import java.util.ArrayDeque; |
42 |
import java.util.Deque; |
|
54216
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
43 |
|
57308
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
44 |
import org.testng.annotations.Test; |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
45 |
import static org.testng.Assert.*; |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
46 |
|
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
47 |
@Test |
54216
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
48 |
public class UdpSocket { |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
49 |
|
57308
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
50 |
/** |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
51 |
* Test using the Socket API to send/receive datagrams |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
52 |
*/ |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
53 |
public void testSendReceive() throws IOException { |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
54 |
final String MESSAGE = "hello"; |
54216
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
55 |
|
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
56 |
try (DatagramChannel dc = DatagramChannel.open()) { |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
57 |
var loopback = InetAddress.getLoopbackAddress(); |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
58 |
dc.bind(new InetSocketAddress(loopback, 0)); |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
59 |
|
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
60 |
int port = ((InetSocketAddress) dc.getLocalAddress()).getPort(); |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
61 |
try (Socket s = new Socket(loopback, port, false)) { |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
62 |
|
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
63 |
// send datagram with socket output stream |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
64 |
byte[] array1 = MESSAGE.getBytes("UTF-8"); |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
65 |
s.getOutputStream().write(array1); |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
66 |
|
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
67 |
// receive the datagram |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
68 |
var buf = ByteBuffer.allocate(100); |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
69 |
SocketAddress remote = dc.receive(buf); |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
70 |
buf.flip(); |
57308
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
71 |
assertTrue(buf.remaining() == MESSAGE.length(), "Unexpected size"); |
54216
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
72 |
|
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
73 |
// echo the datagram |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
74 |
dc.send(buf, remote); |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
75 |
|
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
76 |
// receive datagram with the socket input stream |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
77 |
byte[] array2 = new byte[100]; |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
78 |
int n = s.getInputStream().read(array2); |
57308
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
79 |
assertTrue(n == MESSAGE.length(), "Unexpected size"); |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
80 |
assertTrue(Arrays.equals(array1, 0, n, array2, 0, n), "Unexpected contents"); |
54216
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
81 |
} |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
82 |
} |
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
83 |
} |
57308
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
84 |
|
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
85 |
/** |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
86 |
* Test that the number of UDP sockets is limited when running with a |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
87 |
* security manager. |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
88 |
*/ |
57309 | 89 |
public void testMaxSockets() throws Exception { |
57308
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
90 |
int limit = Integer.getInteger("sun.net.maxDatagramSockets"); |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
91 |
|
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
92 |
// security manager grants all permissions |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
93 |
var securityManager = new SecurityManager() { |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
94 |
@Override public void checkPermission(Permission perm) { } |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
95 |
}; |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
96 |
|
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
97 |
System.setSecurityManager(securityManager); |
57309 | 98 |
Deque<Socket> sockets = new ArrayDeque<>(); |
57308
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
99 |
try { |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
100 |
// create the maximum number of sockets |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
101 |
for (int i=0; i<limit; i++) { |
57309 | 102 |
sockets.offer(newUdpSocket()); |
57308
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
103 |
} |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
104 |
|
57309 | 105 |
// try to create another socket - should fail |
57308
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
106 |
try { |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
107 |
Socket s = newUdpSocket(); |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
108 |
s.close(); |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
109 |
assertTrue(false); |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
110 |
} catch (IOException expected) { } |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
111 |
|
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
112 |
// close one socket |
57309 | 113 |
sockets.pop().close(); |
114 |
||
115 |
// try to create another socket - should succeed |
|
116 |
Socket s = newUdpSocket(); |
|
57308
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
117 |
|
57309 | 118 |
// unreference the socket and wait for it to be closed by the cleaner |
119 |
var ref = new WeakReference<>(s); |
|
120 |
s = null; |
|
121 |
while (ref.get() != null) { |
|
122 |
System.gc(); |
|
123 |
Thread.sleep(100); |
|
124 |
} |
|
125 |
||
126 |
// try to create another socket - should succeed |
|
127 |
s = newUdpSocket(); |
|
57308
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
128 |
s.close(); |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
129 |
} finally { |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
130 |
closeAll(sockets); |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
131 |
System.setSecurityManager(null); |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
132 |
} |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
133 |
} |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
134 |
|
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
135 |
private Socket newUdpSocket() throws IOException { |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
136 |
return new Socket(InetAddress.getLoopbackAddress(), 8000, false); |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
137 |
} |
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
138 |
|
57309 | 139 |
private static void closeAll(Deque<Socket> sockets) throws IOException { |
57313 | 140 |
Socket s; |
141 |
while ((s = sockets.poll()) != null) { |
|
142 |
s.close(); |
|
143 |
} |
|
57308
f22d38b23756
Add -Djdk.net.usePlainSocketImpl to important tests
alanb
parents:
57281
diff
changeset
|
144 |
} |
54216
f10ca228b22f
8221259: New tests for java.net.Socket to exercise long standing behavior
alanb
parents:
diff
changeset
|
145 |
} |