Files
Hermit/tests/test_diff.c
T
WHC 2a43ae6622
ci / linux (clang) (push) Canceled after 0s
ci / linux (gcc) (push) Canceled after 0s
ci / sanitizers (push) Canceled after 0s
ci / musl (push) Canceled after 0s
ci / macos (push) Canceled after 0s
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

237 lines
6.6 KiB
C

/* Built-in unified diff: GNU `diff -u` layout on unambiguous cases, and a
* randomized round trip through our own patcher. */
#define _POSIX_C_SOURCE 200809L
#include "cst_agent.h"
#include "cst_tools.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.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 void put(const char *p, const char *s) {
FILE *fp = fopen(p, "wb");
if (fp) {
fputs(s, fp);
fclose(fp);
}
}
/* GNU diff output, or NULL when diff is not installed. */
static char *gnu_diff(const char *a, const char *b) {
#ifdef _WIN32
(void)a;
(void)b;
return NULL; /* no GNU diff to compare against */
#else
char pa[] = "/tmp/cstdiffaXXXXXX", pb[] = "/tmp/cstdiffbXXXXXX";
int fa = mkstemp(pa), fb = mkstemp(pb);
if (fa < 0 || fb < 0)
return NULL;
close(fa);
close(fb);
put(pa, a);
put(pb, b);
char cmd[256];
snprintf(cmd, sizeof(cmd),
"LC_ALL=C diff -u --label a/f --label b/f %s %s 2>/dev/null", pa,
pb);
FILE *pp = popen(cmd, "r");
char *out = NULL;
if (pp) {
size_t cap = 4096, n = 0;
out = malloc(cap);
int ch;
while (out && (ch = fgetc(pp)) != EOF) {
if (n + 1 >= cap) {
cap *= 2;
out = realloc(out, cap);
}
if (out)
out[n++] = (char)ch;
}
int st = pclose(pp);
if (out)
out[n] = 0;
if (st == -1 || (WEXITSTATUS(st) != 0 && WEXITSTATUS(st) != 1)) {
free(out);
out = NULL;
}
}
unlink(pa);
unlink(pb);
return out;
#endif
}
static int have_gnu_diff(void) {
char *o = gnu_diff("x\n", "y\n");
int ok = o && strstr(o, "@@ -1 +1 @@");
free(o);
return ok;
}
static char *mine(const char *a, const char *b) {
return cst_diff_unified("f", a, strlen(a), b, strlen(b));
}
static void same_as_gnu(const char *name, const char *a, const char *b,
int gnu) {
char *m = mine(a, b);
EXPECT(m != NULL, name);
if (gnu && m) {
char *g = gnu_diff(a, b);
if (g && strcmp(g, m) != 0) {
fprintf(stderr, "FAIL %s: differs from diff -u\n--- ours\n%s--- gnu\n%s",
name, m, g);
nfail++;
}
free(g);
}
free(m);
}
/* Apply our diff with our patcher; must reproduce b. */
static int round_trip(const char *a, const char *b) {
char *d = mine(a, b);
if (!d)
return 0;
int ok;
if (!d[0]) {
ok = strcmp(a, b) == 0;
} else {
const char *h = strstr(d, "\n+++ ");
h = h ? strchr(h + 1, '\n') : NULL;
char *out = NULL;
size_t out_len = 0;
int nh = 0;
char err[256];
int rc = h ? cst_patch_apply_buffer(a, strlen(a), h + 1, strlen(h + 1),
&out, &out_len, &nh, err, sizeof(err))
: -1;
ok = rc == 0 && out && out_len == strlen(b) && memcmp(out, b, out_len) == 0;
if (!ok)
fprintf(stderr, "round trip failed (%s)\n--- a\n%s--- b\n%s--- diff\n%s",
rc ? err : "content", a, b, d);
free(out);
}
free(d);
return ok;
}
static char *random_text(unsigned *seed, int lines) {
static const char *const words[] = {"alpha", "beta", "gamma", "delta",
"{", "}", "", "return 0;"};
size_t cap = (size_t)lines * 16 + 2, n = 0;
char *s = malloc(cap);
for (int i = 0; i < lines; i++) {
const char *w = words[test_rand(seed) % 8];
n += (size_t)snprintf(s + n, cap - n, "%s%d\n", w, test_rand(seed) % 3);
}
s[n] = 0;
if (n && test_rand(seed) % 5 == 0)
s[n - 1] = 0; /* no newline at end of file */
return s;
}
static char *mutate(unsigned *seed, const char *a) {
size_t n = strlen(a), cap = n * 2 + 256, o = 0;
char *b = malloc(cap);
const char *p = a;
while (*p) {
const char *e = strchr(p, '\n');
size_t len = e ? (size_t)(e - p) + 1 : strlen(p);
int r = test_rand(seed) % 10;
if (r == 0) {
/* drop the line */
} else if (r == 1) {
o += (size_t)snprintf(b + o, cap - o, "inserted%d\n", test_rand(seed) % 9);
memcpy(b + o, p, len);
o += len;
} else if (r == 2) {
o += (size_t)snprintf(b + o, cap - o, "changed%d\n", test_rand(seed) % 9);
} else {
memcpy(b + o, p, len);
o += len;
}
p += len;
}
b[o] = 0;
return b;
}
int main(void) {
char *m = mine("same\n", "same\n");
EXPECT(m && m[0] == 0, "identical -> empty");
free(m);
int gnu = have_gnu_diff();
if (!gnu)
fprintf(stderr, "note: GNU diff not found; layout checks use round trip "
"only\n");
same_as_gnu("modify", "a\nb\nc\nd\ne\n", "a\nb\nC\nd\ne\n", gnu);
same_as_gnu("insert head", "a\nb\n", "z\na\nb\n", gnu);
same_as_gnu("append", "a\nb\n", "a\nb\nc\n", gnu);
same_as_gnu("delete", "a\nb\nc\n", "a\nc\n", gnu);
same_as_gnu("new file", "", "x\ny\n", gnu);
same_as_gnu("emptied", "x\ny\n", "", gnu);
same_as_gnu("no eol added", "a\nb", "a\nb\n", gnu);
same_as_gnu("no eol kept", "a\nb", "a\nB", gnu);
same_as_gnu("two hunks",
"1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n",
"1\nTWO\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\nFOURTEEN\n15\n",
gnu);
same_as_gnu("merged hunks", "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n",
"1\nX\n3\n4\n5\n6\n7\nY\n9\n10\n", gnu);
m = mine("a\n", "b\n");
EXPECT(m && strncmp(m, "--- a/f\n+++ b/f\n@@ -1 +1 @@\n-a\n+b\n", 34) == 0,
"header layout");
free(m);
unsigned seed = 12345;
int ok = 0, total = 400;
for (int i = 0; i < total; i++) {
char *a = random_text(&seed, 1 + (int)(test_rand(&seed) % 60));
char *b = mutate(&seed, a);
ok += round_trip(a, b);
free(a);
free(b);
}
EXPECT(ok == total, "random round trips");
/* very different large inputs hit the fallback and still round trip */
{
size_t n = 6000;
char *a = malloc(n * 8), *b = malloc(n * 8);
size_t oa = 0, ob = 0;
for (size_t i = 0; i < n; i++) {
oa += (size_t)sprintf(a + oa, "a%zu\n", i);
ob += (size_t)sprintf(b + ob, "b%zu\n", i);
}
EXPECT(round_trip(a, b), "fallback round trip");
free(a);
free(b);
}
if (nfail) {
fprintf(stderr, "test_diff: %d failure(s)\n", nfail);
return 1;
}
printf("test_diff: OK (%d random round trips)\n", total);
return 0;
}