code wiki / (root) / nx_url_resolve_test.nx

nx_url_resolve_test.nx source

↩ module page · 108 lines · 4738 B

1// nx_url_resolve_test.nx -- KAT for RFC 3986 §5.2 examples. 2// 3// Uses the canonical reference set from RFC 3986 §5.4 with the base 4// http://a/b/c/d;p?q 5// plus a few extras for HTML img-src-in-the-wild patterns. 6// 7// expect_exit: 0 8// license_tier: ORIGINAL 9 10import "nx_syscalls.nx" 11import "nx_url_resolve.nx" 12 13func _fail(n: i64) -> i64 { 14 let b: *u8 = sys_mmap(16) 15 b[0]=0x46; b[1]=0x41; b[2]=0x49; b[3]=0x4C; b[4]=0x3D 16 sys_write(2, b, 5) 17 var x: i64 = n 18 if x < 0 { let m: *u8 = sys_mmap(4); m[0]=0x2D; sys_write(2, m, 1); x = 0 - x } 19 if x == 0 { let z: *u8 = sys_mmap(4); z[0]=0x30; sys_write(2, z, 1) } 20 else { 21 let buf: *u8 = sys_mmap(16) 22 var pos: i64 = 0 23 while x > 0 { buf[pos] = (0x30 + (x % 10)) as u8; x = x / 10; pos = pos + 1 } 24 let out: *u8 = sys_mmap(16) 25 var i: i64 = 0 26 while i < pos { out[i] = buf[pos - 1 - i]; i = i + 1 } 27 sys_write(2, out, pos) 28 } 29 let nl: *u8 = sys_mmap(4); nl[0]=0x0A; sys_write(2, nl, 1) 30 return 0 31} 32 33func _strlen(s: *u8) -> i64 { 34 var n: i64 = 0 35 while s[n] != 0 { n = n + 1 } 36 return n 37} 38 39func _bytes_eq(a: *u8, an: i64, b: *u8, bn: i64) -> i64 { 40 if an != bn { return 0 } 41 var i: i64 = 0 42 while i < an { 43 if a[i] != b[i] { return 0 } 44 i = i + 1 45 } 46 return 1 47} 48 49func _check(case_id: i64, base: *u8, rel: *u8, expected: *u8) -> i64 { 50 let out: *u8 = sys_mmap(512) 51 let out_len_p: *i64 = sys_mmap(8) as *i64 52 let bn: i64 = _strlen(base) 53 let rn: i64 = _strlen(rel) 54 let en: i64 = _strlen(expected) 55 let rc: i64 = nx_url_resolve(base, bn, rel, rn, out, 512, out_len_p) 56 if rc != NX_URL_RESOLVE_OK { _fail(case_id * 10); return 0 - 1 } 57 let actual_len: i64 = out_len_p[0] 58 if _bytes_eq(out, actual_len, expected, en) != 1 { 59 // Dump actual + expected to stderr for debugging. 60 let lab: *u8 = sys_mmap(8) 61 lab[0]=0x67; lab[1]=0x6F; lab[2]=0x74; lab[3]=0x3D; sys_write(2, lab, 4) // got= 62 sys_write(2, out, actual_len) 63 let nl: *u8 = sys_mmap(4); nl[0]=0x0A; sys_write(2, nl, 1) 64 let wnt: *u8 = sys_mmap(8) 65 wnt[0]=0x77; wnt[1]=0x6E; wnt[2]=0x74; wnt[3]=0x3D; sys_write(2, wnt, 4) // wnt= 66 sys_write(2, expected, en) 67 sys_write(2, nl, 1) 68 _fail(case_id); return 0 - 1 69 } 70 return 0 71} 72 73func main() -> i64 { 74 let base: *u8 = "http://a/b/c/d;p?q" 75 76 // RFC 3986 §5.4.1 Normal Examples (subset). 77 if _check(1, base, "g:h" as *u8, "g:h" as *u8) < 0 { return 1 } 78 if _check(2, base, "g" as *u8, "http://a/b/c/g" as *u8) < 0 { return 2 } 79 if _check(3, base, "./g" as *u8, "http://a/b/c/g" as *u8) < 0 { return 3 } 80 if _check(4, base, "g/" as *u8, "http://a/b/c/g/" as *u8) < 0 { return 4 } 81 if _check(5, base, "/g" as *u8, "http://a/g" as *u8) < 0 { return 5 } 82 if _check(6, base, "//g" as *u8, "http://g" as *u8) < 0 { return 6 } 83 if _check(7, base, "?y" as *u8, "http://a/b/c/d;p?y" as *u8) < 0 { return 7 } 84 if _check(8, base, "g?y" as *u8, "http://a/b/c/g?y" as *u8) < 0 { return 8 } 85 if _check(9, base, "#s" as *u8, "http://a/b/c/d;p?q#s" as *u8) < 0 { return 9 } 86 if _check(10, base, "g#s" as *u8, "http://a/b/c/g#s" as *u8) < 0 { return 10 } 87 if _check(11, base, "g?y#s" as *u8, "http://a/b/c/g?y#s" as *u8) < 0 { return 11 } 88 if _check(12, base, ";x" as *u8, "http://a/b/c/;x" as *u8) < 0 { return 12 } 89 90 // Dot-segment resolution. 91 if _check(20, base, "../g" as *u8, "http://a/b/g" as *u8) < 0 { return 20 } 92 if _check(21, base, "../" as *u8, "http://a/b/" as *u8) < 0 { return 21 } 93 if _check(22, base, "../.." as *u8, "http://a/" as *u8) < 0 { return 22 } 94 if _check(23, base, "../../" as *u8, "http://a/" as *u8) < 0 { return 23 } 95 if _check(24, base, "../../g" as *u8, "http://a/g" as *u8) < 0 { return 24 } 96 97 // Img-src patterns common in the wild. 98 let base2: *u8 = "https://www.google.com/" 99 if _check(40, base2, "/images/logo.png" as *u8, "https://www.google.com/images/logo.png" as *u8) < 0 { return 40 } 100 if _check(41, base2, "images/logo.png" as *u8, "https://www.google.com/images/logo.png" as *u8) < 0 { return 41 } 101 if _check(42, base2, "https://other.com/x.png" as *u8, "https://other.com/x.png" as *u8) < 0 { return 42 } 102 if _check(43, base2, "//cdn.com/a.png" as *u8, "https://cdn.com/a.png" as *u8) < 0 { return 43 } 103 104 let pass: *u8 = sys_mmap(16) 105 pass[0]=0x50; pass[1]=0x41; pass[2]=0x53; pass[3]=0x53; pass[4]=0x0A 106 sys_write(1, pass, 5) 107 return 0 108}