vpncloud/src/tests/mod.rs

168 lines
4.5 KiB
Rust
Raw Normal View History

2019-02-26 17:36:54 +00:00
// VpnCloud - Peer-to-Peer VPN
// Copyright (C) 2015-2019 Dennis Schwerdel
// This software is licensed under GPL-3 or newer (see LICENSE.md)
2019-12-04 08:32:35 +00:00
#[macro_use]
mod helper;
2019-12-04 12:09:20 +00:00
mod nat;
2019-02-26 17:36:54 +00:00
mod payload;
2019-12-04 08:32:35 +00:00
mod peers;
2019-02-26 17:36:54 +00:00
pub use std::net::SocketAddr;
2019-12-04 12:09:20 +00:00
use std::{
io::Write,
sync::{
atomic::{AtomicUsize, Ordering},
Once
}
};
2019-12-04 08:32:35 +00:00
pub use super::{
cloud::GenericCloud,
config::Config,
crypto::Crypto,
device::MockDevice,
ethernet::{self, SwitchTable},
ip::{self, RoutingTable},
net::MockSocket,
types::{Protocol, Range, Table},
udpmessage::Message,
util::MockTimeSource
};
2019-02-26 17:36:54 +00:00
2019-12-04 12:09:20 +00:00
static INIT_LOGGER: Once = Once::new();
pub fn init_debug_logger() {
INIT_LOGGER.call_once(|| {
log::set_boxed_logger(Box::new(DebugLogger)).unwrap();
log::set_max_level(log::LevelFilter::Debug);
})
}
struct DebugLogger;
impl log::Log for DebugLogger {
#[inline]
fn enabled(&self, _metadata: &log::Metadata) -> bool {
true
}
#[inline]
fn log(&self, record: &log::Record) {
if self.enabled(record.metadata()) {
eprintln!("{} - {}", record.level(), record.args());
}
}
#[inline]
fn flush(&self) {
std::io::stderr().flush().expect("Failed to flush")
}
}
2019-02-26 17:36:54 +00:00
type TestNode<P, T> = GenericCloud<MockDevice, P, T, MockSocket, MockTimeSource>;
type TapTestNode = TestNode<ethernet::Frame, SwitchTable<MockTimeSource>>;
2019-03-02 11:23:12 +00:00
#[allow(dead_code)]
2019-02-26 17:36:54 +00:00
type TunTestNode = TestNode<ip::Packet, RoutingTable>;
2019-03-01 15:36:18 +00:00
thread_local! {
static NEXT_PORT: AtomicUsize = AtomicUsize::new(1);
}
2019-12-04 12:09:20 +00:00
fn create_tap_node(nat: bool) -> TapTestNode {
create_tap_node_with_config(nat, Config::default())
2019-03-03 13:56:59 +00:00
}
2019-12-04 12:09:20 +00:00
fn create_tap_node_with_config(nat: bool, mut config: Config) -> TapTestNode {
MockSocket::set_nat(nat);
2019-03-03 13:56:59 +00:00
config.port = NEXT_PORT.with(|p| p.fetch_add(1, Ordering::Relaxed)) as u16;
2019-12-04 08:32:35 +00:00
TestNode::new(&config, MockDevice::new(), SwitchTable::new(1800, 10), true, true, vec![], Crypto::None, None)
2019-02-26 17:36:54 +00:00
}
2019-03-02 11:23:12 +00:00
#[allow(dead_code)]
2019-12-04 12:09:20 +00:00
fn create_tun_node(nat: bool, addresses: Vec<Range>) -> TunTestNode {
MockSocket::set_nat(nat);
2019-02-26 17:36:54 +00:00
TestNode::new(
2019-03-01 15:36:18 +00:00
&Config { port: NEXT_PORT.with(|p| p.fetch_add(1, Ordering::Relaxed)) as u16, ..Config::default() },
2019-02-26 17:36:54 +00:00
MockDevice::new(),
RoutingTable::new(),
2019-12-04 08:32:35 +00:00
false,
false,
addresses,
Crypto::None,
None
2019-02-26 17:36:54 +00:00
)
}
fn msg4_get<P: Protocol, T: Table>(node: &mut TestNode<P, T>) -> (SocketAddr, Vec<u8>) {
let msg = node.socket4().pop_outbound();
assert!(msg.is_some());
msg.unwrap()
}
2019-03-02 11:23:12 +00:00
#[allow(dead_code)]
2019-02-26 17:36:54 +00:00
fn msg6_get<P: Protocol, T: Table>(node: &mut TestNode<P, T>) -> (SocketAddr, Vec<u8>) {
let msg = node.socket6().pop_outbound();
assert!(msg.is_some());
msg.unwrap()
}
fn msg4_put<P: Protocol, T: Table>(node: &mut TestNode<P, T>, from: SocketAddr, msg: Vec<u8>) {
2019-12-04 12:09:20 +00:00
if node.socket4().put_inbound(from, msg) {
node.trigger_socket_v4_event();
}
2019-02-26 17:36:54 +00:00
}
fn msg6_put<P: Protocol, T: Table>(node: &mut TestNode<P, T>, from: SocketAddr, msg: Vec<u8>) {
2019-12-04 12:09:20 +00:00
if node.socket6().put_inbound(from, msg) {
node.trigger_socket_v6_event();
}
2019-02-26 17:36:54 +00:00
}
fn simulate<P: Protocol, T: Table>(nodes: &mut [(&mut TestNode<P, T>, SocketAddr)]) {
2019-03-01 22:12:19 +00:00
for (ref mut node, ref _from_addr) in nodes.iter_mut() {
2019-02-26 17:36:54 +00:00
while node.device().has_inbound() {
node.trigger_device_event();
}
}
let mut clean = false;
while !clean {
clean = true;
let mut msgs = Vec::new();
for (ref mut node, ref from_addr) in nodes.iter_mut() {
while let Some((to_addr, msg)) = node.socket4().pop_outbound() {
msgs.push((msg, *from_addr, to_addr));
}
}
clean &= msgs.is_empty();
for (msg, from_addr, to_addr) in msgs {
for (ref mut node, ref addr) in nodes.iter_mut() {
if *addr == to_addr {
msg4_put(node, from_addr, msg);
break
}
}
}
let mut msgs = Vec::new();
for (ref mut node, ref from_addr) in nodes.iter_mut() {
while let Some((to_addr, msg)) = node.socket6().pop_outbound() {
msgs.push((msg, *from_addr, to_addr));
}
}
clean &= msgs.is_empty();
for (msg, from_addr, to_addr) in msgs {
for (ref mut node, ref addr) in nodes.iter_mut() {
if *addr == to_addr {
msg6_put(node, from_addr, msg);
break
}
}
}
}
2019-12-04 08:32:35 +00:00
}