more tests

This commit is contained in:
Dennis Schwerdel 2015-11-23 21:43:00 +01:00
parent 4fd1a8df52
commit 04011a5766
2 changed files with 34 additions and 8 deletions

View File

@ -15,14 +15,14 @@ impl fmt::Debug for Address {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self.0.len() { match self.0.len() {
4 => write!(formatter, "{}.{}.{}.{}", self.0[0], self.0[1], self.0[2], self.0[3]), 4 => write!(formatter, "{}.{}.{}.{}", self.0[0], self.0[1], self.0[2], self.0[3]),
6 => write!(formatter, "{:x}:{:x}:{:x}:{:x}:{:x}:{:x}", 6 => write!(formatter, "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5]), self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5]),
8 => { 8 => {
let vlan = u16::from_be( *unsafe { as_obj(&self.0[0..1]) }); let vlan = u16::from_be( *unsafe { as_obj(&self.0[0..2]) });
write!(formatter, "vlan{}/{:x}:{:x}:{:x}:{:x}:{:x}:{:x}", write!(formatter, "vlan{}/{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
vlan, self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5]) vlan, self.0[2], self.0[3], self.0[4], self.0[5], self.0[6], self.0[7])
}, },
16 => write!(formatter, "{:x}{:x}:{:x}{:x}:{:x}{:x}:{:x}{:x}:{:x}{:x}:{:x}{:x}:{:x}{:x}:{:x}{:x}", 16 => write!(formatter, "{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}",
self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5], self.0[6], self.0[7], self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5], self.0[6], self.0[7],
self.0[8], self.0[9], self.0[10], self.0[11], self.0[12], self.0[13], self.0[14], self.0[15] self.0[8], self.0[9], self.0[10], self.0[11], self.0[12], self.0[13], self.0[14], self.0[15]
), ),
@ -127,3 +127,12 @@ pub enum Error {
TunTapDevError(&'static str), TunTapDevError(&'static str),
CryptoError(&'static str) CryptoError(&'static str)
} }
#[test]
fn address_fmt() {
assert_eq!(format!("{:?}", Address(vec![120,45,22,5])), "120.45.22.5");
assert_eq!(format!("{:?}", Address(vec![120,45,22,5,1,2])), "78:2d:16:05:01:02");
assert_eq!(format!("{:?}", Address(vec![3,56,120,45,22,5,1,2])), "vlan824/78:2d:16:05:01:02");
assert_eq!(format!("{:?}", Address(vec![0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])), "0001:0203:0405:0607:0809:0a0b:0c0d:0e0f");
}

View File

@ -277,6 +277,22 @@ fn encode_message_packet() {
assert_eq!(msg, msg2); assert_eq!(msg, msg2);
} }
#[cfg(feature = "crypto")]
#[test]
fn encode_message_encrypted() {
let mut options = Options::default();
let mut crypto = Crypto::from_shared_key("test");
let payload = [1,2,3,4,5];
let msg = Message::Data(&payload);
let mut buf = [0; 1024];
let size = encode(&mut options, &msg, &mut buf[..], &mut crypto);
assert_eq!(size, 53);
assert_eq!(&buf[..8], &[118,112,110,1,0,0,2,0]);
let (options2, msg2) = decode(&mut buf[..size], &mut crypto).unwrap();
assert_eq!(options, options2);
assert_eq!(msg, msg2);
}
#[test] #[test]
fn encode_message_peers() { fn encode_message_peers() {
use std::str::FromStr; use std::str::FromStr;
@ -311,12 +327,13 @@ fn encode_option_network_id() {
fn encode_message_init() { fn encode_message_init() {
let mut options = Options::default(); let mut options = Options::default();
let mut crypto = Crypto::None; let mut crypto = Crypto::None;
let addrs = vec![]; let addrs = vec![Range{base: Address(vec![0,1,2,3]), prefix_len: 24},
Range{base: Address(vec![0,1,2,3,4,5]), prefix_len: 16}];
let msg = Message::Init(addrs); let msg = Message::Init(addrs);
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, 9); assert_eq!(size, 23);
assert_eq!(&buf[..size], &[118,112,110,1,0,0,0,2,0]); assert_eq!(&buf[..size], &[118,112,110,1,0,0,0,2,2,4,0,1,2,3,24,6,0,1,2,3,4,5,16]);
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);