Files
splinter/tests/test_config.c
T
Konrad 8900d7f03f telegram: refuse to start without a token
The gateway entry point accepted an empty token and then polled with it; the
guard belongs there so a daemon that cannot listen says so instead of looking
alive.

Honest limit of this change: it is NOT the explanation for the running daemon.
That one had a token (resolved from the token file) and failed with 'curl: (35)
Recv failure: Connection reset by peer' thousands of times, and with the env
variable removed it now fails with 'curl failed (-1)' and stops - a different
error, not the missing token. The guard only catches the case where neither the
environment nor the file provide one.

What this commit does prove: with a config that has endpoint, model and
telegram_allow, the daemon starts, reports 'gateway listening (allow: 8300935)',
and stops on the failing poll instead of looping. What it does not prove: that a
valid token makes it work - that needs a real message.
2026-11-08 20:23:10 +00:00

252 lines
6.7 KiB
C

/*
* splinter -- config file tests.
*
* The interesting cases are the refusals: an unknown key, a line without '=',
* a missing file. A config that quietly ignores what it does not understand is
* a config that lies.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.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 void
write_file(const char *path, const char *text)
{
FILE *f = fopen(path, "w");
if (f == NULL)
return;
fputs(text, f);
fclose(f);
}
int
main(void)
{
struct sp_config c;
struct sp_buf err;
char tmpl[] = "/tmp/splinter-cfg-XXXXXX";
char path[640];
char *dir;
if (sp_buf_init(&err) != 0)
return 1;
dir = mkdtemp(tmpl);
CHECK(dir != NULL);
if (dir == NULL)
return 1;
/* ---- a full file ---- */
snprintf(path, sizeof(path), "%s/config", dir);
write_file(path,
"# a comment\n"
"\n"
"endpoint = http://127.0.0.1:8080/v1/chat/completions\n"
"model = qwen-test \n"
"theme = Broica\n"
"autonomous = 1\n"
"steps = 12\n"
"max_bytes = 4096\n"
"plugin = plugins/lcm/lcm.so\n"
"plugin = plugins/cps/cps.so\n"
"cli_cmd = claude -p\n"
"system_prompt = sei knapp\n");
sp_config_init(&c);
sp_buf_clear(&err);
CHECK(sp_config_load(&c, path, &err) == 0);
CHECK(strcmp(c.endpoint,
"http://127.0.0.1:8080/v1/chat/completions") == 0);
CHECK(strcmp(c.model, "qwen-test") == 0); /* trimmed */
CHECK(strcmp(c.theme, "Broica") == 0);
CHECK(c.autonomous == 1);
CHECK(c.steps == 12);
CHECK(c.max_bytes == 4096);
CHECK(c.nplugins == 2);
CHECK(strcmp(c.plugins[1], "plugins/cps/cps.so") == 0);
CHECK(strcmp(c.cli_cmd, "claude -p") == 0);
CHECK(strcmp(c.system_prompt, "sei knapp") == 0);
/* untouched defaults survive */
CHECK(strcmp(c.key_env, "SPLINTER_API_KEY") == 0);
/* ---- an unknown key is refused, with its line number ---- */
write_file(path, "endpoint = x\nwolke = 7\n");
sp_config_init(&c);
sp_buf_clear(&err);
CHECK(sp_config_load(&c, path, &err) == -1);
CHECK(strstr(err.p, "config") != NULL);
CHECK(strstr(err.p, ":2:") != NULL);
CHECK(strstr(err.p, "wolke") != NULL);
/* ---- a line without '=' is refused too ---- */
write_file(path, "endpoint\n");
sp_config_init(&c);
sp_buf_clear(&err);
CHECK(sp_config_load(&c, path, &err) == -1);
CHECK(strstr(err.p, "no '='") != NULL);
/* ---- a missing file says so, distinctly ---- */
sp_config_init(&c);
sp_buf_clear(&err);
CHECK(sp_config_load(&c, "/nonexistent/splinter/config", &err) == -1);
CHECK(strstr(err.p, "not found") != NULL);
/* ---- the example is valid: writing it and reading it back must work ---- */
snprintf(path, sizeof(path), "%s/sub/example", dir);
sp_buf_clear(&err);
CHECK(sp_config_write_example(path, &err) == 0);
sp_config_init(&c);
sp_buf_clear(&err);
CHECK(sp_config_load(&c, path, &err) == 0);
CHECK(strcmp(c.theme, "Delphinium") == 0);
CHECK(c.steps == 8);
CHECK(c.autonomous == 0);
/* ---- the path honours SPLINTER_CONFIG ---- */
setenv("SPLINTER_CONFIG", "/tmp/from-env", 1);
{
char buf[256];
CHECK(sp_config_path(buf, sizeof(buf)) == 0);
CHECK(strcmp(buf, "/tmp/from-env") == 0);
}
unsetenv("SPLINTER_CONFIG");
sp_buf_free(&err);
/* ---- providers from the environment ---- */
{
struct sp_config c;
sp_config_init(&c);
(void)setenv("OLLAMA_KEY", "k1", 1);
(void)setenv("OLLAMA_KEY_0", "k2", 1);
(void)setenv("OLLAMA_KEY_1", "k3", 1);
(void)setenv("CLAUDE_CODE_OAUTH_TOKEN", "t1", 1);
/*
* Environment delegates are opt-in: a token in the environment must not
* add an entry that competes for work nobody assigned it.
*/
CHECK(sp_config_scan_env(&c) == 3);
CHECK(c.ndelegates == 3);
CHECK(c.nexits == 3); /* one exit per Ollama key */
c.env_delegates = 1;
{
struct sp_config c2;
sp_config_init(&c2);
c2.env_delegates = 1;
CHECK(sp_config_scan_env(&c2) == 4);
CHECK(c2.ndelegates == 4);
CHECK(strcmp(c2.delegate[3].kind, "cli") == 0);
CHECK(strcmp(c2.delegate[3].id, "claude") == 0);
CHECK(c2.delegate[3].prio > 0);
}
CHECK(strcmp(c.exit[0].endpoint,
"https://ollama.com/v1/chat/completions") == 0);
(void)unsetenv("OLLAMA_KEY");
(void)unsetenv("OLLAMA_KEY_0");
(void)unsetenv("OLLAMA_KEY_1");
(void)unsetenv("CLAUDE_CODE_OAUTH_TOKEN");
}
/*
* ---- a full exit table keeps the operator's keys ----
* With no room for another exit, an OLLAMA_KEY used to overwrite the
* key_env of the last configured exit: that key then went to an
* unrelated endpoint.
*/
{
struct sp_config c;
size_t k;
sp_config_init(&c);
for (k = 0; k < SP_CONFIG_MAX_DELEGATES; k++) {
snprintf(c.exit[k].id, sizeof(c.exit[k].id), "mine-%zu", k);
snprintf(c.exit[k].key_env, sizeof(c.exit[k].key_env),
"MY_KEY_%zu", k);
}
c.nexits = SP_CONFIG_MAX_DELEGATES;
(void)setenv("OLLAMA_KEY", "k1", 1);
(void)sp_config_scan_env(&c);
CHECK(c.nexits == SP_CONFIG_MAX_DELEGATES);
{
char want[64];
snprintf(want, sizeof(want), "MY_KEY_%d",
SP_CONFIG_MAX_DELEGATES - 1);
CHECK(strcmp(c.exit[SP_CONFIG_MAX_DELEGATES - 1].key_env,
want) == 0);
}
(void)unsetenv("OLLAMA_KEY");
}
/* ---- drop-ins: a directory next to the config, read in name order ---- */
{
char dir[512], main_c[600], drop[600];
FILE *f;
const char *tmp = getenv("TMPDIR");
if (tmp == NULL || *tmp == '\0')
tmp = "/var/tmp";
snprintf(dir, sizeof(dir), "%s/splinter-conf-XXXXXX", tmp);
if (mkdtemp(dir) != NULL) {
struct sp_config c;
struct sp_buf e;
snprintf(main_c, sizeof(main_c), "%s/splinter.conf", dir);
write_file(main_c, "theme = Delphinium\n");
snprintf(drop, sizeof(drop), "%s/splinter.conf.d", dir);
if (mkdir(drop, 0700) == 0) {
char one[700], two[700];
snprintf(one, sizeof(one), "%s/50-late.conf", drop);
snprintf(two, sizeof(two), "%s/10-early.conf", drop);
write_file(one, "theme = Spider\n");
write_file(two, "locale = de\n");
/* a file that is not .conf must be ignored */
{
char other[700];
snprintf(other, sizeof(other),
"%s/ignored.txt", drop);
write_file(other, "kaputt = 1\n");
}
}
sp_config_init(&c);
if (sp_buf_init(&e) == 0) {
CHECK(sp_config_load(&c, main_c, &e) == 0);
/* 50-late.conf is read after the main file */
CHECK(strcmp(c.theme, "Spider") == 0);
CHECK(strcmp(c.locale, "de") == 0);
sp_buf_free(&e);
}
f = NULL;
(void)f;
{
char cmd[700];
snprintf(cmd, sizeof(cmd), "rm -rf '%s'", dir);
if (system(cmd) != 0)
fprintf(stderr, "cleanup failed\n");
}
}
}
if (failures == 0)
printf("test_config: ok\n");
return failures == 0 ? 0 : 1;
}