Skip to content

Commit

Permalink
test: correct existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Zmax0 committed Jan 16, 2024
1 parent 835a5ff commit 6fa1a42
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 175 deletions.
2 changes: 1 addition & 1 deletion urban-spork-client/src/com/urbanspork/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void main(String[] args) {
public static void launch(ClientConfig config, Promise<ServerSocketChannel> promise) {
int port = config.getPort();
ServerConfig current = config.getCurrent();
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ChannelHandler udpTransportHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.urbanspork.common.config.ClientConfig;
import com.urbanspork.common.config.ClientConfigTestCase;
import com.urbanspork.common.protocol.Protocols;
import com.urbanspork.common.protocol.network.Network;
import com.urbanspork.common.protocol.socks.ClientHandshake;
import com.urbanspork.test.TestDice;
import com.urbanspork.test.TestUtil;
Expand All @@ -19,15 +20,15 @@
@DisplayName("Client - Socks Handshake")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ClientSocksHandshakeTestCase {
private static final int[] PORTS = TestUtil.freePorts(2);
private static final int[] PORTS = TestUtil.freePorts(2, Network.TCP);
private final EventLoopGroup group = new NioEventLoopGroup();
private Future<?> future;

@Test
void testUdpEnable() throws InterruptedException {
ClientConfig config = ClientConfigTestCase.testConfig(PORTS[0], PORTS[1]);
config.getServers().getFirst().setProtocol(Protocols.vmess);
future = ClientTestCase.launchClient(config);
future = ClientTestCase.asyncLaunchClient(config);
InetSocketAddress proxyAddress = new InetSocketAddress(config.getPort());
InetSocketAddress dstAddress1 = new InetSocketAddress("localhost", TestDice.rollPort());
assertFailedHandshake(proxyAddress, dstAddress1);
Expand All @@ -36,7 +37,7 @@ void testUdpEnable() throws InterruptedException {
@Test
void testIllegalDstAddress() throws InterruptedException {
ClientConfig config = ClientConfigTestCase.testConfig(PORTS[0], PORTS[1]);
future = ClientTestCase.launchClient(config);
future = ClientTestCase.asyncLaunchClient(config);
InetSocketAddress proxyAddress = new InetSocketAddress(config.getPort());
InetSocketAddress dstAddress1 = new InetSocketAddress("localhost", 0);
assertFailedHandshake(proxyAddress, dstAddress1);
Expand Down
41 changes: 24 additions & 17 deletions urban-spork-test/test/com/urbanspork/client/ClientTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.urbanspork.common.config.ClientConfig;
import com.urbanspork.common.config.ClientConfigTestCase;
import com.urbanspork.common.config.ConfigHandler;
import com.urbanspork.common.protocol.network.Network;
import com.urbanspork.common.protocol.socks.ClientHandshake;
import com.urbanspork.test.TestUtil;
import io.netty.channel.DefaultEventLoop;
Expand All @@ -27,33 +28,39 @@
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class ClientTestCase {
private final int[] ports = TestUtil.freePorts(2);
private static final ExecutorService SERVICE = Executors.newVirtualThreadPerTaskExecutor();
private final int[] ports = TestUtil.freePorts(2, Network.TCP);
private final EventLoopGroup group = new NioEventLoopGroup();

@Test
@Order(1)
void testExit() {
ClientConfig config = ClientConfigTestCase.testConfig(ports);
ConfigHandler.DEFAULT.save(config);
ExecutorService pool = Executors.newVirtualThreadPerTaskExecutor();
Future<?> future = pool.submit(() -> Client.main(null));
try {
future.get(2, TimeUnit.SECONDS);
} catch (Exception e) {
if (e instanceof TimeoutException) {
future.cancel(true);
} else {
throw new RuntimeException(e);
try (ExecutorService pool = Executors.newVirtualThreadPerTaskExecutor()) {
Future<?> future = pool.submit(() -> Client.main(null));
try {
future.get(2, TimeUnit.SECONDS);
} catch (Exception e) {
if (e instanceof TimeoutException) {
future.cancel(true);
} else {
throw new RuntimeException(e);
}
}
Assertions.assertTrue(future.isCancelled());
}
pool.shutdown();
Assertions.assertTrue(future.isCancelled());
}

@RepeatedTest(2)
@Test
@Order(2)
void launchClient() throws InterruptedException {
launchClient(ClientConfigTestCase.testConfig(ports));
void asyncLaunchClient() throws InterruptedException, ExecutionException, TimeoutException {
Future<?> f1 = asyncLaunchClient(ClientConfigTestCase.testConfig(ports));
Future<?> f2 = asyncLaunchClient(ClientConfigTestCase.testConfig(ports));
f2.get(5, TimeUnit.SECONDS);
Assertions.assertFalse(f1.isDone());
Assertions.assertTrue(f2.isDone());
f1.cancel(true);
}

@ParameterizedTest
Expand All @@ -65,10 +72,10 @@ void testHandshake(Socks5CommandType type) {
Assertions.assertThrows(ExecutionException.class, () -> ClientHandshake.noAuth(group, type, proxyAddress, dstAddress).get(10, TimeUnit.SECONDS));
}

public static Future<?> launchClient(ClientConfig config) throws InterruptedException {
public static Future<?> asyncLaunchClient(ClientConfig config) throws InterruptedException {
DefaultEventLoop executor = new DefaultEventLoop();
Promise<ServerSocketChannel> promise = executor.newPromise();
Future<?> future = Executors.newVirtualThreadPerTaskExecutor().submit(() -> Client.launch(config, promise));
Future<?> future = SERVICE.submit(() -> Client.launch(config, promise));
promise.await();
executor.shutdownGracefully();
return future;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.urbanspork.common.config.ServerConfig;
import com.urbanspork.common.config.ServerConfigTestCase;
import com.urbanspork.common.protocol.network.Network;
import com.urbanspork.test.TestUtil;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.jupiter.api.Assertions;
Expand All @@ -14,9 +15,9 @@
class RemoteConnectHandlerTestCase {
@Test
void testConnectFailed() {
ServerConfig config = ServerConfigTestCase.testConfig(TestUtil.freePort());
ServerConfig config = ServerConfigTestCase.testConfig(TestUtil.freePort(Network.TCP));
EmbeddedChannel channel = new EmbeddedChannel(new RemoteConnectHandler(config));
channel.writeInbound(new InetSocketAddress(TestUtil.freePort()));
channel.writeInbound(new InetSocketAddress(TestUtil.freePort(Network.TCP)));
Assertions.assertFalse(channel.isActive());
}
}
62 changes: 32 additions & 30 deletions urban-spork-test/test/com/urbanspork/server/ServerTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void launchFailed() throws InterruptedException {

@Test
void shutdown() {
List<ServerConfig> configs = ServerConfigTestCase.testConfig(TestUtil.freePorts(2));
List<ServerConfig> configs = ServerConfigTestCase.testConfig(TestUtil.freePorts(2, Network.TCP));
Future<?> future;
try (ExecutorService service = Executors.newSingleThreadExecutor()) {
future = service.submit(() -> Server.launch(configs));
Expand All @@ -73,43 +73,45 @@ void shutdown() {

@Test
void sendInvalidUDP() throws InterruptedException, ExecutionException {
List<ServerConfig> configs = ServerConfigTestCase.testConfig(TestUtil.freePorts(1));
List<ServerConfig> configs = ServerConfigTestCase.testConfig(TestUtil.freePorts(1, Network.TCP));
ServerConfig config = configs.getFirst();
config.setNetworks(new Network[]{Network.TCP, Network.UDP});
DefaultEventLoop executor = new DefaultEventLoop();
ExecutorService service = Executors.newVirtualThreadPerTaskExecutor();
Promise<List<ServerSocketChannel>> promise = executor.newPromise();
service.submit(() -> Server.launch(configs, promise));
promise.await().get();
InetSocketAddress serverAddress = new InetSocketAddress(config.getHost(), config.getPort());
Channel channel = new Bootstrap().group(new NioEventLoopGroup())
.channel(NioDatagramChannel.class)
.handler(new LoggingHandler())
.bind(0).syncUninterruptibly().channel();
ChannelFuture future = channel.writeAndFlush(new DatagramPacket(Unpooled.wrappedBuffer(Dice.rollBytes(512)), serverAddress)).sync();
future.get();
Assertions.assertTrue(future.isDone());
executor.shutdownGracefully();
service.shutdown();
try (ExecutorService service = Executors.newVirtualThreadPerTaskExecutor()) {
Promise<List<ServerSocketChannel>> promise = executor.newPromise();
Future<?> server = service.submit(() -> Server.launch(configs, promise));
promise.await().get();
InetSocketAddress serverAddress = new InetSocketAddress(config.getHost(), config.getPort());
Channel channel = new Bootstrap().group(new NioEventLoopGroup())
.channel(NioDatagramChannel.class)
.handler(new LoggingHandler())
.bind(0).syncUninterruptibly().channel();
ChannelFuture future = channel.writeAndFlush(new DatagramPacket(Unpooled.wrappedBuffer(Dice.rollBytes(512)), serverAddress)).sync();
future.get();
Assertions.assertTrue(future.isDone());
executor.shutdownGracefully();
server.cancel(true);
}
}

@Test
void sendInvalidTCP() throws InterruptedException, ExecutionException {
List<ServerConfig> configs = ServerConfigTestCase.testConfig(TestUtil.freePorts(1));
List<ServerConfig> configs = ServerConfigTestCase.testConfig(TestUtil.freePorts(1, Network.TCP));
ServerConfig config = configs.getFirst();
DefaultEventLoop executor = new DefaultEventLoop();
ExecutorService service = Executors.newVirtualThreadPerTaskExecutor();
Promise<List<ServerSocketChannel>> promise = executor.newPromise();
service.submit(() -> Server.launch(configs, promise));
promise.await().get();
Channel channel = new Bootstrap().group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.handler(new LoggingHandler())
.connect(new InetSocketAddress(config.getHost(), config.getPort())).syncUninterruptibly().channel();
ChannelFuture future = channel.writeAndFlush(Unpooled.wrappedBuffer(Dice.rollBytes(512))).sync();
future.get();
Assertions.assertTrue(future.isDone());
executor.shutdownGracefully();
service.shutdown();
try (ExecutorService service = Executors.newVirtualThreadPerTaskExecutor()) {
Promise<List<ServerSocketChannel>> promise = executor.newPromise();
Future<?> server = service.submit(() -> Server.launch(configs, promise));
promise.await().get();
Channel channel = new Bootstrap().group(new NioEventLoopGroup())
.channel(NioSocketChannel.class)
.handler(new LoggingHandler())
.connect(new InetSocketAddress(config.getHost(), config.getPort())).syncUninterruptibly().channel();
ChannelFuture future = channel.writeAndFlush(Unpooled.wrappedBuffer(Dice.rollBytes(512))).sync();
future.get();
Assertions.assertTrue(future.isDone());
executor.shutdownGracefully();
server.cancel(true);
}
}
}

This file was deleted.

31 changes: 13 additions & 18 deletions urban-spork-test/test/com/urbanspork/test/TCPTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
import com.urbanspork.common.config.*;
import com.urbanspork.common.manage.shadowsocks.ServerUserManager;
import com.urbanspork.common.protocol.Protocols;
import com.urbanspork.common.protocol.network.Network;
import com.urbanspork.test.template.Parameter;
import com.urbanspork.test.template.TCPTestTemplate;
import io.netty.channel.DefaultEventLoop;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@DisplayName("TCP")
class TCPTestCase extends TCPTestTemplate {
Expand All @@ -24,26 +23,24 @@ class TCPTestCase extends TCPTestTemplate {
void testByParameter(Parameter parameter) throws ExecutionException, InterruptedException {
Protocols protocol = parameter.protocol();
CipherKind cipher = parameter.cipher();
if (Protocols.shadowsocks == protocol && cipher.isAead2022() && cipher.supportEih()) {
if (cipher.isAead2022() && cipher.supportEih()) {
testShadowsocksAEAD2022EihByParameter(parameter);
}
DefaultEventLoop executor = new DefaultEventLoop();
ExecutorService service = Executors.newVirtualThreadPerTaskExecutor();
int[] ports = TestUtil.freePorts(2);
int[] ports = TestUtil.freePorts(2, Network.TCP);
ClientConfig config = ClientConfigTestCase.testConfig(ports[0], ports[1]);
ServerConfig serverConfig = config.getServers().getFirst();
serverConfig.setProtocol(protocol);
serverConfig.setCipher(cipher);
serverConfig.setPassword(parameter.serverPassword());
launchServer(service, executor, config.getServers());
launchClient(service, executor, config);
Future<?> server = launchServer(pool, executor, config.getServers());
Future<?> client = launchClient(pool, executor, config);
handshakeAndSendBytes(config);
service.shutdown();
executor.shutdownGracefully();
server.cancel(true);
client.cancel(true);
}

void testShadowsocksAEAD2022EihByParameter(Parameter parameter) throws ExecutionException, InterruptedException {
int[] ports = TestUtil.freePorts(2);
int[] ports = TestUtil.freePorts(2, Network.TCP);
Protocols protocol = parameter.protocol();
CipherKind cipher = parameter.cipher();
ServerConfig serverConfig = ServerConfigTestCase.testConfig(ports[1]);
Expand All @@ -53,18 +50,16 @@ void testShadowsocksAEAD2022EihByParameter(Parameter parameter) throws Execution
List<ServerUserConfig> user = new ArrayList<>();
user.add(new ServerUserConfig(TestDice.rollString(10), parameter.clientPassword()));
serverConfig.setUser(user);
ExecutorService service = Executors.newVirtualThreadPerTaskExecutor();
DefaultEventLoop executor = new DefaultEventLoop();
launchServer(service, executor, List.of(serverConfig));
Future<?> server = launchServer(pool, executor, List.of(serverConfig));
ClientConfig config = ClientConfigTestCase.testConfig(ports[0], ports[1]);
ServerConfig current = config.getCurrent();
current.setCipher(cipher);
current.setProtocol(protocol);
current.setPassword(parameter.serverPassword() + ":" + parameter.clientPassword());
launchClient(service, executor, config);
Future<?> client = launchClient(pool, executor, config);
handshakeAndSendBytes(config);
ServerUserManager.DEFAULT.clear();
service.shutdown();
executor.shutdownGracefully();
server.cancel(true);
client.cancel(true);
}
}
Loading

0 comments on commit 6fa1a42

Please sign in to comment.