rework bundle decoding and tests

This commit is contained in:
Joel Wetzell
2026-08-30 09:00:38 -05:00
parent 6012e98efd
commit d3055cb9a5
2 changed files with 44 additions and 42 deletions
+2 -18
View File
@@ -45,24 +45,13 @@ func BundleFromBytes(bytes []byte) (*OSCBundle, []byte, error) {
return nil, bytesAfterBundleHeader, errors.New("OSC Bundle must start with #bundle string") return nil, bytesAfterBundleHeader, errors.New("OSC Bundle must start with #bundle string")
} }
timeTag, bytesAfterTimeTag, err := readOSCTimeTag(bytesAfterBundleHeader) timeTag, bytesAfterTimeTag, _ := readOSCTimeTag(bytesAfterBundleHeader)
if err != nil {
return nil, bytesAfterBundleHeader, err
}
bundleContents := []OSCPacket{} bundleContents := []OSCPacket{}
endOfBundle := false
remainingBytes := bytesAfterTimeTag remainingBytes := bytesAfterTimeTag
for !endOfBundle { contentSize, bytesAfterContentSize, _ := readOSCInt32(remainingBytes)
contentSize, bytesAfterContentSize, err := readOSCInt32(remainingBytes)
if err != nil {
return nil, remainingBytes, err
}
remainingBytes = bytesAfterContentSize remainingBytes = bytesAfterContentSize
@@ -93,11 +82,6 @@ func BundleFromBytes(bytes []byte) (*OSCBundle, []byte, error) {
return nil, remainingBytes, errors.New("bundle contents does not look a bundle or message") return nil, remainingBytes, errors.New("bundle contents does not look a bundle or message")
} }
remainingBytes = bytesAfterContentSize[contentSize:] remainingBytes = bytesAfterContentSize[contentSize:]
if len(remainingBytes) == 0 {
endOfBundle = true
}
}
return &OSCBundle{ return &OSCBundle{
TimeTag: timeTag, TimeTag: timeTag,
+18
View File
@@ -194,6 +194,24 @@ func TestBadOSCBundleDecoding(t *testing.T) {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
errorString: "bundle contents does not look a bundle or message", errorString: "bundle contents does not look a bundle or message",
}, },
{
name: "bundle with bad bundle inside",
bytes: []byte{35, 98, 117, 110, 100, 108, 101, 0, // #bundle
0, 0, 0, 32, 0, 0, 0, 0, // time tag
0, 0, 0, 19, // content size
35, 98, 117, 110, 100, 108, 101, 0, // #bundle
0, 0, 0, 64, 0, 0, 0, 0, // time tag
0, 0, 0},
errorString: "OSC Bundle has to be at least 20 bytes",
},
{
name: "bundle with bad message inside",
bytes: []byte{35, 98, 117, 110, 100, 108, 101, 0, // #bundle
0, 0, 0, 32, 0, 0, 0, 0, // time tag
0, 0, 0, 5, // content size
47, 104, 101, 108, 108, 111},
errorString: "OSC string must be null-terminated",
},
} }
for _, testCase := range testCases { for _, testCase := range testCases {