From 0107b3a77e29cb17073ff4b11531143fe253045f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20Frosteg=C3=A5rd?= Date: Sat, 30 Oct 2021 16:50:53 +0200 Subject: [PATCH] aquatic_udp: add (failing) test for writing ipv6 announce responses --- aquatic_udp/src/lib/common/mod.rs | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/aquatic_udp/src/lib/common/mod.rs b/aquatic_udp/src/lib/common/mod.rs index bcc073b..e8003c7 100644 --- a/aquatic_udp/src/lib/common/mod.rs +++ b/aquatic_udp/src/lib/common/mod.rs @@ -155,6 +155,10 @@ impl TorrentMaps { #[cfg(test)] mod tests { + use std::net::{IpAddr, Ipv6Addr}; + + use crate::{common::MAX_PACKET_SIZE, config::Config}; + #[test] fn test_peer_status_from_event_and_bytes_left() { use crate::common::*; @@ -175,4 +179,36 @@ mod tests { assert_eq!(Seeding, f(AnnounceEvent::None, NumberOfBytes(0))); assert_eq!(Leeching, f(AnnounceEvent::None, NumberOfBytes(1))); } + + // Assumes that announce response with maximum amount of ipv6 peers will + // be the longest + #[test] + fn test_max_package_size() { + use aquatic_udp_protocol::*; + + let config = Config::default(); + + let peers = ::std::iter::repeat(ResponsePeer { + ip_address: IpAddr::V6(Ipv6Addr::new(1, 1, 1, 1, 1, 1, 1, 1)), + port: Port(1), + }) + .take(config.protocol.max_response_peers) + .collect(); + + let response = Response::Announce(AnnounceResponse { + transaction_id: TransactionId(1), + announce_interval: AnnounceInterval(1), + seeders: NumberOfPeers(1), + leechers: NumberOfPeers(1), + peers, + }); + + let mut buf = Vec::new(); + + response.write(&mut buf, IpVersion::IPv6).unwrap(); + + println!("Buffer len: {}", buf.len()); + + assert!(buf.len() <= MAX_PACKET_SIZE); + } }