cacert-gosigner/pkg/protocol/protocol_test.go

255 lines
5.7 KiB
Go
Raw Normal View History

/*
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 protocol
import (
"context"
"strings"
"sync"
"testing"
"time"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"git.cacert.org/cacert-gosigner/pkg/messages"
)
func AssertLogs(t *testing.T, hook *test.Hook, expected []logrus.Entry) {
t.Helper()
logEntries := hook.AllEntries()
assert.Len(t, logEntries, len(expected))
for i, e := range expected {
assert.Equal(t, e.Level, logEntries[i].Level)
assert.Equal(t, e.Message, logEntries[i].Message)
for k, v := range e.Data {
assert.Contains(t, logEntries[i].Data, k)
assert.Equal(t, logEntries[i].Data[k], v)
}
}
}
func TestCommand_String(t *testing.T) {
c := &Command{
Announce: messages.BuildCommandAnnounce(messages.CmdUndef),
Command: "my undefined command",
}
str := c.String()
assert.NotEmpty(t, str)
assert.Contains(t, str, c.Announce.String())
assert.Contains(t, str, c.Command)
assert.Contains(t, str, "announce")
assert.Contains(t, str, "data")
assert.Contains(t, str, "Cmd[")
assert.True(t, strings.HasSuffix(str, "}]"))
}
func TestResponse_String(t *testing.T) {
r := &Response{
Announce: messages.BuildResponseAnnounce(messages.RespUndef, uuid.NewString()),
Response: "my undefined response",
}
str := r.String()
assert.NotEmpty(t, str)
assert.Contains(t, str, r.Announce.String())
assert.Contains(t, str, r.Response)
assert.Contains(t, str, "announce")
assert.Contains(t, str, "data")
assert.Contains(t, str, "Rsp[")
assert.True(t, strings.HasSuffix(str, "}]"))
}
func TestProtocolState_String(t *testing.T) {
goodStates := []struct {
name string
state protocolState
}{
{"command announce", cmdAnnounce},
{"command data", cmdData},
{"handle command", handleCommand},
{"respond", respond},
{"response announce", respAnnounce},
{"response data", respData},
{"handle response", handleResponse},
}
for _, s := range goodStates {
t.Run(s.name, func(t *testing.T) {
str := s.state.String()
assert.NotEmpty(t, str)
assert.NotContains(t, str, "unknown")
})
}
t.Run("unsupported state", func(t *testing.T) {
str := protocolState(-1).String()
assert.NotEmpty(t, str)
assert.Contains(t, str, "unknown")
assert.Contains(t, str, "-1")
})
}
type NoopServerHandler struct{}
func (h *NoopServerHandler) CommandAnnounce(context.Context, <-chan []byte) (*Command, error) {
return nil, nil
}
func (h *NoopServerHandler) CommandData(context.Context, <-chan []byte, *Command) error {
return nil
}
func (h *NoopServerHandler) HandleCommand(context.Context, *Command) (*Response, error) {
return nil, nil
}
func (h *NoopServerHandler) Respond(context.Context, *Response, chan<- []byte) error {
return nil
}
func TestServerProtocol_HandleUnknownProtocolState(t *testing.T) {
protoLog, pHook := test.NewNullLogger()
protoLog.SetLevel(logrus.TraceLevel)
if testing.Short() {
t.Skip("skipping test in short mode")
}
in, out := make(chan []byte), make(chan []byte)
var err error
p := NewServer(&NoopServerHandler{}, in, out, protoLog)
p.state = protocolState(100)
ctx, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
err = p.Handle(ctx)
wg.Done()
}()
time.Sleep(10 * time.Millisecond)
cancel()
wg.Wait()
assert.ErrorContains(t, err, "unknown protocol state")
AssertLogs(t, pHook, []logrus.Entry{
{
Level: logrus.DebugLevel,
Message: "handling protocol state",
Data: map[string]interface{}{"state": protocolState(100)},
},
})
}
type NoopClientHandler struct{}
func (h *NoopClientHandler) Send(context.Context, *Command, chan<- []byte) error {
return nil
}
func (h *NoopClientHandler) ResponseAnnounce(context.Context, <-chan []byte) (*Response, error) {
return nil, nil
}
func (h *NoopClientHandler) ResponseData(context.Context, <-chan []byte, *Response) error {
return nil
}
func (h *NoopClientHandler) HandleResponse(context.Context, *Response) error {
return nil
}
func TestClientProtocol_HandleUnknownProtocolState(t *testing.T) {
protoLog, pHook := test.NewNullLogger()
protoLog.SetLevel(logrus.TraceLevel)
t.Run("unknown-protocol-state", func(t *testing.T) {
t.Cleanup(func() {
pHook.Reset()
})
if testing.Short() {
t.Skip("skipping test in short mode")
}
in, out := make(chan []byte), make(chan []byte)
commands := make(chan *Command, 10)
var (
err error
sent []byte
)
c := NewClient(&NoopClientHandler{}, commands, in, out, protoLog)
c.state = protocolState(100)
ctx, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
err = c.Handle(ctx)
wg.Done()
}()
go func() {
defer wg.Done()
select {
case <-ctx.Done():
return
case sent = <-out:
return
}
}()
time.Sleep(10 * time.Millisecond)
cancel()
wg.Wait()
assert.ErrorContains(t, err, "unknown protocol state")
assert.Nil(t, sent)
AssertLogs(t, pHook, []logrus.Entry{
{
Level: logrus.DebugLevel,
Message: "handling protocol state",
Data: map[string]interface{}{"state": protocolState(100)},
},
})
})
}