|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import subprocess |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +HAS_DKIMPY = True |
| 8 | +try: |
| 9 | + from dkim import ARC |
| 10 | +except ImportError: |
| 11 | + HAS_DKIMPY = False |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(scope='session') |
| 15 | +def dkimpy(): |
| 16 | + if not HAS_DKIMPY: |
| 17 | + pytest.skip('dkimpy not found') |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture(scope='session') |
| 21 | +def perl_mail_dkim(): |
| 22 | + try: |
| 23 | + subprocess.run(['perl', '-MMail::DKIM', '-e', ''], check=True) |
| 24 | + except Exception: |
| 25 | + pytest.skip('Mail::DKIM not found') |
| 26 | + |
| 27 | + |
| 28 | +def test_dkimpy_sign(run_miltertest, private_key, dkimpy): |
| 29 | + hdrs = [ |
| 30 | + ['Subject', ' test message from dkimpy'], |
| 31 | + |
| 32 | + |
| 33 | + ['Authentication-Results', ' dkimpy.example.com; none'], |
| 34 | + ] |
| 35 | + |
| 36 | + msg = b'' |
| 37 | + for h, v in hdrs: |
| 38 | + msg += f'{h}: {v}\r\n'.encode() |
| 39 | + msg += b'\r\ntest body\r\n' |
| 40 | + |
| 41 | + res = ARC(msg).sign(b'dkimpy', b'example.com', private_key['basepath'].joinpath('dkimpy._domainkey.example.com.key').read_bytes(), b'dkimpy.example.com') |
| 42 | + |
| 43 | + hdrs = [ |
| 44 | + *[h.decode().rstrip().split(':', 1) for h in res], |
| 45 | + *hdrs, |
| 46 | + ] |
| 47 | + res = run_miltertest(hdrs, False) |
| 48 | + |
| 49 | + assert res['headers'][0] == ['Authentication-Results', ' example.com; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1'] |
| 50 | + assert res['headers'][1][0] == 'ARC-Seal' |
| 51 | + assert 'cv=pass' in res['headers'][1][1] |
| 52 | + assert res['headers'][2][0] == 'ARC-Message-Signature' |
| 53 | + assert res['headers'][3] == ['ARC-Authentication-Results', ' i=2; example.com; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1'] |
| 54 | + |
| 55 | + |
| 56 | +def test_dkimpy_verify(run_miltertest, private_key, dkimpy): |
| 57 | + # we don't test simple/simple because dkimpy uses the wrong default |
| 58 | + for i in range(0, 3): |
| 59 | + res = run_miltertest(milter_instance=i) |
| 60 | + |
| 61 | + msg = b'' |
| 62 | + for h, v in [*res['headers'], *res['msg_headers']]: |
| 63 | + msg += f'{h}:{v}\r\n'.encode() |
| 64 | + msg += f'\r\n{res["msg_body"]}'.encode() |
| 65 | + |
| 66 | + def dnsfunc(domain, timeout=5): |
| 67 | + with open(private_key['public_keys'], 'rb') as f: |
| 68 | + for line in f: |
| 69 | + if line.startswith(domain[:-1]): |
| 70 | + return line.split(None, 1)[1] |
| 71 | + |
| 72 | + return '' |
| 73 | + |
| 74 | + res = ARC(msg).verify(dnsfunc) |
| 75 | + assert res[0] == b'pass' |
| 76 | + |
| 77 | + |
| 78 | +def test_perl_sign(run_miltertest, private_key, perl_mail_dkim): |
| 79 | + hdrs = [ |
| 80 | + ['Subject', ' test message from Mail::DKIM'], |
| 81 | + |
| 82 | + |
| 83 | + ['Authentication-Results', ' perl.example.com; none'], |
| 84 | + ] |
| 85 | + |
| 86 | + msg = '' |
| 87 | + for h, v in hdrs: |
| 88 | + msg += f'{h}:{v}\r\n' |
| 89 | + msg += '\r\ntest body\r\n' |
| 90 | + |
| 91 | + res = subprocess.run( |
| 92 | + [ |
| 93 | + 'perl', |
| 94 | + '-MMail::DKIM::ARC::Signer', |
| 95 | + '-E', |
| 96 | + f'my $arc = new Mail::DKIM::ARC::Signer(Domain => "example.com", Selector => "perl", SrvId => "perl.example.com", KeyFile => "{private_key["basepath"].joinpath("perl._domainkey.example.com.key")}", Chain => "ar"); while (<STDIN>) {{ $arc->PRINT($_) }}; $arc->CLOSE; say join("\n", $arc->as_strings)', # noqa: E501 |
| 97 | + ], |
| 98 | + input=msg, |
| 99 | + text=True, |
| 100 | + capture_output=True, |
| 101 | + ) |
| 102 | + |
| 103 | + assert res.returncode == 0 |
| 104 | + |
| 105 | + hdrs = [ |
| 106 | + *[x.split(':', 1) for x in res.stdout.splitlines()], |
| 107 | + *hdrs, |
| 108 | + ] |
| 109 | + |
| 110 | + res = run_miltertest(hdrs, False) |
| 111 | + assert res['headers'][0] == ['Authentication-Results', ' example.com; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1'] |
| 112 | + assert res['headers'][1][0] == 'ARC-Seal' |
| 113 | + assert 'cv=pass' in res['headers'][1][1] |
| 114 | + assert res['headers'][2][0] == 'ARC-Message-Signature' |
| 115 | + assert res['headers'][3] == ['ARC-Authentication-Results', ' i=2; example.com; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1'] |
0 commit comments