From 4fead8a3dcfbfdf1d0fc3002ff2df97f0d426739 Mon Sep 17 00:00:00 2001 From: lotherk Date: Sun, 22 Nov 2026 16:11:16 +0000 Subject: [PATCH] libDtChat tests: no /tmp/opencode, and test_i18n finds its catalogs 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 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. --- lib/libDtChat/dc_internal.h | 10 ++++++++++ lib/libDtChat/instance.c | 22 ++++++++++++++++++++++ lib/libDtChat/test_filexfer.c | 12 ++++++------ lib/libDtChat/test_i18n.c | 35 ++++++++++++++++++++++++++++------- lib/libDtChat/test_identity.c | 4 ++-- lib/libDtChat/test_instance.c | 4 ++-- lib/libDtChat/test_local.c | 11 +++++------ lib/libDtChat/test_pair.c | 4 ++-- lib/libDtChat/test_store.c | 4 ++-- 9 files changed, 79 insertions(+), 27 deletions(-) diff --git a/lib/libDtChat/dc_internal.h b/lib/libDtChat/dc_internal.h index 50838aee..15257a41 100644 --- a/lib/libDtChat/dc_internal.h +++ b/lib/libDtChat/dc_internal.h @@ -731,4 +731,14 @@ void dc_instance_close(DcInstance *instance); /* Connect to a running primary and send {"cmd":"open","conv":...}. */ int dc_instance_notify(const char *dir, const char *conv); +/* + * The tests' scratch directories. They used to name /tmp/opencode/-XXXXXX + * -- a directory of the environment they were written in, not of the system: + * mkdtemp() returns NULL when its parent is missing, so every test that needs + * a directory failed on a machine that has no /tmp/opencode (measured + * 2026-11-22). This fills "template" with $TMPDIR (or /tmp) and a name + * unique enough for a test run and makes the directory; 0 on success. + */ +int dc_test_tmpdir(char *template, size_t len, const char *name); + #endif /* _DtChat_dc_internal_h */ diff --git a/lib/libDtChat/instance.c b/lib/libDtChat/instance.c index 8c7949ac..1c623766 100644 --- a/lib/libDtChat/instance.c +++ b/lib/libDtChat/instance.c @@ -446,3 +446,25 @@ fail: free(sock_path); return -1; } + +/* ------------------------------------------------------------------ tests */ + +/* + * A scratch directory for the tests: $TMPDIR (or /tmp) plus "name-XXXXXX". + * The tests used to write /tmp/opencode/-XXXXXX, and mkdtemp() returns + * NULL when that parent does not exist -- every test that needs a directory + * then failed on a machine without /tmp/opencode (measured 2026-11-22). + */ +int +dc_test_tmpdir(char *template, size_t len, const char *name) +{ + const char *base = getenv("TMPDIR"); + int n; + + if (base == NULL || base[0] != '/') + base = "/tmp"; + n = snprintf(template, len, "%s/dtchat-%s-XXXXXX", base, name); + if (n < 0 || (size_t)n >= len) + return -1; + return mkdtemp(template) != NULL ? 0 : -1; +} diff --git a/lib/libDtChat/test_filexfer.c b/lib/libDtChat/test_filexfer.c index 21e8638b..877f80b3 100644 --- a/lib/libDtChat/test_filexfer.c +++ b/lib/libDtChat/test_filexfer.c @@ -197,14 +197,14 @@ test_names(void) int main(void) { - char tmp[] = "/tmp/opencode/dtchat-xfer-XXXXXX"; + char tmp[64]; char dir[512]; char src[600]; char src2[600]; char sha_src[65]; char sha_recv[65]; - char base1[] = "/tmp/opencode/dtchat-xfer-a-XXXXXX"; - char base2[] = "/tmp/opencode/dtchat-xfer-b-XXXXXX"; + char base1[64]; + char base2[64]; char id1[600], id2[600]; DcIdentity *a; DcIdentity *b; @@ -215,7 +215,7 @@ main(void) int steps = 0; memset(&t, 0, sizeof(t)); - CHECK(mkdtemp(tmp) != NULL); + CHECK(dc_test_tmpdir(tmp, sizeof tmp, "xfer") == 0); snprintf(dir, sizeof(dir), "%s/dl", tmp); snprintf(t.dir, sizeof(t.dir), "%s", dir); snprintf(src, sizeof(src), "%s/source.bin", tmp); @@ -224,8 +224,8 @@ main(void) CHECK(dc_file_safe_name("../../escape.bin") != NULL); free(dc_file_safe_name("../../escape.bin")); - CHECK(mkdtemp(base1) != NULL); - CHECK(mkdtemp(base2) != NULL); + CHECK(dc_test_tmpdir(base1, sizeof base1, "xfer-a") == 0); + CHECK(dc_test_tmpdir(base2, sizeof base2, "xfer-b") == 0); snprintf(id1, sizeof(id1), "%s/state", base1); snprintf(id2, sizeof(id2), "%s/state", base2); a = dc_identity_load_or_create(id1); diff --git a/lib/libDtChat/test_i18n.c b/lib/libDtChat/test_i18n.c index 0ba8a536..9f06c62a 100644 --- a/lib/libDtChat/test_i18n.c +++ b/lib/libDtChat/test_i18n.c @@ -64,18 +64,39 @@ use_catalog(const char *dir) int main(int argc, char **argv) { - char tmp[] = "/tmp/opencode/dtchat-i18n-XXXXXX"; + char tmp[64]; char de[1024], en[1024], cat[1200]; char *msg_de; char *msg_en; - if (argc < 3) { - fprintf(stderr, "usage: %s \n", argv[0]); - return 2; + /* make check gives no arguments ($srcdir is exported by automake's + * test driver); tools/mcde-tests.sh names the two files. Both the + * in-tree build and a VPATH build find them this way. */ + if (argc >= 3) { + msg_de = argv[1]; + msg_en = argv[2]; + } else { + const char *srcdir = getenv("srcdir"); + static char bde[1024], ben[1024]; + int n; + + if (srcdir == NULL || srcdir[0] == '\0') + srcdir = "."; + n = snprintf(bde, sizeof(bde), "%s/../../programs/" + "localized/de_DE.UTF-8/msg/dtchat.msg", srcdir); + if (n > 0 && (size_t)n < sizeof(bde) && + access(bde, R_OK) == 0) { + msg_de = bde; + snprintf(ben, sizeof(ben), "%s/../../programs/" + "dtchat/dtchat.msg", srcdir); + msg_en = ben; + } else { + fprintf(stderr, "usage: %s \n", + argv[0]); + return 2; + } } - msg_de = argv[1]; - msg_en = argv[2]; - CHECK(mkdtemp(tmp) != NULL); + CHECK(dc_test_tmpdir(tmp, sizeof tmp, "i18n") == 0); snprintf(de, sizeof(de), "%s/de", tmp); snprintf(en, sizeof(en), "%s/en", tmp); CHECK(mkdir(de, 0700) == 0 || 1); diff --git a/lib/libDtChat/test_identity.c b/lib/libDtChat/test_identity.c index 294e0f75..4b6c1746 100644 --- a/lib/libDtChat/test_identity.c +++ b/lib/libDtChat/test_identity.c @@ -35,7 +35,7 @@ mode_is(const char *path, mode_t want) int main(void) { - char base[] = "/tmp/opencode/dtchat-id-XXXXXX"; + char base[64]; char dir[512]; char other[512]; char path[600]; @@ -44,7 +44,7 @@ main(void) char pin[128]; char pub[128]; - CHECK(mkdtemp(base) != NULL); + CHECK(dc_test_tmpdir(base, sizeof base, "id") == 0); snprintf(dir, sizeof(dir), "%s/state", base); snprintf(other, sizeof(other), "%s/other", base); snprintf(path, sizeof(path), "%s/identity.json", dir); diff --git a/lib/libDtChat/test_instance.c b/lib/libDtChat/test_instance.c index 7311c40c..4da7ad55 100644 --- a/lib/libDtChat/test_instance.c +++ b/lib/libDtChat/test_instance.c @@ -80,7 +80,7 @@ raw_connect(const char *path) int main(void) { - char tmpdir[] = "/tmp/opencode/dtchat-inst-XXXXXX"; + char tmpdir[64]; char dir[256]; char run[512]; char lock[1024]; @@ -92,7 +92,7 @@ main(void) struct Opened opened; int fd; - if (mkdtemp(tmpdir) == NULL) { + if (dc_test_tmpdir(tmpdir, sizeof tmpdir, "inst") != 0) { perror("mkdtemp"); return 1; } diff --git a/lib/libDtChat/test_local.c b/lib/libDtChat/test_local.c index 7e18a63e..8607cdb5 100644 --- a/lib/libDtChat/test_local.c +++ b/lib/libDtChat/test_local.c @@ -32,8 +32,7 @@ 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) + if (dc_test_tmpdir(base, baselen, "local") != 0) return -1; snprintf(dir, dirlen, "%s/state", base); *out = dc_identity_load_or_create(dir); @@ -495,15 +494,15 @@ test_reconnect(void) int main(void) { - char base_c[] = "/tmp/opencode/dtchat-local-c-XXXXXX"; - char base_o[] = "/tmp/opencode/dtchat-local-o-XXXXXX"; + char base_c[64]; + char base_o[64]; 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); + 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); diff --git a/lib/libDtChat/test_pair.c b/lib/libDtChat/test_pair.c index 0e988ddf..e5f695fd 100644 --- a/lib/libDtChat/test_pair.c +++ b/lib/libDtChat/test_pair.c @@ -30,7 +30,7 @@ static int fails; int main(void) { - char tmp[] = "/tmp/opencode/dtchat-pair-XXXXXX"; + char tmp[64]; char dir[512]; DcPins *pins; DcPins *again; @@ -56,7 +56,7 @@ main(void) CHECK(dc_pair_sas("nonsense", PIN_B) == NULL); CHECK(dc_pair_sas(PIN_A, NULL) == NULL); - CHECK(mkdtemp(tmp) != NULL); + CHECK(dc_test_tmpdir(tmp, sizeof tmp, "pair") == 0); snprintf(dir, sizeof(dir), "%s/state", tmp); pins = dc_pins_load(dir); CHECK(pins != NULL); diff --git a/lib/libDtChat/test_store.c b/lib/libDtChat/test_store.c index da0562d3..2bd2ab86 100644 --- a/lib/libDtChat/test_store.c +++ b/lib/libDtChat/test_store.c @@ -273,12 +273,12 @@ test_default_dir(const char *tmpdir) int main(void) { - char tmpdir[] = "/tmp/opencode/dtchat-store-XXXXXX"; + char tmpdir[64]; char dir[512]; char path[512]; DcStore *store; - if (mkdtemp(tmpdir) == NULL) { + if (dc_test_tmpdir(tmpdir, sizeof tmpdir, "store") != 0) { perror("mkdtemp"); return 1; }