code wiki / (root) / nx_https_fetch_follow_test.nx

nx_https_fetch_follow_test.nx source

↩ module page · 49 lines · 2459 B

1// nx_https_fetch_follow_test.nx -- LIVE gate for fetcher F-2 (redirect-follow). 2// Proves end-to-end against real sites: (A) example.com -> 200 + body, (B) 3// google.com -> 301 to www.google.com -> followed -> final 200 (a real hop). 4// expect_exit: 0 license_tier: ORIGINAL 5 6import "nx_syscalls.nx" 7import "nx_x509_trust_store.nx" 8import "nx_trust_store_load_from_certdata.nx" 9import "nx_https_fetch_follow.nx" 10 11func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 12func wn(v: i64) -> i64 { let b: *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{b[i]=t[k-1-i];i=i+1} sys_write(1,b,k); return 0 } 13func contains(hay: *u8, hn: i64, ndl: *u8, nn: i64) -> i64 { 14 if nn == 0 { return 1 } 15 var i: i64 = 0 16 while i + nn <= hn { 17 var j: i64 = 0 18 while j < nn { if hay[i+j] != ndl[j] { break } j = j + 1 } 19 if j == nn { return 1 } 20 i = i + 1 21 } 22 return 0 23} 24 25func main() -> i64 { 26 let r: i64 = nx_trust_store_load_from_certdata("/tmp/mozilla_certdata.txt" as *u8, 300, 4194304) 27 if r <= 0 { w("STORE-FAIL (need /tmp/mozilla_certdata.txt)\n" as *u8); return 1 } 28 let store: *TrustStore = r as *TrustStore 29 let out: *u8 = sys_mmap(262144) 30 31 // --- A: no-redirect fetch via the wrapper --- 32 let stA: *i64 = (sys_mmap(8)) as *i64 33 let nA: i64 = nx_https_fetch_follow("https://example.com/" as *u8, store, out, 262144, 5, stA) 34 w("A example.com status=" as *u8); wn(stA[0]); w(" bytes=" as *u8); wn(nA); w("\n" as *u8) 35 var okA: i64 = 0 36 if stA[0] == 200 { if contains(out, nA, "Example Domain" as *u8, 14) == 1 { okA = 1 } } 37 38 // --- B: redirect-following (google.com -> www.google.com) --- 39 let stB: *i64 = (sys_mmap(8)) as *i64 40 let nB: i64 = nx_https_fetch_follow("https://google.com/" as *u8, store, out, 262144, 5, stB) 41 w("B google.com final status=" as *u8); wn(stB[0]); w(" bytes=" as *u8); wn(nB); w("\n" as *u8) 42 var okB: i64 = 0 43 if stB[0] == 200 { okB = 1 } 44 45 if okA == 1 { w("A PASS (fetch + 200 + body)\n" as *u8) } else { w("A FAIL\n" as *u8) } 46 if okB == 1 { w("B PASS (redirect followed to final 200)\n" as *u8) } else { w("B FAIL (no final 200)\n" as *u8) } 47 if okA == 1 { if okB == 1 { w("F-2 LIVE GREEN\n" as *u8); return 0 } } 48 return 2 49}