Files
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

115 lines
3.1 KiB
C

/*
* splinter -- skills on demand.
*
* The point of skills is that they are NOT in the system prompt, so the test
* checks the two halves that make that safe: search finds a skill by a word in
* its description, and load returns the file's text - not a summary, not an
* empty string.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "splinter.h"
#include "skills.h"
#include "config.h"
static int failures;
#define CHECK(c) do { if (!(c)) { fprintf(stderr, "FAIL %s:%d: %s\n", \
__FILE__, __LINE__, #c); 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_registry r;
struct sp_buf out;
struct sp_config cfg;
char home[512], dir[600], path[700];
const char *tmp = getenv("TMPDIR");
const struct sp_tool *t;
if (tmp == NULL || *tmp == '\0')
tmp = "/var/tmp";
snprintf(home, sizeof(home), "%s/splinter-skill-XXXXXX", tmp);
CHECK(mkdtemp(home) != NULL);
snprintf(dir, sizeof(dir), "%s/.splinter", home);
CHECK(mkdir(dir, 0700) == 0);
snprintf(dir, sizeof(dir), "%s/.splinter/skills", home);
CHECK(mkdir(dir, 0700) == 0);
snprintf(path, sizeof(path), "%s/c99.md", dir);
write_file(path, "---\nname: c99\ndescription: C99 und POSIX, ohne Extensions\n---\n\n"
"# C99\n\nRegel eins: kein VLA.\n");
snprintf(path, sizeof(path), "%s/kanban", dir);
CHECK(mkdir(path, 0700) == 0);
snprintf(path, sizeof(path), "%s/kanban/SKILL.md", dir);
write_file(path, "Karten fuer Agenten.\n");
CHECK(setenv("HOME", home, 1) == 0);
memset(&r, 0, sizeof(r));
memset(&cfg, 0, sizeof(cfg));
CHECK(sp_buf_init(&out) == 0);
CHECK(sp_skills_register(&r, &cfg) == 0);
t = sp_registry_find(&r, "skill_search");
CHECK(t != NULL);
CHECK(t != NULL && t->auth == SP_AUTH_READ);
sp_buf_clear(&out);
CHECK(t->run("{\"query\":\"posix\"}", &out, NULL) == 0);
CHECK(strstr(out.p, "c99") != NULL);
CHECK(strstr(out.p, "POSIX") != NULL);
/* the directory form is found by its name */
sp_buf_clear(&out);
CHECK(t->run("{\"query\":\"kanban\"}", &out, NULL) == 0);
CHECK(strstr(out.p, "kanban") != NULL);
/* a miss says so instead of pretending */
sp_buf_clear(&out);
CHECK(t->run("{\"query\":\"gibtsnicht\"}", &out, NULL) == 0);
CHECK(strstr(out.p, "nothing matches") != NULL);
/* load returns the body */
t = sp_registry_find(&r, "skill_load");
CHECK(t != NULL);
sp_buf_clear(&out);
CHECK(t->run("{\"name\":\"c99\"}", &out, NULL) == 0);
CHECK(strstr(out.p, "Regel eins: kein VLA.") != NULL);
/* an unknown name lists what exists */
sp_buf_clear(&out);
CHECK(t->run("{\"name\":\"nope\"}", &out, NULL) == 0);
CHECK(strstr(out.p, "no skill 'nope'") != NULL);
sp_buf_free(&out);
/* the registry owns what sp_registry_add copied: free it, or the
* test leaks what production never does */
sp_registry_free(&r);
{
char cmd[700];
snprintf(cmd, sizeof(cmd), "rm -rf '%s'", home);
if (system(cmd) != 0)
fprintf(stderr, "cleanup failed\n");
}
if (failures == 0)
printf("test_skills: ok\n");
return failures == 0 ? 0 : 1;
}