@@ -26,7 +26,6 @@ use sp_core::{
26
26
} ;
27
27
use sc_runtime_test:: wasm_binary_unwrap;
28
28
use sp_state_machine:: TestExternalities as CoreTestExternalities ;
29
- use test_case:: test_case;
30
29
use sp_trie:: { TrieConfiguration , trie_types:: Layout } ;
31
30
use sp_wasm_interface:: HostFunctions as _;
32
31
use sp_runtime:: traits:: BlakeTwo256 ;
@@ -37,6 +36,34 @@ use crate::WasmExecutionMethod;
37
36
pub type TestExternalities = CoreTestExternalities < BlakeTwo256 , u64 > ;
38
37
type HostFunctions = sp_io:: SubstrateHostFunctions ;
39
38
39
+ /// Simple macro that runs a given method as test with the available wasm execution methods.
40
+ #[ macro_export]
41
+ macro_rules! test_wasm_execution {
42
+ ( $method_name: ident) => {
43
+ paste:: item! {
44
+ #[ test]
45
+ fn [ <$method_name _interpreted>] ( ) {
46
+ $method_name( WasmExecutionMethod :: Interpreted ) ;
47
+ }
48
+
49
+ #[ test]
50
+ #[ cfg( feature = "wasmtime" ) ]
51
+ fn [ <$method_name _compiled>] ( ) {
52
+ $method_name( WasmExecutionMethod :: Compiled ) ;
53
+ }
54
+ }
55
+ } ;
56
+
57
+ ( interpreted_only $method_name: ident) => {
58
+ paste:: item! {
59
+ #[ test]
60
+ fn [ <$method_name _interpreted>] ( ) {
61
+ $method_name( WasmExecutionMethod :: Interpreted ) ;
62
+ }
63
+ }
64
+ } ;
65
+ }
66
+
40
67
fn call_in_wasm < E : Externalities > (
41
68
function : & str ,
42
69
call_data : & [ u8 ] ,
@@ -59,8 +86,7 @@ fn call_in_wasm<E: Externalities>(
59
86
)
60
87
}
61
88
62
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
63
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
89
+ test_wasm_execution ! ( returning_should_work) ;
64
90
fn returning_should_work ( wasm_method : WasmExecutionMethod ) {
65
91
let mut ext = TestExternalities :: default ( ) ;
66
92
let mut ext = ext. ext ( ) ;
@@ -74,8 +100,7 @@ fn returning_should_work(wasm_method: WasmExecutionMethod) {
74
100
assert_eq ! ( output, vec![ 0u8 ; 0 ] ) ;
75
101
}
76
102
77
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
78
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
103
+ test_wasm_execution ! ( call_not_existing_function) ;
79
104
fn call_not_existing_function ( wasm_method : WasmExecutionMethod ) {
80
105
let mut ext = TestExternalities :: default ( ) ;
81
106
let mut ext = ext. ext ( ) ;
@@ -102,8 +127,7 @@ fn call_not_existing_function(wasm_method: WasmExecutionMethod) {
102
127
}
103
128
}
104
129
105
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
106
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
130
+ test_wasm_execution ! ( call_yet_another_not_existing_function) ;
107
131
fn call_yet_another_not_existing_function ( wasm_method : WasmExecutionMethod ) {
108
132
let mut ext = TestExternalities :: default ( ) ;
109
133
let mut ext = ext. ext ( ) ;
@@ -130,8 +154,7 @@ fn call_yet_another_not_existing_function(wasm_method: WasmExecutionMethod) {
130
154
}
131
155
}
132
156
133
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
134
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
157
+ test_wasm_execution ! ( panicking_should_work) ;
135
158
fn panicking_should_work ( wasm_method : WasmExecutionMethod ) {
136
159
let mut ext = TestExternalities :: default ( ) ;
137
160
let mut ext = ext. ext ( ) ;
@@ -161,8 +184,7 @@ fn panicking_should_work(wasm_method: WasmExecutionMethod) {
161
184
assert ! ( output. is_err( ) ) ;
162
185
}
163
186
164
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
165
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
187
+ test_wasm_execution ! ( storage_should_work) ;
166
188
fn storage_should_work ( wasm_method : WasmExecutionMethod ) {
167
189
let mut ext = TestExternalities :: default ( ) ;
168
190
@@ -191,8 +213,7 @@ fn storage_should_work(wasm_method: WasmExecutionMethod) {
191
213
assert_eq ! ( ext, expected) ;
192
214
}
193
215
194
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
195
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
216
+ test_wasm_execution ! ( clear_prefix_should_work) ;
196
217
fn clear_prefix_should_work ( wasm_method : WasmExecutionMethod ) {
197
218
let mut ext = TestExternalities :: default ( ) ;
198
219
{
@@ -225,8 +246,7 @@ fn clear_prefix_should_work(wasm_method: WasmExecutionMethod) {
225
246
assert_eq ! ( expected, ext) ;
226
247
}
227
248
228
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
229
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
249
+ test_wasm_execution ! ( blake2_256_should_work) ;
230
250
fn blake2_256_should_work ( wasm_method : WasmExecutionMethod ) {
231
251
let mut ext = TestExternalities :: default ( ) ;
232
252
let mut ext = ext. ext ( ) ;
@@ -250,8 +270,7 @@ fn blake2_256_should_work(wasm_method: WasmExecutionMethod) {
250
270
) ;
251
271
}
252
272
253
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
254
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
273
+ test_wasm_execution ! ( blake2_128_should_work) ;
255
274
fn blake2_128_should_work ( wasm_method : WasmExecutionMethod ) {
256
275
let mut ext = TestExternalities :: default ( ) ;
257
276
let mut ext = ext. ext ( ) ;
@@ -275,8 +294,7 @@ fn blake2_128_should_work(wasm_method: WasmExecutionMethod) {
275
294
) ;
276
295
}
277
296
278
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
279
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
297
+ test_wasm_execution ! ( sha2_256_should_work) ;
280
298
fn sha2_256_should_work ( wasm_method : WasmExecutionMethod ) {
281
299
let mut ext = TestExternalities :: default ( ) ;
282
300
let mut ext = ext. ext ( ) ;
@@ -306,8 +324,7 @@ fn sha2_256_should_work(wasm_method: WasmExecutionMethod) {
306
324
) ;
307
325
}
308
326
309
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
310
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
327
+ test_wasm_execution ! ( twox_256_should_work) ;
311
328
fn twox_256_should_work ( wasm_method : WasmExecutionMethod ) {
312
329
let mut ext = TestExternalities :: default ( ) ;
313
330
let mut ext = ext. ext ( ) ;
@@ -335,8 +352,7 @@ fn twox_256_should_work(wasm_method: WasmExecutionMethod) {
335
352
) ;
336
353
}
337
354
338
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
339
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
355
+ test_wasm_execution ! ( twox_128_should_work) ;
340
356
fn twox_128_should_work ( wasm_method : WasmExecutionMethod ) {
341
357
let mut ext = TestExternalities :: default ( ) ;
342
358
let mut ext = ext. ext ( ) ;
@@ -360,8 +376,7 @@ fn twox_128_should_work(wasm_method: WasmExecutionMethod) {
360
376
) ;
361
377
}
362
378
363
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
364
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
379
+ test_wasm_execution ! ( ed25519_verify_should_work) ;
365
380
fn ed25519_verify_should_work ( wasm_method : WasmExecutionMethod ) {
366
381
let mut ext = TestExternalities :: default ( ) ;
367
382
let mut ext = ext. ext ( ) ;
@@ -397,8 +412,7 @@ fn ed25519_verify_should_work(wasm_method: WasmExecutionMethod) {
397
412
) ;
398
413
}
399
414
400
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
401
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
415
+ test_wasm_execution ! ( sr25519_verify_should_work) ;
402
416
fn sr25519_verify_should_work ( wasm_method : WasmExecutionMethod ) {
403
417
let mut ext = TestExternalities :: default ( ) ;
404
418
let mut ext = ext. ext ( ) ;
@@ -434,8 +448,7 @@ fn sr25519_verify_should_work(wasm_method: WasmExecutionMethod) {
434
448
) ;
435
449
}
436
450
437
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
438
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
451
+ test_wasm_execution ! ( ordered_trie_root_should_work) ;
439
452
fn ordered_trie_root_should_work ( wasm_method : WasmExecutionMethod ) {
440
453
let mut ext = TestExternalities :: default ( ) ;
441
454
let trie_input = vec ! [ b"zero" . to_vec( ) , b"one" . to_vec( ) , b"two" . to_vec( ) ] ;
@@ -450,8 +463,7 @@ fn ordered_trie_root_should_work(wasm_method: WasmExecutionMethod) {
450
463
) ;
451
464
}
452
465
453
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
454
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
466
+ test_wasm_execution ! ( offchain_index) ;
455
467
fn offchain_index ( wasm_method : WasmExecutionMethod ) {
456
468
let mut ext = TestExternalities :: default ( ) ;
457
469
let ( offchain, _state) = testing:: TestOffchainExt :: new ( ) ;
@@ -472,8 +484,7 @@ fn offchain_index(wasm_method: WasmExecutionMethod) {
472
484
) ;
473
485
}
474
486
475
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
476
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
487
+ test_wasm_execution ! ( offchain_local_storage_should_work) ;
477
488
fn offchain_local_storage_should_work ( wasm_method : WasmExecutionMethod ) {
478
489
let mut ext = TestExternalities :: default ( ) ;
479
490
let ( offchain, state) = testing:: TestOffchainExt :: new ( ) ;
@@ -490,8 +501,7 @@ fn offchain_local_storage_should_work(wasm_method: WasmExecutionMethod) {
490
501
assert_eq ! ( state. read( ) . persistent_storage. get( b"test" ) , Some ( vec![ ] ) ) ;
491
502
}
492
503
493
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
494
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
504
+ test_wasm_execution ! ( offchain_http_should_work) ;
495
505
fn offchain_http_should_work ( wasm_method : WasmExecutionMethod ) {
496
506
let mut ext = TestExternalities :: default ( ) ;
497
507
let ( offchain, state) = testing:: TestOffchainExt :: new ( ) ;
@@ -519,9 +529,7 @@ fn offchain_http_should_work(wasm_method: WasmExecutionMethod) {
519
529
) ;
520
530
}
521
531
522
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
523
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
524
- #[ should_panic( expected = "Allocator ran out of space" ) ]
532
+ test_wasm_execution ! ( should_trap_when_heap_exhausted) ;
525
533
fn should_trap_when_heap_exhausted ( wasm_method : WasmExecutionMethod ) {
526
534
let mut ext = TestExternalities :: default ( ) ;
527
535
@@ -531,18 +539,20 @@ fn should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) {
531
539
HostFunctions :: host_functions ( ) ,
532
540
8 ,
533
541
) ;
534
- executor. call_in_wasm (
542
+
543
+ let err = executor. call_in_wasm (
535
544
& wasm_binary_unwrap ( ) [ ..] ,
536
545
None ,
537
546
"test_exhaust_heap" ,
538
547
& [ 0 ] ,
539
548
& mut ext. ext ( ) ,
540
549
sp_core:: traits:: MissingHostFunctions :: Allow ,
541
- ) . unwrap ( ) ;
550
+ ) . unwrap_err ( ) ;
551
+
552
+ assert ! ( err. contains( "Allocator ran out of space" ) ) ;
542
553
}
543
554
544
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
545
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
555
+ test_wasm_execution ! ( returns_mutable_static) ;
546
556
fn returns_mutable_static ( wasm_method : WasmExecutionMethod ) {
547
557
let runtime = crate :: wasm_runtime:: create_wasm_runtime_with_code (
548
558
wasm_method,
@@ -567,8 +577,7 @@ fn returns_mutable_static(wasm_method: WasmExecutionMethod) {
567
577
// returned to its initial value and thus the stack space is going to be leaked.
568
578
//
569
579
// See https://github.com/paritytech/substrate/issues/2967 for details
570
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
571
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
580
+ test_wasm_execution ! ( restoration_of_globals) ;
572
581
fn restoration_of_globals ( wasm_method : WasmExecutionMethod ) {
573
582
// Allocate 32 pages (of 65536 bytes) which gives the runtime 2048KB of heap to operate on
574
583
// (plus some additional space unused from the initial pages requested by the wasm runtime
@@ -596,7 +605,7 @@ fn restoration_of_globals(wasm_method: WasmExecutionMethod) {
596
605
assert ! ( res. is_ok( ) ) ;
597
606
}
598
607
599
- # [ test_case ( WasmExecutionMethod :: Interpreted ) ]
608
+ test_wasm_execution ! ( interpreted_only heap_is_reset_between_calls ) ;
600
609
fn heap_is_reset_between_calls ( wasm_method : WasmExecutionMethod ) {
601
610
let runtime = crate :: wasm_runtime:: create_wasm_runtime_with_code (
602
611
wasm_method,
@@ -620,8 +629,7 @@ fn heap_is_reset_between_calls(wasm_method: WasmExecutionMethod) {
620
629
instance. call_export ( "check_and_set_in_heap" , & params) . unwrap ( ) ;
621
630
}
622
631
623
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
624
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
632
+ test_wasm_execution ! ( parallel_execution) ;
625
633
fn parallel_execution ( wasm_method : WasmExecutionMethod ) {
626
634
let executor = std:: sync:: Arc :: new ( crate :: WasmExecutor :: new (
627
635
wasm_method,
@@ -656,7 +664,7 @@ fn parallel_execution(wasm_method: WasmExecutionMethod) {
656
664
}
657
665
}
658
666
659
- # [ test_case ( WasmExecutionMethod :: Interpreted ) ]
667
+ test_wasm_execution ! ( wasm_tracing_should_work ) ;
660
668
fn wasm_tracing_should_work ( wasm_method : WasmExecutionMethod ) {
661
669
662
670
use std:: sync:: { Arc , Mutex } ;
@@ -728,10 +736,8 @@ fn wasm_tracing_should_work(wasm_method: WasmExecutionMethod) {
728
736
assert_eq ! ( len, 2 ) ;
729
737
}
730
738
731
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
732
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
739
+ test_wasm_execution ! ( spawning_runtime_instance_should_work) ;
733
740
fn spawning_runtime_instance_should_work ( wasm_method : WasmExecutionMethod ) {
734
-
735
741
let mut ext = TestExternalities :: default ( ) ;
736
742
let mut ext = ext. ext ( ) ;
737
743
@@ -743,10 +749,8 @@ fn spawning_runtime_instance_should_work(wasm_method: WasmExecutionMethod) {
743
749
) . unwrap ( ) ;
744
750
}
745
751
746
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
747
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
752
+ test_wasm_execution ! ( spawning_runtime_instance_nested_should_work) ;
748
753
fn spawning_runtime_instance_nested_should_work ( wasm_method : WasmExecutionMethod ) {
749
-
750
754
let mut ext = TestExternalities :: default ( ) ;
751
755
let mut ext = ext. ext ( ) ;
752
756
@@ -758,10 +762,8 @@ fn spawning_runtime_instance_nested_should_work(wasm_method: WasmExecutionMethod
758
762
) . unwrap ( ) ;
759
763
}
760
764
761
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
762
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
765
+ test_wasm_execution ! ( panic_in_spawned_instance_panics_on_joining_its_result) ;
763
766
fn panic_in_spawned_instance_panics_on_joining_its_result ( wasm_method : WasmExecutionMethod ) {
764
-
765
767
let mut ext = TestExternalities :: default ( ) ;
766
768
let mut ext = ext. ext ( ) ;
767
769
0 commit comments