@@ -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
use sp_core:: offchain:: OffchainStorage ;
479
490
@@ -492,8 +503,7 @@ fn offchain_local_storage_should_work(wasm_method: WasmExecutionMethod) {
492
503
assert_eq ! ( state. read( ) . persistent_storage. get( b"" , b"test" ) , Some ( vec![ ] ) ) ;
493
504
}
494
505
495
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
496
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
506
+ test_wasm_execution ! ( offchain_http_should_work) ;
497
507
fn offchain_http_should_work ( wasm_method : WasmExecutionMethod ) {
498
508
let mut ext = TestExternalities :: default ( ) ;
499
509
let ( offchain, state) = testing:: TestOffchainExt :: new ( ) ;
@@ -521,9 +531,7 @@ fn offchain_http_should_work(wasm_method: WasmExecutionMethod) {
521
531
) ;
522
532
}
523
533
524
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
525
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
526
- #[ should_panic( expected = "Allocator ran out of space" ) ]
534
+ test_wasm_execution ! ( should_trap_when_heap_exhausted) ;
527
535
fn should_trap_when_heap_exhausted ( wasm_method : WasmExecutionMethod ) {
528
536
let mut ext = TestExternalities :: default ( ) ;
529
537
@@ -533,18 +541,20 @@ fn should_trap_when_heap_exhausted(wasm_method: WasmExecutionMethod) {
533
541
HostFunctions :: host_functions ( ) ,
534
542
8 ,
535
543
) ;
536
- executor. call_in_wasm (
544
+
545
+ let err = executor. call_in_wasm (
537
546
& wasm_binary_unwrap ( ) [ ..] ,
538
547
None ,
539
548
"test_exhaust_heap" ,
540
549
& [ 0 ] ,
541
550
& mut ext. ext ( ) ,
542
551
sp_core:: traits:: MissingHostFunctions :: Allow ,
543
- ) . unwrap ( ) ;
552
+ ) . unwrap_err ( ) ;
553
+
554
+ assert ! ( err. contains( "Allocator ran out of space" ) ) ;
544
555
}
545
556
546
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
547
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
557
+ test_wasm_execution ! ( returns_mutable_static) ;
548
558
fn returns_mutable_static ( wasm_method : WasmExecutionMethod ) {
549
559
let runtime = crate :: wasm_runtime:: create_wasm_runtime_with_code (
550
560
wasm_method,
@@ -569,8 +579,7 @@ fn returns_mutable_static(wasm_method: WasmExecutionMethod) {
569
579
// returned to its initial value and thus the stack space is going to be leaked.
570
580
//
571
581
// See https://github.com/paritytech/substrate/issues/2967 for details
572
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
573
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
582
+ test_wasm_execution ! ( restoration_of_globals) ;
574
583
fn restoration_of_globals ( wasm_method : WasmExecutionMethod ) {
575
584
// Allocate 32 pages (of 65536 bytes) which gives the runtime 2048KB of heap to operate on
576
585
// (plus some additional space unused from the initial pages requested by the wasm runtime
@@ -598,7 +607,7 @@ fn restoration_of_globals(wasm_method: WasmExecutionMethod) {
598
607
assert ! ( res. is_ok( ) ) ;
599
608
}
600
609
601
- # [ test_case ( WasmExecutionMethod :: Interpreted ) ]
610
+ test_wasm_execution ! ( interpreted_only heap_is_reset_between_calls ) ;
602
611
fn heap_is_reset_between_calls ( wasm_method : WasmExecutionMethod ) {
603
612
let runtime = crate :: wasm_runtime:: create_wasm_runtime_with_code (
604
613
wasm_method,
@@ -622,8 +631,7 @@ fn heap_is_reset_between_calls(wasm_method: WasmExecutionMethod) {
622
631
instance. call ( "check_and_set_in_heap" , & params) . unwrap ( ) ;
623
632
}
624
633
625
- #[ test_case( WasmExecutionMethod :: Interpreted ) ]
626
- #[ cfg_attr( feature = "wasmtime" , test_case( WasmExecutionMethod :: Compiled ) ) ]
634
+ test_wasm_execution ! ( parallel_execution) ;
627
635
fn parallel_execution ( wasm_method : WasmExecutionMethod ) {
628
636
let executor = std:: sync:: Arc :: new ( crate :: WasmExecutor :: new (
629
637
wasm_method,
@@ -658,7 +666,7 @@ fn parallel_execution(wasm_method: WasmExecutionMethod) {
658
666
}
659
667
}
660
668
661
- # [ test_case ( WasmExecutionMethod :: Interpreted ) ]
669
+ test_wasm_execution ! ( wasm_tracing_should_work ) ;
662
670
fn wasm_tracing_should_work ( wasm_method : WasmExecutionMethod ) {
663
671
664
672
use std:: sync:: { Arc , Mutex } ;
0 commit comments