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 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 19 | from unittest import TestCase, skipUnless |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 20 | from test import support |
Antoine Pitrou | f6fbf56 | 2013-08-22 00:39:46 +0200 | [diff] [blame] | 21 | from test.support import HOST, HOSTv6 |
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 | |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 24 | TIMEOUT = 3 |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 25 | # the dummy data returned by server over the data channel when |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 26 | # RETR, LIST, NLST, MLSD commands are issued |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 27 | RETR_DATA = 'abcde12345\r\n' * 1000 |
| 28 | LIST_DATA = 'foo\r\nbar\r\n' |
| 29 | NLST_DATA = 'foo\r\nbar\r\n' |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 30 | MLSD_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 Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 45 | |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 46 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 47 | class DummyDTPHandler(asynchat.async_chat): |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 48 | dtp_conn_closed = False |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 49 | |
| 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à | f96482e | 2010-08-04 10:36:18 +0000 | [diff] [blame] | 56 | self.baseclass.last_received_data += self.recv(1024).decode('ascii') |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 57 | |
| 58 | def handle_close(self): |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 59 | # 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 Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 66 | |
| 67 | def push(self, what): |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 68 | 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à | f96482e | 2010-08-04 10:36:18 +0000 | [diff] [blame] | 73 | super(DummyDTPHandler, self).push(what.encode('ascii')) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 74 | |
Giampaolo Rodolà | d930b63 | 2010-05-06 20:21:57 +0000 | [diff] [blame] | 75 | def handle_error(self): |
| 76 | raise |
| 77 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 78 | |
| 79 | class DummyFTPHandler(asynchat.async_chat): |
| 80 | |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 81 | dtp_handler = DummyDTPHandler |
| 82 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 83 | def __init__(self, conn): |
| 84 | asynchat.async_chat.__init__(self, conn) |
Giampaolo Rodola' | 0b5c21f | 2011-05-07 19:03:47 +0200 | [diff] [blame] | 85 | # tells the socket to handle urgent data inline (ABOR command) |
| 86 | self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_OOBINLINE, 1) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 87 | 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' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 93 | self.next_data = None |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 94 | self.rest = None |
Serhiy Storchaka | c30b178 | 2013-10-20 16:58:27 +0300 | [diff] [blame] | 95 | self.next_retr_data = RETR_DATA |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 96 | 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): |
| 121 | raise |
| 122 | |
| 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' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 130 | s = socket.create_connection((ip, port), timeout=TIMEOUT) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 131 | self.dtp = self.dtp_handler(s, baseclass=self) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 132 | self.push('200 active data connection established') |
| 133 | |
| 134 | def cmd_pasv(self, arg): |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 135 | with socket.socket() as sock: |
| 136 | sock.bind((self.socket.getsockname()[0], 0)) |
| 137 | sock.listen(5) |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 138 | sock.settimeout(TIMEOUT) |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 139 | 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 Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 144 | |
| 145 | def cmd_eprt(self, arg): |
| 146 | af, ip, port = arg.split(arg[0])[1:-1] |
| 147 | port = int(port) |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 148 | s = socket.create_connection((ip, port), timeout=TIMEOUT) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 149 | self.dtp = self.dtp_handler(s, baseclass=self) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 150 | self.push('200 active data connection established') |
| 151 | |
| 152 | def cmd_epsv(self, arg): |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 153 | with socket.socket(socket.AF_INET6) as sock: |
| 154 | sock.bind((self.socket.getsockname()[0], 0)) |
| 155 | sock.listen(5) |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 156 | sock.settimeout(TIMEOUT) |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 157 | 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 Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 161 | |
| 162 | def cmd_echo(self, arg): |
| 163 | # sends back the received string (used by the test suite) |
| 164 | self.push(arg) |
| 165 | |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 166 | def cmd_noop(self, arg): |
| 167 | self.push('200 noop ok') |
| 168 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 169 | 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à | f96482e | 2010-08-04 10:36:18 +0000 | [diff] [blame] | 203 | self.push('200 type ok') |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 204 | |
| 205 | def cmd_quit(self, arg): |
| 206 | self.push('221 quit ok') |
| 207 | self.close() |
| 208 | |
Giampaolo Rodola' | 0b5c21f | 2011-05-07 19:03:47 +0200 | [diff] [blame] | 209 | def cmd_abor(self, arg): |
| 210 | self.push('226 abor ok') |
| 211 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 212 | def cmd_stor(self, arg): |
| 213 | self.push('125 stor ok') |
| 214 | |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 215 | def cmd_rest(self, arg): |
| 216 | self.rest = arg |
| 217 | self.push('350 rest ok') |
| 218 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 219 | def cmd_retr(self, arg): |
| 220 | self.push('125 retr ok') |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 221 | if self.rest is not None: |
| 222 | offset = int(self.rest) |
| 223 | else: |
| 224 | offset = 0 |
Serhiy Storchaka | c30b178 | 2013-10-20 16:58:27 +0300 | [diff] [blame] | 225 | self.dtp.push(self.next_retr_data[offset:]) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 226 | self.dtp.close_when_done() |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 227 | self.rest = None |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 228 | |
| 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' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 239 | 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 Storchaka | c30b178 | 2013-10-20 16:58:27 +0300 | [diff] [blame] | 247 | 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 Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 252 | |
| 253 | class 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à | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 266 | self.handler_instance = None |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 267 | |
| 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à | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 288 | def handle_accepted(self, conn, addr): |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 289 | self.handler_instance = self.handler(conn) |
Benjamin Peterson | d06e3b0 | 2008-09-28 21:00:42 +0000 | [diff] [blame] | 290 | |
| 291 | def handle_connect(self): |
| 292 | self.close() |
| 293 | handle_read = handle_connect |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 294 | |
| 295 | def writable(self): |
| 296 | return 0 |
| 297 | |
| 298 | def handle_error(self): |
| 299 | raise |
| 300 | |
| 301 | |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 302 | if ssl is not None: |
| 303 | |
Christian Heimes | e5b5edf | 2013-12-02 02:56:02 +0100 | [diff] [blame] | 304 | CERTFILE = os.path.join(os.path.dirname(__file__), "keycert3.pem") |
| 305 | CAFILE = os.path.join(os.path.dirname(__file__), "pycacert.pem") |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 306 | |
| 307 | class SSLConnection(asyncore.dispatcher): |
| 308 | """An asyncore.dispatcher subclass supporting TLS/SSL.""" |
| 309 | |
| 310 | _ssl_accepting = False |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 311 | _ssl_closing = False |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 312 | |
| 313 | def secure_connection(self): |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 314 | socket = ssl.wrap_socket(self.socket, suppress_ragged_eofs=False, |
| 315 | certfile=CERTFILE, server_side=True, |
| 316 | do_handshake_on_connect=False, |
| 317 | ssl_version=ssl.PROTOCOL_SSLv23) |
Giampaolo Rodola' | 096dcb1 | 2011-06-27 11:17:51 +0200 | [diff] [blame] | 318 | self.del_channel() |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 319 | self.set_socket(socket) |
| 320 | self._ssl_accepting = True |
| 321 | |
| 322 | def _do_ssl_handshake(self): |
| 323 | try: |
| 324 | self.socket.do_handshake() |
| 325 | except ssl.SSLError as err: |
| 326 | if err.args[0] in (ssl.SSL_ERROR_WANT_READ, |
| 327 | ssl.SSL_ERROR_WANT_WRITE): |
| 328 | return |
| 329 | elif err.args[0] == ssl.SSL_ERROR_EOF: |
| 330 | return self.handle_close() |
| 331 | raise |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 332 | except OSError as err: |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 333 | if err.args[0] == errno.ECONNABORTED: |
| 334 | return self.handle_close() |
| 335 | else: |
| 336 | self._ssl_accepting = False |
| 337 | |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 338 | def _do_ssl_shutdown(self): |
| 339 | self._ssl_closing = True |
| 340 | try: |
| 341 | self.socket = self.socket.unwrap() |
| 342 | except ssl.SSLError as err: |
| 343 | if err.args[0] in (ssl.SSL_ERROR_WANT_READ, |
| 344 | ssl.SSL_ERROR_WANT_WRITE): |
| 345 | return |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 346 | except OSError as err: |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 347 | # Any "socket error" corresponds to a SSL_ERROR_SYSCALL return |
| 348 | # from OpenSSL's SSL_shutdown(), corresponding to a |
| 349 | # closed socket condition. See also: |
| 350 | # http://www.mail-archive.com/openssl-users@openssl.org/msg60710.html |
| 351 | pass |
| 352 | self._ssl_closing = False |
Benjamin Peterson | b29614e | 2012-10-09 11:16:03 -0400 | [diff] [blame] | 353 | if getattr(self, '_ccc', False) is False: |
Giampaolo Rodola' | 096dcb1 | 2011-06-27 11:17:51 +0200 | [diff] [blame] | 354 | super(SSLConnection, self).close() |
| 355 | else: |
| 356 | pass |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 357 | |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 358 | def handle_read_event(self): |
| 359 | if self._ssl_accepting: |
| 360 | self._do_ssl_handshake() |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 361 | elif self._ssl_closing: |
| 362 | self._do_ssl_shutdown() |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 363 | else: |
| 364 | super(SSLConnection, self).handle_read_event() |
| 365 | |
| 366 | def handle_write_event(self): |
| 367 | if self._ssl_accepting: |
| 368 | self._do_ssl_handshake() |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 369 | elif self._ssl_closing: |
| 370 | self._do_ssl_shutdown() |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 371 | else: |
| 372 | super(SSLConnection, self).handle_write_event() |
| 373 | |
| 374 | def send(self, data): |
| 375 | try: |
| 376 | return super(SSLConnection, self).send(data) |
| 377 | except ssl.SSLError as err: |
Antoine Pitrou | 5733c08 | 2010-03-22 14:49:10 +0000 | [diff] [blame] | 378 | if err.args[0] in (ssl.SSL_ERROR_EOF, ssl.SSL_ERROR_ZERO_RETURN, |
| 379 | ssl.SSL_ERROR_WANT_READ, |
| 380 | ssl.SSL_ERROR_WANT_WRITE): |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 381 | return 0 |
| 382 | raise |
| 383 | |
| 384 | def recv(self, buffer_size): |
| 385 | try: |
| 386 | return super(SSLConnection, self).recv(buffer_size) |
| 387 | except ssl.SSLError as err: |
Antoine Pitrou | 5733c08 | 2010-03-22 14:49:10 +0000 | [diff] [blame] | 388 | if err.args[0] in (ssl.SSL_ERROR_WANT_READ, |
| 389 | ssl.SSL_ERROR_WANT_WRITE): |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 390 | return b'' |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 391 | if err.args[0] in (ssl.SSL_ERROR_EOF, ssl.SSL_ERROR_ZERO_RETURN): |
| 392 | self.handle_close() |
| 393 | return b'' |
| 394 | raise |
| 395 | |
| 396 | def handle_error(self): |
| 397 | raise |
| 398 | |
| 399 | def close(self): |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 400 | if (isinstance(self.socket, ssl.SSLSocket) and |
| 401 | self.socket._sslobj is not None): |
| 402 | self._do_ssl_shutdown() |
Benjamin Peterson | 1bd93a7 | 2010-10-31 19:58:07 +0000 | [diff] [blame] | 403 | else: |
| 404 | super(SSLConnection, self).close() |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 405 | |
| 406 | |
| 407 | class DummyTLS_DTPHandler(SSLConnection, DummyDTPHandler): |
| 408 | """A DummyDTPHandler subclass supporting TLS/SSL.""" |
| 409 | |
| 410 | def __init__(self, conn, baseclass): |
| 411 | DummyDTPHandler.__init__(self, conn, baseclass) |
| 412 | if self.baseclass.secure_data_channel: |
| 413 | self.secure_connection() |
| 414 | |
| 415 | |
| 416 | class DummyTLS_FTPHandler(SSLConnection, DummyFTPHandler): |
| 417 | """A DummyFTPHandler subclass supporting TLS/SSL.""" |
| 418 | |
| 419 | dtp_handler = DummyTLS_DTPHandler |
| 420 | |
| 421 | def __init__(self, conn): |
| 422 | DummyFTPHandler.__init__(self, conn) |
| 423 | self.secure_data_channel = False |
Giampaolo Rodola' | 096dcb1 | 2011-06-27 11:17:51 +0200 | [diff] [blame] | 424 | self._ccc = False |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 425 | |
| 426 | def cmd_auth(self, line): |
| 427 | """Set up secure control channel.""" |
| 428 | self.push('234 AUTH TLS successful') |
| 429 | self.secure_connection() |
| 430 | |
Giampaolo Rodola' | 096dcb1 | 2011-06-27 11:17:51 +0200 | [diff] [blame] | 431 | def cmd_ccc(self, line): |
| 432 | self.push('220 Reverting back to clear-text') |
| 433 | self._ccc = True |
| 434 | self._do_ssl_shutdown() |
| 435 | |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 436 | def cmd_pbsz(self, line): |
| 437 | """Negotiate size of buffer for secure data transfer. |
| 438 | For TLS/SSL the only valid value for the parameter is '0'. |
| 439 | Any other value is accepted but ignored. |
| 440 | """ |
| 441 | self.push('200 PBSZ=0 successful.') |
| 442 | |
| 443 | def cmd_prot(self, line): |
| 444 | """Setup un/secure data channel.""" |
| 445 | arg = line.upper() |
| 446 | if arg == 'C': |
| 447 | self.push('200 Protection set to Clear') |
| 448 | self.secure_data_channel = False |
| 449 | elif arg == 'P': |
| 450 | self.push('200 Protection set to Private') |
| 451 | self.secure_data_channel = True |
| 452 | else: |
| 453 | self.push("502 Unrecognized PROT type (use C or P).") |
| 454 | |
| 455 | |
| 456 | class DummyTLS_FTPServer(DummyFTPServer): |
| 457 | handler = DummyTLS_FTPHandler |
| 458 | |
| 459 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 460 | class TestFTPClass(TestCase): |
| 461 | |
| 462 | def setUp(self): |
| 463 | self.server = DummyFTPServer((HOST, 0)) |
| 464 | self.server.start() |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 465 | self.client = ftplib.FTP(timeout=TIMEOUT) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 466 | self.client.connect(self.server.host, self.server.port) |
| 467 | |
| 468 | def tearDown(self): |
| 469 | self.client.close() |
| 470 | self.server.stop() |
| 471 | |
Giampaolo Rodola' | 8bc8585 | 2012-01-09 17:10:10 +0100 | [diff] [blame] | 472 | def check_data(self, received, expected): |
| 473 | self.assertEqual(len(received), len(expected)) |
| 474 | self.assertEqual(received, expected) |
| 475 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 476 | def test_getwelcome(self): |
| 477 | self.assertEqual(self.client.getwelcome(), '220 welcome') |
| 478 | |
| 479 | def test_sanitize(self): |
| 480 | self.assertEqual(self.client.sanitize('foo'), repr('foo')) |
| 481 | self.assertEqual(self.client.sanitize('pass 12345'), repr('pass *****')) |
| 482 | self.assertEqual(self.client.sanitize('PASS 12345'), repr('PASS *****')) |
| 483 | |
| 484 | def test_exceptions(self): |
| 485 | self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 400') |
| 486 | self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 499') |
| 487 | self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 500') |
| 488 | self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 599') |
| 489 | self.assertRaises(ftplib.error_proto, self.client.sendcmd, 'echo 999') |
| 490 | |
| 491 | def test_all_errors(self): |
| 492 | exceptions = (ftplib.error_reply, ftplib.error_temp, ftplib.error_perm, |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 493 | ftplib.error_proto, ftplib.Error, OSError, EOFError) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 494 | for x in exceptions: |
| 495 | try: |
| 496 | raise x('exception not included in all_errors set') |
| 497 | except ftplib.all_errors: |
| 498 | pass |
| 499 | |
| 500 | def test_set_pasv(self): |
| 501 | # passive mode is supposed to be enabled by default |
| 502 | self.assertTrue(self.client.passiveserver) |
| 503 | self.client.set_pasv(True) |
| 504 | self.assertTrue(self.client.passiveserver) |
| 505 | self.client.set_pasv(False) |
| 506 | self.assertFalse(self.client.passiveserver) |
| 507 | |
| 508 | def test_voidcmd(self): |
| 509 | self.client.voidcmd('echo 200') |
| 510 | self.client.voidcmd('echo 299') |
| 511 | self.assertRaises(ftplib.error_reply, self.client.voidcmd, 'echo 199') |
| 512 | self.assertRaises(ftplib.error_reply, self.client.voidcmd, 'echo 300') |
| 513 | |
| 514 | def test_login(self): |
| 515 | self.client.login() |
| 516 | |
| 517 | def test_acct(self): |
| 518 | self.client.acct('passwd') |
| 519 | |
| 520 | def test_rename(self): |
| 521 | self.client.rename('a', 'b') |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 522 | self.server.handler_instance.next_response = '200' |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 523 | self.assertRaises(ftplib.error_reply, self.client.rename, 'a', 'b') |
| 524 | |
| 525 | def test_delete(self): |
| 526 | self.client.delete('foo') |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 527 | self.server.handler_instance.next_response = '199' |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 528 | self.assertRaises(ftplib.error_reply, self.client.delete, 'foo') |
| 529 | |
| 530 | def test_size(self): |
| 531 | self.client.size('foo') |
| 532 | |
| 533 | def test_mkd(self): |
| 534 | dir = self.client.mkd('/foo') |
| 535 | self.assertEqual(dir, '/foo') |
| 536 | |
| 537 | def test_rmd(self): |
| 538 | self.client.rmd('foo') |
| 539 | |
Senthil Kumaran | 0d53860 | 2013-08-12 22:25:27 -0700 | [diff] [blame] | 540 | def test_cwd(self): |
| 541 | dir = self.client.cwd('/foo') |
| 542 | self.assertEqual(dir, '250 cwd ok') |
| 543 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 544 | def test_pwd(self): |
| 545 | dir = self.client.pwd() |
| 546 | self.assertEqual(dir, 'pwd ok') |
| 547 | |
| 548 | def test_quit(self): |
| 549 | self.assertEqual(self.client.quit(), '221 quit ok') |
| 550 | # Ensure the connection gets closed; sock attribute should be None |
| 551 | self.assertEqual(self.client.sock, None) |
| 552 | |
Giampaolo Rodola' | 0b5c21f | 2011-05-07 19:03:47 +0200 | [diff] [blame] | 553 | def test_abort(self): |
| 554 | self.client.abort() |
| 555 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 556 | def test_retrbinary(self): |
| 557 | def callback(data): |
| 558 | received.append(data.decode('ascii')) |
| 559 | received = [] |
| 560 | self.client.retrbinary('retr', callback) |
Giampaolo Rodola' | 8bc8585 | 2012-01-09 17:10:10 +0100 | [diff] [blame] | 561 | self.check_data(''.join(received), RETR_DATA) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 562 | |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 563 | def test_retrbinary_rest(self): |
| 564 | def callback(data): |
| 565 | received.append(data.decode('ascii')) |
| 566 | for rest in (0, 10, 20): |
| 567 | received = [] |
| 568 | self.client.retrbinary('retr', callback, rest=rest) |
Giampaolo Rodola' | 8bc8585 | 2012-01-09 17:10:10 +0100 | [diff] [blame] | 569 | self.check_data(''.join(received), RETR_DATA[rest:]) |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 570 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 571 | def test_retrlines(self): |
| 572 | received = [] |
| 573 | self.client.retrlines('retr', received.append) |
Giampaolo Rodola' | 8bc8585 | 2012-01-09 17:10:10 +0100 | [diff] [blame] | 574 | self.check_data(''.join(received), RETR_DATA.replace('\r\n', '')) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 575 | |
| 576 | def test_storbinary(self): |
| 577 | f = io.BytesIO(RETR_DATA.encode('ascii')) |
| 578 | self.client.storbinary('stor', f) |
Giampaolo Rodola' | 8bc8585 | 2012-01-09 17:10:10 +0100 | [diff] [blame] | 579 | self.check_data(self.server.handler_instance.last_received_data, RETR_DATA) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 580 | # test new callback arg |
| 581 | flag = [] |
| 582 | f.seek(0) |
| 583 | self.client.storbinary('stor', f, callback=lambda x: flag.append(None)) |
| 584 | self.assertTrue(flag) |
| 585 | |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 586 | def test_storbinary_rest(self): |
| 587 | f = io.BytesIO(RETR_DATA.replace('\r\n', '\n').encode('ascii')) |
| 588 | for r in (30, '30'): |
| 589 | f.seek(0) |
| 590 | self.client.storbinary('stor', f, rest=r) |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 591 | self.assertEqual(self.server.handler_instance.rest, str(r)) |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 592 | |
Giampaolo Rodolà | f96482e | 2010-08-04 10:36:18 +0000 | [diff] [blame] | 593 | def test_storlines(self): |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 594 | f = io.BytesIO(RETR_DATA.replace('\r\n', '\n').encode('ascii')) |
| 595 | self.client.storlines('stor', f) |
Giampaolo Rodola' | 8bc8585 | 2012-01-09 17:10:10 +0100 | [diff] [blame] | 596 | self.check_data(self.server.handler_instance.last_received_data, RETR_DATA) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 597 | # test new callback arg |
| 598 | flag = [] |
| 599 | f.seek(0) |
| 600 | self.client.storlines('stor foo', f, callback=lambda x: flag.append(None)) |
| 601 | self.assertTrue(flag) |
| 602 | |
Victor Stinner | ed3a303 | 2013-04-02 22:13:27 +0200 | [diff] [blame] | 603 | f = io.StringIO(RETR_DATA.replace('\r\n', '\n')) |
| 604 | # storlines() expects a binary file, not a text file |
Florent Xicluna | 5f3fef3 | 2013-07-06 15:08:21 +0200 | [diff] [blame] | 605 | with support.check_warnings(('', BytesWarning), quiet=True): |
| 606 | self.assertRaises(TypeError, self.client.storlines, 'stor foo', f) |
Victor Stinner | ed3a303 | 2013-04-02 22:13:27 +0200 | [diff] [blame] | 607 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 608 | def test_nlst(self): |
| 609 | self.client.nlst() |
| 610 | self.assertEqual(self.client.nlst(), NLST_DATA.split('\r\n')[:-1]) |
| 611 | |
| 612 | def test_dir(self): |
| 613 | l = [] |
| 614 | self.client.dir(lambda x: l.append(x)) |
| 615 | self.assertEqual(''.join(l), LIST_DATA.replace('\r\n', '')) |
| 616 | |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 617 | def test_mlsd(self): |
| 618 | list(self.client.mlsd()) |
| 619 | list(self.client.mlsd(path='/')) |
| 620 | list(self.client.mlsd(path='/', facts=['size', 'type'])) |
| 621 | |
| 622 | ls = list(self.client.mlsd()) |
| 623 | for name, facts in ls: |
Giampaolo Rodola' | a55efb3 | 2011-05-07 16:06:59 +0200 | [diff] [blame] | 624 | self.assertIsInstance(name, str) |
| 625 | self.assertIsInstance(facts, dict) |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 626 | self.assertTrue(name) |
Giampaolo Rodola' | a55efb3 | 2011-05-07 16:06:59 +0200 | [diff] [blame] | 627 | self.assertIn('type', facts) |
| 628 | self.assertIn('perm', facts) |
| 629 | self.assertIn('unique', facts) |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 630 | |
| 631 | def set_data(data): |
| 632 | self.server.handler_instance.next_data = data |
| 633 | |
| 634 | def test_entry(line, type=None, perm=None, unique=None, name=None): |
| 635 | type = 'type' if type is None else type |
| 636 | perm = 'perm' if perm is None else perm |
| 637 | unique = 'unique' if unique is None else unique |
| 638 | name = 'name' if name is None else name |
| 639 | set_data(line) |
| 640 | _name, facts = next(self.client.mlsd()) |
| 641 | self.assertEqual(_name, name) |
| 642 | self.assertEqual(facts['type'], type) |
| 643 | self.assertEqual(facts['perm'], perm) |
| 644 | self.assertEqual(facts['unique'], unique) |
| 645 | |
| 646 | # plain |
| 647 | test_entry('type=type;perm=perm;unique=unique; name\r\n') |
| 648 | # "=" in fact value |
| 649 | test_entry('type=ty=pe;perm=perm;unique=unique; name\r\n', type="ty=pe") |
| 650 | test_entry('type==type;perm=perm;unique=unique; name\r\n', type="=type") |
| 651 | test_entry('type=t=y=pe;perm=perm;unique=unique; name\r\n', type="t=y=pe") |
| 652 | test_entry('type=====;perm=perm;unique=unique; name\r\n', type="====") |
| 653 | # spaces in name |
| 654 | test_entry('type=type;perm=perm;unique=unique; na me\r\n', name="na me") |
| 655 | test_entry('type=type;perm=perm;unique=unique; name \r\n', name="name ") |
| 656 | test_entry('type=type;perm=perm;unique=unique; name\r\n', name=" name") |
| 657 | test_entry('type=type;perm=perm;unique=unique; n am e\r\n', name="n am e") |
| 658 | # ";" in name |
| 659 | test_entry('type=type;perm=perm;unique=unique; na;me\r\n', name="na;me") |
| 660 | test_entry('type=type;perm=perm;unique=unique; ;name\r\n', name=";name") |
| 661 | test_entry('type=type;perm=perm;unique=unique; ;name;\r\n', name=";name;") |
| 662 | test_entry('type=type;perm=perm;unique=unique; ;;;;\r\n', name=";;;;") |
| 663 | # case sensitiveness |
| 664 | set_data('Type=type;TyPe=perm;UNIQUE=unique; name\r\n') |
| 665 | _name, facts = next(self.client.mlsd()) |
Giampaolo Rodola' | a55efb3 | 2011-05-07 16:06:59 +0200 | [diff] [blame] | 666 | for x in facts: |
| 667 | self.assertTrue(x.islower()) |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 668 | # no data (directory empty) |
| 669 | set_data('') |
| 670 | self.assertRaises(StopIteration, next, self.client.mlsd()) |
| 671 | set_data('') |
| 672 | for x in self.client.mlsd(): |
| 673 | self.fail("unexpected data %s" % data) |
| 674 | |
Benjamin Peterson | 3a53fbb | 2008-09-27 22:04:16 +0000 | [diff] [blame] | 675 | def test_makeport(self): |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 676 | with self.client.makeport(): |
| 677 | # IPv4 is in use, just make sure send_eprt has not been used |
| 678 | self.assertEqual(self.server.handler_instance.last_received_cmd, |
| 679 | 'port') |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 680 | |
| 681 | def test_makepasv(self): |
| 682 | host, port = self.client.makepasv() |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 683 | conn = socket.create_connection((host, port), timeout=TIMEOUT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 684 | conn.close() |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 685 | # 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] | 686 | self.assertEqual(self.server.handler_instance.last_received_cmd, 'pasv') |
| 687 | |
| 688 | def test_with_statement(self): |
| 689 | self.client.quit() |
| 690 | |
| 691 | def is_client_connected(): |
| 692 | if self.client.sock is None: |
| 693 | return False |
| 694 | try: |
| 695 | self.client.sendcmd('noop') |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 696 | except (OSError, EOFError): |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 697 | return False |
| 698 | return True |
| 699 | |
| 700 | # base test |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 701 | with ftplib.FTP(timeout=TIMEOUT) as self.client: |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 702 | self.client.connect(self.server.host, self.server.port) |
| 703 | self.client.sendcmd('noop') |
| 704 | self.assertTrue(is_client_connected()) |
| 705 | self.assertEqual(self.server.handler_instance.last_received_cmd, 'quit') |
| 706 | self.assertFalse(is_client_connected()) |
| 707 | |
| 708 | # QUIT sent inside the with block |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 709 | with ftplib.FTP(timeout=TIMEOUT) as self.client: |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 710 | self.client.connect(self.server.host, self.server.port) |
| 711 | self.client.sendcmd('noop') |
| 712 | self.client.quit() |
| 713 | self.assertEqual(self.server.handler_instance.last_received_cmd, 'quit') |
| 714 | self.assertFalse(is_client_connected()) |
| 715 | |
| 716 | # force a wrong response code to be sent on QUIT: error_perm |
| 717 | # is expected and the connection is supposed to be closed |
| 718 | try: |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 719 | with ftplib.FTP(timeout=TIMEOUT) as self.client: |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 720 | self.client.connect(self.server.host, self.server.port) |
| 721 | self.client.sendcmd('noop') |
| 722 | self.server.handler_instance.next_response = '550 error on quit' |
| 723 | except ftplib.error_perm as err: |
| 724 | self.assertEqual(str(err), '550 error on quit') |
| 725 | else: |
| 726 | self.fail('Exception not raised') |
| 727 | # needed to give the threaded server some time to set the attribute |
| 728 | # which otherwise would still be == 'noop' |
| 729 | time.sleep(0.1) |
| 730 | self.assertEqual(self.server.handler_instance.last_received_cmd, 'quit') |
| 731 | self.assertFalse(is_client_connected()) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 732 | |
Giampaolo Rodolà | 396ff06 | 2011-02-28 19:19:51 +0000 | [diff] [blame] | 733 | def test_source_address(self): |
| 734 | self.client.quit() |
| 735 | port = support.find_unused_port() |
Antoine Pitrou | 6dca527 | 2011-04-03 18:29:45 +0200 | [diff] [blame] | 736 | try: |
| 737 | self.client.connect(self.server.host, self.server.port, |
| 738 | source_address=(HOST, port)) |
| 739 | self.assertEqual(self.client.sock.getsockname()[1], port) |
| 740 | self.client.quit() |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 741 | except OSError as e: |
Antoine Pitrou | 6dca527 | 2011-04-03 18:29:45 +0200 | [diff] [blame] | 742 | if e.errno == errno.EADDRINUSE: |
| 743 | self.skipTest("couldn't bind to port %d" % port) |
| 744 | raise |
Giampaolo Rodolà | 396ff06 | 2011-02-28 19:19:51 +0000 | [diff] [blame] | 745 | |
| 746 | def test_source_address_passive_connection(self): |
| 747 | port = support.find_unused_port() |
| 748 | self.client.source_address = (HOST, port) |
Antoine Pitrou | 6dca527 | 2011-04-03 18:29:45 +0200 | [diff] [blame] | 749 | try: |
| 750 | with self.client.transfercmd('list') as sock: |
| 751 | self.assertEqual(sock.getsockname()[1], port) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 752 | except OSError as e: |
Antoine Pitrou | 6dca527 | 2011-04-03 18:29:45 +0200 | [diff] [blame] | 753 | if e.errno == errno.EADDRINUSE: |
| 754 | self.skipTest("couldn't bind to port %d" % port) |
| 755 | raise |
Giampaolo Rodolà | 396ff06 | 2011-02-28 19:19:51 +0000 | [diff] [blame] | 756 | |
Giampaolo Rodolà | bbc4782 | 2010-08-23 22:10:32 +0000 | [diff] [blame] | 757 | def test_parse257(self): |
| 758 | self.assertEqual(ftplib.parse257('257 "/foo/bar"'), '/foo/bar') |
| 759 | self.assertEqual(ftplib.parse257('257 "/foo/bar" created'), '/foo/bar') |
| 760 | self.assertEqual(ftplib.parse257('257 ""'), '') |
| 761 | self.assertEqual(ftplib.parse257('257 "" created'), '') |
| 762 | self.assertRaises(ftplib.error_reply, ftplib.parse257, '250 "/foo/bar"') |
| 763 | # The 257 response is supposed to include the directory |
| 764 | # name and in case it contains embedded double-quotes |
| 765 | # they must be doubled (see RFC-959, chapter 7, appendix 2). |
| 766 | self.assertEqual(ftplib.parse257('257 "/foo/b""ar"'), '/foo/b"ar') |
| 767 | self.assertEqual(ftplib.parse257('257 "/foo/b""ar" created'), '/foo/b"ar') |
| 768 | |
Serhiy Storchaka | c30b178 | 2013-10-20 16:58:27 +0300 | [diff] [blame] | 769 | def test_line_too_long(self): |
| 770 | self.assertRaises(ftplib.Error, self.client.sendcmd, |
| 771 | 'x' * self.client.maxline * 2) |
| 772 | |
| 773 | def test_retrlines_too_long(self): |
| 774 | self.client.sendcmd('SETLONGRETR %d' % (self.client.maxline * 2)) |
| 775 | received = [] |
| 776 | self.assertRaises(ftplib.Error, |
| 777 | self.client.retrlines, 'retr', received.append) |
| 778 | |
| 779 | def test_storlines_too_long(self): |
| 780 | f = io.BytesIO(b'x' * self.client.maxline * 2) |
| 781 | self.assertRaises(ftplib.Error, self.client.storlines, 'stor', f) |
| 782 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 783 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 784 | @skipUnless(support.IPV6_ENABLED, "IPv6 not enabled") |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 785 | class TestIPv6Environment(TestCase): |
| 786 | |
| 787 | def setUp(self): |
Antoine Pitrou | f6fbf56 | 2013-08-22 00:39:46 +0200 | [diff] [blame] | 788 | self.server = DummyFTPServer((HOSTv6, 0), af=socket.AF_INET6) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 789 | self.server.start() |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 790 | self.client = ftplib.FTP(timeout=TIMEOUT) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 791 | self.client.connect(self.server.host, self.server.port) |
| 792 | |
| 793 | def tearDown(self): |
| 794 | self.client.close() |
| 795 | self.server.stop() |
| 796 | |
| 797 | def test_af(self): |
| 798 | self.assertEqual(self.client.af, socket.AF_INET6) |
| 799 | |
| 800 | def test_makeport(self): |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 801 | with self.client.makeport(): |
| 802 | self.assertEqual(self.server.handler_instance.last_received_cmd, |
| 803 | 'eprt') |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 804 | |
| 805 | def test_makepasv(self): |
| 806 | host, port = self.client.makepasv() |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 807 | conn = socket.create_connection((host, port), timeout=TIMEOUT) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 808 | conn.close() |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 809 | self.assertEqual(self.server.handler_instance.last_received_cmd, 'epsv') |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 810 | |
| 811 | def test_transfer(self): |
| 812 | def retr(): |
| 813 | def callback(data): |
| 814 | received.append(data.decode('ascii')) |
| 815 | received = [] |
| 816 | self.client.retrbinary('retr', callback) |
Giampaolo Rodola' | 8bc8585 | 2012-01-09 17:10:10 +0100 | [diff] [blame] | 817 | self.assertEqual(len(''.join(received)), len(RETR_DATA)) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 818 | self.assertEqual(''.join(received), RETR_DATA) |
| 819 | self.client.set_pasv(True) |
| 820 | retr() |
| 821 | self.client.set_pasv(False) |
| 822 | retr() |
| 823 | |
| 824 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 825 | @skipUnless(ssl, "SSL not available") |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 826 | class TestTLS_FTPClassMixin(TestFTPClass): |
| 827 | """Repeat TestFTPClass tests starting the TLS layer for both control |
| 828 | and data connections first. |
| 829 | """ |
| 830 | |
| 831 | def setUp(self): |
| 832 | self.server = DummyTLS_FTPServer((HOST, 0)) |
| 833 | self.server.start() |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 834 | self.client = ftplib.FTP_TLS(timeout=TIMEOUT) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 835 | self.client.connect(self.server.host, self.server.port) |
| 836 | # enable TLS |
| 837 | self.client.auth() |
| 838 | self.client.prot_p() |
| 839 | |
| 840 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 841 | @skipUnless(ssl, "SSL not available") |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 842 | class TestTLS_FTPClass(TestCase): |
| 843 | """Specific TLS_FTP class tests.""" |
| 844 | |
| 845 | def setUp(self): |
| 846 | self.server = DummyTLS_FTPServer((HOST, 0)) |
| 847 | self.server.start() |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 848 | self.client = ftplib.FTP_TLS(timeout=TIMEOUT) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 849 | self.client.connect(self.server.host, self.server.port) |
| 850 | |
| 851 | def tearDown(self): |
| 852 | self.client.close() |
| 853 | self.server.stop() |
| 854 | |
| 855 | def test_control_connection(self): |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 856 | self.assertNotIsInstance(self.client.sock, ssl.SSLSocket) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 857 | self.client.auth() |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 858 | self.assertIsInstance(self.client.sock, ssl.SSLSocket) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 859 | |
| 860 | def test_data_connection(self): |
| 861 | # clear text |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 862 | with self.client.transfercmd('list') as sock: |
| 863 | self.assertNotIsInstance(sock, ssl.SSLSocket) |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 864 | self.assertEqual(self.client.voidresp(), "226 transfer complete") |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 865 | |
| 866 | # secured, after PROT P |
| 867 | self.client.prot_p() |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 868 | with self.client.transfercmd('list') as sock: |
| 869 | self.assertIsInstance(sock, ssl.SSLSocket) |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 870 | self.assertEqual(self.client.voidresp(), "226 transfer complete") |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 871 | |
| 872 | # PROT C is issued, the connection must be in cleartext again |
| 873 | self.client.prot_c() |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 874 | with self.client.transfercmd('list') as sock: |
| 875 | self.assertNotIsInstance(sock, ssl.SSLSocket) |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 876 | self.assertEqual(self.client.voidresp(), "226 transfer complete") |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 877 | |
| 878 | def test_login(self): |
| 879 | # login() is supposed to implicitly secure the control connection |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 880 | self.assertNotIsInstance(self.client.sock, ssl.SSLSocket) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 881 | self.client.login() |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 882 | self.assertIsInstance(self.client.sock, ssl.SSLSocket) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 883 | # make sure that AUTH TLS doesn't get issued again |
| 884 | self.client.login() |
| 885 | |
| 886 | def test_auth_issued_twice(self): |
| 887 | self.client.auth() |
| 888 | self.assertRaises(ValueError, self.client.auth) |
| 889 | |
| 890 | def test_auth_ssl(self): |
| 891 | try: |
Benjamin Peterson | e32467c | 2014-12-05 21:59:35 -0500 | [diff] [blame^] | 892 | self.client.ssl_version = ssl.PROTOCOL_SSLv23 |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 893 | self.client.auth() |
| 894 | self.assertRaises(ValueError, self.client.auth) |
| 895 | finally: |
| 896 | self.client.ssl_version = ssl.PROTOCOL_TLSv1 |
| 897 | |
Giampaolo Rodolà | a67299e | 2010-05-26 18:06:04 +0000 | [diff] [blame] | 898 | def test_context(self): |
| 899 | self.client.quit() |
| 900 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 901 | self.assertRaises(ValueError, ftplib.FTP_TLS, keyfile=CERTFILE, |
| 902 | context=ctx) |
| 903 | self.assertRaises(ValueError, ftplib.FTP_TLS, certfile=CERTFILE, |
| 904 | context=ctx) |
| 905 | self.assertRaises(ValueError, ftplib.FTP_TLS, certfile=CERTFILE, |
| 906 | keyfile=CERTFILE, context=ctx) |
| 907 | |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 908 | self.client = ftplib.FTP_TLS(context=ctx, timeout=TIMEOUT) |
Giampaolo Rodolà | a67299e | 2010-05-26 18:06:04 +0000 | [diff] [blame] | 909 | self.client.connect(self.server.host, self.server.port) |
| 910 | self.assertNotIsInstance(self.client.sock, ssl.SSLSocket) |
| 911 | self.client.auth() |
| 912 | self.assertIs(self.client.sock.context, ctx) |
| 913 | self.assertIsInstance(self.client.sock, ssl.SSLSocket) |
| 914 | |
| 915 | self.client.prot_p() |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 916 | with self.client.transfercmd('list') as sock: |
| 917 | self.assertIs(sock.context, ctx) |
| 918 | self.assertIsInstance(sock, ssl.SSLSocket) |
Giampaolo Rodolà | a67299e | 2010-05-26 18:06:04 +0000 | [diff] [blame] | 919 | |
Giampaolo Rodola' | 096dcb1 | 2011-06-27 11:17:51 +0200 | [diff] [blame] | 920 | def test_ccc(self): |
| 921 | self.assertRaises(ValueError, self.client.ccc) |
| 922 | self.client.login(secure=True) |
| 923 | self.assertIsInstance(self.client.sock, ssl.SSLSocket) |
| 924 | self.client.ccc() |
| 925 | self.assertRaises(ValueError, self.client.sock.unwrap) |
Giampaolo Rodola' | 096dcb1 | 2011-06-27 11:17:51 +0200 | [diff] [blame] | 926 | |
Christian Heimes | e5b5edf | 2013-12-02 02:56:02 +0100 | [diff] [blame] | 927 | def test_check_hostname(self): |
| 928 | self.client.quit() |
| 929 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
| 930 | ctx.verify_mode = ssl.CERT_REQUIRED |
| 931 | ctx.check_hostname = True |
| 932 | ctx.load_verify_locations(CAFILE) |
| 933 | self.client = ftplib.FTP_TLS(context=ctx, timeout=TIMEOUT) |
| 934 | |
| 935 | # 127.0.0.1 doesn't match SAN |
| 936 | self.client.connect(self.server.host, self.server.port) |
| 937 | with self.assertRaises(ssl.CertificateError): |
| 938 | self.client.auth() |
| 939 | # exception quits connection |
| 940 | |
| 941 | self.client.connect(self.server.host, self.server.port) |
| 942 | self.client.prot_p() |
| 943 | with self.assertRaises(ssl.CertificateError): |
| 944 | with self.client.transfercmd("list") as sock: |
| 945 | pass |
| 946 | self.client.quit() |
| 947 | |
| 948 | self.client.connect("localhost", self.server.port) |
| 949 | self.client.auth() |
| 950 | self.client.quit() |
| 951 | |
| 952 | self.client.connect("localhost", self.server.port) |
| 953 | self.client.prot_p() |
| 954 | with self.client.transfercmd("list") as sock: |
| 955 | pass |
| 956 | |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 957 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 958 | class TestTimeouts(TestCase): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 959 | |
| 960 | def setUp(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 961 | self.evt = threading.Event() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 962 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 963 | self.sock.settimeout(20) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 964 | self.port = support.bind_port(self.sock) |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 965 | self.server_thread = threading.Thread(target=self.server) |
| 966 | self.server_thread.start() |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 967 | # Wait for the server to be ready. |
| 968 | self.evt.wait() |
| 969 | self.evt.clear() |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 970 | self.old_port = ftplib.FTP.port |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 971 | ftplib.FTP.port = self.port |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 972 | |
| 973 | def tearDown(self): |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 974 | ftplib.FTP.port = self.old_port |
| 975 | self.server_thread.join() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 976 | |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 977 | def server(self): |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 978 | # This method sets the evt 3 times: |
| 979 | # 1) when the connection is ready to be accepted. |
| 980 | # 2) when it is safe for the caller to close the connection |
| 981 | # 3) when we have closed the socket |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 982 | self.sock.listen(5) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 983 | # (1) Signal the caller that we are ready to accept the connection. |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 984 | self.evt.set() |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 985 | try: |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 986 | conn, addr = self.sock.accept() |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 987 | except socket.timeout: |
| 988 | pass |
| 989 | else: |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 990 | conn.sendall(b"1 Hola mundo\n") |
| 991 | conn.shutdown(socket.SHUT_WR) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 992 | # (2) Signal the caller that it is safe to close the socket. |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 993 | self.evt.set() |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 994 | conn.close() |
| 995 | finally: |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 996 | self.sock.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 997 | |
| 998 | def testTimeoutDefault(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 999 | # default -- use global socket timeout |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 1000 | self.assertIsNone(socket.getdefaulttimeout()) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1001 | socket.setdefaulttimeout(30) |
| 1002 | try: |
Antoine Pitrou | f6fbf56 | 2013-08-22 00:39:46 +0200 | [diff] [blame] | 1003 | ftp = ftplib.FTP(HOST) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1004 | finally: |
| 1005 | socket.setdefaulttimeout(None) |
| 1006 | self.assertEqual(ftp.sock.gettimeout(), 30) |
| 1007 | self.evt.wait() |
| 1008 | ftp.close() |
| 1009 | |
| 1010 | def testTimeoutNone(self): |
| 1011 | # no timeout -- do not use global socket timeout |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 1012 | self.assertIsNone(socket.getdefaulttimeout()) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1013 | socket.setdefaulttimeout(30) |
| 1014 | try: |
Antoine Pitrou | f6fbf56 | 2013-08-22 00:39:46 +0200 | [diff] [blame] | 1015 | ftp = ftplib.FTP(HOST, timeout=None) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1016 | finally: |
| 1017 | socket.setdefaulttimeout(None) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 1018 | self.assertIsNone(ftp.sock.gettimeout()) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 1019 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1020 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1021 | |
| 1022 | def testTimeoutValue(self): |
| 1023 | # a value |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1024 | ftp = ftplib.FTP(HOST, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1025 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 1026 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1027 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1028 | |
| 1029 | def testTimeoutConnect(self): |
| 1030 | ftp = ftplib.FTP() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1031 | ftp.connect(HOST, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1032 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 1033 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1034 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1035 | |
| 1036 | def testTimeoutDifferentOrder(self): |
| 1037 | ftp = ftplib.FTP(timeout=30) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1038 | ftp.connect(HOST) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1039 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 1040 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1041 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1042 | |
| 1043 | def testTimeoutDirectAccess(self): |
| 1044 | ftp = ftplib.FTP() |
| 1045 | ftp.timeout = 30 |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1046 | ftp.connect(HOST) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1047 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 1048 | self.evt.wait() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1049 | ftp.close() |
| 1050 | |
| 1051 | |
R David Murray | 87632f1 | 2013-02-19 18:32:28 -0500 | [diff] [blame] | 1052 | class TestNetrcDeprecation(TestCase): |
| 1053 | |
| 1054 | def test_deprecation(self): |
| 1055 | with support.temp_cwd(), support.EnvironmentVarGuard() as env: |
| 1056 | env['HOME'] = os.getcwd() |
| 1057 | open('.netrc', 'w').close() |
| 1058 | with self.assertWarns(DeprecationWarning): |
| 1059 | ftplib.Netrc() |
| 1060 | |
| 1061 | |
| 1062 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 1063 | def test_main(): |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 1064 | tests = [TestFTPClass, TestTimeouts, TestNetrcDeprecation, |
| 1065 | TestIPv6Environment, |
| 1066 | TestTLS_FTPClassMixin, TestTLS_FTPClass] |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 1067 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 1068 | thread_info = support.threading_setup() |
| 1069 | try: |
| 1070 | support.run_unittest(*tests) |
| 1071 | finally: |
| 1072 | support.threading_cleanup(*thread_info) |
| 1073 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1074 | |
| 1075 | if __name__ == '__main__': |
| 1076 | test_main() |