Debian Codegraph @

tests/integration_tests.rs

raw symbols SCIP
1 //! Advanced integration tests for complex fork patterns
2 //!
3 //! This module tests advanced usage patterns and combinations of functions.
4 //! These tests verify:
5 //! - Classic double-fork daemon pattern
6 //! - Session creation and management (setsid)
7 //! - Directory changes in child processes (chdir)
8 //! - Process isolation between parent and child
9 //! - Process group queries (getpgrp)
10 //!
11 //! These tests combine multiple fork operations to test real-world
12 //! daemon creation patterns and process management scenarios.
13
14 #![allow(clippy::expect_used)]
15 #![allow(clippy::unwrap_used)]
16 #![allow(clippy::panic)]
17 #![allow(clippy::match_wild_err_arm)]
18 #![allow(clippy::similar_names)]
19 #![allow(clippy::uninlined_format_args)]
20 #![allow(clippy::indexing_slicing)]
21
22 mod common;
23
24 use std::{env, fs, process::exit, thread, time::Duration};
25
26 use fork::{Fork, chdir, fork, getpgrp, getpid, setsid, waitpid};
27
28 use common::{get_test_dir, get_unique_test_dir, setup_test_dir, wait_for_file};
29
30 #[test]
31 fn test_double_fork_daemon_pattern() {
32 let test_dir = setup_test_dir(get_unique_test_dir("int_double_fork"));
33 let daemon_pid_file = test_dir.join("daemon.pid");
34
35 // First fork
36 match fork().expect("First fork failed") {
37 Fork::Parent(_child) => {
38 // Original parent waits for daemon to create PID file
39 // Use longer timeout for CI environments
40 assert!(
41 wait_for_file(&daemon_pid_file, 3000),
42 "Daemon PID file should exist"
43 );
44
45 // Tests the classic double-fork daemon pattern
46 // Expected behavior:
47 // 1. First fork creates a child process
48 // 2. Child calls setsid() to create new session (becomes session leader)
49 // 3. Child forks again (grandchild)
50 // 4. First child exits (leaving grandchild orphaned)
51 // 5. Grandchild is not session leader (prevents controlling terminal acquisition)
52 // 6. Grandchild writes its PID to file
53 // 7. This is the standard daemon creation pattern
54 let pid_str = fs::read_to_string(&daemon_pid_file).expect("Failed to read PID file");
55 let daemon_pid: i32 = pid_str.trim().parse().expect("Failed to parse daemon PID");
56 assert!(daemon_pid > 0, "Daemon PID should be positive");
57
58 // Cleanup
59 fs::remove_file(&daemon_pid_file).ok();
60 }
61 Fork::Child => {
62 // First child - create new session
63 setsid().expect("setsid failed");
64
65 // Second fork to ensure we're not session leader
66 match fork().expect("Second fork failed") {
67 Fork::Parent(_) => {
68 // First child exits
69 exit(0);
70 }
71 Fork::Child => {
72 // This is the daemon process
73 let pid = getpid();
74 let pgid = getpgrp();
75
76 // Write PID to file
77 fs::write(&daemon_pid_file, format!("{}", pid))
78 .expect("Failed to write daemon PID");
79
80 // Daemon should be in its own process group
81 assert!(pgid > 0);
82
83 exit(0);
84 }
85 }
86 }
87 }
88 }
89
90 #[test]
91 fn test_setsid_creates_new_session() {
92 let test_dir = setup_test_dir(get_unique_test_dir("int_setsid"));
93 let session_file = test_dir.join("session.info");
94
95 match fork().expect("Fork failed") {
96 Fork::Parent(_child) => {
97 thread::sleep(Duration::from_millis(50));
98
99 let content = fs::read_to_string(&session_file).expect("Failed to read session file");
100 let parts: Vec<&str> = content.trim().split(',').collect();
101
102 let sid: i32 = parts[0].parse().expect("Failed to parse SID");
103 let pid: i32 = parts[1].parse().expect("Failed to parse PID");
104 let pgid: i32 = parts[2].parse().expect("Failed to parse PGID");
105
106 // After setsid, PID should equal PGID (session leader)
107 assert_eq!(pid, pgid, "Process should be session leader");
108 assert_eq!(sid, pid, "SID should equal PID for session leader");
109 // Tests session creation and management with setsid()
110 // Expected behavior:
111 // 1. Child process calls setsid()
112 // 2. setsid() creates new session and returns SID
113 // 3. Child becomes session leader (PID == PGID == SID)
114 // 4. This is Step 2 of the daemon pattern
115 // 5. Child writes SID, PID, PGID to file for verification
116
117 // Cleanup
118 fs::remove_file(&session_file).ok();
119 }
120 Fork::Child => {
121 // Create new session
122 let sid = setsid().expect("setsid failed");
123 let pid = unsafe { libc::getpid() };
124 let pgid = getpgrp();
125
126 fs::write(&session_file, format!("{},{},{}", sid, pid, pgid))
127 .expect("Failed to write session info");
128
129 exit(0);
130 }
131 }
132 }
133
134 #[test]
135 fn test_chdir_changes_directory() {
136 let test_dir = setup_test_dir(get_test_dir("int_chdir"));
137 let dir_file = test_dir.join("directory.info");
138
139 match fork().expect("Fork failed") {
140 Fork::Parent(_child) => {
141 thread::sleep(Duration::from_millis(50));
142 // Tests directory change in child process
143 // Expected behavior:
144 // 1. Child process calls chdir()
145 // 2. chdir() changes current directory to root (/)
146 // 3. Child verifies current directory is /
147 // 4. Child writes directory path to file
148 // 5. Parent verifies child changed directory correctly
149
150 let content = fs::read_to_string(&dir_file).expect("Failed to read dir file");
151 assert_eq!(content.trim(), "/", "Directory should be root");
152
153 // Cleanup
154 fs::remove_file(&dir_file).ok();
155 }
156 Fork::Child => {
157 // Change to root
158 chdir().expect("chdir failed");
159
160 let current = env::current_dir().expect("Failed to get current dir");
161 fs::write(&dir_file, current.to_str().unwrap())
162 .expect("Failed to write directory info");
163
164 exit(0);
165 }
166 }
167 }
168
169 #[test]
170 // Tests process isolation between parent and child
171 // Expected behavior:
172 // 1. Parent writes data to file before fork
173 // 2. Child can see parent's file (same filesystem)
174 // 3. Child writes its own file
175 // 4. Parent can see child's file after fork completes
176 // 5. Both processes can access shared filesystem but have separate memory
177 fn test_process_isolation() {
178 let test_dir = setup_test_dir(get_test_dir("int_isolation"));
179 let parent_file = test_dir.join("parent.txt");
180 let child_file = test_dir.join("child.txt");
181
182 // Parent writes before fork
183 fs::write(&parent_file, "parent data").expect("Failed to write parent file");
184
185 match fork().expect("Fork failed") {
186 Fork::Parent(_child) => {
187 thread::sleep(Duration::from_millis(50));
188
189 // Parent file should still exist
190 assert!(parent_file.exists(), "Parent file should exist");
191
192 // Child should have created its own file
193 assert!(child_file.exists(), "Child file should exist");
194
195 let child_content = fs::read_to_string(&child_file).expect("Failed to read child file");
196 assert_eq!(child_content.trim(), "child data");
197
198 // Cleanup
199 fs::remove_file(&parent_file).ok();
200 fs::remove_file(&child_file).ok();
201 }
202 Fork::Child => {
203 // Child can see parent's file
204 assert!(parent_file.exists(), "Child should see parent file");
205
206 // Child writes its own file
207 fs::write(&child_file, "child data").expect("Failed to write child file");
208
209 exit(0);
210 // Tests process group queries with getpgrp()
211 // Expected behavior:
212 // 1. Both parent and child can call getpgrp()
213 // 2. Both return valid positive PGID values
214 // 3. Initially parent and child share same process group
215 // 4. Used to verify process group membership
216 // 5. Critical for session and job control
217 }
218 }
219 }
220
221 #[test]
222 fn test_getpgrp_returns_process_group() {
223 match fork().expect("Fork failed") {
224 Fork::Parent(_child) => {
225 let parent_pgid = getpgrp();
226 assert!(parent_pgid > 0, "Parent PGID should be positive");
227
228 thread::sleep(Duration::from_millis(50));
229 }
230 Fork::Child => {
231 let child_pgid = getpgrp();
232 assert!(child_pgid > 0, "Child PGID should be positive");
233
234 exit(0);
235 }
236 }
237 }
238
239 #[test]
240 fn test_chdir_error_handling() {
241 // Test that chdir returns proper io::Error
242 match fork().expect("Fork failed") {
243 Fork::Parent(child) => {
244 waitpid(child).expect("waitpid failed");
245 }
246 Fork::Child => {
247 // chdir() to root should succeed
248 let result = chdir();
249 assert!(result.is_ok(), "chdir to root should succeed");
250
251 // Verify we're actually in root
252 let cwd = env::current_dir().expect("Failed to get current dir");
253 assert_eq!(cwd.to_str().unwrap(), "/", "Should be in root directory");
254
255 exit(0);
256 }
257 }
258 }
259
260 #[test]
261 fn test_chdir_returns_io_error() {
262 // Test that chdir returns a proper io::Error type
263 match fork().expect("Fork failed") {
264 Fork::Parent(child) => {
265 waitpid(child).expect("waitpid failed");
266 }
267 Fork::Child => {
268 // Call chdir and verify return type
269 let result: std::io::Result<()> = chdir();
270
271 // Should succeed
272 assert!(result.is_ok());
273
274 // If it were to fail, we could access error details
275 if let Err(e) = result {
276 let _errno = e.raw_os_error();
277 let _msg = format!("{}", e);
278 }
279
280 exit(0);
281 }
282 }
283 }