2022-12-04 18:21:45 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package messages_test
|
|
|
|
|
|
|
|
import (
|
2022-12-04 19:37:26 +00:00
|
|
|
"bytes"
|
|
|
|
"math/big"
|
2022-12-04 18:21:45 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2022-12-04 19:37:26 +00:00
|
|
|
"github.com/shamaton/msgpackgen/msgpack"
|
2022-12-04 18:21:45 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"git.cacert.org/cacert-gosigner/pkg/messages"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseUsage(t *testing.T) {
|
|
|
|
okValues := []string{
|
|
|
|
"ocsp", "client", "code", "person", "server", "server_client",
|
|
|
|
"org_client", "org_code", "org_email", "org_person", "org_server", "org_server_client",
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range okValues {
|
|
|
|
t.Run(v, func(t *testing.T) {
|
|
|
|
u, err := messages.ParseUsage(v)
|
|
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Greater(t, u, messages.ProfileUsage(0))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("invalid", func(t *testing.T) {
|
|
|
|
u, err := messages.ParseUsage("foo")
|
|
|
|
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, u, messages.UsageInvalid)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildCommandAnnounce(t *testing.T) {
|
|
|
|
commands := []messages.CommandCode{
|
|
|
|
messages.CmdUndef,
|
|
|
|
messages.CmdHealth,
|
|
|
|
messages.CmdFetchCRL,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range commands {
|
|
|
|
t.Run(c.String(), func(t *testing.T) {
|
|
|
|
announce := messages.BuildCommandAnnounce(c)
|
|
|
|
|
|
|
|
require.NotNil(t, announce)
|
|
|
|
|
|
|
|
assert.Equal(t, c, announce.Code)
|
|
|
|
assert.NotEmpty(t, announce.ID)
|
|
|
|
assert.NotEmpty(t, announce.Created)
|
|
|
|
assert.True(t, announce.Created.Before(time.Now().UTC()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildResponseAnnounce(t *testing.T) {
|
|
|
|
responses := []messages.ResponseCode{
|
|
|
|
messages.RespError,
|
|
|
|
messages.RespUndef,
|
|
|
|
messages.RespHealth,
|
|
|
|
messages.RespFetchCRL,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, r := range responses {
|
|
|
|
commandID := uuid.NewString()
|
|
|
|
|
|
|
|
t.Run(r.String(), func(t *testing.T) {
|
|
|
|
announce := messages.BuildResponseAnnounce(r, commandID)
|
|
|
|
|
|
|
|
assert.NotNil(t, announce)
|
|
|
|
|
|
|
|
assert.Equal(t, r, announce.Code)
|
|
|
|
assert.Equal(t, commandID, announce.ID)
|
|
|
|
assert.NotEmpty(t, announce.ID)
|
|
|
|
assert.NotEmpty(t, announce.Created)
|
|
|
|
assert.True(t, announce.Created.Before(time.Now().UTC()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-12-04 19:37:26 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|