Files
mcde/lib/libDtChat/test_frame.c
T
lotherk e645e8160d dtchat: add the core API, JSON reader and line framing (T1.1)
include/DtChat/DtChat.h and Ipc.h define the UI-facing core API and the adapter contract. lib/libDtChat builds libDtChatIpc (strict JSON parser/writer, 1 MiB line framing) and the libDtChat core stub; test_json and test_frame cover escapes, UTF-8, depth 32, duplicate keys, overlong lines, CRLF, NUL and the write path. Wired into configure.ac, lib/Makefile.am and the installed headers. Standalone tests pass, ASan/UBSan clean.
2026-11-16 16:35:48 +00:00

146 lines
2.8 KiB
C

/*
* test_frame.c -- unit tests for libDtChatIpc's line framing
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*/
#include "DtChat/Ipc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.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
feed(DcFrameBuf *frame, const char *s)
{
CHECK(dc_frame_feed(frame, s, strlen(s)) == 0);
}
static void
test_lines(void)
{
DcFrameBuf frame;
char *line;
size_t len;
dc_frame_init(&frame);
feed(&frame, "hello\n");
line = dc_frame_next(&frame, &len);
CHECK(line != NULL);
if (line != NULL) {
CHECK(strcmp(line, "hello") == 0);
CHECK(len == 5);
free(line);
}
CHECK(dc_frame_next(&frame, &len) == NULL);
feed(&frame, "par");
feed(&frame, "tial\n");
line = dc_frame_next(&frame, &len);
CHECK(line != NULL && strcmp(line, "partial") == 0);
free(line);
feed(&frame, "a\nb\n");
line = dc_frame_next(&frame, &len);
CHECK(line != NULL && strcmp(line, "a") == 0);
free(line);
line = dc_frame_next(&frame, &len);
CHECK(line != NULL && strcmp(line, "b") == 0);
free(line);
CHECK(dc_frame_next(&frame, &len) == NULL);
feed(&frame, "x\r\n");
line = dc_frame_next(&frame, &len);
CHECK(line != NULL && strcmp(line, "x") == 0);
CHECK(len == 1);
free(line);
feed(&frame, "\n");
line = dc_frame_next(&frame, &len);
CHECK(line != NULL && strcmp(line, "") == 0);
CHECK(len == 0);
free(line);
dc_frame_free(&frame);
}
static void
test_bad(void)
{
DcFrameBuf frame;
char *big;
size_t n;
dc_frame_init(&frame);
CHECK(dc_frame_feed(&frame, "a\0b", 3) == -1);
dc_frame_free(&frame);
dc_frame_init(&frame);
n = DC_IPC_MAX_LINE + 2;
big = malloc(n);
if (big == NULL)
return;
memset(big, 'a', n);
CHECK(dc_frame_feed(&frame, big, n) == -1);
dc_frame_free(&frame);
/* exactly the maximum line length is allowed */
dc_frame_init(&frame);
big[DC_IPC_MAX_LINE] = '\n';
CHECK(dc_frame_feed(&frame, big, DC_IPC_MAX_LINE + 1) == 0);
{
char *line = dc_frame_next(&frame, NULL);
CHECK(line != NULL);
if (line != NULL) {
CHECK(strlen(line) == DC_IPC_MAX_LINE);
free(line);
}
}
dc_frame_free(&frame);
free(big);
}
static void
test_send(void)
{
int fds[2];
char buf[32];
ssize_t n;
if (pipe(fds) != 0) {
perror("pipe");
fails++;
return;
}
CHECK(dc_frame_send(fds[1], "abc", 3) == 0);
n = read(fds[0], buf, sizeof(buf));
CHECK(n == 4);
CHECK(n == 4 && memcmp(buf, "abc\n", 4) == 0);
close(fds[0]);
close(fds[1]);
}
int
main(void)
{
test_lines();
test_bad();
test_send();
if (fails != 0) {
fprintf(stderr, "%d test(s) failed\n", fails);
return 1;
}
printf("test_frame: all ok\n");
return 0;
}