Skip to content

Commit

Permalink
test: [#1164] assert logged error when connection ID is wrong
Browse files Browse the repository at this point in the history
in UDP tracker. Only for the tests that is currently showing logging
errors.
  • Loading branch information
josecelano committed Dec 26, 2024
1 parent 5f206f0 commit 71e7ef7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
7 changes: 7 additions & 0 deletions tests/common/fixtures.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use aquatic_udp_protocol::TransactionId;
use bittorrent_primitives::info_hash::InfoHash;

#[allow(dead_code)]
Expand All @@ -20,3 +21,9 @@ pub fn random_info_hash() -> InfoHash {

InfoHash::from_bytes(&random_bytes)
}

/// Returns a random transaction id.
pub fn random_transaction_id() -> TransactionId {
let random_value = rand::Rng::gen::<i32>(&mut rand::thread_rng());
TransactionId::new(random_value)
}
19 changes: 15 additions & 4 deletions tests/servers/udp/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ mod receiving_an_announce_request {
use torrust_tracker_configuration::DEFAULT_TIMEOUT;
use torrust_tracker_test_helpers::configuration;

use crate::common::fixtures::random_info_hash;
use crate::common::logging;
use crate::common::fixtures::{random_info_hash, random_transaction_id};
use crate::common::logging::{self, logs_contains_a_line_with};
use crate::servers::udp::asserts::is_ipv4_announce_response;
use crate::servers::udp::contract::send_connection_request;
use crate::servers::udp::Started;
Expand Down Expand Up @@ -235,8 +235,6 @@ mod receiving_an_announce_request {
Err(err) => panic!("{err}"),
};

let tx_id = TransactionId::new(123);

// The eleven first requests should be fine

let invalid_connection_id = ConnectionId::new(0); // Zero is one of the not normal values.
Expand All @@ -245,18 +243,31 @@ mod receiving_an_announce_request {

for x in 0..=10 {
tracing::info!("req no: {x}");

let tx_id = random_transaction_id();

send_and_get_announce(tx_id, invalid_connection_id, info_hash, &client).await;

let transaction_id = tx_id.0.to_string();

assert!(
logs_contains_a_line_with(&["ERROR", "UDP TRACKER", &transaction_id.to_string()]),
"Expected logs to contain: ERROR ... UDP TRACKER ... transaction_id={transaction_id}"
);
}

// The twelfth request should be banned (timeout error)

let tx_id = random_transaction_id();

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

// This should return a timeout error
match client.send(announce_request.into()).await {
Ok(_) => (),
Err(err) => panic!("{err}"),
Expand Down

0 comments on commit 71e7ef7

Please sign in to comment.