1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.unix.tests;
17
18 import io.netty.channel.unix.Buffer;
19 import io.netty.channel.unix.DomainSocketAddress;
20 import io.netty.channel.unix.Errors;
21 import io.netty.channel.unix.Errors.NativeIoException;
22 import io.netty.channel.unix.Limits;
23 import io.netty.channel.unix.Socket;
24 import io.netty.channel.unix.UnixChannelUtil;
25 import io.netty.util.internal.CleanableDirectBuffer;
26 import io.netty.util.internal.PlatformDependent;
27 import org.junit.jupiter.api.AfterEach;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.opentest4j.TestAbortedException;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.nio.ByteBuffer;
35 import java.nio.charset.StandardCharsets;
36 import java.nio.file.Files;
37 import java.util.Arrays;
38
39 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
40 import static org.junit.jupiter.api.Assertions.assertEquals;
41 import static org.junit.jupiter.api.Assertions.assertFalse;
42 import static org.junit.jupiter.api.Assertions.assertNotEquals;
43 import static org.junit.jupiter.api.Assertions.assertThrows;
44 import static org.junit.jupiter.api.Assertions.assertTrue;
45 import static org.junit.jupiter.api.Assumptions.assumeTrue;
46
47 public abstract class SocketTest<T extends Socket> {
48 protected T socket;
49
50 protected abstract T newSocket();
51
52 @BeforeEach
53 public void setup() {
54 socket = newSocket();
55 }
56
57 @AfterEach
58 public void tearDown() throws IOException {
59 socket.close();
60 }
61
62 @Test
63 public void testKeepAlive() throws Exception {
64 assertFalse(socket.isKeepAlive());
65 socket.setKeepAlive(true);
66 assertTrue(socket.isKeepAlive());
67 }
68
69 @Test
70 public void testTcpNoDelay() throws Exception {
71 assertFalse(socket.isTcpNoDelay());
72 socket.setTcpNoDelay(true);
73 assertTrue(socket.isTcpNoDelay());
74 }
75
76 @Test
77 public void testReceivedBufferSize() throws Exception {
78 int size = socket.getReceiveBufferSize();
79 int newSize = 65535;
80 assertTrue(size > 0);
81 socket.setReceiveBufferSize(newSize);
82
83 assertTrue(newSize <= socket.getReceiveBufferSize());
84 }
85
86 @Test
87 public void testSendBufferSize() throws Exception {
88 int size = socket.getSendBufferSize();
89 int newSize = 65535;
90 assertTrue(size > 0);
91 socket.setSendBufferSize(newSize);
92
93 assertTrue(newSize <= socket.getSendBufferSize());
94 }
95
96 @Test
97 public void testSoLinger() throws Exception {
98 assertEquals(-1, socket.getSoLinger());
99 socket.setSoLinger(10);
100 assertEquals(10, socket.getSoLinger());
101 }
102
103 @Test
104 public void testDoubleCloseDoesNotThrow() throws IOException {
105 Socket socket = Socket.newSocketStream();
106 socket.close();
107 socket.close();
108 }
109
110 @Test
111 public void testTrafficClass() throws IOException {
112
113 final int value = 0x08;
114 socket.setTrafficClass(value);
115 assertEquals(value, socket.getTrafficClass());
116 }
117
118 @Test
119 public void testIntOpt() throws IOException {
120 socket.setReuseAddress(false);
121 socket.setIntOpt(level(), optname(), 1);
122
123 assertNotEquals(0, socket.getIntOpt(level(), optname()));
124 socket.setIntOpt(level(), optname(), 0);
125
126 assertEquals(0, socket.getIntOpt(level(), optname()));
127 }
128
129 @Test
130 public void testRawOpt() throws IOException {
131 CleanableDirectBuffer cleanableDirectBuffer = Buffer.allocateDirectBufferWithNativeOrder(4);
132 ByteBuffer buffer = cleanableDirectBuffer.buffer();
133 buffer.putInt(1).flip();
134 socket.setRawOpt(level(), optname(), buffer);
135
136 ByteBuffer out = ByteBuffer.allocate(4);
137 socket.getRawOpt(level(), optname(), out);
138 assertFalse(out.hasRemaining());
139
140 out.flip();
141 assertNotEquals(ByteBuffer.allocate(0), out);
142 cleanableDirectBuffer.clean();
143 }
144
145 @Test
146 public void testSendToDomainSocketWithTooLongPath() throws IOException {
147 byte[] path = new byte[4096];
148 Arrays.fill(path, (byte) 'a');
149 path[0] = '/';
150
151 CleanableDirectBuffer cleanableDirectBuffer = Buffer.allocateDirectBufferWithNativeOrder(1);
152 Socket domainSocket = Socket.newSocketDomainDgram();
153 try {
154 ByteBuffer buffer = cleanableDirectBuffer.buffer();
155
156 NativeIoException e = assertThrows(NativeIoException.class,
157 () -> domainSocket.sendToDomainSocket(buffer, 0, 1, path));
158 assertEquals(Errors.ERRNO_ENAMETOOLONG_NEGATIVE, e.expectedErr());
159 } finally {
160 domainSocket.close();
161 cleanableDirectBuffer.clean();
162 }
163 }
164
165 protected int level() {
166 throw new TestAbortedException("Not supported");
167 }
168
169 protected int optname() {
170 throw new TestAbortedException("Not supported");
171 }
172
173 @Test
174 public void testBindDomainSocketDoesNotRemoveRegularFile() throws Exception {
175 File file = PlatformDependent.createTempFile("NETTY", "UDS", null);
176 try {
177 byte[] content = "do not delete".getBytes(StandardCharsets.US_ASCII);
178 Files.write(file.toPath(), content);
179
180 Socket domainSocket = Socket.newSocketDomain();
181 try {
182 assertThrows(IOException.class, () -> domainSocket.bind(new DomainSocketAddress(file)));
183 } finally {
184 domainSocket.close();
185 }
186 assertTrue(file.isFile());
187 assertArrayEquals(content, Files.readAllBytes(file.toPath()));
188 } finally {
189 file.delete();
190 }
191 }
192
193 @Test
194 public void testBindDomainSocketReplacesStaleSocket() throws Exception {
195 DomainSocketAddress address = UnixTestUtils.newDomainSocketAddress();
196 File file = new File(address.path());
197 try {
198 Socket first = Socket.newSocketDomain();
199 try {
200 first.bind(address);
201 } finally {
202 first.close();
203 }
204
205 assertTrue(file.exists());
206
207 Socket second = Socket.newSocketDomain();
208 try {
209 second.bind(address);
210 } finally {
211 second.close();
212 }
213 } finally {
214 file.delete();
215 }
216 }
217
218 @Test
219 public void testDeleteDomainSocketFileOnlyDeletesSockets() throws Exception {
220 File regular = PlatformDependent.createTempFile("NETTY", "UDS", null);
221 DomainSocketAddress address = UnixTestUtils.newDomainSocketAddress();
222 File socketFile = new File(address.path());
223 try {
224 assertFalse(UnixChannelUtil.deleteDomainSocketFile(regular.getPath()));
225 assertTrue(regular.exists());
226
227 Socket domainSocket = Socket.newSocketDomain();
228 try {
229 domainSocket.bind(address);
230 } finally {
231 domainSocket.close();
232 }
233 assertTrue(UnixChannelUtil.deleteDomainSocketFile(address.path()));
234 assertFalse(socketFile.exists());
235
236 assertFalse(UnixChannelUtil.deleteDomainSocketFile(address.path()));
237 } finally {
238 regular.delete();
239 socketFile.delete();
240 }
241 }
242
243 @Test
244 public void testBindDomainSocketWithTrailingSlashDoesNotRemoveSocket() throws Exception {
245 DomainSocketAddress address = UnixTestUtils.newDomainSocketAddress();
246 File file = new File(address.path());
247 try {
248 Socket first = Socket.newSocketDomain();
249 try {
250 first.bind(address);
251 } finally {
252 first.close();
253 }
254 assertTrue(file.exists());
255
256
257 Socket second = Socket.newSocketDomain();
258 try {
259 assertThrows(IOException.class, () -> second.bind(new DomainSocketAddress(address.path() + '/')));
260 } finally {
261 second.close();
262 }
263 assertTrue(file.exists());
264 } finally {
265 file.delete();
266 }
267 }
268
269 @Test
270 public void testBindDomainSocketDoesNotRemoveFifo() throws Exception {
271 DomainSocketAddress address = UnixTestUtils.newDomainSocketAddress();
272 File fifo = new File(address.path());
273 try {
274 mkfifo(fifo);
275
276 Socket domainSocket = Socket.newSocketDomain();
277 try {
278 assertThrows(IOException.class, () -> domainSocket.bind(address));
279 } finally {
280 domainSocket.close();
281 }
282 assertTrue(fifo.exists());
283 } finally {
284 fifo.delete();
285 }
286 }
287
288 @Test
289 public void testDeleteDomainSocketFileDoesNotDeleteFifo() throws Exception {
290 DomainSocketAddress address = UnixTestUtils.newDomainSocketAddress();
291 File fifo = new File(address.path());
292 try {
293 mkfifo(fifo);
294
295 assertFalse(UnixChannelUtil.deleteDomainSocketFile(fifo.getPath()));
296 assertTrue(fifo.exists());
297 } finally {
298 fifo.delete();
299 }
300 }
301
302 @Test
303 public void testBindAbstractDomainSocket() throws Exception {
304
305 assumeTrue("linux".equals(PlatformDependent.normalizedOs()));
306
307 DomainSocketAddress address = new DomainSocketAddress("\0" + UnixTestUtils.newDomainSocketAddress().path());
308 Socket domainSocket = Socket.newSocketDomain();
309 try {
310 domainSocket.bind(address);
311 } finally {
312 domainSocket.close();
313 }
314 }
315
316 private static void mkfifo(File file) throws Exception {
317 int exitCode;
318 try {
319 exitCode = new ProcessBuilder("mkfifo", file.getPath()).inheritIO().start().waitFor();
320 } catch (IOException e) {
321 throw new TestAbortedException("mkfifo not available", e);
322 }
323 if (exitCode != 0) {
324 throw new TestAbortedException("mkfifo failed with exit code " + exitCode);
325 }
326 }
327
328 @Test
329 public void testConnectDomainSocketWithMaxLengthPath() throws Exception {
330 int max = Limits.UDS_SUN_PATH_SIZE;
331 assertTrue(max >= 100);
332
333 for (int i = 0; i < 100; i++) {
334 assertFalse(isConnectPathTooLong(max));
335 }
336 assertTrue(isConnectPathTooLong(max + 1));
337 }
338
339 private static boolean isConnectPathTooLong(int length) throws IOException {
340 StringBuilder path = new StringBuilder("/");
341 while (path.length() < length) {
342 path.append('a');
343 }
344 Socket domainSocket = Socket.newSocketDomain();
345 try {
346 domainSocket.connect(new DomainSocketAddress(path.toString()));
347 return false;
348 } catch (IOException e) {
349 return e.getMessage() != null && e.getMessage().contains("too long");
350 } finally {
351 domainSocket.close();
352 }
353 }
354 }