Add unit tests for TestMode enum
- from_flags() tests for all flag combinations - Helper method tests (needs_mixnet, uses_lp, tests_wireguard, needs_exit_gateway) - Display and FromStr tests with alternate formats - Roundtrip test ensuring Display/FromStr consistency Closes: nym-39mt
This commit is contained in:
@@ -99,3 +99,162 @@ impl std::str::FromStr for TestMode {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// ============ from_flags() tests ============
|
||||
|
||||
#[test]
|
||||
fn test_from_flags_default_is_mixnet() {
|
||||
// All flags false -> Mixnet (default)
|
||||
assert_eq!(
|
||||
TestMode::from_flags(false, false, false, false),
|
||||
TestMode::Mixnet
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_flags_only_wireguard_is_mixnet() {
|
||||
// only_wireguard still uses mixnet path (WG via authenticator)
|
||||
assert_eq!(
|
||||
TestMode::from_flags(true, false, false, false),
|
||||
TestMode::Mixnet
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_flags_only_lp_registration() {
|
||||
// only_lp_registration -> LpOnly (takes priority)
|
||||
assert_eq!(
|
||||
TestMode::from_flags(false, true, false, false),
|
||||
TestMode::LpOnly
|
||||
);
|
||||
// Even with other flags set, only_lp_registration wins
|
||||
assert_eq!(
|
||||
TestMode::from_flags(true, true, true, true),
|
||||
TestMode::LpOnly
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_flags_test_lp_wg_single_hop() {
|
||||
// test_lp_wg without exit gateway -> SingleHop
|
||||
assert_eq!(
|
||||
TestMode::from_flags(false, false, true, false),
|
||||
TestMode::SingleHop
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_flags_test_lp_wg_two_hop() {
|
||||
// test_lp_wg with exit gateway -> TwoHop
|
||||
assert_eq!(
|
||||
TestMode::from_flags(false, false, true, true),
|
||||
TestMode::TwoHop
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_flags_has_exit_gateway_alone_is_mixnet() {
|
||||
// has_exit_gateway alone doesn't change mode
|
||||
assert_eq!(
|
||||
TestMode::from_flags(false, false, false, true),
|
||||
TestMode::Mixnet
|
||||
);
|
||||
}
|
||||
|
||||
// ============ Helper method tests ============
|
||||
|
||||
#[test]
|
||||
fn test_needs_mixnet() {
|
||||
assert!(TestMode::Mixnet.needs_mixnet());
|
||||
assert!(!TestMode::SingleHop.needs_mixnet());
|
||||
assert!(!TestMode::TwoHop.needs_mixnet());
|
||||
assert!(!TestMode::LpOnly.needs_mixnet());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_uses_lp() {
|
||||
assert!(!TestMode::Mixnet.uses_lp());
|
||||
assert!(TestMode::SingleHop.uses_lp());
|
||||
assert!(TestMode::TwoHop.uses_lp());
|
||||
assert!(TestMode::LpOnly.uses_lp());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tests_wireguard() {
|
||||
assert!(TestMode::Mixnet.tests_wireguard());
|
||||
assert!(TestMode::SingleHop.tests_wireguard());
|
||||
assert!(TestMode::TwoHop.tests_wireguard());
|
||||
assert!(!TestMode::LpOnly.tests_wireguard());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_needs_exit_gateway() {
|
||||
assert!(!TestMode::Mixnet.needs_exit_gateway());
|
||||
assert!(!TestMode::SingleHop.needs_exit_gateway());
|
||||
assert!(TestMode::TwoHop.needs_exit_gateway());
|
||||
assert!(!TestMode::LpOnly.needs_exit_gateway());
|
||||
}
|
||||
|
||||
// ============ Display tests ============
|
||||
|
||||
#[test]
|
||||
fn test_display() {
|
||||
assert_eq!(TestMode::Mixnet.to_string(), "mixnet");
|
||||
assert_eq!(TestMode::SingleHop.to_string(), "single-hop");
|
||||
assert_eq!(TestMode::TwoHop.to_string(), "two-hop");
|
||||
assert_eq!(TestMode::LpOnly.to_string(), "lp-only");
|
||||
}
|
||||
|
||||
// ============ FromStr tests ============
|
||||
|
||||
#[test]
|
||||
fn test_from_str_canonical() {
|
||||
assert_eq!("mixnet".parse::<TestMode>().unwrap(), TestMode::Mixnet);
|
||||
assert_eq!("single-hop".parse::<TestMode>().unwrap(), TestMode::SingleHop);
|
||||
assert_eq!("two-hop".parse::<TestMode>().unwrap(), TestMode::TwoHop);
|
||||
assert_eq!("lp-only".parse::<TestMode>().unwrap(), TestMode::LpOnly);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_str_alternate_formats() {
|
||||
// snake_case
|
||||
assert_eq!("single_hop".parse::<TestMode>().unwrap(), TestMode::SingleHop);
|
||||
assert_eq!("two_hop".parse::<TestMode>().unwrap(), TestMode::TwoHop);
|
||||
assert_eq!("lp_only".parse::<TestMode>().unwrap(), TestMode::LpOnly);
|
||||
|
||||
// no separator
|
||||
assert_eq!("singlehop".parse::<TestMode>().unwrap(), TestMode::SingleHop);
|
||||
assert_eq!("twohop".parse::<TestMode>().unwrap(), TestMode::TwoHop);
|
||||
assert_eq!("lponly".parse::<TestMode>().unwrap(), TestMode::LpOnly);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_str_case_insensitive() {
|
||||
assert_eq!("MIXNET".parse::<TestMode>().unwrap(), TestMode::Mixnet);
|
||||
assert_eq!("Single-Hop".parse::<TestMode>().unwrap(), TestMode::SingleHop);
|
||||
assert_eq!("TWO_HOP".parse::<TestMode>().unwrap(), TestMode::TwoHop);
|
||||
assert_eq!("LpOnly".parse::<TestMode>().unwrap(), TestMode::LpOnly);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_str_invalid() {
|
||||
assert!("invalid".parse::<TestMode>().is_err());
|
||||
assert!("".parse::<TestMode>().is_err());
|
||||
assert!("mix".parse::<TestMode>().is_err());
|
||||
}
|
||||
|
||||
// ============ Roundtrip test ============
|
||||
|
||||
#[test]
|
||||
fn test_display_fromstr_roundtrip() {
|
||||
for mode in [TestMode::Mixnet, TestMode::SingleHop, TestMode::TwoHop, TestMode::LpOnly] {
|
||||
let s = mode.to_string();
|
||||
let parsed: TestMode = s.parse().unwrap();
|
||||
assert_eq!(mode, parsed);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user