|
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 import java.nio.channels.*; |
|
25 import java.nio.ByteBuffer; |
|
26 import java.io.IOException; |
|
27 import static java.nio.charset.StandardCharsets.ISO_8859_1; |
|
28 |
|
29 /* |
|
30 * Make sure that System.inheritedChannel returns null when given a UNIX domain socket |
|
31 */ |
|
32 |
|
33 public class UnixDomainChannelTest { |
|
34 |
|
35 public static class Child { |
|
36 public static void main(String[] args) throws Exception { |
|
37 // we just want to make sure that System.inheritedChannel either |
|
38 // returns a connected channel, or null if it is given a listener |
|
39 Channel channel = System.inheritedChannel(); |
|
40 String result = channel == null ? "N" : "Y"; |
|
41 if (args[0].equals("test1") || args[0].equals("test2")) { |
|
42 // socket is writeable |
|
43 ByteChannel bc = (ByteChannel)channel; |
|
44 ByteBuffer buf = ByteBuffer.wrap(result.getBytes(ISO_8859_1)); |
|
45 bc.write(buf); |
|
46 } else { // test3 |
|
47 // in this case the socket is a listener |
|
48 // we can't write to it. So, use UnixDatagramSocket |
|
49 // to accept a writeable socket |
|
50 UnixDomainSocket listener = new UnixDomainSocket(0); // fd 0 |
|
51 UnixDomainSocket sock = listener.accept(); |
|
52 sock.write((int)result.charAt(0)); |
|
53 } |
|
54 } |
|
55 } |
|
56 |
|
57 static boolean passed = true; |
|
58 |
|
59 public static void main(String args[]) throws Exception { |
|
60 test1(); |
|
61 test2(); |
|
62 test3(); |
|
63 if (!passed) |
|
64 throw new RuntimeException(); |
|
65 } |
|
66 |
|
67 private static void closeAll(UnixDomainSocket... sockets) { |
|
68 for (UnixDomainSocket sock : sockets) { |
|
69 sock.close(); |
|
70 } |
|
71 } |
|
72 |
|
73 // Test with a named connected socket |
|
74 private static void test1() throws Exception { |
|
75 UnixDomainSocket listener = new UnixDomainSocket(); |
|
76 listener.bind("foo.socket"); |
|
77 UnixDomainSocket sock1 = new UnixDomainSocket(); |
|
78 sock1.connect("foo.socket"); |
|
79 UnixDomainSocket sock2 = listener.accept(); |
|
80 |
|
81 Launcher.launchWithUnixDomainSocket("UnixDomainChannelTest$Child", sock2, "test1"); |
|
82 int c = sock1.read(); |
|
83 if (c != 'Y') { |
|
84 System.err.printf("test1: failed %d d\n", c ); |
|
85 passed = false; |
|
86 } |
|
87 closeAll(listener, sock1, sock2); |
|
88 } |
|
89 |
|
90 // Test with unnamed socketpair |
|
91 private static void test2() throws Exception { |
|
92 UnixDomainSocket[] pair = UnixDomainSocket.socketpair(); |
|
93 System.out.println("test2: launching child"); |
|
94 Launcher.launchWithUnixDomainSocket("UnixDomainChannelTest$Child", pair[0], "test2"); |
|
95 if (pair[1].read() != 'Y') { |
|
96 System.err.println("test2: failed"); |
|
97 passed = false; |
|
98 } |
|
99 closeAll(pair[0], pair[1]); |
|
100 } |
|
101 |
|
102 // Test with a named listener |
|
103 private static void test3() throws Exception { |
|
104 UnixDomainSocket listener = new UnixDomainSocket(); |
|
105 listener.bind("foo.socket"); |
|
106 UnixDomainSocket sock1 = new UnixDomainSocket(); |
|
107 System.out.println("test3: launching child"); |
|
108 Launcher.launchWithUnixDomainSocket("UnixDomainChannelTest$Child", listener, "test3"); |
|
109 sock1.connect("foo.socket"); |
|
110 if (sock1.read() != 'N') { |
|
111 System.err.println("test3: failed"); |
|
112 passed = false; |
|
113 } |
|
114 closeAll(listener, sock1); |
|
115 } |
|
116 |
|
117 } |