Skip to content

Commit

Permalink
ctest: add testcase for udp
Browse files Browse the repository at this point in the history
  • Loading branch information
liudongmiao committed Apr 7, 2024
1 parent d967595 commit a4df3c2
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
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)
47 changes: 47 additions & 0 deletions run-test-udp.sh
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions testcase/check-udp.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions testcase/sip003u-client-ss.json
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions testcase/sip003u-client-ws.json
Original file line number Diff line number Diff line change
@@ -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
}
11 changes: 11 additions & 0 deletions testcase/sip003u-server.json
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions testcase/udp-echo-server.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit a4df3c2

Please sign in to comment.