1
0
mirror of https://blitiri.com.ar/repos/chasquid synced 2025-12-21 15:17:01 +00:00
Files
go-chasquid-smtp/internal/courier/procmail_test.go
Alberto Bertogli aac2d3c061 Minor style and simplification cleanups
This patch does various minor style and simplification cleanups, fixing things
detected by tools such as go vet, gofmt -s, and golint.

There are no functional changes, this change is purely cosmetic, but will
enable us to run those tools more regularly now that their output is clean.
2016-09-12 04:06:56 +01:00

99 lines
2.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package courier
import (
"bytes"
"io/ioutil"
"os"
"testing"
"time"
)
func TestProcmail(t *testing.T) {
dir, err := ioutil.TempDir("", "test-chasquid-courier")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
defer os.RemoveAll(dir)
MailDeliveryAgentBin = "tee"
MailDeliveryAgentArgs = []string{dir + "/%user%"}
p := Procmail{}
err = p.Deliver("from@x", "to@local", []byte("data"))
if err != nil {
t.Fatalf("Deliver: %v", err)
}
data, err := ioutil.ReadFile(dir + "/to")
if err != nil || !bytes.Equal(data, []byte("data")) {
t.Errorf("Invalid data: %q - %v", string(data), err)
}
}
func TestProcmailTimeout(t *testing.T) {
MailDeliveryAgentBin = "/bin/sleep"
MailDeliveryAgentArgs = []string{"1"}
procmailTimeout = 100 * time.Millisecond
p := Procmail{}
err := p.Deliver("from", "to@local", []byte("data"))
if err != errTimeout {
t.Errorf("Unexpected error: %v", err)
}
procmailTimeout = 1 * time.Second
}
func TestProcmailBadCommandLine(t *testing.T) {
p := Procmail{}
// Non-existent binary.
MailDeliveryAgentBin = "thisdoesnotexist"
err := p.Deliver("from", "to", []byte("data"))
if err == nil {
t.Errorf("Unexpected success: %q %v",
MailDeliveryAgentBin, MailDeliveryAgentArgs)
}
// Incorrect arguments.
MailDeliveryAgentBin = "cat"
MailDeliveryAgentArgs = []string{"--fail_unknown_option"}
err = p.Deliver("from", "to", []byte("data"))
if err == nil {
t.Errorf("Unexpected success: %q %v",
MailDeliveryAgentBin, MailDeliveryAgentArgs)
}
}
func TestSanitize(t *testing.T) {
cases := []struct{ v, expected string }{
// These are the same.
{"thisisfine", "thisisfine"},
{"ñaca", "ñaca"},
{"123-456_789", "123-456_789"},
{"123+456~789", "123+456~789"},
// These have problematic characters that get dropped.
{"with spaces", "withspaces"},
{"with/slash", "withslash"},
{"quote';andsemicolon", "quoteandsemicolon"},
{"a;b", "ab"},
{`"test"`, "test"},
// Interesting cases taken from
// http://www.user.uni-hannover.de/nhtcapri/bidirectional-text.html
// We allow them, they're the same on both sides.
{"١٩٩٩–١٢–٣١", "١٩٩٩–١٢–٣١"},
{"موزه‌ها", "موزه\u200cها"},
}
for _, c := range cases {
out := sanitizeForProcmail(c.v)
if out != c.expected {
t.Errorf("%q: expected %q, got %q", c.v, c.expected, out)
}
}
}