| 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 udev pcent iuse itotal iused ipcent binfmt |
| 7 |
#![allow( |
| 8 |
clippy::similar_names, |
| 9 |
clippy::cast_possible_truncation, |
| 10 |
clippy::cast_sign_loss, |
| 11 |
clippy::float_cmp |
| 12 |
)] |
| 13 |
|
| 14 |
use std::collections::HashSet; |
| 15 |
|
| 16 |
#[cfg(not(any(target_os = "freebsd", windows)))] |
| 17 |
use uutests::at_and_ucmd; |
| 18 |
use uutests::new_ucmd; |
| 19 |
#[cfg(target_os = "linux")] |
| 20 |
use uutests::util::TestScenario; |
| 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_df_compatible_no_size_arg() { |
| 29 |
new_ucmd!().arg("-a").succeeds(); |
| 30 |
} |
| 31 |
|
| 32 |
#[test] |
| 33 |
fn test_df_shortened_long_argument() { |
| 34 |
new_ucmd!().arg("--a").succeeds(); |
| 35 |
} |
| 36 |
|
| 37 |
#[test] |
| 38 |
fn test_df_compatible() { |
| 39 |
new_ucmd!().arg("-ah").succeeds(); |
| 40 |
} |
| 41 |
|
| 42 |
#[test] |
| 43 |
fn test_df_compatible_type() { |
| 44 |
new_ucmd!().arg("-aT").succeeds(); |
| 45 |
} |
| 46 |
|
| 47 |
#[test] |
| 48 |
fn test_df_compatible_si() { |
| 49 |
new_ucmd!().arg("-aH").succeeds(); |
| 50 |
} |
| 51 |
|
| 52 |
#[test] |
| 53 |
fn test_df_compatible_sync() { |
| 54 |
new_ucmd!().arg("--sync").succeeds(); |
| 55 |
} |
| 56 |
|
| 57 |
#[test] |
| 58 |
fn test_df_arguments_override_themselves() { |
| 59 |
new_ucmd!().args(&["--help", "--help"]).succeeds(); |
| 60 |
new_ucmd!().arg("-aa").succeeds(); |
| 61 |
new_ucmd!() |
| 62 |
.args(&["--block-size=3000", "--block-size=1000"]) |
| 63 |
.succeeds(); |
| 64 |
new_ucmd!().args(&["--total", "--total"]).succeeds(); |
| 65 |
new_ucmd!().arg("-hh").succeeds(); |
| 66 |
new_ucmd!().arg("-HH").succeeds(); |
| 67 |
new_ucmd!().arg("-ii").succeeds(); |
| 68 |
new_ucmd!().arg("-kk").succeeds(); |
| 69 |
new_ucmd!().arg("-ll").succeeds(); |
| 70 |
new_ucmd!().args(&["--no-sync", "--no-sync"]).succeeds(); |
| 71 |
new_ucmd!().arg("-PP").succeeds(); |
| 72 |
new_ucmd!().args(&["--sync", "--sync"]).succeeds(); |
| 73 |
new_ucmd!().arg("-TT").succeeds(); |
| 74 |
} |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn test_df_conflicts_overriding() { |
| 78 |
new_ucmd!().arg("-hH").succeeds(); |
| 79 |
new_ucmd!().arg("-Hh").succeeds(); |
| 80 |
new_ucmd!().args(&["--no-sync", "--sync"]).succeeds(); |
| 81 |
new_ucmd!().args(&["--sync", "--no-sync"]).succeeds(); |
| 82 |
new_ucmd!().args(&["-k", "--block-size=3000"]).succeeds(); |
| 83 |
new_ucmd!().args(&["--block-size=3000", "-k"]).succeeds(); |
| 84 |
} |
| 85 |
|
| 86 |
#[test] |
| 87 |
fn test_df_output_arg() { |
| 88 |
new_ucmd!().args(&["--output=source", "-iPT"]).fails(); |
| 89 |
new_ucmd!().args(&["-iPT", "--output=source"]).fails(); |
| 90 |
new_ucmd!() |
| 91 |
.args(&["--output=source", "--output=source"]) |
| 92 |
.fails(); |
| 93 |
} |
| 94 |
|
| 95 |
#[test] |
| 96 |
#[cfg(windows)] |
| 97 |
fn test_inodes_not_supported_windows() { |
| 98 |
// The notice goes to stdout with exit 0, before any other option is validated. |
| 99 |
for args in [&["-i"][..], &["-i", "--block-size=bogus"][..]] { |
| 100 |
new_ucmd!() |
| 101 |
.args(args) |
| 102 |
.succeeds() |
| 103 |
.stdout_only("df: doesn't support -i option\n"); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
#[test] |
| 108 |
fn test_df_output() { |
| 109 |
let expected = if cfg!(target_vendor = "apple") { |
| 110 |
vec![ |
| 111 |
"Filesystem", |
| 112 |
"Size", |
| 113 |
"Used", |
| 114 |
"Avail", |
| 115 |
"Capacity", |
| 116 |
"Use%", |
| 117 |
"Mounted", |
| 118 |
"on", |
| 119 |
] |
| 120 |
} else { |
| 121 |
vec![ |
| 122 |
"Filesystem", |
| 123 |
"Size", |
| 124 |
"Used", |
| 125 |
"Avail", |
| 126 |
"Use%", |
| 127 |
"Mounted", |
| 128 |
"on", |
| 129 |
] |
| 130 |
}; |
| 131 |
let output = new_ucmd!() |
| 132 |
.arg("-H") |
| 133 |
.arg("--total") |
| 134 |
.succeeds() |
| 135 |
.stdout_str_lossy(); |
| 136 |
let actual = output.lines().take(1).collect::<Vec<&str>>()[0]; |
| 137 |
let actual = actual.split_whitespace().collect::<Vec<_>>(); |
| 138 |
assert_eq!(actual, expected); |
| 139 |
} |
| 140 |
|
| 141 |
#[test] |
| 142 |
fn test_df_follows_symlinks() { |
| 143 |
let output = new_ucmd!() |
| 144 |
.arg("-h") |
| 145 |
.arg("--output=source") |
| 146 |
.succeeds() |
| 147 |
.stdout_str_lossy(); |
| 148 |
|
| 149 |
let filesystems = output.lines().skip(1).collect::<Vec<&str>>(); |
| 150 |
assert!( |
| 151 |
filesystems |
| 152 |
.iter() |
| 153 |
.all(|&x| !std::path::Path::new(x).is_symlink()) |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
#[test] |
| 158 |
fn test_df_trailing_zeros() { |
| 159 |
use regex::Regex; |
| 160 |
|
| 161 |
new_ucmd!() |
| 162 |
.arg("-h") |
| 163 |
.arg("--output=size,used") |
| 164 |
.arg("--total") |
| 165 |
.succeeds() |
| 166 |
.stdout_does_not_match(&Regex::new("\\s[1-9][A-Z]").unwrap()); |
| 167 |
} |
| 168 |
|
| 169 |
#[test] |
| 170 |
fn test_df_rounding() { |
| 171 |
use regex::Regex; |
| 172 |
|
| 173 |
new_ucmd!() |
| 174 |
.arg("-H") |
| 175 |
.arg("--output=size,used") |
| 176 |
.arg("--total") |
| 177 |
.succeeds() |
| 178 |
.stdout_does_not_match(&Regex::new("\\s\\d{3}\\.\\d[A-Z]").unwrap()); |
| 179 |
} |
| 180 |
|
| 181 |
#[test] |
| 182 |
fn test_df_output_overridden() { |
| 183 |
let expected = if cfg!(target_vendor = "apple") { |
| 184 |
vec![ |
| 185 |
"Filesystem", |
| 186 |
"Size", |
| 187 |
"Used", |
| 188 |
"Avail", |
| 189 |
"Capacity", |
| 190 |
"Use%", |
| 191 |
"Mounted", |
| 192 |
"on", |
| 193 |
] |
| 194 |
} else { |
| 195 |
vec![ |
| 196 |
"Filesystem", |
| 197 |
"Size", |
| 198 |
"Used", |
| 199 |
"Avail", |
| 200 |
"Use%", |
| 201 |
"Mounted", |
| 202 |
"on", |
| 203 |
] |
| 204 |
}; |
| 205 |
let output = new_ucmd!() |
| 206 |
.arg("-hH") |
| 207 |
.arg("--total") |
| 208 |
.succeeds() |
| 209 |
.stdout_str_lossy(); |
| 210 |
let actual = output.lines().take(1).collect::<Vec<&str>>()[0]; |
| 211 |
let actual = actual.split_whitespace().collect::<Vec<_>>(); |
| 212 |
assert_eq!(actual, expected); |
| 213 |
} |
| 214 |
|
| 215 |
#[test] |
| 216 |
fn test_default_headers() { |
| 217 |
let expected = if cfg!(target_vendor = "apple") { |
| 218 |
vec![ |
| 219 |
"Filesystem", |
| 220 |
"1K-blocks", |
| 221 |
"Used", |
| 222 |
"Available", |
| 223 |
"Capacity", |
| 224 |
"Use%", |
| 225 |
"Mounted", |
| 226 |
"on", |
| 227 |
] |
| 228 |
} else { |
| 229 |
vec![ |
| 230 |
"Filesystem", |
| 231 |
"1K-blocks", |
| 232 |
"Used", |
| 233 |
"Available", |
| 234 |
"Use%", |
| 235 |
"Mounted", |
| 236 |
"on", |
| 237 |
] |
| 238 |
}; |
| 239 |
let output = new_ucmd!().succeeds().stdout_str_lossy(); |
| 240 |
let actual = output.lines().take(1).collect::<Vec<&str>>()[0]; |
| 241 |
let actual = actual.split_whitespace().collect::<Vec<_>>(); |
| 242 |
assert_eq!(actual, expected); |
| 243 |
} |
| 244 |
|
| 245 |
#[test] |
| 246 |
fn test_precedence_of_human_readable_and_si_header_over_output_header() { |
| 247 |
let args = ["-h", "--human-readable", "-H", "--si"]; |
| 248 |
|
| 249 |
for arg in args { |
| 250 |
let output = new_ucmd!() |
| 251 |
.args(&[arg, "--output=size"]) |
| 252 |
.succeeds() |
| 253 |
.stdout_str_lossy(); |
| 254 |
let header = output.lines().next().unwrap(); |
| 255 |
assert_eq!(header, " Size"); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
#[test] |
| 260 |
fn test_used_header_starts_with_space() { |
| 261 |
let output = new_ucmd!() |
| 262 |
// using -h here to ensure the width of the column's content is <= 4 |
| 263 |
.args(&["-h", "--output=used"]) |
| 264 |
.succeeds() |
| 265 |
.stdout_str_lossy(); |
| 266 |
let header = output.lines().next().unwrap(); |
| 267 |
assert_eq!(header, " Used"); |
| 268 |
} |
| 269 |
|
| 270 |
#[test] |
| 271 |
#[cfg(not(target_os = "freebsd"))] // FIXME: fix this test for FreeBSD |
| 272 |
fn test_total_option_with_single_dash() { |
| 273 |
// These should fail because `-total` should have two dashes, |
| 274 |
// not just one. |
| 275 |
new_ucmd!().arg("-total").fails(); |
| 276 |
} |
| 277 |
|
| 278 |
/// Test that the order of rows in the table does not change across executions. |
| 279 |
#[test] |
| 280 |
fn test_order_same() { |
| 281 |
let output1 = new_ucmd!() |
| 282 |
.arg("--output=source") |
| 283 |
.succeeds() |
| 284 |
.stdout_str_lossy(); |
| 285 |
let output2 = new_ucmd!() |
| 286 |
.arg("--output=source") |
| 287 |
.succeeds() |
| 288 |
.stdout_str_lossy(); |
| 289 |
assert_eq!(output1, output2); |
| 290 |
} |
| 291 |
|
| 292 |
/// Test of mount point begin repeated |
| 293 |
#[cfg(all(unix, not(target_os = "freebsd")))] // FIXME: fix this test for FreeBSD |
| 294 |
#[test] |
| 295 |
fn test_output_mp_repeat() { |
| 296 |
let output1 = new_ucmd!().arg("/").arg("/").succeeds().stdout_str_lossy(); |
| 297 |
let output1: Vec<String> = output1 |
| 298 |
.lines() |
| 299 |
.map(|l| String::from(l.split_once(' ').unwrap().0)) |
| 300 |
.collect(); |
| 301 |
assert_eq!(3, output1.len()); |
| 302 |
assert_eq!(output1[1], output1[2]); |
| 303 |
} |
| 304 |
|
| 305 |
#[test] |
| 306 |
fn test_output_conflict_options() { |
| 307 |
for option in ["-i", "-T", "-P"] { |
| 308 |
new_ucmd!().arg("--output=source").arg(option).fails(); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
#[test] |
| 313 |
fn test_output_option() { |
| 314 |
new_ucmd!().arg("--output").succeeds(); |
| 315 |
new_ucmd!().arg("--output=source,target").succeeds(); |
| 316 |
new_ucmd!().arg("--output=invalid_option").fails(); |
| 317 |
} |
| 318 |
|
| 319 |
#[test] |
| 320 |
#[cfg(not(target_os = "freebsd"))] // FIXME: fix this test for FreeBSD |
| 321 |
fn test_output_option_without_equals_sign() { |
| 322 |
new_ucmd!().arg("--output").arg(".").succeeds(); |
| 323 |
} |
| 324 |
|
| 325 |
#[test] |
| 326 |
fn test_type_option() { |
| 327 |
let fs_types = new_ucmd!() |
| 328 |
.arg("--output=fstype") |
| 329 |
.succeeds() |
| 330 |
.stdout_str_lossy(); |
| 331 |
let fs_type = fs_types.lines().nth(1).unwrap().trim(); |
| 332 |
|
| 333 |
new_ucmd!().args(&["-t", fs_type]).succeeds(); |
| 334 |
new_ucmd!() |
| 335 |
.args(&["-t", fs_type, "-t", "nonexisting"]) |
| 336 |
.succeeds(); |
| 337 |
new_ucmd!() |
| 338 |
.args(&["-t", "nonexisting"]) |
| 339 |
.fails() |
| 340 |
.stderr_contains("no file systems processed"); |
| 341 |
} |
| 342 |
|
| 343 |
#[test] |
| 344 |
#[cfg(not(any(target_os = "freebsd", target_os = "openbsd", windows)))] // FIXME: fix test for FreeBSD, OpenBSD & Win |
| 345 |
#[cfg(not(feature = "selinux"))] |
| 346 |
fn test_type_option_with_file() { |
| 347 |
let fs_type = new_ucmd!() |
| 348 |
.args(&["--output=fstype", "."]) |
| 349 |
.succeeds() |
| 350 |
.stdout_str_lossy(); |
| 351 |
let fs_type = fs_type.lines().nth(1).unwrap().trim(); |
| 352 |
|
| 353 |
new_ucmd!().args(&["-t", fs_type, "."]).succeeds(); |
| 354 |
new_ucmd!() |
| 355 |
.args(&["-t", "nonexisting", "."]) |
| 356 |
.fails() |
| 357 |
.stderr_contains("no file systems processed"); |
| 358 |
|
| 359 |
// Assume the mount point at /dev has a different filesystem type to the mount point at / |
| 360 |
new_ucmd!() |
| 361 |
.args(&["-t", fs_type, "/dev"]) |
| 362 |
.fails() |
| 363 |
.stderr_contains("no file systems processed"); |
| 364 |
|
| 365 |
let fs_types = new_ucmd!() |
| 366 |
.arg("--output=fstype") |
| 367 |
.succeeds() |
| 368 |
.stdout_str_lossy(); |
| 369 |
let fs_types: Vec<_> = fs_types |
| 370 |
.lines() |
| 371 |
.skip(1) |
| 372 |
.filter(|t| t.trim() != fs_type && t.trim() != "") |
| 373 |
.collect(); |
| 374 |
|
| 375 |
if !fs_types.is_empty() { |
| 376 |
new_ucmd!() |
| 377 |
.args(&["-t", fs_types[0], "."]) |
| 378 |
.fails() |
| 379 |
.stderr_contains("no file systems processed"); |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
#[test] |
| 384 |
fn test_exclude_type_option() { |
| 385 |
new_ucmd!().args(&["-x", "ext4", "-x", "ext3"]).succeeds(); |
| 386 |
} |
| 387 |
|
| 388 |
#[test] |
| 389 |
fn test_exclude_all_types() { |
| 390 |
let fs_types = new_ucmd!() |
| 391 |
.arg("--output=fstype") |
| 392 |
.succeeds() |
| 393 |
.stdout_str_lossy(); |
| 394 |
let fs_types: HashSet<_> = fs_types.lines().skip(1).collect(); |
| 395 |
|
| 396 |
let mut args = Vec::new(); |
| 397 |
|
| 398 |
for fs_type in fs_types { |
| 399 |
args.push("-x"); |
| 400 |
args.push(fs_type.trim_end()); |
| 401 |
} |
| 402 |
|
| 403 |
new_ucmd!() |
| 404 |
.args(&args) |
| 405 |
.fails() |
| 406 |
.stderr_contains("no file systems processed"); |
| 407 |
} |
| 408 |
|
| 409 |
#[test] |
| 410 |
fn test_include_exclude_same_type() { |
| 411 |
new_ucmd!() |
| 412 |
.args(&["-t", "ext4", "-x", "ext4"]) |
| 413 |
.fails() |
| 414 |
.stderr_is("df: file system type 'ext4' both selected and excluded\n"); |
| 415 |
new_ucmd!() |
| 416 |
.args(&["-t", "ext4", "-x", "ext4", "-t", "ext3", "-x", "ext3"]) |
| 417 |
.fails() |
| 418 |
.stderr_is( |
| 419 |
"df: file system type 'ext4' both selected and excluded\n\ |
| 420 |
df: file system type 'ext3' both selected and excluded\n", |
| 421 |
); |
| 422 |
} |
| 423 |
|
| 424 |
#[cfg_attr( |
| 425 |
all(target_arch = "aarch64", target_os = "linux"), |
| 426 |
ignore = "Issue #7158 - Test not supported on ARM64 Linux" |
| 427 |
)] |
| 428 |
#[test] |
| 429 |
fn test_total() { |
| 430 |
// Example output: |
| 431 |
// |
| 432 |
// Filesystem 1K-blocks Used Available Use% Mounted on |
| 433 |
// udev 3858016 0 3858016 0% /dev |
| 434 |
// ... |
| 435 |
// /dev/loop14 63488 63488 0 100% /snap/core20/1361 |
| 436 |
// total 258775268 98099712 148220200 40% - |
| 437 |
// Use explicit numeric-only columns to avoid whitespace issues in Filesystem or Mounted on columns |
| 438 |
let output = new_ucmd!() |
| 439 |
.arg("--output=size,used,avail") |
| 440 |
.arg("--total") |
| 441 |
.succeeds() |
| 442 |
.stdout_str_lossy(); |
| 443 |
|
| 444 |
// Skip the header line. |
| 445 |
let lines: Vec<&str> = output.lines().skip(1).collect(); |
| 446 |
|
| 447 |
// Parse the values from the last row (report totals) |
| 448 |
let last_line = lines.last().unwrap(); |
| 449 |
let mut iter = last_line.split_whitespace(); |
| 450 |
let reported_total_size: u64 = iter.next().unwrap().parse().unwrap(); |
| 451 |
let reported_total_used: u64 = iter.next().unwrap().parse().unwrap(); |
| 452 |
let reported_total_avail: u64 = iter.next().unwrap().parse().unwrap(); |
| 453 |
|
| 454 |
// Loop over each row except the last, computing the sum of each column. |
| 455 |
let mut computed_total_size: u64 = 0; |
| 456 |
let mut computed_total_used: u64 = 0; |
| 457 |
let mut computed_total_avail: u64 = 0; |
| 458 |
let n = lines.len(); |
| 459 |
for line in &lines[..n - 1] { |
| 460 |
let mut iter = line.split_whitespace(); |
| 461 |
computed_total_size += iter.next().unwrap().parse::<u64>().unwrap(); |
| 462 |
computed_total_used += iter.next().unwrap().parse::<u64>().unwrap(); |
| 463 |
computed_total_avail += iter.next().unwrap().parse::<u64>().unwrap(); |
| 464 |
} |
| 465 |
|
| 466 |
// Check that the sum of each column matches the reported value in the last row. |
| 467 |
assert_eq!(computed_total_size, reported_total_size); |
| 468 |
assert_eq!(computed_total_used, reported_total_used); |
| 469 |
assert_eq!(computed_total_avail, reported_total_avail); |
| 470 |
} |
| 471 |
|
| 472 |
/// Test that the "total" label appears in the correct column. |
| 473 |
/// |
| 474 |
/// The "total" label should appear in the "source" column, or in the |
| 475 |
/// "target" column if "source" is not visible. |
| 476 |
#[cfg(not(target_os = "freebsd"))] // FIXME: fix this test for FreeBSD |
| 477 |
#[test] |
| 478 |
fn test_total_label_in_correct_column() { |
| 479 |
let output = new_ucmd!() |
| 480 |
.args(&["--output=source", "--total", "."]) |
| 481 |
.succeeds() |
| 482 |
.stdout_str_lossy(); |
| 483 |
let last_line = output.lines().last().unwrap(); |
| 484 |
assert_eq!(last_line.trim(), "total"); |
| 485 |
|
| 486 |
let output = new_ucmd!() |
| 487 |
.args(&["--output=target", "--total", "."]) |
| 488 |
.succeeds() |
| 489 |
.stdout_str_lossy(); |
| 490 |
let last_line = output.lines().last().unwrap(); |
| 491 |
assert_eq!(last_line.trim(), "total"); |
| 492 |
|
| 493 |
let output = new_ucmd!() |
| 494 |
.args(&["--output=source,target", "--total", "."]) |
| 495 |
.succeeds() |
| 496 |
.stdout_str_lossy(); |
| 497 |
let last_line = output.lines().last().unwrap(); |
| 498 |
assert_eq!( |
| 499 |
last_line.split_whitespace().collect::<Vec<&str>>(), |
| 500 |
vec!["total", "-"] |
| 501 |
); |
| 502 |
|
| 503 |
let output = new_ucmd!() |
| 504 |
.args(&["--output=target,source", "--total", "."]) |
| 505 |
.succeeds() |
| 506 |
.stdout_str_lossy(); |
| 507 |
let last_line = output.lines().last().unwrap(); |
| 508 |
assert_eq!( |
| 509 |
last_line.split_whitespace().collect::<Vec<&str>>(), |
| 510 |
vec!["-", "total"] |
| 511 |
); |
| 512 |
} |
| 513 |
|
| 514 |
#[test] |
| 515 |
fn test_use_percentage() { |
| 516 |
let output = new_ucmd!() |
| 517 |
// set block size = 1, otherwise the returned values for |
| 518 |
// "used" and "avail" will be rounded. And using them to calculate |
| 519 |
// the "percentage" values might lead to a mismatch with the returned |
| 520 |
// "percentage" values. |
| 521 |
.args(&["--total", "--output=used,avail,pcent", "--block-size=1"]) |
| 522 |
.succeeds() |
| 523 |
.stdout_str_lossy(); |
| 524 |
|
| 525 |
// Skip the header line. |
| 526 |
let lines: Vec<&str> = output.lines().skip(1).collect(); |
| 527 |
|
| 528 |
for line in lines { |
| 529 |
let mut iter = line.split_whitespace(); |
| 530 |
let reported_used = iter.next().unwrap().parse::<f64>().unwrap(); |
| 531 |
let reported_avail = iter.next().unwrap().parse::<f64>().unwrap(); |
| 532 |
let reported_percentage = iter.next().unwrap(); |
| 533 |
let reported_percentage = reported_percentage[..reported_percentage.len() - 1] |
| 534 |
.parse::<u8>() |
| 535 |
.unwrap(); |
| 536 |
let computed_percentage = |
| 537 |
(100.0 * (reported_used / (reported_used + reported_avail))).ceil() as u8; |
| 538 |
|
| 539 |
assert_eq!(computed_percentage, reported_percentage); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
#[test] |
| 544 |
fn test_iuse_percentage() { |
| 545 |
let output = new_ucmd!() |
| 546 |
.args(&["--total", "--output=itotal,iused,ipcent"]) |
| 547 |
.succeeds() |
| 548 |
.stdout_str_lossy(); |
| 549 |
|
| 550 |
// Skip the header line. |
| 551 |
let lines: Vec<&str> = output.lines().skip(1).collect(); |
| 552 |
|
| 553 |
for line in lines { |
| 554 |
let mut iter = line.split_whitespace(); |
| 555 |
let reported_inodes = iter.next().unwrap().parse::<f64>().unwrap(); |
| 556 |
let reported_iused = iter.next().unwrap().parse::<f64>().unwrap(); |
| 557 |
let reported_percentage = iter.next().unwrap(); |
| 558 |
|
| 559 |
if reported_percentage == "-" { |
| 560 |
assert_eq!(0.0, reported_inodes); |
| 561 |
assert_eq!(0.0, reported_iused); |
| 562 |
} else { |
| 563 |
let reported_percentage = reported_percentage[..reported_percentage.len() - 1] |
| 564 |
.parse::<u8>() |
| 565 |
.unwrap(); |
| 566 |
let computed_percentage = (100.0 * (reported_iused / reported_inodes)).ceil() as u8; |
| 567 |
|
| 568 |
assert_eq!(computed_percentage, reported_percentage); |
| 569 |
} |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
#[test] |
| 574 |
fn test_default_block_size() { |
| 575 |
let output = new_ucmd!() |
| 576 |
.arg("--output=size") |
| 577 |
.succeeds() |
| 578 |
.stdout_str_lossy(); |
| 579 |
let header = output.lines().next().unwrap().trim().to_string(); |
| 580 |
|
| 581 |
assert_eq!(header, "1K-blocks"); |
| 582 |
|
| 583 |
let output = new_ucmd!() |
| 584 |
.arg("--output=size") |
| 585 |
.env("POSIXLY_CORRECT", "1") |
| 586 |
.succeeds() |
| 587 |
.stdout_str_lossy(); |
| 588 |
let header = output.lines().next().unwrap().trim().to_string(); |
| 589 |
|
| 590 |
assert_eq!(header, "512B-blocks"); |
| 591 |
} |
| 592 |
|
| 593 |
#[test] |
| 594 |
fn test_default_block_size_in_posix_portability_mode() { |
| 595 |
fn get_header(s: &str) -> String { |
| 596 |
s.lines() |
| 597 |
.next() |
| 598 |
.unwrap() |
| 599 |
.to_string() |
| 600 |
.split_whitespace() |
| 601 |
.nth(1) |
| 602 |
.unwrap() |
| 603 |
.trim() |
| 604 |
.to_string() |
| 605 |
} |
| 606 |
|
| 607 |
let output = new_ucmd!().arg("-P").succeeds().stdout_str_lossy(); |
| 608 |
assert_eq!(get_header(&output), "1024-blocks"); |
| 609 |
|
| 610 |
let output = new_ucmd!() |
| 611 |
.arg("-P") |
| 612 |
.env("POSIXLY_CORRECT", "1") |
| 613 |
.succeeds() |
| 614 |
.stdout_str_lossy(); |
| 615 |
assert_eq!(get_header(&output), "512-blocks"); |
| 616 |
} |
| 617 |
|
| 618 |
#[test] |
| 619 |
fn test_block_size_1024() { |
| 620 |
fn get_header(block_size: u64) -> String { |
| 621 |
let output = new_ucmd!() |
| 622 |
.args(&["-B", &format!("{block_size}"), "--output=size"]) |
| 623 |
.succeeds() |
| 624 |
.stdout_str_lossy(); |
| 625 |
output.lines().next().unwrap().trim().to_string() |
| 626 |
} |
| 627 |
|
| 628 |
assert_eq!(get_header(1024), "1K-blocks"); |
| 629 |
assert_eq!(get_header(2048), "2K-blocks"); |
| 630 |
assert_eq!(get_header(4096), "4K-blocks"); |
| 631 |
assert_eq!(get_header(1024 * 1024), "1M-blocks"); |
| 632 |
assert_eq!(get_header(2 * 1024 * 1024), "2M-blocks"); |
| 633 |
assert_eq!(get_header(1024 * 1024 * 1024), "1G-blocks"); |
| 634 |
assert_eq!(get_header(34 * 1024 * 1024 * 1024), "34G-blocks"); |
| 635 |
|
| 636 |
// multiples of both 1024 and 1000 |
| 637 |
assert_eq!(get_header(128_000), "128kB-blocks"); |
| 638 |
assert_eq!(get_header(1000 * 1024), "1.1MB-blocks"); |
| 639 |
assert_eq!(get_header(1_000_000_000_000), "1TB-blocks"); |
| 640 |
} |
| 641 |
|
| 642 |
#[test] |
| 643 |
fn test_block_size_with_suffix() { |
| 644 |
fn get_header(block_size: &str) -> String { |
| 645 |
let output = new_ucmd!() |
| 646 |
.args(&["-B", block_size, "--output=size"]) |
| 647 |
.succeeds() |
| 648 |
.stdout_str_lossy(); |
| 649 |
output.lines().next().unwrap().trim().to_string() |
| 650 |
} |
| 651 |
|
| 652 |
assert_eq!(get_header("K"), "1K-blocks"); |
| 653 |
assert_eq!(get_header("M"), "1M-blocks"); |
| 654 |
assert_eq!(get_header("G"), "1G-blocks"); |
| 655 |
assert_eq!(get_header("1K"), "1K-blocks"); |
| 656 |
assert_eq!(get_header("1M"), "1M-blocks"); |
| 657 |
assert_eq!(get_header("1G"), "1G-blocks"); |
| 658 |
assert_eq!(get_header("1KiB"), "1K-blocks"); |
| 659 |
assert_eq!(get_header("1MiB"), "1M-blocks"); |
| 660 |
assert_eq!(get_header("1GiB"), "1G-blocks"); |
| 661 |
assert_eq!(get_header("1KB"), "1kB-blocks"); |
| 662 |
assert_eq!(get_header("1MB"), "1MB-blocks"); |
| 663 |
assert_eq!(get_header("1GB"), "1GB-blocks"); |
| 664 |
} |
| 665 |
|
| 666 |
#[test] |
| 667 |
fn test_df_binary_block_size() { |
| 668 |
fn get_header(block_size: &str) -> String { |
| 669 |
let output = new_ucmd!() |
| 670 |
.args(&["-B", block_size, "--output=size"]) |
| 671 |
.succeeds() |
| 672 |
.stdout_str_lossy(); |
| 673 |
output.lines().next().unwrap().trim().to_string() |
| 674 |
} |
| 675 |
|
| 676 |
let test_cases = [ |
| 677 |
("0b1", "1"), |
| 678 |
("0b10100", "20"), |
| 679 |
("0b1000000000", "512"), |
| 680 |
("0b10K", "2K"), |
| 681 |
]; |
| 682 |
|
| 683 |
for (binary, decimal) in test_cases { |
| 684 |
let binary_result = get_header(binary); |
| 685 |
let decimal_result = get_header(decimal); |
| 686 |
assert_eq!( |
| 687 |
binary_result, decimal_result, |
| 688 |
"Binary {binary} should equal decimal {decimal}" |
| 689 |
); |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
#[test] |
| 694 |
fn test_df_binary_env_block_size() { |
| 695 |
fn get_header(env_var: &str, env_value: &str) -> String { |
| 696 |
let output = new_ucmd!() |
| 697 |
.env(env_var, env_value) |
| 698 |
.args(&["--output=size"]) |
| 699 |
.succeeds() |
| 700 |
.stdout_str_lossy(); |
| 701 |
output.lines().next().unwrap().trim().to_string() |
| 702 |
} |
| 703 |
|
| 704 |
let binary_header = get_header("DF_BLOCK_SIZE", "0b10000000000"); |
| 705 |
let decimal_header = get_header("DF_BLOCK_SIZE", "1024"); |
| 706 |
assert_eq!(binary_header, decimal_header); |
| 707 |
|
| 708 |
let binary_header = get_header("BLOCK_SIZE", "0b10000000000"); |
| 709 |
let decimal_header = get_header("BLOCK_SIZE", "1024"); |
| 710 |
assert_eq!(binary_header, decimal_header); |
| 711 |
} |
| 712 |
|
| 713 |
#[test] |
| 714 |
fn test_block_size_in_posix_portability_mode() { |
| 715 |
fn get_header(block_size: &str) -> String { |
| 716 |
let output = new_ucmd!() |
| 717 |
.args(&["-P", "-B", block_size]) |
| 718 |
.succeeds() |
| 719 |
.stdout_str_lossy(); |
| 720 |
output |
| 721 |
.lines() |
| 722 |
.next() |
| 723 |
.unwrap() |
| 724 |
.to_string() |
| 725 |
.split_whitespace() |
| 726 |
.nth(1) |
| 727 |
.unwrap() |
| 728 |
.trim() |
| 729 |
.to_string() |
| 730 |
} |
| 731 |
|
| 732 |
assert_eq!(get_header("1024"), "1024-blocks"); |
| 733 |
assert_eq!(get_header("1K"), "1024-blocks"); |
| 734 |
assert_eq!(get_header("1KB"), "1000-blocks"); |
| 735 |
assert_eq!(get_header("1M"), "1048576-blocks"); |
| 736 |
assert_eq!(get_header("1MB"), "1000000-blocks"); |
| 737 |
} |
| 738 |
|
| 739 |
#[test] |
| 740 |
fn test_block_size_from_env() { |
| 741 |
fn get_header(env_var: &str, env_value: &str) -> String { |
| 742 |
let output = new_ucmd!() |
| 743 |
.arg("--output=size") |
| 744 |
.env(env_var, env_value) |
| 745 |
.succeeds() |
| 746 |
.stdout_str_lossy(); |
| 747 |
output.lines().next().unwrap().trim().to_string() |
| 748 |
} |
| 749 |
|
| 750 |
assert_eq!(get_header("DF_BLOCK_SIZE", "111"), "111B-blocks"); |
| 751 |
assert_eq!(get_header("BLOCK_SIZE", "222"), "222B-blocks"); |
| 752 |
assert_eq!(get_header("BLOCKSIZE", "333"), "333B-blocks"); |
| 753 |
} |
| 754 |
|
| 755 |
#[test] |
| 756 |
fn test_block_size_from_env_zero() { |
| 757 |
fn get_header(env_var: &str, env_value: &str) -> String { |
| 758 |
let output = new_ucmd!() |
| 759 |
.arg("--output=size") |
| 760 |
.env(env_var, env_value) |
| 761 |
.succeeds() |
| 762 |
.stdout_str_lossy(); |
| 763 |
output.lines().next().unwrap().trim().to_string() |
| 764 |
} |
| 765 |
|
| 766 |
let default_block_size_header = "1K-blocks"; |
| 767 |
|
| 768 |
assert_eq!(get_header("DF_BLOCK_SIZE", "0"), default_block_size_header); |
| 769 |
assert_eq!(get_header("BLOCK_SIZE", "0"), default_block_size_header); |
| 770 |
assert_eq!(get_header("BLOCKSIZE", "0"), default_block_size_header); |
| 771 |
} |
| 772 |
|
| 773 |
#[test] |
| 774 |
fn test_block_size_from_env_precedences() { |
| 775 |
fn get_header(one: (&str, &str), two: (&str, &str)) -> String { |
| 776 |
let (k1, v1) = one; |
| 777 |
let (k2, v2) = two; |
| 778 |
let output = new_ucmd!() |
| 779 |
.arg("--output=size") |
| 780 |
.env(k1, v1) |
| 781 |
.env(k2, v2) |
| 782 |
.succeeds() |
| 783 |
.stdout_str_lossy(); |
| 784 |
output.lines().next().unwrap().trim().to_string() |
| 785 |
} |
| 786 |
|
| 787 |
let df_block_size = ("DF_BLOCK_SIZE", "111"); |
| 788 |
let block_size = ("BLOCK_SIZE", "222"); |
| 789 |
let blocksize = ("BLOCKSIZE", "333"); |
| 790 |
|
| 791 |
assert_eq!(get_header(df_block_size, block_size), "111B-blocks"); |
| 792 |
assert_eq!(get_header(df_block_size, blocksize), "111B-blocks"); |
| 793 |
assert_eq!(get_header(block_size, blocksize), "222B-blocks"); |
| 794 |
} |
| 795 |
|
| 796 |
#[test] |
| 797 |
fn test_precedence_of_block_size_arg_over_env() { |
| 798 |
let output = new_ucmd!() |
| 799 |
.args(&["-B", "999", "--output=size"]) |
| 800 |
.env("DF_BLOCK_SIZE", "111") |
| 801 |
.succeeds() |
| 802 |
.stdout_str_lossy(); |
| 803 |
let header = output.lines().next().unwrap().trim().to_string(); |
| 804 |
|
| 805 |
assert_eq!(header, "999B-blocks"); |
| 806 |
} |
| 807 |
|
| 808 |
#[test] |
| 809 |
fn test_invalid_block_size_from_env() { |
| 810 |
let default_block_size_header = "1K-blocks"; |
| 811 |
|
| 812 |
let output = new_ucmd!() |
| 813 |
.arg("--output=size") |
| 814 |
.env("DF_BLOCK_SIZE", "invalid") |
| 815 |
.succeeds() |
| 816 |
.stdout_str_lossy(); |
| 817 |
let header = output.lines().next().unwrap().trim().to_string(); |
| 818 |
|
| 819 |
assert_eq!(header, default_block_size_header); |
| 820 |
|
| 821 |
let output = new_ucmd!() |
| 822 |
.arg("--output=size") |
| 823 |
.env("DF_BLOCK_SIZE", "invalid") |
| 824 |
.env("BLOCK_SIZE", "222") |
| 825 |
.succeeds() |
| 826 |
.stdout_str_lossy(); |
| 827 |
let header = output.lines().next().unwrap().trim().to_string(); |
| 828 |
|
| 829 |
assert_eq!(header, default_block_size_header); |
| 830 |
|
| 831 |
let output = new_ucmd!() |
| 832 |
.arg("--output=size") |
| 833 |
.env("DF_BLOCK_SIZE", "0") |
| 834 |
.env("BLOCK_SIZE", "222") |
| 835 |
.succeeds() |
| 836 |
.stdout_str_lossy(); |
| 837 |
let header = output.lines().next().unwrap().trim().to_string(); |
| 838 |
|
| 839 |
assert_eq!(header, default_block_size_header); |
| 840 |
} |
| 841 |
|
| 842 |
#[test] |
| 843 |
fn test_ignore_block_size_from_env_in_posix_portability_mode() { |
| 844 |
let default_block_size_header = "1024-blocks"; |
| 845 |
|
| 846 |
let output = new_ucmd!() |
| 847 |
.arg("-P") |
| 848 |
.env("DF_BLOCK_SIZE", "111") |
| 849 |
.env("BLOCK_SIZE", "222") |
| 850 |
.env("BLOCKSIZE", "333") |
| 851 |
.succeeds() |
| 852 |
.stdout_str_lossy(); |
| 853 |
let header = output |
| 854 |
.lines() |
| 855 |
.next() |
| 856 |
.unwrap() |
| 857 |
.to_string() |
| 858 |
.split_whitespace() |
| 859 |
.nth(1) |
| 860 |
.unwrap() |
| 861 |
.trim() |
| 862 |
.to_string(); |
| 863 |
|
| 864 |
assert_eq!(header, default_block_size_header); |
| 865 |
} |
| 866 |
|
| 867 |
#[test] |
| 868 |
fn test_too_large_block_size() { |
| 869 |
fn run_command(size: &str) { |
| 870 |
new_ucmd!() |
| 871 |
.arg(format!("--block-size={size}")) |
| 872 |
.fails() |
| 873 |
.stderr_contains(format!("--block-size argument '{size}' too large")); |
| 874 |
} |
| 875 |
|
| 876 |
let too_large_sizes = vec!["1Y", "1Z"]; |
| 877 |
|
| 878 |
for size in too_large_sizes { |
| 879 |
run_command(size); |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
#[test] |
| 884 |
fn test_invalid_block_size() { |
| 885 |
new_ucmd!() |
| 886 |
.arg("--block-size=x") |
| 887 |
.fails() |
| 888 |
.stderr_contains("invalid --block-size argument 'x'"); |
| 889 |
|
| 890 |
new_ucmd!() |
| 891 |
.arg("--block-size=0") |
| 892 |
.fails() |
| 893 |
.stderr_contains("invalid --block-size argument '0'"); |
| 894 |
|
| 895 |
new_ucmd!() |
| 896 |
.arg("--block-size=0K") |
| 897 |
.fails() |
| 898 |
.stderr_contains("invalid --block-size argument '0K'"); |
| 899 |
} |
| 900 |
|
| 901 |
#[test] |
| 902 |
fn test_invalid_block_size_suffix() { |
| 903 |
new_ucmd!() |
| 904 |
.arg("--block-size=1H") |
| 905 |
.fails() |
| 906 |
.stderr_contains("invalid suffix in --block-size argument '1H'"); |
| 907 |
|
| 908 |
new_ucmd!() |
| 909 |
.arg("--block-size=1.2") |
| 910 |
.fails() |
| 911 |
.stderr_contains("invalid suffix in --block-size argument '1.2'"); |
| 912 |
} |
| 913 |
|
| 914 |
#[test] |
| 915 |
fn test_df_invalid_binary_size() { |
| 916 |
new_ucmd!() |
| 917 |
.arg("--block-size=0b123") |
| 918 |
.fails() |
| 919 |
.stderr_contains("invalid suffix in --block-size argument '0b123'"); |
| 920 |
} |
| 921 |
|
| 922 |
#[test] |
| 923 |
fn test_df_binary_edge_cases() { |
| 924 |
new_ucmd!() |
| 925 |
.arg("-B0b") |
| 926 |
.fails() |
| 927 |
.stderr_contains("invalid --block-size argument '0b'"); |
| 928 |
|
| 929 |
new_ucmd!() |
| 930 |
.arg("-B0B") |
| 931 |
.fails() |
| 932 |
.stderr_contains("invalid suffix in --block-size argument '0B'"); |
| 933 |
|
| 934 |
new_ucmd!() |
| 935 |
.arg("--block-size=0b1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111") |
| 936 |
.fails() |
| 937 |
.stderr_contains("too large"); |
| 938 |
} |
| 939 |
|
| 940 |
#[test] |
| 941 |
fn test_output_selects_columns() { |
| 942 |
let output = new_ucmd!() |
| 943 |
.args(&["--output=source"]) |
| 944 |
.succeeds() |
| 945 |
.stdout_str_lossy(); |
| 946 |
assert_eq!(output.lines().next().unwrap(), "Filesystem"); |
| 947 |
|
| 948 |
let output = new_ucmd!() |
| 949 |
.args(&["--output=source,target"]) |
| 950 |
.succeeds() |
| 951 |
.stdout_str_lossy(); |
| 952 |
assert_eq!( |
| 953 |
output |
| 954 |
.lines() |
| 955 |
.next() |
| 956 |
.unwrap() |
| 957 |
.split_whitespace() |
| 958 |
.collect::<Vec<_>>(), |
| 959 |
vec!["Filesystem", "Mounted", "on"] |
| 960 |
); |
| 961 |
|
| 962 |
let output = new_ucmd!() |
| 963 |
.args(&["--output=source,target,used"]) |
| 964 |
.succeeds() |
| 965 |
.stdout_str_lossy(); |
| 966 |
assert_eq!( |
| 967 |
output |
| 968 |
.lines() |
| 969 |
.next() |
| 970 |
.unwrap() |
| 971 |
.split_whitespace() |
| 972 |
.collect::<Vec<_>>(), |
| 973 |
vec!["Filesystem", "Mounted", "on", "Used"] |
| 974 |
); |
| 975 |
} |
| 976 |
|
| 977 |
#[test] |
| 978 |
fn test_output_multiple_occurrences() { |
| 979 |
let output = new_ucmd!() |
| 980 |
.args(&["--output=source", "--output=target"]) |
| 981 |
.succeeds() |
| 982 |
.stdout_str_lossy(); |
| 983 |
assert_eq!( |
| 984 |
output |
| 985 |
.lines() |
| 986 |
.next() |
| 987 |
.unwrap() |
| 988 |
.split_whitespace() |
| 989 |
.collect::<Vec<_>>(), |
| 990 |
vec!["Filesystem", "Mounted", "on"] |
| 991 |
); |
| 992 |
} |
| 993 |
|
| 994 |
#[test] |
| 995 |
fn test_output_file_all_filesystems() { |
| 996 |
// When run with no positional arguments, `df` lets "-" represent |
| 997 |
// the "File" entry for each row. |
| 998 |
let output = new_ucmd!() |
| 999 |
.arg("--output=file") |
| 1000 |
.succeeds() |
| 1001 |
.stdout_str_lossy(); |
| 1002 |
let mut lines = output.lines(); |
| 1003 |
assert_eq!(lines.next().unwrap(), "File"); |
| 1004 |
for line in lines { |
| 1005 |
assert_eq!(line, "-"); |
| 1006 |
} |
| 1007 |
} |
| 1008 |
|
| 1009 |
#[test] |
| 1010 |
#[cfg(not(any(target_os = "freebsd", windows)))] // FIXME: fix test for FreeBSD & Win |
| 1011 |
fn test_output_file_specific_files() { |
| 1012 |
// Create three files. |
| 1013 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1014 |
at.touch("a"); |
| 1015 |
at.touch("b"); |
| 1016 |
at.touch("c"); |
| 1017 |
|
| 1018 |
// When run with positional arguments, the filesystems should |
| 1019 |
// appear in the "File" column. |
| 1020 |
let output = ucmd |
| 1021 |
.args(&["--output=file", "a", "b", "c"]) |
| 1022 |
.succeeds() |
| 1023 |
.stdout_str_lossy(); |
| 1024 |
let actual: Vec<&str> = output.lines().collect(); |
| 1025 |
assert_eq!(actual, vec!["File", "a", "b", "c"]); |
| 1026 |
} |
| 1027 |
|
| 1028 |
#[test] |
| 1029 |
#[cfg(not(any(target_os = "freebsd", windows)))] // FIXME: fix test for FreeBSD & Win |
| 1030 |
fn test_file_column_width_if_filename_contains_unicode_chars() { |
| 1031 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1032 |
at.touch("äöü.txt"); |
| 1033 |
|
| 1034 |
let output = ucmd |
| 1035 |
.args(&["--output=file,target", "äöü.txt"]) |
| 1036 |
.succeeds() |
| 1037 |
.stdout_str_lossy(); |
| 1038 |
let actual = output.lines().next().unwrap(); |
| 1039 |
// expected width: 7 chars (length of äöü.txt) + 1 char (column separator) |
| 1040 |
assert_eq!(actual, "File Mounted on"); |
| 1041 |
} |
| 1042 |
|
| 1043 |
#[test] |
| 1044 |
fn test_output_field_no_more_than_once() { |
| 1045 |
new_ucmd!() |
| 1046 |
.arg("--output=target,source,target") |
| 1047 |
.fails() |
| 1048 |
.usage_error("option --output: field 'target' used more than once"); |
| 1049 |
} |
| 1050 |
|
| 1051 |
#[test] |
| 1052 |
#[cfg(not(any(target_os = "freebsd", windows)))] // FIXME: fix test for FreeBSD & Win |
| 1053 |
fn test_nonexistent_file() { |
| 1054 |
new_ucmd!() |
| 1055 |
.arg("does-not-exist") |
| 1056 |
.fails() |
| 1057 |
.stderr_only("df: does-not-exist: No such file or directory\n"); |
| 1058 |
new_ucmd!() |
| 1059 |
.args(&["--output=file", "does-not-exist", "."]) |
| 1060 |
.fails() |
| 1061 |
.stderr_is("df: does-not-exist: No such file or directory\n") |
| 1062 |
.stdout_is("File\n.\n"); |
| 1063 |
} |
| 1064 |
|
| 1065 |
#[test] |
| 1066 |
#[cfg(target_os = "linux")] |
| 1067 |
fn test_df_all_shows_binfmt_misc() { |
| 1068 |
// Check if binfmt_misc is mounted |
| 1069 |
let is_mounted = std::fs::read_to_string("/proc/self/mountinfo") |
| 1070 |
.is_ok_and(|content| content.lines().any(|line| line.contains("binfmt_misc"))); |
| 1071 |
|
| 1072 |
if is_mounted { |
| 1073 |
let output = new_ucmd!() |
| 1074 |
.args(&["--all", "--output=fstype,target"]) |
| 1075 |
.succeeds() |
| 1076 |
.stdout_str_lossy(); |
| 1077 |
|
| 1078 |
assert!( |
| 1079 |
output.contains("binfmt_misc"), |
| 1080 |
"Expected binfmt_misc filesystem to appear in df --all output when it's mounted" |
| 1081 |
); |
| 1082 |
} |
| 1083 |
// If binfmt_misc is not mounted, skip the test silently |
| 1084 |
} |
| 1085 |
|
| 1086 |
#[test] |
| 1087 |
#[cfg(target_os = "linux")] |
| 1088 |
fn test_df_hides_binfmt_misc_by_default() { |
| 1089 |
// Check if binfmt_misc is mounted |
| 1090 |
let is_mounted = std::fs::read_to_string("/proc/self/mountinfo") |
| 1091 |
.is_ok_and(|content| content.lines().any(|line| line.contains("binfmt_misc"))); |
| 1092 |
|
| 1093 |
if is_mounted { |
| 1094 |
let output = new_ucmd!() |
| 1095 |
.args(&["--output=fstype,target"]) |
| 1096 |
.succeeds() |
| 1097 |
.stdout_str_lossy(); |
| 1098 |
|
| 1099 |
// binfmt_misc should NOT appear in the output without --all |
| 1100 |
assert!( |
| 1101 |
!output.contains("binfmt_misc"), |
| 1102 |
"Expected binfmt_misc filesystem to be hidden in df output without --all" |
| 1103 |
); |
| 1104 |
} |
| 1105 |
// If binfmt_misc is not mounted, skip the test silently |
| 1106 |
} |
| 1107 |
|
| 1108 |
/// Run df inside a mount namespace where /proc is masked with tmpfs. |
| 1109 |
/// Returns (success, stdout, stderr). |
| 1110 |
#[cfg(target_os = "linux")] |
| 1111 |
fn run_df_with_masked_proc(args: &str) -> Option<(bool, String, String)> { |
| 1112 |
use std::process::Command; |
| 1113 |
|
| 1114 |
// Check if user namespaces are available |
| 1115 |
Command::new("unshare") |
| 1116 |
.args(["-rm", "true"]) |
| 1117 |
.status() |
| 1118 |
.ok() |
| 1119 |
.filter(std::process::ExitStatus::success)?; |
| 1120 |
|
| 1121 |
let df_path = TestScenario::new("df").bin_path.clone(); |
| 1122 |
let output = Command::new("unshare") |
| 1123 |
.args(["-rm", "sh", "-c"]) |
| 1124 |
.arg(format!( |
| 1125 |
"mount -t tmpfs tmpfs /proc && {} df {args}", |
| 1126 |
df_path.display() |
| 1127 |
)) |
| 1128 |
.output() |
| 1129 |
.ok()?; |
| 1130 |
|
| 1131 |
Some(( |
| 1132 |
output.status.success(), |
| 1133 |
String::from_utf8_lossy(&output.stdout).to_string(), |
| 1134 |
String::from_utf8_lossy(&output.stderr).to_string(), |
| 1135 |
)) |
| 1136 |
} |
| 1137 |
|
| 1138 |
/// Test df fallback when /proc is masked - should work with path, fail without or with filters. |
| 1139 |
#[test] |
| 1140 |
#[cfg(target_os = "linux")] |
| 1141 |
fn test_df_masked_proc_fallback() { |
| 1142 |
if let Some((ok, stdout, stderr)) = run_df_with_masked_proc(".") { |
| 1143 |
assert!(ok, "df . should succeed: {stderr}"); |
| 1144 |
assert!(stderr.contains("cannot read table of mounted file systems")); |
| 1145 |
assert!(stdout.contains("Filesystem")); |
| 1146 |
} |
| 1147 |
|
| 1148 |
if let Some((ok, _, _)) = run_df_with_masked_proc("") { |
| 1149 |
assert!(!ok, "df without args should fail when /proc is masked"); |
| 1150 |
} |
| 1151 |
|
| 1152 |
for args in ["-a .", "-l .", "-t ext4 .", "-x tmpfs ."] { |
| 1153 |
if let Some((ok, _, _)) = run_df_with_masked_proc(args) { |
| 1154 |
assert!(!ok, "df {args} should fail when /proc is masked"); |
| 1155 |
} |
| 1156 |
} |
| 1157 |
|
| 1158 |
for args in ["-i .", "-T .", "--total ."] { |
| 1159 |
if let Some((ok, _, stderr)) = run_df_with_masked_proc(args) { |
| 1160 |
assert!(ok, "df {args} should succeed: {stderr}"); |
| 1161 |
} |
| 1162 |
} |
| 1163 |
} |
| 1164 |
|
| 1165 |
#[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))] |
| 1166 |
mod diagnostics { |
| 1167 |
use super::*; |
| 1168 |
|
| 1169 |
#[cfg(unix)] |
| 1170 |
#[test] |
| 1171 |
fn test_snippet_points_at_the_unknown_unit_of_block_size() { |
| 1172 |
let result = new_ucmd!() |
| 1173 |
.terminal_sim_stderr() |
| 1174 |
.args(&["-B", "1fb"]) |
| 1175 |
.fails_with_code(1); |
| 1176 |
|
| 1177 |
assert_eq!( |
| 1178 |
result.stderr_as_displayed(), |
| 1179 |
"\ |
| 1180 |
df: invalid suffix in --block-size argument '1fb' |
| 1181 |
╭─[ df:1:8 ] |
| 1182 |
│ |
| 1183 |
1 │ df -B 1fb |
| 1184 |
│ ─┬ |
| 1185 |
│ ╰── not a known unit |
| 1186 |
│ |
| 1187 |
│ Help: a size is a number and an optional unit: K, M, G and so on for 1024, KB, MB, GB for 1000 |
| 1188 |
───╯" |
| 1189 |
); |
| 1190 |
} |
| 1191 |
|
| 1192 |
#[cfg(unix)] |
| 1193 |
#[test] |
| 1194 |
fn test_snippet_underlines_a_zero_block_size() { |
| 1195 |
let result = new_ucmd!() |
| 1196 |
.terminal_sim_stderr() |
| 1197 |
.arg("--block-size=0") |
| 1198 |
.fails_with_code(1); |
| 1199 |
let stderr = result.stderr_as_displayed(); |
| 1200 |
|
| 1201 |
assert_eq!(result.caret_column(), Some(17)); |
| 1202 |
assert!(!stderr.contains("not a known unit"), "{stderr}"); |
| 1203 |
} |
| 1204 |
|
| 1205 |
#[test] |
| 1206 |
fn test_plain_message_when_stderr_is_a_pipe() { |
| 1207 |
new_ucmd!() |
| 1208 |
.args(&["-B", "1fb"]) |
| 1209 |
.fails_with_code(1) |
| 1210 |
.stderr_is("df: invalid suffix in --block-size argument '1fb'\n"); |
| 1211 |
} |
| 1212 |
} |