-
Notifications
You must be signed in to change notification settings - Fork 12
/
run-testsuite
executable file
·436 lines (370 loc) · 13.9 KB
/
run-testsuite
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import shutil
import subprocess
import sys
import textwrap
import unittest
from os.path import join as joinpath
class TestFixture(unittest.TestCase):
_maildir_dir = "test.maildir.%d" % os.getpid()
_testdata_dir = "test.testdata.%d" % os.getpid()
_testmail_data_a = [
"From: Sender <sender@example.com>\n",
"To: Recipient <recipient@example.com>\n",
"Cc: Carbon Copy <carbon.copy@example.com>\n",
"Subject: A subject with =?utf8?b?csOkdnNtw7ZyZ8Olcw==?=\n",
"Delivered-To: Alpha\n",
"X-BeenThere: Bravo\n",
"X-Mailing-List: Charlie\n",
"Mailing-List: Delta\n",
"\n",
"Body. R\xc3\xa4vsm\xc3\xb6rg\xc3\xa5s.\n",
]
_testmail_data_bad_header = [
"From: Sender <sender@example.com>\n",
"To: Recipient <recipient@example.com>\n",
"Subject: =?ISO-2022-JP?B?gYqBiQ=?=\n",
"\n",
"Body.\n",
]
def setUp(self):
shutil.rmtree(self._testdata_dir, True)
os.mkdir(self._testdata_dir)
shutil.rmtree(self._maildir_dir, True)
os.mkdir(self._maildir_dir)
def tearDown(self):
shutil.rmtree(self._testdata_dir, True)
shutil.rmtree(self._maildir_dir, True)
def makedirs(self, maildirs):
for md in maildirs:
for d in ["cur", "new", "tmp"]:
os.makedirs(joinpath(self._maildir_dir, md, d))
def create(self, name, dest):
fp = open(joinpath(self._maildir_dir, dest), "w")
fp.write("".join(getattr(self, "_testmail_data_%s" % name)))
fp.close()
def run_mdp(self, rc, maildirs, maildir_base=""):
if maildir_base is None:
extra_argv = []
elif maildir_base == "":
extra_argv = ["-b", self._maildir_dir]
else:
extra_argv = ["-b", maildir_base]
argv = [
"./%s" % self._maildirproc_name,
"--once",
"-l", "/dev/null",
"-r", "-",
] + extra_argv
for maildir in maildirs:
argv.extend(["-m", maildir])
sh_cmd = "-c 'a=%s/delivered; echo $1 >$a; cat >>$a'" % (
self._testdata_dir)
p = subprocess.Popen(
argv,
stdin=subprocess.PIPE,
env={
"SENDMAIL": "/bin/sh",
"SENDMAILFLAGS": sh_cmd,
},
)
p.communicate(textwrap.dedent(rc))
p.wait()
assert(p.returncode == 0)
def verify_result(self, result):
actual = {}
for maildir in os.listdir(self._maildir_dir):
i = 0
for subdir in ["cur", "new"]:
path = joinpath(self._maildir_dir, maildir, subdir)
number = len(os.listdir(path))
if number > 0:
actual[joinpath(maildir, subdir)] = number
self.assertEqual(result, actual)
class TestSuite(TestFixture):
def __init__(self, method_name, use_python2, maildirproc_name):
TestFixture.__init__(self, method_name)
self._use_python2 = use_python2
self._maildirproc_name = maildirproc_name
def fix_u(self, script):
return script.replace("__u__", "u" if self._use_python2 else "")
###########################################################################
# Basics.
def test_empty(self):
rc = ""
self.makedirs(["incoming"])
self.create("a", "incoming/new/a")
self.run_mdp(rc, ["incoming"])
self.verify_result({"incoming/new": 1})
def test_default_maildir_base(self):
rc = '''
for mail in processor:
mail.move("%s/dest")
''' % self._maildir_dir
self.makedirs(["incoming", "dest"])
self.create("a", "incoming/cur/a")
self.create("a", "incoming/new/b")
self.run_mdp(rc, [joinpath(self._maildir_dir, "incoming")], None)
self.verify_result({"dest/cur": 1, "dest/new": 1})
for x in ["incoming/tmp", "incoming/cur", "incoming/new", "dest/tmp"]:
self.assertEqual(
len(os.listdir(joinpath(self._maildir_dir, x))),
0)
def test_move(self):
rc = '''
for mail in processor:
mail.move("dest")
'''
self.makedirs(["incoming", "dest"])
self.create("a", "incoming/cur/a")
self.create("a", "incoming/new/b")
self.run_mdp(rc, ["incoming"])
self.verify_result({"dest/cur": 1, "dest/new": 1})
for x in ["incoming/tmp", "incoming/cur", "incoming/new", "dest/tmp"]:
self.assertEqual(
len(os.listdir(joinpath(self._maildir_dir, x))),
0)
def test_move_keeps_flags(self):
rc = '''
for mail in processor:
mail.move("dest")
'''
self.makedirs(["incoming", "dest"])
self.create("a", "incoming/cur/a:2,x")
self.run_mdp(rc, ["incoming"])
self.verify_result({"dest/cur": 1})
filename = os.listdir(joinpath(self._maildir_dir, "dest/cur"))[0]
self.assertNotEqual(filename, "a:2,x")
self.assert_(filename.endswith(":2,x"))
def test_copy(self):
rc = '''
for mail in processor:
mail.copy("dest")
'''
self.makedirs(["incoming", "dest"])
self.create("a", "incoming/cur/a")
self.create("a", "incoming/new/b")
self.run_mdp(rc, ["incoming"])
self.verify_result(
{"incoming/cur": 1,
"incoming/new": 1,
"dest/cur": 1,
"dest/new": 1})
def test_copy_keeps_flags(self):
rc = '''
for mail in processor:
mail.copy("dest")
'''
self.makedirs(["incoming", "dest"])
self.create("a", "incoming/cur/a:2,x")
self.run_mdp(rc, ["incoming"])
self.verify_result({"incoming/cur": 1, "dest/cur": 1})
filename = os.listdir(joinpath(self._maildir_dir, "dest/cur"))[0]
self.assertNotEqual(filename, "a:2,x")
self.assert_(filename.endswith(":2,x"))
def test_forward(self):
rc = '''
for mail in processor:
mail.forward("foo@example.com")
'''
self.makedirs(["incoming"])
self.create("a", "incoming/new/a")
self.run_mdp(rc, ["incoming"])
self.verify_result({})
expected = ["foo@example.com\n"] + self._testmail_data_a
fp = open(joinpath(self._testdata_dir, "delivered"))
lines = fp.readlines()
self.assertEqual(lines, expected)
def test_forward_copy(self):
rc = '''
for mail in processor:
mail.forward_copy("foo@example.com")
'''
self.makedirs(["incoming"])
self.create("a", "incoming/new/a")
self.run_mdp(rc, ["incoming"])
self.verify_result({"incoming/new": 1})
expected = ["foo@example.com\n"] + self._testmail_data_a
fp = open(joinpath(self._testdata_dir, "delivered"))
lines = fp.readlines()
self.assertEqual(lines, expected)
def test_delete(self):
rc = '''
for mail in processor:
mail.delete()
'''
self.makedirs(["incoming"])
self.create("a", "incoming/new/a")
self.run_mdp(rc, ["incoming"])
self.verify_result({})
def test_multiple_mail(self):
rc = '''
for mail in processor:
mail.move("dest")
'''
self.makedirs(["incoming", "dest"])
for i in range(10):
self.create("a", "incoming/new/%d" % i)
self.run_mdp(rc, ["incoming"])
self.verify_result({"dest/new": 10})
###########################################################################
# Mail attributes.
def test_mail_path(self):
rc = '''
for mail in processor:
if mail.path.endswith("incoming/new/a"):
mail.move("dest")
'''
self.makedirs(["incoming", "dest"])
self.create("a", "incoming/new/a")
self.run_mdp(rc, ["incoming"])
self.verify_result({"dest/new": 1})
###########################################################################
# Headers.
def test_ascii_header(self):
rc = '''
for mail in processor:
if mail["From"] == "Sender <sender@example.com>":
mail.move("dest")
'''
self.makedirs(["incoming", "dest"])
self.create("a", "incoming/new/a")
self.run_mdp(rc, ["incoming"])
self.verify_result({"dest/new": 1})
def test_utf8_header(self):
rc = self.fix_u('''
for mail in processor:
if mail["Subject"] == __u__"A subject with r\\xe4vsm\\xf6rg\\xe5s":
mail.move("dest")
''')
self.makedirs(["incoming", "dest"])
self.create("a", "incoming/new/a")
self.run_mdp(rc, ["incoming"])
self.verify_result({"dest/new": 1})
def test_bad_header(self):
rc = self.fix_u('''
for mail in processor:
if mail["subject"] == __u__"=?ISO-2022-JP?B?gYqBiQ=?=":
mail.move("dest")
''')
self.makedirs(["incoming", "dest"])
self.create("bad_header", "incoming/new/bad_header")
self.run_mdp(rc, ["incoming"])
self.verify_result({"dest/new": 1})
###########################################################################
# Matchers.
def test_negative_header_matches(self):
rc = '''
for mail in processor:
if mail["from"].matches("^sender@example\\.com"):
mail.move("dest")
'''
self.makedirs(["incoming", "dest"])
self.create("a", "incoming/new/a")
self.run_mdp(rc, ["incoming"])
self.verify_result({"incoming/new": 1})
def test_positive_header_matches(self):
rc = '''
for mail in processor:
if mail["from"].matches("sender@example\\.com"):
mail.move("dest")
'''
self.makedirs(["incoming", "dest"])
self.create("a", "incoming/new/a")
self.run_mdp(rc, ["incoming"])
self.verify_result({"dest/new": 1})
def test_negative_header_contains(self):
rc = '''
for mail in processor:
if mail["from"].contains("foo"):
mail.move("dest")
'''
self.makedirs(["incoming", "dest"])
self.create("a", "incoming/new/a")
self.run_mdp(rc, ["incoming"])
self.verify_result({"incoming/new": 1})
def test_positive_header_contains(self):
rc = '''
for mail in processor:
if mail["from"].contains("sender@example.com"):
mail.move("dest")
'''
self.makedirs(["incoming", "dest"])
self.create("a", "incoming/new/a")
self.run_mdp(rc, ["incoming"])
self.verify_result({"dest/new": 1})
def test_target_contains(self):
rc = '''
for mail in processor:
if mail.target.contains("recipient@example.com"):
mail.copy("dest")
if mail.target.contains("carbon.copy@example.com"):
mail.move("dest")
'''
self.makedirs(["incoming", "dest"])
self.create("a", "incoming/new/a")
self.run_mdp(rc, ["incoming"])
self.verify_result({"dest/new": 2})
def test_target_matches(self):
rc = '''
for mail in processor:
if mail.target.matches("recipient@example\\.com"):
mail.copy("dest")
if mail.target.matches("carbon\\.copy@example\\.com"):
mail.move("dest")
'''
self.makedirs(["incoming", "dest"])
self.create("a", "incoming/new/a")
self.run_mdp(rc, ["incoming"])
self.verify_result({"dest/new": 2})
def test_from_mailing_list(self):
rc = '''
for mail in processor:
if mail.from_mailing_list("recipient@example.com"):
mail.copy("to")
if mail.from_mailing_list("carbon.copy@example.com"):
mail.copy("cc")
if mail.from_mailing_list("alpha"):
mail.copy("delivered-to")
if mail.from_mailing_list("bravo"):
mail.copy("x-beenthere")
if mail.from_mailing_list("charlie"):
mail.copy("x-mailing-list")
if mail.from_mailing_list("delta"):
mail.copy("mailing-list")
'''
self.makedirs(
["incoming", "to", "cc", "delivered-to",
"x-beenthere", "x-mailing-list", "mailing-list"])
self.create("a", "incoming/new/a")
self.run_mdp(rc, ["incoming"])
self.verify_result({
"incoming/new": 1,
"delivered-to/new": 1,
"x-beenthere/new": 1,
"x-mailing-list/new": 1,
"mailing-list/new": 1,
})
class Python2TestSuite(TestSuite):
def __init__(self, method_name):
TestSuite.__init__(self, method_name, True, "maildirproc")
class Python3TestSuite(TestSuite):
def __init__(self, method_name):
TestSuite.__init__(self, method_name, False, "maildirproc-python3")
######################################################################
test_runner = unittest.TextTestRunner()
test_loader = unittest.defaultTestLoader
test_suite = unittest.TestSuite()
tests = [Python2TestSuite, Python3TestSuite]
args = sys.argv[1:]
if args:
for arg in args:
for test in tests:
test_suite.addTest(test(arg))
else:
for test in tests:
test_suite.addTests(test_loader.loadTestsFromTestCase(test))
result = test_runner.run(test_suite)
sys.exit(0 if result.wasSuccessful() else 1)