| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
// spell-checker:ignore bindgen testtest casetest CASETEST |
| 7 |
|
| 8 |
#![allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)] |
| 9 |
|
| 10 |
#[cfg(not(windows))] |
| 11 |
use libc::mode_t; |
| 12 |
#[cfg(not(windows))] |
| 13 |
use std::os::unix::fs::PermissionsExt; |
| 14 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 15 |
use uucore::selinux::get_getfattr_output; |
| 16 |
#[cfg(not(windows))] |
| 17 |
use uutests::at_and_ucmd; |
| 18 |
use uutests::new_ucmd; |
| 19 |
use uutests::util::TestScenario; |
| 20 |
use uutests::util_name; |
| 21 |
|
| 22 |
#[test] |
| 23 |
fn test_invalid_arg() { |
| 24 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 25 |
} |
| 26 |
|
| 27 |
#[test] |
| 28 |
fn test_version_no_path() { |
| 29 |
use std::process::Command; |
| 30 |
use uutests::get_tests_binary; |
| 31 |
|
| 32 |
// This test verifies that when an individual utility binary is invoked with its full path, |
| 33 |
// the version output shows just "mkdir", not the full path like "/path/to/mkdir". |
| 34 |
// |
| 35 |
// Note: The multicall binary (coreutils) doesn't have this issue because it reads |
| 36 |
// the utility name from ARGV[1], not ARGV[0]. This bug only affects individual binaries. |
| 37 |
|
| 38 |
let tests_binary = get_tests_binary!(); |
| 39 |
let mkdir_binary_path = std::path::Path::new(tests_binary) |
| 40 |
.parent() |
| 41 |
.unwrap() |
| 42 |
.join("mkdir"); |
| 43 |
|
| 44 |
// If the individual mkdir binary exists, test it |
| 45 |
let output = if mkdir_binary_path.exists() { |
| 46 |
// Invoke the individual mkdir binary with its full path |
| 47 |
Command::new(&mkdir_binary_path) |
| 48 |
.arg("--version") |
| 49 |
.output() |
| 50 |
.expect("Failed to execute mkdir binary") |
| 51 |
} else { |
| 52 |
// If only multicall binary exists, test that (it should already pass) |
| 53 |
Command::new(tests_binary) |
| 54 |
.args(["mkdir", "--version"]) |
| 55 |
.output() |
| 56 |
.expect("Failed to execute mkdir via multicall binary") |
| 57 |
}; |
| 58 |
|
| 59 |
let stdout = String::from_utf8_lossy(&output.stdout); |
| 60 |
assert!(stdout.starts_with("mkdir (uutils coreutils)")); |
| 61 |
} |
| 62 |
|
| 63 |
#[test] |
| 64 |
fn test_no_arg() { |
| 65 |
new_ucmd!() |
| 66 |
.fails_with_code(1) |
| 67 |
.stderr_contains("error: the following required arguments were not provided:"); |
| 68 |
} |
| 69 |
|
| 70 |
#[test] |
| 71 |
fn test_mkdir_mkdir() { |
| 72 |
new_ucmd!().arg("test_dir").succeeds(); |
| 73 |
} |
| 74 |
|
| 75 |
#[cfg(feature = "test_risky_names")] |
| 76 |
#[test] |
| 77 |
fn test_mkdir_non_unicode() { |
| 78 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 79 |
|
| 80 |
let target = uucore::os_str_from_bytes(b"some-\xc0-dir-\xf3") |
| 81 |
.expect("Only unix platforms can test non-unicode names"); |
| 82 |
ucmd.arg(target).succeeds(); |
| 83 |
|
| 84 |
assert!(at.dir_exists(target)); |
| 85 |
} |
| 86 |
|
| 87 |
#[test] |
| 88 |
fn test_mkdir_verbose() { |
| 89 |
let expected = "mkdir: created directory 'test_dir'\n"; |
| 90 |
new_ucmd!() |
| 91 |
.arg("test_dir") |
| 92 |
.arg("-v") |
| 93 |
.succeeds() |
| 94 |
.stdout_is(expected); |
| 95 |
} |
| 96 |
|
| 97 |
#[test] |
| 98 |
fn test_mkdir_dup_dir() { |
| 99 |
let scene = TestScenario::new(util_name!()); |
| 100 |
let test_dir = "test_dir"; |
| 101 |
|
| 102 |
scene.ucmd().arg(test_dir).succeeds(); |
| 103 |
scene.ucmd().arg(test_dir).fails(); |
| 104 |
} |
| 105 |
|
| 106 |
#[test] |
| 107 |
fn test_mkdir_mode() { |
| 108 |
new_ucmd!().arg("-m").arg("755").arg("test_dir").succeeds(); |
| 109 |
} |
| 110 |
|
| 111 |
#[test] |
| 112 |
fn test_mkdir_parent() { |
| 113 |
let scene = TestScenario::new(util_name!()); |
| 114 |
let test_dir = "parent_dir/child_dir"; |
| 115 |
|
| 116 |
scene.ucmd().arg("-p").arg(test_dir).succeeds(); |
| 117 |
scene.ucmd().arg("-p").arg("-p").arg(test_dir).succeeds(); |
| 118 |
scene.ucmd().arg("--parent").arg(test_dir).succeeds(); |
| 119 |
scene |
| 120 |
.ucmd() |
| 121 |
.arg("--parent") |
| 122 |
.arg("--parent") |
| 123 |
.arg(test_dir) |
| 124 |
.succeeds(); |
| 125 |
scene.ucmd().arg("--parents").arg(test_dir).succeeds(); |
| 126 |
scene |
| 127 |
.ucmd() |
| 128 |
.arg("--parents") |
| 129 |
.arg("--parents") |
| 130 |
.arg(test_dir) |
| 131 |
.succeeds(); |
| 132 |
} |
| 133 |
|
| 134 |
#[test] |
| 135 |
fn test_mkdir_no_parent() { |
| 136 |
new_ucmd!().arg("parent_dir/child_dir").fails(); |
| 137 |
} |
| 138 |
|
| 139 |
#[test] |
| 140 |
fn test_mkdir_dup_dir_parent() { |
| 141 |
let scene = TestScenario::new(util_name!()); |
| 142 |
let test_dir = "test_dir"; |
| 143 |
|
| 144 |
scene.ucmd().arg(test_dir).succeeds(); |
| 145 |
scene.ucmd().arg("-p").arg(test_dir).succeeds(); |
| 146 |
} |
| 147 |
|
| 148 |
#[cfg(not(windows))] |
| 149 |
#[test] |
| 150 |
fn test_mkdir_parent_mode() { |
| 151 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 152 |
|
| 153 |
let default_umask: mode_t = 0o160; |
| 154 |
|
| 155 |
ucmd.arg("-p") |
| 156 |
.arg("a/b") |
| 157 |
.umask(default_umask) |
| 158 |
.succeeds() |
| 159 |
.no_output(); |
| 160 |
|
| 161 |
assert!(at.dir_exists("a")); |
| 162 |
// parents created by -p have permissions set to "=rwx,u+wx" |
| 163 |
assert_eq!( |
| 164 |
at.metadata("a").permissions().mode() as mode_t, |
| 165 |
((!default_umask & 0o777) | 0o300) + 0o40000 |
| 166 |
); |
| 167 |
assert!(at.dir_exists("a/b")); |
| 168 |
// sub directory's permission is determined only by the umask |
| 169 |
assert_eq!( |
| 170 |
at.metadata("a/b").permissions().mode() as mode_t, |
| 171 |
(!default_umask & 0o777) + 0o40000 |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
#[cfg(not(windows))] |
| 176 |
#[test] |
| 177 |
fn test_mkdir_parent_mode_check_existing_parent() { |
| 178 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 179 |
|
| 180 |
at.mkdir("a"); |
| 181 |
let parent_a_perms = at.metadata("a").permissions().mode(); |
| 182 |
|
| 183 |
let default_umask: mode_t = 0o160; |
| 184 |
|
| 185 |
ucmd.arg("-p") |
| 186 |
.arg("a/b/c") |
| 187 |
.umask(default_umask) |
| 188 |
.succeeds() |
| 189 |
.no_output(); |
| 190 |
|
| 191 |
assert!(at.dir_exists("a")); |
| 192 |
// parent dirs that already exist do not get their permissions modified |
| 193 |
assert_eq!(at.metadata("a").permissions().mode(), parent_a_perms); |
| 194 |
assert!(at.dir_exists("a/b")); |
| 195 |
assert_eq!( |
| 196 |
at.metadata("a/b").permissions().mode() as mode_t, |
| 197 |
((!default_umask & 0o777) | 0o300) + 0o40000 |
| 198 |
); |
| 199 |
assert!(at.dir_exists("a/b/c")); |
| 200 |
assert_eq!( |
| 201 |
at.metadata("a/b/c").permissions().mode() as mode_t, |
| 202 |
(!default_umask & 0o777) + 0o40000 |
| 203 |
); |
| 204 |
} |
| 205 |
|
| 206 |
#[cfg(not(windows))] |
| 207 |
#[test] |
| 208 |
fn test_mkdir_parent_mode_skip_existing_last_component_chmod() { |
| 209 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 210 |
|
| 211 |
at.mkdir("a"); |
| 212 |
at.mkdir("a/b"); |
| 213 |
at.set_mode("a/b", 0); |
| 214 |
|
| 215 |
let default_umask: mode_t = 0o160; |
| 216 |
|
| 217 |
ucmd.arg("-p") |
| 218 |
.arg("a/b") |
| 219 |
.umask(default_umask) |
| 220 |
.succeeds() |
| 221 |
.no_output(); |
| 222 |
|
| 223 |
assert_eq!(at.metadata("a/b").permissions().mode() as mode_t, 0o40000); |
| 224 |
} |
| 225 |
|
| 226 |
#[test] |
| 227 |
fn test_mkdir_dup_file() { |
| 228 |
let scene = TestScenario::new(util_name!()); |
| 229 |
let test_file = "test_file.txt"; |
| 230 |
|
| 231 |
scene.fixtures.touch(test_file); |
| 232 |
|
| 233 |
scene.ucmd().arg(test_file).fails(); |
| 234 |
|
| 235 |
// mkdir should fail for a file even if -p is specified. |
| 236 |
scene.ucmd().arg("-p").arg(test_file).fails(); |
| 237 |
} |
| 238 |
|
| 239 |
#[test] |
| 240 |
#[cfg(not(windows))] |
| 241 |
fn test_symbolic_mode() { |
| 242 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 243 |
let test_dir = "test_dir"; |
| 244 |
|
| 245 |
ucmd.arg("-m").arg("a=rwx").arg(test_dir).succeeds(); |
| 246 |
let perms = at.metadata(test_dir).permissions().mode(); |
| 247 |
assert_eq!(perms, 0o40777); |
| 248 |
} |
| 249 |
|
| 250 |
#[test] |
| 251 |
#[cfg(not(windows))] |
| 252 |
fn test_symbolic_alteration() { |
| 253 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 254 |
let test_dir = "test_dir"; |
| 255 |
|
| 256 |
let default_umask = 0o022; |
| 257 |
|
| 258 |
ucmd.arg("-m") |
| 259 |
.arg("-w") |
| 260 |
.arg(test_dir) |
| 261 |
.umask(default_umask) |
| 262 |
.succeeds(); |
| 263 |
let perms = at.metadata(test_dir).permissions().mode(); |
| 264 |
assert_eq!(perms, 0o40577); |
| 265 |
} |
| 266 |
|
| 267 |
#[test] |
| 268 |
#[cfg(not(windows))] |
| 269 |
fn test_multi_symbolic() { |
| 270 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 271 |
let test_dir = "test_dir"; |
| 272 |
|
| 273 |
ucmd.arg("-m").arg("u=rwx,g=rx,o=").arg(test_dir).succeeds(); |
| 274 |
let perms = at.metadata(test_dir).permissions().mode(); |
| 275 |
assert_eq!(perms, 0o40750); |
| 276 |
} |
| 277 |
|
| 278 |
#[test] |
| 279 |
fn test_recursive_reporting() { |
| 280 |
let test_dir = "test_dir/test_dir_a/test_dir_b"; |
| 281 |
|
| 282 |
new_ucmd!() |
| 283 |
.arg("-p") |
| 284 |
.arg("-v") |
| 285 |
.arg(test_dir) |
| 286 |
.succeeds() |
| 287 |
.stdout_contains("created directory 'test_dir'") |
| 288 |
.stdout_contains("created directory 'test_dir/test_dir_a'") |
| 289 |
.stdout_contains("created directory 'test_dir/test_dir_a/test_dir_b'"); |
| 290 |
new_ucmd!().arg("-v").arg(test_dir).fails().no_stdout(); |
| 291 |
|
| 292 |
let test_dir = "test_dir/../test_dir_a/../test_dir_b"; |
| 293 |
|
| 294 |
new_ucmd!() |
| 295 |
.arg("-p") |
| 296 |
.arg("-v") |
| 297 |
.arg(test_dir) |
| 298 |
.succeeds() |
| 299 |
.stdout_contains("created directory 'test_dir'") |
| 300 |
.stdout_contains("created directory 'test_dir/../test_dir_a'") |
| 301 |
.stdout_contains("created directory 'test_dir/../test_dir_a/../test_dir_b'"); |
| 302 |
} |
| 303 |
|
| 304 |
#[test] |
| 305 |
// Windows don't have acl entries |
| 306 |
// TODO Enable and modify this for macos when xattr processing for macos is added. |
| 307 |
// TODO Enable and modify this for freebsd when xattr processing for freebsd is enabled. |
| 308 |
#[cfg(target_os = "linux")] |
| 309 |
fn test_mkdir_acl() { |
| 310 |
use rustc_hash::FxHashMap; |
| 311 |
use std::ffi::OsString; |
| 312 |
|
| 313 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 314 |
|
| 315 |
at.mkdir("a"); |
| 316 |
|
| 317 |
let mut map: FxHashMap<OsString, Vec<u8>> = FxHashMap::default(); |
| 318 |
// posix_acl entries are in the form of |
| 319 |
// struct posix_acl_entry{ |
| 320 |
// tag: u16, |
| 321 |
// perm: u16, |
| 322 |
// id: u32, |
| 323 |
// } |
| 324 |
// the fields are serialized in little endian. |
| 325 |
// The entries are preceded by a header of value of 0x0002 |
| 326 |
// Reference: `<https://github.com/torvalds/linux/blob/master/include/uapi/linux/posix_acl_xattr.h>` |
| 327 |
// The id is undefined i.e. -1 which in u32 is 0xFFFFFFFF and tag and perm bits as given in the |
| 328 |
// header file. |
| 329 |
// Reference: `<https://github.com/torvalds/linux/blob/master/include/uapi/linux/posix_acl.h>` |
| 330 |
// |
| 331 |
// |
| 332 |
// There is a bindgen bug which generates the ACL_OTHER constant whose value is 0x20 into 32. |
| 333 |
// which when the bug is fixed will need to be changed back to 20 from 32 in the vec 'xattr_val'. |
| 334 |
// |
| 335 |
// Reference `<https://github.com/rust-lang/rust-bindgen/issues/2926>` |
| 336 |
// |
| 337 |
// The xattr_val vector is the header 0x0002 followed by tag and permissions for user_obj , tag |
| 338 |
// and permissions and for group_obj and finally the tag and permissions for ACL_OTHER. Each |
| 339 |
// entry has undefined id as mentioned above. |
| 340 |
// |
| 341 |
// |
| 342 |
|
| 343 |
let xattr_val: Vec<u8> = vec![ |
| 344 |
2, 0, 0, 0, 1, 0, 7, 0, 255, 255, 255, 255, 4, 0, 7, 0, 255, 255, 255, 255, 32, 0, 5, 0, |
| 345 |
255, 255, 255, 255, |
| 346 |
]; |
| 347 |
|
| 348 |
map.insert(OsString::from("system.posix_acl_default"), xattr_val); |
| 349 |
|
| 350 |
uucore::fsxattr::apply_xattrs(at.plus("a"), map).unwrap(); |
| 351 |
|
| 352 |
ucmd.arg("-p").arg("a/b").umask(0x077).succeeds(); |
| 353 |
|
| 354 |
let perms = at.metadata("a/b").permissions().mode(); |
| 355 |
|
| 356 |
// 0x770 would be user:rwx,group:rwx permissions |
| 357 |
assert_eq!(perms, 16893); |
| 358 |
} |
| 359 |
|
| 360 |
#[test] |
| 361 |
#[cfg(target_os = "linux")] |
| 362 |
fn test_mkdir_acl_inheritance_with_restrictive_mask() { |
| 363 |
use rustc_hash::FxHashMap; |
| 364 |
use std::ffi::OsString; |
| 365 |
|
| 366 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 367 |
|
| 368 |
at.mkdir("parent"); |
| 369 |
|
| 370 |
let mut map: FxHashMap<OsString, Vec<u8>> = FxHashMap::default(); |
| 371 |
|
| 372 |
// Default ACL with mask::r-x (0o5) — more restrictive than a umask of 0o022 would allow. |
| 373 |
// With umask 0o022, group bits would be r-x already, but the mask enforces this |
| 374 |
// regardless of what umask would permit. With umask 0o000, without ACL mask the |
| 375 |
// child would get rwx for group, but mask caps it to r-x. |
| 376 |
// |
| 377 |
// Encoding: header(0x0002) + entries: |
| 378 |
// ACL_USER_OBJ (0x0001) perm=7 (rwx) |
| 379 |
// ACL_GROUP_OBJ (0x0004) perm=7 (rwx) — would be rwx without mask |
| 380 |
// ACL_MASK (0x0010) perm=5 (r-x) — restricts group effective to r-x |
| 381 |
// ACL_OTHER (0x0020) perm=0 (---) |
| 382 |
let xattr_val: Vec<u8> = vec![ |
| 383 |
2, 0, 0, 0, // header |
| 384 |
1, 0, 7, 0, 255, 255, 255, 255, // ACL_USER_OBJ rwx |
| 385 |
4, 0, 7, 0, 255, 255, 255, 255, // ACL_GROUP_OBJ rwx (masked to r-x) |
| 386 |
16, 0, 5, 0, 255, 255, 255, 255, // ACL_MASK r-x |
| 387 |
32, 0, 0, 0, 255, 255, 255, 255, // ACL_OTHER --- |
| 388 |
]; |
| 389 |
|
| 390 |
map.insert(OsString::from("system.posix_acl_default"), xattr_val); |
| 391 |
uucore::fsxattr::apply_xattrs(at.plus("parent"), map).unwrap(); |
| 392 |
|
| 393 |
// umask 0o000 — without correct ACL inheritance, group would get rwx (7) |
| 394 |
// With correct inheritance the mask restricts group to r-x (5) |
| 395 |
ucmd.arg("-p").arg("parent/child").umask(0o000).succeeds(); |
| 396 |
|
| 397 |
let perms = at.metadata("parent/child").permissions().mode(); |
| 398 |
// Expected: user=rwx(7), group=r-x(5 from mask), other=---(0) |
| 399 |
// mode bits: 0o750 = 0o40750 with directory bit |
| 400 |
assert_eq!( |
| 401 |
perms & 0o777, |
| 402 |
0o750, |
| 403 |
"Expected group bits capped to r-x by ACL mask, got {:o}", |
| 404 |
perms & 0o777 |
| 405 |
); |
| 406 |
|
| 407 |
// Verify the child itself has an ACL (indicated by presence of xattr) |
| 408 |
assert!( |
| 409 |
uucore::fsxattr::has_acl(at.plus("parent/child"), true), |
| 410 |
"Child directory should have inherited ACL entries" |
| 411 |
); |
| 412 |
} |
| 413 |
|
| 414 |
#[test] |
| 415 |
#[cfg(unix)] |
| 416 |
fn test_mkdir_p_respects_umask_without_acl() { |
| 417 |
use std::os::unix::fs::PermissionsExt; |
| 418 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 419 |
ucmd.arg("-p").arg("a/b/c").umask(0o022).succeeds(); |
| 420 |
let perms = at.metadata("a/b/c").permissions().mode(); |
| 421 |
assert_eq!(perms & 0o777, 0o755); |
| 422 |
} |
| 423 |
|
| 424 |
#[test] |
| 425 |
#[cfg(unix)] |
| 426 |
fn test_mkdir_explicit_mode_zero() { |
| 427 |
use std::os::unix::fs::PermissionsExt; |
| 428 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 429 |
ucmd.arg("-m").arg("0").arg("d").umask(0o022).succeeds(); |
| 430 |
let perms = at.metadata("d").permissions().mode(); |
| 431 |
assert_eq!(perms & 0o777, 0o000); |
| 432 |
} |
| 433 |
|
| 434 |
#[test] |
| 435 |
#[cfg(unix)] |
| 436 |
fn test_mkdir_explicit_mode_with_umask() { |
| 437 |
// -m must win over umask: requesting 0o777 with a restrictive umask must |
| 438 |
// still yield 0o777, since the umask is shaped to not block requested bits. |
| 439 |
use std::os::unix::fs::PermissionsExt; |
| 440 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 441 |
ucmd.arg("-m").arg("777").arg("d").umask(0o077).succeeds(); |
| 442 |
let perms = at.metadata("d").permissions().mode(); |
| 443 |
assert_eq!(perms & 0o777, 0o777); |
| 444 |
} |
| 445 |
|
| 446 |
#[test] |
| 447 |
fn test_mkdir_trailing_dot() { |
| 448 |
new_ucmd!().arg("-p").arg("-v").arg("test_dir").succeeds(); |
| 449 |
|
| 450 |
new_ucmd!() |
| 451 |
.arg("-p") |
| 452 |
.arg("-v") |
| 453 |
.arg("test_dir_a/.") |
| 454 |
.succeeds() |
| 455 |
.stdout_contains("created directory 'test_dir_a'"); |
| 456 |
|
| 457 |
new_ucmd!() |
| 458 |
.arg("-p") |
| 459 |
.arg("-v") |
| 460 |
.arg("test_dir_b/..") |
| 461 |
.succeeds() |
| 462 |
.stdout_contains("created directory 'test_dir_b'"); |
| 463 |
|
| 464 |
let scene = TestScenario::new("ls"); |
| 465 |
let result = scene.ucmd().arg("-al").run(); |
| 466 |
println!("ls dest {}", result.stdout_str()); |
| 467 |
} |
| 468 |
|
| 469 |
#[test] |
| 470 |
fn test_mkdir_trailing_dot_and_slash() { |
| 471 |
new_ucmd!().arg("-p").arg("-v").arg("test_dir").succeeds(); |
| 472 |
|
| 473 |
new_ucmd!() |
| 474 |
.arg("-p") |
| 475 |
.arg("-v") |
| 476 |
.arg("test_dir_a/./") |
| 477 |
.succeeds() |
| 478 |
.stdout_contains("created directory 'test_dir_a'"); |
| 479 |
|
| 480 |
let scene = TestScenario::new("ls"); |
| 481 |
let result = scene.ucmd().arg("-al").run(); |
| 482 |
println!("ls dest {}", result.stdout_str()); |
| 483 |
} |
| 484 |
|
| 485 |
#[test] |
| 486 |
fn test_mkdir_trailing_spaces_and_dots() { |
| 487 |
let scene = TestScenario::new(util_name!()); |
| 488 |
let at = &scene.fixtures; |
| 489 |
|
| 490 |
// Test trailing space |
| 491 |
scene.ucmd().arg("-p").arg("test ").succeeds(); |
| 492 |
assert!(at.dir_exists("test ")); |
| 493 |
|
| 494 |
// Test multiple trailing spaces |
| 495 |
scene.ucmd().arg("-p").arg("test ").succeeds(); |
| 496 |
assert!(at.dir_exists("test ")); |
| 497 |
|
| 498 |
// Test leading dot (hidden on Unix) |
| 499 |
scene.ucmd().arg("-p").arg(".hidden").succeeds(); |
| 500 |
assert!(at.dir_exists(".hidden")); |
| 501 |
|
| 502 |
// Test trailing dot (should work) |
| 503 |
scene.ucmd().arg("-p").arg("test.").succeeds(); |
| 504 |
assert!(at.dir_exists("test.")); |
| 505 |
|
| 506 |
// Test multiple leading dots |
| 507 |
scene.ucmd().arg("-p").arg("...test").succeeds(); |
| 508 |
assert!(at.dir_exists("...test")); |
| 509 |
} |
| 510 |
#[test] |
| 511 |
#[cfg(not(windows))] |
| 512 |
fn test_umask_compliance() { |
| 513 |
fn test_single_case(umask_set: mode_t) { |
| 514 |
let test_dir = "test_dir"; |
| 515 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 516 |
|
| 517 |
ucmd.arg(test_dir).umask(umask_set).succeeds(); |
| 518 |
let perms = at.metadata(test_dir).permissions().mode() as mode_t; |
| 519 |
|
| 520 |
assert_eq!(perms, (!umask_set & 0o0777) + 0o40000); // before compare, add the set GUID, UID bits |
| 521 |
} |
| 522 |
|
| 523 |
for i in 0o0..0o027 { |
| 524 |
// tests all permission combinations |
| 525 |
test_single_case(i as mode_t); |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
#[test] |
| 530 |
fn test_empty_argument() { |
| 531 |
new_ucmd!() |
| 532 |
.arg("") |
| 533 |
.fails() |
| 534 |
.stderr_only("mkdir: cannot create directory '': No such file or directory\n"); |
| 535 |
} |
| 536 |
|
| 537 |
#[test] |
| 538 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 539 |
fn test_selinux() { |
| 540 |
let scene = TestScenario::new(util_name!()); |
| 541 |
let at = &scene.fixtures; |
| 542 |
let dest = "test_dir_a"; |
| 543 |
let args = ["-Z", "--context=unconfined_u:object_r:user_tmp_t:s0"]; |
| 544 |
for arg in args { |
| 545 |
new_ucmd!() |
| 546 |
.arg(arg) |
| 547 |
.arg("-v") |
| 548 |
.arg(at.plus_as_string(dest)) |
| 549 |
.succeeds() |
| 550 |
.stdout_contains("created directory"); |
| 551 |
|
| 552 |
let context_value = get_getfattr_output(&at.plus_as_string(dest)); |
| 553 |
assert!( |
| 554 |
context_value.contains("unconfined_u"), |
| 555 |
"Expected 'unconfined_u' not found in getfattr output:\n{context_value}", |
| 556 |
); |
| 557 |
at.rmdir(dest); |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
#[test] |
| 562 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 563 |
fn test_selinux_invalid() { |
| 564 |
let scene = TestScenario::new(util_name!()); |
| 565 |
let at = &scene.fixtures; |
| 566 |
let dest = "test_dir_a"; |
| 567 |
new_ucmd!() |
| 568 |
.arg("--context=testtest") |
| 569 |
.arg(at.plus_as_string(dest)) |
| 570 |
.fails() |
| 571 |
.no_stdout() |
| 572 |
.stderr_contains("failed to set default file creation context to 'testtest':"); |
| 573 |
// invalid context, so, no directory |
| 574 |
assert!(!at.dir_exists(dest)); |
| 575 |
} |
| 576 |
|
| 577 |
#[test] |
| 578 |
fn test_mkdir_deep_nesting() { |
| 579 |
// Regression test for stack overflow with deeply nested directories. |
| 580 |
// The iterative implementation should handle arbitrary depth without stack overflow. |
| 581 |
let scene = TestScenario::new(util_name!()); |
| 582 |
let at = &scene.fixtures; |
| 583 |
|
| 584 |
// Create a path with 350 levels of nesting |
| 585 |
let depth = 350; |
| 586 |
let dir_name = "d"; |
| 587 |
let mut path = std::path::PathBuf::new(); |
| 588 |
for _ in 0..depth { |
| 589 |
path.push(dir_name); |
| 590 |
} |
| 591 |
|
| 592 |
scene.ucmd().arg("-p").arg(&path).succeeds(); |
| 593 |
|
| 594 |
assert!(at.dir_exists(&path)); |
| 595 |
} |
| 596 |
|
| 597 |
#[test] |
| 598 |
fn test_mkdir_dot_components() { |
| 599 |
// Test handling of "." (current directory) components |
| 600 |
let scene = TestScenario::new(util_name!()); |
| 601 |
let at = &scene.fixtures; |
| 602 |
|
| 603 |
// Test case from review comment: test/././test2/././test3/././test4 |
| 604 |
// GNU mkdir normalizes this path and creates test/test2/test3/test4 |
| 605 |
let path = "test/././test2/././test3/././test4"; |
| 606 |
scene.ucmd().arg("-p").arg(path).succeeds(); |
| 607 |
|
| 608 |
// Verify expected structure exists (GNU compatibility) |
| 609 |
assert!(at.dir_exists("test/test2/test3/test4")); |
| 610 |
|
| 611 |
// Test leading "." - should create test_dot/test_dot2 |
| 612 |
scene |
| 613 |
.ucmd() |
| 614 |
.arg("-p") |
| 615 |
.arg("./test_dot/test_dot2") |
| 616 |
.succeeds(); |
| 617 |
assert!(at.dir_exists("test_dot/test_dot2")); |
| 618 |
|
| 619 |
// Test mixed "." and normal components |
| 620 |
scene |
| 621 |
.ucmd() |
| 622 |
.arg("-p") |
| 623 |
.arg("mixed/./normal/./path") |
| 624 |
.succeeds(); |
| 625 |
assert!(at.dir_exists("mixed/normal/path")); |
| 626 |
|
| 627 |
// Test that the command works without creating redundant directories |
| 628 |
// The key test is that it doesn't fail or create incorrect structure |
| 629 |
// The actual filesystem behavior (whether literal "." dirs exist) |
| 630 |
// may vary, but the logical result should be correct |
| 631 |
} |
| 632 |
|
| 633 |
#[test] |
| 634 |
fn test_mkdir_parent_dir_components() { |
| 635 |
// Test handling of ".." (parent directory) components |
| 636 |
let scene = TestScenario::new(util_name!()); |
| 637 |
let at = &scene.fixtures; |
| 638 |
|
| 639 |
at.mkdir("base"); |
| 640 |
at.mkdir("base/child"); |
| 641 |
|
| 642 |
scene |
| 643 |
.ucmd() |
| 644 |
.arg("-p") |
| 645 |
.arg("base/child/../sibling") |
| 646 |
.succeeds(); |
| 647 |
assert!(at.dir_exists("base/sibling")); |
| 648 |
|
| 649 |
scene |
| 650 |
.ucmd() |
| 651 |
.arg("-p") |
| 652 |
.arg("base/child/../../other") |
| 653 |
.succeeds(); |
| 654 |
assert!(at.dir_exists("other")); |
| 655 |
|
| 656 |
scene |
| 657 |
.ucmd() |
| 658 |
.arg("-p") |
| 659 |
.arg("base/child/../sibling") |
| 660 |
.succeeds(); |
| 661 |
assert!(at.dir_exists("base/sibling")); |
| 662 |
} |
| 663 |
|
| 664 |
#[test] |
| 665 |
fn test_mkdir_mixed_special_components() { |
| 666 |
// Test complex paths with both "." and ".." components |
| 667 |
let scene = TestScenario::new(util_name!()); |
| 668 |
let at = &scene.fixtures; |
| 669 |
|
| 670 |
scene |
| 671 |
.ucmd() |
| 672 |
.arg("-p") |
| 673 |
.arg("./start/./middle/../end/./final") |
| 674 |
.succeeds(); |
| 675 |
assert!(at.dir_exists("start/end/final")); |
| 676 |
} |
| 677 |
|
| 678 |
#[test] |
| 679 |
fn test_mkdir_control_characters() { |
| 680 |
// Test handling of control characters in filenames |
| 681 |
let scene = TestScenario::new(util_name!()); |
| 682 |
let at = &scene.fixtures; |
| 683 |
|
| 684 |
// Test NUL byte (should fail on most systems) |
| 685 |
// Note: This test is skipped because NUL bytes cause panics in the test framework |
| 686 |
// let result = scene.ucmd().arg("test\x00name").run(); |
| 687 |
// assert!(!result.succeeded()); |
| 688 |
|
| 689 |
// Test newline character (may fail on Windows) |
| 690 |
let result = scene.ucmd().arg("-p").arg("test\nname").run(); |
| 691 |
if result.succeeded() { |
| 692 |
assert!(at.dir_exists("test\nname")); |
| 693 |
} |
| 694 |
|
| 695 |
// Test tab character (may fail on Windows) |
| 696 |
let result = scene.ucmd().arg("-p").arg("test\tname").run(); |
| 697 |
if result.succeeded() { |
| 698 |
assert!(at.dir_exists("test\tname")); |
| 699 |
} |
| 700 |
|
| 701 |
// Test space character in directory name (should work on all systems) |
| 702 |
scene.ucmd().arg("-p").arg("test name").succeeds(); |
| 703 |
assert!(at.dir_exists("test name")); |
| 704 |
|
| 705 |
// Test double quotes in path - platform-specific behavior expected |
| 706 |
// On Linux/Unix: Should succeed and create directory with quotes |
| 707 |
// On Windows: Should fail with "Invalid argument" error |
| 708 |
#[cfg(unix)] |
| 709 |
{ |
| 710 |
scene.ucmd().arg("-pv").arg("a/\"\"/b/c").succeeds(); |
| 711 |
assert!(at.dir_exists("a/\"\"/b/c")); |
| 712 |
} |
| 713 |
#[cfg(windows)] |
| 714 |
{ |
| 715 |
let result = scene.ucmd().arg("-pv").arg("a/\"\"/b/c").run(); |
| 716 |
// Should fail after creating 'a' directory |
| 717 |
assert!(at.dir_exists("a")); |
| 718 |
assert!(!at.dir_exists("a/\"\"")); |
| 719 |
assert!(!result.succeeded()); |
| 720 |
// Windows should reject directory names with quotes |
| 721 |
// We don't assert on specific error message as it varies by Windows version |
| 722 |
} |
| 723 |
|
| 724 |
// Test single quotes in path - should work on both Unix and Windows |
| 725 |
scene.ucmd().arg("-p").arg("a/''/b/c").succeeds(); |
| 726 |
assert!(at.dir_exists("a/''/b/c")); |
| 727 |
} |
| 728 |
|
| 729 |
#[test] |
| 730 |
fn test_mkdir_maximum_path_length() { |
| 731 |
let scene = TestScenario::new(util_name!()); |
| 732 |
let at = &scene.fixtures; |
| 733 |
|
| 734 |
// Test moderate length path (should work on all systems) |
| 735 |
let long_path = "a".repeat(50) + "/" + &"b".repeat(50) + "/" + &"c".repeat(50); |
| 736 |
scene.ucmd().arg("-p").arg(&long_path).succeeds(); |
| 737 |
assert!(at.dir_exists(&long_path)); |
| 738 |
|
| 739 |
// Test longer but reasonable path |
| 740 |
let longer_path = "x".repeat(100) + "/" + &"y".repeat(50) + "/" + &"z".repeat(30); |
| 741 |
scene.ucmd().arg("-p").arg(&longer_path).succeeds(); |
| 742 |
assert!(at.dir_exists(&longer_path)); |
| 743 |
|
| 744 |
// Test extremely long path (may fail on some systems) |
| 745 |
let very_long_path = "very_long_directory_name_".repeat(20) + "/final"; |
| 746 |
let result = scene.ucmd().arg("-p").arg(&very_long_path).run(); |
| 747 |
if result.succeeded() { |
| 748 |
assert!(at.dir_exists(&very_long_path)); |
| 749 |
} |
| 750 |
// If it fails, that's acceptable on some systems with path limits |
| 751 |
} |
| 752 |
|
| 753 |
#[test] |
| 754 |
fn test_mkdir_reserved_device_names() { |
| 755 |
let scene = TestScenario::new(util_name!()); |
| 756 |
let at = &scene.fixtures; |
| 757 |
|
| 758 |
// These should work on Unix but may fail on Windows |
| 759 |
let result = scene.ucmd().arg("-p").arg("CON").run(); |
| 760 |
if result.succeeded() { |
| 761 |
assert!(at.dir_exists("CON")); |
| 762 |
} |
| 763 |
|
| 764 |
let result = scene.ucmd().arg("-p").arg("PRN").run(); |
| 765 |
if result.succeeded() { |
| 766 |
assert!(at.dir_exists("PRN")); |
| 767 |
} |
| 768 |
|
| 769 |
let result = scene.ucmd().arg("-p").arg("AUX").run(); |
| 770 |
if result.succeeded() { |
| 771 |
assert!(at.dir_exists("AUX")); |
| 772 |
} |
| 773 |
|
| 774 |
// Test device names with extensions |
| 775 |
let result = scene.ucmd().arg("-p").arg("COM1").run(); |
| 776 |
if result.succeeded() { |
| 777 |
assert!(at.dir_exists("COM1")); |
| 778 |
} |
| 779 |
|
| 780 |
let result = scene.ucmd().arg("-p").arg("LPT1").run(); |
| 781 |
if result.succeeded() { |
| 782 |
assert!(at.dir_exists("LPT1")); |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
#[test] |
| 787 |
fn test_mkdir_case_sensitivity() { |
| 788 |
// Test case sensitivity behavior (varies by filesystem) |
| 789 |
let scene = TestScenario::new(util_name!()); |
| 790 |
let at = &scene.fixtures; |
| 791 |
|
| 792 |
// Create directory with lowercase name |
| 793 |
scene.ucmd().arg("-p").arg("CaseTest").succeeds(); |
| 794 |
assert!(at.dir_exists("CaseTest")); |
| 795 |
|
| 796 |
// Try to create directory with different case |
| 797 |
// On case-sensitive filesystems: creates separate directory |
| 798 |
// On case-insensitive filesystems: fails (directory already exists) |
| 799 |
let result = scene.ucmd().arg("-p").arg("casetest").run(); |
| 800 |
|
| 801 |
// The test passes regardless of filesystem behavior |
| 802 |
// We just verify the command doesn't crash |
| 803 |
if result.succeeded() { |
| 804 |
// Case-sensitive filesystem - both exist |
| 805 |
assert!(at.dir_exists("CaseTest")); |
| 806 |
assert!(at.dir_exists("casetest")); |
| 807 |
} else { |
| 808 |
// Case-insensitive filesystem - only one exists |
| 809 |
assert!(at.dir_exists("CaseTest")); |
| 810 |
} |
| 811 |
|
| 812 |
// Test mixed case variations |
| 813 |
scene.ucmd().arg("-p").arg("CASETEST").succeeds(); |
| 814 |
scene.ucmd().arg("-p").arg("caseTEST").succeeds(); |
| 815 |
} |
| 816 |
|
| 817 |
#[test] |
| 818 |
fn test_mkdir_network_paths() { |
| 819 |
// Test network path formats (UNC paths on Windows) |
| 820 |
let scene = TestScenario::new(util_name!()); |
| 821 |
let at = &scene.fixtures; |
| 822 |
|
| 823 |
// Test UNC-style path prefix (may fail on some systems) |
| 824 |
let result = scene.ucmd().arg("-p").arg("//server/share/test").run(); |
| 825 |
if result.succeeded() { |
| 826 |
assert!(at.dir_exists("//server/share/test")); |
| 827 |
} |
| 828 |
// If it fails, that's acceptable on some systems with read-only restrictions |
| 829 |
|
| 830 |
// Test path that looks like network but is actually local |
| 831 |
scene.ucmd().arg("-p").arg("server_share_test").succeeds(); |
| 832 |
assert!(at.dir_exists("server_share_test")); |
| 833 |
|
| 834 |
// Test path with double slash in middle (should work) |
| 835 |
scene.ucmd().arg("-p").arg("test//double//slash").succeeds(); |
| 836 |
assert!(at.dir_exists("test//double//slash")); |
| 837 |
} |
| 838 |
|
| 839 |
#[test] |
| 840 |
fn test_mkdir_environment_expansion() { |
| 841 |
// Test that mkdir doesn't expand environment variables (unlike some shells) |
| 842 |
let scene = TestScenario::new(util_name!()); |
| 843 |
let at = &scene.fixtures; |
| 844 |
|
| 845 |
unsafe { |
| 846 |
std::env::set_var("TEST_VAR", "expanded_value"); |
| 847 |
} |
| 848 |
|
| 849 |
// Create directory with literal $VAR (should not expand) |
| 850 |
scene.ucmd().arg("-p").arg("$TEST_VAR/dir").succeeds(); |
| 851 |
assert!(at.dir_exists("$TEST_VAR/dir")); |
| 852 |
|
| 853 |
// Verify the literal name exists, not the expanded value |
| 854 |
assert!(!at.dir_exists("expanded_value/dir")); |
| 855 |
|
| 856 |
// Test with braces |
| 857 |
scene |
| 858 |
.ucmd() |
| 859 |
.arg("-p") |
| 860 |
.arg("${TEST_VAR}_braced/dir") |
| 861 |
.succeeds(); |
| 862 |
assert!(at.dir_exists("${TEST_VAR}_braced/dir")); |
| 863 |
|
| 864 |
// Test with tilde (should not expand to home directory) |
| 865 |
scene.ucmd().arg("-p").arg("~/test_dir").succeeds(); |
| 866 |
assert!(at.dir_exists("~/test_dir")); |
| 867 |
|
| 868 |
unsafe { |
| 869 |
std::env::remove_var("TEST_VAR"); |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
/// Test that mkdir -m creates directories with the exact requested mode, |
| 874 |
/// bypassing umask. This verifies the fix for issue #10022. |
| 875 |
/// |
| 876 |
/// Previously, mkdir would create the directory with umask-based permissions |
| 877 |
/// and then chmod afterward, leaving a brief window with wrong permissions. |
| 878 |
/// Now it temporarily sets umask to 0 and creates with the exact mode. |
| 879 |
#[cfg(not(windows))] |
| 880 |
#[test] |
| 881 |
fn test_mkdir_mode_ignores_umask() { |
| 882 |
// Test that -m 0700 with restrictive umask still creates 0700 |
| 883 |
{ |
| 884 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 885 |
let restrictive_umask: mode_t = 0o077; // Would normally block group/other |
| 886 |
|
| 887 |
ucmd.arg("-m") |
| 888 |
.arg("0700") |
| 889 |
.arg("test_700") |
| 890 |
.umask(restrictive_umask) |
| 891 |
.succeeds(); |
| 892 |
|
| 893 |
let perms = at.metadata("test_700").permissions().mode() as mode_t; |
| 894 |
assert_eq!(perms, 0o40700, "Expected 0700, got {:o}", perms & 0o777); |
| 895 |
} |
| 896 |
|
| 897 |
// Test that -m 0777 is honored even with umask 022 |
| 898 |
// This is the key test: without the fix, 0777 & ~022 = 0755 |
| 899 |
{ |
| 900 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 901 |
let common_umask: mode_t = 0o022; |
| 902 |
|
| 903 |
ucmd.arg("-m") |
| 904 |
.arg("0777") |
| 905 |
.arg("test_777") |
| 906 |
.umask(common_umask) |
| 907 |
.succeeds(); |
| 908 |
|
| 909 |
let perms = at.metadata("test_777").permissions().mode() as mode_t; |
| 910 |
assert_eq!( |
| 911 |
perms, |
| 912 |
0o40777, |
| 913 |
"Expected 0777 (umask should be ignored with -m), got {:o}", |
| 914 |
perms & 0o777 |
| 915 |
); |
| 916 |
} |
| 917 |
|
| 918 |
// Test that -m 0755 with umask 077 still creates 0755 |
| 919 |
{ |
| 920 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 921 |
let very_restrictive_umask: mode_t = 0o077; |
| 922 |
|
| 923 |
ucmd.arg("-m") |
| 924 |
.arg("0755") |
| 925 |
.arg("test_755") |
| 926 |
.umask(very_restrictive_umask) |
| 927 |
.succeeds(); |
| 928 |
|
| 929 |
let perms = at.metadata("test_755").permissions().mode() as mode_t; |
| 930 |
assert_eq!(perms, 0o40755, "Expected 0755, got {:o}", perms & 0o777); |
| 931 |
} |
| 932 |
|
| 933 |
// Test symbolic mode also ignores umask |
| 934 |
{ |
| 935 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 936 |
let umask: mode_t = 0o022; |
| 937 |
|
| 938 |
ucmd.arg("-m") |
| 939 |
.arg("a=rwx") |
| 940 |
.arg("test_symbolic") |
| 941 |
.umask(umask) |
| 942 |
.succeeds(); |
| 943 |
|
| 944 |
let perms = at.metadata("test_symbolic").permissions().mode() as mode_t; |
| 945 |
assert_eq!(perms, 0o40777, "Expected 0777, got {:o}", perms & 0o777); |
| 946 |
} |
| 947 |
} |
| 948 |
|
| 949 |
/// Test that mkdir -p -m applies mode correctly: |
| 950 |
/// - Parent directories use umask-derived permissions (with u+wx) |
| 951 |
/// - Final directory uses the exact requested mode (ignoring umask) |
| 952 |
#[cfg(not(windows))] |
| 953 |
#[test] |
| 954 |
fn test_mkdir_parent_mode_with_explicit_mode() { |
| 955 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 956 |
let umask: mode_t = 0o022; |
| 957 |
|
| 958 |
ucmd.arg("-p") |
| 959 |
.arg("-m") |
| 960 |
.arg("0700") |
| 961 |
.arg("parent/child/target") |
| 962 |
.umask(umask) |
| 963 |
.succeeds(); |
| 964 |
|
| 965 |
// Parent directories created by -p use umask-derived mode with u+wx |
| 966 |
let parent_perms = at.metadata("parent").permissions().mode() as mode_t; |
| 967 |
let expected_parent = ((!umask & 0o777) | 0o300) + 0o40000; |
| 968 |
assert_eq!( |
| 969 |
parent_perms, |
| 970 |
expected_parent, |
| 971 |
"Parent should have umask-derived mode, got {:o}", |
| 972 |
parent_perms & 0o777 |
| 973 |
); |
| 974 |
|
| 975 |
let child_perms = at.metadata("parent/child").permissions().mode() as mode_t; |
| 976 |
assert_eq!( |
| 977 |
child_perms, |
| 978 |
expected_parent, |
| 979 |
"Intermediate dir should have umask-derived mode, got {:o}", |
| 980 |
child_perms & 0o777 |
| 981 |
); |
| 982 |
|
| 983 |
// Final directory should have exactly the requested mode |
| 984 |
let target_perms = at.metadata("parent/child/target").permissions().mode() as mode_t; |
| 985 |
assert_eq!( |
| 986 |
target_perms, |
| 987 |
0o40700, |
| 988 |
"Target should have exact requested mode 0700, got {:o}", |
| 989 |
target_perms & 0o777 |
| 990 |
); |
| 991 |
} |
| 992 |
|
| 993 |
/// Test that nested directories inherit the setgid bit with mkdir -p. |
| 994 |
#[test] |
| 995 |
#[cfg(target_os = "linux")] |
| 996 |
fn test_mkdir_parent_inherits_setgid() { |
| 997 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 998 |
|
| 999 |
at.mkdir("parent"); |
| 1000 |
at.set_mode("parent", 0o2755); |
| 1001 |
|
| 1002 |
ucmd.arg("-p") |
| 1003 |
.arg("parent/child/grandchild") |
| 1004 |
.succeeds() |
| 1005 |
.no_output(); |
| 1006 |
|
| 1007 |
// All descendants should inherit the setgid bit (0o2000) |
| 1008 |
assert_eq!(at.metadata("parent").permissions().mode() & 0o2000, 0o2000); |
| 1009 |
assert_eq!( |
| 1010 |
at.metadata("parent/child").permissions().mode() & 0o2000, |
| 1011 |
0o2000 |
| 1012 |
); |
| 1013 |
assert_eq!( |
| 1014 |
at.metadata("parent/child/grandchild").permissions().mode() & 0o2000, |
| 1015 |
0o2000 |
| 1016 |
); |
| 1017 |
} |
| 1018 |
|
| 1019 |
#[test] |
| 1020 |
fn test_mkdir_concurrent_creation() { |
| 1021 |
// Test concurrent mkdir -p operations: 10 iterations, 8 threads, 40 levels nesting |
| 1022 |
use std::thread; |
| 1023 |
|
| 1024 |
for _ in 0..10 { |
| 1025 |
let scene = TestScenario::new(util_name!()); |
| 1026 |
let at = &scene.fixtures; |
| 1027 |
|
| 1028 |
let mut dir = at.plus("concurrent_test"); |
| 1029 |
dir.push("a"); |
| 1030 |
|
| 1031 |
for _ in 0..40 { |
| 1032 |
dir.push("a"); |
| 1033 |
} |
| 1034 |
|
| 1035 |
let path_str = dir.to_string_lossy().to_string(); |
| 1036 |
let bin_path = scene.bin_path.clone(); |
| 1037 |
|
| 1038 |
let mut handles = vec![]; |
| 1039 |
|
| 1040 |
for _ in 0..8 { |
| 1041 |
let path_clone = path_str.clone(); |
| 1042 |
let bin_path_clone = bin_path.clone(); |
| 1043 |
|
| 1044 |
let handle = thread::spawn(move || { |
| 1045 |
// Use the actual uutils mkdir binary to test the real implementation |
| 1046 |
let result = std::process::Command::new(&bin_path_clone) |
| 1047 |
.arg("mkdir") |
| 1048 |
.arg("-p") |
| 1049 |
.arg(&path_clone) |
| 1050 |
.current_dir(std::env::current_dir().unwrap()) |
| 1051 |
.output(); |
| 1052 |
|
| 1053 |
match result { |
| 1054 |
Ok(output) => { |
| 1055 |
assert!( |
| 1056 |
output.status.success(), |
| 1057 |
"mkdir failed: {}", |
| 1058 |
String::from_utf8_lossy(&output.stderr) |
| 1059 |
); |
| 1060 |
} |
| 1061 |
Err(e) => panic!("Failed to execute mkdir: {e}"), |
| 1062 |
} |
| 1063 |
}); |
| 1064 |
handles.push(handle); |
| 1065 |
} |
| 1066 |
|
| 1067 |
handles |
| 1068 |
.drain(..) |
| 1069 |
.map(|handle| handle.join().unwrap()) |
| 1070 |
.count(); |
| 1071 |
|
| 1072 |
assert!(at.dir_exists(&path_str)); |
| 1073 |
} |
| 1074 |
} |
| 1075 |
|
| 1076 |
#[cfg(unix)] |
| 1077 |
#[test] |
| 1078 |
fn test_mkdir_inside_inexistent_dir() { |
| 1079 |
new_ucmd!() |
| 1080 |
.arg("a/b") |
| 1081 |
.fails_with_code(1) |
| 1082 |
.stderr_is("mkdir: cannot create directory 'a/b': No such file or directory\n"); |
| 1083 |
} |
| 1084 |
|
| 1085 |
// The mode is only parsed where a mode means something. |
| 1086 |
#[cfg(unix)] |
| 1087 |
#[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))] |
| 1088 |
mod diagnostics { |
| 1089 |
use super::*; |
| 1090 |
#[test] |
| 1091 |
fn test_snippet_points_at_the_bad_operator() { |
| 1092 |
let result = new_ucmd!() |
| 1093 |
.terminal_sim_stderr() |
| 1094 |
.args(&["-m", "u+rw?x", "some_dir"]) |
| 1095 |
.fails_with_code(1); |
| 1096 |
let stderr = result.stderr_str(); |
| 1097 |
|
| 1098 |
assert!(stderr.contains("invalid operator"), "{stderr}"); |
| 1099 |
// The caret lands on `?`: three columns of `-m ` and four of mode. |
| 1100 |
assert_eq!(result.caret_column(), Some(8), "{stderr}"); |
| 1101 |
} |
| 1102 |
|
| 1103 |
#[test] |
| 1104 |
fn test_snippet_points_into_the_second_clause() { |
| 1105 |
let result = new_ucmd!() |
| 1106 |
.terminal_sim_stderr() |
| 1107 |
.args(&["-m", "u=r,g!w", "some_dir"]) |
| 1108 |
.fails_with_code(1); |
| 1109 |
let stderr = result.stderr_str(); |
| 1110 |
|
| 1111 |
// Clauses are parsed one at a time, but the caret is placed in the |
| 1112 |
// whole mode: `!` is its sixth character, after `-m `. |
| 1113 |
assert_eq!(result.caret_column(), Some(9), "{stderr}"); |
| 1114 |
} |
| 1115 |
|
| 1116 |
#[test] |
| 1117 |
fn test_plain_message_when_stderr_is_a_pipe() { |
| 1118 |
// The test harness pipes stderr, so the report must not appear. |
| 1119 |
let result = new_ucmd!() |
| 1120 |
.args(&["-m", "u+rw?x", "some_dir"]) |
| 1121 |
.fails_with_code(1); |
| 1122 |
let stderr = result.stderr_str(); |
| 1123 |
|
| 1124 |
assert!(stderr.starts_with("mkdir: "), "{stderr}"); |
| 1125 |
assert!(!stderr.contains(":1:"), "{stderr}"); |
| 1126 |
} |
| 1127 |
} |
| 1128 |
|
| 1129 |
#[test] |
| 1130 |
fn test_mkdir_concurrent_non_recursive() { |
| 1131 |
// Test concurrent mkdir operations without -p: exactly one process must succeed per round |
| 1132 |
use std::sync::Arc; |
| 1133 |
use std::sync::atomic::{AtomicUsize, Ordering}; |
| 1134 |
use std::thread; |
| 1135 |
|
| 1136 |
for round in 0..10 { |
| 1137 |
let scene = TestScenario::new(util_name!()); |
| 1138 |
let target_dir = scene.fixtures.plus(format!("concurrent_target_{round}")); |
| 1139 |
let path_str = target_dir.to_string_lossy().to_string(); |
| 1140 |
let bin_path = scene.bin_path.clone(); |
| 1141 |
|
| 1142 |
let winners = Arc::new(AtomicUsize::new(0)); |
| 1143 |
let mut handles = vec![]; |
| 1144 |
|
| 1145 |
for _ in 0..16 { |
| 1146 |
let path_clone = path_str.clone(); |
| 1147 |
let bin_path_clone = bin_path.clone(); |
| 1148 |
let winners_clone = Arc::clone(&winners); |
| 1149 |
|
| 1150 |
let handle = thread::spawn(move || { |
| 1151 |
let result = std::process::Command::new(&bin_path_clone) |
| 1152 |
.arg("mkdir") |
| 1153 |
.arg(&path_clone) |
| 1154 |
.current_dir(std::env::current_dir().unwrap()) |
| 1155 |
.output() |
| 1156 |
.expect("failed to run binary"); |
| 1157 |
if result.status.success() { |
| 1158 |
winners_clone.fetch_add(1, Ordering::SeqCst); |
| 1159 |
} |
| 1160 |
}); |
| 1161 |
handles.push(handle); |
| 1162 |
} |
| 1163 |
|
| 1164 |
for h in handles { |
| 1165 |
h.join().unwrap(); |
| 1166 |
} |
| 1167 |
|
| 1168 |
assert_eq!( |
| 1169 |
winners.load(Ordering::SeqCst), |
| 1170 |
1, |
| 1171 |
"round {round}: expected exactly 1 winner for concurrent non-recursive mkdir" |
| 1172 |
); |
| 1173 |
assert!( |
| 1174 |
scene |
| 1175 |
.fixtures |
| 1176 |
.dir_exists(format!("concurrent_target_{round}")) |
| 1177 |
); |
| 1178 |
} |
| 1179 |
} |