More tests and one fix

This commit is contained in:
Dennis Schwerdel 2016-02-02 23:58:47 +01:00
parent 693f9692a3
commit 1db4fea091
3 changed files with 27 additions and 3 deletions

View File

@ -5,9 +5,11 @@ This project follows [semantic versioning](http://semver.org).
### UNRELEASED ### UNRELEASED
- [added] Script for performance measurements - [added] Script for performance measurements
- [added] Added more tests
- [changed] Using display format for addresses - [changed] Using display format for addresses
- [changed] Updated dependencies - [changed] Updated dependencies
- [changed] New measurements - [changed] New measurements
- [fixed] Fixed wrong hex address formatting
### v0.4.3 (2016-02-02) ### v0.4.3 (2016-02-02)

View File

@ -3,7 +3,7 @@ use std::str::FromStr;
use super::ethernet::{Frame, SwitchTable}; use super::ethernet::{Frame, SwitchTable};
use super::ip::{RoutingTable, Packet}; use super::ip::{RoutingTable, Packet};
use super::types::{Protocol, Address, Range, Table}; use super::types::{Error, Protocol, Address, Range, Table};
use super::udpmessage::{Options, Message, decode, encode}; use super::udpmessage::{Options, Message, decode, encode};
use super::crypto::{Crypto, CryptoMethod}; use super::crypto::{Crypto, CryptoMethod};
@ -296,6 +296,28 @@ fn address_parse_fmt() {
assert_eq!(format!("{}", Address::from_str("78:2d:16:05:01:02").unwrap()), "78:2d:16:05:01:02"); assert_eq!(format!("{}", Address::from_str("78:2d:16:05:01:02").unwrap()), "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: [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::from_str("0001:0203:0405:0607:0809:0a0b:0c0d:0e0f").unwrap()), "0001:0203:0405:0607:0809:0a0b:0c0d:0e0f"); assert_eq!(format!("{}", Address::from_str("0001:0203:0405:0607:0809:0a0b:0c0d:0e0f").unwrap()), "0001:0203:0405:0607:0809:0a0b:0c0d:0e0f");
assert_eq!(format!("{:?}", Address{data: [1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0], len: 2}), "0102");
assert_eq!(Address::from_str(""), Err(Error::ParseError("Failed to parse address")));
}
#[test]
fn address_decode_encode() {
let mut buf = [0; 32];
let addr = Address::from_str("120.45.22.5").unwrap();
assert_eq!(addr.write_to(&mut buf), 5);
assert_eq!(&buf[0..5], &[4, 120, 45, 22, 5]);
assert_eq!((addr, 5), Address::read_from(&buf).unwrap());
assert_eq!(addr, Address::read_from_fixed(&buf[1..], 4).unwrap());
let addr = Address::from_str("78:2d:16:05:01:02").unwrap();
assert_eq!(addr.write_to(&mut buf), 7);
assert_eq!(&buf[0..7], &[6, 0x78, 0x2d, 0x16, 0x05, 0x01, 0x02]);
assert_eq!((addr, 7), Address::read_from(&buf).unwrap());
assert_eq!(addr, Address::read_from_fixed(&buf[1..], 6).unwrap());
assert_eq!(Address::read_from(&buf[0..0]), Err(Error::ParseError("Address too short")));
buf[0] = 100;
assert_eq!(Address::read_from(&buf), Err(Error::ParseError("Invalid address, too long")));
buf[0] = 5;
assert_eq!(Address::read_from(&buf[0..4]), Err(Error::ParseError("Address too short")));
} }
#[test] #[test]

View File

@ -84,7 +84,7 @@ impl fmt::Display for Address {
}, },
16 => write!(formatter, "{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}", 16 => write!(formatter, "{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}:{:02x}{:02x}",
d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15]), d[0], d[1], d[2], d[3], d[4], d[5], d[6], d[7], d[8], d[9], d[10], d[11], d[12], d[13], d[14], d[15]),
_ => write!(formatter, "{}", bytes_to_hex(d)) _ => write!(formatter, "{}", bytes_to_hex(&d[0..self.len as usize]))
} }
} }
} }
@ -222,7 +222,7 @@ pub trait Protocol: Sized {
fn parse(&[u8]) -> Result<(Address, Address), Error>; fn parse(&[u8]) -> Result<(Address, Address), Error>;
} }
#[derive(Debug)] #[derive(Debug, PartialEq)]
pub enum Error { pub enum Error {
ParseError(&'static str), ParseError(&'static str),
WrongNetwork(Option<NetworkId>), WrongNetwork(Option<NetworkId>),