5 changed files with 73 additions and 11 deletions
@ -0,0 +1,56 @@
|
||||
#ifndef TEST_NET_H |
||||
#define TEST_NET_H |
||||
|
||||
#include "../src/utun_instance.h" |
||||
#include "../src/transport_layer/etcp_connections.h" |
||||
#include "../lib/debug_config.h" |
||||
#include "../lib/socket_compat.h" |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
|
||||
// Читаем порт уже открытого сокета; владение fd и резервирование порта сохраняются.
|
||||
static inline uint16_t test_bound_port(socket_t fd) { |
||||
struct sockaddr_storage address; socklen_t size = sizeof(address); |
||||
if (getsockname(fd, (struct sockaddr*)&address, &size) != 0 || !ss_get_port(&address)) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "test fixture: cannot read bound port fd=%d error=%d", (int)fd, socket_get_error()); |
||||
abort(); |
||||
} |
||||
return ss_get_port(&address); |
||||
} |
||||
|
||||
static inline struct ETCP_SOCKET* test_etcp_bound_socket(struct UTUN_INSTANCE* inst, const char* name) { |
||||
for (struct ETCP_SOCKET* s = inst ? inst->etcp_sockets : NULL; s; s = s->next) { |
||||
if (strcmp(s->name, name)) continue; |
||||
if (ss_get_port(&s->local_addr) && ss_get_port(&s->local_addr) == ss_get_port(&s->interface_addr)) return s; |
||||
DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "test fixture: socket %s has no valid bound endpoint", name); abort(); |
||||
} |
||||
DEBUG_ERROR(DEBUG_CATEGORY_SOCKET, "test fixture: missing listener %s", name); abort(); |
||||
} |
||||
|
||||
// Связываем явно указанные endpoints до start; никаких замен по номеру порта или обхода эмуляторов.
|
||||
static inline void test_etcp_set_peer(struct UTUN_INSTANCE* client, const char* client_name, |
||||
struct UTUN_INSTANCE* peer, const char* socket_name) { |
||||
struct ETCP_SOCKET* target = test_etcp_bound_socket(peer, socket_name); |
||||
if (!client || !client->config || client->utun_started) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "test fixture: client %s must be configured before start", client_name); abort(); |
||||
} |
||||
for (struct CFG_CLIENT* c = client->config->clients; c; c = c->next) { |
||||
if (strcmp(c->name, client_name)) continue; |
||||
if (!c->links) { DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "test fixture: client %s has no links", client_name); abort(); } |
||||
for (struct CFG_CLIENT_LINK* link = c->links; link; link = link->next) { |
||||
if (link->remote_addr.ss_family != target->interface_addr.ss_family || |
||||
(link->local_srv && link->local_srv->transport != target->is_tcp)) { |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "test fixture: incompatible endpoints client=%s listener=%s", client_name, socket_name); |
||||
abort(); |
||||
} |
||||
link->remote_addr = target->interface_addr; |
||||
debug_set_category_level(DEBUG_CATEGORY_DEBUG, DEBUG_LEVEL_INFO); |
||||
DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "test fixture: client=%s listener=%s peer=%016llx port=%u", |
||||
client_name, socket_name, (unsigned long long)peer->node_id, ss_get_port(&link->remote_addr)); |
||||
} |
||||
return; |
||||
} |
||||
DEBUG_ERROR(DEBUG_CATEGORY_CONFIG, "test fixture: missing client %s", client_name); abort(); |
||||
} |
||||
|
||||
#endif |
||||
Loading…
Reference in new issue