Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add server tests #4488

Merged
merged 2 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions massa-api/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
mod apiv2;
mod mock;
mod public;
mod server;
219 changes: 219 additions & 0 deletions massa-api/src/tests/server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
use std::{net::SocketAddr, str::FromStr};

use jsonrpsee::{
client_transport::ws::Url, core::client::ClientT, http_client::HttpClientBuilder, rpc_params,
ws_client::WsClientBuilder,
};
use massa_api_exports::operation::OperationInfo;
use massa_models::operation::OperationId;

use crate::{tests::mock::get_apiv2_server, ApiServer, RpcServer};

#[tokio::test]
async fn max_conn() {
let addr: SocketAddr = "[::]:5037".parse().unwrap();
let (mut api_server, mut api_config) = get_apiv2_server(&addr);

api_server.0.api_settings.max_connections = 10;
api_config.max_connections = 10;

let uri = Url::parse(&format!(
"ws://localhost:{}",
addr.to_string().split(':').into_iter().last().unwrap()
))
.unwrap();

let api_handle = api_server
.serve(&addr, &api_config)
.await
.expect("failed to start MASSA API V2");

let mut clients = vec![];
for _ in 0..15 {
let client1 = WsClientBuilder::default().build(&uri).await;
if clients.len() < 10 {
assert!(client1.is_ok());
clients.push(client1.unwrap());
} else {
assert!(client1.is_err());
}
}

api_handle.stop().await;
}

#[tokio::test]
async fn max_request_size() {
let addr: SocketAddr = "[::]:5038".parse().unwrap();
let (mut api_server, mut api_config) = crate::tests::mock::start_public_api(addr.clone());

api_config.max_request_body_size = 10;
api_server.0.api_settings.max_request_body_size = 10;

let api_handle = api_server
.serve(&addr, &api_config)
.await
.expect("failed to start MASSA API V2");

let client = HttpClientBuilder::default()
.build(format!(
"http://localhost:{}",
addr.to_string().split(':').into_iter().last().unwrap()
))
.unwrap();
let params = rpc_params![vec![
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
OperationId::from_str("O1q4CBcuYo8YANEV34W4JRWVHrzcYns19VJfyAB7jT4qfitAnMC").unwrap(),
]];
let response: Result<Vec<OperationInfo>, jsonrpsee::core::Error> =
client.request("get_operations", params).await;

assert!(response
.unwrap_err()
.to_string()
.contains("status code: 413"));

api_handle.stop().await;
}

#[tokio::test]
async fn ws_disabled() {
let addr: SocketAddr = "[::]:5039".parse().unwrap();
let (mut api_server, mut api_config) = get_apiv2_server(&addr);

api_server.0.api_settings.enable_ws = false;
api_config.enable_ws = false;

let uri = Url::parse(&format!(
"ws://localhost:{}",
addr.to_string().split(':').into_iter().last().unwrap()
))
.unwrap();

let api_handle = api_server
.serve(&addr, &api_config)
.await
.expect("failed to start MASSA API V2");
let response = WsClientBuilder::default().build(&uri).await;

assert!(response
.unwrap_err()
.to_string()
.contains("status code: 403"));

api_handle.stop().await;
}

#[tokio::test]
async fn http_disabled() {
let addr: SocketAddr = "[::]:5040".parse().unwrap();
let (mut api_server, mut api_config) = crate::tests::mock::start_public_api(addr.clone());

api_server.0.api_settings.enable_http = false;
api_config.enable_http = false;

let api_handle = api_server
.serve(&addr, &api_config)
.await
.expect("failed to start MASSA API V2");

let client = HttpClientBuilder::default()
.build(format!(
"http://localhost:{}",
addr.to_string().split(':').into_iter().last().unwrap()
))
.unwrap();

let response: Result<Vec<OperationInfo>, jsonrpsee::core::Error> =
client.request("get_operations", rpc_params![]).await;

assert!(response
.unwrap_err()
.to_string()
.contains("status code: 403"));

api_handle.stop().await;
}

#[tokio::test]
async fn host_allowed() {
let addr: SocketAddr = "[::]:5041".parse().unwrap();
let (mut api_server, mut api_config) = crate::tests::mock::start_public_api(addr.clone());

let hosts = vec![format!(
"http://localhost:{}",
addr.to_string().split(':').into_iter().last().unwrap()
)];

api_server.0.api_settings.allow_hosts = hosts.clone();
api_config.allow_hosts = hosts;

let api_handle = api_server
.serve(&addr, &api_config)
.await
.expect("failed to start MASSA API V2");

let client = HttpClientBuilder::default()
.build(format!(
"http://localhost:{}",
addr.to_string().split(':').into_iter().last().unwrap()
))
.unwrap();

let response: Result<Vec<OperationInfo>, jsonrpsee::core::Error> =
client.request("get_operations", rpc_params![]).await;

// response OK but invalid params (no params provided)
assert!(response.unwrap_err().to_string().contains("Invalid params"));

// now start new server with different host allowed
let addr2: SocketAddr = "[::]:5042".parse().unwrap();
let (mut api_server2, mut api_config2) = crate::tests::mock::start_public_api(addr2.clone());

let hosts2 = vec!["http://123.456.789.1".to_string()];

api_server2.0.api_settings.allow_hosts = hosts2.clone();
api_config2.allow_hosts = hosts2;

let api_handle2 = api_server2
.serve(&addr2, &api_config2)
.await
.expect("failed to start MASSA API V2");

let client = HttpClientBuilder::default()
.build(format!(
"http://localhost:{}",
addr2.to_string().split(':').into_iter().last().unwrap()
))
.unwrap();

let response: Result<Vec<OperationInfo>, jsonrpsee::core::Error> =
client.request("get_operations", rpc_params![]).await;

// host not allowed
assert!(response
.unwrap_err()
.to_string()
.contains("status code: 403"));

api_handle.stop().await;
api_handle2.stop().await;
}
Loading