From cafdfa810713b8dc1c18c26e6b4f45d983596025 Mon Sep 17 00:00:00 2001 From: Andronik Ordian Date: Fri, 9 Aug 2019 13:14:41 +0200 Subject: [PATCH] [evmbin] add more tests to main.rs (#10956) --- evmbin/src/display/std_json.rs | 19 ++--- evmbin/src/info.rs | 4 +- evmbin/src/main.rs | 137 ++++++++++++++++++++++----------- 3 files changed, 103 insertions(+), 57 deletions(-) diff --git a/evmbin/src/display/std_json.rs b/evmbin/src/display/std_json.rs index 340cae4e1..a5974bc91 100644 --- a/evmbin/src/display/std_json.rs +++ b/evmbin/src/display/std_json.rs @@ -332,20 +332,21 @@ pub mod tests { } } - pub fn informant() -> (Informant, Arc>>) { + pub fn informant() -> (Informant, TestWriter, TestWriter) { let trace_writer: TestWriter = Default::default(); let out_writer: TestWriter = Default::default(); - let res = trace_writer.0.clone(); - (Informant::new(trace_writer, out_writer), res) + let trace_copy = Clone::clone(&trace_writer); + let out_copy = Clone::clone(&out_writer); + (Informant::new(trace_writer, out_writer), trace_copy, out_copy) } #[test] fn should_trace_failure() { - let (inf, res) = informant(); + let (inf, res, _) = informant(); run_test( inf, move |_, expected| { - let bytes = res.lock().unwrap(); + let bytes = res.0.lock().unwrap(); assert_eq!(expected, &String::from_utf8_lossy(&**bytes)) }, "60F8d6", @@ -355,11 +356,11 @@ pub mod tests { "#, ); - let (inf, res) = informant(); + let (inf, res, _) = informant(); run_test( inf, move |_, expected| { - let bytes = res.lock().unwrap(); + let bytes = res.0.lock().unwrap(); assert_eq!(expected, &String::from_utf8_lossy(&**bytes)) }, "F8d6", @@ -371,11 +372,11 @@ pub mod tests { #[test] fn should_trace_create_correctly() { - let (informant, res) = informant(); + let (informant, res, _) = informant(); run_test( informant, move |_, expected| { - let bytes = res.lock().unwrap(); + let bytes = res.0.lock().unwrap(); assert_eq!(expected, &String::from_utf8_lossy(&**bytes)) }, "32343434345830f138343438323439f0", diff --git a/evmbin/src/info.rs b/evmbin/src/info.rs index 3167850df..cd5816181 100644 --- a/evmbin/src/info.rs +++ b/evmbin/src/info.rs @@ -274,7 +274,7 @@ pub mod tests { fn should_call_account_from_spec() { use display::std_json::tests::informant; - let (inf, res) = informant(); + let (inf, res, _) = informant(); let mut params = ActionParams::default(); params.code_address = Address::from_low_u64_be(0x20); params.gas = 0xffff.into(); @@ -284,7 +284,7 @@ pub mod tests { let _result = run_action(&spec, params, inf, TrieSpec::Secure); assert_eq!( - &String::from_utf8_lossy(&**res.lock().unwrap()), + &String::from_utf8_lossy(&**res.0.lock().unwrap()), r#"{"pc":0,"op":98,"opName":"PUSH3","gas":"0xffff","stack":[],"storage":{},"depth":1} {"pc":4,"op":96,"opName":"PUSH1","gas":"0xfffc","stack":["0xaaaaaa"],"storage":{},"depth":1} {"pc":6,"op":96,"opName":"PUSH1","gas":"0xfff9","stack":["0xaaaaaa","0xaa"],"storage":{},"depth":1} diff --git a/evmbin/src/main.rs b/evmbin/src/main.rs index 61d12a90f..159683ec2 100644 --- a/evmbin/src/main.rs +++ b/evmbin/src/main.rs @@ -464,17 +464,14 @@ fn die(msg: T) -> ! { #[cfg(test)] mod tests { - use std::str::FromStr; + use display::std_json::tests::informant; use docopt::Docopt; - use super::{Args, USAGE, Address}; - use ethjson::state::test::{State}; use ethcore::{TrieSpec}; - use ethereum_types::{H256}; + use ethjson::state::test::{State}; + use info::{self, TxInput}; + use super::{Args, USAGE, Address, run_call}; use types::transaction; - use info; - use info::{TxInput}; - use display; #[derive(Debug, PartialEq, Deserialize)] pub struct SampleStateTests { @@ -551,7 +548,32 @@ mod tests { } #[test] - fn should_verify_state_root_using_sample_state_test_json_file() { + #[should_panic] + fn should_not_parse_only_flag_without_state_test() { + let _ = run(&[ + "parity-evm", + "./file.json", + "--chain", "homestead", + "--only=add11", + "--json", + ]); + } + + #[test] + #[should_panic] + fn should_not_parse_only_flag_with_stats() { + let _ = run(&[ + "parity-evm", + "stats", + "./file.json", + "--chain", "homestead", + "--only=add11", + "--json", + ]); + } + + #[test] + fn should_not_verify_state_root_using_sample_state_test_json_file() { let state_tests = include_str!("../res/teststate.json"); // Parse the specified state test JSON file to simulate the CLI command `state-test `. let deserialized_state_tests: SampleStateTests = serde_json::from_str(state_tests) @@ -559,41 +581,30 @@ mod tests { // Simulate the name CLI option `--only NAME` let state_test_name = "add11"; - // Simulate the chain `--chain CHAIN` let pre = deserialized_state_tests.add11.pre_state.into(); let env_info = deserialized_state_tests.add11.env.into(); let multitransaction = deserialized_state_tests.add11.transaction; - let post_roots = [ - // EIP-150 - [ - H256::from_str("f4455d9332a9e171fc41b48350457147c21fc0a92364d9925913f7421e15aa95").unwrap(), - H256::from_str("a0bc824c4186c4c1543851894fbf707b5b1cf771d15e74f3517daf0a3415fe5b").unwrap(), - ], - // EIP-158 - [ - H256::from_str("f4455d9332a9e171fc41b48350457147c21fc0a92364d9925913f7421e15aa95").unwrap(), - H256::from_str("27682055e1899031c92d253ee1d22c40f70a6943724168c0b694a1a503664e0a").unwrap(), - ], - ]; - for (fork_index, (fork_spec_name, tx_states)) in deserialized_state_tests.add11.post_states.iter().enumerate() { + for (fork_spec_name, tx_states) in deserialized_state_tests.add11.post_states.iter() { for (tx_index, tx_state) in tx_states.into_iter().enumerate() { - let post_root = post_roots[fork_index][tx_index]; - let informant = display::json::Informant::default(); - let trie_spec = TrieSpec::Secure; // TrieSpec::Fat for --std_dump_json + let (informant, _, res) = informant(); + let trie_spec = TrieSpec::Secure; let transaction: transaction::SignedTransaction = multitransaction.select(&tx_state.indexes).into(); let tx_input = TxInput { state_test_name: &state_test_name, tx_index, fork_spec_name: &fork_spec_name, pre_state: &pre, - post_root, + post_root: tx_states[tx_index].hash.0, env_info: &env_info, transaction, informant, trie_spec, }; - assert!(info::run_transaction(tx_input)); + assert!(!info::run_transaction(tx_input)); + assert!( + &String::from_utf8_lossy(&**res.0.lock().unwrap()).contains("State root mismatch") + ); } } } @@ -607,28 +618,12 @@ mod tests { // Simulate the name CLI option `--only NAME` let state_test_name = "create2callPrecompiles"; - let post_roots = [ - // Constantinople - [ - H256::from_str("3dfdcd1d19badbbba8b0c953504e8b4685270ee5b86e155350b6ef1042c9ce43").unwrap(), - H256::from_str("88803085d3420aec76078e215f67fc5f7b6f297fbe19d85c2236ad685d0fc7fc").unwrap(), - H256::from_str("57181dda5c067cb31f084c4118791b40d5028c39071e83e60e7f7403d683527e").unwrap(), - H256::from_str("f04c1039893eb6959354c3c16e9fe025d4b9dc3981362f79c56cc427dca0d544").unwrap(), - H256::from_str("5d5db3d6c4377b34b74ecf8638f684acb220cc7ce286ae5f000ffa74faf38bae").unwrap(), - H256::from_str("f8343b2e05ae120bf25947de840cedf1ca2c1bcda1cdb89d218427d8a84d4798").unwrap(), - H256::from_str("305a8a8a7d9da97d14ed2259503d9373d803ea4b7fbf8c360f50b1b30a3d04ed").unwrap(), - H256::from_str("de1d3953b508913c6e3e9bd412cd50daf60bb177517e5d1e8ccb0dab193aed03").unwrap(), - ], - ]; let pre = deserialized_state_tests.create2call_precompiles.pre_state.into(); let env_info = deserialized_state_tests.create2call_precompiles.env.into(); let multitransaction = deserialized_state_tests.create2call_precompiles.transaction; - for (fork_index, (fork_spec_name, tx_states)) in - deserialized_state_tests.create2call_precompiles.post_states.iter().enumerate() { + for (fork_spec_name, tx_states) in deserialized_state_tests.create2call_precompiles.post_states.iter() { for (tx_index, tx_state) in tx_states.into_iter().enumerate() { - let informant = display::json::Informant::default(); - // Hash of latest transaction index in the chain - let post_root = post_roots[fork_index][tx_index]; + let (informant, _, _) = informant(); let trie_spec = TrieSpec::Secure; // TrieSpec::Fat for --std_dump_json let transaction: transaction::SignedTransaction = multitransaction.select(&tx_state.indexes).into(); let tx_input = TxInput { @@ -636,7 +631,7 @@ mod tests { tx_index, fork_spec_name: &fork_spec_name, pre_state: &pre, - post_root, + post_root: tx_states[tx_index].hash.0, env_info: &env_info, transaction, informant, @@ -646,4 +641,54 @@ mod tests { } } } + + #[test] + fn should_error_out_of_gas() { + let args = run(&[ + "parity-evm", + "stats", + "--to", "0000000000000000000000000000000000000004", + "--from", "0000000000000000000000000000000000000003", + "--code", "05", + "--input", "06", + "--gas", "1", + "--gas-price", "2", + "--only=add11", + "--std-json", + "--std-out-only", + ]); + + let (inf, _, res) = informant(); + run_call(args, inf); + + assert!( + &String::from_utf8_lossy(&**res.0.lock().unwrap()) + .starts_with(r#"{"error":"EVM: Out of gas","gasUsed":"0x1","#), + ); + } + + #[test] + fn should_not_error_out_of_gas() { + let args = run(&[ + "parity-evm", + "stats", + "--to", "0000000000000000000000000000000000000004", + "--from", "0000000000000000000000000000000000000003", + "--code", "05", + "--input", "06", + "--gas", "21", + "--gas-price", "2", + "--only=add11", + "--std-json", + "--std-out-only", + ]); + + let (inf, _, res) = informant(); + run_call(args, inf); + + assert!( + &String::from_utf8_lossy(&**res.0.lock().unwrap()) + .starts_with(r#"{"output":"0x06","gasUsed":"0x12","#), + ); + } }