wasm: math_sub_with_overflow test (#6474)

This commit is contained in:
Svyatoslav Nikolsky 2017-09-11 12:43:33 +03:00 committed by Gav Wood
parent dcea17f8c6
commit 03e039b13d

View File

@ -436,16 +436,12 @@ macro_rules! reqrep_test {
fake_ext.info = $info; fake_ext.info = $info;
fake_ext.blockhashes = $block_hashes; fake_ext.blockhashes = $block_hashes;
let (gas_left, result) = {
let mut interpreter = wasm_interpreter(); let mut interpreter = wasm_interpreter();
let result = interpreter.exec(params, &mut fake_ext).expect("Interpreter to execute without any errors"); interpreter.exec(params, &mut fake_ext)
match result { .map(|result| match result {
GasLeft::Known(_) => { panic!("Test is expected to return payload to check"); }, GasLeft::Known(_) => { panic!("Test is expected to return payload to check"); },
GasLeft::NeedsReturn { gas_left: gas, data: result, apply_state: _apply } => (gas, result.to_vec()), GasLeft::NeedsReturn { gas_left: gas, data: result, apply_state: _apply } => (gas, result.to_vec()),
} })
};
(gas_left, result)
} }
}; };
} }
@ -470,7 +466,7 @@ fn math_add() {
arg_b.to_big_endian(&mut args[33..65]); arg_b.to_big_endian(&mut args[33..65]);
args.to_vec() args.to_vec()
} }
); ).expect("Interpreter to execute without any errors");
assert_eq!(gas_left, U256::from(98177)); assert_eq!(gas_left, U256::from(98177));
assert_eq!( assert_eq!(
@ -492,7 +488,7 @@ fn math_mul() {
arg_b.to_big_endian(&mut args[33..65]); arg_b.to_big_endian(&mut args[33..65]);
args.to_vec() args.to_vec()
} }
); ).expect("Interpreter to execute without any errors");
assert_eq!(gas_left, U256::from(97326)); assert_eq!(gas_left, U256::from(97326));
assert_eq!( assert_eq!(
@ -501,7 +497,7 @@ fn math_mul() {
); );
} }
// substraction // subtraction
#[test] #[test]
fn math_sub() { fn math_sub() {
let (gas_left, result) = reqrep_test!( let (gas_left, result) = reqrep_test!(
@ -514,7 +510,7 @@ fn math_sub() {
arg_b.to_big_endian(&mut args[33..65]); arg_b.to_big_endian(&mut args[33..65]);
args.to_vec() args.to_vec()
} }
); ).expect("Interpreter to execute without any errors");
assert_eq!(gas_left, U256::from(98221)); assert_eq!(gas_left, U256::from(98221));
assert_eq!( assert_eq!(
@ -523,6 +519,24 @@ fn math_sub() {
); );
} }
// subtraction with overflow
#[test]
fn math_sub_with_overflow() {
let result = reqrep_test!(
"math.wasm",
{
let mut args = [2u8; 65];
let arg_a = U256::from_dec_str("888888888888888888888888888888").unwrap();
let arg_b = U256::from_dec_str("999999999999999999999999999999").unwrap();
arg_a.to_big_endian(&mut args[1..33]);
arg_b.to_big_endian(&mut args[33..65]);
args.to_vec()
}
);
assert_eq!(result, Err(vm::Error::Wasm("Wasm runtime error: User(Panic(\"arithmetic operation overflow\"))".into())));
}
#[test] #[test]
fn math_div() { fn math_div() {
let (gas_left, result) = reqrep_test!( let (gas_left, result) = reqrep_test!(
@ -535,7 +549,7 @@ fn math_div() {
arg_b.to_big_endian(&mut args[33..65]); arg_b.to_big_endian(&mut args[33..65]);
args.to_vec() args.to_vec()
} }
); ).expect("Interpreter to execute without any errors");
assert_eq!(gas_left, U256::from(91510)); assert_eq!(gas_left, U256::from(91510));
assert_eq!( assert_eq!(
@ -572,7 +586,7 @@ fn externs() {
); );
hashes hashes
} }
); ).expect("Interpreter to execute without any errors");
assert_eq!( assert_eq!(
&result[0..64].to_vec(), &result[0..64].to_vec(),