#![cfg(feature = "wasm-ast")]
use pmat::cli::commands::AnalyzeCommands;
use pmat::cli::enums::WasmOutputFormat;
use pmat::cli::handlers::wasm_handler;
use std::path::PathBuf;
use tempfile::tempdir;
#[test]
fn test_wasm_analyze_command_structure() {
let wasm_cmd = AnalyzeCommands::Wasm {
wasm_file: PathBuf::from("test.wasm"),
format: WasmOutputFormat::Summary,
verify: false,
security: false,
profile: false,
baseline: None,
output: None,
verbose: false,
};
match wasm_cmd {
AnalyzeCommands::Wasm { .. } => {
}
_ => panic!("WASM command not properly structured"),
}
}
#[test]
fn test_wasm_handler_basic_analysis() {
let temp_dir = tempdir().unwrap();
let wasm_path = temp_dir.path().join("test.wasm");
std::fs::write(
&wasm_path,
[
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, ],
)
.unwrap();
let result = tokio_test::block_on(async {
wasm_handler::handle_analyze_wasm(
wasm_path,
WasmOutputFormat::Summary,
false, false, false, None, None, false, )
.await
});
assert!(result.is_ok(), "WASM analysis should succeed");
}
#[test]
fn test_wasm_security_scanning() {
let temp_dir = tempdir().unwrap();
let wasm_path = temp_dir.path().join("vulnerable.wasm");
std::fs::write(&wasm_path, create_vulnerable_wasm()).unwrap();
let result = tokio_test::block_on(async {
wasm_handler::handle_analyze_wasm(
wasm_path,
WasmOutputFormat::Json,
false, true, false, None, None, false, )
.await
});
match result {
Ok(_) => {}
Err(e) => panic!("Security scanning failed with error: {}", e),
}
}
#[test]
fn test_wasm_verification() {
let temp_dir = tempdir().unwrap();
let wasm_path = temp_dir.path().join("verify.wasm");
std::fs::write(&wasm_path, create_safe_wasm()).unwrap();
let result = tokio_test::block_on(async {
wasm_handler::handle_analyze_wasm(
wasm_path,
WasmOutputFormat::Summary,
true, false, false, None, None, false, )
.await
});
assert!(result.is_ok(), "Verification should complete");
}
#[test]
fn test_wasm_profiling() {
let temp_dir = tempdir().unwrap();
let wasm_path = temp_dir.path().join("profile.wasm");
std::fs::write(&wasm_path, create_complex_wasm()).unwrap();
let result = tokio_test::block_on(async {
wasm_handler::handle_analyze_wasm(
wasm_path,
WasmOutputFormat::Detailed,
false, false, true, None, None, false, )
.await
});
match result {
Ok(_) => {}
Err(e) => panic!("Profiling failed with error: {}", e),
}
}
#[test]
fn test_wasm_baseline_comparison() {
let temp_dir = tempdir().unwrap();
let wasm_path = temp_dir.path().join("current.wasm");
let baseline_path = temp_dir.path().join("baseline.wasm");
std::fs::write(&wasm_path, create_complex_wasm()).unwrap();
std::fs::write(&baseline_path, create_safe_wasm()).unwrap();
let result = tokio_test::block_on(async {
wasm_handler::handle_analyze_wasm(
wasm_path,
WasmOutputFormat::Summary,
false, false, false, Some(baseline_path), None, false, )
.await
});
match result {
Ok(_) => {}
Err(e) => panic!("Baseline comparison failed with error: {}", e),
}
}
#[test]
fn test_wasm_output_formats() {
let temp_dir = tempdir().unwrap();
let wasm_path = temp_dir.path().join("test.wasm");
std::fs::write(&wasm_path, create_safe_wasm()).unwrap();
for format in [
WasmOutputFormat::Summary,
WasmOutputFormat::Json,
WasmOutputFormat::Detailed,
WasmOutputFormat::Sarif,
] {
let format_clone = format.clone();
let result = tokio_test::block_on(async {
wasm_handler::handle_analyze_wasm(
wasm_path.clone(),
format_clone,
false,
false,
false,
None,
None,
false,
)
.await
});
assert!(result.is_ok(), "Format {:?} should work", format);
}
}
#[test]
fn test_wasm_file_not_found() {
let result = tokio_test::block_on(async {
wasm_handler::handle_analyze_wasm(
PathBuf::from("nonexistent.wasm"),
WasmOutputFormat::Summary,
false,
false,
false,
None,
None,
false,
)
.await
});
assert!(result.is_err(), "Should error on missing file");
}
#[test]
fn test_wasm_invalid_binary() {
let temp_dir = tempdir().unwrap();
let wasm_path = temp_dir.path().join("invalid.wasm");
std::fs::write(&wasm_path, b"not a wasm file").unwrap();
let result = tokio_test::block_on(async {
wasm_handler::handle_analyze_wasm(
wasm_path,
WasmOutputFormat::Summary,
false,
false,
false,
None,
None,
false,
)
.await
});
assert!(result.is_err(), "Should error on invalid WASM");
}
fn create_vulnerable_wasm() -> Vec<u8> {
create_safe_wasm()
}
fn create_safe_wasm() -> Vec<u8> {
vec![
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, ]
}
fn create_complex_wasm() -> Vec<u8> {
create_safe_wasm()
}