code wiki / _hdl_build / nx_route_gate.nx
nx_route_gate.nx source
↩ module page · 77 lines · 4886 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"
7
8func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
9func 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 }
10func 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 }
11
12const 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
13
14// check one route; ec=expected code, eo=expected out (ignored for 404/bad). returns 1 pass.
15func chk(path: *u8, ec: i64, eo: *u8) -> i64 {
16 let out: *u8 = sys_mmap(1024)
17 let c: i64 = route(path, MANIFEST, out)
18 var ok: i64 = 1
19 if c != ec { ok = 0 }
20 if c == 0 { if eq(out, eo) == 0 { ok = 0 } }
21 if c == 1 { if eq(out, eo) == 0 { ok = 0 } }
22 w(" "); w(path); w(" -> code="); wn(c); if c<=1 { w(" '"); w(out); w("'") }
23 if ok == 1 { w(" ok\n") } else { w(" FAIL(exp "); wn(ec); w(")\n") }
24 return ok
25}
26
27func main(argc: i64, argv: *i64) -> i64 {
28 w("=== nx_route_gate -- S-class routing (clean, canonical, safe, consistent) ===\n" as *u8)
29 var pass: i64 = 0; var tot: i64 = 0
30
31 w(" T1 clean extensionless URLs resolve:\n" as *u8)
32 var a: i64 = 1
33 if chk("/vn" as *u8, 0, "/vn.html" as *u8) == 0 { a = 0 }
34 if chk("/games" as *u8, 0, "/games.html" as *u8) == 0 { a = 0 }
35 if chk("/wiki/intro" as *u8, 0, "/wiki/intro.html" as *u8) == 0 { a = 0 }
36 if chk("/blog" as *u8, 0, "/blog/index.html" as *u8) == 0 { a = 0 }
37 if chk("/" as *u8, 0, "/index.html" as *u8) == 0 { a = 0 }
38 tot = tot + 1; if a == 1 { pass = pass + 1; w(" PASS T1\n" as *u8) } else { w(" FAIL T1\n" as *u8) }
39
40 w(" T2 ONE canonical URL (.html + trailing-slash -> 301 clean) -- the /vn vs /vn.html mess fixed:\n" as *u8)
41 var b: i64 = 1
42 if chk("/vn.html" as *u8, 1, "/vn" as *u8) == 0 { b = 0 }
43 if chk("/vn/" as *u8, 1, "/vn" as *u8) == 0 { b = 0 }
44 if chk("/wiki/intro.html" as *u8, 1, "/wiki/intro" as *u8) == 0 { b = 0 }
45 tot = tot + 1; if b == 1 { pass = pass + 1; w(" PASS T2\n" as *u8) } else { w(" FAIL T2\n" as *u8) }
46
47 w(" T3 assets served exact:\n" as *u8)
48 var c3: i64 = 1
49 if chk("/style.css" as *u8, 0, "/style.css" as *u8) == 0 { c3 = 0 }
50 if chk("/vn_bg.png" as *u8, 0, "/vn_bg.png" as *u8) == 0 { c3 = 0 }
51 tot = tot + 1; if c3 == 1 { pass = pass + 1; w(" PASS T3\n" as *u8) } else { w(" FAIL T3\n" as *u8) }
52
53 w(" T4 traversal blocked (neg-control) + 404:\n" as *u8)
54 var d: i64 = 1
55 if chk("/a/../secret" as *u8, 3, "" as *u8) == 0 { d = 0 }
56 if chk("/nope" as *u8, 2, "" as *u8) == 0 { d = 0 }
57 tot = tot + 1; if d == 1 { pass = pass + 1; w(" PASS T4\n" as *u8) } else { w(" FAIL T4\n" as *u8) }
58
59 // T5 QUANTITATIVE: canonical round-trip holds for EVERY clean URL via the ONE engine
60 w(" T5 quantitative consistency (round-trip canonical over all clean URLs):\n" as *u8)
61 let cleans: *i64 = sys_mmap(8*8) as *i64
62 cleans[0] = "/vn" as *u8 as i64; cleans[1] = "/games" as *u8 as i64; cleans[2] = "/wiki/intro" as *u8 as i64
63 var rt: i64 = 1; var i: i64 = 0; let out: *u8 = sys_mmap(1024); let hpath: *u8 = sys_mmap(1024)
64 while i < 3 {
65 let cl: *u8 = cleans[i] as *u8
66 if route(cl, MANIFEST, out) != 0 { rt = 0 } // clean URL serves
67 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
68 if route(hpath, MANIFEST, out) != 1 { rt = 0 } // its .html 301s
69 if eq(out, cl) == 0 { rt = 0 } // ...back to the clean URL
70 i = i + 1
71 }
72 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) }
73
74 w("nx_route_gate pass="); wn(pass); w("/"); wn(tot)
75 if pass == tot { w(" verdict=GREEN (S-class routing: clean + canonical + safe + uniform for all delivery)\n" as *u8); sys_exit(0); return 0 }
76 w(" verdict=RED\n" as *u8); sys_exit(1); return 1
77}