Code redundancy between ext implementation and testing. (#3830)

* fix child_storage_hash

* extract common implementation for ext and testing

* cleaning impl.

* replace ExtBasisMut by actual Ext

* remove extbasis.

* Update tests to use Ext from test externalities.

* use Ext constructor for getting ext from TestExternalities.

* Add missing extensions from ext.

* fix wasmi test

* Fix merge error.
This commit is contained in:
cheme
2019-10-18 09:52:25 +02:00
committed by Bastian Köcher
parent fddfcbacea
commit d9cffa0bb5
8 changed files with 148 additions and 228 deletions
+1
View File
@@ -98,6 +98,7 @@ mod tests {
#[test]
fn call_in_interpreted_wasm_works() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let res = call_in_wasm(
"test_empty_return",
&[],
+10
View File
@@ -610,6 +610,7 @@ mod tests {
#[test]
fn sandbox_should_work() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
let code = wabt::wat2wasm(r#"
@@ -642,6 +643,7 @@ mod tests {
#[test]
fn sandbox_trap() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
let code = wabt::wat2wasm(r#"
@@ -663,6 +665,7 @@ mod tests {
#[test]
fn sandbox_should_trap_when_heap_exhausted() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
let code = wabt::wat2wasm(r#"
@@ -691,6 +694,7 @@ mod tests {
#[test]
fn start_called() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
let code = wabt::wat2wasm(r#"
@@ -729,6 +733,7 @@ mod tests {
#[test]
fn invoke_args() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
let code = wabt::wat2wasm(r#"
@@ -763,6 +768,7 @@ mod tests {
#[test]
fn return_val() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
let code = wabt::wat2wasm(r#"
@@ -785,6 +791,7 @@ mod tests {
#[test]
fn unlinkable_module() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
let code = wabt::wat2wasm(r#"
@@ -805,6 +812,7 @@ mod tests {
#[test]
fn corrupted_module() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
// Corrupted wasm file
@@ -819,6 +827,7 @@ mod tests {
#[test]
fn start_fn_ok() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
let code = wabt::wat2wasm(r#"
@@ -842,6 +851,7 @@ mod tests {
#[test]
fn start_fn_traps() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
let code = wabt::wat2wasm(r#"
+43 -25
View File
@@ -681,6 +681,7 @@ mod tests {
#[test]
fn returning_should_work() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
let output = call(&mut ext, 8, &test_code[..], "test_empty_return", &[]).unwrap();
@@ -690,6 +691,7 @@ mod tests {
#[test]
fn panicking_should_work() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
let output = call(&mut ext, 8, &test_code[..], "test_panic", &[]);
@@ -705,18 +707,22 @@ mod tests {
#[test]
fn storage_should_work() {
let mut ext = TestExternalities::default();
ext.set_storage(b"foo".to_vec(), b"bar".to_vec());
let test_code = WASM_BINARY;
let output = call(
&mut ext,
8,
&test_code[..],
"test_data_in",
&b"Hello world".to_vec().encode(),
).unwrap();
{
let mut ext = ext.ext();
ext.set_storage(b"foo".to_vec(), b"bar".to_vec());
let test_code = WASM_BINARY;
assert_eq!(output, b"all ok!".to_vec().encode());
let output = call(
&mut ext,
8,
&test_code[..],
"test_data_in",
&b"Hello world".to_vec().encode(),
).unwrap();
assert_eq!(output, b"all ok!".to_vec().encode());
}
let expected = TestExternalities::new((map![
b"input".to_vec() => b"Hello world".to_vec(),
@@ -729,23 +735,26 @@ mod tests {
#[test]
fn clear_prefix_should_work() {
let mut ext = TestExternalities::default();
ext.set_storage(b"aaa".to_vec(), b"1".to_vec());
ext.set_storage(b"aab".to_vec(), b"2".to_vec());
ext.set_storage(b"aba".to_vec(), b"3".to_vec());
ext.set_storage(b"abb".to_vec(), b"4".to_vec());
ext.set_storage(b"bbb".to_vec(), b"5".to_vec());
let test_code = WASM_BINARY;
{
let mut ext = ext.ext();
ext.set_storage(b"aaa".to_vec(), b"1".to_vec());
ext.set_storage(b"aab".to_vec(), b"2".to_vec());
ext.set_storage(b"aba".to_vec(), b"3".to_vec());
ext.set_storage(b"abb".to_vec(), b"4".to_vec());
ext.set_storage(b"bbb".to_vec(), b"5".to_vec());
let test_code = WASM_BINARY;
// This will clear all entries which prefix is "ab".
let output = call(
&mut ext,
8,
&test_code[..],
"test_clear_prefix",
&b"ab".to_vec().encode(),
).unwrap();
// This will clear all entries which prefix is "ab".
let output = call(
&mut ext,
8,
&test_code[..],
"test_clear_prefix",
&b"ab".to_vec().encode(),
).unwrap();
assert_eq!(output, b"all ok!".to_vec().encode());
assert_eq!(output, b"all ok!".to_vec().encode());
}
let expected = TestExternalities::new((map![
b"aaa".to_vec() => b"1".to_vec(),
@@ -758,6 +767,7 @@ mod tests {
#[test]
fn blake2_256_should_work() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
assert_eq!(
call(&mut ext, 8, &test_code[..], "test_blake2_256", &[0]).unwrap(),
@@ -778,6 +788,7 @@ mod tests {
#[test]
fn blake2_128_should_work() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
assert_eq!(
call(&mut ext, 8, &test_code[..], "test_blake2_128", &[0]).unwrap(),
@@ -798,6 +809,7 @@ mod tests {
#[test]
fn twox_256_should_work() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
assert_eq!(
call(&mut ext, 8, &test_code[..], "test_twox_256", &[0]).unwrap(),
@@ -822,6 +834,7 @@ mod tests {
#[test]
fn twox_128_should_work() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
assert_eq!(
call(&mut ext, 8, &test_code[..], "test_twox_128", &[0]).unwrap(),
@@ -842,6 +855,7 @@ mod tests {
#[test]
fn ed25519_verify_should_work() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
let key = ed25519::Pair::from_seed(&blake2_256(b"test"));
let sig = key.sign(b"all ok!");
@@ -868,6 +882,7 @@ mod tests {
#[test]
fn sr25519_verify_should_work() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let test_code = WASM_BINARY;
let key = sr25519::Pair::from_seed(&blake2_256(b"test"));
let sig = key.sign(b"all ok!");
@@ -894,6 +909,7 @@ mod tests {
#[test]
fn ordered_trie_root_should_work() {
let mut ext = TestExternalities::default();
let mut ext = ext.ext();
let trie_input = vec![b"zero".to_vec(), b"one".to_vec(), b"two".to_vec()];
let test_code = WASM_BINARY;
assert_eq!(
@@ -910,6 +926,7 @@ mod tests {
let (offchain, state) = testing::TestOffchainExt::new();
ext.register_extension(OffchainExt::new(offchain));
let test_code = WASM_BINARY;
let mut ext = ext.ext();
assert_eq!(
call(&mut ext, 8, &test_code[..], "test_offchain_local_storage", &[0]).unwrap(),
true.encode(),
@@ -937,6 +954,7 @@ mod tests {
);
let test_code = WASM_BINARY;
let mut ext = ext.ext();
assert_eq!(
call(&mut ext, 8, &test_code[..], "test_offchain_http", &[0]).unwrap(),
true.encode(),