Skip to content

Commit

Permalink
add a test to make sure things serialize as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
jmwample committed Oct 30, 2024
1 parent f0aa889 commit c483e42
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions nym-vpn-core/crates/nym-wg-go/src/amnezia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -67,7 +67,7 @@ pub struct AmneziaConfig {

impl Default for AmneziaConfig {
fn default() -> Self {
DEFAULT.clone()
OFF.clone()
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}

Expand Down Expand Up @@ -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"
);
}
}

0 comments on commit c483e42

Please sign in to comment.