Merge pull request #14 from greatest-ape/bug-fixes

Bug fixes
This commit is contained in:
Joakim Frostegård 2021-10-30 17:38:58 +02:00 committed by GitHub
commit 3a598956a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 7 deletions

View file

@ -355,14 +355,23 @@ mod tests {
#[test] #[test]
fn quickcheck_serde_identity_request() { fn quickcheck_serde_identity_request() {
fn prop(request: Request) -> TestResult { fn prop(request: Request) -> TestResult {
if let Request::Announce(AnnounceRequest { match request {
Request::Announce(AnnounceRequest {
key: Some(ref key), .. key: Some(ref key), ..
}) = request }) => {
{
if key.len() > 30 { if key.len() > 30 {
return TestResult::discard(); return TestResult::discard();
} }
} }
Request::Scrape(ScrapeRequest {
ref info_hashes,
}) => {
if info_hashes.is_empty() {
return TestResult::discard();
}
}
_ => {}
}
let mut bytes = Vec::new(); let mut bytes = Vec::new();

View file

@ -14,7 +14,7 @@ use crate::config::Config;
pub mod handlers; pub mod handlers;
pub mod network; 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 { pub trait Ip: Hash + PartialEq + Eq + Clone + Copy {
fn ip_addr(self) -> IpAddr; fn ip_addr(self) -> IpAddr;
@ -155,6 +155,10 @@ impl TorrentMaps {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::net::{IpAddr, Ipv6Addr};
use crate::{common::MAX_PACKET_SIZE, config::Config};
#[test] #[test]
fn test_peer_status_from_event_and_bytes_left() { fn test_peer_status_from_event_and_bytes_left() {
use crate::common::*; use crate::common::*;
@ -175,4 +179,36 @@ mod tests {
assert_eq!(Seeding, f(AnnounceEvent::None, NumberOfBytes(0))); assert_eq!(Seeding, f(AnnounceEvent::None, NumberOfBytes(0)));
assert_eq!(Leeching, f(AnnounceEvent::None, NumberOfBytes(1))); 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);
}
} }