From f494e6ef49acaee6572b5ad53d36b1b0e6709409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Kohlschu=CC=88tter?= Date: Wed, 11 Sep 2024 22:04:12 +0200 Subject: [PATCH] test: vsock: Fix false-positive test failure With some Linux kernels, certain vsock-related tests fail even though should just "pass with issues" (vsock is unavailable). This is due to the fact that we didn't catch NotYetConnectedException with an InvalidSocketException cause. Observed on Oracle cloud with Linux 5.4.17-2136.305.5.3.el8uek.x86_64 --- .../org/newsclub/net/unix/SocketTestBase.java | 17 +++++++++++++- .../net/unix/vsock/SocketChannelTest.java | 23 +++++++++++++++---- .../net/unix/vsock/ThroughputTest.java | 21 +++++++++++++++++ 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/junixsocket-common/src/test/java/org/newsclub/net/unix/SocketTestBase.java b/junixsocket-common/src/test/java/org/newsclub/net/unix/SocketTestBase.java index 6ebd2c83b..1929150ed 100644 --- a/junixsocket-common/src/test/java/org/newsclub/net/unix/SocketTestBase.java +++ b/junixsocket-common/src/test/java/org/newsclub/net/unix/SocketTestBase.java @@ -496,7 +496,22 @@ protected final void connectSocket(Socket socket, SocketAddress endpoint) throws protected final boolean connectSocket(SocketChannel socketChannel, SocketAddress endpoint) throws IOException { - return asp.connectSocket(socketChannel, endpoint); + try { + return asp.connectSocket(socketChannel, endpoint); + } catch (IllegalStateException | IOException e) { + throw handleConnectSocketException(socketChannel, endpoint, e); + } + } + + protected IOException handleConnectSocketException(SocketChannel socketChannel, + SocketAddress endpoint, Exception e) { + if (e instanceof IOException) { + return (IOException) e; + } else if (e instanceof RuntimeException) { + throw (RuntimeException) e; + } else { + throw new IllegalStateException(e); + } } protected CloseablePair newInterconnectedSockets() throws IOException { diff --git a/junixsocket-vsock/src/test/java/org/newsclub/net/unix/vsock/SocketChannelTest.java b/junixsocket-vsock/src/test/java/org/newsclub/net/unix/vsock/SocketChannelTest.java index a58fe0cd6..41594b85c 100644 --- a/junixsocket-vsock/src/test/java/org/newsclub/net/unix/vsock/SocketChannelTest.java +++ b/junixsocket-vsock/src/test/java/org/newsclub/net/unix/vsock/SocketChannelTest.java @@ -20,6 +20,7 @@ import java.io.IOException; import java.net.SocketAddress; import java.net.SocketTimeoutException; +import java.nio.channels.NotYetConnectedException; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; @@ -77,14 +78,26 @@ MessageType.TEST_ABORTED_SHORT_WITH_ISSUES, msg, summaryImportantMessage(msg)).i @Override protected boolean handleConnect(SocketChannel sc, SocketAddress sa) throws IOException { + Exception ex; try { return super.handleConnect(sc, sa); + } catch (NotYetConnectedException e) { + Throwable cause = e.getCause(); + if (cause instanceof InvalidSocketException) { + ex = e; + // continue below + } else { + throw e; + } } catch (InvalidSocketException e) { - String msg = "Could not connect AF_VSOCK socket to CID=" + ((AFVSOCKSocketAddress) sa) - .getVSOCKCID() + "; check kernel capabilities."; - throw (TestAbortedWithImportantMessageException) new TestAbortedWithImportantMessageException( - MessageType.TEST_ABORTED_SHORT_WITH_ISSUES, msg, summaryImportantMessage(msg)).initCause( - e); + ex = e; + // continue below } + + String msg = "Could not connect AF_VSOCK socket to CID=" + ((AFVSOCKSocketAddress) sa) + .getVSOCKCID() + "; check kernel capabilities."; + throw (TestAbortedWithImportantMessageException) new TestAbortedWithImportantMessageException( + MessageType.TEST_ABORTED_SHORT_WITH_ISSUES, msg, summaryImportantMessage(msg)).initCause( + ex); } } diff --git a/junixsocket-vsock/src/test/java/org/newsclub/net/unix/vsock/ThroughputTest.java b/junixsocket-vsock/src/test/java/org/newsclub/net/unix/vsock/ThroughputTest.java index 420a956ff..56c3a618c 100644 --- a/junixsocket-vsock/src/test/java/org/newsclub/net/unix/vsock/ThroughputTest.java +++ b/junixsocket-vsock/src/test/java/org/newsclub/net/unix/vsock/ThroughputTest.java @@ -17,12 +17,19 @@ */ package org.newsclub.net.unix.vsock; +import java.io.IOException; +import java.net.SocketAddress; +import java.nio.channels.SocketChannel; + import org.junit.jupiter.api.Test; import org.newsclub.net.unix.AFSocketCapability; import org.newsclub.net.unix.AFSocketCapabilityRequirement; import org.newsclub.net.unix.AFVSOCKSocketAddress; +import org.newsclub.net.unix.InvalidSocketException; import com.kohlschutter.annotations.compiletime.SuppressFBWarnings; +import com.kohlschutter.testutil.TestAbortedWithImportantMessageException; +import com.kohlschutter.testutil.TestAbortedWithImportantMessageException.MessageType; @AFSocketCapabilityRequirement(AFSocketCapability.CAPABILITY_VSOCK) @SuppressFBWarnings("NM_SAME_SIMPLE_NAME_AS_SUPERCLASS") @@ -72,4 +79,18 @@ public void testDatagramChannelNonBlocking() throws Exception { public void testDatagramChannelNonBlockingDirect() throws Exception { super.testDatagramChannelNonBlockingDirect(); } + + @Override + protected IOException handleConnectSocketException(SocketChannel channel, SocketAddress sa, + Exception e) { + if (e instanceof InvalidSocketException || e.getCause() instanceof InvalidSocketException) { + String msg = "Could not connect AF_VSOCK socket to CID=" + ((AFVSOCKSocketAddress) sa) + .getVSOCKCID() + "; check kernel capabilities."; + throw (TestAbortedWithImportantMessageException) new TestAbortedWithImportantMessageException( + MessageType.TEST_ABORTED_SHORT_WITH_ISSUES, msg, summaryImportantMessage(msg)).initCause( + e); + + } + return super.handleConnectSocketException(channel, sa, e); + } }