nx_print_response_test.nx source
↩ module page · 121 lines · 4629 B
1// nx_print_response_test.nx -- exercise the response-mapper +
2// pause/cancel request builders.
3//
4// Closed-form invariants:
5// (a) NONE -> NONE response, no bytes written
6// (b) STUCK / FLOW / RUNAWAY / BED / HOTEND / RUNOUT -> PAUSE
7// (c) CHAMBER -> NOTIFY (no bytes written)
8// (d) Z_BACKWARD / LAYER_SHIFT -> HARD_STOP (cancel)
9// (e) Built pause request contains "POST /printer/print/pause"
10// (f) Built cancel request contains "POST /printer/print/cancel"
11// (g) Built request contains Host header + Content-Length: 0
12// (h) Buffer overflow returns -1
13//
14// expect_exit: 0
15// license_tier: ORIGINAL
16
17import "nx_syscalls.nx"
18import "nx_print_runtime_monitor.nx"
19import "nx_print_response.nx"
20
21// substring match helper
22func contains_substring(hay: *u8, hay_len: i64, needle: *u8) -> i64 {
23 var n_len: i64 = 0
24 while needle[n_len] != 0 { n_len = n_len + 1 }
25 if n_len > hay_len { return 0 }
26 var i: i64 = 0
27 while i <= hay_len - n_len {
28 var j: i64 = 0
29 var ok: i64 = 1
30 while j < n_len {
31 if hay[i + j] != needle[j] { ok = 0; j = n_len }
32 j = j + 1
33 }
34 if ok == 1 { return 1 }
35 i = i + 1
36 }
37 return 0
38}
39
40func main() -> i64 {
41 // ===== (a) NONE =====
42 if nx_print_response_for_issue(NX_RT_ISSUE_NONE) != NX_PRINT_RESPONSE_NONE { return 10 }
43
44 // ===== (b) PAUSE kinds =====
45 if nx_print_response_for_issue(NX_RT_ISSUE_STUCK_LAYER) != NX_PRINT_RESPONSE_PAUSE { return 20 }
46 if nx_print_response_for_issue(NX_RT_ISSUE_FLOW_ANOMALY) != NX_PRINT_RESPONSE_PAUSE { return 21 }
47 if nx_print_response_for_issue(NX_RT_ISSUE_EXTRUDER_RUNAWAY) != NX_PRINT_RESPONSE_PAUSE { return 22 }
48 if nx_print_response_for_issue(NX_RT_ISSUE_BED_TEMP_DROP) != NX_PRINT_RESPONSE_PAUSE { return 23 }
49 if nx_print_response_for_issue(NX_RT_ISSUE_HOTEND_TEMP_DROP) != NX_PRINT_RESPONSE_PAUSE { return 24 }
50 if nx_print_response_for_issue(NX_RT_ISSUE_FILAMENT_RUNOUT) != NX_PRINT_RESPONSE_PAUSE { return 25 }
51
52 // ===== (c) NOTIFY (chamber) =====
53 if nx_print_response_for_issue(NX_RT_ISSUE_CHAMBER_TEMP_DROP) != NX_PRINT_RESPONSE_NOTIFY { return 30 }
54
55 // ===== (d) HARD_STOP =====
56 if nx_print_response_for_issue(NX_RT_ISSUE_Z_BACKWARD) != NX_PRINT_RESPONSE_HARD_STOP { return 40 }
57 if nx_print_response_for_issue(NX_RT_ISSUE_LAYER_SHIFT) != NX_PRINT_RESPONSE_HARD_STOP { return 41 }
58
59 // ===== (e) Pause request bytes =====
60 let buf: *u8 = sys_mmap(1024)
61 let host: *u8 = "192.168.1.123:7125"
62 let n_pause: i64 = nx_print_build_pause_request(
63 host, 18,
64 0 as *u8, 0,
65 buf, 1024)
66 if n_pause <= 0 { return 50 }
67 if contains_substring(buf, n_pause, "POST /printer/print/pause") != 1 { return 51 }
68 if contains_substring(buf, n_pause, "Host: 192.168.1.123:7125") != 1 { return 52 }
69 if contains_substring(buf, n_pause, "Content-Length: 0") != 1 { return 53 }
70
71 // ===== (f) Cancel request bytes =====
72 let buf2: *u8 = sys_mmap(1024)
73 let n_cancel: i64 = nx_print_build_cancel_request(
74 host, 18,
75 0 as *u8, 0,
76 buf2, 1024)
77 if n_cancel <= 0 { return 60 }
78 if contains_substring(buf2, n_cancel, "POST /printer/print/cancel") != 1 { return 61 }
79
80 // ===== (g) Dispatcher returns correct response per kind =====
81 let buf3: *u8 = sys_mmap(1024)
82 // PAUSE kind -> bytes written, contains "pause"
83 let n_d1: i64 = nx_print_build_response_for_issue(
84 NX_RT_ISSUE_STUCK_LAYER,
85 host, 18, 0 as *u8, 0,
86 buf3, 1024)
87 if n_d1 <= 0 { return 70 }
88 if contains_substring(buf3, n_d1, "pause") != 1 { return 71 }
89
90 // HARD_STOP kind -> bytes written, contains "cancel"
91 let buf4: *u8 = sys_mmap(1024)
92 let n_d2: i64 = nx_print_build_response_for_issue(
93 NX_RT_ISSUE_LAYER_SHIFT,
94 host, 18, 0 as *u8, 0,
95 buf4, 1024)
96 if n_d2 <= 0 { return 72 }
97 if contains_substring(buf4, n_d2, "cancel") != 1 { return 73 }
98
99 // NOTIFY kind -> 0 bytes
100 let buf5: *u8 = sys_mmap(1024)
101 let n_d3: i64 = nx_print_build_response_for_issue(
102 NX_RT_ISSUE_CHAMBER_TEMP_DROP,
103 host, 18, 0 as *u8, 0,
104 buf5, 1024)
105 if n_d3 != 0 { return 74 }
106
107 // NONE -> 0 bytes
108 let n_d4: i64 = nx_print_build_response_for_issue(
109 NX_RT_ISSUE_NONE,
110 host, 18, 0 as *u8, 0,
111 buf5, 1024)
112 if n_d4 != 0 { return 75 }
113
114 // ===== (h) Overflow returns -1 =====
115 let tiny: *u8 = sys_mmap(16)
116 let n_ovf: i64 = nx_print_build_pause_request(
117 host, 18, 0 as *u8, 0, tiny, 16)
118 if n_ovf != -1 { return 80 }
119
120 return 0
121}