From c483e42e0b24613bbf880d5743455b7770471311 Mon Sep 17 00:00:00 2001 From: jmwample Date: Wed, 30 Oct 2024 14:19:28 -0600 Subject: [PATCH] add a test to make sure things serialize as expected --- nym-vpn-core/crates/nym-wg-go/src/amnezia.rs | 46 ++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/nym-vpn-core/crates/nym-wg-go/src/amnezia.rs b/nym-vpn-core/crates/nym-wg-go/src/amnezia.rs index f5c02d7f10..25f4e3ae16 100644 --- a/nym-vpn-core/crates/nym-wg-go/src/amnezia.rs +++ b/nym-vpn-core/crates/nym-wg-go/src/amnezia.rs @@ -17,7 +17,7 @@ const OFF: AmneziaConfig = AmneziaConfig { transport_pkt_magic_header: 4, }; -const DEFAULT: AmneziaConfig = AmneziaConfig { +const BASE: AmneziaConfig = AmneziaConfig { junk_pkt_count: 4, junk_pkt_min_size: 40, junk_pkt_max_size: 70, @@ -67,7 +67,7 @@ pub struct AmneziaConfig { impl Default for AmneziaConfig { fn default() -> Self { - DEFAULT.clone() + OFF.clone() } } @@ -102,6 +102,12 @@ impl AmneziaConfig { OFF.clone() } + /// Returns a configuration that enables only the basic junk packet feature + /// of amneziawg + pub fn basic() -> Self { + BASE.clone() + } + /// Adds the contained AmneziaWG parameters to the UAPI Config pub fn append_to(&self, config_builder: &mut UapiConfigBuilder) { if self == &OFF { @@ -111,7 +117,7 @@ impl AmneziaConfig { config_builder.add("Jmin", self.junk_pkt_min_size.to_string().as_str()); config_builder.add("Jmax", self.junk_pkt_max_size.to_string().as_str()); - if self == &DEFAULT { + if self == &BASE { return; } @@ -168,3 +174,37 @@ impl AmneziaConfig { true } } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_encode_amnezia_config() { + let mut config_builder = UapiConfigBuilder::new(); + OFF.append_to(&mut config_builder); + assert_eq!(config_builder.into_bytes(), b"\n"); + + let mut config_builder = UapiConfigBuilder::new(); + BASE.append_to(&mut config_builder); + assert_eq!(config_builder.into_bytes(), b"Jc=4\nJmin=40\nJmax=70\n\n"); + + let c = AmneziaConfig { + junk_pkt_count: 1, + junk_pkt_min_size: 20, + junk_pkt_max_size: 30, + init_pkt_junk_size: 40, + response_pkt_junk_size: 50, + init_pkt_magic_header: 11, + response_pkt_magic_header: 12, + under_load_pkt_magic_header: 13, + transport_pkt_magic_header: 14, + }; + let mut config_builder = UapiConfigBuilder::new(); + c.append_to(&mut config_builder); + assert_eq!( + config_builder.into_bytes(), + b"Jc=1\nJmin=20\nJmax=30\nS1=40\nS2=50\nH1=11\nH2=12\nH3=13\nH4=14\n\n" + ); + } +}