Skip to content

Commit b9188c7

Browse files
committed
test: [torrust#1251] add tests for whitelist is DB drivers
1 parent 613efb2 commit b9188c7

File tree

2 files changed

+61
-12
lines changed

2 files changed

+61
-12
lines changed

packages/tracker-core/src/core_tests.rs

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::sync::Arc;
44

55
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId};
66
use bittorrent_primitives::info_hash::InfoHash;
7+
use rand::Rng;
78
use torrust_tracker_configuration::Configuration;
89
#[cfg(test)]
910
use torrust_tracker_configuration::Core;
@@ -20,6 +21,16 @@ use super::torrent::repository::persisted::DatabasePersistentTorrentRepository;
2021
use super::whitelist::repository::in_memory::InMemoryWhitelist;
2122
use super::whitelist::{self};
2223

24+
/// Generates a random `InfoHash`.
25+
#[must_use]
26+
pub fn random_info_hash() -> InfoHash {
27+
let mut rng = rand::rng();
28+
let mut random_bytes = [0u8; 20];
29+
rng.fill(&mut random_bytes);
30+
31+
InfoHash::from_bytes(&random_bytes)
32+
}
33+
2334
/// # Panics
2435
///
2536
/// Will panic if the string representation of the info hash is not a valid info hash.

packages/tracker-core/src/databases/driver/mod.rs

+50-12
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ mod tests {
117117

118118
// Whitelist (for listed trackers)
119119

120-
// todo
120+
handling_the_whitelist::it_should_add_and_get_infohashes(driver);
121+
handling_the_whitelist::it_should_remove_an_infohash_from_the_whitelist(driver);
122+
handling_the_whitelist::it_should_fail_trying_to_add_the_same_infohash_twice(driver);
123+
handling_the_whitelist::it_load_the_whitelist(driver);
121124

122125
driver.drop_database_tables().unwrap();
123126
}
@@ -129,7 +132,7 @@ mod tests {
129132
}
130133
tokio::time::sleep(Duration::from_secs(2)).await;
131134
}
132-
Err("MySQL is not ready after retries.".into())
135+
Err("Database is not ready after retries.".into())
133136
}
134137

135138
mod handling_torrent_persistence {
@@ -162,22 +165,18 @@ mod tests {
162165
use crate::databases::Database;
163166

164167
pub fn it_should_save_and_load_permanent_authentication_keys(driver: &Arc<Box<dyn Database>>) {
165-
// Add a new permanent key
166168
let peer_key = generate_permanent_key();
167169
driver.add_key_to_keys(&peer_key).unwrap();
168170

169-
// Get the key back
170171
let stored_peer_key = driver.get_key_from_keys(&peer_key.key()).unwrap().unwrap();
171172

172173
assert_eq!(stored_peer_key, peer_key);
173174
}
174175

175176
pub fn it_should_save_and_load_expiring_authentication_keys(driver: &Arc<Box<dyn Database>>) {
176-
// Add a new expiring key
177177
let peer_key = generate_key(Some(Duration::from_secs(120)));
178178
driver.add_key_to_keys(&peer_key).unwrap();
179179

180-
// Get the key back
181180
let stored_peer_key = driver.get_key_from_keys(&peer_key.key()).unwrap().unwrap();
182181

183182
assert_eq!(stored_peer_key, peer_key);
@@ -186,26 +185,65 @@ mod tests {
186185

187186
pub fn it_should_remove_a_permanent_authentication_key(driver: &Arc<Box<dyn Database>>) {
188187
let peer_key = generate_permanent_key();
189-
190-
// Add a new key
191188
driver.add_key_to_keys(&peer_key).unwrap();
192189

193-
// Remove the key
194190
driver.remove_key_from_keys(&peer_key.key()).unwrap();
195191

196192
assert!(driver.get_key_from_keys(&peer_key.key()).unwrap().is_none());
197193
}
198194

199195
pub fn it_should_remove_an_expiring_authentication_key(driver: &Arc<Box<dyn Database>>) {
200196
let peer_key = generate_key(Some(Duration::from_secs(120)));
201-
202-
// Add a new key
203197
driver.add_key_to_keys(&peer_key).unwrap();
204198

205-
// Remove the key
206199
driver.remove_key_from_keys(&peer_key.key()).unwrap();
207200

208201
assert!(driver.get_key_from_keys(&peer_key.key()).unwrap().is_none());
209202
}
210203
}
204+
205+
mod handling_the_whitelist {
206+
207+
use std::sync::Arc;
208+
209+
use crate::core_tests::random_info_hash;
210+
use crate::databases::Database;
211+
212+
pub fn it_should_add_and_get_infohashes(driver: &Arc<Box<dyn Database>>) {
213+
let infohash = random_info_hash();
214+
215+
driver.add_info_hash_to_whitelist(infohash).unwrap();
216+
217+
let stored_infohash = driver.get_info_hash_from_whitelist(infohash).unwrap().unwrap();
218+
219+
assert_eq!(stored_infohash, infohash);
220+
}
221+
222+
pub fn it_should_remove_an_infohash_from_the_whitelist(driver: &Arc<Box<dyn Database>>) {
223+
let infohash = random_info_hash();
224+
driver.add_info_hash_to_whitelist(infohash).unwrap();
225+
226+
driver.remove_info_hash_from_whitelist(infohash).unwrap();
227+
228+
assert!(driver.get_info_hash_from_whitelist(infohash).unwrap().is_none());
229+
}
230+
231+
pub fn it_should_fail_trying_to_add_the_same_infohash_twice(driver: &Arc<Box<dyn Database>>) {
232+
let infohash = random_info_hash();
233+
234+
driver.add_info_hash_to_whitelist(infohash).unwrap();
235+
let result = driver.add_info_hash_to_whitelist(infohash);
236+
237+
assert!(result.is_err());
238+
}
239+
240+
pub fn it_load_the_whitelist(driver: &Arc<Box<dyn Database>>) {
241+
let infohash = random_info_hash();
242+
driver.add_info_hash_to_whitelist(infohash).unwrap();
243+
244+
let whitelist = driver.load_whitelist().unwrap();
245+
246+
assert!(whitelist.contains(&infohash));
247+
}
248+
}
211249
}

0 commit comments

Comments
 (0)