Browse Source

Add tests for peer message encoding and decoding.

wip
Titouan Rigoudy 7 years ago
parent
commit
1e633d9a43
1 changed files with 33 additions and 2 deletions
  1. +33
    -2
      src/proto/peer/message.rs

+ 33
- 2
src/proto/peer/message.rs View File

@ -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<T: Debug + Eq + ProtoDecode + ProtoEncode>(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,
}));
}
}

Loading…
Cancel
Save