1//go:build integration && cgo && !nosqlite32// +build integration,cgo,!nosqlite334/*5Maddy Mail Server - Composable all-in-one email server.6Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors78This program is free software: you can redistribute it and/or modify9it under the terms of the GNU General Public License as published by10the Free Software Foundation, either version 3 of the License, or11(at your option) any later version.1213This program is distributed in the hope that it will be useful,14but WITHOUT ANY WARRANTY; without even the implied warranty of15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16GNU General Public License for more details.1718You should have received a copy of the GNU General Public License19along with this program. If not, see <https://www.gnu.org/licenses/>.20*/2122package tests_test2324import (25 "testing"26 "time"2728 "github.com/foxcpp/maddy/tests"29)3031// Smoke test to ensure message delivery is handled correctly.3233func TestImapsqlDelivery(tt *testing.T) {34 tt.Parallel()35 t := tests.NewT(tt)3637 t.DNS(nil)38 t.Port("imap")39 t.Port("smtp")40 t.Config(`41 storage.imapsql test_store {42 driver sqlite343 dsn imapsql.db44 }4546 imap tcp://127.0.0.1:{env:TEST_PORT_imap} {47 tls off4849 auth dummy50 storage &test_store51 }5253 smtp tcp://127.0.0.1:{env:TEST_PORT_smtp} {54 hostname maddy.test55 tls off5657 deliver_to &test_store58 }59 `)60 t.Run(2)61 defer t.Close()6263 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 *`)7677 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*")9394 time.Sleep(500 * time.Millisecond)9596 imapConn.Writeln(". NOOP")97 imapConn.ExpectPattern(`\* 1 EXISTS`)98 imapConn.ExpectPattern(`\* 1 RECENT`)99 imapConn.ExpectPattern(". OK *")100101 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}116117func TestImapsqlDeliveryMap(tt *testing.T) {118 tt.Parallel()119 t := tests.NewT(tt)120121 t.DNS(nil)122 t.Port("imap")123 t.Port("smtp")124 t.Config(`125 storage.imapsql test_store {126 delivery_map email_localpart127 auth_normalize precis128129 driver sqlite3130 dsn imapsql.db131 }132133 imap tcp://127.0.0.1:{env:TEST_PORT_imap} {134 tls off135136 auth dummy137 storage &test_store138 }139140 smtp tcp://127.0.0.1:{env:TEST_PORT_smtp} {141 hostname maddy.test142 tls off143144 deliver_to &test_store145 }146 `)147 t.Run(2)148 defer t.Close()149150 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 *`)163164 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*")180181 time.Sleep(500 * time.Millisecond)182183 imapConn.Writeln(". NOOP")184 imapConn.ExpectPattern(`\* 1 EXISTS`)185 imapConn.ExpectPattern(`\* 1 RECENT`)186 imapConn.ExpectPattern(". OK *")187}188189func TestImapsqlAuthMap(tt *testing.T) {190 tt.Parallel()191 t := tests.NewT(tt)192193 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 precis200201 driver sqlite3202 dsn imapsql.db203 }204205 imap tcp://127.0.0.1:{env:TEST_PORT_imap} {206 tls off207208 auth dummy209 storage &test_store210 }211212 smtp tcp://127.0.0.1:{env:TEST_PORT_smtp} {213 hostname maddy.test214 tls off215216 deliver_to &test_store217 }218 `)219 t.Run(2)220 defer t.Close()221222 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 *`)235236 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*")252253 time.Sleep(500 * time.Millisecond)254255 imapConn.Writeln(". NOOP")256 imapConn.ExpectPattern(`\* 1 EXISTS`)257 imapConn.ExpectPattern(`\* 1 RECENT`)258 imapConn.ExpectPattern(". OK *")259}