-
Notifications
You must be signed in to change notification settings - Fork 25
/
rpc.rs
134 lines (117 loc) · 5.14 KB
/
rpc.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2019-2023 Artem Vorotnikov and Monero Rust Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#[cfg(feature = "rpc_authentication")]
use monero::Hash;
#[cfg(feature = "rpc_authentication")]
use monero_rpc::RpcAuthentication;
#[cfg(feature = "rpc_authentication")]
use monero_rpc::RpcClient;
#[cfg(feature = "rpc_authentication")]
use monero_rpc::RpcClientBuilder;
#[cfg(feature = "rpc_authentication")]
use std::env;
mod clients_tests;
#[tokio::test]
async fn main_functional_test() {
/* FAQ:
*
* - Q: What is the purpose of each test function?
* A: See the comments in each of ther files in `tests/clients_tests.rs`.
*
* - Q: Why are the functions called in the order below?
* A: `basic_wallet` only calls functions from the `WalletClient` that do not modify
* the blockchain. For example: it creates wallets, but it does not create transfers.
*
* At the same time, `empty_blockchain` needs the blockchain to be empty, and so it can
* run at the same time `basic_wallet` runs. Moreover, `basic_daemon_rpc` has only
* one function: `get_transactions`; and since neither `basic_wallet` nor `empty_blockchain`
* nor `non_empty_blockchain` create transactions, it can also run at the same time then
* other functions are running because its result will always be the same.
*
* Now, inside `handle2`, `empty_blockchain` runs before `non_empty_blockchain`
* because, as said, `empty_blockchain` needs a fresh blockchain, but `non_empty_blockchain`
* modifies the blockchain by adding blocks to it.
*
* Finally, `all_clients_interaction` runs at the end because it modifies the blockchain
* in ways the other tests do not (for example, it creates transacions), and it also creates
* blocks, so the other tests would not work if running at the same time
* `all_clients_interaction` runs. Also, it makes sense `all_clients_interaction` to
* run last because the other tests test each client individually, but `all_clients_interaction`
* calls functions from all clients.
*
*/
let handle1 = tokio::spawn(clients_tests::basic_wallet::run());
let handle2 = tokio::spawn(async {
clients_tests::empty_blockchain::run().await;
clients_tests::non_empty_blockchain::run().await;
});
let handle3 = tokio::spawn(async {
clients_tests::basic_daemon_rpc::run().await;
});
let res = tokio::try_join!(handle1, handle2, handle3);
res.unwrap();
clients_tests::all_clients_interaction::run().await;
}
// Authentication tests need to run against a monero daemon and wallet rpc with
// the correct username and password configure ("foo" "bar").
#[cfg(feature = "rpc_authentication")]
fn setup_rpc_auth_client(username: &str, password: &str, port: u32) -> RpcClient {
let whost = env::var("MONERO_WALLET_HOST_1").unwrap_or_else(|_| "localhost".into());
let rpc_credentials = RpcAuthentication::Credentials {
username: username.into(),
password: password.into(),
};
RpcClientBuilder::new()
.rpc_authentication(rpc_credentials)
.build(format!("http://{}:{}", whost, port))
.unwrap()
}
#[tokio::test]
#[cfg(feature = "rpc_authentication")]
async fn test_daemon_rpc_auth() {
let rpc_client = setup_rpc_auth_client("foo", "bar", 18085).daemon();
let daemon_transactions = rpc_client.get_block_count().await;
assert!(daemon_transactions.is_ok());
}
#[tokio::test]
#[cfg(feature = "rpc_authentication")]
async fn test_daemon_rpc_auth_fail() {
let rpc_client = setup_rpc_auth_client("invalid", "bar", 18085).daemon();
let daemon_transactions = rpc_client.get_block_count().await;
assert!(daemon_transactions.is_err());
}
#[tokio::test]
#[cfg(feature = "rpc_authentication")]
async fn test_daemon_rpc_rpc_auth() {
let rpc_client = setup_rpc_auth_client("foo", "bar", 18085).daemon_rpc();
let transactions = vec![Hash::from_low_u64_be(1)];
let daemon_transactions = rpc_client
.get_transactions(transactions, Some(true), Some(true))
.await;
assert!(daemon_transactions.is_ok());
}
#[tokio::test]
#[cfg(feature = "rpc_authentication")]
async fn test_rpc_auth() {
let rpc_client = setup_rpc_auth_client("foo", "bar", 18084).wallet();
assert!(rpc_client.get_version().await.is_ok());
let version = rpc_client.get_version().await.unwrap();
assert!(version.0 > 0);
}
#[tokio::test]
#[cfg(feature = "rpc_authentication")]
async fn test_rpc_auth_fail() {
let rpc_client = setup_rpc_auth_client("invalid", "auth", 18084).wallet();
assert!(rpc_client.get_version().await.is_err());
}