Implement unit tests for public packages

This commit adds a comprehensive unit test suite for all public packages.
This commit is contained in:
Jan Dittberner 2022-12-01 21:36:10 +01:00
parent 51afebf2c1
commit 19436c06c2
5 changed files with 2499 additions and 239 deletions

View file

@ -115,13 +115,13 @@ func (c *clientSimulator) Run(ctx context.Context) error {
generatorErrors := make(chan error) generatorErrors := make(chan error)
go func() { go func() {
err := c.framer.ReadFrames(os.Stdin, c.framesIn) err := c.framer.ReadFrames(ctx, os.Stdin, c.framesIn)
framerErrors <- err framerErrors <- err
}() }()
go func() { go func() {
err := c.framer.WriteFrames(os.Stdout, c.framesOut) err := c.framer.WriteFrames(ctx, os.Stdout, c.framesOut)
framerErrors <- err framerErrors <- err
}() }()
@ -129,7 +129,7 @@ func (c *clientSimulator) Run(ctx context.Context) error {
go func() { go func() {
clientProtocol := protocol.NewClient(c.clientHandler, c.commandGenerator.commands, c.framesIn, c.framesOut, c.logger) clientProtocol := protocol.NewClient(c.clientHandler, c.commandGenerator.commands, c.framesIn, c.framesOut, c.logger)
err := clientProtocol.Handle() err := clientProtocol.Handle(ctx)
protocolErrors <- err protocolErrors <- err
}() }()
@ -170,7 +170,7 @@ type ClientHandler struct {
logger *logrus.Logger logger *logrus.Logger
} }
func (c *ClientHandler) Send(command *protocol.Command, out chan []byte) error { func (c *ClientHandler) Send(ctx context.Context, command *protocol.Command, out chan []byte) error {
var ( var (
frame []byte frame []byte
err error err error
@ -185,7 +185,12 @@ func (c *ClientHandler) Send(command *protocol.Command, out chan []byte) error {
c.logger.Trace("writing command announcement") c.logger.Trace("writing command announcement")
out <- frame select {
case <-ctx.Done():
return nil
case out <- frame:
break
}
frame, err = msgpack.Marshal(command.Command) frame, err = msgpack.Marshal(command.Command)
if err != nil { if err != nil {
@ -194,17 +199,24 @@ func (c *ClientHandler) Send(command *protocol.Command, out chan []byte) error {
c.logger.WithField("command", command.Command).Info("write command data") c.logger.WithField("command", command.Command).Info("write command data")
out <- frame select {
case <-ctx.Done():
return nil
case out <- frame:
break
}
return nil return nil
} }
func (c *ClientHandler) ResponseAnnounce(in chan []byte) (*protocol.Response, error) { func (c *ClientHandler) ResponseAnnounce(ctx context.Context, in chan []byte) (*protocol.Response, error) {
response := &protocol.Response{} response := &protocol.Response{}
var announce messages.ResponseAnnounce var announce messages.ResponseAnnounce
select { select {
case <-ctx.Done():
return nil, nil
case frame := <-in: case frame := <-in:
if err := msgpack.Unmarshal(frame, &announce); err != nil { if err := msgpack.Unmarshal(frame, &announce); err != nil {
return nil, fmt.Errorf("could not unmarshal response announcement: %w", err) return nil, fmt.Errorf("could not unmarshal response announcement: %w", err)
@ -220,8 +232,10 @@ func (c *ClientHandler) ResponseAnnounce(in chan []byte) (*protocol.Response, er
} }
} }
func (c *ClientHandler) ResponseData(in chan []byte, response *protocol.Response) error { func (c *ClientHandler) ResponseData(ctx context.Context, in chan []byte, response *protocol.Response) error {
select { select {
case <-ctx.Done():
return nil
case frame := <-in: case frame := <-in:
switch response.Announce.Code { switch response.Announce.Code {
case messages.RespHealth: case messages.RespHealth:
@ -248,7 +262,7 @@ func (c *ClientHandler) ResponseData(in chan []byte, response *protocol.Response
return nil return nil
} }
func (c *ClientHandler) HandleResponse(response *protocol.Response) error { func (c *ClientHandler) HandleResponse(_ context.Context, response *protocol.Response) error {
c.logger.WithField("response", response.Announce).Info("handled response") c.logger.WithField("response", response.Announce).Info("handled response")
c.logger.WithField("response", response).Debug("full response") c.logger.WithField("response", response).Debug("full response")

View file

@ -18,10 +18,10 @@ limitations under the License.
package handler package handler
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"math/big" "math/big"
"sync"
"time" "time"
"github.com/shamaton/msgpackgen/msgpack" "github.com/shamaton/msgpackgen/msgpack"
@ -43,35 +43,33 @@ type MsgPackHandler struct {
logger *logrus.Logger logger *logrus.Logger
healthHandler *health.Handler healthHandler *health.Handler
fetchCRLHandler *revoking.FetchCRLHandler fetchCRLHandler *revoking.FetchCRLHandler
lock sync.Mutex
} }
func (m *MsgPackHandler) CommandAnnounce(frames chan []byte) (*protocol.Command, error) { func (m *MsgPackHandler) CommandAnnounce(ctx context.Context, frames chan []byte) (*protocol.Command, error) {
m.lock.Lock()
defer m.lock.Unlock()
frame := <-frames
var ann messages.CommandAnnounce
if err := msgpack.Unmarshal(frame, &ann); err != nil {
return nil, fmt.Errorf("could not unmarshal command announcement: %w", err)
}
if ann.Code == messages.CmdUndef {
return nil, fmt.Errorf("received undefined command announcement: %s", ann)
}
m.logger.WithField("announcement", &ann).Debug("received command announcement")
return &protocol.Command{Announce: &ann}, nil
}
func (m *MsgPackHandler) CommandData(frames chan []byte, command *protocol.Command) error {
m.lock.Lock()
defer m.lock.Unlock()
select { select {
case <-ctx.Done():
return nil, nil
case frame := <-frames:
var ann messages.CommandAnnounce
if err := msgpack.Unmarshal(frame, &ann); err != nil {
return nil, fmt.Errorf("could not unmarshal command announcement: %w", err)
}
if ann.Code == messages.CmdUndef {
return nil, fmt.Errorf("received undefined command announcement: %s", ann)
}
m.logger.WithField("announcement", &ann).Debug("received command announcement")
return &protocol.Command{Announce: &ann}, nil
}
}
func (m *MsgPackHandler) CommandData(ctx context.Context, frames chan []byte, command *protocol.Command) error {
select {
case <-ctx.Done():
return nil
case frame := <-frames: case frame := <-frames:
err := m.parseCommand(frame, command) err := m.parseCommand(frame, command)
if err != nil { if err != nil {
@ -84,10 +82,7 @@ func (m *MsgPackHandler) CommandData(frames chan []byte, command *protocol.Comma
} }
} }
func (m *MsgPackHandler) HandleCommand(command *protocol.Command) (*protocol.Response, error) { func (m *MsgPackHandler) HandleCommand(_ context.Context, command *protocol.Command) (*protocol.Response, error) {
m.lock.Lock()
defer m.lock.Unlock()
var ( var (
response *protocol.Response response *protocol.Response
err error err error
@ -110,10 +105,7 @@ func (m *MsgPackHandler) logCommandResponse(command *protocol.Command, response
m.logger.WithField("command", command).WithField("response", response).Debug("command and response") m.logger.WithField("command", command).WithField("response", response).Debug("command and response")
} }
func (m *MsgPackHandler) Respond(response *protocol.Response, out chan []byte) error { func (m *MsgPackHandler) Respond(ctx context.Context, response *protocol.Response, out chan []byte) error {
m.lock.Lock()
defer m.lock.Unlock()
announce, err := msgpack.Marshal(response.Announce) announce, err := msgpack.Marshal(response.Announce)
if err != nil { if err != nil {
return fmt.Errorf("could not marshal response announcement: %w", err) return fmt.Errorf("could not marshal response announcement: %w", err)
@ -121,7 +113,12 @@ func (m *MsgPackHandler) Respond(response *protocol.Response, out chan []byte) e
m.logger.WithField("length", len(announce)).Debug("write response announcement") m.logger.WithField("length", len(announce)).Debug("write response announcement")
out <- announce select {
case <-ctx.Done():
return nil
case out <- announce:
break
}
data, err := msgpack.Marshal(response.Response) data, err := msgpack.Marshal(response.Response)
if err != nil { if err != nil {
@ -130,7 +127,12 @@ func (m *MsgPackHandler) Respond(response *protocol.Response, out chan []byte) e
m.logger.WithField("length", len(data)).Debug("write response") m.logger.WithField("length", len(data)).Debug("write response")
out <- data select {
case <-ctx.Done():
return nil
case out <- data:
break
}
return nil return nil
} }

View file

@ -63,13 +63,13 @@ func (h *Handler) Run(ctx context.Context) error {
protocolErrors, framerErrors := make(chan error), make(chan error) protocolErrors, framerErrors := make(chan error), make(chan error)
go func() { go func() {
err := h.framer.ReadFrames(h.port, h.framesIn) err := h.framer.ReadFrames(ctx, h.port, h.framesIn)
framerErrors <- err framerErrors <- err
}() }()
go func() { go func() {
err := h.framer.WriteFrames(h.port, h.framesOut) err := h.framer.WriteFrames(ctx, h.port, h.framesOut)
framerErrors <- err framerErrors <- err
}() }()
@ -77,7 +77,7 @@ func (h *Handler) Run(ctx context.Context) error {
go func() { go func() {
serverProtocol := protocol.NewServer(h.serverHandler, h.framesIn, h.framesOut, h.logger) serverProtocol := protocol.NewServer(h.serverHandler, h.framesIn, h.framesOut, h.logger)
err := serverProtocol.Handle() err := serverProtocol.Handle(ctx)
protocolErrors <- err protocolErrors <- err
}() }()

View file

@ -20,6 +20,7 @@ package protocol
import ( import (
"bytes" "bytes"
"context"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -54,25 +55,27 @@ func (r *Response) String() string {
// ServerHandler is responsible for parsing incoming frames and calling commands // ServerHandler is responsible for parsing incoming frames and calling commands
type ServerHandler interface { type ServerHandler interface {
// CommandAnnounce handles the initial announcement of a command. // CommandAnnounce handles the initial announcement of a command.
CommandAnnounce(chan []byte) (*Command, error) CommandAnnounce(context.Context, chan []byte) (*Command, error)
// CommandData handles the command data. // CommandData handles the command data.
CommandData(chan []byte, *Command) error CommandData(context.Context, chan []byte, *Command) error
// HandleCommand executes the command, generating a response. // HandleCommand executes the command, generating a response.
HandleCommand(*Command) (*Response, error) HandleCommand(context.Context, *Command) (*Response, error)
// Respond generates the response for a command. // Respond generates the response for a command.
Respond(*Response, chan []byte) error Respond(context.Context, *Response, chan []byte) error
} }
type ClientHandler interface { type ClientHandler interface {
Send(*Command, chan []byte) error Send(context.Context, *Command, chan []byte) error
ResponseAnnounce(chan []byte) (*Response, error) ResponseAnnounce(context.Context, chan []byte) (*Response, error)
ResponseData(chan []byte, *Response) error ResponseData(context.Context, chan []byte, *Response) error
HandleResponse(*Response) error HandleResponse(context.Context, *Response) error
} }
var ( var (
errCommandExpected = errors.New("command must not be nil") errCommandExpected = errors.New("command must not be nil")
errResponseExpected = errors.New("response must not be nil") errCommandAnnounceExpected = errors.New("command must have an announcement")
errCommandDataExpected = errors.New("command must have data")
errResponseExpected = errors.New("response must not be nil")
ErrResponseAnnounceTimeoutExpired = errors.New("response announce timeout expired") ErrResponseAnnounceTimeoutExpired = errors.New("response announce timeout expired")
ErrResponseDataTimeoutExpired = errors.New("response data timeout expired") ErrResponseDataTimeoutExpired = errors.New("response data timeout expired")
@ -115,7 +118,7 @@ type ServerProtocol struct {
state protocolState state protocolState
} }
func (p *ServerProtocol) Handle() error { func (p *ServerProtocol) Handle(ctx context.Context) error {
var ( var (
command *Command command *Command
response *Response response *Response
@ -123,69 +126,109 @@ func (p *ServerProtocol) Handle() error {
) )
for { for {
p.logger.Debugf("handling protocol state %s", p.state) select {
case <-ctx.Done():
close(p.out)
switch p.state { return nil
case cmdAnnounce:
command, err = p.handler.CommandAnnounce(p.in)
if err != nil {
p.logger.WithError(err).Error("could not handle command announce")
break
}
p.state = cmdData
case cmdData:
if command == nil {
return errCommandExpected
}
err = p.handler.CommandData(p.in, command)
if err != nil {
p.logger.WithError(err).Error("could not handle command data")
p.state = cmdAnnounce
break
}
p.state = handleCommand
case handleCommand:
if command == nil {
return errCommandExpected
}
response, err = p.handler.HandleCommand(command)
if err != nil {
p.logger.WithError(err).Error("could not handle command")
p.state = cmdAnnounce
break
}
p.state = respond
case respond:
if response == nil {
return errResponseExpected
}
err = p.handler.Respond(response, p.out)
if err != nil {
p.logger.WithError(err).Error("could not respond")
p.state = cmdAnnounce
break
}
p.state = cmdAnnounce
default: default:
return fmt.Errorf("unknown protocol state %s", p.state) p.logger.Debugf("handling protocol state %s", p.state)
switch p.state {
case cmdAnnounce:
command = p.commandAnnounce(ctx)
case cmdData:
err = p.commandData(ctx, command)
if err != nil {
return err
}
case handleCommand:
response, err = p.handleCommand(ctx, command)
if err != nil {
return err
}
case respond:
err = p.respond(ctx, response)
if err != nil {
return err
}
default:
return fmt.Errorf("unknown protocol state %s", p.state)
}
} }
} }
} }
func (p *ServerProtocol) commandAnnounce(ctx context.Context) *Command {
command, err := p.handler.CommandAnnounce(ctx, p.in)
if err != nil {
p.logger.WithError(err).Error("could not handle command announce")
return nil
}
p.state = cmdData
return command
}
func (p *ServerProtocol) commandData(ctx context.Context, command *Command) error {
if command == nil || command.Announce == nil {
return errCommandAnnounceExpected
}
err := p.handler.CommandData(ctx, p.in, command)
if err != nil {
p.logger.WithError(err).Error("could not handle command data")
p.state = cmdAnnounce
return nil
}
p.state = handleCommand
return nil
}
func (p *ServerProtocol) handleCommand(ctx context.Context, command *Command) (*Response, error) {
if command == nil || command.Announce == nil || command.Command == nil {
return nil, errCommandDataExpected
}
response, err := p.handler.HandleCommand(ctx, command)
if err != nil {
p.logger.WithError(err).Error("could not handle command")
p.state = cmdAnnounce
return nil, nil
}
p.state = respond
return response, nil
}
func (p *ServerProtocol) respond(ctx context.Context, response *Response) error {
if response == nil {
return errResponseExpected
}
err := p.handler.Respond(ctx, response, p.out)
if err != nil {
p.logger.WithError(err).Error("could not respond")
p.state = cmdAnnounce
return nil
}
p.state = cmdAnnounce
return nil
}
func NewServer(handler ServerHandler, in, out chan []byte, logger *logrus.Logger) *ServerProtocol { func NewServer(handler ServerHandler, in, out chan []byte, logger *logrus.Logger) *ServerProtocol {
return &ServerProtocol{ return &ServerProtocol{
handler: handler, handler: handler,
@ -196,14 +239,6 @@ func NewServer(handler ServerHandler, in, out chan []byte, logger *logrus.Logger
} }
} }
// Framer handles bytes on the wire by adding or removing framing information.
type Framer interface {
// ReadFrames reads data frames and publishes unframed data to the channel.
ReadFrames(io.Reader, chan []byte) error
// WriteFrames takes data from the channel and writes framed data to the writer.
WriteFrames(io.Writer, chan []byte) error
}
type ClientProtocol struct { type ClientProtocol struct {
handler ClientHandler handler ClientHandler
commands chan *Command commands chan *Command
@ -212,82 +247,123 @@ type ClientProtocol struct {
state protocolState state protocolState
} }
func (p *ClientProtocol) Handle() error { func (p *ClientProtocol) Handle(ctx context.Context) error {
var ( var (
command *Command
response *Response response *Response
err error err error
) )
for { for {
p.logger.Debugf("handling protocol state %s", p.state) select {
case <-ctx.Done():
switch p.state { return nil
case cmdAnnounce:
command = <-p.commands
if command == nil {
return errCommandExpected
}
err = p.handler.Send(command, p.out)
if err != nil {
p.logger.WithError(err).Error("could not send command announce")
break
}
p.state = respAnnounce
case respAnnounce:
response, err = p.handler.ResponseAnnounce(p.in)
if err != nil {
p.logger.WithError(err).Error("could not handle response announce")
p.state = cmdAnnounce
break
}
p.state = respData
case respData:
if response == nil {
return errResponseExpected
}
err = p.handler.ResponseData(p.in, response)
if err != nil {
p.logger.WithError(err).Error("could not handle response data")
if errors.Is(err, ErrResponseDataTimeoutExpired) {
p.state = cmdAnnounce
} else {
p.state = respAnnounce
}
break
}
p.state = handleResponse
case handleResponse:
if response == nil {
return errResponseExpected
}
err = p.handler.HandleResponse(response)
if err != nil {
p.logger.WithError(err).Error("could not handle response")
p.state = cmdAnnounce
break
}
p.state = cmdAnnounce
default: default:
return fmt.Errorf("unknown protocol state %s", p.state) p.logger.Debugf("handling protocol state %s", p.state)
switch p.state {
case cmdAnnounce:
err = p.cmdAnnounce(ctx)
if err != nil {
return err
}
case respAnnounce:
response = p.respAnnounce(ctx)
case respData:
err = p.respData(ctx, response)
if err != nil {
return err
}
case handleResponse:
err = p.handleResponse(ctx, response)
if err != nil {
return err
}
default:
return fmt.Errorf("unknown protocol state %s", p.state)
}
} }
} }
} }
func (p *ClientProtocol) cmdAnnounce(ctx context.Context) error {
select {
case <-ctx.Done():
return nil
case command := <-p.commands:
if command == nil {
return errCommandExpected
}
err := p.handler.Send(ctx, command, p.out)
if err != nil {
p.logger.WithError(err).Error("could not send command announce")
return nil
}
}
p.state = respAnnounce
return nil
}
func (p *ClientProtocol) respAnnounce(ctx context.Context) *Response {
response, err := p.handler.ResponseAnnounce(ctx, p.in)
if err != nil {
p.logger.WithError(err).Error("could not handle response announce")
p.state = cmdAnnounce
return nil
}
p.state = respData
return response
}
func (p *ClientProtocol) respData(ctx context.Context, response *Response) error {
if response == nil || response.Announce == nil {
return errResponseExpected
}
err := p.handler.ResponseData(ctx, p.in, response)
if err != nil {
p.logger.WithError(err).Error("could not handle response data")
if errors.Is(err, ErrResponseDataTimeoutExpired) {
p.state = cmdAnnounce
} else {
p.state = respAnnounce
}
return nil
}
p.state = handleResponse
return nil
}
func (p *ClientProtocol) handleResponse(ctx context.Context, response *Response) error {
if response == nil || response.Announce == nil || response.Response == nil {
return errResponseExpected
}
err := p.handler.HandleResponse(ctx, response)
if err != nil {
p.logger.WithError(err).Error("could not handle response")
p.state = cmdAnnounce
return nil
}
p.state = cmdAnnounce
return nil
}
func NewClient( func NewClient(
handler ClientHandler, handler ClientHandler,
commands chan *Command, commands chan *Command,
@ -304,6 +380,14 @@ func NewClient(
} }
} }
// Framer handles bytes on the wire by adding or removing framing information.
type Framer interface {
// ReadFrames reads data frames and publishes unframed data to the channel.
ReadFrames(context.Context, io.Reader, chan []byte) error
// WriteFrames takes data from the channel and writes framed data to the writer.
WriteFrames(context.Context, io.Writer, chan []byte) error
}
const bufferSize = 1024 const bufferSize = 1024
const readInterval = 50 * time.Millisecond const readInterval = 50 * time.Millisecond
@ -319,7 +403,7 @@ func NewCOBSFramer(logger *logrus.Logger) *COBSFramer {
} }
} }
func (c *COBSFramer) ReadFrames(reader io.Reader, frameChan chan []byte) error { func (c *COBSFramer) ReadFrames(ctx context.Context, reader io.Reader, frameChan chan []byte) error {
var ( var (
err error err error
raw, data, frame []byte raw, data, frame []byte
@ -328,49 +412,55 @@ func (c *COBSFramer) ReadFrames(reader io.Reader, frameChan chan []byte) error {
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
for { for {
raw, err = c.readRaw(reader) select {
if err != nil { case <-ctx.Done():
close(frameChan) return nil
default:
return err raw, err = c.readRaw(reader)
}
if len(raw) == 0 {
time.Sleep(readInterval)
continue
}
c.logger.Tracef("read %d raw bytes", len(raw))
buffer.Write(raw)
for {
data, err = buffer.ReadBytes(c.config.SpecialByte)
if err != nil { if err != nil {
if errors.Is(err, io.EOF) { close(frameChan)
buffer.Write(data)
break return err
}
return fmt.Errorf("could not read from buffer: %w", err)
} }
if err = cobs.Verify(data, c.config); err != nil { if len(raw) == 0 {
c.logger.WithError(err).Warnf("skipping invalid frame of %d bytes", len(data)) time.Sleep(readInterval)
continue continue
} }
frame = cobs.Decode(data, c.config) c.logger.Tracef("read %d raw bytes", len(raw))
c.logger.Tracef("frame decoded to length %d", len(frame)) buffer.Write(raw)
frameChan <- frame for {
data, err = buffer.ReadBytes(c.config.SpecialByte)
if err != nil {
if errors.Is(err, io.EOF) {
buffer.Write(data)
break
}
// this is a safety measure, buffer.ReadBytes should only return io.EOF
return fmt.Errorf("could not read from buffer: %w", err)
}
if err = cobs.Verify(data, c.config); err != nil {
c.logger.WithError(err).Warnf("skipping invalid frame of %d bytes", len(data))
continue
}
frame = cobs.Decode(data, c.config)
c.logger.Tracef("frame decoded to length %d", len(frame))
frameChan <- frame
}
c.logger.Tracef("read buffer is now %d bytes long", buffer.Len())
} }
c.logger.Tracef("read buffer is now %d bytes long", buffer.Len())
} }
} }
@ -380,7 +470,7 @@ func (c *COBSFramer) readRaw(reader io.Reader) ([]byte, error) {
count, err := reader.Read(buf) count, err := reader.Read(buf)
if err != nil { if err != nil {
if errors.Is(err, io.EOF) { if errors.Is(err, io.EOF) {
return []byte{}, nil return buf[:count], nil
} }
return nil, fmt.Errorf("could not read data: %w", err) return nil, fmt.Errorf("could not read data: %w", err)
@ -391,23 +481,26 @@ func (c *COBSFramer) readRaw(reader io.Reader) ([]byte, error) {
return raw, nil return raw, nil
} }
func (c *COBSFramer) WriteFrames(writer io.Writer, frameChan chan []byte) error { func (c *COBSFramer) WriteFrames(ctx context.Context, writer io.Writer, frameChan chan []byte) error {
for { for {
frame := <-frameChan select {
case <-ctx.Done():
if frame == nil {
c.logger.Debug("channel closed")
return nil return nil
case frame := <-frameChan:
if frame == nil {
c.logger.Debug("channel closed")
return nil
}
encoded := cobs.Encode(frame, c.config)
n, err := io.Copy(writer, bytes.NewReader(encoded))
if err != nil {
return fmt.Errorf("cold not write data: %w", err)
}
c.logger.Tracef("wrote %d bytes", n)
} }
encoded := cobs.Encode(frame, c.config)
n, err := io.Copy(writer, bytes.NewReader(encoded))
if err != nil {
return fmt.Errorf("cold not write data: %w", err)
}
c.logger.Tracef("wrote %d bytes", n)
} }
} }

File diff suppressed because it is too large Load diff