Skip to content

Commit adb9614

Browse files
committed
tests: [torrust#1251] add test for loading keys in DB drivers
1 parent 700482d commit adb9614

File tree

2 files changed

+34
-13
lines changed
  • packages/tracker-core/src

2 files changed

+34
-13
lines changed

packages/tracker-core/src/authentication/key/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ pub fn generate_permanent_key() -> PeerKey {
6767
generate_key(None)
6868
}
6969

70+
/// It generates a new expiring random key [`PeerKey`].
71+
#[must_use]
72+
pub fn generate_expiring_key(lifetime: Duration) -> PeerKey {
73+
generate_key(Some(lifetime))
74+
}
75+
7076
/// It generates a new random 32-char authentication [`PeerKey`].
7177
///
7278
/// It can be an expiring or permanent key.

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

+28-13
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ mod tests {
104104

105105
// Authentication keys (for private trackers)
106106

107+
handling_authentication_keys::it_should_load_the_keys(driver);
108+
107109
// Permanent keys
108110
handling_authentication_keys::it_should_save_and_load_permanent_authentication_keys(driver);
109111
handling_authentication_keys::it_should_remove_a_permanent_authentication_key(driver);
@@ -114,10 +116,10 @@ mod tests {
114116

115117
// Whitelist (for listed trackers)
116118

119+
handling_the_whitelist::it_should_load_the_whitelist(driver);
117120
handling_the_whitelist::it_should_add_and_get_infohashes(driver);
118121
handling_the_whitelist::it_should_remove_an_infohash_from_the_whitelist(driver);
119122
handling_the_whitelist::it_should_fail_trying_to_add_the_same_infohash_twice(driver);
120-
handling_the_whitelist::it_load_the_whitelist(driver);
121123
}
122124

123125
/// It initializes the database schema.
@@ -172,9 +174,22 @@ mod tests {
172174
use std::sync::Arc;
173175
use std::time::Duration;
174176

175-
use crate::authentication::key::{generate_key, generate_permanent_key};
177+
use crate::authentication::key::{generate_expiring_key, generate_permanent_key};
176178
use crate::databases::Database;
177179

180+
pub fn it_should_load_the_keys(driver: &Arc<Box<dyn Database>>) {
181+
let permanent_peer_key = generate_permanent_key();
182+
driver.add_key_to_keys(&permanent_peer_key).unwrap();
183+
184+
let expiring_peer_key = generate_expiring_key(Duration::from_secs(120));
185+
driver.add_key_to_keys(&expiring_peer_key).unwrap();
186+
187+
let keys = driver.load_keys().unwrap();
188+
189+
assert!(keys.contains(&permanent_peer_key));
190+
assert!(keys.contains(&expiring_peer_key));
191+
}
192+
178193
pub fn it_should_save_and_load_permanent_authentication_keys(driver: &Arc<Box<dyn Database>>) {
179194
let peer_key = generate_permanent_key();
180195
driver.add_key_to_keys(&peer_key).unwrap();
@@ -185,7 +200,7 @@ mod tests {
185200
}
186201

187202
pub fn it_should_save_and_load_expiring_authentication_keys(driver: &Arc<Box<dyn Database>>) {
188-
let peer_key = generate_key(Some(Duration::from_secs(120)));
203+
let peer_key = generate_expiring_key(Duration::from_secs(120));
189204
driver.add_key_to_keys(&peer_key).unwrap();
190205

191206
let stored_peer_key = driver.get_key_from_keys(&peer_key.key()).unwrap().unwrap();
@@ -204,7 +219,7 @@ mod tests {
204219
}
205220

206221
pub fn it_should_remove_an_expiring_authentication_key(driver: &Arc<Box<dyn Database>>) {
207-
let peer_key = generate_key(Some(Duration::from_secs(120)));
222+
let peer_key = generate_expiring_key(Duration::from_secs(120));
208223
driver.add_key_to_keys(&peer_key).unwrap();
209224

210225
driver.remove_key_from_keys(&peer_key.key()).unwrap();
@@ -220,6 +235,15 @@ mod tests {
220235
use crate::core_tests::random_info_hash;
221236
use crate::databases::Database;
222237

238+
pub fn it_should_load_the_whitelist(driver: &Arc<Box<dyn Database>>) {
239+
let infohash = random_info_hash();
240+
driver.add_info_hash_to_whitelist(infohash).unwrap();
241+
242+
let whitelist = driver.load_whitelist().unwrap();
243+
244+
assert!(whitelist.contains(&infohash));
245+
}
246+
223247
pub fn it_should_add_and_get_infohashes(driver: &Arc<Box<dyn Database>>) {
224248
let infohash = random_info_hash();
225249

@@ -247,14 +271,5 @@ mod tests {
247271

248272
assert!(result.is_err());
249273
}
250-
251-
pub fn it_load_the_whitelist(driver: &Arc<Box<dyn Database>>) {
252-
let infohash = random_info_hash();
253-
driver.add_info_hash_to_whitelist(infohash).unwrap();
254-
255-
let whitelist = driver.load_whitelist().unwrap();
256-
257-
assert!(whitelist.contains(&infohash));
258-
}
259274
}
260275
}

0 commit comments

Comments
 (0)