Compare commits

...

2 commits

Author SHA1 Message Date
eebbaa2513
Refactor and reintegrate content type tests in file_test.go
Reintegrates previously commented out tests for file content types, ensuring they are part of a structured t.Run block. Enhances readability and maintains functionality by checking the presence and correctness of attachments in a more concise manner.
2024-10-25 09:40:15 +02:00
8353b4b255
Follow upstream for HELO during Quit bug
I reported the bug I fixed in 74fa3f6f62 to Go upstream. They fixed simpler by just ignoring the error (See: https://go.dev/cl/622476). We follow this patch accordingly. The upstream test has been adopted as well.
2024-10-25 09:33:45 +02:00
3 changed files with 62 additions and 49 deletions

View file

@ -155,39 +155,34 @@ func TestFile(t *testing.T) {
}) })
} }
}) })
} t.Run("WithFileContentType", func(t *testing.T) {
/*
// TestFile_WithFileContentType tests the WithFileContentType option
func TestFile_WithFileContentType(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
ct ContentType contentType ContentType
want string
}{ }{
{"File content-type: text/plain", TypeTextPlain, "text/plain"}, {"File content-type: text/plain", TypeTextPlain},
{"File content-type: html/html", TypeTextHTML, "text/html"}, {"File content-type: html/html", TypeTextHTML},
{"File content-type: application/octet-stream", TypeAppOctetStream, "application/octet-stream"}, {"File content-type: application/octet-stream", TypeAppOctetStream},
{"File content-type: application/pgp-encrypted", TypePGPEncrypted, "application/pgp-encrypted"}, {"File content-type: application/pgp-encrypted", TypePGPEncrypted},
{"File content-type: application/pgp-signature", TypePGPSignature, "application/pgp-signature"}, {"File content-type: application/pgp-signature", TypePGPSignature},
} }
for _, tt := range tests { for _, tt := range tests {
m := NewMsg()
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
m.AttachFile("file.go", WithFileContentType(tt.ct)) message := NewMsg()
al := m.GetAttachments() message.AttachFile("file.go", WithFileContentType(tt.contentType))
if len(al) <= 0 { attachments := message.GetAttachments()
t.Errorf("AttachFile() failed. Attachment list is empty") if len(attachments) <= 0 {
t.Fatalf("failed to retrieve attachments list")
} }
a := al[0] firstAttachment := attachments[0]
if a.ContentType != ContentType(tt.want) { if firstAttachment == nil {
t.Errorf("WithFileContentType() failed. Expected: %s, got: %s", tt.want, a.ContentType) t.Fatalf("failed to retrieve first attachment, got nil")
}
if firstAttachment.ContentType != tt.contentType {
t.Errorf("WithFileContentType() failed. Expected: %s, got: %s", tt.contentType,
firstAttachment.ContentType)
} }
}) })
} }
})
} }
*/

View file

@ -554,20 +554,9 @@ func (c *Client) Noop() error {
// Quit sends the QUIT command and closes the connection to the server. // Quit sends the QUIT command and closes the connection to the server.
func (c *Client) Quit() error { func (c *Client) Quit() error {
// If we already tried to send a EHLO/HELO but it failed, we still need to be able to send // See https://github.com/golang/go/issues/70011
// a QUIT to close the connection. _ = c.hello() // ignore error; we're quitting anyhow
// c.hello() will return the global helloErr of the Client, which will always be set if the HELO
// failed before. Therefore if we already sent a HELO and the error is not nil, we skip another
// EHLO/HELO try
c.mutex.RLock()
didHello := c.didHello
helloErr := c.helloError
c.mutex.RUnlock()
if !didHello || helloErr == nil {
if err := c.hello(); err != nil {
return err
}
}
_, _, err := c.cmd(221, "QUIT") _, _, err := c.cmd(221, "QUIT")
if err != nil { if err != nil {
return err return err

View file

@ -900,6 +900,35 @@ Goodbye.
QUIT QUIT
` `
func TestHELOFailed(t *testing.T) {
serverLines := `502 EH?
502 EH?
221 OK
`
clientLines := `EHLO localhost
HELO localhost
QUIT
`
server := strings.Join(strings.Split(serverLines, "\n"), "\r\n")
client := strings.Join(strings.Split(clientLines, "\n"), "\r\n")
var cmdbuf strings.Builder
bcmdbuf := bufio.NewWriter(&cmdbuf)
var fake faker
fake.ReadWriter = bufio.NewReadWriter(bufio.NewReader(strings.NewReader(server)), bcmdbuf)
c := &Client{Text: textproto.NewConn(fake), localName: "localhost"}
if err := c.Hello("localhost"); err == nil {
t.Fatal("expected EHLO to fail")
}
if err := c.Quit(); err != nil {
t.Errorf("QUIT failed: %s", err)
}
_ = bcmdbuf.Flush()
actual := cmdbuf.String()
if client != actual {
t.Errorf("Got:\n%s\nWant:\n%s", actual, client)
}
}
func TestExtensions(t *testing.T) { func TestExtensions(t *testing.T) {
fake := func(server string) (c *Client, bcmdbuf *bufio.Writer, cmdbuf *strings.Builder) { fake := func(server string) (c *Client, bcmdbuf *bufio.Writer, cmdbuf *strings.Builder) {
server = strings.Join(strings.Split(server, "\n"), "\r\n") server = strings.Join(strings.Split(server, "\n"), "\r\n")