code wiki / _hdl_build / nx_capreg_health_test.nx
nx_capreg_health_test.nx source
↩ module page · 85 lines · 4083 B
1// nx_capreg_health_test.nx -- ENGINEER's gate for the health organ. Reproduces every observed registry
2// disease on scratch files: crash-wiped live, live-only leak (legacy register bypassing dual-write),
3// idx collision, snapshot-bloated journal -- and proves check->heal->recheck lands GREEN each time.
4// The load-bearing KAT: a leaked live-only entry SURVIVES the heal (ingest-before-canonicalize; the
5// naive heal would have dropped it). license_tier: ORIGINAL
6import "nx_capreg_health.nx"
7import "nx_syscalls.nx"
8
9func th_w(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 }
10func th_wn(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m==0{sys_write(1,"0" as *u8,1); return 0} let t: *u8=sys_mmap(28); var k: i64=0; while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(1,bb,k); return 0 }
11func th_find(b: *u8, n: i64, s: *u8) -> i64 {
12 var i: i64 = 0
13 while i < n {
14 var k: i64 = 0
15 var ok: i64 = 1
16 while s[k] != (0 as u8) {
17 if i + k >= n { ok = 0 }
18 if ok == 1 { if b[i+k] != s[k] { ok = 0 } }
19 k = k + 1
20 }
21 if ok == 1 { return 1 }
22 i = i + 1
23 }
24 return 0
25}
26// (re)write a file with the 3 baseline entries
27func th_seed(path: *u8) -> i64 {
28 let fd: i64 = sys_openat_wr(path, 0x1a4); if fd < 0 { return 0 }
29 th_w(fd, "CAPREG idx=10 layer=2 status=2 name=ALPHA\n" as *u8)
30 th_w(fd, "CAPREG idx=11 layer=3 status=2 name=BETA\n" as *u8)
31 th_w(fd, "CAPREG idx=12 layer=5 status=2 name=GAMMA\n" as *u8)
32 sys_close(fd)
33 return 1
34}
35func th_append(path: *u8, line: *u8) -> i64 {
36 let fd: i64 = sys_openat_append(path, 0x1a4); if fd < 0 { return 0 }
37 th_w(fd, line); sys_close(fd)
38 return 1
39}
40
41func main() -> i64 {
42 let lp: *u8 = "/tmp/_ch_live.log" as *u8
43 let jp: *u8 = "/tmp/_ch_journal.log" as *u8
44 let rp: *u8 = "/tmp/_ch_report.log" as *u8
45 let out: *i64 = sys_mmap(32) as *i64
46 var pass: i64 = 0
47 th_seed(lp); th_seed(jp)
48 // S1 clean pair: GREEN with no medicine
49 if ch_check(lp, jp) == 0 { pass = pass + 1 }
50 if ch_run(lp, jp, out) == 1 { if out[1] == 0 { pass = pass + 1 } }
51 // S2 live-only leak (legacy register bypassed dual-write) -> bit 8; entry must SURVIVE the heal
52 th_append(lp, "CAPREG idx=20 layer=2 status=2 name=DELTA20\n" as *u8)
53 if ch_check(lp, jp) == 8 { pass = pass + 1 }
54 if ch_run(lp, jp, out) == 1 { pass = pass + 1 }
55 if out[1] == 1 { pass = pass + 1 }
56 let jl: *i64 = sys_mmap(16) as *i64
57 let jb: *u8 = sys_read_file(jp, jl)
58 if th_find(jb, jl[0], "name=DELTA20" as *u8) == 1 { pass = pass + 1 }
59 // S3 idx collision leaked into live -> bits 2+8; heal renumbers it, content preserved
60 th_append(lp, "CAPREG idx=10 layer=4 status=2 name=OMEGA\n" as *u8)
61 if ch_check(lp, jp) == 10 { pass = pass + 1 }
62 if ch_run(lp, jp, out) == 1 { pass = pass + 1 }
63 let jl2: *i64 = sys_mmap(16) as *i64
64 let jb2: *u8 = sys_read_file(jp, jl2)
65 if th_find(jb2, jl2[0], "name=OMEGA remapped_from=10" as *u8) == 1 { pass = pass + 1 }
66 // S4 the crash: live wiped -> bit 1; heal restores it from the journal
67 let wf: i64 = sys_openat_wr(lp, 0x1a4); sys_close(wf)
68 if ch_check(lp, jp) == 1 { pass = pass + 1 }
69 if ch_run(lp, jp, out) == 1 { pass = pass + 1 }
70 if ch_count(lp) == ch_count(jp) { if ch_count(lp) == 5 { pass = pass + 1 } }
71 // S5 snapshot bloat: exact-dup line appended to the journal -> bit 4; heal dedups
72 th_append(jp, "CAPREG idx=10 layer=2 status=2 name=ALPHA\n" as *u8)
73 if ch_check(lp, jp) == 4 { pass = pass + 1 }
74 if ch_run(lp, jp, out) == 1 { pass = pass + 1 }
75 // S6 the durable verdict line
76 ch_report(rp, out, 1)
77 let rl: *i64 = sys_mmap(16) as *i64
78 let rb: *u8 = sys_read_file(rp, rl)
79 if th_find(rb, rl[0], "verdict=GREEN" as *u8) == 1 { pass = pass + 1 }
80
81 th_w(1, "HEALTH KATs passed=" as *u8); th_wn(pass); th_w(1, "/15\n" as *u8)
82 if pass == 15 { sys_exit(0) }
83 sys_exit(1)
84 return 1
85}