diff --git a/aquatic_http_protocol/src/request.rs b/aquatic_http_protocol/src/request.rs index 9b8e2ee..eeefa12 100644 --- a/aquatic_http_protocol/src/request.rs +++ b/aquatic_http_protocol/src/request.rs @@ -355,13 +355,22 @@ mod tests { #[test] fn quickcheck_serde_identity_request() { fn prop(request: Request) -> TestResult { - if let Request::Announce(AnnounceRequest { - key: Some(ref key), .. - }) = request - { - if key.len() > 30 { - return TestResult::discard(); + match request { + Request::Announce(AnnounceRequest { + key: Some(ref key), .. + }) => { + if key.len() > 30 { + return TestResult::discard(); + } } + Request::Scrape(ScrapeRequest { + ref info_hashes, + }) => { + if info_hashes.is_empty() { + return TestResult::discard(); + } + } + _ => {} } let mut bytes = Vec::new(); diff --git a/aquatic_udp/src/lib/common/mod.rs b/aquatic_udp/src/lib/common/mod.rs index bcc073b..cf47c8c 100644 --- a/aquatic_udp/src/lib/common/mod.rs +++ b/aquatic_udp/src/lib/common/mod.rs @@ -14,7 +14,7 @@ use crate::config::Config; pub mod handlers; pub mod network; -pub const MAX_PACKET_SIZE: usize = 4096; +pub const MAX_PACKET_SIZE: usize = 8192; pub trait Ip: Hash + PartialEq + Eq + Clone + Copy { fn ip_addr(self) -> IpAddr; @@ -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); + } }