-
Notifications
You must be signed in to change notification settings - Fork 21
/
testutils.py
64 lines (55 loc) · 2.03 KB
/
testutils.py
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
from __future__ import print_function
import unittest
import doctest
import os
import Milter.utils
from Milter.cache import AddrCache
from Milter.dynip import is_dynip
from Milter.pyip6 import inet_ntop
class AddrCacheTestCase(unittest.TestCase):
def setUp(self):
self.fname = 'test.dat'
def tearDown(self):
if os.path.exists(self.fname):
os.remove(self.fname)
def testAdd(self):
cache = AddrCache(fname=self.fname)
cache['[email protected]'] = None
cache.addperm('[email protected]')
cache['[email protected]'] = 'testing'
self.assertTrue(cache.has_key('[email protected]'))
self.assertTrue(not cache.has_key('[email protected]'))
self.assertTrue('[email protected]' in cache)
self.assertEqual(cache['[email protected]'],'testing')
s = open(self.fname).readlines()
self.assertTrue(len(s) == 2)
self.assertTrue(s[0].startswith('[email protected] '))
self.assertEqual(s[1].strip(),'[email protected]')
# check that new result overrides old
cache['[email protected]'] = None
self.assertTrue(not cache['[email protected]'])
def testDomain(self):
with open(self.fname,'w') as fp:
print('spammer.com',file=fp)
cache = AddrCache(fname=self.fname)
cache.load(self.fname,30)
self.assertTrue('spammer.com' in cache)
def testParseHeader(self):
s='=?UTF-8?B?TGFzdCBGZXcgQ29sZHBsYXkgQWxidW0gQXJ0d29ya3MgQXZhaWxhYmxlAA?='
h = Milter.utils.parse_header(s)
self.assertEqual(h,'Last Few Coldplay Album Artworks Available\x00')
s='=?iso-8859-1?Q?Peter_=D8rum?= <[email protected]>'
h = Milter.utils.parse_header(s)
self.assertEqual(h,'Peter \xd8rum <[email protected]>')
@unittest.expectedFailure
def testParseAddress(self):
s = Milter.utils.parseaddr('a(WRONG)@b')
self.assertEqual(s,('WRONG', 'a@b'))
def suite():
s = unittest.TestLoader().loadTestsFromTestCase(AddrCacheTestCase)
s.addTest(doctest.DocTestSuite(Milter.utils))
s.addTest(doctest.DocTestSuite(Milter.dynip))
s.addTest(doctest.DocTestSuite(Milter.pyip6))
return s
if __name__ == '__main__':
unittest.TextTestRunner().run(suite())