Two defects in the test suite, both of the kind that fails on another
machine but not on the one it was written on.
1. Seven tests put their scratch directories below /tmp/opencode --
a directory of the environment they were written in, not of the
system. mkdtemp() returns NULL when the parent is missing, so on a
machine without /tmp/opencode every one of them failed with a "NULL"
check or perror("mkdtemp"), and nothing was wrong with the code under
test. dc_test_tmpdir() (dc_internal.h, instance.c) now uses $TMPDIR,
or /tmp when it is unset or not absolute.
Measured: after renaming /tmp/opencode away (sudo mv), make check in
lib/libDtChat is 24/24; before the change the same run failed on
test_identity, test_pair, test_store, test_instance, test_filexfer,
test_local and test_i18n.
2. test_i18n takes <de.msg> <en.msg> but TESTS runs it without arguments
(f46a914c added it to TESTS without a runner), so `make check' always
reported "FAIL: test_i18n" with the usage line -- measured 2026-11-22
on the merge build, and identically on build/mcde-host, so not a
regression of that merge. tools/mcde-tests.sh names the files and
passes; the test now falls back to $srcdir (which automake's test
driver exports) and finds them itself, the arguments still winning.
Test: make check in lib/libDtChat -> 24/24 PASS (was 23/24). Both call
forms of test_i18n give "test_i18n: all ok": with the two arguments and
without them. With /tmp/opencode renamed away the full check is green as
well, and the seven scratch directories appear under $TMPDIR when set.
530 lines
13 KiB
C
530 lines
13 KiB
C
/*
|
|
* test_local.c -- tests for the local TLS listener, client and protocol
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*/
|
|
#include "dc_internal.h"
|
|
|
|
#include <poll.h>
|
|
#include <pthread.h>
|
|
#include <signal.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <unistd.h>
|
|
|
|
#include <tls.h>
|
|
|
|
static int fails;
|
|
|
|
#define CHECK(cond) do {\
|
|
if (!(cond)) {\
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__,\
|
|
#cond);\
|
|
fails++;\
|
|
}\
|
|
} while (0)
|
|
|
|
static int
|
|
mkidentity(char *base, size_t baselen, char *dir, size_t dirlen,
|
|
DcIdentity **out)
|
|
{
|
|
if (dc_test_tmpdir(base, baselen, "local") != 0)
|
|
return -1;
|
|
snprintf(dir, dirlen, "%s/state", base);
|
|
*out = dc_identity_load_or_create(dir);
|
|
return *out != NULL ? 0 : -1;
|
|
}
|
|
|
|
/* -------------------- T5.4: the blocking TLS client --------------------- */
|
|
|
|
struct ClientArg {
|
|
DcIdentity *identity;
|
|
int port;
|
|
int done;
|
|
int ack_ok;
|
|
int ack_seen;
|
|
};
|
|
|
|
static void *
|
|
client_thread(void *arg)
|
|
{
|
|
struct ClientArg *c = arg;
|
|
struct tls_config *cfg;
|
|
struct tls *ctx;
|
|
char port[16];
|
|
char hello[512];
|
|
size_t off = 0;
|
|
int fd = -1;
|
|
|
|
snprintf(port, sizeof(port), "%d", c->port);
|
|
snprintf(hello, sizeof(hello),
|
|
"{\"type\":\"hello\",\"id\":\"%s\",\"pin\":\"%s\"}\n",
|
|
dc_identity_public_b64(c->identity),
|
|
dc_identity_pin(c->identity));
|
|
if (tls_init() != 0)
|
|
goto out;
|
|
cfg = tls_config_new();
|
|
if (cfg == NULL)
|
|
goto out;
|
|
tls_config_insecure_noverifycert(cfg);
|
|
tls_config_insecure_noverifyname(cfg);
|
|
if (tls_config_set_cert_mem(cfg,
|
|
(const uint8_t *)dc_identity_cert_pem(c->identity),
|
|
strlen(dc_identity_cert_pem(c->identity))) != 0 ||
|
|
tls_config_set_key_mem(cfg,
|
|
(const uint8_t *)dc_identity_key_pem(c->identity),
|
|
strlen(dc_identity_key_pem(c->identity))) != 0) {
|
|
tls_config_free(cfg);
|
|
goto out;
|
|
}
|
|
ctx = tls_client();
|
|
if (ctx == NULL || tls_configure(ctx, cfg) != 0) {
|
|
tls_config_free(cfg);
|
|
goto out;
|
|
}
|
|
tls_config_free(cfg);
|
|
{
|
|
struct sockaddr_in addr;
|
|
|
|
fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (fd < 0) {
|
|
tls_free(ctx);
|
|
goto out;
|
|
}
|
|
memset(&addr, 0, sizeof(addr));
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_port = htons((uint16_t)c->port);
|
|
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
|
|
close(fd);
|
|
fd = -1;
|
|
tls_free(ctx);
|
|
goto out;
|
|
}
|
|
if (tls_connect_socket(ctx, fd, "127.0.0.1") != 0 ||
|
|
tls_handshake(ctx) != 0) {
|
|
close(fd);
|
|
fd = -1;
|
|
tls_free(ctx);
|
|
goto out;
|
|
}
|
|
}
|
|
while (off < strlen(hello)) {
|
|
ssize_t n = tls_write(ctx, hello + off, strlen(hello) - off);
|
|
|
|
if (n == TLS_WANT_POLLIN || n == TLS_WANT_POLLOUT) {
|
|
struct pollfd p;
|
|
|
|
p.fd = fd;
|
|
p.events = n == TLS_WANT_POLLIN ? POLLIN : POLLOUT;
|
|
p.revents = 0;
|
|
if (poll(&p, 1, 5000) <= 0)
|
|
break;
|
|
continue;
|
|
}
|
|
if (n <= 0)
|
|
break;
|
|
off += (size_t)n;
|
|
}
|
|
{
|
|
char line[1024];
|
|
size_t used = 0;
|
|
|
|
while (used < sizeof(line) - 1) {
|
|
ssize_t n = tls_read(ctx, line + used,
|
|
sizeof(line) - 1 - used);
|
|
DcJson *ack;
|
|
char *nl;
|
|
|
|
if (n == TLS_WANT_POLLIN || n == TLS_WANT_POLLOUT) {
|
|
struct pollfd p;
|
|
|
|
p.fd = fd;
|
|
p.events = n == TLS_WANT_POLLIN ?
|
|
POLLIN : POLLOUT;
|
|
p.revents = 0;
|
|
if (poll(&p, 1, 5000) <= 0)
|
|
break;
|
|
continue;
|
|
}
|
|
if (n <= 0)
|
|
break;
|
|
used += (size_t)n;
|
|
line[used] = '\0';
|
|
nl = strchr(line, '\n');
|
|
if (nl == NULL)
|
|
continue;
|
|
*nl = '\0';
|
|
ack = dc_json_parse(line, strlen(line), NULL, 0);
|
|
if (ack != NULL) {
|
|
c->ack_seen = 1;
|
|
c->ack_ok = dc_json_bool(dc_json_get(ack,
|
|
"ok"));
|
|
dc_json_free(ack);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
(void)tls_close(ctx);
|
|
tls_free(ctx);
|
|
if (fd >= 0)
|
|
close(fd);
|
|
out:
|
|
c->done = 1;
|
|
return NULL;
|
|
}
|
|
|
|
struct ServerCtx {
|
|
int called;
|
|
char id[128];
|
|
char pin[128];
|
|
};
|
|
|
|
static void
|
|
hello_cb(DcLocalConn *conn, const DcJson *hello, void *arg)
|
|
{
|
|
struct ServerCtx *ctx = arg;
|
|
|
|
ctx->called++;
|
|
if (hello != NULL) {
|
|
const char *id = dc_json_string(dc_json_get(hello, "id"));
|
|
const char *pin = dc_json_string(dc_json_get(hello, "pin"));
|
|
|
|
if (id != NULL)
|
|
snprintf(ctx->id, sizeof(ctx->id), "%s", id);
|
|
if (pin != NULL)
|
|
snprintf(ctx->pin, sizeof(ctx->pin), "%s", pin);
|
|
}
|
|
(void)conn;
|
|
}
|
|
|
|
static void
|
|
run_case(DcIdentity *client, const char *expect_id, const char *expect_pin,
|
|
int expect_ok, int expect_cb)
|
|
{
|
|
char base[256], dir[512];
|
|
DcIdentity *identity;
|
|
DcLoop *loop;
|
|
DcLocalServer *server;
|
|
struct ServerCtx ctx;
|
|
struct ClientArg carg;
|
|
pthread_t thread;
|
|
long long deadline;
|
|
|
|
memset(&ctx, 0, sizeof(ctx));
|
|
memset(&carg, 0, sizeof(carg));
|
|
CHECK(mkidentity(base, sizeof(base), dir, sizeof(dir), &identity) == 0);
|
|
if (identity == NULL)
|
|
return;
|
|
loop = dc_loop_new();
|
|
CHECK(loop != NULL);
|
|
server = dc_local_server_new(loop, identity, "127.0.0.1", 0, expect_id,
|
|
expect_pin, hello_cb, NULL, NULL, &ctx);
|
|
CHECK(server != NULL);
|
|
if (server == NULL)
|
|
return;
|
|
carg.identity = client;
|
|
carg.port = dc_local_server_port(server);
|
|
CHECK(pthread_create(&thread, NULL, client_thread, &carg) == 0);
|
|
deadline = dc_loop_now_ms() + 5000;
|
|
while (!carg.done && dc_loop_now_ms() < deadline)
|
|
(void)dc_loop_run(loop, 50);
|
|
pthread_join(thread, NULL);
|
|
CHECK(carg.ack_seen);
|
|
CHECK(carg.ack_ok == expect_ok);
|
|
if (expect_cb) {
|
|
CHECK(ctx.called == 1);
|
|
CHECK(strcmp(ctx.id, dc_identity_public_b64(client)) == 0);
|
|
CHECK(strcmp(ctx.pin, dc_identity_pin(client)) == 0);
|
|
} else {
|
|
CHECK(ctx.called == 0);
|
|
}
|
|
dc_local_server_free(server);
|
|
dc_loop_free(loop);
|
|
dc_identity_free(identity);
|
|
}
|
|
|
|
/* ----------------------- T5.5: the local protocol ----------------------- */
|
|
|
|
struct Protocol {
|
|
int server_ready;
|
|
int client_ready;
|
|
int client_ok;
|
|
int server_msg;
|
|
int client_ack;
|
|
int server_typing;
|
|
int client_presence;
|
|
int client_closed;
|
|
char msg[128];
|
|
char ack_id[64];
|
|
};
|
|
|
|
static void
|
|
server_hello(DcLocalConn *conn, const DcJson *hello, void *arg)
|
|
{
|
|
struct Protocol *p = arg;
|
|
|
|
(void)conn;
|
|
(void)hello;
|
|
p->server_ready = 1;
|
|
}
|
|
|
|
static void
|
|
server_line(DcLocalConn *conn, const DcJson *json, void *arg)
|
|
{
|
|
struct Protocol *p = arg;
|
|
const char *type = dc_json_string(dc_json_get(json, "type"));
|
|
|
|
if (type == NULL)
|
|
return;
|
|
if (strcmp(type, "msg") == 0) {
|
|
const char *text = dc_json_string(dc_json_get(json, "text"));
|
|
|
|
p->server_msg++;
|
|
if (text != NULL)
|
|
snprintf(p->msg, sizeof(p->msg), "%s", text);
|
|
(void)dc_local_send_msg_ack(conn, "c1");
|
|
} else if (strcmp(type, "typing") == 0) {
|
|
p->server_typing = dc_json_bool(dc_json_get(json, "on"));
|
|
}
|
|
}
|
|
|
|
static void
|
|
client_line(DcLocalConn *conn, const DcJson *json, void *arg)
|
|
{
|
|
struct Protocol *p = arg;
|
|
const char *type = dc_json_string(dc_json_get(json, "type"));
|
|
|
|
(void)conn;
|
|
if (type == NULL)
|
|
return;
|
|
if (strcmp(type, "msg-ack") == 0) {
|
|
const char *id = dc_json_string(dc_json_get(json, "client_id"));
|
|
|
|
p->client_ack++;
|
|
if (id != NULL)
|
|
snprintf(p->ack_id, sizeof(p->ack_id), "%s", id);
|
|
} else if (strcmp(type, "presence") == 0) {
|
|
p->client_presence++;
|
|
}
|
|
}
|
|
|
|
static void
|
|
client_closed(DcLocalConn *conn, void *arg)
|
|
{
|
|
struct Protocol *p = arg;
|
|
|
|
(void)conn;
|
|
p->client_closed = 1;
|
|
}
|
|
|
|
static void
|
|
client_ready(DcLocalConn *conn, int ok, void *arg)
|
|
{
|
|
struct Protocol *p = arg;
|
|
|
|
(void)conn;
|
|
p->client_ready = 1;
|
|
p->client_ok = ok;
|
|
}
|
|
|
|
static void
|
|
test_protocol(void)
|
|
{
|
|
char base1[256], base2[256], dir1[512], dir2[512];
|
|
DcIdentity *a;
|
|
DcIdentity *b;
|
|
DcLoop *loop;
|
|
DcLocalServer *server;
|
|
DcLocalConn *client;
|
|
struct Protocol p;
|
|
long long deadline;
|
|
|
|
memset(&p, 0, sizeof(p));
|
|
CHECK(mkidentity(base1, sizeof(base1), dir1, sizeof(dir1), &a) == 0);
|
|
CHECK(mkidentity(base2, sizeof(base2), dir2, sizeof(dir2), &b) == 0);
|
|
if (a == NULL || b == NULL)
|
|
return;
|
|
loop = dc_loop_new();
|
|
CHECK(loop != NULL);
|
|
server = dc_local_server_new(loop, b, "127.0.0.1", 0,
|
|
dc_identity_public_b64(a), dc_identity_pin(a), server_hello,
|
|
server_line, NULL, &p);
|
|
CHECK(server != NULL);
|
|
if (server == NULL)
|
|
return;
|
|
client = dc_local_connect(loop, a, "127.0.0.1",
|
|
dc_local_server_port(server), dc_identity_pin(b), client_line,
|
|
client_ready, client_closed, &p);
|
|
CHECK(client != NULL);
|
|
if (client == NULL)
|
|
return;
|
|
deadline = dc_loop_now_ms() + 5000;
|
|
while ((!p.server_ready || !p.client_ready) &&
|
|
dc_loop_now_ms() < deadline)
|
|
(void)dc_loop_run(loop, 20);
|
|
CHECK(p.server_ready == 1);
|
|
CHECK(p.client_ready == 1);
|
|
CHECK(p.client_ok == 1);
|
|
CHECK(client != NULL);
|
|
|
|
CHECK(dc_local_send_msg(client, "hello peer", "c1") == 0);
|
|
deadline = dc_loop_now_ms() + 3000;
|
|
while (p.server_msg == 0 && dc_loop_now_ms() < deadline)
|
|
(void)dc_loop_run(loop, 20);
|
|
CHECK(p.server_msg == 1);
|
|
CHECK(strcmp(p.msg, "hello peer") == 0);
|
|
|
|
deadline = dc_loop_now_ms() + 3000;
|
|
while (p.client_ack == 0 && dc_loop_now_ms() < deadline)
|
|
(void)dc_loop_run(loop, 20);
|
|
CHECK(p.client_ack == 1);
|
|
CHECK(strcmp(p.ack_id, "c1") == 0);
|
|
|
|
CHECK(dc_local_send_typing(client, 1) == 0);
|
|
deadline = dc_loop_now_ms() + 3000;
|
|
while (p.server_typing == 0 && dc_loop_now_ms() < deadline)
|
|
(void)dc_loop_run(loop, 20);
|
|
CHECK(p.server_typing == 1);
|
|
|
|
CHECK(dc_local_send_presence(client, "online", "here") == 0);
|
|
deadline = dc_loop_now_ms() + 3000;
|
|
while (p.client_presence == 0 && dc_loop_now_ms() < deadline)
|
|
(void)dc_loop_run(loop, 20);
|
|
/* presence goes to the server, not back to the client */
|
|
CHECK(p.client_presence == 0);
|
|
|
|
if (!p.client_closed)
|
|
dc_local_conn_close(client);
|
|
dc_local_server_free(server);
|
|
dc_loop_free(loop);
|
|
dc_identity_free(a);
|
|
dc_identity_free(b);
|
|
}
|
|
|
|
/* ------------------------- T5.5: the reconnect -------------------------- */
|
|
|
|
struct Link {
|
|
DcLoop *loop;
|
|
int online;
|
|
int offline;
|
|
int transitions;
|
|
};
|
|
|
|
static void
|
|
link_state(DcLocalPeer *peer, int online, void *arg)
|
|
{
|
|
struct Link *l = arg;
|
|
|
|
(void)peer;
|
|
if (online) {
|
|
l->online++;
|
|
} else {
|
|
l->offline++;
|
|
}
|
|
l->transitions++;
|
|
}
|
|
|
|
static void
|
|
test_reconnect(void)
|
|
{
|
|
char base1[256], base2[256], dir1[512], dir2[512];
|
|
DcIdentity *a;
|
|
DcIdentity *b;
|
|
DcLoop *loop;
|
|
DcLocalServer *server;
|
|
DcLocalPeer *peer;
|
|
struct Link link;
|
|
int port;
|
|
long long deadline;
|
|
|
|
memset(&link, 0, sizeof(link));
|
|
CHECK(mkidentity(base1, sizeof(base1), dir1, sizeof(dir1), &a) == 0);
|
|
CHECK(mkidentity(base2, sizeof(base2), dir2, sizeof(dir2), &b) == 0);
|
|
if (a == NULL || b == NULL)
|
|
return;
|
|
loop = dc_loop_new();
|
|
CHECK(loop != NULL);
|
|
link.loop = loop;
|
|
server = dc_local_server_new(loop, b, "127.0.0.1", 0,
|
|
dc_identity_public_b64(a), dc_identity_pin(a), NULL, NULL, NULL,
|
|
NULL);
|
|
CHECK(server != NULL);
|
|
if (server == NULL)
|
|
return;
|
|
port = dc_local_server_port(server);
|
|
peer = dc_local_peer_new(loop, a, "127.0.0.1", port,
|
|
dc_identity_pin(b), NULL, link_state, &link);
|
|
CHECK(peer != NULL);
|
|
if (peer == NULL)
|
|
return;
|
|
(void)dc_local_peer_start(peer);
|
|
deadline = dc_loop_now_ms() + 5000;
|
|
while (!dc_local_peer_online(peer) && dc_loop_now_ms() < deadline)
|
|
(void)dc_loop_run(loop, 20);
|
|
CHECK(dc_local_peer_online(peer) == 1);
|
|
CHECK(link.online == 1);
|
|
|
|
/* the server goes away: offline, then a retry succeeds */
|
|
dc_local_server_free(server);
|
|
deadline = dc_loop_now_ms() + 2000;
|
|
while (link.offline == 0 && dc_loop_now_ms() < deadline)
|
|
(void)dc_loop_run(loop, 20);
|
|
CHECK(link.offline == 1);
|
|
server = dc_local_server_new(loop, b, "127.0.0.1", port,
|
|
dc_identity_public_b64(a), dc_identity_pin(a), NULL, NULL, NULL,
|
|
NULL);
|
|
CHECK(server != NULL);
|
|
deadline = dc_loop_now_ms() + 5000;
|
|
while (!dc_local_peer_online(peer) && dc_loop_now_ms() < deadline)
|
|
(void)dc_loop_run(loop, 20);
|
|
CHECK(dc_local_peer_online(peer) == 1);
|
|
CHECK(link.online >= 2);
|
|
dc_local_peer_free(peer);
|
|
dc_local_server_free(server);
|
|
dc_loop_free(loop);
|
|
dc_identity_free(a);
|
|
dc_identity_free(b);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
char base_c[64];
|
|
char base_o[64];
|
|
char dir_c[512], dir_o[512];
|
|
DcIdentity *client;
|
|
DcIdentity *other;
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
CHECK(dc_test_tmpdir(base_c, sizeof base_c, "local-c") == 0);
|
|
CHECK(dc_test_tmpdir(base_o, sizeof base_o, "local-o") == 0);
|
|
snprintf(dir_c, sizeof(dir_c), "%s/state", base_c);
|
|
snprintf(dir_o, sizeof(dir_o), "%s/state", base_o);
|
|
client = dc_identity_load_or_create(dir_c);
|
|
other = dc_identity_load_or_create(dir_o);
|
|
CHECK(client != NULL && other != NULL);
|
|
if (client == NULL || other == NULL)
|
|
return 1;
|
|
|
|
run_case(client, dc_identity_public_b64(client),
|
|
dc_identity_pin(client), 1, 1);
|
|
run_case(client, dc_identity_public_b64(client),
|
|
dc_identity_pin(other), 0, 0);
|
|
run_case(client, NULL, NULL, 1, 1);
|
|
test_protocol();
|
|
test_reconnect();
|
|
dc_identity_free(client);
|
|
dc_identity_free(other);
|
|
if (fails != 0) {
|
|
fprintf(stderr, "%d test(s) failed\n", fails);
|
|
return 1;
|
|
}
|
|
printf("test_local: all ok\n");
|
|
return 0;
|
|
}
|