Skip to content

Commit 7fb92b5

Browse files
committed
test(tracker): [#207] add tests for whitelist in Tracker
1 parent af949af commit 7fb92b5

File tree

4 files changed

+116
-13
lines changed

4 files changed

+116
-13
lines changed

src/apis/handlers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub async fn remove_torrent_from_whitelist_handler(
8686
}
8787

8888
pub async fn reload_whitelist_handler(State(tracker): State<Arc<Tracker>>) -> Response {
89-
match tracker.load_whitelist().await {
89+
match tracker.load_whitelist_from_database().await {
9090
Ok(_) => ok_response(),
9191
Err(e) => failed_to_reload_whitelist_response(e),
9292
}
@@ -117,7 +117,7 @@ pub async fn delete_auth_key_handler(
117117
}
118118

119119
pub async fn reload_keys_handler(State(tracker): State<Arc<Tracker>>) -> Response {
120-
match tracker.load_keys().await {
120+
match tracker.load_keys_from_database().await {
121121
Ok(_) => ok_response(),
122122
Err(e) => failed_to_reload_keys_response(e),
123123
}

src/setup.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ pub async fn setup(config: &Configuration, tracker: Arc<tracker::Tracker>) -> Ve
1616

1717
// Load peer keys
1818
if tracker.is_private() {
19-
tracker.load_keys().await.expect("Could not retrieve keys from database.");
19+
tracker
20+
.load_keys_from_database()
21+
.await
22+
.expect("Could not retrieve keys from database.");
2023
}
2124

2225
// Load whitelisted torrents
2326
if tracker.is_whitelisted() {
2427
tracker
25-
.load_whitelist()
28+
.load_whitelist_from_database()
2629
.await
2730
.expect("Could not load whitelist from database.");
2831
}

src/tracker/mod.rs

Lines changed: 106 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ pub struct Tracker {
3838

3939
#[derive(Debug, PartialEq, Default)]
4040
pub struct TorrentsMetrics {
41+
// code-review: consider using `SwamStats` for
42+
// `seeders`, `completed`, and `leechers` attributes.
43+
// pub swam_stats: SwamStats;
4144
pub seeders: u64,
4245
pub completed: u64,
4346
pub leechers: u64,
@@ -223,7 +226,7 @@ impl Tracker {
223226
/// # Errors
224227
///
225228
/// Will return a `database::Error` if unable to `load_keys` from the database.
226-
pub async fn load_keys(&self) -> Result<(), databases::error::Error> {
229+
pub async fn load_keys_from_database(&self) -> Result<(), databases::error::Error> {
227230
let keys_from_database = self.database.load_keys().await?;
228231
let mut keys = self.keys.write().await;
229232

@@ -301,7 +304,7 @@ impl Tracker {
301304
/// # Errors
302305
///
303306
/// Will return a `database::Error` if unable to load the list whitelisted `info_hash`s from the database.
304-
pub async fn load_whitelist(&self) -> Result<(), databases::error::Error> {
307+
pub async fn load_whitelist_from_database(&self) -> Result<(), databases::error::Error> {
305308
let whitelisted_torrents_from_database = self.database.load_whitelist().await?;
306309
let mut whitelist = self.whitelist.write().await;
307310

@@ -402,7 +405,7 @@ impl Tracker {
402405
/// # Errors
403406
///
404407
/// Will return a `database::Error` if unable to load the list of `persistent_torrents` from the database.
405-
pub async fn load_persistent_torrents(&self) -> Result<(), databases::error::Error> {
408+
pub async fn load_torrents_from_database(&self) -> Result<(), databases::error::Error> {
406409
let persistent_torrents = self.database.load_persistent_torrents().await?;
407410

408411
let mut torrents = self.torrents.write().await;
@@ -700,6 +703,55 @@ mod tests {
700703
);
701704
}
702705

706+
#[tokio::test]
707+
async fn it_should_return_all_the_peers_for_a_given_torrent() {
708+
let tracker = public_tracker();
709+
710+
let info_hash = sample_info_hash();
711+
let peer = sample_peer();
712+
713+
tracker.update_torrent_with_peer_and_get_stats(&info_hash, &peer).await;
714+
715+
let peers = tracker.get_all_torrent_peers(&info_hash).await;
716+
717+
assert_eq!(peers, vec![peer]);
718+
}
719+
720+
#[tokio::test]
721+
async fn it_should_return_all_the_peers_for_a_given_torrent_excluding_a_given_peer() {
722+
let tracker = public_tracker();
723+
724+
let info_hash = sample_info_hash();
725+
let peer = sample_peer();
726+
727+
tracker.update_torrent_with_peer_and_get_stats(&info_hash, &peer).await;
728+
729+
let peers = tracker.get_peers_for_peer(&info_hash, &peer).await;
730+
731+
assert_eq!(peers, vec![]);
732+
}
733+
734+
#[tokio::test]
735+
async fn it_should_return_the_torrent_metrics() {
736+
let tracker = public_tracker();
737+
738+
tracker
739+
.update_torrent_with_peer_and_get_stats(&sample_info_hash(), &leecher())
740+
.await;
741+
742+
let torrent_metrics = tracker.get_torrents_metrics().await;
743+
744+
assert_eq!(
745+
torrent_metrics,
746+
TorrentsMetrics {
747+
seeders: 0,
748+
completed: 0,
749+
leechers: 1,
750+
torrents: 1,
751+
}
752+
);
753+
}
754+
703755
mod for_all_config_modes {
704756

705757
mod handling_an_announce_request {
@@ -984,6 +1036,55 @@ mod tests {
9841036
}
9851037
}
9861038

1039+
mod handling_the_torrent_whitelist {
1040+
use crate::tracker::tests::the_tracker::{sample_info_hash, whitelisted_tracker};
1041+
1042+
#[tokio::test]
1043+
async fn it_should_add_a_torrent_to_the_whitelist() {
1044+
let tracker = whitelisted_tracker();
1045+
1046+
let info_hash = sample_info_hash();
1047+
1048+
tracker.add_torrent_to_whitelist(&info_hash).await.unwrap();
1049+
1050+
assert!(tracker.is_info_hash_whitelisted(&info_hash).await);
1051+
}
1052+
1053+
#[tokio::test]
1054+
async fn it_should_remove_a_torrent_from_the_whitelist() {
1055+
let tracker = whitelisted_tracker();
1056+
1057+
let info_hash = sample_info_hash();
1058+
1059+
tracker.add_torrent_to_whitelist(&info_hash).await.unwrap();
1060+
1061+
tracker.remove_torrent_from_whitelist(&info_hash).await.unwrap();
1062+
1063+
assert!(!tracker.is_info_hash_whitelisted(&info_hash).await);
1064+
}
1065+
1066+
mod persistence {
1067+
use crate::tracker::tests::the_tracker::{sample_info_hash, whitelisted_tracker};
1068+
1069+
#[tokio::test]
1070+
async fn it_should_load_the_whitelist_from_the_database() {
1071+
let tracker = whitelisted_tracker();
1072+
1073+
let info_hash = sample_info_hash();
1074+
1075+
tracker.add_torrent_to_whitelist(&info_hash).await.unwrap();
1076+
1077+
// Remove torrent from the in-memory whitelist
1078+
tracker.whitelist.write().await.remove(&info_hash);
1079+
assert!(!tracker.is_info_hash_whitelisted(&info_hash).await);
1080+
1081+
tracker.load_whitelist_from_database().await.unwrap();
1082+
1083+
assert!(tracker.is_info_hash_whitelisted(&info_hash).await);
1084+
}
1085+
}
1086+
}
1087+
9871088
mod handling_an_announce_request {}
9881089

9891090
mod handling_an_scrape_request {
@@ -1112,7 +1213,7 @@ mod tests {
11121213
// Remove the newly generated key in memory
11131214
tracker.keys.write().await.remove(&key.id());
11141215

1115-
let result = tracker.load_keys().await;
1216+
let result = tracker.load_keys_from_database().await;
11161217

11171218
assert!(result.is_ok());
11181219
assert!(tracker.verify_auth_key(&key.id()).await.is_ok());
@@ -1152,13 +1253,10 @@ mod tests {
11521253
let swarm_stats = tracker.update_torrent_with_peer_and_get_stats(&info_hash, &peer).await;
11531254
assert_eq!(swarm_stats.completed, 1);
11541255

1155-
let torrents = tracker.get_all_torrent_peers(&info_hash).await;
1156-
assert_eq!(torrents.len(), 1);
1157-
11581256
// Remove the newly updated torrent from memory
11591257
tracker.torrents.write().await.remove(&info_hash);
11601258

1161-
tracker.load_persistent_torrents().await.unwrap();
1259+
tracker.load_torrents_from_database().await.unwrap();
11621260

11631261
let torrents = tracker.get_torrents().await;
11641262
assert!(torrents.contains_key(&info_hash));

src/tracker/torrent.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub struct Entry {
1414
pub completed: u32,
1515
}
1616

17+
/// Swarm statistics for one torrent.
1718
/// Swarm metadata dictionary in the scrape response.
1819
/// BEP 48: <https://www.bittorrent.org/beps/bep_0048.html>
1920
#[derive(Debug, PartialEq, Default)]
@@ -30,7 +31,8 @@ impl SwarmMetadata {
3031
}
3132
}
3233

33-
/// Swarm statistics. Alternative struct for swarm metadata in scrape response.
34+
/// Swarm statistics for one torrent.
35+
/// Alternative struct for swarm metadata in scrape response.
3436
#[derive(Debug, PartialEq, Default)]
3537
pub struct SwamStats {
3638
pub completed: u32, // The number of peers that have ever completed downloading

0 commit comments

Comments
 (0)