Files
splinter/tests/test_fuzz.c
T
lotherk f07128cf9b tests: deterministic fuzz over the parsers that read foreign text
The markdown renderer, its chunk cursor, the SSE reader and the JSON parser
all consume text from a model or a provider. test_fuzz drives 4000 inputs from
a fixed seed through all four and checks the two properties a plain crash test
would miss: the renderer is deterministic, and the cursor always finishes (a
piece that does not advance would loop forever in the Telegram sender).

Red proof: with sp_tgmd_next() forced to return without advancing, the test
fails on "piece.len > 0"; with the real code it passes.

make check: all tests passed (test_fuzz included).
make check-asan: all 40 test binaries clean.
2026-11-19 07:15:30 +00:00

192 lines
4.5 KiB
C

/*
* splinter -- deterministic fuzz over the parsers that read other people's text.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* The markdown renderer, its chunk cursor, the SSE reader and the JSON parser
* all take text from a model or a provider. A fixed seed makes this a normal
* test: the same inputs every run, no flakes, and ASAN turns the interesting
* case (an out-of-bounds read) into a failure instead of a crash somewhere
* else an hour later.
*
* The cursor carries one property beyond "does not crash": it must finish. A
* piece that does not advance the position would loop forever in the Telegram
* sender, so the loop has a hard cap that is generous but finite.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "splinter.h"
#include "mcp.h"
#include "tgmd.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \
#cond); \
failures++; \
} \
} while (0)
/* xorshift64*, fixed seed: reproducible on every machine */
static unsigned long long rng_state = 0x9e3779b97f4a7c15ULL;
static unsigned long long
rnd(void)
{
unsigned long long x = rng_state;
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
rng_state = x;
return x * 0x2545f4914f6cdd1dULL;
}
/*
* An alphabet that pushes on the parser's edges: the tags it knows, the
* characters it escapes, attributes, partial tags, a NUL byte can not be
* represented in a C string, so control characters stand in for the rest.
*/
static const char *const atoms[] = {
"<", ">", "&", "\"", "'", "/", "=", "\n", "\t", " ", "a", "b",
"<b>", "</b>", "<i>", "</i>", "<s>", "</s>", "<code>", "</code>",
"<pre>", "</pre>", "<blockquote>", "</blockquote>",
"<a href=\"", "</a>", "&amp;", "&lt;", "&quot;", "&#x", "\x01",
"\x7f", "\xc3\xa4", "```", "---", "| x | y |", "*", "_", "~", "\r\n",
};
static void
make_text(char *buf, size_t cap)
{
size_t at = 0;
while (at + 8 < cap) {
const char *a = atoms[rnd() % (sizeof(atoms) /
sizeof(atoms[0]))];
size_t n = strlen(a);
if (at + n + 1 >= cap)
break;
memcpy(buf + at, a, n);
at += n;
}
buf[at] = '\0';
}
/* every piece must fit the budget (tag overhead included) and the loop must end */
static void
check_cursor(const char *html, size_t limit)
{
struct sp_tgmd_cursor c;
struct sp_buf piece;
size_t iter = 0, cap = strlen(html) * 2 + 1000;
int rc;
if (sp_buf_init(&piece) != 0)
return;
CHECK(sp_tgmd_cursor_init(&c, html) == 0);
for (;;) {
sp_buf_clear(&piece);
rc = sp_tgmd_next(&c, limit, &piece);
if (rc <= 0)
break;
CHECK(piece.len > 0);
CHECK(piece.len <= limit + 64); /* the tag overhead is bounded */
if (++iter > cap) {
CHECK(0 && "tgmd cursor did not finish");
break;
}
}
sp_tgmd_cursor_free(&c);
sp_buf_free(&piece);
}
int
main(void)
{
char md[512];
struct sp_buf out, err;
unsigned i;
CHECK(sp_buf_init(&out) == 0);
CHECK(sp_buf_init(&err) == 0);
for (i = 0; i < 4000; i++) {
struct sp_sse *sse;
make_text(md, sizeof(md));
/* the renderer is deterministic */
sp_buf_clear(&out);
CHECK(sp_tgmd_render(md, &out) == 0);
{
struct sp_buf again;
CHECK(sp_buf_init(&again) == 0);
CHECK(sp_tgmd_render(md, &again) == 0);
CHECK(again.len == out.len &&
(out.len == 0 ||
memcmp(again.p, out.p, out.len) == 0));
sp_buf_free(&again);
}
if (out.p != NULL && out.len > 0)
check_cursor(out.p,
8 + (size_t)(rnd() % 200));
/* the SSE reader: split the same text into lines */
sse = sp_sse_new();
CHECK(sse != NULL);
if (sse != NULL) {
char *copy = strdup(md), *line, *save = NULL;
CHECK(copy != NULL);
if (copy != NULL) {
for (line = strtok_r(copy, "\n", &save);
line != NULL;
line = strtok_r(NULL, "\n", &save)) {
struct sp_buf delta;
if (sp_buf_init(&delta) != 0)
continue;
sp_buf_clear(&err);
(void)sp_sse_line(sse, line, &delta,
&err);
sp_buf_free(&delta);
}
free(copy);
}
{
struct sp_reply rep;
memset(&rep, 0, sizeof(rep));
sp_buf_clear(&err);
(void)sp_sse_finish(sse, &rep, &err);
sp_reply_free(&rep);
}
sp_sse_free(sse);
}
/* the JSON parser: random text must parse or fail, never crash */
{
mcp_json *v = mcp_parse(md);
if (v != NULL)
mcp_json_free(v);
v = mcp_parse_exact(md);
if (v != NULL)
mcp_json_free(v);
}
}
sp_buf_free(&out);
sp_buf_free(&err);
if (failures == 0)
printf("test_fuzz: ok (4000 inputs)\n");
return failures == 0 ? 0 : 1;
}