diff --git a/src/app_test.rs b/src/app_test.rs deleted file mode 100644 index fb1dd01c8..000000000 --- a/src/app_test.rs +++ /dev/null @@ -1,62 +0,0 @@ -//! This file contains only functions used for testing. -use std::sync::Arc; - -use torrust_tracker_configuration::Configuration; - -use crate::core::authentication::handler::KeysHandler; -use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository; -use crate::core::authentication::key::repository::persisted::DatabaseKeyRepository; -use crate::core::authentication::service::{self, AuthenticationService}; -use crate::core::databases::Database; -use crate::core::services::initialize_database; -use crate::core::torrent::manager::TorrentsManager; -use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; -use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; -use crate::core::whitelist; -use crate::core::whitelist::repository::in_memory::InMemoryWhitelist; - -/// Initialize the tracker dependencies. -#[allow(clippy::type_complexity)] -#[must_use] -pub fn initialize_tracker_dependencies( - config: &Configuration, -) -> ( - Arc>, - Arc, - Arc, - Arc, - Arc, - Arc, - Arc, -) { - let database = initialize_database(config); - let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); - let whitelist_authorization = Arc::new(whitelist::authorization::WhitelistAuthorization::new( - &config.core, - &in_memory_whitelist.clone(), - )); - let db_key_repository = Arc::new(DatabaseKeyRepository::new(&database)); - let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default()); - let authentication_service = Arc::new(service::AuthenticationService::new(&config.core, &in_memory_key_repository)); - let _keys_handler = Arc::new(KeysHandler::new( - &db_key_repository.clone(), - &in_memory_key_repository.clone(), - )); - 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( - &config.core, - &in_memory_torrent_repository, - &db_torrent_repository, - )); - - ( - database, - in_memory_whitelist, - whitelist_authorization, - authentication_service, - in_memory_torrent_repository, - db_torrent_repository, - torrents_manager, - ) -} diff --git a/src/core/services/statistics/mod.rs b/src/core/services/statistics/mod.rs index 18d96605e..79bc5f268 100644 --- a/src/core/services/statistics/mod.rs +++ b/src/core/services/statistics/mod.rs @@ -117,8 +117,8 @@ mod tests { use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics; use torrust_tracker_test_helpers::configuration; - use crate::app_test::initialize_tracker_dependencies; use crate::core::services::statistics::{self, get_metrics, TrackerMetrics}; + use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; use crate::core::{self}; use crate::servers::udp::server::banning::BanService; use crate::servers::udp::server::launcher::MAX_CONNECTION_ID_ERRORS_PER_IP; @@ -131,19 +131,9 @@ mod tests { async fn the_statistics_service_should_return_the_tracker_metrics() { let config = tracker_configuration(); - let ( - _database, - _in_memory_whitelist, - _whitelist_authorization, - _authentication_service, - in_memory_torrent_repository, - _db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); - + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); let (_stats_event_sender, stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics); let stats_repository = Arc::new(stats_repository); - let ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP))); let tracker_metrics = get_metrics( diff --git a/src/core/services/torrent.rs b/src/core/services/torrent.rs index 6ae2c26a4..d809fc266 100644 --- a/src/core/services/torrent.rs +++ b/src/core/services/torrent.rs @@ -112,29 +112,10 @@ pub async fn get_torrents( #[cfg(test)] mod tests { use std::net::{IpAddr, Ipv4Addr, SocketAddr}; - use std::sync::Arc; use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId}; - use torrust_tracker_configuration::Configuration; use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch}; - use crate::app_test::initialize_tracker_dependencies; - use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; - - fn initialize_in_memory_torrent_repository(config: &Configuration) -> Arc { - let ( - _database, - _in_memory_whitelist, - _whitelist_authorization, - _authentication_service, - in_memory_torrent_repository, - _db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(config); - - in_memory_torrent_repository - } - fn sample_peer() -> peer::Peer { peer::Peer { peer_id: PeerId(*b"-qB00000000000000000"), @@ -153,17 +134,11 @@ mod tests { use std::sync::Arc; use bittorrent_primitives::info_hash::InfoHash; - use torrust_tracker_configuration::Configuration; - use torrust_tracker_test_helpers::configuration; - use crate::core::services::torrent::tests::{initialize_in_memory_torrent_repository, sample_peer}; + use crate::core::services::torrent::tests::sample_peer; use crate::core::services::torrent::{get_torrent_info, Info}; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; - pub fn tracker_configuration() -> Configuration { - configuration::ephemeral() - } - #[tokio::test] async fn should_return_none_if_the_tracker_does_not_have_the_torrent() { let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); @@ -179,9 +154,7 @@ mod tests { #[tokio::test] async fn should_return_the_torrent_info_if_the_tracker_has_the_torrent() { - let config = tracker_configuration(); - - let in_memory_torrent_repository = initialize_in_memory_torrent_repository(&config); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned(); let info_hash = InfoHash::from_str(&hash).unwrap(); @@ -210,17 +183,11 @@ mod tests { use std::sync::Arc; use bittorrent_primitives::info_hash::InfoHash; - use torrust_tracker_configuration::Configuration; - use torrust_tracker_test_helpers::configuration; - use crate::core::services::torrent::tests::{initialize_in_memory_torrent_repository, sample_peer}; + use crate::core::services::torrent::tests::sample_peer; use crate::core::services::torrent::{get_torrents_page, BasicInfo, Pagination}; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; - pub fn tracker_configuration() -> Configuration { - configuration::ephemeral() - } - #[tokio::test] async fn should_return_an_empty_result_if_the_tracker_does_not_have_any_torrent() { let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); @@ -232,9 +199,7 @@ mod tests { #[tokio::test] async fn should_return_a_summarized_info_for_all_torrents() { - let config = tracker_configuration(); - - let in_memory_torrent_repository = initialize_in_memory_torrent_repository(&config); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); let hash = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned(); let info_hash = InfoHash::from_str(&hash).unwrap(); @@ -256,9 +221,7 @@ mod tests { #[tokio::test] async fn should_allow_limiting_the_number_of_torrents_in_the_result() { - let config = tracker_configuration(); - - let in_memory_torrent_repository = initialize_in_memory_torrent_repository(&config); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned(); let info_hash1 = InfoHash::from_str(&hash1).unwrap(); @@ -279,9 +242,7 @@ mod tests { #[tokio::test] async fn should_allow_using_pagination_in_the_result() { - let config = tracker_configuration(); - - let in_memory_torrent_repository = initialize_in_memory_torrent_repository(&config); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned(); let info_hash1 = InfoHash::from_str(&hash1).unwrap(); @@ -311,9 +272,7 @@ mod tests { #[tokio::test] async fn should_return_torrents_ordered_by_info_hash() { - let config = tracker_configuration(); - - let in_memory_torrent_repository = initialize_in_memory_torrent_repository(&config); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); let hash1 = "9e0217d0fa71c87332cd8bf9dbeabcb2c2cf3c4d".to_owned(); let info_hash1 = InfoHash::from_str(&hash1).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 8e0e64db0..212430605 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -491,7 +491,6 @@ use torrust_tracker_clock::clock; pub mod app; -pub mod app_test; pub mod bootstrap; pub mod console; pub mod container; diff --git a/src/servers/http/v1/handlers/announce.rs b/src/servers/http/v1/handlers/announce.rs index 247c6b8c6..d6c850327 100644 --- a/src/servers/http/v1/handlers/announce.rs +++ b/src/servers/http/v1/handlers/announce.rs @@ -250,76 +250,73 @@ mod tests { use bittorrent_http_protocol::v1::requests::announce::Announce; use bittorrent_http_protocol::v1::responses; use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources; - use bittorrent_primitives::info_hash::InfoHash; use torrust_tracker_configuration::{Configuration, Core}; use torrust_tracker_test_helpers::configuration; - use crate::app_test::initialize_tracker_dependencies; use crate::core::announce_handler::AnnounceHandler; + use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository; use crate::core::authentication::service::AuthenticationService; - use crate::core::services::statistics; + use crate::core::core_tests::sample_info_hash; + use crate::core::services::{initialize_database, statistics}; use crate::core::statistics::event::sender::Sender; - use crate::core::whitelist; - - type TrackerAndDeps = ( - Arc, - Arc, - Arc>>, - Arc, - Arc, - ); - - fn private_tracker() -> TrackerAndDeps { - initialize_tracker_and_deps(configuration::ephemeral_private()) + use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; + use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; + use crate::core::whitelist::authorization::WhitelistAuthorization; + use crate::core::whitelist::repository::in_memory::InMemoryWhitelist; + + struct CoreTrackerServices { + pub core_config: Arc, + pub announce_handler: Arc, + pub stats_event_sender: Arc>>, + pub whitelist_authorization: Arc, + pub authentication_service: Arc, } - fn whitelisted_tracker() -> TrackerAndDeps { - initialize_tracker_and_deps(configuration::ephemeral_listed()) + fn initialize_private_tracker() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_private()) } - fn tracker_on_reverse_proxy() -> TrackerAndDeps { - initialize_tracker_and_deps(configuration::ephemeral_with_reverse_proxy()) + fn initialize_listed_tracker() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_listed()) } - fn tracker_not_on_reverse_proxy() -> TrackerAndDeps { - initialize_tracker_and_deps(configuration::ephemeral_without_reverse_proxy()) + fn initialize_tracker_on_reverse_proxy() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_with_reverse_proxy()) } - /// Initialize tracker's dependencies and tracker. - fn initialize_tracker_and_deps(config: Configuration) -> TrackerAndDeps { - let ( - _database, - _in_memory_whitelist, - whitelist_authorization, - authentication_service, - in_memory_torrent_repository, - db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); + fn initialize_tracker_not_on_reverse_proxy() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_without_reverse_proxy()) + } + fn initialize_core_tracker_services(config: &Configuration) -> CoreTrackerServices { + let core_config = Arc::new(config.core.clone()); + let database = initialize_database(config); + let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); + let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone())); + let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default()); + let authentication_service = Arc::new(AuthenticationService::new(&config.core, &in_memory_key_repository)); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); + let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics); let stats_event_sender = Arc::new(stats_event_sender); - let announce_handler = Arc::new(AnnounceHandler::new( &config.core, &in_memory_torrent_repository, &db_torrent_repository, )); - let config = Arc::new(config.core); - - ( - config, + CoreTrackerServices { + core_config, announce_handler, stats_event_sender, whitelist_authorization, authentication_service, - ) + } } fn sample_announce_request() -> Announce { Announce { - info_hash: "3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::().unwrap(), + info_hash: sample_info_hash(), peer_id: PeerId(*b"-qB00000000000000001"), port: 17548, downloaded: None, @@ -348,28 +345,24 @@ mod tests { mod with_tracker_in_private_mode { use std::str::FromStr; - use std::sync::Arc; - use super::{private_tracker, sample_announce_request, sample_client_ip_sources}; + use super::{initialize_private_tracker, sample_announce_request, sample_client_ip_sources}; use crate::core::authentication; use crate::servers::http::v1::handlers::announce::handle_announce; use crate::servers::http::v1::handlers::announce::tests::assert_error_response; #[tokio::test] async fn it_should_fail_when_the_authentication_key_is_missing() { - let (config, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = - private_tracker(); - - let stats_event_sender = Arc::new(stats_event_sender); + let core_tracker_services = initialize_private_tracker(); let maybe_key = None; let response = handle_announce( - &config, - &announce_handler, - &authentication_service, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, &sample_announce_request(), &sample_client_ip_sources(), maybe_key, @@ -385,21 +378,18 @@ mod tests { #[tokio::test] async fn it_should_fail_when_the_authentication_key_is_invalid() { - let (config, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = - private_tracker(); - - let stats_event_sender = Arc::new(stats_event_sender); + let core_tracker_services = initialize_private_tracker(); let unregistered_key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap(); let maybe_key = Some(unregistered_key); let response = handle_announce( - &config, - &announce_handler, - &authentication_service, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, &sample_announce_request(), &sample_client_ip_sources(), maybe_key, @@ -413,27 +403,22 @@ mod tests { mod with_tracker_in_listed_mode { - use std::sync::Arc; - - use super::{sample_announce_request, sample_client_ip_sources, whitelisted_tracker}; + use super::{initialize_listed_tracker, sample_announce_request, sample_client_ip_sources}; use crate::servers::http::v1::handlers::announce::handle_announce; use crate::servers::http::v1::handlers::announce::tests::assert_error_response; #[tokio::test] async fn it_should_fail_when_the_announced_torrent_is_not_whitelisted() { - let (config, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = - whitelisted_tracker(); - - let stats_event_sender = Arc::new(stats_event_sender); + let core_tracker_services = initialize_listed_tracker(); let announce_request = sample_announce_request(); let response = handle_announce( - &config, - &announce_handler, - &authentication_service, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, &announce_request, &sample_client_ip_sources(), None, @@ -453,20 +438,15 @@ mod tests { mod with_tracker_on_reverse_proxy { - use std::sync::Arc; - use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources; - use super::{sample_announce_request, tracker_on_reverse_proxy}; + use super::{initialize_tracker_on_reverse_proxy, sample_announce_request}; use crate::servers::http::v1::handlers::announce::handle_announce; use crate::servers::http::v1::handlers::announce::tests::assert_error_response; #[tokio::test] async fn it_should_fail_when_the_right_most_x_forwarded_for_header_ip_is_not_available() { - let (config, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = - tracker_on_reverse_proxy(); - - let stats_event_sender = Arc::new(stats_event_sender); + let core_tracker_services = initialize_tracker_on_reverse_proxy(); let client_ip_sources = ClientIpSources { right_most_x_forwarded_for: None, @@ -474,11 +454,11 @@ mod tests { }; let response = handle_announce( - &config, - &announce_handler, - &authentication_service, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, &sample_announce_request(), &client_ip_sources, None, @@ -495,20 +475,15 @@ mod tests { mod with_tracker_not_on_reverse_proxy { - use std::sync::Arc; - use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources; - use super::{sample_announce_request, tracker_not_on_reverse_proxy}; + use super::{initialize_tracker_not_on_reverse_proxy, sample_announce_request}; use crate::servers::http::v1::handlers::announce::handle_announce; use crate::servers::http::v1::handlers::announce::tests::assert_error_response; #[tokio::test] async fn it_should_fail_when_the_client_ip_from_the_connection_info_is_not_available() { - let (config, announce_handler, stats_event_sender, whitelist_authorization, authentication_service) = - tracker_not_on_reverse_proxy(); - - let stats_event_sender = Arc::new(stats_event_sender); + let core_tracker_services = initialize_tracker_not_on_reverse_proxy(); let client_ip_sources = ClientIpSources { right_most_x_forwarded_for: None, @@ -516,11 +491,11 @@ mod tests { }; let response = handle_announce( - &config, - &announce_handler, - &authentication_service, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, &sample_announce_request(), &client_ip_sources, None, diff --git a/src/servers/http/v1/handlers/scrape.rs b/src/servers/http/v1/handlers/scrape.rs index c4013d8e9..a197263e8 100644 --- a/src/servers/http/v1/handlers/scrape.rs +++ b/src/servers/http/v1/handlers/scrape.rs @@ -171,137 +171,62 @@ mod tests { use bittorrent_http_protocol::v1::responses; use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources; use bittorrent_primitives::info_hash::InfoHash; - use torrust_tracker_configuration::Core; + use torrust_tracker_configuration::{Configuration, Core}; use torrust_tracker_test_helpers::configuration; - use crate::app_test::initialize_tracker_dependencies; + use crate::core::authentication::key::repository::in_memory::InMemoryKeyRepository; use crate::core::authentication::service::AuthenticationService; use crate::core::scrape_handler::ScrapeHandler; use crate::core::services::statistics; - - #[allow(clippy::type_complexity)] - fn private_tracker() -> ( - Arc, - Arc, - Arc>>, - Arc, - ) { - let config = configuration::ephemeral_private(); - - let ( - _database, - _in_memory_whitelist, - whitelist_authorization, - authentication_service, - in_memory_torrent_repository, - _db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); - - let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics); - - let stats_event_sender = Arc::new(stats_event_sender); - - let core_config = Arc::new(config.core.clone()); - - let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); - - (core_config, scrape_handler, stats_event_sender, authentication_service) + use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; + use crate::core::whitelist::authorization::WhitelistAuthorization; + use crate::core::whitelist::repository::in_memory::InMemoryWhitelist; + + struct CoreTrackerServices { + pub core_config: Arc, + pub scrape_handler: Arc, + pub stats_event_sender: Arc>>, + pub authentication_service: Arc, } - #[allow(clippy::type_complexity)] - fn whitelisted_tracker() -> ( - Arc, - Arc, - Arc>>, - Arc, - ) { - let config = configuration::ephemeral_listed(); - - let ( - _database, - _in_memory_whitelist, - whitelist_authorization, - authentication_service, - in_memory_torrent_repository, - _db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); - - let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics); - - let stats_event_sender = Arc::new(stats_event_sender); - - let core_config = Arc::new(config.core.clone()); - - let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); - - (core_config, scrape_handler, stats_event_sender, authentication_service) + fn initialize_private_tracker() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_private()) } - #[allow(clippy::type_complexity)] - fn tracker_on_reverse_proxy() -> ( - Arc, - Arc, - Arc>>, - Arc, - ) { - let config = configuration::ephemeral_with_reverse_proxy(); - - let ( - _database, - _in_memory_whitelist, - whitelist_authorization, - authentication_service, - in_memory_torrent_repository, - _db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); - - let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics); - - let stats_event_sender = Arc::new(stats_event_sender); - - let core_config = Arc::new(config.core.clone()); - - let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); - - (core_config, scrape_handler, stats_event_sender, authentication_service) + fn initialize_listed_tracker() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_listed()) } - #[allow(clippy::type_complexity)] - fn tracker_not_on_reverse_proxy() -> ( - Arc, - Arc, - Arc>>, - Arc, - ) { - let config = configuration::ephemeral_without_reverse_proxy(); + fn initialize_tracker_on_reverse_proxy() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_with_reverse_proxy()) + } - let ( - _database, - _in_memory_whitelist, - whitelist_authorization, - authentication_service, - in_memory_torrent_repository, - _db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); + fn initialize_tracker_not_on_reverse_proxy() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_without_reverse_proxy()) + } + fn initialize_core_tracker_services(config: &Configuration) -> CoreTrackerServices { + let core_config = Arc::new(config.core.clone()); + let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); + let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone())); + let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default()); + let authentication_service = Arc::new(AuthenticationService::new(&config.core, &in_memory_key_repository)); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics); - let stats_event_sender = Arc::new(stats_event_sender); - - let core_config = Arc::new(config.core.clone()); - let scrape_handler = Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)); - (core_config, scrape_handler, stats_event_sender, authentication_service) + CoreTrackerServices { + core_config, + scrape_handler, + stats_event_sender, + authentication_service, + } } fn sample_scrape_request() -> Scrape { Scrape { - info_hashes: vec!["3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::().unwrap()], // # DevSkim: ignore DS173237 + info_hashes: vec!["3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::().unwrap()], // DevSkim: ignore DS173237 } } @@ -324,22 +249,22 @@ mod tests { use torrust_tracker_primitives::core::ScrapeData; - use super::{private_tracker, sample_client_ip_sources, sample_scrape_request}; + use super::{initialize_private_tracker, sample_client_ip_sources, sample_scrape_request}; use crate::core::authentication; use crate::servers::http::v1::handlers::scrape::handle_scrape; #[tokio::test] async fn it_should_return_zeroed_swarm_metadata_when_the_authentication_key_is_missing() { - let (core_config, scrape_handler, stats_event_sender, authentication_service) = private_tracker(); + let core_tracker_services = initialize_private_tracker(); let scrape_request = sample_scrape_request(); let maybe_key = None; let scrape_data = handle_scrape( - &core_config, - &scrape_handler, - &authentication_service, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.scrape_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.stats_event_sender, &scrape_request, &sample_client_ip_sources(), maybe_key, @@ -354,17 +279,17 @@ mod tests { #[tokio::test] async fn it_should_return_zeroed_swarm_metadata_when_the_authentication_key_is_invalid() { - let (core_config, scrape_handler, stats_event_sender, authentication_service) = private_tracker(); + let core_tracker_services = initialize_private_tracker(); let scrape_request = sample_scrape_request(); let unregistered_key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap(); let maybe_key = Some(unregistered_key); let scrape_data = handle_scrape( - &core_config, - &scrape_handler, - &authentication_service, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.scrape_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.stats_event_sender, &scrape_request, &sample_client_ip_sources(), maybe_key, @@ -382,20 +307,20 @@ mod tests { use torrust_tracker_primitives::core::ScrapeData; - use super::{sample_client_ip_sources, sample_scrape_request, whitelisted_tracker}; + use super::{initialize_listed_tracker, sample_client_ip_sources, sample_scrape_request}; use crate::servers::http::v1::handlers::scrape::handle_scrape; #[tokio::test] async fn it_should_return_zeroed_swarm_metadata_when_the_torrent_is_not_whitelisted() { - let (core_config, scrape_handler, stats_event_sender, authentication_service) = whitelisted_tracker(); + let core_tracker_services = initialize_listed_tracker(); let scrape_request = sample_scrape_request(); let scrape_data = handle_scrape( - &core_config, - &scrape_handler, - &authentication_service, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.scrape_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.stats_event_sender, &scrape_request, &sample_client_ip_sources(), None, @@ -413,13 +338,13 @@ mod tests { use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources; - use super::{sample_scrape_request, tracker_on_reverse_proxy}; + use super::{initialize_tracker_on_reverse_proxy, sample_scrape_request}; use crate::servers::http::v1::handlers::scrape::handle_scrape; use crate::servers::http::v1::handlers::scrape::tests::assert_error_response; #[tokio::test] async fn it_should_fail_when_the_right_most_x_forwarded_for_header_ip_is_not_available() { - let (core_config, scrape_handler, stats_event_sender, authentication_service) = tracker_on_reverse_proxy(); + let core_tracker_services = initialize_tracker_on_reverse_proxy(); let client_ip_sources = ClientIpSources { right_most_x_forwarded_for: None, @@ -427,10 +352,10 @@ mod tests { }; let response = handle_scrape( - &core_config, - &scrape_handler, - &authentication_service, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.scrape_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.stats_event_sender, &sample_scrape_request(), &client_ip_sources, None, @@ -449,13 +374,13 @@ mod tests { use bittorrent_http_protocol::v1::services::peer_ip_resolver::ClientIpSources; - use super::{sample_scrape_request, tracker_not_on_reverse_proxy}; + use super::{initialize_tracker_not_on_reverse_proxy, sample_scrape_request}; use crate::servers::http::v1::handlers::scrape::handle_scrape; use crate::servers::http::v1::handlers::scrape::tests::assert_error_response; #[tokio::test] async fn it_should_fail_when_the_client_ip_from_the_connection_info_is_not_available() { - let (core_config, scrape_handler, stats_event_sender, authentication_service) = tracker_not_on_reverse_proxy(); + let core_tracker_services = initialize_tracker_not_on_reverse_proxy(); let client_ip_sources = ClientIpSources { right_most_x_forwarded_for: None, @@ -463,10 +388,10 @@ mod tests { }; let response = handle_scrape( - &core_config, - &scrape_handler, - &authentication_service, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.scrape_handler, + &core_tracker_services.authentication_service, + &core_tracker_services.stats_event_sender, &sample_scrape_request(), &client_ip_sources, None, diff --git a/src/servers/http/v1/services/announce.rs b/src/servers/http/v1/services/announce.rs index c8c2980c3..e96face6a 100644 --- a/src/servers/http/v1/services/announce.rs +++ b/src/servers/http/v1/services/announce.rs @@ -64,36 +64,38 @@ mod tests { use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch}; use torrust_tracker_test_helpers::configuration; - use crate::app_test::initialize_tracker_dependencies; use crate::core::announce_handler::AnnounceHandler; - use crate::core::services::statistics; + use crate::core::services::{initialize_database, statistics}; use crate::core::statistics::event::sender::Sender; + use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; + use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; - #[allow(clippy::type_complexity)] - fn public_tracker() -> (Arc, Arc, Arc>>) { + struct CoreTrackerServices { + pub core_config: Arc, + pub announce_handler: Arc, + pub stats_event_sender: Arc>>, + } + + fn initialize_core_tracker_services() -> CoreTrackerServices { let config = configuration::ephemeral_public(); - let ( - _database, - _in_memory_whitelist, - _whitelist_authorization, - _authentication_service, - in_memory_torrent_repository, - db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); + let core_config = Arc::new(config.core.clone()); + let database = initialize_database(&config); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); + let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics); let stats_event_sender = Arc::new(stats_event_sender); - let announce_handler = Arc::new(AnnounceHandler::new( &config.core, &in_memory_torrent_repository, &db_torrent_repository, )); - let core_config = Arc::new(config.core.clone()); - - (core_config, announce_handler, stats_event_sender) + CoreTrackerServices { + core_config, + announce_handler, + stats_event_sender, + } } fn sample_peer_using_ipv4() -> peer::Peer { @@ -133,25 +135,21 @@ mod tests { use torrust_tracker_test_helpers::configuration; use super::{sample_peer_using_ipv4, sample_peer_using_ipv6}; - use crate::app_test::initialize_tracker_dependencies; use crate::core::announce_handler::{AnnounceHandler, PeersWanted}; use crate::core::core_tests::sample_info_hash; + use crate::core::services::initialize_database; use crate::core::statistics; + use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; + use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; use crate::servers::http::v1::services::announce::invoke; - use crate::servers::http::v1::services::announce::tests::{public_tracker, sample_peer}; + use crate::servers::http::v1::services::announce::tests::{initialize_core_tracker_services, sample_peer}; fn initialize_announce_handler() -> Arc { let config = configuration::ephemeral(); - let ( - _database, - _in_memory_whitelist, - _whitelist_authorization, - _authentication_service, - in_memory_torrent_repository, - db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); + let database = initialize_database(&config); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); + let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); Arc::new(AnnounceHandler::new( &config.core, @@ -162,13 +160,13 @@ mod tests { #[tokio::test] async fn it_should_return_the_announce_data() { - let (core_config, announce_handler, stats_event_sender) = public_tracker(); + let core_tracker_services = initialize_core_tracker_services(); let mut peer = sample_peer(); let announce_data = invoke( - announce_handler.clone(), - stats_event_sender.clone(), + core_tracker_services.announce_handler.clone(), + core_tracker_services.stats_event_sender.clone(), sample_info_hash(), &mut peer, &PeersWanted::All, @@ -182,7 +180,7 @@ mod tests { complete: 1, incomplete: 0, }, - policy: core_config.announce_policy, + policy: core_tracker_services.core_config.announce_policy, }; assert_eq!(announce_data, expected_announce_data); diff --git a/src/servers/http/v1/services/scrape.rs b/src/servers/http/v1/services/scrape.rs index 6cd7213be..7e65b9442 100644 --- a/src/servers/http/v1/services/scrape.rs +++ b/src/servers/http/v1/services/scrape.rs @@ -80,30 +80,28 @@ mod tests { use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch}; use torrust_tracker_test_helpers::configuration; - use crate::app_test::initialize_tracker_dependencies; use crate::core::announce_handler::AnnounceHandler; use crate::core::core_tests::sample_info_hash; use crate::core::scrape_handler::ScrapeHandler; + use crate::core::services::initialize_database; + use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; + use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; + use crate::core::whitelist::authorization::WhitelistAuthorization; + use crate::core::whitelist::repository::in_memory::InMemoryWhitelist; - fn public_tracker_and_announce_and_scrape_handlers() -> (Arc, Arc) { + fn initialize_announce_and_scrape_handlers_for_public_tracker() -> (Arc, Arc) { let config = configuration::ephemeral_public(); - let ( - _database, - _in_memory_whitelist, - whitelist_authorization, - _authentication_service, - in_memory_torrent_repository, - db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); - + let database = initialize_database(&config); + let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); + let whitelist_authorization = Arc::new(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) @@ -128,15 +126,9 @@ mod tests { fn initialize_scrape_handler() -> Arc { let config = configuration::ephemeral(); - let ( - _database, - _in_memory_whitelist, - whitelist_authorization, - _authentication_service, - in_memory_torrent_repository, - _db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); + let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); + let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone())); + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); Arc::new(ScrapeHandler::new(&whitelist_authorization, &in_memory_torrent_repository)) } @@ -155,8 +147,8 @@ mod tests { use crate::core::statistics; use crate::servers::http::v1::services::scrape::invoke; use crate::servers::http::v1::services::scrape::tests::{ - initialize_scrape_handler, public_tracker_and_announce_and_scrape_handlers, sample_info_hash, sample_info_hashes, - sample_peer, + initialize_announce_and_scrape_handlers_for_public_tracker, initialize_scrape_handler, sample_info_hash, + sample_info_hashes, sample_peer, }; #[tokio::test] @@ -164,7 +156,7 @@ mod tests { let (stats_event_sender, _stats_repository) = crate::core::services::statistics::setup::factory(false); let stats_event_sender = Arc::new(stats_event_sender); - let (announce_handler, scrape_handler) = public_tracker_and_announce_and_scrape_handlers(); + let (announce_handler, scrape_handler) = initialize_announce_and_scrape_handlers_for_public_tracker(); let info_hash = sample_info_hash(); let info_hashes = vec![info_hash]; @@ -239,7 +231,7 @@ mod tests { use crate::core::statistics; use crate::servers::http::v1::services::scrape::fake; use crate::servers::http::v1::services::scrape::tests::{ - public_tracker_and_announce_and_scrape_handlers, sample_info_hash, sample_info_hashes, sample_peer, + initialize_announce_and_scrape_handlers_for_public_tracker, sample_info_hash, sample_info_hashes, sample_peer, }; #[tokio::test] @@ -247,7 +239,7 @@ mod tests { let (stats_event_sender, _stats_repository) = crate::core::services::statistics::setup::factory(false); let stats_event_sender = Arc::new(stats_event_sender); - let (announce_handler, _scrape_handler) = public_tracker_and_announce_and_scrape_handlers(); + let (announce_handler, _scrape_handler) = initialize_announce_and_scrape_handlers_for_public_tracker(); let info_hash = sample_info_hash(); let info_hashes = vec![info_hash]; diff --git a/src/servers/udp/handlers.rs b/src/servers/udp/handlers.rs index 2e753404d..43dc69019 100644 --- a/src/servers/udp/handlers.rs +++ b/src/servers/udp/handlers.rs @@ -498,79 +498,68 @@ mod tests { use torrust_tracker_test_helpers::configuration; use super::gen_remote_fingerprint; - use crate::app_test::initialize_tracker_dependencies; use crate::core::announce_handler::AnnounceHandler; use crate::core::scrape_handler::ScrapeHandler; - use crate::core::services::{initialize_whitelist_manager, statistics}; + use crate::core::services::{initialize_database, statistics}; use crate::core::statistics::event::sender::Sender; use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; + use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; use crate::core::whitelist; - use crate::core::whitelist::manager::WhitelistManager; + use crate::core::whitelist::authorization::WhitelistAuthorization; use crate::core::whitelist::repository::in_memory::InMemoryWhitelist; use crate::CurrentClock; - type TrackerAndDeps = ( - Arc, - Arc, - Arc, - Arc, - Arc>>, - Arc, - Arc, - Arc, - ); - - fn tracker_configuration() -> Configuration { - default_testing_tracker_configuration() + struct CoreTrackerServices { + pub core_config: Arc, + pub announce_handler: Arc, + pub scrape_handler: Arc, + pub in_memory_torrent_repository: Arc, + pub stats_event_sender: Arc>>, + pub in_memory_whitelist: Arc, + pub whitelist_authorization: Arc, } fn default_testing_tracker_configuration() -> Configuration { configuration::ephemeral() } - fn public_tracker() -> TrackerAndDeps { - initialize_tracker_and_deps(&configuration::ephemeral_public()) + fn initialize_core_tracker_services_for_default_tracker_configuration() -> CoreTrackerServices { + initialize_core_tracker_services(&default_testing_tracker_configuration()) } - fn whitelisted_tracker() -> TrackerAndDeps { - initialize_tracker_and_deps(&configuration::ephemeral_listed()) + fn initialize_core_tracker_services_for_public_tracker() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_public()) } - fn initialize_tracker_and_deps(config: &Configuration) -> TrackerAndDeps { - let core_config = Arc::new(config.core.clone()); - - let ( - database, - in_memory_whitelist, - whitelist_authorization, - _authentication_service, - in_memory_torrent_repository, - db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(config); + fn initialize_core_tracker_services_for_listed_tracker() -> CoreTrackerServices { + initialize_core_tracker_services(&configuration::ephemeral_listed()) + } + fn initialize_core_tracker_services(config: &Configuration) -> CoreTrackerServices { + let core_config = Arc::new(config.core.clone()); + let database = initialize_database(config); + let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); + let whitelist_authorization = Arc::new(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 (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics); let stats_event_sender = Arc::new(stats_event_sender); - let whitelist_manager = initialize_whitelist_manager(database.clone(), in_memory_whitelist.clone()); - 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)); - ( + CoreTrackerServices { core_config, announce_handler, scrape_handler, in_memory_torrent_repository, stats_event_sender, in_memory_whitelist, - whitelist_manager, whitelist_authorization, - ) + } } fn sample_ipv4_remote_addr() -> SocketAddr { @@ -667,38 +656,6 @@ mod tests { } } - #[allow(clippy::type_complexity)] - fn test_tracker_factory() -> ( - Arc, - Arc, - Arc, - Arc, - ) { - let config = tracker_configuration(); - - let core_config = Arc::new(config.core.clone()); - - let ( - _database, - _in_memory_whitelist, - whitelist_authorization, - _authentication_service, - in_memory_torrent_repository, - db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); - - 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)); - - (core_config, announce_handler, scrape_handler, whitelist_authorization) - } - mod connect_request { use std::future; @@ -916,23 +873,15 @@ mod tests { use crate::servers::udp::connection_cookie::make; use crate::servers::udp::handlers::tests::announce_request::AnnounceRequestBuilder; use crate::servers::udp::handlers::tests::{ - gen_remote_fingerprint, public_tracker, sample_cookie_valid_range, sample_ipv4_socket_address, sample_issue_time, - test_tracker_factory, TorrentPeerBuilder, + gen_remote_fingerprint, initialize_core_tracker_services_for_default_tracker_configuration, + initialize_core_tracker_services_for_public_tracker, sample_cookie_valid_range, sample_ipv4_socket_address, + sample_issue_time, TorrentPeerBuilder, }; use crate::servers::udp::handlers::{handle_announce, AnnounceResponseFixedData}; #[tokio::test] async fn an_announced_peer_should_be_added_to_the_tracker() { - let ( - core_config, - announce_handler, - _scrape_handler, - in_memory_torrent_repository, - stats_event_sender, - _in_memory_whitelist, - _whitelist_manager, - whitelist_authorization, - ) = public_tracker(); + let core_tracker_services = initialize_core_tracker_services_for_public_tracker(); let client_ip = Ipv4Addr::new(126, 0, 0, 1); let client_port = 8080; @@ -952,16 +901,18 @@ mod tests { handle_announce( remote_addr, &request, - &core_config, - &announce_handler, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, sample_cookie_valid_range(), ) .await .unwrap(); - let peers = in_memory_torrent_repository.get_torrent_peers(&info_hash.0.into()); + let peers = core_tracker_services + .in_memory_torrent_repository + .get_torrent_peers(&info_hash.0.into()); let expected_peer = TorrentPeerBuilder::new() .with_peer_id(peer_id) @@ -973,16 +924,7 @@ mod tests { #[tokio::test] async fn the_announced_peer_should_not_be_included_in_the_response() { - let ( - core_config, - announce_handler, - _scrape_handler, - _in_memory_torrent_repository, - stats_event_sender, - _in_memory_whitelist, - _whitelist_manager, - whitelist_authorization, - ) = public_tracker(); + let core_tracker_services = initialize_core_tracker_services_for_public_tracker(); let remote_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080); @@ -993,10 +935,10 @@ mod tests { let response = handle_announce( remote_addr, &request, - &core_config, - &announce_handler, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, sample_cookie_valid_range(), ) .await @@ -1023,16 +965,7 @@ mod tests { // From the BEP 15 (https://www.bittorrent.org/beps/bep_0015.html): // "Do note that most trackers will only honor the IP address field under limited circumstances." - let ( - core_config, - announce_handler, - _scrape_handler, - in_memory_torrent_repository, - stats_event_sender, - _in_memory_whitelist, - _whitelist_manager, - whitelist_authorization, - ) = public_tracker(); + let core_tracker_services = initialize_core_tracker_services_for_public_tracker(); let info_hash = AquaticInfoHash([0u8; 20]); let peer_id = AquaticPeerId([255u8; 20]); @@ -1055,16 +988,18 @@ mod tests { handle_announce( remote_addr, &request, - &core_config, - &announce_handler, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, sample_cookie_valid_range(), ) .await .unwrap(); - let peers = in_memory_torrent_repository.get_torrent_peers(&info_hash.0.into()); + let peers = core_tracker_services + .in_memory_torrent_repository + .get_torrent_peers(&info_hash.0.into()); assert_eq!(peers[0].peer_addr, SocketAddr::new(IpAddr::V4(remote_client_ip), client_port)); } @@ -1113,21 +1048,16 @@ mod tests { #[tokio::test] async fn when_the_announce_request_comes_from_a_client_using_ipv4_the_response_should_not_include_peers_using_ipv6() { - let ( - core_config, - announce_handler, - _scrape_handler, - in_memory_torrent_repository, - _stats_event_sender, - _in_memory_whitelist, - _whitelist_manager, - whitelist_authorization, - ) = public_tracker(); - - add_a_torrent_peer_using_ipv6(&in_memory_torrent_repository); - - let response = - announce_a_new_peer_using_ipv4(core_config.clone(), announce_handler.clone(), whitelist_authorization).await; + let core_tracker_services = initialize_core_tracker_services_for_public_tracker(); + + add_a_torrent_peer_using_ipv6(&core_tracker_services.in_memory_torrent_repository); + + let response = announce_a_new_peer_using_ipv4( + core_tracker_services.core_config.clone(), + core_tracker_services.announce_handler.clone(), + core_tracker_services.whitelist_authorization, + ) + .await; // The response should not contain the peer using IPV6 let peers: Option>> = match response { @@ -1149,14 +1079,14 @@ mod tests { let stats_event_sender: Arc>> = Arc::new(Some(Box::new(stats_event_sender_mock))); - let (core_config, announce_handler, _scrape_handler, whitelist_authorization) = test_tracker_factory(); + let core_tracker_services = initialize_core_tracker_services_for_default_tracker_configuration(); handle_announce( sample_ipv4_socket_address(), &AnnounceRequestBuilder::default().into(), - &core_config, - &announce_handler, - &whitelist_authorization, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.whitelist_authorization, &stats_event_sender, sample_cookie_valid_range(), ) @@ -1174,21 +1104,13 @@ mod tests { use crate::servers::udp::handlers::handle_announce; use crate::servers::udp::handlers::tests::announce_request::AnnounceRequestBuilder; use crate::servers::udp::handlers::tests::{ - gen_remote_fingerprint, public_tracker, sample_cookie_valid_range, sample_issue_time, TorrentPeerBuilder, + gen_remote_fingerprint, initialize_core_tracker_services_for_public_tracker, sample_cookie_valid_range, + sample_issue_time, TorrentPeerBuilder, }; #[tokio::test] async fn the_peer_ip_should_be_changed_to_the_external_ip_in_the_tracker_configuration_if_defined() { - let ( - core_config, - announce_handler, - _scrape_handler, - in_memory_torrent_repository, - stats_event_sender, - _in_memory_whitelist, - _whitelist_manager, - whitelist_authorization, - ) = public_tracker(); + let core_tracker_services = initialize_core_tracker_services_for_public_tracker(); let client_ip = Ipv4Addr::new(127, 0, 0, 1); let client_port = 8080; @@ -1208,18 +1130,20 @@ mod tests { handle_announce( remote_addr, &request, - &core_config, - &announce_handler, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, sample_cookie_valid_range(), ) .await .unwrap(); - let peers = in_memory_torrent_repository.get_torrent_peers(&info_hash.0.into()); + let peers = core_tracker_services + .in_memory_torrent_repository + .get_torrent_peers(&info_hash.0.into()); - let external_ip_in_tracker_configuration = core_config.net.external_ip.unwrap(); + let external_ip_in_tracker_configuration = core_tracker_services.core_config.net.external_ip.unwrap(); let expected_peer = TorrentPeerBuilder::new() .with_peer_id(peer_id) @@ -1250,23 +1174,15 @@ mod tests { use crate::servers::udp::connection_cookie::make; use crate::servers::udp::handlers::tests::announce_request::AnnounceRequestBuilder; use crate::servers::udp::handlers::tests::{ - gen_remote_fingerprint, public_tracker, sample_cookie_valid_range, sample_ipv6_remote_addr, sample_issue_time, - test_tracker_factory, TorrentPeerBuilder, + gen_remote_fingerprint, initialize_core_tracker_services_for_default_tracker_configuration, + initialize_core_tracker_services_for_public_tracker, sample_cookie_valid_range, sample_ipv6_remote_addr, + sample_issue_time, TorrentPeerBuilder, }; use crate::servers::udp::handlers::{handle_announce, AnnounceResponseFixedData}; #[tokio::test] async fn an_announced_peer_should_be_added_to_the_tracker() { - let ( - core_config, - announce_handler, - _scrape_handler, - in_memory_torrent_repository, - stats_event_sender, - _in_memory_whitelist, - _whitelist_manager, - whitelist_authorization, - ) = public_tracker(); + let core_tracker_services = initialize_core_tracker_services_for_public_tracker(); let client_ip_v4 = Ipv4Addr::new(126, 0, 0, 1); let client_ip_v6 = client_ip_v4.to_ipv6_compatible(); @@ -1287,16 +1203,18 @@ mod tests { handle_announce( remote_addr, &request, - &core_config, - &announce_handler, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, sample_cookie_valid_range(), ) .await .unwrap(); - let peers = in_memory_torrent_repository.get_torrent_peers(&info_hash.0.into()); + let peers = core_tracker_services + .in_memory_torrent_repository + .get_torrent_peers(&info_hash.0.into()); let expected_peer = TorrentPeerBuilder::new() .with_peer_id(peer_id) @@ -1308,16 +1226,7 @@ mod tests { #[tokio::test] async fn the_announced_peer_should_not_be_included_in_the_response() { - let ( - core_config, - announce_handler, - _scrape_handler, - _in_memory_torrent_repository, - stats_event_sender, - _in_memory_whitelist, - _whitelist_manager, - whitelist_authorization, - ) = public_tracker(); + let core_tracker_services = initialize_core_tracker_services_for_public_tracker(); let client_ip_v4 = Ipv4Addr::new(126, 0, 0, 1); let client_ip_v6 = client_ip_v4.to_ipv6_compatible(); @@ -1331,10 +1240,10 @@ mod tests { let response = handle_announce( remote_addr, &request, - &core_config, - &announce_handler, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, sample_cookie_valid_range(), ) .await @@ -1361,16 +1270,7 @@ mod tests { // From the BEP 15 (https://www.bittorrent.org/beps/bep_0015.html): // "Do note that most trackers will only honor the IP address field under limited circumstances." - let ( - core_config, - announce_handler, - _scrape_handler, - in_memory_torrent_repository, - stats_event_sender, - _in_memory_whitelist, - _whitelist_manager, - whitelist_authorization, - ) = public_tracker(); + let core_tracker_services = initialize_core_tracker_services_for_public_tracker(); let info_hash = AquaticInfoHash([0u8; 20]); let peer_id = AquaticPeerId([255u8; 20]); @@ -1393,16 +1293,18 @@ mod tests { handle_announce( remote_addr, &request, - &core_config, - &announce_handler, - &whitelist_authorization, - &stats_event_sender, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.whitelist_authorization, + &core_tracker_services.stats_event_sender, sample_cookie_valid_range(), ) .await .unwrap(); - let peers = in_memory_torrent_repository.get_torrent_peers(&info_hash.0.into()); + let peers = core_tracker_services + .in_memory_torrent_repository + .get_torrent_peers(&info_hash.0.into()); // When using IPv6 the tracker converts the remote client ip into a IPv4 address assert_eq!(peers[0].peer_addr, SocketAddr::new(IpAddr::V6(remote_client_ip), client_port)); @@ -1454,21 +1356,16 @@ mod tests { #[tokio::test] async fn when_the_announce_request_comes_from_a_client_using_ipv6_the_response_should_not_include_peers_using_ipv4() { - let ( - core_config, - announce_handler, - _scrape_handler, - in_memory_torrent_repository, - _stats_event_sender, - _in_memory_whitelist, - _whitelist_manager, - whitelist_authorization, - ) = public_tracker(); - - add_a_torrent_peer_using_ipv4(&in_memory_torrent_repository); - - let response = - announce_a_new_peer_using_ipv6(core_config.clone(), announce_handler.clone(), whitelist_authorization).await; + let core_tracker_services = initialize_core_tracker_services_for_public_tracker(); + + add_a_torrent_peer_using_ipv4(&core_tracker_services.in_memory_torrent_repository); + + let response = announce_a_new_peer_using_ipv6( + core_tracker_services.core_config.clone(), + core_tracker_services.announce_handler.clone(), + core_tracker_services.whitelist_authorization, + ) + .await; // The response should not contain the peer using IPV4 let peers: Option>> = match response { @@ -1490,7 +1387,7 @@ mod tests { let stats_event_sender: Arc>> = Arc::new(Some(Box::new(stats_event_sender_mock))); - let (core_config, announce_handler, _scrape_handler, whitelist_authorization) = test_tracker_factory(); + let core_tracker_services = initialize_core_tracker_services_for_default_tracker_configuration(); let remote_addr = sample_ipv6_remote_addr(); @@ -1501,9 +1398,9 @@ mod tests { handle_announce( remote_addr, &announce_request, - &core_config, - &announce_handler, - &whitelist_authorization, + &core_tracker_services.core_config, + &core_tracker_services.announce_handler, + &core_tracker_services.whitelist_authorization, &stats_event_sender, sample_cookie_valid_range(), ) @@ -1519,9 +1416,13 @@ mod tests { use aquatic_udp_protocol::{InfoHash as AquaticInfoHash, PeerId as AquaticPeerId}; use mockall::predicate::eq; - use crate::app_test::initialize_tracker_dependencies; use crate::core::announce_handler::AnnounceHandler; + use crate::core::services::initialize_database; use crate::core::statistics; + use crate::core::torrent::repository::in_memory::InMemoryTorrentRepository; + use crate::core::torrent::repository::persisted::DatabasePersistentTorrentRepository; + use crate::core::whitelist::authorization::WhitelistAuthorization; + use crate::core::whitelist::repository::in_memory::InMemoryWhitelist; use crate::servers::udp::connection_cookie::make; use crate::servers::udp::handlers::handle_announce; use crate::servers::udp::handlers::tests::announce_request::AnnounceRequestBuilder; @@ -1533,15 +1434,12 @@ mod tests { async fn the_peer_ip_should_be_changed_to_the_external_ip_in_the_tracker_configuration() { let config = Arc::new(TrackerConfigurationBuilder::default().with_external_ip("::126.0.0.1").into()); - let ( - _database, - _in_memory_whitelist, - whitelist_authorization, - _authentication_service, - in_memory_torrent_repository, - db_torrent_repository, - _torrents_manager, - ) = initialize_tracker_dependencies(&config); + let database = initialize_database(&config); + let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); + let whitelist_authorization = + Arc::new(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 mut stats_event_sender_mock = statistics::event::sender::MockSender::new(); stats_event_sender_mock @@ -1625,7 +1523,8 @@ mod tests { use crate::servers::udp::connection_cookie::make; use crate::servers::udp::handlers::handle_scrape; use crate::servers::udp::handlers::tests::{ - public_tracker, sample_cookie_valid_range, sample_ipv4_remote_addr, sample_issue_time, + initialize_core_tracker_services_for_public_tracker, sample_cookie_valid_range, sample_ipv4_remote_addr, + sample_issue_time, }; fn zeroed_torrent_statistics() -> TorrentScrapeStatistics { @@ -1638,16 +1537,7 @@ mod tests { #[tokio::test] async fn should_return_no_stats_when_the_tracker_does_not_have_any_torrent() { - let ( - _core_config, - _announce_handler, - scrape_handler, - _in_memory_torrent_repository, - stats_event_sender, - _in_memory_whitelist, - _whitelist_manager, - _whitelist_authorization, - ) = public_tracker(); + let core_tracker_services = initialize_core_tracker_services_for_public_tracker(); let remote_addr = sample_ipv4_remote_addr(); @@ -1663,8 +1553,8 @@ mod tests { let response = handle_scrape( remote_addr, &request, - &scrape_handler, - &stats_event_sender, + &core_tracker_services.scrape_handler, + &core_tracker_services.stats_event_sender, sample_cookie_valid_range(), ) .await @@ -1742,24 +1632,19 @@ mod tests { mod with_a_public_tracker { use aquatic_udp_protocol::{NumberOfDownloads, NumberOfPeers, TorrentScrapeStatistics}; - use crate::servers::udp::handlers::tests::public_tracker; + use crate::servers::udp::handlers::tests::initialize_core_tracker_services_for_public_tracker; use crate::servers::udp::handlers::tests::scrape_request::{add_a_sample_seeder_and_scrape, match_scrape_response}; #[tokio::test] async fn should_return_torrent_statistics_when_the_tracker_has_the_requested_torrent() { - let ( - _core_config, - _announce_handler, - scrape_handler, - in_memory_torrent_repository, - _stats_event_sender, - _in_memory_whitelist, - _whitelist_manager, - _whitelist_authorization, - ) = public_tracker(); + let core_tracker_services = initialize_core_tracker_services_for_public_tracker(); let torrent_stats = match_scrape_response( - add_a_sample_seeder_and_scrape(in_memory_torrent_repository.clone(), scrape_handler.clone()).await, + add_a_sample_seeder_and_scrape( + core_tracker_services.in_memory_torrent_repository.clone(), + core_tracker_services.scrape_handler.clone(), + ) + .await, ); let expected_torrent_stats = vec![TorrentScrapeStatistics { @@ -1779,27 +1664,25 @@ mod tests { use crate::servers::udp::handlers::tests::scrape_request::{ add_a_seeder, build_scrape_request, match_scrape_response, zeroed_torrent_statistics, }; - use crate::servers::udp::handlers::tests::{sample_cookie_valid_range, sample_ipv4_remote_addr, whitelisted_tracker}; + use crate::servers::udp::handlers::tests::{ + initialize_core_tracker_services_for_listed_tracker, sample_cookie_valid_range, sample_ipv4_remote_addr, + }; #[tokio::test] async fn should_return_the_torrent_statistics_when_the_requested_torrent_is_whitelisted() { - let ( - _core_config, - _announce_handler, - scrape_handler, - in_memory_torrent_repository, - stats_event_sender, - in_memory_whitelist, - _whitelist_manager, - _whitelist_authorization, - ) = whitelisted_tracker(); + let core_tracker_services = initialize_core_tracker_services_for_listed_tracker(); let remote_addr = sample_ipv4_remote_addr(); let info_hash = InfoHash([0u8; 20]); - add_a_seeder(in_memory_torrent_repository.clone(), &remote_addr, &info_hash).await; + add_a_seeder( + core_tracker_services.in_memory_torrent_repository.clone(), + &remote_addr, + &info_hash, + ) + .await; - in_memory_whitelist.add(&info_hash.0.into()).await; + core_tracker_services.in_memory_whitelist.add(&info_hash.0.into()).await; let request = build_scrape_request(&remote_addr, &info_hash); @@ -1807,8 +1690,8 @@ mod tests { handle_scrape( remote_addr, &request, - &scrape_handler, - &stats_event_sender, + &core_tracker_services.scrape_handler, + &core_tracker_services.stats_event_sender, sample_cookie_valid_range(), ) .await @@ -1827,21 +1710,17 @@ mod tests { #[tokio::test] async fn should_return_zeroed_statistics_when_the_requested_torrent_is_not_whitelisted() { - let ( - _core_config, - _announce_handler, - scrape_handler, - in_memory_torrent_repository, - stats_event_sender, - _in_memory_whitelist, - _whitelist_manager, - _whitelist_authorization, - ) = whitelisted_tracker(); + let core_tracker_services = initialize_core_tracker_services_for_listed_tracker(); let remote_addr = sample_ipv4_remote_addr(); let info_hash = InfoHash([0u8; 20]); - add_a_seeder(in_memory_torrent_repository.clone(), &remote_addr, &info_hash).await; + add_a_seeder( + core_tracker_services.in_memory_torrent_repository.clone(), + &remote_addr, + &info_hash, + ) + .await; let request = build_scrape_request(&remote_addr, &info_hash); @@ -1849,8 +1728,8 @@ mod tests { handle_scrape( remote_addr, &request, - &scrape_handler, - &stats_event_sender, + &core_tracker_services.scrape_handler, + &core_tracker_services.stats_event_sender, sample_cookie_valid_range(), ) .await @@ -1885,7 +1764,8 @@ mod tests { use crate::core::statistics; use crate::servers::udp::handlers::handle_scrape; use crate::servers::udp::handlers::tests::{ - sample_cookie_valid_range, sample_ipv4_remote_addr, test_tracker_factory, + initialize_core_tracker_services_for_default_tracker_configuration, sample_cookie_valid_range, + sample_ipv4_remote_addr, }; #[tokio::test] @@ -1901,12 +1781,12 @@ mod tests { let remote_addr = sample_ipv4_remote_addr(); - let (_core_config, _announce_handler, scrape_handler, _whitelist_authorization) = test_tracker_factory(); + let core_tracker_services = initialize_core_tracker_services_for_default_tracker_configuration(); handle_scrape( remote_addr, &sample_scrape_request(&remote_addr), - &scrape_handler, + &core_tracker_services.scrape_handler, &stats_event_sender, sample_cookie_valid_range(), ) @@ -1925,7 +1805,8 @@ mod tests { use crate::core::statistics; use crate::servers::udp::handlers::handle_scrape; use crate::servers::udp::handlers::tests::{ - sample_cookie_valid_range, sample_ipv6_remote_addr, test_tracker_factory, + initialize_core_tracker_services_for_default_tracker_configuration, sample_cookie_valid_range, + sample_ipv6_remote_addr, }; #[tokio::test] @@ -1941,12 +1822,12 @@ mod tests { let remote_addr = sample_ipv6_remote_addr(); - let (_core_config, _announce_handler, scrape_handler, _whitelist_authorization) = test_tracker_factory(); + let core_tracker_services = initialize_core_tracker_services_for_default_tracker_configuration(); handle_scrape( remote_addr, &sample_scrape_request(&remote_addr), - &scrape_handler, + &core_tracker_services.scrape_handler, &stats_event_sender, sample_cookie_valid_range(), )