| 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 fname, tname, fpath, specfile, testfile, unspec, ifile, ofile, outfile, fullblock, urand, fileio, atoe, atoibm, availible, behaviour, bmax, bremain, btotal, cflags, creat, ctable, ctty, datastructures, doesnt, etoa, fileout, fname, gnudd, iconvflags, iseek, nocache, noctty, noerror, nofollow, nolinks, nonblock, oconvflags, oseek, outfile, parseargs, rlen, rmax, rposition, rremain, rsofar, rstat, sigusr, sigval, wlen, wstat abcdefghijklm abcdefghi nabcde nabcdefg abcdefg fifoname FADV DONTNEED FSIZE SIGXFSZ sighandler |
| 7 |
|
| 8 |
use uutests::at_and_ucmd; |
| 9 |
use uutests::new_ucmd; |
| 10 |
use uutests::util::TestScenario; |
| 11 |
#[cfg(all(unix, not(feature = "selinux")))] |
| 12 |
use uutests::util::run_ucmd_as_root_with_stdin_stdout; |
| 13 |
#[cfg(not(windows))] |
| 14 |
use uutests::util::{UCommand, get_tests_binary}; |
| 15 |
use uutests::util_name; |
| 16 |
|
| 17 |
use regex::Regex; |
| 18 |
use uucore::io::OwnedFileDescriptorOrHandle; |
| 19 |
|
| 20 |
use std::fs::{File, OpenOptions}; |
| 21 |
use std::io::{BufReader, Read, Write}; |
| 22 |
use std::path::PathBuf; |
| 23 |
#[cfg(all(unix, not(target_vendor = "apple"), not(target_os = "freebsd"),))] |
| 24 |
use std::process::Command; |
| 25 |
use std::process::Stdio; |
| 26 |
#[cfg(not(windows))] |
| 27 |
use std::thread::sleep; |
| 28 |
#[cfg(not(windows))] |
| 29 |
use std::time::Duration; |
| 30 |
use tempfile::tempfile; |
| 31 |
|
| 32 |
macro_rules! inf { |
| 33 |
($fname:expr) => { |
| 34 |
format!("if={}", $fname) |
| 35 |
}; |
| 36 |
} |
| 37 |
|
| 38 |
macro_rules! of { |
| 39 |
($fname:expr) => { |
| 40 |
format!("of={}", $fname) |
| 41 |
}; |
| 42 |
} |
| 43 |
|
| 44 |
macro_rules! fixture_path { |
| 45 |
($fname:expr) => {{ PathBuf::from(format!("./tests/fixtures/dd/{}", $fname)) }}; |
| 46 |
} |
| 47 |
|
| 48 |
macro_rules! assert_fixture_exists { |
| 49 |
($fname:expr) => {{ |
| 50 |
let fpath = fixture_path!($fname); |
| 51 |
assert!(fpath.exists(), "Fixture missing: {fpath:?}"); |
| 52 |
}}; |
| 53 |
} |
| 54 |
|
| 55 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 56 |
macro_rules! assert_fixture_not_exists { |
| 57 |
($fname:expr) => {{ |
| 58 |
let fpath = PathBuf::from(format!("./fixtures/dd/{}", $fname)); |
| 59 |
assert!(!fpath.exists(), "Fixture present: {fpath:?}"); |
| 60 |
}}; |
| 61 |
} |
| 62 |
|
| 63 |
macro_rules! build_test_file { |
| 64 |
($fp:expr, $data:expr) => {{ |
| 65 |
OpenOptions::new() |
| 66 |
.write(true) |
| 67 |
.create(true) |
| 68 |
.truncate(true) |
| 69 |
.open($fp) |
| 70 |
.unwrap() |
| 71 |
.write_all($data) |
| 72 |
.unwrap() |
| 73 |
}}; |
| 74 |
} |
| 75 |
|
| 76 |
macro_rules! cmp_file ( |
| 77 |
($spec:expr, $test:expr) => |
| 78 |
{ |
| 79 |
let specfile_len = $spec.metadata().unwrap().len(); |
| 80 |
let testfile_len = $test.metadata().unwrap().len(); |
| 81 |
assert_eq!(testfile_len, specfile_len); |
| 82 |
|
| 83 |
let spec = BufReader::new($spec); |
| 84 |
let test = BufReader::new($test); |
| 85 |
|
| 86 |
for (b_spec, b_test) in spec.bytes().zip(test.bytes()) |
| 87 |
{ |
| 88 |
assert_eq!(b_spec.unwrap(), |
| 89 |
b_test.unwrap()); |
| 90 |
} |
| 91 |
}; |
| 92 |
); |
| 93 |
|
| 94 |
fn build_ascii_block(n: usize) -> Vec<u8> { |
| 95 |
(0..=127).cycle().take(n).collect() |
| 96 |
} |
| 97 |
|
| 98 |
#[test] |
| 99 |
fn test_invalid_arg() { |
| 100 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 101 |
} |
| 102 |
|
| 103 |
// Sanity Tests |
| 104 |
#[test] |
| 105 |
fn version() { |
| 106 |
new_ucmd!().args(&["--version"]).succeeds(); |
| 107 |
} |
| 108 |
|
| 109 |
#[test] |
| 110 |
fn help() { |
| 111 |
new_ucmd!() |
| 112 |
.arg("--help") |
| 113 |
.succeeds() |
| 114 |
.stdout_contains("\nOperands:\n") |
| 115 |
.stdout_contains("\nConversion options:\n") |
| 116 |
.stdout_does_not_contain("###") |
| 117 |
.stdout_does_not_contain("```"); |
| 118 |
} |
| 119 |
|
| 120 |
#[test] |
| 121 |
fn test_out_of_memory() { |
| 122 |
new_ucmd!() |
| 123 |
.arg("bs=1PB") |
| 124 |
.fails_with_code(1) |
| 125 |
.stderr_contains("memory"); //todo: improve error message at all platforms |
| 126 |
} |
| 127 |
|
| 128 |
#[test] |
| 129 |
fn test_out_of_memory_skip() { |
| 130 |
new_ucmd!() |
| 131 |
.arg("bs=1PB") |
| 132 |
.arg("skip=1") |
| 133 |
.fails_with_code(1) |
| 134 |
.stderr_contains("memory"); |
| 135 |
} |
| 136 |
|
| 137 |
#[test] |
| 138 |
fn test_huge_block_size_is_rejected_without_panicking() { |
| 139 |
// Regression test for #12844: a block size >= i64::MAX used to panic |
| 140 |
// ("attempt to multiply with overflow"); it must be rejected cleanly. |
| 141 |
for arg in [ |
| 142 |
"bs=9223372036854775807", // i64::MAX |
| 143 |
"ibs=99999999999999999999", // larger than u64::MAX |
| 144 |
"obs=18446744073709551616", // u64::MAX + 1 |
| 145 |
"cbs=9223372036854775808", // i64::MAX + 1 |
| 146 |
"ibs=1778172772721772786161B", // overflows via the 'B' suffix |
| 147 |
] { |
| 148 |
new_ucmd!() |
| 149 |
.arg(arg) |
| 150 |
.fails_with_code(1) |
| 151 |
.no_stdout() |
| 152 |
.stderr_contains("Value too large for defined data type"); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
#[test] |
| 157 |
fn test_count_past_u64_is_rejected() { |
| 158 |
// A number past u64 used to saturate, so count= silently copied the whole |
| 159 |
// input and exited 0 where GNU rejects the operand. |
| 160 |
for arg in ["count=99999999999999999999", "skip=18446744073709551616"] { |
| 161 |
new_ucmd!() |
| 162 |
.args(&["if=/dev/null", arg]) |
| 163 |
.fails_with_code(1) |
| 164 |
.stderr_contains("Value too large for defined data type"); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
#[test] |
| 169 |
fn test_huge_obs_reports_memory_error_instead_of_aborting() { |
| 170 |
// Regression test for #12847: a valid but huge `obs` used to abort |
| 171 |
// ("memory allocation of N bytes failed"); it must fail gracefully instead. |
| 172 |
new_ucmd!() |
| 173 |
.arg("obs=1PB") |
| 174 |
.fails_with_code(1) |
| 175 |
.stderr_contains("memory"); |
| 176 |
} |
| 177 |
|
| 178 |
#[test] |
| 179 |
fn test_stdin_stdout() { |
| 180 |
let input = build_ascii_block(521); |
| 181 |
let output = String::from_utf8(input.clone()).unwrap(); |
| 182 |
new_ucmd!() |
| 183 |
.args(&["status=none"]) |
| 184 |
.pipe_in(input) |
| 185 |
.succeeds() |
| 186 |
.stdout_only(output); |
| 187 |
} |
| 188 |
|
| 189 |
// Top-Level Items |
| 190 |
// count=N, skip=N, status=LEVEL, conv=FLAG, *flag=FLAG |
| 191 |
#[test] |
| 192 |
fn test_stdin_stdout_count() { |
| 193 |
let input = build_ascii_block(521); |
| 194 |
let mut output = String::from_utf8(input.clone()).unwrap(); |
| 195 |
output.truncate(256); |
| 196 |
new_ucmd!() |
| 197 |
.args(&["status=none", "count=2", "ibs=128"]) |
| 198 |
.pipe_in(input) |
| 199 |
.succeeds() |
| 200 |
.stdout_only(output); |
| 201 |
} |
| 202 |
|
| 203 |
#[test] |
| 204 |
fn test_stdin_stdout_count_bytes() { |
| 205 |
let input = build_ascii_block(521); |
| 206 |
let mut output = String::from_utf8(input.clone()).unwrap(); |
| 207 |
output.truncate(256); |
| 208 |
new_ucmd!() |
| 209 |
.args(&["status=none", "count=256", "iflag=count_bytes"]) |
| 210 |
.pipe_in(input) |
| 211 |
.succeeds() |
| 212 |
.stdout_only(output); |
| 213 |
} |
| 214 |
|
| 215 |
#[test] |
| 216 |
fn test_stdin_stdout_skip() { |
| 217 |
let input = build_ascii_block(521); |
| 218 |
let mut output = String::from_utf8(input.clone()).unwrap(); |
| 219 |
let _ = output.drain(..256); |
| 220 |
new_ucmd!() |
| 221 |
.args(&["status=none", "skip=2", "ibs=128"]) |
| 222 |
.pipe_in(input) |
| 223 |
.succeeds() |
| 224 |
.stdout_only(output); |
| 225 |
} |
| 226 |
|
| 227 |
#[test] |
| 228 |
fn test_stdin_stdout_skip_bytes() { |
| 229 |
let input = build_ascii_block(521); |
| 230 |
let mut output = String::from_utf8(input.clone()).unwrap(); |
| 231 |
let _ = output.drain(..256); |
| 232 |
new_ucmd!() |
| 233 |
.args(&["status=none", "skip=256", "ibs=128", "iflag=skip_bytes"]) |
| 234 |
.pipe_in(input) |
| 235 |
.succeeds() |
| 236 |
.stdout_only(output); |
| 237 |
} |
| 238 |
|
| 239 |
#[test] |
| 240 |
fn test_stdin_stdout_skip_w_multiplier() { |
| 241 |
let input = build_ascii_block(10 * 1024); |
| 242 |
let output = String::from_utf8(input[5 * 1024..].to_vec()).unwrap(); |
| 243 |
new_ucmd!() |
| 244 |
.args(&["status=none", "skip=5K", "iflag=skip_bytes"]) |
| 245 |
.pipe_in(input) |
| 246 |
.succeeds() |
| 247 |
.stdout_is(output); |
| 248 |
} |
| 249 |
|
| 250 |
#[test] |
| 251 |
fn test_stdin_stdout_count_w_multiplier() { |
| 252 |
let input = build_ascii_block(5 * 1024); |
| 253 |
let output = String::from_utf8(input[..2 * 1024].to_vec()).unwrap(); |
| 254 |
new_ucmd!() |
| 255 |
.args(&["status=none", "count=2KiB", "iflag=count_bytes"]) |
| 256 |
.pipe_in(input) |
| 257 |
.succeeds() |
| 258 |
.stdout_only(output); |
| 259 |
} |
| 260 |
|
| 261 |
#[test] |
| 262 |
fn test_b_multiplier() { |
| 263 |
// "2b" means 2 * 512, which is 1024. |
| 264 |
new_ucmd!() |
| 265 |
.args(&["bs=2b", "count=1"]) |
| 266 |
.pipe_in("a".repeat(1025)) |
| 267 |
.succeeds() |
| 268 |
.stdout_is("a".repeat(1024)); |
| 269 |
} |
| 270 |
|
| 271 |
#[test] |
| 272 |
fn test_x_multiplier() { |
| 273 |
// "2x3" means 2 * 3, which is 6. |
| 274 |
new_ucmd!() |
| 275 |
.args(&["bs=2x3", "count=1"]) |
| 276 |
.pipe_in("abcdefghi") |
| 277 |
.succeeds() |
| 278 |
.stdout_is("abcdef"); |
| 279 |
} |
| 280 |
|
| 281 |
#[test] |
| 282 |
fn test_zero_multiplier_warning() { |
| 283 |
for arg in ["count", "seek", "skip"] { |
| 284 |
new_ucmd!() |
| 285 |
.args(&[format!("{arg}=0").as_str(), "status=none"]) |
| 286 |
.pipe_in("") |
| 287 |
.succeeds() |
| 288 |
.no_output(); |
| 289 |
|
| 290 |
new_ucmd!() |
| 291 |
.args(&[format!("{arg}=00x1").as_str(), "status=none"]) |
| 292 |
.pipe_in("") |
| 293 |
.succeeds() |
| 294 |
.no_output(); |
| 295 |
|
| 296 |
new_ucmd!() |
| 297 |
.args(&[format!("{arg}=0x1").as_str(), "status=none"]) |
| 298 |
.pipe_in("") |
| 299 |
.succeeds() |
| 300 |
.no_stdout() |
| 301 |
.stderr_contains("warning: '0x' is a zero multiplier; use '00x' if that is intended"); |
| 302 |
|
| 303 |
new_ucmd!() |
| 304 |
.args(&[format!("{arg}=0x0x1").as_str(), "status=none"]) |
| 305 |
.pipe_in("") |
| 306 |
.succeeds() |
| 307 |
.no_stdout() |
| 308 |
.stderr_is("dd: warning: '0x' is a zero multiplier; use '00x' if that is intended\ndd: warning: '0x' is a zero multiplier; use '00x' if that is intended\n"); |
| 309 |
|
| 310 |
new_ucmd!() |
| 311 |
.args(&[format!("{arg}=1x0x1").as_str(), "status=none"]) |
| 312 |
.pipe_in("") |
| 313 |
.succeeds() |
| 314 |
.no_stdout() |
| 315 |
.stderr_contains("warning: '0x' is a zero multiplier; use '00x' if that is intended"); |
| 316 |
|
| 317 |
new_ucmd!() |
| 318 |
.args(&[format!("{arg}=0x0x0").as_str(), "status=none"]) |
| 319 |
.pipe_in("") |
| 320 |
.succeeds() |
| 321 |
.no_stdout() |
| 322 |
.stderr_is("dd: warning: '0x' is a zero multiplier; use '00x' if that is intended\ndd: warning: '0x' is a zero multiplier; use '00x' if that is intended\n"); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
#[test] |
| 327 |
fn test_final_stats_noxfer() { |
| 328 |
new_ucmd!() |
| 329 |
.args(&["status=noxfer"]) |
| 330 |
.succeeds() |
| 331 |
.stderr_only("0+0 records in\n0+0 records out\n"); |
| 332 |
} |
| 333 |
|
| 334 |
#[test] |
| 335 |
fn test_final_stats_unspec() { |
| 336 |
new_ucmd!() |
| 337 |
.succeeds() |
| 338 |
.stderr_contains("0+0 records in\n0+0 records out\n0 bytes copied, ") |
| 339 |
.stderr_matches(&Regex::new(r"\d(\.\d+)?(e-\d\d)? s, ").unwrap()) |
| 340 |
.stderr_contains("0.0 B/s"); |
| 341 |
} |
| 342 |
|
| 343 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 344 |
#[test] |
| 345 |
fn test_excl_causes_failure_when_present() { |
| 346 |
let fname = "this-file-exists-excl.txt"; |
| 347 |
assert_fixture_exists!(&fname); |
| 348 |
|
| 349 |
let (_fix, mut ucmd) = at_and_ucmd!(); |
| 350 |
ucmd.args(&["of=this-file-exists-excl.txt", "conv=excl"]) |
| 351 |
.fails(); |
| 352 |
} |
| 353 |
|
| 354 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 355 |
#[test] |
| 356 |
fn test_noatime_does_not_update_infile_atime() { |
| 357 |
// NOTE: Not all environments support tracking access time. If this |
| 358 |
// test fails on some systems and passes on others, assume the functionality |
| 359 |
// is not working and the systems that pass it simply don't update file access time. |
| 360 |
let fname = "this-ifile-exists-noatime.txt"; |
| 361 |
assert_fixture_exists!(&fname); |
| 362 |
|
| 363 |
let (fix, mut ucmd) = at_and_ucmd!(); |
| 364 |
ucmd.args(&["status=none", "iflag=noatime", &inf!(fname)]); |
| 365 |
|
| 366 |
let pre_atime = fix.metadata(fname).accessed().unwrap(); |
| 367 |
|
| 368 |
ucmd.succeeds().no_output(); |
| 369 |
|
| 370 |
let post_atime = fix.metadata(fname).accessed().unwrap(); |
| 371 |
assert_eq!(pre_atime, post_atime); |
| 372 |
} |
| 373 |
|
| 374 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 375 |
#[test] |
| 376 |
fn test_noatime_does_not_update_ofile_atime() { |
| 377 |
// NOTE: Not all environments support tracking access time. If this |
| 378 |
// test fails on some systems and passes on others, assume the functionality |
| 379 |
// is not working and the systems that pass it simply don't update file access time. |
| 380 |
let fname = "this-ofile-exists-noatime.txt"; |
| 381 |
assert_fixture_exists!(&fname); |
| 382 |
|
| 383 |
let (fix, mut ucmd) = at_and_ucmd!(); |
| 384 |
ucmd.args(&["status=none", "oflag=noatime", &of!(fname)]); |
| 385 |
|
| 386 |
let pre_atime = fix.metadata(fname).accessed().unwrap(); |
| 387 |
|
| 388 |
ucmd.pipe_in("").succeeds().no_output(); |
| 389 |
|
| 390 |
let post_atime = fix.metadata(fname).accessed().unwrap(); |
| 391 |
assert_eq!(pre_atime, post_atime); |
| 392 |
} |
| 393 |
|
| 394 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 395 |
#[test] |
| 396 |
fn test_nocreat_causes_failure_when_outfile_not_present() { |
| 397 |
let fname = "this-file-does-not-exist.txt"; |
| 398 |
assert_fixture_not_exists!(&fname); |
| 399 |
|
| 400 |
let (fix, mut ucmd) = at_and_ucmd!(); |
| 401 |
ucmd.args(&["conv=nocreat", &of!(&fname)]) |
| 402 |
.pipe_in("") |
| 403 |
.fails() |
| 404 |
.stderr_only( |
| 405 |
"dd: failed to open 'this-file-does-not-exist.txt': No such file or directory\n", |
| 406 |
); |
| 407 |
assert!(!fix.file_exists(fname)); |
| 408 |
} |
| 409 |
|
| 410 |
#[test] |
| 411 |
fn test_notrunc_does_not_truncate() { |
| 412 |
// Set up test if needed (eg. after failure) |
| 413 |
let fname = "this-file-exists-notrunc.txt"; |
| 414 |
let fpath = fixture_path!(fname); |
| 415 |
match fpath.metadata() { |
| 416 |
Ok(m) if m.len() == 256 => {} |
| 417 |
_ => build_test_file!(&fpath, &build_ascii_block(256)), |
| 418 |
} |
| 419 |
|
| 420 |
let (fix, mut ucmd) = at_and_ucmd!(); |
| 421 |
ucmd.args(&["status=none", "conv=notrunc", &of!(&fname), "if=null.txt"]) |
| 422 |
.succeeds() |
| 423 |
.no_output(); |
| 424 |
|
| 425 |
assert_eq!(256, fix.metadata(fname).len()); |
| 426 |
} |
| 427 |
|
| 428 |
#[test] |
| 429 |
fn test_existing_file_truncated() { |
| 430 |
// Set up test if needed (eg. after failure) |
| 431 |
let fname = "this-file-exists-truncated.txt"; |
| 432 |
let fpath = fixture_path!(fname); |
| 433 |
match fpath.metadata() { |
| 434 |
Ok(m) if m.len() == 256 => {} |
| 435 |
_ => build_test_file!(&fpath, &vec![0; 256]), |
| 436 |
} |
| 437 |
|
| 438 |
let (fix, mut ucmd) = at_and_ucmd!(); |
| 439 |
ucmd.args(&["status=none", "if=null.txt", &of!(fname)]) |
| 440 |
.succeeds() |
| 441 |
.no_output(); |
| 442 |
|
| 443 |
assert_eq!(0, fix.metadata(fname).len()); |
| 444 |
} |
| 445 |
|
| 446 |
#[test] |
| 447 |
fn test_null_stats() { |
| 448 |
new_ucmd!() |
| 449 |
.arg("if=null.txt") |
| 450 |
.succeeds() |
| 451 |
.stderr_contains("0+0 records in\n0+0 records out\n0 bytes copied, ") |
| 452 |
.stderr_matches(&Regex::new(r"\d(\.\d+)?(e-\d\d)? s, ").unwrap()) |
| 453 |
.stderr_contains("0.0 B/s"); |
| 454 |
} |
| 455 |
|
| 456 |
#[test] |
| 457 |
fn test_null_fullblock() { |
| 458 |
new_ucmd!() |
| 459 |
.args(&["if=null.txt", "status=none", "iflag=fullblock"]) |
| 460 |
.succeeds() |
| 461 |
.no_output(); |
| 462 |
} |
| 463 |
|
| 464 |
#[cfg(unix)] |
| 465 |
#[ignore = "See note below before using this test."] |
| 466 |
#[test] |
| 467 |
fn test_fullblock() { |
| 468 |
let tname = "fullblock-from-urand"; |
| 469 |
let tmp_fn = format!("TESTFILE-{tname}.tmp"); |
| 470 |
let exp_stats = vec![ |
| 471 |
"1+0 records in\n", |
| 472 |
"1+0 records out\n", |
| 473 |
"134217728 bytes (134 MB, 128 MiB) copied,", |
| 474 |
]; |
| 475 |
let exp_stats = exp_stats.into_iter().fold(Vec::new(), |mut acc, s| { |
| 476 |
acc.extend(s.bytes()); |
| 477 |
acc |
| 478 |
}); |
| 479 |
|
| 480 |
let ucmd = new_ucmd!() |
| 481 |
.args(&[ |
| 482 |
"if=/dev/urandom", |
| 483 |
&of!(&tmp_fn), |
| 484 |
"bs=128M", |
| 485 |
// Note: In order for this test to actually test iflag=fullblock, the bs=VALUE |
| 486 |
// must be big enough to 'overwhelm' the urandom store of bytes. |
| 487 |
// Try executing 'dd if=/dev/urandom bs=128M count=1' (i.e without iflag=fullblock). |
| 488 |
// The stats should contain the line: '0+1 records in' indicating a partial read. |
| 489 |
// Since my system only copies 32 MiB without fullblock, I expect 128 MiB to be |
| 490 |
// a reasonable value for testing most systems. |
| 491 |
"count=1", |
| 492 |
"iflag=fullblock", |
| 493 |
]) |
| 494 |
.succeeds(); |
| 495 |
|
| 496 |
let run_stats = &ucmd.stderr()[..exp_stats.len()]; |
| 497 |
assert_eq!(exp_stats, run_stats); |
| 498 |
} |
| 499 |
|
| 500 |
// Fileio |
| 501 |
#[test] |
| 502 |
fn test_ys_to_stdout() { |
| 503 |
let output: Vec<_> = String::from("y\n").bytes().cycle().take(1024).collect(); |
| 504 |
let output = String::from_utf8(output).unwrap(); |
| 505 |
|
| 506 |
new_ucmd!() |
| 507 |
.args(&["status=none", "if=y-nl-1k.txt"]) |
| 508 |
.succeeds() |
| 509 |
.stdout_only(output); |
| 510 |
} |
| 511 |
|
| 512 |
#[test] |
| 513 |
fn test_zeros_to_stdout() { |
| 514 |
let output = vec![0; 256 * 1024]; |
| 515 |
let output = String::from_utf8(output).unwrap(); |
| 516 |
new_ucmd!() |
| 517 |
.args(&["status=none", "if=zero-256k.txt"]) |
| 518 |
.succeeds() |
| 519 |
.stdout_only(output); |
| 520 |
} |
| 521 |
|
| 522 |
#[cfg(target_pointer_width = "32")] |
| 523 |
#[test] |
| 524 |
fn test_oversized_bs_32_bit() { |
| 525 |
for bs_param in ["bs", "ibs", "obs", "cbs"] { |
| 526 |
new_ucmd!() |
| 527 |
.args(&[format!("{bs_param}=5GB")]) |
| 528 |
.fails() |
| 529 |
.no_stdout() |
| 530 |
.code_is(1) |
| 531 |
.stderr_is(format!("dd: {bs_param}=N cannot fit into memory\n")); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
#[test] |
| 536 |
fn test_to_stdout_with_ibs_obs() { |
| 537 |
let output: Vec<_> = String::from("y\n").bytes().cycle().take(1024).collect(); |
| 538 |
let output = String::from_utf8(output).unwrap(); |
| 539 |
|
| 540 |
new_ucmd!() |
| 541 |
.args(&["status=none", "if=y-nl-1k.txt", "ibs=521", "obs=1031"]) |
| 542 |
.succeeds() |
| 543 |
.stdout_only(output); |
| 544 |
} |
| 545 |
|
| 546 |
#[test] |
| 547 |
fn test_ascii_10k_to_stdout() { |
| 548 |
let output = build_ascii_block(1024 * 1024); |
| 549 |
// build_test_file!("ascii-10k.txt", &output); |
| 550 |
let output = String::from_utf8(output).unwrap(); |
| 551 |
|
| 552 |
new_ucmd!() |
| 553 |
.args(&["status=none", "if=ascii-10k.txt"]) |
| 554 |
.succeeds() |
| 555 |
.stdout_only(output); |
| 556 |
} |
| 557 |
|
| 558 |
#[test] |
| 559 |
fn test_zeros_to_file() { |
| 560 |
let tname = "zero-256k"; |
| 561 |
let test_fn = format!("{tname}.txt"); |
| 562 |
let tmp_fn = format!("TESTFILE-{tname}.tmp"); |
| 563 |
assert_fixture_exists!(test_fn); |
| 564 |
|
| 565 |
let (fix, mut ucmd) = at_and_ucmd!(); |
| 566 |
ucmd.args(&["status=none", &inf!(test_fn), &of!(tmp_fn)]) |
| 567 |
.succeeds() |
| 568 |
.no_output(); |
| 569 |
|
| 570 |
cmp_file!( |
| 571 |
File::open(fixture_path!(&test_fn)).unwrap(), |
| 572 |
fix.open(&tmp_fn) |
| 573 |
); |
| 574 |
} |
| 575 |
|
| 576 |
#[test] |
| 577 |
fn test_to_file_with_ibs_obs() { |
| 578 |
let tname = "zero-256k"; |
| 579 |
let test_fn = format!("{tname}.txt"); |
| 580 |
let tmp_fn = format!("TESTFILE-{tname}.tmp"); |
| 581 |
assert_fixture_exists!(test_fn); |
| 582 |
|
| 583 |
let (fix, mut ucmd) = at_and_ucmd!(); |
| 584 |
ucmd.args(&[ |
| 585 |
"status=none", |
| 586 |
&inf!(test_fn), |
| 587 |
&of!(tmp_fn), |
| 588 |
"ibs=222", |
| 589 |
"obs=111", |
| 590 |
]) |
| 591 |
.succeeds() |
| 592 |
.no_output(); |
| 593 |
|
| 594 |
cmp_file!( |
| 595 |
File::open(fixture_path!(&test_fn)).unwrap(), |
| 596 |
fix.open(&tmp_fn) |
| 597 |
); |
| 598 |
} |
| 599 |
|
| 600 |
#[test] |
| 601 |
fn test_ascii_521k_to_file() { |
| 602 |
let tname = "ascii-521k"; |
| 603 |
let input = build_ascii_block(512 * 1024); |
| 604 |
let tmp_fn = format!("TESTFILE-{tname}.tmp"); |
| 605 |
|
| 606 |
let (fix, mut ucmd) = at_and_ucmd!(); |
| 607 |
ucmd.args(&["status=none", &of!(tmp_fn)]) |
| 608 |
.pipe_in(input.clone()) |
| 609 |
.succeeds() |
| 610 |
.no_output(); |
| 611 |
|
| 612 |
assert_eq!(512 * 1024, fix.metadata(&tmp_fn).len()); |
| 613 |
|
| 614 |
cmp_file!( |
| 615 |
{ |
| 616 |
let mut input_f = tempfile().unwrap(); |
| 617 |
input_f.write_all(&input).unwrap(); |
| 618 |
input_f |
| 619 |
}, |
| 620 |
fix.open(&tmp_fn) |
| 621 |
); |
| 622 |
} |
| 623 |
|
| 624 |
#[ignore = ""] |
| 625 |
#[cfg(unix)] |
| 626 |
#[test] |
| 627 |
fn test_ascii_5_gibi_to_file() { |
| 628 |
let tname = "ascii-5G"; |
| 629 |
let tmp_fn = format!("TESTFILE-{tname}.tmp"); |
| 630 |
|
| 631 |
let (fix, mut ucmd) = at_and_ucmd!(); |
| 632 |
ucmd.args(&[ |
| 633 |
"status=none", |
| 634 |
"count=5G", |
| 635 |
"iflag=count_bytes", |
| 636 |
"if=/dev/zero", |
| 637 |
&of!(tmp_fn), |
| 638 |
]) |
| 639 |
.succeeds() |
| 640 |
.no_output(); |
| 641 |
|
| 642 |
assert_eq!(5 * 1024 * 1024 * 1024, fix.metadata(&tmp_fn).len()); |
| 643 |
} |
| 644 |
|
| 645 |
#[test] |
| 646 |
fn test_self_transfer() { |
| 647 |
let fname = "self-transfer-256k.txt"; |
| 648 |
assert_fixture_exists!(fname); |
| 649 |
|
| 650 |
let (fix, mut ucmd) = at_and_ucmd!(); |
| 651 |
ucmd.args(&["status=none", "conv=notrunc", &inf!(fname), &of!(fname)]); |
| 652 |
|
| 653 |
assert!(fix.file_exists(fname)); |
| 654 |
assert_eq!(256 * 1024, fix.metadata(fname).len()); |
| 655 |
|
| 656 |
ucmd.succeeds().no_output(); |
| 657 |
|
| 658 |
assert!(fix.file_exists(fname)); |
| 659 |
assert_eq!(256 * 1024, fix.metadata(fname).len()); |
| 660 |
} |
| 661 |
|
| 662 |
#[test] |
| 663 |
fn test_unicode_filenames() { |
| 664 |
let tname = "😎💚🦊"; |
| 665 |
let test_fn = format!("{tname}.txt"); |
| 666 |
let tmp_fn = format!("TESTFILE-{tname}.tmp"); |
| 667 |
assert_fixture_exists!(test_fn); |
| 668 |
|
| 669 |
let (fix, mut ucmd) = at_and_ucmd!(); |
| 670 |
ucmd.args(&["status=none", &inf!(test_fn), &of!(tmp_fn)]) |
| 671 |
.succeeds() |
| 672 |
.no_output(); |
| 673 |
|
| 674 |
cmp_file!( |
| 675 |
File::open(fixture_path!(&test_fn)).unwrap(), |
| 676 |
fix.open(&tmp_fn) |
| 677 |
); |
| 678 |
} |
| 679 |
|
| 680 |
#[test] |
| 681 |
fn test_conv_ascii_implies_unblock() { |
| 682 |
// 0x40 = 0o100 = 64, which gets converted to ' ' |
| 683 |
// 0xc1 = 0o301 = 193, which gets converted to 'A' |
| 684 |
// |
| 685 |
// `conv=ascii` implies `conv=unblock`, which means trailing paces |
| 686 |
// are stripped and a newline is appended at the end of each |
| 687 |
// block. |
| 688 |
// |
| 689 |
// `cbs=4` means use a conversion block size of 4 bytes per block. |
| 690 |
new_ucmd!() |
| 691 |
.args(&["conv=ascii", "cbs=4"]) |
| 692 |
.pipe_in(b"\x40\xc1\x40\xc1\x40\xc1\x40\x40".to_vec()) |
| 693 |
.succeeds() |
| 694 |
.stdout_is(" A A\n A\n"); |
| 695 |
} |
| 696 |
|
| 697 |
#[test] |
| 698 |
fn test_conv_ebcdic_implies_block() { |
| 699 |
// 0x40 = 0o100 = 64, which is the result of converting from ' ' |
| 700 |
// 0xc1 = 0o301 = 193, which is the result of converting from 'A' |
| 701 |
// |
| 702 |
// `conv=ebcdic` implies `conv=block`, which means trailing spaces |
| 703 |
// are added to pad each block. |
| 704 |
// |
| 705 |
// `cbs=4` means use a conversion block size of 4 bytes per block. |
| 706 |
new_ucmd!() |
| 707 |
.args(&["conv=ebcdic", "cbs=4"]) |
| 708 |
.pipe_in(" A A\n A\n") |
| 709 |
.succeeds() |
| 710 |
.stdout_is_bytes(b"\x40\xc1\x40\xc1\x40\xc1\x40\x40"); |
| 711 |
} |
| 712 |
|
| 713 |
/// Test for seeking forward N bytes in the output file before copying. |
| 714 |
#[test] |
| 715 |
fn test_seek_bytes() { |
| 716 |
// Since the output file is stdout, seeking forward by eight bytes |
| 717 |
// results in a prefix of eight null bytes. |
| 718 |
new_ucmd!() |
| 719 |
.args(&["seek=8", "oflag=seek_bytes"]) |
| 720 |
.pipe_in("abcdefghijklm\n") |
| 721 |
.succeeds() |
| 722 |
.stdout_is("\0\0\0\0\0\0\0\0abcdefghijklm\n"); |
| 723 |
} |
| 724 |
|
| 725 |
/// Test for skipping beyond the number of bytes in a file. |
| 726 |
#[test] |
| 727 |
fn test_skip_beyond_file() { |
| 728 |
new_ucmd!() |
| 729 |
.args(&["bs=1", "skip=5", "count=0", "status=noxfer"]) |
| 730 |
.pipe_in("abcd") |
| 731 |
.succeeds() |
| 732 |
.no_stdout() |
| 733 |
.stderr_contains( |
| 734 |
"'standard input': cannot skip to specified offset\n0+0 records in\n0+0 records out\n", |
| 735 |
); |
| 736 |
} |
| 737 |
|
| 738 |
#[test] |
| 739 |
#[cfg(unix)] |
| 740 |
fn test_skip_beyond_file_seekable_stdin() { |
| 741 |
// When stdin is a seekable file, dd should use seek to skip bytes. |
| 742 |
// This tests that skipping beyond the file size issues a warning. |
| 743 |
|
| 744 |
// Test cases: (bs, skip) pairs that skip beyond a 4-byte file |
| 745 |
let test_cases = [ |
| 746 |
("bs=1", "skip=5"), // skip 5 bytes |
| 747 |
("bs=3", "skip=2"), // skip 6 bytes |
| 748 |
]; |
| 749 |
|
| 750 |
for (bs, skip) in test_cases { |
| 751 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 752 |
at.write("in", "abcd"); |
| 753 |
|
| 754 |
let stdin = OwnedFileDescriptorOrHandle::open_file( |
| 755 |
OpenOptions::new().read(true), |
| 756 |
at.plus("in").as_path(), |
| 757 |
) |
| 758 |
.unwrap(); |
| 759 |
|
| 760 |
ucmd.args(&[bs, skip, "count=0", "status=noxfer"]) |
| 761 |
.set_stdin(Stdio::from(stdin)) |
| 762 |
.succeeds() |
| 763 |
.no_stdout() |
| 764 |
.stderr_contains( |
| 765 |
"'standard input': cannot skip to specified offset\n0+0 records in\n0+0 records out\n", |
| 766 |
); |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
#[test] |
| 771 |
fn test_seek_do_not_overwrite() { |
| 772 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 773 |
let mut outfile = at.make_file("outfile"); |
| 774 |
outfile.write_all(b"abc").unwrap(); |
| 775 |
// Skip the first byte of the input, seek past the first byte of |
| 776 |
// the output, and write only one byte to the output. |
| 777 |
ucmd.args(&[ |
| 778 |
"bs=1", |
| 779 |
"skip=1", |
| 780 |
"seek=1", |
| 781 |
"count=1", |
| 782 |
"status=noxfer", |
| 783 |
"of=outfile", |
| 784 |
]) |
| 785 |
.pipe_in("123") |
| 786 |
.succeeds() |
| 787 |
.stderr_is("1+0 records in\n1+0 records out\n") |
| 788 |
.no_stdout(); |
| 789 |
assert_eq!(at.read("outfile"), "a2"); |
| 790 |
} |
| 791 |
|
| 792 |
#[test] |
| 793 |
fn test_partial_records_out() { |
| 794 |
new_ucmd!() |
| 795 |
.args(&["bs=2", "status=noxfer"]) |
| 796 |
.pipe_in("abc") |
| 797 |
.succeeds() |
| 798 |
.stdout_is("abc") |
| 799 |
.stderr_is("1+1 records in\n1+1 records out\n"); |
| 800 |
} |
| 801 |
|
| 802 |
#[test] |
| 803 |
fn test_block_cbs16() { |
| 804 |
new_ucmd!() |
| 805 |
.args(&["conv=block", "cbs=16"]) |
| 806 |
.pipe_in_fixture("dd-block-cbs16.test") |
| 807 |
.succeeds() |
| 808 |
.stdout_is_fixture_bytes("dd-block-cbs16.spec"); |
| 809 |
} |
| 810 |
|
| 811 |
#[test] |
| 812 |
fn test_block_cbs16_as_cbs8() { |
| 813 |
new_ucmd!() |
| 814 |
.args(&["conv=block", "cbs=8"]) |
| 815 |
.pipe_in_fixture("dd-block-cbs16.test") |
| 816 |
.succeeds() |
| 817 |
.stdout_is_fixture_bytes("dd-block-cbs8.spec"); |
| 818 |
} |
| 819 |
|
| 820 |
#[test] |
| 821 |
fn test_block_consecutive_nl() { |
| 822 |
new_ucmd!() |
| 823 |
.args(&["conv=block", "cbs=16"]) |
| 824 |
.pipe_in_fixture("dd-block-consecutive-nl.test") |
| 825 |
.succeeds() |
| 826 |
.stdout_is_fixture_bytes("dd-block-consecutive-nl-cbs16.spec"); |
| 827 |
} |
| 828 |
|
| 829 |
#[test] |
| 830 |
fn test_unblock_multi_16() { |
| 831 |
new_ucmd!() |
| 832 |
.args(&["conv=unblock", "cbs=16"]) |
| 833 |
.pipe_in_fixture("dd-unblock-cbs16.test") |
| 834 |
.succeeds() |
| 835 |
.stdout_is_fixture_bytes("dd-unblock-cbs16.spec"); |
| 836 |
} |
| 837 |
|
| 838 |
#[test] |
| 839 |
fn test_unblock_multi_16_as_8() { |
| 840 |
new_ucmd!() |
| 841 |
.args(&["conv=unblock", "cbs=8"]) |
| 842 |
.pipe_in_fixture("dd-unblock-cbs16.test") |
| 843 |
.succeeds() |
| 844 |
.stdout_is_fixture_bytes("dd-unblock-cbs8.spec"); |
| 845 |
} |
| 846 |
|
| 847 |
#[test] |
| 848 |
fn test_atoe_conv_spec_test() { |
| 849 |
new_ucmd!() |
| 850 |
.args(&["conv=ebcdic"]) |
| 851 |
.pipe_in_fixture("seq-byte-values-b632a992d3aed5d8d1a59cc5a5a455ba.test") |
| 852 |
.succeeds() |
| 853 |
.stdout_is_fixture_bytes("gnudd-conv-atoe-seq-byte-values.spec"); |
| 854 |
} |
| 855 |
|
| 856 |
#[test] |
| 857 |
fn test_etoa_conv_spec_test() { |
| 858 |
new_ucmd!() |
| 859 |
.args(&["conv=ascii"]) |
| 860 |
.pipe_in_fixture("seq-byte-values-b632a992d3aed5d8d1a59cc5a5a455ba.test") |
| 861 |
.succeeds() |
| 862 |
.stdout_is_fixture_bytes("gnudd-conv-etoa-seq-byte-values.spec"); |
| 863 |
} |
| 864 |
|
| 865 |
#[test] |
| 866 |
fn test_atoibm_conv_spec_test() { |
| 867 |
new_ucmd!() |
| 868 |
.args(&["conv=ibm"]) |
| 869 |
.pipe_in_fixture("seq-byte-values-b632a992d3aed5d8d1a59cc5a5a455ba.test") |
| 870 |
.succeeds() |
| 871 |
.stdout_is_fixture_bytes("gnudd-conv-atoibm-seq-byte-values.spec"); |
| 872 |
} |
| 873 |
|
| 874 |
#[test] |
| 875 |
fn test_lcase_ascii_to_ucase_ascii() { |
| 876 |
new_ucmd!() |
| 877 |
.args(&["conv=ucase"]) |
| 878 |
.pipe_in_fixture("lcase-ascii.test") |
| 879 |
.succeeds() |
| 880 |
.stdout_is_fixture_bytes("ucase-ascii.test"); |
| 881 |
} |
| 882 |
|
| 883 |
#[test] |
| 884 |
fn test_ucase_ascii_to_lcase_ascii() { |
| 885 |
new_ucmd!() |
| 886 |
.args(&["conv=lcase"]) |
| 887 |
.pipe_in_fixture("ucase-ascii.test") |
| 888 |
.succeeds() |
| 889 |
.stdout_is_fixture_bytes("lcase-ascii.test"); |
| 890 |
} |
| 891 |
|
| 892 |
#[test] |
| 893 |
fn test_atoe_and_ucase_conv_spec_test() { |
| 894 |
new_ucmd!() |
| 895 |
.args(&["conv=ebcdic,ucase"]) |
| 896 |
.pipe_in_fixture("seq-byte-values-b632a992d3aed5d8d1a59cc5a5a455ba.test") |
| 897 |
.succeeds() |
| 898 |
.stdout_is_fixture_bytes("ucase-ebcdic.test"); |
| 899 |
} |
| 900 |
|
| 901 |
#[test] |
| 902 |
fn test_atoe_and_lcase_conv_spec_test() { |
| 903 |
new_ucmd!() |
| 904 |
.args(&["conv=ebcdic,lcase"]) |
| 905 |
.pipe_in_fixture("seq-byte-values-b632a992d3aed5d8d1a59cc5a5a455ba.test") |
| 906 |
.succeeds() |
| 907 |
.stdout_is_fixture_bytes("lcase-ebcdic.test"); |
| 908 |
} |
| 909 |
|
| 910 |
// TODO I think uppercase and lowercase are unintentionally swapped in |
| 911 |
// the code that parses the command-line arguments. See this line from |
| 912 |
// `parseargs.rs`: |
| 913 |
// |
| 914 |
// (ConvFlag::FmtAtoI, ConvFlag::UCase) => Some(&ASCII_TO_IBM_UCASE_TO_LCASE), |
| 915 |
// (ConvFlag::FmtAtoI, ConvFlag::LCase) => Some(&ASCII_TO_IBM_LCASE_TO_UCASE), |
| 916 |
// |
| 917 |
// If my reading is correct and that is a typo, then the |
| 918 |
// UCASE_TO_LCASE and LCASE_TO_UCASE in those lines should be swapped, |
| 919 |
// and the expected output for the following two tests should be |
| 920 |
// updated accordingly. |
| 921 |
#[test] |
| 922 |
fn test_atoibm_and_ucase_conv_spec_test() { |
| 923 |
new_ucmd!() |
| 924 |
.args(&["conv=ibm,ucase"]) |
| 925 |
.pipe_in_fixture("seq-byte-values-b632a992d3aed5d8d1a59cc5a5a455ba.test") |
| 926 |
.succeeds() |
| 927 |
.stdout_is_fixture_bytes("lcase-ibm.test"); |
| 928 |
} |
| 929 |
|
| 930 |
#[test] |
| 931 |
fn test_atoibm_and_lcase_conv_spec_test() { |
| 932 |
new_ucmd!() |
| 933 |
.args(&["conv=ibm,lcase"]) |
| 934 |
.pipe_in_fixture("seq-byte-values-b632a992d3aed5d8d1a59cc5a5a455ba.test") |
| 935 |
.succeeds() |
| 936 |
.stdout_is_fixture_bytes("ucase-ibm.test"); |
| 937 |
} |
| 938 |
|
| 939 |
#[test] |
| 940 |
fn test_swab_256_test() { |
| 941 |
new_ucmd!() |
| 942 |
.args(&["conv=swab"]) |
| 943 |
.pipe_in_fixture("seq-byte-values.test") |
| 944 |
.succeeds() |
| 945 |
.stdout_is_fixture_bytes("seq-byte-values-swapped.test"); |
| 946 |
} |
| 947 |
|
| 948 |
#[test] |
| 949 |
fn test_swab_257_test() { |
| 950 |
new_ucmd!() |
| 951 |
.args(&["conv=swab"]) |
| 952 |
.pipe_in_fixture("seq-byte-values-odd.test") |
| 953 |
.succeeds() |
| 954 |
.stdout_is_fixture_bytes("seq-byte-values-odd.spec"); |
| 955 |
} |
| 956 |
|
| 957 |
#[test] |
| 958 |
fn test_block_lower() { |
| 959 |
new_ucmd!() |
| 960 |
.args(&["conv=block,lcase", "cbs=8"]) |
| 961 |
.pipe_in_fixture("dd-block8-lowercase.test") |
| 962 |
.succeeds() |
| 963 |
.stdout_is_fixture_bytes("dd-block8-lowercase.spec"); |
| 964 |
} |
| 965 |
|
| 966 |
#[test] |
| 967 |
fn test_lower_block() { |
| 968 |
new_ucmd!() |
| 969 |
.args(&["conv=lcase,block", "cbs=8"]) |
| 970 |
.pipe_in_fixture("dd-block8-lowercase.test") |
| 971 |
.succeeds() |
| 972 |
.stdout_is_fixture_bytes("dd-block8-lowercase.spec"); |
| 973 |
} |
| 974 |
|
| 975 |
#[test] |
| 976 |
fn test_unblock_lower() { |
| 977 |
new_ucmd!() |
| 978 |
.args(&["conv=unblock,lcase", "cbs=8"]) |
| 979 |
.pipe_in_fixture("dd-unblock8-lowercase.test") |
| 980 |
.succeeds() |
| 981 |
.stdout_is_fixture_bytes("dd-unblock8-lowercase.spec"); |
| 982 |
} |
| 983 |
|
| 984 |
#[test] |
| 985 |
fn test_zeros_4k_conv_sync_obs_gt_ibs() { |
| 986 |
new_ucmd!() |
| 987 |
.args(&["conv=sync", "ibs=521", "obs=1031"]) |
| 988 |
.pipe_in_fixture("zeros-620f0b67a91f7f74151bc5be745b7110.test") |
| 989 |
.succeeds() |
| 990 |
.stdout_is_fixture_bytes("gnudd-conv-sync-ibs-521-obs-1031-zeros.spec"); |
| 991 |
} |
| 992 |
|
| 993 |
#[test] |
| 994 |
fn test_zeros_4k_conv_sync_ibs_gt_obs() { |
| 995 |
new_ucmd!() |
| 996 |
.args(&["conv=sync", "ibs=1031", "obs=521"]) |
| 997 |
.pipe_in_fixture("zeros-620f0b67a91f7f74151bc5be745b7110.test") |
| 998 |
.succeeds() |
| 999 |
.stdout_is_fixture_bytes("gnudd-conv-sync-ibs-1031-obs-521-zeros.spec"); |
| 1000 |
} |
| 1001 |
|
| 1002 |
#[test] |
| 1003 |
fn test_deadbeef_32k_conv_sync_obs_gt_ibs() { |
| 1004 |
new_ucmd!() |
| 1005 |
.args(&[ |
| 1006 |
"conv=sync", |
| 1007 |
"ibs=521", |
| 1008 |
"obs=1031", |
| 1009 |
"if=deadbeef-18d99661a1de1fc9af21b0ec2cd67ba3.test", |
| 1010 |
]) |
| 1011 |
.succeeds() |
| 1012 |
.stdout_is_fixture_bytes("gnudd-conv-sync-ibs-521-obs-1031-deadbeef.spec"); |
| 1013 |
} |
| 1014 |
|
| 1015 |
#[test] |
| 1016 |
fn test_deadbeef_32k_conv_sync_ibs_gt_obs() { |
| 1017 |
new_ucmd!() |
| 1018 |
.args(&[ |
| 1019 |
"conv=sync", |
| 1020 |
"ibs=1031", |
| 1021 |
"obs=521", |
| 1022 |
"if=deadbeef-18d99661a1de1fc9af21b0ec2cd67ba3.test", |
| 1023 |
]) |
| 1024 |
.succeeds() |
| 1025 |
.stdout_is_fixture_bytes("gnudd-conv-sync-ibs-1031-obs-521-deadbeef.spec"); |
| 1026 |
} |
| 1027 |
|
| 1028 |
#[test] |
| 1029 |
fn test_random_73k_test_bs_prime_obs_gt_ibs_sync() { |
| 1030 |
new_ucmd!() |
| 1031 |
.args(&[ |
| 1032 |
"conv=sync", |
| 1033 |
"ibs=521", |
| 1034 |
"obs=1031", |
| 1035 |
"if=random-5828891cb1230748e146f34223bbd3b5.test", |
| 1036 |
]) |
| 1037 |
.succeeds() |
| 1038 |
.stdout_is_fixture_bytes("gnudd-conv-sync-ibs-521-obs-1031-random.spec"); |
| 1039 |
} |
| 1040 |
|
| 1041 |
#[test] |
| 1042 |
fn test_random_73k_test_bs_prime_ibs_gt_obs_sync() { |
| 1043 |
new_ucmd!() |
| 1044 |
.args(&[ |
| 1045 |
"conv=sync", |
| 1046 |
"ibs=1031", |
| 1047 |
"obs=521", |
| 1048 |
"if=random-5828891cb1230748e146f34223bbd3b5.test", |
| 1049 |
]) |
| 1050 |
.succeeds() |
| 1051 |
.stdout_is_fixture_bytes("gnudd-conv-sync-ibs-1031-obs-521-random.spec"); |
| 1052 |
} |
| 1053 |
|
| 1054 |
#[test] |
| 1055 |
fn test_identity() { |
| 1056 |
new_ucmd!() |
| 1057 |
.args(&["if=zeros-620f0b67a91f7f74151bc5be745b7110.test"]) |
| 1058 |
.succeeds() |
| 1059 |
.stdout_is_fixture_bytes("zeros-620f0b67a91f7f74151bc5be745b7110.test"); |
| 1060 |
new_ucmd!() |
| 1061 |
.args(&["if=ones-6ae59e64850377ee5470c854761551ea.test"]) |
| 1062 |
.succeeds() |
| 1063 |
.stdout_is_fixture_bytes("ones-6ae59e64850377ee5470c854761551ea.test"); |
| 1064 |
new_ucmd!() |
| 1065 |
.args(&["if=deadbeef-18d99661a1de1fc9af21b0ec2cd67ba3.test"]) |
| 1066 |
.succeeds() |
| 1067 |
.stdout_is_fixture_bytes("deadbeef-18d99661a1de1fc9af21b0ec2cd67ba3.test"); |
| 1068 |
new_ucmd!() |
| 1069 |
.args(&["if=random-5828891cb1230748e146f34223bbd3b5.test"]) |
| 1070 |
.succeeds() |
| 1071 |
.stdout_is_fixture_bytes("random-5828891cb1230748e146f34223bbd3b5.test"); |
| 1072 |
} |
| 1073 |
|
| 1074 |
#[test] |
| 1075 |
fn test_random_73k_test_not_a_multiple_obs_gt_ibs() { |
| 1076 |
new_ucmd!() |
| 1077 |
.args(&[ |
| 1078 |
"ibs=521", |
| 1079 |
"obs=1031", |
| 1080 |
"if=random-5828891cb1230748e146f34223bbd3b5.test", |
| 1081 |
]) |
| 1082 |
.succeeds() |
| 1083 |
.stdout_is_fixture_bytes("random-5828891cb1230748e146f34223bbd3b5.test"); |
| 1084 |
} |
| 1085 |
|
| 1086 |
#[test] |
| 1087 |
fn test_random_73k_test_obs_lt_not_a_multiple_ibs() { |
| 1088 |
new_ucmd!() |
| 1089 |
.args(&[ |
| 1090 |
"ibs=1031", |
| 1091 |
"obs=521", |
| 1092 |
"if=random-5828891cb1230748e146f34223bbd3b5.test", |
| 1093 |
]) |
| 1094 |
.succeeds() |
| 1095 |
.stdout_is_fixture_bytes("random-5828891cb1230748e146f34223bbd3b5.test"); |
| 1096 |
} |
| 1097 |
|
| 1098 |
#[cfg(not(windows))] |
| 1099 |
#[test] |
| 1100 |
fn test_random_73k_test_lazy_fullblock() { |
| 1101 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1102 |
at.mkfifo("fifo"); |
| 1103 |
let child = ucmd |
| 1104 |
.args(&[ |
| 1105 |
"ibs=521", |
| 1106 |
"obs=1031", |
| 1107 |
"iflag=fullblock", |
| 1108 |
"if=fifo", |
| 1109 |
"status=noxfer", |
| 1110 |
]) |
| 1111 |
.run_no_wait(); |
| 1112 |
let data = at.read_bytes("random-5828891cb1230748e146f34223bbd3b5.test"); |
| 1113 |
{ |
| 1114 |
let mut fifo = OpenOptions::new() |
| 1115 |
.write(true) |
| 1116 |
.open(at.plus("fifo")) |
| 1117 |
.unwrap(); |
| 1118 |
for chunk in data.chunks(521 / 2) { |
| 1119 |
fifo.write_all(chunk).unwrap(); |
| 1120 |
sleep(Duration::from_millis(10)); |
| 1121 |
} |
| 1122 |
} |
| 1123 |
child |
| 1124 |
.wait() |
| 1125 |
.unwrap() |
| 1126 |
.success() |
| 1127 |
.stdout_is_bytes(&data) |
| 1128 |
.stderr_is("142+1 records in\n72+1 records out\n"); |
| 1129 |
} |
| 1130 |
|
| 1131 |
#[test] |
| 1132 |
fn test_deadbeef_all_32k_test_count_reads() { |
| 1133 |
new_ucmd!() |
| 1134 |
.args(&[ |
| 1135 |
"bs=1024", |
| 1136 |
"count=32", |
| 1137 |
"if=deadbeef-18d99661a1de1fc9af21b0ec2cd67ba3.test", |
| 1138 |
]) |
| 1139 |
.succeeds() |
| 1140 |
.stdout_is_fixture_bytes("deadbeef-18d99661a1de1fc9af21b0ec2cd67ba3.test"); |
| 1141 |
} |
| 1142 |
|
| 1143 |
#[test] |
| 1144 |
fn test_deadbeef_all_32k_test_count_bytes() { |
| 1145 |
new_ucmd!() |
| 1146 |
.args(&[ |
| 1147 |
"ibs=531", |
| 1148 |
"obs=1031", |
| 1149 |
"count=32x1024", |
| 1150 |
"oflag=count_bytes", |
| 1151 |
"if=deadbeef-18d99661a1de1fc9af21b0ec2cd67ba3.test", |
| 1152 |
]) |
| 1153 |
.succeeds() |
| 1154 |
.stdout_is_fixture_bytes("deadbeef-18d99661a1de1fc9af21b0ec2cd67ba3.test"); |
| 1155 |
} |
| 1156 |
|
| 1157 |
#[test] |
| 1158 |
fn test_deadbeef_32k_to_16k_test_count_reads() { |
| 1159 |
new_ucmd!() |
| 1160 |
.args(&[ |
| 1161 |
"ibs=1024", |
| 1162 |
"obs=1031", |
| 1163 |
"count=16", |
| 1164 |
"if=deadbeef-18d99661a1de1fc9af21b0ec2cd67ba3.test", |
| 1165 |
]) |
| 1166 |
.succeeds() |
| 1167 |
.stdout_is_fixture_bytes("gnudd-deadbeef-first-16k.spec"); |
| 1168 |
} |
| 1169 |
|
| 1170 |
#[test] |
| 1171 |
fn test_deadbeef_32k_to_12345_test_count_bytes() { |
| 1172 |
new_ucmd!() |
| 1173 |
.args(&[ |
| 1174 |
"ibs=531", |
| 1175 |
"obs=1031", |
| 1176 |
"count=12345", |
| 1177 |
"iflag=count_bytes", |
| 1178 |
"if=deadbeef-18d99661a1de1fc9af21b0ec2cd67ba3.test", |
| 1179 |
]) |
| 1180 |
.succeeds() |
| 1181 |
.stdout_is_fixture_bytes("gnudd-deadbeef-first-12345.spec"); |
| 1182 |
} |
| 1183 |
|
| 1184 |
#[test] |
| 1185 |
fn test_random_73k_test_count_reads() { |
| 1186 |
new_ucmd!() |
| 1187 |
.args(&[ |
| 1188 |
"bs=1024", |
| 1189 |
"count=32", |
| 1190 |
"if=random-5828891cb1230748e146f34223bbd3b5.test", |
| 1191 |
]) |
| 1192 |
.succeeds() |
| 1193 |
.stdout_is_fixture_bytes("gnudd-random-first-32k.spec"); |
| 1194 |
} |
| 1195 |
|
| 1196 |
#[test] |
| 1197 |
fn test_random_73k_test_count_bytes() { |
| 1198 |
new_ucmd!() |
| 1199 |
.args(&[ |
| 1200 |
"ibs=521", |
| 1201 |
"obs=1031", |
| 1202 |
"count=32x1024", |
| 1203 |
"iflag=count_bytes", |
| 1204 |
"if=random-5828891cb1230748e146f34223bbd3b5.test", |
| 1205 |
]) |
| 1206 |
.succeeds() |
| 1207 |
.stdout_is_fixture_bytes("gnudd-random-first-32k.spec"); |
| 1208 |
} |
| 1209 |
|
| 1210 |
#[test] |
| 1211 |
fn test_all_valid_ascii_ebcdic_ascii_roundtrip_conv_test() { |
| 1212 |
let tmp = new_ucmd!() |
| 1213 |
.args(&["ibs=128", "obs=1024", "conv=ebcdic"]) |
| 1214 |
.pipe_in_fixture("all-valid-ascii-chars-37eff01866ba3f538421b30b7cbefcac.test") |
| 1215 |
.succeeds() |
| 1216 |
.stdout_move_bytes(); |
| 1217 |
new_ucmd!() |
| 1218 |
.args(&["ibs=256", "obs=1024", "conv=ascii"]) |
| 1219 |
.pipe_in(tmp) |
| 1220 |
.succeeds() |
| 1221 |
.stdout_is_fixture_bytes("all-valid-ascii-chars-37eff01866ba3f538421b30b7cbefcac.test"); |
| 1222 |
} |
| 1223 |
|
| 1224 |
#[test] |
| 1225 |
fn test_skip_zero() { |
| 1226 |
new_ucmd!() |
| 1227 |
.args(&["skip=0", "status=noxfer"]) |
| 1228 |
.succeeds() |
| 1229 |
.no_stdout() |
| 1230 |
.stderr_is("0+0 records in\n0+0 records out\n"); |
| 1231 |
} |
| 1232 |
|
| 1233 |
#[test] |
| 1234 |
fn test_truncated_record() { |
| 1235 |
new_ucmd!() |
| 1236 |
.args(&["cbs=1", "conv=block", "status=noxfer"]) |
| 1237 |
.pipe_in("ab") |
| 1238 |
.succeeds() |
| 1239 |
.stdout_is("a") |
| 1240 |
.stderr_is("0+1 records in\n0+1 records out\n1 truncated record\n"); |
| 1241 |
new_ucmd!() |
| 1242 |
.args(&["cbs=1", "conv=block", "status=noxfer"]) |
| 1243 |
.pipe_in("ab\ncd\n") |
| 1244 |
.succeeds() |
| 1245 |
.stdout_is("ac") |
| 1246 |
.stderr_is("0+1 records in\n0+1 records out\n2 truncated records\n"); |
| 1247 |
} |
| 1248 |
|
| 1249 |
/// Test that the output file can be `/dev/null`. |
| 1250 |
#[cfg(unix)] |
| 1251 |
#[test] |
| 1252 |
fn test_outfile_dev_null() { |
| 1253 |
new_ucmd!().arg("of=/dev/null").succeeds().no_stdout(); |
| 1254 |
} |
| 1255 |
|
| 1256 |
#[test] |
| 1257 |
fn test_block_sync() { |
| 1258 |
new_ucmd!() |
| 1259 |
.args(&["ibs=5", "cbs=5", "conv=block,sync", "status=noxfer"]) |
| 1260 |
.pipe_in("012\nabcde\n") |
| 1261 |
.succeeds() |
| 1262 |
// blocks: 1 2 |
| 1263 |
.stdout_is("012 abcde") |
| 1264 |
.stderr_is("2+0 records in\n0+1 records out\n"); |
| 1265 |
|
| 1266 |
// It seems that a partial record in is represented as an |
| 1267 |
// all-spaces block at the end of the output. The "1 truncated |
| 1268 |
// record" line is present in the status report due to the line |
| 1269 |
// "abcdefg\n" being truncated to "abcde". |
| 1270 |
new_ucmd!() |
| 1271 |
.args(&["ibs=5", "cbs=5", "conv=block,sync", "status=noxfer"]) |
| 1272 |
.pipe_in("012\nabcdefg\n") |
| 1273 |
.succeeds() |
| 1274 |
// blocks: 1 2 3 |
| 1275 |
.stdout_is("012 abcde ") |
| 1276 |
.stderr_is("2+1 records in\n0+1 records out\n1 truncated record\n"); |
| 1277 |
} |
| 1278 |
|
| 1279 |
#[test] |
| 1280 |
fn test_bytes_iseek_bytes_iflag() { |
| 1281 |
new_ucmd!() |
| 1282 |
.args(&["iseek=10", "iflag=skip_bytes", "bs=2"]) |
| 1283 |
.pipe_in("0123456789abcdefghijklm") |
| 1284 |
.succeeds() |
| 1285 |
.stdout_is("abcdefghijklm"); |
| 1286 |
} |
| 1287 |
|
| 1288 |
#[test] |
| 1289 |
fn test_bytes_iseek_skip_not_additive() { |
| 1290 |
new_ucmd!() |
| 1291 |
.args(&["iseek=4", "skip=4", "iflag=skip_bytes", "bs=2"]) |
| 1292 |
.pipe_in("0123456789abcdefghijklm") |
| 1293 |
.succeeds() |
| 1294 |
.stdout_is("456789abcdefghijklm"); |
| 1295 |
} |
| 1296 |
|
| 1297 |
#[test] |
| 1298 |
fn test_bytes_oseek_bytes_oflag() { |
| 1299 |
new_ucmd!() |
| 1300 |
.args(&["oseek=8", "oflag=seek_bytes", "bs=2"]) |
| 1301 |
.pipe_in("abcdefghijklm") |
| 1302 |
.succeeds() |
| 1303 |
.stdout_is_fixture_bytes("dd-bytes-alphabet-null.spec"); |
| 1304 |
} |
| 1305 |
|
| 1306 |
#[test] |
| 1307 |
fn test_bytes_oseek_bytes_trunc_oflag() { |
| 1308 |
new_ucmd!() |
| 1309 |
.args(&["oseek=8", "oflag=seek_bytes", "bs=2", "count=0"]) |
| 1310 |
.pipe_in("abcdefghijklm") |
| 1311 |
.ignore_stdin_write_error() |
| 1312 |
.succeeds() |
| 1313 |
.stdout_is_fixture_bytes("dd-bytes-null-trunc.spec"); |
| 1314 |
} |
| 1315 |
|
| 1316 |
#[test] |
| 1317 |
fn test_bytes_oseek_seek_not_additive() { |
| 1318 |
new_ucmd!() |
| 1319 |
.args(&["oseek=8", "seek=8", "oflag=seek_bytes", "bs=2"]) |
| 1320 |
.pipe_in("abcdefghijklm") |
| 1321 |
.succeeds() |
| 1322 |
.stdout_is_fixture_bytes("dd-bytes-alphabet-null.spec"); |
| 1323 |
} |
| 1324 |
|
| 1325 |
#[test] |
| 1326 |
fn test_final_stats_less_than_one_kb_si() { |
| 1327 |
let result = new_ucmd!().pipe_in("0".repeat(999)).succeeds(); |
| 1328 |
let s = result.stderr_str(); |
| 1329 |
assert!(s.starts_with("1+1 records in\n1+1 records out\n999 bytes copied,")); |
| 1330 |
} |
| 1331 |
|
| 1332 |
#[test] |
| 1333 |
fn test_final_stats_less_than_one_kb_iec() { |
| 1334 |
let result = new_ucmd!().pipe_in("0".repeat(1000)).succeeds(); |
| 1335 |
let s = result.stderr_str(); |
| 1336 |
assert!(s.starts_with("1+1 records in\n1+1 records out\n1000 bytes (1.0 kB) copied,")); |
| 1337 |
|
| 1338 |
let result = new_ucmd!().pipe_in("0".repeat(1023)).succeeds(); |
| 1339 |
let s = result.stderr_str(); |
| 1340 |
assert!(s.starts_with("1+1 records in\n1+1 records out\n1023 bytes (1.0 kB) copied,")); |
| 1341 |
} |
| 1342 |
|
| 1343 |
#[test] |
| 1344 |
fn test_final_stats_more_than_one_kb() { |
| 1345 |
let result = new_ucmd!().pipe_in("0".repeat(1024)).succeeds(); |
| 1346 |
let s = result.stderr_str(); |
| 1347 |
assert!(s.starts_with("2+0 records in\n2+0 records out\n1024 bytes (1.0 kB, 1.0 KiB) copied,")); |
| 1348 |
} |
| 1349 |
|
| 1350 |
#[test] |
| 1351 |
fn test_final_stats_three_char_limit() { |
| 1352 |
let result = new_ucmd!().pipe_in("0".repeat(10_000)).succeeds(); |
| 1353 |
let s = result.stderr_str(); |
| 1354 |
assert!( |
| 1355 |
s.starts_with("19+1 records in\n19+1 records out\n10000 bytes (10 kB, 9.8 KiB) copied,") |
| 1356 |
); |
| 1357 |
|
| 1358 |
let result = new_ucmd!().pipe_in("0".repeat(100_000)).succeeds(); |
| 1359 |
let s = result.stderr_str(); |
| 1360 |
assert!( |
| 1361 |
s.starts_with("195+1 records in\n195+1 records out\n100000 bytes (100 kB, 98 KiB) copied,") |
| 1362 |
); |
| 1363 |
} |
| 1364 |
|
| 1365 |
#[test] |
| 1366 |
fn test_invalid_number_arg_gnu_compatibility() { |
| 1367 |
let commands = vec!["bs", "cbs", "count", "ibs", "obs", "seek", "skip"]; |
| 1368 |
|
| 1369 |
for command in commands { |
| 1370 |
new_ucmd!() |
| 1371 |
.args(&[format!("{command}=")]) |
| 1372 |
.fails() |
| 1373 |
.stderr_is("dd: invalid number: ''\n"); |
| 1374 |
|
| 1375 |
new_ucmd!() |
| 1376 |
.args(&[format!("{command}=29d")]) |
| 1377 |
.fails() |
| 1378 |
.stderr_is("dd: invalid number: '29d'\n"); |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
#[test] |
| 1383 |
fn test_invalid_flag_arg_gnu_compatibility() { |
| 1384 |
// GNU names the direction the flag was given in. |
| 1385 |
for (command, direction) in [("iflag", "input"), ("oflag", "output")] { |
| 1386 |
new_ucmd!() |
| 1387 |
.args(&[format!("{command}=")]) |
| 1388 |
.fails() |
| 1389 |
.usage_error(format!("invalid {direction} flag: ''")); |
| 1390 |
|
| 1391 |
new_ucmd!() |
| 1392 |
.args(&[format!("{command}=29d")]) |
| 1393 |
.fails() |
| 1394 |
.usage_error(format!("invalid {direction} flag: '29d'")); |
| 1395 |
} |
| 1396 |
} |
| 1397 |
|
| 1398 |
#[test] |
| 1399 |
fn test_invalid_file_arg_gnu_compatibility() { |
| 1400 |
new_ucmd!() |
| 1401 |
.args(&["if="]) |
| 1402 |
.fails() |
| 1403 |
.stderr_is("dd: failed to open '': No such file or directory\n"); |
| 1404 |
|
| 1405 |
new_ucmd!() |
| 1406 |
.args(&["if=81as9bn8as9g302az8ns9.pdf.zip.pl.com"]) |
| 1407 |
.fails() |
| 1408 |
.stderr_is( |
| 1409 |
"dd: failed to open '81as9bn8as9g302az8ns9.pdf.zip.pl.com': No such file or directory\n", |
| 1410 |
); |
| 1411 |
|
| 1412 |
new_ucmd!() |
| 1413 |
.args(&["of="]) |
| 1414 |
.fails() |
| 1415 |
.stderr_is("dd: failed to open '': No such file or directory\n"); |
| 1416 |
|
| 1417 |
new_ucmd!() |
| 1418 |
.args(&["of=81as9bn8as9g302az8ns9.pdf.zip.pl.com"]) |
| 1419 |
.pipe_in("") |
| 1420 |
.succeeds(); |
| 1421 |
} |
| 1422 |
|
| 1423 |
#[test] |
| 1424 |
fn test_ucase_lcase() { |
| 1425 |
new_ucmd!() |
| 1426 |
.arg("conv=ucase,lcase") |
| 1427 |
.fails() |
| 1428 |
.stderr_contains("lcase") |
| 1429 |
.stderr_contains("ucase"); |
| 1430 |
} |
| 1431 |
|
| 1432 |
#[test] |
| 1433 |
fn test_big_multiplication() { |
| 1434 |
new_ucmd!() |
| 1435 |
.arg("ibs=10x10x10x10x10x10x10x10x10x10x10x10x10x10x10x10x10x10x10x10x10x10x10") |
| 1436 |
.fails() |
| 1437 |
.stderr_contains("invalid number"); |
| 1438 |
} |
| 1439 |
|
| 1440 |
/// Test for count, seek, and skip given in units of bytes. |
| 1441 |
#[test] |
| 1442 |
fn test_bytes_suffix() { |
| 1443 |
new_ucmd!() |
| 1444 |
.args(&["count=3B", "status=none"]) |
| 1445 |
.pipe_in("abcdef") |
| 1446 |
.succeeds() |
| 1447 |
.stdout_only("abc"); |
| 1448 |
new_ucmd!() |
| 1449 |
.args(&["skip=3B", "status=none"]) |
| 1450 |
.pipe_in("abcdef") |
| 1451 |
.succeeds() |
| 1452 |
.stdout_only("def"); |
| 1453 |
new_ucmd!() |
| 1454 |
.args(&["iseek=3B", "status=none"]) |
| 1455 |
.pipe_in("abcdef") |
| 1456 |
.succeeds() |
| 1457 |
.stdout_only("def"); |
| 1458 |
new_ucmd!() |
| 1459 |
.args(&["seek=3B", "status=none"]) |
| 1460 |
.pipe_in("abcdef") |
| 1461 |
.succeeds() |
| 1462 |
.stdout_only("\0\0\0abcdef"); |
| 1463 |
new_ucmd!() |
| 1464 |
.args(&["oseek=3B", "status=none"]) |
| 1465 |
.pipe_in("abcdef") |
| 1466 |
.succeeds() |
| 1467 |
.stdout_only("\0\0\0abcdef"); |
| 1468 |
} |
| 1469 |
|
| 1470 |
#[test] |
| 1471 |
// the recursive nature of the suffix allows any string with a 'B' in it treated as bytes. |
| 1472 |
fn test_bytes_suffix_recursive() { |
| 1473 |
new_ucmd!() |
| 1474 |
.args(&["count=2Bx2", "status=none"]) |
| 1475 |
.pipe_in("abcdef") |
| 1476 |
.succeeds() |
| 1477 |
.stdout_only("abcd"); |
| 1478 |
new_ucmd!() |
| 1479 |
.args(&["skip=2Bx2", "status=none"]) |
| 1480 |
.pipe_in("abcdef") |
| 1481 |
.succeeds() |
| 1482 |
.stdout_only("ef"); |
| 1483 |
new_ucmd!() |
| 1484 |
.args(&["iseek=2Bx2", "status=none"]) |
| 1485 |
.pipe_in("abcdef") |
| 1486 |
.succeeds() |
| 1487 |
.stdout_only("ef"); |
| 1488 |
new_ucmd!() |
| 1489 |
.args(&["seek=2Bx2", "status=none"]) |
| 1490 |
.pipe_in("abcdef") |
| 1491 |
.succeeds() |
| 1492 |
.stdout_only("\0\0\0\0abcdef"); |
| 1493 |
new_ucmd!() |
| 1494 |
.args(&["oseek=2Bx2", "status=none"]) |
| 1495 |
.pipe_in("abcdef") |
| 1496 |
.succeeds() |
| 1497 |
.stdout_only("\0\0\0\0abcdef"); |
| 1498 |
} |
| 1499 |
|
| 1500 |
/// Test for "conv=sync" with a slow reader. |
| 1501 |
#[cfg(not(windows))] |
| 1502 |
#[test] |
| 1503 |
fn test_sync_delayed_reader() { |
| 1504 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1505 |
at.mkfifo("fifo"); |
| 1506 |
let child = ucmd |
| 1507 |
.args(&["ibs=16", "obs=32", "conv=sync", "if=fifo", "status=noxfer"]) |
| 1508 |
.run_no_wait(); |
| 1509 |
{ |
| 1510 |
let mut fifo = OpenOptions::new() |
| 1511 |
.write(true) |
| 1512 |
.open(at.plus("fifo")) |
| 1513 |
.unwrap(); |
| 1514 |
for _ in 0..8 { |
| 1515 |
fifo.write_all(&[0xF; 8]).unwrap(); |
| 1516 |
// Each write must be read as its own short record, so leave dd |
| 1517 |
// enough time to drain the pipe: if two writes pile up, it reads a |
| 1518 |
// full 16-byte block, pads nothing and the output no longer matches. |
| 1519 |
sleep(Duration::from_millis(100)); |
| 1520 |
} |
| 1521 |
} |
| 1522 |
// Expected output is 0xFFFFFFFF00000000FFFFFFFF00000000... |
| 1523 |
let mut expected: [u8; 8 * 16] = [0; 8 * 16]; |
| 1524 |
for i in 0..8 { |
| 1525 |
for j in 0..8 { |
| 1526 |
expected[16 * i + j] = 0xF; |
| 1527 |
} |
| 1528 |
} |
| 1529 |
|
| 1530 |
child |
| 1531 |
.wait() |
| 1532 |
.unwrap() |
| 1533 |
.success() |
| 1534 |
.stdout_is_bytes(expected) |
| 1535 |
.stderr_is("0+8 records in\n4+0 records out\n"); |
| 1536 |
} |
| 1537 |
|
| 1538 |
/// Test for making a sparse copy of the input file. |
| 1539 |
#[test] |
| 1540 |
fn test_sparse() { |
| 1541 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1542 |
|
| 1543 |
// Create a file and make it a large sparse file. |
| 1544 |
// |
| 1545 |
// On common Linux filesystems, setting the length to one megabyte |
| 1546 |
// should cause the file to become a sparse file, but it depends |
| 1547 |
// on the system. |
| 1548 |
File::create(at.plus("infile")) |
| 1549 |
.unwrap() |
| 1550 |
.set_len(1024 * 1024) |
| 1551 |
.unwrap(); |
| 1552 |
|
| 1553 |
// Perform a sparse copy. |
| 1554 |
ucmd.args(&["bs=32K", "if=infile", "of=outfile", "conv=sparse"]) |
| 1555 |
.succeeds(); |
| 1556 |
|
| 1557 |
// The number of bytes in the file should be accurate though the |
| 1558 |
// number of blocks stored on disk may be zero. |
| 1559 |
assert_eq!(at.metadata("infile").len(), at.metadata("outfile").len()); |
| 1560 |
} |
| 1561 |
|
| 1562 |
/// Test that a seek on an output FIFO results in a read. |
| 1563 |
#[test] |
| 1564 |
#[cfg(unix)] |
| 1565 |
fn test_seek_output_fifo() { |
| 1566 |
let ts = TestScenario::new(util_name!()); |
| 1567 |
let at = &ts.fixtures; |
| 1568 |
at.mkfifo("fifo"); |
| 1569 |
|
| 1570 |
let mut ucmd = ts.ucmd(); |
| 1571 |
let child = ucmd |
| 1572 |
.args(&["count=0", "seek=1", "of=fifo", "status=noxfer"]) |
| 1573 |
.run_no_wait(); |
| 1574 |
|
| 1575 |
std::fs::write(at.plus("fifo"), vec![0; 512]).unwrap(); |
| 1576 |
|
| 1577 |
child |
| 1578 |
.wait() |
| 1579 |
.unwrap() |
| 1580 |
.success() |
| 1581 |
.stderr_only("0+0 records in\n0+0 records out\n"); |
| 1582 |
} |
| 1583 |
|
| 1584 |
/// Test that a skip on an input FIFO results in a read. |
| 1585 |
#[test] |
| 1586 |
#[cfg(unix)] |
| 1587 |
fn test_skip_input_fifo() { |
| 1588 |
let ts = TestScenario::new(util_name!()); |
| 1589 |
let at = &ts.fixtures; |
| 1590 |
at.mkfifo("fifo"); |
| 1591 |
|
| 1592 |
let mut ucmd = ts.ucmd(); |
| 1593 |
let child = ucmd |
| 1594 |
.args(&["count=0", "skip=1", "if=fifo", "status=noxfer"]) |
| 1595 |
.run_no_wait(); |
| 1596 |
|
| 1597 |
std::fs::write(at.plus("fifo"), vec![0; 512]).unwrap(); |
| 1598 |
|
| 1599 |
child |
| 1600 |
.wait() |
| 1601 |
.unwrap() |
| 1602 |
.success() |
| 1603 |
.stderr_only("0+0 records in\n0+0 records out\n"); |
| 1604 |
} |
| 1605 |
|
| 1606 |
/// Test for reading part of stdin from each of two child processes. |
| 1607 |
#[cfg(not(windows))] |
| 1608 |
#[test] |
| 1609 |
fn test_multiple_processes_reading_stdin() { |
| 1610 |
// TODO Investigate if this is possible on Windows. |
| 1611 |
let printf = "printf 'abcdef\n'".to_string(); |
| 1612 |
let dd_skip = format!("{} dd bs=1 skip=3 count=0", get_tests_binary()); |
| 1613 |
let dd = format!("{} dd", get_tests_binary()); |
| 1614 |
UCommand::new() |
| 1615 |
.arg(format!("{printf} | ( {dd_skip} && {dd} ) 2> /dev/null")) |
| 1616 |
.succeeds() |
| 1617 |
.stdout_only("def\n"); |
| 1618 |
} |
| 1619 |
|
| 1620 |
/// Test that discarding system file cache fails for stdin. |
| 1621 |
#[test] |
| 1622 |
#[cfg(target_os = "linux")] |
| 1623 |
fn test_nocache_stdin_error() { |
| 1624 |
#[cfg(not(target_env = "musl"))] |
| 1625 |
let detail = "Illegal seek"; |
| 1626 |
#[cfg(target_env = "musl")] |
| 1627 |
let detail = "Invalid seek"; |
| 1628 |
new_ucmd!() |
| 1629 |
.args(&["iflag=nocache", "count=0", "status=noxfer"]) |
| 1630 |
.fails_with_code(1) |
| 1631 |
.stderr_only(format!("dd: failed to discard cache for: 'standard input': {detail}\n0+0 records in\n0+0 records out\n")); |
| 1632 |
} |
| 1633 |
|
| 1634 |
/// Test that dd fails when no number in count. |
| 1635 |
#[test] |
| 1636 |
fn test_empty_count_number() { |
| 1637 |
new_ucmd!() |
| 1638 |
.args(&["count=B"]) |
| 1639 |
.fails_with_code(1) |
| 1640 |
.stderr_only("dd: invalid number: 'B'\n"); |
| 1641 |
} |
| 1642 |
|
| 1643 |
/// Test for discarding system file cache. |
| 1644 |
#[test] |
| 1645 |
#[cfg(target_os = "linux")] |
| 1646 |
fn test_nocache_file() { |
| 1647 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1648 |
at.write_bytes("f", b"a".repeat(1 << 20).as_slice()); |
| 1649 |
ucmd.args(&["if=f", "of=/dev/null", "iflag=nocache", "status=noxfer"]) |
| 1650 |
.succeeds() |
| 1651 |
.stderr_only("2048+0 records in\n2048+0 records out\n"); |
| 1652 |
} |
| 1653 |
|
| 1654 |
#[test] |
| 1655 |
#[cfg(unix)] |
| 1656 |
#[cfg(not(feature = "selinux"))] |
| 1657 |
// Disabled on SELinux for now |
| 1658 |
fn test_skip_past_dev() { |
| 1659 |
// NOTE: This test intends to trigger code which can only be reached with root permissions. |
| 1660 |
let ts = TestScenario::new(util_name!()); |
| 1661 |
|
| 1662 |
if !ts.fixtures.file_exists("/dev/sda1") { |
| 1663 |
print!("Test skipped; no /dev/sda1 device found"); |
| 1664 |
} else if let Ok(result) = run_ucmd_as_root_with_stdin_stdout( |
| 1665 |
&ts, |
| 1666 |
&["bs=1", "skip=10000000000000000", "count=0", "status=noxfer"], |
| 1667 |
Some("/dev/sda1"), |
| 1668 |
None, |
| 1669 |
) { |
| 1670 |
result.stderr_contains("dd: 'standard input': cannot skip: Invalid argument"); |
| 1671 |
result.stderr_contains("0+0 records in"); |
| 1672 |
result.stderr_contains("0+0 records out"); |
| 1673 |
result.code_is(1); |
| 1674 |
} else { |
| 1675 |
print!("Test skipped; requires root user"); |
| 1676 |
} |
| 1677 |
} |
| 1678 |
|
| 1679 |
#[test] |
| 1680 |
#[cfg(unix)] |
| 1681 |
#[cfg(not(feature = "selinux"))] |
| 1682 |
fn test_seek_past_dev() { |
| 1683 |
// NOTE: This test intends to trigger code which can only be reached with root permissions. |
| 1684 |
let ts = TestScenario::new(util_name!()); |
| 1685 |
|
| 1686 |
if !ts.fixtures.file_exists("/dev/sda1") { |
| 1687 |
print!("Test skipped; no /dev/sda1 device found"); |
| 1688 |
} else if let Ok(result) = run_ucmd_as_root_with_stdin_stdout( |
| 1689 |
&ts, |
| 1690 |
&["bs=1", "seek=10000000000000000", "count=0", "status=noxfer"], |
| 1691 |
None, |
| 1692 |
Some("/dev/sda1"), |
| 1693 |
) { |
| 1694 |
result.stderr_contains("dd: 'standard output': cannot seek: Invalid argument"); |
| 1695 |
result.stderr_contains("0+0 records in"); |
| 1696 |
result.stderr_contains("0+0 records out"); |
| 1697 |
result.code_is(1); |
| 1698 |
} else { |
| 1699 |
print!("Test skipped; requires root user"); |
| 1700 |
} |
| 1701 |
} |
| 1702 |
|
| 1703 |
#[test] |
| 1704 |
#[cfg(all(unix, not(target_vendor = "apple"), not(target_os = "freebsd"),))] |
| 1705 |
fn test_reading_partial_blocks_from_fifo() { |
| 1706 |
// Create the FIFO. |
| 1707 |
let ts = TestScenario::new(util_name!()); |
| 1708 |
let at = &ts.fixtures; |
| 1709 |
at.mkfifo("fifo"); |
| 1710 |
let fifoname = at.plus_as_string("fifo"); |
| 1711 |
|
| 1712 |
// Start a `dd` process that reads from the fifo (so it will wait |
| 1713 |
// until the writer process starts). |
| 1714 |
let mut reader_command = Command::new(get_tests_binary()); |
| 1715 |
let child = reader_command |
| 1716 |
.args(["dd", "ibs=3", "obs=3", &format!("if={fifoname}")]) |
| 1717 |
.stdout(Stdio::piped()) |
| 1718 |
.stderr(Stdio::piped()) |
| 1719 |
.env("LC_ALL", "C") |
| 1720 |
.env("LANG", "C") |
| 1721 |
.env("LANGUAGE", "C") |
| 1722 |
.spawn() |
| 1723 |
.unwrap(); |
| 1724 |
|
| 1725 |
// Start different processes to write to the FIFO, with a small |
| 1726 |
// pause in between. |
| 1727 |
let mut writer_command = Command::new("sh"); |
| 1728 |
let _ = writer_command |
| 1729 |
.args([ |
| 1730 |
"-c", |
| 1731 |
&format!("(printf \"ab\"; sleep 0.1; printf \"cd\") > {fifoname}"), |
| 1732 |
]) |
| 1733 |
.spawn() |
| 1734 |
.unwrap() |
| 1735 |
.wait(); |
| 1736 |
|
| 1737 |
let output = child.wait_with_output().unwrap(); |
| 1738 |
assert_eq!(output.stdout, b"abcd"); |
| 1739 |
let expected = b"0+2 records in\n1+1 records out\n4 bytes copied"; |
| 1740 |
assert!(output.stderr.starts_with(expected)); |
| 1741 |
} |
| 1742 |
|
| 1743 |
#[test] |
| 1744 |
#[cfg(all(unix, not(target_vendor = "apple"), not(target_os = "freebsd"),))] |
| 1745 |
fn test_reading_partial_blocks_from_fifo_unbuffered() { |
| 1746 |
// Create the FIFO. |
| 1747 |
let ts = TestScenario::new(util_name!()); |
| 1748 |
let at = &ts.fixtures; |
| 1749 |
at.mkfifo("fifo"); |
| 1750 |
let fifoname = at.plus_as_string("fifo"); |
| 1751 |
|
| 1752 |
// Start a `dd` process that reads from the fifo (so it will wait |
| 1753 |
// until the writer process starts). |
| 1754 |
// |
| 1755 |
// `bs=N` takes precedence over `ibs=N` and `obs=N`. |
| 1756 |
let mut reader_command = Command::new(get_tests_binary()); |
| 1757 |
let child = reader_command |
| 1758 |
.args(["dd", "bs=3", "ibs=1", "obs=1", &format!("if={fifoname}")]) |
| 1759 |
.stdout(Stdio::piped()) |
| 1760 |
.stderr(Stdio::piped()) |
| 1761 |
.env("LC_ALL", "C") |
| 1762 |
.env("LANG", "C") |
| 1763 |
.env("LANGUAGE", "C") |
| 1764 |
.spawn() |
| 1765 |
.unwrap(); |
| 1766 |
|
| 1767 |
// Start different processes to write to the FIFO, with a small |
| 1768 |
// pause in between. |
| 1769 |
let mut writer_command = Command::new("sh"); |
| 1770 |
let _ = writer_command |
| 1771 |
.args([ |
| 1772 |
"-c", |
| 1773 |
&format!("(printf \"ab\"; sleep 0.1; printf \"cd\") > {fifoname}"), |
| 1774 |
]) |
| 1775 |
.spawn() |
| 1776 |
.unwrap() |
| 1777 |
.wait(); |
| 1778 |
|
| 1779 |
let output = child.wait_with_output().unwrap(); |
| 1780 |
assert_eq!(output.stdout, b"abcd"); |
| 1781 |
let expected = b"0+2 records in\n0+2 records out\n4 bytes copied"; |
| 1782 |
assert!(output.stderr.starts_with(expected)); |
| 1783 |
} |
| 1784 |
|
| 1785 |
/// Regression test for <https://github.com/uutils/coreutils/issues/13458>: |
| 1786 |
/// two deliberately short reads (ibs=3 sees only 2 bytes each) must be |
| 1787 |
/// gathered into a single obs=6 output block without pulling stale bytes |
| 1788 |
/// from the ibs-aligned gap into the output. |
| 1789 |
/// |
| 1790 |
/// The writer below runs `printf` inside `sh`, where it is a builtin, so this |
| 1791 |
/// needs no `printf` feature. |
| 1792 |
#[test] |
| 1793 |
#[cfg(all(unix, not(target_vendor = "apple"), not(target_os = "freebsd")))] |
| 1794 |
fn test_reading_partial_blocks_from_fifo_gathered_into_larger_obs() { |
| 1795 |
// Create the FIFO. |
| 1796 |
let ts = TestScenario::new(util_name!()); |
| 1797 |
let at = &ts.fixtures; |
| 1798 |
at.mkfifo("fifo"); |
| 1799 |
let fifoname = at.plus_as_string("fifo"); |
| 1800 |
|
| 1801 |
// Start a `dd` process that reads from the fifo (so it will wait |
| 1802 |
// until the writer process starts). |
| 1803 |
let mut reader_command = Command::new(get_tests_binary()); |
| 1804 |
let child = reader_command |
| 1805 |
.args(["dd", "ibs=3", "obs=6", &format!("if={fifoname}")]) |
| 1806 |
.stdout(Stdio::piped()) |
| 1807 |
.stderr(Stdio::piped()) |
| 1808 |
.env("LC_ALL", "C") |
| 1809 |
.env("LANG", "C") |
| 1810 |
.env("LANGUAGE", "C") |
| 1811 |
.spawn() |
| 1812 |
.unwrap(); |
| 1813 |
|
| 1814 |
// Start different processes to write to the FIFO, with a small |
| 1815 |
// pause in between. |
| 1816 |
let mut writer_command = Command::new("sh"); |
| 1817 |
let _ = writer_command |
| 1818 |
.args([ |
| 1819 |
"-c", |
| 1820 |
&format!("(printf \"ab\"; sleep 0.1; printf \"cd\") > {fifoname}"), |
| 1821 |
]) |
| 1822 |
.spawn() |
| 1823 |
.unwrap() |
| 1824 |
.wait(); |
| 1825 |
|
| 1826 |
let output = child.wait_with_output().unwrap(); |
| 1827 |
assert_eq!(output.stdout, b"abcd"); |
| 1828 |
let expected = b"0+2 records in\n0+1 records out\n4 bytes copied"; |
| 1829 |
assert!(output.stderr.starts_with(expected)); |
| 1830 |
} |
| 1831 |
|
| 1832 |
// `O_DIRECT` requires the read buffer to satisfy the device's DMA alignment; |
| 1833 |
// with a misaligned buffer the kernel fails the read with EINVAL (#12085). |
| 1834 |
// Reading a regular file with `iflag=direct` exercises the same requirement |
| 1835 |
// on filesystems that support it. |
| 1836 |
#[test] |
| 1837 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 1838 |
fn test_iflag_direct_read_uses_aligned_buffer() { |
| 1839 |
use std::os::unix::fs::OpenOptionsExt; |
| 1840 |
|
| 1841 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1842 |
// A multiple of the block size: the EOF read then happens at an aligned |
| 1843 |
// file offset. A trailing partial block would leave the offset |
| 1844 |
// misaligned, which some kernels (e.g. f2fs on Android) reject with |
| 1845 |
// EINVAL instead of reporting end of file. |
| 1846 |
let data = build_ascii_block(2 * 4096); |
| 1847 |
at.write_bytes("direct-in.bin", &data); |
| 1848 |
|
| 1849 |
if OpenOptions::new() |
| 1850 |
.read(true) |
| 1851 |
.custom_flags(libc::O_DIRECT) |
| 1852 |
.open(at.plus("direct-in.bin")) |
| 1853 |
.is_err() |
| 1854 |
{ |
| 1855 |
print!("Test skipped; filesystem does not support O_DIRECT"); |
| 1856 |
return; |
| 1857 |
} |
| 1858 |
|
| 1859 |
ucmd.args(&[ |
| 1860 |
"if=direct-in.bin", |
| 1861 |
"of=direct-out.bin", |
| 1862 |
"iflag=direct", |
| 1863 |
"bs=4096", |
| 1864 |
]) |
| 1865 |
.succeeds(); |
| 1866 |
assert_eq!(at.read_bytes("direct-out.bin"), data); |
| 1867 |
} |
| 1868 |
|
| 1869 |
#[test] |
| 1870 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 1871 |
fn test_iflag_directory_fails_when_file_is_passed_via_std_in() { |
| 1872 |
let ts = TestScenario::new(util_name!()); |
| 1873 |
let at = &ts.fixtures; |
| 1874 |
at.make_file("input"); |
| 1875 |
let filename = at.plus_as_string("input"); |
| 1876 |
new_ucmd!() |
| 1877 |
.args(&["iflag=directory", "count=0"]) |
| 1878 |
.set_stdin(Stdio::from(File::open(filename).unwrap())) |
| 1879 |
.fails() |
| 1880 |
.stderr_only("dd: setting flags for 'standard input': Not a directory\n"); |
| 1881 |
} |
| 1882 |
|
| 1883 |
#[test] |
| 1884 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 1885 |
fn test_iflag_directory_passes_when_dir_is_redirected() { |
| 1886 |
new_ucmd!() |
| 1887 |
.args(&["iflag=directory", "count=0"]) |
| 1888 |
.set_stdin(Stdio::from(File::open(".").unwrap())) |
| 1889 |
.succeeds(); |
| 1890 |
} |
| 1891 |
|
| 1892 |
#[test] |
| 1893 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 1894 |
fn test_iflag_directory_fails_when_file_is_piped_via_std_in() { |
| 1895 |
new_ucmd!() |
| 1896 |
.arg("iflag=directory") |
| 1897 |
.pipe_in("") |
| 1898 |
.fails() |
| 1899 |
.stderr_only("dd: setting flags for 'standard input': Not a directory\n"); |
| 1900 |
} |
| 1901 |
|
| 1902 |
#[test] |
| 1903 |
fn test_stdin_stdout_not_rewound_even_when_connected_to_seekable_file() { |
| 1904 |
let ts = TestScenario::new(util_name!()); |
| 1905 |
let at = &ts.fixtures; |
| 1906 |
|
| 1907 |
at.write("in", "abcde"); |
| 1908 |
|
| 1909 |
let stdin = OwnedFileDescriptorOrHandle::open_file( |
| 1910 |
OpenOptions::new().read(true), |
| 1911 |
at.plus("in").as_path(), |
| 1912 |
) |
| 1913 |
.unwrap(); |
| 1914 |
let stdout = OwnedFileDescriptorOrHandle::open_file( |
| 1915 |
OpenOptions::new().create(true).write(true), |
| 1916 |
at.plus("out").as_path(), |
| 1917 |
) |
| 1918 |
.unwrap(); |
| 1919 |
let stderr = OwnedFileDescriptorOrHandle::open_file( |
| 1920 |
OpenOptions::new().create(true).write(true), |
| 1921 |
at.plus("err").as_path(), |
| 1922 |
) |
| 1923 |
.unwrap(); |
| 1924 |
|
| 1925 |
ts.ucmd() |
| 1926 |
.args(&["bs=1", "skip=1", "count=1"]) |
| 1927 |
.set_stdin(Stdio::from(stdin.try_clone().unwrap())) |
| 1928 |
.set_stdout(Stdio::from(stdout.try_clone().unwrap())) |
| 1929 |
.set_stderr(Stdio::from(stderr.try_clone().unwrap())) |
| 1930 |
.succeeds(); |
| 1931 |
|
| 1932 |
ts.ucmd() |
| 1933 |
.args(&["bs=1", "skip=1"]) |
| 1934 |
.set_stdin(stdin) |
| 1935 |
.set_stdout(stdout) |
| 1936 |
.set_stderr(stderr) |
| 1937 |
.succeeds(); |
| 1938 |
|
| 1939 |
let err_file_content = std::fs::read_to_string(at.plus_as_string("err")).unwrap(); |
| 1940 |
println!("stderr:\n{err_file_content}"); |
| 1941 |
|
| 1942 |
let out_file_content = std::fs::read_to_string(at.plus_as_string("out")).unwrap(); |
| 1943 |
println!("stdout:\n{out_file_content}"); |
| 1944 |
assert_eq!(out_file_content, "bde"); |
| 1945 |
} |
| 1946 |
|
| 1947 |
#[test] |
| 1948 |
fn test_wrong_number_err_msg() { |
| 1949 |
new_ucmd!() |
| 1950 |
.args(&["count=kBb"]) |
| 1951 |
.fails() |
| 1952 |
.stderr_contains("dd: invalid number: 'kBb'\n"); |
| 1953 |
|
| 1954 |
new_ucmd!() |
| 1955 |
.args(&["count=1kBb555"]) |
| 1956 |
.fails() |
| 1957 |
.stderr_contains("dd: invalid number: '1kBb555'\n"); |
| 1958 |
} |
| 1959 |
|
| 1960 |
#[test] |
| 1961 |
#[cfg(unix)] |
| 1962 |
fn test_no_dropped_writes() { |
| 1963 |
const BLK_SIZE: usize = 0x4000; |
| 1964 |
const COUNT: usize = 1000; |
| 1965 |
const NUM_BYTES: usize = BLK_SIZE * COUNT; |
| 1966 |
|
| 1967 |
let result = new_ucmd!() |
| 1968 |
.args(&[ |
| 1969 |
"if=/dev/urandom", |
| 1970 |
&format!("bs={BLK_SIZE}"), |
| 1971 |
&format!("count={COUNT}"), |
| 1972 |
]) |
| 1973 |
.set_stdout(Stdio::piped()) |
| 1974 |
.set_stderr(Stdio::piped()) |
| 1975 |
.succeeds(); |
| 1976 |
|
| 1977 |
assert_eq!(result.stdout().len(), NUM_BYTES); |
| 1978 |
assert!(result.stderr_str().contains(&format!("{NUM_BYTES} bytes"))); |
| 1979 |
} |
| 1980 |
|
| 1981 |
#[test] |
| 1982 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 1983 |
fn test_oflag_direct_partial_block() { |
| 1984 |
// Test for issue #9003: dd should handle partial blocks with oflag=direct |
| 1985 |
// This reproduces the scenario where writing a partial block with O_DIRECT fails |
| 1986 |
|
| 1987 |
let ts = TestScenario::new(util_name!()); |
| 1988 |
let at = &ts.fixtures; |
| 1989 |
|
| 1990 |
// Create input file with size that's not a multiple of block size |
| 1991 |
// This will trigger the partial block write issue |
| 1992 |
let input_file = "test_direct_input.iso"; |
| 1993 |
let output_file = "test_direct_output.img"; |
| 1994 |
let block_size = 8192; // 8K blocks |
| 1995 |
let input_size = block_size * 3 + 511; // 3 full blocks + 511 byte partial block |
| 1996 |
|
| 1997 |
// Create test input file with known pattern |
| 1998 |
let input_data = vec![0x42; input_size]; // Use non-zero pattern for better verification |
| 1999 |
at.write_bytes(input_file, &input_data); |
| 2000 |
|
| 2001 |
// Get full paths for the dd command |
| 2002 |
let input_path = at.plus(input_file); |
| 2003 |
let output_path = at.plus(output_file); |
| 2004 |
|
| 2005 |
// Test with oflag=direct - should succeed with the fix |
| 2006 |
new_ucmd!() |
| 2007 |
.args(&[ |
| 2008 |
format!("if={}", input_path.display()), |
| 2009 |
format!("of={}", output_path.display()), |
| 2010 |
"oflag=direct".to_string(), |
| 2011 |
format!("bs={block_size}"), |
| 2012 |
"status=none".to_string(), |
| 2013 |
]) |
| 2014 |
.succeeds() |
| 2015 |
.no_output(); |
| 2016 |
assert!(output_path.exists()); |
| 2017 |
let output_size = output_path.metadata().unwrap().len() as usize; |
| 2018 |
assert_eq!(output_size, input_size); |
| 2019 |
|
| 2020 |
// Verify content matches input |
| 2021 |
let output_content = std::fs::read(&output_path).unwrap(); |
| 2022 |
assert_eq!(output_content.len(), input_size); |
| 2023 |
assert_eq!(output_content, input_data); |
| 2024 |
|
| 2025 |
// Clean up |
| 2026 |
at.remove(input_file); |
| 2027 |
at.remove(output_file); |
| 2028 |
} |
| 2029 |
|
| 2030 |
#[test] |
| 2031 |
fn test_skip_overflow() { |
| 2032 |
new_ucmd!() |
| 2033 |
.args(&["bs=1", "skip=9223372036854775808", "count=0"]) |
| 2034 |
.fails() |
| 2035 |
.stderr_contains( |
| 2036 |
"dd: invalid number: '9223372036854775808': Value too large for defined data type", |
| 2037 |
); |
| 2038 |
} |
| 2039 |
|
| 2040 |
#[test] |
| 2041 |
fn test_skip_blocks_times_ibs_overflow_does_not_wrap() { |
| 2042 |
// 17592186044416 * 1048576 == 2^64 would wrap around to 0, and |
| 2043 |
// must be rejected |
| 2044 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2045 |
at.write("in.f", "0123456789abcdef"); |
| 2046 |
ucmd.args(&["if=in.f", "skip=17592186044416", "ibs=1048576", "count=1"]) |
| 2047 |
.fails() |
| 2048 |
.no_stdout() |
| 2049 |
.stderr_contains("Value too large for defined data type"); |
| 2050 |
} |
| 2051 |
|
| 2052 |
#[test] |
| 2053 |
fn test_seek_blocks_times_obs_overflow_does_not_wrap() { |
| 2054 |
// Same as test_skip_blocks_times_ibs_overflow_does_not_wrap, |
| 2055 |
// but for `seek=`/`obs=`. |
| 2056 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2057 |
at.write("out.f", "0123456789abcdef"); |
| 2058 |
ucmd.args(&[ |
| 2059 |
"if=/dev/null", |
| 2060 |
"of=out.f", |
| 2061 |
"seek=17592186044416", |
| 2062 |
"obs=1048576", |
| 2063 |
"conv=notrunc", |
| 2064 |
]) |
| 2065 |
.fails() |
| 2066 |
.stderr_contains("Value too large for defined data type"); |
| 2067 |
// The output file must be untouched, not overwritten at offset 0. |
| 2068 |
assert_eq!(at.read("out.f"), "0123456789abcdef"); |
| 2069 |
} |
| 2070 |
|
| 2071 |
#[test] |
| 2072 |
#[cfg(target_os = "linux")] |
| 2073 |
fn test_nocache_eof() { |
| 2074 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2075 |
at.write_bytes("in.f", &vec![0u8; 1_234_567]); |
| 2076 |
ucmd.args(&[ |
| 2077 |
"if=in.f", |
| 2078 |
"of=out.f", |
| 2079 |
"bs=1M", |
| 2080 |
"oflag=nocache,sync", |
| 2081 |
"status=noxfer", |
| 2082 |
]) |
| 2083 |
.succeeds(); |
| 2084 |
assert_eq!(at.read_bytes("out.f").len(), 1_234_567); |
| 2085 |
} |
| 2086 |
|
| 2087 |
#[test] |
| 2088 |
#[cfg(target_os = "linux")] |
| 2089 |
fn test_nocache_eof_fadvise_zero_length() { |
| 2090 |
use std::process::Command; |
| 2091 |
let (at, _ucmd) = at_and_ucmd!(); |
| 2092 |
at.write_bytes("in.f", &vec![0u8; 1_234_567]); |
| 2093 |
|
| 2094 |
let strace_file = at.plus_as_string("strace.out"); |
| 2095 |
let result = Command::new("strace") |
| 2096 |
.args(["-o", &strace_file, "-e", "fadvise64,fadvise64_64"]) |
| 2097 |
.arg(get_tests_binary()) |
| 2098 |
.args([ |
| 2099 |
"dd", |
| 2100 |
"if=in.f", |
| 2101 |
"of=out.f", |
| 2102 |
"bs=1M", |
| 2103 |
"oflag=nocache,sync", |
| 2104 |
"status=none", |
| 2105 |
]) |
| 2106 |
.current_dir(at.as_string()) |
| 2107 |
.output(); |
| 2108 |
|
| 2109 |
if result.is_err() { |
| 2110 |
return; // strace not available |
| 2111 |
} |
| 2112 |
|
| 2113 |
let strace = at.read("strace.out"); |
| 2114 |
assert!( |
| 2115 |
strace.contains(", 0, POSIX_FADV_DONTNEED"), |
| 2116 |
"Expected len=0 at EOF: {strace}" |
| 2117 |
); |
| 2118 |
} |
| 2119 |
|
| 2120 |
#[test] |
| 2121 |
#[cfg(not(target_os = "openbsd"))] |
| 2122 |
fn test_iso8859_1_case_conversion() { |
| 2123 |
use std::process::Command; |
| 2124 |
// Test ISO-8859-1 case conversion for accented characters |
| 2125 |
// Skip test if required locale is not available (common in CI environments) |
| 2126 |
let locale_test = Command::new("locale") |
| 2127 |
.arg("-a") |
| 2128 |
.output() |
| 2129 |
.ok() |
| 2130 |
.and_then(|output| String::from_utf8(output.stdout).ok()) |
| 2131 |
.is_some_and(|locales| locales.contains("fr_FR")); |
| 2132 |
|
| 2133 |
if !locale_test { |
| 2134 |
eprintln!("Skipping ISO-8859-1 test: French locale not available"); |
| 2135 |
return; |
| 2136 |
} |
| 2137 |
|
| 2138 |
let locale = "fr_FR"; |
| 2139 |
|
| 2140 |
// É (0xC9) should convert to é (0xE9) with lcase |
| 2141 |
let input = vec![0xC9, 0x0A]; // É\n in ISO-8859-1 |
| 2142 |
let expected = vec![0xE9, 0x0A]; // é\n in ISO-8859-1 |
| 2143 |
let result = new_ucmd!() |
| 2144 |
.args(&["conv=lcase", "status=none"]) |
| 2145 |
.env("LC_ALL", locale) |
| 2146 |
.pipe_in(input) |
| 2147 |
.succeeds(); |
| 2148 |
assert_eq!(result.stdout(), expected); |
| 2149 |
|
| 2150 |
// é (0xE9) should convert to É (0xC9) with ucase |
| 2151 |
let input = vec![0xE9, 0x0A]; // é\n in ISO-8859-1 |
| 2152 |
let expected = vec![0xC9, 0x0A]; // É\n in ISO-8859-1 |
| 2153 |
let result = new_ucmd!() |
| 2154 |
.args(&["conv=ucase", "status=none"]) |
| 2155 |
.env("LC_ALL", locale) |
| 2156 |
.pipe_in(input) |
| 2157 |
.succeeds(); |
| 2158 |
assert_eq!(result.stdout(), expected); |
| 2159 |
} |
| 2160 |
|
| 2161 |
#[test] |
| 2162 |
fn test_locale_aware_case_conversion() { |
| 2163 |
// Test that case conversion respects different single-byte locales |
| 2164 |
|
| 2165 |
// Test Turkish (ISO-8859-9) where 'I' has special behavior |
| 2166 |
// Turkish has İ (0xDD) ↔ i (0xFD) and I (0x49) ↔ ı (0xFD in some positions) |
| 2167 |
// For simplicity, test some basic accented characters that differ between locales |
| 2168 |
|
| 2169 |
// Test with ISO-8859-9 (Turkish) - Ğ (0xD0) should convert to ğ (0xF0) |
| 2170 |
let input = vec![0xD0, 0x0A]; // Ğ\n in ISO-8859-9 |
| 2171 |
let expected = vec![0xF0, 0x0A]; // ğ\n in ISO-8859-9 |
| 2172 |
let result = new_ucmd!() |
| 2173 |
.args(&["conv=lcase", "status=none"]) |
| 2174 |
.env("LC_ALL", "tr_TR.iso8859-9") |
| 2175 |
.pipe_in(input) |
| 2176 |
.succeeds(); |
| 2177 |
|
| 2178 |
// Note: This test may not work if the system doesn't have Turkish locale installed |
| 2179 |
// In that case, it should fall back to C locale behavior |
| 2180 |
if result.stdout() == expected { |
| 2181 |
println!("Turkish locale case conversion working correctly"); |
| 2182 |
} else { |
| 2183 |
println!("Turkish locale not available, using fallback behavior"); |
| 2184 |
// Test that it at least doesn't crash and produces some output |
| 2185 |
assert!(!result.stdout().is_empty()); |
| 2186 |
} |
| 2187 |
} |
| 2188 |
|
| 2189 |
#[test] |
| 2190 |
fn test_french_locale_case_conversion() { |
| 2191 |
// Test French (ISO-8859-1) case conversion for French accented characters |
| 2192 |
// This test uses the same charset as the previous ISO-8859-1 test but with French locale |
| 2193 |
|
| 2194 |
// Test French accented characters: À (0xC0) should convert to à (0xE0) with lcase |
| 2195 |
let input = vec![0xC0, 0x0A]; // À\n in ISO-8859-1 |
| 2196 |
let expected = vec![0xE0, 0x0A]; // à\n in ISO-8859-1 |
| 2197 |
let result = new_ucmd!() |
| 2198 |
.args(&["conv=lcase", "status=none"]) |
| 2199 |
.env("LC_ALL", "fr_FR.iso8859-1") |
| 2200 |
.pipe_in(input) |
| 2201 |
.succeeds(); |
| 2202 |
|
| 2203 |
// Note: This test may not work if the system doesn't have French locale installed |
| 2204 |
// In that case, it should fall back to C locale behavior |
| 2205 |
if result.stdout() == expected { |
| 2206 |
println!("French locale case conversion working correctly for À -> à"); |
| 2207 |
} else { |
| 2208 |
println!("French locale not available, using fallback behavior"); |
| 2209 |
// Test that it at least doesn't crash and produces some output |
| 2210 |
assert!(!result.stdout().is_empty()); |
| 2211 |
} |
| 2212 |
|
| 2213 |
// Test reverse conversion: à (0xE0) should convert to À (0xC0) with ucase |
| 2214 |
let input = vec![0xE0, 0x0A]; // à\n in ISO-8859-1 |
| 2215 |
let expected = vec![0xC0, 0x0A]; // À\n in ISO-8859-1 |
| 2216 |
let result = new_ucmd!() |
| 2217 |
.args(&["conv=ucase", "status=none"]) |
| 2218 |
.env("LC_ALL", "fr_FR.iso8859-1") |
| 2219 |
.pipe_in(input) |
| 2220 |
.succeeds(); |
| 2221 |
|
| 2222 |
if result.stdout() == expected { |
| 2223 |
println!("French locale case conversion working correctly for à -> À"); |
| 2224 |
} else { |
| 2225 |
println!("French locale not available for reverse conversion, using fallback behavior"); |
| 2226 |
assert!(!result.stdout().is_empty()); |
| 2227 |
} |
| 2228 |
|
| 2229 |
// Test another French character: Ç (0xC7) should convert to ç (0xE7) with lcase |
| 2230 |
let input = vec![0xC7, 0x0A]; // Ç\n in ISO-8859-1 |
| 2231 |
let expected = vec![0xE7, 0x0A]; // ç\n in ISO-8859-1 |
| 2232 |
let result = new_ucmd!() |
| 2233 |
.args(&["conv=lcase", "status=none"]) |
| 2234 |
.env("LC_ALL", "fr_FR.iso8859-1") |
| 2235 |
.pipe_in(input) |
| 2236 |
.succeeds(); |
| 2237 |
|
| 2238 |
if result.stdout() == expected { |
| 2239 |
println!("French locale case conversion working correctly for Ç -> ç"); |
| 2240 |
} else { |
| 2241 |
println!("French locale not available for Ç conversion, using fallback behavior"); |
| 2242 |
assert!(!result.stdout().is_empty()); |
| 2243 |
} |
| 2244 |
} |
| 2245 |
|
| 2246 |
#[test] |
| 2247 |
fn test_ascii_case_conversion_fallback() { |
| 2248 |
// Test that ASCII characters always convert correctly regardless of locale |
| 2249 |
let input = vec![b'A', b'B', b'C', 0x0A]; // ABC\n |
| 2250 |
let expected = vec![b'a', b'b', b'c', 0x0A]; // abc\n |
| 2251 |
let result = new_ucmd!() |
| 2252 |
.args(&["conv=lcase", "status=none"]) |
| 2253 |
.env("LC_ALL", "C") |
| 2254 |
.pipe_in(input.clone()) |
| 2255 |
.succeeds(); |
| 2256 |
assert_eq!(result.stdout(), expected); |
| 2257 |
|
| 2258 |
// Test reverse conversion |
| 2259 |
let result = new_ucmd!() |
| 2260 |
.args(&["conv=ucase", "status=none"]) |
| 2261 |
.env("LC_ALL", "C") |
| 2262 |
.pipe_in(expected) |
| 2263 |
.succeeds(); |
| 2264 |
assert_eq!(result.stdout(), input); |
| 2265 |
} |
| 2266 |
|
| 2267 |
#[test] |
| 2268 |
fn test_bs_not_positive() { |
| 2269 |
for bs in [-5, 0, 0x0] { |
| 2270 |
for bs_param in ["bs", "ibs", "obs", "cbs"] { |
| 2271 |
new_ucmd!() |
| 2272 |
.args(&[format!("{bs_param}={bs}")]) |
| 2273 |
.fails() |
| 2274 |
.no_stdout() |
| 2275 |
.code_is(1) |
| 2276 |
.stderr_is(format!("dd: invalid number: '{bs}'\n")); |
| 2277 |
} |
| 2278 |
} |
| 2279 |
} |
| 2280 |
|
| 2281 |
#[test] |
| 2282 |
fn test_count_bytes_with_expanding_block_conv() { |
| 2283 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2284 |
let mut input = vec![b'a'; 1000]; |
| 2285 |
input.extend([b'Z'; 24]); |
| 2286 |
at.write_bytes("input.txt", &input); |
| 2287 |
ucmd.args(&[ |
| 2288 |
"if=input.txt", |
| 2289 |
"of=output.bin", |
| 2290 |
"conv=block", |
| 2291 |
"cbs=1024", |
| 2292 |
"count=1000", |
| 2293 |
"iflag=count_bytes", |
| 2294 |
]) |
| 2295 |
.succeeds(); |
| 2296 |
let output = at.read_bytes("output.bin"); |
| 2297 |
assert_eq!(bytecount::count(&output, b'a'), 1000); |
| 2298 |
assert!(!output.contains(&b'Z')); |
| 2299 |
} |
| 2300 |
|
| 2301 |
// A failed copy still has to report what it transferred, including complete |
| 2302 |
// and partial records. |
| 2303 |
#[test] |
| 2304 |
#[cfg(all(unix, not(target_vendor = "apple")))] |
| 2305 |
fn test_stats_are_reported_when_a_write_fails() { |
| 2306 |
use rlimit::Resource; |
| 2307 |
|
| 2308 |
// Restores the previous SIGXFSZ disposition even if an assertion panics. |
| 2309 |
struct SigxfszGuard(libc::sighandler_t); |
| 2310 |
impl Drop for SigxfszGuard { |
| 2311 |
fn drop(&mut self) { |
| 2312 |
// SAFETY: restoring the disposition saved below. |
| 2313 |
unsafe { libc::signal(libc::SIGXFSZ, self.0) }; |
| 2314 |
} |
| 2315 |
} |
| 2316 |
|
| 2317 |
const CAP: u64 = 768 * 1024; |
| 2318 |
|
| 2319 |
// The child inherits the ignored SIGXFSZ, so exceeding RLIMIT_FSIZE shows |
| 2320 |
// up as a short write() instead of killing the process. |
| 2321 |
// SAFETY: signal() with SIG_IGN is async-signal-safe and the guard puts |
| 2322 |
// the old handler back. |
| 2323 |
let _sigxfsz = SigxfszGuard(unsafe { libc::signal(libc::SIGXFSZ, libc::SIG_IGN) }); |
| 2324 |
|
| 2325 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2326 |
let result = ucmd |
| 2327 |
.args(&["if=/dev/zero", "of=capped.bin", "bs=512K", "count=3"]) |
| 2328 |
.limit(Resource::FSIZE, CAP, CAP) |
| 2329 |
.fails(); |
| 2330 |
|
| 2331 |
// Under a 768 KiB cap, the first 512 KiB block is written in full, the |
| 2332 |
// second one is cut short at 256 KiB, and the third write fails. |
| 2333 |
result.stderr_contains("1+1 records out"); |
| 2334 |
result.stderr_contains("786432 bytes"); |
| 2335 |
assert_eq!(at.metadata("capped.bin").len(), CAP); |
| 2336 |
} |
| 2337 |
|
| 2338 |
#[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))] |
| 2339 |
mod diagnostics { |
| 2340 |
use super::*; |
| 2341 |
|
| 2342 |
#[cfg(unix)] |
| 2343 |
#[test] |
| 2344 |
fn test_snippet_points_at_the_unrecognized_key() { |
| 2345 |
let result = new_ucmd!() |
| 2346 |
.terminal_sim_stderr() |
| 2347 |
.args(&["bsx=1"]) |
| 2348 |
.pipe_in("") |
| 2349 |
.fails_with_code(1); |
| 2350 |
|
| 2351 |
// The caret takes the key alone: the value is fine, it is the operand |
| 2352 |
// name that dd does not know. The usage hint that follows names the |
| 2353 |
// binary as it was invoked, so it is checked apart from the report. |
| 2354 |
let stderr = result.stderr_as_displayed(); |
| 2355 |
let (report, hint) = stderr.trim_end().rsplit_once('\n').unwrap(); |
| 2356 |
assert_eq!( |
| 2357 |
report, |
| 2358 |
"\ |
| 2359 |
dd: unrecognized operand 'bsx=1' |
| 2360 |
╭─[ dd:1:4 ] |
| 2361 |
│ |
| 2362 |
1 │ dd bsx=1 |
| 2363 |
│ ─── |
| 2364 |
│ |
| 2365 |
│ Help: an operand is KEY=VALUE, as in if=file bs=4k count=10 |
| 2366 |
───╯" |
| 2367 |
); |
| 2368 |
assert!( |
| 2369 |
hint.ends_with("dd --help' for more information."), |
| 2370 |
"{stderr}" |
| 2371 |
); |
| 2372 |
} |
| 2373 |
|
| 2374 |
#[cfg(unix)] |
| 2375 |
#[test] |
| 2376 |
fn test_snippet_points_at_the_failing_flag_of_a_list() { |
| 2377 |
let result = new_ucmd!() |
| 2378 |
.terminal_sim_stderr() |
| 2379 |
.args(&["conv=ucase,zap"]) |
| 2380 |
.pipe_in("") |
| 2381 |
.fails_with_code(1); |
| 2382 |
let stderr = result.stderr_as_displayed(); |
| 2383 |
|
| 2384 |
// Only the second flag is wrong, and the caret says so. |
| 2385 |
assert!(stderr.contains("dd:1:15"), "{stderr}"); |
| 2386 |
assert!(stderr.contains("1 │ dd conv=ucase,zap"), "{stderr}"); |
| 2387 |
} |
| 2388 |
|
| 2389 |
#[cfg(unix)] |
| 2390 |
#[test] |
| 2391 |
fn test_snippet_names_the_flags_rather_than_the_commas() { |
| 2392 |
let result = new_ucmd!() |
| 2393 |
.terminal_sim_stderr() |
| 2394 |
.args(&["conv=ucase,zap"]) |
| 2395 |
.pipe_in("") |
| 2396 |
.fails_with_code(1); |
| 2397 |
let stderr = result.stderr_as_displayed(); |
| 2398 |
|
| 2399 |
// The list syntax parsed: it is the flag that is unknown, so the |
| 2400 |
// report names the conversions instead of explaining commas. |
| 2401 |
assert!(stderr.contains("not a known conversion"), "{stderr}"); |
| 2402 |
assert!(stderr.contains("conv= is one of ascii"), "{stderr}"); |
| 2403 |
} |
| 2404 |
|
| 2405 |
#[cfg(unix)] |
| 2406 |
#[test] |
| 2407 |
fn test_snippet_points_at_the_failing_flag_and_not_the_one_it_starts() { |
| 2408 |
let result = new_ucmd!() |
| 2409 |
.terminal_sim_stderr() |
| 2410 |
.args(&["conv=notrunc,not"]) |
| 2411 |
.pipe_in("") |
| 2412 |
.fails_with_code(1); |
| 2413 |
let stderr = result.stderr_as_displayed(); |
| 2414 |
|
| 2415 |
// `not` also opens the `notrunc` in front of it, and the caret belongs |
| 2416 |
// on the flag that failed rather than on the one that parsed. |
| 2417 |
assert!(stderr.contains("dd:1:17"), "{stderr}"); |
| 2418 |
assert!( |
| 2419 |
stderr.contains("1 \u{2502} dd conv=notrunc,not"), |
| 2420 |
"{stderr}" |
| 2421 |
); |
| 2422 |
} |
| 2423 |
|
| 2424 |
#[cfg(unix)] |
| 2425 |
#[test] |
| 2426 |
fn test_snippet_points_at_the_value_of_a_count() { |
| 2427 |
let result = new_ucmd!() |
| 2428 |
.terminal_sim_stderr() |
| 2429 |
.args(&["count=8x"]) |
| 2430 |
.pipe_in("") |
| 2431 |
.fails_with_code(1); |
| 2432 |
let stderr = result.stderr_as_displayed(); |
| 2433 |
|
| 2434 |
assert!(stderr.contains("dd:1:10"), "{stderr}"); |
| 2435 |
assert!(stderr.contains("a number may be followed by"), "{stderr}"); |
| 2436 |
} |
| 2437 |
|
| 2438 |
#[cfg(unix)] |
| 2439 |
#[test] |
| 2440 |
fn test_snippet_keeps_the_try_help_hint_of_a_flag_message() { |
| 2441 |
let result = new_ucmd!() |
| 2442 |
.terminal_sim_stderr() |
| 2443 |
.args(&["iflag=nope"]) |
| 2444 |
.pipe_in("") |
| 2445 |
.fails_with_code(1); |
| 2446 |
let stderr = result.stderr_as_displayed(); |
| 2447 |
|
| 2448 |
assert!(stderr.contains("dd:1:10"), "{stderr}"); |
| 2449 |
// The caret replaces the message, not the usage hint: a pipe and a |
| 2450 |
// terminal must not disagree on whether one was printed. |
| 2451 |
assert!( |
| 2452 |
stderr |
| 2453 |
.trim_end() |
| 2454 |
.ends_with("dd --help' for more information."), |
| 2455 |
"{stderr}" |
| 2456 |
); |
| 2457 |
} |
| 2458 |
|
| 2459 |
#[test] |
| 2460 |
fn test_plain_message_keeps_the_try_help_hint_of_a_flag_message() { |
| 2461 |
new_ucmd!() |
| 2462 |
.args(&["iflag=nope"]) |
| 2463 |
.pipe_in("") |
| 2464 |
.fails_with_code(1) |
| 2465 |
.stderr_contains("dd: invalid input flag: 'nope'") |
| 2466 |
.stderr_contains("--help' for more information."); |
| 2467 |
} |
| 2468 |
|
| 2469 |
#[test] |
| 2470 |
fn test_plain_message_when_stderr_is_a_pipe() { |
| 2471 |
new_ucmd!() |
| 2472 |
.args(&["bsx=1"]) |
| 2473 |
.pipe_in("") |
| 2474 |
.fails_with_code(1) |
| 2475 |
.stderr_contains("dd: unrecognized operand 'bsx=1'\n") |
| 2476 |
.stderr_contains("--help' for more information.") |
| 2477 |
.stderr_does_not_contain("╭─"); |
| 2478 |
} |
| 2479 |
|
| 2480 |
// The three below cover the shared switch in `uucore::diagnostics`. |
| 2481 |
|
| 2482 |
#[test] |
| 2483 |
fn test_report_is_drawn_into_a_pipe_when_asked_for() { |
| 2484 |
let result = new_ucmd!() |
| 2485 |
.env("UUTILS_DIAG", "always") |
| 2486 |
.args(&["bsx=1"]) |
| 2487 |
.pipe_in("") |
| 2488 |
.fails_with_code(1); |
| 2489 |
let stderr = result.stderr_str(); |
| 2490 |
|
| 2491 |
// No terminal anywhere, and the report is drawn all the same. |
| 2492 |
assert!(stderr.contains("dd:1:4"), "{stderr}"); |
| 2493 |
assert!(stderr.contains("1 \u{2502} dd bsx=1"), "{stderr}"); |
| 2494 |
} |
| 2495 |
|
| 2496 |
#[cfg(unix)] |
| 2497 |
#[test] |
| 2498 |
fn test_plain_message_at_a_terminal_when_asked_for() { |
| 2499 |
let result = new_ucmd!() |
| 2500 |
.env("UUTILS_DIAG", "never") |
| 2501 |
.terminal_sim_stderr() |
| 2502 |
.args(&["bsx=1"]) |
| 2503 |
.pipe_in("") |
| 2504 |
.fails_with_code(1); |
| 2505 |
let stderr = result.stderr_as_displayed(); |
| 2506 |
|
| 2507 |
assert!( |
| 2508 |
stderr.contains("dd: unrecognized operand 'bsx=1'"), |
| 2509 |
"{stderr}" |
| 2510 |
); |
| 2511 |
assert!(!stderr.contains('\u{256d}'), "{stderr}"); |
| 2512 |
} |
| 2513 |
|
| 2514 |
#[test] |
| 2515 |
fn test_unknown_mode_leaves_the_terminal_in_charge() { |
| 2516 |
// A value nobody meant behaves as if the variable were unset. |
| 2517 |
new_ucmd!() |
| 2518 |
.env("UUTILS_DIAG", "sometimes") |
| 2519 |
.args(&["bsx=1"]) |
| 2520 |
.pipe_in("") |
| 2521 |
.fails_with_code(1) |
| 2522 |
.stderr_contains("dd: unrecognized operand 'bsx=1'\n") |
| 2523 |
.stderr_does_not_contain("╭─"); |
| 2524 |
} |
| 2525 |
} |