From a4df3c23a5c2df1226177f8605c7c51e79a2bcab Mon Sep 17 00:00:00 2001 From: Liu Dongmiao Date: Sun, 7 Apr 2024 18:52:01 +0800 Subject: [PATCH] ctest: add testcase for udp --- CMakeLists.txt | 3 ++- run-test-udp.sh | 47 +++++++++++++++++++++++++++++++++ testcase/check-udp.py | 19 +++++++++++++ testcase/sip003u-client-ss.json | 13 +++++++++ testcase/sip003u-client-ws.json | 13 +++++++++ testcase/sip003u-server.json | 11 ++++++++ testcase/udp-echo-server.py | 14 ++++++++++ 7 files changed, 119 insertions(+), 1 deletion(-) create mode 100755 run-test-udp.sh create mode 100644 testcase/check-udp.py create mode 100644 testcase/sip003u-client-ss.json create mode 100644 testcase/sip003u-client-ws.json create mode 100644 testcase/sip003u-server.json create mode 100644 testcase/udp-echo-server.py diff --git a/CMakeLists.txt b/CMakeLists.txt index 3889b07..703cad6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -61,4 +61,5 @@ target_compile_definitions(wss-proxy-server PRIVATE -DWSS_PROXY_SERVER) install(TARGETS wss-proxy-server DESTINATION bin) include(CTest) -add_test(NAME run-test COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/run-test.sh) \ No newline at end of file +add_test(NAME run-test COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/run-test.sh) +add_test(NAME run-test-udp COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/run-test-udp.sh) diff --git a/run-test-udp.sh b/run-test-udp.sh new file mode 100755 index 0000000..fff4045 --- /dev/null +++ b/run-test-udp.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +set -e +laddr=127.0.0.1 +lport=1081 +sport=8888 + +if which ssserver; then + sslocal=sslocal + ssserver=ssserver +else + curl -L https://github.com/shadowsocks/shadowsocks-rust/releases/download/v1.18.2/shadowsocks-v1.18.2.x86_64-unknown-linux-musl.tar.xz -O + tar xf shadowsocks-v1.18.2.x86_64-unknown-linux-musl.tar.xz + sslocal=./sslocal + ssserver=./ssserver +fi + +python3 ../testcase/udp-echo-server.py & +echo_server_pid=$! + +$ssserver -c ../testcase/sip003u-server.json & +spid=$! + +function cleanup() { + kill $echo_server_pid $lpid $spid + rm -rf o.* +} + +trap cleanup SIGINT SIGTERM ERR EXIT + +echo wss-proxy udp client - ss +$sslocal -c ../testcase/sip003u-client-ss.json & +lpid=$! +sleep 1 +if ! python3 ../testcase/check-udp.py; then + exit 1 +fi + +kill $lpid + +echo wss-proxy udp client - wss +$sslocal -c ../testcase/sip003u-client-ws.json & +lpid=$! +sleep 1 +if ! python3 ../testcase/check-udp.py; then + exit 1 +fi diff --git a/testcase/check-udp.py b/testcase/check-udp.py new file mode 100644 index 0000000..2955265 --- /dev/null +++ b/testcase/check-udp.py @@ -0,0 +1,19 @@ +import socket +import socks + +if __name__ == '__main__': + for x in range(4): + sock = socks.socksocket(socket.AF_INET, socket.SOCK_DGRAM) + sock.set_proxy(socks.SOCKS5, "localhost", 1081) + for y in range(65): + data = ("deadbeef_%d_%d" % (x, y)) + data += 'c' * (1024 * y - len(data)) + req = data.encode('utf8') + try: + sock.sendto(req, ("127.0.0.1", 1235)) + except OSError: + break + (res, addr) = sock.recvfrom(65535) + print("round %d_%d, length: %d" % (x, y, len(data))) + if req != res: + raise ValueError diff --git a/testcase/sip003u-client-ss.json b/testcase/sip003u-client-ss.json new file mode 100644 index 0000000..c28385c --- /dev/null +++ b/testcase/sip003u-client-ss.json @@ -0,0 +1,13 @@ +{ + "local_address": "127.0.0.1", + "local_port": 1081, + "method": "chacha20-ietf-poly1305", + "mode": "tcp_and_udp", + "password": "sip003u", + "plugin": "./wss-proxy-client", + "plugin_mode": "tcp_and_udp", + "plugin_opts": "mux=0;ws=0;loglevel=debug", + "server": "127.0.0.1", + "server_port": 8888, + "timeout": 60 +} \ No newline at end of file diff --git a/testcase/sip003u-client-ws.json b/testcase/sip003u-client-ws.json new file mode 100644 index 0000000..8d7fba3 --- /dev/null +++ b/testcase/sip003u-client-ws.json @@ -0,0 +1,13 @@ +{ + "local_address": "127.0.0.1", + "local_port": 1081, + "method": "chacha20-ietf-poly1305", + "mode": "tcp_and_udp", + "password": "sip003u", + "plugin": "./wss-proxy-client", + "plugin_mode": "tcp_and_udp", + "plugin_opts": "mux=0;loglevel=debug", + "server": "127.0.0.1", + "server_port": 8888, + "timeout": 60 +} \ No newline at end of file diff --git a/testcase/sip003u-server.json b/testcase/sip003u-server.json new file mode 100644 index 0000000..5e7bc53 --- /dev/null +++ b/testcase/sip003u-server.json @@ -0,0 +1,11 @@ +{ + "method": "chacha20-ietf-poly1305", + "mode": "tcp_and_udp", + "password": "sip003u", + "plugin": "./wss-proxy-server", + "plugin_mode": "tcp_and_udp", + "plugin_opts": "mux=0;loglevel=debug", + "server": "127.0.0.1", + "server_port": 8888, + "timeout": 60 +} \ No newline at end of file diff --git a/testcase/udp-echo-server.py b/testcase/udp-echo-server.py new file mode 100644 index 0000000..1f09430 --- /dev/null +++ b/testcase/udp-echo-server.py @@ -0,0 +1,14 @@ +import socket +import sys + +host = '127.0.0.1' +port = int(len(sys.argv) > 1 and sys.argv or '1235') + +server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + +server_socket.bind((host, port)) + +while True: + data, addr = server_socket.recvfrom(65535) + print("received %d bytes" % len(data)) + server_socket.sendto(data, addr)