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

354 lines
8.6 KiB
C

/*
* test_filexfer.c -- tests for the local file transfer (T5.6)
*
* 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/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)
struct T {
DcLoop *loop;
DcLocalConn *client;
DcFileSend *send;
DcFileRecv *recv;
int srv_ready;
int cli_ready;
int cli_ok;
int cli_closed;
int accepted;
int done;
int done_ok;
int cancelled;
int rejected;
char dir[512];
char err[256];
};
static void
srv_hello(DcLocalConn *conn, const DcJson *hello, void *arg)
{
struct T *t = arg;
(void)conn;
(void)hello;
t->srv_ready = 1;
}
static void
srv_line(DcLocalConn *conn, const DcJson *json, void *arg)
{
struct T *t = arg;
const char *type = dc_json_string(dc_json_get(json, "type"));
const char *id = dc_json_string(dc_json_get(json, "id"));
if (type == NULL)
return;
if (strcmp(type, "file-offer") == 0) {
t->recv = dc_file_recv_new(t->dir, json, t->err,
sizeof(t->err));
if (t->recv == NULL) {
DcJson *rej = dc_json_new_object();
(void)dc_json_object_set(rej, "type",
dc_json_new_string("file-reject"));
(void)dc_json_object_set(rej, "id",
dc_json_new_string(id != NULL ? id : ""));
(void)dc_json_object_set(rej, "reason",
dc_json_new_string(t->err));
(void)dc_local_conn_send_json(conn, rej);
dc_json_free(rej);
return;
}
{
DcJson *acc = dc_json_new_object();
(void)dc_json_object_set(acc, "type",
dc_json_new_string("file-accept"));
(void)dc_json_object_set(acc, "id",
dc_json_new_string(id != NULL ? id : ""));
(void)dc_local_conn_send_json(conn, acc);
dc_json_free(acc);
}
} else if (strcmp(type, "file-data") == 0) {
const char *data = dc_json_string(dc_json_get(json, "data"));
if (t->recv != NULL)
(void)dc_file_recv_data(t->recv, data, t->err,
sizeof(t->err));
} else if (strcmp(type, "file-end") == 0) {
DcJson *done = dc_json_new_object();
int ok = t->recv != NULL &&
dc_file_recv_end(t->recv, t->err, sizeof(t->err)) == 0;
(void)dc_json_object_set(done, "type",
dc_json_new_string("file-done"));
(void)dc_json_object_set(done, "id",
dc_json_new_string(id != NULL ? id : ""));
(void)dc_json_object_set(done, "ok", dc_json_new_bool(ok));
(void)dc_local_conn_send_json(conn, done);
dc_json_free(done);
t->done = 1;
t->done_ok = ok;
} else if (strcmp(type, "file-cancel") == 0) {
dc_file_recv_cancel(t->recv);
t->recv = NULL;
t->cancelled = 1;
}
}
static void
cli_line(DcLocalConn *conn, const DcJson *json, void *arg)
{
struct T *t = arg;
const char *type = dc_json_string(dc_json_get(json, "type"));
(void)conn;
if (type == NULL)
return;
if (strcmp(type, "file-accept") == 0) {
t->accepted = 1;
dc_file_send_start(t->send);
} else if (strcmp(type, "file-reject") == 0) {
t->rejected = 1;
} else if (strcmp(type, "file-done") == 0) {
t->done = 1;
t->done_ok = dc_json_bool(dc_json_get(json, "ok"));
}
}
static void
cli_ready(DcLocalConn *conn, int ok, void *arg)
{
struct T *t = arg;
(void)conn;
t->cli_ready = 1;
t->cli_ok = ok;
}
static void
cli_closed(DcLocalConn *conn, void *arg)
{
struct T *t = arg;
(void)conn;
t->cli_closed = 1;
}
static void
fill_file(const char *path, long long size)
{
FILE *fp = fopen(path, "wb");
unsigned int x = 12345;
long long i;
for (i = 0; i < size; i++) {
x = x * 1103515245u + 12345u;
fputc((int)((x >> 16) & 0xff), fp);
}
fclose(fp);
}
static int
run_until(DcLoop *loop, int *flag, int ms)
{
long long deadline = dc_loop_now_ms() + ms;
while (!*flag && dc_loop_now_ms() < deadline)
(void)dc_loop_run(loop, 10);
return *flag;
}
static void
test_names(void)
{
char *name;
name = dc_file_safe_name("../../etc/passwd");
CHECK(name != NULL && strcmp(name, "passwd") == 0);
free(name);
name = dc_file_safe_name("a/b\\c.txt");
CHECK(name != NULL && strcmp(name, "c.txt") == 0);
free(name);
name = dc_file_safe_name("..");
CHECK(name == NULL);
name = dc_file_safe_name("");
CHECK(name == NULL);
name = dc_file_safe_name("bad\x01name");
CHECK(name != NULL && strcmp(name, "bad_name") == 0);
free(name);
}
int
main(void)
{
char tmp[64];
char dir[512];
char src[600];
char src2[600];
char sha_src[65];
char sha_recv[65];
char base1[64];
char base2[64];
char id1[600], id2[600];
DcIdentity *a;
DcIdentity *b;
struct T t;
DcLocalServer *server;
long long size = 1024 * 1024 + 12345;
long long deadline;
int steps = 0;
memset(&t, 0, sizeof(t));
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);
fill_file(src, size);
CHECK(dc_file_sha256(src, sha_src, NULL) == 0);
CHECK(dc_file_safe_name("../../escape.bin") != NULL);
free(dc_file_safe_name("../../escape.bin"));
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);
b = dc_identity_load_or_create(id2);
CHECK(a != NULL && b != NULL);
if (a == NULL || b == NULL)
return 1;
t.loop = dc_loop_new();
CHECK(t.loop != NULL);
server = dc_local_server_new(t.loop, b, "127.0.0.1", 0,
dc_identity_public_b64(a), dc_identity_pin(a), srv_hello, srv_line,
NULL, &t);
CHECK(server != NULL);
if (server == NULL)
return 1;
t.client = dc_local_connect(t.loop, a, "127.0.0.1",
dc_local_server_port(server), dc_identity_pin(b), cli_line,
cli_ready, cli_closed, &t);
CHECK(t.client != NULL);
if (t.client == NULL)
return 1;
CHECK(run_until(t.loop, &t.cli_ready, 5000));
CHECK(t.cli_ok == 1);
CHECK(run_until(t.loop, &t.srv_ready, 2000));
t.send = dc_file_send_new(t.client, "f1", src, t.err, sizeof(t.err));
CHECK(t.send != NULL);
if (t.send == NULL) {
fprintf(stderr, "send_new: %s\n", t.err);
return 1;
}
CHECK(run_until(t.loop, &t.accepted, 3000));
deadline = dc_loop_now_ms() + 15000;
while (deadline > dc_loop_now_ms() && !t.done) {
int rc = dc_file_send_step(t.send);
if (rc < 0) {
CHECK(0);
break;
}
if (rc == 1)
break;
steps++;
(void)dc_loop_run(t.loop, 5);
}
CHECK(steps > 1); /* the 1 MiB file needs several chunks */
CHECK(run_until(t.loop, &t.done, 5000));
CHECK(t.done_ok == 1);
CHECK(t.recv != NULL && dc_file_recv_path(t.recv) != NULL);
if (t.recv != NULL && dc_file_recv_path(t.recv) != NULL) {
CHECK(strstr(dc_file_recv_path(t.recv), "source.bin") != NULL);
CHECK(dc_file_sha256(dc_file_recv_path(t.recv), sha_recv,
NULL) == 0);
CHECK(strcmp(sha_src, sha_recv) == 0);
}
dc_file_send_free(t.send);
t.send = NULL;
if (t.recv != NULL) {
dc_file_recv_cancel(t.recv);
t.recv = NULL;
}
/* a file above 512 MiB is refused before anything is written */
{
DcJson *offer = dc_json_new_object();
char sha[65];
memset(sha, 'a', 64);
sha[64] = '\0';
(void)dc_json_object_set(offer, "type",
dc_json_new_string("file-offer"));
(void)dc_json_object_set(offer, "id",
dc_json_new_string("big"));
(void)dc_json_object_set(offer, "name",
dc_json_new_string("big.bin"));
(void)dc_json_object_set(offer, "size",
dc_json_new_int(DC_FILE_MAX + 1));
(void)dc_json_object_set(offer, "sha256",
dc_json_new_string(sha));
CHECK(dc_file_recv_new(dir, offer, t.err,
sizeof(t.err)) == NULL);
dc_json_free(offer);
}
/* cancel: the receiver drops the part file */
snprintf(src2, sizeof(src2), "%s/cancel.bin", tmp);
fill_file(src2, 300000);
t.done = 0;
t.done_ok = 0;
t.cancelled = 0;
t.accepted = 0;
t.send = dc_file_send_new(t.client, "f2", src2, t.err, sizeof(t.err));
CHECK(t.send != NULL);
CHECK(run_until(t.loop, &t.accepted, 3000));
(void)dc_file_send_step(t.send);
(void)dc_loop_run(t.loop, 20);
dc_file_send_cancel(t.send, "user");
dc_file_send_free(t.send);
t.send = NULL;
CHECK(run_until(t.loop, &t.cancelled, 3000));
{
char part[700];
snprintf(part, sizeof(part), "%s/cancel.bin.part", dir);
CHECK(access(part, F_OK) != 0);
snprintf(part, sizeof(part), "%s/cancel.bin", dir);
CHECK(access(part, F_OK) != 0);
}
dc_file_recv_cancel(t.recv);
t.recv = NULL;
test_names();
if (!t.cli_closed)
dc_local_conn_close(t.client);
dc_local_server_free(server);
dc_loop_free(t.loop);
dc_identity_free(a);
dc_identity_free(b);
if (fails != 0) {
fprintf(stderr, "%d test(s) failed\n", fails);
return 1;
}
printf("test_filexfer: all ok\n");
return 0;
}