-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: [#1182] move structs to new mods in whitelist
- Loading branch information
1 parent
07f53a4
commit cc2bc7b
Showing
4 changed files
with
164 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use bittorrent_primitives::info_hash::InfoHash; | ||
|
||
/// The in-memory list of allowed torrents. | ||
#[derive(Debug, Default)] | ||
pub struct InMemoryWhitelist { | ||
/// The list of allowed torrents. | ||
whitelist: tokio::sync::RwLock<std::collections::HashSet<InfoHash>>, | ||
} | ||
|
||
impl InMemoryWhitelist { | ||
/// It adds a torrent from the whitelist in memory. | ||
pub async fn add(&self, info_hash: &InfoHash) -> bool { | ||
self.whitelist.write().await.insert(*info_hash) | ||
} | ||
|
||
/// It removes a torrent from the whitelist in memory. | ||
pub async fn remove(&self, info_hash: &InfoHash) -> bool { | ||
self.whitelist.write().await.remove(info_hash) | ||
} | ||
|
||
/// It checks if it contains an info-hash. | ||
pub async fn contains(&self, info_hash: &InfoHash) -> bool { | ||
self.whitelist.read().await.contains(info_hash) | ||
} | ||
|
||
/// It clears the whitelist. | ||
pub async fn clear(&self) { | ||
let mut whitelist = self.whitelist.write().await; | ||
whitelist.clear(); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use bittorrent_primitives::info_hash::InfoHash; | ||
|
||
use crate::core::whitelist::in_memory::InMemoryWhitelist; | ||
|
||
fn sample_info_hash() -> InfoHash { | ||
"3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0".parse::<InfoHash>().unwrap() // # DevSkim: ignore DS173237 | ||
} | ||
|
||
#[tokio::test] | ||
async fn should_allow_adding_a_new_torrent_to_the_whitelist() { | ||
let info_hash = sample_info_hash(); | ||
|
||
let whitelist = InMemoryWhitelist::default(); | ||
|
||
whitelist.add(&info_hash).await; | ||
|
||
assert!(whitelist.contains(&info_hash).await); | ||
} | ||
|
||
#[tokio::test] | ||
async fn should_allow_removing_a_new_torrent_to_the_whitelist() { | ||
let info_hash = sample_info_hash(); | ||
|
||
let whitelist = InMemoryWhitelist::default(); | ||
|
||
whitelist.add(&info_hash).await; | ||
whitelist.remove(&sample_info_hash()).await; | ||
|
||
assert!(!whitelist.contains(&info_hash).await); | ||
} | ||
|
||
#[tokio::test] | ||
async fn should_allow_clearing_the_whitelist() { | ||
let info_hash = sample_info_hash(); | ||
|
||
let whitelist = InMemoryWhitelist::default(); | ||
|
||
whitelist.add(&info_hash).await; | ||
whitelist.clear().await; | ||
|
||
assert!(!whitelist.contains(&info_hash).await); | ||
} | ||
|
||
#[tokio::test] | ||
async fn should_allow_checking_if_an_infohash_is_whitelisted() { | ||
let info_hash = sample_info_hash(); | ||
|
||
let whitelist = InMemoryWhitelist::default(); | ||
|
||
whitelist.add(&info_hash).await; | ||
|
||
assert!(whitelist.contains(&info_hash).await); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::sync::Arc; | ||
|
||
use bittorrent_primitives::info_hash::InfoHash; | ||
|
||
use super::databases::{self, Database}; | ||
|
||
/// The persisted list of allowed torrents. | ||
pub struct DatabaseWhitelist { | ||
/// A database driver implementation: [`Sqlite3`](crate::core::databases::sqlite) | ||
/// or [`MySQL`](crate::core::databases::mysql) | ||
database: Arc<Box<dyn Database>>, | ||
} | ||
|
||
impl DatabaseWhitelist { | ||
#[must_use] | ||
pub fn new(database: Arc<Box<dyn Database>>) -> Self { | ||
Self { database } | ||
} | ||
|
||
/// It adds a torrent to the whitelist if it has not been whitelisted previously | ||
/// | ||
/// # 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> { | ||
let is_whitelisted = self.database.is_info_hash_whitelisted(*info_hash)?; | ||
|
||
if is_whitelisted { | ||
return Ok(()); | ||
} | ||
|
||
self.database.add_info_hash_to_whitelist(*info_hash)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// It removes a torrent from the whitelist in the database. | ||
/// | ||
/// # 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> { | ||
let is_whitelisted = self.database.is_info_hash_whitelisted(*info_hash)?; | ||
|
||
if !is_whitelisted { | ||
return Ok(()); | ||
} | ||
|
||
self.database.remove_info_hash_from_whitelist(*info_hash)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
/// It loads the whitelist from the database. | ||
/// | ||
/// # 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<Vec<InfoHash>, databases::error::Error> { | ||
self.database.load_whitelist() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters