Coverage Report

Created: 2024-05-16 12:16

/__w/smoldot/smoldot/repo/full-node/tests/json-rpc-basic.rs
Line
Count
Source (jump to first uncovered line)
1
// Smoldot
2
// Copyright (C) 2023  Pierre Krieger
3
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
4
5
// This program is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
10
// This program is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
15
// You should have received a copy of the GNU General Public License
16
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
use smoldot::json_rpc;
19
use std::sync::Arc;
20
21
#[test]
22
1
fn send_request_errs_if_malformed() {
23
1
    smol::block_on(async move {
24
1
        let client = smoldot_full_node::start(smoldot_full_node::Config {
25
1
            chain: smoldot_full_node::ChainConfig {
26
1
                chain_spec: (&include_bytes!("./substrate-node-template.json")[..]).into(),
27
1
                additional_bootnodes: Vec::new(),
28
1
                keystore_memory: vec![],
29
1
                sqlite_database_path: None,
30
1
                sqlite_cache_size: 256 * 1024 * 1024,
31
1
                keystore_path: None,
32
1
                json_rpc_listen: None,
33
1
            },
34
1
            relay_chain: None,
35
1
            libp2p_key: Box::new([0; 32]),
36
1
            listen_addresses: Vec::new(),
37
37
            tasks_executor: Arc::new(|task| smol::spawn(task).detach()),
38
2
            log_callback: Arc::new(move |_, _| {}),
39
1
            jaeger_agent: None,
40
1
        })
41
5
        .await
42
1
        .unwrap();
43
1
44
1
        client.send_json_rpc_request(r#"thisisnotproperjsonrpc"#.to_owned());
45
1
        let response_raw = client.next_json_rpc_response().await;
46
1
        match json_rpc::parse::parse_response(&response_raw).unwrap() {
47
1
            json_rpc::parse::Response::ParseError { .. } => {}
48
0
            _ => unreachable!(),
49
        }
50
1
    });
51
1
}
52
53
#[test]
54
1
fn send_request_works_if_unknown_request() {
55
1
    smol::block_on(async move {
56
1
        let client = smoldot_full_node::start(smoldot_full_node::Config {
57
1
            chain: smoldot_full_node::ChainConfig {
58
1
                chain_spec: (&include_bytes!("./substrate-node-template.json")[..]).into(),
59
1
                additional_bootnodes: Vec::new(),
60
1
                keystore_memory: vec![],
61
1
                sqlite_database_path: None,
62
1
                sqlite_cache_size: 256 * 1024 * 1024,
63
1
                keystore_path: None,
64
1
                json_rpc_listen: None,
65
1
            },
66
1
            relay_chain: None,
67
1
            libp2p_key: Box::new([0; 32]),
68
1
            listen_addresses: Vec::new(),
69
37
            tasks_executor: Arc::new(|task| smol::spawn(task).detach()),
70
2
            log_callback: Arc::new(move |_, _| {}),
71
1
            jaeger_agent: None,
72
1
        })
73
5
        .await
74
1
        .unwrap();
75
1
76
1
        client.send_json_rpc_request(
77
1
            r#"{"jsonrpc":"2.0","id":1,"method":"thisjsonrpcmethoddoesntexist","params":[]}"#
78
1
                .to_owned(),
79
1
        );
80
1
        let response_raw = client.next_json_rpc_response().
await0
;
81
1
        match json_rpc::parse::parse_response(&response_raw).unwrap() {
82
1
            json_rpc::parse::Response::Error { id_json, .. } => {
83
1
                assert_eq!(id_json, "1");
84
            }
85
0
            _ => unreachable!(),
86
        }
87
1
    });
88
1
}