Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix validate_packet_checksum #20

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion bestool/src/beslink/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub fn read_message(serial_port: &mut Box<dyn SerialPort>) -> Result<BesMessage,
}
}
pub fn validate_packet_checksum(packet: &[u8]) -> Result<(), BESLinkError> {
let checksum = calculate_message_checksum(&packet[1..packet.len()]);
let checksum = calculate_message_checksum(&packet[0..packet.len()-1]);
if checksum == packet[packet.len() - 1] {
return Ok(());
}
Expand All @@ -193,6 +193,7 @@ pub fn calculate_message_checksum(packet: &[u8]) -> u8 {
#[cfg(test)]
mod tests {
use crate::beslink::message::calculate_message_checksum;
use crate::beslink::message::validate_packet_checksum;
use crate::beslink::{BesMessage, MessageTypes, BES_SYNC};

#[test]
Expand Down Expand Up @@ -246,4 +247,35 @@ mod tests {
assert_eq!(old_checksum, new_checksum);
}
}
#[test]
fn test_validate_packet_checksum() {
let test_messages: Vec<Vec<u8>> = vec![
vec![0xBE, 0x50, 0x00, 0x03, 0x00, 0x00, 0x01, 0xED],
vec![0xBE, 0x50, 0x00, 0x01, 0x01, 0xEF],
vec![0xBE, 0x53, 0x00, 0x01, 0x00, 0xED],
vec![0xBE, 0x65, 0x02, 0x01, 0x11, 0xC8],
vec![0xBE, 0x65, 0x03, 0x01, 0x12, 0xC6],
vec![
0xBE, 0x62, 0xC1, 0x0B, 0x00, 0x80, 0x00, 0x00, 0xAB, 0x77, 0x7F, 0xF4, 0x00, 0x00,
0x00, 0xFE,
],
vec![
0xBE, 0x62, 0xC2, 0x0B, 0x00, 0x80, 0x00, 0x00, 0x34, 0x90, 0x61, 0xF9, 0x01, 0x00,
0x00, 0x73,
],
vec![
0xBE, 0x61, 0x07, 0x0C, 0x00, 0x00, 0x00, 0x3C, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x80,
0x00, 0x00, 0x04,
],
vec![
0xBE, 0x03, 0x06, 0x08, 0x00, 0xF0, 0x0F, 0x3C, 0x00, 0x10, 0x00, 0x00, 0xE5,
],
vec![
0xBE, 0x03, 0x05, 0x08, 0x00, 0xE0, 0x0F, 0x3C, 0x00, 0x10, 0x00, 0x00, 0xF6,
],
];
for v in test_messages {
assert!(validate_packet_checksum(&v).is_ok())
}
}
}