2
# -*- test-case-name: twisted.mail.test.test_pop3client -*-
4
from twisted.internet.protocol import Factory
5
from twisted.protocols import basic
6
from twisted.internet import reactor
16
INVALID_SERVER_RESPONSE = False
17
INVALID_CAPABILITY_RESPONSE = False
18
INVALID_LOGIN_RESPONSE = False
19
DENY_CONNECTION = False
20
DROP_CONNECTION = False
21
BAD_TLS_RESPONSE = False
22
TIMEOUT_RESPONSE = False
23
TIMEOUT_DEFERRED = False
27
CONNECTION_MADE = "+OK POP3 localhost v2003.83 server ready"
36
CAPABILITIES_SSL = "STLS"
37
CAPABILITIES_UIDL = "UIDL"
40
INVALID_RESPONSE = "-ERR Unknown request"
41
VALID_RESPONSE = "+OK Command Completed"
42
AUTH_DECLINED = "-ERR LOGIN failed"
43
AUTH_ACCEPTED = "+OK Mailbox open, 0 messages"
44
TLS_ERROR = "-ERR server side error start TLS handshake"
45
LOGOUT_COMPLETE = "+OK quit completed"
46
NOT_LOGGED_IN = "-ERR Unknown AUHORIZATION state command"
48
UIDL = "+OK Unique-ID listing follows\r\n."
49
LIST = "+OK Mailbox scan listing follows\r\n."
50
CAP_START = "+OK Capability list follows:"
53
class POP3TestServer(basic.LineReceiver):
54
def __init__(self, contextFactory = None):
58
self.ctx = contextFactory
60
def sendSTATResp(self, req):
63
def sendUIDLResp(self, req):
66
def sendLISTResp(self, req):
69
def sendCapabilities(self):
71
self.caps = [CAP_START]
74
self.caps.append(CAPABILITIES_UIDL)
77
self.caps.append(CAPABILITIES_SSL)
79
for cap in CAPABILITIES:
81
resp = '\r\n'.join(self.caps)
87
def connectionMade(self):
93
reactor.callLater(20, self.sendGreeting)
98
def sendGreeting(self):
99
self.sendLine(CONNECTION_MADE)
101
def lineReceived(self, line):
102
"""Error Conditions"""
105
find = lambda s: uline.find(s) != -1
108
# Do not respond to clients request
116
if INVALID_CAPABILITY_RESPONSE:
117
self.sendLine(INVALID_RESPONSE)
119
self.sendCapabilities()
121
elif find("STLS") and SSL_SUPPORT:
125
if INVALID_LOGIN_RESPONSE:
126
self.sendLine(INVALID_RESPONSE)
131
self.tmpUser = line.split(" ")[1]
132
resp = VALID_RESPONSE
141
pwd = line.split(" ")[1]
143
if self.tmpUser is None or pwd is None:
145
elif self.tmpUser == USER and pwd == PASS:
156
self.loggedIn = False
157
self.sendLine(LOGOUT_COMPLETE)
160
elif INVALID_SERVER_RESPONSE:
161
self.sendLine(INVALID_RESPONSE)
163
elif not self.loggedIn:
164
self.sendLine(NOT_LOGGED_IN)
167
self.sendLine(VALID_RESPONSE)
182
elif not UIDL_SUPPORT:
183
self.sendLine(INVALID_RESPONSE)
192
if SSL_SUPPORT and self.ctx is not None:
193
self.sendLine('+OK Begin TLS negotiation now')
194
self.transport.startTLS(self.ctx)
196
self.sendLine('-ERR TLS not available')
198
def disconnect(self):
199
self.transport.loseConnection()
201
def getContext(self):
203
from twisted.internet import ssl
207
self.ctx = ssl.ClientContextFactory()
208
self.ctx.method = ssl.SSL.TLSv1_METHOD
211
usage = """popServer.py [arg] (default is Standard POP Server with no messages)
212
no_ssl - Start with no SSL support
213
no_uidl - Start with no UIDL support
214
bad_resp - Send a non-RFC compliant response to the Client
215
bad_cap_resp - send a non-RFC compliant response when the Client sends a 'CAPABILITY' request
216
bad_login_resp - send a non-RFC compliant response when the Client sends a 'LOGIN' request
217
deny - Deny the connection
218
drop - Drop the connection after sending the greeting
219
bad_tls - Send a bad response to a STARTTLS
220
timeout - Do not return a response to a Client request
221
to_deferred - Do not return a response on a 'Select' request. This
222
will test Deferred callback handling
223
slow - Wait 20 seconds after the connection is made to return a Server Greeting
226
def printMessage(msg):
227
print "Server Starting in %s mode" % msg
231
if arg.lower() == 'no_ssl':
234
printMessage("NON-SSL")
236
elif arg.lower() == 'no_uidl':
239
printMessage("NON-UIDL")
241
elif arg.lower() == 'bad_resp':
242
global INVALID_SERVER_RESPONSE
243
INVALID_SERVER_RESPONSE = True
244
printMessage("Invalid Server Response")
246
elif arg.lower() == 'bad_cap_resp':
247
global INVALID_CAPABILITY_RESPONSE
248
INVALID_CAPABILITY_RESPONSE = True
249
printMessage("Invalid Capability Response")
251
elif arg.lower() == 'bad_login_resp':
252
global INVALID_LOGIN_RESPONSE
253
INVALID_LOGIN_RESPONSE = True
254
printMessage("Invalid Capability Response")
256
elif arg.lower() == 'deny':
257
global DENY_CONNECTION
258
DENY_CONNECTION = True
259
printMessage("Deny Connection")
261
elif arg.lower() == 'drop':
262
global DROP_CONNECTION
263
DROP_CONNECTION = True
264
printMessage("Drop Connection")
267
elif arg.lower() == 'bad_tls':
268
global BAD_TLS_RESPONSE
269
BAD_TLS_RESPONSE = True
270
printMessage("Bad TLS Response")
272
elif arg.lower() == 'timeout':
273
global TIMEOUT_RESPONSE
274
TIMEOUT_RESPONSE = True
275
printMessage("Timeout Response")
277
elif arg.lower() == 'to_deferred':
278
global TIMEOUT_DEFERRED
279
TIMEOUT_DEFERRED = True
280
printMessage("Timeout Deferred Response")
282
elif arg.lower() == 'slow':
285
printMessage("Slow Greeting")
287
elif arg.lower() == '--help':
297
if len(sys.argv) < 2:
298
printMessage("POP3 with no messages")
306
f.protocol = POP3TestServer
307
reactor.listenTCP(PORT, f)
310
if __name__ == '__main__':