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.
166 lines
3.7 KiB
C
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[64];
|
|
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 (dc_test_tmpdir(tmpdir, sizeof tmpdir, "inst") != 0) {
|
|
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;
|
|
}
|