From 2522b67f770b277d8d0cbea35205b7c660389526 Mon Sep 17 00:00:00 2001 From: Ben Leadbetter Date: Sat, 18 Jan 2025 16:31:58 +0100 Subject: [PATCH] fix: packet from long slice buffers --- src/channel_voice1/packet.rs | 46 +++++++++++++++++++++++++++++++++- src/channel_voice2/packet.rs | 48 ++++++++++++++++++++++++++++++++++-- src/flex_data/packet.rs | 13 +++++++++- src/packet.rs | 10 ++++++++ src/sysex7/packet.rs | 11 ++++++++- src/sysex8/packet.rs | 11 ++++++++- src/system_common/packet.rs | 5 ++++ src/ump_stream/packet.rs | 13 +++++++++- src/utility/packet.rs | 5 ++++ 9 files changed, 155 insertions(+), 7 deletions(-) diff --git a/src/channel_voice1/packet.rs b/src/channel_voice1/packet.rs index 3c066dc..af7567b 100644 --- a/src/channel_voice1/packet.rs +++ b/src/channel_voice1/packet.rs @@ -33,7 +33,7 @@ impl<'a> core::convert::TryFrom<&'a [u32]> for Packet { Ok(Packet({ let mut buffer = [0x0; 1]; - buffer[..data.len()].copy_from_slice(data); + buffer[0] = data[0]; buffer })) } @@ -84,6 +84,16 @@ mod tests { assert!(Packet::try_from(&[0x2000_0000][..]).is_ok()); } + #[test] + fn construction_long_slice() { + assert!(Packet::try_from(&[0x2000_0000, 0x0, 0x0, 0x0][..]).is_ok()); + } + + #[test] + fn construction_very_long_slice() { + assert!(Packet::try_from(&[0x2000_0000, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0][..]).is_ok()); + } + #[test] fn construction_incorrect_ump_message_type() { assert_eq!( @@ -94,6 +104,40 @@ mod tests { ); } + #[test] + fn channel() { + use crate::Channeled; + assert_eq!( + Packet::try_from(&[0x2008_0000][..]).unwrap().channel(), + ux::u4::new(0x8) + ); + } + + #[test] + fn set_channel() { + use crate::Channeled; + let mut packet = Packet::try_from(&[0x2000_0000][..]).unwrap(); + packet.set_channel(ux::u4::new(0x8)); + assert_eq!(&*packet, &[0x2008_0000][..]); + } + + #[test] + fn group() { + use crate::Grouped; + assert_eq!( + Packet::try_from(&[0x2A00_0000][..]).unwrap().group(), + ux::u4::new(0xA) + ); + } + + #[test] + fn set_group() { + use crate::Grouped; + let mut packet = Packet::try_from(&[0x2000_0000][..]).unwrap(); + packet.set_group(ux::u4::new(0xA)); + assert_eq!(&*packet, &[0x2A00_0000][..]); + } + #[test] fn construction_short_slice() { assert_eq!( diff --git a/src/channel_voice2/packet.rs b/src/channel_voice2/packet.rs index 01bb43e..9f4774b 100644 --- a/src/channel_voice2/packet.rs +++ b/src/channel_voice2/packet.rs @@ -33,7 +33,8 @@ impl<'a> core::convert::TryFrom<&'a [u32]> for Packet { Ok(Packet({ let mut buffer = [0x0; 2]; - buffer[..data.len()].copy_from_slice(data); + let sz = 2.min(data.len()); + buffer[..sz].copy_from_slice(&data[..sz]); buffer })) } @@ -77,13 +78,22 @@ impl crate::Channeled<[u32; 2]> for Packet { #[cfg(test)] mod tests { use super::*; - use pretty_assertions::assert_eq; #[test] fn construction() { assert!(Packet::try_from(&[0x4000_0000][..]).is_ok()); } + #[test] + fn construction_slice_size2() { + assert!(Packet::try_from(&[0x4000_0000, 0x0][..]).is_ok()); + } + + #[test] + fn construction_slice_size4() { + assert!(Packet::try_from(&[0x4000_0000, 0x0, 0x0, 0x0][..]).is_ok()); + } + #[test] fn construction_incorrect_ump_message_type() { assert_eq!( @@ -103,4 +113,38 @@ mod tests { )), ); } + + #[test] + fn channel() { + use crate::Channeled; + assert_eq!( + Packet::try_from(&[0x4008_0000][..]).unwrap().channel(), + ux::u4::new(0x8) + ); + } + + #[test] + fn set_channel() { + use crate::Channeled; + let mut packet = Packet::try_from(&[0x4000_0000][..]).unwrap(); + packet.set_channel(ux::u4::new(0x8)); + assert_eq!(&*packet, &[0x4008_0000, 0x0][..]); + } + + #[test] + fn group() { + use crate::Grouped; + assert_eq!( + Packet::try_from(&[0x4A00_0000][..]).unwrap().group(), + ux::u4::new(0xA) + ); + } + + #[test] + fn set_group() { + use crate::Grouped; + let mut packet = Packet::try_from(&[0x4000_0000][..]).unwrap(); + packet.set_group(ux::u4::new(0xA)); + assert_eq!(&*packet, &[0x4A00_0000, 0x0][..]); + } } diff --git a/src/flex_data/packet.rs b/src/flex_data/packet.rs index 1fc3739..74dffda 100644 --- a/src/flex_data/packet.rs +++ b/src/flex_data/packet.rs @@ -31,7 +31,8 @@ impl<'a> core::convert::TryFrom<&'a [u32]> for Packet { Ok(Packet({ let mut buffer = [0x0; 4]; - buffer[..data.len()].copy_from_slice(data); + let sz = 4.min(data.len()); + buffer[..sz].copy_from_slice(&data[..sz]); buffer })) } @@ -94,6 +95,16 @@ mod tests { assert!(Packet::try_from(&[0xD000_0000, 0x0000_0000][..]).is_ok()) } + #[test] + fn construction_long_slice() { + assert!(Packet::try_from(&[0xD000_0000, 0x0000_0000, 0x0, 0x0][..]).is_ok()) + } + + #[test] + fn construction_very_long_slice() { + assert!(Packet::try_from(&[0xD000_0000, 0x0000_0000, 0x0, 0x0, 0x0][..]).is_ok()) + } + #[test] fn construction_short_slice() { assert_eq!( diff --git a/src/packet.rs b/src/packet.rs index 34fad11..4d76b55 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -107,6 +107,16 @@ mod tests { assert_eq!(Packet::try_from(&data[..]).unwrap().deref(), &[0x0]); } + #[test] + #[cfg(feature = "sysex8")] + fn construction_from_long_slice() { + let data = [0x5001_0000, 0x0, 0x0, 0x0, 0x0]; + assert_eq!( + &*Packet::try_from(&data[..]).unwrap(), + &[0x5001_0000, 0x0, 0x0, 0x0] + ); + } + #[test] fn construction_from_empty_data() { let data = []; diff --git a/src/sysex7/packet.rs b/src/sysex7/packet.rs index 1f7818e..77ae3de 100644 --- a/src/sysex7/packet.rs +++ b/src/sysex7/packet.rs @@ -35,7 +35,8 @@ impl<'a> core::convert::TryFrom<&'a [u32]> for Packet { Ok(Packet({ let mut buffer = [0x0; 2]; - buffer[..data.len()].copy_from_slice(data); + let sz = 2.min(data.len()); + buffer[..sz].copy_from_slice(&data[..sz]); buffer })) } @@ -100,6 +101,14 @@ mod tests { ); } + #[test] + fn construction_long_slice() { + assert_eq!( + &*Packet::try_from(&[0x3000_0000, 0x0, 0x0, 0x0][..]).unwrap(), + &[0x3000_0000, 0x0000_0000][..], + ); + } + #[test] fn construction_short_slice() { assert_eq!( diff --git a/src/sysex8/packet.rs b/src/sysex8/packet.rs index 24f9783..cc997a3 100644 --- a/src/sysex8/packet.rs +++ b/src/sysex8/packet.rs @@ -35,7 +35,8 @@ impl<'a> core::convert::TryFrom<&'a [u32]> for Packet { Ok(Packet({ let mut buffer = [0x0; 4]; - buffer[..data.len()].copy_from_slice(data); + let sz = 4.min(data.len()); + buffer[..sz].copy_from_slice(&data[..sz]); buffer })) } @@ -121,6 +122,14 @@ mod tests { ); } + #[test] + fn construction_long_slice() { + assert!( + Packet::try_from(&[0x5001_0000, 0x0000_0000, 0x0000_0000, 0x0000_0000, 0x0][..]) + .is_ok() + ); + } + #[test] fn construction_short_slice() { assert_eq!( diff --git a/src/system_common/packet.rs b/src/system_common/packet.rs index f4a6f42..e700aec 100644 --- a/src/system_common/packet.rs +++ b/src/system_common/packet.rs @@ -70,6 +70,11 @@ mod tests { assert!(Packet::try_from(&[0x1000_0000][..]).is_ok()); } + #[test] + fn construction_long_slice() { + assert!(Packet::try_from(&[0x1000_0000, 0x0][..]).is_ok()); + } + #[test] fn construction_incorrect_ump_message_type() { assert_eq!( diff --git a/src/ump_stream/packet.rs b/src/ump_stream/packet.rs index 8859ebd..73ccdcd 100644 --- a/src/ump_stream/packet.rs +++ b/src/ump_stream/packet.rs @@ -33,7 +33,8 @@ impl<'a> core::convert::TryFrom<&'a [u32]> for Packet { Ok(Packet({ let mut buffer = [0x0; 4]; - buffer[..data.len()].copy_from_slice(data); + let sz = 4.min(data.len()); + buffer[..sz].copy_from_slice(&data[..sz]); buffer })) } @@ -82,6 +83,16 @@ mod tests { assert!(Packet::try_from(&[0xF000_0000][..]).is_ok()) } + #[test] + fn construction_long_slice() { + assert!(Packet::try_from(&[0xF000_0000, 0x0, 0x0, 0x0][..]).is_ok()) + } + + #[test] + fn construction_very_long_slice() { + assert!(Packet::try_from(&[0xF000_0000, 0x0, 0x0, 0x0, 0x0][..]).is_ok()) + } + #[test] fn construction_short_slice() { assert_eq!( diff --git a/src/utility/packet.rs b/src/utility/packet.rs index 422ec5d..475c91c 100644 --- a/src/utility/packet.rs +++ b/src/utility/packet.rs @@ -56,6 +56,11 @@ mod tests { assert!(Packet::try_from(&[0x0000_0000][..]).is_ok()); } + #[test] + fn construction_long_slice() { + assert!(Packet::try_from(&[0x0000_0000, 0x0][..]).is_ok()); + } + #[test] fn construction_incorrect_ump_message_type() { assert_eq!(