Debian Codegraph @

junixsocket-rmi/src/test/java/org/newsclub/net/unix/rmi/RemoteFileDescriptorTest.java

raw
1 /*
2 * junixsocket
3 *
4 * Copyright 2009-2022 Christian Kohlschütter
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.newsclub.net.unix.rmi;
19
20 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertNotNull;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.io.FileInputStream;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.lang.reflect.Proxy;
29 import java.nio.charset.StandardCharsets;
30 import java.rmi.NotBoundException;
31 import java.rmi.server.RMISocketFactory;
32
33 import org.junit.jupiter.api.Test;
34 import org.newsclub.net.unix.AFSocketCapability;
35
36 import com.kohlschutter.util.IOUtil;
37
38 @AFSocketCapabilityRequirement({
39 AFSocketCapability.CAPABILITY_UNIX_DOMAIN, AFSocketCapability.CAPABILITY_FILE_DESCRIPTORS})
40 public class RemoteFileDescriptorTest extends TestBase {
41 private static final byte[] HELLO_WORLD = "Hello World :-)\n".getBytes(StandardCharsets.US_ASCII);
42 private static final byte[] SMILEY = ":-)\n".getBytes(StandardCharsets.US_ASCII);
43
44 @Test
45 public void testServiceProxy() throws IOException, NotBoundException {
46 TestService svc = lookupTestService();
47 assertTrue(Proxy.isProxyClass(svc.getClass()));
48 }
49
50 @Test
51 public void testRemoteStdout() throws IOException, NotBoundException {
52 TestService svc = lookupTestService();
53
54 try (RemoteFileDescriptor stdout = svc.stdout()) {
55 try (FileOutputStream fos = new FileOutputStream(stdout.getFileDescriptor())) {
56 // fos.write(SMILEY); // uncomment to write a smiley to stdout
57 fos.flush();
58 }
59 }
60 }
61
62 @Test
63 public void testRemoteStdoutNoop() throws IOException, NotBoundException {
64 TestService svc = lookupTestService();
65
66 try (RemoteFileDescriptor stdout = svc.stdout()) {
67 // not doing anything here should trigger descriptor cleanup in RemoteFileDescriptor#close
68 }
69 }
70
71 @Test
72 public void testWriteAndReadHello() throws IOException, NotBoundException {
73 TestService svc = lookupTestService();
74
75 try (FileOutputStream fos = svc.output().asFileOutputStream()) {
76 fos.write(HELLO_WORLD);
77 }
78 svc.verifyContents(HELLO_WORLD);
79
80 try (FileInputStream fin = svc.input(12).asFileInputStream()) {
81 byte[] data = IOUtil.readAllBytes(fin);
82 assertArrayEquals(SMILEY, data);
83 }
84
85 try (NaiveFileInputStreamRemote rfis = svc.naiveInputStreamRemote();
86 FileInputStream fin = rfis.getRemoteFileDescriptor().asFileInputStream()) {
87 assertEquals('H', rfis.read());
88 assertEquals('e', fin.read());
89 assertEquals('l', fin.read());
90 assertEquals('l', fin.read());
91 fin.close(); // it's OK to close the remote file descriptor we received via RMI
92 assertEquals('o', rfis.read());
93 }
94 }
95
96 @Test
97 public void testFindSocketFactory() throws IOException, NotBoundException {
98 TestService svc = lookupTestService();
99
100 RemotePeerInfo rci = RemotePeerInfo.getConnectionInfo(svc);
101 RMISocketFactory factory = rci.getSocketFactory();
102 assertNotNull(factory);
103 assertEquals(namingSocketFactory(), factory);
104 }
105
106 @Test
107 public void testReadWrite() throws IOException, NotBoundException {
108 TestService svc = lookupTestService();
109
110 byte[] expected = new byte[5000];
111 for (int i = 0; i < expected.length; i++) {
112 expected[i] = (byte) ((i + 123) % 256);
113 }
114
115 try (FileOutputStream fos = svc.output().asFileOutputStream()) {
116 fos.write(expected);
117 }
118
119 byte[] actual;
120 try (FileInputStream fin = svc.input().asFileInputStream()) {
121 actual = IOUtil.readAllBytes(fin);
122 }
123 assertArrayEquals(expected, actual);
124
125 try (NaiveFileInputStreamRemote fin = svc.naiveInputStreamRemote()) {
126 actual = fin.readAllBytes();
127 }
128 assertArrayEquals(expected, actual);
129 }
130 }