| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 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 |
|
| 20 |
use fork::{Fork, WEXITSTATUS, WIFEXITED, WIFSIGNALED, WTERMSIG, fork, waitpid}; |
| 21 |
|
| 22 |
#[test] |
| 23 |
fn test_wifexited_macro_works() { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
match fork() { |
| 30 |
Ok(Fork::Parent(child_pid)) => { |
| 31 |
let status = waitpid(child_pid).expect("waitpid failed"); |
| 32 |
|
| 33 |
|
| 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 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 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 |
|
| 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 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
match fork() { |
| 75 |
Ok(Fork::Parent(child_pid)) => { |
| 76 |
let status = waitpid(child_pid).expect("waitpid failed"); |
| 77 |
|
| 78 |
|
| 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); |
| 86 |
} |
| 87 |
Err(_) => panic!("Fork failed"), |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
#[test] |
| 92 |
fn test_wtermsig_macro_works() { |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 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 |
|
| 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); |
| 113 |
} |
| 114 |
Err(_) => panic!("Fork failed"), |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
#[test] |
| 119 |
fn test_all_macros_together() { |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
match fork() { |
| 127 |
Ok(Fork::Parent(child_pid)) => { |
| 128 |
let status = waitpid(child_pid).expect("waitpid failed"); |
| 129 |
|
| 130 |
|
| 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 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 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 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 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 |
|
| 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); |
| 221 |
} |
| 222 |
Err(_) => panic!("Fork failed"), |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
#[test] |
| 227 |
fn test_no_libc_import_needed() { |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
match fork() { |
| 239 |
Ok(Fork::Parent(child_pid)) => { |
| 240 |
let status = waitpid(child_pid).expect("waitpid failed"); |
| 241 |
|
| 242 |
|
| 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 |
} |