Debian Codegraph @

tests/error_handling_tests.rs

raw symbols SCIP
1 //! Error handling and edge case tests
2 //!
3 //! This module tests error scenarios and edge cases for fork library functions:
4 //! - `setsid()` called when already a session leader (EPERM)
5 //! - `close_fd()` error handling
6 //! - Error type verification (`io::Error`)
7 //!
8 //! These tests ensure proper error propagation and handling.
9
10 #![allow(clippy::expect_used)]
11 #![allow(clippy::unwrap_used)]
12 #![allow(clippy::panic)]
13 #![allow(clippy::match_wild_err_arm)]
14 #![allow(clippy::similar_names)]
15 #![allow(clippy::uninlined_format_args)]
16
17 use std::process::exit;
18
19 use fork::{Fork, close_fd, fork, getpgrp, setsid, waitpid};
20
21 #[test]
22 fn test_setsid_error_when_already_session_leader() {
23 // Tests that setsid returns error when called by a session leader
24 // Expected behavior:
25 // 1. Child calls setsid() to become session leader (succeeds)
26 // 2. Child immediately calls setsid() again (should fail with EPERM)
27 // 3. Verifies proper error handling
28 match fork() {
29 Ok(Fork::Parent(child)) => {
30 waitpid(child).expect("waitpid failed");
31 }
32 Ok(Fork::Child) => {
33 // First setsid should succeed
34 let sid = setsid().expect("First setsid should succeed");
35 assert!(sid > 0, "SID should be positive");
36
37 // Verify we're a session leader (PID == PGID)
38 let pid = unsafe { libc::getpid() };
39 let pgid = getpgrp();
40 assert_eq!(pid, pgid, "Should be session leader");
41
42 // Second setsid should fail with EPERM
43 let result = setsid();
44 assert!(result.is_err(), "Second setsid should fail");
45
46 let err = result.unwrap_err();
47 assert_eq!(
48 err.raw_os_error(),
49 Some(libc::EPERM),
50 "Should return EPERM when already session leader"
51 );
52
53 exit(0);
54 }
55 Err(_) => panic!("Fork failed"),
56 }
57 }
58
59 #[test]
60 fn test_fork_returns_io_error_type() {
61 // Tests that fork returns proper io::Error type
62 // Expected behavior:
63 // 1. fork() returns io::Result<Fork>
64 // 2. Error type can be inspected with raw_os_error()
65 // 3. Verifies type signature from v0.4.0
66 match fork() {
67 Ok(Fork::Parent(child)) => {
68 waitpid(child).expect("waitpid failed");
69 }
70 Ok(Fork::Child) => exit(0),
71 Err(e) => {
72 // If fork somehow fails, verify it's a proper io::Error
73 let _errno = e.raw_os_error();
74 let _error_msg = format!("{}", e);
75 panic!("Fork failed: {}", e);
76 }
77 }
78 }
79
80 #[test]
81 fn test_waitpid_returns_io_error_type() {
82 // Tests that waitpid returns proper io::Error on failure
83 // Expected behavior:
84 // 1. waitpid on invalid PID returns Err(io::Error)
85 // 2. Error has raw_os_error() method
86 // 3. Error can be formatted as string
87 match fork() {
88 Ok(Fork::Parent(_)) => {
89 // Try to wait on invalid PID
90 let result = waitpid(999_999);
91 assert!(result.is_err(), "Should fail on invalid PID");
92
93 let err = result.unwrap_err();
94 // Verify io::Error properties
95 assert!(err.raw_os_error().is_some(), "Should have errno");
96 let error_string = format!("{}", err);
97 assert!(!error_string.is_empty(), "Should have error message");
98 }
99 Ok(Fork::Child) => exit(0),
100 Err(_) => panic!("Fork failed"),
101 }
102 }
103
104 #[test]
105 fn test_setsid_returns_io_error_type() {
106 // Tests that setsid returns proper io::Error on failure
107 match fork() {
108 Ok(Fork::Parent(child)) => {
109 waitpid(child).expect("waitpid failed");
110 }
111 Ok(Fork::Child) => {
112 // First call succeeds
113 setsid().expect("First setsid should succeed");
114
115 // Second call fails
116 let result = setsid();
117 assert!(result.is_err(), "Second setsid should fail");
118
119 let err = result.unwrap_err();
120 // Verify io::Error properties
121 assert_eq!(err.raw_os_error(), Some(libc::EPERM));
122 let error_string = format!("{}", err);
123 assert!(
124 error_string.contains("Operation not permitted") || error_string.contains("EPERM"),
125 "Error message should indicate permission error: {}",
126 error_string
127 );
128
129 exit(0);
130 }
131 Err(_) => panic!("Fork failed"),
132 }
133 }
134
135 #[test]
136 fn test_getpgrp_returns_pid_type() {
137 // Tests that getpgrp returns pid_t directly (not Result)
138 // getpgrp() always succeeds per POSIX and cannot fail
139 match fork() {
140 Ok(Fork::Parent(child)) => {
141 waitpid(child).expect("waitpid failed");
142 }
143 Ok(Fork::Child) => {
144 // getpgrp() always succeeds and returns pid_t directly (not Result)
145 let pgid: libc::pid_t = getpgrp();
146 assert!(pgid > 0, "PGID should be positive");
147
148 exit(0);
149 }
150 Err(_) => panic!("Fork failed"),
151 }
152 }
153
154 #[test]
155 fn test_close_fd_error_handling() {
156 // Tests that close_fd handles errors properly
157 // Note: This is tricky because closing stdin/stdout/stderr
158 // should normally succeed. This test just verifies the function
159 // returns io::Result and can be error-checked.
160 match fork() {
161 Ok(Fork::Parent(child)) => {
162 waitpid(child).expect("waitpid failed");
163 }
164 Ok(Fork::Child) => {
165 // Close fds - should succeed normally
166 let result = close_fd();
167 assert!(result.is_ok(), "close_fd should succeed normally");
168
169 // Second call might fail since fds are already closed
170 // but this is implementation-dependent
171 let result2 = close_fd();
172
173 // Either succeeds (idempotent) or fails - both are acceptable
174 match result2 {
175 Ok(()) => {
176 // Idempotent - fine
177 }
178 Err(e) => {
179 // Failed - verify it's a proper io::Error
180 assert!(e.raw_os_error().is_some(), "Should have errno");
181 }
182 }
183
184 exit(0);
185 }
186 Err(_) => panic!("Fork failed"),
187 }
188 }
189
190 #[test]
191 fn test_error_kind_matching() {
192 // Tests that io::Error kinds can be matched for specific handling
193 // Expected behavior:
194 // 1. Errors return standard io::ErrorKind variants
195 // 2. Can pattern match on error kinds
196 // 3. Demonstrates error handling patterns
197 match fork() {
198 Ok(Fork::Parent(_)) => {
199 // Try to wait on invalid PID
200 let result = waitpid(999_999);
201
202 if let Err(e) = result {
203 // Can match on error kind for different handling
204 if e.kind() == std::io::ErrorKind::NotFound {
205 // ECHILD can map to NotFound on some systems
206 }
207
208 // Verify we can access errno
209 assert!(e.raw_os_error().is_some(), "Should have raw OS error code");
210 }
211 }
212 Ok(Fork::Child) => exit(0),
213 Err(_) => panic!("Fork failed"),
214 }
215 }
216
217 #[test]
218 fn test_fork_child_pid_method() {
219 // Tests Fork::child_pid() method returns correct PID
220 match fork() {
221 Ok(Fork::Parent(child_pid)) => {
222 let fork_result = Fork::Parent(child_pid);
223
224 // Test child_pid() method
225 let extracted_pid = fork_result.child_pid();
226 assert!(extracted_pid.is_some(), "Parent should have child PID");
227 assert_eq!(
228 extracted_pid.unwrap(),
229 child_pid,
230 "child_pid() should match fork result"
231 );
232
233 waitpid(child_pid).expect("waitpid failed");
234 }
235 Ok(Fork::Child) => {
236 let fork_result = Fork::Child;
237
238 // Test child_pid() method
239 let extracted_pid = fork_result.child_pid();
240 assert!(extracted_pid.is_none(), "Child should not have child PID");
241
242 exit(0);
243 }
244 Err(_) => panic!("Fork failed"),
245 }
246 }
247
248 #[test]
249 fn test_fork_is_parent_is_child_methods() {
250 // Tests Fork::is_parent() and Fork::is_child() methods
251 match fork() {
252 Ok(result) => {
253 if result.is_parent() {
254 // In parent
255 assert!(!result.is_child(), "Parent should not be child");
256 assert!(result.child_pid().is_some(), "Parent should have child PID");
257
258 let child_pid = result.child_pid().unwrap();
259 waitpid(child_pid).expect("waitpid failed");
260 } else if result.is_child() {
261 // In child
262 assert!(!result.is_parent(), "Child should not be parent");
263 assert!(
264 result.child_pid().is_none(),
265 "Child should not have child PID"
266 );
267 exit(0);
268 }
269 }
270 Err(_) => panic!("Fork failed"),
271 }
272 }