Fixing hash deserialisation (#1674)

This commit is contained in:
Tomasz Drwięga 2016-07-20 12:42:12 +02:00 committed by Gav Wood
parent 7dd29825c4
commit 46b5801730
3 changed files with 44 additions and 2 deletions

View File

@ -608,6 +608,28 @@ fn rpc_eth_send_transaction() {
assert_eq!(tester.io.handle_request(&request), Some(response)); assert_eq!(tester.io.handle_request(&request), Some(response));
} }
#[test]
fn rpc_eth_send_transaction_with_bad_to() {
let tester = EthTester::default();
let address = tester.accounts_provider.new_account("").unwrap();
let request = r#"{
"jsonrpc": "2.0",
"method": "eth_sendTransaction",
"params": [{
"from": ""#.to_owned() + format!("0x{:?}", address).as_ref() + r#"",
"to": "",
"gas": "0x76c0",
"gasPrice": "0x9184e72a000",
"value": "0x9184e72a"
}],
"id": 1
}"#;
let response = r#"{"jsonrpc":"2.0","error":{"code":-32602,"message":"Invalid params","data":null},"id":1}"#;
assert_eq!(tester.io.handle_request(&request), Some(response.into()));
}
#[test] #[test]
fn rpc_eth_send_transaction_error() { fn rpc_eth_send_transaction_error() {

View File

@ -116,13 +116,17 @@ macro_rules! impl_hash {
type Value = $name; type Value = $name;
fn visit_str<E>(&mut self, value: &str) -> Result<Self::Value, E> where E: serde::Error { fn visit_str<E>(&mut self, value: &str) -> Result<Self::Value, E> where E: serde::Error {
if value.len() != 2 + $size * 2 {
return Err(serde::Error::custom("Invalid length."));
}
match value[2..].from_hex() { match value[2..].from_hex() {
Ok(ref v) if v.len() == $size => { Ok(ref v) => {
let mut result = [0u8; $size]; let mut result = [0u8; $size];
result.copy_from_slice(v); result.copy_from_slice(v);
Ok($name(result)) Ok($name(result))
}, },
Ok(_) => Err(serde::Error::custom("Invalid length.")),
_ => Err(serde::Error::custom("Invalid hex value.")) _ => Err(serde::Error::custom("Invalid hex value."))
} }
} }

View File

@ -188,6 +188,22 @@ mod tests {
}); });
} }
#[test]
fn transaction_request_deserialize_error() {
let s = r#"{
"from":"0xb5f7502a2807cb23615c7456055e1d65b2508625",
"to":"",
"data":"0x8595bab1",
"gas":"0x2fd618",
"gasPrice":"0x0ba43b7400"
}"#;
let deserialized = serde_json::from_str::<TransactionRequest>(s);
assert!(deserialized.is_err(), "Should be error because to is empty");
}
#[test] #[test]
fn should_deserialize_modification() { fn should_deserialize_modification() {
// given // given