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