From 37d3b1e02146cbfb865a06bec3fedb3294e140e8 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Fri, 2 Dec 2022 09:09:52 +0100 Subject: [PATCH] Update dependencies This commit updates all external dependencies. The cobs module has a changed API and code using cobs has been adapted to this new API. --- cmd/clientsim/main.go | 9 ++++-- go.mod | 12 ++++---- go.sum | 52 +++++++++++++++++++++++++++++------ internal/serial/seriallink.go | 9 ++++-- pkg/protocol/protocol.go | 25 ++++++++++------- pkg/protocol/protocol_test.go | 28 +++++++++++++------ 6 files changed, 98 insertions(+), 37 deletions(-) diff --git a/cmd/clientsim/main.go b/cmd/clientsim/main.go index 6aaa72c..fdac7ca 100644 --- a/cmd/clientsim/main.go +++ b/cmd/clientsim/main.go @@ -280,6 +280,11 @@ func main() { messages.RegisterGeneratedResolver() + cobsFramer, err := protocol.NewCOBSFramer(logger) + if err != nil { + logger.WithError(err).Fatal("could not create COBS framer") + } + sim := &clientSimulator{ commandGenerator: &TestCommandGenerator{ logger: logger, @@ -288,11 +293,11 @@ func main() { logger: logger, framesIn: make(chan []byte), framesOut: make(chan []byte), - framer: protocol.NewCOBSFramer(logger), + framer: cobsFramer, clientHandler: newClientHandler(logger), } - err := sim.Run(context.Background()) + err = sim.Run(context.Background()) if err != nil { logger.WithError(err).Error("simulator returned an error") } diff --git a/go.mod b/go.mod index 2ee142d..afe553c 100644 --- a/go.mod +++ b/go.mod @@ -5,24 +5,24 @@ go 1.17 require ( github.com/ThalesIgnite/crypto11 v1.2.5 github.com/balacode/go-delta v0.1.0 - github.com/dave/jennifer v1.4.1 + github.com/dave/jennifer v1.6.0 github.com/google/uuid v1.3.0 - github.com/justincpresley/go-cobs v1.2.0 + github.com/justincpresley/go-cobs v1.3.1 github.com/shamaton/msgpackgen v0.3.0 github.com/sirupsen/logrus v1.9.0 github.com/stretchr/testify v1.8.0 github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 - golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 + golang.org/x/term v0.2.0 gopkg.in/yaml.v3 v3.0.1 ) require ( - github.com/balacode/zr v1.0.0 // indirect + github.com/balacode/zr v1.1.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/miekg/pkcs11 v1.1.1 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - github.com/shamaton/msgpack/v2 v2.1.0 // indirect + github.com/shamaton/msgpack/v2 v2.1.1 // indirect github.com/thales-e-security/pool v0.0.2 // indirect - golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect + golang.org/x/sys v0.2.0 // indirect ) diff --git a/go.sum b/go.sum index c803152..b53f4c8 100644 --- a/go.sum +++ b/go.sum @@ -2,17 +2,26 @@ github.com/ThalesIgnite/crypto11 v1.2.5 h1:1IiIIEqYmBvUYFeMnHqRft4bwf/O36jryEUpY github.com/ThalesIgnite/crypto11 v1.2.5/go.mod h1:ILDKtnCKiQ7zRoNxcp36Y1ZR8LBPmR2E23+wTQe/MlE= github.com/balacode/go-delta v0.1.0 h1:pwz4CMn06P2bIaIfAx3GSabMPwJp/Ww4if+7SgPYa3I= github.com/balacode/go-delta v0.1.0/go.mod h1:wLNrwTI3lHbPBvnLzqbHmA7HVVlm1u22XLvhbeA6t3o= -github.com/balacode/zr v1.0.0 h1:MCupkEoXvrnCljc4KddiDOhR04ZLUAACgtKuo3o+9vc= github.com/balacode/zr v1.0.0/go.mod h1:pLeSAL3DhZ9L0JuiRkUtIX3mLOCtzBLnDhfmykbSmkE= -github.com/dave/jennifer v1.4.1 h1:XyqG6cn5RQsTj3qlWQTKlRGAyrTcsk1kUmWdZBzRjDw= +github.com/balacode/zr v1.1.2 h1:DtZYCKtSTw8Giw/uv8zwkr2wb+C0Y7tx+947M077Ly0= +github.com/balacode/zr v1.1.2/go.mod h1:hN/XmTlDUdBTg3nd6sfc65aCrdKpVd1Y4sTYInF+T+U= +github.com/dave/astrid v0.0.0-20170323122508-8c2895878b14/go.mod h1:Sth2QfxfATb/nW4EsrSi2KyJmbcniZ8TgTaji17D6ms= +github.com/dave/brenda v1.1.0/go.mod h1:4wCUr6gSlu5/1Tk7akE5X7UorwiQ8Rij0SKH3/BGMOM= +github.com/dave/courtney v0.3.0/go.mod h1:BAv3hA06AYfNUjfjQr+5gc6vxeBVOupLqrColj+QSD8= +github.com/dave/gopackages v0.0.0-20170318123100-46e7023ec56e/go.mod h1:i00+b/gKdIDIxuLDFob7ustLAVqhsZRk2qVZrArELGQ= github.com/dave/jennifer v1.4.1/go.mod h1:7jEdnm+qBcxl8PC0zyp7vxcpSRnzXSt9r39tpTVGlwA= +github.com/dave/jennifer v1.6.0 h1:MQ/6emI2xM7wt0tJzJzyUik2Q3Tcn2eE0vtYgh4GPVI= +github.com/dave/jennifer v1.6.0/go.mod h1:AxTG893FiZKqxy3FP1kL80VMshSMuz2G+EgvszgGRnk= +github.com/dave/kerr v0.0.0-20170318121727-bc25dd6abe8e/go.mod h1:qZqlPyPvfsDJt+3wHJ1EvSXDuVjFTK0j2p/ca+gtsb8= +github.com/dave/patsy v0.0.0-20210517141501-957256f50cba/go.mod h1:qfR88CgEGLoiqDaE+xxDCi5QA5v4vUoW0UCX2Nd5Tlc= +github.com/dave/rebecca v0.9.1/go.mod h1:N6XYdMD/OKw3lkF3ywh8Z6wPGuwNFDNtWYEMFWEmXBA= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/justincpresley/go-cobs v1.2.0 h1:dyWszWzXObEv8sxVMJTAIo9XT7HEM10vkAOZq2eVEsQ= -github.com/justincpresley/go-cobs v1.2.0/go.mod h1:L0d+EbGirv6IzsXNzwULduI2/z3ijkkAmsAuPMpLfqA= +github.com/justincpresley/go-cobs v1.3.1 h1:PbQ6FqJ5GTIECZz+FwgDTJlHl2avdboWpGrjJG9Khjg= +github.com/justincpresley/go-cobs v1.3.1/go.mod h1:L0d+EbGirv6IzsXNzwULduI2/z3ijkkAmsAuPMpLfqA= github.com/miekg/pkcs11 v1.0.3-0.20190429190417-a667d056470f/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs= github.com/miekg/pkcs11 v1.1.1 h1:Ugu9pdy6vAYku5DEpVWVFPYnzV+bxB+iRdbuFSu7TvU= github.com/miekg/pkcs11 v1.1.1/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs= @@ -21,8 +30,9 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/shamaton/msgpack/v2 v2.1.0 h1:9jJ2eGZw2Wa9KExPX3KaDDckVjgr4zhXGFCfWagUWqg= github.com/shamaton/msgpack/v2 v2.1.0/go.mod h1:aTUEmh31ziGX1Ml7wMPLVY0f4vT3CRsCvZRoSCs+VGg= +github.com/shamaton/msgpack/v2 v2.1.1 h1:gAMxOtVJz93R0EwewwUc8tx30n34aV6BzJuwHE8ogAk= +github.com/shamaton/msgpack/v2 v2.1.1/go.mod h1:aTUEmh31ziGX1Ml7wMPLVY0f4vT3CRsCvZRoSCs+VGg= github.com/shamaton/msgpackgen v0.3.0 h1:q6o7prOEJFdF9BAPgkOtfzJbs55pQi7g44RUnEVUxtM= github.com/shamaton/msgpackgen v0.3.0/go.mod h1:fd99fDDuxuTiWzkHC59uEGzrt/WDu+ltGZTbEWwVXIc= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= @@ -38,11 +48,35 @@ github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07 h1:UyzmZLoiDWMRywV4DUY github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA= github.com/thales-e-security/pool v0.0.2 h1:RAPs4q2EbWsTit6tpzuvTFlgFRJ3S8Evf5gtvVDbmPg= github.com/thales-e-security/pool v0.0.2/go.mod h1:qtpMm2+thHtqhLzTwgDBj/OuNnMpupY8mv0Phz0gjhU= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 h1:EH1Deb8WZJ0xc0WK//leUHXcX9aLE5SymusoTmMZye8= -golang.org/x/term v0.0.0-20220411215600-e5f449aeb171/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.2.0 h1:z85xZCsEl7bi/KwbNADeBYoOP0++7W1ipu+aGnpwzRM= +golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/serial/seriallink.go b/internal/serial/seriallink.go index 71c09f2..ea1a2a3 100644 --- a/internal/serial/seriallink.go +++ b/internal/serial/seriallink.go @@ -107,16 +107,21 @@ func New( logger *logrus.Logger, protocolHandler protocol.ServerHandler, ) (*Handler, error) { + cobsFramer, err := protocol.NewCOBSFramer(logger) + if err != nil { + return nil, fmt.Errorf("could not create COBS framer: %w", err) + } + h := &Handler{ serverHandler: protocolHandler, logger: logger, framesIn: make(chan []byte), framesOut: make(chan []byte), - framer: protocol.NewCOBSFramer(logger), + framer: cobsFramer, } h.config = &serial.Config{Name: cfg.Device, Baud: cfg.Baud, ReadTimeout: cfg.Timeout} - err := h.setupConnection() + err = h.setupConnection() if err != nil { return nil, err } diff --git a/pkg/protocol/protocol.go b/pkg/protocol/protocol.go index 49f066f..cb1c1c7 100644 --- a/pkg/protocol/protocol.go +++ b/pkg/protocol/protocol.go @@ -392,15 +392,20 @@ const bufferSize = 1024 const readInterval = 50 * time.Millisecond type COBSFramer struct { - config cobs.Config - logger *logrus.Logger + logger *logrus.Logger + encoder cobs.Encoder } -func NewCOBSFramer(logger *logrus.Logger) *COBSFramer { - return &COBSFramer{ - config: cobs.Config{SpecialByte: CobsDelimiter, Delimiter: true, EndingSave: true}, - logger: logger, +func NewCOBSFramer(logger *logrus.Logger) (*COBSFramer, error) { + encoder, err := cobs.NewEncoder(cobs.Config{SpecialByte: CobsDelimiter, Delimiter: true, EndingSave: true}) + if err != nil { + return nil, fmt.Errorf("could not setup encoder: %w", err) } + + return &COBSFramer{ + encoder: encoder, + logger: logger, + }, nil } func (c *COBSFramer) ReadFrames(ctx context.Context, reader io.Reader, frameChan chan []byte) error { @@ -434,7 +439,7 @@ func (c *COBSFramer) ReadFrames(ctx context.Context, reader io.Reader, frameChan buffer.Write(raw) for { - data, err = buffer.ReadBytes(c.config.SpecialByte) + data, err = buffer.ReadBytes(CobsDelimiter) if err != nil { if errors.Is(err, io.EOF) { buffer.Write(data) @@ -446,13 +451,13 @@ func (c *COBSFramer) ReadFrames(ctx context.Context, reader io.Reader, frameChan return fmt.Errorf("could not read from buffer: %w", err) } - if err = cobs.Verify(data, c.config); err != nil { + if err = c.encoder.Verify(data); err != nil { c.logger.WithError(err).Warnf("skipping invalid frame of %d bytes", len(data)) continue } - frame = cobs.Decode(data, c.config) + frame = c.encoder.Decode(data) c.logger.Tracef("frame decoded to length %d", len(frame)) @@ -493,7 +498,7 @@ func (c *COBSFramer) WriteFrames(ctx context.Context, writer io.Writer, frameCha return nil } - encoded := cobs.Encode(frame, c.config) + encoded := c.encoder.Encode(frame) n, err := io.Copy(writer, bytes.NewReader(encoded)) if err != nil { diff --git a/pkg/protocol/protocol_test.go b/pkg/protocol/protocol_test.go index b5ecbfb..464fab3 100644 --- a/pkg/protocol/protocol_test.go +++ b/pkg/protocol/protocol_test.go @@ -30,7 +30,6 @@ import ( "time" "github.com/google/uuid" - "github.com/justincpresley/go-cobs" "github.com/sirupsen/logrus" "github.com/sirupsen/logrus/hooks/test" "github.com/stretchr/testify/assert" @@ -943,10 +942,15 @@ func (h *testClientHandler) Send(ctx context.Context, command *Command, out chan break } + outBytes, ok := command.Command.(string) + if !ok { + return fmt.Errorf("could not use command '%+v' as string", command.Command) + } + select { case <-ctx.Done(): return nil - case out <- []byte(command.Command.(string)): //nolint:forcetypeassert + case out <- []byte(outBytes): break } @@ -1809,7 +1813,9 @@ func sendClientCommand(ctx context.Context, out chan []byte) ([]byte, []byte) { func TestNewCOBSFramer(t *testing.T) { logger, _ := test.NewNullLogger() - framer := NewCOBSFramer(logger) + framer, err := NewCOBSFramer(logger) + + assert.NoError(t, err) require.NotNil(t, framer) assert.IsType(t, (*COBSFramer)(nil), framer) @@ -1820,7 +1826,10 @@ func TestCOBSFramer_ReadFrames(t *testing.T) { logger, loggerHook := test.NewNullLogger() logger.SetLevel(logrus.TraceLevel) - framer := NewCOBSFramer(logger) + framer, err := NewCOBSFramer(logger) + + require.NoError(t, err) + require.NotNil(t, framer) t.Run("read error", func(t *testing.T) { t.Cleanup(func() { @@ -1968,7 +1977,7 @@ func TestCOBSFramer_ReadFrames(t *testing.T) { readFrames := make(chan []byte) - reader := bytes.NewBuffer(cobs.Encode([]byte("some bytes"), framer.config)) + reader := bytes.NewBuffer(framer.encoder.Encode([]byte("some bytes"))) var ( err error @@ -2027,7 +2036,10 @@ func TestCOBSFramer_WriteFrames(t *testing.T) { logger, loggerHook := test.NewNullLogger() logger.SetLevel(logrus.TraceLevel) - framer := NewCOBSFramer(logger) + framer, err := NewCOBSFramer(logger) + + require.NoError(t, err) + require.NotNil(t, framer) t.Run("closed channel", func(t *testing.T) { t.Cleanup(func() { loggerHook.Reset() }) @@ -2145,7 +2157,7 @@ func TestCOBSFramer_WriteFrames(t *testing.T) { assert.Equal(t, logrus.TraceLevel, logEntries[0].Level) frame := out.Bytes() - assert.NoError(t, cobs.Verify(frame, framer.config)) - assert.Equal(t, []byte("test message"), cobs.Decode(out.Bytes(), framer.config)) + assert.NoError(t, framer.encoder.Verify(frame)) + assert.Equal(t, []byte("test message"), framer.encoder.Decode(out.Bytes())) }) }