Skip to content

Commit

Permalink
fix: packet from long slice buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Leadbetter committed Jan 18, 2025
1 parent 7c5419c commit 2522b67
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 7 deletions.
46 changes: 45 additions & 1 deletion src/channel_voice1/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}))
}
Expand Down Expand Up @@ -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!(
Expand All @@ -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!(
Expand Down
48 changes: 46 additions & 2 deletions src/channel_voice2/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}))
}
Expand Down Expand Up @@ -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!(
Expand All @@ -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][..]);
}
}
13 changes: 12 additions & 1 deletion src/flex_data/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}))
}
Expand Down Expand Up @@ -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!(
Expand Down
10 changes: 10 additions & 0 deletions src/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
11 changes: 10 additions & 1 deletion src/sysex7/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}))
}
Expand Down Expand Up @@ -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!(
Expand Down
11 changes: 10 additions & 1 deletion src/sysex8/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}))
}
Expand Down Expand Up @@ -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!(
Expand Down
5 changes: 5 additions & 0 deletions src/system_common/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
13 changes: 12 additions & 1 deletion src/ump_stream/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}))
}
Expand Down Expand Up @@ -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!(
Expand Down
5 changes: 5 additions & 0 deletions src/utility/packet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down

0 comments on commit 2522b67

Please sign in to comment.