-
Notifications
You must be signed in to change notification settings - Fork 33
/
testpop.py
executable file
·266 lines (219 loc) · 7.93 KB
/
testpop.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
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
#!/usr/bin/python
# Copyright (C) 2004 Paul J Stevens paul at nfg dot nl
#
# This program 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 2 of the License, or (at your option) any later
# version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# For a protocol trace set to 4
DEBUG = 0
#DEBUG = 4
# select 'stream' for non-forking mode
#TYPE = 'stream'
# select 'network' for forking mode
TYPE = 'network'
import unittest
import poplib
import string
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email import message_from_string
unimplementedError = 'Dbmail testcase unimplemented'
# for network testing
HOST, PORT = "localhost", 10110
# for stdin/stdout testing
DAEMONBIN = "./dbmail-pop3d -n -f /etc/dbmail/dbmail-test.conf"
# with valgrind
#DAEMONBIN = "valgrind --leak-check=full ./dbmail-imapd -n /etc/dbmail/dbmail-test.conf"
TESTMSG = {}
def getMessageStrict():
m = MIMEText("""
this is a test message
""")
m.add_header("To", "testuser@foo.org")
m.add_header("From", "somewher@foo.org")
m.add_header("Date", "Mon, 26 Sep 2005 13:26:39 +0200")
m.add_header("Subject", "dbmail test message")
return m
def getMultiPart():
m = MIMEMultipart()
m.attach(getMessageStrict())
m.add_header("To", "testaddr@bar.org")
m.add_header("From", "testuser@foo.org")
m.add_header("Date", "Sun, 25 Sep 2005 13:26:39 +0200")
m.add_header("Subject", "dbmail multipart message")
return m
TESTMSG['strict822'] = getMessageStrict()
TESTMSG['multipart'] = getMultiPart()
def getsock():
if TYPE == 'network':
return poplib.POP3(HOST, PORT)
elif TYPE == 'stream':
return poplib.POP3_stream(DAEMONBIN)
def strip_crlf(s):
return string.replace(s, '\r', '')
class testPopServer(unittest.TestCase):
def setUp(self, username="testuser1", password="test"):
self.o = getsock()
self.o.set_debuglevel(DEBUG)
self.o.user(username)
return self.o.pass_(password)
def test_getwelcome(self):
"""
`getwelcome()'
Returns the greeting string sent by the POP3 server.
"""
self.assertEquals(self.o.getwelcome()[:4], '+OK ')
def test_user(self):
"""
`user(username)'
Send user command, response should indicate that a password is
required.
"""
o = getsock()
o.set_debuglevel(DEBUG)
self.assertEquals(o.user('foobar')[:12], "+OK Password")
def test_pass(self):
"""
`pass_(password)'
Send password, response includes message count and mailbox size.
Note: the mailbox on the server is locked until `quit()' is
called.
"""
o = getsock()
o.set_debuglevel(DEBUG)
o.user('testuser2')
self.assertEquals(o.pass_('test')[:13], "+OK testuser2")
def test_apop(self):
"""
`apop(user, secret)'
Use the more secure APOP authentication to log into the POP3
server.
"""
def test_rpop(self):
"""
`rpop(user)'
Use RPOP authentication (similar to UNIX r-commands) to log into
POP3 server.
"""
def test_stat(self):
"""
`stat()'
Get mailbox status. The result is a tuple of 2 integers:
`(MESSAGE COUNT, MAILBOX SIZE)'.
"""
tpl = self.o.stat()
self.assertEquals(len(tpl), 2)
self.assertEquals(type(tpl[0]), type(123))
def test_list(self):
"""
`list([which])'
Request message list, result is in the form `(RESPONSE, ['mesg_num
octets', ...])'. If WHICH is set, it is the message to list.
"""
l = self.o.list()
self.assertEquals(l[0][:3], "+OK")
count = string.split(l[0])[1]
n, s = string.split(l[1][int(count) - 1])
self.assertEquals(self.o.list(n), "+OK %s %s" % (n, s))
def test_retr(self):
"""
`retr(which)'
Retrieve whole message number WHICH, and set its seen flag.
Result is in form `(RESPONSE, ['line', ...], OCTETS)'.
"""
l = self.o.list()
count = string.split(l[0])[1]
n, s = string.split(l[1][int(count) - 1])
for i in range(1, int(n)):
result = self.o.retr(i)
r = string.split(result[0])
self.assertEquals(r[0], "+OK")
message = '\r\n'.join(result[1])
expectedlen = len(message)
bytestuffed = [x for x in result[1] if x.startswith('.')]
expectedlen = expectedlen + len(bytestuffed)
self.assertEquals(int(r[1]),
expectedlen,
"[%s] msgid %d octets %s message %d" % (
result[1], i, r[1], expectedlen))
def test_dele(self):
"""
`dele(which)'
Flag message number WHICH for deletion. On most servers deletions
are not actually performed until QUIT (the major exception is
Eudora QPOP, which deliberately violates the RFCs by doing pending
deletes on any disconnect).
"""
self.assertEquals(self.o.dele(1), '+OK message 1 deleted')
self.o.rset()
def test_rset(self):
"""
`rset()'
Remove any deletion marks for the mailbox.
"""
result = self.o.rset()
self.assertEquals(result[:4], '+OK ')
def test_noop(self):
"""
`noop()'
Do nothing. Might be used as a keep-alive.
"""
self.assertEquals(self.o.noop()[:3], '+OK')
def test_quit(self):
"""
`quit()'
Signoff: commit changes, unlock mailbox, drop connection.
"""
def test_top(self):
"""
`top(which, howmuch)'
Retrieves the message header plus HOWMUCH lines of the message
after the header of message number WHICH. Result is in form
`(RESPONSE, ['line', ...], OCTETS)'.
The POP3 TOP command this method uses, unlike the RETR command,
doesn't set the message's seen flag; unfortunately, TOP is poorly
specified in the RFCs and is frequently broken in off-brand
servers. Test this method by hand against the POP3 servers you
will use before trusting it.
"""
result = self.o.top(1, 10)
self.assertEquals(result[0], '+OK 10 lines of message 1')
result = self.o.top(1, 0)
msg = message_from_string('\r\n'.join(result[1]))
self.assertEquals(msg.get_payload(), "")
result = self.o.top(1, 2)
msg = message_from_string('\r\n'.join(result[1]))
self.assertEquals(len(msg.get_payload().split('\r\n')), 3)
def test_uidl(self):
"""
`uidl([which])'
Return message digest (unique id) list. If WHICH is specified,
result contains the unique id for that message in the form
`'RESPONSE MESGNUM UID', otherwise result is list `(RESPONSE,
['mesgnum uid', ...], OCTETS)'.
"""
all = self.o.uidl()
self.assertEquals(all[0][:4], '+OK ')
one = self.o.uidl(1)
self.assertEquals(one[4:], all[1][0])
def tearDown(self):
self.o.rset()
self.o.quit()
def usage():
print """testpop.py: test dbmail imapserver
"""
if __name__ == '__main__':
unittest.main()
#EOF