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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use crate::{methods, runtime, tests};
use diem_config::config;
use diem_proptest_helpers::ValueGenerator;
use diem_types::account_state_blob::AccountStateWithProof;
use futures::{channel::mpsc::channel, StreamExt};
use std::sync::Arc;
use warp::reply::Reply;
#[macro_export]
macro_rules! gen_request_params {
($params: tt) => {
serde_json::to_vec(&serde_json::json!($params))
.expect("failed to convert JSON to byte array")
};
}
#[test]
fn test_json_rpc_service_fuzzer() {
let mut gen = ValueGenerator::new();
let data = generate_corpus(&mut gen);
fuzzer(&data);
}
#[test]
fn test_method_fuzzer() {
method_fuzzer(&gen_request_params!([]), "get_metadata");
method_fuzzer(&gen_request_params!([0]), "get_metadata");
method_fuzzer(
&gen_request_params!(["000000000000000000000000000000dd"]),
"get_account",
);
method_fuzzer(&gen_request_params!([0, 1, true]), "get_transactions");
method_fuzzer(
&gen_request_params!(["000000000000000000000000000000dd", 0, true]),
"get_account_transaction",
);
method_fuzzer(
&gen_request_params!(["000000000000000000000000000000dd", 0, 1, true]),
"get_account_transactions",
);
method_fuzzer(
&gen_request_params!(["00000000000000000000000000000000000000000a550c18", 0, 10]),
"get_events",
);
method_fuzzer(&gen_request_params!([]), "get_currencies");
method_fuzzer(&gen_request_params!([]), "get_network_status");
method_fuzzer(&gen_request_params!([1]), "get_state_proof");
method_fuzzer(
&gen_request_params!([]),
"get_accumulator_consistency_proof",
);
method_fuzzer(
&gen_request_params!(["000000000000000000000000000000dd", 0, 1]),
"get_account_state_with_proof",
);
method_fuzzer(
&gen_request_params!([0, 1, true]),
"get_transactions_with_proofs",
);
method_fuzzer(
&gen_request_params!(["000000000000000000000000000000dd", 0, 1, true]),
"get_account_transactions_with_proofs",
);
method_fuzzer(
&gen_request_params!(["00000000000000000000000000000000000000000a550c18", 0, 1]),
"get_events_with_proofs",
);
method_fuzzer(
&gen_request_params!(["00000000000000000000000000000000000000000a550c18", 0]),
"get_event_by_version_with_proof",
);
}
pub fn method_fuzzer(params_data: &[u8], method: &str) {
let params = match serde_json::from_slice::<serde_json::Value>(params_data) {
Err(_) => {
if cfg!(test) {
panic!();
}
return;
}
Ok(request) => request,
};
let request =
serde_json::json!({"jsonrpc": "2.0", "method": method, "params": params, "id": 1});
request_fuzzer(request)
}
pub fn generate_corpus(gen: &mut ValueGenerator) -> Vec<u8> {
let txn = gen.generate(proptest::arbitrary::any::<
diem_types::transaction::SignedTransaction,
>());
let payload = hex::encode(bcs::to_bytes(&txn).unwrap());
let request =
serde_json::json!({"jsonrpc": "2.0", "method": "submit", "params": [payload], "id": 1});
serde_json::to_vec(&request).expect("failed to convert JSON to byte array")
}
pub fn fuzzer(data: &[u8]) {
let json_request = match serde_json::from_slice::<serde_json::Value>(data) {
Err(_) => {
if cfg!(test) {
panic!();
}
return;
}
Ok(request) => request,
};
request_fuzzer(json_request)
}
pub fn request_fuzzer(json_request: serde_json::Value) {
let (mp_sender, mut mp_events) = channel(1);
let mut gen = ValueGenerator::new();
let account_state_with_proof = gen.generate(proptest::prelude::any::<AccountStateWithProof>());
let db = tests::utils::MockDiemDB {
version: 1,
genesis: std::collections::HashMap::new(),
all_accounts: std::collections::HashMap::new(),
all_txns: vec![],
events: vec![],
account_state_with_proof: vec![account_state_with_proof],
timestamps: vec![1598223353000000],
};
let service = methods::JsonRpcService::new(
Arc::new(db),
mp_sender,
config::RoleType::Validator,
diem_types::chain_id::ChainId::test(),
config::DEFAULT_BATCH_SIZE_LIMIT,
config::DEFAULT_PAGE_SIZE_LIMIT,
);
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
rt.spawn(async move {
if let Some((_, cb)) = mp_events.next().await {
cb.send(Ok((
diem_types::mempool_status::MempoolStatus::new(
diem_types::mempool_status::MempoolStatusCode::Accepted,
),
None,
)))
.unwrap();
}
});
let body = rt.block_on(async {
let reply = runtime::rpc_endpoint(json_request, service, None)
.await
.unwrap();
let resp = reply.into_response();
let (_, body) = resp.into_parts();
hyper::body::to_bytes(body).await.unwrap()
});
let response: serde_json::Value = serde_json::from_slice(body.as_ref()).expect("json");
match response {
serde_json::Value::Array(batch_response) => {
for resp in batch_response {
assert_response(resp)
}
}
_ => assert_response(response),
};
}
fn assert_response(response: serde_json::Value) {
let json_rpc_protocol = response.get("jsonrpc");
assert_eq!(
json_rpc_protocol,
Some(&serde_json::Value::String("2.0".to_string())),
"JSON RPC response with incorrect protocol: {:?}",
response
);
if !cfg!(test) {
return;
}
assert!(response.get("result").is_some(), "{}", response);
assert!(response.get("error").is_none(), "{}", response);
let response_id: u64 = serde_json::from_value(
response
.get("id")
.unwrap_or_else(|| panic!("failed to get ID from response: {}", response))
.clone(),
)
.unwrap_or_else(|_| panic!("Failed to deserialize ID from: {}", response));
assert_eq!(response_id, 1, "mismatch ID in JSON RPC: {}", response);
}