blob: 12fabc5e8be3d69a28f7b2c13e6ee9fffb6a8db6 [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
Serhiy Storchaka43767632013-11-03 21:31:38 +020019from unittest import TestCase, skipUnless
Benjamin Petersonee8712c2008-05-20 21:35:26 +000020from test import support
Antoine Pitrouf6fbf562013-08-22 00:39:46 +020021from test.support import HOST, HOSTv6
Victor Stinner45df8202010-04-28 22:31:17 +000022threading = support.import_module('threading')
Guido van Rossumd8faa362007-04-27 19:54:29 +000023
Giampaolo Rodola'0d4f08c2013-05-16 15:12:01 +020024TIMEOUT = 3
Benjamin Petersonbe17a112008-09-27 21:49:47 +000025# the dummy data returned by server over the data channel when
Giampaolo Rodola'd78def92011-05-06 19:49:08 +020026# RETR, LIST, NLST, MLSD commands are issued
Benjamin Petersonbe17a112008-09-27 21:49:47 +000027RETR_DATA = 'abcde12345\r\n' * 1000
28LIST_DATA = 'foo\r\nbar\r\n'
29NLST_DATA = 'foo\r\nbar\r\n'
Giampaolo Rodola'd78def92011-05-06 19:49:08 +020030MLSD_DATA = ("type=cdir;perm=el;unique==keVO1+ZF4; test\r\n"
31 "type=pdir;perm=e;unique==keVO1+d?3; ..\r\n"
32 "type=OS.unix=slink:/foobar;perm=;unique==keVO1+4G4; foobar\r\n"
33 "type=OS.unix=chr-13/29;perm=;unique==keVO1+5G4; device\r\n"
34 "type=OS.unix=blk-11/108;perm=;unique==keVO1+6G4; block\r\n"
35 "type=file;perm=awr;unique==keVO1+8G4; writable\r\n"
36 "type=dir;perm=cpmel;unique==keVO1+7G4; promiscuous\r\n"
37 "type=dir;perm=;unique==keVO1+1t2; no-exec\r\n"
38 "type=file;perm=r;unique==keVO1+EG4; two words\r\n"
39 "type=file;perm=r;unique==keVO1+IH4; leading space\r\n"
40 "type=file;perm=r;unique==keVO1+1G4; file1\r\n"
41 "type=dir;perm=cpmel;unique==keVO1+7G4; incoming\r\n"
42 "type=file;perm=r;unique==keVO1+1G4; file2\r\n"
43 "type=file;perm=r;unique==keVO1+1G4; file3\r\n"
44 "type=file;perm=r;unique==keVO1+1G4; file4\r\n")
Christian Heimes836baa52008-02-26 08:18:30 +000045
Christian Heimes836baa52008-02-26 08:18:30 +000046
Benjamin Petersonbe17a112008-09-27 21:49:47 +000047class DummyDTPHandler(asynchat.async_chat):
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +000048 dtp_conn_closed = False
Benjamin Petersonbe17a112008-09-27 21:49:47 +000049
50 def __init__(self, conn, baseclass):
51 asynchat.async_chat.__init__(self, conn)
52 self.baseclass = baseclass
53 self.baseclass.last_received_data = ''
54
55 def handle_read(self):
Giampaolo Rodolàf96482e2010-08-04 10:36:18 +000056 self.baseclass.last_received_data += self.recv(1024).decode('ascii')
Benjamin Petersonbe17a112008-09-27 21:49:47 +000057
58 def handle_close(self):
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +000059 # XXX: this method can be called many times in a row for a single
60 # connection, including in clear-text (non-TLS) mode.
61 # (behaviour witnessed with test_data_connection)
62 if not self.dtp_conn_closed:
63 self.baseclass.push('226 transfer complete')
64 self.close()
65 self.dtp_conn_closed = True
Benjamin Petersonbe17a112008-09-27 21:49:47 +000066
67 def push(self, what):
Giampaolo Rodola'd78def92011-05-06 19:49:08 +020068 if self.baseclass.next_data is not None:
69 what = self.baseclass.next_data
70 self.baseclass.next_data = None
71 if not what:
72 return self.close_when_done()
Giampaolo Rodolàf96482e2010-08-04 10:36:18 +000073 super(DummyDTPHandler, self).push(what.encode('ascii'))
Benjamin Petersonbe17a112008-09-27 21:49:47 +000074
Giampaolo Rodolàd930b632010-05-06 20:21:57 +000075 def handle_error(self):
Berker Peksag8f791d32014-11-01 10:45:57 +020076 raise Exception
Giampaolo Rodolàd930b632010-05-06 20:21:57 +000077
Benjamin Petersonbe17a112008-09-27 21:49:47 +000078
79class DummyFTPHandler(asynchat.async_chat):
80
Antoine Pitrouf988cd02009-11-17 20:21:14 +000081 dtp_handler = DummyDTPHandler
82
Benjamin Petersonbe17a112008-09-27 21:49:47 +000083 def __init__(self, conn):
84 asynchat.async_chat.__init__(self, conn)
Giampaolo Rodola'0b5c21f2011-05-07 19:03:47 +020085 # tells the socket to handle urgent data inline (ABOR command)
86 self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_OOBINLINE, 1)
Benjamin Petersonbe17a112008-09-27 21:49:47 +000087 self.set_terminator(b"\r\n")
88 self.in_buffer = []
89 self.dtp = None
90 self.last_received_cmd = None
91 self.last_received_data = ''
92 self.next_response = ''
Giampaolo Rodola'd78def92011-05-06 19:49:08 +020093 self.next_data = None
Antoine Pitrou648bcd72009-11-27 13:23:26 +000094 self.rest = None
Serhiy Storchakac30b1782013-10-20 16:58:27 +030095 self.next_retr_data = RETR_DATA
Benjamin Petersonbe17a112008-09-27 21:49:47 +000096 self.push('220 welcome')
97
98 def collect_incoming_data(self, data):
99 self.in_buffer.append(data)
100
101 def found_terminator(self):
102 line = b''.join(self.in_buffer).decode('ascii')
103 self.in_buffer = []
104 if self.next_response:
105 self.push(self.next_response)
106 self.next_response = ''
107 cmd = line.split(' ')[0].lower()
108 self.last_received_cmd = cmd
109 space = line.find(' ')
110 if space != -1:
111 arg = line[space + 1:]
112 else:
113 arg = ""
114 if hasattr(self, 'cmd_' + cmd):
115 method = getattr(self, 'cmd_' + cmd)
116 method(arg)
117 else:
118 self.push('550 command "%s" not understood.' %cmd)
119
120 def handle_error(self):
Berker Peksag8f791d32014-11-01 10:45:57 +0200121 raise Exception
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000122
123 def push(self, data):
124 asynchat.async_chat.push(self, data.encode('ascii') + b'\r\n')
125
126 def cmd_port(self, arg):
127 addr = list(map(int, arg.split(',')))
128 ip = '%d.%d.%d.%d' %tuple(addr[:4])
129 port = (addr[4] * 256) + addr[5]
Giampaolo Rodola'0d4f08c2013-05-16 15:12:01 +0200130 s = socket.create_connection((ip, port), timeout=TIMEOUT)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000131 self.dtp = self.dtp_handler(s, baseclass=self)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000132 self.push('200 active data connection established')
133
134 def cmd_pasv(self, arg):
Brett Cannon918e2d42010-10-29 23:26:25 +0000135 with socket.socket() as sock:
136 sock.bind((self.socket.getsockname()[0], 0))
Charles-François Natali6e204602014-07-23 19:28:13 +0100137 sock.listen()
Giampaolo Rodola'0d4f08c2013-05-16 15:12:01 +0200138 sock.settimeout(TIMEOUT)
Brett Cannon918e2d42010-10-29 23:26:25 +0000139 ip, port = sock.getsockname()[:2]
140 ip = ip.replace('.', ','); p1 = port / 256; p2 = port % 256
141 self.push('227 entering passive mode (%s,%d,%d)' %(ip, p1, p2))
142 conn, addr = sock.accept()
143 self.dtp = self.dtp_handler(conn, baseclass=self)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000144
145 def cmd_eprt(self, arg):
146 af, ip, port = arg.split(arg[0])[1:-1]
147 port = int(port)
Giampaolo Rodola'0d4f08c2013-05-16 15:12:01 +0200148 s = socket.create_connection((ip, port), timeout=TIMEOUT)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000149 self.dtp = self.dtp_handler(s, baseclass=self)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000150 self.push('200 active data connection established')
151
152 def cmd_epsv(self, arg):
Brett Cannon918e2d42010-10-29 23:26:25 +0000153 with socket.socket(socket.AF_INET6) as sock:
154 sock.bind((self.socket.getsockname()[0], 0))
Charles-François Natali6e204602014-07-23 19:28:13 +0100155 sock.listen()
Giampaolo Rodola'0d4f08c2013-05-16 15:12:01 +0200156 sock.settimeout(TIMEOUT)
Brett Cannon918e2d42010-10-29 23:26:25 +0000157 port = sock.getsockname()[1]
158 self.push('229 entering extended passive mode (|||%d|)' %port)
159 conn, addr = sock.accept()
160 self.dtp = self.dtp_handler(conn, baseclass=self)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000161
162 def cmd_echo(self, arg):
163 # sends back the received string (used by the test suite)
164 self.push(arg)
165
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000166 def cmd_noop(self, arg):
167 self.push('200 noop ok')
168
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000169 def cmd_user(self, arg):
170 self.push('331 username ok')
171
172 def cmd_pass(self, arg):
173 self.push('230 password ok')
174
175 def cmd_acct(self, arg):
176 self.push('230 acct ok')
177
178 def cmd_rnfr(self, arg):
179 self.push('350 rnfr ok')
180
181 def cmd_rnto(self, arg):
182 self.push('250 rnto ok')
183
184 def cmd_dele(self, arg):
185 self.push('250 dele ok')
186
187 def cmd_cwd(self, arg):
188 self.push('250 cwd ok')
189
190 def cmd_size(self, arg):
191 self.push('250 1000')
192
193 def cmd_mkd(self, arg):
194 self.push('257 "%s"' %arg)
195
196 def cmd_rmd(self, arg):
197 self.push('250 rmd ok')
198
199 def cmd_pwd(self, arg):
200 self.push('257 "pwd ok"')
201
202 def cmd_type(self, arg):
Giampaolo Rodolàf96482e2010-08-04 10:36:18 +0000203 self.push('200 type ok')
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000204
205 def cmd_quit(self, arg):
206 self.push('221 quit ok')
207 self.close()
208
Giampaolo Rodola'0b5c21f2011-05-07 19:03:47 +0200209 def cmd_abor(self, arg):
210 self.push('226 abor ok')
211
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000212 def cmd_stor(self, arg):
213 self.push('125 stor ok')
214
Antoine Pitrou648bcd72009-11-27 13:23:26 +0000215 def cmd_rest(self, arg):
216 self.rest = arg
217 self.push('350 rest ok')
218
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000219 def cmd_retr(self, arg):
220 self.push('125 retr ok')
Antoine Pitrou648bcd72009-11-27 13:23:26 +0000221 if self.rest is not None:
222 offset = int(self.rest)
223 else:
224 offset = 0
Serhiy Storchakac30b1782013-10-20 16:58:27 +0300225 self.dtp.push(self.next_retr_data[offset:])
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000226 self.dtp.close_when_done()
Antoine Pitrou648bcd72009-11-27 13:23:26 +0000227 self.rest = None
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000228
229 def cmd_list(self, arg):
230 self.push('125 list ok')
231 self.dtp.push(LIST_DATA)
232 self.dtp.close_when_done()
233
234 def cmd_nlst(self, arg):
235 self.push('125 nlst ok')
236 self.dtp.push(NLST_DATA)
237 self.dtp.close_when_done()
238
Giampaolo Rodola'd78def92011-05-06 19:49:08 +0200239 def cmd_opts(self, arg):
240 self.push('200 opts ok')
241
242 def cmd_mlsd(self, arg):
243 self.push('125 mlsd ok')
244 self.dtp.push(MLSD_DATA)
245 self.dtp.close_when_done()
246
Serhiy Storchakac30b1782013-10-20 16:58:27 +0300247 def cmd_setlongretr(self, arg):
248 # For testing. Next RETR will return long line.
249 self.next_retr_data = 'x' * int(arg)
250 self.push('125 setlongretr ok')
251
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000252
253class DummyFTPServer(asyncore.dispatcher, threading.Thread):
254
255 handler = DummyFTPHandler
256
257 def __init__(self, address, af=socket.AF_INET):
258 threading.Thread.__init__(self)
259 asyncore.dispatcher.__init__(self)
260 self.create_socket(af, socket.SOCK_STREAM)
261 self.bind(address)
262 self.listen(5)
263 self.active = False
264 self.active_lock = threading.Lock()
265 self.host, self.port = self.socket.getsockname()[:2]
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000266 self.handler_instance = None
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000267
268 def start(self):
269 assert not self.active
270 self.__flag = threading.Event()
271 threading.Thread.start(self)
272 self.__flag.wait()
273
274 def run(self):
275 self.active = True
276 self.__flag.set()
277 while self.active and asyncore.socket_map:
278 self.active_lock.acquire()
279 asyncore.loop(timeout=0.1, count=1)
280 self.active_lock.release()
281 asyncore.close_all(ignore_all=True)
282
283 def stop(self):
284 assert self.active
285 self.active = False
286 self.join()
287
Giampaolo Rodolà977c7072010-10-04 21:08:36 +0000288 def handle_accepted(self, conn, addr):
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000289 self.handler_instance = self.handler(conn)
Benjamin Petersond06e3b02008-09-28 21:00:42 +0000290
291 def handle_connect(self):
292 self.close()
293 handle_read = handle_connect
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000294
295 def writable(self):
296 return 0
297
298 def handle_error(self):
Berker Peksag8f791d32014-11-01 10:45:57 +0200299 raise Exception
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000300
301
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000302if ssl is not None:
303
Christian Heimese5b5edf2013-12-02 02:56:02 +0100304 CERTFILE = os.path.join(os.path.dirname(__file__), "keycert3.pem")
305 CAFILE = os.path.join(os.path.dirname(__file__), "pycacert.pem")
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000306
307 class SSLConnection(asyncore.dispatcher):
308 """An asyncore.dispatcher subclass supporting TLS/SSL."""
309
310 _ssl_accepting = False
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000311 _ssl_closing = False
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000312
313 def secure_connection(self):
Christian Heimesd0486372016-09-10 23:23:33 +0200314 context = ssl.SSLContext()
315 context.load_cert_chain(CERTFILE)
316 socket = context.wrap_socket(self.socket,
317 suppress_ragged_eofs=False,
318 server_side=True,
319 do_handshake_on_connect=False)
Giampaolo Rodola'096dcb12011-06-27 11:17:51 +0200320 self.del_channel()
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000321 self.set_socket(socket)
322 self._ssl_accepting = True
323
324 def _do_ssl_handshake(self):
325 try:
326 self.socket.do_handshake()
327 except ssl.SSLError as err:
328 if err.args[0] in (ssl.SSL_ERROR_WANT_READ,
329 ssl.SSL_ERROR_WANT_WRITE):
330 return
331 elif err.args[0] == ssl.SSL_ERROR_EOF:
332 return self.handle_close()
333 raise
Andrew Svetlov0832af62012-12-18 23:10:48 +0200334 except OSError as err:
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000335 if err.args[0] == errno.ECONNABORTED:
336 return self.handle_close()
337 else:
338 self._ssl_accepting = False
339
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000340 def _do_ssl_shutdown(self):
341 self._ssl_closing = True
342 try:
343 self.socket = self.socket.unwrap()
344 except ssl.SSLError as err:
345 if err.args[0] in (ssl.SSL_ERROR_WANT_READ,
346 ssl.SSL_ERROR_WANT_WRITE):
347 return
Andrew Svetlov0832af62012-12-18 23:10:48 +0200348 except OSError as err:
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000349 # Any "socket error" corresponds to a SSL_ERROR_SYSCALL return
350 # from OpenSSL's SSL_shutdown(), corresponding to a
351 # closed socket condition. See also:
352 # http://www.mail-archive.com/openssl-users@openssl.org/msg60710.html
353 pass
354 self._ssl_closing = False
Benjamin Petersonb29614e2012-10-09 11:16:03 -0400355 if getattr(self, '_ccc', False) is False:
Giampaolo Rodola'096dcb12011-06-27 11:17:51 +0200356 super(SSLConnection, self).close()
357 else:
358 pass
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000359
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000360 def handle_read_event(self):
361 if self._ssl_accepting:
362 self._do_ssl_handshake()
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000363 elif self._ssl_closing:
364 self._do_ssl_shutdown()
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000365 else:
366 super(SSLConnection, self).handle_read_event()
367
368 def handle_write_event(self):
369 if self._ssl_accepting:
370 self._do_ssl_handshake()
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000371 elif self._ssl_closing:
372 self._do_ssl_shutdown()
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000373 else:
374 super(SSLConnection, self).handle_write_event()
375
376 def send(self, data):
377 try:
378 return super(SSLConnection, self).send(data)
379 except ssl.SSLError as err:
Antoine Pitrou5733c082010-03-22 14:49:10 +0000380 if err.args[0] in (ssl.SSL_ERROR_EOF, ssl.SSL_ERROR_ZERO_RETURN,
381 ssl.SSL_ERROR_WANT_READ,
382 ssl.SSL_ERROR_WANT_WRITE):
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000383 return 0
384 raise
385
386 def recv(self, buffer_size):
387 try:
388 return super(SSLConnection, self).recv(buffer_size)
389 except ssl.SSLError as err:
Antoine Pitrou5733c082010-03-22 14:49:10 +0000390 if err.args[0] in (ssl.SSL_ERROR_WANT_READ,
391 ssl.SSL_ERROR_WANT_WRITE):
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000392 return b''
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000393 if err.args[0] in (ssl.SSL_ERROR_EOF, ssl.SSL_ERROR_ZERO_RETURN):
394 self.handle_close()
395 return b''
396 raise
397
398 def handle_error(self):
Berker Peksag8f791d32014-11-01 10:45:57 +0200399 raise Exception
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000400
401 def close(self):
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000402 if (isinstance(self.socket, ssl.SSLSocket) and
403 self.socket._sslobj is not None):
404 self._do_ssl_shutdown()
Benjamin Peterson1bd93a72010-10-31 19:58:07 +0000405 else:
406 super(SSLConnection, self).close()
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000407
408
409 class DummyTLS_DTPHandler(SSLConnection, DummyDTPHandler):
410 """A DummyDTPHandler subclass supporting TLS/SSL."""
411
412 def __init__(self, conn, baseclass):
413 DummyDTPHandler.__init__(self, conn, baseclass)
414 if self.baseclass.secure_data_channel:
415 self.secure_connection()
416
417
418 class DummyTLS_FTPHandler(SSLConnection, DummyFTPHandler):
419 """A DummyFTPHandler subclass supporting TLS/SSL."""
420
421 dtp_handler = DummyTLS_DTPHandler
422
423 def __init__(self, conn):
424 DummyFTPHandler.__init__(self, conn)
425 self.secure_data_channel = False
Giampaolo Rodola'096dcb12011-06-27 11:17:51 +0200426 self._ccc = False
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000427
428 def cmd_auth(self, line):
429 """Set up secure control channel."""
430 self.push('234 AUTH TLS successful')
431 self.secure_connection()
432
Giampaolo Rodola'096dcb12011-06-27 11:17:51 +0200433 def cmd_ccc(self, line):
434 self.push('220 Reverting back to clear-text')
435 self._ccc = True
436 self._do_ssl_shutdown()
437
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000438 def cmd_pbsz(self, line):
439 """Negotiate size of buffer for secure data transfer.
440 For TLS/SSL the only valid value for the parameter is '0'.
441 Any other value is accepted but ignored.
442 """
443 self.push('200 PBSZ=0 successful.')
444
445 def cmd_prot(self, line):
446 """Setup un/secure data channel."""
447 arg = line.upper()
448 if arg == 'C':
449 self.push('200 Protection set to Clear')
450 self.secure_data_channel = False
451 elif arg == 'P':
452 self.push('200 Protection set to Private')
453 self.secure_data_channel = True
454 else:
455 self.push("502 Unrecognized PROT type (use C or P).")
456
457
458 class DummyTLS_FTPServer(DummyFTPServer):
459 handler = DummyTLS_FTPHandler
460
461
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000462class TestFTPClass(TestCase):
463
464 def setUp(self):
465 self.server = DummyFTPServer((HOST, 0))
466 self.server.start()
Giampaolo Rodola'0d4f08c2013-05-16 15:12:01 +0200467 self.client = ftplib.FTP(timeout=TIMEOUT)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000468 self.client.connect(self.server.host, self.server.port)
469
470 def tearDown(self):
471 self.client.close()
472 self.server.stop()
473
Giampaolo Rodola'8bc85852012-01-09 17:10:10 +0100474 def check_data(self, received, expected):
475 self.assertEqual(len(received), len(expected))
476 self.assertEqual(received, expected)
477
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000478 def test_getwelcome(self):
479 self.assertEqual(self.client.getwelcome(), '220 welcome')
480
481 def test_sanitize(self):
482 self.assertEqual(self.client.sanitize('foo'), repr('foo'))
483 self.assertEqual(self.client.sanitize('pass 12345'), repr('pass *****'))
484 self.assertEqual(self.client.sanitize('PASS 12345'), repr('PASS *****'))
485
486 def test_exceptions(self):
487 self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 400')
488 self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 499')
489 self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 500')
490 self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 599')
491 self.assertRaises(ftplib.error_proto, self.client.sendcmd, 'echo 999')
492
493 def test_all_errors(self):
494 exceptions = (ftplib.error_reply, ftplib.error_temp, ftplib.error_perm,
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200495 ftplib.error_proto, ftplib.Error, OSError, EOFError)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000496 for x in exceptions:
497 try:
498 raise x('exception not included in all_errors set')
499 except ftplib.all_errors:
500 pass
501
502 def test_set_pasv(self):
503 # passive mode is supposed to be enabled by default
504 self.assertTrue(self.client.passiveserver)
505 self.client.set_pasv(True)
506 self.assertTrue(self.client.passiveserver)
507 self.client.set_pasv(False)
508 self.assertFalse(self.client.passiveserver)
509
510 def test_voidcmd(self):
511 self.client.voidcmd('echo 200')
512 self.client.voidcmd('echo 299')
513 self.assertRaises(ftplib.error_reply, self.client.voidcmd, 'echo 199')
514 self.assertRaises(ftplib.error_reply, self.client.voidcmd, 'echo 300')
515
516 def test_login(self):
517 self.client.login()
518
519 def test_acct(self):
520 self.client.acct('passwd')
521
522 def test_rename(self):
523 self.client.rename('a', 'b')
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000524 self.server.handler_instance.next_response = '200'
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000525 self.assertRaises(ftplib.error_reply, self.client.rename, 'a', 'b')
526
527 def test_delete(self):
528 self.client.delete('foo')
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000529 self.server.handler_instance.next_response = '199'
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000530 self.assertRaises(ftplib.error_reply, self.client.delete, 'foo')
531
532 def test_size(self):
533 self.client.size('foo')
534
535 def test_mkd(self):
536 dir = self.client.mkd('/foo')
537 self.assertEqual(dir, '/foo')
538
539 def test_rmd(self):
540 self.client.rmd('foo')
541
Senthil Kumaran0d538602013-08-12 22:25:27 -0700542 def test_cwd(self):
543 dir = self.client.cwd('/foo')
544 self.assertEqual(dir, '250 cwd ok')
545
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000546 def test_pwd(self):
547 dir = self.client.pwd()
548 self.assertEqual(dir, 'pwd ok')
549
550 def test_quit(self):
551 self.assertEqual(self.client.quit(), '221 quit ok')
552 # Ensure the connection gets closed; sock attribute should be None
553 self.assertEqual(self.client.sock, None)
554
Giampaolo Rodola'0b5c21f2011-05-07 19:03:47 +0200555 def test_abort(self):
556 self.client.abort()
557
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000558 def test_retrbinary(self):
559 def callback(data):
560 received.append(data.decode('ascii'))
561 received = []
562 self.client.retrbinary('retr', callback)
Giampaolo Rodola'8bc85852012-01-09 17:10:10 +0100563 self.check_data(''.join(received), RETR_DATA)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000564
Antoine Pitrou648bcd72009-11-27 13:23:26 +0000565 def test_retrbinary_rest(self):
566 def callback(data):
567 received.append(data.decode('ascii'))
568 for rest in (0, 10, 20):
569 received = []
570 self.client.retrbinary('retr', callback, rest=rest)
Giampaolo Rodola'8bc85852012-01-09 17:10:10 +0100571 self.check_data(''.join(received), RETR_DATA[rest:])
Antoine Pitrou648bcd72009-11-27 13:23:26 +0000572
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000573 def test_retrlines(self):
574 received = []
575 self.client.retrlines('retr', received.append)
Giampaolo Rodola'8bc85852012-01-09 17:10:10 +0100576 self.check_data(''.join(received), RETR_DATA.replace('\r\n', ''))
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000577
578 def test_storbinary(self):
579 f = io.BytesIO(RETR_DATA.encode('ascii'))
580 self.client.storbinary('stor', f)
Giampaolo Rodola'8bc85852012-01-09 17:10:10 +0100581 self.check_data(self.server.handler_instance.last_received_data, RETR_DATA)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000582 # test new callback arg
583 flag = []
584 f.seek(0)
585 self.client.storbinary('stor', f, callback=lambda x: flag.append(None))
586 self.assertTrue(flag)
587
Antoine Pitrou648bcd72009-11-27 13:23:26 +0000588 def test_storbinary_rest(self):
589 f = io.BytesIO(RETR_DATA.replace('\r\n', '\n').encode('ascii'))
590 for r in (30, '30'):
591 f.seek(0)
592 self.client.storbinary('stor', f, rest=r)
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000593 self.assertEqual(self.server.handler_instance.rest, str(r))
Antoine Pitrou648bcd72009-11-27 13:23:26 +0000594
Giampaolo Rodolàf96482e2010-08-04 10:36:18 +0000595 def test_storlines(self):
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000596 f = io.BytesIO(RETR_DATA.replace('\r\n', '\n').encode('ascii'))
597 self.client.storlines('stor', f)
Giampaolo Rodola'8bc85852012-01-09 17:10:10 +0100598 self.check_data(self.server.handler_instance.last_received_data, RETR_DATA)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000599 # test new callback arg
600 flag = []
601 f.seek(0)
602 self.client.storlines('stor foo', f, callback=lambda x: flag.append(None))
603 self.assertTrue(flag)
604
Victor Stinnered3a3032013-04-02 22:13:27 +0200605 f = io.StringIO(RETR_DATA.replace('\r\n', '\n'))
606 # storlines() expects a binary file, not a text file
Florent Xicluna5f3fef32013-07-06 15:08:21 +0200607 with support.check_warnings(('', BytesWarning), quiet=True):
608 self.assertRaises(TypeError, self.client.storlines, 'stor foo', f)
Victor Stinnered3a3032013-04-02 22:13:27 +0200609
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000610 def test_nlst(self):
611 self.client.nlst()
612 self.assertEqual(self.client.nlst(), NLST_DATA.split('\r\n')[:-1])
613
614 def test_dir(self):
615 l = []
616 self.client.dir(lambda x: l.append(x))
617 self.assertEqual(''.join(l), LIST_DATA.replace('\r\n', ''))
618
Giampaolo Rodola'd78def92011-05-06 19:49:08 +0200619 def test_mlsd(self):
620 list(self.client.mlsd())
621 list(self.client.mlsd(path='/'))
622 list(self.client.mlsd(path='/', facts=['size', 'type']))
623
624 ls = list(self.client.mlsd())
625 for name, facts in ls:
Giampaolo Rodola'a55efb32011-05-07 16:06:59 +0200626 self.assertIsInstance(name, str)
627 self.assertIsInstance(facts, dict)
Giampaolo Rodola'd78def92011-05-06 19:49:08 +0200628 self.assertTrue(name)
Giampaolo Rodola'a55efb32011-05-07 16:06:59 +0200629 self.assertIn('type', facts)
630 self.assertIn('perm', facts)
631 self.assertIn('unique', facts)
Giampaolo Rodola'd78def92011-05-06 19:49:08 +0200632
633 def set_data(data):
634 self.server.handler_instance.next_data = data
635
636 def test_entry(line, type=None, perm=None, unique=None, name=None):
637 type = 'type' if type is None else type
638 perm = 'perm' if perm is None else perm
639 unique = 'unique' if unique is None else unique
640 name = 'name' if name is None else name
641 set_data(line)
642 _name, facts = next(self.client.mlsd())
643 self.assertEqual(_name, name)
644 self.assertEqual(facts['type'], type)
645 self.assertEqual(facts['perm'], perm)
646 self.assertEqual(facts['unique'], unique)
647
648 # plain
649 test_entry('type=type;perm=perm;unique=unique; name\r\n')
650 # "=" in fact value
651 test_entry('type=ty=pe;perm=perm;unique=unique; name\r\n', type="ty=pe")
652 test_entry('type==type;perm=perm;unique=unique; name\r\n', type="=type")
653 test_entry('type=t=y=pe;perm=perm;unique=unique; name\r\n', type="t=y=pe")
654 test_entry('type=====;perm=perm;unique=unique; name\r\n', type="====")
655 # spaces in name
656 test_entry('type=type;perm=perm;unique=unique; na me\r\n', name="na me")
657 test_entry('type=type;perm=perm;unique=unique; name \r\n', name="name ")
658 test_entry('type=type;perm=perm;unique=unique; name\r\n', name=" name")
659 test_entry('type=type;perm=perm;unique=unique; n am e\r\n', name="n am e")
660 # ";" in name
661 test_entry('type=type;perm=perm;unique=unique; na;me\r\n', name="na;me")
662 test_entry('type=type;perm=perm;unique=unique; ;name\r\n', name=";name")
663 test_entry('type=type;perm=perm;unique=unique; ;name;\r\n', name=";name;")
664 test_entry('type=type;perm=perm;unique=unique; ;;;;\r\n', name=";;;;")
665 # case sensitiveness
666 set_data('Type=type;TyPe=perm;UNIQUE=unique; name\r\n')
667 _name, facts = next(self.client.mlsd())
Giampaolo Rodola'a55efb32011-05-07 16:06:59 +0200668 for x in facts:
669 self.assertTrue(x.islower())
Giampaolo Rodola'd78def92011-05-06 19:49:08 +0200670 # no data (directory empty)
671 set_data('')
672 self.assertRaises(StopIteration, next, self.client.mlsd())
673 set_data('')
674 for x in self.client.mlsd():
Berker Peksag8f791d32014-11-01 10:45:57 +0200675 self.fail("unexpected data %s" % x)
Giampaolo Rodola'd78def92011-05-06 19:49:08 +0200676
Benjamin Peterson3a53fbb2008-09-27 22:04:16 +0000677 def test_makeport(self):
Brett Cannon918e2d42010-10-29 23:26:25 +0000678 with self.client.makeport():
679 # IPv4 is in use, just make sure send_eprt has not been used
680 self.assertEqual(self.server.handler_instance.last_received_cmd,
681 'port')
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000682
683 def test_makepasv(self):
684 host, port = self.client.makepasv()
Giampaolo Rodola'0d4f08c2013-05-16 15:12:01 +0200685 conn = socket.create_connection((host, port), timeout=TIMEOUT)
Guido van Rossumd8faa362007-04-27 19:54:29 +0000686 conn.close()
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000687 # IPv4 is in use, just make sure send_epsv has not been used
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000688 self.assertEqual(self.server.handler_instance.last_received_cmd, 'pasv')
689
690 def test_with_statement(self):
691 self.client.quit()
692
693 def is_client_connected():
694 if self.client.sock is None:
695 return False
696 try:
697 self.client.sendcmd('noop')
Andrew Svetlov0832af62012-12-18 23:10:48 +0200698 except (OSError, EOFError):
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000699 return False
700 return True
701
702 # base test
Giampaolo Rodola'0d4f08c2013-05-16 15:12:01 +0200703 with ftplib.FTP(timeout=TIMEOUT) as self.client:
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000704 self.client.connect(self.server.host, self.server.port)
705 self.client.sendcmd('noop')
706 self.assertTrue(is_client_connected())
707 self.assertEqual(self.server.handler_instance.last_received_cmd, 'quit')
708 self.assertFalse(is_client_connected())
709
710 # QUIT sent inside the with block
Giampaolo Rodola'0d4f08c2013-05-16 15:12:01 +0200711 with ftplib.FTP(timeout=TIMEOUT) as self.client:
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000712 self.client.connect(self.server.host, self.server.port)
713 self.client.sendcmd('noop')
714 self.client.quit()
715 self.assertEqual(self.server.handler_instance.last_received_cmd, 'quit')
716 self.assertFalse(is_client_connected())
717
718 # force a wrong response code to be sent on QUIT: error_perm
719 # is expected and the connection is supposed to be closed
720 try:
Giampaolo Rodola'0d4f08c2013-05-16 15:12:01 +0200721 with ftplib.FTP(timeout=TIMEOUT) as self.client:
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000722 self.client.connect(self.server.host, self.server.port)
723 self.client.sendcmd('noop')
724 self.server.handler_instance.next_response = '550 error on quit'
725 except ftplib.error_perm as err:
726 self.assertEqual(str(err), '550 error on quit')
727 else:
728 self.fail('Exception not raised')
729 # needed to give the threaded server some time to set the attribute
730 # which otherwise would still be == 'noop'
731 time.sleep(0.1)
732 self.assertEqual(self.server.handler_instance.last_received_cmd, 'quit')
733 self.assertFalse(is_client_connected())
Guido van Rossumd8faa362007-04-27 19:54:29 +0000734
Giampaolo Rodolà396ff062011-02-28 19:19:51 +0000735 def test_source_address(self):
736 self.client.quit()
737 port = support.find_unused_port()
Antoine Pitrou6dca5272011-04-03 18:29:45 +0200738 try:
739 self.client.connect(self.server.host, self.server.port,
740 source_address=(HOST, port))
741 self.assertEqual(self.client.sock.getsockname()[1], port)
742 self.client.quit()
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200743 except OSError as e:
Antoine Pitrou6dca5272011-04-03 18:29:45 +0200744 if e.errno == errno.EADDRINUSE:
745 self.skipTest("couldn't bind to port %d" % port)
746 raise
Giampaolo Rodolà396ff062011-02-28 19:19:51 +0000747
748 def test_source_address_passive_connection(self):
749 port = support.find_unused_port()
750 self.client.source_address = (HOST, port)
Antoine Pitrou6dca5272011-04-03 18:29:45 +0200751 try:
752 with self.client.transfercmd('list') as sock:
753 self.assertEqual(sock.getsockname()[1], port)
Andrew Svetlovf7a17b42012-12-25 16:47:37 +0200754 except OSError as e:
Antoine Pitrou6dca5272011-04-03 18:29:45 +0200755 if e.errno == errno.EADDRINUSE:
756 self.skipTest("couldn't bind to port %d" % port)
757 raise
Giampaolo Rodolà396ff062011-02-28 19:19:51 +0000758
Giampaolo Rodolàbbc47822010-08-23 22:10:32 +0000759 def test_parse257(self):
760 self.assertEqual(ftplib.parse257('257 "/foo/bar"'), '/foo/bar')
761 self.assertEqual(ftplib.parse257('257 "/foo/bar" created'), '/foo/bar')
762 self.assertEqual(ftplib.parse257('257 ""'), '')
763 self.assertEqual(ftplib.parse257('257 "" created'), '')
764 self.assertRaises(ftplib.error_reply, ftplib.parse257, '250 "/foo/bar"')
765 # The 257 response is supposed to include the directory
766 # name and in case it contains embedded double-quotes
767 # they must be doubled (see RFC-959, chapter 7, appendix 2).
768 self.assertEqual(ftplib.parse257('257 "/foo/b""ar"'), '/foo/b"ar')
769 self.assertEqual(ftplib.parse257('257 "/foo/b""ar" created'), '/foo/b"ar')
770
Serhiy Storchakac30b1782013-10-20 16:58:27 +0300771 def test_line_too_long(self):
772 self.assertRaises(ftplib.Error, self.client.sendcmd,
773 'x' * self.client.maxline * 2)
774
775 def test_retrlines_too_long(self):
776 self.client.sendcmd('SETLONGRETR %d' % (self.client.maxline * 2))
777 received = []
778 self.assertRaises(ftplib.Error,
779 self.client.retrlines, 'retr', received.append)
780
781 def test_storlines_too_long(self):
782 f = io.BytesIO(b'x' * self.client.maxline * 2)
783 self.assertRaises(ftplib.Error, self.client.storlines, 'stor', f)
784
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000785
Serhiy Storchaka43767632013-11-03 21:31:38 +0200786@skipUnless(support.IPV6_ENABLED, "IPv6 not enabled")
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000787class TestIPv6Environment(TestCase):
788
789 def setUp(self):
Antoine Pitrouf6fbf562013-08-22 00:39:46 +0200790 self.server = DummyFTPServer((HOSTv6, 0), af=socket.AF_INET6)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000791 self.server.start()
Giampaolo Rodola'0d4f08c2013-05-16 15:12:01 +0200792 self.client = ftplib.FTP(timeout=TIMEOUT)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000793 self.client.connect(self.server.host, self.server.port)
794
795 def tearDown(self):
796 self.client.close()
797 self.server.stop()
798
799 def test_af(self):
800 self.assertEqual(self.client.af, socket.AF_INET6)
801
802 def test_makeport(self):
Brett Cannon918e2d42010-10-29 23:26:25 +0000803 with self.client.makeport():
804 self.assertEqual(self.server.handler_instance.last_received_cmd,
805 'eprt')
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000806
807 def test_makepasv(self):
808 host, port = self.client.makepasv()
Giampaolo Rodola'0d4f08c2013-05-16 15:12:01 +0200809 conn = socket.create_connection((host, port), timeout=TIMEOUT)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000810 conn.close()
Giampaolo Rodolàbd576b72010-05-10 14:53:29 +0000811 self.assertEqual(self.server.handler_instance.last_received_cmd, 'epsv')
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000812
813 def test_transfer(self):
814 def retr():
815 def callback(data):
816 received.append(data.decode('ascii'))
817 received = []
818 self.client.retrbinary('retr', callback)
Giampaolo Rodola'8bc85852012-01-09 17:10:10 +0100819 self.assertEqual(len(''.join(received)), len(RETR_DATA))
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000820 self.assertEqual(''.join(received), RETR_DATA)
821 self.client.set_pasv(True)
822 retr()
823 self.client.set_pasv(False)
824 retr()
825
826
Serhiy Storchaka43767632013-11-03 21:31:38 +0200827@skipUnless(ssl, "SSL not available")
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000828class TestTLS_FTPClassMixin(TestFTPClass):
829 """Repeat TestFTPClass tests starting the TLS layer for both control
830 and data connections first.
831 """
832
833 def setUp(self):
834 self.server = DummyTLS_FTPServer((HOST, 0))
835 self.server.start()
Giampaolo Rodola'0d4f08c2013-05-16 15:12:01 +0200836 self.client = ftplib.FTP_TLS(timeout=TIMEOUT)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000837 self.client.connect(self.server.host, self.server.port)
838 # enable TLS
839 self.client.auth()
840 self.client.prot_p()
841
842
Serhiy Storchaka43767632013-11-03 21:31:38 +0200843@skipUnless(ssl, "SSL not available")
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000844class TestTLS_FTPClass(TestCase):
845 """Specific TLS_FTP class tests."""
846
847 def setUp(self):
848 self.server = DummyTLS_FTPServer((HOST, 0))
849 self.server.start()
Giampaolo Rodola'0d4f08c2013-05-16 15:12:01 +0200850 self.client = ftplib.FTP_TLS(timeout=TIMEOUT)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000851 self.client.connect(self.server.host, self.server.port)
852
853 def tearDown(self):
854 self.client.close()
855 self.server.stop()
856
857 def test_control_connection(self):
Ezio Melottie9615932010-01-24 19:26:24 +0000858 self.assertNotIsInstance(self.client.sock, ssl.SSLSocket)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000859 self.client.auth()
Ezio Melottie9615932010-01-24 19:26:24 +0000860 self.assertIsInstance(self.client.sock, ssl.SSLSocket)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000861
862 def test_data_connection(self):
863 # clear text
Brett Cannon918e2d42010-10-29 23:26:25 +0000864 with self.client.transfercmd('list') as sock:
865 self.assertNotIsInstance(sock, ssl.SSLSocket)
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000866 self.assertEqual(self.client.voidresp(), "226 transfer complete")
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000867
868 # secured, after PROT P
869 self.client.prot_p()
Brett Cannon918e2d42010-10-29 23:26:25 +0000870 with self.client.transfercmd('list') as sock:
871 self.assertIsInstance(sock, ssl.SSLSocket)
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000872 self.assertEqual(self.client.voidresp(), "226 transfer complete")
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000873
874 # PROT C is issued, the connection must be in cleartext again
875 self.client.prot_c()
Brett Cannon918e2d42010-10-29 23:26:25 +0000876 with self.client.transfercmd('list') as sock:
877 self.assertNotIsInstance(sock, ssl.SSLSocket)
Antoine Pitrou2c4f98b2010-04-23 00:16:21 +0000878 self.assertEqual(self.client.voidresp(), "226 transfer complete")
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000879
880 def test_login(self):
881 # login() is supposed to implicitly secure the control connection
Ezio Melottie9615932010-01-24 19:26:24 +0000882 self.assertNotIsInstance(self.client.sock, ssl.SSLSocket)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000883 self.client.login()
Ezio Melottie9615932010-01-24 19:26:24 +0000884 self.assertIsInstance(self.client.sock, ssl.SSLSocket)
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000885 # make sure that AUTH TLS doesn't get issued again
886 self.client.login()
887
888 def test_auth_issued_twice(self):
889 self.client.auth()
890 self.assertRaises(ValueError, self.client.auth)
891
892 def test_auth_ssl(self):
893 try:
Benjamin Petersone32467c2014-12-05 21:59:35 -0500894 self.client.ssl_version = ssl.PROTOCOL_SSLv23
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000895 self.client.auth()
896 self.assertRaises(ValueError, self.client.auth)
897 finally:
898 self.client.ssl_version = ssl.PROTOCOL_TLSv1
899
Giampaolo Rodolàa67299e2010-05-26 18:06:04 +0000900 def test_context(self):
901 self.client.quit()
902 ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
903 self.assertRaises(ValueError, ftplib.FTP_TLS, keyfile=CERTFILE,
904 context=ctx)
905 self.assertRaises(ValueError, ftplib.FTP_TLS, certfile=CERTFILE,
906 context=ctx)
907 self.assertRaises(ValueError, ftplib.FTP_TLS, certfile=CERTFILE,
908 keyfile=CERTFILE, context=ctx)
909
Giampaolo Rodola'0d4f08c2013-05-16 15:12:01 +0200910 self.client = ftplib.FTP_TLS(context=ctx, timeout=TIMEOUT)
Giampaolo Rodolàa67299e2010-05-26 18:06:04 +0000911 self.client.connect(self.server.host, self.server.port)
912 self.assertNotIsInstance(self.client.sock, ssl.SSLSocket)
913 self.client.auth()
914 self.assertIs(self.client.sock.context, ctx)
915 self.assertIsInstance(self.client.sock, ssl.SSLSocket)
916
917 self.client.prot_p()
Brett Cannon918e2d42010-10-29 23:26:25 +0000918 with self.client.transfercmd('list') as sock:
919 self.assertIs(sock.context, ctx)
920 self.assertIsInstance(sock, ssl.SSLSocket)
Giampaolo Rodolàa67299e2010-05-26 18:06:04 +0000921
Giampaolo Rodola'096dcb12011-06-27 11:17:51 +0200922 def test_ccc(self):
923 self.assertRaises(ValueError, self.client.ccc)
924 self.client.login(secure=True)
925 self.assertIsInstance(self.client.sock, ssl.SSLSocket)
926 self.client.ccc()
927 self.assertRaises(ValueError, self.client.sock.unwrap)
Giampaolo Rodola'096dcb12011-06-27 11:17:51 +0200928
Christian Heimese5b5edf2013-12-02 02:56:02 +0100929 def test_check_hostname(self):
930 self.client.quit()
931 ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
932 ctx.verify_mode = ssl.CERT_REQUIRED
933 ctx.check_hostname = True
934 ctx.load_verify_locations(CAFILE)
935 self.client = ftplib.FTP_TLS(context=ctx, timeout=TIMEOUT)
936
937 # 127.0.0.1 doesn't match SAN
938 self.client.connect(self.server.host, self.server.port)
939 with self.assertRaises(ssl.CertificateError):
940 self.client.auth()
941 # exception quits connection
942
943 self.client.connect(self.server.host, self.server.port)
944 self.client.prot_p()
945 with self.assertRaises(ssl.CertificateError):
946 with self.client.transfercmd("list") as sock:
947 pass
948 self.client.quit()
949
950 self.client.connect("localhost", self.server.port)
951 self.client.auth()
952 self.client.quit()
953
954 self.client.connect("localhost", self.server.port)
955 self.client.prot_p()
956 with self.client.transfercmd("list") as sock:
957 pass
958
Antoine Pitrouf988cd02009-11-17 20:21:14 +0000959
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000960class TestTimeouts(TestCase):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000961
962 def setUp(self):
Guido van Rossumd8faa362007-04-27 19:54:29 +0000963 self.evt = threading.Event()
Christian Heimes5e696852008-04-09 08:37:03 +0000964 self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Antoine Pitrou08d02722012-12-19 20:44:02 +0100965 self.sock.settimeout(20)
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000966 self.port = support.bind_port(self.sock)
Antoine Pitrou08d02722012-12-19 20:44:02 +0100967 self.server_thread = threading.Thread(target=self.server)
968 self.server_thread.start()
Christian Heimes836baa52008-02-26 08:18:30 +0000969 # Wait for the server to be ready.
970 self.evt.wait()
971 self.evt.clear()
Antoine Pitrou08d02722012-12-19 20:44:02 +0100972 self.old_port = ftplib.FTP.port
Christian Heimes5e696852008-04-09 08:37:03 +0000973 ftplib.FTP.port = self.port
Guido van Rossumd8faa362007-04-27 19:54:29 +0000974
975 def tearDown(self):
Antoine Pitrou08d02722012-12-19 20:44:02 +0100976 ftplib.FTP.port = self.old_port
977 self.server_thread.join()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000978
Antoine Pitrou08d02722012-12-19 20:44:02 +0100979 def server(self):
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000980 # This method sets the evt 3 times:
981 # 1) when the connection is ready to be accepted.
982 # 2) when it is safe for the caller to close the connection
983 # 3) when we have closed the socket
Charles-François Natali6e204602014-07-23 19:28:13 +0100984 self.sock.listen()
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000985 # (1) Signal the caller that we are ready to accept the connection.
Antoine Pitrou08d02722012-12-19 20:44:02 +0100986 self.evt.set()
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000987 try:
Antoine Pitrou08d02722012-12-19 20:44:02 +0100988 conn, addr = self.sock.accept()
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000989 except socket.timeout:
990 pass
991 else:
Antoine Pitrou08d02722012-12-19 20:44:02 +0100992 conn.sendall(b"1 Hola mundo\n")
993 conn.shutdown(socket.SHUT_WR)
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000994 # (2) Signal the caller that it is safe to close the socket.
Antoine Pitrou08d02722012-12-19 20:44:02 +0100995 self.evt.set()
Benjamin Petersonbe17a112008-09-27 21:49:47 +0000996 conn.close()
997 finally:
Antoine Pitrou08d02722012-12-19 20:44:02 +0100998 self.sock.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +0000999
1000 def testTimeoutDefault(self):
Georg Brandlf78e02b2008-06-10 17:40:04 +00001001 # default -- use global socket timeout
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +02001002 self.assertIsNone(socket.getdefaulttimeout())
Georg Brandlf78e02b2008-06-10 17:40:04 +00001003 socket.setdefaulttimeout(30)
1004 try:
Antoine Pitrouf6fbf562013-08-22 00:39:46 +02001005 ftp = ftplib.FTP(HOST)
Georg Brandlf78e02b2008-06-10 17:40:04 +00001006 finally:
1007 socket.setdefaulttimeout(None)
1008 self.assertEqual(ftp.sock.gettimeout(), 30)
1009 self.evt.wait()
1010 ftp.close()
1011
1012 def testTimeoutNone(self):
1013 # no timeout -- do not use global socket timeout
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +02001014 self.assertIsNone(socket.getdefaulttimeout())
Georg Brandlf78e02b2008-06-10 17:40:04 +00001015 socket.setdefaulttimeout(30)
1016 try:
Antoine Pitrouf6fbf562013-08-22 00:39:46 +02001017 ftp = ftplib.FTP(HOST, timeout=None)
Georg Brandlf78e02b2008-06-10 17:40:04 +00001018 finally:
1019 socket.setdefaulttimeout(None)
Serhiy Storchaka25d8aea2014-02-08 14:50:08 +02001020 self.assertIsNone(ftp.sock.gettimeout())
Christian Heimes836baa52008-02-26 08:18:30 +00001021 self.evt.wait()
Georg Brandlf78e02b2008-06-10 17:40:04 +00001022 ftp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001023
1024 def testTimeoutValue(self):
1025 # a value
Christian Heimes5e696852008-04-09 08:37:03 +00001026 ftp = ftplib.FTP(HOST, timeout=30)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001027 self.assertEqual(ftp.sock.gettimeout(), 30)
Christian Heimes836baa52008-02-26 08:18:30 +00001028 self.evt.wait()
Georg Brandlf78e02b2008-06-10 17:40:04 +00001029 ftp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001030
1031 def testTimeoutConnect(self):
1032 ftp = ftplib.FTP()
Christian Heimes5e696852008-04-09 08:37:03 +00001033 ftp.connect(HOST, timeout=30)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001034 self.assertEqual(ftp.sock.gettimeout(), 30)
Christian Heimes836baa52008-02-26 08:18:30 +00001035 self.evt.wait()
Georg Brandlf78e02b2008-06-10 17:40:04 +00001036 ftp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001037
1038 def testTimeoutDifferentOrder(self):
1039 ftp = ftplib.FTP(timeout=30)
Christian Heimes5e696852008-04-09 08:37:03 +00001040 ftp.connect(HOST)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001041 self.assertEqual(ftp.sock.gettimeout(), 30)
Christian Heimes836baa52008-02-26 08:18:30 +00001042 self.evt.wait()
Georg Brandlf78e02b2008-06-10 17:40:04 +00001043 ftp.close()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001044
1045 def testTimeoutDirectAccess(self):
1046 ftp = ftplib.FTP()
1047 ftp.timeout = 30
Christian Heimes5e696852008-04-09 08:37:03 +00001048 ftp.connect(HOST)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001049 self.assertEqual(ftp.sock.gettimeout(), 30)
Christian Heimes836baa52008-02-26 08:18:30 +00001050 self.evt.wait()
Guido van Rossumd8faa362007-04-27 19:54:29 +00001051 ftp.close()
1052
1053
Martin Panter19e69c52015-11-14 12:46:42 +00001054class MiscTestCase(TestCase):
1055 def test__all__(self):
1056 blacklist = {'MSG_OOB', 'FTP_PORT', 'MAXLINE', 'CRLF', 'B_CRLF',
1057 'Error', 'parse150', 'parse227', 'parse229', 'parse257',
1058 'print_line', 'ftpcp', 'test'}
1059 support.check__all__(self, ftplib, blacklist=blacklist)
1060
1061
Benjamin Petersonbe17a112008-09-27 21:49:47 +00001062def test_main():
Berker Peksag8f791d32014-11-01 10:45:57 +02001063 tests = [TestFTPClass, TestTimeouts,
Serhiy Storchaka43767632013-11-03 21:31:38 +02001064 TestIPv6Environment,
Martin Panter19e69c52015-11-14 12:46:42 +00001065 TestTLS_FTPClassMixin, TestTLS_FTPClass,
1066 MiscTestCase]
Antoine Pitrouf988cd02009-11-17 20:21:14 +00001067
Benjamin Petersonbe17a112008-09-27 21:49:47 +00001068 thread_info = support.threading_setup()
1069 try:
1070 support.run_unittest(*tests)
1071 finally:
1072 support.threading_cleanup(*thread_info)
1073
Guido van Rossumd8faa362007-04-27 19:54:29 +00001074
1075if __name__ == '__main__':
1076 test_main()