From 1e633d9a43e5edbe90b8ebe896209ad93a86b1f3 Mon Sep 17 00:00:00 2001 From: Titouan Rigoudy Date: Wed, 27 Dec 2017 12:52:54 -0500 Subject: [PATCH] Add tests for peer message encoding and decoding. --- src/proto/peer/message.rs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/src/proto/peer/message.rs b/src/proto/peer/message.rs index de47ff2..f1c4e63 100644 --- a/src/proto/peer/message.rs +++ b/src/proto/peer/message.rs @@ -9,7 +9,7 @@ use proto::peer::constants::*; *=========*/ /// This enum contains all the possible messages peers can exchange. -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Eq, PartialEq)] pub enum Message { PierceFirewall(u32), PeerInit(PeerInit), @@ -94,7 +94,7 @@ impl WriteToPacket for Message { } } -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Eq, PartialEq)] pub struct PeerInit { pub user_name: String, pub connection_type: String, @@ -144,3 +144,34 @@ impl ProtoDecode for PeerInit { }) } } + +#[cfg(test)] +mod tests { + use std::fmt::Debug; + use std::io; + + use bytes::BytesMut; + + use proto::{DecodeError, ProtoDecode, ProtoDecoder, ProtoEncode, ProtoEncoder}; + + use super::*; + + fn roundtrip(input: T) { + let mut bytes = BytesMut::new(); + input.encode(&mut ProtoEncoder::new(&mut bytes)).unwrap(); + + let mut cursor = io::Cursor::new(bytes); + let output = T::decode(&mut ProtoDecoder::new(&mut cursor)).unwrap(); + + assert_eq!(output, input); + } + + #[test] + fn roundtrip_peer_init() { + roundtrip(Message::PeerInit(PeerInit { + user_name: "alice".to_string(), + connection_type: "P".to_string(), + token: 1337, + })); + } +}