From 9452268042e0a6487752cbb1a04772ed50849f88 Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Mon, 13 Apr 2026 19:11:11 -0500 Subject: [PATCH] add layout for testing OSCBundle error cases --- bundle.go | 6 ++-- bundle_test.go | 79 ++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/bundle.go b/bundle.go index 022d8e7..da1c644 100644 --- a/bundle.go +++ b/bundle.go @@ -34,11 +34,11 @@ func (b *OSCBundle) ToBytes() ([]byte, error) { func BundleFromBytes(bytes []byte) (*OSCBundle, []byte, error) { if len(bytes) < 20 { - return nil, bytes, errors.New("bundle has to be at least 20 bytes") + return nil, bytes, errors.New("OSC Bundle has to be at least 20 bytes") } if bytes[0] != 35 { - return nil, bytes, errors.New("bundle must start with a #") + return nil, bytes, errors.New("OSC Bundle must start with a #") } bundleHeader, bytesAfterBundleHeader, err := readOSCString(bytes) @@ -48,7 +48,7 @@ func BundleFromBytes(bytes []byte) (*OSCBundle, []byte, error) { } if bundleHeader != "#bundle" { - return nil, bytesAfterBundleHeader, errors.New("bundle must start with #bundle string") + return nil, bytesAfterBundleHeader, errors.New("OSC Bundle must start with #bundle string") } timeTag, bytesAfterTimeTag, err := readOSCTimeTag(bytesAfterBundleHeader) diff --git a/bundle_test.go b/bundle_test.go index cef3236..10f8a40 100644 --- a/bundle_test.go +++ b/bundle_test.go @@ -5,7 +5,7 @@ import ( "testing" ) -func TestOSCBundleEncoding(t *testing.T) { +func TestGoodOSCBundleEncoding(t *testing.T) { testCases := []struct { name string @@ -13,15 +13,15 @@ func TestOSCBundleEncoding(t *testing.T) { expected []byte }{ { - "simple contents single message", - &OSCBundle{ + name: "simple contents single message", + bundle: &OSCBundle{ TimeTag: OSCTimeTag{ seconds: 32, fractionalSeconds: 0, }, Contents: []OSCPacket{&OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: float32(440)}}}}, }, - []byte{35, 98, 117, 110, 100, 108, 101, 0, 0, 0, 0, + expected: []byte{35, 98, 117, 110, 100, 108, 101, 0, 0, 0, 0, 32, 0, 0, 0, 0, 0, 0, 0, 32, 47, 111, 115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52, 47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0, @@ -45,7 +45,31 @@ func TestOSCBundleEncoding(t *testing.T) { } } -func TestOSCBundleDecoding(t *testing.T) { +func TestBadOSCBundleEncoding(t *testing.T) { + + testCases := []struct { + name string + bundle *OSCBundle + errorString string + }{} + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + + got, err := testCase.bundle.ToBytes() + + if err == nil { + t.Fatalf("OSCBundle.ToBytes() expected to fail but got: %+v", got) + } + + if err.Error() != testCase.errorString { + t.Fatalf("OSCBundle.ToBytes() got error '%s', expected '%s'", err.Error(), testCase.errorString) + } + }) + } +} + +func TestGoodOSCBundleDecoding(t *testing.T) { testCases := []struct { name string expected *OSCBundle @@ -86,3 +110,48 @@ func TestOSCBundleDecoding(t *testing.T) { }) } } + +func TestBadOSCBundleDecoding(t *testing.T) { + testCases := []struct { + name string + bytes []byte + errorString string + }{ + { + name: "empty byte array", + bytes: []byte{}, + errorString: "OSC Bundle has to be at least 20 bytes", + }, + { + name: "does not start with #", + bytes: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + errorString: "OSC Bundle must start with a #", + }, + { + name: "does not start with #bundle", + bytes: []byte{35, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + errorString: "OSC Bundle must start with #bundle string", + }, + { + name: "bundle header not properly null terminated", + bytes: []byte{35, 98, 117, 110, 100, 108, 101, 35, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0}, + errorString: "OSC Bundle must start with #bundle string", + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + got, _, err := BundleFromBytes(testCase.bytes) + + if err == nil { + t.Fatalf("BundleFromBytes expected to fail but got: %+v", got) + } + + if err.Error() != testCase.errorString { + t.Fatalf("BundleFromBytes got error '%s', expected '%s'", err.Error(), testCase.errorString) + } + }) + } +}