Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 2 additions & 122 deletions src/uu/dd/src/dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,26 +817,9 @@ 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 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,
}
use crate::{calc_bsize, Output, Parser};

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])
}
}
use std::path::Path;

#[test]
fn bsize_test_primes() {
Expand Down Expand Up @@ -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();
}
}
Binary file not shown.
Binary file removed src/uu/dd/test-resources/deadbeef-16.spec
Binary file not shown.
1 change: 0 additions & 1 deletion src/uu/dd/test-resources/deadbeef-16.test

This file was deleted.

Binary file not shown.
69 changes: 69 additions & 0 deletions tests/by-util/test_dd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment thread
jfinkels marked this conversation as resolved.
Outdated
#[cfg(not(windows))]
use std::time::Duration;
Comment thread
sylvestre marked this conversation as resolved.
Outdated
use tempfile::tempfile;

macro_rules! inf {
Expand Down Expand Up @@ -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!()
Expand Down Expand Up @@ -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");
}