forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
344 lines (254 loc) · 12.2 KB
/
mod.rs
File metadata and controls
344 lines (254 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//! Database driver factory.
use mysql::Mysql;
use serde::{Deserialize, Serialize};
use sqlite::Sqlite;
use super::error::Error;
use super::Database;
/// Metric name in DB for the total number of downloads across all torrents.
const TORRENTS_DOWNLOADS_TOTAL: &str = "torrents_downloads_total";
/// The database management system used by the tracker.
///
/// Refer to:
///
/// - [Torrust Tracker Configuration](https://docs.rs/torrust-tracker-configuration).
/// - [Torrust Tracker](https://docs.rs/torrust-tracker).
///
/// For more information about persistence.
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, derive_more::Display, Clone)]
pub enum Driver {
/// The Sqlite3 database driver.
Sqlite3,
/// The `MySQL` database driver.
MySQL,
}
/// It builds a new database driver.
///
/// Example for `SQLite3`:
///
/// ```text
/// use bittorrent_tracker_core::databases;
/// use bittorrent_tracker_core::databases::driver::Driver;
///
/// let db_driver = Driver::Sqlite3;
/// let db_path = "./storage/tracker/lib/database/sqlite3.db".to_string();
/// let database = databases::driver::build(&db_driver, &db_path);
/// ```
///
/// Example for `MySQL`:
///
/// ```text
/// use bittorrent_tracker_core::databases;
/// use bittorrent_tracker_core::databases::driver::Driver;
///
/// let db_driver = Driver::MySQL;
/// let db_path = "mysql://db_user:db_user_secret_password@mysql:3306/torrust_tracker".to_string();
/// let database = databases::driver::build(&db_driver, &db_path);
/// ```
///
/// Refer to the [configuration documentation](https://docs.rs/torrust-tracker-configuration)
/// for more information about the database configuration.
///
/// > **WARNING**: The driver instantiation runs database migrations.
///
/// # Errors
///
/// This function will return an error if unable to connect to the database.
///
/// # Panics
///
/// This function will panic if unable to create database tables.
pub mod mysql;
pub mod sqlite;
/// It builds a new database driver.
///
/// # Panics
///
/// Will panic if unable to create database tables.
///
/// # Errors
///
/// Will return `Error` if unable to build the driver.
pub(crate) fn build(driver: &Driver, db_path: &str) -> Result<Box<dyn Database>, Error> {
let database: Box<dyn Database> = match driver {
Driver::Sqlite3 => Box::new(Sqlite::new(db_path)?),
Driver::MySQL => Box::new(Mysql::new(db_path)?),
};
database.create_database_tables().expect("Could not create database tables.");
Ok(database)
}
#[cfg(test)]
pub(crate) mod tests {
use std::sync::Arc;
use std::time::Duration;
use crate::databases::Database;
pub async fn run_tests(driver: &Arc<Box<dyn Database>>) {
// Since the interface is very simple and there are no conflicts between
// tests, we share the same database. If we want to isolate the tests in
// the future, we can create a new database for each test.
database_setup(driver).await;
// Persistent torrents (stats)
// Torrent metrics
handling_torrent_persistence::it_should_save_and_load_persistent_torrents(driver);
handling_torrent_persistence::it_should_load_all_persistent_torrents(driver);
handling_torrent_persistence::it_should_increase_the_number_of_downloads_for_a_given_torrent(driver);
// Aggregate metrics for all torrents
handling_torrent_persistence::it_should_save_and_load_the_global_number_of_downloads(driver);
handling_torrent_persistence::it_should_load_the_global_number_of_downloads(driver);
handling_torrent_persistence::it_should_increase_the_global_number_of_downloads(driver);
// Authentication keys (for private trackers)
handling_authentication_keys::it_should_load_the_keys(driver);
// Permanent keys
handling_authentication_keys::it_should_save_and_load_permanent_authentication_keys(driver);
handling_authentication_keys::it_should_remove_a_permanent_authentication_key(driver);
// Expiring keys
handling_authentication_keys::it_should_save_and_load_expiring_authentication_keys(driver);
handling_authentication_keys::it_should_remove_an_expiring_authentication_key(driver);
// Whitelist (for listed trackers)
handling_the_whitelist::it_should_load_the_whitelist(driver);
handling_the_whitelist::it_should_add_and_get_infohashes(driver);
handling_the_whitelist::it_should_remove_an_infohash_from_the_whitelist(driver);
handling_the_whitelist::it_should_fail_trying_to_add_the_same_infohash_twice(driver);
}
/// It initializes the database schema.
///
/// Since the drop SQL queries don't check if the tables already exist,
/// we have to create them first, and then drop them.
///
/// The method to drop tables does not use "DROP TABLE IF EXISTS". We can
/// change this function when we update the `Database::drop_database_tables`
/// method to use "DROP TABLE IF EXISTS".
async fn database_setup(driver: &Arc<Box<dyn Database>>) {
create_database_tables(driver).await.expect("database tables creation failed");
driver.drop_database_tables().expect("old database tables deletion failed");
create_database_tables(driver)
.await
.expect("database tables creation from empty schema failed");
}
async fn create_database_tables(driver: &Arc<Box<dyn Database>>) -> Result<(), Box<dyn std::error::Error>> {
for _ in 0..5 {
if driver.create_database_tables().is_ok() {
return Ok(());
}
tokio::time::sleep(Duration::from_secs(2)).await;
}
Err("Database is not ready after retries.".into())
}
mod handling_torrent_persistence {
use std::sync::Arc;
use crate::databases::Database;
use crate::test_helpers::tests::sample_info_hash;
// Metrics per torrent
pub fn it_should_save_and_load_persistent_torrents(driver: &Arc<Box<dyn Database>>) {
let infohash = sample_info_hash();
let number_of_downloads = 1;
driver.save_torrent_downloads(&infohash, number_of_downloads).unwrap();
let number_of_downloads = driver.load_torrent_downloads(&infohash).unwrap().unwrap();
assert_eq!(number_of_downloads, 1);
}
pub fn it_should_load_all_persistent_torrents(driver: &Arc<Box<dyn Database>>) {
let infohash = sample_info_hash();
let number_of_downloads = 1;
driver.save_torrent_downloads(&infohash, number_of_downloads).unwrap();
let torrents = driver.load_all_torrents_downloads().unwrap();
assert_eq!(torrents.len(), 1);
assert_eq!(torrents.get(&infohash), Some(number_of_downloads).as_ref());
}
pub fn it_should_increase_the_number_of_downloads_for_a_given_torrent(driver: &Arc<Box<dyn Database>>) {
let infohash = sample_info_hash();
let number_of_downloads = 1;
driver.save_torrent_downloads(&infohash, number_of_downloads).unwrap();
driver.increase_downloads_for_torrent(&infohash).unwrap();
let number_of_downloads = driver.load_torrent_downloads(&infohash).unwrap().unwrap();
assert_eq!(number_of_downloads, 2);
}
// Aggregate metrics for all torrents
pub fn it_should_save_and_load_the_global_number_of_downloads(driver: &Arc<Box<dyn Database>>) {
let number_of_downloads = 1;
driver.save_global_downloads(number_of_downloads).unwrap();
let number_of_downloads = driver.load_global_downloads().unwrap().unwrap();
assert_eq!(number_of_downloads, 1);
}
pub fn it_should_load_the_global_number_of_downloads(driver: &Arc<Box<dyn Database>>) {
let number_of_downloads = 1;
driver.save_global_downloads(number_of_downloads).unwrap();
let number_of_downloads = driver.load_global_downloads().unwrap().unwrap();
assert_eq!(number_of_downloads, 1);
}
pub fn it_should_increase_the_global_number_of_downloads(driver: &Arc<Box<dyn Database>>) {
let number_of_downloads = 1;
driver.save_global_downloads(number_of_downloads).unwrap();
driver.increase_global_downloads().unwrap();
let number_of_downloads = driver.load_global_downloads().unwrap().unwrap();
assert_eq!(number_of_downloads, 2);
}
}
mod handling_authentication_keys {
use std::sync::Arc;
use std::time::Duration;
use crate::authentication::key::{generate_expiring_key, generate_permanent_key};
use crate::databases::Database;
pub fn it_should_load_the_keys(driver: &Arc<Box<dyn Database>>) {
let permanent_peer_key = generate_permanent_key();
driver.add_key_to_keys(&permanent_peer_key).unwrap();
let expiring_peer_key = generate_expiring_key(Duration::from_secs(120));
driver.add_key_to_keys(&expiring_peer_key).unwrap();
let keys = driver.load_keys().unwrap();
assert!(keys.contains(&permanent_peer_key));
assert!(keys.contains(&expiring_peer_key));
}
pub fn it_should_save_and_load_permanent_authentication_keys(driver: &Arc<Box<dyn Database>>) {
let peer_key = generate_permanent_key();
driver.add_key_to_keys(&peer_key).unwrap();
let stored_peer_key = driver.get_key_from_keys(&peer_key.key()).unwrap().unwrap();
assert_eq!(stored_peer_key, peer_key);
}
pub fn it_should_save_and_load_expiring_authentication_keys(driver: &Arc<Box<dyn Database>>) {
let peer_key = generate_expiring_key(Duration::from_secs(120));
driver.add_key_to_keys(&peer_key).unwrap();
let stored_peer_key = driver.get_key_from_keys(&peer_key.key()).unwrap().unwrap();
assert_eq!(stored_peer_key, peer_key);
assert_eq!(stored_peer_key.expiry_time(), peer_key.expiry_time());
}
pub fn it_should_remove_a_permanent_authentication_key(driver: &Arc<Box<dyn Database>>) {
let peer_key = generate_permanent_key();
driver.add_key_to_keys(&peer_key).unwrap();
driver.remove_key_from_keys(&peer_key.key()).unwrap();
assert!(driver.get_key_from_keys(&peer_key.key()).unwrap().is_none());
}
pub fn it_should_remove_an_expiring_authentication_key(driver: &Arc<Box<dyn Database>>) {
let peer_key = generate_expiring_key(Duration::from_secs(120));
driver.add_key_to_keys(&peer_key).unwrap();
driver.remove_key_from_keys(&peer_key.key()).unwrap();
assert!(driver.get_key_from_keys(&peer_key.key()).unwrap().is_none());
}
}
mod handling_the_whitelist {
use std::sync::Arc;
use crate::databases::Database;
use crate::test_helpers::tests::random_info_hash;
pub fn it_should_load_the_whitelist(driver: &Arc<Box<dyn Database>>) {
let infohash = random_info_hash();
driver.add_info_hash_to_whitelist(infohash).unwrap();
let whitelist = driver.load_whitelist().unwrap();
assert!(whitelist.contains(&infohash));
}
pub fn it_should_add_and_get_infohashes(driver: &Arc<Box<dyn Database>>) {
let infohash = random_info_hash();
driver.add_info_hash_to_whitelist(infohash).unwrap();
let stored_infohash = driver.get_info_hash_from_whitelist(infohash).unwrap().unwrap();
assert_eq!(stored_infohash, infohash);
}
pub fn it_should_remove_an_infohash_from_the_whitelist(driver: &Arc<Box<dyn Database>>) {
let infohash = random_info_hash();
driver.add_info_hash_to_whitelist(infohash).unwrap();
driver.remove_info_hash_from_whitelist(infohash).unwrap();
assert!(driver.get_info_hash_from_whitelist(infohash).unwrap().is_none());
}
pub fn it_should_fail_trying_to_add_the_same_infohash_twice(driver: &Arc<Box<dyn Database>>) {
let infohash = random_info_hash();
driver.add_info_hash_to_whitelist(infohash).unwrap();
let result = driver.add_info_hash_to_whitelist(infohash);
assert!(result.is_err());
}
}
}