nx_moonraker_io_ipv4_test.nx source
↩ module page · 84 lines · 2896 B
1// nx_moonraker_io_ipv4_test.nx -- exercise the operator-facing
2// arbitrary-IPv4 API (nx_mr_post_ipv4) via loopback (127, 0, 0, 1).
3// Same fork pattern as the v2 loopback smoke but uses the v3 generic
4// API. Proves the operator-facing flow works -- they substitute
5// their printer's LAN IP (e.g., 192, 168, 1, 42) and the same code
6// talks to the real X-Max 3.
7//
8// expect_exit: 0
9// license_tier: ORIGINAL
10
11import "nx_syscalls.nx"
12import "nx_tls13_io.nx"
13import "nx_moonraker_client.nx"
14import "nx_moonraker_io.nx"
15
16const TEST_PORT: i64 = 18448
17
18func smoke_contains(buf: *u8, len: i64, needle: *u8) -> i64 {
19 var nlen: i64 = 0
20 while needle[nlen] != 0 { nlen = nlen + 1 }
21 if len < nlen { return 0 }
22 var i: i64 = 0
23 while i <= len - nlen {
24 var j: i64 = 0
25 var matched: i64 = 1
26 while j < nlen {
27 if buf[i + j] != needle[j] { matched = 0; j = nlen }
28 j = j + 1
29 }
30 if matched == 1 { return 1 }
31 i = i + 1
32 }
33 return 0
34}
35
36func main() -> i64 {
37 let listen_fd: i64 = tcp_listen_loopback(TEST_PORT)
38 if listen_fd < 0 { return 1 }
39
40 let pid: i64 = sys_fork()
41 if pid < 0 { return 2 }
42
43 if pid == 0 {
44 // Child: use the v3 ipv4 API explicitly with (127, 0, 0, 1)
45 var spin: i64 = 0
46 while spin < 50000 { spin = spin + 1 }
47 let scratch: *u8 = sys_mmap(4096)
48 let resp: *u8 = sys_mmap(4096)
49 let host: *u8 = "127.0.0.1:18448"
50 let path: *u8 = "/printer/info"
51 let api: *u8 = "demo"
52 let ipv4: i64 = nx_mr_pack_ipv4(127, 0, 0, 1)
53 let n_resp: i64 = nx_mr_get_ipv4(ipv4, TEST_PORT,
54 host, 15,
55 path, 13,
56 api, 4,
57 scratch, 4096,
58 resp, 4096)
59 if n_resp <= 0 { sys_exit(10) }
60 if nx_mr_parse_status(resp, n_resp) != 200 { sys_exit(11) }
61 sys_exit(0)
62 }
63
64 let cfd: i64 = sys_accept(listen_fd)
65 if cfd < 0 { return 3 }
66
67 let req_buf: *u8 = sys_mmap(4096)
68 let n_req: i64 = sys_read(cfd, req_buf, 4096)
69 if n_req <= 0 { return 4 }
70 if smoke_contains(req_buf, n_req, "GET /printer/info") != 1 { return 5 }
71 if smoke_contains(req_buf, n_req, "Host: 127.0.0.1:18448") != 1 { return 6 }
72 if smoke_contains(req_buf, n_req, "X-Api-Key: demo") != 1 { return 7 }
73
74 // Canned 200 OK with JSON body
75 let resp: *u8 = "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 30\r\nConnection: close\r\n\r\n{\"result\":{\"state\":\"ready\"}}"
76 var rl: i64 = 0
77 while resp[rl] != 0 { rl = rl + 1 }
78 let n_w: i64 = sys_write(cfd, resp, rl)
79 if n_w != rl { return 8 }
80
81 sys_close(cfd)
82 sys_close(listen_fd)
83 return 0
84}