Even more tests

This commit is contained in:
Dennis Schwerdel 2016-11-28 12:14:56 +01:00
parent e323ac38b8
commit dd804fe6df
4 changed files with 25 additions and 13 deletions

View File

@ -77,7 +77,7 @@ fn message_decode(b: &mut Bencher) {
#[bench] #[bench]
fn switch_learn(b: &mut Bencher) { fn switch_learn(b: &mut Bencher) {
let mut table = SwitchTable::new(10); let mut table = SwitchTable::new(10, 0);
let addr = Address::from_str("12:34:56:78:90:ab").unwrap(); let addr = Address::from_str("12:34:56:78:90:ab").unwrap();
let peer = "1.2.3.4:5678".to_socket_addrs().unwrap().next().unwrap(); let peer = "1.2.3.4:5678".to_socket_addrs().unwrap().next().unwrap();
b.iter(|| { b.iter(|| {
@ -88,7 +88,7 @@ fn switch_learn(b: &mut Bencher) {
#[bench] #[bench]
fn switch_lookup(b: &mut Bencher) { fn switch_lookup(b: &mut Bencher) {
let mut table = SwitchTable::new(10); let mut table = SwitchTable::new(10, 0);
let addr = Address::from_str("12:34:56:78:90:ab").unwrap(); let addr = Address::from_str("12:34:56:78:90:ab").unwrap();
let peer = "1.2.3.4:5678".to_socket_addrs().unwrap().next().unwrap(); let peer = "1.2.3.4:5678".to_socket_addrs().unwrap().next().unwrap();
table.learn(addr.clone(), None, peer); table.learn(addr.clone(), None, peer);
@ -152,7 +152,7 @@ fn epoll_wait(b: &mut Bencher) {
fn handle_interface_data(b: &mut Bencher) { fn handle_interface_data(b: &mut Bencher) {
let mut node = GenericCloud::<Frame>::new( let mut node = GenericCloud::<Frame>::new(
MAGIC, Device::dummy("vpncloud0", "/dev/null", Type::Tap).unwrap(), 0, MAGIC, Device::dummy("vpncloud0", "/dev/null", Type::Tap).unwrap(), 0,
Box::new(SwitchTable::new(300)), 1800, true, true, vec![], Crypto::None, None Box::new(SwitchTable::new(300, 10)), 1800, true, true, vec![], Crypto::None, None
); );
let mut data = [0; 1500]; let mut data = [0; 1500];
data[105] = 45; data[105] = 45;
@ -166,7 +166,7 @@ fn handle_interface_data(b: &mut Bencher) {
fn handle_net_message(b: &mut Bencher) { fn handle_net_message(b: &mut Bencher) {
let mut node = GenericCloud::<Frame>::new( let mut node = GenericCloud::<Frame>::new(
MAGIC, Device::dummy("vpncloud0", "/dev/null", Type::Tap).unwrap(), 0, MAGIC, Device::dummy("vpncloud0", "/dev/null", Type::Tap).unwrap(), 0,
Box::new(SwitchTable::new(300)), 1800, true, true, vec![], Crypto::None, None Box::new(SwitchTable::new(300, 10)), 1800, true, true, vec![], Crypto::None, None
); );
let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 1)); let addr = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), 1));
let mut data = [0; 1500]; let mut data = [0; 1500];

View File

@ -71,13 +71,15 @@ pub struct SwitchTable {
/// The table storing the actual mapping /// The table storing the actual mapping
table: HashMap<Address, SwitchTableValue, Hash>, table: HashMap<Address, SwitchTableValue, Hash>,
/// Timeout period for forgetting learnt addresses /// Timeout period for forgetting learnt addresses
timeout: Duration timeout: Duration,
// Timeout period for not overwriting learnt addresses
protection_period: Duration,
} }
impl SwitchTable { impl SwitchTable {
/// Creates a new switch table /// Creates a new switch table
pub fn new(timeout: Duration) -> Self { pub fn new(timeout: Duration, protection_period: Duration) -> Self {
SwitchTable{table: HashMap::default(), timeout: timeout} SwitchTable{table: HashMap::default(), timeout: timeout, protection_period: protection_period}
} }
} }
@ -108,7 +110,7 @@ impl Table for SwitchTable {
}, },
Entry::Occupied(mut entry) => { Entry::Occupied(mut entry) => {
let mut entry = entry.get_mut(); let mut entry = entry.get_mut();
if entry.timeout + 10 >= deadline { if entry.timeout + self.protection_period as Time > deadline {
// Do not override recently learnt entries // Do not override recently learnt entries
return return
} }

View File

@ -150,12 +150,12 @@ fn run<T: Protocol> (config: Config) {
let peer_timeout = config.peer_timeout; let peer_timeout = config.peer_timeout;
let (learning, broadcasting, table): (bool, bool, Box<Table>) = match config.mode { let (learning, broadcasting, table): (bool, bool, Box<Table>) = match config.mode {
Mode::Normal => match config.device_type { Mode::Normal => match config.device_type {
Type::Tap => (true, true, Box::new(SwitchTable::new(dst_timeout))), Type::Tap => (true, true, Box::new(SwitchTable::new(dst_timeout, 10))),
Type::Tun => (false, false, Box::new(RoutingTable::new())) Type::Tun => (false, false, Box::new(RoutingTable::new()))
}, },
Mode::Router => (false, false, Box::new(RoutingTable::new())), Mode::Router => (false, false, Box::new(RoutingTable::new())),
Mode::Switch => (true, true, Box::new(SwitchTable::new(dst_timeout))), Mode::Switch => (true, true, Box::new(SwitchTable::new(dst_timeout, 10))),
Mode::Hub => (false, true, Box::new(SwitchTable::new(dst_timeout))) Mode::Hub => (false, true, Box::new(SwitchTable::new(dst_timeout, 10)))
}; };
let magic = config.get_magic(); let magic = config.get_magic();
Crypto::init(); Crypto::init();

View File

@ -4,6 +4,8 @@
use std::net::{ToSocketAddrs, SocketAddr}; use std::net::{ToSocketAddrs, SocketAddr};
use std::str::FromStr; use std::str::FromStr;
use std::thread;
use std::time::Duration;
use super::MAGIC; use super::MAGIC;
use super::ethernet::{Frame, SwitchTable}; use super::ethernet::{Frame, SwitchTable};
@ -237,12 +239,19 @@ fn decode_invalid_packet() {
#[test] #[test]
fn switch() { fn switch() {
let mut table = SwitchTable::new(10); let mut table = SwitchTable::new(10, 1);
let addr = Address::from_str("12:34:56:78:90:ab").unwrap(); let addr = Address::from_str("12:34:56:78:90:ab").unwrap();
let peer = "1.2.3.4:5678".to_socket_addrs().unwrap().next().unwrap(); let peer = "1.2.3.4:5678".to_socket_addrs().unwrap().next().unwrap();
let peer2 = "1.2.3.5:7890".to_socket_addrs().unwrap().next().unwrap();
assert!(table.lookup(&addr).is_none()); assert!(table.lookup(&addr).is_none());
table.learn(addr.clone(), None, peer.clone()); table.learn(addr.clone(), None, peer.clone());
assert_eq!(table.lookup(&addr), Some(peer)); assert_eq!(table.lookup(&addr), Some(peer));
// Do not override within 1 seconds
table.learn(addr.clone(), None, peer2.clone());
assert_eq!(table.lookup(&addr), Some(peer));
thread::sleep(Duration::from_secs(1));
table.learn(addr.clone(), None, peer2.clone());
assert_eq!(table.lookup(&addr), Some(peer2));
} }
#[test] #[test]
@ -493,6 +502,7 @@ fn config_merge() {
flag_listen: Some(3211), flag_listen: Some(3211),
flag_peer_timeout: Some(1801), flag_peer_timeout: Some(1801),
flag_dst_timeout: Some(301), flag_dst_timeout: Some(301),
flag_mode: Some(Mode::Switch),
flag_subnet: vec![], flag_subnet: vec![],
flag_connect: vec!["another:3210".to_string()], flag_connect: vec!["another:3210".to_string()],
flag_no_port_forwarding: true, flag_no_port_forwarding: true,
@ -514,7 +524,7 @@ fn config_merge() {
peers: vec!["remote.machine.foo:3210".to_string(), "remote.machine.bar:3210".to_string(), "another:3210".to_string()], peers: vec!["remote.machine.foo:3210".to_string(), "remote.machine.bar:3210".to_string(), "another:3210".to_string()],
peer_timeout: 1801, peer_timeout: 1801,
dst_timeout: 301, dst_timeout: 301,
mode: Mode::Normal, mode: Mode::Switch,
port_forwarding: false, port_forwarding: false,
subnets: vec!["10.0.1.0/24".to_string()], subnets: vec!["10.0.1.0/24".to_string()],
user: Some("root".to_string()), user: Some("root".to_string()),