Files
CST-agent/tests/test_undo.c
T
WHC 2a43ae6622 Windows: official port -- UCRT static build, UTF-8 throughout, Low IL sandbox, ncurses TUI, installer; v0.6.0
Build
  * cross build switches to pkgsCross.ucrt64 (shell-win.nix); curl (Schannel),
    winpthreads and ncurses 6.6 are linked statically, so the release is
    cst-agent.exe + busybox.exe (busybox-w32 Unicode) with no extra DLLs
  * windres embeds a manifest with activeCodePage=UTF-8 and a version resource
  * ncurses 6.6 Windows console driver patches (mouse button state, A_DIM,
    wide-char declarations): scripts/patches/
  * scripts/bootstrap_msys2.sh builds the same release natively in MSYS2
    UCRT64; PR #1's MINGW64 notes now point there
  * vendor/tre-regex (musl's TRE) provides <regex.h> on MinGW

UTF-8
  * paths cross the system call boundary as UTF-16 (*W APIs); console code
    pages set to 65001 and restored on exit; files are read and written in
    binary mode
  * child output that is not valid UTF-8 is decoded from the console, OEM or
    ANSI system code page
  * read_file hides CR; edit_file, apply_patch and write_file keep CRLF line
    endings and a UTF-8 BOM

OS layer and processes
  * os_dir_* and cst_fnmatch replace the POSIX-only directory walks in
    subagent, plugins, skills, undo and fs_tools; realpath / path_under and
    symlink rejection work on Windows
  * every child goes through CreateProcessW with its own environment block
    inside a Job Object, so timeouts and Esc end the whole process tree

Sandbox (auto mode, "auto · lowil")
  * run_shell runs with a restricted Low integrity token; the workspace gets
    an inheritable Low label once, the sandbox TEMP is labeled Low, and the
    credentials file gets a no-read-up label
  * drive roots, the profile and system folders are never labeled
  * network commands still ask, since Low IL does not block the network;
    Access denied and MSYS2's 0xc0000142 offer a rerun outside the sandbox
  * /sandbox lists labeled folders, /sandbox reset-labels restores them

TUI
  * raster console fonts are switched to Consolas / NSimSun while running
  * the home logo falls back to text when the console font lacks U+2580
  * mouse drag copy goes to the Windows clipboard

Distribution
  * scripts/install_windows.ps1 installs a Gitea release into
    %LOCALAPPDATA%\Programs\cst-agent and adds it to the user PATH;
    cst-agent-update.cmd reruns it; scripts/release_windows.sh uploads the
    zip with the token taken from the environment
  * scripts/run_windows.bat is gone; docs/README-windows.md is rewritten

Tests
  * new test_lowil, test_win_utf8 and test_os_dir; tests made portable
    (cst_test_util.h)
  * scripts/win_test.sh and scripts/winvm/ run the suite on English and
    Chinese Windows Server VMs as Administrator, a standard user and a
    Chinese user name: 29/29 on all six
2026-10-06 23:08:14 +08:00

194 lines
7.4 KiB
C

/* Undo snapshots, persistent permission rules and session store helpers. */
#define _POSIX_C_SOURCE 200809L
#include "cst_agent.h"
#include "cst_perms.h"
#include "cst_undo.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "cst_test_util.h"
static int nfail;
#define EXPECT(cond, msg) \
do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, msg); \
nfail++; \
} \
} while (0)
static char g_ws[256];
static void wfile(const char *rel, const char *s) {
char p[512];
snprintf(p, sizeof(p), "%s/%s", g_ws, rel);
FILE *fp = fopen(p, "wb");
if (fp) {
fputs(s, fp);
fclose(fp);
}
}
static int rfile_eq(const char *rel, const char *want) {
char p[512], buf[256] = {0};
snprintf(p, sizeof(p), "%s/%s", g_ws, rel);
FILE *fp = fopen(p, "rb");
if (!fp)
return want == NULL;
size_t n = fread(buf, 1, sizeof(buf) - 1, fp);
fclose(fp);
buf[n] = 0;
return want && strcmp(buf, want) == 0;
}
static void test_undo(void) {
const char *sid = "s1";
wfile("a.txt", "v1\n");
EXPECT(cst_undo_count(g_ws, sid) == 0, "no undo yet");
/* turn 1: edit a.txt twice, create new.txt */
EXPECT(cst_undo_snapshot(g_ws, sid, "t1", "a.txt") == 0, "snap a");
wfile("a.txt", "v2\n");
EXPECT(cst_undo_snapshot(g_ws, sid, "t1", "a.txt") == 0, "snap a again");
wfile("a.txt", "v3\n");
EXPECT(cst_undo_snapshot(g_ws, sid, "t1", "new.txt") == 0, "snap absent");
wfile("new.txt", "hello\n");
EXPECT(cst_undo_count(g_ws, sid) == 1, "one turn");
/* turn 2: edit a.txt again */
EXPECT(cst_undo_snapshot(g_ws, sid, "t2", "a.txt") == 0, "snap t2");
wfile("a.txt", "v4\n");
EXPECT(cst_undo_count(g_ws, sid) == 2, "two turns");
char rep[1024];
EXPECT(cst_undo_restore_last(g_ws, sid, rep, sizeof(rep)) == 0, "undo t2");
EXPECT(rfile_eq("a.txt", "v3\n"), "a back to v3");
EXPECT(rfile_eq("new.txt", "hello\n"), "new.txt kept by t2 undo");
EXPECT(strstr(rep, "a.txt") != NULL, "report names a.txt");
EXPECT(cst_undo_restore_last(g_ws, sid, rep, sizeof(rep)) == 0, "undo t1");
EXPECT(rfile_eq("a.txt", "v1\n"), "a back to v1 (first snapshot wins)");
EXPECT(rfile_eq("new.txt", NULL), "new.txt removed");
EXPECT(cst_undo_count(g_ws, sid) == 0, "nothing left");
EXPECT(cst_undo_restore_last(g_ws, sid, rep, sizeof(rep)) != 0,
"undo with nothing fails");
/* subdirectory file that did not exist, parent dirs created later */
EXPECT(cst_undo_snapshot(g_ws, sid, "t3", "d/e/f.txt") == 0, "snap nested");
char p[512];
snprintf(p, sizeof(p), "%s/d", g_ws);
test_mkdir(p, 0755);
snprintf(p, sizeof(p), "%s/d/e", g_ws);
test_mkdir(p, 0755);
wfile("d/e/f.txt", "x");
EXPECT(cst_undo_restore_last(g_ws, sid, rep, sizeof(rep)) == 0, "undo t3");
EXPECT(rfile_eq("d/e/f.txt", NULL), "nested file removed");
/* escaping paths are refused */
EXPECT(cst_undo_snapshot(g_ws, sid, "t4", "../evil") != 0, "reject ..");
EXPECT(cst_undo_snapshot(g_ws, sid, "t4", "/etc/passwd") != 0,
"reject absolute");
}
static void test_perms(void) {
cst_perms_t *p = cst_perms_open(g_ws);
EXPECT(p != NULL, "open perms");
EXPECT(!cst_perms_tool_allowed(p, "write_file"), "empty rules");
EXPECT(cst_perms_allow_tool(p, "write_file") == 0, "allow tool");
EXPECT(cst_perms_allow_prefix(p, "git status") == 0, "allow prefix");
cst_perms_close(p);
p = cst_perms_open(g_ws);
EXPECT(cst_perms_tool_allowed(p, "write_file"), "tool rule persisted");
EXPECT(!cst_perms_tool_allowed(p, "delete_file"), "other tool not allowed");
EXPECT(cst_perms_shell_allowed(p, "git status"), "exact prefix");
EXPECT(cst_perms_shell_allowed(p, "git status -s"), "prefix + args");
EXPECT(!cst_perms_shell_allowed(p, "git statusx"), "word boundary");
EXPECT(!cst_perms_shell_allowed(p, "git status; rm -rf ~"), "meta ;");
EXPECT(!cst_perms_shell_allowed(p, "git status && make"), "meta &&");
EXPECT(!cst_perms_shell_allowed(p, "git status $(id)"), "meta $(");
EXPECT(!cst_perms_shell_allowed(p, "git status > x"), "meta >");
EXPECT(!cst_perms_shell_allowed(p, "git status\nrm x"), "newline");
char *d = cst_perms_describe(p);
EXPECT(d && strstr(d, "git status") && strstr(d, "write_file"), "describe");
free(d);
EXPECT(cst_perms_reset(p) == 0, "reset");
EXPECT(!cst_perms_tool_allowed(p, "write_file"), "reset clears tools");
EXPECT(!cst_perms_shell_allowed(p, "git status"), "reset clears prefixes");
cst_perms_close(p);
char out[128];
cst_perms_suggest_prefix("git commit -m 'x'", out, sizeof(out));
EXPECT(strcmp(out, "git commit") == 0, "git two words");
cst_perms_suggest_prefix(" ls -la src", out, sizeof(out));
EXPECT(strcmp(out, "ls") == 0, "ls one word");
cst_perms_suggest_prefix("make -j8", out, sizeof(out));
EXPECT(strcmp(out, "make") == 0, "make");
cst_perms_suggest_prefix("rm -rf build", out, sizeof(out));
EXPECT(out[0] == 0, "no rule for rm");
cst_perms_suggest_prefix("sudo make install", out, sizeof(out));
EXPECT(out[0] == 0, "no rule for sudo");
cst_perms_suggest_prefix("python3 -c 'print(1)'", out, sizeof(out));
EXPECT(out[0] == 0, "no rule for python3 -c");
cst_perms_suggest_prefix("python3 -m pytest", out, sizeof(out));
EXPECT(out[0] == 0, "no rule for python3 -m");
cst_perms_suggest_prefix("npm test", out, sizeof(out));
EXPECT(strcmp(out, "npm test") == 0, "npm test");
}
static void test_store(void) {
EXPECT(cst_agent_store_ensure(g_ws) == 0, "store ensure");
char path[1024];
EXPECT(cst_agent_store_path_for_id(g_ws, "20260101-000000-abcd", path,
sizeof(path)) == 0,
"path for id");
FILE *fp = fopen(path, "wb");
EXPECT(fp != NULL, "create session file");
if (fp) {
fputs("{\"role\":\"system\",\"content\":\"sys\"}\n", fp);
fputs("{\"role\":\"user\",\"content\":\"fix the\\nbuild\"}\n", fp);
fputs("{\"role\":\"assistant\",\"content\":\"ok\"}\n", fp);
fclose(fp);
}
cst_agent_store_set_current(g_ws, "20260101-000000-abcd");
cst_session_info_t in;
EXPECT(cst_agent_store_info(g_ws, "20260101-000000-abcd", &in) == 0, "info");
EXPECT(in.messages == 3, "message count");
EXPECT(strcmp(in.first_user, "fix the build") == 0, "first user line");
EXPECT(in.title[0] == 0, "no title yet");
EXPECT(cst_agent_store_title_set(g_ws, "20260101-000000-abcd", "Build fix") ==
0,
"set title");
cst_agent_store_info(g_ws, "20260101-000000-abcd", &in);
EXPECT(strcmp(in.title, "Build fix") == 0, "title read back");
EXPECT(cst_agent_store_title_set(g_ws, "../x", "bad") != 0, "bad id");
EXPECT(cst_agent_store_delete(g_ws, "20260101-000000-abcd") == 0, "delete");
EXPECT(access(path, F_OK) != 0, "session file gone");
char cur[128];
EXPECT(cst_agent_store_get_current(g_ws, cur, sizeof(cur)) != 0,
"CURRENT cleared");
}
int main(void) {
if (test_tmpdir(g_ws, sizeof(g_ws), "cst_test_undo") != 0) {
perror("mkdtemp");
return 1;
}
test_undo();
test_perms();
test_store();
if (test_rmrf(g_ws) != 0)
fprintf(stderr, "warning: cleanup failed\n");
if (nfail) {
fprintf(stderr, "test_undo: %d failure(s)\n", nfail);
return 1;
}
printf("test_undo: ok\n");
return 0;
}