diff --git a/ably/internal/ablyutil/msgpack.go b/ably/internal/ablyutil/msgpack.go index 98dd66ce..b0ad4462 100644 --- a/ably/internal/ablyutil/msgpack.go +++ b/ably/internal/ablyutil/msgpack.go @@ -12,7 +12,6 @@ var handle codec.MsgpackHandle func init() { handle.Raw = true handle.WriteExt = true - handle.RawToString = true } // UnmarshalMsgpack decodes the MessagePack-encoded data and stores the result in the diff --git a/ably/internal/ablyutil/msgpack_test.go b/ably/internal/ablyutil/msgpack_test.go index 59e98cef..7563a522 100644 --- a/ably/internal/ablyutil/msgpack_test.go +++ b/ably/internal/ablyutil/msgpack_test.go @@ -5,9 +5,61 @@ package ablyutil import ( "bytes" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "testing" ) +func TestUnmarshallByte(t *testing.T) { + buf := []byte{ + 0xc4, // bin8 + 2, // len + 'a', 'a', // bytes + } + var target interface{} + + err := UnmarshalMsgpack(buf, &target) + require.NoError(t, err) + assert.IsType(t, []byte{}, target, + "bin8 should be decoded as []byte, but instead we got %T", target) +} + +func TestMsgPackDecodingOpts(t *testing.T) { + for _, rts := range []bool{false, true} { + handle.RawToString = rts + for _, msg := range []interface{}{ + //"aa", + []byte("aa"), + //strings.Repeat("a", 300), + bytes.Repeat([]byte("a"), 300), + bytes.Repeat([]byte("a"), 64000), + } { + buf, err := MarshalMsgpack(msg) + require.NoError(t, err) + var target interface{} + err = UnmarshalMsgpack(buf, &target) + require.NoError(t, err) + if len(buf) > 6 { + buf = buf[:5] + } + t.Logf("WriteExt=%v RawToString=%v Msg=%T len=%d, -> %T, %#v", handle.WriteExt, rts, msg, Len(msg), target, buf) + + } + } + +} + +func Len(a interface{}) int { + switch v := a.(type) { + case string: + return len(v) + case []byte: + return len(v) + default: + panic(v) + } +} + func TestMsgpack(t *testing.T) { type Object1 struct { Key int64 `codec:"my_key"` diff --git a/ably/proto_protocol_message_test.go b/ably/proto_protocol_message_test.go index 8783c4c8..c2a57707 100644 --- a/ably/proto_protocol_message_test.go +++ b/ably/proto_protocol_message_test.go @@ -5,6 +5,8 @@ package ably_test import ( "bytes" + "encoding/json" + "github.com/davecgh/go-spew/spew" "strconv" "testing" @@ -12,8 +14,75 @@ import ( "github.com/ably/ably-go/ably/internal/ablyutil" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) +func FuzzUnmarshalMsgpack(f *testing.F) { + f.Add([]byte{1, 2, 3}) + f.Fuzz(func(t *testing.T, buf []byte) { + var msg ably.ProtocolMessage + err := ablyutil.UnmarshalMsgpack(buf, &msg) + if err != nil { + return + } + _, err = ablyutil.MarshalMsgpack(msg) + require.NoError(t, err) + if len(buf) < 10 { + return + } + }) +} + +func TestMsgpackEncodesBytesAsBytes(t *testing.T) { + msg := ably.Message{Data: []byte("abc")} + + encoded, err := ablyutil.MarshalMsgpack(msg) + assert.NoError(t, err) + spew.Dump(encoded) + + var decodedMsg ably.Message + err = ablyutil.UnmarshalMsgpack(encoded, &decodedMsg) + require.NoError(t, err) + assert.IsType(t, []byte{}, decodedMsg.Data) +} + +func TestMsgpackEncodesStringAsString(t *testing.T) { + msg := ably.Message{Data: "abc"} + + encoded, err := ablyutil.MarshalMsgpack(msg) + assert.NoError(t, err) + spew.Dump(encoded) + + var decodedMsg ably.Message + err = ablyutil.UnmarshalMsgpack(encoded, &decodedMsg) + require.NoError(t, err) + assert.IsType(t, "", decodedMsg.Data) +} + +func TestJsonEncodesBytesAsBytes(t *testing.T) { + msg := ably.Message{Data: []byte("abc")} + + encoded, err := json.Marshal(msg) + assert.NoError(t, err) + + var decodedMsg ably.Message + err = json.Unmarshal(encoded, &decodedMsg) + require.NoError(t, err) + assert.IsType(t, []byte{}, decodedMsg.Data) +} + +func TestJsonEncodesStringAsString(t *testing.T) { + msg := ably.Message{Data: "abc"} + + encoded, err := json.Marshal(msg) + assert.NoError(t, err) + + var decodedMsg ably.Message + err = json.Unmarshal(encoded, &decodedMsg) + require.NoError(t, err) + assert.IsType(t, "", decodedMsg.Data) +} + // TestProtocolMessageEncodeZeroSerials tests that zero-valued serials are // explicitly encoded into msgpack (as required by the realtime API) func TestProtocolMessageEncodeZeroSerials(t *testing.T) { diff --git a/go.mod b/go.mod index 9ff9d62e..00a20498 100644 --- a/go.mod +++ b/go.mod @@ -1,17 +1,17 @@ module github.com/ably/ably-go +go 1.18 + require ( - github.com/stretchr/testify v1.7.1 - github.com/ugorji/go/codec v1.1.9 - golang.org/x/sys v0.2.0 + github.com/davecgh/go-spew v1.1.1 + github.com/stretchr/testify v1.8.1 + github.com/ugorji/go/codec v1.2.8 + golang.org/x/sys v0.4.0 nhooyr.io/websocket v1.8.7 ) require ( - github.com/davecgh/go-spew v1.1.1 // indirect - github.com/klauspost/compress v1.10.3 // indirect + github.com/klauspost/compress v1.15.14 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) - -go 1.17 diff --git a/go.sum b/go.sum index 59a3d2b7..21b04088 100644 --- a/go.sum +++ b/go.sum @@ -28,8 +28,9 @@ github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvK github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= -github.com/klauspost/compress v1.10.3 h1:OP96hzwJVBIHYU52pVTI6CczrxPvrGfgqF9N5eTO0Q8= github.com/klauspost/compress v1.10.3/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= +github.com/klauspost/compress v1.15.14 h1:i7WCKDToww0wA+9qrUZ1xOjp218vfFo3nTU6UHp+gOc= +github.com/klauspost/compress v1.15.14/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM= github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= @@ -41,19 +42,21 @@ github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lN github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= -github.com/ugorji/go v1.1.9 h1:SObrQTaSuP8WOv2WNCj8gECiNSJIUvk3Q7N26c96Gws= -github.com/ugorji/go v1.1.9/go.mod h1:chLrngdsg43geAaeId+nXO57YsDdl5OZqd/QtBiD19g= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= -github.com/ugorji/go/codec v1.1.9 h1:J/7hhpkQwgypRNvaeh/T5gzJ2gEI/l8S3qyRrdEa1fA= -github.com/ugorji/go/codec v1.1.9/go.mod h1:+SWgpdqOgdW5sBaiDfkHilQ1SxQ1hBkq/R+kHfL7Suo= +github.com/ugorji/go/codec v1.2.8 h1:sgBJS6COt0b/P40VouWKdseidkDgHxYGm0SAglUHfP0= +github.com/ugorji/go/codec v1.2.8/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= -golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= +golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -64,7 +67,8 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g= nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=