Bump the version to 2.1.0.
[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 "io/ioutil"
12 "net/mail"
13 "os"
14 "path/filepath"
15 "testing"
16
17 "go.uber.org/zap"
18
19 "src.bluestatic.org/mailpopbox/smtp"
20 )
21
22 func TestVerifyAddress(t *testing.T) {
23 dir, err := ioutil.TempDir("", "maildrop")
24 if err != nil {
25 t.Errorf("Failed to create temp dir: %v", err)
26 return
27 }
28 defer os.RemoveAll(dir)
29
30 s := smtpServer{
31 config: Config{
32 Hostname: "mx.example.com",
33 Servers: []Server{
34 {
35 Domain: "example.com",
36 MaildropPath: dir,
37 },
38 },
39 },
40 log: zap.NewNop(),
41 }
42
43 if s.VerifyAddress(mail.Address{Address: "example@example.com"}) != smtp.ReplyOK {
44 t.Errorf("Valid mailbox is not reported to be valid")
45 }
46 if s.VerifyAddress(mail.Address{Address: "mailbox@example.com"}) != smtp.ReplyOK {
47 t.Errorf("Valid mailbox is not reported to be valid")
48 }
49 if s.VerifyAddress(mail.Address{Address: "hello@other.net"}) == smtp.ReplyOK {
50 t.Errorf("Invalid mailbox reports to be valid")
51 }
52 if s.VerifyAddress(mail.Address{Address: "hello@mx.example.com"}) == smtp.ReplyOK {
53 t.Errorf("Invalid mailbox reports to be valid")
54 }
55 if s.VerifyAddress(mail.Address{Address: "unknown"}) == smtp.ReplyOK {
56 t.Errorf("Invalid mailbox reports to be valid")
57 }
58 }
59
60 func TestMessageDelivery(t *testing.T) {
61 dir, err := ioutil.TempDir("", "maildrop")
62 if err != nil {
63 t.Errorf("Failed to create temp dir: %v", err)
64 return
65 }
66 defer os.RemoveAll(dir)
67
68 s := smtpServer{
69 config: Config{
70 Hostname: "mx.example.com",
71 Servers: []Server{
72 {
73 Domain: "example.com",
74 MaildropPath: dir,
75 },
76 },
77 },
78 log: zap.NewNop(),
79 }
80
81 env := smtp.Envelope{
82 MailFrom: mail.Address{Address: "sender@mail.net"},
83 RcptTo: []mail.Address{{Address: "receive@example.com"}},
84 Data: []byte("Hello, world"),
85 ID: "msgid",
86 }
87
88 if rl := s.DeliverMessage(env); rl != nil {
89 t.Errorf("Failed to deliver message: %v", rl)
90 }
91
92 f, err := os.Open(filepath.Join(dir, "msgid.msg"))
93 if err != nil {
94 t.Errorf("Failed to open delivered message: %v", err)
95 }
96 defer f.Close()
97
98 data, err := ioutil.ReadAll(f)
99 if err != nil {
100 t.Errorf("Failed to read message: %v", err)
101 }
102
103 if !bytes.Contains(data, env.Data) {
104 t.Errorf("Could not find expected data in message")
105 }
106 }
107
108 func TestAuthenticate(t *testing.T) {
109 server := smtpServer{
110 config: Config{
111 Servers: []Server{
112 Server{
113 Domain: "domain1.net",
114 MailboxPassword: "d1",
115 },
116 Server{
117 Domain: "domain2.xyz",
118 MailboxPassword: "d2",
119 },
120 },
121 },
122 }
123
124 authTests := []struct {
125 authz, authc, passwd string
126 ok bool
127 }{
128 {"foo@domain1.net", "mailbox@domain1.net", "d1", true},
129 {"", "mailbox@domain1.net", "d1", true},
130 {"foo@domain2.xyz", "mailbox@domain1.xyz", "d1", false},
131 {"foo@domain2.xyz", "mailbox@domain1.xyz", "d2", false},
132 {"foo@domain2.xyz", "mailbox@domain2.xyz", "d2", true},
133 {"invalid", "mailbox@domain2.xyz", "d2", false},
134 {"", "mailbox@domain2.xyz", "d2", true},
135 {"", "", "", false},
136 }
137
138 for i, test := range authTests {
139 actual := server.Authenticate(test.authz, test.authc, test.passwd)
140 if actual != test.ok {
141 t.Errorf("Test %d, got %v, expected %v", i, actual, test.ok)
142 }
143 }
144 }