Browse Source

tests: assign independent ports to BGP topology listeners

master
evgeny 3 days ago
parent
commit
ce8144b79e
  1. 31
      tests/test_bgp_route_exchange.c
  2. 42
      tests/test_bgp_triangle.c

31
tests/test_bgp_route_exchange.c

@ -27,6 +27,7 @@
#include "../lib/platform_compat.h"
#include "test_utils.h"
#include "test_clock.h"
#include "test_net.h"
#ifdef _WIN32
#include <windows.h>
@ -82,8 +83,6 @@ static int cond_single_link_up(void) { return single_link_up; }
static char temp_dir[] = "/tmp/utun_bgp_mesh_XXXXXX";
static char config_a[256], config_b[256], config_c[256];
static int port_b_srv = 0, port_b_cli = 0, port_c_srv = 0;
static int port_a_srv1 = 0, port_a_srv2 = 0;
static int write_file(const char* path, const char* fmt, ...) {
va_list ap;
@ -106,9 +105,6 @@ static char* get_pubkey(const char* path) {
static int create_temp_configs(void) {
if (test_mkdtemp(temp_dir) != 0) { fprintf(stderr, "mkdtemp failed\n"); return -1; }
int base = 41000 + (getpid() % 15000);
port_b_srv = base; port_b_cli = base + 1; port_c_srv = base + 2;
port_a_srv1 = base + 10; port_a_srv2 = base + 11;
snprintf(config_a, sizeof(config_a), "%s/a.conf", temp_dir);
snprintf(config_b, sizeof(config_b), "%s/b.conf", temp_dir);
@ -126,12 +122,11 @@ static int create_temp_configs(void) {
"my_subnet=192.168.30.0/24\n"
"\n"
"[server: c_srv]\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",
port_c_srv) != 0) return -1;
"allow_all=1\n") != 0) return -1;
if (config_ensure_keys_and_node_id(config_c) != 0) return -1;
char* pub_c = get_pubkey(config_c);
if (!pub_c) return -1;
@ -148,21 +143,21 @@ static int create_temp_configs(void) {
"my_subnet=192.168.20.0/24\n"
"\n"
"[server: b_srv]\n"
"addr=127.0.0.1:%d\n"
"addr=127.0.0.1:0\n"
"type=public\n"
"\n"
"[server: b_cli_srv]\n"
"addr=127.0.0.1:%d\n"
"addr=127.0.0.1:0\n"
"type=public\n"
"\n"
"[client: to_c]\n"
"keepalive=1\n"
"peer_public_key=%s\n"
"link=b_cli_srv:127.0.0.1:%d\n"
"link=b_cli_srv:127.0.0.1:0\n"
"\n"
"[allowed_keys]\n"
"allow_all=1\n",
port_b_srv, port_b_cli, pub_c, port_c_srv) != 0) { free(pub_c); return -1; }
pub_c) != 0) { free(pub_c); return -1; }
free(pub_c);
if (config_ensure_keys_and_node_id(config_b) != 0) return -1;
char* pub_b = get_pubkey(config_b);
@ -180,22 +175,22 @@ static int create_temp_configs(void) {
"my_subnet=192.168.10.0/24\n"
"\n"
"[server: a_srv1]\n"
"addr=127.0.0.1:%d\n"
"addr=127.0.0.1:0\n"
"type=public\n"
"\n"
"[server: a_srv2]\n"
"addr=127.0.0.1:%d\n"
"addr=127.0.0.1:0\n"
"type=public\n"
"\n"
"[client: to_b]\n"
"keepalive=1\n"
"peer_public_key=%s\n"
"link=a_srv1:127.0.0.1:%d\n"
"link=a_srv2:127.0.0.1:%d\n"
"link=a_srv1:127.0.0.1:0\n"
"link=a_srv2:127.0.0.1:0\n"
"\n"
"[allowed_keys]\n"
"allow_all=1\n",
port_a_srv1, port_a_srv2, pub_b, port_b_srv, port_b_srv) != 0) { free(pub_b); return -1; }
pub_b) != 0) { free(pub_b); return -1; }
free(pub_b);
if (config_ensure_keys_and_node_id(config_a) != 0) return -1;
@ -324,6 +319,8 @@ int main(void) {
inst_a = utun_instance_create(ua, config_a); ASSERT(inst_a, "inst_a create");
inst_b = utun_instance_create(ua, config_b); ASSERT(inst_b, "inst_b create");
inst_c = utun_instance_create(ua, config_c); ASSERT(inst_c, "inst_c create");
test_etcp_set_peer(inst_a, "to_b", inst_b, "b_srv");
test_etcp_set_peer(inst_b, "to_c", inst_c, "c_srv");
// В этой топологии A и C доступны только через B, прямой обход запрещён.
struct UTUN_INSTANCE* endpoints[] = { inst_a, inst_c };

42
tests/test_bgp_triangle.c

@ -30,6 +30,7 @@
#include "../lib/platform_compat.h"
#include "test_utils.h"
#include "test_clock.h"
#include "test_net.h"
#ifdef _WIN32
#include <windows.h>
@ -78,9 +79,9 @@ static struct test_connection_loss loss_ab, loss_ac;
* Config generator
* ================================================================ */
struct lnk { const char* local_srv; int remote_port; };
struct lnk { const char* local_srv; };
struct cli { const char* name; const char* peer_pubkey_hex; int lnk_cnt; struct lnk lnks[2]; };
struct srv { const char* name; int port; };
struct srv { const char* name; };
struct ncfg {
uint64_t node_id; const char* tun_ip; const char* my_subnet;
const char* priv_hex; const char* pub_hex;
@ -105,13 +106,13 @@ static void build_node_config(char* buf, size_t size, const struct ncfg* c) {
c->priv_hex, c->pub_hex, c->tun_ip, c->my_subnet);
for (int i = 0; i < c->srv_cnt; i++)
off += snprintf(buf + off, size - off,
"[server: %s]\naddr=127.0.0.1:%d\ntype=public\n\n", c->srvs[i].name, c->srvs[i].port);
"[server: %s]\naddr=127.0.0.1:0\ntype=public\n\n", c->srvs[i].name);
for (int i = 0; i < c->cli_cnt; i++) {
off += snprintf(buf + off, size - off,
"[client: %s]\nkeepalive=1\npeer_public_key=%s\n", c->clis[i].name, c->clis[i].peer_pubkey_hex);
for (int j = 0; j < c->clis[i].lnk_cnt; j++)
off += snprintf(buf + off, size - off,
"link=%s:127.0.0.1:%d\n", c->clis[i].lnks[j].local_srv, c->clis[i].lnks[j].remote_port);
"link=%s:127.0.0.1:0\n", c->clis[i].lnks[j].local_srv);
off += snprintf(buf + off, size - off, "\n");
}
off += snprintf(buf + off, size - off, "[allowed_keys]\nallow_all=1\n");
@ -275,57 +276,49 @@ int main(void) {
bytes_to_hex(keys[i].private_key, SC_PRIVKEY_SIZE, priv_hex[i], sizeof(priv_hex[i]));
}
/* ---------- Ports ---------- */
int base = 41000 + (getpid() % 15000);
int p_ab_a = base++, p_ab_b = base++; // A↔B
int p_ac_a = base++, p_ac_c = base++; // A↔C
int p_bc_b = base++, p_bc_c = base++; // B↔C (C's second server for B)
int p_be_b = base++, p_be_e = base++; // B↔E
int p_cd_c = base++, p_cd_d = base++; // C↔D
/* ---------- Build config strings ---------- */
char cfg_a[2048], cfg_b[2048], cfg_c[2048], cfg_d[2048], cfg_e[2048];
build_node_config(cfg_a, sizeof(cfg_a), &(struct ncfg){
.node_id = g_node_id_a, .tun_ip = "10.100.0.1/24", .my_subnet = "192.168.10.0/24",
.priv_hex = priv_hex[0], .pub_hex = pub_hex[0],
.srv_cnt = 2, .srvs = {{"a_srv_b", p_ab_a}, {"a_srv_c", p_ac_a}},
.srv_cnt = 2, .srvs = {{"a_srv_b"}, {"a_srv_c"}},
.cli_cnt = 2, .clis = {
{"to_b", pub_hex[1], 1, {{"a_srv_b", p_ab_b}}},
{"to_c", pub_hex[2], 1, {{"a_srv_c", p_ac_c}}},
{"to_b", pub_hex[1], 1, {{"a_srv_b"}}},
{"to_c", pub_hex[2], 1, {{"a_srv_c"}}},
}
});
build_node_config(cfg_b, sizeof(cfg_b), &(struct ncfg){
.node_id = g_node_id_b, .tun_ip = "10.100.0.2/24", .my_subnet = "192.168.20.0/24",
.priv_hex = priv_hex[1], .pub_hex = pub_hex[1],
.srv_cnt = 3, .srvs = {{"b_srv", p_ab_b}, {"b_cli_c", p_bc_b}, {"b_cli_e", p_be_b}},
.srv_cnt = 3, .srvs = {{"b_srv"}, {"b_cli_c"}, {"b_cli_e"}},
.cli_cnt = 2, .clis = {
{"to_c", pub_hex[2], 1, {{"b_cli_c", p_bc_c}}},
{"to_e", pub_hex[4], 1, {{"b_cli_e", p_be_e}}},
{"to_c", pub_hex[2], 1, {{"b_cli_c"}}},
{"to_e", pub_hex[4], 1, {{"b_cli_e"}}},
}
});
build_node_config(cfg_c, sizeof(cfg_c), &(struct ncfg){
.node_id = g_node_id_c, .tun_ip = "10.100.0.3/24", .my_subnet = "192.168.30.0/24",
.priv_hex = priv_hex[2], .pub_hex = pub_hex[2],
.srv_cnt = 3, .srvs = {{"c_srv_a", p_ac_c}, {"c_srv_b", p_bc_c}, {"c_cli_d", p_cd_c}},
.srv_cnt = 3, .srvs = {{"c_srv_a"}, {"c_srv_b"}, {"c_cli_d"}},
.cli_cnt = 1, .clis = {
{"to_d", pub_hex[3], 1, {{"c_cli_d", p_cd_d}}},
{"to_d", pub_hex[3], 1, {{"c_cli_d"}}},
}
});
build_node_config(cfg_d, sizeof(cfg_d), &(struct ncfg){
.node_id = g_node_id_d, .tun_ip = "10.100.0.4/24", .my_subnet = "192.168.40.0/24",
.priv_hex = priv_hex[3], .pub_hex = pub_hex[3],
.srv_cnt = 1, .srvs = {{"d_srv", p_cd_d}},
.srv_cnt = 1, .srvs = {{"d_srv"}},
.cli_cnt = 0, .clis = {},
});
build_node_config(cfg_e, sizeof(cfg_e), &(struct ncfg){
.node_id = g_node_id_e, .tun_ip = "10.100.0.5/24", .my_subnet = "192.168.50.0/24",
.priv_hex = priv_hex[4], .pub_hex = pub_hex[4],
.srv_cnt = 1, .srvs = {{"e_srv", p_be_e}},
.srv_cnt = 1, .srvs = {{"e_srv"}},
.cli_cnt = 0, .clis = {},
});
@ -338,6 +331,11 @@ int main(void) {
inst_c = utun_instance_create_from_str(ua, cfg_c); ASSERT(inst_c, "inst_c");
inst_d = utun_instance_create_from_str(ua, cfg_d); ASSERT(inst_d, "inst_d");
inst_e = utun_instance_create_from_str(ua, cfg_e); ASSERT(inst_e, "inst_e");
test_etcp_set_peer(inst_a, "to_b", inst_b, "b_srv");
test_etcp_set_peer(inst_a, "to_c", inst_c, "c_srv_a");
test_etcp_set_peer(inst_b, "to_c", inst_c, "c_srv_b");
test_etcp_set_peer(inst_b, "to_e", inst_e, "e_srv");
test_etcp_set_peer(inst_c, "to_d", inst_d, "d_srv");
ASSERT(utun_instance_init(inst_a) == 0, "init a");
ASSERT(utun_instance_init(inst_b) == 0, "init b");

Loading…
Cancel
Save