Skip to content

Commit 92505b9

Browse files
committed
refactor: [torrust#1316] UDP tracker integration tests to pkg
1 parent 08efc3b commit 92505b9

File tree

16 files changed

+131
-55
lines changed

16 files changed

+131
-55
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ aquatic_udp_protocol = "0"
3838
axum-server = { version = "0", features = ["tls-rustls-no-provider"] }
3939
bittorrent-http-tracker-core = { version = "3.0.0-develop", path = "packages/http-tracker-core" }
4040
bittorrent-primitives = "0.1.0"
41-
bittorrent-tracker-client = { version = "3.0.0-develop", path = "packages/tracker-client" }
4241
bittorrent-tracker-core = { version = "3.0.0-develop", path = "packages/tracker-core" }
4342
bittorrent-udp-tracker-core = { version = "3.0.0-develop", path = "packages/udp-tracker-core" }
4443
chrono = { version = "0", default-features = false, features = ["clock"] }

packages/udp-tracker-server/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ zerocopy = "0.7"
3838
[dev-dependencies]
3939
local-ip-address = "0"
4040
mockall = "0"
41+
rand = "0"
4142
torrust-tracker-test-helpers = { version = "3.0.0-develop", path = "../test-helpers" }

tests/servers/udp/environment.rs packages/udp-tracker-server/src/environment.rs

+24-7
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ use bittorrent_udp_tracker_core::container::UdpTrackerCoreContainer;
77
use torrust_server_lib::registar::Registar;
88
use torrust_tracker_configuration::{logging, Configuration, DEFAULT_TIMEOUT};
99
use torrust_tracker_primitives::peer;
10-
use torrust_udp_tracker_server::server::spawner::Spawner;
11-
use torrust_udp_tracker_server::server::states::{Running, Stopped};
12-
use torrust_udp_tracker_server::server::Server;
10+
11+
use crate::server::spawner::Spawner;
12+
use crate::server::states::{Running, Stopped};
13+
use crate::server::Server;
14+
15+
pub type Started = Environment<Running>;
1316

1417
pub struct Environment<S>
1518
where
@@ -37,6 +40,7 @@ where
3740

3841
impl Environment<Stopped> {
3942
#[allow(dead_code)]
43+
#[must_use]
4044
pub fn new(configuration: &Arc<Configuration>) -> Self {
4145
initialize_global_services(configuration);
4246

@@ -53,6 +57,9 @@ impl Environment<Stopped> {
5357
}
5458
}
5559

60+
/// # Panics
61+
///
62+
/// Will panic if it cannot start the server.
5663
#[allow(dead_code)]
5764
pub async fn start(self) -> Environment<Running> {
5865
let cookie_lifetime = self.container.udp_tracker_core_container.udp_tracker_config.cookie_lifetime;
@@ -74,12 +81,18 @@ impl Environment<Stopped> {
7481
}
7582

7683
impl Environment<Running> {
84+
/// # Panics
85+
///
86+
/// Will panic if it cannot start the server within the timeout.
7787
pub async fn new(configuration: &Arc<Configuration>) -> Self {
7888
tokio::time::timeout(DEFAULT_TIMEOUT, Environment::<Stopped>::new(configuration).start())
7989
.await
8090
.expect("it should create an environment within the timeout")
8191
}
8292

93+
/// # Panics
94+
///
95+
/// Will panic if it cannot stop the service within the timeout.
8396
#[allow(dead_code)]
8497
pub async fn stop(self) -> Environment<Stopped> {
8598
let stopped = tokio::time::timeout(DEFAULT_TIMEOUT, self.server.stop())
@@ -89,10 +102,11 @@ impl Environment<Running> {
89102
Environment {
90103
container: self.container,
91104
registar: Registar::default(),
92-
server: stopped.expect("it stop the udp tracker service"),
105+
server: stopped.expect("it should stop the udp tracker service"),
93106
}
94107
}
95108

109+
#[must_use]
96110
pub fn bind_address(&self) -> SocketAddr {
97111
self.server.state.local_addr
98112
}
@@ -104,6 +118,10 @@ pub struct EnvContainer {
104118
}
105119

106120
impl EnvContainer {
121+
/// # Panics
122+
///
123+
/// Will panic if the configuration is missing the UDP tracker configuration.
124+
#[must_use]
107125
pub fn initialize(configuration: &Configuration) -> Self {
108126
let core_config = Arc::new(configuration.core.clone());
109127
let udp_tracker_configurations = configuration.udp_trackers.clone().expect("missing UDP tracker configuration");
@@ -134,10 +152,9 @@ mod tests {
134152
use std::time::Duration;
135153

136154
use tokio::time::sleep;
137-
use torrust_tracker_test_helpers::configuration;
155+
use torrust_tracker_test_helpers::{configuration, logging};
138156

139-
use crate::common::logging;
140-
use crate::servers::udp::Started;
157+
use crate::environment::Started;
141158

142159
#[tokio::test]
143160
async fn it_should_make_and_stop_udp_server() {

packages/udp-tracker-server/src/lib.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -634,15 +634,15 @@
634634
//! documentation by [Arvid Norberg](https://github.com/arvidn) was very
635635
//! supportive in the development of this documentation. Some descriptions were
636636
//! taken from the [libtorrent](https://www.rasterbar.com/products/libtorrent/udp_tracker_protocol.html).
637+
pub mod environment;
638+
pub mod error;
639+
pub mod handlers;
640+
pub mod server;
637641

638642
use std::net::SocketAddr;
639643

640644
use torrust_tracker_clock::clock;
641645

642-
pub mod error;
643-
pub mod handlers;
644-
pub mod server;
645-
646646
/// The maximum number of bytes in a UDP packet.
647647
pub const MAX_PACKET_SIZE: usize = 1496;
648648

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use aquatic_udp_protocol::TransactionId;
2+
use bittorrent_primitives::info_hash::InfoHash;
3+
use rand::prelude::*;
4+
5+
/// Returns a random info hash.
6+
pub fn random_info_hash() -> InfoHash {
7+
let mut rng = rand::thread_rng();
8+
let random_bytes: [u8; 20] = rng.gen();
9+
10+
InfoHash::from_bytes(&random_bytes)
11+
}
12+
13+
/// Returns a random transaction id.
14+
pub fn random_transaction_id() -> TransactionId {
15+
let random_value = rand::thread_rng().gen();
16+
TransactionId::new(random_value)
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod fixtures;
2+
pub mod udp;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use std::net::SocketAddr;
2+
use std::sync::Arc;
3+
4+
use tokio::net::UdpSocket;
5+
6+
/// A generic UDP client
7+
pub struct Client {
8+
pub socket: Arc<UdpSocket>,
9+
}
10+
11+
impl Client {
12+
#[allow(dead_code)]
13+
pub async fn connected(remote_socket_addr: &SocketAddr, local_socket_addr: &SocketAddr) -> Client {
14+
let client = Client::bind(local_socket_addr).await;
15+
client.connect(remote_socket_addr).await;
16+
client
17+
}
18+
19+
pub async fn bind(local_socket_addr: &SocketAddr) -> Self {
20+
let socket = UdpSocket::bind(local_socket_addr).await.unwrap();
21+
Self {
22+
socket: Arc::new(socket),
23+
}
24+
}
25+
26+
pub async fn connect(&self, remote_address: &SocketAddr) {
27+
self.socket.connect(remote_address).await.unwrap();
28+
}
29+
30+
#[allow(dead_code)]
31+
pub async fn send(&self, bytes: &[u8]) -> usize {
32+
self.socket.writable().await.unwrap();
33+
self.socket.send(bytes).await.unwrap()
34+
}
35+
36+
#[allow(dead_code)]
37+
pub async fn receive(&self, bytes: &mut [u8]) -> usize {
38+
self.socket.readable().await.unwrap();
39+
self.socket.recv(bytes).await.unwrap()
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//! Integration tests.
2+
//!
3+
//! ```text
4+
//! cargo test --test integration
5+
//! ```
6+
mod common;
7+
mod server;
8+
9+
use torrust_tracker_clock::clock;
10+
11+
/// This code needs to be copied into each crate.
12+
/// Working version, for production.
13+
#[cfg(not(test))]
14+
#[allow(dead_code)]
15+
pub(crate) type CurrentClock = clock::Working;
16+
17+
/// Stopped version, for testing.
18+
#[cfg(test)]
19+
#[allow(dead_code)]
20+
pub(crate) type CurrentClock = clock::Stopped;

tests/servers/udp/contract.rs packages/udp-tracker-server/tests/server/contract.rs

+17-24
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,10 @@ use core::panic;
88
use aquatic_udp_protocol::{ConnectRequest, ConnectionId, Response, TransactionId};
99
use bittorrent_tracker_client::udp::client::UdpTrackerClient;
1010
use torrust_tracker_configuration::DEFAULT_TIMEOUT;
11-
use torrust_tracker_test_helpers::configuration;
11+
use torrust_tracker_test_helpers::{configuration, logging};
1212
use torrust_udp_tracker_server::MAX_PACKET_SIZE;
1313

14-
use crate::common::logging;
15-
use crate::servers::udp::asserts::get_error_response_message;
16-
use crate::servers::udp::Started;
14+
use crate::server::asserts::get_error_response_message;
1715

1816
fn empty_udp_request() -> [u8; MAX_PACKET_SIZE] {
1917
[0; MAX_PACKET_SIZE]
@@ -42,7 +40,7 @@ async fn send_connection_request(transaction_id: TransactionId, client: &UdpTrac
4240
async fn should_return_a_bad_request_response_when_the_client_sends_an_empty_request() {
4341
logging::setup();
4442

45-
let env = Started::new(&configuration::ephemeral().into()).await;
43+
let env = torrust_udp_tracker_server::environment::Started::new(&configuration::ephemeral().into()).await;
4644

4745
let client = match UdpTrackerClient::new(env.bind_address(), DEFAULT_TIMEOUT).await {
4846
Ok(udp_client) => udp_client,
@@ -70,17 +68,15 @@ mod receiving_a_connection_request {
7068
use aquatic_udp_protocol::{ConnectRequest, TransactionId};
7169
use bittorrent_tracker_client::udp::client::UdpTrackerClient;
7270
use torrust_tracker_configuration::DEFAULT_TIMEOUT;
73-
use torrust_tracker_test_helpers::configuration;
71+
use torrust_tracker_test_helpers::{configuration, logging};
7472

75-
use crate::common::logging;
76-
use crate::servers::udp::asserts::is_connect_response;
77-
use crate::servers::udp::Started;
73+
use crate::server::asserts::is_connect_response;
7874

7975
#[tokio::test]
8076
async fn should_return_a_connect_response() {
8177
logging::setup();
8278

83-
let env = Started::new(&configuration::ephemeral().into()).await;
79+
let env = torrust_udp_tracker_server::environment::Started::new(&configuration::ephemeral().into()).await;
8480

8581
let client = match UdpTrackerClient::new(env.bind_address(), DEFAULT_TIMEOUT).await {
8682
Ok(udp_tracker_client) => udp_tracker_client,
@@ -116,13 +112,12 @@ mod receiving_an_announce_request {
116112
};
117113
use bittorrent_tracker_client::udp::client::UdpTrackerClient;
118114
use torrust_tracker_configuration::DEFAULT_TIMEOUT;
119-
use torrust_tracker_test_helpers::configuration;
115+
use torrust_tracker_test_helpers::logging::logs_contains_a_line_with;
116+
use torrust_tracker_test_helpers::{configuration, logging};
120117

121118
use crate::common::fixtures::{random_info_hash, random_transaction_id};
122-
use crate::common::logging::{self, logs_contains_a_line_with};
123-
use crate::servers::udp::asserts::is_ipv4_announce_response;
124-
use crate::servers::udp::contract::send_connection_request;
125-
use crate::servers::udp::Started;
119+
use crate::server::asserts::is_ipv4_announce_response;
120+
use crate::server::contract::send_connection_request;
126121

127122
pub async fn assert_send_and_get_announce(
128123
tx_id: TransactionId,
@@ -181,7 +176,7 @@ mod receiving_an_announce_request {
181176
async fn should_return_an_announce_response() {
182177
logging::setup();
183178

184-
let env = Started::new(&configuration::ephemeral().into()).await;
179+
let env = torrust_udp_tracker_server::environment::Started::new(&configuration::ephemeral().into()).await;
185180

186181
let client = match UdpTrackerClient::new(env.bind_address(), DEFAULT_TIMEOUT).await {
187182
Ok(udp_tracker_client) => udp_tracker_client,
@@ -203,7 +198,7 @@ mod receiving_an_announce_request {
203198
async fn should_return_many_announce_response() {
204199
logging::setup();
205200

206-
let env = Started::new(&configuration::ephemeral().into()).await;
201+
let env = torrust_udp_tracker_server::environment::Started::new(&configuration::ephemeral().into()).await;
207202

208203
let client = match UdpTrackerClient::new(env.bind_address(), DEFAULT_TIMEOUT).await {
209204
Ok(udp_tracker_client) => udp_tracker_client,
@@ -228,7 +223,7 @@ mod receiving_an_announce_request {
228223
async fn should_ban_the_client_ip_if_it_sends_more_than_10_requests_with_a_cookie_value_not_normal() {
229224
logging::setup();
230225

231-
let env = Started::new(&configuration::ephemeral().into()).await;
226+
let env = torrust_udp_tracker_server::environment::Started::new(&configuration::ephemeral().into()).await;
232227
let ban_service = env.container.udp_tracker_core_container.ban_service.clone();
233228

234229
let client = match UdpTrackerClient::new(env.bind_address(), DEFAULT_TIMEOUT).await {
@@ -309,18 +304,16 @@ mod receiving_an_scrape_request {
309304
use aquatic_udp_protocol::{ConnectionId, InfoHash, ScrapeRequest, TransactionId};
310305
use bittorrent_tracker_client::udp::client::UdpTrackerClient;
311306
use torrust_tracker_configuration::DEFAULT_TIMEOUT;
312-
use torrust_tracker_test_helpers::configuration;
307+
use torrust_tracker_test_helpers::{configuration, logging};
313308

314-
use crate::common::logging;
315-
use crate::servers::udp::asserts::is_scrape_response;
316-
use crate::servers::udp::contract::send_connection_request;
317-
use crate::servers::udp::Started;
309+
use crate::server::asserts::is_scrape_response;
310+
use crate::server::contract::send_connection_request;
318311

319312
#[tokio::test]
320313
async fn should_return_a_scrape_response() {
321314
logging::setup();
322315

323-
let env = Started::new(&configuration::ephemeral().into()).await;
316+
let env = torrust_udp_tracker_server::environment::Started::new(&configuration::ephemeral().into()).await;
324317

325318
let client = match UdpTrackerClient::new(env.bind_address(), DEFAULT_TIMEOUT).await {
326319
Ok(udp_tracker_client) => udp_tracker_client,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod asserts;
2+
pub mod contract;

tests/common/fixtures.rs

-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use aquatic_udp_protocol::TransactionId;
21
use bittorrent_primitives::info_hash::InfoHash;
32

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

2221
InfoHash::from_bytes(&random_bytes)
2322
}
24-
25-
/// Returns a random transaction id.
26-
pub fn random_transaction_id() -> TransactionId {
27-
let random_value = rand::Rng::random::<i32>(&mut rand::rng());
28-
TransactionId::new(random_value)
29-
}

tests/servers/health_check_api/contract.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,14 @@ mod udp {
225225

226226
use crate::common::logging;
227227
use crate::servers::health_check_api::client::get;
228-
use crate::servers::udp;
229228

230229
#[tokio::test]
231230
pub(crate) async fn it_should_return_good_health_for_udp_service() {
232231
logging::setup();
233232

234233
let configuration = Arc::new(configuration::ephemeral());
235234

236-
let service = udp::Started::new(&configuration).await;
235+
let service = torrust_udp_tracker_server::environment::Started::new(&configuration).await;
237236

238237
let registar = service.registar.clone();
239238

@@ -276,7 +275,7 @@ mod udp {
276275

277276
let configuration = Arc::new(configuration::ephemeral());
278277

279-
let service = udp::Started::new(&configuration).await;
278+
let service = torrust_udp_tracker_server::environment::Started::new(&configuration).await;
280279

281280
let binding = service.bind_address();
282281

tests/servers/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
mod health_check_api;
22
mod http;
3-
mod udp;

tests/servers/udp/mod.rs

-7
This file was deleted.

0 commit comments

Comments
 (0)