nx_pages_config_test.nx source
↩ module page · 66 lines · 2626 B
1// nx_pages_config_test.nx -- KAT for D1.6 config-file parser.
2// Writes a test config to a temp file, loads it, asserts vhost+fallback
3// match expectations. Returns 0 on all pass, otherwise the index of
4// the failing assertion.
5
6import "nx_syscalls.nx"
7import "nx_pages_config.nx"
8import "nx_pages_vhost.nx"
9
10func _pct_strlen(s: *u8) -> i64 {
11 var n: i64 = 0
12 while s[n] != 0 { n = n + 1 }
13 return n
14}
15
16func main() -> i64 {
17 // Write a test config file via openat_wr + write.
18 let path: *u8 = "/tmp/nx_pages_config_test.conf" as *u8
19 let body: *u8 = "# header comment\nvhost=nishifamily.com:/share/nishi-pages/dist\n \nvhost=www.nishifamily.com:/share/nishi-pages/dist\nvhost=andelinwest.com:/share/andelin-pages/dist\nfallback=0\n# trailing comment\n" as *u8
20 let body_n: i64 = _pct_strlen(body)
21 let fd: i64 = sys_openat_wr(path, 0o644 as i64)
22 if fd < 0 { return 1 }
23 let w: i64 = sys_write(fd, body, body_n)
24 sys_close(fd)
25 if w != body_n { return 2 }
26
27 // Set up vhost table + load.
28 let arena: *u8 = sys_mmap(4096)
29 let vhosts: *i64 = sys_mmap(16 * NX_VHOST_STRIDE * 8) as *i64
30 let table: *NxVHostTable = sys_mmap(40) as *NxVHostTable
31 let cursor: *i64 = sys_mmap(8) as *i64
32 cursor[0] = 0
33 nx_vhost_table_init(table, arena, 4096 as i64, vhosts)
34
35 let rc: i64 = nx_pages_config_load(path, table, cursor)
36 if rc != NX_PCFG_OK { return 10 + rc }
37
38 // Assert 3 vhosts parsed.
39 if table.n_vhosts != 3 { return 50 + table.n_vhosts }
40
41 // Assert vhost 0 = nishifamily.com (15 chars) -> /share/nishi-pages/dist (23 chars).
42 if table.vhosts[0 * NX_VHOST_STRIDE + NX_VHOST_HNAME_LEN] != 15 { return 21 }
43 if table.vhosts[0 * NX_VHOST_STRIDE + NX_VHOST_ROOT_LEN] != 23 { return 22 }
44
45 // Assert vhost 1 = www.nishifamily.com (19) -> /share/nishi-pages/dist (23).
46 if table.vhosts[1 * NX_VHOST_STRIDE + NX_VHOST_HNAME_LEN] != 19 { return 23 }
47 if table.vhosts[1 * NX_VHOST_STRIDE + NX_VHOST_ROOT_LEN] != 23 { return 24 }
48
49 // Assert vhost 2 = andelinwest.com (15) -> /share/andelin-pages/dist (25).
50 if table.vhosts[2 * NX_VHOST_STRIDE + NX_VHOST_HNAME_LEN] != 15 { return 25 }
51 if table.vhosts[2 * NX_VHOST_STRIDE + NX_VHOST_ROOT_LEN] != 25 { return 26 }
52
53 // Assert fallback set to 0.
54 if table.fallback_idx != 0 { return 27 }
55
56 // Verify arena bytes match the host strings (sanity).
57 // arena[0..15] should be "nishifamily.com"
58 let exp_host: *u8 = "nishifamily.com" as *u8
59 var i: i64 = 0
60 while i < 15 {
61 if arena[i] != exp_host[i] { return 30 + i }
62 i = i + 1
63 }
64
65 return 0
66}