|
1 | | -// // This file is part of Substrate. |
2 | | - |
3 | | -// // Copyright (C) 2020-2021 Parity Technologies (UK) Ltd. |
4 | | -// // SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 |
5 | | - |
6 | | -// // This program is free software: you can redistribute it and/or modify |
7 | | -// // it under the terms of the GNU General Public License as published by |
8 | | -// // the Free Software Foundation, either version 3 of the License, or |
9 | | -// // (at your option) any later version. |
10 | | - |
11 | | -// // This program is distributed in the hope that it will be useful, |
12 | | -// // but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | -// // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | -// // GNU General Public License for more details. |
15 | | - |
16 | | -// // You should have received a copy of the GNU General Public License |
17 | | -// // along with this program. If not, see <https://www.gnu.org/licenses/>. |
18 | | - |
19 | | -// use super::*; |
20 | | -// use assert_matches::assert_matches; |
21 | | -// use sp_core::{offchain::storage::InMemOffchainStorage, Bytes}; |
22 | | - |
23 | | -// #[test] |
24 | | -// fn local_storage_should_work() { |
25 | | -// let storage = InMemOffchainStorage::default(); |
26 | | -// let offchain = Offchain::new(storage, DenyUnsafe::No); |
27 | | -// let key = Bytes(b"offchain_storage".to_vec()); |
28 | | -// let value = Bytes(b"offchain_value".to_vec()); |
29 | | - |
30 | | -// assert_matches!( |
31 | | -// offchain.set_local_storage(StorageKind::PERSISTENT, key.clone(), value.clone()), |
32 | | -// Ok(()) |
33 | | -// ); |
34 | | -// assert_matches!( |
35 | | -// offchain.get_local_storage(StorageKind::PERSISTENT, key), |
36 | | -// Ok(Some(ref v)) if *v == value |
37 | | -// ); |
38 | | -// } |
39 | | - |
40 | | -// #[test] |
41 | | -// fn offchain_calls_considered_unsafe() { |
42 | | -// let storage = InMemOffchainStorage::default(); |
43 | | -// let offchain = Offchain::new(storage, DenyUnsafe::Yes); |
44 | | -// let key = Bytes(b"offchain_storage".to_vec()); |
45 | | -// let value = Bytes(b"offchain_value".to_vec()); |
46 | | - |
47 | | -// assert_matches!( |
48 | | -// offchain.set_local_storage(StorageKind::PERSISTENT, key.clone(), value.clone()), |
49 | | -// Err(Error::UnsafeRpcCalled(_)) |
50 | | -// ); |
51 | | -// assert_matches!( |
52 | | -// offchain.get_local_storage(StorageKind::PERSISTENT, key), |
53 | | -// Err(Error::UnsafeRpcCalled(_)) |
54 | | -// ); |
55 | | -// } |
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd. |
| 4 | +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 |
| 5 | + |
| 6 | +// This program is free software: you can redistribute it and/or modify |
| 7 | +// it under the terms of the GNU General Public License as published by |
| 8 | +// the Free Software Foundation, either version 3 of the License, or |
| 9 | +// (at your option) any later version. |
| 10 | + |
| 11 | +// This program is distributed in the hope that it will be useful, |
| 12 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +// GNU General Public License for more details. |
| 15 | + |
| 16 | +// You should have received a copy of the GNU General Public License |
| 17 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +use super::*; |
| 20 | +use assert_matches::assert_matches; |
| 21 | +use sp_core::{offchain::storage::InMemOffchainStorage, Bytes}; |
| 22 | + |
| 23 | +#[test] |
| 24 | +fn local_storage_should_work() { |
| 25 | + let storage = InMemOffchainStorage::default(); |
| 26 | + let offchain = Offchain::new(storage, DenyUnsafe::No); |
| 27 | + let key = Bytes(b"offchain_storage".to_vec()); |
| 28 | + let value = Bytes(b"offchain_value".to_vec()); |
| 29 | + |
| 30 | + assert_matches!( |
| 31 | + offchain.set_local_storage(StorageKind::PERSISTENT, key.clone(), value.clone()), |
| 32 | + Ok(()) |
| 33 | + ); |
| 34 | + assert_matches!( |
| 35 | + offchain.get_local_storage(StorageKind::PERSISTENT, key), |
| 36 | + Ok(Some(ref v)) if *v == value |
| 37 | + ); |
| 38 | +} |
| 39 | + |
| 40 | +#[test] |
| 41 | +fn offchain_calls_considered_unsafe() { |
| 42 | + use jsonrpsee::types::CallError; |
| 43 | + let storage = InMemOffchainStorage::default(); |
| 44 | + let offchain = Offchain::new(storage, DenyUnsafe::Yes); |
| 45 | + let key = Bytes(b"offchain_storage".to_vec()); |
| 46 | + let value = Bytes(b"offchain_value".to_vec()); |
| 47 | + |
| 48 | + assert_matches!( |
| 49 | + offchain.set_local_storage(StorageKind::PERSISTENT, key.clone(), value.clone()), |
| 50 | + Err(JsonRpseeError::Call(CallError::Failed(err))) => { |
| 51 | + assert_eq!(err.to_string(), "RPC call is unsafe to be called externally") |
| 52 | + } |
| 53 | + ); |
| 54 | + assert_matches!( |
| 55 | + offchain.get_local_storage(StorageKind::PERSISTENT, key), |
| 56 | + Err(JsonRpseeError::Call(CallError::Failed(err))) => { |
| 57 | + assert_eq!(err.to_string(), "RPC call is unsafe to be called externally") |
| 58 | + } |
| 59 | + ); |
| 60 | +} |
0 commit comments