diff --git a/Makefile b/Makefile index 4f23f82c9..b8a16495a 100644 --- a/Makefile +++ b/Makefile @@ -158,6 +158,7 @@ endif # Programs with usable tests TEST_PROGS := \ + base64 \ basename \ cat \ cp \ diff --git a/src/base64/base64.rs b/src/base64/base64.rs index 01bcf3ec1..85e2dd47e 100644 --- a/src/base64/base64.rs +++ b/src/base64/base64.rs @@ -46,7 +46,7 @@ pub fn uumain(args: Vec) -> i32 { opts.optflag("V", "version", "output version information and exit"); let matches = match opts.parse(&args[1..]) { Ok(m) => m, - Err(e) => { crash!(1, "error: {}", e) } + Err(e) => { crash!(1, "{}", e) } }; let mode = if matches.opt_present("help") { @@ -63,7 +63,7 @@ pub fn uumain(args: Vec) -> i32 { Some(s) => match s.parse() { Ok(s) => s, Err(e)=> { - crash!(1, "error: Argument to option 'wrap' improperly formatted: {}", e); + crash!(1, "Argument to option 'wrap' improperly formatted: {}", e); } }, None => 76 @@ -122,7 +122,7 @@ fn decode(input: &mut FileOrStdReader, ignore_garbage: bool) { } } Err(s) => { - crash!(1, "error: {} ({:?})", s.description(), s); + crash!(1, "{} ({:?})", s.description(), s); } } } diff --git a/test/base64.rs b/test/base64.rs new file mode 100644 index 000000000..3da7b54ac --- /dev/null +++ b/test/base64.rs @@ -0,0 +1,95 @@ +use std::io::{Read, Write}; +use std::process::{Command, Stdio}; +use std::str::from_utf8; + +static PROGNAME: &'static str = "./base64"; + +struct CmdResult { + success: bool, + stdout: String, + stderr: 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(), + } +} + +macro_rules! assert_empty_stderr( + ($cond:expr) => ( + if $cond.stderr.len() > 0 { + panic!(format!("stderr: {}", $cond.stderr)) + } + ); +); + +#[test] +fn test_encode() { + let input = "hello, world!"; + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd, input.as_bytes()); + + assert_empty_stderr!(result); + assert!(result.success); + assert_eq!(result.stdout, "aGVsbG8sIHdvcmxkIQ==\n"); +} + +#[test] +fn test_decode() { + let input = "aGVsbG8sIHdvcmxkIQ=="; + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.arg("-d"), input.as_bytes()); + + assert_empty_stderr!(result); + assert!(result.success); + assert_eq!(result.stdout, "hello, world!"); +} + +#[test] +fn test_garbage() { + let input = "aGVsbG8sIHdvcmxkIQ==\0"; + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.arg("-d"), input.as_bytes()); + + assert!(!result.success); + assert_eq!(result.stderr, "base64: error: invalid character (Invalid character '0' at position 20)\n"); +} + +#[test] +fn test_ignore_garbage() { + let input = "aGVsbG8sIHdvcmxkIQ==\0"; + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.arg("-d").arg("-i"), input.as_bytes()); + + assert_empty_stderr!(result); + assert!(result.success); + assert_eq!(result.stdout, "hello, world!"); +} + +#[test] +fn test_wrap() { + let input = "The quick brown fox jumps over the lazy dog."; + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.arg("-w").arg("20"), input.as_bytes()); + + assert_empty_stderr!(result); + assert!(result.success); + assert_eq!(result.stdout, "VGhlIHF1aWNrIGJyb3du\nIGZveCBqdW1wcyBvdmVy\nIHRoZSBsYXp5IGRvZy4=\n"); +}