author | michaelm |
Thu, 14 Nov 2019 15:01:49 +0000 | |
branch | unixdomainchannels |
changeset 59082 | 5e250ee9259e |
parent 59078 | 4e648a2d8480 |
permissions | -rw-r--r-- |
59078 | 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 8231358 |
|
27 |
* @run main Bind |
|
28 |
* @summary Bind test |
|
29 |
*/ |
|
30 |
||
31 |
import java.io.Closeable; |
|
32 |
import java.io.IOException; |
|
33 |
import java.net.*; |
|
34 |
import java.nio.ByteBuffer; |
|
35 |
import java.nio.channels.*; |
|
36 |
import java.nio.file.Files; |
|
37 |
import java.nio.file.Path; |
|
38 |
import java.util.Set; |
|
39 |
||
40 |
/** |
|
41 |
* Check that all bind variations work |
|
42 |
*/ |
|
43 |
public class Bind { |
|
44 |
||
45 |
static Path spath, cpath; |
|
46 |
||
47 |
static UnixDomainSocketAddress sAddr, cAddr, nullAddr; |
|
48 |
static ServerSocketChannel server; |
|
49 |
static SocketChannel client, accept1; |
|
50 |
||
51 |
public static void main(String args[]) throws Exception { |
|
52 |
if (!supported()) { |
|
53 |
System.out.println("Unix domain channels not supported"); |
|
54 |
return; |
|
55 |
} |
|
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
56 |
spath = Path.of("server.sock"); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
57 |
cpath = Path.of("client.sock"); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
58 |
sAddr = new UnixDomainSocketAddress(spath); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
59 |
cAddr = new UnixDomainSocketAddress(cpath); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
60 |
nullAddr = new UnixDomainSocketAddress(""); |
59078 | 61 |
runTests(); |
62 |
} |
|
63 |
||
64 |
static boolean supported() { |
|
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
65 |
try { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
66 |
SocketChannel.open(StandardProtocolFamily.UNIX); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
67 |
} catch (UnsupportedAddressTypeException e) { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
68 |
return false; |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
69 |
} catch (Exception e) { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
70 |
return true; // continue test to see what problem is |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
71 |
} |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
72 |
return true; |
59078 | 73 |
} |
74 |
||
75 |
static interface ThrowingRunnable { |
|
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
76 |
public void run() throws Exception; |
59078 | 77 |
} |
78 |
||
79 |
static void init() throws IOException { |
|
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
80 |
Files.deleteIfExists(cpath); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
81 |
Files.deleteIfExists(spath); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
82 |
client = null; server = null; accept1 = null; |
59078 | 83 |
} |
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
84 |
|
59078 | 85 |
static void checkNormal(ThrowingRunnable r) { |
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
86 |
try { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
87 |
init(); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
88 |
r.run(); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
89 |
System.out.println("PASS:"); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
90 |
} catch (Exception e) { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
91 |
throw new RuntimeException(e); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
92 |
} finally { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
93 |
cleanup(); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
94 |
} |
59078 | 95 |
} |
96 |
||
97 |
static void checkException(Class<? extends Exception> expected, ThrowingRunnable r) { |
|
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
98 |
try { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
99 |
init(); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
100 |
r.run(); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
101 |
throw new RuntimeException("Exception expected"); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
102 |
} catch (Exception e) { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
103 |
if (!expected.isAssignableFrom(e.getClass())) { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
104 |
String msg = "Expected: " + expected + " Got: " + e.getClass(); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
105 |
throw new RuntimeException(msg); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
106 |
} |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
107 |
System.out.println("PASS: Got " + e); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
108 |
} finally { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
109 |
cleanup(); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
110 |
} |
59078 | 111 |
} |
112 |
||
113 |
static void cleanup() { |
|
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
114 |
try { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
115 |
if (server != null) |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
116 |
server.close(); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
117 |
if (client != null) |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
118 |
client.close(); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
119 |
if (accept1 != null) |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
120 |
accept1.close(); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
121 |
} catch (IOException e) {} |
59078 | 122 |
} |
123 |
||
124 |
static void assertClientAddress(SocketAddress a) { |
|
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
125 |
assertAddress(a, cAddr, "client"); |
59078 | 126 |
} |
127 |
||
128 |
static void assertServerAddress(SocketAddress a) { |
|
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
129 |
assertAddress(a, sAddr, "server"); |
59078 | 130 |
} |
131 |
||
132 |
static void assertAddress(SocketAddress a, UnixDomainSocketAddress a1, String s) { |
|
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
133 |
if (!(a instanceof UnixDomainSocketAddress)) { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
134 |
System.err.println("adddr = " + a); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
135 |
throw new RuntimeException("wrong address type"); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
136 |
} |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
137 |
UnixDomainSocketAddress ua = (UnixDomainSocketAddress)a; |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
138 |
if (!a.equals(a1)) |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
139 |
throw new RuntimeException("this is not the " + s + " address"); |
59078 | 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 |
}); |
|
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
151 |
// Repeat first two to make sure they are repeatable |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
152 |
checkNormal(() -> { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
153 |
client = SocketChannel.open(StandardProtocolFamily.UNIX); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
154 |
client.bind(cAddr); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
155 |
}); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
156 |
checkNormal(() -> { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
157 |
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
158 |
server.bind(sAddr); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
159 |
}); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
160 |
// client bind to null: allowed |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
161 |
checkNormal(() -> { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
162 |
client = SocketChannel.open(StandardProtocolFamily.UNIX); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
163 |
client.bind(null); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
164 |
SocketAddress a = client.getLocalAddress(); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
165 |
assertAddress(client.getLocalAddress(), nullAddr, "null address"); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
166 |
}); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
167 |
// server bind to null: not allowed |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
168 |
checkException( |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
169 |
BindException.class, () -> { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
170 |
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
171 |
server.bind(null); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
172 |
} |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
173 |
); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
174 |
// server no bind : not allowed |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
175 |
checkException( |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
176 |
NotYetBoundException.class, () -> { |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
177 |
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
178 |
server.accept(); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
179 |
} |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
180 |
); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
181 |
// client implicit bind and connect |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
182 |
checkNormal(() -> { |
59078 | 183 |
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX); |
184 |
client = SocketChannel.open(StandardProtocolFamily.UNIX); |
|
185 |
server.bind(sAddr); |
|
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
186 |
client.connect(sAddr); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
187 |
assertAddress(client.getLocalAddress(), nullAddr, "null address"); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
188 |
assertServerAddress(server.getLocalAddress()); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
189 |
}); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
190 |
// client null bind and connect (check all addresses) |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
191 |
checkNormal(() -> { |
59078 | 192 |
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX); |
193 |
client = SocketChannel.open(StandardProtocolFamily.UNIX); |
|
194 |
server.bind(sAddr); |
|
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
195 |
client.bind(null); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
196 |
client.connect(sAddr); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
197 |
assertAddress(client.getLocalAddress(), nullAddr, "null address"); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
198 |
assertServerAddress(server.getLocalAddress()); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
199 |
}); |
59078 | 200 |
// client explicit bind and connect (check all addresses) |
201 |
checkNormal(() -> { |
|
202 |
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX); |
|
203 |
client = SocketChannel.open(StandardProtocolFamily.UNIX); |
|
204 |
server.bind(sAddr); |
|
205 |
client.bind(cAddr); |
|
206 |
client.connect(sAddr); |
|
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
207 |
accept1 = server.accept(); |
59078 | 208 |
assertClientAddress(client.getLocalAddress()); |
209 |
assertServerAddress(server.getLocalAddress()); |
|
59082
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
210 |
assertAddress(client.getRemoteAddress(), sAddr, "client's remote server address"); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
211 |
assertAddress(accept1.getLocalAddress(), sAddr, "accepted local address (server)"); |
5e250ee9259e
unixdomainchannels: fix some sockaddr_un address handling
michaelm
parents:
59078
diff
changeset
|
212 |
assertAddress(accept1.getRemoteAddress(), cAddr, "accepted remote address (client)"); |
59078 | 213 |
}); |
214 |
// server multiple bind : not allowed |
|
215 |
checkException( |
|
216 |
AlreadyBoundException.class, () -> { |
|
217 |
server = ServerSocketChannel.open(StandardProtocolFamily.UNIX); |
|
218 |
server.bind(sAddr); |
|
219 |
server.bind(sAddr); |
|
220 |
} |
|
221 |
); |
|
222 |
// client multiple bind : not allowed |
|
223 |
checkException( |
|
224 |
AlreadyBoundException.class, () -> { |
|
225 |
client = SocketChannel.open(StandardProtocolFamily.UNIX); |
|
226 |
client.bind(cAddr); |
|
227 |
client.bind(cAddr); |
|
228 |
} |
|
229 |
); |
|
230 |
} |
|
231 |
} |