feat: add tests for invalid/failure cases (#6951)

This commit is contained in:
Sunny Gleason 2019-11-14 11:41:26 -05:00 committed by GitHub
parent dbbd9663b2
commit 42af8b199f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 0 deletions

View File

@ -22,6 +22,8 @@ fn test_rpc_client() {
solana_clap_utils::version!()
);
assert!(client.get_account(&bob_pubkey).is_err());
assert_eq!(client.get_balance(&bob_pubkey).unwrap(), 0);
assert_eq!(client.get_balance(&alice.pubkey()).unwrap(), 1_000_000);

View File

@ -95,3 +95,74 @@ fn test_rpc_send_tx() {
server.close().unwrap();
remove_dir_all(ledger_path).unwrap();
}
#[test]
fn test_rpc_invalid_requests() {
solana_logger::setup();
let (server, leader_data, _alice, ledger_path) = new_validator_for_tests();
let bob_pubkey = Pubkey::new_rand();
// test invalid get_balance request
let client = reqwest::Client::new();
let request = json!({
"jsonrpc": "2.0",
"id": 1,
"method": "getBalance",
"params": json!(["invalid9999"])
});
let rpc_addr = leader_data.rpc;
let rpc_string = get_rpc_request_str(rpc_addr, false);
let mut response = client
.post(&rpc_string)
.header(CONTENT_TYPE, "application/json")
.body(request.to_string())
.send()
.unwrap();
let json: Value = serde_json::from_str(&response.text().unwrap()).unwrap();
let the_error = json["error"]["message"].as_str().unwrap();
assert_eq!(the_error, "Invalid request");
// test invalid get_account_info request
let client = reqwest::Client::new();
let request = json!({
"jsonrpc": "2.0",
"id": 1,
"method": "getAccountInfo",
"params": json!(["invalid9999"])
});
let rpc_addr = leader_data.rpc;
let rpc_string = get_rpc_request_str(rpc_addr, false);
let mut response = client
.post(&rpc_string)
.header(CONTENT_TYPE, "application/json")
.body(request.to_string())
.send()
.unwrap();
let json: Value = serde_json::from_str(&response.text().unwrap()).unwrap();
let the_error = json["error"]["message"].as_str().unwrap();
assert_eq!(the_error, "Invalid request");
// test invalid get_account_info request
let client = reqwest::Client::new();
let request = json!({
"jsonrpc": "2.0",
"id": 1,
"method": "getAccountInfo",
"params": json!([bob_pubkey.to_string()])
});
let rpc_addr = leader_data.rpc;
let rpc_string = get_rpc_request_str(rpc_addr, false);
let mut response = client
.post(&rpc_string)
.header(CONTENT_TYPE, "application/json")
.body(request.to_string())
.send()
.unwrap();
let json: Value = serde_json::from_str(&response.text().unwrap()).unwrap();
let the_value = &json["result"]["value"];
assert!(the_value.is_null());
server.close().unwrap();
remove_dir_all(ledger_path).unwrap();
}