package osc import ( "math" "reflect" "testing" ) func TestGoodOSCMessageEncoding(t *testing.T) { testCases := []struct { name string message *OSCMessage expected []byte }{ { name: "simple hello", message: &OSCMessage{ Address: "/hello", Args: []OSCArg{}, }, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 0, 0, 0}, }, { name: "simple address string arg", message: &OSCMessage{ Address: "/hello", Args: []OSCArg{ { Type: "s", Value: "arg1", }, }, }, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 115, 0, 0, 97, 114, 103, 49, 0, 0, 0, 0}, }, { name: "simple address integer arg", message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "i", Value: 35}}}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35}, }, { name: "simple address float arg", message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "f", Value: 34.5}}}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 102, 0, 0, 66, 10, 0, 0}, }, { name: "simple address blob arg", message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "b", Value: []byte{98, 108, 111, 98}}}}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, 4, 98, 108, 111, 98}, }, { name: "simple address True arg", message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "T", Value: true}}}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 84, 0, 0}, }, { name: "simple address False arg", message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "F", Value: false}}}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0}, }, { name: "simple address color arg", message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "r", Value: OSCColor{r: 20, g: 21, b: 22, a: 10}}}}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10}, }, { name: "simple address nil arg", message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "N", Value: nil}}}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0}, }, { name: "simple address int64 arg", message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "h", Value: 281474976710655}}}, expected: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255}, }, { name: "simple address float64 arg", message: &OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "d", Value: 12.7654763}}}, expected: []byte{ 47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6, }, }, // TODO(jwetzell): get array args working working // { // name: "simple address array arg", // message: OSCMessage{ // Address: "/hello", // Args: []OSCArg{ // []OSCArg{ // {Type: "d", Value: 12.7654763}, // {Type: "i", Value: 1000}, // }, // }, // }, // expected: []byte{ // 47, 104, 101, 108, 108, 111, 0, 0, 44, 91, 100, 105, 93, 0, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6, // 0, 0, 3, 232, // }, // }, { name: "osc 1.0 spec example 1", message: &OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: 440}}}, expected: []byte{ 47, 111, 115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52, 47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0, 44, 102, 0, 0, 67, 220, 0, 0, }, }, { name: "osc 1.0 spec example 2", message: &OSCMessage{ Address: "/foo", Args: []OSCArg{ {Type: "i", Value: 1000}, {Type: "i", Value: -1}, {Type: "s", Value: "hello"}, // thanks IEEE 754 {Type: "f", Value: 1.2339999675750732421875}, {Type: "f", Value: 5.677999973297119140625}, }, }, expected: []byte{ 47, 102, 111, 111, 0, 0, 0, 0, 44, 105, 105, 115, 102, 102, 0, 0, 0, 0, 3, 232, 255, 255, 255, 255, 104, 101, 108, 108, 111, 0, 0, 0, 63, 157, 243, 182, 64, 181, 178, 45, }, }, } for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { got, err := testCase.message.ToBytes() if err != nil { t.Fatalf("failed to encode properly: %s", err.Error()) } if !reflect.DeepEqual(got, testCase.expected) { t.Fatalf("failed to encode properly got '%v', expected '%v'", got, testCase.expected) } }) } } func TestBadOSCMessageEncoding(t *testing.T) { testCases := []struct { name string message *OSCMessage errorString string }{ { name: "empty message", message: &OSCMessage{}, errorString: "OSC Message must have an address", }, { name: "address does not start with /", message: &OSCMessage{Address: "hello"}, errorString: "OSC Message address must start with /", }, { name: "arg with unsupported type", message: &OSCMessage{ Address: "/hello", Args: []OSCArg{{Type: "x", Value: "unsupported"}}, }, errorString: "unsupported OSC argument type: x", }, { name: "string arg that is not a string", message: &OSCMessage{ Address: "/hello", Args: []OSCArg{{Type: "s", Value: 123}}, }, errorString: "OSC arg had string type but non-string value", }, { name: "int32 arg that is not a number", message: &OSCMessage{ Address: "/hello", Args: []OSCArg{{Type: "i", Value: "not an int"}}, }, errorString: "OSC arg had int32 type but non-number value", }, { name: "float32 arg that is not a number", message: &OSCMessage{ Address: "/hello", Args: []OSCArg{{Type: "f", Value: "not a float"}}, }, errorString: "OSC arg had float32 type but non-number value", }, { name: "int64 arg that is not a number", message: &OSCMessage{ Address: "/hello", Args: []OSCArg{{Type: "h", Value: "not an int"}}, }, errorString: "OSC arg had int64 type but non-number value", }, { name: "float64 arg that is not a number", message: &OSCMessage{ Address: "/hello", Args: []OSCArg{{Type: "d", Value: "not a float"}}, }, errorString: "OSC arg had float64 type but non-number value", }, { name: "blob arg that is not a byte array", message: &OSCMessage{ Address: "/hello", Args: []OSCArg{{Type: "b", Value: "not a blob"}}, }, errorString: "OSC arg had blob type but non-blob value", }, { name: "color arg that is not an OSCColor", message: &OSCMessage{ Address: "/hello", Args: []OSCArg{{Type: "r", Value: "not a color"}}, }, errorString: "OSC arg had color type but non-color value", }, } for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { got, err := testCase.message.ToBytes() if err == nil { t.Fatalf("OSCMessage.ToBytes() expected to fail but got: %+v", got) } if err.Error() != testCase.errorString { t.Fatalf("OSCMessage.ToBytes() got error '%s', expected '%s'", err.Error(), testCase.errorString) } }) } } func TestGoodOSCMessageDecoding(t *testing.T) { testCases := []struct { name string bytes []byte expected OSCMessage }{ { name: "simple address no args", bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 0, 0, 0}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{}}, }, { name: "simple address string arg", bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 115, 0, 0, 97, 114, 103, 49, 0, 0, 0, 0}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "s", Value: "arg1"}}}, }, { name: "simple address integer arg", bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "i", Value: int32(35)}}}, }, { name: "simple address float arg", bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 102, 0, 0, 66, 10, 0, 0}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "f", Value: float32(34.5)}}}, }, { name: "simple address blob arg", bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, 4, 98, 108, 111, 98}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "b", Value: []byte{98, 108, 111, 98}}}}, }, { name: "simple address True arg", bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 84, 0, 0}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "T", Value: true}}}, }, { name: "simple address False arg", bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 70, 0, 0}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "F", Value: false}}}, }, { name: "simple address color arg", bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, 22, 10}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "r", Value: OSCColor{r: 20, g: 21, b: 22, a: 10}}}}, }, { name: "simple address nil arg", bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 78, 0, 0}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "N", Value: nil}}}, }, { name: "simple address Inifinitum arg", bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 73, 0, 0}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "I", Value: math.MaxInt32}}}, }, { name: "simple address int64 arg", bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255}, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "h", Value: int64(281474976710655)}}}, }, { name: "simple address float64 arg", bytes: []byte{ 47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6, }, expected: OSCMessage{Address: "/hello", Args: []OSCArg{{Type: "d", Value: float64(12.7654763)}}}, }, // TODO(jwetzell): support OSC array // { // name: "simple address array arg", // bytes: []byte{ // 47, 104, 101, 108, 108, 111, 0, 0, 44, 91, 100, 105, 93, 0, 0, 0, 0x40, 0x29, 0x87, 0xec, 0x82, 0x74, 0xb9, 0xe6, // 0, 0, 3, 232, // }, // expected: OSCMessage{ // Address: "/hello", // Args: []OSCArg{ // []OSCArg{ // {Type: "d", Value: 12.7654763}, // {Type: "i", Value: 1000}, // }, // }, // }, // }, { name: "simple address no type string", bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0}, expected: OSCMessage{ Address: "/hello", Args: []OSCArg{}, }, }, { name: "osc 1.0 spec example 1", bytes: []byte{ 47, 111, 115, 99, 105, 108, 108, 97, 116, 111, 114, 47, 52, 47, 102, 114, 101, 113, 117, 101, 110, 99, 121, 0, 44, 102, 0, 0, 67, 220, 0, 0, }, expected: OSCMessage{Address: "/oscillator/4/frequency", Args: []OSCArg{{Type: "f", Value: float32(440)}}}, }, { name: "osc 1.0 spec example 2", bytes: []byte{ 47, 102, 111, 111, 0, 0, 0, 0, 44, 105, 105, 115, 102, 102, 0, 0, 0, 0, 3, 232, 255, 255, 255, 255, 104, 101, 108, 108, 111, 0, 0, 0, 63, 157, 243, 182, 64, 181, 178, 45, }, expected: OSCMessage{ Address: "/foo", Args: []OSCArg{ {Type: "i", Value: int32(1000)}, {Type: "i", Value: int32(-1)}, {Type: "s", Value: "hello"}, // thanks IEEE 754 {Type: "f", Value: float32(1.2339999675750732421875)}, {Type: "f", Value: float32(5.677999973297119140625)}, }, }, }, } for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { actual, err := MessageFromBytes(testCase.bytes) if err != nil { t.Fatalf("failed to encode properly: %s", err.Error()) } if !reflect.DeepEqual(actual.Address, testCase.expected.Address) { t.Fatalf("failed to encode address propertly got '%s', expected '%s'", actual.Address, testCase.expected.Address) } if !reflect.DeepEqual(actual.Args, testCase.expected.Args) { t.Fatalf("failed to encode args properly got '%+v', expected '%+v'", actual.Args, testCase.expected.Args) } }) } } func TestBadOSCMessageDecoding(t *testing.T) { testCases := []struct { name string bytes []byte errorString string }{ { name: "empty byte array", bytes: []byte{}, errorString: "cannot create OSC Message from empty byte array", }, { name: "does not start with /", bytes: []byte{0, 104, 101, 108, 108, 111, 0, 0}, errorString: "OSC Message must start with /", }, { name: "address string not padded", bytes: []byte{47, 104, 101, 108, 108, 111, 0}, errorString: "OSC string is not properly padded", }, { name: "type string not padded", bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 0}, errorString: "OSC string is not properly padded", }, { name: "type string does not start with ,", bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 45, 0, 0, 0}, errorString: "type string is malformed", }, { name: "string arg not null-terminated", bytes: []byte{ 47, 104, 101, 108, 108, 111, 0, 0, 44, 115, 0, 0, 97, 114, 103, 49, }, errorString: "OSC string must be null-terminated", }, { name: "string arg not padded", bytes: []byte{ 47, 104, 101, 108, 108, 111, 0, 0, 44, 115, 0, 0, 104, 105, 0, }, errorString: "OSC string is not properly padded", }, { name: "int32 arg not 4 bytes", bytes: []byte{ 47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, }, errorString: "OSC int32 arg is not 4 bytes", }, { name: "int64 arg not 8 bytes", bytes: []byte{ 47, 104, 101, 108, 108, 111, 0, 0, 44, 104, 0, 0, 0, 0, 0, 0, }, errorString: "OSC int64 arg is not 8 bytes", }, { name: "float32 arg not 4 bytes", bytes: []byte{ 47, 104, 101, 108, 108, 111, 0, 0, 44, 102, 0, 0, 66, }, errorString: "OSC float32 arg is not 4 bytes", }, { name: "float64 arg not 8 bytes", bytes: []byte{ 47, 104, 101, 108, 108, 111, 0, 0, 44, 100, 0, 0, 0, }, errorString: "OSC float64 arg is not 8 bytes", }, { name: "blob arg size not valid", bytes: []byte{ 47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, }, errorString: "OSC blob arg size not valid: OSC int32 arg is not 4 bytes", }, { name: "blob arg size mismatch", bytes: []byte{ 47, 104, 101, 108, 108, 111, 0, 0, 44, 98, 0, 0, 0, 0, 0, 4, 98, 108, 111, }, errorString: "OSC blob arg size not valid: size specified is larger than remaining bytes", }, { name: "color arg not 4 bytes", bytes: []byte{ 47, 104, 101, 108, 108, 111, 0, 0, 44, 114, 0, 0, 20, 21, }, errorString: "OSC color arg is not 4 bytes", }, { name: "time tag arg seconds not complete", bytes: []byte{ 47, 104, 101, 108, 108, 111, 0, 0, 44, 116, 0, 0, 0, }, errorString: "OSC time tag seconds are not valid: OSC int32 arg is not 4 bytes", }, { name: "time tag arg fractional seconds not complete", bytes: []byte{ 47, 104, 101, 108, 108, 111, 0, 0, 44, 116, 0, 0, 0, 32, 0, 0, 0, }, errorString: "OSC time tag fractional seconds are not valid: OSC int32 arg is not 4 bytes", }, { name: "unknown arg type", bytes: []byte{ 47, 104, 101, 108, 108, 111, 0, 0, 44, 120, 0, 0, }, errorString: "unsupported OSC argument type: x", }, } for _, testCase := range testCases { t.Run(testCase.name, func(t *testing.T) { got, err := MessageFromBytes(testCase.bytes) if err == nil { t.Fatalf("MessageFromBytes expected to fail but got: %+v", got) } if err.Error() != testCase.errorString { t.Fatalf("MessageFromBytes got error '%s', expected '%s'", err.Error(), testCase.errorString) } }) } } func BenchmarkMessageToBytes(b *testing.B) { message := &OSCMessage{ Address: "/hello", Args: []OSCArg{ {Type: "i", Value: 35}, }, } for b.Loop() { _, err := message.ToBytes() if err != nil { b.Fatalf("failed to encode properly: %s", err.Error()) } } } func BenchmarkMessageFromBytes(b *testing.B) { bytes := []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35} for b.Loop() { _, err := MessageFromBytes(bytes) if err != nil { b.Fatalf("failed to decode properly: %s", err.Error()) } } } func FuzzMessageFromBytes(f *testing.F) { seedBytes := [][]byte{ {}, {47, 104, 101, 108, 108, 111, 0, 0}, {47, 104, 101, 108, 108, 111, 0, 0, 44, 105, 0, 0, 0, 0, 0, 35}, } for _, seed := range seedBytes { f.Add(seed) } f.Fuzz(func(t *testing.T, data []byte) { _, _ = MessageFromBytes(data) }) }