Files
mcde/lib/libDtChat/test_local.c
T
lotherk 134cacc93d dtchat: bind local identities to their keys and gate files on pairing (T10.2)
Security review findings: (1) the local peer identity was only a claimed pin; the listener now requests the client certificate and compares the certificate hash that completed the handshake with the pin in hello, refusing a mismatch or a missing certificate, and the client presents its identity certificate (test_local uses real identities: matching, other identity, any). (2) unpaired peers could send file offers; the UI manager carries a paired flag, answers offers from unpaired peers with file-reject 'pairing required' and refuses to send. The review document lists both fixes and the accepted limits (no passphrase, public pins, media nonces later, adapter spawn, file names, QR, parser limits). Standalone: test_local, test_filexfer, tests/p2p.sh and the UI pairing/message run pass with the changes. The in-tree build is pending because another release chain holds the lock (pid 4040149).
2026-11-17 06:49:35 +00:00

531 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)
{
snprintf(base, baselen, "/tmp/opencode/dtchat-local-XXXXXX");
if (mkdtemp(base) == NULL)
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[] = "/tmp/opencode/dtchat-local-c-XXXXXX";
char base_o[] = "/tmp/opencode/dtchat-local-o-XXXXXX";
char dir_c[512], dir_o[512];
DcIdentity *client;
DcIdentity *other;
signal(SIGPIPE, SIG_IGN);
CHECK(mkdtemp(base_c) != NULL);
CHECK(mkdtemp(base_o) != NULL);
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;
}