diff --git a/src/proto/codec.rs b/src/proto/codec.rs index 2b910d9..eed20b8 100644 --- a/src/proto/codec.rs +++ b/src/proto/codec.rs @@ -338,9 +338,10 @@ impl ProtoEncode for Vec { *=======*/ #[cfg(test)] -mod tests { +pub mod tests { use super::{ProtoDecoder, ProtoEncoder, ProtoDecode, ProtoEncode}; + use std::fmt; use std::io; use std::net; use std::u16; @@ -348,6 +349,16 @@ mod tests { use bytes::{Buf, BytesMut}; + pub 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); + } + // Helper for succinctness in tests below. fn new_cursor(vec: Vec) -> io::Cursor { io::Cursor::new(BytesMut::from(vec)) @@ -387,6 +398,13 @@ mod tests { } } + #[test] + fn roundtrip_u32() { + for &(val, _) in &U32_ENCODINGS { + roundtrip(val) + } + } + #[test] fn encode_bool() { let mut bytes = BytesMut::from(vec![13]); @@ -418,6 +436,12 @@ mod tests { ProtoDecoder::new(&mut cursor).decode_bool().unwrap(); } + #[test] + fn roundtrip_bool() { + roundtrip(false); + roundtrip(true); + } + #[test] fn encode_u16() { for &(val, ref encoded_bytes) in &U32_ENCODINGS { @@ -450,6 +474,22 @@ mod tests { } } + #[test] + #[should_panic] + fn decode_u16_invalid() { + let mut cursor = new_cursor(vec![0, 0, 1, 0]); + ProtoDecoder::new(&mut cursor).decode_u16().unwrap(); + } + + #[test] + fn roundtrip_u16() { + for &(val, _) in &U32_ENCODINGS { + if val <= u16::MAX as u32 { + roundtrip(val) + } + } + } + #[test] fn encode_ipv4() { for &(val, ref encoded_bytes) in &U32_ENCODINGS { @@ -475,6 +515,13 @@ mod tests { } } + #[test] + fn roundtrip_ipv4() { + for &(val, _) in &U32_ENCODINGS { + roundtrip(net::Ipv4Addr::from(val)) + } + } + // A few strings and their corresponding encodings. const STRING_ENCODINGS: [(&'static str, &'static [u8]); 3] = [ @@ -515,6 +562,13 @@ mod tests { } } + #[test] + fn roundtrip_string() { + for &(string, _) in &STRING_ENCODINGS { + roundtrip(string.to_string()) + } + } + #[test] fn encode_u32_vector() { let mut vec = vec![]; @@ -545,4 +599,9 @@ mod tests { assert_eq!(vec, expected_vec); assert_eq!(cursor.remaining(), 0); } + + #[test] + fn roundtrip_u32_vector() { + roundtrip(vec![0u32, 1, 2, 3, 4, 5, 6, 7, 8, 9]) + } } diff --git a/src/proto/peer/message.rs b/src/proto/peer/message.rs index 5452c6f..632821d 100644 --- a/src/proto/peer/message.rs +++ b/src/proto/peer/message.rs @@ -155,19 +155,10 @@ mod tests { use bytes::BytesMut; use proto::{DecodeError, ProtoDecode, ProtoDecoder, ProtoEncode, ProtoEncoder}; + use proto::codec::tests::roundtrip; use super::*; - fn roundtrip(input: Message) { - let mut bytes = BytesMut::new(); - input.encode(&mut ProtoEncoder::new(&mut bytes)).unwrap(); - - let mut cursor = io::Cursor::new(bytes); - let output = Message::decode(&mut ProtoDecoder::new(&mut cursor)).unwrap(); - - assert_eq!(output, input); - } - #[test] fn invalid_code() { let mut bytes = BytesMut::new(); diff --git a/src/proto/server/request.rs b/src/proto/server/request.rs index 27e8f75..f37dd74 100644 --- a/src/proto/server/request.rs +++ b/src/proto/server/request.rs @@ -590,19 +590,10 @@ mod tests { use bytes::BytesMut; use proto::{DecodeError, ProtoDecode, ProtoDecoder, ProtoEncode, ProtoEncoder}; + use proto::codec::tests::roundtrip; use super::*; - fn roundtrip(input: ServerRequest) { - let mut bytes = BytesMut::new(); - input.encode(&mut ProtoEncoder::new(&mut bytes)).unwrap(); - - let mut cursor = io::Cursor::new(bytes); - let output = ServerRequest::decode(&mut ProtoDecoder::new(&mut cursor)).unwrap(); - - assert_eq!(output, input); - } - #[test] fn invalid_code() { let mut bytes = BytesMut::new();