1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

dd: move tests of slow reader to test_dd.rs

Move some tests that simulate a slow reader from `dd.rs` to
`tests/by-util/test_dd.rs`, and employ a FIFO and `sleep()` to
simulate the slow reader instead of a custom struct that implements
`Read`. This change restricts the type of `Input`s the
`Output::dd_out()` function can accept, facilitating a future change
to make `Input` an enum.
This commit is contained in:
Jeffrey Finkelstein 2022-11-18 22:45:34 -05:00
parent 5acb0227aa
commit 903ad1656d
6 changed files with 70 additions and 122 deletions

View file

@ -817,27 +817,10 @@ pub fn uu_app() -> Command {
#[cfg(test)]
mod tests {
use crate::datastructures::{IConvFlags, IFlags};
use crate::{calc_bsize, Density, Dest, Input, Output, Parser, Settings};
use crate::{calc_bsize, Output, Parser};
use std::cmp;
use std::fs;
use std::fs::File;
use std::io;
use std::io::{BufReader, Read};
use std::path::Path;
struct LazyReader<R: Read> {
src: R,
}
impl<R: Read> Read for LazyReader<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let reduced = cmp::max(buf.len() / 2, 1);
self.src.read(&mut buf[..reduced])
}
}
#[test]
fn bsize_test_primes() {
let (n, m) = (7901, 7919);
@ -916,107 +899,4 @@ mod tests {
Output::new_file(Path::new(settings.outfile.as_ref().unwrap()), &settings).is_err()
);
}
#[test]
fn test_deadbeef_16_delayed() {
let settings = Settings {
ibs: 16,
obs: 32,
count: None,
iconv: IConvFlags {
sync: Some(0),
..Default::default()
},
..Default::default()
};
let input = Input {
src: LazyReader {
src: File::open("./test-resources/deadbeef-16.test").unwrap(),
},
settings: &settings,
};
let output = Output {
dst: Dest::File(
File::create("./test-resources/FAILED-deadbeef-16-delayed.test").unwrap(),
Density::Dense,
),
settings: &settings,
};
output.dd_out(input).unwrap();
let tmp_fname = "./test-resources/FAILED-deadbeef-16-delayed.test";
let spec = File::open("./test-resources/deadbeef-16.spec").unwrap();
let res = File::open(tmp_fname).unwrap();
// Check test file isn't empty (unless spec file is too)
assert_eq!(
res.metadata().unwrap().len(),
spec.metadata().unwrap().len()
);
let spec = BufReader::new(spec);
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).unwrap();
}
#[test]
fn test_random_73k_test_lazy_fullblock() {
let settings = Settings {
ibs: 521,
obs: 1031,
count: None,
iflags: IFlags {
fullblock: true,
..IFlags::default()
},
..Default::default()
};
let input = Input {
src: LazyReader {
src: File::open("./test-resources/random-5828891cb1230748e146f34223bbd3b5.test")
.unwrap(),
},
settings: &settings,
};
let output = Output {
dst: Dest::File(
File::create("./test-resources/FAILED-random_73k_test_lazy_fullblock.test")
.unwrap(),
Density::Dense,
),
settings: &settings,
};
output.dd_out(input).unwrap();
let tmp_fname = "./test-resources/FAILED-random_73k_test_lazy_fullblock.test";
let spec =
File::open("./test-resources/random-5828891cb1230748e146f34223bbd3b5.test").unwrap();
let res = File::open(tmp_fname).unwrap();
// Check test file isn't empty (unless spec file is too)
assert_eq!(
res.metadata().unwrap().len(),
spec.metadata().unwrap().len()
);
let spec = BufReader::new(spec);
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).unwrap();
}
}

View file

@ -1 +0,0 @@
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>

View file

@ -5,6 +5,10 @@ use crate::common::util::*;
use std::fs::{File, OpenOptions};
use std::io::{BufReader, Read, Write};
use std::path::PathBuf;
#[cfg(not(windows))]
use std::thread::sleep;
#[cfg(not(windows))]
use std::time::Duration;
use tempfile::tempfile;
macro_rules! inf {
@ -1007,6 +1011,38 @@ fn test_random_73k_test_obs_lt_not_a_multiple_ibs() {
.stdout_is_fixture_bytes("random-5828891cb1230748e146f34223bbd3b5.test");
}
#[cfg(not(windows))]
#[test]
fn test_random_73k_test_lazy_fullblock() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkfifo("fifo");
let child = ucmd
.args(&[
"ibs=521",
"obs=1031",
"iflag=fullblock",
"if=fifo",
"status=noxfer",
])
.run_no_wait();
let data = at.read_bytes("random-5828891cb1230748e146f34223bbd3b5.test");
{
let mut fifo = OpenOptions::new()
.write(true)
.open(at.plus("fifo"))
.unwrap();
for chunk in data.chunks(521 / 2) {
fifo.write_all(chunk).unwrap();
sleep(Duration::from_millis(10));
}
}
let output = child.wait_with_output().unwrap();
assert!(output.status.success());
assert_eq!(&output.stdout, &data);
assert_eq!(&output.stderr, b"142+1 records in\n72+1 records out\n");
}
#[test]
fn test_deadbeef_all_32k_test_count_reads() {
new_ucmd!()
@ -1325,3 +1361,36 @@ fn test_bytes_suffix() {
.succeeds()
.stdout_only("\0\0\0abcdef");
}
/// Test for "conv=sync" with a slow reader.
#[cfg(not(windows))]
#[test]
fn test_sync_delayed_reader() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkfifo("fifo");
let child = ucmd
.args(&["ibs=16", "obs=32", "conv=sync", "if=fifo", "status=noxfer"])
.run_no_wait();
{
let mut fifo = OpenOptions::new()
.write(true)
.open(at.plus("fifo"))
.unwrap();
for _ in 0..8 {
fifo.write_all(&[0xF; 8]).unwrap();
sleep(Duration::from_millis(10));
}
}
let output = child.wait_with_output().unwrap();
assert!(output.status.success());
// Expected output is 0xFFFFFFFF00000000FFFFFFFF00000000...
let mut expected: [u8; 8 * 16] = [0; 8 * 16];
for i in 0..8 {
for j in 0..8 {
expected[16 * i + j] = 0xF;
}
}
assert_eq!(&output.stdout, &expected);
assert_eq!(&output.stderr, b"0+8 records in\n4+0 records out\n");
}