code wiki / (root) / nx_html_links_test.nx

nx_html_links_test.nx source

↩ module page · 38 lines · 1742 B

1// nx_html_links_test.nx -- KATs for the link-extraction kernel (GATE). 2// Construction-known sample: 3 admissible https links (double-quote, single- 3// quote, uppercase HREF) among http/relative/fragment/unquoted decoys + one 4// malformed (unterminated) that must be dropped, not corrupted. 5import "nx_str.nx" 6import "nx_syscalls.nx" 7import "nx_html_links.nx" 8 9func tp(s: *u8) -> i64 { sys_write(1, s, nx_str_len(s)); return 0 } 10 11func teq(a: *u8, b: *u8) -> i64 { 12 var i: i64 = 0 13 while a[i] != (0 as u8) { if a[i] != b[i] { return 0 } i = i + 1 } 14 if b[i] != (0 as u8) { return 0 } 15 return 1 16} 17 18func main() -> i64 { 19 tp("=== nx_html_links KAT ===\n") 20 let html: *u8 = "<a href=\"https://one.example/a\">x</a> <a href='https://two.example/b?q=1'>y</a> <a HREF=\"https://Three.example/C\">z</a> <a href=\"http://plain.example/\">no</a> <a href=\"/relative\">no</a> <a href=\"#frag\">no</a> <a href=bare>no</a> <a href=\"https://broken.example/unterminated<div>" 21 let n: i64 = nx_str_len(html) 22 let store: *u8 = sys_mmap(4096) 23 let offs: *i64 = sys_mmap(64 * 8) as *i64 24 let cnt: i64 = nx_html_links(html, n, store, 4096, offs, 64) 25 var pass: i64 = 1 26 if cnt != 3 { pass = 0 } 27 if pass == 1 { 28 if teq((((store as i64) + offs[0]) as *u8), "https://one.example/a") == 0 { pass = 0 } 29 if teq((((store as i64) + offs[1]) as *u8), "https://two.example/b?q=1") == 0 { pass = 0 } 30 if teq((((store as i64) + offs[2]) as *u8), "https://Three.example/C") == 0 { pass = 0 } 31 } 32 tp(" links=") 33 if cnt == 3 { tp("3/3") } else { tp("WRONG-COUNT") } 34 if pass == 1 { tp(" HTML-LINKS-KAT: PASS\n"); sys_exit(0); return 0 } 35 tp(" HTML-LINKS-KAT: FAIL\n") 36 sys_exit(1) 37 return 1 38}