1
1
Fork 0
mirror of https://github.com/docker-mailserver/docker-mailserver synced 2024-06-09 05:16:07 +02:00

tests: improve `_send_email` (#3105)

This commit is contained in:
Georg Lauterbach 2023-02-24 10:44:18 +01:00 committed by GitHub
parent 199e3c7721
commit ae05e6a7c3
Signed by: GitHub
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 88 additions and 93 deletions

View File

@ -468,7 +468,7 @@ function _service_log_should_contain_string() {
# Filters the mail log for lines that belong to a certain email identified
# by its ID. You can obtain the ID of an email you want to send by using
# `_send_mail_and_get_id`.
# `_send_email_and_get_id`.
#
# @param ${1} = email ID
# @param ${2} = container name [OPTIONAL]

View File

@ -7,25 +7,29 @@
# ! ATTENTION: This file is loaded by `common.sh` - do not load it yourself!
# ! ATTENTION: This file requires helper functions from `common.sh`!
# Sends a mail from localhost (127.0.0.1) via port 25 to the container. To send
# a custom email, create a file at `test/test-files/email-templates/<TEST FILE>`,
# Sends a mail from localhost (127.0.0.1) to a container. To send
# a custom email, create a file at `test/test-files/<TEST FILE>`,
# and provide `<TEST FILE>` as an argument to this function.
#
# @param ${1} = template file (path) name
# @param ${2} = container name [OPTIONAL]
# @param ${3} = port `nc` will use [OPTIONAL]
# @param ${2} = parameters for `nc` [OPTIONAL] (default: `0.0.0.0 25`)
#
# ## Attention
#
# This function assumes `CONTAINER_NAME` to be properly set (to the container
# name the command should be executed in)!
#
# This function will just send the email in an "asynchronous" fashion, i.e. it will
# send the email but it will not make sure the mail queue is empty after the mail
# has been sent.
function _send_email() {
local TEMPLATE_FILE=${1:?Must provide name of template file}
local CONTAINER_NAME=$(__handle_container_name "${2:-}")
local PORT=${3:-25}
local NC_PARAMETERS=${2:-0.0.0.0 25}
_run_in_container_bash "nc 0.0.0.0 ${PORT} < /tmp/docker-mailserver-test/email-templates/${TEMPLATE_FILE}.txt"
assert_not_equal "${NC_PARAMETERS}" ''
assert_not_equal "${CONTAINER_NAME:-}" ''
_run_in_container_bash "nc ${NC_PARAMETERS} < /tmp/docker-mailserver-test/${TEMPLATE_FILE}.txt"
assert_success
}
@ -39,7 +43,12 @@ function _send_email() {
# test file and need to assert certain log entries for each mail individually.
#
# @param ${1} = template file (path) name
# @param ${2} = container name [OPTIONAL]
# @param ${2} = parameters for `nc` [OPTIONAL] (default: `0.0.0.0 25`)
#
# ## Attention
#
# This function assumes `CONTAINER_NAME` to be properly set (to the container
# name the command should be executed in)!
#
# ## Safety
#
@ -47,11 +56,14 @@ function _send_email() {
# If two clients send simultaneously, there is no guarantee the correct ID is
# chosen. Sending more than one mail at any given point in time with this function
# is UNDEFINED BEHAVIOR!
function _send_mail_and_get_id() {
function _send_email_and_get_id() {
local TEMPLATE_FILE=${1:?Must provide name of template file}
local CONTAINER_NAME=$(__handle_container_name "${2:-}")
local NC_PARAMETERS=${2:-0.0.0.0 25}
local MAIL_ID
assert_not_equal "${NC_PARAMETERS}" ''
assert_not_equal "${CONTAINER_NAME:-}" ''
_wait_for_empty_mail_queue_in_container
_send_email "${TEMPLATE_FILE}"
_wait_for_empty_mail_queue_in_container
@ -63,8 +75,6 @@ function _send_mail_and_get_id() {
| grep -E -m 1 'postfix/smtpd.*: [A-Z0-9]+: client=localhost' \
| grep -E -o '[A-Z0-9]{9,12}' || true)
run bash -c "-z ${MAIL_ID}"
assert_success 'Could not obtain mail ID - aborting!'
assert_not_equal "${MAIL_ID}" ''
echo "${MAIL_ID}"
}

View File

@ -26,9 +26,9 @@ function setup_file() {
_wait_for_smtp_port_in_container
# Single mail sent from 'spam@spam.com' that is handled by User (relocate) and Global (copy) sieves for user1:
_send_email 'sieve-spam-folder'
_send_email 'email-templates/sieve-spam-folder'
# Mail for user2 triggers the sieve-pipe:
_send_email 'sieve-pipe'
_send_email 'email-templates/sieve-pipe'
_wait_for_empty_mail_queue_in_container
}

View File

@ -26,7 +26,7 @@ function teardown() { _default_teardown ; }
_common_container_setup 'CUSTOM_SETUP_ARGUMENTS'
_wait_for_smtp_port_in_container
_send_email 'existing-user1'
_send_email 'email-templates/existing-user1'
_wait_for_empty_mail_queue_in_container
# Mail received should be stored as `u.1` (one file per message)
@ -47,7 +47,7 @@ function teardown() { _default_teardown ; }
_common_container_setup 'CUSTOM_SETUP_ARGUMENTS'
_wait_for_smtp_port_in_container
_send_email 'existing-user1'
_send_email 'email-templates/existing-user1'
_wait_for_empty_mail_queue_in_container
# Mail received should be stored in `m.1` (1 or more messages)

View File

@ -14,7 +14,7 @@ function setup_file() {
function teardown_file() { _default_teardown ; }
@test 'normal delivery works' {
_send_email 'existing-user1'
_send_email 'email-templates/existing-user1'
_count_files_in_directory_in_container /var/mail/localhost.localdomain/user1/new 1
}
@ -26,8 +26,7 @@ function teardown_file() { _default_teardown ; }
}
@test "(IMAP) special-use folders should be created when necessary" {
_run_in_container_bash "nc -w 8 0.0.0.0 143 < /tmp/docker-mailserver-test/nc_templates/imap_special_use_folders.txt"
assert_success
_send_email 'nc_templates/imap_special_use_folders' '-w 8 0.0.0.0 143'
assert_output --partial 'Drafts'
assert_output --partial 'Junk'
assert_output --partial 'Trash'

View File

@ -26,7 +26,9 @@ function setup_file() {
_common_container_setup 'CUSTOM_SETUP_ARGUMENTS'
}
function teardown_file() { _default_teardown ; }
function teardown_file() {
docker rm -f "${CONTAINER1_NAME}" "${CONTAINER2_NAME}"
}
@test '(Amavis enabled) configuration should be correct' {
export CONTAINER_NAME=${CONTAINER1_NAME}

View File

@ -25,7 +25,7 @@ function setup_file() {
_wait_for_service postfix
_wait_for_smtp_port_in_container
_send_email 'amavis-virus'
_send_email 'email-templates/amavis-virus'
_wait_for_empty_mail_queue_in_container
}

View File

@ -17,7 +17,7 @@ function setup_file() {
_common_container_setup 'CUSTOM_SETUP_ARGUMENTS'
_wait_for_smtp_port_in_container
_send_email 'existing-user1'
_send_email 'email-templates/existing-user1'
_wait_for_empty_mail_queue_in_container
}

View File

@ -74,8 +74,8 @@ function teardown_file() {
@test "ban ip on multiple failed login" {
CONTAINER1_IP=$(_get_container_ip "${CONTAINER1_NAME}")
# Trigger a ban by failing to login twice:
_run_in_container_explicit "${CONTAINER2_NAME}" bash -c "nc ${CONTAINER1_IP} 465 < /tmp/docker-mailserver-test/auth/smtp-auth-login-wrong.txt"
_run_in_container_explicit "${CONTAINER2_NAME}" bash -c "nc ${CONTAINER1_IP} 465 < /tmp/docker-mailserver-test/auth/smtp-auth-login-wrong.txt"
CONTAINER_NAME=${CONTAINER2_NAME} _send_email 'auth/smtp-auth-login-wrong' "${CONTAINER1_IP} 465"
CONTAINER_NAME=${CONTAINER2_NAME} _send_email 'auth/smtp-auth-login-wrong' "${CONTAINER1_IP} 465"
# Checking that CONTAINER2_IP is banned in "${CONTAINER1_NAME}"
CONTAINER2_IP=$(_get_container_ip "${CONTAINER2_NAME}")

View File

@ -37,9 +37,8 @@ function teardown_file() {
docker rm -f "${CONTAINER1_NAME}" "${CONTAINER2_NAME}"
}
@test "should fail send when talking out of turn" {
_run_in_container_explicit "${CONTAINER2_NAME}" bash -c "nc ${CONTAINER1_IP} 25 < /tmp/docker-mailserver-test/email-templates/postscreen.txt"
assert_success
@test 'should fail send when talking out of turn' {
CONTAINER_NAME=${CONTAINER2_NAME} _send_email 'email-templates/postscreen' "${CONTAINER1_IP} 25"
assert_output --partial 'Protocol error'
# Expected postscreen log entry:
@ -62,7 +61,7 @@ function teardown_file() {
}
# When postscreen is active, it prevents the usual method of piping a file through nc:
# (Won't work: _run_in_container_explicit "${CLIENT_CONTAINER_NAME}" bash -c "nc ${TARGET_CONTAINER_IP} 25 < ${SMTP_TEMPLATE}")
# (Won't work: CONTAINER_NAME=${CLIENT_CONTAINER_NAME} _send_email "${SMTP_TEMPLATE}" "${TARGET_CONTAINER_IP} 25")
# The below workaround respects `postscreen_greet_wait` time (default 6 sec), talking to the mail-server in turn:
# https://www.postfix.org/postconf.5.html#postscreen_greet_wait
function _should_wait_turn_speaking_smtp() {

View File

@ -31,9 +31,9 @@ function setup_file() {
# We will send 3 emails: the first one should pass just fine; the second one should
# be rejected due to spam; the third one should be rejected due to a virus.
export MAIL_ID1=$(_send_mail_and_get_id 'existing-user1')
export MAIL_ID2=$(_send_mail_and_get_id 'rspamd-spam')
export MAIL_ID3=$(_send_mail_and_get_id 'rspamd-virus')
export MAIL_ID1=$(_send_email_and_get_id 'email-templates/existing-user1')
export MAIL_ID2=$(_send_email_and_get_id 'email-templates/rspamd-spam')
export MAIL_ID3=$(_send_email_and_get_id 'email-templates/rspamd-virus')
# add a nested option to a module
_exec_in_container_bash "echo -e 'complicated {\n anOption = someValue;\n}' >/etc/rspamd/override.d/testmodule_complicated.conf"

View File

@ -72,7 +72,7 @@ function teardown() { _default_teardown ; }
function _should_send_spam_message() {
_wait_for_smtp_port_in_container
_wait_for_tcp_port_in_container 10024 # port 10024 is for Amavis
_send_email 'amavis-spam'
_send_email 'email-templates/amavis-spam'
}
function _should_be_received_by_amavis() {

View File

@ -206,7 +206,7 @@ function _should_have_correct_mail_headers() {
# (eg: OVERRIDE_HOSTNAME or `--hostname mail --domainname example.test`)
local EXPECTED_HOSTNAME=${3:-${EXPECTED_FQDN}}
_send_email 'existing-user1'
_send_email 'email-templates/existing-user1'
_wait_for_empty_mail_queue_in_container
_count_files_in_directory_in_container '/var/mail/localhost.localdomain/user1/new/' '1'

View File

@ -38,7 +38,7 @@ function teardown_file() { _default_teardown ; }
@test "delivers mail to existing account" {
_wait_for_smtp_port_in_container
_send_email 'existing-user1' # send a test email
_send_email 'email-templates/existing-user1' # send a test email
# Verify delivery was successful, log line should look similar to:
# postfix/lmtp[1274]: 0EA424ABE7D9: to=<user1@localhost.localdomain>, relay=127.0.0.1[127.0.0.1]:24, delay=0.13, delays=0.07/0.01/0.01/0.05, dsn=2.0.0, status=sent (250 2.0.0 <user1@localhost.localdomain> ixPpB+Zvv2P7BAAAUi6ngw Saved)

View File

@ -55,40 +55,41 @@ function setup_file() {
_wait_for_tcp_port_in_container 10024
_wait_for_smtp_port_in_container_to_respond
# see https://github.com/docker-mailserver/docker-mailserver/pull/3105#issuecomment-1441055103
# Amavis may still not be ready to receive mail, sleep a little to avoid connection failures:
sleep 1
sleep 5
### Send mail to queue for delivery ###
# TODO: Move to clamav tests (For use when ClamAV is enabled):
# _repeat_in_container_until_success_or_timeout 60 "${CONTAINER_NAME}" test -e /var/run/clamav/clamd.ctl
# _run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/amavis-virus.txt"
# _send_email 'email-templates/amavis-virus'
# Required for 'delivers mail to existing alias':
_send_email 'existing-alias-external'
_send_email 'email-templates/existing-alias-external'
# Required for 'delivers mail to existing alias with recipient delimiter':
_send_email 'existing-alias-recipient-delimiter'
_send_email 'email-templates/existing-alias-recipient-delimiter'
# Required for 'delivers mail to existing catchall':
_send_email 'existing-catchall-local'
_send_email 'email-templates/existing-catchall-local'
# Required for 'delivers mail to regexp alias':
_send_email 'existing-regexp-alias-local'
_send_email 'email-templates/existing-regexp-alias-local'
# Required for 'rejects mail to unknown user':
_send_email 'non-existing-user'
_send_email 'email-templates/non-existing-user'
# Required for 'redirects mail to external aliases':
_send_email 'existing-regexp-alias-external'
_send_email 'existing-alias-local'
_send_email 'email-templates/existing-regexp-alias-external'
_send_email 'email-templates/existing-alias-local'
# Required for 'rejects spam':
_send_email 'amavis-spam'
_send_email 'email-templates/amavis-spam'
# Required for 'delivers mail to existing account':
_send_email 'existing-user1'
_send_email 'existing-user2'
_send_email 'existing-user3'
_send_email 'existing-added'
_send_email 'existing-user-and-cc-local-alias'
_send_email 'sieve-spam-folder'
_send_email 'sieve-pipe'
_send_email 'email-templates/existing-user1'
_send_email 'email-templates/existing-user2'
_send_email 'email-templates/existing-user3'
_send_email 'email-templates/existing-added'
_send_email 'email-templates/existing-user-and-cc-local-alias'
_send_email 'email-templates/sieve-spam-folder'
_send_email 'email-templates/sieve-pipe'
_run_in_container_bash 'sendmail root < /tmp/docker-mailserver-test/email-templates/root-email.txt'
}
@ -102,50 +103,43 @@ function setup_file() {
}
@test "should successfully authenticate with good password (plain)" {
_run_in_container_bash 'nc -w 5 0.0.0.0 465 < /tmp/docker-mailserver-test/auth/smtp-auth-plain.txt'
assert_success
_send_email 'auth/smtp-auth-plain' '-w 5 0.0.0.0 465'
assert_output --partial 'Authentication successful'
}
@test "should fail to authenticate with wrong password (plain)" {
_run_in_container_bash 'nc -w 20 0.0.0.0 465 < /tmp/docker-mailserver-test/auth/smtp-auth-plain-wrong.txt'
_send_email 'auth/smtp-auth-plain-wrong' '-w 20 0.0.0.0 465'
assert_output --partial 'authentication failed'
assert_success
}
@test "should successfully authenticate with good password (login)" {
_run_in_container_bash 'nc -w 5 0.0.0.0 465 < /tmp/docker-mailserver-test/auth/smtp-auth-login.txt'
assert_success
_send_email 'auth/smtp-auth-login' '-w 5 0.0.0.0 465'
assert_output --partial 'Authentication successful'
}
@test "should fail to authenticate with wrong password (login)" {
_run_in_container_bash 'nc -w 20 0.0.0.0 465 < /tmp/docker-mailserver-test/auth/smtp-auth-login-wrong.txt'
_send_email 'auth/smtp-auth-login-wrong' '-w 20 0.0.0.0 465'
assert_output --partial 'authentication failed'
assert_success
}
@test "[user: 'added'] should successfully authenticate with good password (plain)" {
_run_in_container_bash 'nc -w 5 0.0.0.0 465 < /tmp/docker-mailserver-test/auth/added-smtp-auth-plain.txt'
assert_success
_send_email 'auth/added-smtp-auth-plain' '-w 5 0.0.0.0 465'
assert_output --partial 'Authentication successful'
}
@test "[user: 'added'] should fail to authenticate with wrong password (plain)" {
_run_in_container_bash 'nc -w 20 0.0.0.0 465 < /tmp/docker-mailserver-test/auth/added-smtp-auth-plain-wrong.txt'
assert_success
_send_email 'auth/added-smtp-auth-plain-wrong' '-w 20 0.0.0.0 465'
assert_output --partial 'authentication failed'
}
@test "[user: 'added'] should successfully authenticate with good password (login)" {
_run_in_container_bash 'nc -w 5 0.0.0.0 465 < /tmp/docker-mailserver-test/auth/added-smtp-auth-login.txt'
_send_email 'auth/added-smtp-auth-login' '-w 5 0.0.0.0 465'
assert_success
assert_output --partial 'Authentication successful'
}
@test "[user: 'added'] should fail to authenticate with wrong password (login)" {
_run_in_container_bash 'nc -w 20 0.0.0.0 465 < /tmp/docker-mailserver-test/auth/added-smtp-auth-login-wrong.txt'
assert_success
_send_email 'auth/added-smtp-auth-login-wrong' '-w 20 0.0.0.0 465'
assert_output --partial 'authentication failed'
}
@ -264,8 +258,7 @@ function setup_file() {
# Dovecot does not support SMTPUTF8, so while we can send we cannot receive
# Better disable SMTPUTF8 support entirely if we can't handle it correctly
@test "not advertising smtputf8" {
_run_in_container_bash 'nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/smtp-ehlo.txt'
assert_success
_send_email 'email-templates/smtp-ehlo'
refute_output --partial 'SMTPUTF8'
}

View File

@ -32,7 +32,7 @@ function teardown_file() { _default_teardown ; }
assert_success
# it looks as if someone tries to send mail to another domain outside of DMS
_send_email 'smtp-only'
_send_email 'email-templates/smtp-only'
_wait_for_empty_mail_queue_in_container
# this seemingly succeeds, but looking at the logs, it doesn't

View File

@ -24,13 +24,11 @@ function teardown_file() { _default_teardown ; }
}
@test 'authentication works' {
_run_in_container_bash 'nc -w 1 0.0.0.0 110 < /tmp/docker-mailserver-test/auth/pop3-auth.txt'
assert_success
_send_email 'auth/pop3-auth' '-w 1 0.0.0.0 110'
}
@test 'added user authentication works' {
_run_in_container_bash 'nc -w 1 0.0.0.0 110 < /tmp/docker-mailserver-test/auth/added-pop3-auth.txt'
assert_success
_send_email 'auth/added-pop3-auth' '-w 1 0.0.0.0 110'
}
@test '/var/log/mail/mail.log is error-free' {

View File

@ -21,8 +21,7 @@ function setup_file() {
function teardown_file() { _default_teardown ; }
@test '(Dovecot) LDAP RIMAP connection and authentication works' {
_run_in_container_bash "nc -w 1 0.0.0.0 143 < /tmp/docker-mailserver-test/auth/imap-auth.txt"
assert_success
_send_email 'auth/imap-auth' '-w 1 0.0.0.0 143'
}
@test '(SASLauthd) SASL RIMAP authentication works' {
@ -31,14 +30,13 @@ function teardown_file() { _default_teardown ; }
}
@test '(SASLauthd) RIMAP SMTP authentication works' {
_run_in_container_bash 'nc -w 5 0.0.0.0 25 < /tmp/docker-mailserver-test/auth/smtp-auth-login.txt'
assert_success
_send_email 'auth/smtp-auth-login' '-w 5 0.0.0.0 25'
assert_output --partial 'Error: authentication not enabled'
_run_in_container_bash 'nc -w 5 0.0.0.0 465 < /tmp/docker-mailserver-test/auth/smtp-auth-login.txt'
assert_success
_send_email 'auth/smtp-auth-login' '-w 5 0.0.0.0 465'
assert_output --partial 'Authentication successful'
_run_in_container_bash 'nc -w 5 0.0.0.0 587 < /tmp/docker-mailserver-test/auth/smtp-auth-login.txt'
assert_success
_send_email 'auth/smtp-auth-login' '-w 5 0.0.0.0 587'
assert_output --partial 'Authentication successful'
}

View File

@ -81,13 +81,11 @@ function teardown_file() { _default_teardown ; }
}
@test "imap: authentication works" {
_run_in_container_bash "nc -w 1 0.0.0.0 143 < /tmp/docker-mailserver-test/auth/imap-auth.txt"
assert_success
_send_email 'auth/imap-auth' '-w 1 0.0.0.0 143'
}
@test "imap: added user authentication works" {
_run_in_container_bash "nc -w 1 0.0.0.0 143 < /tmp/docker-mailserver-test/auth/added-imap-auth.txt"
assert_success
_send_email 'auth/added-imap-auth' '-w 1 0.0.0.0 143'
}
#
@ -416,12 +414,10 @@ EOF
sleep 10
# send some big emails
_run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/quota-exceeded.txt"
assert_success
_run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/quota-exceeded.txt"
assert_success
_run_in_container_bash "nc 0.0.0.0 25 < /tmp/docker-mailserver-test/email-templates/quota-exceeded.txt"
assert_success
_send_email 'email-templates/quota-exceeded' '0.0.0.0 25'
_send_email 'email-templates/quota-exceeded' '0.0.0.0 25'
_send_email 'email-templates/quota-exceeded' '0.0.0.0 25'
# check for quota warn message existence
run _repeat_until_success_or_timeout 20 _exec_in_container_bash 'grep \"Subject: quota warning\" /var/mail/otherdomain.tld/quotauser/new/ -R'
assert_success