code wiki / (root) / nx_bspfacts_gate.nx

nx_bspfacts_gate.nx source

↩ module page · 274 lines · 13395 B

1// nx_bspfacts_gate.nx -- BITE-PROOF for the BodySlide preset dialect, both directions. 2// 3// IT DRIVES THE SHIPPING CODE: every tooth calls nx_bspfacts_lib directly -- the same bsp_probe, 4// bsp_decode and bsp_val_permil the CLI calls. A tooth that tests a re-implementation proves 5// nothing about what ships. 6// 7// FIXTURES ARE ASSEMBLED AT RUNTIME under /tmp/<gate>/ -- a detector that scans source will find 8// its own test fixture, and prose is source bytes too, so no fixture literal (and no quote 9// character) exists in this file. Every path is unlinked before it is written (idempotence). 10// 11// FIXTURE HONESTY, DECLARED: these fixtures are SYNTHETIC, derived from the mirrored GPL-3.0 12// reference parser (SliderPresets.cpp: value is FloatAttribute/100 on load, int percent on save). 13// The real-community-preset fixture is a named watch contract on /compare/koikatsu 14// (bsp_fixture_real) -- the operator's adult-gated candidate needs a logged-in bank first. 15// 16// license_tier: ORIGINAL expect_exit: 0 17import "nx_syscalls.nx" 18import "nx_gate_verdict.nx" 19import "nx_bspfacts_lib.nx" 20 21const BG_DIR: *u8 = "/tmp/nx_bspfacts_gate" as *u8 22const BG_F_GOOD: *u8 = "/tmp/nx_bspfacts_gate/good.xml" as *u8 23const BG_F_BADVAL: *u8 = "/tmp/nx_bspfacts_gate/badval.xml" as *u8 24const BG_F_PLAIN: *u8 = "/tmp/nx_bspfacts_gate/plain.xml" as *u8 25const BG_F_JUNK: *u8 = "/tmp/nx_bspfacts_gate/junk.bin" as *u8 26const BG_F_EMPTY: *u8 = "/tmp/nx_bspfacts_gate/empty.xml" as *u8 27const BG_BUFCAP: i64 = 65536 28const BG_NL: i64 = 10 29const BG_TRUNC_HEAD: i64 = 64 // enough head to find the root + first Preset, less than the file 30const BG_MAXVALS: i64 = 64 31const BG_V_BREASTS: i64 = 570 // value 57 percent 32const BG_V_WAIST: i64 = 575 // value 57.5 percent -- tenth kept 33const BG_V_THIGH: i64 = 0 - 250 // value -25 percent -- sign carried 34const BG_V_SMALL: i64 = 400 // value 40 percent on the SMALL axis of the SAME slider name 35const BG_V_SECOND: i64 = 990 // value 99 percent, SECOND preset -- must never leak into the first window 36 37func bg_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 38func bg_cat(dst: *u8, at: i64, s: *u8) -> i64 { 39 var i: i64 = 0 40 var o: i64 = at 41 while s[i] != (0 as u8) { dst[o] = s[i]; o = o + 1; i = i + 1 } 42 return o 43} 44func bg_q(dst: *u8, at: i64) -> i64 { dst[at] = BSP_QUOTE as u8; return at + 1 } 45func bg_nl(dst: *u8, at: i64) -> i64 { dst[at] = BG_NL as u8; return at + 1 } 46func bg_qv(dst: *u8, at: i64, v: *u8) -> i64 { 47 var o: i64 = bg_q(dst, at) 48 o = bg_cat(dst, o, v) 49 return bg_q(dst, o) 50} 51// one SetSlider row: <SetSlider name=Qn.Q size=Qs.Q value=Qv.Q/> 52func bg_slider(dst: *u8, at: i64, name: *u8, size: *u8, value: *u8) -> i64 { 53 var o: i64 = bg_cat(dst, at, "<SetSlider name=" as *u8) 54 o = bg_qv(dst, o, name) 55 o = bg_cat(dst, o, " size=" as *u8) 56 o = bg_qv(dst, o, size) 57 o = bg_cat(dst, o, " value=" as *u8) 58 o = bg_qv(dst, o, value) 59 o = bg_cat(dst, o, "/>" as *u8) 60 return bg_nl(dst, o) 61} 62func bg_preset_open(dst: *u8, at: i64, name: *u8, set: *u8) -> i64 { 63 var o: i64 = bg_cat(dst, at, "<Preset name=" as *u8) 64 o = bg_qv(dst, o, name) 65 o = bg_cat(dst, o, " set=" as *u8) 66 o = bg_qv(dst, o, set) 67 o = bg_cat(dst, o, ">" as *u8) 68 return bg_nl(dst, o) 69} 70func bg_write(path: *u8, body: *u8, n: i64) -> i64 { 71 sys_unlinkat(path) 72 let fd: i64 = sys_openat_wr(path, MODE_0644) 73 if fd < 0 { return 0 - 1 } 74 if n > 0 { sys_write(fd, body, n) } 75 sys_close(fd) 76 return n 77} 78// span equality against a nul-str -- for asserting decoded NAME spans 79func bg_speq(buf: *u8, off: i64, len: i64, want: *u8) -> i64 { 80 if off < 0 { return 0 } 81 if len != bg_slen(want) { return 0 } 82 var i: i64 = 0 83 while i < len { if buf[off + i] != want[i] { return 0 } i = i + 1 } 84 return 1 85} 86 87// assemble the GOOD fixture; returns byte length 88func bg_build_good(dst: *u8) -> i64 { 89 var o: i64 = bg_cat(dst, 0, "<SliderPresets>" as *u8) 90 o = bg_nl(dst, o) 91 o = bg_preset_open(dst, o, "GateFixture" as *u8, "CBBE Body" as *u8) 92 var g: i64 = bg_cat(dst, o, "<Group name=" as *u8) 93 g = bg_qv(dst, g, "CBBE" as *u8) 94 g = bg_cat(dst, g, "/>" as *u8) 95 o = bg_nl(dst, g) 96 o = bg_slider(dst, o, "Breasts" as *u8, "big" as *u8, "57" as *u8) 97 o = bg_slider(dst, o, "Waist" as *u8, "big" as *u8, "57.5" as *u8) 98 o = bg_slider(dst, o, "ThighOuter" as *u8, "big" as *u8, "-25" as *u8) 99 o = bg_slider(dst, o, "Breasts" as *u8, "small" as *u8, "40" as *u8) 100 o = bg_cat(dst, o, "</Preset>" as *u8) 101 o = bg_nl(dst, o) 102 o = bg_preset_open(dst, o, "Second" as *u8, "CBBE Body" as *u8) 103 o = bg_slider(dst, o, "Other" as *u8, "big" as *u8, "99" as *u8) 104 o = bg_cat(dst, o, "</Preset>" as *u8) 105 o = bg_nl(dst, o) 106 o = bg_cat(dst, o, "</SliderPresets>" as *u8) 107 return bg_nl(dst, o) 108} 109// same head, but the second big value is not a number -- the honest-partial fixture 110func bg_build_badval(dst: *u8) -> i64 { 111 var o: i64 = bg_cat(dst, 0, "<SliderPresets>" as *u8) 112 o = bg_nl(dst, o) 113 o = bg_preset_open(dst, o, "Bad" as *u8, "CBBE Body" as *u8) 114 o = bg_slider(dst, o, "Breasts" as *u8, "big" as *u8, "57" as *u8) 115 o = bg_slider(dst, o, "Waist" as *u8, "big" as *u8, "abc" as *u8) 116 o = bg_slider(dst, o, "ThighOuter" as *u8, "big" as *u8, "10" as *u8) 117 o = bg_cat(dst, o, "</Preset>" as *u8) 118 o = bg_nl(dst, o) 119 o = bg_cat(dst, o, "</SliderPresets>" as *u8) 120 return bg_nl(dst, o) 121} 122// a real XML with a Preset element but NO SliderPresets root -- both signals present at once 123func bg_build_plain(dst: *u8) -> i64 { 124 var o: i64 = bg_cat(dst, 0, "<Other>" as *u8) 125 o = bg_nl(dst, o) 126 o = bg_preset_open(dst, o, "x" as *u8, "y" as *u8) 127 o = bg_cat(dst, o, "</Preset>" as *u8) 128 o = bg_nl(dst, o) 129 o = bg_cat(dst, o, "</Other>" as *u8) 130 return bg_nl(dst, o) 131} 132 133func main(argc: i64, argv: *i64) -> i64 { 134 let ctr: *i64 = gv_ctr() 135 gv_head("=== NX-BSPFACTS GATE -- the Bethesda preset dialect must decode, and must refuse everything else ===" as *u8) 136 137 sys_mkdir(BG_DIR, MODE_0755) 138 let asmbuf: *u8 = sys_mmap(BG_BUFCAP) 139 let sc: *i64 = sys_mmap(64) as *i64 140 141 // ---- SETUP: assemble at runtime, unlink before write, write, and CAPTURE the planted sizes. 142 let n_good: i64 = bg_build_good(asmbuf) 143 let w_good: i64 = bg_write(BG_F_GOOD, asmbuf, n_good) 144 let n_bad: i64 = bg_build_badval(asmbuf) 145 let w_bad: i64 = bg_write(BG_F_BADVAL, asmbuf, n_bad) 146 let n_plain: i64 = bg_build_plain(asmbuf) 147 let w_plain: i64 = bg_write(BG_F_PLAIN, asmbuf, n_plain) 148 let n_junk: i64 = bg_cat(asmbuf, 0, "these bytes are no kind of xml and never will be" as *u8) 149 let w_junk: i64 = bg_write(BG_F_JUNK, asmbuf, n_junk) 150 let w_empty: i64 = bg_write(BG_F_EMPTY, asmbuf, 0) 151 152 gv_puts(" planted: good=" as *u8); gv_num(w_good) 153 gv_puts(" badval=" as *u8); gv_num(w_bad) 154 gv_puts(" plain=" as *u8); gv_num(w_plain) 155 gv_puts(" junk=" as *u8); gv_num(w_junk) 156 gv_puts(" empty=" as *u8); gv_num(w_empty); gv_puts("\n" as *u8) 157 158 var built: i64 = 0 159 if w_good == n_good { if w_good > 0 { if w_bad == n_bad { if w_plain == n_plain { 160 if w_junk == n_junk { if w_empty == 0 { built = 1 } } } } } } 161 gv_need("every fixture reached its planted condition (written bytes == assembled bytes, good nonempty, empty empty)" as *u8, built, ctr) 162 163 // ---- read them back the way a consumer would 164 let good: *u8 = sys_read_file(BG_F_GOOD, sc) 165 let good_n: i64 = sc[0] 166 let facts: *i64 = sys_mmap(BSP_N_SLOTS * 8) as *i64 167 168 // ---- T1 THE BITE: fires on a preset, does not fire on junk 169 bsp_probe(good, good_n, good_n, facts) 170 var fired_good: i64 = 0 171 if facts[0] == 1 { fired_good = 1 } 172 let isp_good: i64 = facts[0] 173 let c_presets: i64 = facts[1] 174 let c_sliders: i64 = facts[2] 175 let c_groups: i64 = facts[3] 176 let c_big: i64 = facts[4] 177 let c_small: i64 = facts[5] 178 let id_noff: i64 = facts[6] 179 let id_nlen: i64 = facts[7] 180 let id_soff: i64 = facts[8] 181 let id_slen: i64 = facts[9] 182 let junk: *u8 = sys_read_file(BG_F_JUNK, sc) 183 let junk_n: i64 = sc[0] 184 bsp_probe(junk, junk_n, junk_n, facts) 185 var fired_junk: i64 = 0 186 if facts[0] == 1 { fired_junk = 1 } 187 gv_bite("T1 bsp_probe fires on a real preset and does NOT fire on non-xml bytes" as *u8, fired_good, fired_junk, ctr) 188 189 // ---- T1b BOTH SIGNALS AT ONCE: a Preset element without the SliderPresets root must not pass 190 let plain: *u8 = sys_read_file(BG_F_PLAIN, sc) 191 let plain_n: i64 = sc[0] 192 bsp_probe(plain, plain_n, plain_n, facts) 193 var t1b: i64 = 0 194 if facts[0] == 0 { t1b = 1 } 195 gv_check("T1b neg-control-a-Preset-element-without-the-SliderPresets-root-is-refused (discrimination, not keyword spotting)" as *u8, t1b, ctr) 196 197 // ---- T2 the census is EXACT -- a trivial always-yes implementation cannot produce these five numbers 198 gv_puts(" census: presets=" as *u8); gv_num(c_presets) 199 gv_puts(" setsliders=" as *u8); gv_num(c_sliders) 200 gv_puts(" groups=" as *u8); gv_num(c_groups) 201 gv_puts(" big=" as *u8); gv_num(c_big) 202 gv_puts(" small=" as *u8); gv_num(c_small); gv_puts("\n" as *u8) 203 var t2: i64 = 0 204 if c_presets == 2 { if c_sliders == 5 { if c_groups == 1 { if c_big == 4 { if c_small == 1 { t2 = 1 } } } } } 205 gv_check("T2 census exact: 2 presets, 5 setsliders, 1 group, 4 big, 1 small" as *u8, t2, ctr) 206 207 // ---- T3 first-preset identity spans 208 var t3: i64 = 0 209 if bg_speq(good, id_noff, id_nlen, "GateFixture" as *u8) == 1 { 210 if bg_speq(good, id_soff, id_slen, "CBBE Body" as *u8) == 1 { t3 = 1 } } 211 gv_check("T3 first preset identity: name and set attribute spans read back exactly" as *u8, t3, ctr) 212 213 // ---- T4 decode the BIG axis: negative sign and tenth-truncation in the same tooth 214 let noffs: *i64 = sys_mmap(BG_MAXVALS * 8) as *i64 215 let nlens: *i64 = sys_mmap(BG_MAXVALS * 8) as *i64 216 let vals: *i64 = sys_mmap(BG_MAXVALS * 8) as *i64 217 let meta: *i64 = sys_mmap(16) as *i64 218 let p_big: i64 = bsp_decode(good, good_n, 0, noffs, nlens, vals, BG_MAXVALS, meta) 219 gv_puts(" big: declared=" as *u8); gv_num(meta[0]) 220 gv_puts(" parsed=" as *u8); gv_num(p_big) 221 gv_puts(" v0=" as *u8); gv_num(vals[0]) 222 gv_puts(" v1=" as *u8); gv_num(vals[1]) 223 gv_puts(" v2=" as *u8); gv_num(vals[2]); gv_puts("\n" as *u8) 224 var t4: i64 = 0 225 if meta[0] == 3 { if p_big == 3 { if vals[0] == BG_V_BREASTS { if vals[1] == BG_V_WAIST { if vals[2] == BG_V_THIGH { t4 = 1 } } } } } 226 gv_check("T4 big decode exact: 57 -> 570, 57.5 -> 575 (tenth kept), -25 -> -250 (sign carried)" as *u8, t4, ctr) 227 228 // ---- T5 the SIZE axis discriminates: the SAME slider name carries a different small value 229 let p_small: i64 = bsp_decode(good, good_n, 1, noffs, nlens, vals, BG_MAXVALS, meta) 230 var t5: i64 = 0 231 if meta[0] == 1 { if p_small == 1 { if vals[0] == BG_V_SMALL { t5 = 1 } } } 232 gv_check("T5 small decode selects the small axis of the same slider name: 40 -> 400, exactly one row" as *u8, t5, ctr) 233 234 // ---- T6 the decoded pair carries its NAME span 235 let p_big2: i64 = bsp_decode(good, good_n, 0, noffs, nlens, vals, BG_MAXVALS, meta) 236 var t6: i64 = 0 237 if p_big2 == 3 { if bg_speq(good, noffs[0], nlens[0], "Breasts" as *u8) == 1 { t6 = 1 } } 238 gv_check("T6 pair zero names its slider: the span reads Breasts" as *u8, t6, ctr) 239 240 // ---- T7 HONEST-PARTIAL: a malformed value STOPS the parse and parsed < declared is visible 241 let bad: *u8 = sys_read_file(BG_F_BADVAL, sc) 242 let bad_n: i64 = sc[0] 243 let p_bad: i64 = bsp_decode(bad, bad_n, 0, noffs, nlens, vals, BG_MAXVALS, meta) 244 gv_puts(" badval: declared=" as *u8); gv_num(meta[0]) 245 gv_puts(" parsed=" as *u8); gv_num(p_bad); gv_puts("\n" as *u8) 246 var t7: i64 = 0 247 if meta[0] == 2 { if p_bad == 1 { t7 = 1 } } 248 gv_check("T7 a non-numeric value stops the parse: parsed=1 under declared=2, never papered over" as *u8, t7, ctr) 249 250 // ---- T8 TRUNCATION HONESTY: a partial read abstains from the census instead of undercounting 251 bsp_probe(good, BG_TRUNC_HEAD, good_n, facts) 252 var t8: i64 = 0 253 if facts[0] == 1 { if facts[1] == (0 - 1) { if facts[2] == (0 - 1) { t8 = 1 } } } 254 gv_check("T8 a truncated read still recognizes the dialect but ABSTAINS from every count (-1, not a small number)" as *u8, t8, ctr) 255 256 // ---- T9 the empty file is refused 257 let emp: *u8 = sys_read_file(BG_F_EMPTY, sc) 258 let emp_n: i64 = sc[0] 259 bsp_probe(emp, emp_n, emp_n, facts) 260 var t9: i64 = 0 261 if facts[0] == 0 { t9 = 1 } 262 gv_check("T9 neg-control-a-zero-byte-file-is-not-a-preset" as *u8, t9, ctr) 263 264 // ---- T10 FIRST-PRESET WINDOWING: the second preset's value never leaks into the first window 265 var leak: i64 = 0 266 var vi: i64 = 0 267 while vi < p_big2 { if vals[vi] == BG_V_SECOND { leak = 1 } vi = vi + 1 } 268 var t10: i64 = 0 269 if p_big2 == 3 { if leak == 0 { t10 = 1 } } 270 gv_check("T10 decode windows to the FIRST preset: the second preset's 990 never appears among its pairs" as *u8, t10, ctr) 271 272 return gv_verdict("BSPFACTS" as *u8, ctr, 273 "the Bethesda body-preset dialect decodes to named permil pairs with fail-closed honesty; fixtures are runtime-assembled and the real-community fixture is a declared watch contract, never a silent gap" as *u8) 274}