bittorrent_udp: implement quickcheck::Arbitrary for request types

This commit is contained in:
Joakim Frostegård 2020-04-11 14:43:58 +02:00
parent 9a1143ffac
commit 554895a9df
2 changed files with 91 additions and 1 deletions

View file

@ -61,6 +61,34 @@ impl quickcheck::Arbitrary for IpVersion {
}
#[cfg(test)]
impl quickcheck::Arbitrary for InfoHash {
fn arbitrary<G: quickcheck::Gen>(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: quickcheck::Gen>(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: quickcheck::Gen>(g: &mut G) -> Self {

View file

@ -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<ConnectRequest> for Request {
fn from(r: ConnectRequest) -> Self {
Self::Connect(r)
@ -67,4 +69,64 @@ impl From<ScrapeRequest> for Request {
fn from(r: ScrapeRequest) -> Self {
Self::Scrape(r)
}
}
#[cfg(test)]
impl quickcheck::Arbitrary for AnnounceEvent {
fn arbitrary<G: quickcheck::Gen>(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: quickcheck::Gen>(g: &mut G) -> Self {
Self {
transaction_id: TransactionId(i32::arbitrary(g)),
}
}
}
#[cfg(test)]
impl quickcheck::Arbitrary for AnnounceRequest {
fn arbitrary<G: quickcheck::Gen>(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: quickcheck::Gen>(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,
}
}
}