From 40b3219c7ed8d1bbc5143b32cb6440b54ef30a72 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Sun, 4 Dec 2022 20:37:26 +0100 Subject: [PATCH] Add serialization/deserialization tests --- pkg/messages/messages_it_test.go | 101 +++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/pkg/messages/messages_it_test.go b/pkg/messages/messages_it_test.go index 34191f2..d87d871 100644 --- a/pkg/messages/messages_it_test.go +++ b/pkg/messages/messages_it_test.go @@ -18,10 +18,13 @@ limitations under the License. package messages_test import ( + "bytes" + "math/big" "testing" "time" "github.com/google/uuid" + "github.com/shamaton/msgpackgen/msgpack" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -96,3 +99,101 @@ func TestBuildResponseAnnounce(t *testing.T) { }) } } + +func TestMsgPackSerialization(t *testing.T) { + messages.RegisterGeneratedResolver() + + t.Run("test-command-announce", func(t *testing.T) { + c := messages.BuildCommandAnnounce(messages.CmdUndef) + + serialized, err := msgpack.Marshal(&c) + + assert.NoError(t, err) + require.NotNil(t, serialized) + assert.True(t, bytes.Contains(serialized, []byte("code"))) + assert.True(t, bytes.Contains(serialized, []byte("id"))) + assert.True(t, bytes.Contains(serialized, []byte("created"))) + + var deserialized messages.CommandAnnounce + + err = msgpack.Unmarshal(serialized, &deserialized) + + assert.NoError(t, err) + + assert.Equal(t, c.ID, deserialized.ID) + assert.Equal(t, c.Code, deserialized.Code) + assert.True(t, c.Created.Equal(deserialized.Created)) + }) + + var messageTests = []struct { + Name string + Value interface{} + Result interface{} + }{ + {"health-command", &messages.HealthCommand{}, &messages.HealthCommand{}}, + {"ca-info-command", &messages.CAInfoCommand{Name: "test"}, &messages.CAInfoCommand{}}, + { + "fetch-crl-command", + &messages.FetchCRLCommand{IssuerID: "test", LastKnownID: big.NewInt(10).Bytes()}, + &messages.FetchCRLCommand{}, + }, + { + "health-response", + &messages.HealthResponse{ + Version: "foobar", + Healthy: true, + Info: []*messages.HealthInfo{{ + Source: "baz", + Healthy: true, + MoreInfo: map[string]string{"alice": "bob"}, + }}, + }, + &messages.HealthResponse{}, + }, + { + "ca-info-response", + &messages.CAInfoResponse{ + Name: "test", + Certificate: []byte{0x42}, + Signing: false, + Profiles: []messages.CAProfile{ + { + Name: "client", + UseFor: messages.UsageClient, + }, + }, + }, + &messages.CAInfoResponse{}, + }, + { + "fetch-crl-response", + &messages.FetchCRLResponse{ + IssuerID: "test", + IsDelta: false, + UnChanged: false, + CRLData: []byte{0x8, 0x15}, + CRLNumber: big.NewInt(4711).Bytes(), + }, + &messages.FetchCRLResponse{}, + }, + { + "error-response", + &messages.ErrorResponse{Message: "command failed"}, + &messages.ErrorResponse{}, + }, + } + + for _, v := range messageTests { + t.Run(v.Name, func(t *testing.T) { + serialized, err := msgpack.Marshal(v.Value) + + assert.NoError(t, err) + assert.NotNil(t, serialized) + + err = msgpack.Unmarshal(serialized, v.Result) + + assert.NoError(t, err) + assert.Equal(t, v.Value, v.Result) + }) + } +}