diff --git a/packages/tracker-core/src/announce_handler.rs b/packages/tracker-core/src/announce_handler.rs index 85dd354b..cd0a9b86 100644 --- a/packages/tracker-core/src/announce_handler.rs +++ b/packages/tracker-core/src/announce_handler.rs @@ -182,8 +182,8 @@ mod tests { use torrust_tracker_test_helpers::configuration; use crate::announce_handler::AnnounceHandler; - use crate::core_tests::initialize_handlers; use crate::scrape_handler::ScrapeHandler; + use crate::test_helpers::tests::initialize_handlers; fn public_tracker() -> (Arc, Arc) { let config = configuration::ephemeral_public(); @@ -244,7 +244,7 @@ mod tests { peer_ip, public_tracker, sample_peer_1, sample_peer_2, sample_peer_3, }; use crate::announce_handler::PeersWanted; - use crate::core_tests::{sample_info_hash, sample_peer}; + use crate::test_helpers::tests::{sample_info_hash, sample_peer}; mod should_assign_the_ip_to_the_peer { @@ -411,7 +411,7 @@ mod tests { use crate::announce_handler::tests::the_announce_handler::{peer_ip, public_tracker}; use crate::announce_handler::PeersWanted; - use crate::core_tests::{completed_peer, leecher, sample_info_hash, seeder, started_peer}; + use crate::test_helpers::tests::{completed_peer, leecher, sample_info_hash, seeder, started_peer}; #[tokio::test] async fn when_the_peer_is_a_seeder() { @@ -474,8 +474,8 @@ mod tests { use crate::announce_handler::tests::the_announce_handler::peer_ip; use crate::announce_handler::{AnnounceHandler, PeersWanted}; - use crate::core_tests::{sample_info_hash, sample_peer}; use crate::databases::setup::initialize_database; + use crate::test_helpers::tests::{sample_info_hash, sample_peer}; use crate::torrent::manager::TorrentsManager; use crate::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository; diff --git a/packages/tracker-core/src/authentication/handler.rs b/packages/tracker-core/src/authentication/handler.rs index f758830a..13606091 100644 --- a/packages/tracker-core/src/authentication/handler.rs +++ b/packages/tracker-core/src/authentication/handler.rs @@ -132,7 +132,7 @@ impl KeysHandler { /// # Errors /// /// Will return a `database::Error` if unable to add the `auth_key` to the database. - pub async fn generate_permanent_peer_key(&self) -> Result { + pub(crate) async fn generate_permanent_peer_key(&self) -> Result { self.generate_expiring_peer_key(None).await } @@ -170,7 +170,7 @@ impl KeysHandler { /// # Arguments /// /// * `key` - The pre-generated key. - pub async fn add_permanent_peer_key(&self, key: Key) -> Result { + pub(crate) async fn add_permanent_peer_key(&self, key: Key) -> Result { self.add_expiring_peer_key(key, None).await } @@ -188,7 +188,7 @@ impl KeysHandler { /// * `key` - The pre-generated key. /// * `lifetime` - The duration in seconds for the new key. The key will be /// no longer valid after `lifetime` seconds. - pub async fn add_expiring_peer_key( + pub(crate) async fn add_expiring_peer_key( &self, key: Key, valid_until: Option, @@ -219,7 +219,7 @@ impl KeysHandler { } /// It removes an authentication key from memory. - pub async fn remove_in_memory_auth_key(&self, key: &Key) { + pub(crate) async fn remove_in_memory_auth_key(&self, key: &Key) { self.in_memory_key_repository.remove(key).await; } diff --git a/packages/tracker-core/src/authentication/key/mod.rs b/packages/tracker-core/src/authentication/key/mod.rs index 8ec368eb..fce18c0d 100644 --- a/packages/tracker-core/src/authentication/key/mod.rs +++ b/packages/tracker-core/src/authentication/key/mod.rs @@ -59,17 +59,19 @@ pub type ParseKeyError = peer_key::ParseKeyError; /// /// For more information see function [`generate_key`](crate::authentication::key::generate_key) to generate the /// [`PeerKey`](crate::authentication::PeerKey). -pub const AUTH_KEY_LENGTH: usize = 32; +pub(crate) const AUTH_KEY_LENGTH: usize = 32; /// It generates a new permanent random key [`PeerKey`]. +#[cfg(test)] #[must_use] -pub fn generate_permanent_key() -> PeerKey { +pub(crate) fn generate_permanent_key() -> PeerKey { generate_key(None) } /// It generates a new expiring random key [`PeerKey`]. +#[cfg(test)] #[must_use] -pub fn generate_expiring_key(lifetime: Duration) -> PeerKey { +pub(crate) fn generate_expiring_key(lifetime: Duration) -> PeerKey { generate_key(Some(lifetime)) } diff --git a/packages/tracker-core/src/authentication/key/repository/in_memory.rs b/packages/tracker-core/src/authentication/key/repository/in_memory.rs index 0a2fc50c..13664e27 100644 --- a/packages/tracker-core/src/authentication/key/repository/in_memory.rs +++ b/packages/tracker-core/src/authentication/key/repository/in_memory.rs @@ -9,21 +9,22 @@ pub struct InMemoryKeyRepository { impl InMemoryKeyRepository { /// It adds a new authentication key. - pub async fn insert(&self, auth_key: &PeerKey) { + pub(crate) async fn insert(&self, auth_key: &PeerKey) { self.keys.write().await.insert(auth_key.key.clone(), auth_key.clone()); } /// It removes an authentication key. - pub async fn remove(&self, key: &Key) { + pub(crate) async fn remove(&self, key: &Key) { self.keys.write().await.remove(key); } - pub async fn get(&self, key: &Key) -> Option { + pub(crate) async fn get(&self, key: &Key) -> Option { self.keys.read().await.get(key).cloned() } /// It clears all the authentication keys. - pub async fn clear(&self) { + #[allow(dead_code)] + pub(crate) async fn clear(&self) { let mut keys = self.keys.write().await; keys.clear(); } diff --git a/packages/tracker-core/src/authentication/key/repository/persisted.rs b/packages/tracker-core/src/authentication/key/repository/persisted.rs index 7edee62c..95a3b874 100644 --- a/packages/tracker-core/src/authentication/key/repository/persisted.rs +++ b/packages/tracker-core/src/authentication/key/repository/persisted.rs @@ -21,7 +21,7 @@ impl DatabaseKeyRepository { /// # Errors /// /// Will return a `databases::error::Error` if unable to add the `auth_key` to the database. - pub fn add(&self, peer_key: &PeerKey) -> Result<(), databases::error::Error> { + pub(crate) fn add(&self, peer_key: &PeerKey) -> Result<(), databases::error::Error> { self.database.add_key_to_keys(peer_key)?; Ok(()) } @@ -31,7 +31,7 @@ impl DatabaseKeyRepository { /// # Errors /// /// Will return a `database::Error` if unable to remove the `key` from the database. - pub fn remove(&self, key: &Key) -> Result<(), databases::error::Error> { + pub(crate) fn remove(&self, key: &Key) -> Result<(), databases::error::Error> { self.database.remove_key_from_keys(key)?; Ok(()) } @@ -41,7 +41,7 @@ impl DatabaseKeyRepository { /// # Errors /// /// Will return a `database::Error` if unable to load the keys from the database. - pub fn load_keys(&self) -> Result, databases::error::Error> { + pub(crate) fn load_keys(&self) -> Result, databases::error::Error> { let keys = self.database.load_keys()?; Ok(keys) } diff --git a/packages/tracker-core/src/core_tests.rs b/packages/tracker-core/src/core_tests.rs deleted file mode 100644 index 165c8790..00000000 --- a/packages/tracker-core/src/core_tests.rs +++ /dev/null @@ -1,215 +0,0 @@ -//! Some generic test helpers functions. -use std::net::{IpAddr, Ipv4Addr, SocketAddr}; -use std::sync::Arc; - -use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId}; -use bittorrent_primitives::info_hash::InfoHash; -use rand::Rng; -use torrust_tracker_configuration::Configuration; -#[cfg(test)] -use torrust_tracker_configuration::Core; -use torrust_tracker_primitives::peer::Peer; -use torrust_tracker_primitives::DurationSinceUnixEpoch; -#[cfg(test)] -use torrust_tracker_test_helpers::configuration::ephemeral_sqlite_database; - -use super::announce_handler::AnnounceHandler; -use super::databases::setup::initialize_database; -use super::scrape_handler::ScrapeHandler; -use super::torrent::repository::in_memory::InMemoryTorrentRepository; -use super::torrent::repository::persisted::DatabasePersistentTorrentRepository; -use super::whitelist::repository::in_memory::InMemoryWhitelist; -use super::whitelist::{self}; - -/// Generates a random `InfoHash`. -#[must_use] -pub fn random_info_hash() -> InfoHash { - let mut rng = rand::rng(); - let mut random_bytes = [0u8; 20]; - rng.fill(&mut random_bytes); - - InfoHash::from_bytes(&random_bytes) -} - -/// # Panics -/// -/// Will panic if the string representation of the info hash is not a valid info hash. -#[must_use] -pub fn sample_info_hash() -> InfoHash { - "3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0" // DevSkim: ignore DS173237 - .parse::() - .expect("String should be a valid info hash") -} - -/// # Panics -/// -/// Will panic if the string representation of the info hash is not a valid info hash. -#[must_use] -pub fn sample_info_hash_one() -> InfoHash { - "3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0" // DevSkim: ignore DS173237 - .parse::() - .expect("String should be a valid info hash") -} - -/// # Panics -/// -/// Will panic if the string representation of the info hash is not a valid info hash. -#[must_use] -pub fn sample_info_hash_two() -> InfoHash { - "99c82bb73505a3c0b453f9fa0e881d6e5a32a0c1" // DevSkim: ignore DS173237 - .parse::() - .expect("String should be a valid info hash") -} - -/// # Panics -/// -/// Will panic if the string representation of the info hash is not a valid info hash. -#[must_use] -pub fn sample_info_hash_alphabetically_ordered_after_sample_info_hash_one() -> InfoHash { - "99c82bb73505a3c0b453f9fa0e881d6e5a32a0c1" // DevSkim: ignore DS173237 - .parse::() - .expect("String should be a valid info hash") -} - -/// Sample peer whose state is not relevant for the tests. -#[must_use] -pub fn sample_peer() -> Peer { - Peer { - peer_id: PeerId(*b"-qB00000000000000000"), - peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080), - updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0), - uploaded: NumberOfBytes::new(0), - downloaded: NumberOfBytes::new(0), - left: NumberOfBytes::new(0), // No bytes left to download - event: AnnounceEvent::Completed, - } -} - -#[must_use] -pub fn sample_peer_one() -> Peer { - Peer { - peer_id: PeerId(*b"-qB00000000000000001"), - peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8081), - updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0), - uploaded: NumberOfBytes::new(0), - downloaded: NumberOfBytes::new(0), - left: NumberOfBytes::new(0), // No bytes left to download - event: AnnounceEvent::Completed, - } -} - -#[must_use] -pub fn sample_peer_two() -> Peer { - Peer { - peer_id: PeerId(*b"-qB00000000000000002"), - peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 2)), 8082), - updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0), - uploaded: NumberOfBytes::new(0), - downloaded: NumberOfBytes::new(0), - left: NumberOfBytes::new(0), // No bytes left to download - event: AnnounceEvent::Completed, - } -} - -#[must_use] -pub fn seeder() -> Peer { - complete_peer() -} - -#[must_use] -pub fn leecher() -> Peer { - incomplete_peer() -} - -#[must_use] -pub fn started_peer() -> Peer { - incomplete_peer() -} - -#[must_use] -pub fn completed_peer() -> Peer { - complete_peer() -} - -/// A peer that counts as `complete` is swarm metadata -/// IMPORTANT!: it only counts if the it has been announce at least once before -/// announcing the `AnnounceEvent::Completed` event. -#[must_use] -pub fn complete_peer() -> Peer { - Peer { - peer_id: PeerId(*b"-qB00000000000000000"), - peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080), - updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0), - uploaded: NumberOfBytes::new(0), - downloaded: NumberOfBytes::new(0), - left: NumberOfBytes::new(0), // No bytes left to download - event: AnnounceEvent::Completed, - } -} - -/// A peer that counts as `incomplete` is swarm metadata -#[must_use] -pub fn incomplete_peer() -> Peer { - Peer { - peer_id: PeerId(*b"-qB00000000000000000"), - peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080), - updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0), - uploaded: NumberOfBytes::new(0), - downloaded: NumberOfBytes::new(0), - left: NumberOfBytes::new(1000), // Still bytes to download - event: AnnounceEvent::Started, - } -} - -#[must_use] -pub fn initialize_handlers(config: &Configuration) -> (Arc, Arc) { - let database = initialize_database(&config.core); - let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); - let whitelist_authorization = Arc::new(whitelist::authorization::WhitelistAuthorization::new( - &config.core, - &in_memory_whitelist.clone(), - )); - let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); - let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); - - let announce_handler = Arc::new(AnnounceHandler::new( - &config.core, - &in_memory_torrent_repository, - &db_torrent_repository, - )); - - let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); - - (announce_handler, scrape_handler) -} - -/// # Panics -/// -/// Will panic if the temporary database file path is not a valid UFT string. -#[cfg(test)] -#[must_use] -pub fn ephemeral_configuration() -> Core { - let mut config = Core::default(); - - let temp_file = ephemeral_sqlite_database(); - temp_file.to_str().unwrap().clone_into(&mut config.database.path); - - config -} - -/// # Panics -/// -/// Will panic if the temporary database file path is not a valid UFT string. -#[cfg(test)] -#[must_use] -pub fn ephemeral_configuration_for_listed_tracker() -> Core { - let mut config = Core { - listed: true, - ..Default::default() - }; - - let temp_file = ephemeral_sqlite_database(); - temp_file.to_str().unwrap().clone_into(&mut config.database.path); - - config -} diff --git a/packages/tracker-core/src/databases/driver/mod.rs b/packages/tracker-core/src/databases/driver/mod.rs index 1e42e441..2bc6a1e3 100644 --- a/packages/tracker-core/src/databases/driver/mod.rs +++ b/packages/tracker-core/src/databases/driver/mod.rs @@ -73,7 +73,7 @@ pub mod sqlite; /// # Errors /// /// Will return `Error` if unable to build the driver. -pub fn build(driver: &Driver, db_path: &str) -> Result, Error> { +pub(crate) fn build(driver: &Driver, db_path: &str) -> Result, Error> { let database: Box = match driver { Driver::Sqlite3 => Box::new(Sqlite::new(db_path)?), Driver::MySQL => Box::new(Mysql::new(db_path)?), @@ -85,7 +85,7 @@ pub fn build(driver: &Driver, db_path: &str) -> Result, Error> } #[cfg(test)] -mod tests { +pub(crate) mod tests { use std::sync::Arc; use std::time::Duration; @@ -152,8 +152,8 @@ mod tests { use std::sync::Arc; - use crate::core_tests::sample_info_hash; use crate::databases::Database; + use crate::test_helpers::tests::sample_info_hash; pub fn it_should_save_and_load_persistent_torrents(driver: &Arc>) { let infohash = sample_info_hash(); @@ -232,8 +232,8 @@ mod tests { use std::sync::Arc; - use crate::core_tests::random_info_hash; use crate::databases::Database; + use crate::test_helpers::tests::random_info_hash; pub fn it_should_load_the_whitelist(driver: &Arc>) { let infohash = random_info_hash(); diff --git a/packages/tracker-core/src/databases/driver/mysql.rs b/packages/tracker-core/src/databases/driver/mysql.rs index 1e1e29f3..365bd0ad 100644 --- a/packages/tracker-core/src/databases/driver/mysql.rs +++ b/packages/tracker-core/src/databases/driver/mysql.rs @@ -15,7 +15,7 @@ use crate::authentication::{self, Key}; const DRIVER: Driver = Driver::MySQL; -pub struct Mysql { +pub(crate) struct Mysql { pool: Pool, } diff --git a/packages/tracker-core/src/databases/driver/sqlite.rs b/packages/tracker-core/src/databases/driver/sqlite.rs index 37f5254a..36ca4eab 100644 --- a/packages/tracker-core/src/databases/driver/sqlite.rs +++ b/packages/tracker-core/src/databases/driver/sqlite.rs @@ -14,7 +14,7 @@ use crate::authentication::{self, Key}; const DRIVER: Driver = Driver::Sqlite3; -pub struct Sqlite { +pub(crate) struct Sqlite { pool: Pool, } diff --git a/packages/tracker-core/src/error.rs b/packages/tracker-core/src/error.rs index 515510b8..dcdd8966 100644 --- a/packages/tracker-core/src/error.rs +++ b/packages/tracker-core/src/error.rs @@ -41,8 +41,8 @@ mod tests { mod whitelist_error { - use crate::core_tests::sample_info_hash; use crate::error::WhitelistError; + use crate::test_helpers::tests::sample_info_hash; #[test] fn torrent_not_whitelisted() { diff --git a/packages/tracker-core/src/lib.rs b/packages/tracker-core/src/lib.rs index 9334e4a0..ecbaef9c 100644 --- a/packages/tracker-core/src/lib.rs +++ b/packages/tracker-core/src/lib.rs @@ -391,8 +391,8 @@ pub mod scrape_handler; pub mod torrent; pub mod whitelist; -pub mod core_tests; pub mod peer_tests; +pub mod test_helpers; use torrust_tracker_clock::clock; /// This code needs to be copied into each crate. @@ -416,8 +416,8 @@ mod tests { use torrust_tracker_test_helpers::configuration; use crate::announce_handler::AnnounceHandler; - use crate::core_tests::initialize_handlers; use crate::scrape_handler::ScrapeHandler; + use crate::test_helpers::tests::initialize_handlers; fn initialize_handlers_for_public_tracker() -> (Arc, Arc) { let config = configuration::ephemeral_public(); @@ -445,7 +445,7 @@ mod tests { use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use crate::announce_handler::PeersWanted; - use crate::core_tests::{complete_peer, incomplete_peer}; + use crate::test_helpers::tests::{complete_peer, incomplete_peer}; use crate::tests::the_tracker::initialize_handlers_for_public_tracker; #[tokio::test] @@ -500,7 +500,7 @@ mod tests { use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use crate::announce_handler::PeersWanted; - use crate::core_tests::{complete_peer, incomplete_peer}; + use crate::test_helpers::tests::{complete_peer, incomplete_peer}; use crate::tests::the_tracker::{initialize_handlers_for_listed_tracker, peer_ip}; #[tokio::test] diff --git a/packages/tracker-core/src/test_helpers.rs b/packages/tracker-core/src/test_helpers.rs new file mode 100644 index 00000000..06f5ce38 --- /dev/null +++ b/packages/tracker-core/src/test_helpers.rs @@ -0,0 +1,219 @@ +//! Some generic test helpers functions. + +#[cfg(test)] +pub(crate) mod tests { + use std::net::{IpAddr, Ipv4Addr, SocketAddr}; + use std::sync::Arc; + + use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId}; + use bittorrent_primitives::info_hash::InfoHash; + use rand::Rng; + use torrust_tracker_configuration::Configuration; + #[cfg(test)] + use torrust_tracker_configuration::Core; + use torrust_tracker_primitives::peer::Peer; + use torrust_tracker_primitives::DurationSinceUnixEpoch; + #[cfg(test)] + use torrust_tracker_test_helpers::configuration::ephemeral_sqlite_database; + + use crate::announce_handler::AnnounceHandler; + use crate::databases::setup::initialize_database; + use crate::scrape_handler::ScrapeHandler; + use crate::torrent::repository::in_memory::InMemoryTorrentRepository; + use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository; + use crate::whitelist::repository::in_memory::InMemoryWhitelist; + use crate::whitelist::{self}; + + /// Generates a random `InfoHash`. + #[must_use] + pub fn random_info_hash() -> InfoHash { + let mut rng = rand::rng(); + let mut random_bytes = [0u8; 20]; + rng.fill(&mut random_bytes); + + InfoHash::from_bytes(&random_bytes) + } + + /// # Panics + /// + /// Will panic if the string representation of the info hash is not a valid info hash. + #[must_use] + pub fn sample_info_hash() -> InfoHash { + "3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0" // DevSkim: ignore DS173237 + .parse::() + .expect("String should be a valid info hash") + } + + /// # Panics + /// + /// Will panic if the string representation of the info hash is not a valid info hash. + #[must_use] + pub fn sample_info_hash_one() -> InfoHash { + "3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0" // DevSkim: ignore DS173237 + .parse::() + .expect("String should be a valid info hash") + } + + /// # Panics + /// + /// Will panic if the string representation of the info hash is not a valid info hash. + #[must_use] + pub fn sample_info_hash_two() -> InfoHash { + "99c82bb73505a3c0b453f9fa0e881d6e5a32a0c1" // DevSkim: ignore DS173237 + .parse::() + .expect("String should be a valid info hash") + } + + /// # Panics + /// + /// Will panic if the string representation of the info hash is not a valid info hash. + #[must_use] + pub fn sample_info_hash_alphabetically_ordered_after_sample_info_hash_one() -> InfoHash { + "99c82bb73505a3c0b453f9fa0e881d6e5a32a0c1" // DevSkim: ignore DS173237 + .parse::() + .expect("String should be a valid info hash") + } + + /// Sample peer whose state is not relevant for the tests. + #[must_use] + pub fn sample_peer() -> Peer { + Peer { + peer_id: PeerId(*b"-qB00000000000000000"), + peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080), + updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0), + uploaded: NumberOfBytes::new(0), + downloaded: NumberOfBytes::new(0), + left: NumberOfBytes::new(0), // No bytes left to download + event: AnnounceEvent::Completed, + } + } + + #[must_use] + pub fn sample_peer_one() -> Peer { + Peer { + peer_id: PeerId(*b"-qB00000000000000001"), + peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8081), + updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0), + uploaded: NumberOfBytes::new(0), + downloaded: NumberOfBytes::new(0), + left: NumberOfBytes::new(0), // No bytes left to download + event: AnnounceEvent::Completed, + } + } + + #[must_use] + pub fn sample_peer_two() -> Peer { + Peer { + peer_id: PeerId(*b"-qB00000000000000002"), + peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 2)), 8082), + updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0), + uploaded: NumberOfBytes::new(0), + downloaded: NumberOfBytes::new(0), + left: NumberOfBytes::new(0), // No bytes left to download + event: AnnounceEvent::Completed, + } + } + + #[must_use] + pub fn seeder() -> Peer { + complete_peer() + } + + #[must_use] + pub fn leecher() -> Peer { + incomplete_peer() + } + + #[must_use] + pub fn started_peer() -> Peer { + incomplete_peer() + } + + #[must_use] + pub fn completed_peer() -> Peer { + complete_peer() + } + + /// A peer that counts as `complete` is swarm metadata + /// IMPORTANT!: it only counts if the it has been announce at least once before + /// announcing the `AnnounceEvent::Completed` event. + #[must_use] + pub fn complete_peer() -> Peer { + Peer { + peer_id: PeerId(*b"-qB00000000000000000"), + peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080), + updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0), + uploaded: NumberOfBytes::new(0), + downloaded: NumberOfBytes::new(0), + left: NumberOfBytes::new(0), // No bytes left to download + event: AnnounceEvent::Completed, + } + } + + /// A peer that counts as `incomplete` is swarm metadata + #[must_use] + pub fn incomplete_peer() -> Peer { + Peer { + peer_id: PeerId(*b"-qB00000000000000000"), + peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080), + updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0), + uploaded: NumberOfBytes::new(0), + downloaded: NumberOfBytes::new(0), + left: NumberOfBytes::new(1000), // Still bytes to download + event: AnnounceEvent::Started, + } + } + + #[must_use] + pub fn initialize_handlers(config: &Configuration) -> (Arc, Arc) { + let database = initialize_database(&config.core); + let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); + let whitelist_authorization = Arc::new(whitelist::authorization::WhitelistAuthorization::new( + &config.core, + &in_memory_whitelist.clone(), + )); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); + let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); + + let announce_handler = Arc::new(AnnounceHandler::new( + &config.core, + &in_memory_torrent_repository, + &db_torrent_repository, + )); + + let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); + + (announce_handler, scrape_handler) + } + + /// # Panics + /// + /// Will panic if the temporary database file path is not a valid UFT string. + #[cfg(test)] + #[must_use] + pub fn ephemeral_configuration() -> Core { + let mut config = Core::default(); + + let temp_file = ephemeral_sqlite_database(); + temp_file.to_str().unwrap().clone_into(&mut config.database.path); + + config + } + + /// # Panics + /// + /// Will panic if the temporary database file path is not a valid UFT string. + #[cfg(test)] + #[must_use] + pub fn ephemeral_configuration_for_listed_tracker() -> Core { + let mut config = Core { + listed: true, + ..Default::default() + }; + + let temp_file = ephemeral_sqlite_database(); + temp_file.to_str().unwrap().clone_into(&mut config.database.path); + + config + } +} diff --git a/packages/tracker-core/src/torrent/manager.rs b/packages/tracker-core/src/torrent/manager.rs index 778ac6d9..9dac3525 100644 --- a/packages/tracker-core/src/torrent/manager.rs +++ b/packages/tracker-core/src/torrent/manager.rs @@ -16,6 +16,7 @@ pub struct TorrentsManager { in_memory_torrent_repository: Arc, /// The persistent torrents repository. + #[allow(dead_code)] db_torrent_repository: Arc, } @@ -40,7 +41,8 @@ impl TorrentsManager { /// # Errors /// /// Will return a `database::Error` if unable to load the list of `persistent_torrents` from the database. - pub fn load_torrents_from_database(&self) -> Result<(), databases::error::Error> { + #[allow(dead_code)] + pub(crate) fn load_torrents_from_database(&self) -> Result<(), databases::error::Error> { let persistent_torrents = self.db_torrent_repository.load_all()?; self.in_memory_torrent_repository.import_persistent(&persistent_torrents); @@ -71,8 +73,8 @@ mod tests { use torrust_tracker_torrent_repository::entry::EntrySync; use super::{DatabasePersistentTorrentRepository, TorrentsManager}; - use crate::core_tests::{ephemeral_configuration, sample_info_hash}; use crate::databases::setup::initialize_database; + use crate::test_helpers::tests::{ephemeral_configuration, sample_info_hash}; use crate::torrent::repository::in_memory::InMemoryTorrentRepository; struct TorrentsManagerDeps { @@ -138,7 +140,7 @@ mod tests { use torrust_tracker_clock::clock::{self}; use torrust_tracker_primitives::DurationSinceUnixEpoch; - use crate::core_tests::{ephemeral_configuration, sample_info_hash, sample_peer}; + use crate::test_helpers::tests::{ephemeral_configuration, sample_info_hash, sample_peer}; use crate::torrent::manager::tests::{initialize_torrents_manager, initialize_torrents_manager_with}; use crate::torrent::repository::in_memory::InMemoryTorrentRepository; diff --git a/packages/tracker-core/src/torrent/mod.rs b/packages/tracker-core/src/torrent/mod.rs index 340f049d..7ca9000f 100644 --- a/packages/tracker-core/src/torrent/mod.rs +++ b/packages/tracker-core/src/torrent/mod.rs @@ -29,8 +29,11 @@ pub mod manager; pub mod repository; pub mod services; -use torrust_tracker_torrent_repository::{EntryMutexStd, TorrentsSkipMapMutexStd}; +#[cfg(test)] +use torrust_tracker_torrent_repository::EntryMutexStd; +use torrust_tracker_torrent_repository::TorrentsSkipMapMutexStd; // Currently used types from the torrent repository crate. -pub type Torrents = TorrentsSkipMapMutexStd; -pub type TorrentEntry = EntryMutexStd; +pub(crate) type Torrents = TorrentsSkipMapMutexStd; +#[cfg(test)] +pub(crate) type TorrentEntry = EntryMutexStd; diff --git a/packages/tracker-core/src/torrent/repository/in_memory.rs b/packages/tracker-core/src/torrent/repository/in_memory.rs index baa0c4fd..26302260 100644 --- a/packages/tracker-core/src/torrent/repository/in_memory.rs +++ b/packages/tracker-core/src/torrent/repository/in_memory.rs @@ -32,33 +32,34 @@ impl InMemoryTorrentRepository { self.torrents.upsert_peer(info_hash, peer); } + #[cfg(test)] #[must_use] - pub fn remove(&self, key: &InfoHash) -> Option { + pub(crate) fn remove(&self, key: &InfoHash) -> Option { self.torrents.remove(key) } - pub fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch) { + pub(crate) fn remove_inactive_peers(&self, current_cutoff: DurationSinceUnixEpoch) { self.torrents.remove_inactive_peers(current_cutoff); } - pub fn remove_peerless_torrents(&self, policy: &TrackerPolicy) { + pub(crate) fn remove_peerless_torrents(&self, policy: &TrackerPolicy) { self.torrents.remove_peerless_torrents(policy); } #[must_use] - pub fn get(&self, key: &InfoHash) -> Option { + pub(crate) fn get(&self, key: &InfoHash) -> Option { self.torrents.get(key) } #[must_use] - pub fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, EntryMutexStd)> { + pub(crate) fn get_paginated(&self, pagination: Option<&Pagination>) -> Vec<(InfoHash, EntryMutexStd)> { self.torrents.get_paginated(pagination) } /// It returns the data for a `scrape` response or empty if the torrent is /// not found. #[must_use] - pub fn get_swarm_metadata(&self, info_hash: &InfoHash) -> SwarmMetadata { + pub(crate) fn get_swarm_metadata(&self, info_hash: &InfoHash) -> SwarmMetadata { match self.torrents.get(info_hash) { Some(torrent_entry) => torrent_entry.get_swarm_metadata(), None => SwarmMetadata::zeroed(), @@ -69,7 +70,7 @@ impl InMemoryTorrentRepository { /// /// It filters out the client making the request. #[must_use] - pub fn get_peers_for(&self, info_hash: &InfoHash, peer: &peer::Peer, limit: usize) -> Vec> { + pub(crate) fn get_peers_for(&self, info_hash: &InfoHash, peer: &peer::Peer, limit: usize) -> Vec> { match self.torrents.get(info_hash) { None => vec![], Some(entry) => entry.get_peers_for_client(&peer.peer_addr, Some(max(limit, TORRENT_PEERS_LIMIT))), @@ -135,7 +136,7 @@ mod tests { use std::sync::Arc; - use crate::core_tests::{sample_info_hash, sample_peer}; + use crate::test_helpers::tests::{sample_info_hash, sample_peer}; use crate::torrent::repository::in_memory::InMemoryTorrentRepository; #[tokio::test] @@ -171,7 +172,7 @@ mod tests { use torrust_tracker_primitives::peer::Peer; use torrust_tracker_primitives::DurationSinceUnixEpoch; - use crate::core_tests::{sample_info_hash, sample_peer}; + use crate::test_helpers::tests::{sample_info_hash, sample_peer}; use crate::torrent::repository::in_memory::tests::the_in_memory_torrent_repository::numeric_peer_id; use crate::torrent::repository::in_memory::InMemoryTorrentRepository; @@ -233,7 +234,7 @@ mod tests { use torrust_tracker_primitives::peer::Peer; use torrust_tracker_primitives::DurationSinceUnixEpoch; - use crate::core_tests::{sample_info_hash, sample_peer}; + use crate::test_helpers::tests::{sample_info_hash, sample_peer}; use crate::torrent::repository::in_memory::tests::the_in_memory_torrent_repository::numeric_peer_id; use crate::torrent::repository::in_memory::InMemoryTorrentRepository; @@ -303,7 +304,7 @@ mod tests { use torrust_tracker_configuration::TrackerPolicy; use torrust_tracker_primitives::DurationSinceUnixEpoch; - use crate::core_tests::{sample_info_hash, sample_peer}; + use crate::test_helpers::tests::{sample_info_hash, sample_peer}; use crate::torrent::repository::in_memory::InMemoryTorrentRepository; #[tokio::test] @@ -374,7 +375,7 @@ mod tests { use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use torrust_tracker_torrent_repository::entry::EntrySync; - use crate::core_tests::{sample_info_hash, sample_peer}; + use crate::test_helpers::tests::{sample_info_hash, sample_peer}; use crate::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::torrent::TorrentEntry; @@ -429,7 +430,7 @@ mod tests { use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; - use crate::core_tests::{sample_info_hash, sample_peer}; + use crate::test_helpers::tests::{sample_info_hash, sample_peer}; use crate::torrent::repository::in_memory::tests::the_in_memory_torrent_repository::returning_torrent_entries::TorrentEntryInfo; use crate::torrent::repository::in_memory::InMemoryTorrentRepository; @@ -467,7 +468,7 @@ mod tests { use torrust_tracker_primitives::pagination::Pagination; use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; - use crate::core_tests::{ + use crate::test_helpers::tests::{ sample_info_hash_alphabetically_ordered_after_sample_info_hash_one, sample_info_hash_one, sample_peer_one, sample_peer_two, }; @@ -577,7 +578,7 @@ mod tests { use bittorrent_primitives::info_hash::fixture::gen_seeded_infohash; use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; - use crate::core_tests::{complete_peer, leecher, sample_info_hash, seeder}; + use crate::test_helpers::tests::{complete_peer, leecher, sample_info_hash, seeder}; use crate::torrent::repository::in_memory::InMemoryTorrentRepository; // todo: refactor to use test parametrization @@ -689,7 +690,7 @@ mod tests { use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; - use crate::core_tests::{leecher, sample_info_hash}; + use crate::test_helpers::tests::{leecher, sample_info_hash}; use crate::torrent::repository::in_memory::InMemoryTorrentRepository; #[tokio::test] @@ -728,7 +729,7 @@ mod tests { use torrust_tracker_primitives::PersistentTorrents; - use crate::core_tests::sample_info_hash; + use crate::test_helpers::tests::sample_info_hash; use crate::torrent::repository::in_memory::InMemoryTorrentRepository; #[tokio::test] diff --git a/packages/tracker-core/src/torrent/repository/persisted.rs b/packages/tracker-core/src/torrent/repository/persisted.rs index 224919d0..0430f03b 100644 --- a/packages/tracker-core/src/torrent/repository/persisted.rs +++ b/packages/tracker-core/src/torrent/repository/persisted.rs @@ -29,7 +29,7 @@ impl DatabasePersistentTorrentRepository { /// # Errors /// /// Will return a database `Err` if unable to load. - pub fn load_all(&self) -> Result { + pub(crate) fn load_all(&self) -> Result { self.database.load_persistent_torrents() } @@ -38,7 +38,7 @@ impl DatabasePersistentTorrentRepository { /// # Errors /// /// Will return a database `Err` if unable to save. - pub fn save(&self, info_hash: &InfoHash, downloaded: u32) -> Result<(), Error> { + pub(crate) fn save(&self, info_hash: &InfoHash, downloaded: u32) -> Result<(), Error> { self.database.save_persistent_torrent(info_hash, downloaded) } } @@ -49,8 +49,8 @@ mod tests { use torrust_tracker_primitives::PersistentTorrents; use super::DatabasePersistentTorrentRepository; - use crate::core_tests::{ephemeral_configuration, sample_info_hash, sample_info_hash_one, sample_info_hash_two}; use crate::databases::setup::initialize_database; + use crate::test_helpers::tests::{ephemeral_configuration, sample_info_hash, sample_info_hash_one, sample_info_hash_two}; fn initialize_db_persistent_torrent_repository() -> DatabasePersistentTorrentRepository { let config = ephemeral_configuration(); diff --git a/packages/tracker-core/src/torrent/services.rs b/packages/tracker-core/src/torrent/services.rs index c36190ed..4c470bb7 100644 --- a/packages/tracker-core/src/torrent/services.rs +++ b/packages/tracker-core/src/torrent/services.rs @@ -302,7 +302,7 @@ mod tests { use std::sync::Arc; - use crate::core_tests::sample_info_hash; + use crate::test_helpers::tests::sample_info_hash; use crate::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::torrent::services::tests::sample_peer; use crate::torrent::services::{get_torrents, BasicInfo}; diff --git a/packages/tracker-core/src/whitelist/authorization.rs b/packages/tracker-core/src/whitelist/authorization.rs index 66f90922..3b7b8b4f 100644 --- a/packages/tracker-core/src/whitelist/authorization.rs +++ b/packages/tracker-core/src/whitelist/authorization.rs @@ -88,8 +88,8 @@ mod tests { use torrust_tracker_configuration::Core; - use crate::core_tests::sample_info_hash; use crate::error::WhitelistError; + use crate::test_helpers::tests::sample_info_hash; use crate::whitelist::authorization::tests::the_whitelist_authorization_for_announce_and_scrape_actions::{ initialize_whitelist_authorization_and_dependencies_with, initialize_whitelist_authorization_with, }; @@ -129,7 +129,7 @@ mod tests { use torrust_tracker_configuration::Core; - use crate::core_tests::sample_info_hash; + use crate::test_helpers::tests::sample_info_hash; use crate::whitelist::authorization::tests::the_whitelist_authorization_for_announce_and_scrape_actions::{ initialize_whitelist_authorization_and_dependencies_with, initialize_whitelist_authorization_with, }; diff --git a/packages/tracker-core/src/whitelist/manager.rs b/packages/tracker-core/src/whitelist/manager.rs index e1cd2f89..5ebd6db3 100644 --- a/packages/tracker-core/src/whitelist/manager.rs +++ b/packages/tracker-core/src/whitelist/manager.rs @@ -73,9 +73,9 @@ mod tests { use torrust_tracker_configuration::Core; - use crate::core_tests::ephemeral_configuration_for_listed_tracker; use crate::databases::setup::initialize_database; use crate::databases::Database; + use crate::test_helpers::tests::ephemeral_configuration_for_listed_tracker; use crate::whitelist::manager::WhitelistManager; use crate::whitelist::repository::in_memory::InMemoryWhitelist; use crate::whitelist::repository::persisted::DatabaseWhitelist; @@ -111,7 +111,7 @@ mod tests { mod configured_as_whitelisted { mod handling_the_torrent_whitelist { - use crate::core_tests::sample_info_hash; + use crate::test_helpers::tests::sample_info_hash; use crate::whitelist::manager::tests::initialize_whitelist_manager_for_whitelisted_tracker; #[tokio::test] @@ -141,7 +141,7 @@ mod tests { } mod persistence { - use crate::core_tests::sample_info_hash; + use crate::test_helpers::tests::sample_info_hash; use crate::whitelist::manager::tests::initialize_whitelist_manager_for_whitelisted_tracker; #[tokio::test] diff --git a/packages/tracker-core/src/whitelist/mod.rs b/packages/tracker-core/src/whitelist/mod.rs index 8521485f..a39768e9 100644 --- a/packages/tracker-core/src/whitelist/mod.rs +++ b/packages/tracker-core/src/whitelist/mod.rs @@ -2,7 +2,7 @@ pub mod authorization; pub mod manager; pub mod repository; pub mod setup; -pub mod whitelist_tests; +pub mod test_helpers; #[cfg(test)] mod tests { @@ -10,8 +10,8 @@ mod tests { mod configured_as_whitelisted { mod handling_authorization { - use crate::core_tests::sample_info_hash; - use crate::whitelist::whitelist_tests::initialize_whitelist_services_for_listed_tracker; + use crate::test_helpers::tests::sample_info_hash; + use crate::whitelist::test_helpers::tests::initialize_whitelist_services_for_listed_tracker; #[tokio::test] async fn it_should_authorize_the_announce_and_scrape_actions_on_whitelisted_torrents() { diff --git a/packages/tracker-core/src/whitelist/repository/in_memory.rs b/packages/tracker-core/src/whitelist/repository/in_memory.rs index befd6fed..4faeda78 100644 --- a/packages/tracker-core/src/whitelist/repository/in_memory.rs +++ b/packages/tracker-core/src/whitelist/repository/in_memory.rs @@ -14,7 +14,7 @@ impl InMemoryWhitelist { } /// It removes a torrent from the whitelist in memory. - pub async fn remove(&self, info_hash: &InfoHash) -> bool { + pub(crate) async fn remove(&self, info_hash: &InfoHash) -> bool { self.whitelist.write().await.remove(info_hash) } @@ -24,7 +24,7 @@ impl InMemoryWhitelist { } /// It clears the whitelist. - pub async fn clear(&self) { + pub(crate) async fn clear(&self) { let mut whitelist = self.whitelist.write().await; whitelist.clear(); } @@ -33,7 +33,7 @@ impl InMemoryWhitelist { #[cfg(test)] mod tests { - use crate::core_tests::sample_info_hash; + use crate::test_helpers::tests::sample_info_hash; use crate::whitelist::repository::in_memory::InMemoryWhitelist; #[tokio::test] diff --git a/packages/tracker-core/src/whitelist/repository/persisted.rs b/packages/tracker-core/src/whitelist/repository/persisted.rs index 5101b5e3..4773cfbe 100644 --- a/packages/tracker-core/src/whitelist/repository/persisted.rs +++ b/packages/tracker-core/src/whitelist/repository/persisted.rs @@ -22,7 +22,7 @@ impl DatabaseWhitelist { /// # Errors /// /// Will return a `database::Error` if unable to add the `info_hash` to the whitelist database. - pub fn add(&self, info_hash: &InfoHash) -> Result<(), databases::error::Error> { + pub(crate) fn add(&self, info_hash: &InfoHash) -> Result<(), databases::error::Error> { let is_whitelisted = self.database.is_info_hash_whitelisted(*info_hash)?; if is_whitelisted { @@ -39,7 +39,7 @@ impl DatabaseWhitelist { /// # Errors /// /// Will return a `database::Error` if unable to remove the `info_hash` from the whitelist database. - pub fn remove(&self, info_hash: &InfoHash) -> Result<(), databases::error::Error> { + pub(crate) fn remove(&self, info_hash: &InfoHash) -> Result<(), databases::error::Error> { let is_whitelisted = self.database.is_info_hash_whitelisted(*info_hash)?; if !is_whitelisted { @@ -56,7 +56,7 @@ impl DatabaseWhitelist { /// # Errors /// /// Will return a `database::Error` if unable to load the list whitelisted `info_hash`s from the database. - pub fn load_from_database(&self) -> Result, databases::error::Error> { + pub(crate) fn load_from_database(&self) -> Result, databases::error::Error> { self.database.load_whitelist() } } @@ -65,8 +65,8 @@ impl DatabaseWhitelist { mod tests { mod the_persisted_whitelist_repository { - use crate::core_tests::{ephemeral_configuration_for_listed_tracker, sample_info_hash}; use crate::databases::setup::initialize_database; + use crate::test_helpers::tests::{ephemeral_configuration_for_listed_tracker, sample_info_hash}; use crate::whitelist::repository::persisted::DatabaseWhitelist; fn initialize_database_whitelist() -> DatabaseWhitelist { diff --git a/packages/tracker-core/src/whitelist/test_helpers.rs b/packages/tracker-core/src/whitelist/test_helpers.rs new file mode 100644 index 00000000..cc30c447 --- /dev/null +++ b/packages/tracker-core/src/whitelist/test_helpers.rs @@ -0,0 +1,32 @@ +//! Some generic test helpers functions. + +#[cfg(test)] +pub(crate) mod tests { + + use std::sync::Arc; + + use torrust_tracker_configuration::Configuration; + + use crate::databases::setup::initialize_database; + use crate::whitelist::authorization::WhitelistAuthorization; + use crate::whitelist::manager::WhitelistManager; + use crate::whitelist::repository::in_memory::InMemoryWhitelist; + use crate::whitelist::setup::initialize_whitelist_manager; + + #[must_use] + pub fn initialize_whitelist_services(config: &Configuration) -> (Arc, Arc) { + let database = initialize_database(&config.core); + let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); + let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone())); + let whitelist_manager = initialize_whitelist_manager(database.clone(), in_memory_whitelist.clone()); + + (whitelist_authorization, whitelist_manager) + } + + #[must_use] + pub fn initialize_whitelist_services_for_listed_tracker() -> (Arc, Arc) { + use torrust_tracker_test_helpers::configuration; + + initialize_whitelist_services(&configuration::ephemeral_listed()) + } +} diff --git a/packages/tracker-core/src/whitelist/whitelist_tests.rs b/packages/tracker-core/src/whitelist/whitelist_tests.rs deleted file mode 100644 index d2fd275f..00000000 --- a/packages/tracker-core/src/whitelist/whitelist_tests.rs +++ /dev/null @@ -1,27 +0,0 @@ -use std::sync::Arc; - -use torrust_tracker_configuration::Configuration; - -use super::authorization::WhitelistAuthorization; -use super::manager::WhitelistManager; -use super::repository::in_memory::InMemoryWhitelist; -use crate::databases::setup::initialize_database; -use crate::whitelist::setup::initialize_whitelist_manager; - -#[must_use] -pub fn initialize_whitelist_services(config: &Configuration) -> (Arc, Arc) { - let database = initialize_database(&config.core); - let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); - let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone())); - let whitelist_manager = initialize_whitelist_manager(database.clone(), in_memory_whitelist.clone()); - - (whitelist_authorization, whitelist_manager) -} - -#[cfg(test)] -#[must_use] -pub fn initialize_whitelist_services_for_listed_tracker() -> (Arc, Arc) { - use torrust_tracker_test_helpers::configuration; - - initialize_whitelist_services(&configuration::ephemeral_listed()) -} diff --git a/src/bootstrap/app.rs b/src/bootstrap/app.rs index 0236215f..e0d77ab8 100644 --- a/src/bootstrap/app.rs +++ b/src/bootstrap/app.rs @@ -120,6 +120,7 @@ pub fn initialize_app_container(configuration: &Configuration) -> AppContainer { )); let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); + let torrents_manager = Arc::new(TorrentsManager::new( &configuration.core, &in_memory_torrent_repository, diff --git a/src/servers/http/mod.rs b/src/servers/http/mod.rs index fa0ccc77..6bc93992 100644 --- a/src/servers/http/mod.rs +++ b/src/servers/http/mod.rs @@ -306,6 +306,7 @@ use serde::{Deserialize, Serialize}; pub mod server; +pub mod test_helpers; pub mod v1; pub const HTTP_TRACKER_LOG_TARGET: &str = "HTTP TRACKER"; diff --git a/src/servers/http/test_helpers.rs b/src/servers/http/test_helpers.rs new file mode 100644 index 00000000..8c3020c5 --- /dev/null +++ b/src/servers/http/test_helpers.rs @@ -0,0 +1,16 @@ +//! Some generic test helpers functions. + +#[cfg(test)] +pub(crate) mod tests { + use bittorrent_primitives::info_hash::InfoHash; + + /// # Panics + /// + /// Will panic if the string representation of the info hash is not a valid info hash. + #[must_use] + pub fn sample_info_hash() -> InfoHash { + "3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0" // DevSkim: ignore DS173237 + .parse::() + .expect("String should be a valid info hash") + } +} diff --git a/src/servers/http/v1/handlers/announce.rs b/src/servers/http/v1/handlers/announce.rs index f76aa7a0..64939ff4 100644 --- a/src/servers/http/v1/handlers/announce.rs +++ b/src/servers/http/v1/handlers/announce.rs @@ -254,7 +254,6 @@ mod tests { use bittorrent_tracker_core::announce_handler::AnnounceHandler; use bittorrent_tracker_core::authentication::key::repository::in_memory::InMemoryKeyRepository; use bittorrent_tracker_core::authentication::service::AuthenticationService; - use bittorrent_tracker_core::core_tests::sample_info_hash; use bittorrent_tracker_core::databases::setup::initialize_database; use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository; use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository; @@ -264,6 +263,7 @@ mod tests { use torrust_tracker_test_helpers::configuration; use crate::packages::http_tracker_core; + use crate::servers::http::test_helpers::tests::sample_info_hash; struct CoreTrackerServices { pub core_config: Arc, diff --git a/src/servers/http/v1/services/announce.rs b/src/servers/http/v1/services/announce.rs index 4de9296b..e321ad01 100644 --- a/src/servers/http/v1/services/announce.rs +++ b/src/servers/http/v1/services/announce.rs @@ -153,7 +153,6 @@ mod tests { use std::sync::Arc; use bittorrent_tracker_core::announce_handler::{AnnounceHandler, PeersWanted}; - use bittorrent_tracker_core::core_tests::sample_info_hash; use bittorrent_tracker_core::databases::setup::initialize_database; use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository; use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository; @@ -165,6 +164,7 @@ mod tests { use super::{sample_peer_using_ipv4, sample_peer_using_ipv6}; use crate::packages::http_tracker_core; + use crate::servers::http::test_helpers::tests::sample_info_hash; use crate::servers::http::v1::services::announce::invoke; use crate::servers::http::v1::services::announce::tests::{ initialize_core_tracker_services, sample_peer, MockHttpStatsEventSender, diff --git a/src/servers/http/v1/services/scrape.rs b/src/servers/http/v1/services/scrape.rs index 3a232369..e2eb4f87 100644 --- a/src/servers/http/v1/services/scrape.rs +++ b/src/servers/http/v1/services/scrape.rs @@ -84,7 +84,6 @@ mod tests { use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId}; use bittorrent_primitives::info_hash::InfoHash; use bittorrent_tracker_core::announce_handler::AnnounceHandler; - use bittorrent_tracker_core::core_tests::sample_info_hash; use bittorrent_tracker_core::databases::setup::initialize_database; use bittorrent_tracker_core::scrape_handler::ScrapeHandler; use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository; @@ -98,6 +97,7 @@ mod tests { use torrust_tracker_test_helpers::configuration; use crate::packages::http_tracker_core; + use crate::servers::http::test_helpers::tests::sample_info_hash; fn initialize_announce_and_scrape_handlers_for_public_tracker() -> (Arc, Arc) { let config = configuration::ephemeral_public(); @@ -162,10 +162,11 @@ mod tests { use torrust_tracker_primitives::swarm_metadata::SwarmMetadata; use crate::packages::{self, http_tracker_core}; + use crate::servers::http::test_helpers::tests::sample_info_hash; use crate::servers::http::v1::services::scrape::invoke; use crate::servers::http::v1::services::scrape::tests::{ - initialize_announce_and_scrape_handlers_for_public_tracker, initialize_scrape_handler, sample_info_hash, - sample_info_hashes, sample_peer, MockHttpStatsEventSender, + initialize_announce_and_scrape_handlers_for_public_tracker, initialize_scrape_handler, sample_info_hashes, + sample_peer, MockHttpStatsEventSender, }; #[tokio::test] @@ -247,10 +248,10 @@ mod tests { use torrust_tracker_primitives::core::ScrapeData; use crate::packages::{self, http_tracker_core}; + use crate::servers::http::test_helpers::tests::sample_info_hash; use crate::servers::http::v1::services::scrape::fake; use crate::servers::http::v1::services::scrape::tests::{ - initialize_announce_and_scrape_handlers_for_public_tracker, sample_info_hash, sample_info_hashes, sample_peer, - MockHttpStatsEventSender, + initialize_announce_and_scrape_handlers_for_public_tracker, sample_info_hashes, sample_peer, MockHttpStatsEventSender, }; #[tokio::test]