More tests

This commit is contained in:
Dennis Schwerdel 2019-03-03 14:56:59 +01:00
parent 82b7e5f18b
commit 553e09611e
4 changed files with 94 additions and 8 deletions

View File

@ -352,11 +352,18 @@ impl<D: Device, P: Protocol, T: Table, S: Socket, TS: TimeSource> GenericCloud<D
/// connect to the peer if it is not already connected.
pub fn add_reconnect_peer(&mut self, add: String) {
let now = TS::now();
let resolved = match resolve(&add as &str) {
Ok(addrs) => addrs,
Err(err) => {
warn!("Failed to resolve {}: {:?}", add, err);
vec![]
}
};
self.reconnect_peers.push(ReconnectEntry {
address: add,
tries: 0,
timeout: 1,
resolved: vec![],
resolved: resolved,
next_resolve: now,
next: now
})
@ -819,6 +826,10 @@ impl<P: Protocol, T: Table> GenericCloud<MockDevice, P, T, MockSocket, MockTimeS
self.handle_device_event(&mut buffer);
}
pub fn trigger_housekeep(&mut self) {
assert!(self.housekeep().is_ok())
}
pub fn node_id(&self) -> NodeId {
self.node_id
}

View File

@ -13,7 +13,7 @@ macro_rules! assert_message4 {
let (addr, mut data) = msg4_get(&mut $from);
assert_eq!($to_addr, addr);
{
let message = $to.decode_message(&mut data).unwrap();
let message = $from.decode_message(&mut data).unwrap();
assert_eq!($message, message.without_data());
}
msg4_put(&mut $to, $from_addr, data);
@ -26,7 +26,7 @@ macro_rules! assert_message6 {
let (addr, mut data) = msg6_get(&mut $from);
assert_eq!($to_addr, addr);
{
let message = $to.decode_message(&mut data).unwrap();
let message = $from.decode_message(&mut data).unwrap();
assert_eq!($message, message.without_data());
}
msg6_put(&mut $to, $from_addr, data);

View File

@ -33,8 +33,13 @@ thread_local! {
}
fn create_tap_node() -> TapTestNode {
create_tap_node_with_config(Config::default())
}
fn create_tap_node_with_config(mut config: Config) -> TapTestNode {
config.port = NEXT_PORT.with(|p| p.fetch_add(1, Ordering::Relaxed)) as u16;
TestNode::new(
&Config { port: NEXT_PORT.with(|p| p.fetch_add(1, Ordering::Relaxed)) as u16, ..Config::default() },
&config,
MockDevice::new(),
SwitchTable::new(1800, 10),
true, true, vec![], Crypto::None, None

View File

@ -95,22 +95,92 @@ fn cross_connect() {
#[test]
fn connect_via_beacons() {
//TODO
MockTimeSource::set_time(0);
let beacon_path = "target/.vpncloud_test";
let mut node1 = create_tap_node_with_config(Config { beacon_store: Some(beacon_path.to_string()), ..Config::default()});
let node1_addr = node1.address().unwrap().0;
let mut node2 = create_tap_node_with_config(Config { beacon_load: Some(beacon_path.to_string()), ..Config::default()});
let node2_addr = addr!("2.2.2.2:2222");
assert!(!node1.peers().contains_node(&node2.node_id()));
assert!(!node2.peers().contains_node(&node1.node_id()));
MockTimeSource::set_time(5000);
node1.trigger_housekeep();
MockTimeSource::set_time(10000);
node2.trigger_housekeep();
simulate!(node1 => node1_addr, node2 => node2_addr);
assert_clean!(node1, node2);
assert_connected!(node1, node2);
}
#[test]
fn reconnect_after_timeout() {
//TODO
MockTimeSource::set_time(0);
let mut node1 = create_tap_node();
let node1_addr = addr!("1.1.1.1:1111");
let mut node2 = create_tap_node();
let node2_addr = addr!("2.2.2.2:2222");
node1.add_reconnect_peer("2.2.2.2:2222".to_string());
node1.connect(node2_addr).unwrap();
simulate!(node1 => node1_addr, node2 => node2_addr);
assert_connected!(node1, node2);
MockTimeSource::set_time(5000);
node1.trigger_housekeep();
node2.trigger_housekeep();
assert!(!node1.peers().contains_node(&node2.node_id()));
assert!(!node2.peers().contains_node(&node1.node_id()));
simulate!(node1 => node1_addr, node2 => node2_addr);
assert_connected!(node1, node2);
}
#[test]
fn lost_init1() {
//TODO
let mut node1 = create_tap_node();
let node1_addr = addr!("1.2.3.4:5678");
let mut node2 = create_tap_node();
let node2_addr = addr!("2.3.4.5:6789");
node1.connect("2.3.4.5:6789").unwrap();
// Node 1 -> Node 2: Init 0
assert_message4!(node1, node1_addr, node2, node2_addr, Message::Init(0, node1.node_id(), vec![]));
assert_clean!(node1);
// Node 2 -> Node 1: Init 1 | Node 2 -> Node 1: Peers
assert!(node2.socket4().pop_outbound().is_some());
assert!(!node1.peers().contains_node(&node2.node_id()));
simulate!(node1 => node1_addr, node2 => node2_addr);
assert_connected!(node1, node2);
}
#[test]
fn wrong_magic() {
//TODO
let mut node1 = create_tap_node();
let node1_addr = addr!("1.2.3.4:5678");
let mut node2 = create_tap_node_with_config(Config { magic: Some("hash:different".to_string()), ..Config::default()});
let node2_addr = addr!("2.3.4.5:6789");
node1.connect("2.3.4.5:6789").unwrap();
assert_message4!(node1, node1_addr, node2, node2_addr, Message::Init(0, node1.node_id(), vec![]));
assert_clean!(node1, node2);
assert!(!node1.peers().contains_node(&node2.node_id()));
assert!(!node2.peers().contains_node(&node1.node_id()));
}
#[test]