nx_html_decode_entities_test.nx source
↩ module page · 33 lines · 1652 B
1// nx_html_decode_entities_test.nx -- KAT for nx_html_decode_entities (the render-side entity decoder).
2// Named refs, numeric dec/hex, and the passthrough-on-unknown contract (so malformed input survives).
3import "nx_syscalls.nx"
4import "nx_html_entities.nx"
5
6func de_slen(s: *u8) -> i64 { var k: i64 = 0; while s[k] != (0 as u8) { k = k + 1 } return k }
7func de_eq(a: *u8, alen: i64, b: *u8) -> i64 {
8 let bl: i64 = de_slen(b)
9 if alen != bl { return 0 }
10 var i: i64 = 0
11 while i < alen { if (a[i]&0xff) != (b[i]&0xff) { return 0 } i = i + 1 }
12 return 1
13}
14func de_check(input: *u8, expect: *u8) -> i64 {
15 let il: i64 = de_slen(input)
16 let out: *u8 = sys_mmap(256)
17 let n: i64 = nx_html_decode_entities(input, 0, il, out, 256)
18 return de_eq(out, n, expect)
19}
20
21func main() -> i64 {
22 if de_check("&\x00" as *u8, "&\x00" as *u8) == 0 { return 1 }
23 if de_check("a<b>c\x00" as *u8, "a<b>c\x00" as *u8) == 0 { return 2 }
24 if de_check("A\x00" as *u8, "A\x00" as *u8) == 0 { return 3 }
25 if de_check("A\x00" as *u8, "A\x00" as *u8) == 0 { return 4 }
26 if de_check("'\x00" as *u8, "'\x00" as *u8) == 0 { return 5 }
27 if de_check("&bogus;\x00" as *u8, "&bogus;\x00" as *u8) == 0 { return 6 } // unknown -> literal passthrough
28 if de_check("x & y\x00" as *u8, "x & y\x00" as *u8) == 0 { return 7 } // bare & -> literal
29 if de_check("plain text.\x00" as *u8, "plain text.\x00" as *u8) == 0 { return 8 }
30 if de_check("Tom & Jerry <3\x00" as *u8, "Tom & Jerry <3\x00" as *u8) == 0 { return 9 }
31 sys_write(1, "nx_html_decode_entities: 9/9 PASS\n" as *u8, 34)
32 return 0
33}