Files
lotherk 38304c4378 dtchat: add the local peer model (T5.3)
peers.c keeps one peer per identity id: the TXT record (v/id/name/pin/port, ';'-joined) merges by id and marks the peer online; a service that disappears is marked offline, a manual peer stays; blocking is a property that survives announcements. Strings are owned by the peer. test_peers covers announce/merge, port preference, offline per peer, block survival, manual peers and malformed records; built test all ok, make mcde-host exit=0, tools/mcde-tests.sh 26 ok/0 bad.
2026-11-16 19:50:16 +00:00

114 lines
3.2 KiB
C

/*
* test_peers.c -- tests for the local peer model (T5.3)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*/
#include "dc_internal.h"
#include <stdio.h>
#include <string.h>
static int fails;
#define CHECK(cond) do {\
if (!(cond)) {\
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__,\
#cond);\
fails++;\
}\
} while (0)
int
main(void)
{
DcPeers *peers = dc_peers_new();
DcPeer *p;
CHECK(peers != NULL);
if (peers == NULL)
return 1;
CHECK(dc_peers_count(peers) == 0);
CHECK(dc_peers_announce(peers,
"v=1;id=abc;name=Alice;pin=sha256:aa;port=4711",
"192.168.1.5", 0) == 0);
CHECK(dc_peers_count(peers) == 1);
p = dc_peers_find(peers, "abc");
CHECK(p != NULL);
if (p != NULL) {
CHECK(strcmp(dc_peer_id(p), "abc") == 0);
CHECK(strcmp(dc_peer_name(p), "Alice") == 0);
CHECK(strcmp(dc_peer_host(p), "192.168.1.5") == 0);
CHECK(strcmp(dc_peer_pin(p), "sha256:aa") == 0);
CHECK(dc_peer_port(p) == 4711);
CHECK(dc_peer_online(p) == 1);
CHECK(dc_peer_blocked(p) == 0);
CHECK(dc_peer_manual(p) == 0);
}
/* the same id updates instead of duplicating; the explicit port wins */
CHECK(dc_peers_announce(peers, "id=abc;name=Alice 2", "10.0.0.7",
5000) == 0);
CHECK(dc_peers_count(peers) == 1);
p = dc_peers_find(peers, "abc");
CHECK(p != NULL && strcmp(dc_peer_name(p), "Alice 2") == 0);
CHECK(dc_peer_port(p) == 5000);
CHECK(strcmp(dc_peer_host(p), "10.0.0.7") == 0);
CHECK(strcmp(dc_peer_pin(p), "sha256:aa") == 0);
/* a second identity is a second peer */
CHECK(dc_peers_announce(peers, "id=def;name=Bob", "10.0.0.8", 6000) == 0);
CHECK(dc_peers_count(peers) == 2);
/* offline is per peer */
CHECK(dc_peers_offline(peers, "abc") == 0);
p = dc_peers_find(peers, "abc");
CHECK(p != NULL && dc_peer_online(p) == 0);
p = dc_peers_find(peers, "def");
CHECK(p != NULL && dc_peer_online(p) == 1);
CHECK(dc_peers_offline(peers, "nope") == -1);
/* blocking survives an announcement */
p = dc_peers_find(peers, "abc");
dc_peer_set_blocked(p, 1);
CHECK(dc_peers_announce(peers, "id=abc;name=Alice 3", "10.0.0.7",
5000) == 0);
p = dc_peers_find(peers, "abc");
CHECK(p != NULL && dc_peer_blocked(p) == 1);
CHECK(dc_peer_online(p) == 1);
dc_peer_set_blocked(p, 0);
CHECK(dc_peer_blocked(p) == 0);
/* manual peer: no mDNS, stays after the service list changes */
p = dc_peers_add_manual(peers, "manual1", "Carol", "192.168.1.9",
7000, "sha256:bb");
CHECK(p != NULL);
CHECK(dc_peers_count(peers) == 3);
if (p != NULL) {
CHECK(dc_peer_manual(p) == 1);
CHECK(dc_peer_online(p) == 0);
CHECK(strcmp(dc_peer_name(p), "Carol") == 0);
CHECK(dc_peer_port(p) == 7000);
}
CHECK(dc_peers_offline(peers, "manual1") == 0);
p = dc_peers_find(peers, "manual1");
CHECK(p != NULL && dc_peer_online(p) == 0);
/* a record without an id is refused and changes nothing */
CHECK(dc_peers_announce(peers, "name=Nobody;port=1", NULL, 0) == -1);
CHECK(dc_peers_count(peers) == 3);
CHECK(dc_peers_announce(peers, "", NULL, 0) == -1);
CHECK(dc_peers_at(peers, 0) != NULL);
CHECK(dc_peers_at(peers, 3) == NULL);
dc_peers_free(peers);
dc_peers_free(NULL);
if (fails != 0) {
fprintf(stderr, "%d test(s) failed\n", fails);
return 1;
}
printf("test_peers: all ok\n");
return 0;
}