Skip to content

Commit

Permalink
test: Unify (most) SMTP client calls
Browse files Browse the repository at this point in the history
To send mails, today some tests use msmtp and others our internal smtpc.py.

This works, but msmtp slows down the tests significantly, and smtpc.py
is also not particularly fast, and also has some limitations.

This patch introduces a new SMTP client tool written in Go, and makes
almost all the tests use it.

Some tests still remain on msmtp, mainly for client-check compatibility.
It's likely that this will be moved in later patches to a separate
special-purpose test.

With this patch, integration tests take ~20% less time than before.
  • Loading branch information
albertito committed Mar 9, 2024
1 parent 1d0abb0 commit 1f58dd3
Show file tree
Hide file tree
Showing 54 changed files with 282 additions and 332 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ test/util/generate_cert/generate_cert
test/util/gocovcat/gocovcat
test/util/loadgen/loadgen
test/util/minidns/minidns
test/util/smtpc/smtpc

# Test binary, generated during coverage tests.
chasquid.test
Expand Down
14 changes: 0 additions & 14 deletions test/t-02-exim/msmtprc

This file was deleted.

16 changes: 8 additions & 8 deletions test/t-02-exim/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
# someone@srv-chasquid.
#
# Test:
# msmtp --> chasquid --> exim --> chasquid --> local delivery
# smtpc --> chasquid --> exim --> chasquid --> local delivery
#
# msmtp will auth as user@srv-chasquid to chasquid, and send an email with
# smtpc will auth as user@srv-chasquid to chasquid, and send an email with
# recipient someone@srv-exim.
#
# chasquid will deliver the mail to exim.
Expand All @@ -28,15 +28,15 @@ set -e
init
check_hostaliases

if ! .exim4/exim4 --version > /dev/null; then
skip "exim4 binary at .exim4/exim4 is not functional"
fi

# Create a temporary directory for exim4 to use, and generate the exim4
# config based on the template.
mkdir -p .exim4
EXIMDIR="$PWD/.exim4" envsubst < config/exim4.in > .exim4/config

if ! .exim4/exim4 -C "$PWD/.exim4/config" --version > /dev/null; then
skip "exim4 binary at .exim4/exim4 is not functional"
fi

# Build with the DNS override, so we can fake DNS records.
export GOTAGS="dnsoverride"

Expand All @@ -62,8 +62,8 @@ wait_until_ready 9053
.exim4/exim4 -bd -d -C "$PWD/.exim4/config" > .exim4/log 2>&1 &
wait_until_ready 2025

# msmtp will use chasquid to send an email to someone@srv-exim.
run_msmtp someone@srv-exim < content
# smtpc will use chasquid to send an email to someone@srv-exim.
smtpc someone@srv-exim < content

wait_for_file .mail/someone@srv-chasquid

Expand Down
4 changes: 4 additions & 0 deletions test/t-02-exim/smtpc.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
addr localhost:1465
server_cert config/certs/srv-chasquid/fullchain.pem
user user@srv-chasquid
password secretpassword
1 change: 1 addition & 0 deletions test/t-04-aliases/content
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
From: user@testserver
Subject: Prueba desde el test

Crece desde el test el futuro
Expand Down
14 changes: 0 additions & 14 deletions test/t-04-aliases/msmtprc

This file was deleted.

10 changes: 5 additions & 5 deletions test/t-04-aliases/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ chasquid -v=2 --logfile=.logs/chasquid.log --config_dir=config &
wait_until_ready 1025

function send_and_check() {
run_msmtp "$1@testserver" < content
smtpc "$1@testserver" < content
shift
for i in "$@"; do
wait_for_file ".mail/$i@testserver"
Expand Down Expand Up @@ -42,7 +42,7 @@ send_and_check añil+blah azul índigo

# Test the pipe alias separately.
rm -f .data/pipe_alias_worked
run_msmtp tubo@testserver < content
smtpc tubo@testserver < content
wait_for_file .data/pipe_alias_worked
mail_diff content .data/pipe_alias_worked

Expand All @@ -58,17 +58,17 @@ send_and_check vic.uña+abc uña

# Test the pipe alias separately.
rm -f .data/pipe_alias_worked
run_msmtp ñandú@testserver < content
smtpc ñandú@testserver < content
wait_for_file .data/pipe_alias_worked
mail_diff content .data/pipe_alias_worked

# Test when alias-resolve exits with an error
if run_msmtp roto@testserver < content 2> .logs/msmtp.out; then
if smtpc roto@testserver < content 2> .logs/smtpc.out; then
fail "expected delivery to roto@ to fail, but succeeded"
fi

# Test a non-existent alias.
if run_msmtp nono@testserver < content 2> .logs/msmtp.out; then
if smtpc nono@testserver < content 2> .logs/smtpc.out; then
fail "expected delivery to nono@ to fail, but succeeded"
fi

Expand Down
4 changes: 4 additions & 0 deletions test/t-04-aliases/smtpc.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
addr localhost:1465
server_cert config/certs/testserver/fullchain.pem
user user@testserver
password secretpassword
1 change: 0 additions & 1 deletion test/t-05-null_address/expected_dsr
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ Received: from localhost
tls *
(over *
; *
Date: *
From: Mailer daemon <somewhere@báratro>
Subject: I've come to haunt you
Message-Id: <booooo>
Expand Down
13 changes: 0 additions & 13 deletions test/t-05-null_address/msmtprc

This file was deleted.

2 changes: 1 addition & 1 deletion test/t-05-null_address/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ rm -f .mail/user@testserver


# Test that we get mail back for a failed delivery
run_msmtp fail@testserver < content
smtpc fail@testserver < content
wait_for_file .mail/user@testserver
mail_diff expected_dsr .mail/user@testserver

Expand Down
4 changes: 4 additions & 0 deletions test/t-05-null_address/smtpc.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
addr localhost:1465
server_cert config/certs/testserver/fullchain.pem
user user@testserver
password secretpassword
1 change: 1 addition & 0 deletions test/t-06-idna/A/chasquid.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
smtp_address: ":1025"
submission_address: ":1587"
submission_over_tls_address: ":1465"
monitoring_address: ":1099"

mail_delivery_agent_bin: "test-mda"
Expand Down
1 change: 1 addition & 0 deletions test/t-06-idna/B/chasquid.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
smtp_address: ":2025"
submission_address: ":2587"
submission_over_tls_address: ":2465"
monitoring_address: ":2099"

mail_delivery_agent_bin: "test-mda"
Expand Down
16 changes: 8 additions & 8 deletions test/t-06-idna/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ check_hostaliases

rm -rf .data-A .data-B .mail

skip_if_python_is_too_old

# Build with the DNS override, so we can fake DNS records.
export GOTAGS="dnsoverride"

Expand Down Expand Up @@ -37,20 +35,22 @@ chasquid -v=2 --logfile=.logs-B/chasquid.log --config_dir=B \
--testing__dns_addr=127.0.0.1:9053 \
--testing__outgoing_smtp_port=1025 &

wait_until_ready 1025
wait_until_ready 2025
wait_until_ready 1465
wait_until_ready 2465
wait_until_ready 9053

# Send from A to B.
smtpc.py --server=localhost:1025 --user=nadaA@nadaA --password=nadaA \
< from_A_to_B
smtpc --addr=localhost:1465 --user=nadaA@nadaA --password=nadaA \
--server_cert=A/certs/srv-ñ/fullchain.pem \
pingüino@srv-ü < from_A_to_B

wait_for_file .mail/pingüino@srv-ü
mail_diff from_A_to_B .mail/pingüino@srv-ü

# Send from B to A.
smtpc.py --server=localhost:2025 --user=nadaB@nadaB --password=nadaB \
< from_B_to_A
smtpc --addr=localhost:2465 --user=nadaB@nadaB --password=nadaB \
--server_cert=B/certs/srv-ü/fullchain.pem \
ñangapirí@srv-ñ < from_B_to_A

wait_for_file .mail/ñangapirí@srv-ñ
mail_diff from_B_to_A .mail/ñangapirí@srv-ñ
Expand Down
1 change: 1 addition & 0 deletions test/t-07-smtputf8/config/chasquid.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
smtp_address: ":1025"
submission_address: ":1587"
submission_over_tls_address: ":1465"
monitoring_address: ":1099"

mail_delivery_agent_bin: "test-mda"
Expand Down
20 changes: 9 additions & 11 deletions test/t-07-smtputf8/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,23 @@ set -e

init

skip_if_python_is_too_old

generate_certs_for ñoños

# Intentionally have a config directory for upper case; this should be
# normalized to lowercase internally (and match the cert accordingly).
add_user ñandú@ñoñOS araño
add_user ñangapirí@ñoñOS antaño

# Python doesn't support UTF8 for auth, use an ascii user and domain.
add_user nada@nada nada

mkdir -p .logs
chasquid -v=2 --logfile=.logs/chasquid.log --config_dir=config &
wait_until_ready 1025

# The envelope from and to are taken from the content, and use a mix of upper
# and lower case.
smtpc.py --server=localhost:1025 --user=nada@nada --password=nada \
< content
wait_until_ready 1465

# Use a mix of upper and lower case in the from, to, and username, to check
# normalization is well handled end-to-end.
smtpc --addr=localhost:1465 \
--server_cert=config/certs/ñoños/fullchain.pem \
--user=ñanDÚ@ñoños --password=araño \
Ñangapirí@Ñoños < content

# The MDA should see the normalized users and domains, in lower case.
wait_for_file .mail/ñangapirí@ñoños
Expand Down
14 changes: 0 additions & 14 deletions test/t-09-loop/msmtprc

This file was deleted.

2 changes: 1 addition & 1 deletion test/t-09-loop/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ wait_until_ready 1025
wait_until_ready 2025
wait_until_ready 9053

run_msmtp aliasB@srv-B < content
smtpc aliasB@srv-B < content

# Get some of the debugging pages, for troubleshooting, and to make sure they
# work reasonably well.
Expand Down
4 changes: 4 additions & 0 deletions test/t-09-loop/smtpc.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
addr localhost:1465
server_cert A/certs/srv-A/fullchain.pem
user userA@srv-A
password userA
1 change: 1 addition & 0 deletions test/t-10-hooks/config/chasquid.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
smtp_address: ":1025"
submission_address: ":1587"
submission_over_tls_address: ":1465"
monitoring_address: ":1099"

mail_delivery_agent_bin: "test-mda"
Expand Down
15 changes: 0 additions & 15 deletions test/t-10-hooks/msmtprc

This file was deleted.

16 changes: 8 additions & 8 deletions test/t-10-hooks/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ wait_until_ready 1025

cp config/hooks/post-data.good config/hooks/post-data

run_msmtp someone@testserver < content
smtpc someone@testserver < content

wait_for_file .mail/someone@testserver

Expand Down Expand Up @@ -51,28 +51,28 @@ check "SPF_PASS=0"

# Check that failures in the script result in failing delivery.
# Transient failure.
if run_msmtp blockme@testserver < content 2>/dev/null; then
if smtpc blockme@testserver < content >.logs/smtpc.log 2>&1; then
fail "ERROR: hook did not block email as expected"
fi
if ! tail -n 1 .logs/msmtp | grep -q "smtpstatus=451"; then
tail -n 1 .logs/msmtp
if ! grep -q "451 ¡No pasarán!" .logs/smtpc.log; then
cat .logs/smtpc.log
fail "ERROR: transient hook error not returned correctly"
fi

# Permanent failure.
if run_msmtp permanent@testserver < content 2>/dev/null; then
if smtpc permanent@testserver < content >.logs/smtpc.log 2>&1; then
fail "ERROR: hook did not block email as expected"
fi
if ! tail -n 1 .logs/msmtp | grep -q "smtpstatus=554"; then
tail -n 1 .logs/msmtp
if ! grep -q "554 Nos hacemos la permanente" .logs/smtpc.log; then
cat .logs/smtpc.log
fail "ERROR: permanent hook error not returned correctly"
fi

# Check that the bad hooks don't prevent delivery.
for i in config/hooks/post-data.bad*; do
cp "$i" config/hooks/post-data

run_msmtp someone@testserver < content
smtpc someone@testserver < content
wait_for_file .mail/someone@testserver
mail_diff content .mail/someone@testserver
done
Expand Down
4 changes: 4 additions & 0 deletions test/t-10-hooks/smtpc.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
addr localhost:1465
server_cert config/certs/testserver/fullchain.pem
user user@testserver
password secretpassword
1 change: 1 addition & 0 deletions test/t-11-dovecot/content
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
From: user@srv
Subject: Prueba desde el test

Crece desde el test el futuro
Expand Down
Loading

0 comments on commit 1f58dd3

Please sign in to comment.