Browse Source

Add round-trip encoding test for basic protocol types.

wip
Titouan Rigoudy 7 years ago
parent
commit
0a58bfde91
3 changed files with 62 additions and 21 deletions
  1. +60
    -1
      src/proto/codec.rs
  2. +1
    -10
      src/proto/peer/message.rs
  3. +1
    -10
      src/proto/server/request.rs

+ 60
- 1
src/proto/codec.rs View File

@ -338,9 +338,10 @@ impl<T: ProtoEncode> ProtoEncode for Vec<T> {
*=======*/
#[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<T: fmt::Debug + Eq + PartialEq + 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);
}
// Helper for succinctness in tests below.
fn new_cursor(vec: Vec<u8>) -> io::Cursor<BytesMut> {
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])
}
}

+ 1
- 10
src/proto/peer/message.rs View File

@ -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();


+ 1
- 10
src/proto/server/request.rs View File

@ -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();


Loading…
Cancel
Save