diff --git a/src/error.rs b/src/error.rs index 5761b147..2e1fc445 100644 --- a/src/error.rs +++ b/src/error.rs @@ -180,3 +180,16 @@ impl From> for StableResult { /// Return value of programs and syscalls pub type ProgramResult = StableResult; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_program_result_is_stable() { + let ok = ProgramResult::Ok(42); + assert_eq!(ok.discriminant(), 0); + let err = ProgramResult::Err(EbpfError::JitNotCompiled); + assert_eq!(err.discriminant(), 1); + } +} diff --git a/src/program.rs b/src/program.rs index 279d4ea2..55bbb20d 100644 --- a/src/program.rs +++ b/src/program.rs @@ -341,3 +341,39 @@ macro_rules! declare_builtin_function { } }; } + +#[cfg(test)] +mod tests { + use super::*; + use crate::{program::BuiltinFunction, syscalls, vm::TestContextObject}; + + #[test] + fn test_builtin_program_eq() { + let mut function_registry_a = + FunctionRegistry::>::default(); + function_registry_a + .register_function_hashed(*b"log", syscalls::SyscallString::vm) + .unwrap(); + function_registry_a + .register_function_hashed(*b"log_64", syscalls::SyscallU64::vm) + .unwrap(); + let mut function_registry_b = + FunctionRegistry::>::default(); + function_registry_b + .register_function_hashed(*b"log_64", syscalls::SyscallU64::vm) + .unwrap(); + function_registry_b + .register_function_hashed(*b"log", syscalls::SyscallString::vm) + .unwrap(); + let mut function_registry_c = + FunctionRegistry::>::default(); + function_registry_c + .register_function_hashed(*b"log_64", syscalls::SyscallU64::vm) + .unwrap(); + let builtin_program_a = BuiltinProgram::new_loader(Config::default(), function_registry_a); + let builtin_program_b = BuiltinProgram::new_loader(Config::default(), function_registry_b); + assert_eq!(builtin_program_a, builtin_program_b); + let builtin_program_c = BuiltinProgram::new_loader(Config::default(), function_registry_c); + assert_ne!(builtin_program_a, builtin_program_c); + } +} diff --git a/src/vm.rs b/src/vm.rs index eba59184..f91a7de5 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -422,47 +422,3 @@ impl<'a, C: ContextObject> EbpfVm<'a, C> { (instruction_count, result) } } - -#[cfg(test)] -mod tests { - use super::*; - use crate::{program::BuiltinFunction, syscalls}; - - #[test] - fn test_program_result_is_stable() { - let ok = ProgramResult::Ok(42); - assert_eq!(ok.discriminant(), 0); - let err = ProgramResult::Err(EbpfError::JitNotCompiled); - assert_eq!(err.discriminant(), 1); - } - - #[test] - fn test_builtin_program_eq() { - let mut function_registry_a = - FunctionRegistry::>::default(); - function_registry_a - .register_function_hashed(*b"log", syscalls::SyscallString::vm) - .unwrap(); - function_registry_a - .register_function_hashed(*b"log_64", syscalls::SyscallU64::vm) - .unwrap(); - let mut function_registry_b = - FunctionRegistry::>::default(); - function_registry_b - .register_function_hashed(*b"log_64", syscalls::SyscallU64::vm) - .unwrap(); - function_registry_b - .register_function_hashed(*b"log", syscalls::SyscallString::vm) - .unwrap(); - let mut function_registry_c = - FunctionRegistry::>::default(); - function_registry_c - .register_function_hashed(*b"log_64", syscalls::SyscallU64::vm) - .unwrap(); - let builtin_program_a = BuiltinProgram::new_loader(Config::default(), function_registry_a); - let builtin_program_b = BuiltinProgram::new_loader(Config::default(), function_registry_b); - assert_eq!(builtin_program_a, builtin_program_b); - let builtin_program_c = BuiltinProgram::new_loader(Config::default(), function_registry_c); - assert_ne!(builtin_program_a, builtin_program_c); - } -}