More tests

This commit is contained in:
Dennis Schwerdel 2015-11-27 00:40:11 +01:00
parent 01d26218b6
commit f09557d43b
2 changed files with 24 additions and 6 deletions

View File

@ -44,11 +44,15 @@ fn encode_message_peers() {
use std::str::FromStr; use std::str::FromStr;
let mut options = Options::default(); let mut options = Options::default();
let mut crypto = Crypto::None; let mut crypto = Crypto::None;
let msg = Message::Peers(vec![SocketAddr::from_str("1.2.3.4:123").unwrap(), SocketAddr::from_str("5.6.7.8:12345").unwrap()]); let msg = Message::Peers(vec![SocketAddr::from_str("1.2.3.4:123").unwrap(), SocketAddr::from_str("5.6.7.8:12345").unwrap(), SocketAddr::from_str("[0001:0203:0405:0607:0809:0a0b:0c0d:0e0f]:6789").unwrap()]);
let mut buf = [0; 1024]; let mut buf = [0; 1024];
let size = encode(&mut options, &msg, &mut buf[..], &mut crypto); let size = encode(&mut options, &msg, &mut buf[..], &mut crypto);
assert_eq!(size, 22); assert_eq!(size, 40);
assert_eq!(&buf[..size], &[118,112,110,1,0,0,0,1,2,1,2,3,4,0,123,5,6,7,8,48,57,0]); let should = [118,112,110,1,0,0,0,1,2,1,2,3,4,0,123,5,6,7,8,48,57,1,0,1,2,3,4,5,6,7,
8,9,10,11,12,13,14,15,26,133];
for i in 0..size {
assert_eq!(buf[i], should[i]);
}
let (options2, msg2) = decode(&mut buf[..size], &mut crypto).unwrap(); let (options2, msg2) = decode(&mut buf[..size], &mut crypto).unwrap();
assert_eq!(options, options2); assert_eq!(options, options2);
assert_eq!(msg, msg2); assert_eq!(msg, msg2);
@ -181,3 +185,17 @@ fn address_fmt() {
assert_eq!(format!("{}", Address{data: [3,56,120,45,22,5,1,2,0,0,0,0,0,0,0,0], len: 8}), "vlan824/78:2d:16:05:01:02"); assert_eq!(format!("{}", Address{data: [3,56,120,45,22,5,1,2,0,0,0,0,0,0,0,0], len: 8}), "vlan824/78:2d:16:05:01:02");
assert_eq!(format!("{}", Address{data: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], len: 16}), "0001:0203:0405:0607:0809:0a0b:0c0d:0e0f"); assert_eq!(format!("{}", Address{data: [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], len: 16}), "0001:0203:0405:0607:0809:0a0b:0c0d:0e0f");
} }
#[test]
fn message_fmt() {
assert_eq!(format!("{:?}", Message::Data(&[1,2,3,4,5])), "Data(5 bytes)");
assert_eq!(format!("{:?}", Message::Peers(vec![SocketAddr::from_str("1.2.3.4:123").unwrap(),
SocketAddr::from_str("5.6.7.8:12345").unwrap(),
SocketAddr::from_str("[0001:0203:0405:0607:0809:0a0b:0c0d:0e0f]:6789").unwrap()])),
"Peers [1.2.3.4:123, 5.6.7.8:12345, [1:203:405:607:809:a0b:c0d:e0f]:6789]");
assert_eq!(format!("{:?}", Message::Init(0, vec![
Range{base: Address{data: [0,1,2,3,0,0,0,0,0,0,0,0,0,0,0,0], len: 4}, prefix_len: 24},
Range{base: Address{data: [0,1,2,3,4,5,0,0,0,0,0,0,0,0,0,0], len: 6}, prefix_len: 16}
])), "Init(stage=0, [0.1.2.3/24, 00:01:02:03:04:05/16])");
assert_eq!(format!("{:?}", Message::Close), "Close");
}

View File

@ -72,7 +72,7 @@ pub enum Message<'a> {
impl<'a> fmt::Debug for Message<'a> { impl<'a> fmt::Debug for Message<'a> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self { match self {
&Message::Data(ref data) => write!(formatter, "Data(data: {} bytes)", data.len()), &Message::Data(ref data) => write!(formatter, "Data({} bytes)", data.len()),
&Message::Peers(ref peers) => { &Message::Peers(ref peers) => {
try!(write!(formatter, "Peers [")); try!(write!(formatter, "Peers ["));
let mut first = true; let mut first = true;
@ -81,7 +81,7 @@ impl<'a> fmt::Debug for Message<'a> {
try!(write!(formatter, ", ")); try!(write!(formatter, ", "));
} }
first = false; first = false;
try!(p.fmt(formatter)); try!(write!(formatter, "{}", p));
} }
write!(formatter, "]") write!(formatter, "]")
}, },