aboutsummaryrefslogtreecommitdiffstats
path: root/src/platform/dummy/tun.rs
diff options
context:
space:
mode:
authorMathias Hall-Andersen <mathias@hall-andersen.dk>2019-10-28 14:48:24 +0100
committerMathias Hall-Andersen <mathias@hall-andersen.dk>2019-10-28 14:48:24 +0100
commit4ff328b7da876fb3305fefd83865553af9c8ab2c (patch)
treebd1680d9f7316415e3044fd152ef503729d97239 /src/platform/dummy/tun.rs
parentFixed Ordering::Acquire -> Ordering::SeqCst typo (diff)
downloadwireguard-rs-4ff328b7da876fb3305fefd83865553af9c8ab2c.tar.xz
wireguard-rs-4ff328b7da876fb3305fefd83865553af9c8ab2c.zip
First full test of pure WireGuard
Diffstat (limited to 'src/platform/dummy/tun.rs')
-rw-r--r--src/platform/dummy/tun.rs27
1 files changed, 26 insertions, 1 deletions
diff --git a/src/platform/dummy/tun.rs b/src/platform/dummy/tun.rs
index fb87d2f..185b328 100644
--- a/src/platform/dummy/tun.rs
+++ b/src/platform/dummy/tun.rs
@@ -1,3 +1,8 @@
+use hex;
+use log::debug;
+use rand::rngs::OsRng;
+use rand::Rng;
+
use std::cmp::min;
use std::error::Error;
use std::fmt;
@@ -61,16 +66,19 @@ impl fmt::Display for TunError {
pub struct TunTest {}
pub struct TunFakeIO {
+ id: u32,
store: bool,
tx: SyncSender<Vec<u8>>,
rx: Receiver<Vec<u8>>,
}
pub struct TunReader {
+ id: u32,
rx: Receiver<Vec<u8>>,
}
pub struct TunWriter {
+ id: u32,
store: bool,
tx: Mutex<SyncSender<Vec<u8>>>,
}
@@ -88,6 +96,12 @@ impl Reader for TunReader {
Ok(msg) => {
let n = min(buf.len() - offset, msg.len());
buf[offset..offset + n].copy_from_slice(&msg[..n]);
+ debug!(
+ "dummy::TUN({}) : read ({}, {})",
+ self.id,
+ n,
+ hex::encode(&buf[offset..offset + n])
+ );
Ok(n)
}
Err(_) => Err(TunError::Disconnected),
@@ -99,6 +113,12 @@ impl Writer for TunWriter {
type Error = TunError;
fn write(&self, src: &[u8]) -> Result<(), Self::Error> {
+ debug!(
+ "dummy::TUN({}) : write ({}, {})",
+ self.id,
+ src.len(),
+ hex::encode(src)
+ );
if self.store {
let m = src.to_owned();
match self.tx.lock().unwrap().send(m) {
@@ -149,13 +169,18 @@ impl TunTest {
sync_channel(1)
};
+ let mut rng = OsRng::new().unwrap();
+ let id: u32 = rng.gen();
+
let fake = TunFakeIO {
+ id,
tx: tx1,
rx: rx2,
store,
};
- let reader = TunReader { rx: rx1 };
+ let reader = TunReader { id, rx: rx1 };
let writer = TunWriter {
+ id,
tx: Mutex::new(tx2),
store,
};