Improved tests

This commit is contained in:
Dennis Schwerdel 2019-03-01 16:36:18 +01:00
parent 5be254db4a
commit c5ba4b197b
5 changed files with 79 additions and 7 deletions

View File

@ -12,7 +12,7 @@ use std::marker::PhantomData;
use fnv::FnvHasher; use fnv::FnvHasher;
use super::types::{Error, Table, Protocol, Address}; use super::types::{Error, Table, Protocol, Address};
use super::util::{TimeSource, Time, Duration, MockTimeSource}; use super::util::{TimeSource, Time, Duration};
/// An ethernet frame dissector /// An ethernet frame dissector
/// ///
@ -165,6 +165,7 @@ impl<TS: TimeSource> Table for SwitchTable<TS> {
#[cfg(test)] use std::str::FromStr; #[cfg(test)] use std::str::FromStr;
#[cfg(test)] use std::net::ToSocketAddrs; #[cfg(test)] use std::net::ToSocketAddrs;
#[cfg(test)] use super::util::MockTimeSource;
#[test] #[test]
fn decode_frame_without_vlan() { fn decode_frame_without_vlan() {

View File

@ -3,10 +3,11 @@
// This software is licensed under GPL-3 or newer (see LICENSE.md) // This software is licensed under GPL-3 or newer (see LICENSE.md)
#[macro_use] mod helper; #[macro_use] mod helper;
mod connect; mod peers;
mod payload; mod payload;
pub use std::net::SocketAddr; pub use std::net::SocketAddr;
use std::sync::atomic::{Ordering, AtomicUsize};
pub use super::ethernet::{self, SwitchTable}; pub use super::ethernet::{self, SwitchTable};
pub use super::util::MockTimeSource; pub use super::util::MockTimeSource;
@ -26,9 +27,13 @@ type TapTestNode = TestNode<ethernet::Frame, SwitchTable<MockTimeSource>>;
type TunTestNode = TestNode<ip::Packet, RoutingTable>; type TunTestNode = TestNode<ip::Packet, RoutingTable>;
thread_local! {
static NEXT_PORT: AtomicUsize = AtomicUsize::new(1);
}
fn create_tap_node() -> TapTestNode { fn create_tap_node() -> TapTestNode {
TestNode::new( TestNode::new(
&Config::default(), &Config { port: NEXT_PORT.with(|p| p.fetch_add(1, Ordering::Relaxed)) as u16, ..Config::default() },
MockDevice::new(), MockDevice::new(),
SwitchTable::new(1800, 10), SwitchTable::new(1800, 10),
true, true, vec![], Crypto::None, None true, true, vec![], Crypto::None, None
@ -37,7 +42,7 @@ fn create_tap_node() -> TapTestNode {
fn create_tun_node(addresses: Vec<Range>) -> TunTestNode { fn create_tun_node(addresses: Vec<Range>) -> TunTestNode {
TestNode::new( TestNode::new(
&Config::default(), &Config { port: NEXT_PORT.with(|p| p.fetch_add(1, Ordering::Relaxed)) as u16, ..Config::default() },
MockDevice::new(), MockDevice::new(),
RoutingTable::new(), RoutingTable::new(),
false, false, addresses, Crypto::None, None false, false, addresses, Crypto::None, None

View File

@ -5,7 +5,7 @@
use super::*; use super::*;
#[test] #[test]
fn ethernet_send() { fn ethernet_delivers() {
let mut node1 = create_tap_node(); let mut node1 = create_tap_node();
let node1_addr = addr!("1.2.3.4:5678"); let node1_addr = addr!("1.2.3.4:5678");
let mut node2 = create_tap_node(); let mut node2 = create_tap_node();
@ -27,7 +27,7 @@ fn ethernet_send() {
} }
#[test] #[test]
fn learning_switch() { fn switch_learns() {
let mut node1 = create_tap_node(); let mut node1 = create_tap_node();
let node1_addr = addr!("1.2.3.4:5678"); let node1_addr = addr!("1.2.3.4:5678");
let mut node2 = create_tap_node(); let mut node2 = create_tap_node();
@ -42,6 +42,8 @@ fn learning_switch() {
let payload = vec![2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5]; let payload = vec![2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5];
// Nothing learnt so far, node1 broadcasts
node1.device().put_inbound(payload.clone()); node1.device().put_inbound(payload.clone());
simulate!(node1 => node1_addr, node2 => node2_addr, node3 => node3_addr); simulate!(node1 => node1_addr, node2 => node2_addr, node3 => node3_addr);
@ -51,6 +53,8 @@ fn learning_switch() {
let payload = vec![1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 5, 4, 3, 2, 1]; let payload = vec![1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 5, 4, 3, 2, 1];
// Node 2 learned the address by receiving it, does not broadcast
node2.device().put_inbound(payload.clone()); node2.device().put_inbound(payload.clone());
simulate!(node1 => node1_addr, node2 => node2_addr, node3 => node3_addr); simulate!(node1 => node1_addr, node2 => node2_addr, node3 => node3_addr);
@ -59,4 +63,24 @@ fn learning_switch() {
assert_clean!(node3); assert_clean!(node3);
assert_clean!(node1, node2, node3); assert_clean!(node1, node2, node3);
}
#[test]
fn switch_honours_vlans() {
//TODO
}
#[test]
fn switch_forgets() {
//TODO
}
#[test]
fn router_delivers() {
//TODO
}
#[test]
fn router_drops_unknown_dest() {
//TODO
} }

View File

@ -90,5 +90,46 @@ fn cross_connect() {
assert_connected!(node3, node2); assert_connected!(node3, node2);
// transient connections 2nd degree // transient connections 2nd degree
assert_connected!(node1, node4); assert_connected!(node2, node4);
} }
#[test]
fn connect_via_beacons() {
//TODO
}
#[test]
fn reconnect_after_timeout() {
//TODO
}
#[test]
fn lost_init1() {
//TODO
}
#[test]
fn wrong_magic() {
//TODO
}
#[test]
fn peer_exchange() {
//TODO
}
#[test]
fn lost_peer_exchange() {
//TODO
}
#[test]
fn remove_dead_peers() {
//TODO
}
#[test]
fn update_primary_address() {
//TODO
}

View File

@ -129,6 +129,7 @@ pub fn resolve<Addr: ToSocketAddrs+fmt::Debug>(addr: Addr) -> Result<Vec<SocketA
Ok(addrs) Ok(addrs)
} }
#[allow(unused_macros)]
macro_rules! addr { macro_rules! addr {
($addr: expr) => { ($addr: expr) => {
{ {