Files
krang/tests/test_server.c
T
lotherk fb6f97ba8e krang: autonomous agent daemon on libsplinter
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.
2026-11-21 17:00:12 +00:00

136 lines
3.5 KiB
C

/*
* krang -- offline socket test: the dashboard and MCP listeners.
*
* Copyright (c) 2026 Konrad Lother
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*/
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.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
get_port(int fd)
{
struct sockaddr_in sa;
socklen_t sl = sizeof(sa);
if (getsockname(fd, (struct sockaddr *)&sa, &sl) != 0)
return -1;
return ntohs(sa.sin_port);
}
static int
roundtrip(int port, const char *request, char *out, size_t cap, int line)
{
struct sockaddr_in sa;
int fd;
size_t used = 0;
ssize_t n;
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
return -1;
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons((unsigned short)port);
sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) != 0) {
close(fd);
return -1;
}
if (k_write_all(fd, request, strlen(request)) != 0) {
close(fd);
return -1;
}
while (used + 1 < cap &&
(n = read(fd, out + used, cap - used - 1)) > 0) {
used += (size_t)n;
if (line && memchr(out, '\n', used) != NULL)
break;
}
out[used] = '\0';
close(fd);
return 0;
}
int
main(void)
{
struct krang k;
char err[512], req[512], out[8192];
int port;
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.dashboard_host, sizeof(k.cfg.dashboard_host),
"127.0.0.1");
k.cfg.dashboard_port = 0;
snprintf(k.cfg.mcp_host, sizeof(k.cfg.mcp_host), "127.0.0.1");
k.cfg.mcp_port = 0;
snprintf(k.st.state, sizeof(k.st.state), "sleeping");
snprintf(k.st.goal, sizeof(k.st.goal), "test the server");
k.st.started_at = k_now();
CHECK(khttp_start(&k, err, sizeof(err)) == 0);
port = get_port(k.http_fd);
CHECK(port > 0);
snprintf(req, sizeof(req),
"GET /healthz HTTP/1.1\r\nHost: localhost\r\n\r\n");
CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0);
CHECK(strstr(out, "200 OK") != NULL);
CHECK(strstr(out, "\"ok\":true") != NULL);
snprintf(req, sizeof(req), "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n");
CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0);
CHECK(strstr(out, "200 OK") != NULL);
CHECK(strstr(out, "<title>krang</title>") != NULL);
snprintf(req, sizeof(req),
"GET /api/state HTTP/1.1\r\nHost: localhost\r\n\r\n");
CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0);
CHECK(strstr(out, "\"goal\":\"test the server\"") != NULL);
khttp_stop(&k);
CHECK(kmcp_start(&k, err, sizeof(err)) == 0);
port = get_port(k.mcp_fd);
CHECK(port > 0);
snprintf(req, sizeof(req),
"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/list\"}\n");
CHECK(roundtrip(port, req, out, sizeof(out), 1) == 0);
CHECK(strstr(out, "krang_state") != NULL);
snprintf(req, sizeof(req),
"{\"jsonrpc\":\"2.0\",\"id\":2,\"method\":\"tools/call\","
"\"params\":{\"name\":\"krang_state\",\"arguments\":{}}}\n");
CHECK(roundtrip(port, req, out, sizeof(out), 1) == 0);
CHECK(strstr(out, "sleeping") != NULL);
kmcp_stop(&k);
pthread_mutex_destroy(&k.lock);
pthread_cond_destroy(&k.cond);
sp_buf_free(&k.err);
if (failures != 0) {
fprintf(stderr, "%d test(s) failed\n", failures);
return 1;
}
printf("test_server: ok\n");
return 0;
}