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 |
Antoine Pitrou | a6a4dc8 | 2017-09-07 18:56:24 +0200 | [diff] [blame] | 13 | import threading |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 14 | import time |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 15 | try: |
| 16 | import ssl |
| 17 | except ImportError: |
| 18 | ssl = None |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 19 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 20 | from unittest import TestCase, skipUnless |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 21 | from test import support |
Hai Shi | e80697d | 2020-05-28 06:10:27 +0800 | [diff] [blame] | 22 | from test.support import threading_helper |
Serhiy Storchaka | 1699491 | 2020-04-25 10:06:29 +0300 | [diff] [blame] | 23 | from test.support import socket_helper |
Hai Shi | 4660597 | 2020-08-04 00:49:18 +0800 | [diff] [blame] | 24 | from test.support import warnings_helper |
Serhiy Storchaka | 1699491 | 2020-04-25 10:06:29 +0300 | [diff] [blame] | 25 | from test.support.socket_helper import HOST, HOSTv6 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 26 | |
Victor Stinner | 07871b2 | 2019-12-10 20:32:59 +0100 | [diff] [blame] | 27 | TIMEOUT = support.LOOPBACK_TIMEOUT |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 28 | DEFAULT_ENCODING = 'utf-8' |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 29 | # the dummy data returned by server over the data channel when |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 30 | # RETR, LIST, NLST, MLSD commands are issued |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 31 | RETR_DATA = 'abcde12345\r\n' * 1000 + 'non-ascii char \xAE\r\n' |
| 32 | LIST_DATA = 'foo\r\nbar\r\n non-ascii char \xAE\r\n' |
| 33 | NLST_DATA = 'foo\r\nbar\r\n non-ascii char \xAE\r\n' |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 34 | MLSD_DATA = ("type=cdir;perm=el;unique==keVO1+ZF4; test\r\n" |
| 35 | "type=pdir;perm=e;unique==keVO1+d?3; ..\r\n" |
| 36 | "type=OS.unix=slink:/foobar;perm=;unique==keVO1+4G4; foobar\r\n" |
| 37 | "type=OS.unix=chr-13/29;perm=;unique==keVO1+5G4; device\r\n" |
| 38 | "type=OS.unix=blk-11/108;perm=;unique==keVO1+6G4; block\r\n" |
| 39 | "type=file;perm=awr;unique==keVO1+8G4; writable\r\n" |
| 40 | "type=dir;perm=cpmel;unique==keVO1+7G4; promiscuous\r\n" |
| 41 | "type=dir;perm=;unique==keVO1+1t2; no-exec\r\n" |
| 42 | "type=file;perm=r;unique==keVO1+EG4; two words\r\n" |
| 43 | "type=file;perm=r;unique==keVO1+IH4; leading space\r\n" |
| 44 | "type=file;perm=r;unique==keVO1+1G4; file1\r\n" |
| 45 | "type=dir;perm=cpmel;unique==keVO1+7G4; incoming\r\n" |
| 46 | "type=file;perm=r;unique==keVO1+1G4; file2\r\n" |
| 47 | "type=file;perm=r;unique==keVO1+1G4; file3\r\n" |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 48 | "type=file;perm=r;unique==keVO1+1G4; file4\r\n" |
| 49 | "type=dir;perm=cpmel;unique==SGP1; dir \xAE non-ascii char\r\n" |
| 50 | "type=file;perm=r;unique==SGP2; file \xAE non-ascii char\r\n") |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 51 | |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 52 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 53 | class DummyDTPHandler(asynchat.async_chat): |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 54 | dtp_conn_closed = False |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 55 | |
| 56 | def __init__(self, conn, baseclass): |
| 57 | asynchat.async_chat.__init__(self, conn) |
| 58 | self.baseclass = baseclass |
| 59 | self.baseclass.last_received_data = '' |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 60 | self.encoding = baseclass.encoding |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 61 | |
| 62 | def handle_read(self): |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 63 | new_data = self.recv(1024).decode(self.encoding, 'replace') |
| 64 | self.baseclass.last_received_data += new_data |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 65 | |
| 66 | def handle_close(self): |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 67 | # XXX: this method can be called many times in a row for a single |
| 68 | # connection, including in clear-text (non-TLS) mode. |
| 69 | # (behaviour witnessed with test_data_connection) |
| 70 | if not self.dtp_conn_closed: |
| 71 | self.baseclass.push('226 transfer complete') |
| 72 | self.close() |
| 73 | self.dtp_conn_closed = True |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 74 | |
| 75 | def push(self, what): |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 76 | if self.baseclass.next_data is not None: |
| 77 | what = self.baseclass.next_data |
| 78 | self.baseclass.next_data = None |
| 79 | if not what: |
| 80 | return self.close_when_done() |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 81 | super(DummyDTPHandler, self).push(what.encode(self.encoding)) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 82 | |
Giampaolo Rodolà | d930b63 | 2010-05-06 20:21:57 +0000 | [diff] [blame] | 83 | def handle_error(self): |
Berker Peksag | 8f791d3 | 2014-11-01 10:45:57 +0200 | [diff] [blame] | 84 | raise Exception |
Giampaolo Rodolà | d930b63 | 2010-05-06 20:21:57 +0000 | [diff] [blame] | 85 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 86 | |
| 87 | class DummyFTPHandler(asynchat.async_chat): |
| 88 | |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 89 | dtp_handler = DummyDTPHandler |
| 90 | |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 91 | def __init__(self, conn, encoding=DEFAULT_ENCODING): |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 92 | asynchat.async_chat.__init__(self, conn) |
Giampaolo Rodola' | 0b5c21f | 2011-05-07 19:03:47 +0200 | [diff] [blame] | 93 | # tells the socket to handle urgent data inline (ABOR command) |
| 94 | self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_OOBINLINE, 1) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 95 | self.set_terminator(b"\r\n") |
| 96 | self.in_buffer = [] |
| 97 | self.dtp = None |
| 98 | self.last_received_cmd = None |
| 99 | self.last_received_data = '' |
| 100 | self.next_response = '' |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 101 | self.next_data = None |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 102 | self.rest = None |
Serhiy Storchaka | c30b178 | 2013-10-20 16:58:27 +0300 | [diff] [blame] | 103 | self.next_retr_data = RETR_DATA |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 104 | self.push('220 welcome') |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 105 | self.encoding = encoding |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 106 | |
| 107 | def collect_incoming_data(self, data): |
| 108 | self.in_buffer.append(data) |
| 109 | |
| 110 | def found_terminator(self): |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 111 | line = b''.join(self.in_buffer).decode(self.encoding) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 112 | self.in_buffer = [] |
| 113 | if self.next_response: |
| 114 | self.push(self.next_response) |
| 115 | self.next_response = '' |
| 116 | cmd = line.split(' ')[0].lower() |
| 117 | self.last_received_cmd = cmd |
| 118 | space = line.find(' ') |
| 119 | if space != -1: |
| 120 | arg = line[space + 1:] |
| 121 | else: |
| 122 | arg = "" |
| 123 | if hasattr(self, 'cmd_' + cmd): |
| 124 | method = getattr(self, 'cmd_' + cmd) |
| 125 | method(arg) |
| 126 | else: |
| 127 | self.push('550 command "%s" not understood.' %cmd) |
| 128 | |
| 129 | def handle_error(self): |
Berker Peksag | 8f791d3 | 2014-11-01 10:45:57 +0200 | [diff] [blame] | 130 | raise Exception |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 131 | |
| 132 | def push(self, data): |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 133 | asynchat.async_chat.push(self, data.encode(self.encoding) + b'\r\n') |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 134 | |
| 135 | def cmd_port(self, arg): |
| 136 | addr = list(map(int, arg.split(','))) |
| 137 | ip = '%d.%d.%d.%d' %tuple(addr[:4]) |
| 138 | port = (addr[4] * 256) + addr[5] |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 139 | s = socket.create_connection((ip, port), timeout=TIMEOUT) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 140 | self.dtp = self.dtp_handler(s, baseclass=self) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 141 | self.push('200 active data connection established') |
| 142 | |
| 143 | def cmd_pasv(self, arg): |
Giampaolo Rodola | eb7e29f | 2019-04-09 00:34:02 +0200 | [diff] [blame] | 144 | with socket.create_server((self.socket.getsockname()[0], 0)) as sock: |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 145 | sock.settimeout(TIMEOUT) |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 146 | ip, port = sock.getsockname()[:2] |
| 147 | ip = ip.replace('.', ','); p1 = port / 256; p2 = port % 256 |
| 148 | self.push('227 entering passive mode (%s,%d,%d)' %(ip, p1, p2)) |
| 149 | conn, addr = sock.accept() |
| 150 | self.dtp = self.dtp_handler(conn, baseclass=self) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 151 | |
| 152 | def cmd_eprt(self, arg): |
| 153 | af, ip, port = arg.split(arg[0])[1:-1] |
| 154 | port = int(port) |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 155 | s = socket.create_connection((ip, port), timeout=TIMEOUT) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 156 | self.dtp = self.dtp_handler(s, baseclass=self) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 157 | self.push('200 active data connection established') |
| 158 | |
| 159 | def cmd_epsv(self, arg): |
Giampaolo Rodola | eb7e29f | 2019-04-09 00:34:02 +0200 | [diff] [blame] | 160 | with socket.create_server((self.socket.getsockname()[0], 0), |
| 161 | family=socket.AF_INET6) as sock: |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 162 | sock.settimeout(TIMEOUT) |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 163 | port = sock.getsockname()[1] |
| 164 | self.push('229 entering extended passive mode (|||%d|)' %port) |
| 165 | conn, addr = sock.accept() |
| 166 | self.dtp = self.dtp_handler(conn, baseclass=self) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 167 | |
| 168 | def cmd_echo(self, arg): |
| 169 | # sends back the received string (used by the test suite) |
| 170 | self.push(arg) |
| 171 | |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 172 | def cmd_noop(self, arg): |
| 173 | self.push('200 noop ok') |
| 174 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 175 | def cmd_user(self, arg): |
| 176 | self.push('331 username ok') |
| 177 | |
| 178 | def cmd_pass(self, arg): |
| 179 | self.push('230 password ok') |
| 180 | |
| 181 | def cmd_acct(self, arg): |
| 182 | self.push('230 acct ok') |
| 183 | |
| 184 | def cmd_rnfr(self, arg): |
| 185 | self.push('350 rnfr ok') |
| 186 | |
| 187 | def cmd_rnto(self, arg): |
| 188 | self.push('250 rnto ok') |
| 189 | |
| 190 | def cmd_dele(self, arg): |
| 191 | self.push('250 dele ok') |
| 192 | |
| 193 | def cmd_cwd(self, arg): |
| 194 | self.push('250 cwd ok') |
| 195 | |
| 196 | def cmd_size(self, arg): |
| 197 | self.push('250 1000') |
| 198 | |
| 199 | def cmd_mkd(self, arg): |
| 200 | self.push('257 "%s"' %arg) |
| 201 | |
| 202 | def cmd_rmd(self, arg): |
| 203 | self.push('250 rmd ok') |
| 204 | |
| 205 | def cmd_pwd(self, arg): |
| 206 | self.push('257 "pwd ok"') |
| 207 | |
| 208 | def cmd_type(self, arg): |
Giampaolo Rodolà | f96482e | 2010-08-04 10:36:18 +0000 | [diff] [blame] | 209 | self.push('200 type ok') |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 210 | |
| 211 | def cmd_quit(self, arg): |
| 212 | self.push('221 quit ok') |
| 213 | self.close() |
| 214 | |
Giampaolo Rodola' | 0b5c21f | 2011-05-07 19:03:47 +0200 | [diff] [blame] | 215 | def cmd_abor(self, arg): |
| 216 | self.push('226 abor ok') |
| 217 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 218 | def cmd_stor(self, arg): |
| 219 | self.push('125 stor ok') |
| 220 | |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 221 | def cmd_rest(self, arg): |
| 222 | self.rest = arg |
| 223 | self.push('350 rest ok') |
| 224 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 225 | def cmd_retr(self, arg): |
| 226 | self.push('125 retr ok') |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 227 | if self.rest is not None: |
| 228 | offset = int(self.rest) |
| 229 | else: |
| 230 | offset = 0 |
Serhiy Storchaka | c30b178 | 2013-10-20 16:58:27 +0300 | [diff] [blame] | 231 | self.dtp.push(self.next_retr_data[offset:]) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 232 | self.dtp.close_when_done() |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 233 | self.rest = None |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 234 | |
| 235 | def cmd_list(self, arg): |
| 236 | self.push('125 list ok') |
| 237 | self.dtp.push(LIST_DATA) |
| 238 | self.dtp.close_when_done() |
| 239 | |
| 240 | def cmd_nlst(self, arg): |
| 241 | self.push('125 nlst ok') |
| 242 | self.dtp.push(NLST_DATA) |
| 243 | self.dtp.close_when_done() |
| 244 | |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 245 | def cmd_opts(self, arg): |
| 246 | self.push('200 opts ok') |
| 247 | |
| 248 | def cmd_mlsd(self, arg): |
| 249 | self.push('125 mlsd ok') |
| 250 | self.dtp.push(MLSD_DATA) |
| 251 | self.dtp.close_when_done() |
| 252 | |
Serhiy Storchaka | c30b178 | 2013-10-20 16:58:27 +0300 | [diff] [blame] | 253 | def cmd_setlongretr(self, arg): |
| 254 | # For testing. Next RETR will return long line. |
| 255 | self.next_retr_data = 'x' * int(arg) |
| 256 | self.push('125 setlongretr ok') |
| 257 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 258 | |
| 259 | class DummyFTPServer(asyncore.dispatcher, threading.Thread): |
| 260 | |
| 261 | handler = DummyFTPHandler |
| 262 | |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 263 | def __init__(self, address, af=socket.AF_INET, encoding=DEFAULT_ENCODING): |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 264 | threading.Thread.__init__(self) |
| 265 | asyncore.dispatcher.__init__(self) |
Christian Heimes | 529525f | 2018-05-23 22:24:45 +0200 | [diff] [blame] | 266 | self.daemon = True |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 267 | self.create_socket(af, socket.SOCK_STREAM) |
| 268 | self.bind(address) |
| 269 | self.listen(5) |
| 270 | self.active = False |
| 271 | self.active_lock = threading.Lock() |
| 272 | self.host, self.port = self.socket.getsockname()[:2] |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 273 | self.handler_instance = None |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 274 | self.encoding = encoding |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 275 | |
| 276 | def start(self): |
| 277 | assert not self.active |
| 278 | self.__flag = threading.Event() |
| 279 | threading.Thread.start(self) |
| 280 | self.__flag.wait() |
| 281 | |
| 282 | def run(self): |
| 283 | self.active = True |
| 284 | self.__flag.set() |
| 285 | while self.active and asyncore.socket_map: |
| 286 | self.active_lock.acquire() |
| 287 | asyncore.loop(timeout=0.1, count=1) |
| 288 | self.active_lock.release() |
| 289 | asyncore.close_all(ignore_all=True) |
| 290 | |
| 291 | def stop(self): |
| 292 | assert self.active |
| 293 | self.active = False |
| 294 | self.join() |
| 295 | |
Giampaolo Rodolà | 977c707 | 2010-10-04 21:08:36 +0000 | [diff] [blame] | 296 | def handle_accepted(self, conn, addr): |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 297 | self.handler_instance = self.handler(conn, encoding=self.encoding) |
Benjamin Peterson | d06e3b0 | 2008-09-28 21:00:42 +0000 | [diff] [blame] | 298 | |
| 299 | def handle_connect(self): |
| 300 | self.close() |
| 301 | handle_read = handle_connect |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 302 | |
| 303 | def writable(self): |
| 304 | return 0 |
| 305 | |
| 306 | def handle_error(self): |
Berker Peksag | 8f791d3 | 2014-11-01 10:45:57 +0200 | [diff] [blame] | 307 | raise Exception |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 308 | |
| 309 | |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 310 | if ssl is not None: |
| 311 | |
Christian Heimes | e5b5edf | 2013-12-02 02:56:02 +0100 | [diff] [blame] | 312 | CERTFILE = os.path.join(os.path.dirname(__file__), "keycert3.pem") |
| 313 | CAFILE = os.path.join(os.path.dirname(__file__), "pycacert.pem") |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 314 | |
| 315 | class SSLConnection(asyncore.dispatcher): |
| 316 | """An asyncore.dispatcher subclass supporting TLS/SSL.""" |
| 317 | |
| 318 | _ssl_accepting = False |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 319 | _ssl_closing = False |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 320 | |
| 321 | def secure_connection(self): |
Christian Heimes | d048637 | 2016-09-10 23:23:33 +0200 | [diff] [blame] | 322 | context = ssl.SSLContext() |
| 323 | context.load_cert_chain(CERTFILE) |
| 324 | socket = context.wrap_socket(self.socket, |
| 325 | suppress_ragged_eofs=False, |
| 326 | server_side=True, |
| 327 | do_handshake_on_connect=False) |
Giampaolo Rodola' | 096dcb1 | 2011-06-27 11:17:51 +0200 | [diff] [blame] | 328 | self.del_channel() |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 329 | self.set_socket(socket) |
| 330 | self._ssl_accepting = True |
| 331 | |
| 332 | def _do_ssl_handshake(self): |
| 333 | try: |
| 334 | self.socket.do_handshake() |
| 335 | except ssl.SSLError as err: |
| 336 | if err.args[0] in (ssl.SSL_ERROR_WANT_READ, |
| 337 | ssl.SSL_ERROR_WANT_WRITE): |
| 338 | return |
| 339 | elif err.args[0] == ssl.SSL_ERROR_EOF: |
| 340 | return self.handle_close() |
Christian Heimes | 61d478c | 2018-01-27 15:51:38 +0100 | [diff] [blame] | 341 | # TODO: SSLError does not expose alert information |
| 342 | elif "SSLV3_ALERT_BAD_CERTIFICATE" in err.args[1]: |
| 343 | return self.handle_close() |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 344 | raise |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 345 | except OSError as err: |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 346 | if err.args[0] == errno.ECONNABORTED: |
| 347 | return self.handle_close() |
| 348 | else: |
| 349 | self._ssl_accepting = False |
| 350 | |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 351 | def _do_ssl_shutdown(self): |
| 352 | self._ssl_closing = True |
| 353 | try: |
| 354 | self.socket = self.socket.unwrap() |
| 355 | except ssl.SSLError as err: |
| 356 | if err.args[0] in (ssl.SSL_ERROR_WANT_READ, |
| 357 | ssl.SSL_ERROR_WANT_WRITE): |
| 358 | return |
Pablo Galindo | 293dd23 | 2019-11-19 21:34:03 +0000 | [diff] [blame] | 359 | except OSError: |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 360 | # Any "socket error" corresponds to a SSL_ERROR_SYSCALL return |
| 361 | # from OpenSSL's SSL_shutdown(), corresponding to a |
| 362 | # closed socket condition. See also: |
| 363 | # http://www.mail-archive.com/openssl-users@openssl.org/msg60710.html |
| 364 | pass |
| 365 | self._ssl_closing = False |
Benjamin Peterson | b29614e | 2012-10-09 11:16:03 -0400 | [diff] [blame] | 366 | if getattr(self, '_ccc', False) is False: |
Giampaolo Rodola' | 096dcb1 | 2011-06-27 11:17:51 +0200 | [diff] [blame] | 367 | super(SSLConnection, self).close() |
| 368 | else: |
| 369 | pass |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 370 | |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 371 | def handle_read_event(self): |
| 372 | if self._ssl_accepting: |
| 373 | self._do_ssl_handshake() |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 374 | elif self._ssl_closing: |
| 375 | self._do_ssl_shutdown() |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 376 | else: |
| 377 | super(SSLConnection, self).handle_read_event() |
| 378 | |
| 379 | def handle_write_event(self): |
| 380 | if self._ssl_accepting: |
| 381 | self._do_ssl_handshake() |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 382 | elif self._ssl_closing: |
| 383 | self._do_ssl_shutdown() |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 384 | else: |
| 385 | super(SSLConnection, self).handle_write_event() |
| 386 | |
| 387 | def send(self, data): |
| 388 | try: |
| 389 | return super(SSLConnection, self).send(data) |
| 390 | except ssl.SSLError as err: |
Antoine Pitrou | 5733c08 | 2010-03-22 14:49:10 +0000 | [diff] [blame] | 391 | if err.args[0] in (ssl.SSL_ERROR_EOF, ssl.SSL_ERROR_ZERO_RETURN, |
| 392 | ssl.SSL_ERROR_WANT_READ, |
| 393 | ssl.SSL_ERROR_WANT_WRITE): |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 394 | return 0 |
| 395 | raise |
| 396 | |
| 397 | def recv(self, buffer_size): |
| 398 | try: |
| 399 | return super(SSLConnection, self).recv(buffer_size) |
| 400 | except ssl.SSLError as err: |
Antoine Pitrou | 5733c08 | 2010-03-22 14:49:10 +0000 | [diff] [blame] | 401 | if err.args[0] in (ssl.SSL_ERROR_WANT_READ, |
| 402 | ssl.SSL_ERROR_WANT_WRITE): |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 403 | return b'' |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 404 | if err.args[0] in (ssl.SSL_ERROR_EOF, ssl.SSL_ERROR_ZERO_RETURN): |
| 405 | self.handle_close() |
| 406 | return b'' |
| 407 | raise |
| 408 | |
| 409 | def handle_error(self): |
Berker Peksag | 8f791d3 | 2014-11-01 10:45:57 +0200 | [diff] [blame] | 410 | raise Exception |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 411 | |
| 412 | def close(self): |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 413 | if (isinstance(self.socket, ssl.SSLSocket) and |
Christian Heimes | 529525f | 2018-05-23 22:24:45 +0200 | [diff] [blame] | 414 | self.socket._sslobj is not None): |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 415 | self._do_ssl_shutdown() |
Benjamin Peterson | 1bd93a7 | 2010-10-31 19:58:07 +0000 | [diff] [blame] | 416 | else: |
| 417 | super(SSLConnection, self).close() |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 418 | |
| 419 | |
| 420 | class DummyTLS_DTPHandler(SSLConnection, DummyDTPHandler): |
| 421 | """A DummyDTPHandler subclass supporting TLS/SSL.""" |
| 422 | |
| 423 | def __init__(self, conn, baseclass): |
| 424 | DummyDTPHandler.__init__(self, conn, baseclass) |
| 425 | if self.baseclass.secure_data_channel: |
| 426 | self.secure_connection() |
| 427 | |
| 428 | |
| 429 | class DummyTLS_FTPHandler(SSLConnection, DummyFTPHandler): |
| 430 | """A DummyFTPHandler subclass supporting TLS/SSL.""" |
| 431 | |
| 432 | dtp_handler = DummyTLS_DTPHandler |
| 433 | |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 434 | def __init__(self, conn, encoding=DEFAULT_ENCODING): |
| 435 | DummyFTPHandler.__init__(self, conn, encoding=encoding) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 436 | self.secure_data_channel = False |
Giampaolo Rodola' | 096dcb1 | 2011-06-27 11:17:51 +0200 | [diff] [blame] | 437 | self._ccc = False |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 438 | |
| 439 | def cmd_auth(self, line): |
| 440 | """Set up secure control channel.""" |
| 441 | self.push('234 AUTH TLS successful') |
| 442 | self.secure_connection() |
| 443 | |
Giampaolo Rodola' | 096dcb1 | 2011-06-27 11:17:51 +0200 | [diff] [blame] | 444 | def cmd_ccc(self, line): |
| 445 | self.push('220 Reverting back to clear-text') |
| 446 | self._ccc = True |
| 447 | self._do_ssl_shutdown() |
| 448 | |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 449 | def cmd_pbsz(self, line): |
| 450 | """Negotiate size of buffer for secure data transfer. |
| 451 | For TLS/SSL the only valid value for the parameter is '0'. |
| 452 | Any other value is accepted but ignored. |
| 453 | """ |
| 454 | self.push('200 PBSZ=0 successful.') |
| 455 | |
| 456 | def cmd_prot(self, line): |
| 457 | """Setup un/secure data channel.""" |
| 458 | arg = line.upper() |
| 459 | if arg == 'C': |
| 460 | self.push('200 Protection set to Clear') |
| 461 | self.secure_data_channel = False |
| 462 | elif arg == 'P': |
| 463 | self.push('200 Protection set to Private') |
| 464 | self.secure_data_channel = True |
| 465 | else: |
| 466 | self.push("502 Unrecognized PROT type (use C or P).") |
| 467 | |
| 468 | |
| 469 | class DummyTLS_FTPServer(DummyFTPServer): |
| 470 | handler = DummyTLS_FTPHandler |
| 471 | |
| 472 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 473 | class TestFTPClass(TestCase): |
| 474 | |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 475 | def setUp(self, encoding=DEFAULT_ENCODING): |
| 476 | self.server = DummyFTPServer((HOST, 0), encoding=encoding) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 477 | self.server.start() |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 478 | self.client = ftplib.FTP(timeout=TIMEOUT, encoding=encoding) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 479 | self.client.connect(self.server.host, self.server.port) |
| 480 | |
| 481 | def tearDown(self): |
| 482 | self.client.close() |
| 483 | self.server.stop() |
Victor Stinner | d403a29 | 2017-09-13 03:58:25 -0700 | [diff] [blame] | 484 | # Explicitly clear the attribute to prevent dangling thread |
| 485 | self.server = None |
Victor Stinner | 7352864 | 2017-06-30 17:36:57 +0200 | [diff] [blame] | 486 | asyncore.close_all(ignore_all=True) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 487 | |
Giampaolo Rodola' | 8bc8585 | 2012-01-09 17:10:10 +0100 | [diff] [blame] | 488 | def check_data(self, received, expected): |
| 489 | self.assertEqual(len(received), len(expected)) |
| 490 | self.assertEqual(received, expected) |
| 491 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 492 | def test_getwelcome(self): |
| 493 | self.assertEqual(self.client.getwelcome(), '220 welcome') |
| 494 | |
| 495 | def test_sanitize(self): |
| 496 | self.assertEqual(self.client.sanitize('foo'), repr('foo')) |
| 497 | self.assertEqual(self.client.sanitize('pass 12345'), repr('pass *****')) |
| 498 | self.assertEqual(self.client.sanitize('PASS 12345'), repr('PASS *****')) |
| 499 | |
| 500 | def test_exceptions(self): |
Dong-hee Na | 2b1e6e9 | 2017-07-23 02:20:22 +0900 | [diff] [blame] | 501 | self.assertRaises(ValueError, self.client.sendcmd, 'echo 40\r\n0') |
| 502 | self.assertRaises(ValueError, self.client.sendcmd, 'echo 40\n0') |
| 503 | self.assertRaises(ValueError, self.client.sendcmd, 'echo 40\r0') |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 504 | self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 400') |
| 505 | self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 499') |
| 506 | self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 500') |
| 507 | self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 599') |
| 508 | self.assertRaises(ftplib.error_proto, self.client.sendcmd, 'echo 999') |
| 509 | |
| 510 | def test_all_errors(self): |
| 511 | exceptions = (ftplib.error_reply, ftplib.error_temp, ftplib.error_perm, |
Dong-hee Na | 2b1e6e9 | 2017-07-23 02:20:22 +0900 | [diff] [blame] | 512 | ftplib.error_proto, ftplib.Error, OSError, |
| 513 | EOFError) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 514 | for x in exceptions: |
| 515 | try: |
| 516 | raise x('exception not included in all_errors set') |
| 517 | except ftplib.all_errors: |
| 518 | pass |
| 519 | |
| 520 | def test_set_pasv(self): |
| 521 | # passive mode is supposed to be enabled by default |
| 522 | self.assertTrue(self.client.passiveserver) |
| 523 | self.client.set_pasv(True) |
| 524 | self.assertTrue(self.client.passiveserver) |
| 525 | self.client.set_pasv(False) |
| 526 | self.assertFalse(self.client.passiveserver) |
| 527 | |
| 528 | def test_voidcmd(self): |
| 529 | self.client.voidcmd('echo 200') |
| 530 | self.client.voidcmd('echo 299') |
| 531 | self.assertRaises(ftplib.error_reply, self.client.voidcmd, 'echo 199') |
| 532 | self.assertRaises(ftplib.error_reply, self.client.voidcmd, 'echo 300') |
| 533 | |
| 534 | def test_login(self): |
| 535 | self.client.login() |
| 536 | |
| 537 | def test_acct(self): |
| 538 | self.client.acct('passwd') |
| 539 | |
| 540 | def test_rename(self): |
| 541 | self.client.rename('a', 'b') |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 542 | self.server.handler_instance.next_response = '200' |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 543 | self.assertRaises(ftplib.error_reply, self.client.rename, 'a', 'b') |
| 544 | |
| 545 | def test_delete(self): |
| 546 | self.client.delete('foo') |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 547 | self.server.handler_instance.next_response = '199' |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 548 | self.assertRaises(ftplib.error_reply, self.client.delete, 'foo') |
| 549 | |
| 550 | def test_size(self): |
| 551 | self.client.size('foo') |
| 552 | |
| 553 | def test_mkd(self): |
| 554 | dir = self.client.mkd('/foo') |
| 555 | self.assertEqual(dir, '/foo') |
| 556 | |
| 557 | def test_rmd(self): |
| 558 | self.client.rmd('foo') |
| 559 | |
Senthil Kumaran | 0d53860 | 2013-08-12 22:25:27 -0700 | [diff] [blame] | 560 | def test_cwd(self): |
| 561 | dir = self.client.cwd('/foo') |
| 562 | self.assertEqual(dir, '250 cwd ok') |
| 563 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 564 | def test_pwd(self): |
| 565 | dir = self.client.pwd() |
| 566 | self.assertEqual(dir, 'pwd ok') |
| 567 | |
| 568 | def test_quit(self): |
| 569 | self.assertEqual(self.client.quit(), '221 quit ok') |
| 570 | # Ensure the connection gets closed; sock attribute should be None |
| 571 | self.assertEqual(self.client.sock, None) |
| 572 | |
Giampaolo Rodola' | 0b5c21f | 2011-05-07 19:03:47 +0200 | [diff] [blame] | 573 | def test_abort(self): |
| 574 | self.client.abort() |
| 575 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 576 | def test_retrbinary(self): |
| 577 | def callback(data): |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 578 | received.append(data.decode(self.client.encoding)) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 579 | received = [] |
| 580 | self.client.retrbinary('retr', callback) |
Giampaolo Rodola' | 8bc8585 | 2012-01-09 17:10:10 +0100 | [diff] [blame] | 581 | self.check_data(''.join(received), RETR_DATA) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 582 | |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 583 | def test_retrbinary_rest(self): |
| 584 | def callback(data): |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 585 | received.append(data.decode(self.client.encoding)) |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 586 | for rest in (0, 10, 20): |
| 587 | received = [] |
| 588 | self.client.retrbinary('retr', callback, rest=rest) |
Giampaolo Rodola' | 8bc8585 | 2012-01-09 17:10:10 +0100 | [diff] [blame] | 589 | self.check_data(''.join(received), RETR_DATA[rest:]) |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 590 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 591 | def test_retrlines(self): |
| 592 | received = [] |
| 593 | self.client.retrlines('retr', received.append) |
Giampaolo Rodola' | 8bc8585 | 2012-01-09 17:10:10 +0100 | [diff] [blame] | 594 | self.check_data(''.join(received), RETR_DATA.replace('\r\n', '')) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 595 | |
| 596 | def test_storbinary(self): |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 597 | f = io.BytesIO(RETR_DATA.encode(self.client.encoding)) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 598 | self.client.storbinary('stor', f) |
Giampaolo Rodola' | 8bc8585 | 2012-01-09 17:10:10 +0100 | [diff] [blame] | 599 | self.check_data(self.server.handler_instance.last_received_data, RETR_DATA) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 600 | # test new callback arg |
| 601 | flag = [] |
| 602 | f.seek(0) |
| 603 | self.client.storbinary('stor', f, callback=lambda x: flag.append(None)) |
| 604 | self.assertTrue(flag) |
| 605 | |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 606 | def test_storbinary_rest(self): |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 607 | data = RETR_DATA.replace('\r\n', '\n').encode(self.client.encoding) |
| 608 | f = io.BytesIO(data) |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 609 | for r in (30, '30'): |
| 610 | f.seek(0) |
| 611 | self.client.storbinary('stor', f, rest=r) |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 612 | self.assertEqual(self.server.handler_instance.rest, str(r)) |
Antoine Pitrou | 648bcd7 | 2009-11-27 13:23:26 +0000 | [diff] [blame] | 613 | |
Giampaolo Rodolà | f96482e | 2010-08-04 10:36:18 +0000 | [diff] [blame] | 614 | def test_storlines(self): |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 615 | data = RETR_DATA.replace('\r\n', '\n').encode(self.client.encoding) |
| 616 | f = io.BytesIO(data) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 617 | self.client.storlines('stor', f) |
Giampaolo Rodola' | 8bc8585 | 2012-01-09 17:10:10 +0100 | [diff] [blame] | 618 | self.check_data(self.server.handler_instance.last_received_data, RETR_DATA) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 619 | # test new callback arg |
| 620 | flag = [] |
| 621 | f.seek(0) |
| 622 | self.client.storlines('stor foo', f, callback=lambda x: flag.append(None)) |
| 623 | self.assertTrue(flag) |
| 624 | |
Victor Stinner | ed3a303 | 2013-04-02 22:13:27 +0200 | [diff] [blame] | 625 | f = io.StringIO(RETR_DATA.replace('\r\n', '\n')) |
| 626 | # storlines() expects a binary file, not a text file |
Hai Shi | 4660597 | 2020-08-04 00:49:18 +0800 | [diff] [blame] | 627 | with warnings_helper.check_warnings(('', BytesWarning), quiet=True): |
Florent Xicluna | 5f3fef3 | 2013-07-06 15:08:21 +0200 | [diff] [blame] | 628 | self.assertRaises(TypeError, self.client.storlines, 'stor foo', f) |
Victor Stinner | ed3a303 | 2013-04-02 22:13:27 +0200 | [diff] [blame] | 629 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 630 | def test_nlst(self): |
| 631 | self.client.nlst() |
| 632 | self.assertEqual(self.client.nlst(), NLST_DATA.split('\r\n')[:-1]) |
| 633 | |
| 634 | def test_dir(self): |
| 635 | l = [] |
| 636 | self.client.dir(lambda x: l.append(x)) |
| 637 | self.assertEqual(''.join(l), LIST_DATA.replace('\r\n', '')) |
| 638 | |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 639 | def test_mlsd(self): |
| 640 | list(self.client.mlsd()) |
| 641 | list(self.client.mlsd(path='/')) |
| 642 | list(self.client.mlsd(path='/', facts=['size', 'type'])) |
| 643 | |
| 644 | ls = list(self.client.mlsd()) |
| 645 | for name, facts in ls: |
Giampaolo Rodola' | a55efb3 | 2011-05-07 16:06:59 +0200 | [diff] [blame] | 646 | self.assertIsInstance(name, str) |
| 647 | self.assertIsInstance(facts, dict) |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 648 | self.assertTrue(name) |
Giampaolo Rodola' | a55efb3 | 2011-05-07 16:06:59 +0200 | [diff] [blame] | 649 | self.assertIn('type', facts) |
| 650 | self.assertIn('perm', facts) |
| 651 | self.assertIn('unique', facts) |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 652 | |
| 653 | def set_data(data): |
| 654 | self.server.handler_instance.next_data = data |
| 655 | |
| 656 | def test_entry(line, type=None, perm=None, unique=None, name=None): |
| 657 | type = 'type' if type is None else type |
| 658 | perm = 'perm' if perm is None else perm |
| 659 | unique = 'unique' if unique is None else unique |
| 660 | name = 'name' if name is None else name |
| 661 | set_data(line) |
| 662 | _name, facts = next(self.client.mlsd()) |
| 663 | self.assertEqual(_name, name) |
| 664 | self.assertEqual(facts['type'], type) |
| 665 | self.assertEqual(facts['perm'], perm) |
| 666 | self.assertEqual(facts['unique'], unique) |
| 667 | |
| 668 | # plain |
| 669 | test_entry('type=type;perm=perm;unique=unique; name\r\n') |
| 670 | # "=" in fact value |
| 671 | test_entry('type=ty=pe;perm=perm;unique=unique; name\r\n', type="ty=pe") |
| 672 | test_entry('type==type;perm=perm;unique=unique; name\r\n', type="=type") |
| 673 | test_entry('type=t=y=pe;perm=perm;unique=unique; name\r\n', type="t=y=pe") |
| 674 | test_entry('type=====;perm=perm;unique=unique; name\r\n', type="====") |
| 675 | # spaces in name |
| 676 | test_entry('type=type;perm=perm;unique=unique; na me\r\n', name="na me") |
| 677 | test_entry('type=type;perm=perm;unique=unique; name \r\n', name="name ") |
| 678 | test_entry('type=type;perm=perm;unique=unique; name\r\n', name=" name") |
| 679 | test_entry('type=type;perm=perm;unique=unique; n am e\r\n', name="n am e") |
| 680 | # ";" in name |
| 681 | test_entry('type=type;perm=perm;unique=unique; na;me\r\n', name="na;me") |
| 682 | test_entry('type=type;perm=perm;unique=unique; ;name\r\n', name=";name") |
| 683 | test_entry('type=type;perm=perm;unique=unique; ;name;\r\n', name=";name;") |
| 684 | test_entry('type=type;perm=perm;unique=unique; ;;;;\r\n', name=";;;;") |
| 685 | # case sensitiveness |
| 686 | set_data('Type=type;TyPe=perm;UNIQUE=unique; name\r\n') |
| 687 | _name, facts = next(self.client.mlsd()) |
Giampaolo Rodola' | a55efb3 | 2011-05-07 16:06:59 +0200 | [diff] [blame] | 688 | for x in facts: |
| 689 | self.assertTrue(x.islower()) |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 690 | # no data (directory empty) |
| 691 | set_data('') |
| 692 | self.assertRaises(StopIteration, next, self.client.mlsd()) |
| 693 | set_data('') |
| 694 | for x in self.client.mlsd(): |
Berker Peksag | 8f791d3 | 2014-11-01 10:45:57 +0200 | [diff] [blame] | 695 | self.fail("unexpected data %s" % x) |
Giampaolo Rodola' | d78def9 | 2011-05-06 19:49:08 +0200 | [diff] [blame] | 696 | |
Benjamin Peterson | 3a53fbb | 2008-09-27 22:04:16 +0000 | [diff] [blame] | 697 | def test_makeport(self): |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 698 | with self.client.makeport(): |
| 699 | # IPv4 is in use, just make sure send_eprt has not been used |
| 700 | self.assertEqual(self.server.handler_instance.last_received_cmd, |
| 701 | 'port') |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 702 | |
| 703 | def test_makepasv(self): |
| 704 | host, port = self.client.makepasv() |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 705 | conn = socket.create_connection((host, port), timeout=TIMEOUT) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 706 | conn.close() |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 707 | # 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] | 708 | self.assertEqual(self.server.handler_instance.last_received_cmd, 'pasv') |
| 709 | |
| 710 | def test_with_statement(self): |
| 711 | self.client.quit() |
| 712 | |
| 713 | def is_client_connected(): |
| 714 | if self.client.sock is None: |
| 715 | return False |
| 716 | try: |
| 717 | self.client.sendcmd('noop') |
Andrew Svetlov | 0832af6 | 2012-12-18 23:10:48 +0200 | [diff] [blame] | 718 | except (OSError, EOFError): |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 719 | return False |
| 720 | return True |
| 721 | |
| 722 | # base test |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 723 | with ftplib.FTP(timeout=TIMEOUT) as self.client: |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 724 | self.client.connect(self.server.host, self.server.port) |
| 725 | self.client.sendcmd('noop') |
| 726 | self.assertTrue(is_client_connected()) |
| 727 | self.assertEqual(self.server.handler_instance.last_received_cmd, 'quit') |
| 728 | self.assertFalse(is_client_connected()) |
| 729 | |
| 730 | # QUIT sent inside the with block |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 731 | with ftplib.FTP(timeout=TIMEOUT) as self.client: |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 732 | self.client.connect(self.server.host, self.server.port) |
| 733 | self.client.sendcmd('noop') |
| 734 | self.client.quit() |
| 735 | self.assertEqual(self.server.handler_instance.last_received_cmd, 'quit') |
| 736 | self.assertFalse(is_client_connected()) |
| 737 | |
| 738 | # force a wrong response code to be sent on QUIT: error_perm |
| 739 | # is expected and the connection is supposed to be closed |
| 740 | try: |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 741 | with ftplib.FTP(timeout=TIMEOUT) as self.client: |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 742 | self.client.connect(self.server.host, self.server.port) |
| 743 | self.client.sendcmd('noop') |
| 744 | self.server.handler_instance.next_response = '550 error on quit' |
| 745 | except ftplib.error_perm as err: |
| 746 | self.assertEqual(str(err), '550 error on quit') |
| 747 | else: |
| 748 | self.fail('Exception not raised') |
| 749 | # needed to give the threaded server some time to set the attribute |
| 750 | # which otherwise would still be == 'noop' |
| 751 | time.sleep(0.1) |
| 752 | self.assertEqual(self.server.handler_instance.last_received_cmd, 'quit') |
| 753 | self.assertFalse(is_client_connected()) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 754 | |
Giampaolo Rodolà | 396ff06 | 2011-02-28 19:19:51 +0000 | [diff] [blame] | 755 | def test_source_address(self): |
| 756 | self.client.quit() |
Serhiy Storchaka | 1699491 | 2020-04-25 10:06:29 +0300 | [diff] [blame] | 757 | port = socket_helper.find_unused_port() |
Antoine Pitrou | 6dca527 | 2011-04-03 18:29:45 +0200 | [diff] [blame] | 758 | try: |
| 759 | self.client.connect(self.server.host, self.server.port, |
| 760 | source_address=(HOST, port)) |
| 761 | self.assertEqual(self.client.sock.getsockname()[1], port) |
| 762 | self.client.quit() |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 763 | except OSError as e: |
Antoine Pitrou | 6dca527 | 2011-04-03 18:29:45 +0200 | [diff] [blame] | 764 | if e.errno == errno.EADDRINUSE: |
| 765 | self.skipTest("couldn't bind to port %d" % port) |
| 766 | raise |
Giampaolo Rodolà | 396ff06 | 2011-02-28 19:19:51 +0000 | [diff] [blame] | 767 | |
| 768 | def test_source_address_passive_connection(self): |
Serhiy Storchaka | 1699491 | 2020-04-25 10:06:29 +0300 | [diff] [blame] | 769 | port = socket_helper.find_unused_port() |
Giampaolo Rodolà | 396ff06 | 2011-02-28 19:19:51 +0000 | [diff] [blame] | 770 | self.client.source_address = (HOST, port) |
Antoine Pitrou | 6dca527 | 2011-04-03 18:29:45 +0200 | [diff] [blame] | 771 | try: |
| 772 | with self.client.transfercmd('list') as sock: |
| 773 | self.assertEqual(sock.getsockname()[1], port) |
Andrew Svetlov | f7a17b4 | 2012-12-25 16:47:37 +0200 | [diff] [blame] | 774 | except OSError as e: |
Antoine Pitrou | 6dca527 | 2011-04-03 18:29:45 +0200 | [diff] [blame] | 775 | if e.errno == errno.EADDRINUSE: |
| 776 | self.skipTest("couldn't bind to port %d" % port) |
| 777 | raise |
Giampaolo Rodolà | 396ff06 | 2011-02-28 19:19:51 +0000 | [diff] [blame] | 778 | |
Giampaolo Rodolà | bbc4782 | 2010-08-23 22:10:32 +0000 | [diff] [blame] | 779 | def test_parse257(self): |
| 780 | self.assertEqual(ftplib.parse257('257 "/foo/bar"'), '/foo/bar') |
| 781 | self.assertEqual(ftplib.parse257('257 "/foo/bar" created'), '/foo/bar') |
| 782 | self.assertEqual(ftplib.parse257('257 ""'), '') |
| 783 | self.assertEqual(ftplib.parse257('257 "" created'), '') |
| 784 | self.assertRaises(ftplib.error_reply, ftplib.parse257, '250 "/foo/bar"') |
| 785 | # The 257 response is supposed to include the directory |
| 786 | # name and in case it contains embedded double-quotes |
| 787 | # they must be doubled (see RFC-959, chapter 7, appendix 2). |
| 788 | self.assertEqual(ftplib.parse257('257 "/foo/b""ar"'), '/foo/b"ar') |
| 789 | self.assertEqual(ftplib.parse257('257 "/foo/b""ar" created'), '/foo/b"ar') |
| 790 | |
Serhiy Storchaka | c30b178 | 2013-10-20 16:58:27 +0300 | [diff] [blame] | 791 | def test_line_too_long(self): |
| 792 | self.assertRaises(ftplib.Error, self.client.sendcmd, |
| 793 | 'x' * self.client.maxline * 2) |
| 794 | |
| 795 | def test_retrlines_too_long(self): |
| 796 | self.client.sendcmd('SETLONGRETR %d' % (self.client.maxline * 2)) |
| 797 | received = [] |
| 798 | self.assertRaises(ftplib.Error, |
| 799 | self.client.retrlines, 'retr', received.append) |
| 800 | |
| 801 | def test_storlines_too_long(self): |
| 802 | f = io.BytesIO(b'x' * self.client.maxline * 2) |
| 803 | self.assertRaises(ftplib.Error, self.client.storlines, 'stor', f) |
| 804 | |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 805 | def test_encoding_param(self): |
| 806 | encodings = ['latin-1', 'utf-8'] |
| 807 | for encoding in encodings: |
| 808 | with self.subTest(encoding=encoding): |
| 809 | self.tearDown() |
| 810 | self.setUp(encoding=encoding) |
| 811 | self.assertEqual(encoding, self.client.encoding) |
| 812 | self.test_retrbinary() |
| 813 | self.test_storbinary() |
| 814 | self.test_retrlines() |
| 815 | new_dir = self.client.mkd('/non-ascii dir \xAE') |
| 816 | self.check_data(new_dir, '/non-ascii dir \xAE') |
| 817 | # Check default encoding |
| 818 | client = ftplib.FTP(timeout=TIMEOUT) |
| 819 | self.assertEqual(DEFAULT_ENCODING, client.encoding) |
| 820 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 821 | |
Serhiy Storchaka | 1699491 | 2020-04-25 10:06:29 +0300 | [diff] [blame] | 822 | @skipUnless(socket_helper.IPV6_ENABLED, "IPv6 not enabled") |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 823 | class TestIPv6Environment(TestCase): |
| 824 | |
| 825 | def setUp(self): |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 826 | self.server = DummyFTPServer((HOSTv6, 0), |
| 827 | af=socket.AF_INET6, |
| 828 | encoding=DEFAULT_ENCODING) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 829 | self.server.start() |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 830 | self.client = ftplib.FTP(timeout=TIMEOUT, encoding=DEFAULT_ENCODING) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 831 | self.client.connect(self.server.host, self.server.port) |
| 832 | |
| 833 | def tearDown(self): |
| 834 | self.client.close() |
| 835 | self.server.stop() |
Victor Stinner | d403a29 | 2017-09-13 03:58:25 -0700 | [diff] [blame] | 836 | # Explicitly clear the attribute to prevent dangling thread |
| 837 | self.server = None |
Victor Stinner | 7352864 | 2017-06-30 17:36:57 +0200 | [diff] [blame] | 838 | asyncore.close_all(ignore_all=True) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 839 | |
| 840 | def test_af(self): |
| 841 | self.assertEqual(self.client.af, socket.AF_INET6) |
| 842 | |
| 843 | def test_makeport(self): |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 844 | with self.client.makeport(): |
| 845 | self.assertEqual(self.server.handler_instance.last_received_cmd, |
| 846 | 'eprt') |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 847 | |
| 848 | def test_makepasv(self): |
| 849 | host, port = self.client.makepasv() |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 850 | conn = socket.create_connection((host, port), timeout=TIMEOUT) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 851 | conn.close() |
Giampaolo Rodolà | bd576b7 | 2010-05-10 14:53:29 +0000 | [diff] [blame] | 852 | self.assertEqual(self.server.handler_instance.last_received_cmd, 'epsv') |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 853 | |
| 854 | def test_transfer(self): |
| 855 | def retr(): |
| 856 | def callback(data): |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 857 | received.append(data.decode(self.client.encoding)) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 858 | received = [] |
| 859 | self.client.retrbinary('retr', callback) |
Giampaolo Rodola' | 8bc8585 | 2012-01-09 17:10:10 +0100 | [diff] [blame] | 860 | self.assertEqual(len(''.join(received)), len(RETR_DATA)) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 861 | self.assertEqual(''.join(received), RETR_DATA) |
| 862 | self.client.set_pasv(True) |
| 863 | retr() |
| 864 | self.client.set_pasv(False) |
| 865 | retr() |
| 866 | |
| 867 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 868 | @skipUnless(ssl, "SSL not available") |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 869 | class TestTLS_FTPClassMixin(TestFTPClass): |
| 870 | """Repeat TestFTPClass tests starting the TLS layer for both control |
| 871 | and data connections first. |
| 872 | """ |
| 873 | |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 874 | def setUp(self, encoding=DEFAULT_ENCODING): |
| 875 | self.server = DummyTLS_FTPServer((HOST, 0), encoding=encoding) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 876 | self.server.start() |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 877 | self.client = ftplib.FTP_TLS(timeout=TIMEOUT, encoding=encoding) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 878 | self.client.connect(self.server.host, self.server.port) |
| 879 | # enable TLS |
| 880 | self.client.auth() |
| 881 | self.client.prot_p() |
| 882 | |
| 883 | |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 884 | @skipUnless(ssl, "SSL not available") |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 885 | class TestTLS_FTPClass(TestCase): |
| 886 | """Specific TLS_FTP class tests.""" |
| 887 | |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 888 | def setUp(self, encoding=DEFAULT_ENCODING): |
| 889 | self.server = DummyTLS_FTPServer((HOST, 0), encoding=encoding) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 890 | self.server.start() |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 891 | self.client = ftplib.FTP_TLS(timeout=TIMEOUT) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 892 | self.client.connect(self.server.host, self.server.port) |
| 893 | |
| 894 | def tearDown(self): |
| 895 | self.client.close() |
| 896 | self.server.stop() |
Victor Stinner | d403a29 | 2017-09-13 03:58:25 -0700 | [diff] [blame] | 897 | # Explicitly clear the attribute to prevent dangling thread |
| 898 | self.server = None |
Victor Stinner | 7352864 | 2017-06-30 17:36:57 +0200 | [diff] [blame] | 899 | asyncore.close_all(ignore_all=True) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 900 | |
| 901 | def test_control_connection(self): |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 902 | self.assertNotIsInstance(self.client.sock, ssl.SSLSocket) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 903 | self.client.auth() |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 904 | self.assertIsInstance(self.client.sock, ssl.SSLSocket) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 905 | |
| 906 | def test_data_connection(self): |
| 907 | # clear text |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 908 | with self.client.transfercmd('list') as sock: |
| 909 | self.assertNotIsInstance(sock, ssl.SSLSocket) |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 910 | self.assertEqual(sock.recv(1024), |
| 911 | LIST_DATA.encode(self.client.encoding)) |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 912 | self.assertEqual(self.client.voidresp(), "226 transfer complete") |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 913 | |
| 914 | # secured, after PROT P |
| 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.assertIsInstance(sock, ssl.SSLSocket) |
Christian Heimes | 1590c39 | 2018-08-16 19:43:44 +0200 | [diff] [blame] | 918 | # consume from SSL socket to finalize handshake and avoid |
| 919 | # "SSLError [SSL] shutdown while in init" |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 920 | self.assertEqual(sock.recv(1024), |
| 921 | LIST_DATA.encode(self.client.encoding)) |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 922 | self.assertEqual(self.client.voidresp(), "226 transfer complete") |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 923 | |
| 924 | # PROT C is issued, the connection must be in cleartext again |
| 925 | self.client.prot_c() |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 926 | with self.client.transfercmd('list') as sock: |
| 927 | self.assertNotIsInstance(sock, ssl.SSLSocket) |
Sebastian Pedersen | a1a0eb4 | 2020-04-14 01:07:56 +0200 | [diff] [blame] | 928 | self.assertEqual(sock.recv(1024), |
| 929 | LIST_DATA.encode(self.client.encoding)) |
Antoine Pitrou | 2c4f98b | 2010-04-23 00:16:21 +0000 | [diff] [blame] | 930 | self.assertEqual(self.client.voidresp(), "226 transfer complete") |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 931 | |
| 932 | def test_login(self): |
| 933 | # login() is supposed to implicitly secure the control connection |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 934 | self.assertNotIsInstance(self.client.sock, ssl.SSLSocket) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 935 | self.client.login() |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 936 | self.assertIsInstance(self.client.sock, ssl.SSLSocket) |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 937 | # make sure that AUTH TLS doesn't get issued again |
| 938 | self.client.login() |
| 939 | |
| 940 | def test_auth_issued_twice(self): |
| 941 | self.client.auth() |
| 942 | self.assertRaises(ValueError, self.client.auth) |
| 943 | |
Giampaolo Rodolà | a67299e | 2010-05-26 18:06:04 +0000 | [diff] [blame] | 944 | def test_context(self): |
| 945 | self.client.quit() |
Christian Heimes | a170fa1 | 2017-09-15 20:27:30 +0200 | [diff] [blame] | 946 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 947 | ctx.check_hostname = False |
| 948 | ctx.verify_mode = ssl.CERT_NONE |
Giampaolo Rodolà | a67299e | 2010-05-26 18:06:04 +0000 | [diff] [blame] | 949 | self.assertRaises(ValueError, ftplib.FTP_TLS, keyfile=CERTFILE, |
| 950 | context=ctx) |
| 951 | self.assertRaises(ValueError, ftplib.FTP_TLS, certfile=CERTFILE, |
| 952 | context=ctx) |
| 953 | self.assertRaises(ValueError, ftplib.FTP_TLS, certfile=CERTFILE, |
| 954 | keyfile=CERTFILE, context=ctx) |
| 955 | |
Giampaolo Rodola' | 0d4f08c | 2013-05-16 15:12:01 +0200 | [diff] [blame] | 956 | self.client = ftplib.FTP_TLS(context=ctx, timeout=TIMEOUT) |
Giampaolo Rodolà | a67299e | 2010-05-26 18:06:04 +0000 | [diff] [blame] | 957 | self.client.connect(self.server.host, self.server.port) |
| 958 | self.assertNotIsInstance(self.client.sock, ssl.SSLSocket) |
| 959 | self.client.auth() |
| 960 | self.assertIs(self.client.sock.context, ctx) |
| 961 | self.assertIsInstance(self.client.sock, ssl.SSLSocket) |
| 962 | |
| 963 | self.client.prot_p() |
Brett Cannon | 918e2d4 | 2010-10-29 23:26:25 +0000 | [diff] [blame] | 964 | with self.client.transfercmd('list') as sock: |
| 965 | self.assertIs(sock.context, ctx) |
| 966 | self.assertIsInstance(sock, ssl.SSLSocket) |
Giampaolo Rodolà | a67299e | 2010-05-26 18:06:04 +0000 | [diff] [blame] | 967 | |
Giampaolo Rodola' | 096dcb1 | 2011-06-27 11:17:51 +0200 | [diff] [blame] | 968 | def test_ccc(self): |
| 969 | self.assertRaises(ValueError, self.client.ccc) |
| 970 | self.client.login(secure=True) |
| 971 | self.assertIsInstance(self.client.sock, ssl.SSLSocket) |
| 972 | self.client.ccc() |
| 973 | self.assertRaises(ValueError, self.client.sock.unwrap) |
Giampaolo Rodola' | 096dcb1 | 2011-06-27 11:17:51 +0200 | [diff] [blame] | 974 | |
Victor Stinner | 51500f3 | 2018-01-29 13:21:34 +0100 | [diff] [blame] | 975 | @skipUnless(False, "FIXME: bpo-32706") |
Christian Heimes | e5b5edf | 2013-12-02 02:56:02 +0100 | [diff] [blame] | 976 | def test_check_hostname(self): |
| 977 | self.client.quit() |
Christian Heimes | a170fa1 | 2017-09-15 20:27:30 +0200 | [diff] [blame] | 978 | ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) |
| 979 | self.assertEqual(ctx.verify_mode, ssl.CERT_REQUIRED) |
| 980 | self.assertEqual(ctx.check_hostname, True) |
Christian Heimes | e5b5edf | 2013-12-02 02:56:02 +0100 | [diff] [blame] | 981 | ctx.load_verify_locations(CAFILE) |
| 982 | self.client = ftplib.FTP_TLS(context=ctx, timeout=TIMEOUT) |
| 983 | |
| 984 | # 127.0.0.1 doesn't match SAN |
| 985 | self.client.connect(self.server.host, self.server.port) |
| 986 | with self.assertRaises(ssl.CertificateError): |
| 987 | self.client.auth() |
| 988 | # exception quits connection |
| 989 | |
| 990 | self.client.connect(self.server.host, self.server.port) |
| 991 | self.client.prot_p() |
| 992 | with self.assertRaises(ssl.CertificateError): |
| 993 | with self.client.transfercmd("list") as sock: |
| 994 | pass |
| 995 | self.client.quit() |
| 996 | |
| 997 | self.client.connect("localhost", self.server.port) |
| 998 | self.client.auth() |
| 999 | self.client.quit() |
| 1000 | |
| 1001 | self.client.connect("localhost", self.server.port) |
| 1002 | self.client.prot_p() |
| 1003 | with self.client.transfercmd("list") as sock: |
| 1004 | pass |
| 1005 | |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 1006 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 1007 | class TestTimeouts(TestCase): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1008 | |
| 1009 | def setUp(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1010 | self.evt = threading.Event() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1011 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 1012 | self.sock.settimeout(20) |
Serhiy Storchaka | 1699491 | 2020-04-25 10:06:29 +0300 | [diff] [blame] | 1013 | self.port = socket_helper.bind_port(self.sock) |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 1014 | self.server_thread = threading.Thread(target=self.server) |
Christian Heimes | 529525f | 2018-05-23 22:24:45 +0200 | [diff] [blame] | 1015 | self.server_thread.daemon = True |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 1016 | self.server_thread.start() |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 1017 | # Wait for the server to be ready. |
| 1018 | self.evt.wait() |
| 1019 | self.evt.clear() |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 1020 | self.old_port = ftplib.FTP.port |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1021 | ftplib.FTP.port = self.port |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1022 | |
| 1023 | def tearDown(self): |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 1024 | ftplib.FTP.port = self.old_port |
| 1025 | self.server_thread.join() |
Victor Stinner | b157ce1 | 2017-09-13 06:43:58 -0700 | [diff] [blame] | 1026 | # Explicitly clear the attribute to prevent dangling thread |
| 1027 | self.server_thread = None |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1028 | |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 1029 | def server(self): |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 1030 | # This method sets the evt 3 times: |
| 1031 | # 1) when the connection is ready to be accepted. |
| 1032 | # 2) when it is safe for the caller to close the connection |
| 1033 | # 3) when we have closed the socket |
Charles-François Natali | 6e20460 | 2014-07-23 19:28:13 +0100 | [diff] [blame] | 1034 | self.sock.listen() |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 1035 | # (1) Signal the caller that we are ready to accept the connection. |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 1036 | self.evt.set() |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 1037 | try: |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 1038 | conn, addr = self.sock.accept() |
Christian Heimes | 03c8ddd | 2020-11-20 09:26:07 +0100 | [diff] [blame] | 1039 | except TimeoutError: |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 1040 | pass |
| 1041 | else: |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 1042 | conn.sendall(b"1 Hola mundo\n") |
| 1043 | conn.shutdown(socket.SHUT_WR) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 1044 | # (2) Signal the caller that it is safe to close the socket. |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 1045 | self.evt.set() |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 1046 | conn.close() |
| 1047 | finally: |
Antoine Pitrou | 08d0272 | 2012-12-19 20:44:02 +0100 | [diff] [blame] | 1048 | self.sock.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1049 | |
| 1050 | def testTimeoutDefault(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1051 | # default -- use global socket timeout |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 1052 | self.assertIsNone(socket.getdefaulttimeout()) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1053 | socket.setdefaulttimeout(30) |
| 1054 | try: |
Antoine Pitrou | f6fbf56 | 2013-08-22 00:39:46 +0200 | [diff] [blame] | 1055 | ftp = ftplib.FTP(HOST) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1056 | finally: |
| 1057 | socket.setdefaulttimeout(None) |
| 1058 | self.assertEqual(ftp.sock.gettimeout(), 30) |
| 1059 | self.evt.wait() |
| 1060 | ftp.close() |
| 1061 | |
| 1062 | def testTimeoutNone(self): |
| 1063 | # no timeout -- do not use global socket timeout |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 1064 | self.assertIsNone(socket.getdefaulttimeout()) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1065 | socket.setdefaulttimeout(30) |
| 1066 | try: |
Antoine Pitrou | f6fbf56 | 2013-08-22 00:39:46 +0200 | [diff] [blame] | 1067 | ftp = ftplib.FTP(HOST, timeout=None) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1068 | finally: |
| 1069 | socket.setdefaulttimeout(None) |
Serhiy Storchaka | 25d8aea | 2014-02-08 14:50:08 +0200 | [diff] [blame] | 1070 | self.assertIsNone(ftp.sock.gettimeout()) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 1071 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1072 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1073 | |
| 1074 | def testTimeoutValue(self): |
| 1075 | # a value |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1076 | ftp = ftplib.FTP(HOST, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1077 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 1078 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1079 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1080 | |
Dong-hee Na | a190e2a | 2020-01-14 04:34:34 +0900 | [diff] [blame] | 1081 | # bpo-39259 |
| 1082 | with self.assertRaises(ValueError): |
| 1083 | ftplib.FTP(HOST, timeout=0) |
| 1084 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1085 | def testTimeoutConnect(self): |
| 1086 | ftp = ftplib.FTP() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1087 | ftp.connect(HOST, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1088 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 1089 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1090 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1091 | |
| 1092 | def testTimeoutDifferentOrder(self): |
| 1093 | ftp = ftplib.FTP(timeout=30) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1094 | ftp.connect(HOST) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1095 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 1096 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 1097 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1098 | |
| 1099 | def testTimeoutDirectAccess(self): |
| 1100 | ftp = ftplib.FTP() |
| 1101 | ftp.timeout = 30 |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 1102 | ftp.connect(HOST) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1103 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 1104 | self.evt.wait() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1105 | ftp.close() |
| 1106 | |
| 1107 | |
Martin Panter | 19e69c5 | 2015-11-14 12:46:42 +0000 | [diff] [blame] | 1108 | class MiscTestCase(TestCase): |
| 1109 | def test__all__(self): |
Victor Stinner | fbf43f0 | 2020-08-17 07:20:40 +0200 | [diff] [blame] | 1110 | not_exported = { |
| 1111 | 'MSG_OOB', 'FTP_PORT', 'MAXLINE', 'CRLF', 'B_CRLF', 'Error', |
| 1112 | 'parse150', 'parse227', 'parse229', 'parse257', 'print_line', |
| 1113 | 'ftpcp', 'test'} |
| 1114 | support.check__all__(self, ftplib, not_exported=not_exported) |
Martin Panter | 19e69c5 | 2015-11-14 12:46:42 +0000 | [diff] [blame] | 1115 | |
| 1116 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 1117 | def test_main(): |
Berker Peksag | 8f791d3 | 2014-11-01 10:45:57 +0200 | [diff] [blame] | 1118 | tests = [TestFTPClass, TestTimeouts, |
Serhiy Storchaka | 4376763 | 2013-11-03 21:31:38 +0200 | [diff] [blame] | 1119 | TestIPv6Environment, |
Martin Panter | 19e69c5 | 2015-11-14 12:46:42 +0000 | [diff] [blame] | 1120 | TestTLS_FTPClassMixin, TestTLS_FTPClass, |
| 1121 | MiscTestCase] |
Antoine Pitrou | f988cd0 | 2009-11-17 20:21:14 +0000 | [diff] [blame] | 1122 | |
Hai Shi | e80697d | 2020-05-28 06:10:27 +0800 | [diff] [blame] | 1123 | thread_info = threading_helper.threading_setup() |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 1124 | try: |
| 1125 | support.run_unittest(*tests) |
| 1126 | finally: |
Hai Shi | e80697d | 2020-05-28 06:10:27 +0800 | [diff] [blame] | 1127 | threading_helper.threading_cleanup(*thread_info) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 1128 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1129 | |
| 1130 | if __name__ == '__main__': |
| 1131 | test_main() |