-
Notifications
You must be signed in to change notification settings - Fork 2
src: codec: Fix SKIP_CRC_VALIDATION panic and hygiene #8
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
Open
joaoantoniocardoso
wants to merge
6
commits into
bluerobotics:master
Choose a base branch
from
joaoantoniocardoso:split/codec-skip-crc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5e9529d
src: codec: Cleanup commented buf.reserve
joaoantoniocardoso fe0cfc1
src: Fix panic in codec when using SKIP_CRC_VALIDATION
joaoantoniocardoso 7a943a7
tests: Add SKIP_CRC_VALIDATION tests
joaoantoniocardoso bd81149
src: Add docstring to MavlinkCodec
joaoantoniocardoso 6d449b2
codec: Use the correct packet version STX, although they are the same
joaoantoniocardoso 2a53019
src: Fix obfuscated_if_else (clippy)
joaoantoniocardoso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| /target | ||
| /dhat-heap.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| use bytes::BytesMut; | ||
| use dev_utils::{create_random_v1_raw_message, create_random_v2_raw_message}; | ||
| use mavlink_codec::{codec::MavlinkCodec, error::DecoderError, Packet}; | ||
| use rand::{rngs::StdRng, SeedableRng}; | ||
| use tokio_util::codec::Decoder; | ||
|
|
||
| const SEED: u64 = 42; | ||
|
|
||
| type SkipV1Codec = MavlinkCodec<true, false, false, false, true, false>; | ||
| type SkipV2Codec = MavlinkCodec<false, true, false, false, true, false>; | ||
| type StrictV1Codec = MavlinkCodec<true, false, false, false, false, false>; | ||
| type StrictV2Codec = MavlinkCodec<false, true, false, false, false, false>; | ||
|
|
||
| fn corrupt_crc(buf: &mut [u8]) { | ||
| let len = buf.len(); | ||
| assert!(len >= 2, "packet must have at least two bytes to corrupt"); | ||
| buf[len - 2] = buf[len - 2].wrapping_add(1); | ||
| buf[len - 1] = buf[len - 1].wrapping_add(1); | ||
| } | ||
|
|
||
| #[test] | ||
| fn skip_crc_v1_decodes_valid_packet() { | ||
| let mut rng: StdRng = SeedableRng::seed_from_u64(SEED); | ||
| let raw = create_random_v1_raw_message(&mut rng); | ||
| let total = raw.raw_bytes().len(); | ||
| let mut buf = BytesMut::from(raw.raw_bytes()); | ||
|
|
||
| let mut codec = SkipV1Codec::default(); | ||
| let decoded = codec.decode(&mut buf).unwrap(); | ||
|
|
||
| assert!( | ||
| matches!(decoded, Some(Ok(Packet::V1(_)))), | ||
| "expected Packet::V1, got {decoded:?}" | ||
| ); | ||
| assert!( | ||
| buf.is_empty(), | ||
| "F3: {} of {total} bytes remained after a single successful decode", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is F3 ? |
||
| buf.len() | ||
| ); | ||
| assert!( | ||
| codec.decode(&mut buf).unwrap().is_none(), | ||
| "F3: decoder emitted a ghost packet from bytes that should already have been consumed" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn skip_crc_v2_decodes_valid_packet() { | ||
| let mut rng: StdRng = SeedableRng::seed_from_u64(SEED); | ||
| let raw = create_random_v2_raw_message(&mut rng); | ||
| let total = raw.raw_bytes().len(); | ||
| let mut buf = BytesMut::from(raw.raw_bytes()); | ||
|
|
||
| let mut codec = SkipV2Codec::default(); | ||
| let decoded = codec.decode(&mut buf).unwrap(); | ||
|
|
||
| assert!( | ||
| matches!(decoded, Some(Ok(Packet::V2(_)))), | ||
| "expected Packet::V2, got {decoded:?}" | ||
| ); | ||
| assert!( | ||
| buf.is_empty(), | ||
| "F3: {} of {total} bytes remained after a single successful decode", | ||
| buf.len() | ||
| ); | ||
| assert!( | ||
| codec.decode(&mut buf).unwrap().is_none(), | ||
| "F3: decoder emitted a ghost packet from bytes that should already have been consumed" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn skip_crc_v1_accepts_corrupted_crc() { | ||
| let mut rng: StdRng = SeedableRng::seed_from_u64(SEED); | ||
| let raw = create_random_v1_raw_message(&mut rng); | ||
|
|
||
| let mut corrupted: Vec<u8> = raw.raw_bytes().to_vec(); | ||
| corrupt_crc(&mut corrupted); | ||
|
|
||
| // Skip codec must accept despite the broken CRC. | ||
| { | ||
| let mut buf = BytesMut::from(corrupted.as_slice()); | ||
| let mut codec = SkipV1Codec::default(); | ||
| let decoded = codec.decode(&mut buf).unwrap(); | ||
| assert!( | ||
| matches!(decoded, Some(Ok(Packet::V1(_)))), | ||
| "skip-CRC codec must accept a packet with corrupted CRC, got {decoded:?}" | ||
| ); | ||
| assert!( | ||
| buf.is_empty(), | ||
| "F3: {} bytes remained after decoding a corrupted-CRC packet under SKIP_CRC_VALIDATION", | ||
| buf.len() | ||
| ); | ||
| } | ||
|
|
||
| // Non-skip codec must reject with InvalidCRC, proving the toggle's scope. | ||
| { | ||
| let mut buf = BytesMut::from(corrupted.as_slice()); | ||
| let mut codec = StrictV1Codec::default(); | ||
| let decoded = codec.decode(&mut buf).unwrap(); | ||
| assert!( | ||
| matches!(decoded, Some(Err(DecoderError::InvalidCRC { .. }))), | ||
| "strict codec must reject corrupted CRC, got {decoded:?}" | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn skip_crc_v2_accepts_corrupted_crc() { | ||
| let mut rng: StdRng = SeedableRng::seed_from_u64(SEED); | ||
| let raw = create_random_v2_raw_message(&mut rng); | ||
|
|
||
| let mut corrupted: Vec<u8> = raw.raw_bytes().to_vec(); | ||
| corrupt_crc(&mut corrupted); | ||
|
|
||
| { | ||
| let mut buf = BytesMut::from(corrupted.as_slice()); | ||
| let mut codec = SkipV2Codec::default(); | ||
| let decoded = codec.decode(&mut buf).unwrap(); | ||
| assert!( | ||
| matches!(decoded, Some(Ok(Packet::V2(_)))), | ||
| "skip-CRC codec must accept a packet with corrupted CRC, got {decoded:?}" | ||
| ); | ||
| assert!( | ||
| buf.is_empty(), | ||
| "F3: {} bytes remained after decoding a corrupted-CRC packet under SKIP_CRC_VALIDATION", | ||
| buf.len() | ||
| ); | ||
| } | ||
|
|
||
| { | ||
| let mut buf = BytesMut::from(corrupted.as_slice()); | ||
| let mut codec = StrictV2Codec::default(); | ||
| let decoded = codec.decode(&mut buf).unwrap(); | ||
| assert!( | ||
| matches!(decoded, Some(Err(DecoderError::InvalidCRC { .. }))), | ||
| "strict codec must reject corrupted CRC, got {decoded:?}" | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for V2, you need to check if buf[2] has first bit on.
If that's the case, the crc will be in
buf[len - 13 - 2]andbuf[len - 13 - 1]