🚨 fix all clippy warnings

main
_ 2021-12-09 00:23:48 +00:00
parent ee51bb7d3d
commit bf9d185092
3 changed files with 24 additions and 132 deletions

View File

@ -148,7 +148,7 @@ struct ServerResponse {
fn client <I : Iterator <Item=String>> (mut args: I) -> Result <(), AppError> { fn client <I : Iterator <Item=String>> (mut args: I) -> Result <(), AppError> {
use rand::RngCore; use rand::RngCore;
let mut common_params = CommonParams::default (); let common_params = CommonParams::default ();
let mut bind_addr = "0.0.0.0".to_string (); let mut bind_addr = "0.0.0.0".to_string ();
while let Some (arg) = args.next () { while let Some (arg) = args.next () {
@ -208,7 +208,7 @@ fn client <I : Iterator <Item=String>> (mut args: I) -> Result <(), AppError> {
} }
let mut peers: Vec <_> = peers.into_iter ().collect (); let mut peers: Vec <_> = peers.into_iter ().collect ();
peers.sort_by_key (|(k, v)| v.mac); peers.sort_by_key (|(_, v)| v.mac);
println! ("Found {} peers:", peers.len ()); println! ("Found {} peers:", peers.len ());
for (ip, resp) in peers.into_iter () { for (ip, resp) in peers.into_iter () {
@ -236,7 +236,7 @@ fn client <I : Iterator <Item=String>> (mut args: I) -> Result <(), AppError> {
fn server <I: Iterator <Item=String>> (mut args: I) -> Result <(), AppError> fn server <I: Iterator <Item=String>> (mut args: I) -> Result <(), AppError>
{ {
let mut common_params = CommonParams::default (); let common_params = CommonParams::default ();
let mut bind_addr = "0.0.0.0".to_string (); let mut bind_addr = "0.0.0.0".to_string ();
let mut nickname = String::new (); let mut nickname = String::new ();

View File

@ -138,28 +138,6 @@ impl Message {
Ok (cursor.into_inner ()) Ok (cursor.into_inner ())
} }
fn read1 <R: std::io::Read> (r: &mut R) -> Result <Self, MessageError> {
let t = tlv::Reader::u8 (r)?;
Ok (match t {
1 => {
let mut idem_id = [0u8; 8];
r.read_exact (&mut idem_id)?;
let mac = Self::read_mac_opt (r)?;
Self::Request1 {
idem_id,
mac,
}
},
2 => {
let mac = Self::read_mac_opt (r)?;
Self::Response1 (mac)
},
_ => return Err (MessageError::UnknownType),
})
}
fn read2 <R: std::io::Read> (r: &mut R) -> Result <Self, MessageError> { fn read2 <R: std::io::Read> (r: &mut R) -> Result <Self, MessageError> {
let t = tlv::Reader::u8 (r)?; let t = tlv::Reader::u8 (r)?;
@ -179,24 +157,12 @@ impl Message {
Self::Response1 (mac) Self::Response1 (mac)
}, },
3 => { 3 => {
let mut len = [0; 4]; tlv::Reader::<_>::length (r)?;
r.read_exact (&mut len)?;
let _len = len;
let mut idem_id = [0; 8]; let mut idem_id = [0; 8];
r.read_exact (&mut idem_id)?; r.read_exact (&mut idem_id)?;
let mut nickname_len = [0; 4]; let nickname = tlv::Reader::<_>::lv_bytes_to_vec (r, 64)?;
r.read_exact (&mut nickname_len)?;
let nickname_len = u32::from_le_bytes (nickname_len);
let nickname_len = usize::try_from (nickname_len)?;
if nickname_len > 64 {
return Err (MessageError::LengthPrefixTooLong ((64, nickname_len)));
}
let mut nickname = vec! [0u8; nickname_len];
r.read_exact (&mut nickname)?;
let nickname = String::from_utf8 (nickname)?; let nickname = String::from_utf8 (nickname)?;
Self::Response2 (Response2 { Self::Response2 (Response2 {
@ -221,12 +187,6 @@ impl Message {
}) })
} }
pub fn from_slice1 (buf: &[u8]) -> Result <Self, MessageError> {
let mut cursor = Cursor::new (buf);
tlv::Reader::expect (&mut cursor, &MAGIC_NUMBER)?;
Self::read1 (&mut cursor)
}
pub fn from_slice2 (buf: &[u8]) -> Result <Vec <Self>, MessageError> { pub fn from_slice2 (buf: &[u8]) -> Result <Vec <Self>, MessageError> {
let mut cursor = Cursor::new (buf); let mut cursor = Cursor::new (buf);
tlv::Reader::expect (&mut cursor, &MAGIC_NUMBER)?; tlv::Reader::expect (&mut cursor, &MAGIC_NUMBER)?;
@ -375,78 +335,4 @@ mod test {
assert_eq! (input, decoded); assert_eq! (input, decoded);
} }
} }
#[test]
fn test_read_1 () {
for input in [
Message::Request1 {
idem_id: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08],
mac: None,
},
Message::Response1 (Some ([0x11, 0x22, 0x33, 0x44, 0x55, 0x66])),
Message::Response1 (None),
].into_iter () {
let encoded = input.to_vec ().unwrap ();
let decoded = Message::from_slice1 (&encoded).unwrap ();
assert_eq! (input, decoded);
}
for (expected, input) in [
(
Message::Request1 {
idem_id: [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08],
mac: None,
},
vec! [
154, 74, 67, 129,
// Request tag
1,
// Idem ID
1, 2, 3, 4, 5, 6, 7, 8,
// MAC is None
0,
],
),
(
Message::Response1 (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,
],
),
(
Message::Response1 (None),
vec! [
// Magic number for LookAround packets
154, 74, 67, 129,
// Response tag
2,
// MAC is None
0,
],
),
(
Message::Response1 (None),
vec! [
// Magic number for LookAround packets
154, 74, 67, 129,
// Response tag
2,
// MAC is None
0,
// New tag that older versions will just ignore
255,
],
),
].into_iter () {
let actual = Message::from_slice1 (&input).unwrap ();
assert_eq! (actual, expected, "{:?}", actual);
}
}
} }

View File

@ -6,8 +6,14 @@ type Result <T> = std::result::Result <T, TlvError>;
pub enum TlvError { pub enum TlvError {
#[error ("Buffer too big")] #[error ("Buffer too big")]
BufferTooBig, BufferTooBig,
#[error ("Caller-provided buffer too small")]
CallerBufferTooSmall, // Violets are purple,
// To live is to suffer,
// The data is too big,
// For the gosh-darn buffer.
#[error ("Data too big")]
DataTooBig,
#[error (transparent)] #[error (transparent)]
Io (#[from] std::io::Error), Io (#[from] std::io::Error),
#[error ("Actual bytes didn't match expected bytes")] #[error ("Actual bytes didn't match expected bytes")]
@ -54,22 +60,24 @@ impl <R: std::io::Read> Reader <R> {
Ok (()) Ok (())
} }
fn length (r: &mut R) -> Result <u32> { pub fn length (r: &mut R) -> Result <u32> {
let mut buf = [0; 4]; let mut buf = [0; 4];
r.read_exact (&mut buf)?; r.read_exact (&mut buf)?;
Ok (u32::from_le_bytes (buf)) Ok (u32::from_le_bytes (buf))
} }
fn lv_bytes (r: &mut R, buf: &mut [u8]) -> Result <u32> { pub fn lv_bytes_to_vec (r: &mut R, limit: usize) -> Result <Vec <u8>> {
let l = Self::length (r)?; let l = Self::length (r)?;
if usize::try_from (l)? > buf.len () { let l = usize::try_from (l)?;
return Err (TlvError::CallerBufferTooSmall); if l > limit {
return Err (TlvError::DataTooBig);
} }
r.read_exact (&mut buf [0..usize::try_from (l)?])?; let mut v = vec! [0u8; l];
r.read_exact (&mut v)?;
Ok (l) Ok (v)
} }
pub fn u8 (r: &mut R) -> std::io::Result <u8> { pub fn u8 (r: &mut R) -> std::io::Result <u8> {
@ -102,11 +110,9 @@ mod test {
let mut r = Cursor::new (v); let mut r = Cursor::new (v);
let mut buf = vec! [0; 1024]; let buf = super::Reader::lv_bytes_to_vec (&mut r, 1024).unwrap ();
let bytes_read = super::Reader::lv_bytes (&mut r, &mut buf).unwrap (); assert_eq! (buf.len (), b.len ());
assert_eq! (b, &buf [0..usize::try_from (buf.len ()).unwrap ()]);
assert_eq! (usize::try_from (bytes_read).unwrap (), b.len ());
assert_eq! (b, &buf [0..usize::try_from (bytes_read).unwrap ()]);
} }
} }