Move send-as handling out of the smtp package and into the core server.
[mailpopbox.git] / smtp_test.go
1 // mailpopbox
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
6
7 package main
8
9 import (
10 "bytes"
11 "fmt"
12 "io/ioutil"
13 "net/mail"
14 "os"
15 "path/filepath"
16 "strings"
17 "testing"
18
19 "go.uber.org/zap"
20
21 "src.bluestatic.org/mailpopbox/smtp"
22 )
23
24 func TestVerifyAddress(t *testing.T) {
25 dir, err := ioutil.TempDir("", "maildrop")
26 if err != nil {
27 t.Errorf("Failed to create temp dir: %v", err)
28 return
29 }
30 defer os.RemoveAll(dir)
31
32 s := smtpServer{
33 config: Config{
34 Hostname: "mx.example.com",
35 Servers: []Server{
36 {
37 Domain: "example.com",
38 MaildropPath: dir,
39 },
40 },
41 },
42 log: zap.NewNop(),
43 }
44
45 if s.VerifyAddress(mail.Address{Address: "example@example.com"}) != smtp.ReplyOK {
46 t.Errorf("Valid mailbox is not reported to be valid")
47 }
48 if s.VerifyAddress(mail.Address{Address: "mailbox@example.com"}) != smtp.ReplyOK {
49 t.Errorf("Valid mailbox is not reported to be valid")
50 }
51 if s.VerifyAddress(mail.Address{Address: "hello@other.net"}) == smtp.ReplyOK {
52 t.Errorf("Invalid mailbox reports to be valid")
53 }
54 if s.VerifyAddress(mail.Address{Address: "hello@mx.example.com"}) == smtp.ReplyOK {
55 t.Errorf("Invalid mailbox reports to be valid")
56 }
57 if s.VerifyAddress(mail.Address{Address: "unknown"}) == smtp.ReplyOK {
58 t.Errorf("Invalid mailbox reports to be valid")
59 }
60 }
61
62 func TestMessageDelivery(t *testing.T) {
63 dir, err := ioutil.TempDir("", "maildrop")
64 if err != nil {
65 t.Errorf("Failed to create temp dir: %v", err)
66 return
67 }
68 defer os.RemoveAll(dir)
69
70 s := smtpServer{
71 config: Config{
72 Hostname: "mx.example.com",
73 Servers: []Server{
74 {
75 Domain: "example.com",
76 MaildropPath: dir,
77 },
78 },
79 },
80 log: zap.NewNop(),
81 }
82
83 env := smtp.Envelope{
84 MailFrom: mail.Address{Address: "sender@mail.net"},
85 RcptTo: []mail.Address{{Address: "receive@example.com"}},
86 Data: []byte("Hello, world"),
87 ID: "msgid",
88 }
89
90 if rl := s.DeliverMessage(env); rl != nil {
91 t.Errorf("Failed to deliver message: %v", rl)
92 }
93
94 f, err := os.Open(filepath.Join(dir, "msgid.msg"))
95 if err != nil {
96 t.Errorf("Failed to open delivered message: %v", err)
97 }
98 defer f.Close()
99
100 data, err := ioutil.ReadAll(f)
101 if err != nil {
102 t.Errorf("Failed to read message: %v", err)
103 }
104
105 if !bytes.Contains(data, env.Data) {
106 t.Errorf("Could not find expected data in message")
107 }
108 }
109
110 func TestAuthenticate(t *testing.T) {
111 server := smtpServer{
112 config: Config{
113 Servers: []Server{
114 Server{
115 Domain: "domain1.net",
116 MailboxPassword: "d1",
117 },
118 Server{
119 Domain: "domain2.xyz",
120 MailboxPassword: "d2",
121 },
122 },
123 },
124 }
125
126 authTests := []struct {
127 authz, authc, passwd string
128 ok bool
129 }{
130 {"foo@domain1.net", "mailbox@domain1.net", "d1", true},
131 {"", "mailbox@domain1.net", "d1", true},
132 {"foo@domain2.xyz", "mailbox@domain1.xyz", "d1", false},
133 {"foo@domain2.xyz", "mailbox@domain1.xyz", "d2", false},
134 {"foo@domain2.xyz", "mailbox@domain2.xyz", "d2", true},
135 {"invalid", "mailbox@domain2.xyz", "d2", false},
136 {"", "mailbox@domain2.xyz", "d2", true},
137 {"", "", "", false},
138 }
139
140 for i, test := range authTests {
141 actual := server.Authenticate(test.authz, test.authc, test.passwd)
142 if actual != test.ok {
143 t.Errorf("Test %d, got %v, expected %v", i, actual, test.ok)
144 }
145 }
146 }
147
148 type testMTA struct {
149 relayed chan smtp.Envelope
150 }
151
152 func (m *testMTA) RelayMessage(en smtp.Envelope) {
153 m.relayed <- en
154 }
155
156 func newTestMTA() *testMTA {
157 return &testMTA{
158 relayed: make(chan smtp.Envelope),
159 }
160 }
161
162 func TestBasicRelay(t *testing.T) {
163 mta := newTestMTA()
164 server := smtpServer{
165 mta: mta,
166 log: zap.NewNop(),
167 }
168
169 buf := new(bytes.Buffer)
170 fmt.Fprintln(buf, "From: <mailbox@example.com>\r")
171 fmt.Fprintln(buf, "To: <dest@another.net>\r")
172 fmt.Fprintf(buf, "Subject: Basic relay\n\n")
173 fmt.Fprintln(buf, "This is a basic relay message")
174
175 en := smtp.Envelope{
176 MailFrom: mail.Address{Address: "mailbox@example.com"},
177 RcptTo: []mail.Address{{Address: "dest@another.com"}},
178 Data: buf.Bytes(),
179 ID: "id1",
180 }
181
182 server.RelayMessage(en, en.MailFrom.Address)
183
184 relayed := <-mta.relayed
185
186 if !bytes.Equal(relayed.Data, en.Data) {
187 t.Errorf("Relayed message data does not match")
188 }
189 }
190
191 func TestSendAsRelay(t *testing.T) {
192 mta := newTestMTA()
193 server := smtpServer{
194 mta: mta,
195 log: zap.NewNop(),
196 }
197
198 buf := new(bytes.Buffer)
199 fmt.Fprintln(buf, "Received: msg from wherever")
200 fmt.Fprintln(buf, "From: <mailbox@example.com>")
201 fmt.Fprintln(buf, "To: <valid@dest.xyz>")
202 fmt.Fprintf(buf, "Subject: Send-as relay [sendas:source]\n\n")
203 fmt.Fprintln(buf, "We've switched the senders!")
204
205 en := smtp.Envelope{
206 MailFrom: mail.Address{Address: "mailbox@example.com"},
207 RcptTo: []mail.Address{{Address: "valid@dest.xyz"}},
208 Data: buf.Bytes(),
209 ID: "id1",
210 }
211
212 server.RelayMessage(en, en.MailFrom.Address)
213
214 relayed := <-mta.relayed
215
216 replaced := "source@example.com"
217 original := "mailbox@example.com"
218
219 if want, got := replaced, relayed.MailFrom.Address; want != got {
220 t.Errorf("Want mail to be from %q, got %q", want, got)
221 }
222
223 if want, got := 1, len(relayed.RcptTo); want != got {
224 t.Errorf("Want %d recipient, got %d", want, got)
225 }
226 if want, got := "valid@dest.xyz", relayed.RcptTo[0].Address; want != got {
227 t.Errorf("Unexpected RcptTo %q", got)
228 }
229
230 msg := string(relayed.Data)
231
232 if strings.Index(msg, original) != -1 {
233 t.Errorf("Should not find %q in message %q", original, msg)
234 }
235
236 if strings.Index(msg, "\nFrom: <source@example.com>\n") == -1 {
237 t.Errorf("Could not find From: header in message %q", msg)
238 }
239
240 if strings.Index(msg, "\nSubject: Send-as relay \n") == -1 {
241 t.Errorf("Could not find modified Subject: header in message %q", msg)
242 }
243 }