Skip to content

Commit

Permalink
Merge #210: Add unit tests for Tracker
Browse files Browse the repository at this point in the history
d0c3054 fix(tracker): typo, rename SwamStats to SwarmStats (Jose Celano)
7fb92b5 test(tracker): [#207] add tests for whitelist in Tracker (Jose Celano)
af949af test(tracker): [#207] add tests for torrent persistence in Tracker (Jose Celano)
40ff249 test(tracker): [#207] add test for authorization (whitelist) in Tracker (Jose Celano)
fad6834 test(tracker): [#207] add tests for HTTP authentication in Tracker (Jose Celano)
d1a7b7f fix(cargo): fix output path in cargo allias (Jose Celano)
ed58a32 test(tracker): [#207] add test for Tracker::scrape (Jose Celano)
1e7eff5 docs(tracker): add code-review (Jose Celano)
d50372f feat(cargo): add cargo alias to generate coverage reports (Jose Celano)
fca5353 test(tracker): [#207] add test for Tracker::announce (Jose Celano)
ee5b088 docs(http): add comment for duplicate struct info (Jose Celano)
2ddefa8 feat: add .coverage dir to .gitignore (Jose Celano)

Pull request description:

  I'm double-checking the new Axum HTTP tracker and adding more tests.

Top commit has no ACKs.

Tree-SHA512: 78da5c5b2c121a781ad39afdad4f95e0f770f6f2567c381b38a11fcc4f52f22c907e9c8f6424bf4082c77faab4ca10876aea11301dfc7b18b02cbf26503cdb2a
  • Loading branch information
josecelano committed Mar 4, 2023
2 parents f3892cf + d0c3054 commit 6a15202
Show file tree
Hide file tree
Showing 12 changed files with 678 additions and 170 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[alias]
cov = "llvm-cov --lcov --output-path=./.coverage/lcov.info"
cov-html = "llvm-cov --html"
11 changes: 5 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
.env
/target
**/*.rs.bk
/database.json.bz2
/database.db
/.coverage/
/.idea/
/.vscode/launch.json
/config.toml
/data.db
/.vscode/launch.json
/database.db
/database.json.bz2
/storage/


/target
1 change: 1 addition & 0 deletions cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"infohashes",
"infoschema",
"intervali",
"lcov",
"leecher",
"leechers",
"libtorrent",
Expand Down
4 changes: 2 additions & 2 deletions src/apis/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub async fn remove_torrent_from_whitelist_handler(
}

pub async fn reload_whitelist_handler(State(tracker): State<Arc<Tracker>>) -> Response {
match tracker.load_whitelist().await {
match tracker.load_whitelist_from_database().await {
Ok(_) => ok_response(),
Err(e) => failed_to_reload_whitelist_response(e),
}
Expand Down Expand Up @@ -117,7 +117,7 @@ pub async fn delete_auth_key_handler(
}

pub async fn reload_keys_handler(State(tracker): State<Arc<Tracker>>) -> Response {
match tracker.load_keys().await {
match tracker.load_keys_from_database().await {
Ok(_) => ok_response(),
Err(e) => failed_to_reload_keys_response(e),
}
Expand Down
8 changes: 4 additions & 4 deletions src/http/axum_implementation/responses/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ impl From<AnnounceData> for NonCompact {
Self {
interval: domain_announce_response.interval,
interval_min: domain_announce_response.interval_min,
complete: domain_announce_response.swam_stats.seeders,
incomplete: domain_announce_response.swam_stats.leechers,
complete: domain_announce_response.swarm_stats.seeders,
incomplete: domain_announce_response.swarm_stats.leechers,
peers,
}
}
Expand Down Expand Up @@ -237,8 +237,8 @@ impl From<AnnounceData> for Compact {
Self {
interval: domain_announce_response.interval,
interval_min: domain_announce_response.interval_min,
complete: domain_announce_response.swam_stats.seeders,
incomplete: domain_announce_response.swam_stats.leechers,
complete: domain_announce_response.swarm_stats.seeders,
incomplete: domain_announce_response.swarm_stats.leechers,
peers,
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/http/axum_implementation/services/scrape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ pub async fn invoke(tracker: &Arc<Tracker>, info_hashes: &Vec<InfoHash>, origina
/// When the peer is not authenticated and the tracker is running in `private` mode,
/// the tracker returns empty stats for all the torrents.
pub async fn fake_invoke(tracker: &Arc<Tracker>, info_hashes: &Vec<InfoHash>, original_peer_ip: &IpAddr) -> ScrapeData {
let scrape_data = tracker.empty_scrape_for(info_hashes);

send_scrape_event(original_peer_ip, tracker).await;

scrape_data
ScrapeData::zeroed(info_hashes)
}

async fn send_scrape_event(original_peer_ip: &IpAddr, tracker: &Arc<Tracker>) {
Expand Down
4 changes: 2 additions & 2 deletions src/http/warp_implementation/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub async fn handle_announce(

send_announce_response(
&announce_request,
&response.swam_stats,
&response.swarm_stats,
&response.peers,
tracker.config.announce_interval,
tracker.config.min_announce_interval,
Expand Down Expand Up @@ -129,7 +129,7 @@ pub async fn handle_scrape(
#[allow(clippy::ptr_arg)]
fn send_announce_response(
announce_request: &request::Announce,
torrent_stats: &torrent::SwamStats,
torrent_stats: &torrent::SwarmStats,
peers: &Vec<peer::Peer>,
interval: u32,
interval_min: u32,
Expand Down
7 changes: 5 additions & 2 deletions src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ pub async fn setup(config: &Configuration, tracker: Arc<tracker::Tracker>) -> Ve

// Load peer keys
if tracker.is_private() {
tracker.load_keys().await.expect("Could not retrieve keys from database.");
tracker
.load_keys_from_database()
.await
.expect("Could not retrieve keys from database.");
}

// Load whitelisted torrents
if tracker.is_whitelisted() {
tracker
.load_whitelist()
.load_whitelist_from_database()
.await
.expect("Could not load whitelist from database.");
}
Expand Down
3 changes: 3 additions & 0 deletions src/tracker/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ pub fn verify(auth_key: &ExpiringKey) -> Result<(), Error> {
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
pub struct ExpiringKey {
pub id: KeyId,
// todo: we can remove the `Option`. An `ExpiringKey` that does not expire
// is a `KeyId`. In other words, all `ExpiringKeys` must have an
// expiration time.
pub valid_until: Option<DurationSinceUnixEpoch>,
}

Expand Down
Loading

0 comments on commit 6a15202

Please sign in to comment.