mod common;
use std::process::Command;
use common::{binscout_bin, builders, cleanup, temp_db, tmpdir, write_fixture};
fn run(args: &[&str]) -> (bool, serde_json::Value) {
let out = Command::new(binscout_bin())
.args(args)
.output()
.expect("spawn binscout");
let text = String::from_utf8(out.stdout).expect("stdout is utf-8");
let value: serde_json::Value = serde_json::from_str(text.trim_end())
.unwrap_or_else(|_| panic!("stdout must be one JSON value, got: {text:?}"));
(out.status.success(), value)
}
struct Expected {
name: &'static str,
vaddr: u64,
size: u64,
file_off: u64,
file_size: usize,
readable: bool,
writable: bool,
executable: bool,
}
fn expected() -> Vec<Expected> {
vec![
Expected {
name: ".text",
vaddr: builders::TEXT_VA,
size: 0x200,
file_off: 0x200,
file_size: 0x200,
readable: true,
writable: false,
executable: true,
},
Expected {
name: ".rdata",
vaddr: builders::RDATA_VA,
size: 0x200,
file_off: 0x400,
file_size: 0x200,
readable: true,
writable: false,
executable: false,
},
Expected {
name: ".data",
vaddr: builders::DATA_VA,
size: 0x200,
file_off: 0x600,
file_size: 0x200,
readable: true,
writable: true,
executable: false,
},
]
}
fn check_rows(rows: &serde_json::Value) {
let rows = rows.as_array().expect("sections is an array");
assert_eq!(rows.len(), 3, "{rows:?}");
for (row, e) in rows.iter().zip(expected()) {
assert_eq!(row["name"], e.name, "{row}");
assert_eq!(row["vaddr"], e.vaddr, "{row}");
assert_eq!(row["size"], e.size, "{row}");
assert_eq!(row["file_off"], e.file_off, "{row}");
assert_eq!(row["file_size"], e.file_size, "{row}");
let entropy = row["entropy"].as_f64().expect("entropy is a number");
assert!(
(0.0..=8.0).contains(&entropy),
"entropy is Shannon bits per byte: {row}"
);
assert_eq!(row["readable"], e.readable, "{row}");
assert_eq!(row["writable"], e.writable, "{row}");
assert_eq!(row["executable"], e.executable, "{row}");
}
}
#[test]
fn sections_json_matches_builder_table() {
let dir = tmpdir("sections-lib");
let p = common::open_test_project(&dir, builders::BASIC_NAME);
check_rows(&p.sections_json());
cleanup(&dir);
}
#[test]
fn sections_cli_matches_builder_table() {
let dir = tmpdir("sections-cli");
let bin = write_fixture(&dir, builders::BASIC_NAME);
let bin_s = bin.display().to_string();
let db = temp_db(&dir, "basic").display().to_string();
let (ok, v) = run(&["--db", &db, "sections", &bin_s]);
assert!(ok, "{v}");
check_rows(&v);
cleanup(&dir);
}
#[test]
fn sections_empty_without_table() {
let dir = tmpdir("sections-raw");
let p = common::open_test_project(&dir, builders::GARBAGE_NAME);
assert_eq!(
p.sections_json(),
serde_json::Value::Array(Vec::new()),
"raw must report no sections"
);
cleanup(&dir);
}
#[test]
fn sections_cached_matches_full_open() {
let dir = tmpdir("sections-cached");
let bin = write_fixture(&dir, builders::BASIC_NAME);
let db = temp_db(&dir, "basic");
let full = binscout::Project::open_with_db(&bin, &db).expect("fixture must open");
full.save().expect("warm the sidecar");
let want = full.sections_json();
let cached = match binscout::open_mapped_loader_only(
&bin,
Some(&db),
None,
None,
&binscout::DebugOptions::default(),
)
.expect("loader-only open")
{
binscout::LoaderOpened::Cached(p) => p,
binscout::LoaderOpened::Fresh(_) => panic!("warmed sidecar must hit"),
};
let got = cached.sections_json();
assert_eq!(got, want, "cached sections match the full open");
for row in got.as_array().expect("sections is an array") {
assert!(
row["entropy"].is_number(),
"cached rows carry entropy: {row}"
);
}
cleanup(&dir);
}