Files
mcde/lib/libDtChat/fake_adapter.c
T
lotherk 4774c2c7d8 dtchat: verify adapter files whose hash arrives at the end (T4.5)
Telegram and WhatsApp learn a document's SHA-256 only when the transfer is
finished, so their file-offer carries none and the digest comes with
file-end.  The UI used to verify only the offer hash, which meant those two
paths wrote the bytes unchecked.  accounts_run now takes the offer's hash
when there is one and the file-end's otherwise, hashes the .part file after
the last chunk and renames only on a match; a mismatch still removes the
part file and reports it in the status line and the transcript.

The fake adapter gained the matching modes: latehash offers without a hash
and sends the right one in file-end, badendhash sends a wrong one there.
tests/file_ui.sh drives all four shapes through the real UI under its own
Xvfb with DTCHAT_TEST_FILE_ACCEPT: files and latehash write the verified
hello world, badhash and badendhash are rejected and leave neither the file
nor a .part.  Measured with the built binaries: four ok, exit 0.

Harness entry DtChat/file_ui added; the full run is 53 ok/0 bad
(make mcde-host exit=0, build/logs/mcde-host-2026-11-17-endhash.log).
2026-11-17 13:08:55 +00:00

284 lines
8.0 KiB
C

/*
* fake_adapter.c -- the test adapter for T1.5
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* Speaks the JSON-lines protocol on stdin/stdout. It sends a valid hello,
* answers configure/connect, echoes send/join and exits on disconnect.
* Settings {"mode":"crash"} makes it exit after connect so the manager's
* backoff can be tested; {"mode":"garbage"} sends an invalid line;
* {"mode":"files"} offers one file after going online and answers accept
* with one data chunk and the end ({"mode":"badhash"} offers a wrong hash,
* {"mode":"latehash"}/{"mode":"badendhash"} send the hash only in the
* end, right or wrong).
*/
#include "dc_internal.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
static char mode[32] = "normal";
static void
send_json(DcJson *json)
{
size_t len = 0;
char *text;
if (json == NULL)
return;
text = dc_json_write(json, &len);
if (text != NULL) {
(void)dc_frame_send(STDOUT_FILENO, text, len);
free(text);
}
dc_json_free(json);
}
static void
send_state(const char *state)
{
DcJson *json = dc_json_new_object();
(void)dc_json_object_set(json, "type", dc_json_new_string("state"));
(void)dc_json_object_set(json, "state", dc_json_new_string(state));
send_json(json);
}
static void
send_file_offer(void)
{
DcJson *json = dc_json_new_object();
const char *sha = strcmp(mode, "badhash") == 0 ?
"0000000000000000000000000000000000000000000000000000000000000000" :
"b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9";
int late = strcmp(mode, "latehash") == 0 ||
strcmp(mode, "badendhash") == 0;
(void)dc_json_object_set(json, "type",
dc_json_new_string("file-offer"));
(void)dc_json_object_set(json, "conv", dc_json_new_string("alice"));
(void)dc_json_object_set(json, "id", dc_json_new_string("f1"));
(void)dc_json_object_set(json, "name", dc_json_new_string("note.txt"));
(void)dc_json_object_set(json, "size", dc_json_new_int(11));
if (!late)
(void)dc_json_object_set(json, "sha256",
dc_json_new_string(sha));
send_json(json);
}
static void
send_hello(void)
{
DcJson *hello = dc_json_new_object();
DcJson *caps = dc_json_new_array();
(void)dc_json_array_append(caps, dc_json_new_string("text"));
(void)dc_json_array_append(caps, dc_json_new_string("channels"));
(void)dc_json_object_set(hello, "type", dc_json_new_string("hello"));
(void)dc_json_object_set(hello, "ipc", dc_json_new_int(DC_IPC_VERSION));
(void)dc_json_object_set(hello, "protocol",
dc_json_new_string("fake"));
(void)dc_json_object_set(hello, "version",
dc_json_new_string("1.0"));
(void)dc_json_object_set(hello, "caps", caps);
send_json(hello);
}
static void
handle(DcJson *cmd)
{
const char *type = dc_json_string(dc_json_get(cmd, "type"));
const char *conv;
const char *text;
const char *client_id;
if (type == NULL)
return;
if (strcmp(type, "configure") == 0) {
const char *m = dc_json_string(dc_json_get(
dc_json_get(cmd, "settings"), "mode"));
if (m != NULL) {
snprintf(mode, sizeof(mode), "%s", m);
}
return;
}
if (strcmp(type, "connect") == 0) {
if (strcmp(mode, "crash") == 0)
_exit(1);
if (strcmp(mode, "garbage") == 0) {
(void)write(STDOUT_FILENO, "{bad\n", 5);
_exit(1);
}
send_state("online");
if (strcmp(mode, "files") == 0 ||
strcmp(mode, "badhash") == 0 ||
strcmp(mode, "latehash") == 0 ||
strcmp(mode, "badendhash") == 0)
send_file_offer();
return;
}
if (strcmp(type, "send") == 0) {
DcJson *sent = dc_json_new_object();
DcJson *message = dc_json_new_object();
conv = dc_json_string(dc_json_get(cmd, "conv"));
text = dc_json_string(dc_json_get(cmd, "text"));
client_id = dc_json_string(dc_json_get(cmd, "client_id"));
(void)dc_json_object_set(sent, "type",
dc_json_new_string("sent"));
(void)dc_json_object_set(sent, "conv",
dc_json_new_string(conv));
if (client_id != NULL)
(void)dc_json_object_set(sent, "client_id",
dc_json_new_string(client_id));
(void)dc_json_object_set(sent, "ts",
dc_json_new_int((long long)time(NULL)));
send_json(sent);
(void)dc_json_object_set(message, "type",
dc_json_new_string("message"));
(void)dc_json_object_set(message, "conv",
dc_json_new_string(conv));
(void)dc_json_object_set(message, "from",
dc_json_new_string("fake"));
(void)dc_json_object_set(message, "text",
dc_json_new_string(text != NULL ? text : ""));
(void)dc_json_object_set(message, "ts",
dc_json_new_int((long long)time(NULL)));
send_json(message);
return;
}
if (strcmp(type, "join") == 0) {
DcJson *conversation = dc_json_new_object();
conv = dc_json_string(dc_json_get(cmd, "conv"));
(void)dc_json_object_set(conversation, "type",
dc_json_new_string("conversation"));
(void)dc_json_object_set(conversation, "conv",
dc_json_new_string(conv));
(void)dc_json_object_set(conversation, "kind",
dc_json_new_string("channel"));
(void)dc_json_object_set(conversation, "title",
dc_json_new_string(conv));
send_json(conversation);
return;
}
if (strcmp(type, "file-send") == 0) {
DcJson *json = dc_json_new_object();
(void)dc_json_object_set(json, "type",
dc_json_new_string("file-sent"));
(void)dc_json_object_set(json, "id",
dc_json_new_string(dc_json_string(dc_json_get(cmd,
"id"))));
(void)dc_json_object_set(json, "size", dc_json_new_int(0));
send_json(json);
return;
}
if (strcmp(type, "file-accept") == 0) {
DcJson *data = dc_json_new_object();
DcJson *end = dc_json_new_object();
(void)dc_json_object_set(data, "type",
dc_json_new_string("file-data"));
(void)dc_json_object_set(data, "id",
dc_json_new_string("f1"));
(void)dc_json_object_set(data, "data",
dc_json_new_string("aGVsbG8gd29ybGQ="));
(void)dc_json_object_set(data, "size", dc_json_new_int(11));
send_json(data);
(void)dc_json_object_set(end, "type",
dc_json_new_string("file-end"));
(void)dc_json_object_set(end, "id",
dc_json_new_string("f1"));
(void)dc_json_object_set(end, "size", dc_json_new_int(11));
if (strcmp(mode, "latehash") == 0)
(void)dc_json_object_set(end, "sha256",
dc_json_new_string("b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"));
else if (strcmp(mode, "badendhash") == 0)
(void)dc_json_object_set(end, "sha256",
dc_json_new_string("0000000000000000000000000000000000000000000000000000000000000000"));
send_json(end);
return;
}
if (strcmp(type, "file-reject") == 0) {
DcJson *json = dc_json_new_object();
(void)dc_json_object_set(json, "type",
dc_json_new_string("file-reject"));
(void)dc_json_object_set(json, "id",
dc_json_new_string("f1"));
(void)dc_json_object_set(json, "reason",
dc_json_new_string("rejected"));
send_json(json);
return;
}
if (strcmp(type, "answer") == 0) {
DcJson *answer = dc_json_new_object();
(void)dc_json_object_set(answer, "type",
dc_json_new_string("answer-received"));
(void)dc_json_object_set(answer, "id",
dc_json_new_int(dc_json_int(dc_json_get(cmd, "id"))));
send_json(answer);
return;
}
if (strcmp(type, "disconnect") == 0) {
DcJson *bye = dc_json_new_object();
(void)dc_json_object_set(bye, "type",
dc_json_new_string("bye"));
(void)dc_json_object_set(bye, "reason",
dc_json_new_string("requested"));
send_json(bye);
_exit(0);
}
}
int
main(int argc, char **argv)
{
DcFrameBuf frame;
char buf[4096];
(void)argc;
(void)argv;
dc_frame_init(&frame);
send_hello();
for (;;) {
ssize_t n = read(STDIN_FILENO, buf, sizeof(buf));
if (n < 0) {
if (errno == EINTR)
continue;
break;
}
if (n == 0)
break;
if (dc_frame_feed(&frame, buf, (size_t)n) != 0)
break;
for (;;) {
size_t len = 0;
char *line = dc_frame_next(&frame, &len);
DcJson *cmd;
if (line == NULL)
break;
cmd = dc_json_parse(line, len, NULL, 0);
free(line);
if (cmd != NULL) {
handle(cmd);
dc_json_free(cmd);
}
}
}
dc_frame_free(&frame);
return 0;
}