Debian Codegraph @

tests/status_macro_tests.rs

raw symbols SCIP
1 //! Tests for status macro re-exports
2 //!
3 //! This module tests that users can import status inspection macros
4 //! directly from the fork crate instead of requiring libc:
5 //! - `WIFEXITED` - Check if child exited normally
6 //! - `WEXITSTATUS` - Get exit code
7 //! - `WIFSIGNALED` - Check if child was terminated by signal
8 //! - `WTERMSIG` - Get terminating signal
9 //!
10 //! These tests verify the convenience re-exports work correctly.
11
12 #![allow(clippy::expect_used)]
13 #![allow(clippy::unwrap_used)]
14 #![allow(clippy::panic)]
15 #![allow(clippy::match_wild_err_arm)]
16
17 use std::process::exit;
18
19 // Import macros from fork crate (not libc)
20 use fork::{Fork, WEXITSTATUS, WIFEXITED, WIFSIGNALED, WTERMSIG, fork, waitpid};
21
22 #[test]
23 fn test_wifexited_macro_works() {
24 // Tests that WIFEXITED macro can be imported from fork crate
25 // Expected behavior:
26 // 1. Child exits normally with code 0
27 // 2. Parent uses WIFEXITED to check normal exit
28 // 3. WIFEXITED returns true
29 match fork() {
30 Ok(Fork::Parent(child_pid)) => {
31 let status = waitpid(child_pid).expect("waitpid failed");
32
33 // This macro is re-exported from fork, not libc
34 assert!(WIFEXITED(status), "Child should exit normally");
35 }
36 Ok(Fork::Child) => {
37 exit(0);
38 }
39 Err(_) => panic!("Fork failed"),
40 }
41 }
42
43 #[test]
44 fn test_wexitstatus_macro_works() {
45 // Tests that WEXITSTATUS macro can be imported from fork crate
46 // Expected behavior:
47 // 1. Child exits with code 42
48 // 2. Parent uses WEXITSTATUS to get exit code
49 // 3. WEXITSTATUS returns 42
50 match fork() {
51 Ok(Fork::Parent(child_pid)) => {
52 let status = waitpid(child_pid).expect("waitpid failed");
53
54 assert!(WIFEXITED(status), "Child should exit normally");
55
56 // This macro is re-exported from fork, not libc
57 let exit_code = WEXITSTATUS(status);
58 assert_eq!(exit_code, 42, "Exit code should be 42");
59 }
60 Ok(Fork::Child) => {
61 exit(42);
62 }
63 Err(_) => panic!("Fork failed"),
64 }
65 }
66
67 #[test]
68 fn test_wifsignaled_macro_works() {
69 // Tests that WIFSIGNALED macro can be imported from fork crate
70 // Expected behavior:
71 // 1. Child kills itself with SIGKILL
72 // 2. Parent uses WIFSIGNALED to check signal termination
73 // 3. WIFSIGNALED returns true
74 match fork() {
75 Ok(Fork::Parent(child_pid)) => {
76 let status = waitpid(child_pid).expect("waitpid failed");
77
78 // This macro is re-exported from fork, not libc
79 assert!(WIFSIGNALED(status), "Child should be terminated by signal");
80 }
81 Ok(Fork::Child) => {
82 unsafe {
83 libc::raise(libc::SIGKILL);
84 }
85 exit(0); // Never reached
86 }
87 Err(_) => panic!("Fork failed"),
88 }
89 }
90
91 #[test]
92 fn test_wtermsig_macro_works() {
93 // Tests that WTERMSIG macro can be imported from fork crate
94 // Expected behavior:
95 // 1. Child kills itself with SIGTERM
96 // 2. Parent uses WTERMSIG to get signal number
97 // 3. WTERMSIG returns SIGTERM
98 match fork() {
99 Ok(Fork::Parent(child_pid)) => {
100 let status = waitpid(child_pid).expect("waitpid failed");
101
102 assert!(WIFSIGNALED(status), "Child should be terminated by signal");
103
104 // This macro is re-exported from fork, not libc
105 let signal = WTERMSIG(status);
106 assert_eq!(signal, libc::SIGTERM, "Signal should be SIGTERM");
107 }
108 Ok(Fork::Child) => {
109 unsafe {
110 libc::raise(libc::SIGTERM);
111 }
112 exit(0); // Never reached
113 }
114 Err(_) => panic!("Fork failed"),
115 }
116 }
117
118 #[test]
119 fn test_all_macros_together() {
120 // Tests using all re-exported macros together
121 // Expected behavior:
122 // 1. Child exits with code 7
123 // 2. Parent uses all macros to inspect status
124 // 3. WIFEXITED true, WIFSIGNALED false
125 // 4. WEXITSTATUS returns 7
126 match fork() {
127 Ok(Fork::Parent(child_pid)) => {
128 let status = waitpid(child_pid).expect("waitpid failed");
129
130 // All these macros are re-exported from fork
131 if WIFEXITED(status) {
132 let code = WEXITSTATUS(status);
133 assert_eq!(code, 7, "Exit code should be 7");
134 } else if WIFSIGNALED(status) {
135 let signal = WTERMSIG(status);
136 panic!("Child unexpectedly terminated by signal {signal}");
137 } else {
138 panic!("Child in unexpected state");
139 }
140 }
141 Ok(Fork::Child) => {
142 exit(7);
143 }
144 Err(_) => panic!("Fork failed"),
145 }
146 }
147
148 #[test]
149 fn test_macros_with_multiple_exit_codes() {
150 // Tests macros work with various exit codes
151 // Expected behavior:
152 // 1. Create children with different exit codes
153 // 2. Parent uses macros to verify each code
154 // 3. All codes match expected values
155 let exit_codes = [0, 1, 42, 127, 255];
156
157 for expected_code in exit_codes {
158 match fork() {
159 Ok(Fork::Parent(child_pid)) => {
160 let status = waitpid(child_pid).expect("waitpid failed");
161
162 assert!(
163 WIFEXITED(status),
164 "Child should exit normally with code {expected_code}"
165 );
166
167 let actual_code = WEXITSTATUS(status);
168 assert_eq!(
169 actual_code, expected_code,
170 "Exit code should be {expected_code}"
171 );
172 }
173 Ok(Fork::Child) => {
174 exit(expected_code);
175 }
176 Err(_) => panic!("Fork failed"),
177 }
178 }
179 }
180
181 #[test]
182 fn test_macros_distinguish_exit_vs_signal() {
183 // Tests macros can distinguish normal exit from signal termination
184 // Expected behavior:
185 // 1. First child exits normally
186 // 2. Second child is killed by signal
187 // 3. Macros correctly identify each case
188
189 // Test normal exit
190 match fork() {
191 Ok(Fork::Parent(child_pid)) => {
192 let status = waitpid(child_pid).expect("waitpid failed");
193
194 assert!(WIFEXITED(status), "First child should exit normally");
195 assert!(!WIFSIGNALED(status), "First child should not be signaled");
196 assert_eq!(WEXITSTATUS(status), 0, "Exit code should be 0");
197 }
198 Ok(Fork::Child) => {
199 exit(0);
200 }
201 Err(_) => panic!("Fork failed"),
202 }
203
204 // Test signal termination
205 match fork() {
206 Ok(Fork::Parent(child_pid)) => {
207 let status = waitpid(child_pid).expect("waitpid failed");
208
209 assert!(
210 WIFSIGNALED(status),
211 "Second child should be terminated by signal"
212 );
213 assert!(!WIFEXITED(status), "Second child should not exit normally");
214 assert_eq!(WTERMSIG(status), libc::SIGABRT, "Signal should be SIGABRT");
215 }
216 Ok(Fork::Child) => {
217 unsafe {
218 libc::raise(libc::SIGABRT);
219 }
220 exit(0); // Never reached
221 }
222 Err(_) => panic!("Fork failed"),
223 }
224 }
225
226 #[test]
227 fn test_no_libc_import_needed() {
228 // Tests that users don't need to import libc for status macros
229 // Expected behavior:
230 // 1. This test file only imports from fork, not libc
231 // 2. All status macros work correctly
232 // 3. Code compiles without libc import (except for signals)
233
234 // This is a compile-time test - if it compiles, it passes!
235 // The fact that we can use WIFEXITED, WEXITSTATUS, WIFSIGNALED, WTERMSIG
236 // without importing libc proves the re-exports work.
237
238 match fork() {
239 Ok(Fork::Parent(child_pid)) => {
240 let status = waitpid(child_pid).expect("waitpid failed");
241
242 // No libc:: prefix needed!
243 assert!(WIFEXITED(status));
244 assert_eq!(WEXITSTATUS(status), 13);
245 }
246 Ok(Fork::Child) => {
247 exit(13);
248 }
249 Err(_) => panic!("Fork failed"),
250 }
251 }