Skip to content

Commit

Permalink
More code cleanup, thanks to infer
Browse files Browse the repository at this point in the history
  • Loading branch information
kohlschuetter committed Jul 27, 2023
1 parent ded9326 commit f96ce5c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ protected String checkKnownBugAcceptTimeout(SocketAddress serverAddr) {
@Test
public void testAcceptWithoutBindToService() throws Exception {
ServerSocket ss = newServerSocket();
assertThrows(SocketException.class, () -> ss.accept());
assertThrows(SocketException.class, () -> {
try (Socket s = ss.accept()) {
fail("Should not be reached");
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ private static List<String> lsofUnixSockets(long pid) throws IOException, TestAb
assumeTrue(false, "incompatible lsof binary");
}
}
p.waitFor();
} finally {
p.destroy();
assumeTrue(p.exitValue() == 0, "lsof should terminate with RC=0");
}
assumeTrue(p.waitFor() == 0, "lsof should terminate with RC=0");
return lines;
}

Expand All @@ -90,7 +93,6 @@ protected Object preRunCheck(Process process) throws TestAbortedException, IOExc
protected void postRunCheck(Process process, Object linesBeforeObj) throws TestAbortedException,
IOException, InterruptedException {
assumeTrue(linesBeforeObj != null, "Environment does not support lsof check");
Objects.requireNonNull(linesBeforeObj);

@SuppressWarnings("unchecked")
List<String> linesBefore = (List<String>) linesBeforeObj;
Expand All @@ -102,20 +104,22 @@ protected void postRunCheck(Process process, Object linesBeforeObj) throws TestA
break;
}
linesAfter = lsofUnixSockets(process.pid());
if (linesAfter.size() < linesBefore.size()) {
if (linesBefore == null || linesAfter.size() < linesBefore.size()) {
break;
}
}

assumeTrue(Objects.requireNonNull(linesAfter).size() > 0, "lsof may fail to return anything");

if (linesAfter.size() >= linesBefore.size()) {
System.err.println("lsof: Unexpected output");
System.err.println("lsof: Output before: " + linesBefore);
System.err.println("lsof: Output after: " + linesAfter);
if (linesBefore != null) {
if (linesAfter.size() >= linesBefore.size()) {
System.err.println("lsof: Unexpected output");
System.err.println("lsof: Output before: " + linesBefore);
System.err.println("lsof: Output after: " + linesAfter);
}
assertTrue(linesAfter.size() < linesBefore.size(),
"Our unix socket file handle should have been cleared out");
}
assertTrue(linesAfter.size() < linesBefore.size(),
"Our unix socket file handle should have been cleared out");
} finally {
process.destroy();
process.waitFor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,15 @@ public void testTunnelPingPong() throws Exception {
String utun = "utun" + (rsa.getUnit() - 1);
// System.out.println(utun);

int rcIfconfig = Runtime.getRuntime().exec(new String[] {
"/sbin/ifconfig", utun, UTUN_SRC_IP.getHostAddress(), UTUN_DST_IP.getHostAddress()})
.waitFor();
Process p = Runtime.getRuntime().exec(new String[] {
"/sbin/ifconfig", utun, UTUN_SRC_IP.getHostAddress(), UTUN_DST_IP.getHostAddress()});
int rcIfconfig;
try {
rcIfconfig = p.waitFor();
} finally {
p.destroyForcibly();
}

assertEquals(0, rcIfconfig, "Could not set IP address for " + utun);

AFSYSTEMDatagramChannel channel = socket.getChannel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.nio.charset.StandardCharsets;
import java.rmi.NotBoundException;
import java.rmi.server.RMISocketFactory;
import java.util.Objects;

import org.junit.jupiter.api.Test;
import org.newsclub.net.unix.AFSocketCapability;
Expand Down Expand Up @@ -83,7 +84,8 @@ public void testWriteAndReadHello() throws IOException, NotBoundException {
}

try (NaiveFileInputStreamRemote rfis = svc.naiveInputStreamRemote();
FileInputStream fin = rfis.getRemoteFileDescriptor().asFileInputStream()) {
FileInputStream fin = Objects.requireNonNull(rfis.getRemoteFileDescriptor()
.asFileInputStream())) {
assertEquals('H', rfis.read());
assertEquals('e', fin.read());
assertEquals('l', fin.read());
Expand Down

0 comments on commit f96ce5c

Please sign in to comment.