One fixed session, born once, kept and compacted forever. The daemon wakes on its own impulse, works on a persistent goal, detects repeated failures, reflects, changes approach, and records everything. It starts as root, builds a debootstrap chroot, then drops to an unprivileged uid and applies Landlock and seccomp. Dashboard, HTTP API and MCP expose the same snapshot; the event log is root-owned and append-only. Offline suites cover the engine, the daemon, both sandbox layers and the dashboard/MCP sockets; a root-only script covers debootstrap, chroot, uid drop and plugin loading.
290 lines
7.1 KiB
C
290 lines
7.1 KiB
C
/*
|
|
* krang -- offline unit tests: config, birth, stuck, snapshot, HTTP, MCP.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
#include "krang.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
|
|
contains(const char *hay, const char *needle)
|
|
{
|
|
return hay != NULL && strstr(hay, needle) != NULL;
|
|
}
|
|
|
|
static void
|
|
init_krang(struct krang *k)
|
|
{
|
|
memset(k, 0, sizeof(*k));
|
|
k->log.fd = -1;
|
|
pthread_mutex_init(&k->lock, NULL);
|
|
pthread_cond_init(&k->cond, NULL);
|
|
sp_buf_init(&k->err);
|
|
kconfig_defaults(&k->cfg);
|
|
snprintf(k->cfg.goal, sizeof(k->cfg.goal), "test the apparatus");
|
|
snprintf(k->cfg.endpoint, sizeof(k->cfg.endpoint),
|
|
"http://127.0.0.1:1/v1");
|
|
snprintf(k->cfg.model, sizeof(k->cfg.model), "test-model");
|
|
snprintf(k->st.state, sizeof(k->st.state), "sleeping");
|
|
snprintf(k->st.goal, sizeof(k->st.goal), "test the apparatus");
|
|
snprintf(k->st.session, sizeof(k->st.session), "krang");
|
|
snprintf(k->st.sandbox_uid, sizeof(k->st.sandbox_uid), "1000");
|
|
snprintf(k->st.sandbox_gid, sizeof(k->st.sandbox_gid), "1000");
|
|
snprintf(k->st.landlock, sizeof(k->st.landlock), "applied");
|
|
snprintf(k->st.seccomp, sizeof(k->st.seccomp), "applied");
|
|
k->st.started_at = k_now();
|
|
k->st.born_at = k->st.started_at;
|
|
}
|
|
|
|
static void
|
|
test_config(void)
|
|
{
|
|
struct kconfig c;
|
|
char path[] = "/tmp/krang-test-conf-XXXXXX";
|
|
char err[512];
|
|
int fd;
|
|
FILE *f;
|
|
|
|
fd = mkstemp(path);
|
|
CHECK(fd >= 0);
|
|
if (fd < 0)
|
|
return;
|
|
f = fdopen(fd, "w");
|
|
CHECK(f != NULL);
|
|
if (f == NULL)
|
|
return;
|
|
fprintf(f, "# a test\n");
|
|
fprintf(f, "goal = keep the apparatus honest # inline comment\n");
|
|
fprintf(f, "endpoint = http://127.0.0.1:1/v1\n");
|
|
fprintf(f, "model = test\n");
|
|
fprintf(f, "impulse_seconds = 7\n");
|
|
fprintf(f, "max_steps = 9\n");
|
|
fprintf(f, "dashboard_port = 18096\n");
|
|
fprintf(f, "seccomp = no\n");
|
|
fclose(f);
|
|
CHECK(kconfig_load(&c, path, err, sizeof(err)) == 0);
|
|
CHECK(strcmp(c.goal, "keep the apparatus honest") == 0);
|
|
CHECK(c.impulse_seconds == 7);
|
|
CHECK(c.max_steps == 9);
|
|
CHECK(c.dashboard_port == 18096);
|
|
CHECK(c.seccomp == 0);
|
|
unlink(path);
|
|
}
|
|
|
|
static void
|
|
test_birth(void)
|
|
{
|
|
struct kconfig c;
|
|
struct sp_buf b;
|
|
|
|
kconfig_defaults(&c);
|
|
snprintf(c.goal, sizeof(c.goal), "map the sandbox");
|
|
sp_buf_init(&b);
|
|
k_birth_prompt(&c, &b);
|
|
CHECK(contains(b.p, "You are krang. You are awake."));
|
|
CHECK(contains(b.p, "no user"));
|
|
CHECK(contains(b.p, "map the sandbox"));
|
|
CHECK(contains(b.p, "Good luck."));
|
|
CHECK(contains(b.p, KRANG_IN_WORKSPACE));
|
|
sp_buf_free(&b);
|
|
}
|
|
|
|
static void
|
|
test_stuck(void)
|
|
{
|
|
struct kstuck s;
|
|
|
|
memset(&s, 0, sizeof(s));
|
|
kstuck_call(&s, "run", "{\"command\":\"false\"}");
|
|
kstuck_call(&s, "run", "{\"command\":\"false\"}");
|
|
CHECK(s.repeated_calls >= 1);
|
|
kstuck_result(&s, "run", "error: nope", 1);
|
|
kstuck_result(&s, "run", "error: nope", 1);
|
|
CHECK(s.same_error >= 2);
|
|
CHECK(kstuck_level(&s) >= 1);
|
|
CHECK(kstuck_should_check(&s, k_now()) >= 1);
|
|
kstuck_reset(&s);
|
|
CHECK(s.level == 0);
|
|
CHECK(s.attempts == 0);
|
|
}
|
|
|
|
static void
|
|
test_state(void)
|
|
{
|
|
struct krang k;
|
|
struct sp_buf b;
|
|
|
|
init_krang(&k);
|
|
sp_buf_init(&b);
|
|
kstate_json(&k, &b);
|
|
CHECK(contains(b.p, "\"state\":\"sleeping\""));
|
|
CHECK(contains(b.p, "\"goal\":\"test the apparatus\""));
|
|
CHECK(contains(b.p, "\"version\":\"0.1.0\""));
|
|
CHECK(contains(b.p, "\"sandbox\":"));
|
|
CHECK(contains(b.p, "\"config\":"));
|
|
sp_buf_free(&b);
|
|
|
|
sp_buf_init(&b);
|
|
kstate_impulses_json(&k, &b, 10);
|
|
CHECK(strcmp(b.p, "[]") == 0);
|
|
sp_buf_free(&b);
|
|
|
|
sp_buf_init(&b);
|
|
kstate_tools_json(&k, &b);
|
|
CHECK(strcmp(b.p, "[]") == 0);
|
|
sp_buf_free(&b);
|
|
pthread_mutex_destroy(&k.lock);
|
|
pthread_cond_destroy(&k.cond);
|
|
sp_buf_free(&k.err);
|
|
}
|
|
|
|
static void
|
|
test_birth_flow(void)
|
|
{
|
|
struct krang k;
|
|
struct sp_session_info si;
|
|
struct sp_buf err, logjson;
|
|
char tmpl[] = "/tmp/krang-birth-XXXXXX";
|
|
char *dir;
|
|
char goal[KRANG_GOAL];
|
|
int newborn = -1;
|
|
|
|
dir = mkdtemp(tmpl);
|
|
CHECK(dir != NULL);
|
|
if (dir == NULL)
|
|
return;
|
|
memset(&k, 0, sizeof(k));
|
|
k.log.fd = -1;
|
|
pthread_mutex_init(&k.lock, NULL);
|
|
pthread_cond_init(&k.cond, NULL);
|
|
sp_buf_init(&k.err);
|
|
sp_buf_init(&err);
|
|
kconfig_defaults(&k.cfg);
|
|
snprintf(k.cfg.goal, sizeof(k.cfg.goal), "be born");
|
|
snprintf(k.session_dir, sizeof(k.session_dir), "%s/sessions", dir);
|
|
(void)mkdir(k.session_dir, 0700);
|
|
{
|
|
char lp[700];
|
|
|
|
snprintf(lp, sizeof(lp), "%s/events.jsonl", dir);
|
|
CHECK(klog_open(&k.log, lp, k.last_error,
|
|
sizeof(k.last_error)) == 0);
|
|
}
|
|
CHECK(kdaemon_session_open(&k, &newborn, goal, sizeof(goal),
|
|
k.last_error, sizeof(k.last_error)) == 0);
|
|
CHECK(newborn == 1);
|
|
CHECK(strcmp(goal, "be born") == 0);
|
|
CHECK(k.st.born_at > 0);
|
|
CHECK(sp_session_open(&si, k.session_dir, "krang", &err) == 0);
|
|
CHECK(sp_session_append(&si, "user", "first breath", &err) == 0);
|
|
sp_buf_free(&err);
|
|
|
|
newborn = -1;
|
|
CHECK(kdaemon_session_open(&k, &newborn, goal, sizeof(goal),
|
|
k.last_error, sizeof(k.last_error)) == 0);
|
|
CHECK(newborn == 0);
|
|
CHECK(strcmp(goal, "be born") == 0);
|
|
|
|
snprintf(k.cfg.goal, sizeof(k.cfg.goal), "a different goal");
|
|
CHECK(kdaemon_session_open(&k, &newborn, goal, sizeof(goal),
|
|
k.last_error, sizeof(k.last_error)) == 0);
|
|
CHECK(strcmp(goal, "be born") == 0);
|
|
sp_buf_init(&logjson);
|
|
klog_json(&k.log, &logjson, 20);
|
|
CHECK(contains(logjson.p, "config_goal_ignored"));
|
|
sp_buf_free(&logjson);
|
|
klog_close(&k.log);
|
|
pthread_mutex_destroy(&k.lock);
|
|
pthread_cond_destroy(&k.cond);
|
|
sp_buf_free(&k.err);
|
|
}
|
|
|
|
static void
|
|
test_http(void)
|
|
{
|
|
struct krang k;
|
|
struct sp_buf b;
|
|
|
|
init_krang(&k);
|
|
sp_buf_init(&b);
|
|
khttp_handle_buffer(&k, "GET /healthz HTTP/1.1\r\nHost: x\r\n\r\n",
|
|
&b);
|
|
CHECK(contains(b.p, "200 OK"));
|
|
CHECK(contains(b.p, "\"ok\":true"));
|
|
sp_buf_free(&b);
|
|
|
|
sp_buf_init(&b);
|
|
khttp_handle_buffer(&k, "GET /api/state HTTP/1.1\r\nHost: x\r\n\r\n",
|
|
&b);
|
|
CHECK(contains(b.p, "200 OK"));
|
|
CHECK(contains(b.p, "\"state\":\"sleeping\""));
|
|
sp_buf_free(&b);
|
|
|
|
sp_buf_init(&b);
|
|
khttp_handle_buffer(&k, "GET /nope HTTP/1.1\r\nHost: x\r\n\r\n", &b);
|
|
CHECK(contains(b.p, "404 Not Found"));
|
|
sp_buf_free(&b);
|
|
pthread_mutex_destroy(&k.lock);
|
|
pthread_cond_destroy(&k.cond);
|
|
sp_buf_free(&k.err);
|
|
}
|
|
|
|
static void
|
|
test_mcp(void)
|
|
{
|
|
struct krang k;
|
|
char *reply;
|
|
|
|
init_krang(&k);
|
|
kmcp_prepare(&k);
|
|
reply = kmcp_handle(&k,
|
|
"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/list\"}");
|
|
CHECK(reply != NULL);
|
|
CHECK(contains(reply, "krang_state"));
|
|
CHECK(contains(reply, "krang_say"));
|
|
free(reply);
|
|
reply = kmcp_handle(&k,
|
|
"{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/call\","
|
|
"\"params\":{\"name\":\"krang_state\",\"arguments\":{}}}");
|
|
CHECK(reply != NULL);
|
|
CHECK(contains(reply, "sleeping"));
|
|
free(reply);
|
|
pthread_mutex_destroy(&k.lock);
|
|
pthread_cond_destroy(&k.cond);
|
|
sp_buf_free(&k.err);
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
test_config();
|
|
test_birth();
|
|
test_stuck();
|
|
test_birth_flow();
|
|
test_state();
|
|
test_http();
|
|
test_mcp();
|
|
if (failures != 0) {
|
|
fprintf(stderr, "%d test(s) failed\n", failures);
|
|
return 1;
|
|
}
|
|
printf("test_krang: ok\n");
|
|
return 0;
|
|
}
|