-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransaction_rcpt_to_test.go
187 lines (179 loc) · 4.77 KB
/
transaction_rcpt_to_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package msmtpd
import (
"crypto/tls"
"net/mail"
"net/smtp"
"testing"
"github.com/vodolaz095/msmtpd/internal"
)
func TestRecipientCheck(t *testing.T) {
rc := make([]RecipientChecker, 0)
rc = append(rc, func(tr *Transaction, name *mail.Address) error {
return ErrorSMTP{Code: 552, Message: "Denied"}
})
addr, closer := RunTestServerWithoutTLS(t, &Server{
RecipientCheckers: rc,
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = c.Mail("sender@example.org"); err != nil {
t.Errorf("Mail failed: %v", err)
}
if err = c.Rcpt("recipient@example.net"); err == nil {
t.Error("Unexpected RCPT success")
}
err = c.Close()
if err != nil {
t.Errorf("%s : while closing transaction", err)
}
}
func TestMaxRecipients(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{
MaxRecipients: 1,
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = c.Mail("sender@example.org"); err != nil {
t.Errorf("MAIL failed: %v", err)
}
if err = c.Rcpt("recipient@example.net"); err != nil {
t.Errorf("RCPT failed: %v", err)
}
if err = c.Rcpt("recipient@example.net"); err == nil {
t.Error("RCPT succeeded despite MaxRecipients = 1")
}
if err = c.Quit(); err != nil {
t.Errorf("QUIT failed: %v", err)
}
}
func TestInvalidRecipient(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("sender@example.org"); err != nil {
t.Errorf("Mail failed: %v", err)
}
if err = c.Rcpt("invalid@@example.org"); err == nil {
t.Error("Unexpected RCPT success")
}
err = c.Close()
if err != nil {
t.Errorf("%s : while closing", err)
}
}
func TestMalformedRcpt(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("sender@example.org"); err != nil {
t.Errorf("Mail failed: %v", err)
}
err = internal.DoCommand(c.Text, 502, "RCPT")
if err != nil {
t.Errorf("%s : while making malformed rcpt to", err)
}
err = internal.DoCommand(c.Text, 502, "RCPT FOR")
if err != nil {
t.Errorf("%s : while making malformed rcpt to", err)
}
err = c.Close()
if err != nil {
t.Errorf("%s : while closing", err)
}
}
func TestRCPTinWrongOrder(t *testing.T) {
addr, closer := RunTestServerWithTLS(t, &Server{
ForceTLS: true,
Authenticator: AuthenticatorForTestsThatAlwaysWorks,
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
err = c.Rcpt("bill.gates@microsoft.com")
if err != nil {
if err.Error() != "502 Please introduce yourself first." {
t.Errorf("%s : wrong error while helo not called", err)
}
} else {
t.Error("error not thrown when DATA called before HELO")
}
err = c.Hello("localhost")
if err != nil {
t.Errorf("%s : while sending HELO", err)
}
err = c.Rcpt("bill.gates@microsoft.com")
if err != nil {
if err.Error() != "502 Please turn on TLS by issuing a STARTTLS command." {
t.Errorf("%s : wrong error while STARTTLS not called", err)
}
} else {
t.Error("error not thrown when DATA called before STARTTLS")
}
err = c.StartTLS(&tls.Config{InsecureSkipVerify: true})
if err != nil {
t.Errorf("%s : while sending STARTTLS", err)
}
err = c.Rcpt("bill.gates@microsoft.com")
if err != nil {
if err.Error() != "530 Authentication Required." {
t.Errorf("%s : wrong error while STARTTLS not called", err)
}
} else {
t.Error("error not thrown when DATA called before AUTH")
}
err = c.Auth(smtp.PlainAuth("", "who", "cares", "127.0.0.1"))
if err != nil {
t.Errorf("%s : while sending AUTH", err)
}
err = c.Rcpt("bill.gates@microsoft.com")
if err != nil {
if err.Error() != "502 It seems you haven't called MAIL FROM in order to explain who sends your message." {
t.Errorf("%s : wrong error while MAIL FROM not called", err)
}
} else {
t.Error("error not thrown when DATA called before MAIL FROM")
}
err = c.Mail("somebody@example.org")
if err != nil {
t.Errorf("%s : while sending MAILFROM", err)
}
err = c.Rcpt("bill.gates@microsoft.com")
if err != nil {
t.Errorf("%s : while sending RCPT TO", err)
}
err = c.Close()
if err != nil {
t.Errorf("%s : while closing", err)
}
}
func TestDataCalledBeforeRCPT(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("sender@example.org"); err != nil {
t.Errorf("MAIL failed: %v", err)
}
if _, err = c.Data(); err == nil {
t.Error("Data accepted despite no recipients")
}
if err = c.Quit(); err != nil {
t.Errorf("QUIT failed: %v", err)
}
}