Skip to content

Commit

Permalink
provide unit test for invalid fopts_len
Browse files Browse the repository at this point in the history
  • Loading branch information
lthiery committed Aug 16, 2023
1 parent 6f8ada0 commit 7d88d6a
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lorawan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl Fhdr {
payload_type: MType,
reader: &mut dyn Buf,
) -> Result<Self, LoraWanError> {
// Checj minimum length requirements
// Check minimum length requirements
if reader.remaining() < FHDR_MIN_SIZE {
return Err(LoraWanError::InvalidPacketSize(
payload_type,
Expand Down Expand Up @@ -604,6 +604,26 @@ mod test {
}
}

#[test]
fn test_fopts_len_error() {
// FCtrl indicates 8 bytes in FOpts but we will pass empty FOpts
let mut fctrl_uplink = FCtrlUplink(0);
fctrl_uplink.set_fopts_len(8);
let fhdr = Fhdr {
dev_addr: 0,
fcnt: 0,
fctrl: FCtrl::Uplink(fctrl_uplink),
fopts: Bytes::new(),
};
let mut buffer = Vec::new();
fhdr.write(&mut buffer).unwrap();
let read = Fhdr::read(Direction::Uplink, MType::UnconfirmedUp, &mut &buffer[..]);
match read {
Err(LoraWanError::InvalidPacketSize(MType::UnconfirmedUp, 0)) => (),
_ => panic!("Error was expected! There was none or it was the wrong type"),
}
}

impl TryFrom<&[u8]> for Routing {
type Error = LoraWanError;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
Expand Down

0 comments on commit 7d88d6a

Please sign in to comment.