Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor & add test #10

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,20 @@ 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
github.com/jmespath/go-jmespath v0.4.0 // indirect
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
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down Expand Up @@ -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=
11 changes: 10 additions & 1 deletion pkg/proc/cat_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
13 changes: 8 additions & 5 deletions pkg/proc/interaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down
38 changes: 38 additions & 0 deletions pkg/proc/message_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}