]>
src.bluestatic.org Git - mailpopbox.git/blob - smtp_test.go
2 // Copyright 2020 Blue Static <https://www.bluestatic.org>
3 // This program is free software licensed under the GNU General Public License,
4 // version 3.0. The full text of the license can be found in LICENSE.txt.
5 // SPDX-License-Identifier: GPL-3.0-only
21 "src.bluestatic.org/mailpopbox/smtp"
24 func TestVerifyAddress(t
*testing
.T
) {
25 dir
, err
:= ioutil
.TempDir("", "maildrop")
27 t
.Errorf("Failed to create temp dir: %v", err
)
30 defer os
.RemoveAll(dir
)
34 Hostname
: "mx.example.com",
37 Domain
: "example.com",
39 BlockedAddresses
: []string{
40 "blocked@example.com",
48 if s
.VerifyAddress(mail
.Address
{Address
: "example@example.com"}) != smtp
.ReplyOK
{
49 t
.Errorf("Valid mailbox is not reported to be valid")
51 if s
.VerifyAddress(mail
.Address
{Address
: "mailbox@example.com"}) != smtp
.ReplyOK
{
52 t
.Errorf("Valid mailbox is not reported to be valid")
54 if s
.VerifyAddress(mail
.Address
{Address
: "hello@other.net"}) == smtp
.ReplyOK
{
55 t
.Errorf("Invalid mailbox reports to be valid")
57 if s
.VerifyAddress(mail
.Address
{Address
: "hello@mx.example.com"}) == smtp
.ReplyOK
{
58 t
.Errorf("Invalid mailbox reports to be valid")
60 if s
.VerifyAddress(mail
.Address
{Address
: "unknown"}) == smtp
.ReplyOK
{
61 t
.Errorf("Invalid mailbox reports to be valid")
63 if s
.VerifyAddress(mail
.Address
{Address
: "blocked@example.com"}) == smtp
.ReplyOK
{
64 t
.Errorf("Blocked mailbox reports to be valid")
68 func TestMessageDelivery(t
*testing
.T
) {
69 dir
, err
:= ioutil
.TempDir("", "maildrop")
71 t
.Errorf("Failed to create temp dir: %v", err
)
74 defer os
.RemoveAll(dir
)
78 Hostname
: "mx.example.com",
81 Domain
: "example.com",
90 MailFrom
: mail
.Address
{Address
: "sender@mail.net"},
91 RcptTo
: []mail
.Address
{{Address
: "receive@example.com"}},
92 Data
: []byte("Hello, world"),
96 if rl
:= s
.DeliverMessage(env
); rl
!= nil {
97 t
.Errorf("Failed to deliver message: %v", rl
)
100 f
, err
:= os
.Open(filepath
.Join(dir
, "msgid.msg"))
102 t
.Errorf("Failed to open delivered message: %v", err
)
106 data
, err
:= ioutil
.ReadAll(f
)
108 t
.Errorf("Failed to read message: %v", err
)
111 if !bytes
.Contains(data
, env
.Data
) {
112 t
.Errorf("Could not find expected data in message")
116 func TestAuthenticate(t
*testing
.T
) {
117 server
:= smtpServer
{
121 Domain
: "domain1.net",
122 MailboxPassword
: "d1",
125 Domain
: "domain2.xyz",
126 MailboxPassword
: "d2",
132 authTests
:= []struct {
133 authz
, authc
, passwd
string
136 {"foo@domain1.net", "mailbox@domain1.net", "d1", true},
137 {"", "mailbox@domain1.net", "d1", true},
138 {"foo@domain2.xyz", "mailbox@domain1.xyz", "d1", false},
139 {"foo@domain2.xyz", "mailbox@domain1.xyz", "d2", false},
140 {"foo@domain2.xyz", "mailbox@domain2.xyz", "d2", true},
141 {"invalid", "mailbox@domain2.xyz", "d2", false},
142 {"", "mailbox@domain2.xyz", "d2", true},
146 for i
, test
:= range authTests
{
147 actual
:= server
.Authenticate(test
.authz
, test
.authc
, test
.passwd
)
148 if actual
!= test
.ok
{
149 t
.Errorf("Test %d, got %v, expected %v", i
, actual
, test
.ok
)
154 type testMTA
struct {
155 relayed
chan smtp
.Envelope
158 func (m
*testMTA
) RelayMessage(en smtp
.Envelope
) {
162 func newTestMTA() *testMTA
{
164 relayed
: make(chan smtp
.Envelope
),
168 func TestBasicRelay(t
*testing
.T
) {
170 server
:= smtpServer
{
175 buf
:= new(bytes
.Buffer
)
176 fmt
.Fprintln(buf
, "From: <mailbox@example.com>\r")
177 fmt
.Fprintln(buf
, "To: <dest@another.net>\r")
178 fmt
.Fprintf(buf
, "Subject: Basic relay\n\n")
179 fmt
.Fprintln(buf
, "This is a basic relay message")
182 MailFrom
: mail
.Address
{Address
: "mailbox@example.com"},
183 RcptTo
: []mail
.Address
{{Address
: "dest@another.com"}},
188 server
.RelayMessage(en
, en
.MailFrom
.Address
)
190 relayed
:= <-mta
.relayed
192 if !bytes
.Equal(relayed
.Data
, en
.Data
) {
193 t
.Errorf("Relayed message data does not match")
197 func TestSendAsRelay(t
*testing
.T
) {
199 server
:= smtpServer
{
204 buf
:= new(bytes
.Buffer
)
205 fmt
.Fprintln(buf
, "Received: msg from wherever")
206 fmt
.Fprintln(buf
, "From: <mailbox@example.com>")
207 fmt
.Fprintln(buf
, "To: <valid@dest.xyz>")
208 fmt
.Fprintf(buf
, "Subject: Send-as relay [sendas:source]\n\n")
209 fmt
.Fprintln(buf
, "We've switched the senders!")
212 MailFrom
: mail
.Address
{Address
: "mailbox@example.com"},
213 RcptTo
: []mail
.Address
{{Address
: "valid@dest.xyz"}},
218 server
.RelayMessage(en
, en
.MailFrom
.Address
)
220 relayed
:= <-mta
.relayed
222 replaced
:= "source@example.com"
223 original
:= "mailbox@example.com"
225 if want
, got
:= replaced
, relayed
.MailFrom
.Address
; want
!= got
{
226 t
.Errorf("Want mail to be from %q, got %q", want
, got
)
229 if want
, got
:= 1, len(relayed
.RcptTo
); want
!= got
{
230 t
.Errorf("Want %d recipient, got %d", want
, got
)
232 if want
, got
:= "valid@dest.xyz", relayed
.RcptTo
[0].Address
; want
!= got
{
233 t
.Errorf("Unexpected RcptTo %q", got
)
236 msg
:= string(relayed
.Data
)
238 if strings
.Index(msg
, original
) != -1 {
239 t
.Errorf("Should not find %q in message %q", original
, msg
)
242 if strings
.Index(msg
, "\nFrom: <source@example.com>\n") == -1 {
243 t
.Errorf("Could not find From: header in message %q", msg
)
246 if strings
.Index(msg
, "\nSubject: Send-as relay \n") == -1 {
247 t
.Errorf("Could not find modified Subject: header in message %q", msg
)