Skip to content

Commit

Permalink
test: [#1096] add E2E test for banning IP sending bad connection IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed Dec 9, 2024
1 parent 2898a44 commit b94359f
Showing 1 changed file with 61 additions and 19 deletions.
80 changes: 61 additions & 19 deletions tests/servers/udp/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,31 @@ mod receiving_an_announce_request {
use crate::servers::udp::contract::send_connection_request;
use crate::servers::udp::Started;

pub async fn send_and_get_announce(tx_id: TransactionId, c_id: ConnectionId, client: &UdpTrackerClient) {
// Send announce request
pub async fn assert_send_and_get_announce(tx_id: TransactionId, c_id: ConnectionId, client: &UdpTrackerClient) {
let response = send_and_get_announce(tx_id, c_id, client).await;
assert!(is_ipv4_announce_response(&response));
}

pub async fn send_and_get_announce(
tx_id: TransactionId,
c_id: ConnectionId,
client: &UdpTrackerClient,
) -> aquatic_udp_protocol::Response {
let announce_request = build_sample_announce_request(tx_id, c_id, client.client.socket.local_addr().unwrap().port());

match client.send(announce_request.into()).await {
Ok(_) => (),
Err(err) => panic!("{err}"),
};

match client.receive().await {
Ok(response) => response,
Err(err) => panic!("{err}"),
}
}

let announce_request = AnnounceRequest {
fn build_sample_announce_request(tx_id: TransactionId, c_id: ConnectionId, port: u16) -> AnnounceRequest {
AnnounceRequest {
connection_id: ConnectionId(c_id.0),
action_placeholder: AnnounceActionPlaceholder::default(),
transaction_id: tx_id,
Expand All @@ -146,26 +167,34 @@ mod receiving_an_announce_request {
ip_address: Ipv4Addr::new(0, 0, 0, 0).into(),
key: PeerKey::new(0i32),
peers_wanted: NumberOfPeers(1i32.into()),
port: Port(client.client.socket.local_addr().unwrap().port().into()),
};
port: Port(port.into()),
}
}

match client.send(announce_request.into()).await {
Ok(_) => (),
Err(err) => panic!("{err}"),
};
#[tokio::test]
async fn should_return_an_announce_response() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});

let response = match client.receive().await {
Ok(response) => response,
let env = Started::new(&configuration::ephemeral().into()).await;

let client = match UdpTrackerClient::new(env.bind_address(), DEFAULT_TIMEOUT).await {
Ok(udp_tracker_client) => udp_tracker_client,
Err(err) => panic!("{err}"),
};

// println!("test response {response:?}");
let tx_id = TransactionId::new(123);

assert!(is_ipv4_announce_response(&response));
let c_id = send_connection_request(tx_id, &client).await;

assert_send_and_get_announce(tx_id, c_id, &client).await;

env.stop().await;
}

#[tokio::test]
async fn should_return_an_announce_response() {
async fn should_return_many_announce_response() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});
Expand All @@ -181,13 +210,16 @@ mod receiving_an_announce_request {

let c_id = send_connection_request(tx_id, &client).await;

send_and_get_announce(tx_id, c_id, &client).await;
for x in 0..1000 {
tracing::info!("req no: {x}");
assert_send_and_get_announce(tx_id, c_id, &client).await;
}

env.stop().await;
}

#[tokio::test]
async fn should_return_many_announce_response() {
async fn should_ban_the_client_ip_if_it_sends_more_than_10_requests_with_a_cookie_value_not_normal() {
INIT.call_once(|| {
tracing_stderr_init(LevelFilter::ERROR);
});
Expand All @@ -201,13 +233,23 @@ mod receiving_an_announce_request {

let tx_id = TransactionId::new(123);

let c_id = send_connection_request(tx_id, &client).await;
let invalid_connection_id = ConnectionId::new(0); // Zero is one of the not normal values.

for x in 0..1000 {
// The ten first requests should be fine
for x in 0..=10 {
tracing::info!("req no: {x}");
send_and_get_announce(tx_id, c_id, &client).await;
assert_send_and_get_announce(tx_id, invalid_connection_id, &client).await;
}

let announce_request = build_sample_announce_request(
tx_id,
invalid_connection_id,
client.client.socket.local_addr().unwrap().port(),
);

// The eleventh request should be banned
assert!(client.send(announce_request.into()).await.is_err());

env.stop().await;
}
}
Expand Down

0 comments on commit b94359f

Please sign in to comment.