-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction_mailfrom_test.go
162 lines (152 loc) · 4.02 KB
/
transaction_mailfrom_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package msmtpd
import (
"fmt"
"net/smtp"
"strings"
"testing"
"github.com/vodolaz095/msmtpd/internal"
)
func TestLongLineInMailFrom(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = c.Mail(fmt.Sprintf("%[email protected]", strings.Repeat("x", 65*1024))); err == nil {
t.Errorf("MAIL failed: %v", err)
}
if err = c.Quit(); err != nil {
t.Errorf("Quit failed: %v", err)
}
}
func TestNoBracketsSender(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = internal.DoCommand(c.Text, 250, "HELO localhost"); err != nil {
t.Errorf("HELO failed: %v", err)
}
if err = internal.DoCommand(c.Text, 250, "MAIL FROM:[email protected]"); err != nil {
t.Errorf("MAIL without brackets failed: %v", err)
}
if err = c.Quit(); err != nil {
t.Errorf("Quit failed: %v", err)
}
}
func TestInvalidSender(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = c.Mail("invalid@@example.org"); err == nil {
t.Error("Unexpected MAIL success")
}
err = c.Close()
if err != nil {
t.Errorf("%s : while closing transaction", err)
}
}
func TestNullSender(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = internal.DoCommand(c.Text, 250, "HELO localhost"); err != nil {
t.Errorf("HELO failed: %v", err)
}
if err = internal.DoCommand(c.Text, 250, "MAIL FROM:<>"); err != nil {
t.Errorf("MAIL with null sender failed: %v", err)
}
if err = c.Quit(); err != nil {
t.Errorf("Quit failed: %v", err)
}
}
func TestSenderCheck(t *testing.T) {
sc := make([]SenderChecker, 0)
sc = append(sc, func(tr *Transaction) error {
if tr.MailFrom.Address != "[email protected]" {
t.Errorf("wrong sender %s", tr.MailFrom.String())
}
return ErrorSMTP{Code: 552, Message: "Denied"}
})
addr, closer := RunTestServerWithoutTLS(t, &Server{
SenderCheckers: sc,
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = c.Mail("[email protected]"); err == nil {
t.Error("Unexpected MAIL success")
}
if err = c.Quit(); err != nil {
t.Errorf("%s : while quiting", err)
}
}
func TestMailFromWithExtraSpace(t *testing.T) {
sc := make([]SenderChecker, 0)
sc = append(sc, func(tr *Transaction) error {
if tr.MailFrom.Address != "[email protected]" {
return ErrorSMTP{Code: 502, Message: "Denied"}
}
return nil
})
addr, closer := RunTestServerWithoutTLS(t, &Server{
SenderCheckers: sc,
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = c.Hello("localhost"); err != nil {
t.Errorf("HELO failed: %v", err)
}
err = internal.DoCommand(c.Text, 502, "MAIL")
if err != nil {
t.Errorf("%s : while sending malformed command", err)
}
if err = internal.DoCommand(c.Text, 250, "MAIL FROM: <[email protected]>"); err != nil {
t.Errorf("MAIL FROM failed with extra whitespace: %v", err)
}
if err = c.Quit(); err != nil {
t.Errorf("Quit failed: %v", err)
}
}
func TestRcptToBeforeMAIL(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = c.Rcpt("[email protected]"); err == nil {
t.Error("Unexpected RCPT success")
}
if err = c.Quit(); err != nil {
t.Errorf("%s : while quiting", err)
}
}
func TestDataBeforeMailFrom(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if _, err = c.Data(); err == nil {
t.Error("Data accepted despite no sender")
}
if err = c.Quit(); err != nil {
t.Errorf("QUIT failed: %v", err)
}
}