Add a test for handling non-utf8 data

This commit is contained in:
Graham Christensen 2018-01-24 09:08:33 -05:00
parent 70d1f6f048
commit f92db96f6a
No known key found for this signature in database
GPG key ID: ACA1C1D120C83D5C

View file

@ -40,12 +40,16 @@ fn reader_tx<R: 'static + Read + Send>(read: R, tx: Sender<String>) -> thread::J
thread::spawn(move || { thread::spawn(move || {
for line in read.lines() { for line in read.lines() {
if let Ok(line) = line { let to_send: String = match line {
if let Err(e) = tx.send(line) { Ok(line) => line,
error!("Failed to send log line: {:?}", e); Err(e) => {
error!("Error reading data in reader_tx: {:?}", e);
"Non-UTF8 data omitted from the log.".to_owned()
} }
} else { };
error!("Got in reader tx's else: {:?}", line);
if let Err(e) = tx.send(to_send) {
error!("Failed to send log line: {:?}", e);
} }
} }
}) })
@ -125,8 +129,8 @@ impl AsyncCmd {
info!("interior status: {:?}", interior_result); info!("interior status: {:?}", interior_result);
match interior_result { match interior_result {
WaitResult::Thread(_) => { WaitResult::Thread(t) => {
// Don't think there is anything special to do here debug!("thread result: {:?}", t);
} }
WaitResult::Process(t) => { WaitResult::Process(t) => {
return_status = Some(t); return_status = Some(t);
@ -172,6 +176,8 @@ impl SpawnedAsyncCmd {
mod tests { mod tests {
use super::AsyncCmd; use super::AsyncCmd;
use std::process::Command; use std::process::Command;
use std::ffi::{OsStr,OsString};
use std::os::unix::ffi::OsStrExt;
#[test] #[test]
fn basic_echo_test() { fn basic_echo_test() {
@ -241,4 +247,26 @@ mod tests {
let exit_status = child_result.expect("The child should have no problem exiting"); let exit_status = child_result.expect("The child should have no problem exiting");
assert_eq!(true, exit_status.success()); assert_eq!(true, exit_status.success());
} }
#[test]
fn bad_utf8_test() {
let mut echos = OsString::from("echo hi; echo ");
echos.push(OsStr::from_bytes(&[0xffu8]));
echos.push("; echo there;");
let mut cmd = Command::new("/bin/sh");
cmd.arg("-c");
cmd.arg(echos);
let acmd = AsyncCmd::new(cmd);
let mut spawned = acmd.spawn();
let lines: Vec<String> = spawned.lines().into_iter().collect();
assert_eq!(lines, vec![
"hi",
"Non-UTF8 data omitted from the log.",
"there",
]);
let ret = spawned.wait().unwrap().unwrap().unwrap().success();
assert_eq!(true, ret);
}
} }