[evmbin] add more tests to main.rs (#10956)

This commit is contained in:
Andronik Ordian 2019-08-09 13:14:41 +02:00 committed by Marek Kotewicz
parent 49f219451b
commit cafdfa8107
3 changed files with 103 additions and 57 deletions

View File

@ -332,20 +332,21 @@ pub mod tests {
} }
} }
pub fn informant() -> (Informant<TestWriter, TestWriter>, Arc<Mutex<Vec<u8>>>) { pub fn informant() -> (Informant<TestWriter, TestWriter>, TestWriter, TestWriter) {
let trace_writer: TestWriter = Default::default(); let trace_writer: TestWriter = Default::default();
let out_writer: TestWriter = Default::default(); let out_writer: TestWriter = Default::default();
let res = trace_writer.0.clone(); let trace_copy = Clone::clone(&trace_writer);
(Informant::new(trace_writer, out_writer), res) let out_copy = Clone::clone(&out_writer);
(Informant::new(trace_writer, out_writer), trace_copy, out_copy)
} }
#[test] #[test]
fn should_trace_failure() { fn should_trace_failure() {
let (inf, res) = informant(); let (inf, res, _) = informant();
run_test( run_test(
inf, inf,
move |_, expected| { move |_, expected| {
let bytes = res.lock().unwrap(); let bytes = res.0.lock().unwrap();
assert_eq!(expected, &String::from_utf8_lossy(&**bytes)) assert_eq!(expected, &String::from_utf8_lossy(&**bytes))
}, },
"60F8d6", "60F8d6",
@ -355,11 +356,11 @@ pub mod tests {
"#, "#,
); );
let (inf, res) = informant(); let (inf, res, _) = informant();
run_test( run_test(
inf, inf,
move |_, expected| { move |_, expected| {
let bytes = res.lock().unwrap(); let bytes = res.0.lock().unwrap();
assert_eq!(expected, &String::from_utf8_lossy(&**bytes)) assert_eq!(expected, &String::from_utf8_lossy(&**bytes))
}, },
"F8d6", "F8d6",
@ -371,11 +372,11 @@ pub mod tests {
#[test] #[test]
fn should_trace_create_correctly() { fn should_trace_create_correctly() {
let (informant, res) = informant(); let (informant, res, _) = informant();
run_test( run_test(
informant, informant,
move |_, expected| { move |_, expected| {
let bytes = res.lock().unwrap(); let bytes = res.0.lock().unwrap();
assert_eq!(expected, &String::from_utf8_lossy(&**bytes)) assert_eq!(expected, &String::from_utf8_lossy(&**bytes))
}, },
"32343434345830f138343438323439f0", "32343434345830f138343438323439f0",

View File

@ -274,7 +274,7 @@ pub mod tests {
fn should_call_account_from_spec() { fn should_call_account_from_spec() {
use display::std_json::tests::informant; use display::std_json::tests::informant;
let (inf, res) = informant(); let (inf, res, _) = informant();
let mut params = ActionParams::default(); let mut params = ActionParams::default();
params.code_address = Address::from_low_u64_be(0x20); params.code_address = Address::from_low_u64_be(0x20);
params.gas = 0xffff.into(); params.gas = 0xffff.into();
@ -284,7 +284,7 @@ pub mod tests {
let _result = run_action(&spec, params, inf, TrieSpec::Secure); let _result = run_action(&spec, params, inf, TrieSpec::Secure);
assert_eq!( 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} 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":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} {"pc":6,"op":96,"opName":"PUSH1","gas":"0xfff9","stack":["0xaaaaaa","0xaa"],"storage":{},"depth":1}

View File

@ -464,17 +464,14 @@ fn die<T: fmt::Display>(msg: T) -> ! {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::str::FromStr; use display::std_json::tests::informant;
use docopt::Docopt; use docopt::Docopt;
use super::{Args, USAGE, Address};
use ethjson::state::test::{State};
use ethcore::{TrieSpec}; 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 types::transaction;
use info;
use info::{TxInput};
use display;
#[derive(Debug, PartialEq, Deserialize)] #[derive(Debug, PartialEq, Deserialize)]
pub struct SampleStateTests { pub struct SampleStateTests {
@ -551,7 +548,32 @@ mod tests {
} }
#[test] #[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"); let state_tests = include_str!("../res/teststate.json");
// Parse the specified state test JSON file to simulate the CLI command `state-test <file>`. // Parse the specified state test JSON file to simulate the CLI command `state-test <file>`.
let deserialized_state_tests: SampleStateTests = serde_json::from_str(state_tests) let deserialized_state_tests: SampleStateTests = serde_json::from_str(state_tests)
@ -559,41 +581,30 @@ mod tests {
// Simulate the name CLI option `--only NAME` // Simulate the name CLI option `--only NAME`
let state_test_name = "add11"; let state_test_name = "add11";
// Simulate the chain `--chain CHAIN`
let pre = deserialized_state_tests.add11.pre_state.into(); let pre = deserialized_state_tests.add11.pre_state.into();
let env_info = deserialized_state_tests.add11.env.into(); let env_info = deserialized_state_tests.add11.env.into();
let multitransaction = deserialized_state_tests.add11.transaction; let multitransaction = deserialized_state_tests.add11.transaction;
let post_roots = [ for (fork_spec_name, tx_states) in deserialized_state_tests.add11.post_states.iter() {
// 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 (tx_index, tx_state) in tx_states.into_iter().enumerate() { for (tx_index, tx_state) in tx_states.into_iter().enumerate() {
let post_root = post_roots[fork_index][tx_index]; let (informant, _, res) = informant();
let informant = display::json::Informant::default(); let trie_spec = TrieSpec::Secure;
let trie_spec = TrieSpec::Secure; // TrieSpec::Fat for --std_dump_json
let transaction: transaction::SignedTransaction = multitransaction.select(&tx_state.indexes).into(); let transaction: transaction::SignedTransaction = multitransaction.select(&tx_state.indexes).into();
let tx_input = TxInput { let tx_input = TxInput {
state_test_name: &state_test_name, state_test_name: &state_test_name,
tx_index, tx_index,
fork_spec_name: &fork_spec_name, fork_spec_name: &fork_spec_name,
pre_state: &pre, pre_state: &pre,
post_root, post_root: tx_states[tx_index].hash.0,
env_info: &env_info, env_info: &env_info,
transaction, transaction,
informant, informant,
trie_spec, 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` // Simulate the name CLI option `--only NAME`
let state_test_name = "create2callPrecompiles"; 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 pre = deserialized_state_tests.create2call_precompiles.pre_state.into();
let env_info = deserialized_state_tests.create2call_precompiles.env.into(); let env_info = deserialized_state_tests.create2call_precompiles.env.into();
let multitransaction = deserialized_state_tests.create2call_precompiles.transaction; let multitransaction = deserialized_state_tests.create2call_precompiles.transaction;
for (fork_index, (fork_spec_name, tx_states)) in for (fork_spec_name, tx_states) in deserialized_state_tests.create2call_precompiles.post_states.iter() {
deserialized_state_tests.create2call_precompiles.post_states.iter().enumerate() {
for (tx_index, tx_state) in tx_states.into_iter().enumerate() { for (tx_index, tx_state) in tx_states.into_iter().enumerate() {
let informant = display::json::Informant::default(); let (informant, _, _) = informant();
// Hash of latest transaction index in the chain
let post_root = post_roots[fork_index][tx_index];
let trie_spec = TrieSpec::Secure; // TrieSpec::Fat for --std_dump_json let trie_spec = TrieSpec::Secure; // TrieSpec::Fat for --std_dump_json
let transaction: transaction::SignedTransaction = multitransaction.select(&tx_state.indexes).into(); let transaction: transaction::SignedTransaction = multitransaction.select(&tx_state.indexes).into();
let tx_input = TxInput { let tx_input = TxInput {
@ -636,7 +631,7 @@ mod tests {
tx_index, tx_index,
fork_spec_name: &fork_spec_name, fork_spec_name: &fork_spec_name,
pre_state: &pre, pre_state: &pre,
post_root, post_root: tx_states[tx_index].hash.0,
env_info: &env_info, env_info: &env_info,
transaction, transaction,
informant, 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","#),
);
}
} }