added observer testcase

This commit is contained in:
Dominik Maier 2020-10-28 16:02:50 +01:00
parent db9413e223
commit 0b6c80133c
3 changed files with 68 additions and 1 deletions

View File

@ -81,3 +81,46 @@ impl Executor for InMemoryExecutor {
self.base.observers.push(observer);
}
}
impl InMemoryExecutor {
fn new(harness_fn: HarnessFunction) -> InMemoryExecutor {
InMemoryExecutor {
base: ExecutorBase {
observers: vec![],
cur_input: Option::None,
},
harness: harness_fn,
}
}
}
#[cfg(test)]
mod tests {
use crate::executors::{Executor, ExitKind, InMemoryExecutor};
use crate::observers::Observer;
use crate::AflError;
struct Nopserver {}
impl Observer for Nopserver {
fn reset(&mut self) -> Result<(), AflError> {
Err(AflError::Unknown)
}
fn post_exec(&mut self) -> Result<(), AflError> {
Err(AflError::Unknown)
}
}
fn test_harness_fn_nop(_executor: &dyn Executor, buf: &[u8]) -> ExitKind {
println! {"Fake exec with buf of len {}", buf.len()};
ExitKind::Ok
}
#[test]
fn test_inmem_post_exec() {
let mut in_mem_executor = InMemoryExecutor::new(test_harness_fn_nop);
let nopserver = Nopserver {};
in_mem_executor.add_observer(Box::new(nopserver));
assert_eq!(in_mem_executor.post_exec_observers().is_err(), true);
}
}

View File

@ -16,3 +16,27 @@ impl Input for BytesInput {
Ok(())
}
}
#[cfg(test)]
mod tests {
use crate::utils::{next_pow2, Rand, Xoshiro256StarRand};
#[test]
fn test_input() {
let mut rand = Xoshiro256StarRand::new();
assert_ne!(rand.next(), rand.next());
assert!(rand.below(100) < 100);
assert_eq!(rand.below(1), 0);
assert_eq!(rand.between(10, 10), 10);
assert!(rand.between(11, 20) > 10);
}
#[test]
fn test_next_pow2() {
assert_eq!(next_pow2(0), 0);
assert_eq!(next_pow2(1), 1);
assert_eq!(next_pow2(2), 2);
assert_eq!(next_pow2(3), 4);
assert_eq!(next_pow2(1000), 1024);
}
}

View File

@ -88,7 +88,7 @@ impl Xoshiro256StarRand {
}
/// Get the next higher power of two
fn next_pow2(val: u64) -> u64 {
pub fn next_pow2(val: u64) -> u64 {
// Early exit so we don't have to do a wrapping subtract;
if val <= 2 {
return val;