| 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 crème brûlée |
| 7 |
|
| 8 |
use uutests::at_and_ucmd; |
| 9 |
use uutests::new_ucmd; |
| 10 |
use uutests::unwrap_or_return; |
| 11 |
use uutests::util::{TestScenario, expected_result}; |
| 12 |
use uutests::util_name; |
| 13 |
|
| 14 |
use std::fs::{File, FileTimes, metadata}; |
| 15 |
use std::os::unix::fs::MetadataExt; |
| 16 |
use std::time::{Duration, UNIX_EPOCH}; |
| 17 |
|
| 18 |
#[test] |
| 19 |
fn test_invalid_arg() { |
| 20 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 21 |
} |
| 22 |
|
| 23 |
#[test] |
| 24 |
fn test_invalid_option() { |
| 25 |
new_ucmd!().arg("-w").arg("-q").arg("/").fails(); |
| 26 |
} |
| 27 |
|
| 28 |
#[test] |
| 29 |
fn test_format_hyphen_leading_as_separate_arg() { |
| 30 |
// A hyphen-leading format string passed as its own argument (not |
| 31 |
// attached with `=`) must not be mistaken for a new, unrecognized |
| 32 |
// flag. |
| 33 |
new_ucmd!() |
| 34 |
.args(&["--format", "-%n", "/"]) |
| 35 |
.succeeds() |
| 36 |
.stdout_is("-/\n"); |
| 37 |
new_ucmd!() |
| 38 |
.args(&["--printf", "-%n", "/"]) |
| 39 |
.succeeds() |
| 40 |
.stdout_is("-/"); |
| 41 |
new_ucmd!() |
| 42 |
.args(&["-c", "-%n", "/"]) |
| 43 |
.succeeds() |
| 44 |
.stdout_is("-/\n"); |
| 45 |
} |
| 46 |
|
| 47 |
#[cfg(unix)] |
| 48 |
const NORMAL_FORMAT_STR: &str = |
| 49 |
"%a %A %b %B %d %D %f %F %g %G %h %i %m %n %o %s %u %U %x %X %y %Y %z %Z"; // avoid "%w %W" (birth/creation) due to `stat` limitations and linux kernel & rust version capability variations |
| 50 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 51 |
const DEV_FORMAT_STR: &str = |
| 52 |
"%a %A %b %B %d %D %f %F %g %G %h %i %m %n %o %s (%t/%T) %u %U %w %W %x %X %y %Y %z %Z"; |
| 53 |
#[cfg(target_os = "linux")] |
| 54 |
const FS_FORMAT_STR: &str = "%b %c %i %l %n %s %S %t %T"; // avoid "%a %d %f" which can cause test failure due to race conditions |
| 55 |
|
| 56 |
#[test] |
| 57 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 58 |
fn test_terse_fs_format() { |
| 59 |
let args = ["-f", "-t", "/proc"]; |
| 60 |
let ts = TestScenario::new(util_name!()); |
| 61 |
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); |
| 62 |
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout); |
| 63 |
} |
| 64 |
|
| 65 |
#[test] |
| 66 |
#[cfg(target_os = "linux")] |
| 67 |
fn test_fs_format() { |
| 68 |
let args = ["-f", "-c", FS_FORMAT_STR, "/dev/shm"]; |
| 69 |
let ts = TestScenario::new(util_name!()); |
| 70 |
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); |
| 71 |
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout); |
| 72 |
} |
| 73 |
|
| 74 |
#[test] |
| 75 |
// `stat -f` is only implemented for these targets; elsewhere `fs_type` is |
| 76 |
// still `unimplemented!()`. |
| 77 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 78 |
fn test_fs_default_format_block_size_label() { |
| 79 |
// GNU prints "Block size:", not "Block Size:". |
| 80 |
new_ucmd!() |
| 81 |
.args(&["-f", "/"]) |
| 82 |
.succeeds() |
| 83 |
.stdout_contains("Block size:"); |
| 84 |
} |
| 85 |
|
| 86 |
#[cfg(unix)] |
| 87 |
#[test] |
| 88 |
fn test_terse_normal_format() { |
| 89 |
// note: contains birth/creation date which increases test fragility |
| 90 |
// * results may vary due to built-in `stat` limitations as well as linux kernel and rust version capability variations |
| 91 |
let args = ["-t", "/"]; |
| 92 |
let ts = TestScenario::new(util_name!()); |
| 93 |
let actual = ts.ucmd().args(&args).succeeds().stdout_move_str(); |
| 94 |
let expect = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); |
| 95 |
println!("actual: {actual:?}"); |
| 96 |
println!("expect: {expect:?}"); |
| 97 |
let v_actual: Vec<&str> = actual.trim().split(' ').collect(); |
| 98 |
let mut v_expect: Vec<&str> = expect.trim().split(' ').collect(); |
| 99 |
assert!(!v_expect.is_empty()); |
| 100 |
|
| 101 |
// uu_stat does not support selinux |
| 102 |
if v_actual.len() == v_expect.len() - 1 && v_expect[v_expect.len() - 1].contains(':') { |
| 103 |
// assume last element contains: `SELinux security context string` |
| 104 |
v_expect.pop(); |
| 105 |
} |
| 106 |
|
| 107 |
// * allow for inequality if `stat` (aka, expect) returns "0" (unknown value) |
| 108 |
assert!( |
| 109 |
expect == "0" |
| 110 |
|| expect == "0\n" |
| 111 |
|| v_actual |
| 112 |
.iter() |
| 113 |
.zip(v_expect.iter()) |
| 114 |
.all(|(a, e)| a == e || *e == "0" || *e == "0\n") |
| 115 |
); |
| 116 |
} |
| 117 |
|
| 118 |
#[cfg(unix)] |
| 119 |
#[test] |
| 120 |
fn test_format_created_time() { |
| 121 |
let args = ["-c", "%w", "/bin"]; |
| 122 |
let ts = TestScenario::new(util_name!()); |
| 123 |
let actual = ts.ucmd().args(&args).succeeds().stdout_move_str(); |
| 124 |
let expect = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); |
| 125 |
println!("actual: {actual:?}"); |
| 126 |
println!("expect: {expect:?}"); |
| 127 |
// note: using a regex instead of `split_whitespace()` in order to detect whitespace differences |
| 128 |
let re = regex::Regex::new(r"\s").unwrap(); |
| 129 |
let v_actual: Vec<&str> = re.split(&actual).collect(); |
| 130 |
let v_expect: Vec<&str> = re.split(&expect).collect(); |
| 131 |
assert!(!v_expect.is_empty()); |
| 132 |
// * allow for inequality if `stat` (aka, expect) returns "-" (unknown value) |
| 133 |
assert!( |
| 134 |
expect == "-" |
| 135 |
|| expect == "-\n" |
| 136 |
|| v_actual |
| 137 |
.iter() |
| 138 |
.zip(v_expect.iter()) |
| 139 |
.all(|(a, e)| a == e || *e == "-" || *e == "-\n") |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
#[cfg(unix)] |
| 144 |
#[test] |
| 145 |
fn test_format_created_seconds() { |
| 146 |
let args = ["-c", "%W", "/bin"]; |
| 147 |
let ts = TestScenario::new(util_name!()); |
| 148 |
let actual = ts.ucmd().args(&args).succeeds().stdout_move_str(); |
| 149 |
let expect = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); |
| 150 |
println!("actual: {actual:?}"); |
| 151 |
println!("expect: {expect:?}"); |
| 152 |
// note: using a regex instead of `split_whitespace()` in order to detect whitespace differences |
| 153 |
let re = regex::Regex::new(r"\s").unwrap(); |
| 154 |
let v_actual: Vec<&str> = re.split(&actual).collect(); |
| 155 |
let v_expect: Vec<&str> = re.split(&expect).collect(); |
| 156 |
assert!(!v_expect.is_empty()); |
| 157 |
// * allow for inequality if `stat` (aka, expect) returns "0" (unknown value) |
| 158 |
assert!( |
| 159 |
expect == "0" |
| 160 |
|| expect == "0\n" |
| 161 |
|| v_actual |
| 162 |
.iter() |
| 163 |
.zip(v_expect.iter()) |
| 164 |
.all(|(a, e)| a == e || *e == "0" || *e == "0\n") |
| 165 |
); |
| 166 |
} |
| 167 |
|
| 168 |
#[cfg(unix)] |
| 169 |
#[test] |
| 170 |
fn test_normal_format() { |
| 171 |
let args = ["-c", NORMAL_FORMAT_STR, "/bin"]; |
| 172 |
let ts = TestScenario::new(util_name!()); |
| 173 |
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); |
| 174 |
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout); |
| 175 |
} |
| 176 |
|
| 177 |
#[cfg(unix)] |
| 178 |
#[cfg(not(target_os = "openbsd"))] |
| 179 |
#[test] |
| 180 |
fn test_symlinks() { |
| 181 |
let ts = TestScenario::new(util_name!()); |
| 182 |
let at = &ts.fixtures; |
| 183 |
|
| 184 |
let mut tested: bool = false; |
| 185 |
// arbitrarily chosen symlinks with hope that the CI environment provides at least one of them |
| 186 |
for file in [ |
| 187 |
"/bin/sh", |
| 188 |
"/data/data/com.termux/files/usr/bin/sh", // spell-checker:disable-line |
| 189 |
"/bin/sudoedit", |
| 190 |
"/usr/bin/ex", |
| 191 |
"/etc/localtime", |
| 192 |
"/etc/aliases", |
| 193 |
] { |
| 194 |
if at.file_exists(file) && at.is_symlink(file) { |
| 195 |
tested = true; |
| 196 |
let args = ["-c", NORMAL_FORMAT_STR, file]; |
| 197 |
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); |
| 198 |
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout); |
| 199 |
// -L, --dereference follow links |
| 200 |
let args = ["-L", "-c", NORMAL_FORMAT_STR, file]; |
| 201 |
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); |
| 202 |
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout); |
| 203 |
} |
| 204 |
} |
| 205 |
assert!(tested, "No symlink found to test in this environment"); |
| 206 |
} |
| 207 |
|
| 208 |
#[cfg(any(target_vendor = "apple", target_os = "linux", target_os = "android"))] |
| 209 |
#[test] |
| 210 |
fn test_char() { |
| 211 |
// TODO: "(%t) (%x) (%w)" deviate from GNU stat for `character special file` on macOS |
| 212 |
// Diff < left / right > : |
| 213 |
// <"(f0000) (2021-05-20 23:08:03.442555000 +0200) (1970-01-01 01:00:00.000000000 +0100)\n" |
| 214 |
// >"(f) (2021-05-20 23:08:03.455598000 +0200) (-)\n" |
| 215 |
let args = [ |
| 216 |
"-c", |
| 217 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 218 |
DEV_FORMAT_STR, |
| 219 |
#[cfg(target_os = "linux")] |
| 220 |
"/dev/pts/ptmx", |
| 221 |
#[cfg(target_vendor = "apple")] |
| 222 |
"%a %A %b %B %d %D %f %F %g %G %h %i %m %n %o %s (/%T) %u %U %W %X %y %Y %z %Z", |
| 223 |
#[cfg(any(target_vendor = "apple", target_os = "android"))] |
| 224 |
"/dev/ptmx", |
| 225 |
]; |
| 226 |
let ts = TestScenario::new(util_name!()); |
| 227 |
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); |
| 228 |
eprintln!("{expected_stdout}"); |
| 229 |
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout); |
| 230 |
} |
| 231 |
|
| 232 |
#[cfg(target_os = "linux")] |
| 233 |
#[test] |
| 234 |
fn test_printf_atime_ctime_mtime_precision() { |
| 235 |
let args = ["-c", "%.0Y %.1Y %.2X %.2Y %.2Z", "/dev/pts/ptmx"]; |
| 236 |
let ts = TestScenario::new(util_name!()); |
| 237 |
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); |
| 238 |
eprintln!("{expected_stdout}"); |
| 239 |
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout); |
| 240 |
} |
| 241 |
|
| 242 |
#[cfg(feature = "touch")] |
| 243 |
#[test] |
| 244 |
fn test_timestamp_format() { |
| 245 |
let ts = TestScenario::new(util_name!()); |
| 246 |
|
| 247 |
// Create a file with a specific timestamp for testing |
| 248 |
ts.ccmd("touch") |
| 249 |
.args(&["-d", "1970-01-01 18:43:33.023456789", "k"]) |
| 250 |
.succeeds() |
| 251 |
.no_stderr(); |
| 252 |
|
| 253 |
let test_cases = vec![ |
| 254 |
// Basic timestamp formats |
| 255 |
("%Y", "67413"), |
| 256 |
("%.Y", "67413.023456789"), |
| 257 |
("%.1Y", "67413.0"), |
| 258 |
("%.3Y", "67413.023"), |
| 259 |
("%.6Y", "67413.023456"), |
| 260 |
("%.9Y", "67413.023456789"), |
| 261 |
// Width and padding tests |
| 262 |
("%13.6Y", " 67413.023456"), |
| 263 |
("%013.6Y", "067413.023456"), |
| 264 |
("%-13.6Y", "67413.023456 "), |
| 265 |
// Longer width/precision combinations |
| 266 |
("%18.10Y", " 67413.0234567890"), |
| 267 |
("%I18.10Y", " 67413.0234567890"), |
| 268 |
("%018.10Y", "0067413.0234567890"), |
| 269 |
("%-18.10Y", "67413.0234567890 "), |
| 270 |
]; |
| 271 |
|
| 272 |
for (format_str, expected) in test_cases { |
| 273 |
let result = ts |
| 274 |
.ucmd() |
| 275 |
.args(&["-c", format_str, "k"]) |
| 276 |
.succeeds() |
| 277 |
.stdout_move_str(); |
| 278 |
|
| 279 |
assert_eq!( |
| 280 |
result, |
| 281 |
format!("{expected}\n"), |
| 282 |
"Format '{format_str}' failed.\nExpected: '{expected}'\nGot: '{result}'", |
| 283 |
); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
#[test] |
| 288 |
fn test_timestamp_format_preserves_nanoseconds() { |
| 289 |
let ts = TestScenario::new(util_name!()); |
| 290 |
let path = ts.fixtures.plus("timestamp"); |
| 291 |
let file = File::create(&path).unwrap(); |
| 292 |
|
| 293 |
let timestamp = UNIX_EPOCH + Duration::new(1_755_300_000, 123_456_789); |
| 294 |
file.set_times( |
| 295 |
FileTimes::new() |
| 296 |
.set_accessed(timestamp) |
| 297 |
.set_modified(timestamp), |
| 298 |
) |
| 299 |
.unwrap(); |
| 300 |
|
| 301 |
let metadata = metadata(&path).unwrap(); |
| 302 |
let expected = format!( |
| 303 |
"1755300000.123456789 1755300000.123456789 {}.{:09}\n", |
| 304 |
metadata.ctime(), |
| 305 |
metadata.ctime_nsec() |
| 306 |
); |
| 307 |
|
| 308 |
ts.ucmd() |
| 309 |
.args(&["-c", "%.9X %.9Y %.9Z", "timestamp"]) |
| 310 |
.succeeds() |
| 311 |
.stdout_is(expected); |
| 312 |
} |
| 313 |
|
| 314 |
#[test] |
| 315 |
fn test_timestamp_format_before_epoch() { |
| 316 |
let ts = TestScenario::new(util_name!()); |
| 317 |
let path = ts.fixtures.plus("timestamp"); |
| 318 |
let file = File::create(&path).unwrap(); |
| 319 |
|
| 320 |
let timestamp = UNIX_EPOCH - Duration::new(0, 876_543_211); |
| 321 |
file.set_times( |
| 322 |
FileTimes::new() |
| 323 |
.set_accessed(timestamp) |
| 324 |
.set_modified(timestamp), |
| 325 |
) |
| 326 |
.unwrap(); |
| 327 |
|
| 328 |
ts.ucmd() |
| 329 |
.args(&["-c", "%.1X %.3X %.9X %.1Y %.3Y %.9Y", "timestamp"]) |
| 330 |
.succeeds() |
| 331 |
.stdout_is("-0.9 -0.877 -0.876543211 -0.9 -0.877 -0.876543211\n"); |
| 332 |
} |
| 333 |
|
| 334 |
#[cfg(any(target_vendor = "apple", target_os = "linux", target_os = "android"))] |
| 335 |
#[test] |
| 336 |
fn test_date() { |
| 337 |
// Just test the date for the time 0.3 change |
| 338 |
let args = [ |
| 339 |
"-c", |
| 340 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 341 |
"%z", |
| 342 |
#[cfg(target_os = "linux")] |
| 343 |
"/bin/sh", |
| 344 |
#[cfg(target_vendor = "apple")] |
| 345 |
"%z", |
| 346 |
#[cfg(any(target_vendor = "apple", target_os = "android"))] |
| 347 |
"/bin/sh", |
| 348 |
]; |
| 349 |
let ts = TestScenario::new(util_name!()); |
| 350 |
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); |
| 351 |
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout); |
| 352 |
// Just test the date for the time 0.3 change |
| 353 |
let args = [ |
| 354 |
"-c", |
| 355 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 356 |
"%z", |
| 357 |
#[cfg(target_os = "linux")] |
| 358 |
"/dev/ptmx", |
| 359 |
#[cfg(target_vendor = "apple")] |
| 360 |
"%z", |
| 361 |
#[cfg(any(target_vendor = "apple", target_os = "android"))] |
| 362 |
"/dev/ptmx", |
| 363 |
]; |
| 364 |
let ts = TestScenario::new(util_name!()); |
| 365 |
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); |
| 366 |
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout); |
| 367 |
} |
| 368 |
#[cfg(unix)] |
| 369 |
#[test] |
| 370 |
fn test_multi_files() { |
| 371 |
let args = [ |
| 372 |
"-c", |
| 373 |
NORMAL_FORMAT_STR, |
| 374 |
"/dev", |
| 375 |
"/usr/lib", |
| 376 |
#[cfg(target_os = "linux")] |
| 377 |
"/etc/fstab", |
| 378 |
"/var", |
| 379 |
]; |
| 380 |
let ts = TestScenario::new(util_name!()); |
| 381 |
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); |
| 382 |
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout); |
| 383 |
} |
| 384 |
|
| 385 |
#[cfg(unix)] |
| 386 |
#[test] |
| 387 |
fn test_printf() { |
| 388 |
let args = [ |
| 389 |
"--printf=123%-# 15q\\r\\\"\\\\\\a\\b\\x1B\\f\\x0B%+020.23m\\x12\\167\\132\\112\\n", |
| 390 |
"/", |
| 391 |
]; |
| 392 |
let ts = TestScenario::new(util_name!()); |
| 393 |
let expected_stdout = unwrap_or_return!(expected_result(&ts, &args)).stdout_move_str(); |
| 394 |
ts.ucmd().args(&args).succeeds().stdout_is(expected_stdout); |
| 395 |
} |
| 396 |
|
| 397 |
#[test] |
| 398 |
#[cfg(unix)] |
| 399 |
fn test_pipe_fifo() { |
| 400 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 401 |
at.mkfifo("FIFO"); |
| 402 |
ucmd.arg("FIFO") |
| 403 |
.succeeds() |
| 404 |
.no_stderr() |
| 405 |
.stdout_contains("fifo") |
| 406 |
.stdout_contains("File: FIFO"); |
| 407 |
} |
| 408 |
|
| 409 |
// TODO(#7583): Re-enable on Mac OS X (and possibly other Unix platforms) |
| 410 |
#[test] |
| 411 |
#[cfg(all( |
| 412 |
unix, |
| 413 |
not(any( |
| 414 |
target_vendor = "apple", |
| 415 |
target_os = "android", |
| 416 |
target_os = "freebsd", |
| 417 |
target_os = "openbsd" |
| 418 |
)) |
| 419 |
))] |
| 420 |
fn test_stdin_pipe_fifo1() { |
| 421 |
// $ echo | stat - |
| 422 |
// File: - |
| 423 |
// Size: 0 Blocks: 0 IO Block: 4096 fifo |
| 424 |
new_ucmd!() |
| 425 |
.arg("-") |
| 426 |
.set_stdin(std::process::Stdio::piped()) |
| 427 |
.succeeds() |
| 428 |
.no_stderr() |
| 429 |
.stdout_contains("fifo") |
| 430 |
.stdout_contains("File: -"); |
| 431 |
new_ucmd!() |
| 432 |
.args(&["-L", "-"]) |
| 433 |
.set_stdin(std::process::Stdio::piped()) |
| 434 |
.succeeds() |
| 435 |
.no_stderr() |
| 436 |
.stdout_contains("fifo") |
| 437 |
.stdout_contains("File: -"); |
| 438 |
} |
| 439 |
|
| 440 |
// TODO(#7583): Re-enable on Mac OS X (and maybe Android) |
| 441 |
#[test] |
| 442 |
#[cfg(all(unix, not(any(target_vendor = "apple", target_os = "android"))))] |
| 443 |
fn test_stdin_pipe_fifo2() { |
| 444 |
// $ stat - |
| 445 |
// File: - |
| 446 |
// Size: 0 Blocks: 0 IO Block: 1024 character special file |
| 447 |
new_ucmd!() |
| 448 |
.arg("-") |
| 449 |
.set_stdin(std::process::Stdio::null()) |
| 450 |
.succeeds() |
| 451 |
.no_stderr() |
| 452 |
.stdout_contains("character special file") |
| 453 |
.stdout_contains("File: -"); |
| 454 |
} |
| 455 |
|
| 456 |
#[test] |
| 457 |
#[cfg(all(unix, not(target_os = "android")))] |
| 458 |
fn test_stdin_with_fs_option() { |
| 459 |
// $ stat -f - |
| 460 |
new_ucmd!() |
| 461 |
.arg("-f") |
| 462 |
.arg("-") |
| 463 |
.set_stdin(std::process::Stdio::null()) |
| 464 |
.fails_with_code(1) |
| 465 |
.stderr_contains("using '-' to denote standard input does not work in file system mode"); |
| 466 |
} |
| 467 |
|
| 468 |
#[test] |
| 469 |
#[cfg(all( |
| 470 |
unix, |
| 471 |
not(any( |
| 472 |
target_vendor = "apple", |
| 473 |
target_os = "android", |
| 474 |
target_os = "freebsd", |
| 475 |
target_os = "openbsd" |
| 476 |
)) |
| 477 |
))] |
| 478 |
fn test_stdin_redirect() { |
| 479 |
// $ touch f && stat - < f |
| 480 |
// File: - |
| 481 |
// Size: 0 Blocks: 0 IO Block: 4096 regular empty file |
| 482 |
let ts = TestScenario::new(util_name!()); |
| 483 |
let at = &ts.fixtures; |
| 484 |
at.touch("f"); |
| 485 |
ts.ucmd() |
| 486 |
.arg("-") |
| 487 |
.set_stdin(File::open(at.plus("f")).unwrap()) |
| 488 |
.succeeds() |
| 489 |
.no_stderr() |
| 490 |
.stdout_contains("regular empty file") |
| 491 |
.stdout_contains("File: -"); |
| 492 |
} |
| 493 |
|
| 494 |
#[test] |
| 495 |
fn test_without_argument() { |
| 496 |
new_ucmd!() |
| 497 |
.fails() |
| 498 |
.stderr_contains("the following required arguments were not provided"); // clap provided message |
| 499 |
} |
| 500 |
|
| 501 |
#[test] |
| 502 |
fn test_quoting_style_locale() { |
| 503 |
let ts = TestScenario::new(util_name!()); |
| 504 |
let at = &ts.fixtures; |
| 505 |
at.touch("'"); |
| 506 |
ts.ucmd() |
| 507 |
.env("QUOTING_STYLE", "locale") |
| 508 |
.args(&["-c", "%N", "'"]) |
| 509 |
.succeeds() |
| 510 |
.stdout_only("'\\''\n"); |
| 511 |
|
| 512 |
ts.ucmd() |
| 513 |
.args(&["-c", "%N", "'"]) |
| 514 |
.succeeds() |
| 515 |
.stdout_only("\"'\"\n"); |
| 516 |
|
| 517 |
// testing file having " |
| 518 |
at.touch("\""); |
| 519 |
ts.ucmd() |
| 520 |
.args(&["-c", "%N", "\""]) |
| 521 |
.succeeds() |
| 522 |
.stdout_only("\'\"\'\n"); |
| 523 |
} |
| 524 |
|
| 525 |
#[test] |
| 526 |
fn test_quoting_newline_in_filename() { |
| 527 |
let ts = TestScenario::new(util_name!()); |
| 528 |
let at = &ts.fixtures; |
| 529 |
|
| 530 |
// example from issue #9925 |
| 531 |
at.touch("test\nnewline"); |
| 532 |
ts.ucmd() |
| 533 |
.args(&["-c", r#"{"name":"%N"}"#, "test\nnewline"]) |
| 534 |
.succeeds() |
| 535 |
.stdout_only("{\"name\":\"'test'$'\\n''newline'\"}\n"); |
| 536 |
|
| 537 |
// with stat, contiguous escape characters are clumped into one escape sequence |
| 538 |
at.touch("contiguous\n\nescape_characters"); |
| 539 |
ts.ucmd() |
| 540 |
.args(&["-c", "%N", "contiguous\n\nescape_characters"]) |
| 541 |
.succeeds() |
| 542 |
.stdout_only("'contiguous'$'\\n\\n''escape_characters'\n"); |
| 543 |
|
| 544 |
at.touch("multiple\nescape\ncharacters"); |
| 545 |
ts.ucmd() |
| 546 |
.args(&["-c", "%N", "multiple\nescape\ncharacters"]) |
| 547 |
.succeeds() |
| 548 |
.stdout_only("'multiple'$'\\n''escape'$'\\n''characters'\n"); |
| 549 |
|
| 550 |
// testing other escape characters |
| 551 |
at.touch("\t \n \r \x01"); |
| 552 |
ts.ucmd() |
| 553 |
.args(&["-c", "%N", "\t \n \r \x01"]) |
| 554 |
.succeeds() |
| 555 |
.stdout_only("''$'\\t'' '$'\\n'' '$'\\r'' '$'\\001'\n"); |
| 556 |
} |
| 557 |
|
| 558 |
#[test] |
| 559 |
fn test_quoting_style_invalid_env() { |
| 560 |
let ts = TestScenario::new(util_name!()); |
| 561 |
let at = &ts.fixtures; |
| 562 |
at.touch("baguette"); |
| 563 |
at.touch("Croissant"); |
| 564 |
at.touch("Escargot"); |
| 565 |
|
| 566 |
let needle = "ignoring invalid value of environment variable QUOTING_STYLE"; |
| 567 |
|
| 568 |
// A bogus value triggers exactly one warning across multiple files and the |
| 569 |
// output falls back to the default (shell-escape) style. |
| 570 |
let res = ts |
| 571 |
.ucmd() |
| 572 |
.env("QUOTING_STYLE", "fromage") |
| 573 |
.args(&["-c", "nom=[%N]", "baguette", "Croissant", "Escargot"]) |
| 574 |
.succeeds(); |
| 575 |
res.stdout_is("nom=['baguette']\nnom=['Croissant']\nnom=['Escargot']\n"); |
| 576 |
assert_eq!(res.stderr_str().matches(needle).count(), 1); |
| 577 |
|
| 578 |
// An empty value is also invalid and must be reported with empty quotes. |
| 579 |
ts.ucmd() |
| 580 |
.env("QUOTING_STYLE", "") |
| 581 |
.args(&["-c", "%N", "baguette"]) |
| 582 |
.succeeds() |
| 583 |
.stdout_is("'baguette'\n") |
| 584 |
.stderr_is("stat: ignoring invalid value of environment variable QUOTING_STYLE: ''\n"); |
| 585 |
|
| 586 |
// %%%N: a literal '%' followed by the quoted name, fallback style applies. |
| 587 |
ts.ucmd() |
| 588 |
.env("QUOTING_STYLE", "soufflé") |
| 589 |
.args(&["-c", "%%%N", "baguette"]) |
| 590 |
.succeeds() |
| 591 |
.stdout_is("%'baguette'\n") |
| 592 |
.stderr_is( |
| 593 |
"stat: ignoring invalid value of environment variable QUOTING_STYLE: 'soufflé'\n", |
| 594 |
); |
| 595 |
|
| 596 |
// When the format never consults %N, QUOTING_STYLE must not be parsed at all. |
| 597 |
ts.ucmd() |
| 598 |
.env("QUOTING_STYLE", "crème-brûlée") |
| 599 |
.args(&["-c", "taille=%s genre:%F brut=%n", "baguette"]) |
| 600 |
.succeeds() |
| 601 |
.no_stderr(); |
| 602 |
} |
| 603 |
|
| 604 |
#[test] |
| 605 |
fn test_printf_octal_1() { |
| 606 |
let ts = TestScenario::new(util_name!()); |
| 607 |
let expected_stdout = vec![0x0A, 0xFF]; // Newline + byte 255 |
| 608 |
ts.ucmd() |
| 609 |
.args(&["--printf=\\012\\377", "."]) |
| 610 |
.succeeds() |
| 611 |
.stdout_is_bytes(expected_stdout); |
| 612 |
} |
| 613 |
|
| 614 |
#[test] |
| 615 |
fn test_printf_octal_2() { |
| 616 |
let ts = TestScenario::new(util_name!()); |
| 617 |
let expected_stdout = vec![b'.', 0x0A, b'a', 0xFF, b'b']; |
| 618 |
ts.ucmd() |
| 619 |
.args(&["--printf=.\\012a\\377b", "."]) |
| 620 |
.succeeds() |
| 621 |
.stdout_is_bytes(expected_stdout); |
| 622 |
} |
| 623 |
|
| 624 |
#[test] |
| 625 |
fn test_printf_octal_out_of_range() { |
| 626 |
// Octal escapes whose value exceeds 255 wrap around, as they do in GNU stat. |
| 627 |
let ts = TestScenario::new(util_name!()); |
| 628 |
let expected_stdout = vec![0x00, 0xFF]; // \400 -> 256 & 0xFF, \777 -> 511 & 0xFF |
| 629 |
ts.ucmd() |
| 630 |
.args(&["--printf=\\400\\777", "."]) |
| 631 |
.succeeds() |
| 632 |
.stdout_is_bytes(expected_stdout); |
| 633 |
} |
| 634 |
|
| 635 |
#[test] |
| 636 |
fn test_printf_incomplete_hex() { |
| 637 |
let ts = TestScenario::new(util_name!()); |
| 638 |
ts.ucmd() |
| 639 |
.args(&["--printf=\\x", "."]) |
| 640 |
.succeeds() |
| 641 |
.stderr_contains("warning: incomplete hex escape"); |
| 642 |
} |
| 643 |
|
| 644 |
#[test] |
| 645 |
fn test_printf_bel_etc() { |
| 646 |
let ts = TestScenario::new(util_name!()); |
| 647 |
let expected_stdout = vec![0x07, 0x08, 0x0C, 0x0A, 0x0D, 0x09]; // BEL, BS, FF, LF, CR, TAB |
| 648 |
ts.ucmd() |
| 649 |
.args(&["--printf=\\a\\b\\f\\n\\r\\t", "."]) |
| 650 |
.succeeds() |
| 651 |
.stdout_is_bytes(expected_stdout); |
| 652 |
} |
| 653 |
|
| 654 |
#[test] |
| 655 |
fn test_printf_invalid_directive() { |
| 656 |
let ts = TestScenario::new(util_name!()); |
| 657 |
|
| 658 |
ts.ucmd() |
| 659 |
.args(&["--printf=%9", "."]) |
| 660 |
.fails_with_code(1) |
| 661 |
.stderr_contains("'%9': invalid directive"); |
| 662 |
|
| 663 |
ts.ucmd() |
| 664 |
.args(&["--printf=%9%", "."]) |
| 665 |
.fails_with_code(1) |
| 666 |
.stderr_contains("'%9%': invalid directive"); |
| 667 |
} |
| 668 |
|
| 669 |
#[test] |
| 670 |
fn test_invalid_directive_after_multibyte_char() { |
| 671 |
let ts = TestScenario::new(util_name!()); |
| 672 |
// The text before the directive is printed, as GNU does. What is checked |
| 673 |
// here is the directive named: a multibyte char must not shift its offset. |
| 674 |
for (fmt, before, directive) in [("€%-", "€", "%-"), ("ä%0", "ä", "%0"), ("€%.", "€", "%.")] |
| 675 |
{ |
| 676 |
ts.ucmd() |
| 677 |
.args(&["-c", fmt, "."]) |
| 678 |
.fails_with_code(1) |
| 679 |
.stdout_is(before) |
| 680 |
.stderr_is(format!("stat: '{directive}': invalid directive\n")); |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
#[test] |
| 685 |
fn test_precision_splits_multibyte_char_in_value() { |
| 686 |
let ts = TestScenario::new(util_name!()); |
| 687 |
let at = &ts.fixtures; |
| 688 |
at.touch("é"); |
| 689 |
ts.ucmd() |
| 690 |
.args(&["-c", "%.1n", "é"]) |
| 691 |
.succeeds() |
| 692 |
.stdout_only_bytes([0xc3, b'\n']); |
| 693 |
} |
| 694 |
|
| 695 |
#[test] |
| 696 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 697 |
fn test_stat_selinux() { |
| 698 |
let ts = TestScenario::new(util_name!()); |
| 699 |
let at = &ts.fixtures; |
| 700 |
at.touch("f"); |
| 701 |
ts.ucmd() |
| 702 |
.arg("--printf='%C'") |
| 703 |
.arg("f") |
| 704 |
.succeeds() |
| 705 |
.no_stderr() |
| 706 |
.stdout_contains("unconfined_u"); |
| 707 |
ts.ucmd() |
| 708 |
.arg("--printf='%C'") |
| 709 |
.arg("/bin/") |
| 710 |
.succeeds() |
| 711 |
.no_stderr() |
| 712 |
.stdout_contains("system_u"); |
| 713 |
// Count that we have 4 fields |
| 714 |
let result = ts.ucmd().arg("--printf='%C'").arg("/bin/").succeeds(); |
| 715 |
let s: Vec<_> = result.stdout_str().split(':').collect(); |
| 716 |
assert_eq!(s.len(), 4); |
| 717 |
} |
| 718 |
|
| 719 |
#[cfg(unix)] |
| 720 |
#[test] |
| 721 |
fn test_mount_point_basic() { |
| 722 |
let ts = TestScenario::new(util_name!()); |
| 723 |
let result = ts.ucmd().args(&["-c", "%m", "/"]).succeeds(); |
| 724 |
let output = result.stdout_str().trim(); |
| 725 |
assert!(!output.is_empty(), "Mount point should not be empty"); |
| 726 |
assert_eq!(output, "/"); |
| 727 |
} |
| 728 |
|
| 729 |
#[cfg(unix)] |
| 730 |
#[test] |
| 731 |
fn test_mount_point_width_and_alignment() { |
| 732 |
let ts = TestScenario::new(util_name!()); |
| 733 |
|
| 734 |
// Right-aligned, width 15 |
| 735 |
let result = ts.ucmd().args(&["-c", "%15m", "/"]).succeeds(); |
| 736 |
let output = result.stdout_str(); |
| 737 |
assert!( |
| 738 |
output.trim().len() <= 15 && output.len() >= 15, |
| 739 |
"Output should be padded to width 15" |
| 740 |
); |
| 741 |
|
| 742 |
// Left-aligned, width 15 |
| 743 |
let result = ts.ucmd().args(&["-c", "%-15m", "/"]).succeeds(); |
| 744 |
let output = result.stdout_str(); |
| 745 |
|
| 746 |
assert!( |
| 747 |
output.trim().len() <= 15 && output.len() >= 15, |
| 748 |
"Output should be padded to width 15 (left-aligned)" |
| 749 |
); |
| 750 |
} |
| 751 |
|
| 752 |
#[cfg(unix)] |
| 753 |
#[test] |
| 754 |
fn test_mount_point_combined_with_other_specifiers() { |
| 755 |
let ts = TestScenario::new(util_name!()); |
| 756 |
let result = ts.ucmd().args(&["-c", "%m %n %s", "/bin/sh"]).succeeds(); |
| 757 |
let output = result.stdout_str(); |
| 758 |
let parts: Vec<&str> = output.split_whitespace().collect(); |
| 759 |
assert!( |
| 760 |
parts.len() >= 3, |
| 761 |
"Should print mount point, file name, and size" |
| 762 |
); |
| 763 |
} |
| 764 |
|
| 765 |
#[cfg(unix)] |
| 766 |
#[test] |
| 767 |
fn test_percent_escaping() { |
| 768 |
let ts = TestScenario::new(util_name!()); |
| 769 |
let result = ts |
| 770 |
.ucmd() |
| 771 |
.args(&["--printf", "%%%m%%m%m%%%", "/bin/sh"]) |
| 772 |
.succeeds(); |
| 773 |
assert_eq!(result.stdout_str(), "%/%m/%%"); |
| 774 |
} |
| 775 |
|
| 776 |
#[cfg(unix)] |
| 777 |
#[test] |
| 778 |
fn test_correct_metadata() { |
| 779 |
use uucore::fs::{major, minor}; |
| 780 |
|
| 781 |
let ts = TestScenario::new(util_name!()); |
| 782 |
let parse = |(i, str): (usize, &str)| { |
| 783 |
// Some outputs (%[fDRtT]) are in hex; they're redundant, but we might |
| 784 |
// as well also test case conversion. |
| 785 |
let radix = if matches!(i, 2 | 10 | 14..) { 16 } else { 10 }; |
| 786 |
i128::from_str_radix(str, radix) |
| 787 |
}; |
| 788 |
for device in ["/", "/dev/null"] { |
| 789 |
let metadata = metadata(device).unwrap(); |
| 790 |
// We avoid time vals because of fs race conditions, especially with |
| 791 |
// access time and status time (this previously killed an otherwise |
| 792 |
// perfect 11-hour-long CI run...). The large number of as-casts is |
| 793 |
// due to inconsistencies on some platforms (read: BSDs), and we use |
| 794 |
// i128 as a lowest-common denominator. |
| 795 |
let test_str = "%u %g %f %b %s %h %i %d %Hd %Ld %D %r %Hr %Lr %R %t %T"; |
| 796 |
let expected = [ |
| 797 |
metadata.uid() as _, |
| 798 |
metadata.gid() as _, |
| 799 |
metadata.mode() as _, |
| 800 |
metadata.blocks() as _, |
| 801 |
metadata.size() as _, |
| 802 |
metadata.nlink() as _, |
| 803 |
metadata.ino() as _, |
| 804 |
metadata.dev() as _, |
| 805 |
major(metadata.dev() as _) as _, |
| 806 |
minor(metadata.dev() as _) as _, |
| 807 |
metadata.dev() as _, |
| 808 |
metadata.rdev() as _, |
| 809 |
major(metadata.rdev() as _) as _, |
| 810 |
minor(metadata.rdev() as _) as _, |
| 811 |
metadata.rdev() as _, |
| 812 |
major(metadata.rdev() as _) as _, |
| 813 |
minor(metadata.rdev() as _) as _, |
| 814 |
]; |
| 815 |
let result = ts.ucmd().args(&["--printf", test_str, device]).succeeds(); |
| 816 |
let output = result |
| 817 |
.stdout_str() |
| 818 |
.split(' ') |
| 819 |
.enumerate() |
| 820 |
.map(parse) |
| 821 |
.collect::<Result<Vec<i128>, _>>() |
| 822 |
.unwrap(); |
| 823 |
assert_eq!(output, &expected); |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
#[test] |
| 828 |
fn test_no_such_directory_message() { |
| 829 |
let ts = TestScenario::new(util_name!()); |
| 830 |
ts.ucmd() |
| 831 |
.arg("a") |
| 832 |
.fails_with_code(1) |
| 833 |
.stderr_is("stat: cannot statx 'a': No such file or directory\n"); |
| 834 |
} |
| 835 |
|
| 836 |
#[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))] |
| 837 |
mod diagnostics { |
| 838 |
use super::*; |
| 839 |
|
| 840 |
#[cfg(unix)] |
| 841 |
#[test] |
| 842 |
fn test_snippet_points_at_the_failing_directive() { |
| 843 |
let result = new_ucmd!() |
| 844 |
.terminal_sim_stderr() |
| 845 |
.args(&["-c", "%d%.3", "/dev/null"]) |
| 846 |
.fails_with_code(1); |
| 847 |
|
| 848 |
// The first directive is fine; the caret takes the second one alone. |
| 849 |
assert_eq!( |
| 850 |
result.stderr_as_displayed(), |
| 851 |
"\ |
| 852 |
stat: '%.3': invalid directive |
| 853 |
╭─[ stat:1:11 ] |
| 854 |
│ |
| 855 |
1 │ stat -c %d%.3 /dev/null |
| 856 |
│ ─── |
| 857 |
│ |
| 858 |
│ Help: a directive is %[FLAGS][WIDTH][.PRECISION]LETTER, as in %-10.2s; a literal % is written %% |
| 859 |
───╯" |
| 860 |
); |
| 861 |
} |
| 862 |
|
| 863 |
#[cfg(unix)] |
| 864 |
#[test] |
| 865 |
fn test_snippet_points_inside_a_printf_format() { |
| 866 |
let result = new_ucmd!() |
| 867 |
.terminal_sim_stderr() |
| 868 |
.args(&["--printf=%12", "/dev/null"]) |
| 869 |
.fails_with_code(1); |
| 870 |
let stderr = result.stderr_as_displayed(); |
| 871 |
|
| 872 |
assert!(stderr.contains("stat:1:15"), "{stderr}"); |
| 873 |
assert!(stderr.contains("'%12': invalid directive"), "{stderr}"); |
| 874 |
} |
| 875 |
|
| 876 |
#[test] |
| 877 |
fn test_plain_message_when_stderr_is_a_pipe() { |
| 878 |
new_ucmd!() |
| 879 |
.args(&["-c", "%d%.3", "/dev/null"]) |
| 880 |
.fails_with_code(1) |
| 881 |
.stderr_is("stat: '%.3': invalid directive\n"); |
| 882 |
} |
| 883 |
} |