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
19 "src.bluestatic.org/mailpopbox/smtp"
22 func TestVerifyAddress(t *testing.T) {
23 dir, err := ioutil.TempDir("", "maildrop")
25 t.Errorf("Failed to create temp dir: %v", err)
28 defer os.RemoveAll(dir)
32 Hostname: "mx.example.com",
35 Domain: "example.com",
43 if s.VerifyAddress(mail.Address{Address: "example@example.com"}) != smtp.ReplyOK {
44 t.Errorf("Valid mailbox is not reported to be valid")
46 if s.VerifyAddress(mail.Address{Address: "mailbox@example.com"}) != smtp.ReplyOK {
47 t.Errorf("Valid mailbox is not reported to be valid")
49 if s.VerifyAddress(mail.Address{Address: "hello@other.net"}) == smtp.ReplyOK {
50 t.Errorf("Invalid mailbox reports to be valid")
52 if s.VerifyAddress(mail.Address{Address: "hello@mx.example.com"}) == smtp.ReplyOK {
53 t.Errorf("Invalid mailbox reports to be valid")
55 if s.VerifyAddress(mail.Address{Address: "unknown"}) == smtp.ReplyOK {
56 t.Errorf("Invalid mailbox reports to be valid")
60 func TestMessageDelivery(t *testing.T) {
61 dir, err := ioutil.TempDir("", "maildrop")
63 t.Errorf("Failed to create temp dir: %v", err)
66 defer os.RemoveAll(dir)
70 Hostname: "mx.example.com",
73 Domain: "example.com",
82 MailFrom: mail.Address{Address: "sender@mail.net"},
83 RcptTo: []mail.Address{{Address: "receive@example.com"}},
84 Data: []byte("Hello, world"),
88 if rl := s.DeliverMessage(env); rl != nil {
89 t.Errorf("Failed to deliver message: %v", rl)
92 f, err := os.Open(filepath.Join(dir, "msgid.msg"))
94 t.Errorf("Failed to open delivered message: %v", err)
98 data, err := ioutil.ReadAll(f)
100 t.Errorf("Failed to read message: %v", err)
103 if !bytes.Contains(data, env.Data) {
104 t.Errorf("Could not find expected data in message")
108 func TestAuthenticate(t *testing.T) {
109 server := smtpServer{
113 Domain: "domain1.net",
114 MailboxPassword: "d1",
117 Domain: "domain2.xyz",
118 MailboxPassword: "d2",
124 authTests := []struct {
125 authz, authc, passwd string
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},
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)