From 1db4fea091fab800779cb582d1dfd03fb7c12947 Mon Sep 17 00:00:00 2001 From: Dennis Schwerdel Date: Tue, 2 Feb 2016 23:58:47 +0100 Subject: [PATCH] More tests and one fix --- CHANGELOG.md | 2 ++ src/tests.rs | 24 +++++++++++++++++++++++- src/types.rs | 4 ++-- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3631368..c2208f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,11 @@ This project follows [semantic versioning](http://semver.org). ### UNRELEASED - [added] Script for performance measurements +- [added] Added more tests - [changed] Using display format for addresses - [changed] Updated dependencies - [changed] New measurements +- [fixed] Fixed wrong hex address formatting ### v0.4.3 (2016-02-02) diff --git a/src/tests.rs b/src/tests.rs index 2adf857..5aacf99 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -3,7 +3,7 @@ use std::str::FromStr; use super::ethernet::{Frame, SwitchTable}; 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::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{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{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] diff --git a/src/types.rs b/src/types.rs index b5b060a..6d4f3e7 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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}", 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>; } -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum Error { ParseError(&'static str), WrongNetwork(Option),