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
|
import datetime
import logging
import email
import unittest
from dateutil.tz import tzoffset
from rply import Token
import parse_received
logging.basicConfig(level=logging.DEBUG)
INPUT_01 = \
"""from server.mymailhost.com
(mail.mymailhost.com [126.43.75.123])
by pilot01.cl.msu.edu (8.10.2/8.10.2) with ESMTP id NAA23597;
Fri, 12 Jul 2002 16:11:20 -0400 (EDT)"""
INPUT_02 = \
"""from d1080.master.cz (p-lab.cz [89.185.245.149] (may be forged))
by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t07GaC1j031854
for <mcepl@redhat.com>; Wed, 7 Jan 2015 11:36:13 -0500"""
class TestReceivedLexer(unittest.TestCase):
maxDiff = None
def test_simple_01(self):
expected = [
Token('FROMSEP', 'from'),
Token('DOMAIN', 'server.mymailhost.com'),
Token('DOMAIN', 'mail.mymailhost.com'),
Token('IPV4ADDRESS', '[126.43.75.123]'),
Token('BYSEP', 'by'),
Token('DOMAIN', 'pilot01.cl.msu.edu'),
Token('SMTPVERSION', '8.10.2/8.10.2'),
Token('WITHSEP', 'with'),
Token('SMTPSEP', 'ESMTP'),
Token('IDSEP', 'id'),
Token('STRING', 'NAA23597;'),
Token('DATETIME', 'Fri, 12 Jul 2002 16:11:20 -0400 (EDT)')
]
parsed = list(parse_received.lexer.lex(INPUT_01))
self.assertEqual(parsed, expected)
def test_simple_02(self):
expected = [
Token('FROMSEP', 'from'),
Token('DOMAIN', 'd1080.master.cz'),
Token('DOMAIN', 'p-lab.cz'),
Token('IPV4ADDRESS', '[89.185.245.149]'),
Token('IGNORABLESTR', 'may be forged'),
Token('BYSEP', 'by'),
Token('DOMAIN', 'mx1.redhat.com'),
Token('SMTPVERSION', '8.14.4/8.14.4'),
Token('WITHSEP', 'with'),
Token('SMTPSEP', 'ESMTP'),
Token('IDSEP', 'id'),
Token('STRING', 't07GaC1j031854'),
Token('FORSEP', 'for'),
Token('EMAILADDR', '<mcepl@redhat.com>'),
Token('DATETIME', 'Wed, 7 Jan 2015 11:36:13 -0500')
]
parsed = list(parse_received.lexer.lex(INPUT_02))
self.assertEqual(parsed, expected)
class TestReceivedParser(unittest.TestCase):
maxDiff = None
def test_simple_01(self):
expected = [
[Token('FROMSEP', 'from'),
[Token('DOMAIN', 'server.mymailhost.com'),
[Token('DOMAIN', 'mail.mymailhost.com'),
Token('IPV4ADDRESS', '[126.43.75.123]')]]
],
[Token('BYSEP', 'by'),
[Token('DOMAIN', 'pilot01.cl.msu.edu'),
[Token('SMTPVERSION', '8.10.2/8.10.2')]]
],
[[[],
[Token('SMTPSEP', 'ESMTP')],
[Token('STRING', 'NAA23597;')], []]
],
Token('DATETIME', 'Fri, 12 Jul 2002 16:11:20 -0400 (EDT)')
]
stream = parse_received.lexer.lex(INPUT_01)
stream.idx = 0
parsed = parse_received.parser.parse(stream)
self.assertEqual(parsed, expected)
def test_simple_02(self):
expected = [
[Token('FROMSEP', 'from'),
[Token('DOMAIN', 'd1080.master.cz'),
[Token('DOMAIN', 'p-lab.cz'),
Token('IPV4ADDRESS', '[89.185.245.149]')]]
],
[Token('BYSEP', 'by'),
[Token('DOMAIN', 'mx1.redhat.com'),
[Token('SMTPVERSION', '8.14.4/8.14.4')]]
],
[[[],
[Token('SMTPSEP', 'ESMTP')],
[Token('STRING', 't07GaC1j031854')],
[Token('FORSEP', 'for')]]
],
Token('DATETIME', 'Wed, 7 Jan 2015 11:36:13 -0500')
]
stream = parse_received.lexer.lex(INPUT_02)
stream.idx = 0
parsed = parse_received.parser.parse(stream)
self.assertEqual(parsed, expected)
def test_parse_01(self):
expected = {
'by': {'server': 'pilot01.cl.msu.edu'},
'date': datetime.datetime(
2002, 7, 12, 16, 11, 20, tzinfo=tzoffset(u'EDT', -14400)),
'from': {
'halo': 'server.mymailhost.com',
'ipaddr': '[126.43.75.123]',
'reveresed': 'mail.mymailhost.com'
}
}
observed = parse_received.parse_header(INPUT_01)
self.assertEqual(observed, expected)
def test_parse_02(self):
expected = {
'by': {'server': 'mx1.redhat.com'},
'date': datetime.datetime(
2015, 1, 7, 11, 36, 13, tzinfo=tzoffset(None, -18000)),
'from': {
'halo': 'd1080.master.cz',
'ipaddr': '[89.185.245.149]',
'reveresed': 'p-lab.cz'
}
}
observed = parse_received.parse_header(INPUT_02)
self.assertEqual(observed, expected)
class TestEmailHeaderParser(unittest.TestCase):
maxDiff = None
def test_email_01(self):
with open('test/samples/testing-email-01.eml', 'r') as inmail:
msg = email.message_from_file(inmail)
received_hdrs = msg.get_all('Received')
expected = []
parsed_headers = []
for hdr in received_hdrs:
logging.debug('hdr:\n%s', hdr)
parsed_headers.append(parse_received.parse_header(hdr))
self.assertEqual(parsed_headers, expected)
def test_email_02(self):
with open('examples/mail', 'r') as inmail:
msg = email.message_from_file(inmail)
received_hdrs = msg.get_all('Received')
expected = []
parsed_headers = []
for hdr in received_hdrs:
logging.debug('hdr:\n%s', hdr)
parsed_headers.append(parse_received.parse_header(hdr))
self.assertEqual(parsed_headers, expected)
if __name__ == '__main__':
unittest.main()
|