Files
ghidra-cli/tests/script_tests.rs
T
Alexander Kiselev 912cd0137b feat: Add PyGhidra installation support and enhance daemon command handling
- Implemented `install_pyghidra` function to set up PyGhidra in a Python virtual environment for Ghidra installations.
- Enhanced `install_ghidra` to call `install_pyghidra` after Ghidra installation.
- Updated daemon command handling to include program name in start, restart, and stop commands.
- Refactored project path resolution to streamline project management.
- Improved socket path handling to respect `GHIDRA_CLI_SOCKET` environment variable for testing.
- Modified tests to remove ignore flags, allowing for automated testing without Ghidra installation.
- Added analysis step in test project setup to ensure comments and other features work correctly.
2026-01-25 16:39:23 -08:00

132 lines
3.1 KiB
Rust

//! Tests for script execution operations.
use assert_cmd::Command;
use predicates::prelude::*;
use serial_test::serial;
use std::fs;
use std::path::PathBuf;
#[macro_use]
mod common;
use common::{ensure_test_project, DaemonTestHarness};
const TEST_PROJECT: &str = "script-test";
const TEST_PROGRAM: &str = "sample_binary";
fn get_test_script_path() -> PathBuf {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("tests");
path.push("fixtures");
path.push("test_script.py");
path
}
fn create_test_script() -> PathBuf {
let script_path = get_test_script_path();
fs::create_dir_all(script_path.parent().unwrap()).ok();
let script_content = r#"# Test script
# @category Test
print("Test script executed")
"#;
fs::write(&script_path, script_content).expect("Failed to write test script");
script_path
}
#[test]
#[serial]
fn test_script_list() {
ensure_test_project(TEST_PROJECT, TEST_PROGRAM);
let harness = DaemonTestHarness::new(TEST_PROJECT, TEST_PROGRAM)
.expect("Failed to start daemon");
Command::cargo_bin("ghidra")
.unwrap()
.env("GHIDRA_CLI_SOCKET", harness.socket_path())
.arg("script")
.arg("list")
.arg("--program")
.arg(TEST_PROGRAM)
.assert()
.success()
.stdout(predicate::str::contains("scripts"));
drop(harness);
}
#[test]
#[serial]
fn test_script_run() {
ensure_test_project(TEST_PROJECT, TEST_PROGRAM);
let script_path = create_test_script();
let harness = DaemonTestHarness::new(TEST_PROJECT, TEST_PROGRAM)
.expect("Failed to start daemon");
Command::cargo_bin("ghidra")
.unwrap()
.env("GHIDRA_CLI_SOCKET", harness.socket_path())
.arg("script")
.arg("run")
.arg(script_path.to_str().unwrap())
.arg("--program")
.arg(TEST_PROGRAM)
.assert()
.success()
.stdout(predicate::str::contains("executed"));
drop(harness);
fs::remove_file(script_path).ok();
}
#[test]
#[serial]
fn test_script_python_inline() {
ensure_test_project(TEST_PROJECT, TEST_PROGRAM);
let harness = DaemonTestHarness::new(TEST_PROJECT, TEST_PROGRAM)
.expect("Failed to start daemon");
Command::cargo_bin("ghidra")
.unwrap()
.env("GHIDRA_CLI_SOCKET", harness.socket_path())
.arg("script")
.arg("python")
.arg("output = 'Hello from Python'")
.arg("--program")
.arg(TEST_PROGRAM)
.assert()
.success()
.stdout(predicate::str::contains("executed"));
drop(harness);
}
#[test]
#[serial]
fn test_script_run_nonexistent() {
ensure_test_project(TEST_PROJECT, TEST_PROGRAM);
let harness = DaemonTestHarness::new(TEST_PROJECT, TEST_PROGRAM)
.expect("Failed to start daemon");
Command::cargo_bin("ghidra")
.unwrap()
.env("GHIDRA_CLI_SOCKET", harness.socket_path())
.arg("script")
.arg("run")
.arg("/nonexistent/script.py")
.arg("--program")
.arg(TEST_PROGRAM)
.assert()
.failure();
drop(harness);
}