/* * krang -- one scripted engine turn under Landlock and seccomp. * * Copyright (c) 2026 Konrad Lother * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * This is the test that matters for the daemon: the engine opens files, * starts threads, calls the mock transport and appends a session while both * one-way layers are already active. No network, no model, no root. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include "krang.h" static int child(const char *ws, const char *st, const char *tmp) { struct krang k; char err[512], script[700], logpath[800]; struct sp_buf sp; FILE *f; int rc; if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) { perror("no_new_privs"); return 1; } if (ksandbox_apply_landlock(ws, st, tmp, 1, err, sizeof(err)) < 0) { fprintf(stderr, "landlock: %s\n", err); return 1; } if (ksandbox_apply_seccomp(1, err, sizeof(err)) < 0) { fprintf(stderr, "seccomp: %s\n", err); return 1; } 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), "survive the layers"); 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.session_dir, sizeof(k.session_dir), "%s/sessions", st); if (mkdir(k.session_dir, 0700) != 0 && errno != EEXIST) { fprintf(stderr, "mkdir sessions: %s\n", strerror(errno)); return 1; } snprintf(logpath, sizeof(logpath), "%s/events.jsonl", st); if (klog_open(&k.log, logpath, err, sizeof(err)) != 0) { fprintf(stderr, "log: %s\n", err); return 1; } sp_buf_init(&sp); k_system_prompt(&k, &sp); snprintf(k.system_prompt, sizeof(k.system_prompt), "%s", sp.p != NULL ? sp.p : ""); sp_buf_free(&sp); snprintf(script, sizeof(script), "%s/script.txt", tmp); f = fopen(script, "w"); if (f == NULL) { fprintf(stderr, "script: %s\n", strerror(errno)); return 1; } fprintf(f, "tool echo {\"text\":\"layers\"}\n"); fprintf(f, "answer sandbox-engine-ok\n"); fclose(f); if (kengine_open(&k, script, err, sizeof(err)) != 0) { fprintf(stderr, "engine: %s\n", err); return 1; } rc = krun_turn(&k, "prove the layers", "test", NULL); if (rc != 0 || strstr(k.st.last_outcome, "sandbox-engine-ok") == NULL) { fprintf(stderr, "turn: rc=%d outcome=%s\n", rc, k.st.last_outcome); return 1; } kengine_close(&k); klog_close(&k.log); pthread_mutex_destroy(&k.lock); pthread_cond_destroy(&k.cond); sp_buf_free(&k.err); return 0; } int main(void) { char root[] = "/tmp/krang-sandbox-engine-XXXXXX"; char ws[600], st[600], tmp[600]; pid_t pid; int status; if (mkdtemp(root) == NULL) { perror("mkdtemp"); return 1; } snprintf(ws, sizeof(ws), "%s/workspace", root); snprintf(st, sizeof(st), "%s/state", root); snprintf(tmp, sizeof(tmp), "%s/tmp", root); (void)mkdir(ws, 0700); (void)mkdir(st, 0700); (void)mkdir(tmp, 0700); (void)setenv("HOME", ws, 1); pid = fork(); if (pid < 0) { perror("fork"); return 1; } if (pid == 0) _exit(child(ws, st, tmp)); if (waitpid(pid, &status, 0) < 0) { perror("waitpid"); return 1; } if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { fprintf(stderr, "test_sandbox_engine: child failed (%d)\n", status); return 1; } printf("test_sandbox_engine: ok\n"); return 0; }