From a0b6fdce986da7b221c655069bb46cd1891363d4 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Sun, 4 Dec 2022 19:21:45 +0100 Subject: [PATCH] Refactor public API tests for messages - move tests for public API to messages_test package --- pkg/messages/messages_it_test.go | 98 ++++++++++++++++++++++++++++++++ pkg/messages/messages_test.go | 69 ---------------------- 2 files changed, 98 insertions(+), 69 deletions(-) create mode 100644 pkg/messages/messages_it_test.go diff --git a/pkg/messages/messages_it_test.go b/pkg/messages/messages_it_test.go new file mode 100644 index 0000000..34191f2 --- /dev/null +++ b/pkg/messages/messages_it_test.go @@ -0,0 +1,98 @@ +/* +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())) + }) + } +} diff --git a/pkg/messages/messages_test.go b/pkg/messages/messages_test.go index b795e07..0490ade 100644 --- a/pkg/messages/messages_test.go +++ b/pkg/messages/messages_test.go @@ -92,27 +92,6 @@ func TestResponseCode_String(t *testing.T) { }) } -func TestBuildCommandAnnounce(t *testing.T) { - commands := []CommandCode{ - CmdUndef, - CmdHealth, - CmdFetchCRL, - } - - for _, c := range commands { - t.Run(c.String(), func(t *testing.T) { - announce := 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 TestCommandAnnounce_String(t *testing.T) { announce := BuildCommandAnnounce(CmdUndef) @@ -126,31 +105,6 @@ func TestCommandAnnounce_String(t *testing.T) { assert.Contains(t, str, "created") } -func TestBuildResponseAnnounce(t *testing.T) { - responses := []ResponseCode{ - RespError, - RespUndef, - RespHealth, - RespFetchCRL, - } - - for _, r := range responses { - commandID := uuid.NewString() - - t.Run(r.String(), func(t *testing.T) { - announce := 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 TestResponseAnnounce_String(t *testing.T) { announce := BuildResponseAnnounce(RespUndef, uuid.NewString()) @@ -276,29 +230,6 @@ func TestProfileUsage_Description(t *testing.T) { }) } -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 := ParseUsage(v) - - assert.NoError(t, err) - assert.Greater(t, u, ProfileUsage(0)) - }) - } - - t.Run("invalid", func(t *testing.T) { - u, err := ParseUsage("foo") - - assert.Error(t, err) - assert.Equal(t, u, UsageInvalid) - }) -} - func TestCAProfile_String(t *testing.T) { profiles := []CAProfile{ {Name: "test-client", UseFor: UsageClient},