diff --git a/bittorrent_udp/src/types/common.rs b/bittorrent_udp/src/types/common.rs index 318c553..57e7233 100644 --- a/bittorrent_udp/src/types/common.rs +++ b/bittorrent_udp/src/types/common.rs @@ -61,6 +61,34 @@ impl quickcheck::Arbitrary for IpVersion { } +#[cfg(test)] +impl quickcheck::Arbitrary for InfoHash { + fn arbitrary(g: &mut G) -> Self { + let mut bytes = [0u8; 20]; + + for byte in bytes.iter_mut() { + *byte = u8::arbitrary(g); + } + + Self(bytes) + } +} + + +#[cfg(test)] +impl quickcheck::Arbitrary for PeerId { + fn arbitrary(g: &mut G) -> Self { + let mut bytes = [0u8; 20]; + + for byte in bytes.iter_mut() { + *byte = u8::arbitrary(g); + } + + Self(bytes) + } +} + + #[cfg(test)] impl quickcheck::Arbitrary for ResponsePeer { fn arbitrary(g: &mut G) -> Self { diff --git a/bittorrent_udp/src/types/request.rs b/bittorrent_udp/src/types/request.rs index 7e7ccbd..3011738 100644 --- a/bittorrent_udp/src/types/request.rs +++ b/bittorrent_udp/src/types/request.rs @@ -11,11 +11,13 @@ pub enum AnnounceEvent { None } + #[derive(PartialEq, Eq, Clone, Debug)] pub struct ConnectRequest { pub transaction_id: TransactionId } + #[derive(PartialEq, Eq, Clone, Debug)] pub struct AnnounceRequest { pub connection_id: ConnectionId, @@ -32,6 +34,7 @@ pub struct AnnounceRequest { pub port: Port } + #[derive(PartialEq, Eq, Clone, Debug)] pub struct ScrapeRequest { pub connection_id: ConnectionId, @@ -48,7 +51,6 @@ pub enum Request { } - impl From for Request { fn from(r: ConnectRequest) -> Self { Self::Connect(r) @@ -67,4 +69,64 @@ impl From for Request { fn from(r: ScrapeRequest) -> Self { Self::Scrape(r) } +} + + +#[cfg(test)] +impl quickcheck::Arbitrary for AnnounceEvent { + fn arbitrary(g: &mut G) -> Self { + match (bool::arbitrary(g), bool::arbitrary(g)){ + (false, false) => Self::Started, + (true, false) => Self::Started, + (false, true) => Self::Completed, + (true, true) => Self::None, + } + } +} + + +#[cfg(test)] +impl quickcheck::Arbitrary for ConnectRequest { + fn arbitrary(g: &mut G) -> Self { + Self { + transaction_id: TransactionId(i32::arbitrary(g)), + } + } +} + + +#[cfg(test)] +impl quickcheck::Arbitrary for AnnounceRequest { + fn arbitrary(g: &mut G) -> Self { + Self { + connection_id: ConnectionId(i64::arbitrary(g)), + transaction_id: TransactionId(i32::arbitrary(g)), + info_hash: InfoHash::arbitrary(g), + peer_id: PeerId::arbitrary(g), + bytes_downloaded: NumberOfBytes(i64::arbitrary(g)), + bytes_uploaded: NumberOfBytes(i64::arbitrary(g)), + bytes_left: NumberOfBytes(i64::arbitrary(g)), + event: AnnounceEvent::arbitrary(g), + ip_address: None, + key: PeerKey(u32::arbitrary(g)), + peers_wanted: NumberOfPeers(i32::arbitrary(g)), + port: Port(u16::arbitrary(g)) + } + } +} + + +#[cfg(test)] +impl quickcheck::Arbitrary for ScrapeRequest { + fn arbitrary(g: &mut G) -> Self { + let info_hashes = (0..u8::arbitrary(g)).map(|_| { + InfoHash::arbitrary(g) + }).collect(); + + Self { + connection_id: ConnectionId(i64::arbitrary(g)), + transaction_id: TransactionId(i32::arbitrary(g)), + info_hashes, + } + } } \ No newline at end of file