| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 67 |
let _unit: () = result.unwrap(); |
| 68 |
exit(0); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
#[test] |
| 74 |
fn test_chdir_changes_actual_working_directory() { |
| 75 |
|
| 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 |
|
| 84 |
let original = env::current_dir().expect("Failed to get original dir"); |
| 85 |
|
| 86 |
|
| 87 |
chdir().expect("chdir failed"); |
| 88 |
|
| 89 |
|
| 90 |
let new_dir = env::current_dir().expect("Failed to get new dir"); |
| 91 |
|
| 92 |
|
| 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 |
|
| 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 |
|
| 117 |
chdir().expect("First chdir failed"); |
| 118 |
chdir().expect("Second chdir failed"); |
| 119 |
chdir().expect("Third chdir failed"); |
| 120 |
|
| 121 |
|
| 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 |
|
| 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 |
|
| 138 |
let status = waitpid(child).expect("waitpid failed"); |
| 139 |
assert!(WIFEXITED(status)); |
| 140 |
|
| 141 |
|
| 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 |
|
| 147 |
chdir().expect("chdir failed"); |
| 148 |
|
| 149 |
|
| 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 |
|
| 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 |
|
| 169 |
chdir().expect("chdir failed"); |
| 170 |
|
| 171 |
|
| 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 |
|
| 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 |
|
| 195 |
chdir().expect("chdir failed"); |
| 196 |
|
| 197 |
|
| 198 |
let content = |
| 199 |
fs::read_to_string(&temp_file).expect("Failed to read with absolute path"); |
| 200 |
assert_eq!(content, "test"); |
| 201 |
|
| 202 |
|
| 203 |
fs::remove_file(&temp_file).ok(); |
| 204 |
|
| 205 |
exit(0); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
#[test] |
| 211 |
fn test_chdir_error_type() { |
| 212 |
|
| 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 |
|
| 223 |
assert!(result.is_ok()); |
| 224 |
|
| 225 |
|
| 226 |
match result { |
| 227 |
Ok(()) => exit(0), |
| 228 |
Err(e) => { |
| 229 |
|
| 230 |
let _: std::io::Error = e; |
| 231 |
exit(1); |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
#[test] |
| 239 |
fn test_chdir_concurrent_forks() { |
| 240 |
|
| 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 |
|
| 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 |
|
| 256 |
thread::sleep(Duration::from_millis(10)); |
| 257 |
|
| 258 |
exit(0); |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 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 |
|
| 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 |
|
| 284 |
setsid().expect("setsid failed"); |
| 285 |
|
| 286 |
|
| 287 |
chdir().expect("chdir failed"); |
| 288 |
|
| 289 |
|
| 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 |
|
| 304 |
|
| 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 |
|
| 313 |
|
| 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 |
|
| 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 |
|
| 337 |
|
| 338 |
unsafe { |
| 339 |
env::set_var("PWD", "/some/fake/path"); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
chdir().expect("chdir failed"); |
| 344 |
|
| 345 |
|
| 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 |
} |