blob: cf961aec2d1ce766a9d26d706c3424f647d96c21 [file] [log] [blame]
Benjamin Petersonbe17a112008-09-27 21:49:47 +00001"""Test script for ftplib module."""
2
Antoine Pitrouf988cd02009-11-17 20:21:14 +00003# Modified by Giampaolo Rodola' to test FTP class, IPv6 and TLS
4# environment
Benjamin Petersonbe17a112008-09-27 21:49:47 +00005
Guido van Rossumd8faa362007-04-27 19:54:29 +00006import ftplib
Benjamin Petersonbe17a112008-09-27 21:49:47 +00007import asyncore
8import asynchat
9import socket
10import io
Antoine Pitrouf988cd02009-11-17 20:21:14 +000011import errno
12import os
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +000013import time
Antoine Pitrouf988cd02009-11-17 20:21:14 +000014try:
15 import ssl
16except ImportError:
17 ssl = None
Guido van Rossumd8faa362007-04-27 19:54:29 +000018
19from unittest import TestCase
Benjamin Petersonee8712c2008-05-20 21:35:26 +000020from test import support
Benjamin Petersonbe17a112008-09-27 21:49:47 +000021from test.support import HOST
Victor Stinner45df8202010-04-28 22:31:17 +000022threading = support.import_module('threading')
Guido van Rossumd8faa362007-04-27 19:54:29 +000023
Benjamin Petersonbe17a112008-09-27 21:49:47 +000024# the dummy data returned by server over the data channel when
Giampaolo Rodola'd78def92011-05-06 19:49:08 +020025# RETR, LIST, NLST, MLSD commands are issued
Benjamin Petersonbe17a112008-09-27 21:49:47 +000026RETR_DATA = 'abcde12345\r\n' * 1000
27LIST_DATA = 'foo\r\nbar\r\n'
28NLST_DATA = 'foo\r\nbar\r\n'
Giampaolo Rodola'd78def92011-05-06 19:49:08 +020029MLSD_DATA = ("type=cdir;perm=el;unique==keVO1+ZF4; test\r\n"
30 "type=pdir;perm=e;unique==keVO1+d?3; ..\r\n"
31 "type=OS.unix=slink:/foobar;perm=;unique==keVO1+4G4; foobar\r\n"
32 "type=OS.unix=chr-13/29;perm=;unique==keVO1+5G4; device\r\n"
33 "type=OS.unix=blk-11/108;perm=;unique==keVO1+6G4; block\r\n"
34 "type=file;perm=awr;unique==keVO1+8G4; writable\r\n"
35 "type=dir;perm=cpmel;unique==keVO1+7G4; promiscuous\r\n"
36 "type=dir;perm=;unique==keVO1+1t2; no-exec\r\n"
37 "type=file;perm=r;unique==keVO1+EG4; two words\r\n"
38 "type=file;perm=r;unique==keVO1+IH4; leading space\r\n"
39 "type=file;perm=r;unique==keVO1+1G4; file1\r\n"
40 "type=dir;perm=cpmel;unique==keVO1+7G4; incoming\r\n"
41 "type=file;perm=r;unique==keVO1+1G4; file2\r\n"
42 "type=file;perm=r;unique==keVO1+1G4; file3\r\n"
43 "type=file;perm=r;unique==keVO1+1G4; file4\r\n")
Christian Heimes836baa52008-02-26 08:18:30 +000044
Christian Heimes836baa52008-02-26 08:18:30 +000045
Benjamin Petersonbe17a112008-09-27 21:49:47 +000046class DummyDTPHandler(asynchat.async_chat):
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +000047 dtp_conn_closed = False
Benjamin Petersonbe17a112008-09-27 21:49:47 +000048
49 def __init__(self, conn, baseclass):
50 asynchat.async_chat.__init__(self, conn)
51 self.baseclass = baseclass
52 self.baseclass.last_received_data = ''
53
54 def handle_read(self):
Giampaolo Rodolàf96482e2010-08-04 10:36:18 +000055 self.baseclass.last_received_data += self.recv(1024).decode('ascii')
Benjamin Petersonbe17a112008-09-27 21:49:47 +000056
57 def handle_close(self):
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +000058 # XXX: this method can be called many times in a row for a single
59 # connection, including in clear-text (non-TLS) mode.
60 # (behaviour witnessed with test_data_connection)
61 if not self.dtp_conn_closed:
62 self.baseclass.push('226 transfer complete')
63 self.close()
64 self.dtp_conn_closed = True
Benjamin Petersonbe17a112008-09-27 21:49:47 +000065
66 def push(self, what):
Giampaolo Rodola'd78def92011-05-06 19:49:08 +020067 if self.baseclass.next_data is not None:
68 what = self.baseclass.next_data
69 self.baseclass.next_data = None
70 if not what:
71 return self.close_when_done()
Giampaolo Rodolàf96482e2010-08-04 10:36:18 +000072 super(DummyDTPHandler, self).push(what.encode('ascii'))
Benjamin Petersonbe17a112008-09-27 21:49:47 +000073
Giampaolo Rodolàd930b632010-05-06 20:21:57 +000074 def handle_error(self):
75 raise
76
Benjamin Petersonbe17a112008-09-27 21:49:47 +000077
78class DummyFTPHandler(asynchat.async_chat):
79
Antoine Pitrouf988cd02009-11-17 20:21:14 +000080 dtp_handler = DummyDTPHandler
81
Benjamin Petersonbe17a112008-09-27 21:49:47 +000082 def __init__(self, conn):
83 asynchat.async_chat.__init__(self, conn)
84 self.set_terminator(b"\r\n")
85 self.in_buffer = []
86 self.dtp = None
87 self.last_received_cmd = None
88 self.last_received_data = ''
89 self.next_response = ''
Giampaolo Rodola'd78def92011-05-06 19:49:08 +020090 self.next_data = None
Antoine Pitrou648bcd72009-11-27 13:23:26 +000091 self.rest = None
Benjamin Petersonbe17a112008-09-27 21:49:47 +000092 self.push('220 welcome')
93
94 def collect_incoming_data(self, data):
95 self.in_buffer.append(data)
96
97 def found_terminator(self):
98 line = b''.join(self.in_buffer).decode('ascii')
99 self.in_buffer = []
100 if self.next_response:
101 self.push(self.next_response)
102 self.next_response = ''
103 cmd = line.split(' ')[0].lower()
104 self.last_received_cmd = cmd
105 space = line.find(' ')
106 if space != -1:
107 arg = line[space + 1:]
108 else:
109 arg = ""
110 if hasattr(self, 'cmd_' + cmd):
111 method = getattr(self, 'cmd_' + cmd)
112 method(arg)
113 else:
114 self.push('550 command "%s" not understood.' %cmd)
115
116 def handle_error(self):
117 raise
118
119 def push(self, data):
120 asynchat.async_chat.push(self, data.encode('ascii') + b'\r\n')
121
122 def cmd_port(self, arg):
123 addr = list(map(int, arg.split(',')))
124 ip = '%d.%d.%d.%d' %tuple(addr[:4])
125 port = (addr[4] * 256) + addr[5]
Antoine Pitroud778e562010-10-14 20:35:26 +0000126 s = socket.create_connection((ip, port), timeout=10)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000127 self.dtp = self.dtp_handler(s, baseclass=self)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000128 self.push('200 active data connection established')
129
130 def cmd_pasv(self, arg):
Brett Cannon918e2d42010-10-29 23:26:25 +0000131 with socket.socket() as sock:
132 sock.bind((self.socket.getsockname()[0], 0))
133 sock.listen(5)
134 sock.settimeout(10)
135 ip, port = sock.getsockname()[:2]
136 ip = ip.replace('.', ','); p1 = port / 256; p2 = port % 256
137 self.push('227 entering passive mode (%s,%d,%d)' %(ip, p1, p2))
138 conn, addr = sock.accept()
139 self.dtp = self.dtp_handler(conn, baseclass=self)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000140
141 def cmd_eprt(self, arg):
142 af, ip, port = arg.split(arg[0])[1:-1]
143 port = int(port)
Antoine Pitroud778e562010-10-14 20:35:26 +0000144 s = socket.create_connection((ip, port), timeout=10)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000145 self.dtp = self.dtp_handler(s, baseclass=self)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000146 self.push('200 active data connection established')
147
148 def cmd_epsv(self, arg):
Brett Cannon918e2d42010-10-29 23:26:25 +0000149 with socket.socket(socket.AF_INET6) as sock:
150 sock.bind((self.socket.getsockname()[0], 0))
151 sock.listen(5)
152 sock.settimeout(10)
153 port = sock.getsockname()[1]
154 self.push('229 entering extended passive mode (|||%d|)' %port)
155 conn, addr = sock.accept()
156 self.dtp = self.dtp_handler(conn, baseclass=self)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000157
158 def cmd_echo(self, arg):
159 # sends back the received string (used by the test suite)
160 self.push(arg)
161
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000162 def cmd_noop(self, arg):
163 self.push('200 noop ok')
164
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000165 def cmd_user(self, arg):
166 self.push('331 username ok')
167
168 def cmd_pass(self, arg):
169 self.push('230 password ok')
170
171 def cmd_acct(self, arg):
172 self.push('230 acct ok')
173
174 def cmd_rnfr(self, arg):
175 self.push('350 rnfr ok')
176
177 def cmd_rnto(self, arg):
178 self.push('250 rnto ok')
179
180 def cmd_dele(self, arg):
181 self.push('250 dele ok')
182
183 def cmd_cwd(self, arg):
184 self.push('250 cwd ok')
185
186 def cmd_size(self, arg):
187 self.push('250 1000')
188
189 def cmd_mkd(self, arg):
190 self.push('257 "%s"' %arg)
191
192 def cmd_rmd(self, arg):
193 self.push('250 rmd ok')
194
195 def cmd_pwd(self, arg):
196 self.push('257 "pwd ok"')
197
198 def cmd_type(self, arg):
Giampaolo Rodolàf96482e2010-08-04 10:36:18 +0000199 self.push('200 type ok')
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000200
201 def cmd_quit(self, arg):
202 self.push('221 quit ok')
203 self.close()
204
205 def cmd_stor(self, arg):
206 self.push('125 stor ok')
207
Antoine Pitrou648bcd72009-11-27 13:23:26 +0000208 def cmd_rest(self, arg):
209 self.rest = arg
210 self.push('350 rest ok')
211
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000212 def cmd_retr(self, arg):
213 self.push('125 retr ok')
Antoine Pitrou648bcd72009-11-27 13:23:26 +0000214 if self.rest is not None:
215 offset = int(self.rest)
216 else:
217 offset = 0
Giampaolo Rodolàf96482e2010-08-04 10:36:18 +0000218 self.dtp.push(RETR_DATA[offset:])
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000219 self.dtp.close_when_done()
Antoine Pitrou648bcd72009-11-27 13:23:26 +0000220 self.rest = None
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000221
222 def cmd_list(self, arg):
223 self.push('125 list ok')
224 self.dtp.push(LIST_DATA)
225 self.dtp.close_when_done()
226
227 def cmd_nlst(self, arg):
228 self.push('125 nlst ok')
229 self.dtp.push(NLST_DATA)
230 self.dtp.close_when_done()
231
Giampaolo Rodola'd78def92011-05-06 19:49:08 +0200232 def cmd_opts(self, arg):
233 self.push('200 opts ok')
234
235 def cmd_mlsd(self, arg):
236 self.push('125 mlsd ok')
237 self.dtp.push(MLSD_DATA)
238 self.dtp.close_when_done()
239
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000240
241class DummyFTPServer(asyncore.dispatcher, threading.Thread):
242
243 handler = DummyFTPHandler
244
245 def __init__(self, address, af=socket.AF_INET):
246 threading.Thread.__init__(self)
247 asyncore.dispatcher.__init__(self)
248 self.create_socket(af, socket.SOCK_STREAM)
249 self.bind(address)
250 self.listen(5)
251 self.active = False
252 self.active_lock = threading.Lock()
253 self.host, self.port = self.socket.getsockname()[:2]
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000254 self.handler_instance = None
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000255
256 def start(self):
257 assert not self.active
258 self.__flag = threading.Event()
259 threading.Thread.start(self)
260 self.__flag.wait()
261
262 def run(self):
263 self.active = True
264 self.__flag.set()
265 while self.active and asyncore.socket_map:
266 self.active_lock.acquire()
267 asyncore.loop(timeout=0.1, count=1)
268 self.active_lock.release()
269 asyncore.close_all(ignore_all=True)
270
271 def stop(self):
272 assert self.active
273 self.active = False
274 self.join()
275
Giampaolo Rodolà977c7072010-10-04 21:08:36 +0000276 def handle_accepted(self, conn, addr):
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000277 self.handler_instance = self.handler(conn)
Benjamin Petersond06e3b02008-09-28 21:00:42 +0000278
279 def handle_connect(self):
280 self.close()
281 handle_read = handle_connect
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000282
283 def writable(self):
284 return 0
285
286 def handle_error(self):
287 raise
288
289
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000290if ssl is not None:
291
292 CERTFILE = os.path.join(os.path.dirname(__file__), "keycert.pem")
293
294 class SSLConnection(asyncore.dispatcher):
295 """An asyncore.dispatcher subclass supporting TLS/SSL."""
296
297 _ssl_accepting = False
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000298 _ssl_closing = False
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000299
300 def secure_connection(self):
301 self.del_channel()
302 socket = ssl.wrap_socket(self.socket, suppress_ragged_eofs=False,
303 certfile=CERTFILE, server_side=True,
304 do_handshake_on_connect=False,
305 ssl_version=ssl.PROTOCOL_SSLv23)
306 self.set_socket(socket)
307 self._ssl_accepting = True
308
309 def _do_ssl_handshake(self):
310 try:
311 self.socket.do_handshake()
312 except ssl.SSLError as err:
313 if err.args[0] in (ssl.SSL_ERROR_WANT_READ,
314 ssl.SSL_ERROR_WANT_WRITE):
315 return
316 elif err.args[0] == ssl.SSL_ERROR_EOF:
317 return self.handle_close()
318 raise
319 except socket.error as err:
320 if err.args[0] == errno.ECONNABORTED:
321 return self.handle_close()
322 else:
323 self._ssl_accepting = False
324
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000325 def _do_ssl_shutdown(self):
326 self._ssl_closing = True
327 try:
328 self.socket = self.socket.unwrap()
329 except ssl.SSLError as err:
330 if err.args[0] in (ssl.SSL_ERROR_WANT_READ,
331 ssl.SSL_ERROR_WANT_WRITE):
332 return
333 except socket.error as err:
334 # Any "socket error" corresponds to a SSL_ERROR_SYSCALL return
335 # from OpenSSL's SSL_shutdown(), corresponding to a
336 # closed socket condition. See also:
337 # http://www.mail-archive.com/openssl-users@openssl.org/msg60710.html
338 pass
339 self._ssl_closing = False
340 super(SSLConnection, self).close()
341
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000342 def handle_read_event(self):
343 if self._ssl_accepting:
344 self._do_ssl_handshake()
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000345 elif self._ssl_closing:
346 self._do_ssl_shutdown()
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000347 else:
348 super(SSLConnection, self).handle_read_event()
349
350 def handle_write_event(self):
351 if self._ssl_accepting:
352 self._do_ssl_handshake()
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000353 elif self._ssl_closing:
354 self._do_ssl_shutdown()
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000355 else:
356 super(SSLConnection, self).handle_write_event()
357
358 def send(self, data):
359 try:
360 return super(SSLConnection, self).send(data)
361 except ssl.SSLError as err:
Antoine Pitrou5733c082010-03-22 14:49:10 +0000362 if err.args[0] in (ssl.SSL_ERROR_EOF, ssl.SSL_ERROR_ZERO_RETURN,
363 ssl.SSL_ERROR_WANT_READ,
364 ssl.SSL_ERROR_WANT_WRITE):
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000365 return 0
366 raise
367
368 def recv(self, buffer_size):
369 try:
370 return super(SSLConnection, self).recv(buffer_size)
371 except ssl.SSLError as err:
Antoine Pitrou5733c082010-03-22 14:49:10 +0000372 if err.args[0] in (ssl.SSL_ERROR_WANT_READ,
373 ssl.SSL_ERROR_WANT_WRITE):
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000374 return b''
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000375 if err.args[0] in (ssl.SSL_ERROR_EOF, ssl.SSL_ERROR_ZERO_RETURN):
376 self.handle_close()
377 return b''
378 raise
379
380 def handle_error(self):
381 raise
382
383 def close(self):
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000384 if (isinstance(self.socket, ssl.SSLSocket) and
385 self.socket._sslobj is not None):
386 self._do_ssl_shutdown()
Benjamin Peterson1bd93a72010-10-31 19:58:07 +0000387 else:
388 super(SSLConnection, self).close()
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000389
390
391 class DummyTLS_DTPHandler(SSLConnection, DummyDTPHandler):
392 """A DummyDTPHandler subclass supporting TLS/SSL."""
393
394 def __init__(self, conn, baseclass):
395 DummyDTPHandler.__init__(self, conn, baseclass)
396 if self.baseclass.secure_data_channel:
397 self.secure_connection()
398
399
400 class DummyTLS_FTPHandler(SSLConnection, DummyFTPHandler):
401 """A DummyFTPHandler subclass supporting TLS/SSL."""
402
403 dtp_handler = DummyTLS_DTPHandler
404
405 def __init__(self, conn):
406 DummyFTPHandler.__init__(self, conn)
407 self.secure_data_channel = False
408
409 def cmd_auth(self, line):
410 """Set up secure control channel."""
411 self.push('234 AUTH TLS successful')
412 self.secure_connection()
413
414 def cmd_pbsz(self, line):
415 """Negotiate size of buffer for secure data transfer.
416 For TLS/SSL the only valid value for the parameter is '0'.
417 Any other value is accepted but ignored.
418 """
419 self.push('200 PBSZ=0 successful.')
420
421 def cmd_prot(self, line):
422 """Setup un/secure data channel."""
423 arg = line.upper()
424 if arg == 'C':
425 self.push('200 Protection set to Clear')
426 self.secure_data_channel = False
427 elif arg == 'P':
428 self.push('200 Protection set to Private')
429 self.secure_data_channel = True
430 else:
431 self.push("502 Unrecognized PROT type (use C or P).")
432
433
434 class DummyTLS_FTPServer(DummyFTPServer):
435 handler = DummyTLS_FTPHandler
436
437
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000438class TestFTPClass(TestCase):
439
440 def setUp(self):
441 self.server = DummyFTPServer((HOST, 0))
442 self.server.start()
Antoine Pitroud778e562010-10-14 20:35:26 +0000443 self.client = ftplib.FTP(timeout=10)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000444 self.client.connect(self.server.host, self.server.port)
445
446 def tearDown(self):
447 self.client.close()
448 self.server.stop()
449
450 def test_getwelcome(self):
451 self.assertEqual(self.client.getwelcome(), '220 welcome')
452
453 def test_sanitize(self):
454 self.assertEqual(self.client.sanitize('foo'), repr('foo'))
455 self.assertEqual(self.client.sanitize('pass 12345'), repr('pass *****'))
456 self.assertEqual(self.client.sanitize('PASS 12345'), repr('PASS *****'))
457
458 def test_exceptions(self):
459 self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 400')
460 self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 499')
461 self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 500')
462 self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 599')
463 self.assertRaises(ftplib.error_proto, self.client.sendcmd, 'echo 999')
464
465 def test_all_errors(self):
466 exceptions = (ftplib.error_reply, ftplib.error_temp, ftplib.error_perm,
467 ftplib.error_proto, ftplib.Error, IOError, EOFError)
468 for x in exceptions:
469 try:
470 raise x('exception not included in all_errors set')
471 except ftplib.all_errors:
472 pass
473
474 def test_set_pasv(self):
475 # passive mode is supposed to be enabled by default
476 self.assertTrue(self.client.passiveserver)
477 self.client.set_pasv(True)
478 self.assertTrue(self.client.passiveserver)
479 self.client.set_pasv(False)
480 self.assertFalse(self.client.passiveserver)
481
482 def test_voidcmd(self):
483 self.client.voidcmd('echo 200')
484 self.client.voidcmd('echo 299')
485 self.assertRaises(ftplib.error_reply, self.client.voidcmd, 'echo 199')
486 self.assertRaises(ftplib.error_reply, self.client.voidcmd, 'echo 300')
487
488 def test_login(self):
489 self.client.login()
490
491 def test_acct(self):
492 self.client.acct('passwd')
493
494 def test_rename(self):
495 self.client.rename('a', 'b')
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000496 self.server.handler_instance.next_response = '200'
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000497 self.assertRaises(ftplib.error_reply, self.client.rename, 'a', 'b')
498
499 def test_delete(self):
500 self.client.delete('foo')
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000501 self.server.handler_instance.next_response = '199'
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000502 self.assertRaises(ftplib.error_reply, self.client.delete, 'foo')
503
504 def test_size(self):
505 self.client.size('foo')
506
507 def test_mkd(self):
508 dir = self.client.mkd('/foo')
509 self.assertEqual(dir, '/foo')
510
511 def test_rmd(self):
512 self.client.rmd('foo')
513
514 def test_pwd(self):
515 dir = self.client.pwd()
516 self.assertEqual(dir, 'pwd ok')
517
518 def test_quit(self):
519 self.assertEqual(self.client.quit(), '221 quit ok')
520 # Ensure the connection gets closed; sock attribute should be None
521 self.assertEqual(self.client.sock, None)
522
523 def test_retrbinary(self):
524 def callback(data):
525 received.append(data.decode('ascii'))
526 received = []
527 self.client.retrbinary('retr', callback)
528 self.assertEqual(''.join(received), RETR_DATA)
529
Antoine Pitrou648bcd72009-11-27 13:23:26 +0000530 def test_retrbinary_rest(self):
531 def callback(data):
532 received.append(data.decode('ascii'))
533 for rest in (0, 10, 20):
534 received = []
535 self.client.retrbinary('retr', callback, rest=rest)
536 self.assertEqual(''.join(received), RETR_DATA[rest:],
537 msg='rest test case %d %d %d' % (rest,
538 len(''.join(received)),
539 len(RETR_DATA[rest:])))
540
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000541 def test_retrlines(self):
542 received = []
543 self.client.retrlines('retr', received.append)
Giampaolo Rodolàf96482e2010-08-04 10:36:18 +0000544 self.assertEqual(''.join(received), RETR_DATA.replace('\r\n', ''))
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000545
546 def test_storbinary(self):
547 f = io.BytesIO(RETR_DATA.encode('ascii'))
548 self.client.storbinary('stor', f)
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000549 self.assertEqual(self.server.handler_instance.last_received_data, RETR_DATA)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000550 # test new callback arg
551 flag = []
552 f.seek(0)
553 self.client.storbinary('stor', f, callback=lambda x: flag.append(None))
554 self.assertTrue(flag)
555
Antoine Pitrou648bcd72009-11-27 13:23:26 +0000556 def test_storbinary_rest(self):
557 f = io.BytesIO(RETR_DATA.replace('\r\n', '\n').encode('ascii'))
558 for r in (30, '30'):
559 f.seek(0)
560 self.client.storbinary('stor', f, rest=r)
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000561 self.assertEqual(self.server.handler_instance.rest, str(r))
Antoine Pitrou648bcd72009-11-27 13:23:26 +0000562
Giampaolo Rodolàf96482e2010-08-04 10:36:18 +0000563 def test_storlines(self):
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000564 f = io.BytesIO(RETR_DATA.replace('\r\n', '\n').encode('ascii'))
565 self.client.storlines('stor', f)
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000566 self.assertEqual(self.server.handler_instance.last_received_data, RETR_DATA)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000567 # test new callback arg
568 flag = []
569 f.seek(0)
570 self.client.storlines('stor foo', f, callback=lambda x: flag.append(None))
571 self.assertTrue(flag)
572
573 def test_nlst(self):
574 self.client.nlst()
575 self.assertEqual(self.client.nlst(), NLST_DATA.split('\r\n')[:-1])
576
577 def test_dir(self):
578 l = []
579 self.client.dir(lambda x: l.append(x))
580 self.assertEqual(''.join(l), LIST_DATA.replace('\r\n', ''))
581
Giampaolo Rodola'd78def92011-05-06 19:49:08 +0200582 def test_mlsd(self):
583 list(self.client.mlsd())
584 list(self.client.mlsd(path='/'))
585 list(self.client.mlsd(path='/', facts=['size', 'type']))
586
587 ls = list(self.client.mlsd())
588 for name, facts in ls:
Giampaolo Rodola'a55efb32011-05-07 16:06:59 +0200589 self.assertIsInstance(name, str)
590 self.assertIsInstance(facts, dict)
Giampaolo Rodola'd78def92011-05-06 19:49:08 +0200591 self.assertTrue(name)
Giampaolo Rodola'a55efb32011-05-07 16:06:59 +0200592 self.assertIn('type', facts)
593 self.assertIn('perm', facts)
594 self.assertIn('unique', facts)
Giampaolo Rodola'd78def92011-05-06 19:49:08 +0200595
596 def set_data(data):
597 self.server.handler_instance.next_data = data
598
599 def test_entry(line, type=None, perm=None, unique=None, name=None):
600 type = 'type' if type is None else type
601 perm = 'perm' if perm is None else perm
602 unique = 'unique' if unique is None else unique
603 name = 'name' if name is None else name
604 set_data(line)
605 _name, facts = next(self.client.mlsd())
606 self.assertEqual(_name, name)
607 self.assertEqual(facts['type'], type)
608 self.assertEqual(facts['perm'], perm)
609 self.assertEqual(facts['unique'], unique)
610
611 # plain
612 test_entry('type=type;perm=perm;unique=unique; name\r\n')
613 # "=" in fact value
614 test_entry('type=ty=pe;perm=perm;unique=unique; name\r\n', type="ty=pe")
615 test_entry('type==type;perm=perm;unique=unique; name\r\n', type="=type")
616 test_entry('type=t=y=pe;perm=perm;unique=unique; name\r\n', type="t=y=pe")
617 test_entry('type=====;perm=perm;unique=unique; name\r\n', type="====")
618 # spaces in name
619 test_entry('type=type;perm=perm;unique=unique; na me\r\n', name="na me")
620 test_entry('type=type;perm=perm;unique=unique; name \r\n', name="name ")
621 test_entry('type=type;perm=perm;unique=unique; name\r\n', name=" name")
622 test_entry('type=type;perm=perm;unique=unique; n am e\r\n', name="n am e")
623 # ";" in name
624 test_entry('type=type;perm=perm;unique=unique; na;me\r\n', name="na;me")
625 test_entry('type=type;perm=perm;unique=unique; ;name\r\n', name=";name")
626 test_entry('type=type;perm=perm;unique=unique; ;name;\r\n', name=";name;")
627 test_entry('type=type;perm=perm;unique=unique; ;;;;\r\n', name=";;;;")
628 # case sensitiveness
629 set_data('Type=type;TyPe=perm;UNIQUE=unique; name\r\n')
630 _name, facts = next(self.client.mlsd())
Giampaolo Rodola'a55efb32011-05-07 16:06:59 +0200631 for x in facts:
632 self.assertTrue(x.islower())
Giampaolo Rodola'd78def92011-05-06 19:49:08 +0200633 # no data (directory empty)
634 set_data('')
635 self.assertRaises(StopIteration, next, self.client.mlsd())
636 set_data('')
637 for x in self.client.mlsd():
638 self.fail("unexpected data %s" % data)
639
Benjamin Peterson3a53fbb2008-09-27 22:04:16 +0000640 def test_makeport(self):
Brett Cannon918e2d42010-10-29 23:26:25 +0000641 with self.client.makeport():
642 # IPv4 is in use, just make sure send_eprt has not been used
643 self.assertEqual(self.server.handler_instance.last_received_cmd,
644 'port')
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000645
646 def test_makepasv(self):
647 host, port = self.client.makepasv()
Antoine Pitroud778e562010-10-14 20:35:26 +0000648 conn = socket.create_connection((host, port), 10)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000649 conn.close()
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000650 # IPv4 is in use, just make sure send_epsv has not been used
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000651 self.assertEqual(self.server.handler_instance.last_received_cmd, 'pasv')
652
653 def test_with_statement(self):
654 self.client.quit()
655
656 def is_client_connected():
657 if self.client.sock is None:
658 return False
659 try:
660 self.client.sendcmd('noop')
661 except (socket.error, EOFError):
662 return False
663 return True
664
665 # base test
Antoine Pitroud778e562010-10-14 20:35:26 +0000666 with ftplib.FTP(timeout=10) as self.client:
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000667 self.client.connect(self.server.host, self.server.port)
668 self.client.sendcmd('noop')
669 self.assertTrue(is_client_connected())
670 self.assertEqual(self.server.handler_instance.last_received_cmd, 'quit')
671 self.assertFalse(is_client_connected())
672
673 # QUIT sent inside the with block
Antoine Pitroud778e562010-10-14 20:35:26 +0000674 with ftplib.FTP(timeout=10) as self.client:
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000675 self.client.connect(self.server.host, self.server.port)
676 self.client.sendcmd('noop')
677 self.client.quit()
678 self.assertEqual(self.server.handler_instance.last_received_cmd, 'quit')
679 self.assertFalse(is_client_connected())
680
681 # force a wrong response code to be sent on QUIT: error_perm
682 # is expected and the connection is supposed to be closed
683 try:
Antoine Pitroud778e562010-10-14 20:35:26 +0000684 with ftplib.FTP(timeout=10) as self.client:
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000685 self.client.connect(self.server.host, self.server.port)
686 self.client.sendcmd('noop')
687 self.server.handler_instance.next_response = '550 error on quit'
688 except ftplib.error_perm as err:
689 self.assertEqual(str(err), '550 error on quit')
690 else:
691 self.fail('Exception not raised')
692 # needed to give the threaded server some time to set the attribute
693 # which otherwise would still be == 'noop'
694 time.sleep(0.1)
695 self.assertEqual(self.server.handler_instance.last_received_cmd, 'quit')
696 self.assertFalse(is_client_connected())
Guido van Rossumd8faa362007-04-27 19:54:29 +0000697
Giampaolo Rodolà396ff062011-02-28 19:19:51 +0000698 def test_source_address(self):
699 self.client.quit()
700 port = support.find_unused_port()
Antoine Pitrou6dca5272011-04-03 18:29:45 +0200701 try:
702 self.client.connect(self.server.host, self.server.port,
703 source_address=(HOST, port))
704 self.assertEqual(self.client.sock.getsockname()[1], port)
705 self.client.quit()
706 except IOError as e:
707 if e.errno == errno.EADDRINUSE:
708 self.skipTest("couldn't bind to port %d" % port)
709 raise
Giampaolo Rodolà396ff062011-02-28 19:19:51 +0000710
711 def test_source_address_passive_connection(self):
712 port = support.find_unused_port()
713 self.client.source_address = (HOST, port)
Antoine Pitrou6dca5272011-04-03 18:29:45 +0200714 try:
715 with self.client.transfercmd('list') as sock:
716 self.assertEqual(sock.getsockname()[1], port)
717 except IOError as e:
718 if e.errno == errno.EADDRINUSE:
719 self.skipTest("couldn't bind to port %d" % port)
720 raise
Giampaolo Rodolà396ff062011-02-28 19:19:51 +0000721
Giampaolo Rodolàbbc47822010-08-23 22:10:32 +0000722 def test_parse257(self):
723 self.assertEqual(ftplib.parse257('257 "/foo/bar"'), '/foo/bar')
724 self.assertEqual(ftplib.parse257('257 "/foo/bar" created'), '/foo/bar')
725 self.assertEqual(ftplib.parse257('257 ""'), '')
726 self.assertEqual(ftplib.parse257('257 "" created'), '')
727 self.assertRaises(ftplib.error_reply, ftplib.parse257, '250 "/foo/bar"')
728 # The 257 response is supposed to include the directory
729 # name and in case it contains embedded double-quotes
730 # they must be doubled (see RFC-959, chapter 7, appendix 2).
731 self.assertEqual(ftplib.parse257('257 "/foo/b""ar"'), '/foo/b"ar')
732 self.assertEqual(ftplib.parse257('257 "/foo/b""ar" created'), '/foo/b"ar')
733
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000734
735class TestIPv6Environment(TestCase):
736
737 def setUp(self):
Victor Stinnerc90e19d2011-05-01 01:23:03 +0200738 self.server = DummyFTPServer(('::1', 0), af=socket.AF_INET6)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000739 self.server.start()
740 self.client = ftplib.FTP()
741 self.client.connect(self.server.host, self.server.port)
742
743 def tearDown(self):
744 self.client.close()
745 self.server.stop()
746
747 def test_af(self):
748 self.assertEqual(self.client.af, socket.AF_INET6)
749
750 def test_makeport(self):
Brett Cannon918e2d42010-10-29 23:26:25 +0000751 with self.client.makeport():
752 self.assertEqual(self.server.handler_instance.last_received_cmd,
753 'eprt')
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000754
755 def test_makepasv(self):
756 host, port = self.client.makepasv()
Antoine Pitroud778e562010-10-14 20:35:26 +0000757 conn = socket.create_connection((host, port), 10)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000758 conn.close()
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000759 self.assertEqual(self.server.handler_instance.last_received_cmd, 'epsv')
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000760
761 def test_transfer(self):
762 def retr():
763 def callback(data):
764 received.append(data.decode('ascii'))
765 received = []
766 self.client.retrbinary('retr', callback)
767 self.assertEqual(''.join(received), RETR_DATA)
768 self.client.set_pasv(True)
769 retr()
770 self.client.set_pasv(False)
771 retr()
772
773
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000774class TestTLS_FTPClassMixin(TestFTPClass):
775 """Repeat TestFTPClass tests starting the TLS layer for both control
776 and data connections first.
777 """
778
779 def setUp(self):
780 self.server = DummyTLS_FTPServer((HOST, 0))
781 self.server.start()
Antoine Pitroud778e562010-10-14 20:35:26 +0000782 self.client = ftplib.FTP_TLS(timeout=10)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000783 self.client.connect(self.server.host, self.server.port)
784 # enable TLS
785 self.client.auth()
786 self.client.prot_p()
787
788
789class TestTLS_FTPClass(TestCase):
790 """Specific TLS_FTP class tests."""
791
792 def setUp(self):
793 self.server = DummyTLS_FTPServer((HOST, 0))
794 self.server.start()
Antoine Pitroud778e562010-10-14 20:35:26 +0000795 self.client = ftplib.FTP_TLS(timeout=10)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000796 self.client.connect(self.server.host, self.server.port)
797
798 def tearDown(self):
799 self.client.close()
800 self.server.stop()
801
802 def test_control_connection(self):
Ezio Melottie9615932010-01-24 19:26:24 +0000803 self.assertNotIsInstance(self.client.sock, ssl.SSLSocket)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000804 self.client.auth()
Ezio Melottie9615932010-01-24 19:26:24 +0000805 self.assertIsInstance(self.client.sock, ssl.SSLSocket)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000806
807 def test_data_connection(self):
808 # clear text
Brett Cannon918e2d42010-10-29 23:26:25 +0000809 with self.client.transfercmd('list') as sock:
810 self.assertNotIsInstance(sock, ssl.SSLSocket)
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000811 self.assertEqual(self.client.voidresp(), "226 transfer complete")
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000812
813 # secured, after PROT P
814 self.client.prot_p()
Brett Cannon918e2d42010-10-29 23:26:25 +0000815 with self.client.transfercmd('list') as sock:
816 self.assertIsInstance(sock, ssl.SSLSocket)
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000817 self.assertEqual(self.client.voidresp(), "226 transfer complete")
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000818
819 # PROT C is issued, the connection must be in cleartext again
820 self.client.prot_c()
Brett Cannon918e2d42010-10-29 23:26:25 +0000821 with self.client.transfercmd('list') as sock:
822 self.assertNotIsInstance(sock, ssl.SSLSocket)
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000823 self.assertEqual(self.client.voidresp(), "226 transfer complete")
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000824
825 def test_login(self):
826 # login() is supposed to implicitly secure the control connection
Ezio Melottie9615932010-01-24 19:26:24 +0000827 self.assertNotIsInstance(self.client.sock, ssl.SSLSocket)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000828 self.client.login()
Ezio Melottie9615932010-01-24 19:26:24 +0000829 self.assertIsInstance(self.client.sock, ssl.SSLSocket)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000830 # make sure that AUTH TLS doesn't get issued again
831 self.client.login()
832
833 def test_auth_issued_twice(self):
834 self.client.auth()
835 self.assertRaises(ValueError, self.client.auth)
836
837 def test_auth_ssl(self):
838 try:
839 self.client.ssl_version = ssl.PROTOCOL_SSLv3
840 self.client.auth()
841 self.assertRaises(ValueError, self.client.auth)
842 finally:
843 self.client.ssl_version = ssl.PROTOCOL_TLSv1
844
Giampaolo Rodolàa67299e2010-05-26 18:06:04 +0000845 def test_context(self):
846 self.client.quit()
847 ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
848 self.assertRaises(ValueError, ftplib.FTP_TLS, keyfile=CERTFILE,
849 context=ctx)
850 self.assertRaises(ValueError, ftplib.FTP_TLS, certfile=CERTFILE,
851 context=ctx)
852 self.assertRaises(ValueError, ftplib.FTP_TLS, certfile=CERTFILE,
853 keyfile=CERTFILE, context=ctx)
854
Antoine Pitroud778e562010-10-14 20:35:26 +0000855 self.client = ftplib.FTP_TLS(context=ctx, timeout=10)
Giampaolo Rodolàa67299e2010-05-26 18:06:04 +0000856 self.client.connect(self.server.host, self.server.port)
857 self.assertNotIsInstance(self.client.sock, ssl.SSLSocket)
858 self.client.auth()
859 self.assertIs(self.client.sock.context, ctx)
860 self.assertIsInstance(self.client.sock, ssl.SSLSocket)
861
862 self.client.prot_p()
Brett Cannon918e2d42010-10-29 23:26:25 +0000863 with self.client.transfercmd('list') as sock:
864 self.assertIs(sock.context, ctx)
865 self.assertIsInstance(sock, ssl.SSLSocket)
Giampaolo Rodolàa67299e2010-05-26 18:06:04 +0000866
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000867
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000868class TestTimeouts(TestCase):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000869
870 def setUp(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000871 self.evt = threading.Event()
Christian Heimes5e696852008-04-09 08:37:03 +0000872 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
873 self.sock.settimeout(3)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000874 self.port = support.bind_port(self.sock)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000875 threading.Thread(target=self.server, args=(self.evt,self.sock)).start()
Christian Heimes836baa52008-02-26 08:18:30 +0000876 # Wait for the server to be ready.
877 self.evt.wait()
878 self.evt.clear()
Christian Heimes5e696852008-04-09 08:37:03 +0000879 ftplib.FTP.port = self.port
Guido van Rossumd8faa362007-04-27 19:54:29 +0000880
881 def tearDown(self):
882 self.evt.wait()
Brett Cannon918e2d42010-10-29 23:26:25 +0000883 self.sock.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000884
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000885 def server(self, evt, serv):
886 # This method sets the evt 3 times:
887 # 1) when the connection is ready to be accepted.
888 # 2) when it is safe for the caller to close the connection
889 # 3) when we have closed the socket
890 serv.listen(5)
891 # (1) Signal the caller that we are ready to accept the connection.
892 evt.set()
893 try:
894 conn, addr = serv.accept()
895 except socket.timeout:
896 pass
897 else:
898 conn.send(b"1 Hola mundo\n")
899 # (2) Signal the caller that it is safe to close the socket.
900 evt.set()
901 conn.close()
902 finally:
903 serv.close()
904 # (3) Signal the caller that we are done.
905 evt.set()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000906
907 def testTimeoutDefault(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +0000908 # default -- use global socket timeout
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000909 self.assertTrue(socket.getdefaulttimeout() is None)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000910 socket.setdefaulttimeout(30)
911 try:
912 ftp = ftplib.FTP("localhost")
913 finally:
914 socket.setdefaulttimeout(None)
915 self.assertEqual(ftp.sock.gettimeout(), 30)
916 self.evt.wait()
917 ftp.close()
918
919 def testTimeoutNone(self):
920 # no timeout -- do not use global socket timeout
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000921 self.assertTrue(socket.getdefaulttimeout() is None)
Georg Brandlf78e02b2008-06-10 17:40:04 +0000922 socket.setdefaulttimeout(30)
923 try:
924 ftp = ftplib.FTP("localhost", timeout=None)
925 finally:
926 socket.setdefaulttimeout(None)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000927 self.assertTrue(ftp.sock.gettimeout() is None)
Christian Heimes836baa52008-02-26 08:18:30 +0000928 self.evt.wait()
Georg Brandlf78e02b2008-06-10 17:40:04 +0000929 ftp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000930
931 def testTimeoutValue(self):
932 # a value
Christian Heimes5e696852008-04-09 08:37:03 +0000933 ftp = ftplib.FTP(HOST, timeout=30)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000934 self.assertEqual(ftp.sock.gettimeout(), 30)
Christian Heimes836baa52008-02-26 08:18:30 +0000935 self.evt.wait()
Georg Brandlf78e02b2008-06-10 17:40:04 +0000936 ftp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000937
938 def testTimeoutConnect(self):
939 ftp = ftplib.FTP()
Christian Heimes5e696852008-04-09 08:37:03 +0000940 ftp.connect(HOST, timeout=30)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000941 self.assertEqual(ftp.sock.gettimeout(), 30)
Christian Heimes836baa52008-02-26 08:18:30 +0000942 self.evt.wait()
Georg Brandlf78e02b2008-06-10 17:40:04 +0000943 ftp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000944
945 def testTimeoutDifferentOrder(self):
946 ftp = ftplib.FTP(timeout=30)
Christian Heimes5e696852008-04-09 08:37:03 +0000947 ftp.connect(HOST)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000948 self.assertEqual(ftp.sock.gettimeout(), 30)
Christian Heimes836baa52008-02-26 08:18:30 +0000949 self.evt.wait()
Georg Brandlf78e02b2008-06-10 17:40:04 +0000950 ftp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000951
952 def testTimeoutDirectAccess(self):
953 ftp = ftplib.FTP()
954 ftp.timeout = 30
Christian Heimes5e696852008-04-09 08:37:03 +0000955 ftp.connect(HOST)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000956 self.assertEqual(ftp.sock.gettimeout(), 30)
Christian Heimes836baa52008-02-26 08:18:30 +0000957 self.evt.wait()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000958 ftp.close()
959
960
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000961def test_main():
962 tests = [TestFTPClass, TestTimeouts]
Antoine Pitrou9c39f3c2011-04-28 19:18:10 +0200963 if support.IPV6_ENABLED:
Victor Stinnerc90e19d2011-05-01 01:23:03 +0200964 tests.append(TestIPv6Environment)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000965
966 if ssl is not None:
967 tests.extend([TestTLS_FTPClassMixin, TestTLS_FTPClass])
968
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000969 thread_info = support.threading_setup()
970 try:
971 support.run_unittest(*tests)
972 finally:
973 support.threading_cleanup(*thread_info)
974
Guido van Rossumd8faa362007-04-27 19:54:29 +0000975
976if __name__ == '__main__':
977 test_main()