Skip to content

Commit

Permalink
Added initial integration testing
Browse files Browse the repository at this point in the history
  • Loading branch information
devttys0 committed Nov 1, 2024
1 parent d1ecf72 commit 88051e4
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* text eol=lf
*.md text eol=lf
*.png binary
*.png binary
*.bin binary
4 changes: 2 additions & 2 deletions src/binwalk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ impl Binwalk {
///
/// ## Example
///
/// ```
/// ```no_run
/// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_binwalk_rs_529_0() -> Result<binwalk::Binwalk, binwalk::BinwalkError> {
/// use binwalk::Binwalk;
///
Expand Down Expand Up @@ -636,7 +636,7 @@ impl Binwalk {
///
/// ## Example
///
/// ```
/// ```no_run
/// # fn main() { #[allow(non_snake_case)] fn _doctest_main_src_binwalk_rs_624_0() -> Result<binwalk::Binwalk, binwalk::BinwalkError> {
/// use binwalk::Binwalk;
///
Expand Down
8 changes: 8 additions & 0 deletions tests/arcadyan.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod common;

#[test]
fn pdf_integration() {
const SIGNATURE_TYPE: &str = "arcadyan";
const INPUT_FILE_NAME: &str = "arcadyan.bin";
let _ = common::integration_test(SIGNATURE_TYPE, INPUT_FILE_NAME);
}
48 changes: 48 additions & 0 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use binwalk::{AnalysisResults, Binwalk};

pub fn integration_test(signature_filter: &str, file_name: &str) -> AnalysisResults {
// Build the path to the input file
let file_path = std::path::Path::new("tests")
.join("inputs")
.join(file_name)
.display()
.to_string();

// Build the path to the output directory
let output_directory = std::path::Path::new(&std::env::temp_dir().display().to_string())
.join("binwalk_integration_test_extractions")
.display()
.to_string();

// Delete the output directory, if it exists
let _ = std::fs::remove_dir_all(&output_directory);

// Configure binwalk
let binwalker = Binwalk::configure(
Some(file_path),
Some(output_directory.clone()),
Some(vec![signature_filter.to_string()]),
None,
None,
false,
)
.expect("Binwalk initialization failed");

// Run analysis
let results = binwalker.analyze(&binwalker.base_target_file, true);

// Each test is expected to have a single result at offset 0 in the file
assert!(results.file_map.len() == 1);
assert!(results.file_map[0].offset == 0);

// Tests which support extraction are expected to have a single successful extraction
if !results.extractions.is_empty() {
assert!(results.extractions.len() == 1);
assert!(results.extractions[&results.file_map[0].id].success);
}

// Clean up the output directory
let _ = std::fs::remove_dir_all(output_directory);

results
}
8 changes: 8 additions & 0 deletions tests/gzip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod common;

#[test]
fn gzip_integration() {
const SIGNATURE_TYPE: &str = "gzip";
const INPUT_FILE_NAME: &str = "gzip.bin";
let _ = common::integration_test(SIGNATURE_TYPE, INPUT_FILE_NAME);
}
Binary file added tests/inputs/arcadyan.bin
Binary file not shown.
Binary file added tests/inputs/gzip.bin
Binary file not shown.
Binary file added tests/inputs/pdf.bin
Binary file not shown.
8 changes: 8 additions & 0 deletions tests/pdf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod common;

#[test]
fn pdf_integration() {
const SIGNATURE_TYPE: &str = "pdf";
const INPUT_FILE_NAME: &str = "pdf.bin";
let _ = common::integration_test(SIGNATURE_TYPE, INPUT_FILE_NAME);
}

0 comments on commit 88051e4

Please sign in to comment.