code wiki / _hdl_build / nx_reader_zoomserve_gate.nx
nx_reader_zoomserve_gate.nx source
↩ module page · 167 lines · 9958 B
1import "nx_gate_grow.nx"
2import "nx_gate_gn.nx"
3import "nx_gate_read.nx"
4// nx_reader_zoomserve_gate.nx -- SOVEREIGN in-process referee for the /api/zoomprep endpoint (zs_handle_zoomprep).
5// No socket/curl/shell: chdir /tmp, build the REAL fixture world there (reader/<slug>/page000.jpg = a synthesized
6// baseline JPEG comic page; ./_offc/nx_reader_page_prep.elf = a byte-copy of the freshly built prep elf), then
7// feed crafted request-path BYTES straight into the handler with a FILE fd as cfd and assert BOTH the HTTP
8// response bytes AND the on-disk pyramid. Mirrors the nx_mgmt_upload_gate lib+gate idiom.
9// T1 prep happy path -> 200 {"ok":1,"cached":0} + dz.json + panels.json + top tile is a REAL JPEG
10// T2 re-prep -> 200 {"ok":1,"cached":1} (idempotent, O(1) second call)
11// T3 NEG traversal slug (..zgate) -> 403 + ok:0 (fail-closed)
12// T4 NEG missing page file -> 404
13// T5 NEG traversal file (../../etc) -> 403
14// T6 NEG prep elf ABSENT -> 500 rc:127 + NO dz.json fabricated (success is decided by dz.json on disk)
15// GREEN iff T1..T6. license_tier: ORIGINAL expect_exit: 0
16import "nx_syscalls.nx"
17import "nx_reader_zoomserve.nx" // zs_handle_zoomprep + zs_slen (the lib under test)
18import "nx_jpeg_color_write.nx"
19import "nx_quant_table.nx"
20import "nx_jpeg_huff_enc.nx"
21import "nx_zigzag.nx"
22import "nx_dct8.nx"
23
24func gw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
25
26func g_has(hay: *u8, n: i64, needle: *u8) -> i64 {
27 var nn: i64 = 0; while needle[nn] != (0 as u8) { nn = nn + 1 }
28 if nn == 0 { return 0 }
29 var i: i64 = 0
30 while i + nn <= n { var m: i64 = 1; var j: i64 = 0; while j < nn { if (hay[i + j] as i64) != (needle[j] as i64) { m = 0; j = nn } else { j = j + 1 } } if m == 1 { return 1 } i = i + 1 }
31 return 0
32}
33func g_exists(path: *u8) -> i64 { let fd: i64 = sys_openat_rd(path); if fd < 0 { return 0 } sys_close(fd); return 1 }
34func g_mkdir(path: *u8) -> i64 { __syscall(258, 0 - 100, path as i64, 0x1ed, 0, 0, 0); return 0 }
35
36// call the handler with a crafted request path; response captured via a plain FILE fd as cfd. Returns resp len.
37func g_call(reqpath: *u8, out: *u8, cap: i64) -> i64 {
38 let rp: *u8 = "zs_rsp.tmp" as *u8
39 __syscall(263, 0 - 100, rp, 0, 0, 0, 0) // unlink old (let-bound ptr: inline literal casts miscompile in syscall args)
40 let fd: i64 = sys_openat_wr("zs_rsp.tmp" as *u8, 0x1a4)
41 if fd < 0 { return 0 - 1 }
42 zs_handle_zoomprep(fd, reqpath, zs_slen(reqpath)) // handler writes + closes fd
43 return g_read("zs_rsp.tmp" as *u8, out, cap)
44}
45
46func main() -> i64 {
47 gw("reader-zoomserve SOVEREIGN in-process gate (fixture world in /tmp -> zs_handle_zoomprep bytes-in + on-disk asserts)\n" as *u8)
48 if sys_chdir("/tmp" as *u8) != 0 { gw("CHDIR /tmp FAIL\n" as *u8); sys_exit(1) }
49
50 // ---- fixture world ----
51 g_mkdir("knowledge" as *u8)
52 g_mkdir("knowledge/staging" as *u8)
53 g_mkdir("knowledge/staging/media" as *u8)
54 g_mkdir("knowledge/staging/media/reader" as *u8)
55 g_mkdir("knowledge/staging/media/reader/zgate" as *u8)
56 g_mkdir("_offc" as *u8)
57
58 // copy the freshly built prep elf into the daemon's fork path ./_offc/nx_reader_page_prep.elf
59 let esz: *i64 = sys_mmap(16) as *i64
60 let eb: *u8 = sys_read_file("/tmp/nx_reader_page_prep.sov.elf" as *u8, esz)
61 var elfok: i64 = 0
62 if (eb as i64) != 0 { if esz[0] > 0 {
63 let efd: i64 = sys_openat_wr("_offc/nx_reader_page_prep.elf" as *u8, 0x1ed)
64 if efd >= 0 { sys_write(efd, eb, esz[0]); sys_close(efd)
65 __syscall(268, 0 - 100, "_offc/nx_reader_page_prep.elf" as *u8 as i64, 0x1ed, 0, 0, 0) // fchmodat 0755
66 elfok = 1
67 }
68 } }
69 if elfok == 0 { gw("FIXTURE-FAIL: /tmp/nx_reader_page_prep.sov.elf missing (build it --build-only first)\n" as *u8); sys_exit(1) }
70
71 // synthesize the 2x2-panel comic page (white paper, 4 dark panels w/ real gutters) -> a REAL baseline JPEG
72 let W: i64 = 800; let H: i64 = 592
73 let rgb: *u8 = sys_mmap(W * H * 3 + 16)
74 var pi: i64 = 0; let npx: i64 = W * H * 3
75 while pi < npx { rgb[pi] = 255 as u8; pi = pi + 1 }
76 var yy: i64 = 0
77 while yy < H { var xx: i64 = 0
78 while xx < W {
79 var inp: i64 = 0
80 if yy >= 40 { if yy < 272 { if xx >= 40 { if xx < 360 { inp = 1 } } if xx >= 440 { if xx < 760 { inp = 1 } } } }
81 if yy >= 320 { if yy < 552 { if xx >= 40 { if xx < 360 { inp = 1 } } if xx >= 440 { if xx < 760 { inp = 1 } } } }
82 if inp == 1 { let o2: i64 = (yy * W + xx) * 3; rgb[o2] = 60 as u8; rgb[o2 + 1] = 60 as u8; rgb[o2 + 2] = 60 as u8 }
83 xx = xx + 1
84 }
85 yy = yy + 1
86 }
87 let dcB: *i64 = sys_mmap(20*8) as *i64; let dcV: *i64 = sys_mmap(20*8) as *i64
88 let acB: *i64 = sys_mmap(20*8) as *i64; let acV: *i64 = sys_mmap(200*8) as *i64
89 let ndc: i64 = jhe_dc_bits(dcB); jhe_dc_val(dcV); let nac: i64 = jhe_ac_bits(acB); jhe_ac_val(acV)
90 let dcCO: *i64 = sys_mmap(256*8) as *i64; let dcSI: *i64 = sys_mmap(256*8) as *i64
91 let acCO: *i64 = sys_mmap(256*8) as *i64; let acSI: *i64 = sys_mmap(256*8) as *i64
92 jhe_gen(dcB, dcV, ndc, dcCO, dcSI); jhe_gen(acB, acV, nac, acCO, acSI)
93 let qtL: *i64 = sys_mmap(64*8) as *i64; nx_qt_luma(qtL, 72)
94 let qtC: *i64 = sys_mmap(64*8) as *i64; nx_qt_chroma(qtC, 72)
95 let to_zz: *i64 = sys_mmap(64*8) as *i64; let from_zz: *i64 = sys_mmap(64*8) as *i64; nx_zigzag_init(to_zz, from_zz)
96 let DM: *i64 = sys_mmap(64*8) as *i64; nx_dct8_init(DM)
97 let scratch: *i64 = sys_mmap(64*8) as *i64; let tti: *i64 = sys_mmap(8*8) as *i64; let tto: *i64 = sys_mmap(8*8) as *i64
98 let jout: *u8 = sys_mmap(W * H * 3 + 8192)
99 let jlen: i64 = jcw_write_color(jout, W, H, rgb, qtL, qtC, dcB, dcV, acB, acV, dcCO, dcSI, acCO, acSI, to_zz, from_zz, DM, scratch, tti, tto)
100 if jlen <= 0 { gw("FIXTURE-FAIL: jpeg encode\n" as *u8); sys_exit(1) }
101 let p0: i64 = sys_openat_wr("knowledge/staging/media/reader/zgate/page000.jpg" as *u8, 0x1a4)
102 if p0 >= 0 { sys_write(p0, jout, jlen); sys_close(p0) }
103 let p1: i64 = sys_openat_wr("knowledge/staging/media/reader/zgate/page001.jpg" as *u8, 0x1a4)
104 if p1 >= 0 { sys_write(p1, jout, jlen); sys_close(p1) }
105
106 // clean any prior run's pyramid so T1 exercises the COLD path
107 let dz0: *u8 = "knowledge/staging/media/reader/zgate/zoom/page000/dz.json" as *u8
108 __syscall(263, 0 - 100, dz0, 0, 0, 0, 0)
109 let dz1: *u8 = "knowledge/staging/media/reader/zgate/zoom/page001/dz.json" as *u8
110 __syscall(263, 0 - 100, dz1, 0, 0, 0, 0)
111
112 let rsp: *u8 = sys_mmap(65536)
113 var pass: i64 = 0
114
115 // ---- T1 happy path (cold) ----
116 var rn: i64 = g_call("/api/zoomprep?slug=zgate&file=page000.jpg&rtl=1" as *u8, rsp, 65536)
117 var t1: i64 = 0
118 if rn > 0 { if g_has(rsp, rn, "HTTP/1.1 200" as *u8) == 1 { if g_has(rsp, rn, "\"ok\":1" as *u8) == 1 { if g_has(rsp, rn, "\"cached\":0" as *u8) == 1 {
119 if g_exists("knowledge/staging/media/reader/zgate/zoom/page000/dz.json" as *u8) == 1 {
120 if g_exists("knowledge/staging/media/reader/zgate/zoom/page000/panels.json" as *u8) == 1 {
121 let tb: *u8 = sys_mmap(1 << 20)
122 let tn: i64 = g_read("knowledge/staging/media/reader/zgate/zoom/page000/10/0_0.jpg" as *u8, tb, 1 << 20)
123 if tn > 3 { if tb[0] == (255 as u8) { if tb[1] == (216 as u8) { t1 = 1 } } }
124 }
125 }
126 } } } }
127 pass = pass + grow("T1 cold prep -> 200 ok cached:0 + dz.json + panels.json + real JPEG top tile\x00" as *u8, t1)
128
129 // ---- T2 idempotent (warm) ----
130 rn = g_call("/api/zoomprep?slug=zgate&file=page000.jpg&rtl=1" as *u8, rsp, 65536)
131 var t2: i64 = 0
132 if rn > 0 { if g_has(rsp, rn, "HTTP/1.1 200" as *u8) == 1 { if g_has(rsp, rn, "\"cached\":1" as *u8) == 1 { t2 = 1 } } }
133 pass = pass + grow("T2 warm re-prep -> 200 cached:1 (idempotent O(1))\x00" as *u8, t2)
134
135 // ---- T3 NEG traversal slug ----
136 rn = g_call("/api/zoomprep?slug=..zgate&file=page000.jpg&rtl=1" as *u8, rsp, 65536)
137 var t3: i64 = 0
138 if rn > 0 { if g_has(rsp, rn, "HTTP/1.1 403" as *u8) == 1 { if g_has(rsp, rn, "\"ok\":0" as *u8) == 1 { t3 = 1 } } }
139 pass = pass + grow("T3 NEG traversal slug -> 403 fail-closed\x00" as *u8, t3)
140
141 // ---- T4 NEG missing page ----
142 rn = g_call("/api/zoomprep?slug=zgate&file=missing.jpg&rtl=1" as *u8, rsp, 65536)
143 var t4: i64 = 0
144 if rn > 0 { if g_has(rsp, rn, "HTTP/1.1 404" as *u8) == 1 { t4 = 1 } }
145 pass = pass + grow("T4 NEG missing page file -> 404\x00" as *u8, t4)
146
147 // ---- T5 NEG traversal file ----
148 rn = g_call("/api/zoomprep?slug=zgate&file=../../etc&rtl=1" as *u8, rsp, 65536)
149 var t5: i64 = 0
150 if rn > 0 { if g_has(rsp, rn, "HTTP/1.1 403" as *u8) == 1 { t5 = 1 } }
151 pass = pass + grow("T5 NEG traversal file -> 403 fail-closed\x00" as *u8, t5)
152
153 // ---- T6 NEG prep elf absent -> 500 + NO fabricated dz.json ----
154 let ep: *u8 = "_offc/nx_reader_page_prep.elf" as *u8
155 __syscall(263, 0 - 100, ep, 0, 0, 0, 0) // remove the elf
156 if g_exists("_offc/nx_reader_page_prep.elf" as *u8) == 1 { gw(" (T6 fixture: unlink FAILED, elf still present)\n" as *u8) }
157 rn = g_call("/api/zoomprep?slug=zgate&file=page001.jpg&rtl=1" as *u8, rsp, 65536)
158 var t6: i64 = 0
159 if rn > 0 { if g_has(rsp, rn, "HTTP/1.1 500" as *u8) == 1 { if g_has(rsp, rn, "\"rc\":127" as *u8) == 1 {
160 if g_exists("knowledge/staging/media/reader/zgate/zoom/page001/dz.json" as *u8) == 0 { t6 = 1 }
161 } } }
162 pass = pass + grow("T6 NEG prep elf absent -> 500 rc:127 + no dz.json fabricated (no fake green)\x00" as *u8, t6)
163
164 gw("pass=" as *u8); gn(pass); gw("/6\n" as *u8)
165 if pass == 6 { gw("verdict=GREEN (zoomprep endpoint: lazy cache-first prep + traversal fail-closed + on-disk truth, tiles ride the existing /api/book allowlist)\n" as *u8); sys_exit(0); return 0 }
166 gw("verdict=RED\n" as *u8); sys_exit(1); return 1
167}