#![allow(clippy::unwrap_used, reason = "test helpers fail loudly")]
use binbun_x86::{DecodeError, Decoder, Mode};
fn sweep(decoder: Decoder<'_>) -> Vec<(u64, String)> {
decoder
.map(|(address, result)| {
let text = result.map_or_else(|e| format!("{e:?}"), |i| i.intel(address).to_string());
(address, text)
})
.collect()
}
#[test]
fn decodes_in_sequence_with_addresses() {
let code = [
0x55, 0x48, 0x89, 0xe5, 0xe8, 0xf7, 0x0f, 0x00, 0x00, 0xc9, 0xc3,
];
let items = sweep(Decoder::new(&code, Mode::Bits64, 0x400));
assert_eq!(
items,
[
(0x400, "push rbp".to_owned()),
(0x401, "mov rbp, rsp".to_owned()),
(0x404, "call 0x1400".to_owned()),
(0x409, "leave ".to_owned()),
(0x40a, "ret ".to_owned()),
]
);
}
#[test]
fn decodes_endbr_as_cet_processors_run_it() {
let code = [
0xf3, 0x0f, 0x1e, 0xfa, 0xf3, 0x0f, 0x1e, 0xfb, 0x0f, 0x1e, 0xfa,
];
let items = sweep(Decoder::new(&code, Mode::Bits64, 0));
assert_eq!(
items,
[
(0, "endbr64".to_owned()),
(4, "endbr32".to_owned()),
(8, "nop edx, edi".to_owned()),
]
);
}
#[test]
fn skips_one_byte_after_an_error() {
let code = [0x06, 0x90, 0xb8, 0x01];
let mut decoder = Decoder::new(&code, Mode::Bits64, 0);
let results: Vec<(u64, Result<usize, DecodeError>)> = decoder
.by_ref()
.map(|(address, result)| (address, result.map(|i| i.len())))
.collect();
assert_eq!(
results,
[
(0, Err(DecodeError::Invalid)),
(1, Ok(1)),
(2, Err(DecodeError::TooShort)),
(3, Err(DecodeError::TooShort)),
]
);
assert!(decoder.next().is_none());
}
#[test]
fn repositions_and_reports_remaining_bytes() {
let code = [0x90, 0x90, 0xc3];
let mut decoder = Decoder::new(&code, Mode::Bits32, 0x7000);
decoder.set_offset(2);
assert_eq!((decoder.offset(), decoder.address()), (2, 0x7002));
assert_eq!(decoder.remaining(), [0xc3]);
assert_eq!(
decoder.next().map(|(a, r)| (a, r.unwrap().len())),
Some((0x7002, 1))
);
assert!(decoder.next().is_none());
decoder.set_offset(10);
assert!(decoder.remaining().is_empty() && decoder.next().is_none());
}