code wiki / _hdl_build / nx_route_gate.nx

nx_route_gate.nx source

↩ module page · 85 lines · 5259 B

1// nx_route_gate.nx -- exhaustive proof of S-class routing. QUALITATIVE: clean extensionless URLs resolve, every 2// .html / trailing-slash collapses to ONE canonical URL (the /vn-vs-/vn.html mess fixed), assets served exact, 3// traversal blocked, misses 404. QUANTITATIVE: the SAME one engine handles every path uniformly + the canonical 4// round-trip holds for all (route(X)->serve, route(X.html)->301 X). license_tier: ORIGINAL expect_exit: 0 5import "nx_route.nx" 6import "nx_syscalls.nx" 7import "nx_gate_verdict.nx" 8 9func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 10func wn(v: i64) -> i64 { var m: i64=v; if m<0{w("-" as *u8);m=0-m} let t:*u8=sys_mmap(24); 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; let o:*u8=sys_mmap(24); while i<k{o[i]=t[k-1-i];i=i+1} sys_write(1,o,k); return 0 } 11func eq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8){ if a[i]!=b[i]{return 0} i=i+1 } if b[i]!=(0 as u8){return 0} return 1 } 12 13const MANIFEST: *u8 = "\n/index.html\n/vn.html\n/games.html\n/wiki/intro.html\n/style.css\n/vn_bg.png\n/blog/index.html\n" as *u8 14 15// check one route; ec=expected code, eo=expected out (ignored for 404/bad). returns 1 pass. 16func chk(path: *u8, ec: i64, eo: *u8) -> i64 { 17 let out: *u8 = sys_mmap(1024) 18 let c: i64 = route(path, MANIFEST, out) 19 var ok: i64 = 1 20 if c != ec { ok = 0 } 21 if c == 0 { if eq(out, eo) == 0 { ok = 0 } } 22 if c == 1 { if eq(out, eo) == 0 { ok = 0 } } 23 w(" "); w(path); w(" -> code="); wn(c); if c<=1 { w(" '"); w(out); w("'") } 24 if ok == 1 { w(" ok\n") } else { w(" FAIL(exp "); wn(ec); w(")\n") } 25 return ok 26} 27 28func main(argc: i64, argv: *i64) -> i64 { 29 w("=== nx_route_gate -- S-class routing (clean, canonical, safe, consistent) ===\n" as *u8) 30 var pass: i64 = 0; var tot: i64 = 0 31 32 w(" T1 clean extensionless URLs resolve:\n" as *u8) 33 var a: i64 = 1 34 if chk("/vn" as *u8, 0, "/vn.html" as *u8) == 0 { a = 0 } 35 if chk("/games" as *u8, 0, "/games.html" as *u8) == 0 { a = 0 } 36 if chk("/wiki/intro" as *u8, 0, "/wiki/intro.html" as *u8) == 0 { a = 0 } 37 if chk("/blog" as *u8, 0, "/blog/index.html" as *u8) == 0 { a = 0 } 38 if chk("/" as *u8, 0, "/index.html" as *u8) == 0 { a = 0 } 39 tot = tot + 1; if a == 1 { pass = pass + 1; w(" PASS T1\n" as *u8) } else { w(" FAIL T1\n" as *u8) } 40 41 w(" T2 ONE canonical URL (.html + trailing-slash -> 301 clean) -- the /vn vs /vn.html mess fixed:\n" as *u8) 42 var b: i64 = 1 43 if chk("/vn.html" as *u8, 1, "/vn" as *u8) == 0 { b = 0 } 44 if chk("/vn/" as *u8, 1, "/vn" as *u8) == 0 { b = 0 } 45 if chk("/wiki/intro.html" as *u8, 1, "/wiki/intro" as *u8) == 0 { b = 0 } 46 tot = tot + 1; if b == 1 { pass = pass + 1; w(" PASS T2\n" as *u8) } else { w(" FAIL T2\n" as *u8) } 47 48 w(" T3 assets served exact:\n" as *u8) 49 var c3: i64 = 1 50 if chk("/style.css" as *u8, 0, "/style.css" as *u8) == 0 { c3 = 0 } 51 if chk("/vn_bg.png" as *u8, 0, "/vn_bg.png" as *u8) == 0 { c3 = 0 } 52 tot = tot + 1; if c3 == 1 { pass = pass + 1; w(" PASS T3\n" as *u8) } else { w(" FAIL T3\n" as *u8) } 53 54 w(" T4 traversal blocked (neg-control) + 404:\n" as *u8) 55 var d: i64 = 1 56 if chk("/a/../secret" as *u8, 3, "" as *u8) == 0 { d = 0 } 57 if chk("/nope" as *u8, 2, "" as *u8) == 0 { d = 0 } 58 tot = tot + 1; if d == 1 { pass = pass + 1; w(" PASS T4\n" as *u8) } else { w(" FAIL T4\n" as *u8) } 59 60 // T5 QUANTITATIVE: canonical round-trip holds for EVERY clean URL via the ONE engine 61 w(" T5 quantitative consistency (round-trip canonical over all clean URLs):\n" as *u8) 62 let cleans: *i64 = sys_mmap(8*8) as *i64 63 cleans[0] = "/vn" as *u8 as i64; cleans[1] = "/games" as *u8 as i64; cleans[2] = "/wiki/intro" as *u8 as i64 64 var rt: i64 = 1; var i: i64 = 0; let out: *u8 = sys_mmap(1024); let hpath: *u8 = sys_mmap(1024) 65 while i < 3 { 66 let cl: *u8 = cleans[i] as *u8 67 if route(cl, MANIFEST, out) != 0 { rt = 0 } // clean URL serves 68 var l: i64 = 0; while cl[l] != (0 as u8) { hpath[l] = cl[l]; l = l + 1 } hpath[l]=46 as u8; hpath[l+1]=104 as u8; hpath[l+2]=116 as u8; hpath[l+3]=109 as u8; hpath[l+4]=108 as u8; hpath[l+5]=0 as u8 69 if route(hpath, MANIFEST, out) != 1 { rt = 0 } // its .html 301s 70 if eq(out, cl) == 0 { rt = 0 } // ...back to the clean URL 71 i = i + 1 72 } 73 tot = tot + 1; if rt == 1 { pass = pass + 1; w(" PASS T5 every clean URL <-> .html round-trips through the ONE engine\n" as *u8) } else { w(" FAIL T5\n" as *u8) } 74 75 w("nx_route_gate pass="); wn(pass); w("/"); wn(tot) 76 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check 77 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled 78 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify. 79 let ctr__dry: *i64 = gv_ctr() 80 ctr__dry[0] = pass 81 ctr__dry[1] = tot 82 let rc__dry: i64 = gv_verdict("ROUTE-GATE" as *u8, ctr__dry, "S-class routing: clean + canonical + safe + uniform for all delivery)" as *u8) 83 sys_exit(rc__dry) 84 return rc__dry 85}