Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 1 | """Test script for ftplib module.""" |
| 2 | |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 3 | # Modified by Giampaolo Rodola' to test FTP class, IPv6 and TLS |
| 4 | # environment |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 6 | import ftplib |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 7 | import asyncore |
| 8 | import asynchat |
| 9 | import socket |
| 10 | import io |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 11 | import errno |
| 12 | import os |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 13 | import time |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 14 | try: |
| 15 | import ssl |
| 16 | except ImportError: |
| 17 | ssl = None |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 18 | |
| 19 | from unittest import TestCase |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 20 | from test import support |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 21 | from test.support import HOST |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 22 | threading = support.import_module('threading') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 23 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 24 | # the dummy data returned by server over the data channel when |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 25 | # RETR, LIST, NLST, MLSD commands are issued |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 26 | RETR_DATA = 'abcde12345\r\n' * 1000 |
| 27 | LIST_DATA = 'foo\r\nbar\r\n' |
| 28 | NLST_DATA = 'foo\r\nbar\r\n' |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 29 | MLSD_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 Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 44 | |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 45 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 46 | class DummyDTPHandler(asynchat.async_chat): |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 47 | dtp_conn_closed = False |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 48 | |
| 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à | f96482e | 2010-08-04 10:36:18 +0000 | [diff] [blame] | 55 | self.baseclass.last_received_data += self.recv(1024).decode('ascii') |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 56 | |
| 57 | def handle_close(self): |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 58 | # 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 Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 65 | |
| 66 | def push(self, what): |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 67 | 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à | f96482e | 2010-08-04 10:36:18 +0000 | [diff] [blame] | 72 | super(DummyDTPHandler, self).push(what.encode('ascii')) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 73 | |
Giampaolo Rodolà | d930b63 | 2010-05-06 20:21:57 +0000 | [diff] [blame] | 74 | def handle_error(self): |
| 75 | raise |
| 76 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 77 | |
| 78 | class DummyFTPHandler(asynchat.async_chat): |
| 79 | |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 80 | dtp_handler = DummyDTPHandler |
| 81 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 82 | def __init__(self, conn): |
| 83 | asynchat.async_chat.__init__(self, conn) |
Giampaolo Rodola' | 0b5c21f | 2011-05-07 19:03:47 +0200 | [diff] [blame] | 84 | # tells the socket to handle urgent data inline (ABOR command) |
| 85 | self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_OOBINLINE, 1) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 86 | self.set_terminator(b"\r\n") |
| 87 | self.in_buffer = [] |
| 88 | self.dtp = None |
| 89 | self.last_received_cmd = None |
| 90 | self.last_received_data = '' |
| 91 | self.next_response = '' |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 92 | self.next_data = None |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 93 | self.rest = None |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 94 | self.push('220 welcome') |
| 95 | |
| 96 | def collect_incoming_data(self, data): |
| 97 | self.in_buffer.append(data) |
| 98 | |
| 99 | def found_terminator(self): |
| 100 | line = b''.join(self.in_buffer).decode('ascii') |
| 101 | self.in_buffer = [] |
| 102 | if self.next_response: |
| 103 | self.push(self.next_response) |
| 104 | self.next_response = '' |
| 105 | cmd = line.split(' ')[0].lower() |
| 106 | self.last_received_cmd = cmd |
| 107 | space = line.find(' ') |
| 108 | if space != -1: |
| 109 | arg = line[space + 1:] |
| 110 | else: |
| 111 | arg = "" |
| 112 | if hasattr(self, 'cmd_' + cmd): |
| 113 | method = getattr(self, 'cmd_' + cmd) |
| 114 | method(arg) |
| 115 | else: |
| 116 | self.push('550 command "%s" not understood.' %cmd) |
| 117 | |
| 118 | def handle_error(self): |
| 119 | raise |
| 120 | |
| 121 | def push(self, data): |
| 122 | asynchat.async_chat.push(self, data.encode('ascii') + b'\r\n') |
| 123 | |
| 124 | def cmd_port(self, arg): |
| 125 | addr = list(map(int, arg.split(','))) |
| 126 | ip = '%d.%d.%d.%d' %tuple(addr[:4]) |
| 127 | port = (addr[4] * 256) + addr[5] |
Giampaolo Rodola' | 842e567 | 2011-05-07 18:47:31 +0200 | [diff] [blame] | 128 | s = socket.create_connection((ip, port), timeout=2) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 129 | self.dtp = self.dtp_handler(s, baseclass=self) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 130 | self.push('200 active data connection established') |
| 131 | |
| 132 | def cmd_pasv(self, arg): |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 133 | with socket.socket() as sock: |
| 134 | sock.bind((self.socket.getsockname()[0], 0)) |
| 135 | sock.listen(5) |
| 136 | sock.settimeout(10) |
| 137 | ip, port = sock.getsockname()[:2] |
| 138 | ip = ip.replace('.', ','); p1 = port / 256; p2 = port % 256 |
| 139 | self.push('227 entering passive mode (%s,%d,%d)' %(ip, p1, p2)) |
| 140 | conn, addr = sock.accept() |
| 141 | self.dtp = self.dtp_handler(conn, baseclass=self) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 142 | |
| 143 | def cmd_eprt(self, arg): |
| 144 | af, ip, port = arg.split(arg[0])[1:-1] |
| 145 | port = int(port) |
Giampaolo Rodola' | 842e567 | 2011-05-07 18:47:31 +0200 | [diff] [blame] | 146 | s = socket.create_connection((ip, port), timeout=2) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 147 | self.dtp = self.dtp_handler(s, baseclass=self) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 148 | self.push('200 active data connection established') |
| 149 | |
| 150 | def cmd_epsv(self, arg): |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 151 | with socket.socket(socket.AF_INET6) as sock: |
| 152 | sock.bind((self.socket.getsockname()[0], 0)) |
| 153 | sock.listen(5) |
| 154 | sock.settimeout(10) |
| 155 | port = sock.getsockname()[1] |
| 156 | self.push('229 entering extended passive mode (|||%d|)' %port) |
| 157 | conn, addr = sock.accept() |
| 158 | self.dtp = self.dtp_handler(conn, baseclass=self) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 159 | |
| 160 | def cmd_echo(self, arg): |
| 161 | # sends back the received string (used by the test suite) |
| 162 | self.push(arg) |
| 163 | |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 164 | def cmd_noop(self, arg): |
| 165 | self.push('200 noop ok') |
| 166 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 167 | def cmd_user(self, arg): |
| 168 | self.push('331 username ok') |
| 169 | |
| 170 | def cmd_pass(self, arg): |
| 171 | self.push('230 password ok') |
| 172 | |
| 173 | def cmd_acct(self, arg): |
| 174 | self.push('230 acct ok') |
| 175 | |
| 176 | def cmd_rnfr(self, arg): |
| 177 | self.push('350 rnfr ok') |
| 178 | |
| 179 | def cmd_rnto(self, arg): |
| 180 | self.push('250 rnto ok') |
| 181 | |
| 182 | def cmd_dele(self, arg): |
| 183 | self.push('250 dele ok') |
| 184 | |
| 185 | def cmd_cwd(self, arg): |
| 186 | self.push('250 cwd ok') |
| 187 | |
| 188 | def cmd_size(self, arg): |
| 189 | self.push('250 1000') |
| 190 | |
| 191 | def cmd_mkd(self, arg): |
| 192 | self.push('257 "%s"' %arg) |
| 193 | |
| 194 | def cmd_rmd(self, arg): |
| 195 | self.push('250 rmd ok') |
| 196 | |
| 197 | def cmd_pwd(self, arg): |
| 198 | self.push('257 "pwd ok"') |
| 199 | |
| 200 | def cmd_type(self, arg): |
Giampaolo Rodolà | f96482e | 2010-08-04 10:36:18 +0000 | [diff] [blame] | 201 | self.push('200 type ok') |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 202 | |
| 203 | def cmd_quit(self, arg): |
| 204 | self.push('221 quit ok') |
| 205 | self.close() |
| 206 | |
Giampaolo Rodola' | 0b5c21f | 2011-05-07 19:03:47 +0200 | [diff] [blame] | 207 | def cmd_abor(self, arg): |
| 208 | self.push('226 abor ok') |
| 209 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 210 | def cmd_stor(self, arg): |
| 211 | self.push('125 stor ok') |
| 212 | |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 213 | def cmd_rest(self, arg): |
| 214 | self.rest = arg |
| 215 | self.push('350 rest ok') |
| 216 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 217 | def cmd_retr(self, arg): |
| 218 | self.push('125 retr ok') |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 219 | if self.rest is not None: |
| 220 | offset = int(self.rest) |
| 221 | else: |
| 222 | offset = 0 |
Giampaolo Rodolà | f96482e | 2010-08-04 10:36:18 +0000 | [diff] [blame] | 223 | self.dtp.push(RETR_DATA[offset:]) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 224 | self.dtp.close_when_done() |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 225 | self.rest = None |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 226 | |
| 227 | def cmd_list(self, arg): |
| 228 | self.push('125 list ok') |
| 229 | self.dtp.push(LIST_DATA) |
| 230 | self.dtp.close_when_done() |
| 231 | |
| 232 | def cmd_nlst(self, arg): |
| 233 | self.push('125 nlst ok') |
| 234 | self.dtp.push(NLST_DATA) |
| 235 | self.dtp.close_when_done() |
| 236 | |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 237 | def cmd_opts(self, arg): |
| 238 | self.push('200 opts ok') |
| 239 | |
| 240 | def cmd_mlsd(self, arg): |
| 241 | self.push('125 mlsd ok') |
| 242 | self.dtp.push(MLSD_DATA) |
| 243 | self.dtp.close_when_done() |
| 244 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 245 | |
| 246 | class DummyFTPServer(asyncore.dispatcher, threading.Thread): |
| 247 | |
| 248 | handler = DummyFTPHandler |
| 249 | |
| 250 | def __init__(self, address, af=socket.AF_INET): |
| 251 | threading.Thread.__init__(self) |
| 252 | asyncore.dispatcher.__init__(self) |
| 253 | self.create_socket(af, socket.SOCK_STREAM) |
| 254 | self.bind(address) |
| 255 | self.listen(5) |
| 256 | self.active = False |
| 257 | self.active_lock = threading.Lock() |
| 258 | self.host, self.port = self.socket.getsockname()[:2] |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 259 | self.handler_instance = None |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 260 | |
| 261 | def start(self): |
| 262 | assert not self.active |
| 263 | self.__flag = threading.Event() |
| 264 | threading.Thread.start(self) |
| 265 | self.__flag.wait() |
| 266 | |
| 267 | def run(self): |
| 268 | self.active = True |
| 269 | self.__flag.set() |
| 270 | while self.active and asyncore.socket_map: |
| 271 | self.active_lock.acquire() |
| 272 | asyncore.loop(timeout=0.1, count=1) |
| 273 | self.active_lock.release() |
| 274 | asyncore.close_all(ignore_all=True) |
| 275 | |
| 276 | def stop(self): |
| 277 | assert self.active |
| 278 | self.active = False |
| 279 | self.join() |
| 280 | |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 281 | def handle_accepted(self, conn, addr): |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 282 | self.handler_instance = self.handler(conn) |
Benjamin Peterson | d06e3b0 | 2008-09-28 21:00:42 +0000 | [diff] [blame] | 283 | |
| 284 | def handle_connect(self): |
| 285 | self.close() |
| 286 | handle_read = handle_connect |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 287 | |
| 288 | def writable(self): |
| 289 | return 0 |
| 290 | |
| 291 | def handle_error(self): |
| 292 | raise |
| 293 | |
| 294 | |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 295 | if ssl is not None: |
| 296 | |
| 297 | CERTFILE = os.path.join(os.path.dirname(__file__), "keycert.pem") |
| 298 | |
| 299 | class SSLConnection(asyncore.dispatcher): |
| 300 | """An asyncore.dispatcher subclass supporting TLS/SSL.""" |
| 301 | |
| 302 | _ssl_accepting = False |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 303 | _ssl_closing = False |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 304 | |
| 305 | def secure_connection(self): |
| 306 | self.del_channel() |
| 307 | socket = ssl.wrap_socket(self.socket, suppress_ragged_eofs=False, |
| 308 | certfile=CERTFILE, server_side=True, |
| 309 | do_handshake_on_connect=False, |
| 310 | ssl_version=ssl.PROTOCOL_SSLv23) |
| 311 | self.set_socket(socket) |
| 312 | self._ssl_accepting = True |
| 313 | |
| 314 | def _do_ssl_handshake(self): |
| 315 | try: |
| 316 | self.socket.do_handshake() |
| 317 | except ssl.SSLError as err: |
| 318 | if err.args[0] in (ssl.SSL_ERROR_WANT_READ, |
| 319 | ssl.SSL_ERROR_WANT_WRITE): |
| 320 | return |
| 321 | elif err.args[0] == ssl.SSL_ERROR_EOF: |
| 322 | return self.handle_close() |
| 323 | raise |
| 324 | except socket.error as err: |
| 325 | if err.args[0] == errno.ECONNABORTED: |
| 326 | return self.handle_close() |
| 327 | else: |
| 328 | self._ssl_accepting = False |
| 329 | |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 330 | def _do_ssl_shutdown(self): |
| 331 | self._ssl_closing = True |
| 332 | try: |
| 333 | self.socket = self.socket.unwrap() |
| 334 | except ssl.SSLError as err: |
| 335 | if err.args[0] in (ssl.SSL_ERROR_WANT_READ, |
| 336 | ssl.SSL_ERROR_WANT_WRITE): |
| 337 | return |
| 338 | except socket.error as err: |
| 339 | # Any "socket error" corresponds to a SSL_ERROR_SYSCALL return |
| 340 | # from OpenSSL's SSL_shutdown(), corresponding to a |
| 341 | # closed socket condition. See also: |
| 342 | # http://www.mail-archive.com/openssl-users@openssl.org/msg60710.html |
| 343 | pass |
| 344 | self._ssl_closing = False |
| 345 | super(SSLConnection, self).close() |
| 346 | |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 347 | def handle_read_event(self): |
| 348 | if self._ssl_accepting: |
| 349 | self._do_ssl_handshake() |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 350 | elif self._ssl_closing: |
| 351 | self._do_ssl_shutdown() |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 352 | else: |
| 353 | super(SSLConnection, self).handle_read_event() |
| 354 | |
| 355 | def handle_write_event(self): |
| 356 | if self._ssl_accepting: |
| 357 | self._do_ssl_handshake() |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 358 | elif self._ssl_closing: |
| 359 | self._do_ssl_shutdown() |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 360 | else: |
| 361 | super(SSLConnection, self).handle_write_event() |
| 362 | |
| 363 | def send(self, data): |
| 364 | try: |
| 365 | return super(SSLConnection, self).send(data) |
| 366 | except ssl.SSLError as err: |
Antoine Pitrou | 5733c08 | 2010-03-22 14:49:10 +0000 | [diff] [blame] | 367 | if err.args[0] in (ssl.SSL_ERROR_EOF, ssl.SSL_ERROR_ZERO_RETURN, |
| 368 | ssl.SSL_ERROR_WANT_READ, |
| 369 | ssl.SSL_ERROR_WANT_WRITE): |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 370 | return 0 |
| 371 | raise |
| 372 | |
| 373 | def recv(self, buffer_size): |
| 374 | try: |
| 375 | return super(SSLConnection, self).recv(buffer_size) |
| 376 | except ssl.SSLError as err: |
Antoine Pitrou | 5733c08 | 2010-03-22 14:49:10 +0000 | [diff] [blame] | 377 | if err.args[0] in (ssl.SSL_ERROR_WANT_READ, |
| 378 | ssl.SSL_ERROR_WANT_WRITE): |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 379 | return b'' |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 380 | if err.args[0] in (ssl.SSL_ERROR_EOF, ssl.SSL_ERROR_ZERO_RETURN): |
| 381 | self.handle_close() |
| 382 | return b'' |
| 383 | raise |
| 384 | |
| 385 | def handle_error(self): |
| 386 | raise |
| 387 | |
| 388 | def close(self): |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 389 | if (isinstance(self.socket, ssl.SSLSocket) and |
| 390 | self.socket._sslobj is not None): |
| 391 | self._do_ssl_shutdown() |
Benjamin Peterson | 1bd93a7 | 2010-10-31 19:58:07 +0000 | [diff] [blame] | 392 | else: |
| 393 | super(SSLConnection, self).close() |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 394 | |
| 395 | |
| 396 | class DummyTLS_DTPHandler(SSLConnection, DummyDTPHandler): |
| 397 | """A DummyDTPHandler subclass supporting TLS/SSL.""" |
| 398 | |
| 399 | def __init__(self, conn, baseclass): |
| 400 | DummyDTPHandler.__init__(self, conn, baseclass) |
| 401 | if self.baseclass.secure_data_channel: |
| 402 | self.secure_connection() |
| 403 | |
| 404 | |
| 405 | class DummyTLS_FTPHandler(SSLConnection, DummyFTPHandler): |
| 406 | """A DummyFTPHandler subclass supporting TLS/SSL.""" |
| 407 | |
| 408 | dtp_handler = DummyTLS_DTPHandler |
| 409 | |
| 410 | def __init__(self, conn): |
| 411 | DummyFTPHandler.__init__(self, conn) |
| 412 | self.secure_data_channel = False |
| 413 | |
| 414 | def cmd_auth(self, line): |
| 415 | """Set up secure control channel.""" |
| 416 | self.push('234 AUTH TLS successful') |
| 417 | self.secure_connection() |
| 418 | |
| 419 | def cmd_pbsz(self, line): |
| 420 | """Negotiate size of buffer for secure data transfer. |
| 421 | For TLS/SSL the only valid value for the parameter is '0'. |
| 422 | Any other value is accepted but ignored. |
| 423 | """ |
| 424 | self.push('200 PBSZ=0 successful.') |
| 425 | |
| 426 | def cmd_prot(self, line): |
| 427 | """Setup un/secure data channel.""" |
| 428 | arg = line.upper() |
| 429 | if arg == 'C': |
| 430 | self.push('200 Protection set to Clear') |
| 431 | self.secure_data_channel = False |
| 432 | elif arg == 'P': |
| 433 | self.push('200 Protection set to Private') |
| 434 | self.secure_data_channel = True |
| 435 | else: |
| 436 | self.push("502 Unrecognized PROT type (use C or P).") |
| 437 | |
| 438 | |
| 439 | class DummyTLS_FTPServer(DummyFTPServer): |
| 440 | handler = DummyTLS_FTPHandler |
| 441 | |
| 442 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 443 | class TestFTPClass(TestCase): |
| 444 | |
| 445 | def setUp(self): |
| 446 | self.server = DummyFTPServer((HOST, 0)) |
| 447 | self.server.start() |
Giampaolo Rodola' | 842e567 | 2011-05-07 18:47:31 +0200 | [diff] [blame] | 448 | self.client = ftplib.FTP(timeout=2) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 449 | self.client.connect(self.server.host, self.server.port) |
| 450 | |
| 451 | def tearDown(self): |
| 452 | self.client.close() |
| 453 | self.server.stop() |
| 454 | |
| 455 | def test_getwelcome(self): |
| 456 | self.assertEqual(self.client.getwelcome(), '220 welcome') |
| 457 | |
| 458 | def test_sanitize(self): |
| 459 | self.assertEqual(self.client.sanitize('foo'), repr('foo')) |
| 460 | self.assertEqual(self.client.sanitize('pass 12345'), repr('pass *****')) |
| 461 | self.assertEqual(self.client.sanitize('PASS 12345'), repr('PASS *****')) |
| 462 | |
| 463 | def test_exceptions(self): |
| 464 | self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 400') |
| 465 | self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 499') |
| 466 | self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 500') |
| 467 | self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 599') |
| 468 | self.assertRaises(ftplib.error_proto, self.client.sendcmd, 'echo 999') |
| 469 | |
| 470 | def test_all_errors(self): |
| 471 | exceptions = (ftplib.error_reply, ftplib.error_temp, ftplib.error_perm, |
| 472 | ftplib.error_proto, ftplib.Error, IOError, EOFError) |
| 473 | for x in exceptions: |
| 474 | try: |
| 475 | raise x('exception not included in all_errors set') |
| 476 | except ftplib.all_errors: |
| 477 | pass |
| 478 | |
| 479 | def test_set_pasv(self): |
| 480 | # passive mode is supposed to be enabled by default |
| 481 | self.assertTrue(self.client.passiveserver) |
| 482 | self.client.set_pasv(True) |
| 483 | self.assertTrue(self.client.passiveserver) |
| 484 | self.client.set_pasv(False) |
| 485 | self.assertFalse(self.client.passiveserver) |
| 486 | |
| 487 | def test_voidcmd(self): |
| 488 | self.client.voidcmd('echo 200') |
| 489 | self.client.voidcmd('echo 299') |
| 490 | self.assertRaises(ftplib.error_reply, self.client.voidcmd, 'echo 199') |
| 491 | self.assertRaises(ftplib.error_reply, self.client.voidcmd, 'echo 300') |
| 492 | |
| 493 | def test_login(self): |
| 494 | self.client.login() |
| 495 | |
| 496 | def test_acct(self): |
| 497 | self.client.acct('passwd') |
| 498 | |
| 499 | def test_rename(self): |
| 500 | self.client.rename('a', 'b') |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 501 | self.server.handler_instance.next_response = '200' |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 502 | self.assertRaises(ftplib.error_reply, self.client.rename, 'a', 'b') |
| 503 | |
| 504 | def test_delete(self): |
| 505 | self.client.delete('foo') |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 506 | self.server.handler_instance.next_response = '199' |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 507 | self.assertRaises(ftplib.error_reply, self.client.delete, 'foo') |
| 508 | |
| 509 | def test_size(self): |
| 510 | self.client.size('foo') |
| 511 | |
| 512 | def test_mkd(self): |
| 513 | dir = self.client.mkd('/foo') |
| 514 | self.assertEqual(dir, '/foo') |
| 515 | |
| 516 | def test_rmd(self): |
| 517 | self.client.rmd('foo') |
| 518 | |
| 519 | def test_pwd(self): |
| 520 | dir = self.client.pwd() |
| 521 | self.assertEqual(dir, 'pwd ok') |
| 522 | |
| 523 | def test_quit(self): |
| 524 | self.assertEqual(self.client.quit(), '221 quit ok') |
| 525 | # Ensure the connection gets closed; sock attribute should be None |
| 526 | self.assertEqual(self.client.sock, None) |
| 527 | |
Giampaolo Rodola' | 0b5c21f | 2011-05-07 19:03:47 +0200 | [diff] [blame] | 528 | def test_abort(self): |
| 529 | self.client.abort() |
| 530 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 531 | def test_retrbinary(self): |
| 532 | def callback(data): |
| 533 | received.append(data.decode('ascii')) |
| 534 | received = [] |
| 535 | self.client.retrbinary('retr', callback) |
| 536 | self.assertEqual(''.join(received), RETR_DATA) |
| 537 | |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 538 | def test_retrbinary_rest(self): |
| 539 | def callback(data): |
| 540 | received.append(data.decode('ascii')) |
| 541 | for rest in (0, 10, 20): |
| 542 | received = [] |
| 543 | self.client.retrbinary('retr', callback, rest=rest) |
| 544 | self.assertEqual(''.join(received), RETR_DATA[rest:], |
| 545 | msg='rest test case %d %d %d' % (rest, |
| 546 | len(''.join(received)), |
| 547 | len(RETR_DATA[rest:]))) |
| 548 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 549 | def test_retrlines(self): |
| 550 | received = [] |
| 551 | self.client.retrlines('retr', received.append) |
Giampaolo Rodolà | f96482e | 2010-08-04 10:36:18 +0000 | [diff] [blame] | 552 | self.assertEqual(''.join(received), RETR_DATA.replace('\r\n', '')) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 553 | |
| 554 | def test_storbinary(self): |
| 555 | f = io.BytesIO(RETR_DATA.encode('ascii')) |
| 556 | self.client.storbinary('stor', f) |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 557 | self.assertEqual(self.server.handler_instance.last_received_data, RETR_DATA) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 558 | # test new callback arg |
| 559 | flag = [] |
| 560 | f.seek(0) |
| 561 | self.client.storbinary('stor', f, callback=lambda x: flag.append(None)) |
| 562 | self.assertTrue(flag) |
| 563 | |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 564 | def test_storbinary_rest(self): |
| 565 | f = io.BytesIO(RETR_DATA.replace('\r\n', '\n').encode('ascii')) |
| 566 | for r in (30, '30'): |
| 567 | f.seek(0) |
| 568 | self.client.storbinary('stor', f, rest=r) |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 569 | self.assertEqual(self.server.handler_instance.rest, str(r)) |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 570 | |
Giampaolo Rodolà | f96482e | 2010-08-04 10:36:18 +0000 | [diff] [blame] | 571 | def test_storlines(self): |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 572 | f = io.BytesIO(RETR_DATA.replace('\r\n', '\n').encode('ascii')) |
| 573 | self.client.storlines('stor', f) |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 574 | self.assertEqual(self.server.handler_instance.last_received_data, RETR_DATA) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 575 | # test new callback arg |
| 576 | flag = [] |
| 577 | f.seek(0) |
| 578 | self.client.storlines('stor foo', f, callback=lambda x: flag.append(None)) |
| 579 | self.assertTrue(flag) |
| 580 | |
| 581 | def test_nlst(self): |
| 582 | self.client.nlst() |
| 583 | self.assertEqual(self.client.nlst(), NLST_DATA.split('\r\n')[:-1]) |
| 584 | |
| 585 | def test_dir(self): |
| 586 | l = [] |
| 587 | self.client.dir(lambda x: l.append(x)) |
| 588 | self.assertEqual(''.join(l), LIST_DATA.replace('\r\n', '')) |
| 589 | |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 590 | def test_mlsd(self): |
| 591 | list(self.client.mlsd()) |
| 592 | list(self.client.mlsd(path='/')) |
| 593 | list(self.client.mlsd(path='/', facts=['size', 'type'])) |
| 594 | |
| 595 | ls = list(self.client.mlsd()) |
| 596 | for name, facts in ls: |
Giampaolo Rodola' | a55efb3 | 2011-05-07 16:06:59 +0200 | [diff] [blame] | 597 | self.assertIsInstance(name, str) |
| 598 | self.assertIsInstance(facts, dict) |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 599 | self.assertTrue(name) |
Giampaolo Rodola' | a55efb3 | 2011-05-07 16:06:59 +0200 | [diff] [blame] | 600 | self.assertIn('type', facts) |
| 601 | self.assertIn('perm', facts) |
| 602 | self.assertIn('unique', facts) |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 603 | |
| 604 | def set_data(data): |
| 605 | self.server.handler_instance.next_data = data |
| 606 | |
| 607 | def test_entry(line, type=None, perm=None, unique=None, name=None): |
| 608 | type = 'type' if type is None else type |
| 609 | perm = 'perm' if perm is None else perm |
| 610 | unique = 'unique' if unique is None else unique |
| 611 | name = 'name' if name is None else name |
| 612 | set_data(line) |
| 613 | _name, facts = next(self.client.mlsd()) |
| 614 | self.assertEqual(_name, name) |
| 615 | self.assertEqual(facts['type'], type) |
| 616 | self.assertEqual(facts['perm'], perm) |
| 617 | self.assertEqual(facts['unique'], unique) |
| 618 | |
| 619 | # plain |
| 620 | test_entry('type=type;perm=perm;unique=unique; name\r\n') |
| 621 | # "=" in fact value |
| 622 | test_entry('type=ty=pe;perm=perm;unique=unique; name\r\n', type="ty=pe") |
| 623 | test_entry('type==type;perm=perm;unique=unique; name\r\n', type="=type") |
| 624 | test_entry('type=t=y=pe;perm=perm;unique=unique; name\r\n', type="t=y=pe") |
| 625 | test_entry('type=====;perm=perm;unique=unique; name\r\n', type="====") |
| 626 | # spaces in name |
| 627 | test_entry('type=type;perm=perm;unique=unique; na me\r\n', name="na me") |
| 628 | test_entry('type=type;perm=perm;unique=unique; name \r\n', name="name ") |
| 629 | test_entry('type=type;perm=perm;unique=unique; name\r\n', name=" name") |
| 630 | test_entry('type=type;perm=perm;unique=unique; n am e\r\n', name="n am e") |
| 631 | # ";" in name |
| 632 | test_entry('type=type;perm=perm;unique=unique; na;me\r\n', name="na;me") |
| 633 | test_entry('type=type;perm=perm;unique=unique; ;name\r\n', name=";name") |
| 634 | test_entry('type=type;perm=perm;unique=unique; ;name;\r\n', name=";name;") |
| 635 | test_entry('type=type;perm=perm;unique=unique; ;;;;\r\n', name=";;;;") |
| 636 | # case sensitiveness |
| 637 | set_data('Type=type;TyPe=perm;UNIQUE=unique; name\r\n') |
| 638 | _name, facts = next(self.client.mlsd()) |
Giampaolo Rodola' | a55efb3 | 2011-05-07 16:06:59 +0200 | [diff] [blame] | 639 | for x in facts: |
| 640 | self.assertTrue(x.islower()) |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 641 | # no data (directory empty) |
| 642 | set_data('') |
| 643 | self.assertRaises(StopIteration, next, self.client.mlsd()) |
| 644 | set_data('') |
| 645 | for x in self.client.mlsd(): |
| 646 | self.fail("unexpected data %s" % data) |
| 647 | |
Benjamin Peterson | 3a53fbb | 2008-09-27 22:04:16 +0000 | [diff] [blame] | 648 | def test_makeport(self): |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 649 | with self.client.makeport(): |
| 650 | # IPv4 is in use, just make sure send_eprt has not been used |
| 651 | self.assertEqual(self.server.handler_instance.last_received_cmd, |
| 652 | 'port') |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 653 | |
| 654 | def test_makepasv(self): |
| 655 | host, port = self.client.makepasv() |
Antoine Pitrou | d778e56 | 2010-10-14 20:35:26 +0000 | [diff] [blame] | 656 | conn = socket.create_connection((host, port), 10) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 657 | conn.close() |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 658 | # IPv4 is in use, just make sure send_epsv has not been used |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 659 | self.assertEqual(self.server.handler_instance.last_received_cmd, 'pasv') |
| 660 | |
| 661 | def test_with_statement(self): |
| 662 | self.client.quit() |
| 663 | |
| 664 | def is_client_connected(): |
| 665 | if self.client.sock is None: |
| 666 | return False |
| 667 | try: |
| 668 | self.client.sendcmd('noop') |
| 669 | except (socket.error, EOFError): |
| 670 | return False |
| 671 | return True |
| 672 | |
| 673 | # base test |
Giampaolo Rodola' | 842e567 | 2011-05-07 18:47:31 +0200 | [diff] [blame] | 674 | with ftplib.FTP(timeout=2) as self.client: |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 675 | self.client.connect(self.server.host, self.server.port) |
| 676 | self.client.sendcmd('noop') |
| 677 | self.assertTrue(is_client_connected()) |
| 678 | self.assertEqual(self.server.handler_instance.last_received_cmd, 'quit') |
| 679 | self.assertFalse(is_client_connected()) |
| 680 | |
| 681 | # QUIT sent inside the with block |
Giampaolo Rodola' | 842e567 | 2011-05-07 18:47:31 +0200 | [diff] [blame] | 682 | with ftplib.FTP(timeout=2) as self.client: |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 683 | self.client.connect(self.server.host, self.server.port) |
| 684 | self.client.sendcmd('noop') |
| 685 | self.client.quit() |
| 686 | self.assertEqual(self.server.handler_instance.last_received_cmd, 'quit') |
| 687 | self.assertFalse(is_client_connected()) |
| 688 | |
| 689 | # force a wrong response code to be sent on QUIT: error_perm |
| 690 | # is expected and the connection is supposed to be closed |
| 691 | try: |
Giampaolo Rodola' | 842e567 | 2011-05-07 18:47:31 +0200 | [diff] [blame] | 692 | with ftplib.FTP(timeout=2) as self.client: |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 693 | self.client.connect(self.server.host, self.server.port) |
| 694 | self.client.sendcmd('noop') |
| 695 | self.server.handler_instance.next_response = '550 error on quit' |
| 696 | except ftplib.error_perm as err: |
| 697 | self.assertEqual(str(err), '550 error on quit') |
| 698 | else: |
| 699 | self.fail('Exception not raised') |
| 700 | # needed to give the threaded server some time to set the attribute |
| 701 | # which otherwise would still be == 'noop' |
| 702 | time.sleep(0.1) |
| 703 | self.assertEqual(self.server.handler_instance.last_received_cmd, 'quit') |
| 704 | self.assertFalse(is_client_connected()) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 705 | |
Giampaolo Rodolà | 396ff06 | 2011-02-28 19:19:51 +0000 | [diff] [blame] | 706 | def test_source_address(self): |
| 707 | self.client.quit() |
| 708 | port = support.find_unused_port() |
Antoine Pitrou | 6dca527 | 2011-04-03 18:29:45 +0200 | [diff] [blame] | 709 | try: |
| 710 | self.client.connect(self.server.host, self.server.port, |
| 711 | source_address=(HOST, port)) |
| 712 | self.assertEqual(self.client.sock.getsockname()[1], port) |
| 713 | self.client.quit() |
| 714 | except IOError as e: |
| 715 | if e.errno == errno.EADDRINUSE: |
| 716 | self.skipTest("couldn't bind to port %d" % port) |
| 717 | raise |
Giampaolo Rodolà | 396ff06 | 2011-02-28 19:19:51 +0000 | [diff] [blame] | 718 | |
| 719 | def test_source_address_passive_connection(self): |
| 720 | port = support.find_unused_port() |
| 721 | self.client.source_address = (HOST, port) |
Antoine Pitrou | 6dca527 | 2011-04-03 18:29:45 +0200 | [diff] [blame] | 722 | try: |
| 723 | with self.client.transfercmd('list') as sock: |
| 724 | self.assertEqual(sock.getsockname()[1], port) |
| 725 | except IOError as e: |
| 726 | if e.errno == errno.EADDRINUSE: |
| 727 | self.skipTest("couldn't bind to port %d" % port) |
| 728 | raise |
Giampaolo Rodolà | 396ff06 | 2011-02-28 19:19:51 +0000 | [diff] [blame] | 729 | |
Giampaolo Rodolà | bbc4782 | 2010-08-23 22:10:32 +0000 | [diff] [blame] | 730 | def test_parse257(self): |
| 731 | self.assertEqual(ftplib.parse257('257 "/foo/bar"'), '/foo/bar') |
| 732 | self.assertEqual(ftplib.parse257('257 "/foo/bar" created'), '/foo/bar') |
| 733 | self.assertEqual(ftplib.parse257('257 ""'), '') |
| 734 | self.assertEqual(ftplib.parse257('257 "" created'), '') |
| 735 | self.assertRaises(ftplib.error_reply, ftplib.parse257, '250 "/foo/bar"') |
| 736 | # The 257 response is supposed to include the directory |
| 737 | # name and in case it contains embedded double-quotes |
| 738 | # they must be doubled (see RFC-959, chapter 7, appendix 2). |
| 739 | self.assertEqual(ftplib.parse257('257 "/foo/b""ar"'), '/foo/b"ar') |
| 740 | self.assertEqual(ftplib.parse257('257 "/foo/b""ar" created'), '/foo/b"ar') |
| 741 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 742 | |
| 743 | class TestIPv6Environment(TestCase): |
| 744 | |
| 745 | def setUp(self): |
Victor Stinner | c90e19d | 2011-05-01 01:23:03 +0200 | [diff] [blame] | 746 | self.server = DummyFTPServer(('::1', 0), af=socket.AF_INET6) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 747 | self.server.start() |
| 748 | self.client = ftplib.FTP() |
| 749 | self.client.connect(self.server.host, self.server.port) |
| 750 | |
| 751 | def tearDown(self): |
| 752 | self.client.close() |
| 753 | self.server.stop() |
| 754 | |
| 755 | def test_af(self): |
| 756 | self.assertEqual(self.client.af, socket.AF_INET6) |
| 757 | |
| 758 | def test_makeport(self): |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 759 | with self.client.makeport(): |
| 760 | self.assertEqual(self.server.handler_instance.last_received_cmd, |
| 761 | 'eprt') |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 762 | |
| 763 | def test_makepasv(self): |
| 764 | host, port = self.client.makepasv() |
Antoine Pitrou | d778e56 | 2010-10-14 20:35:26 +0000 | [diff] [blame] | 765 | conn = socket.create_connection((host, port), 10) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 766 | conn.close() |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 767 | self.assertEqual(self.server.handler_instance.last_received_cmd, 'epsv') |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 768 | |
| 769 | def test_transfer(self): |
| 770 | def retr(): |
| 771 | def callback(data): |
| 772 | received.append(data.decode('ascii')) |
| 773 | received = [] |
| 774 | self.client.retrbinary('retr', callback) |
| 775 | self.assertEqual(''.join(received), RETR_DATA) |
| 776 | self.client.set_pasv(True) |
| 777 | retr() |
| 778 | self.client.set_pasv(False) |
| 779 | retr() |
| 780 | |
| 781 | |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 782 | class TestTLS_FTPClassMixin(TestFTPClass): |
| 783 | """Repeat TestFTPClass tests starting the TLS layer for both control |
| 784 | and data connections first. |
| 785 | """ |
| 786 | |
| 787 | def setUp(self): |
| 788 | self.server = DummyTLS_FTPServer((HOST, 0)) |
| 789 | self.server.start() |
Giampaolo Rodola' | 842e567 | 2011-05-07 18:47:31 +0200 | [diff] [blame] | 790 | self.client = ftplib.FTP_TLS(timeout=2) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 791 | self.client.connect(self.server.host, self.server.port) |
| 792 | # enable TLS |
| 793 | self.client.auth() |
| 794 | self.client.prot_p() |
| 795 | |
| 796 | |
| 797 | class TestTLS_FTPClass(TestCase): |
| 798 | """Specific TLS_FTP class tests.""" |
| 799 | |
| 800 | def setUp(self): |
| 801 | self.server = DummyTLS_FTPServer((HOST, 0)) |
| 802 | self.server.start() |
Giampaolo Rodola' | 842e567 | 2011-05-07 18:47:31 +0200 | [diff] [blame] | 803 | self.client = ftplib.FTP_TLS(timeout=2) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 804 | self.client.connect(self.server.host, self.server.port) |
| 805 | |
| 806 | def tearDown(self): |
| 807 | self.client.close() |
| 808 | self.server.stop() |
| 809 | |
| 810 | def test_control_connection(self): |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 811 | self.assertNotIsInstance(self.client.sock, ssl.SSLSocket) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 812 | self.client.auth() |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 813 | self.assertIsInstance(self.client.sock, ssl.SSLSocket) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 814 | |
| 815 | def test_data_connection(self): |
| 816 | # clear text |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 817 | with self.client.transfercmd('list') as sock: |
| 818 | self.assertNotIsInstance(sock, ssl.SSLSocket) |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 819 | self.assertEqual(self.client.voidresp(), "226 transfer complete") |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 820 | |
| 821 | # secured, after PROT P |
| 822 | self.client.prot_p() |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 823 | with self.client.transfercmd('list') as sock: |
| 824 | self.assertIsInstance(sock, ssl.SSLSocket) |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 825 | self.assertEqual(self.client.voidresp(), "226 transfer complete") |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 826 | |
| 827 | # PROT C is issued, the connection must be in cleartext again |
| 828 | self.client.prot_c() |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 829 | with self.client.transfercmd('list') as sock: |
| 830 | self.assertNotIsInstance(sock, ssl.SSLSocket) |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 831 | self.assertEqual(self.client.voidresp(), "226 transfer complete") |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 832 | |
| 833 | def test_login(self): |
| 834 | # login() is supposed to implicitly secure the control connection |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 835 | self.assertNotIsInstance(self.client.sock, ssl.SSLSocket) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 836 | self.client.login() |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 837 | self.assertIsInstance(self.client.sock, ssl.SSLSocket) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 838 | # make sure that AUTH TLS doesn't get issued again |
| 839 | self.client.login() |
| 840 | |
| 841 | def test_auth_issued_twice(self): |
| 842 | self.client.auth() |
| 843 | self.assertRaises(ValueError, self.client.auth) |
| 844 | |
| 845 | def test_auth_ssl(self): |
| 846 | try: |
| 847 | self.client.ssl_version = ssl.PROTOCOL_SSLv3 |
| 848 | self.client.auth() |
| 849 | self.assertRaises(ValueError, self.client.auth) |
| 850 | finally: |
| 851 | self.client.ssl_version = ssl.PROTOCOL_TLSv1 |
| 852 | |
Giampaolo Rodolà | a67299e | 2010-05-26 18:06:04 +0000 | [diff] [blame] | 853 | def test_context(self): |
| 854 | self.client.quit() |
| 855 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 856 | self.assertRaises(ValueError, ftplib.FTP_TLS, keyfile=CERTFILE, |
| 857 | context=ctx) |
| 858 | self.assertRaises(ValueError, ftplib.FTP_TLS, certfile=CERTFILE, |
| 859 | context=ctx) |
| 860 | self.assertRaises(ValueError, ftplib.FTP_TLS, certfile=CERTFILE, |
| 861 | keyfile=CERTFILE, context=ctx) |
| 862 | |
Giampaolo Rodola' | 842e567 | 2011-05-07 18:47:31 +0200 | [diff] [blame] | 863 | self.client = ftplib.FTP_TLS(context=ctx, timeout=2) |
Giampaolo Rodolà | a67299e | 2010-05-26 18:06:04 +0000 | [diff] [blame] | 864 | self.client.connect(self.server.host, self.server.port) |
| 865 | self.assertNotIsInstance(self.client.sock, ssl.SSLSocket) |
| 866 | self.client.auth() |
| 867 | self.assertIs(self.client.sock.context, ctx) |
| 868 | self.assertIsInstance(self.client.sock, ssl.SSLSocket) |
| 869 | |
| 870 | self.client.prot_p() |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 871 | with self.client.transfercmd('list') as sock: |
| 872 | self.assertIs(sock.context, ctx) |
| 873 | self.assertIsInstance(sock, ssl.SSLSocket) |
Giampaolo Rodolà | a67299e | 2010-05-26 18:06:04 +0000 | [diff] [blame] | 874 | |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 875 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 876 | class TestTimeouts(TestCase): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 877 | |
| 878 | def setUp(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 879 | self.evt = threading.Event() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 880 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 881 | self.sock.settimeout(3) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 882 | self.port = support.bind_port(self.sock) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 883 | threading.Thread(target=self.server, args=(self.evt,self.sock)).start() |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 884 | # Wait for the server to be ready. |
| 885 | self.evt.wait() |
| 886 | self.evt.clear() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 887 | ftplib.FTP.port = self.port |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 888 | |
| 889 | def tearDown(self): |
| 890 | self.evt.wait() |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 891 | self.sock.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 892 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 893 | def server(self, evt, serv): |
| 894 | # This method sets the evt 3 times: |
| 895 | # 1) when the connection is ready to be accepted. |
| 896 | # 2) when it is safe for the caller to close the connection |
| 897 | # 3) when we have closed the socket |
| 898 | serv.listen(5) |
| 899 | # (1) Signal the caller that we are ready to accept the connection. |
| 900 | evt.set() |
| 901 | try: |
| 902 | conn, addr = serv.accept() |
| 903 | except socket.timeout: |
| 904 | pass |
| 905 | else: |
| 906 | conn.send(b"1 Hola mundo\n") |
| 907 | # (2) Signal the caller that it is safe to close the socket. |
| 908 | evt.set() |
| 909 | conn.close() |
| 910 | finally: |
| 911 | serv.close() |
| 912 | # (3) Signal the caller that we are done. |
| 913 | evt.set() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 914 | |
| 915 | def testTimeoutDefault(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 916 | # default -- use global socket timeout |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 917 | self.assertTrue(socket.getdefaulttimeout() is None) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 918 | socket.setdefaulttimeout(30) |
| 919 | try: |
| 920 | ftp = ftplib.FTP("localhost") |
| 921 | finally: |
| 922 | socket.setdefaulttimeout(None) |
| 923 | self.assertEqual(ftp.sock.gettimeout(), 30) |
| 924 | self.evt.wait() |
| 925 | ftp.close() |
| 926 | |
| 927 | def testTimeoutNone(self): |
| 928 | # no timeout -- do not use global socket timeout |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 929 | self.assertTrue(socket.getdefaulttimeout() is None) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 930 | socket.setdefaulttimeout(30) |
| 931 | try: |
| 932 | ftp = ftplib.FTP("localhost", timeout=None) |
| 933 | finally: |
| 934 | socket.setdefaulttimeout(None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 935 | self.assertTrue(ftp.sock.gettimeout() is None) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 936 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 937 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 938 | |
| 939 | def testTimeoutValue(self): |
| 940 | # a value |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 941 | ftp = ftplib.FTP(HOST, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 942 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 943 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 944 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 945 | |
| 946 | def testTimeoutConnect(self): |
| 947 | ftp = ftplib.FTP() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 948 | ftp.connect(HOST, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 949 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 950 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 951 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 952 | |
| 953 | def testTimeoutDifferentOrder(self): |
| 954 | ftp = ftplib.FTP(timeout=30) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 955 | ftp.connect(HOST) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 956 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 957 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 958 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 959 | |
| 960 | def testTimeoutDirectAccess(self): |
| 961 | ftp = ftplib.FTP() |
| 962 | ftp.timeout = 30 |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 963 | ftp.connect(HOST) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 964 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 965 | self.evt.wait() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 966 | ftp.close() |
| 967 | |
| 968 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 969 | def test_main(): |
| 970 | tests = [TestFTPClass, TestTimeouts] |
Antoine Pitrou | 9c39f3c | 2011-04-28 19:18:10 +0200 | [diff] [blame] | 971 | if support.IPV6_ENABLED: |
Victor Stinner | c90e19d | 2011-05-01 01:23:03 +0200 | [diff] [blame] | 972 | tests.append(TestIPv6Environment) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 973 | |
| 974 | if ssl is not None: |
| 975 | tests.extend([TestTLS_FTPClassMixin, TestTLS_FTPClass]) |
| 976 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 977 | thread_info = support.threading_setup() |
| 978 | try: |
| 979 | support.run_unittest(*tests) |
| 980 | finally: |
| 981 | support.threading_cleanup(*thread_info) |
| 982 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 983 | |
| 984 | if __name__ == '__main__': |
| 985 | test_main() |