Files
lotherk 4fead8a3dc 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 <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.
2026-11-22 16:11:16 +00:00

318 lines
7.6 KiB
C

/*
* test_store.c -- unit tests for dtchat's atomic store
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*/
#include "dc_internal.h"
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.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)
static void
path_join(char *out, size_t cap, const char *dir, const char *name)
{
if (snprintf(out, cap, "%s/%s", dir, name) >= (int)cap) {
fprintf(stderr, "path too long: %s/%s\n", dir, name);
exit(1);
}
}
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 void
write_file(const char *path, const char *text)
{
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
size_t len = strlen(text);
CHECK(fd >= 0);
if (fd < 0)
return;
CHECK(write(fd, text, len) == (ssize_t)len);
close(fd);
}
static int
count_tmp(const char *dir)
{
DIR *d = opendir(dir);
struct dirent *e;
int n = 0;
if (d == NULL)
return -1;
while ((e = readdir(d)) != NULL) {
if (strstr(e->d_name, ".tmp") != NULL)
n++;
}
closedir(d);
return n;
}
static DcJson *
accounts_json(const char *id)
{
DcJson *root = dc_json_new_object();
DcJson *array = dc_json_new_array();
DcJson *account = dc_json_new_object();
CHECK(root != NULL && array != NULL && account != NULL);
if (root == NULL || array == NULL || account == NULL) {
dc_json_free(root);
dc_json_free(array);
dc_json_free(account);
return NULL;
}
CHECK(dc_json_object_set(account, "id", dc_json_new_string(id)) == 0);
CHECK(dc_json_array_append(array, account) == 0);
CHECK(dc_json_object_set(root, "accounts", array) == 0);
return root;
}
static const char *
accounts_id(const DcJson *json)
{
const DcJson *accounts = dc_json_get(json, "accounts");
const DcJson *account = dc_json_array_get(accounts, 0);
return dc_json_string(dc_json_get(account, "id"));
}
static void
test_store(DcStore *store, const char *dir)
{
char path[512];
char err[256];
DcJson *json;
int missing = -1;
CHECK(dc_store_dir(store) != NULL);
check_mode(dir, 0700);
json = accounts_json("libera");
CHECK(json != NULL);
CHECK(dc_store_save_accounts(store, json) == 0);
dc_json_free(json);
path_join(path, sizeof(path), dir, "accounts.json");
check_mode(path, 0600);
CHECK(count_tmp(dir) == 0);
json = dc_store_load(store, "accounts.json", &missing, err, sizeof(err));
CHECK(json != NULL);
CHECK(missing == 0);
if (json != NULL) {
CHECK(strcmp(accounts_id(json), "libera") == 0);
dc_json_free(json);
}
/* overwrite */
json = accounts_json("oftc");
CHECK(json != NULL);
CHECK(dc_store_save_accounts(store, json) == 0);
dc_json_free(json);
json = dc_store_load_accounts(store);
CHECK(json != NULL);
if (json != NULL) {
CHECK(strcmp(accounts_id(json), "oftc") == 0);
dc_json_free(json);
}
/* secrets */
json = dc_json_new_object();
{
DcJson *secret = dc_json_new_object();
CHECK(dc_json_object_set(secret, "password",
dc_json_new_string("s3cret")) == 0);
CHECK(dc_json_object_set(json, "libera", secret) == 0);
}
CHECK(dc_store_save_secrets(store, json) == 0);
dc_json_free(json);
path_join(path, sizeof(path), dir, "secrets.json");
check_mode(path, 0600);
json = dc_store_load_secrets(store);
CHECK(json != NULL);
if (json != NULL) {
CHECK(strcmp(dc_json_string(dc_json_get(
dc_json_get(json, "libera"), "password")), "s3cret") == 0);
dc_json_free(json);
}
/* identity, and no default before it exists */
CHECK(dc_store_load_identity(store) == NULL);
json = dc_json_new_object();
CHECK(dc_json_object_set(json, "id",
dc_json_new_string("0123456789abcdef")) == 0);
CHECK(dc_json_object_set(json, "name",
dc_json_new_string("Test")) == 0);
CHECK(dc_store_save_identity(store, json) == 0);
dc_json_free(json);
path_join(path, sizeof(path), dir, "identity.json");
check_mode(path, 0600);
json = dc_store_load_identity(store);
CHECK(json != NULL);
if (json != NULL) {
CHECK(strcmp(dc_json_string(dc_json_get(json, "name")),
"Test") == 0);
dc_json_free(json);
}
/* a wrong mode is repaired on load */
path_join(path, sizeof(path), dir, "accounts.json");
CHECK(chmod(path, 0644) == 0);
json = dc_store_load_accounts(store);
CHECK(json != NULL);
dc_json_free(json);
check_mode(path, 0600);
/* a corrupt file is moved to .bad, not parsed */
write_file(path, "{not json");
json = dc_store_load(store, "accounts.json", &missing, err, sizeof(err));
CHECK(json == NULL);
CHECK(missing == 0);
CHECK(err[0] != '\0');
{
char bad[512];
path_join(bad, sizeof(bad), dir, "accounts.json.bad");
CHECK(access(bad, F_OK) == 0);
}
json = dc_store_load_accounts(store);
CHECK(json != NULL);
if (json != NULL) {
CHECK(dc_json_array_size(dc_json_get(json, "accounts")) == 0);
dc_json_free(json);
}
/* missing file */
json = dc_store_load(store, "nope.json", &missing, err, sizeof(err));
CHECK(json == NULL);
CHECK(missing == 1);
/* invalid names */
CHECK(dc_store_load(store, "../x", NULL, NULL, 0) == NULL);
CHECK(dc_store_load(store, "a/b", NULL, NULL, 0) == NULL);
CHECK(dc_store_load(store, "", NULL, NULL, 0) == NULL);
json = accounts_json("x");
CHECK(dc_store_save(store, "../x", json) == -1);
CHECK(dc_store_save(store, "a/b", json) == -1);
dc_json_free(json);
/* a stale temporary file is ignored */
path_join(path, sizeof(path), dir, "accounts.json.tmp");
write_file(path, "stale");
json = dc_store_load_accounts(store);
CHECK(json != NULL);
dc_json_free(json);
CHECK(count_tmp(dir) >= 1);
unlink(path);
CHECK(count_tmp(dir) == 0);
/* a read-only directory refuses the write and leaves nothing */
CHECK(chmod(dir, 0500) == 0);
json = accounts_json("y");
CHECK(dc_store_save_accounts(store, json) == -1);
dc_json_free(json);
CHECK(chmod(dir, 0700) == 0);
CHECK(count_tmp(dir) == 0);
/* the save path is writable again */
json = accounts_json("z");
CHECK(dc_store_save_accounts(store, json) == 0);
dc_json_free(json);
json = dc_store_load_accounts(store);
CHECK(json != NULL);
if (json != NULL) {
CHECK(strcmp(accounts_id(json), "z") == 0);
dc_json_free(json);
}
}
static void
test_default_dir(const char *tmpdir)
{
char want[512];
DcStore *store;
CHECK(setenv("HOME", tmpdir, 1) == 0);
store = dc_store_open(NULL);
CHECK(store != NULL);
if (store != NULL) {
snprintf(want, sizeof(want), "%s/.dt/dtchat", tmpdir);
CHECK(strcmp(dc_store_dir(store), want) == 0);
dc_store_close(store);
}
}
int
main(void)
{
char tmpdir[64];
char dir[512];
char path[512];
DcStore *store;
if (dc_test_tmpdir(tmpdir, sizeof tmpdir, "store") != 0) {
perror("mkdtemp");
return 1;
}
snprintf(dir, sizeof(dir), "%s/state", tmpdir);
store = dc_store_open(dir);
if (store == NULL) {
perror("dc_store_open");
return 1;
}
test_store(store, dir);
dc_store_close(store);
test_default_dir(tmpdir);
/* clean up */
path_join(path, sizeof(path), dir, "accounts.json");
unlink(path);
path_join(path, sizeof(path), dir, "accounts.json.bad");
unlink(path);
path_join(path, sizeof(path), dir, "secrets.json");
unlink(path);
path_join(path, sizeof(path), dir, "identity.json");
unlink(path);
rmdir(dir);
snprintf(path, sizeof(path), "%s/.dt/dtchat", tmpdir);
rmdir(path);
snprintf(path, sizeof(path), "%s/.dt", tmpdir);
rmdir(path);
rmdir(tmpdir);
if (fails != 0) {
fprintf(stderr, "%d test(s) failed\n", fails);
return 1;
}
printf("test_store: all ok\n");
return 0;
}