Skip to content

Commit

Permalink
Fix decoding of multiple packets
Browse files Browse the repository at this point in the history
The remainder returned by decode_packet is empty, but shouldn't in case
there are multiple packets in a single message.
  • Loading branch information
klingtnet committed Jun 10, 2023
1 parent 795fd10 commit 84311d1
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions tests/decode_encode_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const GOLDEN_MESSAGE_WO_ARGS: &str = "2f736f6d652f6164647200002c000000";
const GOLDEN_MESSAGE_WITH_ALL_TYPES: &str = "2f616e6f746865722f616464726573732f3100002c69686664737362746346544e496d725b695b64645d735d0000000000000004000000000000002a40490fda400921fb54442eea54686973206973206120737472696e672e00000054686973206973206120737472696e6720746f6f2e00000000000003010203000000007b000001c80000006304292a81ffc02a0d0000002a3ff3ae147ae147ae4009ae147ae147ae59617900";
const GOLDEN_EMPTY_BUNDLE: &str = "2362756e646c65000000000400000002";
const GOLDEN_BUNDLE: &str = "2362756e646c6500000004d2000010e10000000c2f766965772f31002c000000000000202f6d697865722f6368616e6e656c2f312f616d70000000002c6600003f666666000000442362756e646c65000000162e0000223d000000142f6f73632f312f66726571002c690000000001b8000000182f6f73632f312f7068617365000000002c660000becccccd";
const GOLDEN_MULTI_PACKET: &str = "000000a02362756e646c6500000004d2000010e10000000c2f766965772f31002c000000000000202f6d697865722f6368616e6e656c2f312f616d70000000002c6600003f666666000000442362756e646c65000000162e0000223d000000142f6f73632f312f66726571002c690000000001b8000000182f6f73632f312f7068617365000000002c660000becccccd000000102f736f6d652f6164647200002c000000";
const GOLDEN_MULTI_PACKET: &str = "000000102f736f6d652f6164647200002c0000000000008c2362756e646c6500000004d2000010e10000000c2f766965772f31002c000000000000202f6d697865722f6368616e6e656c2f312f616d70000000002c6600003f666666000000442362756e646c65000000162e0000223d000000142f6f73632f312f66726571002c690000000001b8000000182f6f73632f312f7068617365000000002c660000becccccd";

fn prefix_with_size(size: u32, golden: &str) -> String {
[hex::encode(size.to_be_bytes()), golden.into()].concat()
Expand Down Expand Up @@ -164,6 +164,7 @@ fn test_bundle() {
],
});

// Datagram encoding and decoding.
let bytes = encoder::encode(&packet).expect("encode failed");
assert_eq!(hex::decode(GOLDEN_BUNDLE).unwrap(), bytes);

Expand All @@ -182,36 +183,38 @@ fn test_bundle() {

#[test]
fn test_multi_packet_message() {
let packets = vec![OscPacket::Bundle(OscBundle {
timetag: (1234, 4321).into(),
content: vec![
OscPacket::Message(OscMessage {
addr: "/view/1".to_string(),
args: vec![],
}),
OscPacket::Message(OscMessage {
addr: "/mixer/channel/1/amp".to_string(),
args: vec![0.9f32.into()],
}),
OscPacket::Bundle(OscBundle {
timetag: (5678, 8765).into(),
content: vec![
OscPacket::Message(OscMessage {
addr: "/osc/1/freq".to_string(),
args: vec![440i32.into()],
}),
OscPacket::Message(OscMessage {
addr: "/osc/1/phase".to_string(),
args: vec![(-0.4f32).into()],
}),
],
}),
OscPacket::Message(OscMessage {
addr: "/some/addr".to_string(),
args: vec![],
}),
],
})];
let packets = vec![
OscPacket::Message(OscMessage {
addr: "/some/addr".to_string(),
args: vec![],
}),
OscPacket::Bundle(OscBundle {
timetag: (1234, 4321).into(),
content: vec![
OscPacket::Message(OscMessage {
addr: "/view/1".to_string(),
args: vec![],
}),
OscPacket::Message(OscMessage {
addr: "/mixer/channel/1/amp".to_string(),
args: vec![0.9f32.into()],
}),
OscPacket::Bundle(OscBundle {
timetag: (5678, 8765).into(),
content: vec![
OscPacket::Message(OscMessage {
addr: "/osc/1/freq".to_string(),
args: vec![440i32.into()],
}),
OscPacket::Message(OscMessage {
addr: "/osc/1/phase".to_string(),
args: vec![(-0.4f32).into()],
}),
],
}),
],
}),
];

// Stream encoding and decoding.
let bytes = encoder::encode_tcp(&packets).expect("stream encode failed");
Expand Down

0 comments on commit 84311d1

Please sign in to comment.