Skip to content

Commit

Permalink
fix: prepare tests for connection cookie
Browse files Browse the repository at this point in the history
Co-authored-by: Jose Celano <josecelano@gmail.com>
  • Loading branch information
da2ce7 and josecelano committed Oct 18, 2022
1 parent ca07f32 commit c8fa30a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/protocol/clock/time_extent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ where
}
}
}

fn now_before(increment: &TimeExtentBase, sub_time: &Duration) -> Option<Result<TimeExtent, TryFromIntError>> {
match Clock::sub(sub_time) {
None => None,
Expand Down
1 change: 0 additions & 1 deletion src/tracker/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ mod test {

use crate::peer::TorrentPeer;
use crate::protocol::utils::get_connection_id;

// todo: duplicate functions is PR 82. Remove duplication once both PR are merged.

fn sample_ipv4_remote_addr() -> SocketAddr {
Expand Down
76 changes: 56 additions & 20 deletions src/udp/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,8 @@ mod tests {
use std::net::Ipv4Addr;

use aquatic_udp_protocol::{
AnnounceEvent, AnnounceRequest, NumberOfBytes, NumberOfPeers, PeerId as AquaticPeerId, PeerKey, Port, TransactionId,
AnnounceEvent, AnnounceRequest, ConnectionId, NumberOfBytes, NumberOfPeers, PeerId as AquaticPeerId, PeerKey, Port,
TransactionId,
};

use crate::protocol::utils::get_connection_id;
Expand Down Expand Up @@ -517,6 +518,11 @@ mod tests {
}
}

pub fn with_connection_id(mut self, connection_id: ConnectionId) -> Self {
self.request.connection_id = connection_id;
self
}

pub fn with_info_hash(mut self, info_hash: aquatic_udp_protocol::InfoHash) -> Self {
self.request.info_hash = info_hash;
self
Expand Down Expand Up @@ -552,6 +558,7 @@ mod tests {
Response, ResponsePeer,
};

use crate::protocol::utils::get_connection_id;
use crate::statistics::TrackerStatisticsEvent;
use crate::tracker::tracker::TorrentTracker;
use crate::udp::handle_announce;
Expand All @@ -571,14 +578,16 @@ mod tests {
let info_hash = AquaticInfoHash([0u8; 20]);
let peer_id = AquaticPeerId([255u8; 20]);

let remote_addr = SocketAddr::new(IpAddr::V4(client_ip), client_port);

let request = AnnounceRequestBuilder::default()
.with_connection_id(get_connection_id(&remote_addr))
.with_info_hash(info_hash)
.with_peer_id(peer_id)
.with_ip_address(client_ip)
.with_port(client_port)
.into();

let remote_addr = SocketAddr::new(IpAddr::V4(client_ip), client_port);
handle_announce(remote_addr, &request, tracker.clone()).await.unwrap();

let peers = tracker.get_all_torrent_peers(&info_hash.0.into()).await;
Expand All @@ -593,9 +602,12 @@ mod tests {

#[tokio::test]
async fn the_announced_peer_should_not_be_included_in_the_response() {
let request = AnnounceRequestBuilder::default().into();
let remote_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080);

let request = AnnounceRequestBuilder::default()
.with_connection_id(get_connection_id(&remote_addr))
.into();

let response = handle_announce(remote_addr, &request, initialized_public_tracker())
.await
.unwrap();
Expand Down Expand Up @@ -629,14 +641,16 @@ mod tests {
let remote_client_port = 8081;
let peer_address = Ipv4Addr::new(126, 0, 0, 2);

let remote_addr = SocketAddr::new(IpAddr::V4(remote_client_ip), remote_client_port);

let request = AnnounceRequestBuilder::default()
.with_connection_id(get_connection_id(&remote_addr))
.with_info_hash(info_hash)
.with_peer_id(peer_id)
.with_ip_address(peer_address)
.with_port(client_port)
.into();

let remote_addr = SocketAddr::new(IpAddr::V4(remote_client_ip), remote_client_port);
handle_announce(remote_addr, &request, tracker.clone()).await.unwrap();

let peers = tracker.get_all_torrent_peers(&info_hash.0.into()).await;
Expand All @@ -663,8 +677,10 @@ mod tests {
}

async fn announce_a_new_peer_using_ipv4(tracker: Arc<TorrentTracker>) -> Response {
let request = AnnounceRequestBuilder::default().into();
let remote_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080);
let request = AnnounceRequestBuilder::default()
.with_connection_id(get_connection_id(&remote_addr))
.into();
let response = handle_announce(remote_addr, &request, tracker.clone()).await.unwrap();
response
}
Expand Down Expand Up @@ -707,6 +723,7 @@ mod tests {

use aquatic_udp_protocol::{InfoHash as AquaticInfoHash, PeerId as AquaticPeerId};

use crate::protocol::utils::get_connection_id;
use crate::udp::handle_announce;
use crate::udp::handlers::tests::announce_request::AnnounceRequestBuilder;
use crate::udp::handlers::tests::{initialized_public_tracker, TorrentPeerBuilder};
Expand All @@ -721,14 +738,16 @@ mod tests {
let info_hash = AquaticInfoHash([0u8; 20]);
let peer_id = AquaticPeerId([255u8; 20]);

let remote_addr = SocketAddr::new(IpAddr::V4(client_ip), client_port);

let request = AnnounceRequestBuilder::default()
.with_connection_id(get_connection_id(&remote_addr))
.with_info_hash(info_hash)
.with_peer_id(peer_id)
.with_ip_address(client_ip)
.with_port(client_port)
.into();

let remote_addr = SocketAddr::new(IpAddr::V4(client_ip), client_port);
handle_announce(remote_addr, &request, tracker.clone()).await.unwrap();

let peers = tracker.get_all_torrent_peers(&info_hash.0.into()).await;
Expand Down Expand Up @@ -756,6 +775,7 @@ mod tests {
Response, ResponsePeer,
};

use crate::protocol::utils::get_connection_id;
use crate::statistics::TrackerStatisticsEvent;
use crate::tracker::tracker::TorrentTracker;
use crate::udp::handle_announce;
Expand All @@ -776,14 +796,16 @@ mod tests {
let info_hash = AquaticInfoHash([0u8; 20]);
let peer_id = AquaticPeerId([255u8; 20]);

let remote_addr = SocketAddr::new(IpAddr::V6(client_ip_v6), client_port);

let request = AnnounceRequestBuilder::default()
.with_connection_id(get_connection_id(&remote_addr))
.with_info_hash(info_hash)
.with_peer_id(peer_id)
.with_ip_address(client_ip_v4)
.with_port(client_port)
.into();

let remote_addr = SocketAddr::new(IpAddr::V6(client_ip_v6), client_port);
handle_announce(remote_addr, &request, tracker.clone()).await.unwrap();

let peers = tracker.get_all_torrent_peers(&info_hash.0.into()).await;
Expand All @@ -798,11 +820,15 @@ mod tests {

#[tokio::test]
async fn the_announced_peer_should_not_be_included_in_the_response() {
let request = AnnounceRequestBuilder::default().into();
let client_ip_v4 = Ipv4Addr::new(126, 0, 0, 1);
let client_ip_v6 = client_ip_v4.to_ipv6_compatible();

let remote_addr = SocketAddr::new(IpAddr::V6(client_ip_v6), 8080);

let request = AnnounceRequestBuilder::default()
.with_connection_id(get_connection_id(&remote_addr))
.into();

let response = handle_announce(remote_addr, &request, initialized_public_tracker())
.await
.unwrap();
Expand Down Expand Up @@ -836,14 +862,16 @@ mod tests {
let remote_client_port = 8081;
let peer_address = "126.0.0.1".parse().unwrap();

let remote_addr = SocketAddr::new(IpAddr::V6(remote_client_ip), remote_client_port);

let request = AnnounceRequestBuilder::default()
.with_connection_id(get_connection_id(&remote_addr))
.with_info_hash(info_hash)
.with_peer_id(peer_id)
.with_ip_address(peer_address)
.with_port(client_port)
.into();

let remote_addr = SocketAddr::new(IpAddr::V6(remote_client_ip), remote_client_port);
handle_announce(remote_addr, &request, tracker.clone()).await.unwrap();

let peers = tracker.get_all_torrent_peers(&info_hash.0.into()).await;
Expand Down Expand Up @@ -874,7 +902,9 @@ mod tests {
let client_ip_v6 = client_ip_v4.to_ipv6_compatible();
let client_port = 8080;
let remote_addr = SocketAddr::new(IpAddr::V6(client_ip_v6), client_port);
let request = AnnounceRequestBuilder::default().into();
let request = AnnounceRequestBuilder::default()
.with_connection_id(get_connection_id(&remote_addr))
.into();
let response = handle_announce(remote_addr, &request, tracker.clone()).await.unwrap();
response
}
Expand Down Expand Up @@ -903,13 +933,16 @@ mod tests {
tracker_stats_service.should_throw_event(TrackerStatisticsEvent::Udp6Announce);

let tracker = Arc::new(TorrentTracker::new(default_tracker_config(), tracker_stats_service).unwrap());
handle_announce(
sample_ipv6_remote_addr(),
&AnnounceRequestBuilder::default().into(),
tracker.clone(),
)
.await
.unwrap();

let remote_addr = sample_ipv6_remote_addr();

let announce_request = AnnounceRequestBuilder::default()
.with_connection_id(get_connection_id(&remote_addr))
.into();

handle_announce(remote_addr, &announce_request, tracker.clone())
.await
.unwrap();
}

mod from_a_loopback_ip {
Expand All @@ -918,6 +951,7 @@ mod tests {

use aquatic_udp_protocol::{InfoHash as AquaticInfoHash, PeerId as AquaticPeerId};

use crate::protocol::utils::get_connection_id;
use crate::statistics::StatsTracker;
use crate::tracker::tracker::TorrentTracker;
use crate::udp::handle_announce;
Expand All @@ -940,14 +974,16 @@ mod tests {
let info_hash = AquaticInfoHash([0u8; 20]);
let peer_id = AquaticPeerId([255u8; 20]);

let remote_addr = SocketAddr::new(IpAddr::V6(client_ip_v6), client_port);

let request = AnnounceRequestBuilder::default()
.with_connection_id(get_connection_id(&remote_addr))
.with_info_hash(info_hash)
.with_peer_id(peer_id)
.with_ip_address(client_ip_v4)
.with_port(client_port)
.into();

let remote_addr = SocketAddr::new(IpAddr::V6(client_ip_v6), client_port);
handle_announce(remote_addr, &request, tracker.clone()).await.unwrap();

let peers = tracker.get_all_torrent_peers(&info_hash.0.into()).await;
Expand Down Expand Up @@ -1036,7 +1072,7 @@ mod tests {
let info_hashes = vec![*info_hash];

ScrapeRequest {
connection_id: get_connection_id(remote_addr),
connection_id: get_connection_id(&remote_addr),
transaction_id: TransactionId(0i32),
info_hashes,
}
Expand Down Expand Up @@ -1181,7 +1217,7 @@ mod tests {
let info_hashes = vec![info_hash];

ScrapeRequest {
connection_id: get_connection_id(remote_addr),
connection_id: get_connection_id(&remote_addr),
transaction_id: TransactionId(0i32),
info_hashes,
}
Expand Down

0 comments on commit c8fa30a

Please sign in to comment.