From 016f324efb7418626f45751deafd4fa00a64d94e Mon Sep 17 00:00:00 2001 From: amnon Date: Tue, 10 Jan 2023 13:35:10 +0000 Subject: [PATCH 01/14] Fix https://github.com/ably/ably-go/issues/574 data sent as []byte should be decoded as []byte --- ably/internal/ablyutil/msgpack.go | 1 - ably/internal/ablyutil/msgpack_test.go | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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..3bec4e32 100644 --- a/ably/internal/ablyutil/msgpack_test.go +++ b/ably/internal/ablyutil/msgpack_test.go @@ -5,9 +5,25 @@ 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 TestMsgpack(t *testing.T) { type Object1 struct { Key int64 `codec:"my_key"` From 68c0afe7bc158472a784b6863ddc89821958c58b Mon Sep 17 00:00:00 2001 From: amnon Date: Wed, 11 Jan 2023 09:47:57 +0000 Subject: [PATCH 02/14] Add integration test to show that data sent as []byte will be received as []byte --- ably/realtime_channel_integration_test.go | 46 +++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/ably/realtime_channel_integration_test.go b/ably/realtime_channel_integration_test.go index b0faf6a4..40e8efaa 100644 --- a/ably/realtime_channel_integration_test.go +++ b/ably/realtime_channel_integration_test.go @@ -6,6 +6,7 @@ package ably_test import ( "context" "fmt" + "github.com/stretchr/testify/require" "net/url" "reflect" "testing" @@ -141,3 +142,48 @@ func TestRealtimeChannel_AttachWhileDisconnected(t *testing.T) { err = ablytest.Wait(<-res, nil) assert.NoError(t, err) } + +// (RSL6a) All messages received will be decoded automatically based on the encoding field +// and the payloads will be converted into the format they were originally sent using i.e. binary, string, or JSON. +func TestRealtimeChannel_ByteSliceDataShouldBeDeliveredAsByteSlice(t *testing.T) { + app, client1 := ablytest.NewRealtime(nil...) + defer safeclose(t, ablytest.FullRealtimeCloser(client1), app) + + client2 := app.NewRealtime(ably.WithEchoMessages(false)) + defer safeclose(t, ablytest.FullRealtimeCloser(client2)) + + err := ablytest.Wait(ablytest.ConnWaiter(client1, client1.Connect, ably.ConnectionEventConnected), nil) + assert.NoError(t, err) + err = ablytest.Wait(ablytest.ConnWaiter(client2, client2.Connect, ably.ConnectionEventConnected), nil) + assert.NoError(t, err) + + channel1 := client1.Channels.Get("test") + channel2 := client2.Channels.Get("test") + + sub1, unsub1, err := ablytest.ReceiveMessages(channel1, "") + assert.NoError(t, err, "client1:.Subscribe(context.Background())=%v", err) + defer unsub1() + + stringMsg := "(RSL6a) When the publisher sends a string, the subscriber should get a string" + byteMsg := []byte("(RSL6a) In this case the message was sent as []byte so should have been received as as []byte") + + err = channel2.Publish(context.Background(), "hello", stringMsg) + require.NoError(t, err) + + timeout := 15 * time.Second + + err = expectMsg(sub1, "hello", stringMsg, timeout, true) + assert.NoError(t, err) + + err = channel2.Publish(context.Background(), "hello", byteMsg) + require.NoError(t, err) + + err = expectMsg(sub1, "hello", byteMsg, timeout, true) + assert.NoError(t, err) + + err = channel2.Publish(context.Background(), "hello", map[string]int{"key": 123}) + require.NoError(t, err) + + err = expectMsg(sub1, "hello", `{"key":123}`, timeout, true) + assert.NoError(t, err, "When a struct is published, we deliver a JSON representation of the struct encoded as a string.") +} From 48f5867aef0501072cba92b76eed6ad06a5d572a Mon Sep 17 00:00:00 2001 From: amnon Date: Fri, 13 Jan 2023 11:27:48 +0000 Subject: [PATCH 03/14] Add decoding test driven by fixtures from test-resources/messages-encoding.json --- ably/proto_message_decoding_test.go | 67 +++++++++++++++++++++++++++++ go.mod | 2 +- 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 ably/proto_message_decoding_test.go diff --git a/ably/proto_message_decoding_test.go b/ably/proto_message_decoding_test.go new file mode 100644 index 00000000..bd007127 --- /dev/null +++ b/ably/proto_message_decoding_test.go @@ -0,0 +1,67 @@ +package ably + +import ( + "encoding/json" + "os" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +type fixture struct { + Data string `json:"data"` + Encoding string `json:"encoding"` + ExpectedType string `json:"expectedType"` + ExpectedValue any `json:"expectedValue"` +} + +func loadFixtures() ([]fixture, error) { + var dec struct { + Messages []fixture + } + + // We can't embed the fixtures as go:embed forbids embedding of resources above the current directory. + text, err := os.ReadFile("../common/test-resources/messages-encoding.json") + if err != nil { + return nil, err + } + + err = json.Unmarshal(text, &dec) + return dec.Messages, err +} + +func Test_decodeMessage(t *testing.T) { + fixtures, err := loadFixtures() + require.NoError(t, err, "failed to load test fixtures") + for _, f := range fixtures { + t.Run(f.Data, func(t *testing.T) { + msg := Message{ + Data: f.Data, + Encoding: f.Encoding, + } + decodedMsg, err := msg.withDecodedData(nil) + assert.NoError(t, err) + switch f.ExpectedType { + case "string": + assert.IsType(t, "string", decodedMsg.Data) + assert.Equal(t, f.ExpectedValue, decodedMsg.Data) + case "jsonObject": + assert.IsType(t, map[string]any{}, decodedMsg.Data) + assert.Equal(t, f.ExpectedValue, decodedMsg.Data) + + case "jsonArray": + assert.IsType(t, []any{}, decodedMsg.Data) + assert.Equal(t, f.ExpectedValue, decodedMsg.Data) + + t.Log(decodedMsg) + case "binary": + assert.IsType(t, []byte{}, decodedMsg.Data) + if f.ExpectedValue == nil { + break + } + assert.Equal(t, []byte(f.ExpectedValue.(string)), decodedMsg.Data) + } + }) + } +} diff --git a/go.mod b/go.mod index 9ff9d62e..e3490502 100644 --- a/go.mod +++ b/go.mod @@ -14,4 +14,4 @@ require ( gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect ) -go 1.17 +go 1.18 From 5508a7c963f9ccee0cf4762d01b35d09ed9b595c Mon Sep 17 00:00:00 2001 From: amnon Date: Fri, 13 Jan 2023 15:22:35 +0000 Subject: [PATCH 04/14] Test rencoding direction. --- ably/proto_message_decoding_test.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ably/proto_message_decoding_test.go b/ably/proto_message_decoding_test.go index bd007127..602ac036 100644 --- a/ably/proto_message_decoding_test.go +++ b/ably/proto_message_decoding_test.go @@ -53,8 +53,6 @@ func Test_decodeMessage(t *testing.T) { case "jsonArray": assert.IsType(t, []any{}, decodedMsg.Data) assert.Equal(t, f.ExpectedValue, decodedMsg.Data) - - t.Log(decodedMsg) case "binary": assert.IsType(t, []byte{}, decodedMsg.Data) if f.ExpectedValue == nil { @@ -62,6 +60,18 @@ func Test_decodeMessage(t *testing.T) { } assert.Equal(t, []byte(f.ExpectedValue.(string)), decodedMsg.Data) } + + // Test that the re-encoding of the decoded message gives us back the original fixture. + reEncoded, err := decodedMsg.withEncodedData(nil) + require.NoError(t, err) + assert.Equal(t, f.Encoding, reEncoded.Encoding) + if f.Encoding == "json" { + require.IsType(t, "", reEncoded.Data) + // json fields could be re-ordered so assert.Equal could fail. + assert.JSONEq(t, f.Data, reEncoded.Data.(string)) + } else { + assert.Equal(t, f.Data, reEncoded.Data) + } }) } } From cf50a55c60f7e3dda18e4d7ba7f682d450f43103 Mon Sep 17 00:00:00 2001 From: amnon Date: Thu, 19 Jan 2023 11:35:55 +0000 Subject: [PATCH 05/14] add fixtures for msgpack decoding. --- ably/proto_message_decoding_test.go | 50 ++++++++++++++++++++++++ ably/testdata/msgpack_test_fixtures.json | 44 +++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 ably/testdata/msgpack_test_fixtures.json diff --git a/ably/proto_message_decoding_test.go b/ably/proto_message_decoding_test.go index 602ac036..163206ff 100644 --- a/ably/proto_message_decoding_test.go +++ b/ably/proto_message_decoding_test.go @@ -1,10 +1,13 @@ package ably import ( + "encoding/base64" "encoding/json" + "github.com/ably/ably-go/ably/internal/ablyutil" "os" "testing" + _ "embed" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -75,3 +78,50 @@ func Test_decodeMessage(t *testing.T) { }) } } + +//go:embed testdata/msgpack_test_fixtures.json +var MsgpackFixtures []byte + +type MsgpackTestFixture struct { + Name string + Data string + Encoding string + NumRepeat int + Type string + MsgPack string +} + +var fixtures []MsgpackTestFixture + +func init() { + err := json.Unmarshal(MsgpackFixtures, &fixtures) + if err != nil { + panic(err) + } +} + +func TestMsgpackDecoding(t *testing.T) { + for _, f := range fixtures { + t.Run(f.Name, func(t *testing.T) { + + msgpackData := make([]byte, len(f.MsgPack)) + n, err := base64.StdEncoding.Decode(msgpackData, []byte(f.MsgPack)) + require.NoError(t, err) + msgpackData = msgpackData[:n] + var protoMsg ProtocolMessage + err = ablyutil.UnmarshalMsgpack(msgpackData, &protoMsg) + require.NoError(t, err) + msg := protoMsg.Messages[0] + decodedMsg, err := msg.withDecodedData(nil) + switch f.Type { + case "string": + require.IsType(t, "string", decodedMsg.Data) + assert.Equal(t, f.NumRepeat, len(decodedMsg.Data.(string))) + case "binary": + require.IsType(t, []byte{}, decodedMsg.Data) + assert.Equal(t, f.NumRepeat, len(decodedMsg.Data.([]byte))) + + } + }) + } +} diff --git a/ably/testdata/msgpack_test_fixtures.json b/ably/testdata/msgpack_test_fixtures.json new file mode 100644 index 00000000..4bc0ad34 --- /dev/null +++ b/ably/testdata/msgpack_test_fixtures.json @@ -0,0 +1,44 @@ +[ + { + "Name": "short string", + "Data": "a", + "NumRepeat": 1, + "Type": "string", + "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GBpGRhdGGhYaltc2dTZXJpYWwA" + }, + { + "Name": "200 byte string string", + "Data": "a", + "NumRepeat": 200, + "Type": "string", + "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GBpGRhdGHZyGFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhqW1zZ1NlcmlhbAA=" + }, + { + "Name": "2000 byte string string", + "Data": "a", + "NumRepeat": 2000, + "Type": "string", + "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GBpGRhdGHaB9BhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhY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WFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYaltc2dTZXJpYWwA" + }, + { + "Name": "short binary", + "Data": "a", + "NumRepeat": 1, + "Type": "string", + "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GBpGRhdGGhYaltc2dTZXJpYWwA" + }, + { + "Name": "200 byte binary", + "Data": "a", + "NumRepeat": 200, + "Type": "string", + "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GBpGRhdGHZyGFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhqW1zZ1NlcmlhbAA=" + }, + { + "Name": "2000 byte binary", + "Data": "a", + "NumRepeat": 200, + "Type": "string", + "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GBpGRhdGHZyGFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhqW1zZ1NlcmlhbAA=" + } +] From e082084336e9a79d5001a63f28b32d4102be7b31 Mon Sep 17 00:00:00 2001 From: amnon Date: Thu, 19 Jan 2023 11:41:00 +0000 Subject: [PATCH 06/14] add binary fixtures --- ably/testdata/msgpack_test_fixtures.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ably/testdata/msgpack_test_fixtures.json b/ably/testdata/msgpack_test_fixtures.json index 4bc0ad34..c9e9086a 100644 --- a/ably/testdata/msgpack_test_fixtures.json +++ b/ably/testdata/msgpack_test_fixtures.json @@ -24,21 +24,21 @@ "Name": "short binary", "Data": "a", "NumRepeat": 1, - "Type": "string", + "Type": "binary", "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GBpGRhdGGhYaltc2dTZXJpYWwA" }, { "Name": "200 byte binary", "Data": "a", "NumRepeat": 200, - "Type": "string", + "Type": "binary", "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GBpGRhdGHZyGFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhqW1zZ1NlcmlhbAA=" }, { "Name": "2000 byte binary", "Data": "a", "NumRepeat": 200, - "Type": "string", + "Type": "binary", "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GBpGRhdGHZyGFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhqW1zZ1NlcmlhbAA=" } ] From 8dcaac0a75f0aa4565103d62c3cd2531b5004280 Mon Sep 17 00:00:00 2001 From: amnon Date: Sun, 22 Jan 2023 13:00:20 +0000 Subject: [PATCH 07/14] TestMsgpackDecoding - a fixture driven test which tests the decoing and encoding of binary messages of different payload types and lengths. --- ably/generate_fixtures_test.go | 52 ++++++++++++++++++++++++ ably/internal/ablyutil/msgpack_test.go | 3 +- ably/proto_message_decoding_test.go | 32 +++++++++++++-- ably/testdata/msgpack_test_fixtures.json | 43 ++++++++++++++++++-- 4 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 ably/generate_fixtures_test.go diff --git a/ably/generate_fixtures_test.go b/ably/generate_fixtures_test.go new file mode 100644 index 00000000..7c7f1294 --- /dev/null +++ b/ably/generate_fixtures_test.go @@ -0,0 +1,52 @@ +//go:build msgpack_artifact_gen + +// A utility that reads testdata/msgpack_test_fixtures.json, updates the msgpack representation, +// and writes the output to testdata/msgpack_test_fixtures.json.new. + +package ably + +import ( + "bytes" + "encoding/base64" + "encoding/json" + "github.com/ably/ably-go/ably/internal/ablyutil" + "github.com/stretchr/testify/require" + "os" + "strings" + "testing" +) + +func TestGenMsgpackFixture(t *testing.T) { + out := make([]MsgpackTestFixture, 0, len(fixtures)) + for i, f := range fixtures { + msg := new(Message) + switch f.Type { + case "string": + msg.Data = strings.Repeat(f.Data.(string), f.NumRepeat) + case "binary": + msg.Data = bytes.Repeat([]byte(f.Data.(string)), f.NumRepeat) + case "jsonObject", "jsonArray": + msg.Data = f.Data + default: + panic("unkown message type: " + f.Type) + } + msg.Encoding = f.Encoding + encodedMsg, err := msg.withEncodedData(nil) + require.NoError(t, err) + pm := ProtocolMessage{ + Messages: []*Message{&encodedMsg}, + } + buf, err := ablyutil.MarshalMsgpack(pm) + require.NoError(t, err) + fixtures[i].MsgPack = base64.StdEncoding.EncodeToString(buf) + fixtures[i].Encoding = msg.Encoding + out = append(out, fixtures[i]) + } + + w, err := os.Create("testdata/msgpack_test_fixtures.json.new") + require.NoError(t, err) + defer w.Close() + enc := json.NewEncoder(w) + enc.SetIndent("", " ") + enc.Encode(out) +} diff --git a/ably/internal/ablyutil/msgpack_test.go b/ably/internal/ablyutil/msgpack_test.go index 3bec4e32..920ed2ca 100644 --- a/ably/internal/ablyutil/msgpack_test.go +++ b/ably/internal/ablyutil/msgpack_test.go @@ -5,9 +5,10 @@ package ablyutil import ( "bytes" + "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "testing" ) func TestUnmarshallByte(t *testing.T) { diff --git a/ably/proto_message_decoding_test.go b/ably/proto_message_decoding_test.go index 163206ff..a1d7031c 100644 --- a/ably/proto_message_decoding_test.go +++ b/ably/proto_message_decoding_test.go @@ -1,13 +1,17 @@ package ably import ( + "bytes" "encoding/base64" "encoding/json" - "github.com/ably/ably-go/ably/internal/ablyutil" "os" + "strings" "testing" + "github.com/ably/ably-go/ably/internal/ablyutil" + _ "embed" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -84,7 +88,7 @@ var MsgpackFixtures []byte type MsgpackTestFixture struct { Name string - Data string + Data any Encoding string NumRepeat int Type string @@ -100,28 +104,50 @@ func init() { } } +func init() { + err := json.Unmarshal(MsgpackFixtures, &fixtures) + if err != nil { + panic(err) + } +} + func TestMsgpackDecoding(t *testing.T) { for _, f := range fixtures { t.Run(f.Name, func(t *testing.T) { - msgpackData := make([]byte, len(f.MsgPack)) n, err := base64.StdEncoding.Decode(msgpackData, []byte(f.MsgPack)) require.NoError(t, err) msgpackData = msgpackData[:n] + var protoMsg ProtocolMessage err = ablyutil.UnmarshalMsgpack(msgpackData, &protoMsg) require.NoError(t, err) + msg := protoMsg.Messages[0] decodedMsg, err := msg.withDecodedData(nil) switch f.Type { case "string": require.IsType(t, "string", decodedMsg.Data) assert.Equal(t, f.NumRepeat, len(decodedMsg.Data.(string))) + assert.Equal(t, strings.Repeat(f.Data.(string), f.NumRepeat), decodedMsg.Data.(string)) case "binary": require.IsType(t, []byte{}, decodedMsg.Data) assert.Equal(t, f.NumRepeat, len(decodedMsg.Data.([]byte))) + assert.Equal(t, bytes.Repeat([]byte(f.Data.(string)), f.NumRepeat), decodedMsg.Data.([]byte)) + case "jsonObject", "jsonArray": + assert.Equal(t, f.Data, decodedMsg.Data) + } + // Now re-encode and check that we get back the original message. + reencodedMsg, err := msg.withEncodedData(nil) + require.NoError(t, err) + newMsg := ProtocolMessage{ + Messages: []*Message{&reencodedMsg}, } + newBytes, err := ablyutil.MarshalMsgpack(newMsg) + require.NoError(t, err) + assert.Equal(t, msgpackData, newBytes) + }) } } diff --git a/ably/testdata/msgpack_test_fixtures.json b/ably/testdata/msgpack_test_fixtures.json index c9e9086a..1d3a772b 100644 --- a/ably/testdata/msgpack_test_fixtures.json +++ b/ably/testdata/msgpack_test_fixtures.json @@ -1,7 +1,20 @@ [ + { + "Name": "json array", + "Data": [ + "a", + "b", + "c" + ], + "Encoding": "json", + "NumRepeat": 0, + "Type": "jsonArray", + "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GCpGRhdGGtWyJhIiwiYiIsImMiXahlbmNvZGluZ6lqc29uL2pzb26pbXNnU2VyaWFsAA==" + }, { "Name": "short string", "Data": "a", + "Encoding": "", "NumRepeat": 1, "Type": "string", "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GBpGRhdGGhYaltc2dTZXJpYWwA" @@ -9,6 +22,7 @@ { "Name": "200 byte string string", "Data": "a", + "Encoding": "", "NumRepeat": 200, "Type": "string", "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GBpG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hYWFhYWFhYaltc2dTZXJpYWwA" @@ -23,22 +38,42 @@ { "Name": "short binary", "Data": "a", + "Encoding": "", "NumRepeat": 1, "Type": "binary", - "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GBpGRhdGGhYaltc2dTZXJpYWwA" + "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GCpGRhdGGkWVE9PahlbmNvZGluZ6ZiYXNlNjSpbXNnU2VyaWFsAA==" }, { "Name": "200 byte binary", "Data": "a", + "Encoding": "", "NumRepeat": 200, "Type": "binary", - "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GBpGRhdGHZyGFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhqW1zZ1NlcmlhbAA=" + "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GCpGRhdGHaAQxZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0Z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VdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0U9qGVuY29kaW5npmJhc2U2NKltc2dTZXJpYWwA" + }, + { + "Name": "an object", + "Data": { + "bar": [ + "a", + 1.2, + { + "boo": "ha" + } + ], + "foo": 42 + }, + "Encoding": "json", + "NumRepeat": 0, + "Type": "jsonObject", + "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GCpGRhdGHZJ3siYmFyIjpbImEiLDEuMix7ImJvbyI6ImhhIn1dLCJmb28iOjQyfahlbmNvZGluZ6lqc29uL2pzb26pbXNnU2VyaWFsAA==" } ] From 074a153c4304abbbd42b3585549dda4353dd9c99 Mon Sep 17 00:00:00 2001 From: amnon Date: Sun, 22 Jan 2023 13:23:51 +0000 Subject: [PATCH 08/14] run goimports --- ably/proto_message_decoding_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ably/proto_message_decoding_test.go b/ably/proto_message_decoding_test.go index a1d7031c..1a9a9779 100644 --- a/ably/proto_message_decoding_test.go +++ b/ably/proto_message_decoding_test.go @@ -2,6 +2,7 @@ package ably import ( "bytes" + _ "embed" "encoding/base64" "encoding/json" "os" @@ -9,9 +10,6 @@ import ( "testing" "github.com/ably/ably-go/ably/internal/ablyutil" - - _ "embed" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) From 322a6963b70ac44d520a9bc6b07e5f9ed03c8937 Mon Sep 17 00:00:00 2001 From: amnon Date: Sun, 22 Jan 2023 13:24:36 +0000 Subject: [PATCH 09/14] remove duplicate init function --- ably/proto_message_decoding_test.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/ably/proto_message_decoding_test.go b/ably/proto_message_decoding_test.go index 1a9a9779..0a446484 100644 --- a/ably/proto_message_decoding_test.go +++ b/ably/proto_message_decoding_test.go @@ -102,13 +102,6 @@ func init() { } } -func init() { - err := json.Unmarshal(MsgpackFixtures, &fixtures) - if err != nil { - panic(err) - } -} - func TestMsgpackDecoding(t *testing.T) { for _, f := range fixtures { t.Run(f.Name, func(t *testing.T) { From 0f674c3cf550ff348338b3f4e0484a78f9fd06a5 Mon Sep 17 00:00:00 2001 From: amnon Date: Sun, 22 Jan 2023 17:36:25 +0000 Subject: [PATCH 10/14] Fix integration test so that it also works with non-binary protocol. --- ably/realtime_channel_integration_test.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/ably/realtime_channel_integration_test.go b/ably/realtime_channel_integration_test.go index 40e8efaa..6c4ebfde 100644 --- a/ably/realtime_channel_integration_test.go +++ b/ably/realtime_channel_integration_test.go @@ -5,17 +5,18 @@ package ably_test import ( "context" + "encoding/base64" "fmt" - "github.com/stretchr/testify/require" "net/url" + "os" "reflect" "testing" "time" "github.com/ably/ably-go/ably" "github.com/ably/ably-go/ablytest" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func expectMsg(ch <-chan *ably.Message, name string, data interface{}, t time.Duration, received bool) error { @@ -143,6 +144,10 @@ func TestRealtimeChannel_AttachWhileDisconnected(t *testing.T) { assert.NoError(t, err) } +func isBinaryProtocol() bool { + return os.Getenv("ABLY_PROTOCOL") != "application/json" +} + // (RSL6a) All messages received will be decoded automatically based on the encoding field // and the payloads will be converted into the format they were originally sent using i.e. binary, string, or JSON. func TestRealtimeChannel_ByteSliceDataShouldBeDeliveredAsByteSlice(t *testing.T) { @@ -178,8 +183,15 @@ func TestRealtimeChannel_ByteSliceDataShouldBeDeliveredAsByteSlice(t *testing.T) err = channel2.Publish(context.Background(), "hello", byteMsg) require.NoError(t, err) - err = expectMsg(sub1, "hello", byteMsg, timeout, true) - assert.NoError(t, err) + if isBinaryProtocol() { + err = expectMsg(sub1, "hello", byteMsg, timeout, true) + assert.NoError(t, err) + } else { + // (RSL4d1) a binary Message payload is encoded as Base64 and + // represented as a JSON string the encoding attribute is set to “base64”. + err = expectMsg(sub1, "hello", base64.StdEncoding.EncodeToString(byteMsg), timeout, true) + assert.NoError(t, err) + } err = channel2.Publish(context.Background(), "hello", map[string]int{"key": 123}) require.NoError(t, err) From ea22488cedcd5b0483e80f26046fc492269eaa11 Mon Sep 17 00:00:00 2001 From: amnon Date: Mon, 23 Jan 2023 07:12:47 +0000 Subject: [PATCH 11/14] rename global fixtures variable --- ably/generate_fixtures_test.go | 10 +++++----- ably/proto_message_decoding_test.go | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/ably/generate_fixtures_test.go b/ably/generate_fixtures_test.go index 7c7f1294..06706d21 100644 --- a/ably/generate_fixtures_test.go +++ b/ably/generate_fixtures_test.go @@ -17,8 +17,8 @@ import ( ) func TestGenMsgpackFixture(t *testing.T) { - out := make([]MsgpackTestFixture, 0, len(fixtures)) - for i, f := range fixtures { + out := make([]MsgpackTestFixture, 0, len(msgpackTestFixtures)) + for i, f := range msgpackTestFixtures { msg := new(Message) switch f.Type { case "string": @@ -38,9 +38,9 @@ func TestGenMsgpackFixture(t *testing.T) { } buf, err := ablyutil.MarshalMsgpack(pm) require.NoError(t, err) - fixtures[i].MsgPack = base64.StdEncoding.EncodeToString(buf) - fixtures[i].Encoding = msg.Encoding - out = append(out, fixtures[i]) + msgpackTestFixtures[i].MsgPack = base64.StdEncoding.EncodeToString(buf) + msgpackTestFixtures[i].Encoding = msg.Encoding + out = append(out, msgpackTestFixtures[i]) } w, err := os.Create("testdata/msgpack_test_fixtures.json.new") diff --git a/ably/proto_message_decoding_test.go b/ably/proto_message_decoding_test.go index 0a446484..e7a49c2b 100644 --- a/ably/proto_message_decoding_test.go +++ b/ably/proto_message_decoding_test.go @@ -93,17 +93,17 @@ type MsgpackTestFixture struct { MsgPack string } -var fixtures []MsgpackTestFixture +var msgpackTestFixtures []MsgpackTestFixture func init() { - err := json.Unmarshal(MsgpackFixtures, &fixtures) + err := json.Unmarshal(MsgpackFixtures, &msgpackTestFixtures) if err != nil { panic(err) } } func TestMsgpackDecoding(t *testing.T) { - for _, f := range fixtures { + for _, f := range msgpackTestFixtures { t.Run(f.Name, func(t *testing.T) { msgpackData := make([]byte, len(f.MsgPack)) n, err := base64.StdEncoding.Decode(msgpackData, []byte(f.MsgPack)) From 80ff55c53bf49ad9b8834e5e8ae51cf544cbfd95 Mon Sep 17 00:00:00 2001 From: amnon Date: Thu, 26 Jan 2023 14:10:35 +0000 Subject: [PATCH 12/14] use test fixtures from ably-common --- ably/generate_fixtures_test.go | 2 + ably/proto_message_decoding_test.go | 18 +++--- ably/testdata/msgpack_test_fixtures.json | 79 ------------------------ 3 files changed, 13 insertions(+), 86 deletions(-) delete mode 100644 ably/testdata/msgpack_test_fixtures.json diff --git a/ably/generate_fixtures_test.go b/ably/generate_fixtures_test.go index 06706d21..b1a13b1a 100644 --- a/ably/generate_fixtures_test.go +++ b/ably/generate_fixtures_test.go @@ -17,6 +17,8 @@ import ( ) func TestGenMsgpackFixture(t *testing.T) { + msgpackTestFixtures, err := loadMsgpackFixtures() + require.NoError(t, err) out := make([]MsgpackTestFixture, 0, len(msgpackTestFixtures)) for i, f := range msgpackTestFixtures { msg := new(Message) diff --git a/ably/proto_message_decoding_test.go b/ably/proto_message_decoding_test.go index e7a49c2b..c85b8755 100644 --- a/ably/proto_message_decoding_test.go +++ b/ably/proto_message_decoding_test.go @@ -81,9 +81,6 @@ func Test_decodeMessage(t *testing.T) { } } -//go:embed testdata/msgpack_test_fixtures.json -var MsgpackFixtures []byte - type MsgpackTestFixture struct { Name string Data any @@ -93,16 +90,23 @@ type MsgpackTestFixture struct { MsgPack string } -var msgpackTestFixtures []MsgpackTestFixture +func loadMsgpackFixtures() ([]MsgpackTestFixture, error) { + var o []MsgpackTestFixture -func init() { - err := json.Unmarshal(MsgpackFixtures, &msgpackTestFixtures) + // We can't embed the fixtures as go:embed forbids embedding of resources above the current directory. + text, err := os.ReadFile("../common/test-resources/msgpack_test_fixtures.json") if err != nil { - panic(err) + return nil, err } + + err = json.Unmarshal(text, &o) + return o, err } func TestMsgpackDecoding(t *testing.T) { + msgpackTestFixtures, err := loadMsgpackFixtures() + require.NoError(t, err) + for _, f := range msgpackTestFixtures { t.Run(f.Name, func(t *testing.T) { msgpackData := make([]byte, len(f.MsgPack)) diff --git a/ably/testdata/msgpack_test_fixtures.json b/ably/testdata/msgpack_test_fixtures.json deleted file mode 100644 index 1d3a772b..00000000 --- a/ably/testdata/msgpack_test_fixtures.json +++ /dev/null @@ -1,79 +0,0 @@ -[ - { - "Name": "json array", - "Data": [ - "a", - "b", - "c" - ], - "Encoding": "json", - "NumRepeat": 0, - "Type": "jsonArray", - "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GCpGRhdGGtWyJhIiwiYiIsImMiXahlbmNvZGluZ6lqc29uL2pzb26pbXNnU2VyaWFsAA==" - }, - { - "Name": "short string", - "Data": "a", - "Encoding": "", - "NumRepeat": 1, - "Type": "string", - "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GBpGRhdGGhYaltc2dTZXJpYWwA" - }, - { - "Name": "200 byte string string", - "Data": "a", - "Encoding": "", - "NumRepeat": 200, - "Type": "string", - "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GBpGRhdGHZyGFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFhYWFh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VdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0ZoWVdGaFlXRmhZV0U9qGVuY29kaW5npmJhc2U2NKltc2dTZXJpYWwA" - }, - { - "Name": "an object", - "Data": { - "bar": [ - "a", - 1.2, - { - "boo": "ha" - } - ], - "foo": 42 - }, - "Encoding": "json", - "NumRepeat": 0, - "Type": "jsonObject", - "MsgPack": "g7Bjb25uZWN0aW9uU2VyaWFsAKhtZXNzYWdlc5GCpGRhdGHZJ3siYmFyIjpbImEiLDEuMix7ImJvbyI6ImhhIn1dLCJmb28iOjQyfahlbmNvZGluZ6lqc29uL2pzb26pbXNnU2VyaWFsAA==" - } -] From 44b45ae70ce9633400cf753ba8d853aac543c251 Mon Sep 17 00:00:00 2001 From: amnon Date: Thu, 26 Jan 2023 15:12:51 +0000 Subject: [PATCH 13/14] embed is no longer needed --- ably/proto_message_decoding_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/ably/proto_message_decoding_test.go b/ably/proto_message_decoding_test.go index c85b8755..37387624 100644 --- a/ably/proto_message_decoding_test.go +++ b/ably/proto_message_decoding_test.go @@ -2,7 +2,6 @@ package ably import ( "bytes" - _ "embed" "encoding/base64" "encoding/json" "os" From 8d8d2c955ec0023ffca7acf974a18d1981a712d1 Mon Sep 17 00:00:00 2001 From: amnon Date: Fri, 27 Jan 2023 10:40:12 +0000 Subject: [PATCH 14/14] fix fixture generation - specifying camelCase json tags --- ably/generate_fixtures_test.go | 1 - ably/proto_message_decoding_test.go | 12 ++++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/ably/generate_fixtures_test.go b/ably/generate_fixtures_test.go index b1a13b1a..f9a7f04a 100644 --- a/ably/generate_fixtures_test.go +++ b/ably/generate_fixtures_test.go @@ -32,7 +32,6 @@ func TestGenMsgpackFixture(t *testing.T) { default: panic("unkown message type: " + f.Type) } - msg.Encoding = f.Encoding encodedMsg, err := msg.withEncodedData(nil) require.NoError(t, err) pm := ProtocolMessage{ diff --git a/ably/proto_message_decoding_test.go b/ably/proto_message_decoding_test.go index 37387624..53fcb679 100644 --- a/ably/proto_message_decoding_test.go +++ b/ably/proto_message_decoding_test.go @@ -81,12 +81,12 @@ func Test_decodeMessage(t *testing.T) { } type MsgpackTestFixture struct { - Name string - Data any - Encoding string - NumRepeat int - Type string - MsgPack string + Name string `json:"name"` + Data any `json:"data"` + Encoding string `json:"encoding"` + NumRepeat int `json:"numRepeat"` + Type string `json:"type"` + MsgPack string `json:"msgpack"` } func loadMsgpackFixtures() ([]MsgpackTestFixture, error) {