nx_uxf_decode_test.nx source
↩ module page · 90 lines · 4334 B
1// nx_uxf_decode_test.nx -- the TOLERANT-READER (house-of-cards) KAT.
2// Proves: (T1) decode reads ALL fields incl one the reader doesn't "know"; (T2) round-trip
3// encode->decode->re-encode reproduces the IDENTICAL CID (unknown field preserved); (T3) the
4// profiled multicodec CID round-trips too; (T4 neg-control) DROPPING a field changes the CID
5// (so preservation is load-bearing -- new data WOULD break an intolerant reader).
6// expect_exit: 0 license_tier: ORIGINAL
7import "nx_syscalls.nx"
8import "nx_canon_cid.nx"
9import "nx_uxf_cid.nx"
10import "nx_uxf_decode.nx"
11
12func t_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
13func t_putn(v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) } let t: *u8 = sys_mmap(28); var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 } 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 }
14
15func t_streq(a: *u8, b: *u8) -> i64 {
16 var i: i64 = 0
17 while 1 == 1 {
18 if a[i] != b[i] { return 0 }
19 if a[i] == (0 as u8) { return 1 }
20 i = i + 1
21 }
22 return 1
23}
24
25func main() -> i64 {
26 // original record {a:1, b:2, c:3} -- 'c' is the field a future/older reader may not know
27 let keys: *i64 = sys_mmap(64) as *i64
28 let vals: *i64 = sys_mmap(64) as *i64
29 keys[0] = ("a\x00") as i64; vals[0] = ("1\x00") as i64
30 keys[1] = ("b\x00") as i64; vals[1] = ("2\x00") as i64
31 keys[2] = ("c\x00") as i64; vals[2] = ("3\x00") as i64
32 let orig: *u8 = sys_mmap(4096)
33 let olen: i64 = canon_encode(keys, vals, 3, orig)
34 let cid_orig: *u8 = sys_mmap(128)
35 cid_of(orig, olen, cid_orig)
36
37 // TOLERANT decode: read EVERY field back (no known-key filter)
38 let dkeys: *i64 = sys_mmap(128) as *i64
39 let dvals: *i64 = sys_mmap(128) as *i64
40 let nf: i64 = canon_decode(orig, olen, dkeys, dvals, 16)
41
42 // re-encode the decoded fields -> should reproduce identical canonical bytes / CID
43 let round: *u8 = sys_mmap(4096)
44 let rlen: i64 = canon_encode(dkeys, dvals, nf, round)
45 let cid_round: *u8 = sys_mmap(128)
46 cid_of(round, rlen, cid_round)
47
48 // profiled round-trip
49 let pcid_orig: *u8 = sys_mmap(128)
50 let pcid_round: *u8 = sys_mmap(128)
51 uxf_cid_profiled(UXF_DATA, orig, olen, pcid_orig)
52 uxf_cid_profiled(UXF_DATA, round, rlen, pcid_round)
53
54 // NEG: an INTOLERANT reader that drops unknown 'c' (re-encode only first 2 decoded fields)
55 let dropc: *u8 = sys_mmap(4096)
56 let dlen: i64 = canon_encode(dkeys, dvals, 2, dropc)
57 let cid_drop: *u8 = sys_mmap(128)
58 cid_of(dropc, dlen, cid_drop)
59
60 var pass: i64 = 0
61 var total: i64 = 0
62
63 t_puts("cid_orig = " as *u8); t_puts(cid_orig); t_puts("\n" as *u8)
64 t_puts("cid_round = " as *u8); t_puts(cid_round); t_puts("\n" as *u8)
65 t_puts("cid_drop = " as *u8); t_puts(cid_drop); t_puts("\n" as *u8)
66
67 // T1: tolerant decode read ALL 3 fields (incl 'c')
68 total = total + 1
69 t_puts("T1 decode all fields (nf=" as *u8); t_putn(nf); t_puts(" want 3): " as *u8)
70 if nf == 3 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
71
72 // T2: round-trip preserves -> identical CID
73 total = total + 1
74 t_puts("T2 round-trip CID stable (preserves unknown 'c'): " as *u8)
75 if t_streq(cid_orig, cid_round) == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
76
77 // T3: profiled CID round-trips too
78 total = total + 1
79 t_puts("T3 profiled CID round-trip stable: " as *u8)
80 if t_streq(pcid_orig, pcid_round) == 1 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
81
82 // T4 neg-control: dropping 'c' MUST change the CID (preservation is load-bearing)
83 total = total + 1
84 t_puts("T4 neg: drop-'c' changes CID (intolerant reader would break): " as *u8)
85 if t_streq(cid_orig, cid_drop) == 0 { pass = pass + 1; t_puts("PASS\n" as *u8) } else { t_puts("FAIL\n" as *u8) }
86
87 t_puts("UXF-TOLERANT-READER-GATE passed " as *u8); t_putn(pass); t_puts("/" as *u8); t_putn(total)
88 if pass == total { t_puts(" verdict=GREEN\n" as *u8); sys_exit(0); return 0 }
89 t_puts(" verdict=RED\n" as *u8); sys_exit(1); return 1
90}