98 lines
2.4 KiB
Go
98 lines
2.4 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 (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"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()))
|
|
})
|
|
}
|
|
}
|