#!/bin/bash

# The data we use for this test come from spamassassin's test suite (build tests):
#
#   - d/t/data/nice contains messages that qualifies as PASS due to the DMARC rules (DMARC_PASS).
#   - d/t/data/spam contains messages that are market as REJECT, QUARENTINE, MISSING or NONE:
#
#             root@Mspamassasin-suggested:~# l spam_mails/
#             nodmarc.eml noneko.eml quarko.eml rejectko.eml strictrejectko.eml
#             root@Mspamassasin-suggested:~# spamc -R < spam_mails/rejectko.eml | grep DMARC
#             1.8 DMARC_REJECT DMARC reject policy
#             root@Mspamassasin-suggested:~# spamc -R < spam_mails/strictrejectko.eml | grep DMARC
#             1.8 DMARC_REJECT DMARC reject policy
#             root@Mspamassasin-suggested:~# spamc -R < spam_mails/quarko.eml | grep DMARC
#             1.2 DMARC_QUAR DMARC quarantine policy
#             root@Mspamassasin-suggested:~# spamc -R < spam_mails/nodmarc.eml | grep DMARC
#             0.0 DMARC_MISSING Missing DMARC policy
#             root@Mspamassasin-suggested:~# spamc -R < spam_mails/noneko.eml | grep DMARC
#             0.9 DMARC_NONE DMARC none policy

set -eo pipefail

check_dmarc_policy_reporting(){
    local eml="${1}"
    local policy="${2}"

    spamc -R < "${eml}" |  grep -qF DMARC_"${policy}"

}

for m in $(find debian/tests/data/nice/ -type f); do
	check_dmarc_policy_reporting "${m}" "PASS"
done

declare -A EXPECTED_RESULT
EXPECTED_RESULT["nodmarc.eml"]="MISSING"
EXPECTED_RESULT["noneko.eml"]="NONE"
EXPECTED_RESULT["quarko.eml"]="QUAR"
EXPECTED_RESULT["rejectko.eml"]="REJECT"
EXPECTED_RESULT["strictrejectko.eml"]="REJECT"

for m in ${!EXPECTED_RESULT[@]}; do
	check_dmarc_policy_reporting "debian/tests/data/spam/${m}" "${EXPECTED_RESULT[${m}]}"
done