Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/unixdomain/Bind.java
41153 views
1
/*
2
* Copyright (c) 2020, 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
* @library /test/lib
28
* @run main/othervm Bind
29
*/
30
31
import java.io.IOException;
32
import java.net.*;
33
import java.nio.channels.*;
34
import java.nio.file.Files;
35
import java.nio.file.Path;
36
import java.util.Arrays;
37
import jtreg.SkippedException;
38
39
/**
40
* Check that all bind variations work
41
*/
42
public class Bind {
43
44
static Path spath, cpath;
45
46
static UnixDomainSocketAddress sAddr, cAddr, UNNAMED, nullAddr;
47
static ServerSocketChannel server;
48
static SocketChannel client, accept1;
49
50
public static void main(String args[]) throws Exception {
51
checkSupported();
52
spath = Path.of("server.sock");
53
cpath = Path.of("client.sock");
54
sAddr = UnixDomainSocketAddress.of(spath);
55
cAddr = UnixDomainSocketAddress.of(cpath);
56
nullAddr = UnixDomainSocketAddress.of("");
57
UNNAMED = nullAddr;
58
runTests();
59
}
60
61
static void checkSupported() {
62
try {
63
SocketChannel.open(StandardProtocolFamily.UNIX).close();
64
} catch (UnsupportedOperationException e) {
65
throw new SkippedException("Unix domain channels not supported");
66
} catch (Exception e) {
67
// continue test to see what problem is
68
}
69
}
70
71
static interface ThrowingRunnable {
72
public void run() throws Exception;
73
}
74
75
static void init() throws IOException {
76
Files.deleteIfExists(cpath);
77
Files.deleteIfExists(spath);
78
client = null; server = null; accept1 = null;
79
}
80
81
static void checkNormal(ThrowingRunnable r) {
82
try {
83
init();
84
r.run();
85
System.out.println("PASS:");
86
} catch (Exception e) {
87
throw new RuntimeException(e);
88
} finally {
89
cleanup();
90
}
91
}
92
93
static void checkException(Class<? extends Exception> expected, ThrowingRunnable r) {
94
try {
95
init();
96
r.run();
97
throw new RuntimeException("Exception expected");
98
} catch (Exception e) {
99
if (!expected.isAssignableFrom(e.getClass())) {
100
String msg = "Expected: " + expected + " Got: " + e.getClass();
101
throw new RuntimeException(msg);
102
}
103
System.out.println("PASS: Got " + e);
104
} finally {
105
cleanup();
106
}
107
}
108
109
static void cleanup() {
110
try {
111
if (server != null)
112
server.close();
113
if (client != null)
114
client.close();
115
if (accept1 != null)
116
accept1.close();
117
} catch (IOException e) {}
118
}
119
120
static void assertClientAddress(SocketAddress a) {
121
assertAddress(a, cAddr, "client");
122
}
123
124
static void assertServerAddress(SocketAddress a) {
125
assertAddress(a, sAddr, "server");
126
}
127
128
static void assertAddress(SocketAddress a, UnixDomainSocketAddress a1, String s) {
129
if (!(a instanceof UnixDomainSocketAddress)) {
130
throw new RuntimeException("wrong address type");
131
}
132
UnixDomainSocketAddress ua = (UnixDomainSocketAddress)a;
133
if (!a.equals(a1))
134
throw new RuntimeException("this is not the " + s + " address");
135
}
136
137
static void assertEquals(Object a, Object b) {
138
if (!a.equals(b))
139
throw new RuntimeException("identity check failed");
140
}
141
142
public static void runTests() throws IOException {
143
checkNormal(() -> {
144
client = SocketChannel.open(StandardProtocolFamily.UNIX);
145
client.bind(cAddr);
146
});
147
checkNormal(() -> {
148
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
149
server.bind(sAddr);
150
});
151
// Repeat first two to make sure they are repeatable
152
checkNormal(() -> {
153
client = SocketChannel.open(StandardProtocolFamily.UNIX);
154
client.bind(cAddr);
155
});
156
checkNormal(() -> {
157
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
158
server.bind(sAddr);
159
});
160
// address with space should work
161
checkNormal(() -> {
162
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
163
UnixDomainSocketAddress usa = UnixDomainSocketAddress.of("with space"); // relative to CWD
164
Files.deleteIfExists(usa.getPath());
165
server.bind(usa);
166
client = SocketChannel.open(usa);
167
Files.delete(usa.getPath());
168
assertAddress(client.getRemoteAddress(), usa, "address");
169
});
170
// client bind to null: allowed
171
checkNormal(() -> {
172
client = SocketChannel.open(StandardProtocolFamily.UNIX);
173
client.bind(null);
174
SocketAddress a = client.getLocalAddress();
175
assertAddress(a, nullAddr, "null address");
176
assertEquals(a, UNNAMED);
177
});
178
// client bind to UNNAMED: allowed
179
checkNormal(() -> {
180
client = SocketChannel.open(StandardProtocolFamily.UNIX);
181
client.bind(UNNAMED);
182
SocketAddress a = client.getLocalAddress();
183
assertAddress(a, nullAddr, "null address");
184
assertEquals(a, UNNAMED);
185
});
186
// server bind to null: should bind to a local address
187
checkNormal(() -> {
188
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
189
server.bind(null);
190
UnixDomainSocketAddress usa = (UnixDomainSocketAddress)server.getLocalAddress();
191
if (usa.getPath().toString().isEmpty())
192
throw new RuntimeException("expected non zero address length");
193
System.out.println("Null server address: " + server.getLocalAddress());
194
});
195
// server no bind : not allowed
196
checkException(
197
NotYetBoundException.class, () -> {
198
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
199
server.accept();
200
}
201
);
202
203
// client implicit bind and connect
204
checkNormal(() -> {
205
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
206
client = SocketChannel.open(StandardProtocolFamily.UNIX);
207
server.bind(sAddr);
208
client.connect(sAddr);
209
SocketAddress cAddr = client.getLocalAddress();
210
assertAddress(cAddr, nullAddr, "null address");
211
assertEquals(cAddr, UNNAMED);
212
assertServerAddress(server.getLocalAddress());
213
});
214
// client null bind and connect (check all addresses)
215
checkNormal(() -> {
216
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
217
client = SocketChannel.open(StandardProtocolFamily.UNIX);
218
server.bind(sAddr);
219
client.bind(null);
220
client.connect(sAddr);
221
assertAddress(client.getLocalAddress(), UNNAMED, "unnamed address");
222
assertServerAddress(server.getLocalAddress());
223
});
224
// client explicit bind and connect (check all addresses)
225
checkNormal(() -> {
226
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
227
client = SocketChannel.open(StandardProtocolFamily.UNIX);
228
server.bind(sAddr);
229
client.bind(cAddr);
230
client.connect(sAddr);
231
accept1 = server.accept();
232
assertClientAddress(client.getLocalAddress());
233
assertServerAddress(server.getLocalAddress());
234
assertAddress(client.getRemoteAddress(), sAddr, "client's remote server address");
235
assertAddress(accept1.getLocalAddress(), sAddr, "accepted local address (server)");
236
assertAddress(accept1.getRemoteAddress(), cAddr, "accepted remote address (client)");
237
});
238
// server multiple bind : not allowed
239
checkException(
240
AlreadyBoundException.class, () -> {
241
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
242
server.bind(sAddr);
243
server.bind(sAddr);
244
}
245
);
246
// client multiple bind : not allowed
247
checkException(
248
AlreadyBoundException.class, () -> {
249
client = SocketChannel.open(StandardProtocolFamily.UNIX);
250
client.bind(cAddr);
251
client.bind(cAddr);
252
}
253
);
254
// client multiple bind to different addresses: not allowed
255
checkException(
256
AlreadyBoundException.class, () -> {
257
client = SocketChannel.open(StandardProtocolFamily.UNIX);
258
client.bind(cAddr);
259
client.bind(sAddr);
260
}
261
);
262
// client multiple bind to differnt addresses, incl null: not allowed
263
checkException(
264
AlreadyBoundException.class, () -> {
265
client = SocketChannel.open(StandardProtocolFamily.UNIX);
266
client.bind(null);
267
client.bind(cAddr);
268
}
269
);
270
271
// server bind to existing name: not allowed
272
273
checkException(
274
BindException.class, () -> {
275
var path = Files.createFile(Path.of("moo.sock"));
276
var addr = UnixDomainSocketAddress.of(path);
277
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
278
try {
279
server.bind(addr);
280
} finally {
281
Files.deleteIfExists(path);
282
}
283
}
284
);
285
286
287
// client bind to existing name: not allowed
288
checkException(
289
BindException.class, () -> {
290
var path = Path.of("temp.sock");
291
Files.deleteIfExists(path);
292
Files.createFile(path);
293
var addr = UnixDomainSocketAddress.of(path);
294
client = SocketChannel.open(StandardProtocolFamily.UNIX);
295
try {
296
client.bind(addr);
297
} finally {
298
Files.deleteIfExists(path);
299
}
300
}
301
);
302
303
// bind and connect to name of close to max size
304
checkNormal(() -> {
305
int len = 100;
306
char[] chars = new char[len];
307
Arrays.fill(chars, 'x');
308
String name = new String(chars);
309
UnixDomainSocketAddress address = UnixDomainSocketAddress.of(name);
310
ServerSocketChannel server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
311
server.bind(address);
312
SocketChannel client = SocketChannel.open(address);
313
assertAddress(server.getLocalAddress(), address, "server");
314
assertAddress(client.getRemoteAddress(), address, "client");
315
Files.delete(address.getPath());
316
});
317
318
// implicit server bind
319
checkNormal(() -> {
320
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX);
321
server.bind(null);
322
UnixDomainSocketAddress usa = (UnixDomainSocketAddress)server.getLocalAddress();
323
client = SocketChannel.open(usa);
324
accept1 = server.accept();
325
assertAddress(client.getRemoteAddress(), usa, "server");
326
Files.delete(usa.getPath());
327
});
328
}
329
}
330
331