From ca8fcc11040fb35f02a8b707cddb1053feaff245 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Wed, 8 Dec 2021 00:26:13 +0000 Subject: [PATCH] :white_check_mark: add test for old-style Response packets --- src/main.rs | 14 ++++++++------ src/message.rs | 21 ++++++++++++++++++++- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index f48cb0a..9021d2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -78,21 +78,20 @@ fn main () -> Result <(), AppError> { let subcommand: Option = args.next (); - let mut common_params = CommonParams::default (); - match subcommand.as_ref ().map (|x| &x[..]) { None => return Err (CliArgError::MissingSubcommand.into ()), - Some ("client") => client (&common_params)?, - Some ("server") => server (&common_params)?, + Some ("client") => client ()?, + Some ("server") => server (args)?, Some (x) => return Err (CliArgError::UnknownSubcommand (x.to_string ()).into ()), } Ok (()) } -fn client (common_params: &CommonParams) -> Result <(), AppError> { +fn client () -> Result <(), AppError> { use rand::RngCore; + let mut common_params = CommonParams::default (); let socket = UdpSocket::bind ("0.0.0.0:0")?; socket.join_multicast_v4 (&common_params.multicast_addr, &([0u8, 0, 0, 0].into ()))?; @@ -143,8 +142,11 @@ fn client (common_params: &CommonParams) -> Result <(), AppError> { Ok (()) } -fn server (common_params: &CommonParams) -> Result <(), AppError> +fn server > (args: I) -> Result <(), AppError> { + let mut common_params = CommonParams::default (); + let mut nickname = String::new (); + let our_mac = get_mac_address ()?.map (|x| x.bytes ()); if our_mac.is_none () { println! ("Warning: Can't find our own MAC address. We won't be able to respond to MAC-specific lookaround requests"); diff --git a/src/message.rs b/src/message.rs index ad91c95..5e1aea9 100644 --- a/src/message.rs +++ b/src/message.rs @@ -113,8 +113,27 @@ impl Message { #[cfg (test)] mod test { + use super::*; + #[test] fn test_1 () { - + for (input, expected) in [ + ( + Message::Response (Some ([0x11, 0x22, 0x33, 0x44, 0x55, 0x66])), + vec! [ + // Magic number for LookAround packets + 154, 74, 67, 129, + // Response tag + 2, + // MAC is Some + 1, + // MAC + 17, 34, 51, 68, 85, 102, + ], + ), + ].into_iter () { + let actual = input.to_vec ().unwrap (); + assert_eq! (actual, expected, "{:?}", input); + } } }