Files
splinter/tests/test_providers.c
T
lotherk 9b2fee66a2 tests: valgrind-clean -- free the registry, NUL where the read stopped
Valgrind over the test binaries found two test-side defects, no
production path:

- test_skills never called sp_registry_free(), so the 448 bytes
  sp_registry_add() copied for the three skill tools were reported as
  "definitely lost" on every run. Freed now.
- test_providers read the /connect drop-in into a 1024-byte stack buffer
  and NUL-ed only the first and last byte, so strstr() walked through
  uninitialised bytes between them ("Conditional jump or move depends on
  uninitialised value"). Terminated at the read length instead.

Both runs are clean: valgrind tests/test_skills -> 0 errors, exit 0;
valgrind tests/test_providers -> 0 errors, exit 0. make check: all tests
passed. tests/check_asan.sh: all 46 test binaries clean.
2026-11-23 13:22:14 +00:00

426 lines
14 KiB
C

/*
* splinter -- the provider list and /connect's config line.
*
* /model shows what the config already says and /connect adds a line to
* <config>.d/20-connect.conf. The line has to survive a restart, so the test
* writes it, loads the config again and looks at the delegate it produced.
*/
#include <dirent.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <unistd.h>
#include "config.h"
#include "providers.h"
#include "splinter.h"
static int failures;
/* every descriptor this process has open: /proc/self/fd, minus the dir handle */
static int
open_fds(void)
{
DIR *d = opendir("/proc/self/fd");
struct dirent *e;
int n = 0;
if (d == NULL)
return -1;
while ((e = readdir(d)) != NULL)
if (e->d_name[0] != '.')
n++;
closedir(d);
return n;
}
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static void
base(struct sp_config *cfg)
{
struct sp_delegate *d;
memset(cfg, 0, sizeof(*cfg));
sp_config_init(cfg);
snprintf(cfg->endpoint, sizeof(cfg->endpoint),
"http://127.0.0.1:8080/v1/chat/completions");
snprintf(cfg->model, sizeof(cfg->model), "local");
snprintf(cfg->key_env, sizeof(cfg->key_env), "LOCAL_KEY");
d = &cfg->delegate[cfg->ndelegates++];
snprintf(d->id, sizeof(d->id), "far");
snprintf(d->kind, sizeof(d->kind), "openai");
snprintf(d->endpoint, sizeof(d->endpoint),
"https://api.example/v1/chat/completions");
snprintf(d->model, sizeof(d->model), "big");
snprintf(d->key_env, sizeof(d->key_env), "FAR_KEY");
/* a cli delegate has no endpoint: it is not a model to switch to */
d = &cfg->delegate[cfg->ndelegates++];
snprintf(d->id, sizeof(d->id), "claude");
snprintf(d->kind, sizeof(d->kind), "cli");
snprintf(d->cmd, sizeof(d->cmd), "claude -p");
/* a delegate without a model is not one either */
d = &cfg->delegate[cfg->ndelegates++];
snprintf(d->id, sizeof(d->id), "empty");
snprintf(d->endpoint, sizeof(d->endpoint), "https://x/v1/chat/completions");
snprintf(cfg->exit[cfg->nexits].id, sizeof(cfg->exit[0].id), "groq");
snprintf(cfg->exit[cfg->nexits].endpoint,
sizeof(cfg->exit[0].endpoint), "https://api.groq.com/openai/v1/chat/completions");
snprintf(cfg->exit[cfg->nexits].model, sizeof(cfg->exit[0].model),
"llama");
cfg->nexits++;
}
int
main(void)
{
struct sp_config cfg, back;
struct sp_provider p;
struct sp_buf err;
char dir[512], drop[600], mainconf[700], path[800];
char line[600];
const char *tmp = getenv("TMPDIR");
FILE *f;
size_t idx;
int nlines = 0;
char buf[1024];
if (tmp == NULL || *tmp == '\0')
tmp = "/var/tmp";
snprintf(dir, sizeof(dir), "%s/splinter-prov-XXXXXX", tmp);
CHECK(mkdtemp(dir) != NULL);
/* the loader reads <config>.d, so /connect writes there too */
snprintf(drop, sizeof(drop), "%s/splinter.conf.d", dir);
base(&cfg);
CHECK(sp_provider_count(&cfg) == 3);
CHECK(sp_provider_at(&cfg, 0, &p) == 0);
CHECK(strcmp(p.id, "main") == 0 && p.main == 1);
CHECK(strcmp(p.model, "local") == 0);
CHECK(sp_provider_at(&cfg, 1, &p) == 0);
CHECK(strcmp(p.id, "far") == 0 && strcmp(p.model, "big") == 0);
CHECK(sp_provider_at(&cfg, 2, &p) == 0);
CHECK(strcmp(p.id, "groq") == 0);
CHECK(sp_provider_at(&cfg, 3, &p) != 0);
CHECK(sp_provider_find(&cfg, "far", &idx) == 1 && idx == 1);
CHECK(sp_provider_find(&cfg, "nope", &idx) == 0);
/* add: one line, and the config remembers it */
CHECK(sp_buf_init(&err) == 0);
CHECK(sp_provider_add(&cfg, drop, "ocgo",
"https://opencode.ai/zen/go/v1/chat/completions",
"deepseek-v4.1-flash", "OPENCODE_GO_API_KEY", &err) == 0);
CHECK(sp_provider_count(&cfg) == 4);
CHECK(sp_provider_find(&cfg, "ocgo", &idx) == 1);
snprintf(path, sizeof(path), "%s/20-connect.conf", drop);
f = fopen(path, "r");
CHECK(f != NULL);
if (f != NULL) {
while (fgets(buf, sizeof(buf), f) != NULL)
if (strstr(buf, "delegate =") != NULL)
nlines++;
fclose(f);
}
CHECK(nlines == 1);
/* a second add replaces the line instead of growing the file */
CHECK(sp_provider_add(&cfg, drop, "ocgo",
"https://opencode.ai/zen/go/v1/chat/completions",
"other-model", "", &err) == 0);
nlines = 0;
f = fopen(path, "r");
if (f != NULL) {
while (fgets(buf, sizeof(buf), f) != NULL)
if (strstr(buf, "delegate =") != NULL)
nlines++;
fclose(f);
}
CHECK(nlines == 1);
CHECK(sp_provider_find(&cfg, "ocgo", &idx) == 1);
CHECK(sp_provider_at(&cfg, idx, &p) == 0);
CHECK(strcmp(p.model, "other-model") == 0 && p.key_env[0] == '\0');
/* the written line is a line the loader accepts */
snprintf(mainconf, sizeof(mainconf), "%s/splinter.conf", dir);
f = fopen(mainconf, "w");
CHECK(f != NULL);
if (f != NULL) {
fputs("endpoint = http://127.0.0.1:8080/v1/chat/completions\n"
"model = local\n", f);
fclose(f);
}
memset(&back, 0, sizeof(back));
sp_config_init(&back);
CHECK(sp_config_load(&back, mainconf, &err) == 0);
{
const struct sp_delegate *d = NULL;
size_t i;
for (i = 0; i < back.ndelegates; i++)
if (strcmp(back.delegate[i].id, "ocgo") == 0)
d = &back.delegate[i];
CHECK(d != NULL);
if (d != NULL) {
CHECK(strcmp(d->model, "other-model") == 0);
CHECK(strcmp(d->endpoint,
"https://opencode.ai/zen/go/v1/chat/completions") == 0);
/* no kind= in the line means openai */
CHECK(d->kind[0] == '\0' || strcmp(d->kind, "openai") == 0);
}
}
/* /model writes 90-model.conf; picking main removes it again */
{
struct sp_provider pick;
char mpath[800], mbuf[1024];
struct sp_buf merr;
FILE *mf;
memset(&pick, 0, sizeof(pick));
snprintf(pick.id, sizeof(pick.id), "ocgo");
snprintf(pick.endpoint, sizeof(pick.endpoint),
"https://opencode.ai/zen/go/v1/chat/completions");
snprintf(pick.model, sizeof(pick.model), "other-model");
snprintf(pick.key_env, sizeof(pick.key_env), "OPENCODE_GO_API_KEY");
sp_buf_init(&merr);
CHECK(sp_provider_save_choice(&back, &pick, &merr) == 0);
snprintf(mpath, sizeof(mpath), "%s.d/90-model.conf", mainconf);
mf = fopen(mpath, "r");
CHECK(mf != NULL);
if (mf != NULL) {
size_t got;
/* terminate where the read stopped: the rest of the
* buffer is not text, and strstr() would walk into it */
got = fread(mbuf, 1, sizeof(mbuf) - 1, mf);
mbuf[got] = '\0';
fclose(mf);
/* the choice is a default; the provider carries the rest */
CHECK(strstr(mbuf, "default = ocgo/other-model") != NULL);
CHECK(strstr(mbuf, "endpoint =") == NULL);
CHECK(strstr(mbuf, "model =") == NULL);
}
{
struct sp_config again;
struct sp_buf rerr;
memset(&again, 0, sizeof(again));
sp_config_init(&again);
CHECK(sp_buf_init(&rerr) == 0);
CHECK(sp_config_load(&again, mainconf, &rerr) == 0);
CHECK(strcmp(again.default_id, "ocgo/other-model") == 0);
sp_buf_free(&rerr);
}
memset(&pick, 0, sizeof(pick));
pick.main = 1;
CHECK(sp_provider_save_choice(&back, &pick, &merr) == 0);
CHECK(access(mpath, F_OK) != 0);
sp_buf_free(&merr);
}
CHECK(sp_provider_line(line, sizeof(line), "ocgo", "https://x/v1",
"m", "K") == 0);
CHECK(strstr(line, "delegate = ocgo |") != NULL);
CHECK(strstr(line, "key_env=K") != NULL);
sp_buf_free(&err);
/*
* A selector as one provider: a pool picks a member, "pool/model" picks
* the member and keeps the model, and a bare id is itself. This is what
* -m and /model accept, so the kind travels with the choice: a pool of
* anthropic members must not be applied as an OpenAI endpoint.
*/
{
struct sp_provider sel;
char model[64];
cfg.ndelegates++;
snprintf(cfg.delegate[3].id, 32, "api0");
snprintf(cfg.delegate[3].kind, 16, "anthropic");
snprintf(cfg.delegate[3].endpoint, 512,
"https://api.anthropic.com/v1/messages");
snprintf(cfg.delegate[3].model, 256, "claude-sonnet-5-5");
cfg.ndelegates++;
snprintf(cfg.delegate[4].id, 32, "api1");
snprintf(cfg.delegate[4].kind, 16, "anthropic");
snprintf(cfg.delegate[4].endpoint, 512,
"https://api.anthropic.com/v1/messages");
snprintf(cfg.delegate[4].model, 256, "claude-sonnet-5-5");
snprintf(cfg.pool[cfg.npools].id, 32, "accounts");
snprintf(cfg.pool[cfg.npools].strategy, 16, "balance");
snprintf(cfg.pool[cfg.npools].member[0], 32, "api0");
snprintf(cfg.pool[cfg.npools].member[1], 32, "api1");
cfg.pool[cfg.npools].nmembers = 2;
cfg.npools++;
CHECK(sp_provider_select(&cfg, "accounts", &sel, model,
sizeof(model)) == 0);
CHECK(strcmp(sel.kind, "anthropic") == 0);
CHECK(model[0] != '\0');
CHECK(sp_provider_select(&cfg, "accounts/opus-5-5", &sel, model,
sizeof(model)) == 0);
CHECK(strcmp(model, "opus-5-5") == 0);
CHECK(sp_provider_select(&cfg, "api0", &sel, model,
sizeof(model)) == 0);
CHECK(strcmp(sel.id, "api0") == 0);
CHECK(sp_provider_select(&cfg, "nope", &sel, model,
sizeof(model)) != 0);
}
/*
* A saved anthropic choice is a default line too. The kind stays in
* the provider block that the default names; writing it into the
* drop-in as a second main endpoint was the old, shadowing way.
*/
{
struct sp_provider pick;
struct sp_buf merr;
char mp[800], mb[1024];
FILE *mf;
memset(&pick, 0, sizeof(pick));
snprintf(pick.id, sizeof(pick.id), "api0");
snprintf(pick.kind, sizeof(pick.kind), "anthropic");
snprintf(pick.endpoint, sizeof(pick.endpoint),
"https://api.anthropic.com/v1/messages");
snprintf(pick.model, sizeof(pick.model), "claude-sonnet-5-5");
snprintf(pick.key_env, sizeof(pick.key_env), "CLAUDE_TOKEN");
CHECK(sp_buf_init(&merr) == 0);
CHECK(sp_provider_save_choice(&back, &pick, &merr) == 0);
snprintf(mp, sizeof(mp), "%s.d/90-model.conf", mainconf);
mf = fopen(mp, "r");
CHECK(mf != NULL);
if (mf != NULL) {
CHECK(fgets(mb, sizeof(mb), mf) != NULL);
CHECK(fgets(mb, sizeof(mb), mf) != NULL);
CHECK(strcmp(mb, "default = api0/claude-sonnet-5-5\n") ==
0);
fclose(mf);
}
sp_buf_free(&merr);
}
/*
* A refused write must still close its stream. Both writers used to
* chain the close: `write_all(...) || fclose(f)` and `fflush() ||
* fsync() || fclose()`. A failing write - a full disk, an
* RLIMIT_FSIZE - then skipped the fclose and leaked one descriptor
* per attempt; a daemon that keeps failing reaches EMFILE, which
* reads like a network error.
*
* The drop-in is padded past one stdio buffer on purpose: a short
* line stays in the buffer, fwrite() reports success and the failure
* only shows up in fclose(), which touches neither branch. Only the
* drop-in of /connect is padded: /model writes a few lines.
*/
{
char dpad[900];
FILE *pf;
struct sp_buf merr;
struct rlimit rl;
struct sigaction sa, oldsa;
int before, after, i;
snprintf(dpad, sizeof(dpad), "%s/20-connect.conf", drop);
pf = fopen(dpad, "w");
CHECK(pf != NULL);
if (pf != NULL) {
for (i = 0; i < 200; i++)
fprintf(pf, "# padding line %04d "
"........................................................"
"........\n", i);
CHECK(fclose(pf) == 0);
}
CHECK(getrlimit(RLIMIT_FSIZE, &rl) == 0);
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
CHECK(sigaction(SIGXFSZ, &sa, &oldsa) == 0);
{
struct rlimit lim = rl;
lim.rlim_cur = 8; /* shorter than the drop-in */
CHECK(setrlimit(RLIMIT_FSIZE, &lim) == 0);
}
sp_buf_init(&merr);
before = open_fds();
for (i = 0; i < 16; i++) {
char id[32];
snprintf(id, sizeof(id), "p%d", i);
sp_buf_clear(&merr);
(void)sp_provider_add(&back, drop, id,
"https://opencode.ai/zen/go/v1/chat/completions",
"other-model", "OCGO_KEY", &merr);
}
after = open_fds();
sp_buf_free(&merr);
CHECK(setrlimit(RLIMIT_FSIZE, &rl) == 0);
CHECK(sigaction(SIGXFSZ, &oldsa, NULL) == 0);
CHECK(before >= 0 && after == before);
CHECK(unlink(dpad) == 0);
}
/* what /connect must refuse before it becomes a line nobody can read */
CHECK(sp_provider_id_ok("ocgo") == 1);
CHECK(sp_provider_id_ok("my-prov_2") == 1);
CHECK(sp_provider_id_ok("Main") == 0);
CHECK(sp_provider_id_ok("main") == 0);
CHECK(sp_provider_id_ok("a b") == 0);
CHECK(sp_provider_id_ok("") == 0);
CHECK(sp_provider_endpoint_ok("http://127.0.0.1:11434/v1/chat/completions") == 1);
/* the config names the base; every route is built from it */
{
char u[256];
CHECK(strcmp(sp_endpoint_url("https://h/v1", "chat/completions",
u, sizeof(u)), "https://h/v1/chat/completions") == 0);
CHECK(strcmp(sp_endpoint_url("https://h/v1/", "models", u,
sizeof(u)), "https://h/v1/models") == 0);
CHECK(strcmp(sp_endpoint_url("https://h/v1/chat/completions",
"models", u, sizeof(u)), "https://h/v1/models") == 0);
CHECK(strcmp(sp_endpoint_url("https://h/v1/chat/completions",
"chat/completions", u, sizeof(u)),
"https://h/v1/chat/completions") == 0);
CHECK(strcmp(sp_endpoint_url("https://api.anthropic.com/v1/messages",
"models", u, sizeof(u)),
"https://api.anthropic.com/v1/models") == 0);
CHECK(strcmp(sp_endpoint_url("https://h/v1/messages", NULL, u,
sizeof(u)), "https://h/v1") == 0);
/* the model list of the three providers Konrad uses */
CHECK(strcmp(sp_endpoint_url("https://opencode.ai/zen/go/v1",
"models", u, sizeof(u)),
"https://opencode.ai/zen/go/v1/models") == 0);
CHECK(strcmp(sp_endpoint_url("https://ollama.com/v1",
"models", u, sizeof(u)),
"https://ollama.com/v1/models") == 0);
CHECK(strcmp(sp_endpoint_url("https://api.anthropic.com/v1",
"models", u, sizeof(u)),
"https://api.anthropic.com/v1/models") == 0);
}
CHECK(sp_provider_endpoint_ok("https://x/v1") == 1);
CHECK(sp_provider_endpoint_ok("x/v1") == 0);
CHECK(sp_provider_endpoint_ok("http://x/a b") == 0);
CHECK(sp_provider_endpoint_ok("http://x/a|b") == 0);
CHECK(sp_provider_model_ok("deepseek-v4.1-flash") == 1);
CHECK(sp_provider_model_ok("") == 0);
CHECK(sp_provider_model_ok("a|b") == 0);
CHECK(sp_provider_key_ok("OPENCODE_GO_API_KEY") == 1);
CHECK(sp_provider_key_ok("") == 1);
CHECK(sp_provider_key_ok("1BAD") == 0);
CHECK(sp_provider_key_ok("has-dash") == 0);
printf("%s", failures == 0 ? "test_providers: ok\n" : "");
return failures == 0 ? 0 : 1;
}