From fdea7e65060633043a9edaee685c54b476ae5978 Mon Sep 17 00:00:00 2001 From: Markus Legner Date: Tue, 21 May 2024 14:31:29 +0200 Subject: [PATCH] test: add test for checksum of misaligned slice --- crates/scion-proto/src/packet/checksum.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/crates/scion-proto/src/packet/checksum.rs b/crates/scion-proto/src/packet/checksum.rs index a8d8e98..a24ece4 100644 --- a/crates/scion-proto/src/packet/checksum.rs +++ b/crates/scion-proto/src/packet/checksum.rs @@ -265,6 +265,24 @@ mod tests { assert_eq!(checksum, !0xddf2); } + #[test] + fn rfc1071_example_slice_unaligned() { + // Construct a slice that is not 2B aligned. + let mut data = b"\0\0\x01\xf2\x03\xf4\xf5\xf6\xf7".to_vec(); + let slice = if data.as_ptr().align_offset(2) == 0 { + &data[1..] + } else { + data.rotate_left(1); + &data[..data.len() - 1] + }; + + assert_eq!(slice.as_ptr().align_offset(2), 1); + assert_eq!( + ChecksumDigest::default().add_slice(slice).checksum(), + !0xddf2 + ); + } + macro_rules! test_checksum { ( name: $name:ident,