use ::anyhow::Result;
use ::demikernel::{
LibOS,
QDesc,
};
pub fn run(libos: &mut LibOS, pipe_name: &str) -> Vec<(String, String, Result<(), anyhow::Error>)> {
let mut result: Vec<(String, String, Result<(), anyhow::Error>)> = Vec::new();
demikernel::collect_test!(result, demikernel::run_test!(close_invalid_pipe(libos)));
demikernel::collect_test!(
result,
demikernel::run_test!(close_pipe_multiple_times(libos, pipe_name))
);
result
}
fn close_invalid_pipe(libos: &mut LibOS) -> Result<()> {
match libos.close(QDesc::from(0)) {
Err(e) if e.errno == libc::EBADF => (),
Ok(_) => anyhow::bail!("close() invalid pipe should fail"),
Err(e) => anyhow::bail!("close() failed ({})", e),
};
match libos.close(QDesc::from(u32::MAX)) {
Err(e) if e.errno == libc::EBADF => (),
Ok(_) => anyhow::bail!("close() invalid pipe should fail"),
Err(e) => anyhow::bail!("close() failed ({})", e),
};
Ok(())
}
fn close_pipe_multiple_times(libos: &mut LibOS, pipe_name: &str) -> Result<()> {
let pipeqd: QDesc = match libos.create_pipe(pipe_name) {
Ok(pipeqd) => pipeqd,
Err(e) => anyhow::bail!("create_pipe() failed ({})", e),
};
match libos.close(pipeqd) {
Ok(_) => (),
Err(e) => {
println!("[ERROR] leaking pipeqd={:?}", pipeqd);
anyhow::bail!("close() failed ({})", e);
},
};
match libos.close(pipeqd) {
Err(e) if e.errno == libc::EBADF => Ok(()),
Ok(_) => anyhow::bail!("close() invalid pipe should fail"),
Err(e) => anyhow::bail!("close() failed ({})", e),
}
}