referrerpolicy=no-referrer-when-downgrade

sp_runtime_interface_test_wasm_deprecated/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Tests for the runtime interface traits and proc macros.
19
20#![cfg_attr(not(feature = "std"), no_std)]
21
22use sp_core::wasm_export_functions;
23use sp_runtime_interface::runtime_interface;
24
25// Include the WASM binary
26#[cfg(feature = "std")]
27include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
28
29/// Wasm binary unwrapped. If built with `SKIP_WASM_BUILD`, the function panics.
30#[cfg(feature = "std")]
31pub fn wasm_binary_unwrap() -> &'static [u8] {
32	WASM_BINARY.expect(
33		"Development wasm binary is not available. Testing is only supported with the flag \
34		 disabled.",
35	)
36}
37
38/// This function is not used, but we require it for the compiler to include `sp-io`.
39/// `sp-io` is required for its panic and oom handler.
40#[cfg(not(feature = "std"))]
41#[no_mangle]
42pub fn import_sp_io() {
43	sp_io::misc::print_utf8(&[]);
44}
45
46#[runtime_interface]
47pub trait TestApi {
48	fn test_versioning(&self, _data: u32) -> bool {
49		// should not be called
50		unimplemented!()
51	}
52}
53
54wasm_export_functions! {
55	fn test_versioning_works() {
56		// old api allows only 42 and 50
57		assert!(test_api::test_versioning(42));
58		assert!(test_api::test_versioning(50));
59
60		assert!(!test_api::test_versioning(142));
61		assert!(!test_api::test_versioning(0));
62	}
63}