Debian Codegraph @

tests/README.md

raw symbols SCIP
1 # Integration Tests
2
3 This directory contains integration tests for the `fork` library. These tests run in separate processes and can properly test functions like `daemon()` that call `exit()`.
4
5 ## Overview
6
7 The integration tests are organized into nine files:
8 - **`daemon_tests.rs`** - Daemon functionality
9 - **`fork_tests.rs`** - Fork/waitpid functionality
10 - **`integration_tests.rs`** - Advanced patterns
11 - **`stdio_redirect_tests.rs`** - Stdio redirection and fd safety
12 - **`waitpid_tests.rs`** - Exit codes, signals, error handling, and non-blocking waits
13 - **`error_handling_tests.rs`** - Error paths and type verification
14 - **`pid_tests.rs`** - PID helper functions (getpid, getppid)
15 - **`status_macro_tests.rs`** - Status macro re-exports
16 - **`chdir_tests.rs`** - Comprehensive chdir() function tests
17 - **`common/mod.rs`** - Shared test utilities
18
19 Comprehensive coverage of process management, daemon creation, stdio safety, fork patterns, exit status handling, non-blocking waits, PID helpers, status macros, directory operations, and error scenarios.
20
21 ## Test Files
22
23 ### `daemon_tests.rs` - Daemon Functionality Tests
24
25 Tests the `daemon()` function with real process daemonization. Each test documents:
26 - What is being tested
27 - Expected behavior (numbered steps)
28 - Why it matters for daemon creation
29
30 Tests include:
31 - **test_daemon_creates_detached_process** - Verifies daemon process creation and PID management
32 - **test_daemon_with_nochdir** - Tests `nochdir` option preserves current directory
33 - **test_daemon_process_group** - Verifies daemon process group structure (double-fork pattern)
34 - **test_daemon_with_command_execution** - Tests command execution in daemon context
35 - **test_daemon_no_controlling_terminal** - Verifies daemon has no controlling terminal
36
37 ### `fork_tests.rs` - Fork Functionality Tests
38
39 Tests the core `fork()` and `waitpid()` functions. Each test explains the expected parent-child behavior.
40
41 Tests include:
42 - **test_fork_basic** - Basic fork/waitpid functionality and cleanup
43 - **test_fork_parent_child_communication** - File-based parent-child IPC pattern
44 - **test_fork_multiple_children** - Creating and managing multiple child processes
45 - **test_fork_child_inherits_environment** - Environment variable inheritance across fork
46 - **test_fork_child_can_execute_commands** - Command execution in child processes
47 - **test_fork_child_has_different_pid** - PID uniqueness between parent and child
48 - **test_waitpid_waits_for_child** - Proper parent-child synchronization
49
50 ### `integration_tests.rs` - Advanced Pattern Tests
51
52 Tests complex usage patterns combining multiple operations. Documents real-world daemon scenarios.
53
54 Tests include:
55 - **test_double_fork_daemon_pattern** - Classic double-fork daemon creation (standard pattern)
56 - **test_setsid_creates_new_session** - Session management and session leader verification
57 - **test_chdir_changes_directory** - Directory changes in child processes
58 - **test_process_isolation** - File system isolation between parent/child (separate memory)
59 - **test_chdir_error_handling** - Ensures chdir propagates errors correctly
60 - **test_chdir_returns_io_error** - Verifies error types returned from chdir
61 - **test_getpgrp_returns_process_group** - Process group queries and verification
62
63 ### `stdio_redirect_tests.rs` - Stdio Redirection Tests
64
65 Tests stdin/stdout/stderr safety and fd reuse protection.
66
67 Tests include:
68 - **test_redirect_stdio_prevents_fd_reuse** - Ensures `/dev/null` redirection blocks fd reuse
69 - **test_redirect_stdio_idempotent** - Multiple calls are safe
70 - **test_redirect_stdio_println_safety** - `println!` goes to `/dev/null` after redirect
71 - **test_daemon_uses_redirect_stdio** - Confirms `daemon()` uses redirect_stdio
72 - **test_redirect_stdio_error_handling** - Propagates errors from failed redirection
73 - **test_fd_reuse_corruption_scenario** - Demonstrates corruption risk when closing stdio
74 - **test_close_fd_allows_fd_reuse** - Shows fd reuse when stdio is closed (expected panic)
75 - **test_close_retry_ok_and_ebadf** - Unit-level check that repeated closes handle EINTR/EBADF gracefully
76
77 ### `waitpid_tests.rs` - Waitpid Comprehensive Tests
78
79 Tests all aspects of the `waitpid()` and `waitpid_nohang()` functions including error handling, exit codes, signal termination, and non-blocking waits.
80
81 **Blocking waitpid() tests:**
82 - **test_waitpid_invalid_pid** - ECHILD error for non-existent PID
83 - **test_waitpid_double_wait** - ECHILD error for already-waited child
84 - **test_waitpid_exit_code_zero** - Exit code 0 handling
85 - **test_waitpid_exit_code_one** - Exit code 1 handling
86 - **test_waitpid_exit_code_42** - Arbitrary exit code handling
87 - **test_waitpid_exit_code_127** - Command not found exit code
88 - **test_waitpid_multiple_exit_codes** - Tests codes 0,1,2,42,100,127,255
89 - **test_waitpid_signal_termination_sigkill** - SIGKILL detection
90 - **test_waitpid_signal_termination_sigterm** - SIGTERM detection
91 - **test_waitpid_signal_termination_sigabrt** - SIGABRT (abort) detection
92 - **test_waitpid_distinguishes_exit_vs_signal** - WIFEXITED vs WIFSIGNALED
93 - **test_waitpid_returns_raw_status** - Raw status code return verification
94
95 **Non-blocking waitpid_nohang() tests:**
96 - **test_waitpid_nohang_child_still_running** - Returns None when child running
97 - **test_waitpid_nohang_child_exited** - Returns Some(status) when child exited
98 - **test_waitpid_nohang_poll_until_exit** - Polling pattern until child exits
99 - **test_waitpid_nohang_invalid_pid** - ECHILD error for non-existent PID
100 - **test_waitpid_nohang_multiple_children** - Poll multiple children without blocking
101 - **test_waitpid_nohang_returns_option** - Verify Option<c_int> return type
102 - **test_waitpid_nohang_vs_blocking** - Compare blocking vs non-blocking behavior
103
104 ### `error_handling_tests.rs` - Error Path Tests
105
106 Tests error scenarios and type verification for all library functions.
107
108 Tests include:
109 - **test_setsid_error_when_already_session_leader** - EPERM when calling setsid twice
110 - **test_setsid_returns_io_error_type** - io::Error type and EPERM verification
111 - **test_fork_returns_io_error_type** - io::Result<Fork> type verification
112 - **test_waitpid_returns_io_error_type** - io::Result<c_int> type verification
113 - **test_getpgrp_returns_pid_type** - pid_t type verification (getpgrp always succeeds per POSIX)
114 - **test_close_fd_error_handling** - close_fd error scenarios
115 - **test_error_kind_matching** - io::ErrorKind pattern matching
116 - **test_fork_child_pid_method** - Fork::child_pid() correctness
117 - **test_fork_is_parent_is_child_methods** - Fork::is_parent() and is_child()
118
119 ### `common/mod.rs` - Shared Test Utilities
120
121 Provides reusable helper functions to reduce code duplication:
122 - `get_unique_test_dir()` - Creates unique test directories with atomic counter
123 - `get_test_dir()` - Creates simple test directories
124 - `setup_test_dir()` - Sets up and cleans test directory
125 - `wait_for_file()` - Waits for file creation with timeout
126 - `cleanup_test_dir()` - Removes test directory
127
128 ### `pid_tests.rs` - PID Helper Function Tests
129
130 Tests the convenience wrapper functions for getting process IDs without requiring unsafe code.
131
132 Tests include:
133 - **test_getpid_returns_valid_pid** - Verifies `getpid()` returns valid positive PID
134 - **test_getppid_returns_valid_pid** - Verifies `getppid()` returns valid parent PID
135 - **test_getpid_different_in_child** - Confirms child has different PID from parent
136 - **test_getpid_matches_fork_result** - Verifies `getpid()` matches fork's returned child PID
137 - **test_getppid_returns_parent_pid** - Confirms child's parent PID matches parent's PID
138 - **test_getpid_no_unsafe_in_user_code** - Proves user can call without unsafe block
139 - **test_getppid_no_unsafe_in_user_code** - Proves parent PID getter hides unsafe
140 - **test_pid_functions_in_multiple_forks** - Tests PID functions with multiple children
141 - **test_getpid_consistency_across_operations** - Verifies PID stability during lifetime
142 - **test_getppid_after_parent_exits** - Tests orphan reparenting to init (PID 1)
143
144 ### `status_macro_tests.rs` - Status Macro Re-export Tests
145
146 Tests that status inspection macros can be imported from `fork` crate instead of requiring `libc`.
147
148 Tests include:
149 - **test_wifexited_macro_works** - Verifies `WIFEXITED` can be imported from fork
150 - **test_wexitstatus_macro_works** - Verifies `WEXITSTATUS` can be imported from fork
151 - **test_wifsignaled_macro_works** - Verifies `WIFSIGNALED` can be imported from fork
152 - **test_wtermsig_macro_works** - Verifies `WTERMSIG` can be imported from fork
153 - **test_all_macros_together** - Tests using all macros together for status inspection
154 - **test_macros_with_multiple_exit_codes** - Tests macros work with various exit codes (0, 1, 42, 127, 255)
155 - **test_macros_distinguish_exit_vs_signal** - Confirms macros correctly identify exit vs signal termination
156 - **test_no_libc_import_needed** - Proves users don't need `libc` import for status macros
157
158 ### `chdir_tests.rs` - Comprehensive chdir() Function Tests
159
160 Tests all aspects of the `chdir()` function including modern `c""` string literal implementation.
161
162 Tests include:
163 - **test_chdir_basic_success** - Verifies successful directory change to root
164 - **test_chdir_returns_unit** - Confirms return type is `io::Result<()>`
165 - **test_chdir_changes_actual_working_directory** - Validates real filesystem effects
166 - **test_chdir_idempotent** - Tests multiple successive calls are safe
167 - **test_chdir_process_isolation** - Verifies child chdir doesn't affect parent
168 - **test_chdir_with_file_operations** - Tests relative path operations after chdir
169 - **test_chdir_with_absolute_path_operations** - Confirms absolute paths still work
170 - **test_chdir_error_type** - Validates proper `io::Error` type on failure
171 - **test_chdir_concurrent_forks** - Tests with multiple concurrent child processes
172 - **test_chdir_before_and_after_setsid** - Integration with setsid (daemon pattern)
173 - **test_chdir_uses_c_string_literal** - Validates modern `c""` literal implementation (calls chdir 100 times)
174 - **test_chdir_with_env_manipulation** - Tests with modified environment variables
175
176 ## Running Tests
177
178 ```bash
179 # Run all tests (unit + integration + doc)
180 cargo test
181
182 # For more stable process-based tests (avoid rare flakiness), run serially
183 RUST_TEST_THREADS=1 cargo test
184
185 # Run only integration tests
186 cargo test --tests
187
188 # Run specific test file
189 cargo test --test daemon_tests
190 cargo test --test fork_tests
191 cargo test --test integration_tests
192 cargo test --test chdir_tests
193
194 # Run specific test
195 cargo test --test daemon_tests test_daemon_creates_detached_process
196
197 # Run with output
198 cargo test --test daemon_tests -- --nocapture
199
200 # Run with verbose output
201 cargo test --test fork_tests -- --nocapture --test-threads=1
202 ```
203
204 ## How Integration Tests Work
205
206 Unlike unit tests in `src/lib.rs`, integration tests:
207
208 1. **Run in separate processes** - Each test file is compiled as its own binary
209 2. **Can call `daemon()`** - The parent process in tests doesn't terminate the test runner
210 3. **Use file-based communication** - Temporary files in `/tmp` for parent-child verification
211 4. **Have proper isolation** - Each test uses unique temporary directories to avoid conflicts
212 5. **Clean up after themselves** - Temporary files are removed after test completion
213 6. **Document expected behavior** - Each test has detailed comments explaining what happens
214
215 ## Test Documentation
216
217 Every test includes comprehensive documentation:
218
219 ```rust
220 #[test]
221 fn test_name() {
222 // Clear description of what is being tested
223 // Expected behavior:
224 // 1. First step
225 // 2. Second step
226 // 3. Third step
227 // 4. Fourth step
228 // 5. Final verification
229
230 [test implementation]
231 }
232 ```
233
234 This makes it easy to:
235 - Understand test purpose at a glance
236 - Debug failures quickly
237 - Use tests as usage examples
238 - Onboard new contributors
239
240 ## Test Isolation
241
242 Each test uses a unique temporary directory to prevent conflicts when running in parallel:
243
244 ```rust
245 // Daemon tests use atomic counter for uniqueness
246 let test_dir = setup_test_dir(get_unique_test_dir("daemon_creates_detached"));
247
248 // Fork tests use descriptive prefixes
249 let test_dir = setup_test_dir(get_test_dir("fork_communication"));
250
251 // Integration tests use specific names
252 let test_dir = setup_test_dir(get_test_dir("int_double_fork"));
253 ```
254
255 This allows tests to run in parallel without interfering with each other.
256
257 ## Coverage
258
259 Integration tests provide coverage for:
260
261 - **Daemon creation** - Real process daemonization (not mocked)
262 - **Process groups** - Session management and process group creation
263 - **File descriptors** - Proper handling of stdin/stdout/stderr
264 - **IPC patterns** - Parent-child communication via files
265 - **Command execution** - Running commands in forked/daemon processes
266 - **Environment inheritance** - Variable passing across fork
267 - **Process isolation** - Memory separation and filesystem sharing
268 - **Double-fork pattern** - Standard daemon creation technique
269 - **PID management** - Process ID tracking and verification
270 - **Exit status handling** - Exit codes (0-255) and status inspection
271 - **Signal termination** - SIGKILL, SIGTERM, SIGABRT detection
272 - **Error scenarios** - ECHILD, EPERM, and invalid input handling
273 - **Type safety** - io::Error verification and error kind matching
274 - **Fork helper methods** - is_parent(), is_child(), child_pid()
275
276
277 ## Module Structure
278
279 ```
280 tests/
281 ├── common/
282 │ └── mod.rs # Shared utilities (51 lines)
283 ├── daemon_tests.rs # Daemon tests (271 lines, 5 tests)
284 ├── fork_tests.rs # Fork tests (301 lines, 7 tests)
285 ├── integration_tests.rs # Advanced tests (284 lines, 7 tests)
286 ├── stdio_redirect_tests.rs # Stdio safety tests (313 lines, 7 tests)
287 ├── waitpid_tests.rs # Waitpid tests (591 lines, 20 tests)
288 ├── error_handling_tests.rs # Error tests (260 lines, 9 tests)
289 ├── pid_tests.rs # PID helper tests (252 lines, 10 tests)
290 ├── status_macro_tests.rs # Status macro tests (211 lines, 8 tests)
291 ├── chdir_tests.rs # chdir tests (346 lines, 12 tests)
292 └── README.md # This file
293 ```