Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/unixdomain/Security.java
41153 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
* @bug 8245194
27
* @run main/othervm/java.security.policy=policy1 Security policy1
28
* @run main/othervm/java.security.policy=policy2 Security policy2
29
* @run main/othervm -Djava.security.manager=allow Security policy3
30
* @summary Security test for Unix Domain socket and server socket channels
31
*/
32
33
import java.io.File;
34
import java.io.IOException;
35
import java.net.SocketAddress;
36
import java.net.UnixDomainSocketAddress;
37
import java.nio.channels.*;
38
import java.nio.file.Files;
39
import java.nio.file.Path;
40
import java.util.Comparator;
41
42
import static java.net.StandardProtocolFamily.UNIX;
43
44
/**
45
* Tests required all with security manager
46
*/
47
48
public class Security {
49
50
static interface Command {
51
public void run() throws Exception;
52
}
53
54
static <T extends Exception> void call(Command r, Class<? extends Exception> expectedException) {
55
boolean threw = false;
56
try {
57
r.run();
58
} catch (Throwable t) {
59
if (expectedException == null) {
60
t.printStackTrace();
61
throw new RuntimeException("an exception was thrown but was not expected");
62
}
63
threw = true;
64
if (!(expectedException.isAssignableFrom(t.getClass()))) {
65
throw new RuntimeException("wrong exception type thrown " + t.toString());
66
}
67
}
68
if (expectedException != null && !threw) {
69
// should have thrown
70
throw new RuntimeException("% was expected".formatted(expectedException.getName()));
71
}
72
}
73
74
75
public static void main(String[] args) throws Exception {
76
try {
77
SocketChannel.open(UNIX);
78
} catch (UnsupportedOperationException e) {
79
System.out.println("Unix domain not supported");
80
return;
81
}
82
83
String policy = args[0];
84
switch (policy) {
85
case "policy1":
86
testPolicy1();
87
break;
88
case "policy2":
89
testPolicy2();
90
break;
91
case "policy3":
92
testPolicy3();
93
break;
94
}
95
}
96
97
static void setSecurityManager(String policy) {
98
String testSrc = System.getProperty("test.src");
99
// Three /// required for Windows below
100
String policyURL = "file:///" + testSrc + File.separator + policy;
101
System.out.println("POLICY: " + policyURL);
102
System.setProperty("java.security.policy", policyURL);
103
System.setSecurityManager(new SecurityManager());
104
}
105
106
static void close(NetworkChannel... channels) {
107
108
for (NetworkChannel chan : channels) {
109
try {
110
chan.close();
111
} catch (Exception e) {
112
}
113
}
114
}
115
116
private static final Class<SecurityException> SE = SecurityException.class;
117
private static final Class<IOException> IOE = IOException.class;
118
119
// No permission
120
121
public static void testPolicy1() throws Exception {
122
Path servername = Path.of("sock");
123
Files.deleteIfExists(servername);
124
// Permission exists to bind a ServerSocketChannel
125
final UnixDomainSocketAddress saddr = UnixDomainSocketAddress.of(servername);
126
try (final ServerSocketChannel server = ServerSocketChannel.open(UNIX)) {
127
try (final SocketChannel client = SocketChannel.open(UNIX)) {
128
call(() -> {
129
server.bind(saddr);
130
}, SE);
131
call(() -> {
132
client.connect(saddr);
133
}, SE);
134
}
135
} finally {
136
Files.deleteIfExists(servername);
137
}
138
}
139
140
// All permissions
141
142
public static void testPolicy2() throws Exception {
143
Path servername = Path.of("sock");
144
Files.deleteIfExists(servername);
145
final UnixDomainSocketAddress saddr = UnixDomainSocketAddress.of(servername);
146
try (final ServerSocketChannel server = ServerSocketChannel.open(UNIX)) {
147
try (final SocketChannel client = SocketChannel.open(UNIX)) {
148
call(() -> {
149
server.bind(saddr);
150
}, null);
151
call(() -> {
152
client.connect(saddr);
153
}, null);
154
try (final SocketChannel peer = server.accept()) {
155
// Should succeed
156
}
157
}
158
} finally {
159
Files.deleteIfExists(servername);
160
}
161
}
162
163
public static void testPolicy3() throws Exception {
164
Path sock1 = Path.of("sock3");
165
Path sock2 = null;
166
Files.deleteIfExists(sock1);
167
final UnixDomainSocketAddress saddr = UnixDomainSocketAddress.of(sock1);
168
try (var s1 = ServerSocketChannel.open(UNIX)) {
169
s1.bind(saddr);
170
try (var s2 = ServerSocketChannel.open(UNIX)) {
171
s2.bind(null);
172
var add2 = (UnixDomainSocketAddress)s2.getLocalAddress();
173
sock2 = add2.getPath();
174
175
// Now set security manager and check if we can see addresses
176
177
setSecurityManager("policy3");
178
179
if (((UnixDomainSocketAddress)s1
180
.getLocalAddress())
181
.getPath()
182
.toString()
183
.length() != 0)
184
{
185
throw new RuntimeException("address should have been empty");
186
}
187
188
if (((UnixDomainSocketAddress)s2
189
.getLocalAddress())
190
.getPath()
191
.toString()
192
.length() != 0)
193
{
194
throw new RuntimeException("address should have been empty");
195
}
196
}
197
} finally {
198
System.setSecurityManager(null);
199
Files.deleteIfExists(sock1);
200
Files.deleteIfExists(sock2);
201
}
202
}
203
}
204
205