Files
mcde/lib/libDtChat/test_instance.c
T
lotherk 581735f013 dtchat: add the single-instance lock and control socket (T1.6)
instance.c: DIR/run 0700, dtchat.lock 0600 with flock(LOCK_EX|LOCK_NB), primary Unix listener at dtchat.sock 0600, one open request per client with a 2 s timeout, non-primary instance plus dc_instance_notify with connect retries, and socket/lock cleanup on close. test_instance covers primary/secondary, modes, open callback, malformed client, notify after close and re-acquisition. make mcde-host exit=0; all eight dtchat tests 'all ok'; ASan/UBSan clean. SIGPIPE is ignored so a dead local client cannot kill the core.
2026-11-16 17:45:58 +00:00

166 lines
3.7 KiB
C

/*
* test_instance.c -- tests for the single-instance lock and control socket
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*/
#include "dc_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
static int fails;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
#cond); \
fails++; \
} \
} while (0)
struct Opened {
char conv[128];
int calls;
};
static void
on_open(const char *conv, void *arg)
{
struct Opened *opened = arg;
opened->calls++;
snprintf(opened->conv, sizeof(opened->conv), "%s",
conv != NULL ? conv : "");
}
static int
wait_calls(DcLoop *loop, struct Opened *opened, int want, int ms)
{
long long start = dc_loop_now_ms();
while (opened->calls < want && dc_loop_now_ms() - start < ms)
(void)dc_loop_run(loop, 20);
return opened->calls >= want;
}
static void
check_mode(const char *path, mode_t want)
{
struct stat st;
CHECK(stat(path, &st) == 0);
if (stat(path, &st) == 0)
CHECK((st.st_mode & 0777) == want);
}
static int
raw_connect(const char *path)
{
struct sockaddr_un addr;
int fd;
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd < 0)
return -1;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", path);
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
close(fd);
return -1;
}
return fd;
}
int
main(void)
{
char tmpdir[] = "/tmp/opencode/dtchat-inst-XXXXXX";
char dir[256];
char run[512];
char lock[1024];
char sock[1024];
char *socket_path;
DcLoop *loop;
DcInstance *primary;
DcInstance *secondary;
struct Opened opened;
int fd;
if (mkdtemp(tmpdir) == NULL) {
perror("mkdtemp");
return 1;
}
snprintf(dir, sizeof(dir), "%s/state", tmpdir);
snprintf(run, sizeof(run), "%s/run", dir);
snprintf(lock, sizeof(lock), "%s/dtchat.lock", run);
snprintf(sock, sizeof(sock), "%s/dtchat.sock", run);
loop = dc_loop_new();
CHECK(loop != NULL);
if (loop == NULL)
return 1;
memset(&opened, 0, sizeof(opened));
primary = dc_instance_acquire(loop, dir, on_open, &opened);
CHECK(primary != NULL);
CHECK(dc_instance_is_primary(primary) == 1);
socket_path = (char *)dc_instance_socket(primary);
CHECK(socket_path != NULL);
if (socket_path != NULL)
CHECK(strcmp(socket_path, sock) == 0);
check_mode(run, 0700);
check_mode(lock, 0600);
CHECK(access(sock, F_OK) == 0);
CHECK(dc_instance_notify(dir, "#cde") == 0);
CHECK(wait_calls(loop, &opened, 1, 2000));
CHECK(strcmp(opened.conv, "#cde") == 0);
/* a second process gets a non-primary instance */
secondary = dc_instance_acquire(loop, dir, on_open, &opened);
CHECK(secondary != NULL);
CHECK(dc_instance_is_primary(secondary) == 0);
dc_instance_close(secondary);
CHECK(dc_instance_notify(dir, "alice") == 0);
CHECK(wait_calls(loop, &opened, 2, 2000));
CHECK(strcmp(opened.conv, "alice") == 0);
/* a malformed client must not call the callback */
fd = raw_connect(sock);
CHECK(fd >= 0);
if (fd >= 0) {
CHECK(write(fd, "{bad\n", 5) == 5);
close(fd);
(void)dc_loop_run(loop, 100);
CHECK(opened.calls == 2);
}
dc_instance_close(primary);
CHECK(access(sock, F_OK) != 0);
CHECK(dc_instance_notify(dir, "nobody") == -1);
/* the lock was released: a new primary can start */
primary = dc_instance_acquire(loop, dir, on_open, &opened);
CHECK(primary != NULL);
CHECK(dc_instance_is_primary(primary) == 1);
dc_instance_close(primary);
unlink(lock);
rmdir(run);
rmdir(dir);
rmdir(tmpdir);
dc_loop_free(loop);
if (fails != 0) {
fprintf(stderr, "%d test(s) failed\n", fails);
return 1;
}
printf("test_instance: all ok\n");
return 0;
}