| 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.assertEquals; |
| 21 |
|
| 22 |
import java.io.Closeable; |
| 23 |
import java.io.File; |
| 24 |
import java.io.FileDescriptor; |
| 25 |
import java.io.FileInputStream; |
| 26 |
import java.io.FileOutputStream; |
| 27 |
import java.io.IOException; |
| 28 |
import java.nio.file.Files; |
| 29 |
import java.util.Arrays; |
| 30 |
|
| 31 |
import org.newsclub.net.unix.AFUNIXSocketCredentials; |
| 32 |
import org.newsclub.net.unix.rmi.RemoteCloseableThing.IsCloseable; |
| 33 |
import org.newsclub.net.unix.rmi.RemoteCloseableThing.NotCloseable; |
| 34 |
|
| 35 |
import com.kohlschutter.annotations.compiletime.SuppressFBWarnings; |
| 36 |
import com.kohlschutter.util.IOUtil; |
| 37 |
|
| 38 |
/** |
| 39 |
* The implementation for the test service. |
| 40 |
* |
| 41 |
* @author Christian Kohlschütter |
| 42 |
*/ |
| 43 |
public class TestServiceImpl implements TestService, Closeable { |
| 44 |
private final File tmpFile; |
| 45 |
private final AFUNIXRMISocketFactory socketFactory; |
| 46 |
private final RemoteCloseableThingImpl.NotCloseableImpl testNotCloseableImpl = |
| 47 |
new RemoteCloseableThingImpl.NotCloseableImpl(); |
| 48 |
private final RemoteCloseableThingImpl.IsCloseableImpl testCloseableImpl = |
| 49 |
new RemoteCloseableThingImpl.IsCloseableImpl(); |
| 50 |
|
| 51 |
@SuppressFBWarnings("EI_EXPOSE_REP2") |
| 52 |
public TestServiceImpl(AFUNIXRMISocketFactory socketFactory) throws IOException { |
| 53 |
this.socketFactory = socketFactory; |
| 54 |
this.tmpFile = File.createTempFile("FDTestService", ".tmp"); |
| 55 |
tmpFile.deleteOnExit(); |
| 56 |
|
| 57 |
AFNaming.exportObject(this, socketFactory); |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public RemoteFileDescriptor stdout() throws IOException { |
| 62 |
return new RemoteFileDescriptor(socketFactory, FileDescriptor.out); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public RemoteFileInput input() throws IOException { |
| 67 |
return new RemoteFileInput(socketFactory, new FileInputStream(tmpFile)); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
@SuppressWarnings("resource") |
| 72 |
public RemoteFileInput input(long skipBytes) throws IOException { |
| 73 |
FileInputStream fin = new FileInputStream(tmpFile); |
| 74 |
try { |
| 75 |
|
| 76 |
long skipped; |
| 77 |
while (skipBytes > 0 && (skipped = fin.skip(skipBytes)) >= 0) { |
| 78 |
skipBytes -= skipped; |
| 79 |
} |
| 80 |
|
| 81 |
return new RemoteFileInput(socketFactory, fin); |
| 82 |
} catch (IOException e) { |
| 83 |
fin.close(); |
| 84 |
throw e; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public RemoteFileOutput output() throws IOException { |
| 90 |
return new RemoteFileOutput(socketFactory, new FileOutputStream(tmpFile)); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public void verifyContents(byte[] expectedData) throws IOException { |
| 95 |
try (FileInputStream fin = new FileInputStream(tmpFile)) { |
| 96 |
byte[] bytes = IOUtil.readAllBytes(fin); |
| 97 |
if (!Arrays.equals(bytes, expectedData)) { |
| 98 |
throw new IOException("Unexpected bytes"); |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
@Override |
| 104 |
public void verifyContents(int numBytes, byte[] expectedData) throws IOException { |
| 105 |
try (FileInputStream fin = new FileInputStream(tmpFile)) { |
| 106 |
byte[] bytes = new byte[numBytes]; |
| 107 |
int numRead = fin.read(bytes); |
| 108 |
if (numRead != bytes.length || !Arrays.equals(bytes, expectedData)) { |
| 109 |
throw new IOException("Unexpected bytes"); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
@Override |
| 115 |
public void deleteFile() throws IOException { |
| 116 |
Files.deleteIfExists(tmpFile.toPath()); |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
public void touchFile() throws IOException { |
| 121 |
Files.createFile(tmpFile.toPath()); |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public NaiveFileInputStreamRemote naiveInputStreamRemote() throws IOException { |
| 126 |
return new NaiveFileInputStreamRemoteImpl(socketFactory, tmpFile); |
| 127 |
} |
| 128 |
|
| 129 |
@Override |
| 130 |
public void close() throws IOException { |
| 131 |
AFNaming.unexportObject(this); |
| 132 |
deleteFile(); |
| 133 |
} |
| 134 |
|
| 135 |
@Override |
| 136 |
public AFUNIXSocketCredentials remotePeerCredentials() { |
| 137 |
AFUNIXSocketCredentials creds = AFUNIXSocketCredentials.remotePeerCredentials(); |
| 138 |
AFUNIXSocketCredentials credsRpi = RemotePeerInfo.remotePeerCredentials(); |
| 139 |
assertEquals(creds, credsRpi); |
| 140 |
return creds; |
| 141 |
} |
| 142 |
|
| 143 |
@SuppressWarnings("unchecked") |
| 144 |
@Override |
| 145 |
public <T extends RemoteCloseableThing> RemoteCloseable<? extends T> remoteCloseable( |
| 146 |
Class<T> klass) throws IOException { |
| 147 |
if (klass.equals(NotCloseable.class)) { |
| 148 |
return (RemoteCloseable<? extends T>) new RemoteCloseableImpl<RemoteCloseableThing.NotCloseable>( |
| 149 |
socketFactory, testNotCloseableImpl); |
| 150 |
} else if (klass.equals(IsCloseable.class)) { |
| 151 |
return (RemoteCloseable<? extends T>) new RemoteCloseableImpl<RemoteCloseableThing.IsCloseable>( |
| 152 |
socketFactory, testCloseableImpl); |
| 153 |
} else { |
| 154 |
throw new UnsupportedOperationException(); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
@Override |
| 159 |
public <T extends RemoteCloseableThing> void remoteCloseableThingResetNumberOfCloseCalls( |
| 160 |
Class<T> klass) throws IOException { |
| 161 |
if (klass.equals(NotCloseable.class)) { |
| 162 |
testNotCloseableImpl.resetCount(); |
| 163 |
} else if (klass.equals(IsCloseable.class)) { |
| 164 |
testCloseableImpl.resetCount(); |
| 165 |
} else { |
| 166 |
throw new UnsupportedOperationException(); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public <T extends RemoteCloseableThing> int remoteCloseableThingNumberOfCloseCalls(Class<T> klass) |
| 172 |
throws IOException { |
| 173 |
if (klass.equals(NotCloseable.class)) { |
| 174 |
return testNotCloseableImpl.numberOfCloseCalls(); |
| 175 |
} else if (klass.equals(IsCloseable.class)) { |
| 176 |
return testCloseableImpl.numberOfCloseCalls(); |
| 177 |
} else { |
| 178 |
throw new UnsupportedOperationException(); |
| 179 |
} |
| 180 |
} |
| 181 |
} |