Add serialization/deserialization tests

main
Jan Dittberner 1 year ago
parent a0b6fdce98
commit 40b3219c7e

@ -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)
})
}
}

Loading…
Cancel
Save