3 changed files with 141 additions and 159 deletions
@ -1,165 +1,129 @@
|
||||
/**
|
||||
* @file test_invite_group_create.c |
||||
* @brief Full invite flow: topo_group_invite_join → B is member → TGI_EVENT_JOIN |
||||
*/ |
||||
/* INVITE_INFO получает подписанное описание канала, но не добавляет нового мембера.
|
||||
* Фиксированный X25519-ключ ответчика начинается с 00: это допустимый ключ. */ |
||||
#include <stdio.h> |
||||
#include <stdlib.h> |
||||
#include <string.h> |
||||
#include <stdarg.h> |
||||
#include "../lib/platform_compat.h" |
||||
#include "../lib/mem.h" |
||||
#include "../lib/socket_compat.h" |
||||
#include "../lib/debug_config.h" |
||||
#include "test_utils.h" |
||||
#ifndef _WIN32 |
||||
#include <unistd.h> |
||||
#endif |
||||
|
||||
#include "../src/transport_layer/etcp.h" |
||||
#include "../src/transport_layer/etcp_connections.h" |
||||
#include "../src/config_parser.h" |
||||
#include "../src/config_updater.h" |
||||
#include "../src/utun_instance.h" |
||||
#include "../src/chat/chat_core.h" |
||||
#include "../src/chat/invite_link.h" |
||||
#include "../src/routing_layer/topo_group.h" |
||||
#include "../src/routing_layer/topo_node.h" |
||||
#include "../src/routing_layer/topo_node_sqlite.h" |
||||
#include "../src/routing_layer/topo_group_invite.h" |
||||
#include "../src/transport_layer/etcp_connections.h" |
||||
|
||||
#define TIMEOUT_TB 50000 |
||||
#define POLL_MS 20 |
||||
|
||||
static void fail(const char* msg) { fprintf(stderr, "FAIL: %s\n", msg); fflush(stderr); exit(1); } |
||||
|
||||
static void wf(const char* path, const char* fmt, ...) { |
||||
va_list ap; va_start(ap, fmt); |
||||
FILE* f = fopen(path, "w"); |
||||
if (!f) { fail("fopen"); return; } |
||||
vfprintf(f, fmt, ap); fclose(f); |
||||
va_end(ap); |
||||
} |
||||
|
||||
static char* ls(const char* p, const char* k) { |
||||
char b[1024]; FILE* f = fopen(p, "r"); if (!f) return NULL; |
||||
size_t n = fread(b, 1, sizeof(b) - 1, f); fclose(f); b[n] = 0; |
||||
char* x = strstr(b, k); if (!x) return NULL; |
||||
x += strlen(k) + 1; while (*x == ' ' || *x == '\t') x++; |
||||
char* r = u_strdup(x); char* e = r; while (*e && *e != '\n' && *e != '\r') e++; *e = 0; |
||||
return r; |
||||
} |
||||
|
||||
static int result = 0; |
||||
|
||||
static void ccb(uint64_t nid, uint64_t gid, int ev, void* arg) { |
||||
(void)gid; (void)arg; |
||||
fprintf(stderr, "CB: ev=%d node=0x%llx\n", ev, (unsigned long long)nid); fflush(stderr); |
||||
if (ev == TGI_EVENT_JOIN) result = 1; |
||||
if (ev == TGI_EVENT_TIMEOUT) result = 2; |
||||
#define REQUIRE(expr) do { if (!(expr)) { \ |
||||
DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "test_invite_group_create: line=%d failed: %s", __LINE__, #expr); \
|
||||
goto cleanup; \
|
||||
} } while (0) |
||||
|
||||
struct invite_result { |
||||
int calls, event; |
||||
uint64_t node_id, group_id; |
||||
}; |
||||
|
||||
/* Сохранить результат; жизненный цикл экземпляров меняется после выхода из callback. */ |
||||
static void invite_done(uint64_t node_id, uint64_t group_id, int event, void* arg) { |
||||
struct invite_result* result = arg; |
||||
result->calls++; result->event = event; result->node_id = node_id; result->group_id = group_id; |
||||
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "test invite result: peer=%016llx group=%016llx event=%d calls=%d", |
||||
(unsigned long long)node_id, (unsigned long long)group_id, event, result->calls); |
||||
} |
||||
|
||||
static void to_cb(void* arg) { (void)arg; fprintf(stderr, "TIMEOUT\n"); fflush(stderr); result = 2; } |
||||
|
||||
int main(void) { |
||||
debug_config_init(); |
||||
debug_set_level(DEBUG_LEVEL_TRACE); |
||||
debug_enable_file_output("/tmp/test_inv_crash.log", 1); |
||||
utun_instance_set_tun_init_enabled(0); |
||||
|
||||
char tdir[] = "/tmp/utin_XXXXXX"; |
||||
if (test_mkdtemp(tdir) != 0) { fail("mkdtemp"); return 1; } |
||||
char ca[256], cb[256]; |
||||
snprintf(ca, sizeof(ca), "%s/a.conf", tdir); |
||||
snprintf(cb, sizeof(cb), "%s/b.conf", tdir); |
||||
int porta = 52000 + (getpid() % 10000), portb = porta + 1; |
||||
|
||||
wf(ca, "[global]\ntun_ip=10.94.0.1/24\ntun_ifname=tun89\ndb_path=%s\n[server: s1]\naddr=127.0.0.1:%d\ntype=public\n[allowed_keys]\nallow_all=1\n", tdir, porta); |
||||
wf(cb, "[global]\ntun_ip=10.94.0.2/24\ntun_ifname=tun88\ndb_path=%s\n[server: s1]\naddr=127.0.0.1:%d\ntype=public\n[allowed_keys]\nallow_all=1\n", tdir, portb); |
||||
config_ensure_keys_and_node_id(ca); config_ensure_keys_and_node_id(cb); |
||||
|
||||
char* rva = ls(ca, "priv"); char* pua = ls(ca, "pub"); char* pub = ls(cb, "pub"); |
||||
wf(ca, "[global]\nmy_private_key=%s\nmy_public_key=%s\ntun_ip=10.94.0.1/24\ntun_ifname=tun89\ndb_path=%s\n[server: s1]\naddr=127.0.0.1:%d\ntype=public\n[client: to_b]\nkeepalive=1\npeer_public_key=%s\nlink=s1:127.0.0.1:%d\n[allowed_keys]\nallow_all=1\n", rva, pua, tdir, porta, pub, portb); |
||||
char* rvb = ls(cb, "priv"); |
||||
wf(cb, "[global]\nmy_private_key=%s\nmy_public_key=%s\ntun_ip=10.94.0.2/24\ntun_ifname=tun88\ndb_path=%s\n[server: s1]\naddr=127.0.0.1:%d\ntype=public\n[allowed_keys]\nallow_all=1\n", rvb, pub, tdir, portb); |
||||
u_free(rva); u_free(pua); u_free(pub); u_free(rvb); |
||||
|
||||
struct UASYNC* ua = uasync_create(); |
||||
struct UTUN_INSTANCE* a = utun_instance_create(ua, ca); |
||||
if (!a) { fail("create A"); goto clean; } |
||||
utun_instance_init(a); topo_group_invite_init(a); |
||||
|
||||
/* ── Add a TCP socket (simulating Android STCP server) ── */ |
||||
{ |
||||
struct ETCP_SOCKET* ts = u_calloc(1, sizeof(*ts)); |
||||
ts->instance = a; |
||||
ts->type = CFG_SERVER_TYPE_PUBLIC; |
||||
ts->sock_id = (uint8_t)a->next_socket_id++; |
||||
struct sockaddr_in sin; memset(&sin, 0, sizeof(sin)); |
||||
sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */ |
||||
sin.sin_port = htons((uint16_t)55555); |
||||
memcpy(&ts->local_addr, &sin, sizeof(sin)); |
||||
ts->interface_addr = ts->local_addr; |
||||
ts->next = a->etcp_sockets; a->etcp_sockets = ts; |
||||
fprintf(stderr, "Added TCP socket ts=%p next=%p\n", (void*)ts, (void*)ts->next); fflush(stderr); |
||||
} |
||||
fprintf(stderr, "etcp_sockets=%p\n", (void*)a->etcp_sockets); fflush(stderr); |
||||
|
||||
/* Get B's node_id and pubkey from its instance */ |
||||
struct UTUN_INSTANCE* b = utun_instance_create(ua, cb); |
||||
if (!b) { fail("create B"); a->running = 0; utun_instance_destroy(a); uasync_destroy(ua, 0); goto clean; } |
||||
utun_instance_init(b); topo_group_invite_init(b); |
||||
|
||||
/* ── Set up B as member of group 0x7777777700000001 ── */ |
||||
{ |
||||
uint64_t gid = 0x7777777700000001ULL; |
||||
char ch_str[32]; snprintf(ch_str, sizeof(ch_str), "%llu", (unsigned long long)gid); |
||||
topo_groups_create_group(b->topo_groups, gid, TOPO_GROUP_TYPE_CHAT, ch_str); |
||||
const char* ch_name = "TestCh"; |
||||
uint8_t sig_msg[256]; size_t slen = 0; |
||||
/* msg: ch_id\0 || name\0 || owner(8 LE) || x25519_pub(32) || ed_pub(32) */ |
||||
size_t ch_id_len = strlen(ch_str) + 1; memcpy(sig_msg + slen, ch_str, ch_id_len); slen += ch_id_len; |
||||
size_t name_len = strlen(ch_name) + 1; memcpy(sig_msg + slen, ch_name, name_len); slen += name_len; |
||||
uint64_t owner = b->node_id; memcpy(sig_msg + slen, &owner, 8); slen += 8; |
||||
memcpy(sig_msg + slen, b->my_keys.public_key, 32); slen += 32; |
||||
memcpy(sig_msg + slen, b->my_ed25519_pubkey, 32); slen += 32; |
||||
uint8_t ch_sig[64]; |
||||
sc_ed25519_sign(b->my_ed25519_privkey, sig_msg, slen, ch_sig); |
||||
topo_node_sqlite_channel_put(b->topo_sqlite_db, ch_str, ch_name, owner, |
||||
b->my_keys.public_key, NULL, |
||||
b->my_ed25519_pubkey, NULL, ch_sig); |
||||
fprintf(stderr, "B: channel setup gid=0x%016llX ch=%s name=%s\n", |
||||
(unsigned long long)gid, ch_str, ch_name); fflush(stderr); |
||||
} |
||||
|
||||
uint64_t nid_b = b->node_id; |
||||
uint8_t pk_b[32]; memcpy(pk_b, b->my_keys.public_key, 32); |
||||
|
||||
/* Build TOPO_NODE for invite */ |
||||
struct TOPO_NODE* ni = u_calloc(1, sizeof(*ni)); |
||||
ni->node_id = nid_b; memcpy(ni->public_key, pk_b, 32); |
||||
struct TOPO_ADDR4* a4 = u_calloc(1, sizeof(*a4)); |
||||
uint8_t ip[4] = {127, 0, 0, 1}; memcpy(a4->addr, ip, 4); |
||||
a4->port = (uint16_t)portb; a4->type = TOPO_ADDR_NAT; a4->protocol = 1; |
||||
ni->v4_addrs = a4; |
||||
|
||||
uint64_t gid = 0x7777777700000001ULL; |
||||
fprintf(stderr, "=== topo_group_invite_join gid=0x%016llX nid=0x%016llX ===\n", |
||||
(unsigned long long)gid, (unsigned long long)nid_b); fflush(stderr); |
||||
|
||||
int r = topo_group_invite_join(a, gid, ni, nid_b, ccb, NULL); |
||||
fprintf(stderr, "topo_group_invite_join => %d\n", r); fflush(stderr); |
||||
|
||||
void* tt = uasync_set_timeout(ua, TIMEOUT_TB, NULL, to_cb, "to"); |
||||
int el = 0; |
||||
while (!result && el < TIMEOUT_TB + 5000) { uasync_poll(ua, POLL_MS); el += POLL_MS; } |
||||
if (tt) uasync_cancel_timeout(ua, tt); |
||||
|
||||
a->running = 0; utun_instance_destroy(a); |
||||
b->running = 0; utun_instance_destroy(b); |
||||
uasync_destroy(ua, 0); |
||||
u_free(a4); u_free(ni); |
||||
|
||||
fprintf(stderr, "=== DONE result=%d ===\n", result); fflush(stderr); |
||||
|
||||
clean: |
||||
test_unlink(ca); test_unlink(cb); test_rmdir(tdir); |
||||
debug_disable_file_output(); |
||||
return (result == 1) ? 0 : 1; |
||||
int result = 1; |
||||
struct UASYNC* ua = NULL; |
||||
struct UTUN_INSTANCE *a = NULL, *b = NULL; |
||||
struct invite_result reply = {0}; |
||||
debug_config_init(); debug_set_level(DEBUG_LEVEL_WARN); |
||||
debug_set_category_level(DEBUG_CATEGORY_GENERAL, DEBUG_LEVEL_INFO); |
||||
debug_set_category_level(DEBUG_CATEGORY_CONNECTION, DEBUG_LEVEL_INFO); |
||||
debug_set_category_level(DEBUG_CATEGORY_BGP, DEBUG_LEVEL_INFO); |
||||
|
||||
ua = uasync_create(); REQUIRE(ua); |
||||
/* У каждого узла своя in-memory БД и настоящий UDP-сокет на свободном порту. */ |
||||
a = utun_instance_create_from_str(ua, |
||||
"[global]\nmy_node_name=invite-A\n" |
||||
"my_private_key=704f2e012c8fa8768130cb0f988a997dccb628372bc5ceccacc78dcbfec5916f\n" |
||||
"my_public_key=b3193173def895bd0fcea6f86af077c7d77216f10395275f627ac18242ec0f01\n" |
||||
"[server: udp]\naddr=127.0.0.1:0\ntype=public\n[allowed_keys]\nallow_all=1\n" |
||||
"[chatserver]\ngroup_autoconnect=0\nstorage_autoload=0\n"); |
||||
b = utun_instance_create_from_str(ua, |
||||
"[global]\nmy_node_name=invite-B\n" |
||||
"my_private_key=a00a000000000000000000000000000000000000000000000000000000000000\n" |
||||
"my_public_key=00b607cef42a8324d990aaa6f19d190d3ee9120369ade4d0f94b539edb050b64\n" |
||||
"[server: udp]\naddr=127.0.0.1:0\ntype=public\n[allowed_keys]\nallow_all=1\n" |
||||
"[chatserver]\ngroup_autoconnect=0\nstorage_autoload=0\n"); |
||||
REQUIRE(a && b); |
||||
REQUIRE(utun_core_start(a) == 0 && chat_service_start(a) == 0); |
||||
REQUIRE(utun_core_start(b) == 0 && chat_service_start(b) == 0); |
||||
topo_group_invite_init(a); topo_group_invite_init(b); |
||||
REQUIRE(a->topo_sqlite_db != b->topo_sqlite_db && !a->utun_started && !b->utun_started); |
||||
REQUIRE(b->my_keys.public_key[0] == 0 && b->my_keys.public_key[1] != 0); |
||||
|
||||
chat_core_create_channel_auto(b, "TestCh"); |
||||
REQUIRE(b->topo_groups->group_list->head); |
||||
struct TOPO_GROUP* group_b = (struct TOPO_GROUP*)b->topo_groups->group_list->head; |
||||
REQUIRE(group_b->group_type == TOPO_GROUP_TYPE_CHAT); |
||||
uint64_t gid = group_b->group_id; |
||||
REQUIRE(!topo_groups_find(a->topo_groups, gid)); |
||||
|
||||
struct sockaddr_in bound = {0}; socklen_t bound_len = sizeof(bound); |
||||
REQUIRE(b->etcp_sockets && !b->etcp_sockets->is_tcp); |
||||
REQUIRE(getsockname(b->etcp_sockets->fd, (struct sockaddr*)&bound, &bound_len) == 0); |
||||
REQUIRE(bound.sin_family == AF_INET && bound.sin_port != 0); |
||||
struct TOPO_ADDR4 address = { .addr = {127, 0, 0, 1}, .protocol = TOPO_PROTO_UDP, .type = TOPO_ADDR_INTERFACE }; |
||||
address.port = ntohs(bound.sin_port); |
||||
struct TOPO_NODE node = { .node_id = b->node_id, .v4_addrs = &address }; |
||||
/* Пустой ключ отклоняется до создания группы и без callback. */ |
||||
REQUIRE(topo_group_invite_join(a, gid, &node, b->node_id, invite_done, &reply) == -1); |
||||
REQUIRE(!reply.calls && !topo_groups_find(a->topo_groups, gid)); |
||||
memcpy(node.public_key, b->my_keys.public_key, sizeof(node.public_key)); |
||||
|
||||
/* Создание ссылки тоже не должно отбрасывать допустимый нулевой префикс ключа. */ |
||||
struct InviteData invite = { .channelId = gid, .join_key = 123, .addrCount = 1 }, decoded; |
||||
invite.addrs[0].family = 4; invite.addrs[0].proto = INVITE_PROTO_UDP; invite.addrs[0].port = address.port; |
||||
memcpy(invite.addrs[0].address, address.addr, 4); |
||||
char link[1024], error[128]; |
||||
REQUIRE(invite_link_encode(&invite, NULL, link, sizeof(link)) == -1); |
||||
memcpy(invite.pubkey, node.public_key, sizeof(invite.pubkey)); |
||||
REQUIRE(invite_link_encode(&invite, "test-password", link, sizeof(link)) > 0); |
||||
REQUIRE(invite_link_decode(link, strlen(link), &decoded, error, sizeof(error)) == 0); |
||||
REQUIRE(decoded.nodeId == b->node_id && decoded.channelId == gid && decoded.join_key == invite.join_key); |
||||
REQUIRE(!memcmp(decoded.pubkey, node.public_key, sizeof(decoded.pubkey))); |
||||
REQUIRE(decoded.addrCount == 1 && decoded.addrs[0].port == address.port && decoded.addrs[0].proto == INVITE_PROTO_UDP); |
||||
REQUIRE(!memcmp(decoded.addrs[0].address, address.addr, 4) && !strcmp(decoded.password, "test-password")); |
||||
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "test invite start: peer=%016llx key_prefix=%02x%02x port=%u", |
||||
(unsigned long long)b->node_id, node.public_key[0], node.public_key[1], address.port); |
||||
REQUIRE(topo_group_invite_join(a, gid, &node, b->node_id, invite_done, &reply) == 0); |
||||
uint64_t deadline = get_time_tb() + 50000; |
||||
while (!reply.calls && get_time_tb() < deadline) uasync_poll(ua, 20); |
||||
REQUIRE(reply.calls == 1 && reply.event == TGI_EVENT_JOIN); |
||||
REQUIRE(reply.node_id == b->node_id && reply.group_id == gid); |
||||
|
||||
struct TOPO_GROUP* group_a = topo_groups_find(a->topo_groups, gid); |
||||
REQUIRE(group_a && group_a->group_type == TOPO_GROUP_TYPE_CHAT); |
||||
char name[128]; uint64_t owner = 0; |
||||
uint8_t x25519[32], ed25519[32], signature[64]; |
||||
REQUIRE(topo_node_sqlite_channel_get(a->topo_sqlite_db, group_a->channel_id, name, sizeof(name), |
||||
&owner, x25519, ed25519, signature) == 0); |
||||
REQUIRE(strcmp(name, "TestCh") == 0 && owner == b->node_id); |
||||
uint8_t expected_x[32], expected_ed[32], expected_signature[64]; |
||||
REQUIRE(topo_node_sqlite_channel_get(b->topo_sqlite_db, group_b->channel_id, name, sizeof(name), |
||||
&owner, expected_x, expected_ed, expected_signature) == 0); |
||||
REQUIRE(!memcmp(x25519, expected_x, 32) && !memcmp(ed25519, expected_ed, 32)); |
||||
REQUIRE(!memcmp(signature, expected_signature, 64)); |
||||
/* Получение описания не заменяет протокол добавления: B ещё не дал членство A. */ |
||||
REQUIRE(!topo_node_sqlite_member_in_channel(b->topo_sqlite_db, group_b->channel_id, a->node_id)); |
||||
REQUIRE(!topo_group_peer_ready(group_b, a->node_id)); |
||||
result = 0; |
||||
cleanup: |
||||
if (a) utun_instance_destroy(a); |
||||
if (b) utun_instance_destroy(b); |
||||
/* Дать event loop освободить отменённые таймеры перед проверкой ресурсов. */ |
||||
if (ua) uasync_poll(ua, 0); |
||||
if (ua) uasync_destroy(ua, 0); |
||||
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "test_invite_group_create: %s", result ? "FAIL" : "PASS"); |
||||
return result; |
||||
} |
||||
|
||||
Loading…
Reference in new issue