-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction_startTLS_test.go
77 lines (74 loc) · 2.16 KB
/
transaction_startTLS_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package msmtpd
import (
"crypto/tls"
"fmt"
"net/smtp"
"strings"
"testing"
"github.com/vodolaz095/msmtpd/internal"
)
func TestSTARTTLS(t *testing.T) {
addr, closer := RunTestServerWithTLS(t, &Server{
Authenticator: AuthenticatorForTestsThatAlwaysWorks,
ForceTLS: true,
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if supported, _ := c.Extension("AUTH"); supported {
t.Error("AUTH supported before TLS")
}
if err = c.Mail("[email protected]"); err == nil {
t.Error("Mail worked before TLS with ForceTLS")
}
if err = internal.DoCommand(c.Text, 220, "STARTTLS"); err != nil {
t.Errorf("STARTTLS failed: %v", err)
}
if err = internal.DoCommand(c.Text, 250, "foobar"); err == nil {
t.Error("STARTTLS didn't fail with invalid handshake")
}
if err = c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
t.Errorf("STARTTLS failed: %v", err)
}
if err = c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err == nil {
t.Error("STARTTLS worked twice")
}
if supported, _ := c.Extension("AUTH"); !supported {
t.Error("AUTH not supported after TLS")
}
if _, mechs := c.Extension("AUTH"); !strings.Contains(mechs, "PLAIN") {
t.Error("PLAIN AUTH not supported after TLS")
}
if _, mechs := c.Extension("AUTH"); !strings.Contains(mechs, "LOGIN") {
t.Error("LOGIN AUTH not supported after TLS")
}
if err = c.Auth(smtp.PlainAuth("foo", "foo", "bar", "127.0.0.1")); err != nil {
t.Errorf("Auth failed: %v", err)
}
if err = c.Mail("[email protected]"); err != nil {
t.Errorf("Mail failed: %v", err)
}
if err = c.Rcpt("[email protected]"); err != nil {
t.Errorf("Rcpt failed: %v", err)
}
if err = c.Rcpt("[email protected]"); err != nil {
t.Errorf("Rcpt2 failed: %v", err)
}
wc, err := c.Data()
if err != nil {
t.Errorf("Data failed: %v", err)
}
_, err = fmt.Fprintf(wc, internal.MakeTestMessage("[email protected]", "[email protected]", "[email protected]"))
if err != nil {
t.Errorf("Data body failed: %v", err)
}
err = wc.Close()
if err != nil {
t.Errorf("Data close failed: %v", err)
}
if err = c.Quit(); err != nil {
t.Errorf("Quit failed: %v", err)
}
}