Files
splinter/tests/test_confblock.c
T
lotherk 21287b88a9 agent: free the reply of a terminal failure, and make the tests leak-clean
ASAN over the unit tests found a real leak the normal gate cannot see: a
transport failure that is not retried (the fourth 500, a 4xx, a dead stream)
returned -1 with the last reply's body and calls still allocated. The caller
only reads the reply on success, so it leaked on every terminal failure -- a
long-running daemon with a flapping provider grows by one reply per failure.
test_agent's four-500 case leaked exactly the last "boom"; it is clean now.

The same run showed the tests leaking their own fixtures: test_anthropic and
test_tasks never freed the registry they filled, test_cli forgot host4 and
host5, test_confblock forgot its error buffer, test_mcpclient its registries.
Fixed; all 39 unit binaries are now clean under ASAN+UBSAN with leak detection.

That gate is `make check-asan`: it rebuilds the tree with the sanitizers, runs
the unit binaries with LSAN, and rebuilds normally afterwards. Three MCP-plugin
teardown allocations are suppressed in tests/asan.supp: its server thread is
detached, shutdown kills the child but cannot join the thread, so the line
buffer (bounded at RPC_MAX_LINE) and the tools registered by that thread can
outlive the host. Bounded, teardown-only, tracked separately.

make check: all tests passed. make check-asan: all 39 test binaries clean.
2026-11-18 23:16:26 +00:00

231 lines
8.0 KiB
C

/*
* splinter -- the block syntax of the config file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "splinter.h"
#include "config.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static int
load(const char *text, struct sp_config *c, struct sp_buf *err)
{
char path[] = "/var/tmp/splinter-cb-XXXXXX";
int fd = mkstemp(path), rc;
ssize_t w;
if (fd < 0)
return -2;
w = write(fd, text, strlen(text));
(void)w;
close(fd);
sp_config_init(c);
rc = sp_config_load(c, path, err);
unlink(path);
return rc;
}
int
main(void)
{
struct sp_config *c = calloc(1, sizeof(*c));
struct sp_buf err;
sp_buf_init(&err);
setenv("CB_TOKEN", "sekrit", 1);
unsetenv("CB_UNSET");
CHECK(load(
"# a comment\n"
"core {\n"
" theme = Delphinium\n"
" steps = 40\n"
" autonomous = true\n"
" default = claude\n"
" plugins = [a.so, \"b c.so\"]\n"
" on_limit = wait\n"
" max_wait = 6h\n"
" ctx = 200000\n"
"}\n"
"telegram { allow = [1, 2] }\n"
"provider local {\n"
" about = \"the local one\"\n"
" endpoint = http://h:1/v1/chat/completions\n"
" model = m\n"
" tags = [local, code]\n"
" prio = 5\n"
" ctx = 131072\n"
" limit = 10/5h\n"
"}\n"
"provider claude-a {\n"
" kind = cli\n"
" cmd = \"claude -p\"\n"
" tags = [cli, big]\n"
" env { CLAUDE_CODE_OAUTH_TOKEN = \"${CB_TOKEN}\" OTHER = \"${CB_UNSET}\" }\n"
"}\n"
"provider hidden { endpoint = http://x/v1 model = z delegate = false }\n"
"pool claude { members = [claude-a, local] strategy = balance }\n"
"mcp junction {\n"
" url = http://127.0.0.1:8090/mcp\n"
" header = \"Authorization: Bearer ${CB_TOKEN}\"\n"
"}\n"
"mcp files {\n"
" cmd = \"npx -y server-filesystem /home\"\n"
" enabled = false\n"
"}\n",
c, &err) == 0);
if (err.len > 0) fprintf(stderr, "err: %s\n", err.p);
CHECK(strcmp(c->theme, "Delphinium") == 0);
CHECK(c->steps == 40);
CHECK(c->autonomous == 1);
CHECK(strcmp(c->default_id, "claude") == 0);
CHECK(c->nplugins == 2 && strcmp(c->plugins[1], "b c.so") == 0);
CHECK(c->max_wait == 6 * 3600);
CHECK(c->ctx == 200000);
CHECK(strcmp(c->telegram_allow, "1,2") == 0);
CHECK(c->ndelegates == 2);
CHECK(strcmp(c->delegate[0].id, "local") == 0);
CHECK(strcmp(c->delegate[0].about, "the local one") == 0);
CHECK(strcmp(c->delegate[0].tags, "local,code") == 0);
CHECK(c->delegate[0].ctx == 131072);
CHECK(c->delegate[0].req_limit == 10 && c->delegate[0].req_window == 18000);
CHECK(c->delegate[1].nenv == 1);
CHECK(strstr(c->delegate[1].env[0], "=sekrit") != NULL);
CHECK(strcmp(c->delegate[1].kind, "cli") == 0);
CHECK(c->npools == 1 && c->pool[0].nmembers == 2);
CHECK(strcmp(c->pool[0].strategy, "balance") == 0);
CHECK(c->nmcp == 2);
CHECK(strcmp(c->mcp[0].id, "junction") == 0);
CHECK(strcmp(c->mcp[0].url, "http://127.0.0.1:8090/mcp") == 0);
CHECK(strstr(c->mcp[0].header, "Bearer sekrit") != NULL);
CHECK(c->mcp[0].enabled == 1);
CHECK(strcmp(c->mcp[1].id, "files") == 0);
CHECK(strcmp(c->mcp[1].cmd, "npx -y server-filesystem /home") == 0);
CHECK(c->mcp[1].enabled == 0);
CHECK(c->mcp[1].url[0] == '\0');
sp_buf_clear(&err);
CHECK(load("mcp noendpoint { header = \"x: y\" }\n", c, &err) != 0);
CHECK(strstr(err.p, "url or a cmd") != NULL);
sp_buf_clear(&err);
CHECK(load("mcp { url = http://h/mcp }\n", c, &err) != 0);
/* errors name the line that was written */
sp_buf_clear(&err);
CHECK(load("core {\n steps = 1\n bogus_key = 2\n}\n", c, &err) != 0);
CHECK(strstr(err.p, ":3:") != NULL && strstr(err.p, "bogus_key") != NULL);
sp_buf_clear(&err);
CHECK(load("provider x {\n nope = 1\n}\n", c, &err) != 0);
CHECK(strstr(err.p, ":2:") != NULL && strstr(err.p, "nope") != NULL);
sp_buf_clear(&err);
CHECK(load("core {\n steps = 1\n", c, &err) != 0);
CHECK(strstr(err.p, "closing }") != NULL);
sp_buf_clear(&err);
CHECK(load("pool p { strategy = balance }\n", c, &err) != 0);
sp_buf_clear(&err);
CHECK(load("provider p { about = \"a|b\" }\n", c, &err) != 0);
/* a newline in a core value must not become a second config line */
sp_buf_clear(&err);
CHECK(load("core { theme = \"x\\nautonomous = 1\" }\n", c, &err) != 0);
CHECK(err.p != NULL && strstr(err.p, "newline") != NULL);
/* the flat form still loads */
sp_buf_clear(&err);
CHECK(load("model = m\nsteps = 7\n", c, &err) == 0 && c->steps == 7);
/* plugin blocks: one per plugin, its own settings beside it */
sp_buf_clear(&err);
setenv("HOME", "/home/cb", 1);
CHECK(load(
"plugin lcm {\n"
" keep = 40\n"
" tags = [a, b]\n"
" opts { depth = 3 }\n"
"}\n"
"plugin tmux { keep = 6; tool_trim = 8192 }\n"
"plugin off { enabled = false\n x = 1 }\n"
"plugin mine {\n"
" path = ~/my/plugin.so\n"
" foo = bar\n"
" server s1 { url = http://h/mcp }\n"
"}\n"
"plugin mcp {\n"
" server junction { url = http://172.25.206.16:8090/mcp }\n"
" server local {\n"
" cmd = \"tasksd --stdio\"\n"
" header = \"Authorization: Bearer ${CB_TOKEN}\"\n"
" enabled = no\n"
" }\n"
"}\n", c, &err) == 0);
CHECK(c->nplugins == 4);
CHECK(c->nplugins == 4 && strcmp(c->plugins[0], "lcm") == 0 &&
strcmp(c->plugins[1], "tmux") == 0 &&
strcmp(c->plugins[2], "/home/cb/my/plugin.so") == 0 &&
strcmp(c->plugins[3], "mcp") == 0);
CHECK(sp_config_plugin_value(c, "lcm", "keep") != NULL &&
strcmp(sp_config_plugin_value(c, "lcm", "keep"), "40") == 0);
CHECK(sp_config_plugin_value(c, "lcm", "tags") != NULL &&
strcmp(sp_config_plugin_value(c, "lcm", "tags"), "a,b") == 0);
CHECK(sp_config_plugin_value(c, "lcm", "opts.depth") != NULL &&
strcmp(sp_config_plugin_value(c, "lcm", "opts.depth"), "3") == 0);
CHECK(sp_config_plugin_value(c, "mine", "foo") != NULL &&
strcmp(sp_config_plugin_value(c, "mine", "foo"), "bar") == 0);
CHECK(sp_config_plugin_value(c, "mine", "server.s1.url") != NULL &&
strcmp(sp_config_plugin_value(c, "mine", "server.s1.url"),
"http://h/mcp") == 0);
CHECK(sp_config_plugin_value(c, "mine", "path") == NULL);
CHECK(sp_config_plugin_value(c, "off", "x") == NULL);
CHECK(sp_config_plugin_value(c, "lcm", "nope") == NULL);
/* one line, ';' between the settings: both are their own key */
CHECK(sp_config_plugin_value(c, "tmux", "keep") != NULL &&
strcmp(sp_config_plugin_value(c, "tmux", "keep"), "6") == 0);
CHECK(sp_config_plugin_value(c, "tmux", "tool_trim") != NULL &&
strcmp(sp_config_plugin_value(c, "tmux", "tool_trim"), "8192") == 0);
/* plugin mcp { server ... } is what mcp ID { } was */
CHECK(c->nmcp == 2);
CHECK(c->nmcp == 2 && strcmp(c->mcp[0].id, "junction") == 0 &&
strcmp(c->mcp[0].url, "http://172.25.206.16:8090/mcp") == 0 &&
c->mcp[0].enabled == 1);
CHECK(c->nmcp == 2 && strcmp(c->mcp[1].id, "local") == 0 &&
strcmp(c->mcp[1].cmd, "tasksd --stdio") == 0 &&
strcmp(c->mcp[1].header, "Authorization: Bearer sekrit") == 0 &&
c->mcp[1].enabled == 0);
/* the old forms still load: plugins = [...] and mcp ID { } */
sp_buf_clear(&err);
CHECK(load("core { plugins = [x/a.so] }\n"
"mcp old { url = http://o/mcp }\n", c, &err) == 0);
CHECK(c->nplugins == 1 && strcmp(c->plugins[0], "x/a.so") == 0);
CHECK(c->nmcp == 1 && strcmp(c->mcp[0].id, "old") == 0);
/* errors */
sp_buf_clear(&err);
CHECK(load("plugin { x = 1 }\n", c, &err) != 0);
sp_buf_clear(&err);
CHECK(load("plugin a/b { }\n", c, &err) != 0);
sp_buf_clear(&err);
CHECK(load("plugin mcp {\n server { url = x }\n}\n", c, &err) != 0);
sp_buf_clear(&err);
CHECK(load("plugin mcp {\n server s { nope = x }\n}\n", c, &err) != 0);
CHECK(strstr(err.p, ":2:") != NULL && strstr(err.p, "nope") != NULL);
sp_buf_clear(&err);
CHECK(load("plugin p { v = \"a|b\" }\n", c, &err) != 0);
free(c);
sp_buf_free(&err);
if (failures == 0)
puts("test_confblock: ok");
return failures != 0;
}