1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# Copyright (C) 2012-2017 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# GNU Mailman. If not, see <http://www.gnu.org/licenses/>.
"""Tests for the LMTP server."""
import os
import smtplib
import unittest
from datetime import datetime
from mailman.app.lifecycle import create_list
from mailman.config import config
from mailman.database.transaction import transaction
from mailman.testing.helpers import get_lmtp_client, get_queue_messages
from mailman.testing.layers import LMTPLayer
class TestLMTP(unittest.TestCase):
"""Test various aspects of the LMTP server."""
layer = LMTPLayer
def setUp(self):
with transaction():
self._mlist = create_list('test@example.com')
self._lmtp = get_lmtp_client(quiet=True)
self._lmtp.lhlo('remote.example.org')
self.addCleanup(self._lmtp.close)
def test_message_id_required(self):
# The message is rejected if it does not have a Message-ID header.
with self.assertRaises(smtplib.SMTPDataError) as cm:
self._lmtp.sendmail('anne@example.com', ['test@example.com'], """\
From: anne@example.com
To: test@example.com
Subject: This has no Message-ID header
""")
# LMTP returns a 550: Requested action not taken: mailbox unavailable
# (e.g., mailbox not found, no access, or command rejected for policy
# reasons)
self.assertEqual(cm.exception.smtp_code, 550)
self.assertEqual(cm.exception.smtp_error,
b'No Message-ID header provided')
def test_message_id_hash_is_added(self):
self._lmtp.sendmail('anne@example.com', ['test@example.com'], """\
From: anne@example.com
To: test@example.com
Message-ID: <ant>
Subject: This has a Message-ID but no Message-ID-Hash
""")
items = get_queue_messages('in', expected_count=1)
self.assertEqual(items[0].msg['message-id-hash'],
'MS6QLWERIJLGCRF44J7USBFDELMNT2BW')
def test_original_message_id_hash_is_overwritten(self):
self._lmtp.sendmail('anne@example.com', ['test@example.com'], """\
From: anne@example.com
To: test@example.com
Message-ID: <ant>
Message-ID-Hash: IGNOREME
Subject: This has a Message-ID but no Message-ID-Hash
""")
items = get_queue_messages('in', expected_count=1)
all_headers = items[0].msg.get_all('message-id-hash')
self.assertEqual(len(all_headers), 1)
self.assertEqual(items[0].msg['message-id-hash'],
'MS6QLWERIJLGCRF44J7USBFDELMNT2BW')
def test_received_time(self):
# The LMTP runner adds a `received_time` key to the metadata.
self._lmtp.sendmail('anne@example.com', ['test@example.com'], """\
From: anne@example.com
To: test@example.com
Subject: This has no Message-ID header
Message-ID: <ant>
""")
items = get_queue_messages('in', expected_count=1)
self.assertEqual(items[0].msgdata['received_time'],
datetime(2005, 8, 1, 7, 49, 23))
def test_queue_directory(self):
# The LMTP runner is not queue runner, so it should not have a
# directory in var/queue.
queue_directory = os.path.join(config.QUEUE_DIR, 'lmtp')
self.assertFalse(os.path.isdir(queue_directory))
def test_nonexistent_mailing_list(self):
# Trying to post to a nonexistent mailing list is an error.
with self.assertRaises(smtplib.SMTPDataError) as cm:
self._lmtp.sendmail('anne@example.com',
['notalist@example.com'], """\
From: anne.person@example.com
To: notalist@example.com
Subject: An interesting message
Message-ID: <aardvark>
""")
self.assertEqual(cm.exception.smtp_code, 550)
self.assertEqual(cm.exception.smtp_error,
b'Requested action not taken: mailbox unavailable')
def test_missing_subaddress(self):
# Trying to send a message to a bogus subaddress is an error.
with self.assertRaises(smtplib.SMTPDataError) as cm:
self._lmtp.sendmail('anne@example.com',
['test-bogus@example.com'], """\
From: anne.person@example.com
To: test-bogus@example.com
Subject: An interesting message
Message-ID: <aardvark>
""")
self.assertEqual(cm.exception.smtp_code, 550)
self.assertEqual(cm.exception.smtp_error,
b'Requested action not taken: mailbox unavailable')
def test_mailing_list_with_subaddress(self):
# A mailing list with a subaddress in its name should be recognized as
# the mailing list, not as a command.
with transaction():
create_list('test-join@example.com')
self._lmtp.sendmail('anne@example.com', ['test-join@example.com'], """\
From: anne@example.com
To: test-join@example.com
Message-ID: <ant>
Subject: This should not be recognized as a join command
""")
# The message is in the incoming queue but not the command queue.
get_queue_messages('in', expected_count=1)
get_queue_messages('command', expected_count=0)
def test_mailing_list_with_subaddress_command(self):
# Like above, but we can still send a command to the mailing list.
with transaction():
create_list('test-join@example.com')
self._lmtp.sendmail('anne@example.com',
['test-join-join@example.com'], """\
From: anne@example.com
To: test-join-join@example.com
Message-ID: <ant>
Subject: This will be recognized as a join command.
""")
# The message is in the command queue but not the incoming queue.
get_queue_messages('in', expected_count=0)
get_queue_messages('command', expected_count=1)
class TestBugs(unittest.TestCase):
"""Test some LMTP related bugs."""
layer = LMTPLayer
def setUp(self):
self._lmtp = get_lmtp_client(quiet=True)
self._lmtp.lhlo('remote.example.org')
def test_lp1117176(self):
# Upper cased list names can't be sent to via LMTP.
with transaction():
create_list('my-LIST@example.com')
self._lmtp.sendmail('anne@example.com', ['my-list@example.com'], """\
From: anne@example.com
To: my-list@example.com
Subject: My subject
Message-ID: <alpha>
""")
items = get_queue_messages('in', expected_count=1)
self.assertEqual(items[0].msgdata['listid'],
'my-list.example.com')
def test_issue140(self):
# Non-UTF-8 data sent to the LMTP server crashes it.
with transaction():
create_list('ant@example.com')
self._lmtp.sendmail('anne@example.com', ['ant@example.com'], b"""\
From: anne@example.com
To: ant@example.com
Subject: My subject
Message-ID: <alpha>
\xa0
""")
items = get_queue_messages('in', expected_count=1)
self.assertEqual(items[0].msg['message-id'], '<alpha>')
|