Skip to content

Commit

Permalink
vsock: test: Skip SocketChannelTest test when bind fails
Browse files Browse the repository at this point in the history
In some environments, such as an already virtualized environment like
WSL 2, binding to a AF_VSOCK server socket may fail with an
AddressUnavailableSocketException.

Handle this situation by failing an assertion, and logging the
exception.
  • Loading branch information
kohlschuetter committed Jul 27, 2023
1 parent da39e26 commit aee4391
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,20 @@ private void testDoubleBind(boolean reuseAddress) throws Exception {
}
}

/**
* Bind the given address to the given {@link ServerSocketChannel}.
*
* By default, this just calls `ssc.bind(sa)`, but you may handle some exceptions by overriding
* this method in a subclass.
*
* @param ssc
* @param sa
* @throws IOException
*/
protected void handleBind(ServerSocketChannel ssc, SocketAddress sa) throws IOException {
ssc.bind(sa);
}

@Test
public void testByteBufferWithPositionOffset() throws Exception {
SocketAddress sa = newTempAddress();
Expand All @@ -240,7 +254,7 @@ public void testByteBufferWithPositionOffset() throws Exception {
getRandom().nextBytes(data);

try (ServerSocketChannel ssc = selectorProvider().openServerSocketChannel()) {
ssc.bind(sa);
handleBind(ssc, sa);

ByteBuffer bb1 = ByteBuffer.allocate(data.length + bb1Offset);
bb1.position(bb1Offset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,16 @@
*/
package org.newsclub.net.unix.vsock;

import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.io.IOException;
import java.net.SocketAddress;
import java.nio.channels.ServerSocketChannel;

import org.newsclub.net.unix.AFSocketCapability;
import org.newsclub.net.unix.AFSocketCapabilityRequirement;
import org.newsclub.net.unix.AFVSOCKSocketAddress;
import org.newsclub.net.unix.AddressUnavailableSocketException;

import com.kohlschutter.annotations.compiletime.SuppressFBWarnings;

Expand All @@ -46,4 +53,14 @@ protected String checkKnownBugFirstAcceptCallNotTerminated() {
return null;
}
}

@Override
protected void handleBind(ServerSocketChannel ssc, SocketAddress sa) throws IOException {
try {
super.handleBind(ssc, sa);
} catch (AddressUnavailableSocketException e) {
e.printStackTrace();
assumeTrue(false, "Could not bind AF_VSOCK server socket; check kernel capabilities");
}
}
}

0 comments on commit aee4391

Please sign in to comment.