use bebytes::BeBytes;
#[derive(Debug, Clone, PartialEq, BeBytes)]
struct CoapMessage {
pub header: u8,
pub token_length: u8,
#[UntilMarker(0xFF)]
pub options: Vec<u8>,
#[AfterMarker(0xFF)]
pub payload: Vec<u8>,
}
#[test]
fn test_until_marker_field() {
let msg = CoapMessage {
header: 0x45,
token_length: 4,
options: vec![0x11, 0x22, 0x33],
payload: vec![0xAA, 0xBB, 0xCC],
};
let bytes = msg.to_be_bytes();
assert_eq!(bytes.len(), 10);
assert_eq!(bytes[0], 0x45); assert_eq!(bytes[1], 4); assert_eq!(bytes[2], 0x11); assert_eq!(bytes[3], 0x22); assert_eq!(bytes[4], 0x33); assert_eq!(bytes[5], 0xFF); assert_eq!(bytes[6], 0xFF); assert_eq!(bytes[7], 0xAA); assert_eq!(bytes[8], 0xBB); assert_eq!(bytes[9], 0xCC);
let (parsed, consumed) = CoapMessage::try_from_be_bytes(&bytes).unwrap();
assert_eq!(consumed, 10);
assert_eq!(parsed.header, 0x45);
assert_eq!(parsed.token_length, 4);
assert_eq!(parsed.options, vec![0x11, 0x22, 0x33]);
assert_eq!(parsed.payload, vec![0xAA, 0xBB, 0xCC]);
}
#[test]
fn test_empty_marker_fields() {
let msg = CoapMessage {
header: 0x40,
token_length: 0,
options: vec![],
payload: vec![],
};
let bytes = msg.to_be_bytes();
assert_eq!(bytes.len(), 4);
assert_eq!(bytes[0], 0x40); assert_eq!(bytes[1], 0); assert_eq!(bytes[2], 0xFF); assert_eq!(bytes[3], 0xFF);
let (parsed, consumed) = CoapMessage::try_from_be_bytes(&bytes).unwrap();
assert_eq!(consumed, 4);
assert_eq!(parsed.header, 0x40);
assert_eq!(parsed.token_length, 0);
assert_eq!(parsed.options, Vec::<u8>::new());
assert_eq!(parsed.payload, Vec::<u8>::new());
}
#[test]
fn test_after_marker_without_marker() {
let bytes = vec![0x40, 0x00];
let result = CoapMessage::try_from_be_bytes(&bytes);
match result {
Ok((msg, consumed)) => {
assert_eq!(consumed, 2);
assert_eq!(msg.header, 0x40);
assert_eq!(msg.token_length, 0);
assert_eq!(msg.options, Vec::<u8>::new());
assert_eq!(msg.payload, Vec::<u8>::new());
}
Err(_) => {
}
}
}