From 2cba139f15a8542a2cfd63d65dff59e62e73425c Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 4 Jul 2019 18:12:16 +0100 Subject: [PATCH] rust: add more tests to EvmcContainer --- bindings/rust/evmc-vm/src/container.rs | 55 +++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/bindings/rust/evmc-vm/src/container.rs b/bindings/rust/evmc-vm/src/container.rs index d3513d9..f2975d2 100644 --- a/bindings/rust/evmc-vm/src/container.rs +++ b/bindings/rust/evmc-vm/src/container.rs @@ -49,6 +49,7 @@ where #[cfg(test)] mod tests { use super::*; + use crate::types::*; use crate::{ExecutionContext, ExecutionResult}; struct TestVm {} @@ -61,6 +62,20 @@ mod tests { } } + unsafe extern "C" fn get_dummy_tx_context( + _context: *mut evmc_sys::evmc_context, + ) -> evmc_sys::evmc_tx_context { + evmc_sys::evmc_tx_context { + tx_gas_price: Uint256::default(), + tx_origin: Address::default(), + block_coinbase: Address::default(), + block_number: 0, + block_timestamp: 0, + block_gas_limit: 0, + block_difficulty: Uint256::default(), + } + } + #[test] fn container_new() { let instance = ::evmc_sys::evmc_instance { @@ -74,10 +89,48 @@ mod tests { set_option: None, }; + let code = [0u8; 0]; + let message = ::evmc_sys::evmc_message { + kind: ::evmc_sys::evmc_call_kind::EVMC_CALL, + flags: 0, + depth: 0, + gas: 0, + destination: ::evmc_sys::evmc_address::default(), + sender: ::evmc_sys::evmc_address::default(), + input_data: std::ptr::null(), + input_size: 0, + value: ::evmc_sys::evmc_uint256be::default(), + create2_salt: ::evmc_sys::evmc_bytes32::default(), + }; + let host = ::evmc_sys::evmc_host_interface { + account_exists: None, + get_storage: None, + set_storage: None, + get_balance: None, + get_code_size: None, + get_code_hash: None, + copy_code: None, + selfdestruct: None, + call: None, + get_tx_context: Some(get_dummy_tx_context), + get_block_hash: None, + emit_log: None, + }; + let mut backing_context = ::evmc_sys::evmc_context { host: &host }; + let context = ExecutionContext::new(&message, &mut backing_context); + let container = EvmcContainer::::new(instance); + assert_eq!( + container.execute(&code, &context).get_status_code(), + ::evmc_sys::evmc_status_code::EVMC_FAILURE + ); let ptr = unsafe { EvmcContainer::into_ffi_pointer(Box::new(container)) }; - unsafe { EvmcContainer::::from_ffi_pointer(ptr) }; + let container = unsafe { EvmcContainer::::from_ffi_pointer(ptr) }; + assert_eq!( + container.execute(&code, &context).get_status_code(), + ::evmc_sys::evmc_status_code::EVMC_FAILURE + ); } }