Debian Codegraph @

tests/pid_tests.rs

raw symbols SCIP
1 //! Tests for PID helper functions (getpid, getppid)
2 //!
3 //! This module tests the convenience wrappers for getting process IDs:
4 //! - `getpid()` - Get current process ID
5 //! - `getppid()` - Get parent process ID
6 //!
7 //! These tests verify that the wrappers correctly hide unsafe code
8 //! and return valid process IDs in both parent and child processes.
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 // Tests that getpid() returns a valid positive process ID
22 // Expected behavior:
23 // 1. getpid() returns current process ID
24 // 2. PID should be positive
25 // 3. PID should be consistent across multiple calls
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 // Tests that getppid() returns a valid parent process ID
36 // Expected behavior:
37 // 1. getppid() returns parent process ID
38 // 2. Parent PID should be positive
39 // 3. Parent PID should be consistent
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 // Tests that child process has different PID from parent
50 // Expected behavior:
51 // 1. Parent gets its PID
52 // 2. Child gets its PID
53 // 3. Child PID != Parent PID
54 // 4. Child's parent PID == Parent's PID
55 let parent_pid = getpid();
56
57 match fork() {
58 Ok(Fork::Parent(child_pid)) => {
59 // Verify fork returned correct child PID
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 // Child's PID should be different from parent's
73 assert_ne!(
74 child_pid, parent_pid,
75 "Child should have different PID from parent"
76 );
77
78 // Child's parent PID should match original parent's PID
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 // Tests that getpid() in child matches PID returned by fork() in parent
93 // Expected behavior:
94 // 1. Parent gets child PID from fork()
95 // 2. Child calls getpid()
96 // 3. Both should match
97 match fork() {
98 Ok(Fork::Parent(fork_child_pid)) => {
99 // Parent waits for child to complete
100 let status = waitpid(fork_child_pid).expect("waitpid failed");
101 assert!(libc::WIFEXITED(status), "Child should exit normally");
102
103 // We can't directly compare here, but child will verify
104 }
105 Ok(Fork::Child) => {
106 // Child verifies its PID
107 let my_pid = getpid();
108 assert!(my_pid > 0, "Child PID should be positive");
109
110 // Note: We can't pass this back to parent easily,
111 // but the fact that both calls succeed validates the wrapper
112
113 exit(0);
114 }
115 Err(_) => panic!("Fork failed"),
116 }
117 }
118
119 #[test]
120 fn test_getppid_returns_parent_pid() {
121 // Tests that child's getppid() returns the parent's getpid()
122 // Expected behavior:
123 // 1. Parent records its PID
124 // 2. Child calls getppid()
125 // 3. Child's parent PID matches parent's PID
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 // Tests that getpid() hides unsafe from user code
149 // Expected behavior:
150 // 1. User can call getpid() without unsafe block
151 // 2. Function returns valid PID
152 // 3. Type is libc::pid_t
153
154 // This compiles without unsafe - that's the test!
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 // Tests that getppid() hides unsafe from user code
163 // Expected behavior:
164 // 1. User can call getppid() without unsafe block
165 // 2. Function returns valid PID
166 // 3. Type is libc::pid_t
167
168 // This compiles without unsafe - that's the test!
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 // Tests PID functions work correctly with multiple forks
177 // Expected behavior:
178 // 1. Create multiple children
179 // 2. Each child has unique PID
180 // 3. All children have same parent PID
181 let parent_pid = getpid();
182 let mut child_pids = vec![];
183
184 // Create 3 children
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 // Verify child has different PID
195 assert_ne!(my_pid, parent_pid, "Child {i} should have different PID");
196
197 // Verify parent PID is correct
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 // Parent waits for all children
210 for child_pid in child_pids {
211 waitpid(child_pid).expect("waitpid failed");
212 }
213
214 // Verify our PID is still the same
215 assert_eq!(getpid(), parent_pid, "Parent PID should not change");
216 }
217
218 #[test]
219 fn test_getpid_consistency_across_operations() {
220 // Tests that getpid() remains consistent during process lifetime
221 // Expected behavior:
222 // 1. PID remains the same throughout process execution
223 // 2. PID doesn't change after fork (in same process)
224 // 3. PID doesn't change after other operations
225 let pid1 = getpid();
226
227 // Do some work
228 let _ppid = getppid();
229
230 let pid2 = getpid();
231 assert_eq!(pid1, pid2, "PID should remain consistent");
232
233 // Fork and verify parent PID doesn't change
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 // Tests that grandchild gets reparented to init (PID 1)
253 // Expected behavior:
254 // 1. Create child, child creates grandchild
255 // 2. Child exits
256 // 3. Grandchild's parent becomes init (PID 1)
257 match fork() {
258 Ok(Fork::Parent(child_pid)) => {
259 // Wait for child to exit
260 waitpid(child_pid).expect("waitpid failed");
261 // Grandchild will be reparented, but we can't directly observe it here
262 }
263 Ok(Fork::Child) => {
264 let child_pid = getpid();
265
266 // Create grandchild
267 match fork() {
268 Ok(Fork::Parent(_)) => {
269 // Child exits immediately, orphaning grandchild
270 exit(0);
271 }
272 Ok(Fork::Child) => {
273 // Give parent time to exit
274 std::thread::sleep(std::time::Duration::from_millis(100));
275
276 // Grandchild's parent should now be init (PID 1)
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 // Verify we're not the original child
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 }