From de3a5f3fda3d54fc13233534247c2c3f7c94fe86 Mon Sep 17 00:00:00 2001 From: reshke Date: Wed, 22 Nov 2023 12:40:38 +0000 Subject: [PATCH] Refactor & add test --- go.mod | 7 +++++++ go.sum | 5 +++++ pkg/proc/cat_message.go | 11 ++++++++++- pkg/proc/interaction.go | 13 ++++++++----- pkg/proc/message_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 pkg/proc/message_test.go diff --git a/go.mod b/go.mod index 5265168..7708e36 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,12 @@ require ( gopkg.in/yaml.v2 v2.4.0 ) +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) + require ( github.com/cloudflare/circl v1.3.3 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect @@ -20,6 +26,7 @@ require ( github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/stretchr/testify v1.8.4 golang.org/x/crypto v0.7.0 // indirect golang.org/x/sys v0.12.0 // indirect ) diff --git a/go.sum b/go.sum index f9e9d45..5859f56 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,8 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 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/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= @@ -36,6 +38,8 @@ github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyh github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= @@ -92,4 +96,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/proc/cat_message.go b/pkg/proc/cat_message.go index 8f8f72b..1c19d77 100644 --- a/pkg/proc/cat_message.go +++ b/pkg/proc/cat_message.go @@ -41,7 +41,16 @@ func (c *CatMessage) Encode() []byte { return append(bs, bt...) } -func GetCatName(b []byte) string { +func (c *CatMessage) Decode(body []byte) error { + c.Name = c.GetCatName(body[4:]) + if body[1] == byte(DecryptMessage) { + c.Decrypt = true + } + + return nil +} + +func (c *CatMessage) GetCatName(b []byte) string { buff := bytes.NewBufferString("") for i := 0; i < len(b); i++ { diff --git a/pkg/proc/interaction.go b/pkg/proc/interaction.go index fa5b3a5..ccaae2e 100644 --- a/pkg/proc/interaction.go +++ b/pkg/proc/interaction.go @@ -24,16 +24,17 @@ func ProcConn(s storage.StorageReader, cr crypt.Crypter, ycl *client.YClient) er switch tp { case MessageTypeCat: // omit first byte - name := GetCatName(body[4:]) - ylogger.Zero.Debug().Str("object-path", name).Msg("cat object") - r, err := s.CatFileFromStorage(name) + msg := CatMessage{} + msg.Decode(body) + ylogger.Zero.Debug().Str("object-path", msg.Name).Msg("cat object") + r, err := s.CatFileFromStorage(msg.Name) if err != nil { _ = ycl.ReplyError(err, "failed to compelete request") return err } - if body[1] == byte(DecryptMessage) { - ylogger.Zero.Debug().Str("object-path", name).Msg("decrypt object ") + if msg.Decrypt { + ylogger.Zero.Debug().Str("object-path", msg.Name).Msg("decrypt object ") r, err = cr.Decrypt(r) if err != nil { _ = ycl.ReplyError(err, "failed to compelete request") @@ -43,6 +44,8 @@ func ProcConn(s storage.StorageReader, cr crypt.Crypter, ycl *client.YClient) er } io.Copy(ycl.Conn, r) + case MessageTypePut: + default: _ = ycl.ReplyError(nil, "wrong request type") diff --git a/pkg/proc/message_test.go b/pkg/proc/message_test.go new file mode 100644 index 0000000..f85da76 --- /dev/null +++ b/pkg/proc/message_test.go @@ -0,0 +1,38 @@ +package proc_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/yezzey-gp/yproxy/pkg/proc" +) + +func TestCatMsg(t *testing.T) { + assert := assert.New(t) + + type tcase struct { + name string + decrypt bool + err error + } + + for _, tt := range []tcase{ + { + "nam1", + true, + nil, + }, + } { + + msg := proc.NewCatMessage(tt.name, tt.decrypt) + body := msg.Encode() + + msg2 := proc.CatMessage{} + + err := msg2.Decode(body[8:]) + + assert.NoError(err) + assert.Equal(msg.Name, msg2.Name) + assert.Equal(msg.Decrypt, msg2.Decrypt) + } +}