]> src.bluestatic.org Git - mailpopbox.git/blob - smtp/relay_test.go
Merge branch 'master' into outbound
[mailpopbox.git] / smtp / relay_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 smtp
8
9 import (
10 "bytes"
11 "net/mail"
12 "testing"
13
14 "github.com/uber-go/zap"
15 )
16
17 type deliveryServer struct {
18 testServer
19 messages []Envelope
20 }
21
22 func (s *deliveryServer) OnMessageDelivered(env Envelope) *ReplyLine {
23 s.messages = append(s.messages, env)
24 return nil
25 }
26
27 func TestRelayRoundTrip(t *testing.T) {
28 s := &deliveryServer{
29 testServer: testServer{domain: "receive.net"},
30 }
31 l := runServer(t, s)
32 defer l.Close()
33
34 env := Envelope{
35 MailFrom: mail.Address{Address: "from@sender.org"},
36 RcptTo: []mail.Address{{Address: "to@receive.net"}},
37 Data: []byte("~~~Message~~~\n"),
38 ID: "ididid",
39 }
40
41 relayMessageToHost(s, env, zap.New(zap.NullEncoder()), env.RcptTo[0].Address, l.Addr().String())
42
43 if len(s.messages) != 1 {
44 t.Errorf("Expected 1 message to be delivered, got %d", len(s.messages))
45 return
46 }
47
48 received := s.messages[0]
49
50 if env.MailFrom.Address != received.MailFrom.Address {
51 t.Errorf("Expected MailFrom %s, got %s", env.MailFrom.Address, received.MailFrom.Address)
52 }
53 if len(received.RcptTo) != 1 {
54 t.Errorf("Expected 1 RcptTo, got %d", len(received.RcptTo))
55 return
56 }
57 if env.RcptTo[0].Address != received.RcptTo[0].Address {
58 t.Errorf("Expected RcptTo %s, got %s", env.RcptTo[0].Address, received.RcptTo[0].Address)
59 }
60
61 if !bytes.HasSuffix(received.Data, env.Data) {
62 t.Errorf("Delivered message does not match relayed one. Delivered=%q Relayed=%q", string(env.Data), string(received.Data))
63 }
64 }