From 43d1e56c8898730729ccc2e1234e926223b61aff Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Sat, 25 Nov 2023 12:22:58 +0000 Subject: [PATCH] Fix sendmail test and rewrite fetch ws call --- api/api.go | 4 ++-- go.mod | 1 + go.sum | 2 ++ imap/client.go | 46 +++++++++++++++++++++----------------- sendmail/fake/fake-smtp.go | 45 ------------------------------------- sendmail/sendmail_test.go | 32 ++++++++++++++------------ 6 files changed, 48 insertions(+), 82 deletions(-) delete mode 100644 sendmail/fake/fake-smtp.go diff --git a/api/api.go b/api/api.go index e169bc3..5685191 100644 --- a/api/api.go +++ b/api/api.go @@ -108,8 +108,8 @@ func SetupApiServer(listen string, auth *AuthChecker, send Smtp, recv Imap) *htt // read incoming message var m struct { - Action string `json:"action"` - Args []string `json:"args"` + Action string `json:"action"` + Args json.RawMessage `json:"args"` } err := c.ReadJSON(&m) if err != nil { diff --git a/go.mod b/go.mod index d392ca7..1117c59 100644 --- a/go.mod +++ b/go.mod @@ -28,4 +28,5 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/net v0.18.0 // indirect golang.org/x/text v0.14.0 // indirect + zappem.net/pub/debug/xxd v1.0.0 // indirect ) diff --git a/go.sum b/go.sum index 70bb1b3..47b4287 100644 --- a/go.sum +++ b/go.sum @@ -85,3 +85,5 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +zappem.net/pub/debug/xxd v1.0.0 h1:weitvkgR0yOBP4QfGTPyQDD25rxAJxYw6+Dy2wrA3Zs= +zappem.net/pub/debug/xxd v1.0.0/go.mod h1:7m1I+mBsdwBWcaVp8P0w0YQP9UWRsJEkrXB4OwF6b/o= diff --git a/imap/client.go b/imap/client.go index 99bfa75..901cd3f 100644 --- a/imap/client.go +++ b/imap/client.go @@ -1,11 +1,11 @@ package imap import ( + "encoding/json" "errors" "github.com/1f349/lotus/imap/marshal" "github.com/emersion/go-imap" "github.com/emersion/go-imap/client" - "strconv" ) var imapStatusFlags = []imap.StatusItem{ @@ -22,7 +22,7 @@ type Client struct { var ErrInvalidArguments = errors.New("invalid arguments") -func (c *Client) HandleWS(action string, args []string) (map[string]any, error) { +func (c *Client) HandleWS(action string, args json.RawMessage) (map[string]any, error) { switch action { case "copy": // TODO: implementation @@ -31,41 +31,45 @@ func (c *Client) HandleWS(action string, args []string) (map[string]any, error) case "delete": // TODO: implementation case "list": - if len(args) != 2 { + var listArgs []string + err := json.Unmarshal(args, &listArgs) + if err != nil { + return nil, err + } + + if len(listArgs) != 2 { return nil, ErrInvalidArguments } // do list - list, err := c.list(args[0], args[1]) + list, err := c.list(listArgs[0], listArgs[1]) if err != nil { return nil, err } return map[string]any{"type": "list", "value": list}, nil case "fetch": - if len(args) != 4 { + var fetchArgs struct { + Sync uint64 `json:"sync"` + Path string `json:"path"` + Start uint32 `json:"start"` + End uint32 `json:"end"` + Limit uint32 `json:"limit"` + } + err := json.Unmarshal(args, &fetchArgs) + if err != nil { + return nil, err + } + + if fetchArgs.Sync == 0 || len(fetchArgs.Path) == 0 || fetchArgs.Start == 0 || fetchArgs.End == 0 || fetchArgs.Limit == 0 { return nil, ErrInvalidArguments } - // parse numeric parameters - arg1i, err := strconv.Atoi(args[1]) - if err != nil { - return nil, err - } - arg2i, err := strconv.Atoi(args[2]) - if err != nil { - return nil, err - } - arg3i, err := strconv.Atoi(args[3]) - if err != nil { - return nil, err - } - // do fetch - fetch, err := c.fetch(args[0], uint32(arg1i), uint32(arg2i), uint32(arg3i)) + fetch, err := c.fetch(fetchArgs.Path, fetchArgs.Start, fetchArgs.End, fetchArgs.Limit) if err != nil { return nil, err } - return map[string]any{"type": "fetch", "value": marshal.MessageSliceJson(fetch)}, nil + return map[string]any{"type": "fetch", "sync": 0, "value": marshal.MessageSliceJson(fetch)}, nil case "move": // TODO: implementation case "rename": diff --git a/sendmail/fake/fake-smtp.go b/sendmail/fake/fake-smtp.go deleted file mode 100644 index 49f6d95..0000000 --- a/sendmail/fake/fake-smtp.go +++ /dev/null @@ -1,45 +0,0 @@ -package fake - -import ( - "github.com/emersion/go-smtp" - "io" - "log" -) - -type SmtpBackend struct { - Debug chan []byte -} - -func (f *SmtpBackend) NewSession(c *smtp.Conn) (smtp.Session, error) { - return &SmtpSession{f.Debug}, nil -} - -type SmtpSession struct { - Debug chan []byte -} - -func (f *SmtpSession) Reset() {} - -func (f *SmtpSession) Logout() error { return nil } - -func (f *SmtpSession) AuthPlain(username, password string) error { return nil } - -func (f *SmtpSession) Mail(from string, opts *smtp.MailOptions) error { - log.Println("MAIL " + from) - f.Debug <- []byte("MAIL " + from + "\n") - return nil -} - -func (f *SmtpSession) Rcpt(to string) error { - f.Debug <- []byte("RCPT " + to + "\n") - return nil -} - -func (f *SmtpSession) Data(r io.Reader) error { - all, err := io.ReadAll(r) - if err != nil { - return err - } - f.Debug <- all - return nil -} diff --git a/sendmail/sendmail_test.go b/sendmail/sendmail_test.go index 49f60e0..6792801 100644 --- a/sendmail/sendmail_test.go +++ b/sendmail/sendmail_test.go @@ -5,8 +5,8 @@ import ( "github.com/emersion/go-message" "github.com/emersion/go-message/mail" "github.com/stretchr/testify/assert" + "io" "log" - "net" "os" "os/exec" "strings" @@ -34,34 +34,38 @@ func init() { sendTestMessage = out.Bytes() } -func TestSmtp_Send(t *testing.T) { +func TestSendMail_Send(t *testing.T) { execCommand = func(name string, arg ...string) *exec.Cmd { - cs := append([]string{"-test.run=TestHelperProcess", "--", name}, arg...) + log.Println("Hello") + cs := append([]string{"-test.run=TestSendMailHelperProcess", "--", name}, arg...) cmd := exec.Command(os.Args[0], cs...) cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} + log.Println(cmd.Path) return cmd } defer func() { execCommand = exec.Command }() m := &Mail{From: &mail.Address{Address: "test@localhost"}, Body: sendTestMessage} - temp, err := os.CreateTemp("", "sendmail") - if err != nil { - return - } - - addr, err := net.ResolveUnixAddr("") - assert.NoError(t, err) - listen, err := net.ListenUnix("", addr) - assert.NoError(t, err) - s := &SendMail{SendMailCommand: "/tmp/sendmailXXXXX"} assert.NoError(t, s.Send(m)) } -func TestSmtpHelperProcess(t *testing.T) { +func TestSendMailHelperProcess(t *testing.T) { if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { return } + log.Println("This is a test") + all, err := io.ReadAll(os.Stdin) + if err != nil { + panic(err) + } + assert.Equal(t, strings.ReplaceAll(`Mime-Version: 1.0 +Content-Type: text/plain; charset=utf-8 +To: "A" +From: "Test" +Subject: Happy Millennium +Date: Sat, 01 Jan 2000 00:00:00 +0000 +Thanks`, "\n", "\r\n"), string(all)) }