You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
3.2 KiB
89 lines
3.2 KiB
// test_etcp_stcp.c — integration: etcp_send + etcp_bind over STCP link |
|
#include "../src/stcp_link.h" |
|
#include "../src/etcp_api.h" |
|
#include "../src/etcp.h" |
|
#include "../src/secure_channel.h" |
|
#include "../src/utun_instance.h" |
|
#include "../lib/u_async.h" |
|
#include "../lib/ll_queue.h" |
|
#include "../lib/debug_config.h" |
|
#include "../lib/mem.h" |
|
#include <stdio.h> |
|
#include <string.h> |
|
#include <stdlib.h> |
|
|
|
#define TEST_PORT 25678 |
|
|
|
static int test_failed = 0; |
|
static struct SC_MYKEYS s_keys, c_keys; |
|
|
|
#define TASSERT(cond) do { \ |
|
if (!(cond)) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, " FAIL: %s", #cond); test_failed = 1; return; } \ |
|
} while(0) |
|
|
|
static int g_srv_recv_count = 0; |
|
static uint8_t g_srv_recv_data[256]; |
|
|
|
static void etcp_recv_cb(struct ETCP_CONN *conn, struct ll_entry *entry) { |
|
(void)conn; |
|
g_srv_recv_count++; |
|
if (entry->dgram && entry->len) memcpy(g_srv_recv_data, entry->dgram, entry->len < 256 ? entry->len : 255); |
|
queue_dgram_free(entry); |
|
queue_entry_free(entry); |
|
} |
|
|
|
static struct stcp_link *g_srv_link; |
|
static void on_srv_link_ready(struct stcp_link *link, void *arg) { (void)arg; g_srv_link = link; } |
|
|
|
int main(void) { |
|
debug_config_init(); |
|
debug_set_level(DEBUG_LEVEL_INFO); |
|
debug_set_categories(DEBUG_CATEGORY_GENERAL | DEBUG_CATEGORY_SOCKET | DEBUG_CATEGORY_CRYPTO); |
|
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "=== etcp_send/bind over STCP link ==="); |
|
|
|
TASSERT(sc_generate_keypair(&s_keys) == SC_OK); |
|
TASSERT(sc_generate_keypair(&c_keys) == SC_OK); |
|
struct UASYNC *ua = uasync_create(); TASSERT(ua); |
|
|
|
struct UTUN_INSTANCE inst; memset(&inst, 0, sizeof(inst)); |
|
inst.ua = ua; |
|
TASSERT(etcp_bind(&inst, 0, etcp_recv_cb) == 0); |
|
|
|
struct stcp_link_config cfg = {.ua = ua, .my_keys = &s_keys, .inst = &inst}; |
|
struct stcp_server *ss = stcp_server_listen(&cfg, TEST_PORT, on_srv_link_ready, NULL); |
|
TASSERT(ss); |
|
|
|
struct sockaddr_in raddr; memset(&raddr, 0, sizeof(raddr)); |
|
raddr.sin_family = AF_INET; raddr.sin_port = htons(TEST_PORT); |
|
raddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); |
|
|
|
struct stcp_link_config cli_cfg = {.ua = ua, .my_keys = &c_keys, .inst = &inst, |
|
.peer_pubkey = s_keys.public_key, .remote_addr = (struct sockaddr_storage *)&raddr}; |
|
struct stcp_link *cli_link = stcp_link_connect(&cli_cfg); TASSERT(cli_link); |
|
struct ETCP_CONN *cli_conn = stcp_link_get_etcp_conn(cli_link); |
|
|
|
int ticks = 0; |
|
while ((!g_srv_link || !stcp_link_is_ready(cli_link)) && ticks < 5000) { uasync_poll(ua, 10); ticks++; } |
|
TASSERT(g_srv_link != NULL); |
|
TASSERT(stcp_link_is_ready(cli_link)); |
|
|
|
const char *msg = "hello via etcp_send!"; |
|
struct ll_entry *e = queue_entry_new(0); |
|
e->dgram = u_malloc(strlen(msg) + 1); |
|
strcpy((char *)e->dgram, msg); |
|
e->len = (uint16_t)strlen(msg); |
|
TASSERT(etcp_send(cli_conn, e) == 0); |
|
|
|
ticks = 0; |
|
while (g_srv_recv_count < 1 && ticks < 2000) { uasync_poll(ua, 10); ticks++; } |
|
TASSERT(g_srv_recv_count == 1); |
|
TASSERT(memcmp(g_srv_recv_data, msg, strlen(msg)) == 0); |
|
|
|
stcp_link_close(cli_link); |
|
stcp_link_server_destroy(ss); |
|
uasync_destroy(ua, 1); |
|
|
|
if (test_failed) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "=== FAILED ==="); return 1; } |
|
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "=== PASSED ==="); |
|
return 0; |
|
}
|
|
|