nx_sov_gate.nx source
↩ module page · 109 lines · 5413 B
1// nx_sov_gate.nx -- gate for the standard sovereign data CLI (the anti-flat-file standard path).
2// T1 put/get roundtrip byte-exact (latest-wins on double-put)
3// T2 has present/absent
4// T3 INTEROP IN: import a key<TAB>value TSV -> keys present in the sovereign store + __ids__ index
5// T4 INTEROP OUT: export -> the derived TSV roundtrips the imported data (boundary view, store=SSOT)
6// T5 NEG: get of an absent key fails cleanly (no fabricated value)
7// T6 fsck CLEAN on the store this gate just wrote (validated record walk, all in-bounds)
8// T7 fsck CAN-FAIL: a crafted TORN segment (huge key-length field past EOF) -> TORN + exit 1
9// (the reader has no bounds validation -- fsck is what makes crash-torn state VISIBLE)
10// license_tier: ORIGINAL expect_exit: 0
11import "nx_sov.nx"
12import "nx_gate_verdict.nx"
13
14const SG_STORE: *u8 = "/tmp/nxsov_gate_store"
15
16func sg_write(path: *u8, s: *u8) -> i64 {
17 let fd: i64 = sys_openat_wr(path, 420)
18 if fd < 0 { return 0 - 1 }
19 sys_write(fd, s, std_slen(s))
20 sys_close(fd)
21 return 0
22}
23
24func sg_streq_n(a: *u8, b: *u8, n: i64) -> i64 {
25 var i: i64 = 0
26 while i < n {
27 if a[i] != b[i] { return 0 }
28 i = i + 1
29 }
30 return 1
31}
32
33func main(argc: i64, argv: *i64) -> i64 {
34 std_putln("NXSOV-GATE: the standard sovereign data path (put/get/has + interop import/export)" as *u8)
35 var pass: i64 = 0
36 let out: *u8 = sys_mmap(65536) as *u8
37 // T1 roundtrip + latest-wins
38 sov_put_str(SG_STORE, "k1" as *u8, "first" as *u8)
39 sov_put_str(SG_STORE, "k1" as *u8, "second-wins" as *u8)
40 let n1: i64 = sov_get_copy(SG_STORE, "k1" as *u8, out, 65536)
41 var t1: i64 = 0
42 if n1 == 11 { if sg_streq_n(out, "second-wins" as *u8, 11) == 1 { t1 = 1 } }
43 if t1 == 1 { pass = pass + 1; std_putln("T1 PASS put/get roundtrip + latest-wins" as *u8) }
44 if t1 == 0 { std_puts("T1 FAIL n=" as *u8); std_pdec(n1); std_puts("\n" as *u8) }
45 // T2 has
46 let h1: i64 = sov_has(SG_STORE, "k1" as *u8)
47 let h0: i64 = sov_has(SG_STORE, "nope" as *u8)
48 if h1 == 1 && h0 == 0 { pass = pass + 1; std_putln("T2 PASS has present/absent" as *u8) }
49 if h1 != 1 || h0 != 0 { std_putln("T2 FAIL has" as *u8) }
50 // T3 import (interop IN)
51 sg_write("/tmp/nxsov_in.tsv" as *u8, "alpha\tvalue-a\nbeta\tvalue-b\n" as *u8)
52 let ir: i64 = nxsov_import(SG_STORE, "/tmp/nxsov_in.tsv" as *u8)
53 let ha: i64 = sov_has(SG_STORE, "alpha" as *u8)
54 let hb: i64 = sov_has(SG_STORE, "beta" as *u8)
55 let hi: i64 = sov_has(SG_STORE, "__ids__" as *u8)
56 if ir == 0 && ha == 1 && hb == 1 && hi == 1 { pass = pass + 1; std_putln("T3 PASS import -> sovereign store + __ids__ index" as *u8) }
57 if ir != 0 || ha != 1 || hb != 1 || hi != 1 { std_putln("T3 FAIL import" as *u8) }
58 // T4 export roundtrip (interop OUT, derived view)
59 let er: i64 = nxsov_export(SG_STORE, "/tmp/nxsov_out.tsv" as *u8)
60 let eb: *u8 = sys_mmap(65536) as *u8
61 let en: i64 = nxsov_read("/tmp/nxsov_out.tsv" as *u8, eb, 65536)
62 var t4: i64 = 0
63 if er == 0 && en > 0 {
64 let want: *u8 = "alpha\tvalue-a\nbeta\tvalue-b\n" as *u8
65 if en == std_slen(want) { if sg_streq_n(eb, want, en) == 1 { t4 = 1 } }
66 }
67 if t4 == 1 { pass = pass + 1; std_putln("T4 PASS export = derived interop view roundtrips" as *u8) }
68 if t4 == 0 { std_puts("T4 FAIL en=" as *u8); std_pdec(en); std_puts("\n" as *u8) }
69 // T5 NEG absent get
70 let n5: i64 = sov_get_copy(SG_STORE, "ghost" as *u8, out, 65536)
71 if n5 < 0 { pass = pass + 1; std_putln("T5 PASS NEG absent key fails cleanly" as *u8) }
72 if n5 >= 0 { std_putln("T5 FAIL ghost returned a value" as *u8) }
73 // T6 fsck clean on the real store this gate populated
74 let f6: i64 = nxsov_fsck(SG_STORE)
75 if f6 == 0 { pass = pass + 1; std_putln("T6 PASS fsck CLEAN on a healthy store" as *u8) }
76 if f6 != 0 { std_putln("T6 FAIL fsck flagged a healthy store" as *u8) }
77 // T7 fsck can-fail: craft a torn segment (kind ok, key-length field points past EOF)
78 sg_write("/tmp/nxsov_torn_manifest.txt" as *u8, "seg-777\n" as *u8)
79 let tf: i64 = sys_openat_wr("/tmp/nxsov_torn_seg-777.docs" as *u8, 420)
80 if tf >= 0 {
81 let tb: *u8 = sys_mmap(16) as *u8
82 tb[0] = 1 as u8
83 tb[1] = 0 as u8
84 tb[2] = 0 as u8
85 tb[3] = 255 as u8
86 tb[4] = 255 as u8
87 tb[5] = 65 as u8
88 tb[6] = 66 as u8
89 tb[7] = 67 as u8
90 tb[8] = 68 as u8
91 tb[9] = 69 as u8
92 sys_write(tf, tb, 10)
93 sys_close(tf)
94 }
95 let f7: i64 = nxsov_fsck("/tmp/nxsov_torn_" as *u8)
96 if f7 == 1 { pass = pass + 1; std_putln("T7 PASS fsck CAN-FAIL: torn segment reported (crash-torn state made visible)" as *u8) }
97 if f7 != 1 { std_puts("T7 FAIL f7=" as *u8); std_pdec(f7); std_puts("\n" as *u8) }
98 std_puts("NXSOV-GATE pass=" as *u8)
99 std_pdec(pass)
100 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
101 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
102 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
103 let ctr__dry: *i64 = gv_ctr()
104 ctr__dry[0] = pass
105 ctr__dry[1] = 7
106 let rc__dry: i64 = gv_verdict("SOV-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
107 sys_exit(rc__dry)
108 return rc__dry
109}