Jan Dittberner
ad6b987c91
- decouple config and messages - cainfo maps from config.Profile to messages.CAProfile - config parses profile usage - validity can be configured per certificate profile, defaults are defined in a defaultValidity method of the profile usage - the client simulator emits certificate signing requests at random intervals - add implementation of SingCertificateCommand to MsgPackHandler - remove indirection signing.RequestSignature
177 lines
4.3 KiB
Go
177 lines
4.3 KiB
Go
/*
|
|
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 (
|
|
"bytes"
|
|
"math/big"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/shamaton/msgpackgen/msgpack"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"git.cacert.org/cacert-gosigner/internal/x509/signing"
|
|
"git.cacert.org/cacert-gosigner/pkg/messages"
|
|
)
|
|
|
|
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()))
|
|
})
|
|
}
|
|
}
|
|
|
|
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: signing.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)
|
|
})
|
|
}
|
|
}
|