From 47464f50a1cd3c3a4683c2ee4d91d01ffdf3b11b Mon Sep 17 00:00:00 2001 From: Tyler Date: Mon, 21 Jun 2021 11:17:47 -0700 Subject: [PATCH] Begin project level testing. Minor fixes. --- src/uu/dd/src/dd.rs | 19 +- .../dd/src/dd_unit_tests/conversion_tests.rs | 25 +- tests/by-util/test_dd.rs | 252 ++++++++++++++---- 3 files changed, 229 insertions(+), 67 deletions(-) diff --git a/src/uu/dd/src/dd.rs b/src/uu/dd/src/dd.rs index 585300d68..459c66e54 100644 --- a/src/uu/dd/src/dd.rs +++ b/src/uu/dd/src/dd.rs @@ -361,7 +361,6 @@ impl Read for Input { fn read(&mut self, mut buf: &mut [u8]) -> io::Result { - // Read from source, ignore read errors if conv=noerror match self.src.read(&mut buf) { Ok(len) => @@ -1052,11 +1051,11 @@ fn make_prog_line(update: &ProgUpdate) -> String .get_appropriate_unit(false) .format(1); - format!("{} bytes ({}, {}) copied, {} s, {}/s", + format!("{} bytes ({}, {}) copied, {:.1} s, {}/s", update.bytes_total, btotal_metric, btotal_bin, - safe_millis * 1000, + update.duration.as_secs_f64(), xfer_rate ).to_string() } @@ -1066,7 +1065,7 @@ fn reprint_prog_line(update: &ProgUpdate) } fn print_prog_line(update: &ProgUpdate) { - eprint!("{}", make_prog_line(update)); + eprintln!("{}", make_prog_line(update)); } fn print_xfer_stats(update: &ProgUpdate) { @@ -1387,6 +1386,18 @@ macro_rules! build_app ( if it is smaller than the block size.", "BYTES" ) + .optopt( + "", + "iflag", + "read as per the comma separated symbol list of flags", + "FLAG" + ) + .optopt( + "", + "oflag", + "write as per the comma separated symbol list of flags", + "FLAG" + ) .optopt( "", "if", diff --git a/src/uu/dd/src/dd_unit_tests/conversion_tests.rs b/src/uu/dd/src/dd_unit_tests/conversion_tests.rs index f7ee7f92c..a1a7ad892 100644 --- a/src/uu/dd/src/dd_unit_tests/conversion_tests.rs +++ b/src/uu/dd/src/dd_unit_tests/conversion_tests.rs @@ -177,20 +177,23 @@ fn all_valid_ascii_ebcdic_ascii_roundtrip_conv_test() dd_fileout(i,o).unwrap(); - let res = { - let res = File::open(&tmp_fname_ea).unwrap(); - let res = BufReader::new(res); + // Final Comparison + let res = File::open(&tmp_fname_ea).unwrap(); + let spec = File::open("./test-resources/all-valid-ascii-chars-37eff01866ba3f538421b30b7cbefcac.test").unwrap(); - let mut h = Md5::new(); - for b in res.bytes() - { - h.update([b.unwrap()]); - } + assert_eq!(res.metadata().unwrap().len(), spec.metadata().unwrap().len()); - h.finalize() - }; + let res = BufReader::new(res); + let spec = BufReader::new(spec); - assert_eq!(hex!("37eff01866ba3f538421b30b7cbefcac"), res[..]); + let res = BufReader::new(res); + + // Check all bytes match + for (b_res, b_spec) in res.bytes().zip(spec.bytes()) + { + assert_eq!(b_res.unwrap(), + b_spec.unwrap()); + } fs::remove_file(&tmp_fname_ae).unwrap(); fs::remove_file(&tmp_fname_ea).unwrap(); diff --git a/tests/by-util/test_dd.rs b/tests/by-util/test_dd.rs index 40ea7e430..b9eb7f3ac 100644 --- a/tests/by-util/test_dd.rs +++ b/tests/by-util/test_dd.rs @@ -1,61 +1,209 @@ use crate::common::util::*; -use std::io::prelude::*; -use std::io::BufReader; -use std::fs::{self, File}; - #[test] -fn dd_zeros_to_stdout_test_from_args() +fn version() { - new_ucmd!() - .args(&[ - "if=../../src/uu/dd/test-resources/zeros-620f0b67a91f7f74151bc5be745b7110.test", - ]) - .succeeds() - .stdout_only( - "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\ - 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000\ - " - ); + new_ucmd!() + .args(&["--version"]) + .succeeds(); } #[test] -fn dd_ones_to_file_test_from_args() +fn help() { - let tmp_fname = "../../src/uu/dd/test-resources/FAILED-ones-to-file-from-args.test"; - - new_ucmd!() - .args(&[ - "if=../../src/uu/dd/test-resources/ones-6ae59e64850377ee5470c854761551ea.test", - &format!("of={}", &tmp_fname), - ]) - .succeeds(); - - let res = File::open(&tmp_fname).unwrap(); - let res = BufReader::new(res); - - let spec = File::open("../../src/uu/dd/test-resources/ones-6ae59e64850377ee5470c854761551ea.test").unwrap(); - let spec = BufReader::new(spec); - - for (b_res, b_spec) in res.bytes().zip(spec.bytes()) - { - assert_eq!(b_res.unwrap(), - b_spec.unwrap()); - } - - fs::remove_file(&tmp_fname).unwrap(); + new_ucmd!() + .args(&["--help"]) + .succeeds(); } + +fn build_ascii_block(n: usize) -> Vec +{ + vec!['a', 'b', 'c', 'd', 'e', 'f'] + .into_iter() + .map(|c| c as u8) + .cycle() + .take(n) + .collect() +} + +#[test] +fn test_stdin_stdout() +{ + let input = build_ascii_block(521); + let output = String::from_utf8(input.clone()).unwrap(); + new_ucmd!() + .args(&["status=none"]) + .pipe_in(input) + .succeeds() + .stdout_only(output); +} + +#[test] +fn test_stdin_stdout_count() +{ + let input = build_ascii_block(521); + let mut output = String::from_utf8(input.clone()).unwrap(); + output.truncate(256); + new_ucmd!() + .args(&[ + "status=none", + "count=2", + "ibs=128", + ]) + .pipe_in(input) + .succeeds() + .stdout_only(output); +} + +#[test] +fn test_stdin_stdout_count_bytes() +{ + let input = build_ascii_block(521); + let mut output = String::from_utf8(input.clone()).unwrap(); + output.truncate(256); + new_ucmd!() + .args(&[ + "status=none", + "count=256", + "iflag=count_bytes", + ]) + .pipe_in(input) + .succeeds() + .stdout_only(output); +} + +#[test] +fn test_stdin_stdout_skip() +{ + let input = build_ascii_block(521); + let mut output = String::from_utf8(input.clone()).unwrap(); + let _ = output.drain(..256); + new_ucmd!() + .args(&[ + "status=none", + "skip=2", + "ibs=128", + ]) + .pipe_in(input) + .succeeds() + .stdout_only(output); +} + +#[test] +fn test_stdin_stdout_skip_bytes() +{ + let input = build_ascii_block(521); + let mut output = String::from_utf8(input.clone()).unwrap(); + let _ = output.drain(..256); + new_ucmd!() + .args(&[ + "status=none", + "skip=256", + "ibs=128", + "iflag=skip_bytes", + ]) + .pipe_in(input) + .succeeds() + .stdout_only(output); +} + +#[test] +fn test_final_stats_noxfer() +{ + new_ucmd!() + .args(&[ + "status=noxfer", + ]) + .succeeds() + .stderr_only(""); +} + +#[test] +fn test_final_stats_unspec() +{ + let output = vec![ + "0+0 records in", + "0+0 records out", + "0 bytes (0 B, 0 B) copied, 0.0 s, 0 B/s", + ]; + let output = output.into_iter() + .fold(String::new(), | mut acc, s | { + acc.push_str(s); + acc.push('\n'); + acc + }); + new_ucmd!() + .succeeds() + .stderr_only(&output); +} + +#[test] +fn test_self_transfer() +{ + panic!(); + // TODO: Make new copy per-test + new_ucmd!() + .args(&[ + "conv=notruc", + "if=../fixtures/dd/zero-256k.copy", + "of=../fixtures/dd/zero-256k.copy", + ]) + .succeeds(); + assert!(false/* Must check that zero256k.copy still == zero-256k.txt */) +} + +#[cfg(unix)] +#[test] +fn test_null() +{ + let stats = vec![ + "0+0 records in", + "0+0 records out", + "0 bytes (0 B, 0 B) copied, 0.0 s, 0 B/s", + ]; + let stats = stats.into_iter() + .fold(String::new(), | mut acc, s | { + acc.push_str(s); + acc.push('\n'); + acc + }); + new_ucmd!() + .args(&[ + "if=/dev/null", + ]) + .succeeds() + .stderr_only(stats) + .stdout_only(""); +} + +#[test] +fn test_ys_to_stdout() +{ + let output: Vec<_> = String::from("y\n") + .bytes() + .cycle() + .take(1024) + .collect(); + let output = String::from_utf8(output).unwrap(); + + new_ucmd!() + .args(&[ + "if=../fixtures/dd/y-nl-1k.txt", + ]) + .run() + .stdout_only(output); +} + +#[test] +fn test_zeros_to_stdout() +{ + let output = vec![0; 256*1024]; + let output = String::from_utf8(output).unwrap(); + new_ucmd!() + .args(&[ + "if=../fixtures/dd/zero-256k.txt", + ]) + .run() + .stdout_only(output); +} +