2019-02-26 17:36:54 +00:00
|
|
|
// VpnCloud - Peer-to-Peer VPN
|
2020-05-28 07:03:48 +00:00
|
|
|
// Copyright (C) 2015-2020 Dennis Schwerdel
|
2019-02-26 17:36:54 +00:00
|
|
|
// This software is licensed under GPL-3 or newer (see LICENSE.md)
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2020-10-19 22:19:38 +00:00
|
|
|
fn switch_delivers() {
|
2020-09-24 17:48:13 +00:00
|
|
|
let config = Config { device_type: Type::Tap, ..Config::default() };
|
|
|
|
let mut sim = TapSimulator::new();
|
|
|
|
let node1 = sim.add_node(false, &config);
|
|
|
|
let node2 = sim.add_node(false, &config);
|
2019-02-26 17:36:54 +00:00
|
|
|
|
2020-09-24 17:48:13 +00:00
|
|
|
sim.connect(node1, node2);
|
|
|
|
sim.simulate_all_messages();
|
|
|
|
assert!(sim.is_connected(node1, node2));
|
|
|
|
assert!(sim.is_connected(node2, node1));
|
2019-02-26 17:36:54 +00:00
|
|
|
|
|
|
|
let payload = vec![2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5];
|
|
|
|
|
2020-09-24 17:48:13 +00:00
|
|
|
sim.put_payload(node1, payload.clone());
|
|
|
|
sim.simulate_all_messages();
|
2019-02-26 17:36:54 +00:00
|
|
|
|
2020-09-24 17:48:13 +00:00
|
|
|
assert_eq!(Some(payload), sim.pop_payload(node2));
|
2019-02-26 17:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-03-01 15:36:18 +00:00
|
|
|
fn switch_learns() {
|
2020-09-24 17:48:13 +00:00
|
|
|
let config = Config { device_type: Type::Tap, ..Config::default() };
|
|
|
|
let mut sim = TapSimulator::new();
|
|
|
|
let node1 = sim.add_node(false, &config);
|
|
|
|
let node2 = sim.add_node(false, &config);
|
|
|
|
let node3 = sim.add_node(false, &config);
|
|
|
|
|
|
|
|
sim.connect(node1, node2);
|
|
|
|
sim.connect(node1, node3);
|
|
|
|
sim.connect(node2, node3);
|
|
|
|
sim.simulate_all_messages();
|
|
|
|
assert!(sim.is_connected(node1, node2));
|
|
|
|
assert!(sim.is_connected(node2, node1));
|
|
|
|
assert!(sim.is_connected(node1, node3));
|
|
|
|
assert!(sim.is_connected(node3, node1));
|
|
|
|
assert!(sim.is_connected(node2, node3));
|
|
|
|
assert!(sim.is_connected(node3, node2));
|
2019-02-26 17:36:54 +00:00
|
|
|
|
|
|
|
let payload = vec![2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5];
|
|
|
|
|
2019-03-01 15:36:18 +00:00
|
|
|
// Nothing learnt so far, node1 broadcasts
|
|
|
|
|
2020-09-24 17:48:13 +00:00
|
|
|
sim.put_payload(node1, payload.clone());
|
|
|
|
sim.simulate_all_messages();
|
2019-02-26 17:36:54 +00:00
|
|
|
|
2020-09-24 17:48:13 +00:00
|
|
|
assert_eq!(Some(payload.clone()), sim.pop_payload(node2));
|
|
|
|
assert_eq!(Some(payload), sim.pop_payload(node3));
|
2019-02-26 17:36:54 +00:00
|
|
|
|
|
|
|
let payload = vec![1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 5, 4, 3, 2, 1];
|
|
|
|
|
2019-03-01 15:36:18 +00:00
|
|
|
// Node 2 learned the address by receiving it, does not broadcast
|
|
|
|
|
2020-09-24 17:48:13 +00:00
|
|
|
sim.put_payload(node2, payload.clone());
|
|
|
|
sim.simulate_all_messages();
|
2019-02-26 17:36:54 +00:00
|
|
|
|
2020-09-24 17:48:13 +00:00
|
|
|
assert_eq!(Some(payload), sim.pop_payload(node1));
|
|
|
|
assert_eq!(None, sim.pop_payload(node3));
|
2019-03-01 15:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn switch_honours_vlans() {
|
2020-10-19 22:19:38 +00:00
|
|
|
let config = Config { device_type: Type::Tap, ..Config::default() };
|
|
|
|
let mut sim = TapSimulator::new();
|
|
|
|
let node1 = sim.add_node(false, &config);
|
|
|
|
let node2 = sim.add_node(false, &config);
|
|
|
|
let node3 = sim.add_node(false, &config);
|
|
|
|
|
|
|
|
sim.connect(node1, node2);
|
|
|
|
sim.connect(node1, node3);
|
|
|
|
sim.connect(node2, node3);
|
|
|
|
sim.simulate_all_messages();
|
|
|
|
assert!(sim.is_connected(node1, node2));
|
|
|
|
assert!(sim.is_connected(node2, node1));
|
|
|
|
assert!(sim.is_connected(node1, node3));
|
|
|
|
assert!(sim.is_connected(node3, node1));
|
|
|
|
assert!(sim.is_connected(node2, node3));
|
|
|
|
assert!(sim.is_connected(node3, node2));
|
|
|
|
|
|
|
|
let payload = vec![2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 0x81, 0, 0, 0x67, 1, 2, 3, 4, 5];
|
|
|
|
|
|
|
|
// Nothing learnt so far, node1 broadcasts
|
|
|
|
|
|
|
|
sim.put_payload(node1, payload.clone());
|
|
|
|
sim.simulate_all_messages();
|
|
|
|
|
|
|
|
assert_eq!(Some(payload.clone()), sim.pop_payload(node2));
|
|
|
|
assert_eq!(Some(payload), sim.pop_payload(node3));
|
|
|
|
|
|
|
|
let payload = vec![1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 0x81, 0, 0, 0x67, 5, 4, 3, 2, 1];
|
|
|
|
|
|
|
|
// Node 2 learned the address by receiving it, does not broadcast
|
|
|
|
|
|
|
|
sim.put_payload(node2, payload.clone());
|
|
|
|
sim.simulate_all_messages();
|
|
|
|
|
|
|
|
assert_eq!(Some(payload), sim.pop_payload(node1));
|
|
|
|
assert_eq!(None, sim.pop_payload(node3));
|
|
|
|
|
|
|
|
let payload = vec![1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 0x81, 0, 0, 0x68, 5, 4, 3, 2, 1];
|
|
|
|
|
|
|
|
// Different VLANs, node 2 does not learn, still broadcasts
|
|
|
|
|
|
|
|
sim.put_payload(node2, payload.clone());
|
|
|
|
sim.simulate_all_messages();
|
|
|
|
|
|
|
|
assert_eq!(Some(payload.clone()), sim.pop_payload(node1));
|
|
|
|
assert_eq!(Some(payload), sim.pop_payload(node3));
|
2019-03-01 15:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-10-19 22:19:38 +00:00
|
|
|
#[ignore]
|
2019-03-01 15:36:18 +00:00
|
|
|
fn switch_forgets() {
|
2020-09-24 17:48:13 +00:00
|
|
|
// TODO Test
|
2020-10-19 22:19:38 +00:00
|
|
|
unimplemented!()
|
2019-03-01 15:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn router_delivers() {
|
2020-10-19 22:19:38 +00:00
|
|
|
let config1 = Config {
|
|
|
|
device_type: Type::Tun,
|
|
|
|
auto_claim: false,
|
|
|
|
claims: vec!["1.1.1.1/32".to_string()],
|
|
|
|
..Config::default()
|
|
|
|
};
|
|
|
|
let config2 = Config {
|
|
|
|
device_type: Type::Tun,
|
|
|
|
auto_claim: false,
|
|
|
|
claims: vec!["2.2.2.2/32".to_string()],
|
|
|
|
..Config::default()
|
|
|
|
};
|
|
|
|
let mut sim = TunSimulator::new();
|
|
|
|
let node1 = sim.add_node(false, &config1);
|
|
|
|
let node2 = sim.add_node(false, &config2);
|
|
|
|
|
|
|
|
sim.connect(node1, node2);
|
|
|
|
sim.simulate_all_messages();
|
|
|
|
assert!(sim.is_connected(node1, node2));
|
|
|
|
assert!(sim.is_connected(node2, node1));
|
|
|
|
|
|
|
|
let payload = vec![0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2];
|
|
|
|
|
|
|
|
sim.put_payload(node1, payload.clone());
|
|
|
|
sim.simulate_all_messages();
|
|
|
|
|
|
|
|
assert_eq!(Some(payload), sim.pop_payload(node2));
|
2019-03-01 15:36:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn router_drops_unknown_dest() {
|
2020-10-19 22:19:38 +00:00
|
|
|
let config1 = Config {
|
|
|
|
device_type: Type::Tun,
|
|
|
|
auto_claim: false,
|
|
|
|
claims: vec!["1.1.1.1/32".to_string()],
|
|
|
|
..Config::default()
|
|
|
|
};
|
|
|
|
let config2 = Config {
|
|
|
|
device_type: Type::Tun,
|
|
|
|
auto_claim: false,
|
|
|
|
claims: vec!["2.2.2.2/32".to_string()],
|
|
|
|
..Config::default()
|
|
|
|
};
|
|
|
|
let mut sim = TunSimulator::new();
|
|
|
|
let node1 = sim.add_node(false, &config1);
|
|
|
|
let node2 = sim.add_node(false, &config2);
|
|
|
|
|
|
|
|
sim.connect(node1, node2);
|
|
|
|
sim.simulate_all_messages();
|
|
|
|
assert!(sim.is_connected(node1, node2));
|
|
|
|
assert!(sim.is_connected(node2, node1));
|
|
|
|
|
|
|
|
let payload = vec![0x40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 3, 3, 3, 3];
|
|
|
|
|
|
|
|
sim.put_payload(node1, payload);
|
|
|
|
sim.simulate_all_messages();
|
|
|
|
|
|
|
|
assert_eq!(None, sim.pop_payload(node2));
|
2019-12-04 08:32:35 +00:00
|
|
|
}
|