Debian Codegraph @

tests/chdir_tests.rs

raw symbols SCIP
1 //! Comprehensive tests for `chdir()` function
2 //!
3 //! This module thoroughly tests the `chdir()` function to ensure:
4 //! - Successful directory change to root (/)
5 //! - Proper error handling and return types
6 //! - Thread safety and process isolation
7 //! - Integration with fork and daemon patterns
8 //! - Multiple successive calls (idempotent behavior)
9 //! - Verification of actual filesystem effects
10
11 #![allow(clippy::expect_used)]
12 #![allow(clippy::unwrap_used)]
13 #![allow(clippy::panic)]
14 #![allow(clippy::match_wild_err_arm)]
15 #![allow(clippy::uninlined_format_args)]
16
17 use std::{
18 env, fs,
19 path::{Path, PathBuf},
20 process::exit,
21 thread,
22 time::Duration,
23 };
24
25 use fork::{Fork, WEXITSTATUS, WIFEXITED, chdir, fork, waitpid};
26
27 #[test]
28 fn test_chdir_basic_success() {
29 // Test that chdir successfully changes to root directory
30 match fork().expect("Fork failed") {
31 Fork::Parent(child) => {
32 let status = waitpid(child).expect("waitpid failed");
33 assert!(WIFEXITED(status), "Child should exit normally");
34 assert_eq!(WEXITSTATUS(status), 0, "Child should exit with code 0");
35 }
36 Fork::Child => {
37 // Change to root directory
38 match chdir() {
39 Ok(()) => {
40 let cwd = env::current_dir().expect("Failed to get current dir");
41 assert_eq!(cwd.to_str().unwrap(), "/", "Should be in root directory");
42 exit(0);
43 }
44 Err(e) => {
45 eprintln!("chdir failed: {}", e);
46 exit(1);
47 }
48 }
49 }
50 }
51 }
52
53 #[test]
54 fn test_chdir_returns_unit() {
55 // Test that chdir returns () on success
56 match fork().expect("Fork failed") {
57 Fork::Parent(child) => {
58 let status = waitpid(child).expect("waitpid failed");
59 assert!(WIFEXITED(status));
60 assert_eq!(WEXITSTATUS(status), 0);
61 }
62 Fork::Child => {
63 let result: std::io::Result<()> = chdir();
64 assert!(result.is_ok());
65
66 // Verify it returns unit type
67 let _unit: () = result.unwrap();
68 exit(0);
69 }
70 }
71 }
72
73 #[test]
74 fn test_chdir_changes_actual_working_directory() {
75 // Verify chdir actually changes the working directory, not just returns Ok
76 match fork().expect("Fork failed") {
77 Fork::Parent(child) => {
78 let status = waitpid(child).expect("waitpid failed");
79 assert!(WIFEXITED(status));
80 assert_eq!(WEXITSTATUS(status), 0);
81 }
82 Fork::Child => {
83 // Get original directory
84 let original = env::current_dir().expect("Failed to get original dir");
85
86 // Change to root
87 chdir().expect("chdir failed");
88
89 // Get new directory
90 let new_dir = env::current_dir().expect("Failed to get new dir");
91
92 // Verify change occurred
93 assert_eq!(new_dir, PathBuf::from("/"), "Should be in root");
94 if original.as_path() != Path::new("/") {
95 assert_ne!(
96 original, new_dir,
97 "Directory should change when not already at /"
98 );
99 }
100
101 exit(0);
102 }
103 }
104 }
105
106 #[test]
107 fn test_chdir_idempotent() {
108 // Test that calling chdir multiple times is safe (idempotent)
109 match fork().expect("Fork failed") {
110 Fork::Parent(child) => {
111 let status = waitpid(child).expect("waitpid failed");
112 assert!(WIFEXITED(status));
113 assert_eq!(WEXITSTATUS(status), 0);
114 }
115 Fork::Child => {
116 // Call chdir multiple times
117 chdir().expect("First chdir failed");
118 chdir().expect("Second chdir failed");
119 chdir().expect("Third chdir failed");
120
121 // Verify still in root
122 let cwd = env::current_dir().expect("Failed to get current dir");
123 assert_eq!(cwd.to_str().unwrap(), "/");
124
125 exit(0);
126 }
127 }
128 }
129
130 #[test]
131 fn test_chdir_process_isolation() {
132 // Test that chdir in child doesn't affect parent
133 let parent_dir = env::current_dir().expect("Failed to get parent dir");
134
135 match fork().expect("Fork failed") {
136 Fork::Parent(child) => {
137 // Parent waits for child
138 let status = waitpid(child).expect("waitpid failed");
139 assert!(WIFEXITED(status));
140
141 // Parent directory should be unchanged
142 let current = env::current_dir().expect("Failed to get current dir");
143 assert_eq!(current, parent_dir, "Parent directory should not change");
144 }
145 Fork::Child => {
146 // Child changes directory
147 chdir().expect("chdir failed");
148
149 // Verify child is in root
150 let cwd = env::current_dir().expect("Failed to get current dir");
151 assert_eq!(cwd.to_str().unwrap(), "/");
152
153 exit(0);
154 }
155 }
156 }
157
158 #[test]
159 fn test_chdir_with_file_operations() {
160 // Test that chdir affects file operations (relative paths)
161 match fork().expect("Fork failed") {
162 Fork::Parent(child) => {
163 let status = waitpid(child).expect("waitpid failed");
164 assert!(WIFEXITED(status));
165 assert_eq!(WEXITSTATUS(status), 0);
166 }
167 Fork::Child => {
168 // Change to root
169 chdir().expect("chdir failed");
170
171 // Confirm relative operations work from new cwd
172 let cwd = env::current_dir().expect("Failed to get current dir");
173 assert_eq!(cwd.to_str().unwrap(), "/");
174 fs::metadata(".").expect("Root directory metadata should be readable");
175
176 exit(0);
177 }
178 }
179 }
180
181 #[test]
182 fn test_chdir_with_absolute_path_operations() {
183 // Test that absolute paths still work after chdir
184 match fork().expect("Fork failed") {
185 Fork::Parent(child) => {
186 let status = waitpid(child).expect("waitpid failed");
187 assert!(WIFEXITED(status));
188 assert_eq!(WEXITSTATUS(status), 0);
189 }
190 Fork::Child => {
191 let temp_file = std::env::temp_dir().join("fork_test_chdir");
192 fs::write(&temp_file, "test").expect("Failed to write test file");
193
194 // Change directory
195 chdir().expect("chdir failed");
196
197 // Absolute path should still work
198 let content =
199 fs::read_to_string(&temp_file).expect("Failed to read with absolute path");
200 assert_eq!(content, "test");
201
202 // Cleanup
203 fs::remove_file(&temp_file).ok();
204
205 exit(0);
206 }
207 }
208 }
209
210 #[test]
211 fn test_chdir_error_type() {
212 // Test that chdir returns proper io::Error type
213 match fork().expect("Fork failed") {
214 Fork::Parent(child) => {
215 let status = waitpid(child).expect("waitpid failed");
216 assert!(WIFEXITED(status));
217 assert_eq!(WEXITSTATUS(status), 0);
218 }
219 Fork::Child => {
220 let result: std::io::Result<()> = chdir();
221
222 // Should succeed for root directory
223 assert!(result.is_ok());
224
225 // Type check - this is an io::Error if it were to fail
226 match result {
227 Ok(()) => exit(0),
228 Err(e) => {
229 // Verify it's a proper io::Error
230 let _: std::io::Error = e;
231 exit(1);
232 }
233 }
234 }
235 }
236 }
237
238 #[test]
239 fn test_chdir_concurrent_forks() {
240 // Test chdir behavior with multiple concurrent child processes
241 let mut children = Vec::new();
242
243 for _ in 0..3 {
244 match fork().expect("Fork failed") {
245 Fork::Parent(child) => {
246 children.push(child);
247 }
248 Fork::Child => {
249 // Each child changes to root independently
250 chdir().expect("chdir failed");
251
252 let cwd = env::current_dir().expect("Failed to get current dir");
253 assert_eq!(cwd.to_str().unwrap(), "/");
254
255 // Small delay to ensure concurrency
256 thread::sleep(Duration::from_millis(10));
257
258 exit(0);
259 }
260 }
261 }
262
263 // Parent waits for all children
264 for child in children {
265 let status = waitpid(child).expect("waitpid failed");
266 assert!(WIFEXITED(status));
267 assert_eq!(WEXITSTATUS(status), 0);
268 }
269 }
270
271 #[test]
272 fn test_chdir_before_and_after_setsid() {
273 // Test that chdir works correctly with setsid (common in daemon creation)
274 match fork().expect("Fork failed") {
275 Fork::Parent(child) => {
276 let status = waitpid(child).expect("waitpid failed");
277 assert!(WIFEXITED(status));
278 assert_eq!(WEXITSTATUS(status), 0);
279 }
280 Fork::Child => {
281 use fork::setsid;
282
283 // Create new session first
284 setsid().expect("setsid failed");
285
286 // Then change directory (typical daemon pattern)
287 chdir().expect("chdir failed");
288
289 // Verify both worked
290 let cwd = env::current_dir().expect("Failed to get current dir");
291 assert_eq!(cwd.to_str().unwrap(), "/");
292
293 let pgid = fork::getpgrp();
294 assert!(pgid > 0);
295
296 exit(0);
297 }
298 }
299 }
300
301 #[test]
302 fn test_chdir_uses_c_string_literal() {
303 // This test verifies that the modern c"" string literal is used correctly
304 // by ensuring chdir works without any runtime string allocation errors
305 match fork().expect("Fork failed") {
306 Fork::Parent(child) => {
307 let status = waitpid(child).expect("waitpid failed");
308 assert!(WIFEXITED(status));
309 assert_eq!(WEXITSTATUS(status), 0);
310 }
311 Fork::Child => {
312 // Call chdir multiple times rapidly
313 // If there were allocation issues, this would likely fail
314 for _ in 0..100 {
315 chdir().expect("chdir failed");
316 }
317
318 let cwd = env::current_dir().expect("Failed to get current dir");
319 assert_eq!(cwd.to_str().unwrap(), "/");
320
321 exit(0);
322 }
323 }
324 }
325
326 #[test]
327 fn test_chdir_with_env_manipulation() {
328 // Test that chdir works correctly even when environment is modified
329 match fork().expect("Fork failed") {
330 Fork::Parent(child) => {
331 let status = waitpid(child).expect("waitpid failed");
332 assert!(WIFEXITED(status));
333 assert_eq!(WEXITSTATUS(status), 0);
334 }
335 Fork::Child => {
336 // Modify environment
337 // SAFETY: This child has no other threads and exits immediately after the test.
338 unsafe {
339 env::set_var("PWD", "/some/fake/path");
340 }
341
342 // chdir should still work correctly
343 chdir().expect("chdir failed");
344
345 // Verify actual directory (not PWD env var)
346 let cwd = env::current_dir().expect("Failed to get current dir");
347 assert_eq!(cwd.to_str().unwrap(), "/");
348
349 exit(0);
350 }
351 }
352 }