mirror of
https://github.com/chatmail/relay.git
synced 2026-05-19 20:38:05 +00:00
more maildata shifting
This commit is contained in:
@@ -2,36 +2,34 @@ from chatmaild.filtermail import check_encrypted, check_DATA, SendRateLimiter
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
def test_reject_forged_from(mailgen):
|
def test_reject_forged_from(get_mail_data, gencreds):
|
||||||
class envelope:
|
class env:
|
||||||
mail_from = "bob@c3.testrun.org"
|
mail_from = gencreds()[0]
|
||||||
rcpt_tos = ["somebody@c3.testrun.org"]
|
rcpt_tos = [gencreds()[0]]
|
||||||
|
|
||||||
# test that the filter lets good mail through
|
# test that the filter lets good mail through
|
||||||
envelope.content = mailgen.get_mail_data(
|
env.content = get_mail_data("plain.eml", from_addr=env.mail_from).as_bytes()
|
||||||
"plain.eml", from_addr=envelope.mail_from
|
assert not check_DATA(envelope=env)
|
||||||
).as_bytes()
|
|
||||||
assert not check_DATA(envelope=envelope)
|
|
||||||
|
|
||||||
# test that the filter rejects forged mail
|
# test that the filter rejects forged mail
|
||||||
envelope.content = mailgen.get_mail_data(
|
env.content = get_mail_data(
|
||||||
"plain.eml", from_addr="forged@c3.testrun.org"
|
"plain.eml", from_addr="forged@c3.testrun.org"
|
||||||
).as_bytes()
|
).as_bytes()
|
||||||
error = check_DATA(envelope=envelope)
|
error = check_DATA(envelope=env)
|
||||||
assert "500" in error
|
assert "500" in error
|
||||||
|
|
||||||
|
|
||||||
def test_filtermail_no_encryption_detection(mailgen):
|
def test_filtermail_no_encryption_detection(get_mail_data):
|
||||||
msg = mailgen.get_mail_data("plain.eml")
|
msg = get_mail_data("plain.eml")
|
||||||
assert not check_encrypted(msg)
|
assert not check_encrypted(msg)
|
||||||
|
|
||||||
# https://xkcd.com/1181/
|
# https://xkcd.com/1181/
|
||||||
msg = mailgen.get_mail_data("fake-encrypted.eml")
|
msg = get_mail_data("fake-encrypted.eml")
|
||||||
assert not check_encrypted(msg)
|
assert not check_encrypted(msg)
|
||||||
|
|
||||||
|
|
||||||
def test_filtermail_encryption_detection(mailgen):
|
def test_filtermail_encryption_detection(get_mail_data):
|
||||||
msg = mailgen.get_mail_data("encrypted.eml")
|
msg = get_mail_data("encrypted.eml")
|
||||||
assert check_encrypted(msg)
|
assert check_encrypted(msg)
|
||||||
|
|
||||||
# if the subject is not "..." it is not considered ac-encrypted
|
# if the subject is not "..." it is not considered ac-encrypted
|
||||||
@@ -39,9 +37,8 @@ def test_filtermail_encryption_detection(mailgen):
|
|||||||
assert not check_encrypted(msg)
|
assert not check_encrypted(msg)
|
||||||
|
|
||||||
|
|
||||||
def test_filtermail_mdn_is_not_encrypted(mailgen):
|
def test_filtermail_mdn_is_not_encrypted(get_mail_data):
|
||||||
msg = mailgen.get_mail_data("mdn.eml")
|
assert not check_encrypted(get_mail_data("mdn.eml"))
|
||||||
assert not check_encrypted(msg)
|
|
||||||
|
|
||||||
|
|
||||||
def test_send_rate_limiter():
|
def test_send_rate_limiter():
|
||||||
|
|||||||
@@ -287,21 +287,19 @@ class Remote:
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mailgen(request, gencreds):
|
def get_mail_data(request, gencreds):
|
||||||
datadir = conftestdir.joinpath("mail-data")
|
datadir = conftestdir.joinpath("mail-data")
|
||||||
|
|
||||||
class Mailgen:
|
def get_mail_data(name, parsed=True, from_addr=None, to_addr=None):
|
||||||
def get_mail_data(self, name, parsed=True, from_addr=None):
|
if from_addr is None:
|
||||||
if from_addr is None:
|
from_addr = gencreds()[0]
|
||||||
from_addr = gencreds()[0]
|
if to_addr is None:
|
||||||
to_addr = gencreds()[0]
|
to_addr = gencreds()[0]
|
||||||
data = datadir.joinpath(name).read_text()
|
data = datadir.joinpath(name).read_text()
|
||||||
text = data.format(from_addr=from_addr, to_addr=to_addr)
|
text = data.format(from_addr=from_addr, to_addr=to_addr)
|
||||||
if parsed:
|
return BytesParser(policy=policy.default).parsebytes(text.encode())
|
||||||
return BytesParser(policy=policy.default).parsebytes(text.encode())
|
|
||||||
return text
|
|
||||||
|
|
||||||
return Mailgen()
|
return get_mail_data
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ def test_use_two_chatmailservers(cmfactory, maildomain2):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("forgeaddr", ["internal", "someone@example.org"])
|
@pytest.mark.parametrize("forgeaddr", ["internal", "someone@example.org"])
|
||||||
def test_reject_forged_from(cmsetup, mailgen, lp, forgeaddr):
|
def test_reject_forged_from(cmsetup, get_mail_data, lp, forgeaddr):
|
||||||
user1, user3 = cmsetup.gen_users(2)
|
user1, user3 = cmsetup.gen_users(2)
|
||||||
|
|
||||||
lp.sec("send encrypted message with forged from")
|
lp.sec("send encrypted message with forged from")
|
||||||
@@ -31,7 +31,8 @@ def test_reject_forged_from(cmsetup, mailgen, lp, forgeaddr):
|
|||||||
addr_to_forge = "someone@example.org"
|
addr_to_forge = "someone@example.org"
|
||||||
|
|
||||||
print("message to inject:")
|
print("message to inject:")
|
||||||
msg = mailgen.get_encrypted(from_addr=addr_to_forge, to_addr=user3.addr)
|
msg = get_mail_data("encrypted.eml", from_addr=addr_to_forge, to_addr=user3.addr)
|
||||||
|
msg = msg.as_string()
|
||||||
for line in msg.split("\n")[:4]:
|
for line in msg.split("\n")[:4]:
|
||||||
print(f" {line}")
|
print(f" {line}")
|
||||||
|
|
||||||
@@ -42,10 +43,10 @@ def test_reject_forged_from(cmsetup, mailgen, lp, forgeaddr):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.slow
|
@pytest.mark.slow
|
||||||
def test_exceed_rate_limit(cmsetup, gencreds, mailgen):
|
def test_exceed_rate_limit(cmsetup, gencreds, get_mail_data):
|
||||||
"""Test that the per-account send-mail limit is exceeded."""
|
"""Test that the per-account send-mail limit is exceeded."""
|
||||||
user1, user2 = cmsetup.gen_users(2)
|
user1, user2 = cmsetup.gen_users(2)
|
||||||
mail = mailgen.get_encrypted(user1.addr, user2.addr)
|
mail = get_mail_data("encrypted", from_addr=user1.addr, to_addr=user2.addr)
|
||||||
for i in range(100):
|
for i in range(100):
|
||||||
print("Sending mail", str(i))
|
print("Sending mail", str(i))
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user