/* * 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 #include #include #include #include #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" "cli_env = CLAUDE_CODE_OAUTH_TOKEN=tok=with=eq\n" "mcp = junction | url=http://127.0.0.1:8090/mcp | " "header=Authorization: Bearer abc | enabled=0\n" "mcp = files | cmd=npx -y server-filesystem /home\n" "delegate = c2 | zweites Konto | kind=cli | cmd=claude -p | " "env=CLAUDE_CODE_OAUTH_TOKEN=abc | env=FOO=bar baz\n" "system_prompt = sei knapp\n" "tools_root = /home/konrad/src\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(c.ncli_env == 1); CHECK(strcmp(c.cli_env[0], "CLAUDE_CODE_OAUTH_TOKEN=tok=with=eq") == 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(strcmp(c.mcp[0].header, "Authorization: Bearer abc") == 0); CHECK(c.mcp[0].enabled == 0); 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 == 1); /* on unless said otherwise */ CHECK(c.ndelegates == 1); CHECK(c.delegate[0].nenv == 2); CHECK(strcmp(c.delegate[0].env[0], "CLAUDE_CODE_OAUTH_TOKEN=abc") == 0); CHECK(strcmp(c.delegate[0].env[1], "FOO=bar baz") == 0); sp_config_apply_env((const char (*)[SP_CONFIG_ENV_LEN])c.delegate[0].env, c.delegate[0].nenv); CHECK(getenv("FOO") != NULL && strcmp(getenv("FOO"), "bar baz") == 0); CHECK(strcmp(c.system_prompt, "sei knapp") == 0); CHECK(strcmp(c.tools_root, "/home/konrad/src") == 0); /* untouched defaults survive */ CHECK(strcmp(c.key_env, "SPLINTER_API_KEY") == 0); CHECK(c.tools_root[0] != '\0'); /* set above, not reset */ /* ---- a leading ~ is the home directory, not a directory named "~" --- */ { const char *home = getenv("HOME"); write_file(path, "session_dir = ~/.splinter\n" "tools_root = ~/src\n" "skills_dir = ~/skills\n" "state_dir = /tmp/state\n"); sp_config_init(&c); sp_buf_clear(&err); CHECK(sp_config_load(&c, path, &err) == 0); if (home != NULL && *home != '\0') { char want[512]; snprintf(want, sizeof(want), "%s/.splinter", home); CHECK(strcmp(c.session_dir, want) == 0); snprintf(want, sizeof(want), "%s/src", home); CHECK(strcmp(c.tools_root, want) == 0); snprintf(want, sizeof(want), "%s/skills", home); CHECK(strcmp(c.skills_dir[0], want) == 0); } /* an absolute path is left exactly as written */ CHECK(strcmp(c.state_dir, "/tmp/state") == 0); } /* ---- a leading ~ is the home directory, not a directory named "~" --- */ { const char *home = getenv("HOME"); write_file(path, "session_dir = ~/.splinter\n" "skills_dir = ~/skills\n" "state_dir = /tmp/state\n"); sp_config_init(&c); sp_buf_clear(&err); CHECK(sp_config_load(&c, path, &err) == 0); if (home != NULL && *home != '\0') { char want[512]; snprintf(want, sizeof(want), "%s/.splinter", home); CHECK(strcmp(c.session_dir, want) == 0); snprintf(want, sizeof(want), "%s/skills", home); CHECK(strcmp(c.skills_dir[0], want) == 0); } /* an absolute path is left exactly as written */ CHECK(strcmp(c.state_dir, "/tmp/state") == 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); /* ---- env without a proper NAME= is refused ---- */ write_file(path, "cli_env = 1BAD=x\n"); sp_config_init(&c); sp_buf_clear(&err); CHECK(sp_config_load(&c, path, &err) == -1); CHECK(strstr(err.p, "NAME=VALUE") != 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, "light") == 0); /* the default theme */ CHECK(c.steps == 150); 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") == 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"); } } } /* a cli provider takes its model from -m, its prefix from provider = */ { struct sp_delegate d; char out[400]; memset(&d, 0, sizeof(d)); snprintf(d.cmd, sizeof(d.cmd), "opencode run"); CHECK(sp_cli_command(&d, NULL, out, sizeof(out)) == 0 && strcmp(out, "opencode run") == 0); snprintf(d.cli_provider, sizeof(d.cli_provider), "opencode"); snprintf(d.model, sizeof(d.model), "big-pickle"); CHECK(sp_cli_command(&d, NULL, out, sizeof(out)) == 0 && strcmp(out, "opencode run --model opencode/big-pickle") == 0); CHECK(sp_cli_command(&d, "deepseek-v4.1-flash-free", out, sizeof(out)) == 0 && strcmp(out, "opencode run --model " "opencode/deepseek-v4.1-flash-free") == 0); CHECK(sp_cli_command(&d, "opencode-go/kimi-k2", out, sizeof(out)) == 0 && strcmp(out, "opencode run --model opencode-go/kimi-k2") == 0); snprintf(d.cmd, sizeof(d.cmd), "claude -p --model {model} --x"); d.cli_provider[0] = '\0'; CHECK(sp_cli_command(&d, "opus", out, sizeof(out)) == 0 && strcmp(out, "claude -p --model opus --x") == 0); CHECK(sp_cli_command(&d, "a;rm -rf ~", out, sizeof(out)) == -1); CHECK(sp_cli_command(&d, "a'b", out, sizeof(out)) == -1); } if (failures == 0) printf("test_config: ok\n"); return failures == 0 ? 0 : 1; }