/* * 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 #include #include #include #include #include #include #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, "krang") != 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); /* a page reached through DNS rebinding carries a foreign Host */ snprintf(req, sizeof(req), "GET /api/state HTTP/1.1\r\nHost: evil.example\r\n\r\n"); CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0); CHECK(strstr(out, "403 Forbidden") != NULL); snprintf(req, sizeof(req), "GET /api/state HTTP/1.1\r\nHost: 127.evil.example\r\n\r\n"); CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0); CHECK(strstr(out, "403 Forbidden") != NULL); snprintf(req, sizeof(req), "GET /api/state HTTP/1.1\r\nHost: 127.0.0.1:8096\r\n\r\n"); CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0); CHECK(strstr(out, "200 OK") != NULL); /* cross-origin requests are refused, same-origin ones are not */ snprintf(req, sizeof(req), "POST /api/say HTTP/1.1\r\nHost: localhost\r\n" "Origin: http://evil.example\r\n" "Content-Type: application/json\r\nContent-Length: 12\r\n" "\r\n{\"text\":\"x\"}"); CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0); CHECK(strstr(out, "403 Forbidden") != NULL); snprintf(req, sizeof(req), "GET /api/state HTTP/1.1\r\nHost: localhost\r\n" "Origin: http://localhost\r\n\r\n"); CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0); CHECK(strstr(out, "200 OK") != NULL); /* with a token, OPTIONS is not a free pass either */ snprintf(k.cfg.token, sizeof(k.cfg.token), "s3cret"); snprintf(req, sizeof(req), "OPTIONS /api/state HTTP/1.1\r\nHost: localhost\r\n" "Origin: http://localhost\r\n\r\n"); CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0); CHECK(strstr(out, "401 Unauthorized") != NULL); snprintf(req, sizeof(req), "GET /api/state HTTP/1.1\r\nHost: localhost\r\n" "Authorization: Bearer s3cret\r\n\r\n"); CHECK(roundtrip(port, req, out, sizeof(out), 0) == 0); CHECK(strstr(out, "200 OK") != NULL); k.cfg.token[0] = '\0'; 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; }