Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/SocketPermission/SocketPermissionTest.java
41149 views
1
/*
2
* Copyright (c) 2014, 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 8047031
27
* @key intermittent
28
* @summary SocketPermission tests for legacy socket types.
29
* This test needs to bind its servers to the wildcard
30
* address and as such may fail intermittently.
31
* @library /test/lib
32
* @build jdk.test.lib.NetworkConfiguration
33
* jdk.test.lib.Platform
34
* @run testng/othervm -Djava.security.manager=allow SocketPermissionTest
35
*/
36
37
import java.io.IOException;
38
import java.net.DatagramPacket;
39
import java.net.DatagramSocket;
40
import java.net.InetAddress;
41
import java.net.MulticastSocket;
42
import java.net.NetworkInterface;
43
import java.net.ServerSocket;
44
import java.net.Socket;
45
import java.net.SocketPermission;
46
import java.security.AccessControlContext;
47
import java.security.AccessController;
48
import java.security.CodeSource;
49
import java.security.Permission;
50
import java.security.PermissionCollection;
51
import java.security.Permissions;
52
import java.security.Policy;
53
import java.security.PrivilegedExceptionAction;
54
import java.security.ProtectionDomain;
55
import java.util.Optional;
56
57
import org.testng.annotations.BeforeMethod;
58
import org.testng.annotations.Test;
59
60
import static org.testng.Assert.*;
61
62
import static jdk.test.lib.NetworkConfiguration.probe;
63
import static java.nio.charset.StandardCharsets.UTF_8;
64
65
public class SocketPermissionTest {
66
67
@BeforeMethod
68
public void setupSecurityManager() throws Exception {
69
// All permissions, a specific ACC will be used to when testing
70
// with a reduced permission set.
71
Policy.setPolicy(new Policy() {
72
final PermissionCollection perms = new Permissions();
73
{ perms.add(new java.security.AllPermission()); }
74
public PermissionCollection getPermissions(ProtectionDomain domain) {
75
return perms;
76
}
77
public PermissionCollection getPermissions(CodeSource codesource) {
78
return perms;
79
}
80
public boolean implies(ProtectionDomain domain, Permission perm) {
81
return perms.implies(perm);
82
}
83
} );
84
System.setSecurityManager(new SecurityManager());
85
}
86
87
static final AccessControlContext RESTRICTED_ACC = getAccessControlContext();
88
89
@Test
90
public void connectSocketTest() throws Exception {
91
try (ServerSocket ss = new ServerSocket(0)) {
92
int port = ss.getLocalPort();
93
94
String addr = "localhost:" + port;
95
AccessControlContext acc = getAccessControlContext(
96
new SocketPermission(addr, "listen,connect,resolve"));
97
98
// Positive
99
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
100
try (Socket client = new Socket(InetAddress.getLocalHost(), port)) {
101
}
102
return null;
103
}, acc);
104
105
//Negative
106
try {
107
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
108
Socket client = new Socket(InetAddress.getLocalHost(), port);
109
fail("Expected SecurityException");
110
return null;
111
}, RESTRICTED_ACC);
112
} catch (SecurityException expected) { }
113
}
114
}
115
116
@Test
117
public void connectDatagramSocketTest() throws Exception {
118
byte[] msg = "Hello".getBytes(UTF_8);
119
InetAddress lh = InetAddress.getLocalHost();
120
121
try (DatagramSocket ds = new DatagramSocket(0)) {
122
int port = ds.getLocalPort();
123
124
String addr = lh.getHostAddress() + ":" + port;
125
AccessControlContext acc = getAccessControlContext(
126
new SocketPermission(addr, "connect,resolve"));
127
128
// Positive
129
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
130
DatagramPacket dp = new DatagramPacket(msg, msg.length, lh, port);
131
ds.send(dp);
132
return null;
133
}, acc);
134
135
// Negative
136
try {
137
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
138
DatagramPacket dp = new DatagramPacket(msg, msg.length, lh, port);
139
ds.send(dp);
140
fail("Expected SecurityException");
141
return null;
142
}, RESTRICTED_ACC);
143
} catch (SecurityException expected) { }
144
}
145
}
146
147
@Test
148
public void acceptServerSocketTest() throws Exception {
149
try (ServerSocket ss = new ServerSocket(0)) {
150
int port = ss.getLocalPort();
151
152
String addr = "localhost:" + port;
153
AccessControlContext acc = getAccessControlContext(
154
new SocketPermission(addr, "listen,connect,resolve"),
155
new SocketPermission("localhost:1024-", "accept"));
156
157
// Positive
158
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
159
InetAddress me = InetAddress.getLocalHost();
160
try (Socket client = new Socket(me, port)) {
161
ss.accept();
162
}
163
return null;
164
}, acc);
165
166
// Negative
167
try {
168
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
169
InetAddress me = InetAddress.getLocalHost();
170
try (Socket client = new Socket(me, port)) {
171
ss.accept();
172
}
173
fail("Expected SecurityException");
174
return null;
175
}, RESTRICTED_ACC);
176
} catch (SecurityException expected) { }
177
}
178
}
179
180
@Test
181
public void sendDatagramPacketTest() throws Exception {
182
byte[] msg = "Hello".getBytes(UTF_8);
183
InetAddress group = InetAddress.getByName("229.227.226.221");
184
185
try (DatagramSocket ds = new DatagramSocket(0)) {
186
int port = ds.getLocalPort();
187
188
String addr = "localhost:" + port;
189
//test for SocketPermission "229.227.226.221", "connect,accept"
190
AccessControlContext acc = getAccessControlContext(
191
new SocketPermission(addr, "listen,resolve"),
192
new SocketPermission("229.227.226.221", "connect,accept"));
193
194
// Positive
195
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
196
DatagramPacket hi = new DatagramPacket(msg, msg.length, group, port);
197
ds.send(hi);
198
return null;
199
}, acc);
200
201
// Negative
202
try {
203
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
204
DatagramPacket hi = new DatagramPacket(msg, msg.length, group, port);
205
ds.send(hi);
206
fail("Expected SecurityException");
207
return null;
208
}, RESTRICTED_ACC);
209
} catch (SecurityException expected) { }
210
}
211
}
212
213
@Test
214
public void joinGroupMulticastTest() throws Exception {
215
InetAddress group = InetAddress.getByName("229.227.226.221");
216
try (MulticastSocket s = new MulticastSocket(0)) {
217
int port = s.getLocalPort();
218
219
String addr = "localhost:" + port;
220
AccessControlContext acc = getAccessControlContext(
221
new SocketPermission(addr, "listen,resolve"),
222
new SocketPermission("229.227.226.221", "connect,accept"));
223
224
// Positive ( requires a functional network interface )
225
Optional<NetworkInterface> onif = probe().ip4MulticastInterfaces().findFirst();
226
if (!onif.isPresent()) {
227
s.setNetworkInterface(onif.get());
228
229
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
230
s.joinGroup(group);
231
s.leaveGroup(group);
232
return null;
233
}, acc);
234
}
235
236
// Negative
237
try {
238
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
239
s.joinGroup(group);
240
s.leaveGroup(group);
241
fail("Expected SecurityException");
242
return null;
243
}, RESTRICTED_ACC);
244
} catch (SecurityException expected) { }
245
}
246
247
}
248
249
@Test
250
public void listenDatagramSocketTest() throws Exception {
251
// the hardcoded port number doesn't really matter since we expect the
252
// security permission to be checked before the underlying operation.
253
int port = 8899;
254
String addr = "localhost:" + port;
255
AccessControlContext acc = getAccessControlContext(
256
new SocketPermission(addr, "listen"));
257
258
// Positive
259
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
260
try (DatagramSocket ds = new DatagramSocket(port)) { }
261
catch (IOException intermittentlyExpected) { /* ignore */ }
262
return null;
263
}, acc);
264
265
// Negative
266
try {
267
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
268
try (DatagramSocket ds = new DatagramSocket(port)) { }
269
catch (IOException intermittentlyExpected) { /* ignore */ }
270
fail("Expected SecurityException");
271
return null;
272
}, RESTRICTED_ACC);
273
} catch (SecurityException expected) { }
274
}
275
276
@Test
277
public void listenMulticastSocketTest() throws Exception {
278
// the hardcoded port number doesn't really matter since we expect the
279
// security permission to be checked before the underlying operation.
280
int port = 8899;
281
String addr = "localhost:" + port;
282
AccessControlContext acc = getAccessControlContext(
283
new SocketPermission(addr, "listen"));
284
285
// Positive
286
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
287
try (MulticastSocket ms = new MulticastSocket(port)) { }
288
catch (IOException intermittentlyExpected) { /* ignore */ }
289
return null;
290
}, acc);
291
292
// Negative
293
try {
294
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
295
try (MulticastSocket ms = new MulticastSocket(port)) { }
296
catch (IOException intermittentlyExpected) { /* ignore */ }
297
fail("Expected SecurityException");
298
return null;
299
}, RESTRICTED_ACC);
300
} catch (SecurityException expected) { }
301
}
302
303
@Test
304
public void listenServerSocketTest() throws Exception {
305
// the hardcoded port number doesn't really matter since we expect the
306
// security permission to be checked before the underlying operation.
307
int port = 8899;
308
String addr = "localhost:" + port;
309
AccessControlContext acc = getAccessControlContext(
310
new SocketPermission(addr, "listen"));
311
312
// Positive
313
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
314
try (ServerSocket ss = new ServerSocket(port)) { }
315
catch (IOException intermittentlyExpected) { /* ignore */ }
316
return null;
317
}, acc);
318
319
// Negative
320
try {
321
AccessController.doPrivileged((PrivilegedExceptionAction<Void>) () -> {
322
try (ServerSocket ss = new ServerSocket(port)) { }
323
catch (IOException intermittentlyExpected) { /* ignore */ }
324
fail("Expected SecurityException");
325
return null;
326
}, RESTRICTED_ACC);
327
} catch (SecurityException expected) { }
328
329
}
330
331
private static AccessControlContext getAccessControlContext(Permission... ps) {
332
Permissions perms = new Permissions();
333
for (Permission p : ps) {
334
perms.add(p);
335
}
336
/*
337
*Create an AccessControlContext that consist a single protection domain
338
* with only the permissions calculated above
339
*/
340
ProtectionDomain pd = new ProtectionDomain(null, perms);
341
return new AccessControlContext(new ProtectionDomain[]{pd});
342
}
343
344
// Standalone entry point for running with, possibly older, JDKs.
345
public static void main(String[] args) throws Throwable {
346
SocketPermissionTest test = new SocketPermissionTest();
347
test.setupSecurityManager();
348
for (java.lang.reflect.Method m : SocketPermissionTest.class.getDeclaredMethods()) {
349
if (m.getAnnotation(Test.class) != null) {
350
System.out.println("Invoking " + m.getName());
351
m.invoke(test);
352
}
353
}
354
}
355
}
356
357