diff --git a/core/tests/client.rs b/core/tests/client.rs index d85a972da..9b6b63147 100644 --- a/core/tests/client.rs +++ b/core/tests/client.rs @@ -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); diff --git a/core/tests/rpc.rs b/core/tests/rpc.rs index 78470cb96..f379105be 100644 --- a/core/tests/rpc.rs +++ b/core/tests/rpc.rs @@ -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(); +}