diff options
Diffstat (limited to 'src/anon_req.rs')
| -rw-r--r-- | src/anon_req.rs | 64 |
1 files changed, 43 insertions, 21 deletions
diff --git a/src/anon_req.rs b/src/anon_req.rs index 60d58e4..acc27b6 100644 --- a/src/anon_req.rs +++ b/src/anon_req.rs @@ -1,6 +1,10 @@ -use chrono::{DateTime, Utc}; +use crate::{ + MeshcoreStringError, + crypto::PublicKey, + string_helper::{PasswordString, password_string_from_slice}, +}; use bytes::{Buf, Bytes}; -use crate::{MeshcoreStringError, crypto::PublicKey, string_helper::{PasswordString, password_string_from_slice}}; +use chrono::{DateTime, Utc}; #[derive(PartialEq, Clone, core::fmt::Debug)] pub struct AnonReq { @@ -9,7 +13,7 @@ pub struct AnonReq { pub mac: u16, pub ciphertext: Bytes, pub request: Option<ClearAnonRequest>, - incomplete: bool + incomplete: bool, } impl AnonReq { @@ -36,15 +40,21 @@ impl From<Bytes> for AnonReq { incomplete: false, }; - if bytes.is_empty() { return anon_req; } + if bytes.is_empty() { + return anon_req; + } anon_req.dest = bytes.get_u8(); - if bytes.len() < 32 { return anon_req; } + if bytes.len() < 32 { + return anon_req; + } if let Ok(pub_key) = PublicKey::try_from(bytes.split_to(32)) { anon_req.public_key = pub_key; } - if bytes.len() < 2 { return anon_req; } + if bytes.len() < 2 { + return anon_req; + } anon_req.mac = bytes.get_u16(); anon_req.ciphertext = bytes; @@ -55,10 +65,18 @@ impl From<Bytes> for AnonReq { impl core::fmt::Display for AnonReq { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - f.write_fmt(format_args!("({:2x?}) -> ({:2x?}) MAC: {:4x?} ", self.public_key.hash_prefix() >> 24, self.dest, self.mac))?; + f.write_fmt(format_args!( + "({:2x?}) -> ({:2x?}) MAC: {:4x?} ", + self.public_key.hash_prefix() >> 24, + self.dest, + self.mac + ))?; if let Some(cleartext) = &self.request { - f.write_fmt(format_args!("at: {} password: \"{}\"", cleartext.timestamp, cleartext.password)) + f.write_fmt(format_args!( + "at: {} password: \"{}\"", + cleartext.timestamp, cleartext.password + )) } else { f.write_str("ENCRYPTED") } @@ -81,16 +99,18 @@ impl From<Bytes> for ClearAnonRequest { // and never changes, and it exercised extensively in tests. timestamp: DateTime::from_timestamp(0, 0).unwrap(), sync_timestamp: None, - password: PasswordString::new() + password: PasswordString::new(), }; // Just check for the whole fixed-size part at once - if bytes.len() < 4 { return anon_req } + if bytes.len() < 4 { + return anon_req; + } if let Some(timestamp) = DateTime::from_timestamp(bytes.get_u32_le() as i64, 0) { anon_req.timestamp = timestamp; } - // Strip-off any null characters after the password + // Strip-off any null characters after the password if let Some(pass) = bytes.split(|b| *b == 0).next() { anon_req.password = password_string_from_slice(pass); } @@ -102,15 +122,15 @@ impl From<Bytes> for ClearAnonRequest { // Tests for std operations #[cfg(test)] mod tests { + use super::*; + use crate::crypto::*; + use crate::packet::*; + use crate::packet_content::PacketContent; + use crate::std_identity::KeystoreInput; + use hex::decode; use std::collections::HashMap; use std::str::FromStr; - use hex::decode; use tinyvec::ArrayVec; - use crate::std_identity::KeystoreInput; - use crate::packet::*; - use crate::crypto::*; - use crate::packet_content::PacketContent; - use super::*; #[test] fn anon_req() { @@ -155,13 +175,15 @@ mod tests { assert_eq!(lhs_packet, rhs_packet); println!("\"{}\"", rhs_packet); - assert_eq!(format!("{}", rhs_packet), " Flood | v1 | | [] | | ANON REQ. | (12) -> (34) MAC: 4e7b ENCRYPTED"); + assert_eq!( + format!("{}", rhs_packet), + " Flood | v1 | | [] | | ANON REQ. | (12) -> (34) MAC: 4e7b ENCRYPTED" + ); rhs_packet.try_decrypt(&keystore); - + let lhs_string = format!("{}", rhs_packet); let rhs_string = " Flood | v1 | | [] | | ANON REQ. | (12) -> (34) MAC: 4e7b at: 2025-11-13 17:21:13 UTC password: \"12345\""; assert_eq!(lhs_string, rhs_string); } - -}
\ No newline at end of file +} |
