use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
fn rustrip_bin() -> PathBuf {
PathBuf::from(env!("CARGO_BIN_EXE_rustrip"))
}
#[test]
fn cli_version_prints_pkg_version() {
let out = Command::new(rustrip_bin())
.arg("--version")
.output()
.expect("run rustrip --version");
assert!(
out.status.success(),
"{:?}",
String::from_utf8_lossy(&out.stderr)
);
let s = String::from_utf8_lossy(&out.stdout);
assert!(
s.contains(env!("CARGO_PKG_VERSION")),
"version output {s:?} does not contain pkg version"
);
}
#[test]
fn cli_help_contains_killer_tagline() {
let out = Command::new(rustrip_bin())
.arg("--help")
.output()
.expect("run rustrip --help");
assert!(
out.status.success(),
"stderr: {:?}",
String::from_utf8_lossy(&out.stderr)
);
let combined =
String::from_utf8_lossy(&out.stdout).into_owned() + &String::from_utf8_lossy(&out.stderr);
assert!(
combined.contains("Make stripped Rust binaries readable again"),
"tagline missing from --help"
);
}
#[test]
fn cli_on_self_binary_produces_table_output() {
let exe = rustrip_bin();
let out = Command::new(&exe)
.arg(&exe)
.arg("-f")
.arg("table")
.output()
.expect("run rustrip self-analysis table");
assert!(
out.status.success(),
"stderr: {:?}",
String::from_utf8_lossy(&out.stderr)
);
let s = String::from_utf8_lossy(&out.stdout);
assert!(s.contains("vaddr"), "no vaddr header: {s:?}");
}
#[test]
fn cli_selective_flags_all_combinatorial() {
let exe = rustrip_bin();
let cases: &[&[&str]] = &[
&["--no-strings"],
&["--no-symbols"],
&["--no-panics"],
&["--no-strings", "--no-symbols"],
&["--no-strings", "--no-panics"],
&["--no-symbols", "--no-panics"],
&["--no-strings", "--no-symbols", "--no-panics"],
];
for flags in cases {
let mut cmd = Command::new(&exe);
cmd.arg(&exe).arg("-f").arg("text");
for f in *flags {
cmd.arg(f);
}
let out = cmd.output().expect("run rustrip selective");
assert!(
out.status.success(),
"flag combination {:?} failed. stderr: {:?}",
flags,
String::from_utf8_lossy(&out.stderr)
);
}
}
#[test]
fn cli_format_aliases_all_accepted() {
let exe = rustrip_bin();
let aliases = [
"text",
"json",
"cli",
"ghidra",
"ghidra-py",
"py-ghidra",
"binja",
"binary-ninja",
"bn",
"py-binja",
];
for alias in aliases {
let mut cmd = Command::new(&exe);
cmd.arg(&exe).arg("-f").arg(alias);
let out = cmd.output().expect("run rustrip with alias");
assert!(
out.status.success() || out.status.code() == Some(0),
"alias {alias} failed: stderr: {:?}",
String::from_utf8_lossy(&out.stderr)
);
}
}
#[test]
fn cli_format_unknown_non_zero_exit() {
let exe = rustrip_bin();
let out = Command::new(&exe)
.arg(&exe)
.arg("-f")
.arg("garbage-format")
.output()
.expect("run rustrip with bad format");
assert!(
!out.status.success(),
"garbage --format should fail; stderr: {:?}",
String::from_utf8_lossy(&out.stderr)
);
}
#[test]
fn cli_missing_path_non_zero_exit() {
let exe = rustrip_bin();
let out = Command::new(&exe)
.arg("C:\\nonexistent-dir-for-rustrip-cli-test\\foo.bin")
.output()
.expect("run rustrip with missing path");
assert!(
!out.status.success(),
"missing path should error; stderr: {:?}",
String::from_utf8_lossy(&out.stderr)
);
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.to_lowercase().contains("opening") || stderr.to_lowercase().contains("path"),
"stderr should mention the path/IO: {stderr:?}"
);
}
#[test]
fn cli_reads_from_stdin_via_dash() {
let mut child = Command::new(rustrip_bin())
.arg("-")
.arg("-f")
.arg("text")
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.spawn()
.expect("spawn rustrip with stdin");
let mut stdin = child.stdin.take().expect("stdin handle");
stdin
.write_all(&std::fs::read(rustrip_bin()).expect("read self binary"))
.expect("stdin write");
drop(stdin);
let out = child.wait_with_output().expect("wait rustrip");
assert!(
out.status.success(),
"stdin self-analysis failed; stderr: {:?}",
String::from_utf8_lossy(&out.stderr)
);
}
#[test]
fn cli_max_string_len_zero_rejects_or_filters() {
let exe = rustrip_bin();
let out = Command::new(&exe)
.arg(&exe)
.arg("-f")
.arg("text")
.arg("--max-string-len")
.arg("0")
.output()
.expect("run rustrip max-string-len=0");
assert!(
out.status.success(),
"max-string-len=0 should run cleanly; stderr: {:?}",
String::from_utf8_lossy(&out.stderr)
);
}
#[test]
fn cli_output_file_writes_table_to_disk() {
let dir = tempfile::tempdir().expect("tempdir");
let out_path = dir.path().join("rustrip-table.txt");
let exe = rustrip_bin();
let out = Command::new(&exe)
.arg(&exe)
.arg("-f")
.arg("table")
.arg("-o")
.arg(&out_path)
.output()
.expect("run rustrip -o table");
assert!(
out.status.success(),
"stderr: {:?}",
String::from_utf8_lossy(&out.stderr)
);
let on_disk = std::fs::read_to_string(&out_path).expect("read output");
assert!(
on_disk.contains("vaddr"),
"no vaddr header on disk: {on_disk:?}"
);
}
#[test]
fn cli_output_file_writes_json_to_disk() {
let dir = tempfile::tempdir().expect("tempdir");
let out_path = dir.path().join("rustrip-report.json");
let exe = rustrip_bin();
let out = Command::new(&exe)
.arg(&exe)
.arg("-f")
.arg("json")
.arg("-o")
.arg(&out_path)
.output()
.expect("run rustrip -o json");
assert!(
out.status.success(),
"stderr: {:?}",
String::from_utf8_lossy(&out.stderr)
);
let on_disk = std::fs::read_to_string(&out_path).expect("read json");
let parsed: serde_json::Value = serde_json::from_str(&on_disk).expect("json parses");
assert!(parsed.is_array(), "top-level must be array");
}
#[test]
fn cli_output_file_writes_ghidra_python_to_disk() {
let dir = tempfile::tempdir().expect("tempdir");
let out_path = dir.path().join("rustrip-report_ghidra.py");
let exe = rustrip_bin();
let out = Command::new(&exe)
.arg(&exe)
.arg("-f")
.arg("ghidra")
.arg("-o")
.arg(&out_path)
.output()
.expect("run rustrip -o ghidra");
assert!(
out.status.success(),
"stderr: {:?}",
String::from_utf8_lossy(&out.stderr)
);
let on_disk = std::fs::read_to_string(&out_path).expect("read ghidra py");
assert!(on_disk.contains("currentProgram"));
assert!(on_disk.contains("_comments"));
}
#[test]
fn cli_output_file_writes_binja_python_to_disk() {
let dir = tempfile::tempdir().expect("tempdir");
let out_path = dir.path().join("rustrip-report_binja.py");
let exe = rustrip_bin();
let out = Command::new(&exe)
.arg(&exe)
.arg("-f")
.arg("binja")
.arg("-o")
.arg(&out_path)
.output()
.expect("run rustrip -o binja");
assert!(
out.status.success(),
"stderr: {:?}",
String::from_utf8_lossy(&out.stderr)
);
let on_disk = std::fs::read_to_string(&out_path).expect("read binja py");
assert!(on_disk.contains("binaryninja"));
assert!(on_disk.contains("_labels"));
}
#[test]
fn cli_tiny_binary_bails_with_friendly_message() {
let dir = tempfile::tempdir().expect("tempdir");
let tiny = dir.path().join("tiny.bin");
std::fs::write(&tiny, [0u8, 0, 0]).expect("write tiny");
let exe = rustrip_bin();
let out = Command::new(&exe)
.arg(&tiny)
.arg("-f")
.arg("text")
.output()
.expect("run rustrip tiny");
assert!(
!out.status.success(),
"tiny input should fail; stderr: {:?}",
String::from_utf8_lossy(&out.stderr)
);
let stderr = String::from_utf8_lossy(&out.stderr).to_lowercase();
assert!(
stderr.contains("too small") || stderr.contains("small"),
"stderr should mention 'too small': {stderr:?}"
);
}
#[test]
fn cli_four_byte_or_larger_does_not_bail_tiny() {
let dir = tempfile::tempdir().expect("tempdir");
let tiny = dir.path().join("four.bin");
std::fs::write(&tiny, [0x7f, b'E', b'L', b'F']).expect("write ELF magic");
let exe = rustrip_bin();
let out = Command::new(&exe)
.arg(&tiny)
.arg("-f")
.arg("text")
.output()
.expect("run rustrip 4-byte ELF magic");
let stderr = String::from_utf8_lossy(&out.stderr).to_lowercase();
assert!(
!stderr.contains("binary too small"),
"4-byte ELF magic should not hit rustrip's tiny-file bail: {stderr:?}"
);
}
#[test]
fn cli_selective_flag_toggles_invert_properly() {
let exe = rustrip_bin();
let kinds_for = |flags: &[&str]| -> std::collections::HashSet<String> {
let mut cmd = Command::new(&exe);
cmd.arg(&exe).arg("-f").arg("json");
for f in flags {
cmd.arg(f);
}
let out = cmd.output().expect("run rustrip");
assert!(
out.status.success(),
"flags {flags:?} failed: {:?}",
String::from_utf8_lossy(&out.stderr)
);
let parsed: serde_json::Value = serde_json::from_slice(&out.stdout).expect("json parses");
let arr = parsed.as_array().expect("array");
arr.iter()
.filter_map(|v| v.get("kind").and_then(|k| k.as_str()).map(str::to_owned))
.collect()
};
let baseline = kinds_for(&[]);
if baseline.contains("string") {
let no_strings = kinds_for(&["--no-strings"]);
assert!(
!no_strings.contains("string"),
"--no-strings should drop 'string' kinds; got: {no_strings:?}"
);
assert!(
no_strings.len() < baseline.len() || no_strings == baseline,
"disabling strings shouldn't *increase* kinds; baseline={baseline:?}, no_strings={no_strings:?}"
);
}
if baseline.contains("panic") {
let no_panics = kinds_for(&["--no-panics"]);
assert!(
!no_panics.contains("panic"),
"--no-panics should drop 'panic' kinds; got: {no_panics:?}"
);
}
if baseline.contains("symbol") {
let no_symbols = kinds_for(&["--no-symbols"]);
assert!(
!no_symbols.contains("symbol"),
"--no-symbols should drop 'symbol' kinds; got: {no_symbols:?}"
);
}
}
#[test]
fn cli_max_string_len_propagates_to_limits() {
let exe = rustrip_bin();
let count_for = |max: usize| -> usize {
let out = Command::new(&exe)
.arg(&exe)
.arg("-f")
.arg("json")
.arg("--max-string-len")
.arg(max.to_string())
.output()
.expect("run rustrip --max-string-len");
assert!(
out.status.success(),
"max={max} failed: {:?}",
String::from_utf8_lossy(&out.stderr)
);
let parsed: serde_json::Value = serde_json::from_slice(&out.stdout).expect("json parses");
let arr = parsed.as_array().expect("array");
arr.iter()
.filter(|v| v.get("kind").and_then(|k| k.as_str()) == Some("string"))
.count()
};
let big = count_for(4096);
let small = count_for(8);
if big > 0 {
assert!(
big > small,
"tighter --max-string-len must produce fewer strings (big={big}, small={small})"
);
}
}