From 8de2305cc73920d838e11634dc9ec619c2608a78 Mon Sep 17 00:00:00 2001 From: Mathias Hall-Andersen Date: Fri, 21 Feb 2020 00:31:00 +0100 Subject: Fixed unit tests --- Cargo.lock | 38 +++++++++++--------- Cargo.toml | 6 ++-- src/platform/linux/udp.rs | 12 +++---- src/wireguard/router/device.rs | 12 +++---- src/wireguard/router/mod.rs | 1 - src/wireguard/router/receive.rs | 7 ++-- src/wireguard/router/send.rs | 10 +++--- src/wireguard/router/tests.rs | 78 +++++++++++++++++++++++------------------ src/wireguard/tests.rs | 33 ++++++++++------- 9 files changed, 107 insertions(+), 90 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6c10be9..81ffad4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,7 +13,7 @@ name = "aho-corasick" version = "0.6.10" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.3.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -31,7 +31,7 @@ name = "atty" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "hermit-abi 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -179,7 +179,7 @@ name = "cpuprofiler" version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "error-chain 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)", + "error-chain 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -242,7 +242,7 @@ dependencies = [ [[package]] name = "env_logger" -version = "0.6.2" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", @@ -254,11 +254,11 @@ dependencies = [ [[package]] name = "error-chain" -version = "0.12.1" +version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "backtrace 0.3.44 (registry+https://github.com/rust-lang/crates.io-index)", - "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -318,7 +318,7 @@ dependencies = [ [[package]] name = "hermit-abi" -version = "0.1.6" +version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", @@ -422,7 +422,7 @@ dependencies = [ [[package]] name = "memchr" -version = "2.3.0" +version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -480,7 +480,7 @@ name = "nom" version = "4.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -497,7 +497,7 @@ name = "num_cpus" version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)", + "hermit-abi 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -813,7 +813,7 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "aho-corasick 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", - "memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "regex-syntax 0.6.14 (registry+https://github.com/rust-lang/crates.io-index)", "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", "utf8-ranges 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1084,6 +1084,11 @@ name = "version_check" version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "version_check" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "wait-timeout" version = "0.2.0" @@ -1232,7 +1237,7 @@ dependencies = [ "crossbeam-channel 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "daemonize 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", - "env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "hjul 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1343,8 +1348,8 @@ dependencies = [ "checksum curve25519-dalek 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "26778518a7f6cffa1d25a44b602b62b979bd88adb9e99ffec546998cf3404839" "checksum daemonize 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "70c24513e34f53b640819f0ac9f705b673fcf4006d7aab8778bee72ebfc89815" "checksum digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" -"checksum env_logger 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3" -"checksum error-chain 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3ab49e9dcb602294bc42f9a7dfc9bc6e936fca4418ea300dbfb84fe16de0b7d9" +"checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +"checksum error-chain 0.12.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d371106cc88ffdfb1eabd7111e432da544f16f3e2d7bf1dfe8bf575f1df045cd" "checksum fnv 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "2fad85553e09a6f881f739c29f0b00b0f01357c743266d478b68951ce23285f3" "checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" "checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" @@ -1353,7 +1358,7 @@ dependencies = [ "checksum getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)" = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" "checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb" "checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" -"checksum hermit-abi 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "eff2656d88f158ce120947499e971d743c05dbcbed62e5bd2f38f1698bbc3772" +"checksum hermit-abi 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "e2c55f143919fbc0bc77e427fe2d74cf23786d7c1875666f2fde3ac3c659bb67" "checksum hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" "checksum hjul 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f3306d10e330382e89dcd079f124c77273ab88920b1f83c237a99a76cc31e179" "checksum hmac 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5dcb5e64cda4c23119ab41ba960d1e170a774c8e4b9d9e6a9bc18aabf5e59695" @@ -1367,7 +1372,7 @@ dependencies = [ "checksum libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)" = "d515b1f41455adea1313a4a2ac8a8a477634fbae63cc6100e3aebb207ce61558" "checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" -"checksum memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3197e20c7edb283f87c071ddfc7a2cca8f8e0b888c242959846a6fce03c72223" +"checksum memchr 2.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "53445de381a1f436797497c61d851644d0e8e88e6140f22872ad33a704933978" "checksum mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)" = "302dec22bcf6bae6dfb69c647187f4b4d0fb6f535521f7bc022430ce8e12008f" "checksum mio-extras 2.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "52403fe290012ce777c4626790c8951324a2b9e3316b3143779c72b029742f19" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" @@ -1445,6 +1450,7 @@ dependencies = [ "checksum untrusted 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60369ef7a31de49bcb3f6ca728d4ba7300d9a1658f94c727d4cab8c8d9f4aece" "checksum utf8-ranges 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b4ae116fef2b7fea257ed6440d3cfcff7f190865f170cdad00bb6465bf18ecba" "checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd" +"checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce" "checksum wait-timeout 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9f200f5b12eb75f8c1ed65abd4b2db8a6e1b138a20de009dacee265a2498f3f6" "checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" "checksum wasm-bindgen 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)" = "5205e9afdf42282b192e2310a5b463a6d1c1d774e30dc3c791ac37ab42d2616c" diff --git a/Cargo.toml b/Cargo.toml index e17b019..b884216 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ rand_core = "^0.5" chacha20poly1305 = "^0.3" aead = "^0.2" clear_on_drop = "0.2.3" -env_logger = "0.6" +env_logger = "0.7" num_cpus = "^1.10" daemonize = "0.4.1" crossbeam-channel = "0.4" @@ -43,9 +43,7 @@ features = ["nightly"] profiler = ["cpuprofiler"] start_up = [] - [dev-dependencies] pnet = "0.25.0" proptest = "0.9.4" -rand_chacha = "0.2.1" - +rand_chacha = "0.2.1" \ No newline at end of file diff --git a/src/platform/linux/udp.rs b/src/platform/linux/udp.rs index 8930266..7b4fa33 100644 --- a/src/platform/linux/udp.rs +++ b/src/platform/linux/udp.rs @@ -216,11 +216,11 @@ impl LinuxUDPReader { let mut control: ControlHeaderV6 = unsafe { mem::MaybeUninit::uninit().assume_init() }; let mut hdr = libc::msghdr { msg_name: safe_cast(&mut src), - msg_namelen: mem::size_of::() as u32, + msg_namelen: mem::size_of_val(&src) as u32, msg_iov: iovs.as_mut_ptr(), msg_iovlen: iovs.len(), msg_control: safe_cast(&mut control), - msg_controllen: mem::size_of::(), + msg_controllen: mem::size_of_val(&control), msg_flags: 0, }; @@ -270,11 +270,11 @@ impl LinuxUDPReader { let mut control: ControlHeaderV4 = unsafe { mem::MaybeUninit::uninit().assume_init() }; let mut hdr = libc::msghdr { msg_name: safe_cast(&mut src), - msg_namelen: mem::size_of::() as u32, + msg_namelen: mem::size_of_val(&src) as u32, msg_iov: iovs.as_mut_ptr(), msg_iovlen: iovs.len(), msg_control: safe_cast(&mut control), - msg_controllen: mem::size_of::(), + msg_controllen: mem::size_of_val(&control), msg_flags: 0, }; @@ -350,7 +350,7 @@ impl LinuxUDPWriter { let mut hdr = libc::msghdr { msg_name: safe_cast(&mut dst.dst), - msg_namelen: mem::size_of_val(&dst.dst).try_into().unwrap(), + msg_namelen: mem::size_of_val(&dst.dst) as u32, msg_iov: iovs.as_mut_ptr(), msg_iovlen: iovs.len(), msg_control: safe_cast(&mut control), @@ -415,7 +415,7 @@ impl LinuxUDPWriter { let mut hdr = libc::msghdr { msg_name: safe_cast(&mut dst.dst), - msg_namelen: mem::size_of_val(&dst.dst).try_into().unwrap(), + msg_namelen: mem::size_of_val(&dst.dst) as u32, msg_iov: iovs.as_mut_ptr(), msg_iovlen: iovs.len(), msg_control: safe_cast(&mut control), diff --git a/src/wireguard/router/device.rs b/src/wireguard/router/device.rs index 8bfa261..7c90f22 100644 --- a/src/wireguard/router/device.rs +++ b/src/wireguard/router/device.rs @@ -121,14 +121,14 @@ impl> DeviceHandle< // start worker threads let mut threads = Vec::with_capacity(num_workers); while let Some(rx) = consumers.pop() { - println!("spawn"); - threads.push(thread::spawn(move || { - println!("spawned"); - worker(rx); - })); + threads.push(thread::spawn(move || worker(rx))); } debug_assert!(num_workers > 0, "zero worker threads"); - debug_assert_eq!(threads.len(), num_workers); + debug_assert_eq!( + threads.len(), + num_workers, + "workers does not match consumers" + ); // return exported device handle DeviceHandle { diff --git a/src/wireguard/router/mod.rs b/src/wireguard/router/mod.rs index 19e037f..699c621 100644 --- a/src/wireguard/router/mod.rs +++ b/src/wireguard/router/mod.rs @@ -24,7 +24,6 @@ use super::types::*; pub const SIZE_TAG: usize = 16; pub const SIZE_MESSAGE_PREFIX: usize = mem::size_of::(); -pub const SIZE_KEEPALIVE: usize = mem::size_of::() + SIZE_TAG; pub const CAPACITY_MESSAGE_POSTFIX: usize = SIZE_TAG; pub const fn message_data_len(payload: usize) -> usize { diff --git a/src/wireguard/router/receive.rs b/src/wireguard/router/receive.rs index 0e5cb0f..45ef423 100644 --- a/src/wireguard/router/receive.rs +++ b/src/wireguard/router/receive.rs @@ -3,7 +3,7 @@ use super::ip::inner_length; use super::messages::TransportHeader; use super::queue::{ParallelJob, Queue, SequentialJob}; use super::types::Callbacks; -use super::{REJECT_AFTER_MESSAGES, SIZE_KEEPALIVE}; +use super::{REJECT_AFTER_MESSAGES, SIZE_TAG}; use super::super::{tun, udp, Endpoint}; @@ -93,7 +93,6 @@ impl> ParallelJob debug_assert_eq!(nonce.len(), CHACHA20_POLY1305.nonce_len()); nonce[4..].copy_from_slice(header.f_counter.as_bytes()); let nonce = Nonce::assume_unique_for_key(nonce); - // do the weird ring AEAD dance let key = LessSafeKey::new( UnboundKey::new(&CHACHA20_POLY1305, &job.state.keypair.recv.key[..]).unwrap(), @@ -111,7 +110,7 @@ impl> ParallelJob } // check crypto-key router - packet.len() == SIZE_KEEPALIVE || peer.device.table.check_route(&peer, &packet) + packet.len() == SIZE_TAG || peer.device.table.check_route(&peer, &packet) })(); // remove message in case of failure: @@ -174,7 +173,7 @@ impl> SequentialJob // check if should be written to TUN // (keep-alive and malformed packets will have no inner length) if let Some(inner) = inner_length(packet) { - if inner >= packet.len() { + if inner + SIZE_TAG <= packet.len() { let _ = peer.device.inbound.write(&packet[..inner]).map_err(|e| { log::debug!("failed to write inbound packet to TUN: {:?}", e); }); diff --git a/src/wireguard/router/send.rs b/src/wireguard/router/send.rs index db6b079..0472e11 100644 --- a/src/wireguard/router/send.rs +++ b/src/wireguard/router/send.rs @@ -91,19 +91,17 @@ impl> ParallelJob nonce[4..].copy_from_slice(header.f_counter.as_bytes()); let nonce = Nonce::assume_unique_for_key(nonce); - // do the weird ring AEAD dance + // encrypt contents of transport message in-place + let tag_offset = packet.len() - SIZE_TAG; let key = LessSafeKey::new( UnboundKey::new(&CHACHA20_POLY1305, &job.keypair.send.key[..]).unwrap(), ); - - // encrypt contents of transport message in-place - let end = packet.len() - SIZE_TAG; let tag = key - .seal_in_place_separate_tag(nonce, Aad::empty(), &mut packet[..end]) + .seal_in_place_separate_tag(nonce, Aad::empty(), &mut packet[..tag_offset]) .unwrap(); // append tag - packet[end..].copy_from_slice(tag.as_ref()); + packet[tag_offset..].copy_from_slice(tag.as_ref()); } // mark ready diff --git a/src/wireguard/router/tests.rs b/src/wireguard/router/tests.rs index 3afa422..842dd52 100644 --- a/src/wireguard/router/tests.rs +++ b/src/wireguard/router/tests.rs @@ -2,7 +2,7 @@ use super::KeyPair; use super::SIZE_MESSAGE_PREFIX; use super::{Callbacks, Device}; -use super::SIZE_KEEPALIVE; +use super::message_data_len; use super::super::dummy; use super::super::dummy_keypair; @@ -21,12 +21,13 @@ use std::time::Duration; use env_logger; use num_cpus; +use rand::Rng; use test::Bencher; extern crate test; const SIZE_MSG: usize = 1024; - +const SIZE_KEEPALIVE: usize = message_data_len(0); const TIMEOUT: Duration = Duration::from_millis(1000); struct EventTracker { @@ -133,10 +134,9 @@ fn init() { let _ = env_logger::builder().is_test(true).try_init(); } -fn make_packet_padded(size: usize, src: IpAddr, dst: IpAddr, id: u64) -> Vec { - let p = make_packet(size, src, dst, id); - let mut o = vec![0; p.len() + SIZE_MESSAGE_PREFIX]; - o[SIZE_MESSAGE_PREFIX..SIZE_MESSAGE_PREFIX + p.len()].copy_from_slice(&p[..]); +fn pad(msg: &[u8]) -> Vec { + let mut o = vec![0; msg.len() + SIZE_MESSAGE_PREFIX]; + o[SIZE_MESSAGE_PREFIX..SIZE_MESSAGE_PREFIX + msg.len()].copy_from_slice(msg); o } @@ -180,7 +180,7 @@ fn bench_outbound(b: &mut Bencher) { IpAddr::V4(_) => "127.0.0.1".parse().unwrap(), IpAddr::V6(_) => "::1".parse().unwrap(), }; - let msg = make_packet_padded(1024, src, dst, 0); + let msg = pad(&make_packet(1024, src, dst, 0)); // every iteration sends 10 GB b.iter(|| { @@ -266,10 +266,10 @@ fn test_outbound() { IpAddr::V4(_) => "127.0.0.1".parse().unwrap(), IpAddr::V6(_) => "::1".parse().unwrap(), }; - let msg = make_packet_padded(SIZE_MSG, src, dst, 0); + let msg = make_packet(SIZE_MSG, src, dst, 0); // crypto-key route the IP packet - let res = router.send(msg); + let res = router.send(pad(&msg)); assert_eq!( res.is_ok(), okay, @@ -303,7 +303,7 @@ fn test_outbound() { if send_payload { assert_eq!( opaque.send.wait(TIMEOUT), - Some((SIZE_KEEPALIVE + SIZE_MSG, false)), + Some((SIZE_KEEPALIVE + msg.len(), false)), "message buffer should be encrypted" ) } @@ -319,6 +319,8 @@ fn test_outbound() { fn test_bidirectional() { init(); + const MAX_SIZE_BODY: usize = 1 << 15; + let tests = [ ( ("192.168.1.0", 24, "192.168.1.20", true), @@ -358,6 +360,8 @@ fn test_bidirectional() { ), ]; + let mut rng = rand::thread_rng(); + for (p1, p2) in tests.iter() { for confirm_with_staged_packet in vec![true, false] { println!( @@ -368,11 +372,7 @@ fn test_bidirectional() { let ((bind_reader1, bind_writer1), (bind_reader2, bind_writer2)) = dummy::PairBind::pair(); - let confirm_packet_size = if confirm_with_staged_packet { - SIZE_KEEPALIVE + SIZE_MSG - } else { - SIZE_KEEPALIVE - }; + let mut confirm_packet_size = SIZE_KEEPALIVE; // create matching device let (_fake, _, tun_writer1, _) = dummy::TunTest::create(false); @@ -412,15 +412,21 @@ fn test_bidirectional() { // create IP packet let (_mask, _len, ip1, _okay) = p1; let (_mask, _len, ip2, _okay) = p2; - let msg = make_packet_padded( + + let msg = make_packet( SIZE_MSG, ip1.parse().unwrap(), // src ip2.parse().unwrap(), // dst 0, ); + // calculate size of encapsulated IP packet + confirm_packet_size = msg.len() + SIZE_KEEPALIVE; + // stage packet for sending - router2.send(msg).expect("failed to sent staged packet"); + router2 + .send(pad(&msg)) + .expect("failed to sent staged packet"); // a new key should have been requested from the handshake machine assert_eq!( @@ -429,6 +435,7 @@ fn test_bidirectional() { "a new key should be requested since a packet was attempted transmitted" ); + // no other events should fire no_events!(opaque1); no_events!(opaque2); } @@ -454,12 +461,7 @@ fn test_bidirectional() { buf.truncate(len); assert_eq!( - len, - if confirm_with_staged_packet { - SIZE_MSG + SIZE_KEEPALIVE - } else { - SIZE_KEEPALIVE - }, + len, confirm_packet_size, "unexpected size of confirmation message" ); @@ -491,31 +493,39 @@ fn test_bidirectional() { // no other events should fire no_events!(opaque1); no_events!(opaque2); + // now that peer1 has an endpoint // route packets in the other direction: peer1 -> peer2 - for id in 1..11 { - println!("packet: {}", id); - - let message_size = 1024; + let mut sizes = vec![0, 1, 1500, MAX_SIZE_BODY]; + for _ in 0..100 { + let body_size: usize = rng.gen(); + let body_size = body_size % MAX_SIZE_BODY; + sizes.push(body_size); + } + for (id, body_size) in sizes.iter().enumerate() { + println!("packet: id = {}, body_size = {}", id, body_size); // pass IP packet to router let (_mask, _len, ip1, _okay) = p1; let (_mask, _len, ip2, _okay) = p2; - let msg = make_packet_padded( - message_size, + let msg = make_packet( + *body_size, ip2.parse().unwrap(), // src ip1.parse().unwrap(), // dst - id, + id as u64, ); + // calculate encrypted size + let encrypted_size = msg.len() + SIZE_KEEPALIVE; + router1 - .send(msg) + .send(pad(&msg)) .expect("we expect routing to be successful"); // encryption succeeds and the correct size is logged assert_eq!( opaque1.send.wait(TIMEOUT), - Some((message_size + SIZE_KEEPALIVE, true)), + Some((encrypted_size, true)), "expected send event for peer1 -> peer2 payload" ); @@ -524,7 +534,7 @@ fn test_bidirectional() { no_events!(opaque2); // receive ("across the internet") on the other end - let mut buf = vec![0u8; 2048]; + let mut buf = vec![0u8; MAX_SIZE_BODY + 512]; let (len, from) = bind_reader2.read(&mut buf).unwrap(); buf.truncate(len); router2.recv(from, buf).unwrap(); @@ -532,7 +542,7 @@ fn test_bidirectional() { // check that decryption succeeds assert_eq!( opaque2.recv.wait(TIMEOUT), - Some((message_size + SIZE_KEEPALIVE, true)), + Some((msg.len() + SIZE_KEEPALIVE, true)), "decryption and routing should succeed" ); diff --git a/src/wireguard/tests.rs b/src/wireguard/tests.rs index 2ed2202..4cc441e 100644 --- a/src/wireguard/tests.rs +++ b/src/wireguard/tests.rs @@ -1,3 +1,7 @@ +use super::dummy; +use super::wireguard::WireGuard; + +use std::convert::TryInto; use std::net::IpAddr; use hex; @@ -8,43 +12,43 @@ use x25519_dalek::{PublicKey, StaticSecret}; use pnet::packet::ipv4::MutableIpv4Packet; use pnet::packet::ipv6::MutableIpv6Packet; -use super::dummy; -use super::wireguard::WireGuard; - pub fn make_packet(size: usize, src: IpAddr, dst: IpAddr, id: u64) -> Vec { // expand pseudo random payload let mut rng: _ = ChaCha8Rng::seed_from_u64(id); let mut p: Vec = vec![0; size]; - rng.fill_bytes(&mut p[..]); + rng.fill_bytes(&mut p); // create "IP packet" let mut msg = Vec::with_capacity(size); - msg.resize(size, 0); match dst { IpAddr::V4(dst) => { - let length = size - MutableIpv4Packet::minimum_packet_size(); + let length = size + MutableIpv4Packet::minimum_packet_size(); + msg.resize(length, 0); + let mut packet = MutableIpv4Packet::new(&mut msg[..]).unwrap(); packet.set_destination(dst); - packet.set_total_length(size as u16); + packet.set_total_length(length.try_into().expect("length too great for IPv4 packet")); packet.set_source(if let IpAddr::V4(src) = src { src } else { panic!("src.version != dst.version") }); - packet.set_payload(&p[..length]); + packet.set_payload(&p); packet.set_version(4); } IpAddr::V6(dst) => { - let length = size - MutableIpv6Packet::minimum_packet_size(); + let length = size + MutableIpv6Packet::minimum_packet_size(); + msg.resize(length, 0); + let mut packet = MutableIpv6Packet::new(&mut msg[..]).unwrap(); packet.set_destination(dst); - packet.set_payload_length(length as u16); + packet.set_payload_length(size.try_into().expect("length too great for IPv6 packet")); packet.set_source(if let IpAddr::V6(src) = src { src } else { panic!("src.version != dst.version") }); - packet.set_payload(&p[..length]); + packet.set_payload(&p); packet.set_version(6); } } @@ -83,7 +87,7 @@ fn test_pure_wireguard() { wg1.add_udp_reader(bind_reader1); wg2.add_udp_reader(bind_reader2); - // generate (public, private) key pairs + // configure (public, private) key pairs let sk1 = StaticSecret::from([ 0x3f, 0x69, 0x86, 0xd1, 0xc0, 0xec, 0x25, 0xa0, 0x9c, 0x8e, 0x56, 0xb5, 0x1d, 0xb7, 0x3c, @@ -107,7 +111,7 @@ fn test_pure_wireguard() { wg1.set_key(Some(sk1)); wg2.set_key(Some(sk2)); - // configure cryptkey router + // configure crypto-key router let peer2 = wg1.lookup_peer(&pk2).unwrap(); let peer1 = wg2.lookup_peer(&pk1).unwrap(); @@ -143,10 +147,13 @@ fn test_pure_wireguard() { let mut backup = packets.clone(); while let Some(p) = packets.pop() { + println!("send"); fake1.write(p); } while let Some(p) = backup.pop() { + println!("read"); + assert_eq!( hex::encode(fake2.read()), hex::encode(p), -- cgit v1.2.3-59-g8ed1b