Skip to content

Commit

Permalink
test: common: SocketChannelTests for JEP380/Java, not-yet conn/bound
Browse files Browse the repository at this point in the history
  • Loading branch information
kohlschuetter committed Jul 2, 2024
1 parent fdf8a3a commit e72f960
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -138,6 +141,11 @@ private void registerTestJavaInet( //
registerTest(COMMON_JAVA_INET, testJava);
}

private void registerTestJEP380( //
Class<? extends SocketTestBase<SocketAddress>> testJava) {
registerTest(COMMON_JEP380, testJava);
}

private void registerTest(String group, Class<?> test) {
if (test != null) {
testMap.computeIfAbsent(group, (key) -> new LinkedHashSet<>()).add(test);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@

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;

import java.io.IOException;
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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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<InetSocketAddress> {

public SocketChannelTest() {
super(JavaAddressSpecifics.INSTANCE);
}
}
Original file line number Diff line number Diff line change
@@ -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<SocketAddress> {

public SocketChannelTest() {
super(JEP380AddressSpecifics.INSTANCE);
}
}

0 comments on commit e72f960

Please sign in to comment.