maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

  1//go:build integration && cgo && !nosqlite3
  2// +build integration,cgo,!nosqlite3
  3
  4/*
  5Maddy Mail Server - Composable all-in-one email server.
  6Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
  7
  8This program is free software: you can redistribute it and/or modify
  9it under the terms of the GNU General Public License as published by
 10the Free Software Foundation, either version 3 of the License, or
 11(at your option) any later version.
 12
 13This program is distributed in the hope that it will be useful,
 14but WITHOUT ANY WARRANTY; without even the implied warranty of
 15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16GNU General Public License for more details.
 17
 18You should have received a copy of the GNU General Public License
 19along with this program.  If not, see <https://www.gnu.org/licenses/>.
 20*/
 21
 22package tests_test
 23
 24import (
 25	"testing"
 26	"time"
 27
 28	"github.com/foxcpp/maddy/tests"
 29)
 30
 31// Smoke test to ensure message delivery is handled correctly.
 32
 33func TestImapsqlDelivery(tt *testing.T) {
 34	tt.Parallel()
 35	t := tests.NewT(tt)
 36
 37	t.DNS(nil)
 38	t.Port("imap")
 39	t.Port("smtp")
 40	t.Config(`
 41		storage.imapsql test_store {
 42			driver sqlite3
 43			dsn imapsql.db
 44		}
 45
 46		imap tcp://127.0.0.1:{env:TEST_PORT_imap} {
 47			tls off
 48
 49			auth dummy
 50			storage &test_store
 51		}
 52
 53		smtp tcp://127.0.0.1:{env:TEST_PORT_smtp} {
 54			hostname maddy.test
 55			tls off
 56
 57			deliver_to &test_store
 58		}
 59	`)
 60	t.Run(2)
 61	defer t.Close()
 62
 63	imapConn := t.Conn("imap")
 64	defer imapConn.Close()
 65	imapConn.ExpectPattern(`\* OK *`)
 66	imapConn.Writeln(". LOGIN testusr@maddy.test 1234")
 67	imapConn.ExpectPattern(". OK *")
 68	imapConn.Writeln(". SELECT INBOX")
 69	imapConn.ExpectPattern(`\* *`)
 70	imapConn.ExpectPattern(`\* *`)
 71	imapConn.ExpectPattern(`\* *`)
 72	imapConn.ExpectPattern(`\* *`)
 73	imapConn.ExpectPattern(`\* *`)
 74	imapConn.ExpectPattern(`\* *`)
 75	imapConn.ExpectPattern(`. OK *`)
 76
 77	smtpConn := t.Conn("smtp")
 78	defer smtpConn.Close()
 79	smtpConn.SMTPNegotation("localhost", nil, nil)
 80	smtpConn.Writeln("MAIL FROM:<sender@maddy.test>")
 81	smtpConn.ExpectPattern("2*")
 82	smtpConn.Writeln("RCPT TO:<testusr@maddy.test>")
 83	smtpConn.ExpectPattern("2*")
 84	smtpConn.Writeln("DATA")
 85	smtpConn.ExpectPattern("354 *")
 86	smtpConn.Writeln("From: <sender@maddy.test>")
 87	smtpConn.Writeln("To: <testusr@maddy.test>")
 88	smtpConn.Writeln("Subject: Hi!")
 89	smtpConn.Writeln("")
 90	smtpConn.Writeln("Hi!")
 91	smtpConn.Writeln(".")
 92	smtpConn.ExpectPattern("2*")
 93
 94	time.Sleep(500 * time.Millisecond)
 95
 96	imapConn.Writeln(". NOOP")
 97	imapConn.ExpectPattern(`\* 1 EXISTS`)
 98	imapConn.ExpectPattern(`\* 1 RECENT`)
 99	imapConn.ExpectPattern(". OK *")
100
101	imapConn.Writeln(". FETCH 1 (BODY.PEEK[])")
102	imapConn.ExpectPattern(`\* 1 FETCH (BODY\[\] {*}*`)
103	imapConn.Expect(`Delivered-To: testusr@maddy.test`)
104	imapConn.Expect(`Return-Path: <sender@maddy.test>`)
105	imapConn.ExpectPattern(`Received: from localhost (client.maddy.test \[` + tests.DefaultSourceIP.String() + `\]) by maddy.test`)
106	imapConn.ExpectPattern(` (envelope-sender <sender@maddy.test>) with ESMTP id *; *`)
107	imapConn.ExpectPattern(` *`)
108	imapConn.Expect("From: <sender@maddy.test>")
109	imapConn.Expect("To: <testusr@maddy.test>")
110	imapConn.Expect("Subject: Hi!")
111	imapConn.Expect("")
112	imapConn.Expect("Hi!")
113	imapConn.Expect(")")
114	imapConn.ExpectPattern(`. OK *`)
115}
116
117func TestImapsqlDeliveryMap(tt *testing.T) {
118	tt.Parallel()
119	t := tests.NewT(tt)
120
121	t.DNS(nil)
122	t.Port("imap")
123	t.Port("smtp")
124	t.Config(`
125		storage.imapsql test_store {
126			delivery_map email_localpart
127			auth_normalize precis
128
129			driver sqlite3
130			dsn imapsql.db
131		}
132
133		imap tcp://127.0.0.1:{env:TEST_PORT_imap} {
134			tls off
135
136			auth dummy
137			storage &test_store
138		}
139
140		smtp tcp://127.0.0.1:{env:TEST_PORT_smtp} {
141			hostname maddy.test
142			tls off
143
144			deliver_to &test_store
145		}
146	`)
147	t.Run(2)
148	defer t.Close()
149
150	imapConn := t.Conn("imap")
151	defer imapConn.Close()
152	imapConn.ExpectPattern(`\* OK *`)
153	imapConn.Writeln(". LOGIN testusr 1234")
154	imapConn.ExpectPattern(". OK *")
155	imapConn.Writeln(". SELECT INBOX")
156	imapConn.ExpectPattern(`\* *`)
157	imapConn.ExpectPattern(`\* *`)
158	imapConn.ExpectPattern(`\* *`)
159	imapConn.ExpectPattern(`\* *`)
160	imapConn.ExpectPattern(`\* *`)
161	imapConn.ExpectPattern(`\* *`)
162	imapConn.ExpectPattern(`. OK *`)
163
164	smtpConn := t.Conn("smtp")
165	defer smtpConn.Close()
166	smtpConn.SMTPNegotation("localhost", nil, nil)
167	smtpConn.Writeln("MAIL FROM:<sender@maddy.test>")
168	smtpConn.ExpectPattern("2*")
169	smtpConn.Writeln("RCPT TO:<testusr@maddy.test>")
170	smtpConn.ExpectPattern("2*")
171	smtpConn.Writeln("DATA")
172	smtpConn.ExpectPattern("354 *")
173	smtpConn.Writeln("From: <sender@maddy.test>")
174	smtpConn.Writeln("To: <testusr@maddy.test>")
175	smtpConn.Writeln("Subject: Hi!")
176	smtpConn.Writeln("")
177	smtpConn.Writeln("Hi!")
178	smtpConn.Writeln(".")
179	smtpConn.ExpectPattern("2*")
180
181	time.Sleep(500 * time.Millisecond)
182
183	imapConn.Writeln(". NOOP")
184	imapConn.ExpectPattern(`\* 1 EXISTS`)
185	imapConn.ExpectPattern(`\* 1 RECENT`)
186	imapConn.ExpectPattern(". OK *")
187}
188
189func TestImapsqlAuthMap(tt *testing.T) {
190	tt.Parallel()
191	t := tests.NewT(tt)
192
193	t.DNS(nil)
194	t.Port("imap")
195	t.Port("smtp")
196	t.Config(`
197		storage.imapsql test_store {
198			auth_map regexp "(.*)" "$1@maddy.test"
199			auth_normalize precis
200
201			driver sqlite3
202			dsn imapsql.db
203		}
204
205		imap tcp://127.0.0.1:{env:TEST_PORT_imap} {
206			tls off
207
208			auth dummy
209			storage &test_store
210		}
211
212		smtp tcp://127.0.0.1:{env:TEST_PORT_smtp} {
213			hostname maddy.test
214			tls off
215
216			deliver_to &test_store
217		}
218	`)
219	t.Run(2)
220	defer t.Close()
221
222	imapConn := t.Conn("imap")
223	defer imapConn.Close()
224	imapConn.ExpectPattern(`\* OK *`)
225	imapConn.Writeln(". LOGIN testusr 1234")
226	imapConn.ExpectPattern(". OK *")
227	imapConn.Writeln(". SELECT INBOX")
228	imapConn.ExpectPattern(`\* *`)
229	imapConn.ExpectPattern(`\* *`)
230	imapConn.ExpectPattern(`\* *`)
231	imapConn.ExpectPattern(`\* *`)
232	imapConn.ExpectPattern(`\* *`)
233	imapConn.ExpectPattern(`\* *`)
234	imapConn.ExpectPattern(`. OK *`)
235
236	smtpConn := t.Conn("smtp")
237	defer smtpConn.Close()
238	smtpConn.SMTPNegotation("localhost", nil, nil)
239	smtpConn.Writeln("MAIL FROM:<sender@maddy.test>")
240	smtpConn.ExpectPattern("2*")
241	smtpConn.Writeln("RCPT TO:<testusr@maddy.test>")
242	smtpConn.ExpectPattern("2*")
243	smtpConn.Writeln("DATA")
244	smtpConn.ExpectPattern("354 *")
245	smtpConn.Writeln("From: <sender@maddy.test>")
246	smtpConn.Writeln("To: <testusr@maddy.test>")
247	smtpConn.Writeln("Subject: Hi!")
248	smtpConn.Writeln("")
249	smtpConn.Writeln("Hi!")
250	smtpConn.Writeln(".")
251	smtpConn.ExpectPattern("2*")
252
253	time.Sleep(500 * time.Millisecond)
254
255	imapConn.Writeln(". NOOP")
256	imapConn.ExpectPattern(`\* 1 EXISTS`)
257	imapConn.ExpectPattern(`\* 1 RECENT`)
258	imapConn.ExpectPattern(". OK *")
259}