Skip to content

Commit c1586de

Browse files
committed
Merge #1252: Overhaul core Tracker: add tests for error mod
ca4ead5 test: [#1250] add tests for tracker core errors (Jose Celano) 0811d67 refactor: [#1250] rename enum (Jose Celano) 3bb4a13 refactor: [#1250] remove unused errors in tracker core (Jose Celano) 58a3741 refactor: [#1250] invert dependency between http-protocol and tracker-core pacakges (Jose Celano) Pull request description: Overhaul core Tracker: add tests for `error` mod. ACKs for top commit: josecelano: ACK ca4ead5 Tree-SHA512: 0d1b1c3cda7595509350552c1edc143bf729c1bb9f05673b4d8707e1fa964473739bd8d8b3b275e16ced68fad27275162bd985b20dfc95e62ac18bf09cdf92b2
2 parents a1046a2 + ca4ead5 commit c1586de

File tree

6 files changed

+98
-34
lines changed

6 files changed

+98
-34
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/http-protocol/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ version.workspace = true
1717
[dependencies]
1818
aquatic_udp_protocol = "0"
1919
bittorrent-primitives = "0.1.0"
20+
bittorrent-tracker-core = { version = "3.0.0-develop", path = "../tracker-core" }
2021
derive_more = { version = "1", features = ["as_ref", "constructor", "from"] }
2122
multimap = "0"
2223
percent-encoding = "2"

packages/http-protocol/src/v1/responses/error.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ impl From<PeerIpResolutionError> for Error {
5555
}
5656
}
5757

58+
impl From<bittorrent_tracker_core::error::WhitelistError> for Error {
59+
fn from(err: bittorrent_tracker_core::error::WhitelistError) -> Self {
60+
Error {
61+
failure_reason: format!("Tracker error: {err}"),
62+
}
63+
}
64+
}
65+
5866
#[cfg(test)]
5967
mod tests {
6068

packages/tracker-core/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ version.workspace = true
1616

1717
[dependencies]
1818
aquatic_udp_protocol = "0"
19-
bittorrent-http-protocol = { version = "3.0.0-develop", path = "../http-protocol" }
2019
bittorrent-primitives = "0.1.0"
2120
chrono = { version = "0", default-features = false, features = ["clock"] }
2221
derive_more = { version = "1", features = ["as_ref", "constructor", "from"] }

packages/tracker-core/src/error.rs

Lines changed: 83 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,23 @@
1-
//! Error returned by the core `Tracker`.
2-
//!
3-
//! Error | Context | Description
4-
//! ---|---|---
5-
//! `PeerKeyNotValid` | Authentication | The supplied key is not valid. It may not be registered or expired.
6-
//! `PeerNotAuthenticated` | Authentication | The peer did not provide the authentication key.
7-
//! `TorrentNotWhitelisted` | Authorization | The action cannot be perform on a not-whitelisted torrent (it only applies for trackers running in `listed` or `private_listed` modes).
8-
//!
1+
//! Errors returned by the core tracker.
92
use std::panic::Location;
103

11-
use bittorrent_http_protocol::v1::responses;
124
use bittorrent_primitives::info_hash::InfoHash;
135
use torrust_tracker_located_error::LocatedError;
146

157
use super::authentication::key::ParseKeyError;
168
use super::databases;
179

18-
/// Authentication or authorization error returned by the core `Tracker`
10+
/// Whitelist errors returned by the core tracker.
1911
#[derive(thiserror::Error, Debug, Clone)]
20-
pub enum Error {
21-
// Authentication errors
22-
#[error("The supplied key: {key:?}, is not valid: {source}")]
23-
PeerKeyNotValid {
24-
key: super::authentication::Key,
25-
source: LocatedError<'static, dyn std::error::Error + Send + Sync>,
26-
},
27-
28-
#[error("The peer is not authenticated, {location}")]
29-
PeerNotAuthenticated { location: &'static Location<'static> },
30-
31-
// Authorization errors
12+
pub enum WhitelistError {
3213
#[error("The torrent: {info_hash}, is not whitelisted, {location}")]
3314
TorrentNotWhitelisted {
3415
info_hash: InfoHash,
3516
location: &'static Location<'static>,
3617
},
3718
}
3819

39-
/// Errors related to peers keys.
20+
/// Peers keys errors returned by the core tracker.
4021
#[allow(clippy::module_name_repetitions)]
4122
#[derive(thiserror::Error, Debug, Clone)]
4223
pub enum PeerKeyError {
@@ -55,10 +36,85 @@ pub enum PeerKeyError {
5536
},
5637
}
5738

58-
impl From<Error> for responses::error::Error {
59-
fn from(err: Error) -> Self {
60-
responses::error::Error {
61-
failure_reason: format!("Tracker error: {err}"),
39+
#[cfg(test)]
40+
mod tests {
41+
42+
mod whitelist_error {
43+
44+
use crate::core_tests::sample_info_hash;
45+
use crate::error::WhitelistError;
46+
47+
#[test]
48+
fn torrent_not_whitelisted() {
49+
let err = WhitelistError::TorrentNotWhitelisted {
50+
info_hash: sample_info_hash(),
51+
location: std::panic::Location::caller(),
52+
};
53+
54+
let err_msg = format!("{err}");
55+
56+
assert!(
57+
err_msg.contains(&format!("The torrent: {}, is not whitelisted", sample_info_hash())),
58+
"Error message did not contain expected text: {err_msg}"
59+
);
60+
}
61+
}
62+
63+
mod peer_key_error {
64+
use torrust_tracker_located_error::Located;
65+
66+
use crate::databases::driver::Driver;
67+
use crate::error::PeerKeyError;
68+
use crate::{authentication, databases};
69+
70+
#[test]
71+
fn duration_overflow() {
72+
let seconds_valid = 100;
73+
74+
let err = PeerKeyError::DurationOverflow { seconds_valid };
75+
76+
let err_msg = format!("{err}");
77+
78+
assert!(
79+
err_msg.contains(&format!("Invalid peer key duration: {seconds_valid}")),
80+
"Error message did not contain expected text: {err_msg}"
81+
);
82+
}
83+
84+
#[test]
85+
fn parsing_from_string() {
86+
let err = authentication::key::ParseKeyError::InvalidKeyLength;
87+
88+
let err = PeerKeyError::InvalidKey {
89+
key: "INVALID KEY".to_string(),
90+
source: Located(err).into(),
91+
};
92+
93+
let err_msg = format!("{err}");
94+
95+
assert!(
96+
err_msg.contains(&"Invalid key: INVALID KEY".to_string()),
97+
"Error message did not contain expected text: {err_msg}"
98+
);
99+
}
100+
101+
#[test]
102+
fn persisting_into_database() {
103+
let err = databases::error::Error::InsertFailed {
104+
location: std::panic::Location::caller(),
105+
driver: Driver::Sqlite3,
106+
};
107+
108+
let err = PeerKeyError::DatabaseError {
109+
source: Located(err).into(),
110+
};
111+
112+
let err_msg = format!("{err}");
113+
114+
assert!(
115+
err_msg.contains(&"Can't persist key".to_string()),
116+
"Error message did not contain expected text: {err}"
117+
);
62118
}
63119
}
64120
}

packages/tracker-core/src/whitelist/authorization.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use torrust_tracker_configuration::Core;
66
use tracing::instrument;
77

88
use super::repository::in_memory::InMemoryWhitelist;
9-
use crate::error::Error;
9+
use crate::error::WhitelistError;
1010

1111
pub struct WhitelistAuthorization {
1212
/// Core tracker configuration.
@@ -32,7 +32,7 @@ impl WhitelistAuthorization {
3232
/// Will return an error if the tracker is running in `listed` mode
3333
/// and the infohash is not whitelisted.
3434
#[instrument(skip(self, info_hash), err)]
35-
pub async fn authorize(&self, info_hash: &InfoHash) -> Result<(), Error> {
35+
pub async fn authorize(&self, info_hash: &InfoHash) -> Result<(), WhitelistError> {
3636
if !self.is_listed() {
3737
return Ok(());
3838
}
@@ -41,7 +41,7 @@ impl WhitelistAuthorization {
4141
return Ok(());
4242
}
4343

44-
Err(Error::TorrentNotWhitelisted {
44+
Err(WhitelistError::TorrentNotWhitelisted {
4545
info_hash: *info_hash,
4646
location: Location::caller(),
4747
})
@@ -89,7 +89,7 @@ mod tests {
8989
use torrust_tracker_configuration::Core;
9090

9191
use crate::core_tests::sample_info_hash;
92-
use crate::error::Error;
92+
use crate::error::WhitelistError;
9393
use crate::whitelist::authorization::tests::the_whitelist_authorization_for_announce_and_scrape_actions::{
9494
initialize_whitelist_authorization_and_dependencies_with, initialize_whitelist_authorization_with,
9595
};
@@ -121,7 +121,7 @@ mod tests {
121121

122122
let result = whitelist_authorization.authorize(&sample_info_hash()).await;
123123

124-
assert!(matches!(result.unwrap_err(), Error::TorrentNotWhitelisted { .. }));
124+
assert!(matches!(result.unwrap_err(), WhitelistError::TorrentNotWhitelisted { .. }));
125125
}
126126
}
127127

0 commit comments

Comments
 (0)