Add initial tests for hashsum.

This commit is contained in:
Joseph Crail 2015-05-16 18:03:09 -04:00
parent 1837146134
commit 9d74bbe532
9 changed files with 107 additions and 0 deletions

View file

@ -167,6 +167,7 @@ TEST_PROGS := \
factor \
false \
fold \
hashsum \
mkdir \
mv \
nl \

1
test/fixtures/hashsum/input.txt vendored Normal file
View file

@ -0,0 +1 @@
hello, world

1
test/fixtures/hashsum/md5.expected vendored Normal file
View file

@ -0,0 +1 @@
e4d7f1b4ed2e42d15898f4b27b019da4

1
test/fixtures/hashsum/sha1.expected vendored Normal file
View file

@ -0,0 +1 @@
b7e23ec29af22b0b4e41da31e868d57226121c84

1
test/fixtures/hashsum/sha224.expected vendored Normal file
View file

@ -0,0 +1 @@
6e1a93e32fb44081a401f3db3ef2e6e108b7bbeeb5705afdaf01fb27

1
test/fixtures/hashsum/sha256.expected vendored Normal file
View file

@ -0,0 +1 @@
09ca7e4eaa6e8ae9c7d261167129184883644d07dfba7cbfbc4c8a2e08360d5b

1
test/fixtures/hashsum/sha384.expected vendored Normal file
View file

@ -0,0 +1 @@
1fcdb6059ce05172a26bbe2a3ccc88ed5a8cd5fc53edfd9053304d429296a6da23b1cd9e5c9ed3bb34f00418a70cdb7e

1
test/fixtures/hashsum/sha512.expected vendored Normal file
View file

@ -0,0 +1 @@
8710339dcb6814d0d9d2290ef422285c9322b7163951f9a0ca8f883d3305286f44139aa374848e4174f5aada663027e4548637b6d19894aec4fb6c46a139fbf9

99
test/hashsum.rs Normal file
View file

@ -0,0 +1,99 @@
use std::fs::File;
use std::io::{Read, Write};
use std::process::{Command, Stdio};
use std::str::from_utf8;
static PROGNAME: &'static str = "./hashsum";
struct CmdResult {
success: bool,
stdout: String,
stderr: String,
}
fn run(cmd: &mut Command) -> CmdResult {
let prog = cmd.output().unwrap();
CmdResult {
success: prog.status.success(),
stdout: from_utf8(&prog.stdout).unwrap().to_string(),
stderr: from_utf8(&prog.stderr).unwrap().to_string(),
}
}
fn run_piped_stdin(cmd: &mut Command, input: &[u8])-> CmdResult {
let mut command = cmd
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.unwrap();
command.stdin
.take()
.unwrap_or_else(|| panic!("Could not take child process stdin"))
.write_all(input)
.unwrap_or_else(|e| panic!("{}", e));
let prog = command.wait_with_output().unwrap();
CmdResult {
success: prog.status.success(),
stdout: from_utf8(&prog.stdout).unwrap().to_string(),
stderr: from_utf8(&prog.stderr).unwrap().to_string(),
}
}
fn get_file_contents(name: &str) -> String {
let mut f = File::open(name).unwrap();
let mut contents = String::new();
let _ = f.read_to_string(&mut contents);
contents
}
macro_rules! assert_empty_stderr(
($cond:expr) => (
if $cond.stderr.len() > 0 {
panic!(format!("stderr: {}", $cond.stderr))
}
);
);
macro_rules! get_hash(
($str:expr) => (
$str.split(' ').collect::<Vec<&str>>()[0]
);
);
macro_rules! test_digest {
($($t:ident)*) => ($(
mod $t {
use std::process::Command;
static DIGEST_ARG: &'static str = concat!("--", stringify!($t));
static EXPECTED_FILE: &'static str = concat!(stringify!($t), ".expected");
#[test]
fn test_single_file() {
let mut cmd = Command::new(::PROGNAME);
let result = ::run(&mut cmd.arg(DIGEST_ARG).arg("input.txt"));
assert_empty_stderr!(result);
assert!(result.success);
assert_eq!(get_hash!(result.stdout), ::get_file_contents(EXPECTED_FILE));
}
#[test]
fn test_stdin() {
let input = ::get_file_contents("input.txt");
let mut cmd = Command::new(::PROGNAME);
let result = ::run_piped_stdin(&mut cmd.arg(DIGEST_ARG), input.as_bytes());
assert_empty_stderr!(result);
assert!(result.success);
assert_eq!(get_hash!(result.stdout), ::get_file_contents(EXPECTED_FILE));
}
}
)*)
}
test_digest! { md5 sha1 sha224 sha256 sha384 sha512 }