add test for old-style Response packets

main
_ 2021-12-08 00:26:13 +00:00
parent 6dc4eb2771
commit ca8fcc1104
2 changed files with 28 additions and 7 deletions

View File

@ -78,21 +78,20 @@ fn main () -> Result <(), AppError> {
let subcommand: Option <String> = args.next (); let subcommand: Option <String> = args.next ();
let mut common_params = CommonParams::default ();
match subcommand.as_ref ().map (|x| &x[..]) { match subcommand.as_ref ().map (|x| &x[..]) {
None => return Err (CliArgError::MissingSubcommand.into ()), None => return Err (CliArgError::MissingSubcommand.into ()),
Some ("client") => client (&common_params)?, Some ("client") => client ()?,
Some ("server") => server (&common_params)?, Some ("server") => server (args)?,
Some (x) => return Err (CliArgError::UnknownSubcommand (x.to_string ()).into ()), Some (x) => return Err (CliArgError::UnknownSubcommand (x.to_string ()).into ()),
} }
Ok (()) Ok (())
} }
fn client (common_params: &CommonParams) -> Result <(), AppError> { fn client () -> Result <(), AppError> {
use rand::RngCore; use rand::RngCore;
let mut common_params = CommonParams::default ();
let socket = UdpSocket::bind ("0.0.0.0:0")?; let socket = UdpSocket::bind ("0.0.0.0:0")?;
socket.join_multicast_v4 (&common_params.multicast_addr, &([0u8, 0, 0, 0].into ()))?; 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 (()) Ok (())
} }
fn server (common_params: &CommonParams) -> Result <(), AppError> fn server <I: Iterator <Item=String>> (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 ()); let our_mac = get_mac_address ()?.map (|x| x.bytes ());
if our_mac.is_none () { 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"); println! ("Warning: Can't find our own MAC address. We won't be able to respond to MAC-specific lookaround requests");

View File

@ -113,8 +113,27 @@ impl Message {
#[cfg (test)] #[cfg (test)]
mod test { mod test {
use super::*;
#[test] #[test]
fn test_1 () { 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);
}
} }
} }