Files
xex2tractor/tests/integration.rs
MechaCat02 b5f2abe09a feat: parse and display XEX2 main header (M1)
Implement XEX2 main header parsing with module flag decoding.
Add error handling, big-endian read utilities, CLI entry point,
and comprehensive unit + integration tests against a sample file.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-28 18:52:15 +01:00

71 lines
2.3 KiB
Rust

use xex2tractor::header::{ModuleFlags, XEX2_MAGIC};
fn sample_data() -> Vec<u8> {
let path = format!("{}/tests/data/default.xex", env!("CARGO_MANIFEST_DIR"));
std::fs::read(&path).expect("sample file should exist at tests/data/default.xex")
}
#[test]
fn test_full_parse() {
let data = sample_data();
let xex = xex2tractor::parse(&data).unwrap();
assert_eq!(xex.header.magic, XEX2_MAGIC);
assert_eq!(xex.header.module_flags, ModuleFlags(0x00000001));
assert_eq!(xex.header.header_size, 0x00003000);
assert_eq!(xex.header.reserved, 0x00000000);
assert_eq!(xex.header.security_offset, 0x00000090);
assert_eq!(xex.header.header_count, 15);
}
#[test]
fn test_parse_empty_file() {
let data = vec![];
assert!(xex2tractor::parse(&data).is_err());
}
#[test]
fn test_parse_invalid_magic() {
let mut data = sample_data();
// Corrupt the magic bytes
data[0] = 0x00;
assert!(xex2tractor::parse(&data).is_err());
}
#[test]
fn test_cli_runs_with_sample() {
let path = format!("{}/tests/data/default.xex", env!("CARGO_MANIFEST_DIR"));
let output = std::process::Command::new(env!("CARGO_BIN_EXE_xex2tractor"))
.arg(&path)
.output()
.expect("failed to run xex2tractor");
assert!(output.status.success(), "CLI should exit successfully");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("XEX2 Header"), "should display header section");
assert!(stdout.contains("0x58455832"), "should display magic value");
assert!(stdout.contains("TITLE"), "should display module flag name");
assert!(stdout.contains("Header Count: 15"), "should show 15 headers");
}
#[test]
fn test_cli_no_args() {
let output = std::process::Command::new(env!("CARGO_BIN_EXE_xex2tractor"))
.output()
.expect("failed to run xex2tractor");
assert!(!output.status.success(), "CLI should fail without args");
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains("Usage"), "should print usage message");
}
#[test]
fn test_cli_missing_file() {
let output = std::process::Command::new(env!("CARGO_BIN_EXE_xex2tractor"))
.arg("/nonexistent/file.xex")
.output()
.expect("failed to run xex2tractor");
assert!(!output.status.success(), "CLI should fail with missing file");
}