Skip to content

Commit

Permalink
feat: add tests for callback abort and exit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Krivosheev authored and Hugal31 committed Jul 22, 2021
1 parent 8f96777 commit 027156c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
2 changes: 0 additions & 2 deletions src/internals/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ impl<'r> CallbackMsg<'r> {
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum CallbackReturn {
Continue,
#[allow(dead_code)]
Abort,
#[allow(dead_code)]
Error,
}

Expand Down
42 changes: 40 additions & 2 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
extern crate yara;

use yara::{CompileErrorLevel, Compiler, ConfigName, Error, Metadata, MetadataValue, Rules, Yara};
use yara::{
CallbackMsg, CallbackReturn, CompileErrorLevel, Compiler, ConfigName, Error, Metadata, MetadataValue,
Rules, Yara,
};
use yara_sys;

const RULES: &str = r#"
rule is_awesome {
Expand All @@ -17,7 +21,16 @@ rule is_ok {
condition:
$go
}"#;
}
rule re_is_ok {
strings:
$go = /[Oo]k/
condition:
$go
}
"#;

fn compile(rule: &str) -> Rules {
let mut compiler = Compiler::new().expect("Should create compiler");
Expand Down Expand Up @@ -95,6 +108,31 @@ fn test_scan_mem() {
assert_eq!(b"Rust", rule.strings[0].matches[0].data.as_slice());
}

#[test]
fn test_scan_mem_callback_abort<'r>() {
let rules = get_default_rules();
let mut results = Vec::new();
let callback = |message: CallbackMsg<'r>| {
if let CallbackMsg::RuleMatching(rule) = message {
results.push(rule);
}
CallbackReturn::Abort
};

let result = rules.scan_mem_callback("rust ok".as_bytes(), 10, callback);
assert!(result.is_ok());
assert_eq!(1, results.len());
}

#[test]
fn test_scan_mem_callback_error<'r>() {
let rules = get_default_rules();
let callback = |_| CallbackReturn::Error;
let result = rules.scan_mem_callback("rust ok".as_bytes(), 10, callback);
let error = result.err().expect("Should be Err");
assert_eq!(yara_sys::Error::CallbackError, error.kind);
}

#[test]
fn test_scan_file() {
let mut compiler = Compiler::new().unwrap();
Expand Down

0 comments on commit 027156c

Please sign in to comment.