use headcrab::target::{read_string, read_usize};
use nix::{
sys::ptrace,
sys::wait::waitpid,
unistd::{execv, fork, ForkResult},
};
use std::ffi::CString;
static BIN_PATH: &str = "./tests/hello";
const STR_ADDR: usize = 0x404028;
#[test]
fn main() -> Result<(), Box<dyn std::error::Error>> {
match fork()? {
ForkResult::Parent { child, .. } => {
let _status = waitpid(child, None);
let str_addr = read_usize(child, STR_ADDR)?;
let rval = read_string(child, str_addr, 13)?;
assert_eq!(rval, "Hello, world!");
ptrace::cont(child, None)?;
}
ForkResult::Child => {
ptrace::traceme()?;
let path = CString::new(BIN_PATH)?;
execv(&path, &[])?;
}
}
Ok(())
}