| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#![allow(clippy::expect_used)] |
| 11 |
#![allow(clippy::unwrap_used)] |
| 12 |
#![allow(clippy::panic)] |
| 13 |
#![allow(clippy::match_wild_err_arm)] |
| 14 |
|
| 15 |
use std::process::exit; |
| 16 |
|
| 17 |
use fork::{Fork, fork, getpid, getppid, waitpid}; |
| 18 |
|
| 19 |
#[test] |
| 20 |
fn test_getpid_returns_valid_pid() { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
let pid1 = getpid(); |
| 27 |
let pid2 = getpid(); |
| 28 |
|
| 29 |
assert!(pid1 > 0, "PID should be positive"); |
| 30 |
assert_eq!(pid1, pid2, "PID should be consistent"); |
| 31 |
} |
| 32 |
|
| 33 |
#[test] |
| 34 |
fn test_getppid_returns_valid_pid() { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
let ppid1 = getppid(); |
| 41 |
let ppid2 = getppid(); |
| 42 |
|
| 43 |
assert!(ppid1 > 0, "Parent PID should be positive"); |
| 44 |
assert_eq!(ppid1, ppid2, "Parent PID should be consistent"); |
| 45 |
} |
| 46 |
|
| 47 |
#[test] |
| 48 |
fn test_getpid_different_in_child() { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
let parent_pid = getpid(); |
| 56 |
|
| 57 |
match fork() { |
| 58 |
Ok(Fork::Parent(child_pid)) => { |
| 59 |
|
| 60 |
assert!(child_pid > 0, "Child PID from fork should be positive"); |
| 61 |
assert_ne!( |
| 62 |
child_pid, parent_pid, |
| 63 |
"Child PID should differ from parent PID" |
| 64 |
); |
| 65 |
|
| 66 |
waitpid(child_pid).expect("waitpid failed"); |
| 67 |
} |
| 68 |
Ok(Fork::Child) => { |
| 69 |
let child_pid = getpid(); |
| 70 |
let child_parent_pid = getppid(); |
| 71 |
|
| 72 |
|
| 73 |
assert_ne!( |
| 74 |
child_pid, parent_pid, |
| 75 |
"Child should have different PID from parent" |
| 76 |
); |
| 77 |
|
| 78 |
|
| 79 |
assert_eq!( |
| 80 |
child_parent_pid, parent_pid, |
| 81 |
"Child's parent PID should match parent's PID" |
| 82 |
); |
| 83 |
|
| 84 |
exit(0); |
| 85 |
} |
| 86 |
Err(_) => panic!("Fork failed"), |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
fn test_getpid_matches_fork_result() { |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
match fork() { |
| 98 |
Ok(Fork::Parent(fork_child_pid)) => { |
| 99 |
|
| 100 |
let status = waitpid(fork_child_pid).expect("waitpid failed"); |
| 101 |
assert!(libc::WIFEXITED(status), "Child should exit normally"); |
| 102 |
|
| 103 |
|
| 104 |
} |
| 105 |
Ok(Fork::Child) => { |
| 106 |
|
| 107 |
let my_pid = getpid(); |
| 108 |
assert!(my_pid > 0, "Child PID should be positive"); |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
exit(0); |
| 114 |
} |
| 115 |
Err(_) => panic!("Fork failed"), |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
#[test] |
| 120 |
fn test_getppid_returns_parent_pid() { |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
let parent_pid = getpid(); |
| 127 |
|
| 128 |
match fork() { |
| 129 |
Ok(Fork::Parent(child_pid)) => { |
| 130 |
waitpid(child_pid).expect("waitpid failed"); |
| 131 |
} |
| 132 |
Ok(Fork::Child) => { |
| 133 |
let my_parent = getppid(); |
| 134 |
|
| 135 |
assert_eq!( |
| 136 |
my_parent, parent_pid, |
| 137 |
"Child's parent PID should match parent's actual PID" |
| 138 |
); |
| 139 |
|
| 140 |
exit(0); |
| 141 |
} |
| 142 |
Err(_) => panic!("Fork failed"), |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
#[test] |
| 147 |
fn test_getpid_no_unsafe_in_user_code() { |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
let pid: libc::pid_t = getpid(); |
| 156 |
|
| 157 |
assert!(pid > 0, "PID should be positive"); |
| 158 |
} |
| 159 |
|
| 160 |
#[test] |
| 161 |
fn test_getppid_no_unsafe_in_user_code() { |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
let ppid: libc::pid_t = getppid(); |
| 170 |
|
| 171 |
assert!(ppid > 0, "Parent PID should be positive"); |
| 172 |
} |
| 173 |
|
| 174 |
#[test] |
| 175 |
fn test_pid_functions_in_multiple_forks() { |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
let parent_pid = getpid(); |
| 182 |
let mut child_pids = vec![]; |
| 183 |
|
| 184 |
|
| 185 |
for i in 0..3 { |
| 186 |
match fork() { |
| 187 |
Ok(Fork::Parent(child_pid)) => { |
| 188 |
child_pids.push(child_pid); |
| 189 |
} |
| 190 |
Ok(Fork::Child) => { |
| 191 |
let my_pid = getpid(); |
| 192 |
let my_parent = getppid(); |
| 193 |
|
| 194 |
|
| 195 |
assert_ne!(my_pid, parent_pid, "Child {i} should have different PID"); |
| 196 |
|
| 197 |
|
| 198 |
assert_eq!( |
| 199 |
my_parent, parent_pid, |
| 200 |
"Child {i} should have correct parent PID" |
| 201 |
); |
| 202 |
|
| 203 |
exit(i); |
| 204 |
} |
| 205 |
Err(_) => panic!("Fork {i} failed"), |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
for child_pid in child_pids { |
| 211 |
waitpid(child_pid).expect("waitpid failed"); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
assert_eq!(getpid(), parent_pid, "Parent PID should not change"); |
| 216 |
} |
| 217 |
|
| 218 |
#[test] |
| 219 |
fn test_getpid_consistency_across_operations() { |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
let pid1 = getpid(); |
| 226 |
|
| 227 |
|
| 228 |
let _ppid = getppid(); |
| 229 |
|
| 230 |
let pid2 = getpid(); |
| 231 |
assert_eq!(pid1, pid2, "PID should remain consistent"); |
| 232 |
|
| 233 |
|
| 234 |
match fork() { |
| 235 |
Ok(Fork::Parent(child_pid)) => { |
| 236 |
let pid3 = getpid(); |
| 237 |
assert_eq!(pid1, pid3, "Parent PID should not change after fork"); |
| 238 |
|
| 239 |
waitpid(child_pid).expect("waitpid failed"); |
| 240 |
} |
| 241 |
Ok(Fork::Child) => { |
| 242 |
let child_pid = getpid(); |
| 243 |
assert_ne!(child_pid, pid1, "Child should have different PID"); |
| 244 |
exit(0); |
| 245 |
} |
| 246 |
Err(_) => panic!("Fork failed"), |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
#[test] |
| 251 |
fn test_getppid_after_parent_exits() { |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
match fork() { |
| 258 |
Ok(Fork::Parent(child_pid)) => { |
| 259 |
|
| 260 |
waitpid(child_pid).expect("waitpid failed"); |
| 261 |
|
| 262 |
} |
| 263 |
Ok(Fork::Child) => { |
| 264 |
let child_pid = getpid(); |
| 265 |
|
| 266 |
|
| 267 |
match fork() { |
| 268 |
Ok(Fork::Parent(_)) => { |
| 269 |
|
| 270 |
exit(0); |
| 271 |
} |
| 272 |
Ok(Fork::Child) => { |
| 273 |
|
| 274 |
std::thread::sleep(std::time::Duration::from_millis(100)); |
| 275 |
|
| 276 |
|
| 277 |
let current_parent = getppid(); |
| 278 |
assert_eq!( |
| 279 |
current_parent, 1, |
| 280 |
"Orphaned grandchild should be reparented to init (PID 1)" |
| 281 |
); |
| 282 |
|
| 283 |
|
| 284 |
let my_pid = getpid(); |
| 285 |
assert_ne!(my_pid, child_pid, "Grandchild should have different PID"); |
| 286 |
|
| 287 |
exit(0); |
| 288 |
} |
| 289 |
Err(_) => exit(1), |
| 290 |
} |
| 291 |
} |
| 292 |
Err(_) => panic!("Fork failed"), |
| 293 |
} |
| 294 |
} |