Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/UdpSocket.java
41149 views
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
* @run testng/othervm -Djava.security.manager=allow -Dsun.net.maxDatagramSockets=32 UdpSocket
27
* @summary Basic test for a Socket to a UDP socket
28
*/
29
30
import java.io.IOException;
31
import java.lang.ref.WeakReference;
32
import java.net.InetAddress;
33
import java.net.InetSocketAddress;
34
import java.net.Socket;
35
import java.net.SocketAddress;
36
import java.nio.ByteBuffer;
37
import java.nio.channels.DatagramChannel;
38
import java.security.Permission;
39
import java.util.Arrays;
40
import java.util.ArrayDeque;
41
import java.util.Deque;
42
import java.net.BindException;
43
44
import org.testng.annotations.Test;
45
import static org.testng.Assert.*;
46
47
@Test
48
public class UdpSocket {
49
50
/**
51
* Test using the Socket API to send/receive datagrams
52
*/
53
public void testSendReceive() throws IOException {
54
final String MESSAGE = "hello";
55
56
try (DatagramChannel dc = DatagramChannel.open()) {
57
var loopback = InetAddress.getLoopbackAddress();
58
dc.bind(new InetSocketAddress(loopback, 0));
59
60
int port = ((InetSocketAddress) dc.getLocalAddress()).getPort();
61
try (Socket s = new Socket(loopback, port, false)) {
62
// send datagram with socket output stream
63
byte[] array1 = MESSAGE.getBytes("UTF-8");
64
s.getOutputStream().write(array1);
65
66
// receive the datagram
67
var buf = ByteBuffer.allocate(100);
68
SocketAddress remote = dc.receive(buf);
69
buf.flip();
70
assertTrue(buf.remaining() == MESSAGE.length(), "Unexpected size");
71
72
// echo the datagram
73
dc.send(buf, remote);
74
75
// receive datagram with the socket input stream
76
byte[] array2 = new byte[100];
77
int n = s.getInputStream().read(array2);
78
assertTrue(n == MESSAGE.length(), "Unexpected size");
79
assertEquals(Arrays.copyOf(array1, n), Arrays.copyOf(array2, n),
80
"Unexpected contents");
81
}
82
}
83
}
84
85
/**
86
* Test that the number of UDP sockets is limited when running with a
87
* security manager.
88
*/
89
public void testMaxSockets() throws Exception {
90
int limit = Integer.getInteger("sun.net.maxDatagramSockets");
91
92
// security manager grants all permissions
93
var securityManager = new SecurityManager() {
94
@Override public void checkPermission(Permission perm) { }
95
};
96
97
System.setSecurityManager(securityManager);
98
Deque<Socket> sockets = new ArrayDeque<>();
99
try {
100
// create the maximum number of sockets
101
for (int i=0; i<limit; i++) {
102
sockets.offer(newUdpSocket());
103
}
104
105
// try to create another socket - should fail
106
try {
107
Socket s = newUdpSocket();
108
s.close();
109
assertTrue(false);
110
} catch (IOException expected) { }
111
112
// close one socket
113
sockets.pop().close();
114
115
// try to create another socket - should succeed
116
Socket s = newUdpSocket();
117
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();
128
s.close();
129
} finally {
130
closeAll(sockets);
131
System.setSecurityManager(null);
132
}
133
}
134
135
136
private Socket newUdpSocket() throws IOException {
137
Socket s = null;
138
139
try {
140
s = new Socket(InetAddress.getLoopbackAddress(), 8000, false);
141
} catch (BindException unexpected) {
142
System.out.println("BindException caught retry Socket creation");
143
s = new Socket(InetAddress.getLoopbackAddress(), 8000, false);
144
}
145
return s;
146
}
147
148
private void closeAll(Deque<Socket> sockets) throws IOException {
149
Socket s;
150
while ((s = sockets.poll()) != null) {
151
s.close();
152
}
153
}
154
}
155
156