diff --git a/junixsocket-common/src/test/java/org/newsclub/net/unix/SelftestProvider.java b/junixsocket-common/src/test/java/org/newsclub/net/unix/SelftestProvider.java index 5a40f9415..3744eda4b 100644 --- a/junixsocket-common/src/test/java/org/newsclub/net/unix/SelftestProvider.java +++ b/junixsocket-common/src/test/java/org/newsclub/net/unix/SelftestProvider.java @@ -20,6 +20,7 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.InetSocketAddress; +import java.net.SocketAddress; import java.nio.charset.Charset; import java.util.Arrays; import java.util.HashSet; @@ -79,9 +80,9 @@ public SelftestProvider() { registerTest(InetAddressTest.class); registerTest(org.newsclub.net.unix.domain.InterruptTest.class); - registerTest(COMMON, org.newsclub.net.unix.domain.InterruptIssue158Test.class); - registerTest(COMMON_JEP380, org.newsclub.net.unix.jep380.InterruptIssue158Test.class); - registerTest(COMMON_JAVA_INET, org.newsclub.net.unix.java.InterruptIssue158Test.class); + registerTest(org.newsclub.net.unix.domain.InterruptIssue158Test.class); + registerTestJavaInet(org.newsclub.net.unix.java.InterruptIssue158Test.class); + registerTestJEP380(org.newsclub.net.unix.jep380.InterruptIssue158Test.class); registerTestJavaInet(org.newsclub.net.unix.java.InterruptTest.class); @@ -103,6 +104,8 @@ public SelftestProvider() { registerTest(org.newsclub.net.unix.domain.SocketAddressTest.class); registerTest(org.newsclub.net.unix.domain.SocketChannelTest.class); + registerTestJavaInet(org.newsclub.net.unix.java.SocketChannelTest.class); + registerTestJEP380(org.newsclub.net.unix.jep380.SocketChannelTest.class); registerTest(org.newsclub.net.unix.domain.SocketFactoryTest.class); @@ -138,6 +141,11 @@ private void registerTestJavaInet( // registerTest(COMMON_JAVA_INET, testJava); } + private void registerTestJEP380( // + Class> testJava) { + registerTest(COMMON_JEP380, testJava); + } + private void registerTest(String group, Class test) { if (test != null) { testMap.computeIfAbsent(group, (key) -> new LinkedHashSet<>()).add(test); diff --git a/junixsocket-common/src/test/java/org/newsclub/net/unix/SocketChannelTest.java b/junixsocket-common/src/test/java/org/newsclub/net/unix/SocketChannelTest.java index a292800c3..27c1d0beb 100644 --- a/junixsocket-common/src/test/java/org/newsclub/net/unix/SocketChannelTest.java +++ b/junixsocket-common/src/test/java/org/newsclub/net/unix/SocketChannelTest.java @@ -19,6 +19,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -26,8 +27,11 @@ import java.net.SocketAddress; import java.net.SocketException; import java.net.SocketTimeoutException; +import java.net.StandardSocketOptions; import java.nio.ByteBuffer; import java.nio.channels.ClosedChannelException; +import java.nio.channels.NotYetBoundException; +import java.nio.channels.NotYetConnectedException; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; import java.util.Objects; @@ -159,7 +163,11 @@ private void testDoubleBind(boolean reuseAddress) throws Exception { }); try (ServerSocketChannel ssc2 = selectorProvider().openServerSocketChannel()) { - ssc2.socket().setReuseAddress(reuseAddress); + try { + ssc2.setOption(StandardSocketOptions.SO_REUSEADDR, reuseAddress); + } catch (UnsupportedOperationException e) { + // ignore + } try { wasRebound.set(true); @@ -406,4 +414,22 @@ protected SocketAddress resolveAddressForSecondBind(SocketAddress originalAddres protected boolean socketDomainPermitsDoubleBind() { return false; } + + @Test + public void testReadNotConnectedYet() throws Exception { + SocketChannel sc = newSocketChannel(); + assertThrows(NotYetConnectedException.class, () -> sc.read(ByteBuffer.allocate(1))); + } + + @Test + public void testWriteNotConnectedYet() throws Exception { + SocketChannel sc = newSocketChannel(); + assertThrows(NotYetConnectedException.class, () -> sc.write(ByteBuffer.allocate(1))); + } + + @Test + public void testAcceptNotBoundYet() throws Exception { + ServerSocketChannel sc = newServerSocketChannel(); + assertThrows(NotYetBoundException.class, sc::accept); + } } diff --git a/junixsocket-common/src/test/java/org/newsclub/net/unix/java/SocketChannelTest.java b/junixsocket-common/src/test/java/org/newsclub/net/unix/java/SocketChannelTest.java new file mode 100644 index 000000000..418316e2e --- /dev/null +++ b/junixsocket-common/src/test/java/org/newsclub/net/unix/java/SocketChannelTest.java @@ -0,0 +1,35 @@ +/* + * junixsocket + * + * Copyright 2009-2024 Christian Kohlschütter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.newsclub.net.unix.java; + +import java.net.InetSocketAddress; + +import org.newsclub.net.unix.AFSocketCapability; +import org.newsclub.net.unix.AFSocketCapabilityRequirement; + +import com.kohlschutter.annotations.compiletime.SuppressFBWarnings; + +@AFSocketCapabilityRequirement(AFSocketCapability.CAPABILITY_UNIX_DOMAIN) +@SuppressFBWarnings("NM_SAME_SIMPLE_NAME_AS_SUPERCLASS") +public final class SocketChannelTest extends + org.newsclub.net.unix.SocketChannelTest { + + public SocketChannelTest() { + super(JavaAddressSpecifics.INSTANCE); + } +} diff --git a/junixsocket-common/src/test/java/org/newsclub/net/unix/jep380/SocketChannelTest.java b/junixsocket-common/src/test/java/org/newsclub/net/unix/jep380/SocketChannelTest.java new file mode 100644 index 000000000..2e53d1c70 --- /dev/null +++ b/junixsocket-common/src/test/java/org/newsclub/net/unix/jep380/SocketChannelTest.java @@ -0,0 +1,35 @@ +/* + * junixsocket + * + * Copyright 2009-2024 Christian Kohlschütter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.newsclub.net.unix.jep380; + +import java.net.SocketAddress; + +import org.newsclub.net.unix.AFSocketCapability; +import org.newsclub.net.unix.AFSocketCapabilityRequirement; + +import com.kohlschutter.annotations.compiletime.SuppressFBWarnings; + +@AFSocketCapabilityRequirement(AFSocketCapability.CAPABILITY_UNIX_DOMAIN) +@SuppressFBWarnings("NM_SAME_SIMPLE_NAME_AS_SUPERCLASS") +public final class SocketChannelTest extends + org.newsclub.net.unix.SocketChannelTest { + + public SocketChannelTest() { + super(JEP380AddressSpecifics.INSTANCE); + } +}