From 9d74bbe5321f0c70e1da2fd59492e774cbc412ed Mon Sep 17 00:00:00 2001 From: Joseph Crail Date: Sat, 16 May 2015 18:03:09 -0400 Subject: [PATCH] Add initial tests for hashsum. --- Makefile | 1 + test/fixtures/hashsum/input.txt | 1 + test/fixtures/hashsum/md5.expected | 1 + test/fixtures/hashsum/sha1.expected | 1 + test/fixtures/hashsum/sha224.expected | 1 + test/fixtures/hashsum/sha256.expected | 1 + test/fixtures/hashsum/sha384.expected | 1 + test/fixtures/hashsum/sha512.expected | 1 + test/hashsum.rs | 99 +++++++++++++++++++++++++++ 9 files changed, 107 insertions(+) create mode 100644 test/fixtures/hashsum/input.txt create mode 100644 test/fixtures/hashsum/md5.expected create mode 100644 test/fixtures/hashsum/sha1.expected create mode 100644 test/fixtures/hashsum/sha224.expected create mode 100644 test/fixtures/hashsum/sha256.expected create mode 100644 test/fixtures/hashsum/sha384.expected create mode 100644 test/fixtures/hashsum/sha512.expected create mode 100644 test/hashsum.rs diff --git a/Makefile b/Makefile index 4cc0e53e2..088b03d7d 100644 --- a/Makefile +++ b/Makefile @@ -167,6 +167,7 @@ TEST_PROGS := \ factor \ false \ fold \ + hashsum \ mkdir \ mv \ nl \ diff --git a/test/fixtures/hashsum/input.txt b/test/fixtures/hashsum/input.txt new file mode 100644 index 000000000..8c01d89ae --- /dev/null +++ b/test/fixtures/hashsum/input.txt @@ -0,0 +1 @@ +hello, world \ No newline at end of file diff --git a/test/fixtures/hashsum/md5.expected b/test/fixtures/hashsum/md5.expected new file mode 100644 index 000000000..9a78d4ab7 --- /dev/null +++ b/test/fixtures/hashsum/md5.expected @@ -0,0 +1 @@ +e4d7f1b4ed2e42d15898f4b27b019da4 \ No newline at end of file diff --git a/test/fixtures/hashsum/sha1.expected b/test/fixtures/hashsum/sha1.expected new file mode 100644 index 000000000..8a50a732e --- /dev/null +++ b/test/fixtures/hashsum/sha1.expected @@ -0,0 +1 @@ +b7e23ec29af22b0b4e41da31e868d57226121c84 \ No newline at end of file diff --git a/test/fixtures/hashsum/sha224.expected b/test/fixtures/hashsum/sha224.expected new file mode 100644 index 000000000..456eca808 --- /dev/null +++ b/test/fixtures/hashsum/sha224.expected @@ -0,0 +1 @@ +6e1a93e32fb44081a401f3db3ef2e6e108b7bbeeb5705afdaf01fb27 \ No newline at end of file diff --git a/test/fixtures/hashsum/sha256.expected b/test/fixtures/hashsum/sha256.expected new file mode 100644 index 000000000..8def59791 --- /dev/null +++ b/test/fixtures/hashsum/sha256.expected @@ -0,0 +1 @@ +09ca7e4eaa6e8ae9c7d261167129184883644d07dfba7cbfbc4c8a2e08360d5b \ No newline at end of file diff --git a/test/fixtures/hashsum/sha384.expected b/test/fixtures/hashsum/sha384.expected new file mode 100644 index 000000000..489dbcef7 --- /dev/null +++ b/test/fixtures/hashsum/sha384.expected @@ -0,0 +1 @@ +1fcdb6059ce05172a26bbe2a3ccc88ed5a8cd5fc53edfd9053304d429296a6da23b1cd9e5c9ed3bb34f00418a70cdb7e \ No newline at end of file diff --git a/test/fixtures/hashsum/sha512.expected b/test/fixtures/hashsum/sha512.expected new file mode 100644 index 000000000..fd8173686 --- /dev/null +++ b/test/fixtures/hashsum/sha512.expected @@ -0,0 +1 @@ +8710339dcb6814d0d9d2290ef422285c9322b7163951f9a0ca8f883d3305286f44139aa374848e4174f5aada663027e4548637b6d19894aec4fb6c46a139fbf9 \ No newline at end of file diff --git a/test/hashsum.rs b/test/hashsum.rs new file mode 100644 index 000000000..c467530da --- /dev/null +++ b/test/hashsum.rs @@ -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::>()[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 }