Browse Source

tests: remove PID ports and allocate SOCKS listeners dynamically

master
evgeny 3 days ago
parent
commit
0dcecdd7b2
  1. 18
      tests/test_etcp_seq_collision.c
  2. 18
      tests/test_etcp_simple_traffic.c
  3. 23
      tests/test_socks_client.c

18
tests/test_etcp_seq_collision.c

@ -3,6 +3,7 @@
#include <string.h>
#include "../lib/platform_compat.h"
#include "test_utils.h"
#include "test_net.h"
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
@ -35,8 +36,6 @@ static struct UASYNC* ua = NULL;
static char temp_dir[] = "/tmp/utun_col_XXXXXX";
static char server_config_path[256];
static char client_config_path[256];
static int server_port = 0;
static int client_port = 0;
static int test_completed = 0;
static void* global_timeout_id = NULL;
@ -49,10 +48,6 @@ static uint8_t packet_buffer[PACKET_SIZE];
// client = 0x1111 (master, smaller node_id), server = 0x2222 (slave)
static int create_temp_configs(void) {
if (test_mkdtemp(temp_dir) != 0) { fprintf(stderr, "Failed to create temp directory\n"); return -1; }
int base_port = 45000 + (getpid() % 10000);
server_port = base_port;
client_port = base_port + 1;
snprintf(server_config_path, sizeof(server_config_path), "%s/server.conf", temp_dir);
snprintf(client_config_path, sizeof(client_config_path), "%s/client.conf", temp_dir);
@ -67,12 +62,11 @@ static int create_temp_configs(void) {
"tun_ifname=tun99\n"
"\n"
"[server: test]\n"
"addr=127.0.0.1:%d\n"
"addr=127.0.0.1:0\n"
"type=public\n"
"\n"
"[allowed_keys]\n"
"allow_all=1\n",
server_port);
"allow_all=1\n");
fclose(f);
f = fopen(client_config_path, "w");
@ -86,14 +80,13 @@ static int create_temp_configs(void) {
"tun_ifname=tun98\n"
"\n"
"[server: test]\n"
"addr=127.0.0.1:%d\n"
"addr=127.0.0.1:0\n"
"type=public\n"
"\n"
"[client: test_client]\n"
"keepalive=1\n"
"peer_public_key=ce8871f07fa056c636d297115f231b08c29cdf94e0d440fce83a07c34416d36a\n"
"link=test:127.0.0.1:%d\n",
client_port, server_port);
"link=test:127.0.0.1:0\n");
fclose(f);
return 0;
}
@ -214,6 +207,7 @@ int main(void) {
uasync_destroy(ua, 0); cleanup_temp_configs(); return 1;
}
client_instance = utun_instance_create(ua, client_config_path);
if (client_instance) test_etcp_set_peer(client_instance, "test_client", server_instance, "test");
if (!client_instance || utun_instance_init(client_instance) < 0) {
fprintf(stderr, "Failed to create client\n");
utun_instance_destroy(server_instance);

18
tests/test_etcp_simple_traffic.c

@ -3,6 +3,7 @@
#include <string.h>
#include "../lib/platform_compat.h"
#include "test_utils.h"
#include "test_net.h"
#ifdef _WIN32
#include <windows.h>
#include <direct.h>
@ -35,8 +36,6 @@ static struct UASYNC* ua = NULL;
static char temp_dir[] = "/tmp/utun_test_XXXXXX";
static char server_config_path[256];
static char client_config_path[256];
static int server_port = 0;
static int client_port = 0;
static int test_completed = 0;
static void* packet_timeout_id = NULL;
@ -51,10 +50,6 @@ static int create_temp_configs(void) {
fprintf(stderr, "Failed to create temp directory\n");
return -1;
}
int base_port = 40000 + (getpid() % 20000);
server_port = base_port;
client_port = base_port + 1;
snprintf(server_config_path, sizeof(server_config_path), "%s/server.conf", temp_dir);
snprintf(client_config_path, sizeof(client_config_path), "%s/client.conf", temp_dir);
@ -69,12 +64,11 @@ static int create_temp_configs(void) {
"tun_ifname=tun99\n"
"\n"
"[server: test]\n"
"addr=127.0.0.1:%d\n"
"addr=127.0.0.1:0\n"
"type=public\n"
"\n"
"[allowed_keys]\n"
"allow_all=1\n",
server_port);
"allow_all=1\n");
fclose(f);
f = fopen(client_config_path, "w");
@ -88,14 +82,13 @@ static int create_temp_configs(void) {
"tun_ifname=tun98\n"
"\n"
"[server: test]\n"
"addr=127.0.0.1:%d\n"
"addr=127.0.0.1:0\n"
"type=public\n"
"\n"
"[client: test_client]\n"
"keepalive=1\n"
"peer_public_key=ce8871f07fa056c636d297115f231b08c29cdf94e0d440fce83a07c34416d36a\n"
"link=test:127.0.0.1:%d\n",
client_port, server_port);
"link=test:127.0.0.1:0\n");
fclose(f);
return 0;
}
@ -218,6 +211,7 @@ int main(void) {
DEBUG_INFO(DEBUG_CATEGORY_ETCP, "Server ready (node_id=%llx)", (unsigned long long)server_instance->node_id);
client_instance = utun_instance_create(ua, client_config_path);
if (client_instance) test_etcp_set_peer(client_instance, "test_client", server_instance, "test");
if (!client_instance || utun_instance_init(client_instance) < 0) {
fprintf(stderr, "Failed to create client\n");
utun_instance_destroy(server_instance);

23
tests/test_socks_client.c

@ -6,6 +6,7 @@
#include "../lib/socket_compat.h"
#include "../lib/platform_compat.h"
#include "test_clock.h"
#include "test_net.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@ -15,8 +16,6 @@ static int test_failed = 0;
if (!(cond)) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, " FAIL: %s", #cond); test_failed = 1; return test_failed; } \
} while(0)
#define MOCK_PORT 24771
enum { MOCK_NOAUTH = 0, MOCK_AUTH = 1, MOCK_REJECT = 2 };
struct mock {
@ -324,9 +323,9 @@ static int run_dial(struct UASYNC *ua, struct socks_cfg *cfg, const char *target
static int test1_noauth_v4(void) {
struct UASYNC *ua = uasync_create(); TASSERT(ua);
struct mock m;
TASSERT(mock_start(&m, ua, MOCK_PORT, MOCK_NOAUTH) == 0);
TASSERT(mock_start(&m, ua, 0, MOCK_NOAUTH) == 0);
struct socks_cfg cfg; memset(&cfg, 0, sizeof(cfg));
strcpy(cfg.host, "127.0.0.1"); cfg.port = MOCK_PORT;
strcpy(cfg.host, "127.0.0.1"); cfg.port = test_bound_port(m.listen_sock);
TASSERT(run_dial(ua, &cfg, "127.0.0.1", 12345) == 0);
TASSERT(g_sock != SOCKET_INVALID);
@ -351,9 +350,9 @@ static int test1_noauth_v4(void) {
static int test2_auth_domain(void) {
struct UASYNC *ua = uasync_create(); TASSERT(ua);
struct mock m;
TASSERT(mock_start(&m, ua, MOCK_PORT, MOCK_AUTH) == 0);
TASSERT(mock_start(&m, ua, 0, MOCK_AUTH) == 0);
struct socks_cfg cfg; memset(&cfg, 0, sizeof(cfg));
strcpy(cfg.host, "127.0.0.1"); cfg.port = MOCK_PORT;
strcpy(cfg.host, "127.0.0.1"); cfg.port = test_bound_port(m.listen_sock);
strcpy(cfg.user, "alice"); strcpy(cfg.pass, "secret");
TASSERT(run_dial(ua, &cfg, "example.com", 443) == 0);
@ -369,9 +368,9 @@ static int test2_auth_domain(void) {
static int test3_reject(void) {
struct UASYNC *ua = uasync_create(); TASSERT(ua);
struct mock m;
TASSERT(mock_start(&m, ua, MOCK_PORT, MOCK_REJECT) == 0);
TASSERT(mock_start(&m, ua, 0, MOCK_REJECT) == 0);
struct socks_cfg cfg; memset(&cfg, 0, sizeof(cfg));
strcpy(cfg.host, "127.0.0.1"); cfg.port = MOCK_PORT;
strcpy(cfg.host, "127.0.0.1"); cfg.port = test_bound_port(m.listen_sock);
int err = run_dial(ua, &cfg, "127.0.0.1", 12345);
TASSERT(err != 0);
@ -385,7 +384,7 @@ static int test3_reject(void) {
static int test4_udp_associate(void) {
struct UASYNC *ua = uasync_create(); TASSERT(ua);
struct udp_mock m;
TASSERT(umock_start(&m, ua, MOCK_PORT) == 0);
TASSERT(umock_start(&m, ua, 0) == 0);
socket_t lfd = socket(AF_INET, SOCK_DGRAM, 0);
TASSERT(lfd != SOCKET_INVALID);
@ -395,7 +394,7 @@ static int test4_udp_associate(void) {
TASSERT(bind(lfd, (struct sockaddr *)&lb, sizeof(lb)) == 0);
struct socks_cfg cfg; memset(&cfg, 0, sizeof(cfg));
strcpy(cfg.host, "127.0.0.1"); cfg.port = MOCK_PORT;
strcpy(cfg.host, "127.0.0.1"); cfg.port = test_bound_port(m.listen_sock);
g_su_ready = 0; g_su_err = 0;
struct socks_udp *su = socks_udp_associate(ua, &cfg, lfd, su_ready_cb, NULL);
@ -443,7 +442,7 @@ static int test4_udp_associate(void) {
static int test5_udp_reassociate(void) {
struct UASYNC *ua = uasync_create(); TASSERT(ua);
struct udp_mock m;
TASSERT(umock_start(&m, ua, MOCK_PORT) == 0);
TASSERT(umock_start(&m, ua, 0) == 0);
socket_t lfd = socket(AF_INET, SOCK_DGRAM, 0);
TASSERT(lfd != SOCKET_INVALID);
@ -453,7 +452,7 @@ static int test5_udp_reassociate(void) {
TASSERT(bind(lfd, (struct sockaddr *)&lb, sizeof(lb)) == 0);
struct socks_cfg cfg; memset(&cfg, 0, sizeof(cfg));
strcpy(cfg.host, "127.0.0.1"); cfg.port = MOCK_PORT;
strcpy(cfg.host, "127.0.0.1"); cfg.port = test_bound_port(m.listen_sock);
g_su_ready = 0; g_su_err = 0;
struct socks_udp *su = socks_udp_associate(ua, &cfg, lfd, su_ready_cb, NULL);

Loading…
Cancel
Save