Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 1 | """Test script for ftplib module.""" |
| 2 | |
| 3 | # Modified by Giampaolo Rodola' to test FTP class and IPv6 environment |
| 4 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 5 | import ftplib |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 6 | import threading |
| 7 | import asyncore |
| 8 | import asynchat |
| 9 | import socket |
| 10 | import io |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 11 | |
| 12 | from unittest import TestCase |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 13 | from test import support |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 14 | from test.support import HOST |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 15 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 16 | # the dummy data returned by server over the data channel when |
| 17 | # RETR, LIST and NLST commands are issued |
| 18 | RETR_DATA = 'abcde12345\r\n' * 1000 |
| 19 | LIST_DATA = 'foo\r\nbar\r\n' |
| 20 | NLST_DATA = 'foo\r\nbar\r\n' |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 21 | |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 22 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 23 | class DummyDTPHandler(asynchat.async_chat): |
| 24 | |
| 25 | def __init__(self, conn, baseclass): |
| 26 | asynchat.async_chat.__init__(self, conn) |
| 27 | self.baseclass = baseclass |
| 28 | self.baseclass.last_received_data = '' |
| 29 | |
| 30 | def handle_read(self): |
| 31 | self.baseclass.last_received_data += self.recv(1024).decode('ascii') |
| 32 | |
| 33 | def handle_close(self): |
| 34 | self.baseclass.push('226 transfer complete') |
| 35 | self.close() |
| 36 | |
| 37 | def push(self, what): |
| 38 | super(DummyDTPHandler, self).push(what.encode('ascii')) |
| 39 | |
| 40 | |
| 41 | class DummyFTPHandler(asynchat.async_chat): |
| 42 | |
| 43 | def __init__(self, conn): |
| 44 | asynchat.async_chat.__init__(self, conn) |
| 45 | self.set_terminator(b"\r\n") |
| 46 | self.in_buffer = [] |
| 47 | self.dtp = None |
| 48 | self.last_received_cmd = None |
| 49 | self.last_received_data = '' |
| 50 | self.next_response = '' |
| 51 | self.push('220 welcome') |
| 52 | |
| 53 | def collect_incoming_data(self, data): |
| 54 | self.in_buffer.append(data) |
| 55 | |
| 56 | def found_terminator(self): |
| 57 | line = b''.join(self.in_buffer).decode('ascii') |
| 58 | self.in_buffer = [] |
| 59 | if self.next_response: |
| 60 | self.push(self.next_response) |
| 61 | self.next_response = '' |
| 62 | cmd = line.split(' ')[0].lower() |
| 63 | self.last_received_cmd = cmd |
| 64 | space = line.find(' ') |
| 65 | if space != -1: |
| 66 | arg = line[space + 1:] |
| 67 | else: |
| 68 | arg = "" |
| 69 | if hasattr(self, 'cmd_' + cmd): |
| 70 | method = getattr(self, 'cmd_' + cmd) |
| 71 | method(arg) |
| 72 | else: |
| 73 | self.push('550 command "%s" not understood.' %cmd) |
| 74 | |
| 75 | def handle_error(self): |
| 76 | raise |
| 77 | |
| 78 | def push(self, data): |
| 79 | asynchat.async_chat.push(self, data.encode('ascii') + b'\r\n') |
| 80 | |
| 81 | def cmd_port(self, arg): |
| 82 | addr = list(map(int, arg.split(','))) |
| 83 | ip = '%d.%d.%d.%d' %tuple(addr[:4]) |
| 84 | port = (addr[4] * 256) + addr[5] |
| 85 | s = socket.create_connection((ip, port), timeout=2) |
| 86 | self.dtp = DummyDTPHandler(s, baseclass=self) |
| 87 | self.push('200 active data connection established') |
| 88 | |
| 89 | def cmd_pasv(self, arg): |
| 90 | sock = socket.socket() |
| 91 | sock.bind((self.socket.getsockname()[0], 0)) |
| 92 | sock.listen(5) |
| 93 | sock.settimeout(2) |
| 94 | ip, port = sock.getsockname()[:2] |
| 95 | ip = ip.replace('.', ','); p1 = port / 256; p2 = port % 256 |
| 96 | self.push('227 entering passive mode (%s,%d,%d)' %(ip, p1, p2)) |
| 97 | conn, addr = sock.accept() |
| 98 | self.dtp = DummyDTPHandler(conn, baseclass=self) |
| 99 | |
| 100 | def cmd_eprt(self, arg): |
| 101 | af, ip, port = arg.split(arg[0])[1:-1] |
| 102 | port = int(port) |
| 103 | s = socket.create_connection((ip, port), timeout=2) |
| 104 | self.dtp = DummyDTPHandler(s, baseclass=self) |
| 105 | self.push('200 active data connection established') |
| 106 | |
| 107 | def cmd_epsv(self, arg): |
| 108 | sock = socket.socket(socket.AF_INET6) |
| 109 | sock.bind((self.socket.getsockname()[0], 0)) |
| 110 | sock.listen(5) |
| 111 | sock.settimeout(2) |
| 112 | port = sock.getsockname()[1] |
| 113 | self.push('229 entering extended passive mode (|||%d|)' %port) |
| 114 | conn, addr = sock.accept() |
| 115 | self.dtp = DummyDTPHandler(conn, baseclass=self) |
| 116 | |
| 117 | def cmd_echo(self, arg): |
| 118 | # sends back the received string (used by the test suite) |
| 119 | self.push(arg) |
| 120 | |
| 121 | def cmd_user(self, arg): |
| 122 | self.push('331 username ok') |
| 123 | |
| 124 | def cmd_pass(self, arg): |
| 125 | self.push('230 password ok') |
| 126 | |
| 127 | def cmd_acct(self, arg): |
| 128 | self.push('230 acct ok') |
| 129 | |
| 130 | def cmd_rnfr(self, arg): |
| 131 | self.push('350 rnfr ok') |
| 132 | |
| 133 | def cmd_rnto(self, arg): |
| 134 | self.push('250 rnto ok') |
| 135 | |
| 136 | def cmd_dele(self, arg): |
| 137 | self.push('250 dele ok') |
| 138 | |
| 139 | def cmd_cwd(self, arg): |
| 140 | self.push('250 cwd ok') |
| 141 | |
| 142 | def cmd_size(self, arg): |
| 143 | self.push('250 1000') |
| 144 | |
| 145 | def cmd_mkd(self, arg): |
| 146 | self.push('257 "%s"' %arg) |
| 147 | |
| 148 | def cmd_rmd(self, arg): |
| 149 | self.push('250 rmd ok') |
| 150 | |
| 151 | def cmd_pwd(self, arg): |
| 152 | self.push('257 "pwd ok"') |
| 153 | |
| 154 | def cmd_type(self, arg): |
| 155 | self.push('200 type ok') |
| 156 | |
| 157 | def cmd_quit(self, arg): |
| 158 | self.push('221 quit ok') |
| 159 | self.close() |
| 160 | |
| 161 | def cmd_stor(self, arg): |
| 162 | self.push('125 stor ok') |
| 163 | |
| 164 | def cmd_retr(self, arg): |
| 165 | self.push('125 retr ok') |
| 166 | self.dtp.push(RETR_DATA) |
| 167 | self.dtp.close_when_done() |
| 168 | |
| 169 | def cmd_list(self, arg): |
| 170 | self.push('125 list ok') |
| 171 | self.dtp.push(LIST_DATA) |
| 172 | self.dtp.close_when_done() |
| 173 | |
| 174 | def cmd_nlst(self, arg): |
| 175 | self.push('125 nlst ok') |
| 176 | self.dtp.push(NLST_DATA) |
| 177 | self.dtp.close_when_done() |
| 178 | |
| 179 | |
| 180 | class DummyFTPServer(asyncore.dispatcher, threading.Thread): |
| 181 | |
| 182 | handler = DummyFTPHandler |
| 183 | |
| 184 | def __init__(self, address, af=socket.AF_INET): |
| 185 | threading.Thread.__init__(self) |
| 186 | asyncore.dispatcher.__init__(self) |
| 187 | self.create_socket(af, socket.SOCK_STREAM) |
| 188 | self.bind(address) |
| 189 | self.listen(5) |
| 190 | self.active = False |
| 191 | self.active_lock = threading.Lock() |
| 192 | self.host, self.port = self.socket.getsockname()[:2] |
| 193 | |
| 194 | def start(self): |
| 195 | assert not self.active |
| 196 | self.__flag = threading.Event() |
| 197 | threading.Thread.start(self) |
| 198 | self.__flag.wait() |
| 199 | |
| 200 | def run(self): |
| 201 | self.active = True |
| 202 | self.__flag.set() |
| 203 | while self.active and asyncore.socket_map: |
| 204 | self.active_lock.acquire() |
| 205 | asyncore.loop(timeout=0.1, count=1) |
| 206 | self.active_lock.release() |
| 207 | asyncore.close_all(ignore_all=True) |
| 208 | |
| 209 | def stop(self): |
| 210 | assert self.active |
| 211 | self.active = False |
| 212 | self.join() |
| 213 | |
| 214 | def handle_accept(self): |
| 215 | conn, addr = self.accept() |
| 216 | self.handler = self.handler(conn) |
Benjamin Peterson | d06e3b0 | 2008-09-28 21:00:42 +0000 | [diff] [blame] | 217 | self.close() |
| 218 | |
| 219 | def handle_connect(self): |
| 220 | self.close() |
| 221 | handle_read = handle_connect |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 222 | |
| 223 | def writable(self): |
| 224 | return 0 |
| 225 | |
| 226 | def handle_error(self): |
| 227 | raise |
| 228 | |
| 229 | |
| 230 | class TestFTPClass(TestCase): |
| 231 | |
| 232 | def setUp(self): |
| 233 | self.server = DummyFTPServer((HOST, 0)) |
| 234 | self.server.start() |
| 235 | self.client = ftplib.FTP(timeout=2) |
| 236 | self.client.connect(self.server.host, self.server.port) |
| 237 | |
| 238 | def tearDown(self): |
| 239 | self.client.close() |
| 240 | self.server.stop() |
| 241 | |
| 242 | def test_getwelcome(self): |
| 243 | self.assertEqual(self.client.getwelcome(), '220 welcome') |
| 244 | |
| 245 | def test_sanitize(self): |
| 246 | self.assertEqual(self.client.sanitize('foo'), repr('foo')) |
| 247 | self.assertEqual(self.client.sanitize('pass 12345'), repr('pass *****')) |
| 248 | self.assertEqual(self.client.sanitize('PASS 12345'), repr('PASS *****')) |
| 249 | |
| 250 | def test_exceptions(self): |
| 251 | self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 400') |
| 252 | self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 499') |
| 253 | self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 500') |
| 254 | self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 599') |
| 255 | self.assertRaises(ftplib.error_proto, self.client.sendcmd, 'echo 999') |
| 256 | |
| 257 | def test_all_errors(self): |
| 258 | exceptions = (ftplib.error_reply, ftplib.error_temp, ftplib.error_perm, |
| 259 | ftplib.error_proto, ftplib.Error, IOError, EOFError) |
| 260 | for x in exceptions: |
| 261 | try: |
| 262 | raise x('exception not included in all_errors set') |
| 263 | except ftplib.all_errors: |
| 264 | pass |
| 265 | |
| 266 | def test_set_pasv(self): |
| 267 | # passive mode is supposed to be enabled by default |
| 268 | self.assertTrue(self.client.passiveserver) |
| 269 | self.client.set_pasv(True) |
| 270 | self.assertTrue(self.client.passiveserver) |
| 271 | self.client.set_pasv(False) |
| 272 | self.assertFalse(self.client.passiveserver) |
| 273 | |
| 274 | def test_voidcmd(self): |
| 275 | self.client.voidcmd('echo 200') |
| 276 | self.client.voidcmd('echo 299') |
| 277 | self.assertRaises(ftplib.error_reply, self.client.voidcmd, 'echo 199') |
| 278 | self.assertRaises(ftplib.error_reply, self.client.voidcmd, 'echo 300') |
| 279 | |
| 280 | def test_login(self): |
| 281 | self.client.login() |
| 282 | |
| 283 | def test_acct(self): |
| 284 | self.client.acct('passwd') |
| 285 | |
| 286 | def test_rename(self): |
| 287 | self.client.rename('a', 'b') |
| 288 | self.server.handler.next_response = '200' |
| 289 | self.assertRaises(ftplib.error_reply, self.client.rename, 'a', 'b') |
| 290 | |
| 291 | def test_delete(self): |
| 292 | self.client.delete('foo') |
| 293 | self.server.handler.next_response = '199' |
| 294 | self.assertRaises(ftplib.error_reply, self.client.delete, 'foo') |
| 295 | |
| 296 | def test_size(self): |
| 297 | self.client.size('foo') |
| 298 | |
| 299 | def test_mkd(self): |
| 300 | dir = self.client.mkd('/foo') |
| 301 | self.assertEqual(dir, '/foo') |
| 302 | |
| 303 | def test_rmd(self): |
| 304 | self.client.rmd('foo') |
| 305 | |
| 306 | def test_pwd(self): |
| 307 | dir = self.client.pwd() |
| 308 | self.assertEqual(dir, 'pwd ok') |
| 309 | |
| 310 | def test_quit(self): |
| 311 | self.assertEqual(self.client.quit(), '221 quit ok') |
| 312 | # Ensure the connection gets closed; sock attribute should be None |
| 313 | self.assertEqual(self.client.sock, None) |
| 314 | |
| 315 | def test_retrbinary(self): |
| 316 | def callback(data): |
| 317 | received.append(data.decode('ascii')) |
| 318 | received = [] |
| 319 | self.client.retrbinary('retr', callback) |
| 320 | self.assertEqual(''.join(received), RETR_DATA) |
| 321 | |
| 322 | def test_retrlines(self): |
| 323 | received = [] |
| 324 | self.client.retrlines('retr', received.append) |
| 325 | self.assertEqual(''.join(received), RETR_DATA.replace('\r\n', '')) |
| 326 | |
| 327 | def test_storbinary(self): |
| 328 | f = io.BytesIO(RETR_DATA.encode('ascii')) |
| 329 | self.client.storbinary('stor', f) |
| 330 | self.assertEqual(self.server.handler.last_received_data, RETR_DATA) |
| 331 | # test new callback arg |
| 332 | flag = [] |
| 333 | f.seek(0) |
| 334 | self.client.storbinary('stor', f, callback=lambda x: flag.append(None)) |
| 335 | self.assertTrue(flag) |
| 336 | |
| 337 | def test_storlines(self): |
| 338 | f = io.BytesIO(RETR_DATA.replace('\r\n', '\n').encode('ascii')) |
| 339 | self.client.storlines('stor', f) |
| 340 | self.assertEqual(self.server.handler.last_received_data, RETR_DATA) |
| 341 | # test new callback arg |
| 342 | flag = [] |
| 343 | f.seek(0) |
| 344 | self.client.storlines('stor foo', f, callback=lambda x: flag.append(None)) |
| 345 | self.assertTrue(flag) |
| 346 | |
| 347 | def test_nlst(self): |
| 348 | self.client.nlst() |
| 349 | self.assertEqual(self.client.nlst(), NLST_DATA.split('\r\n')[:-1]) |
| 350 | |
| 351 | def test_dir(self): |
| 352 | l = [] |
| 353 | self.client.dir(lambda x: l.append(x)) |
| 354 | self.assertEqual(''.join(l), LIST_DATA.replace('\r\n', '')) |
| 355 | |
Benjamin Peterson | 3a53fbb | 2008-09-27 22:04:16 +0000 | [diff] [blame] | 356 | def test_makeport(self): |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 357 | self.client.makeport() |
| 358 | # IPv4 is in use, just make sure send_eprt has not been used |
| 359 | self.assertEqual(self.server.handler.last_received_cmd, 'port') |
| 360 | |
| 361 | def test_makepasv(self): |
| 362 | host, port = self.client.makepasv() |
| 363 | conn = socket.create_connection((host, port), 2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 364 | conn.close() |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 365 | # IPv4 is in use, just make sure send_epsv has not been used |
| 366 | self.assertEqual(self.server.handler.last_received_cmd, 'pasv') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 367 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 368 | |
| 369 | class TestIPv6Environment(TestCase): |
| 370 | |
| 371 | def setUp(self): |
| 372 | self.server = DummyFTPServer((HOST, 0), af=socket.AF_INET6) |
| 373 | self.server.start() |
| 374 | self.client = ftplib.FTP() |
| 375 | self.client.connect(self.server.host, self.server.port) |
| 376 | |
| 377 | def tearDown(self): |
| 378 | self.client.close() |
| 379 | self.server.stop() |
| 380 | |
| 381 | def test_af(self): |
| 382 | self.assertEqual(self.client.af, socket.AF_INET6) |
| 383 | |
| 384 | def test_makeport(self): |
| 385 | self.client.makeport() |
| 386 | self.assertEqual(self.server.handler.last_received_cmd, 'eprt') |
| 387 | |
| 388 | def test_makepasv(self): |
| 389 | host, port = self.client.makepasv() |
| 390 | conn = socket.create_connection((host, port), 2) |
| 391 | conn.close() |
| 392 | self.assertEqual(self.server.handler.last_received_cmd, 'epsv') |
| 393 | |
| 394 | def test_transfer(self): |
| 395 | def retr(): |
| 396 | def callback(data): |
| 397 | received.append(data.decode('ascii')) |
| 398 | received = [] |
| 399 | self.client.retrbinary('retr', callback) |
| 400 | self.assertEqual(''.join(received), RETR_DATA) |
| 401 | self.client.set_pasv(True) |
| 402 | retr() |
| 403 | self.client.set_pasv(False) |
| 404 | retr() |
| 405 | |
| 406 | |
| 407 | class TestTimeouts(TestCase): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 408 | |
| 409 | def setUp(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 410 | self.evt = threading.Event() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 411 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 412 | self.sock.settimeout(3) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 413 | self.port = support.bind_port(self.sock) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 414 | threading.Thread(target=self.server, args=(self.evt,self.sock)).start() |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 415 | # Wait for the server to be ready. |
| 416 | self.evt.wait() |
| 417 | self.evt.clear() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 418 | ftplib.FTP.port = self.port |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 419 | |
| 420 | def tearDown(self): |
| 421 | self.evt.wait() |
| 422 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 423 | def server(self, evt, serv): |
| 424 | # This method sets the evt 3 times: |
| 425 | # 1) when the connection is ready to be accepted. |
| 426 | # 2) when it is safe for the caller to close the connection |
| 427 | # 3) when we have closed the socket |
| 428 | serv.listen(5) |
| 429 | # (1) Signal the caller that we are ready to accept the connection. |
| 430 | evt.set() |
| 431 | try: |
| 432 | conn, addr = serv.accept() |
| 433 | except socket.timeout: |
| 434 | pass |
| 435 | else: |
| 436 | conn.send(b"1 Hola mundo\n") |
| 437 | # (2) Signal the caller that it is safe to close the socket. |
| 438 | evt.set() |
| 439 | conn.close() |
| 440 | finally: |
| 441 | serv.close() |
| 442 | # (3) Signal the caller that we are done. |
| 443 | evt.set() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 444 | |
| 445 | def testTimeoutDefault(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 446 | # default -- use global socket timeout |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 447 | self.assertTrue(socket.getdefaulttimeout() is None) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 448 | socket.setdefaulttimeout(30) |
| 449 | try: |
| 450 | ftp = ftplib.FTP("localhost") |
| 451 | finally: |
| 452 | socket.setdefaulttimeout(None) |
| 453 | self.assertEqual(ftp.sock.gettimeout(), 30) |
| 454 | self.evt.wait() |
| 455 | ftp.close() |
| 456 | |
| 457 | def testTimeoutNone(self): |
| 458 | # no timeout -- do not use global socket timeout |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 459 | self.assertTrue(socket.getdefaulttimeout() is None) |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 460 | socket.setdefaulttimeout(30) |
| 461 | try: |
| 462 | ftp = ftplib.FTP("localhost", timeout=None) |
| 463 | finally: |
| 464 | socket.setdefaulttimeout(None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 465 | self.assertTrue(ftp.sock.gettimeout() is None) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 466 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 467 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 468 | |
| 469 | def testTimeoutValue(self): |
| 470 | # a value |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 471 | ftp = ftplib.FTP(HOST, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 472 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 473 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 474 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 475 | |
| 476 | def testTimeoutConnect(self): |
| 477 | ftp = ftplib.FTP() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 478 | ftp.connect(HOST, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 479 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 480 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 481 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 482 | |
| 483 | def testTimeoutDifferentOrder(self): |
| 484 | ftp = ftplib.FTP(timeout=30) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 485 | ftp.connect(HOST) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 486 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 487 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 488 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 489 | |
| 490 | def testTimeoutDirectAccess(self): |
| 491 | ftp = ftplib.FTP() |
| 492 | ftp.timeout = 30 |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 493 | ftp.connect(HOST) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 494 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 495 | self.evt.wait() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 496 | ftp.close() |
| 497 | |
| 498 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 499 | def test_main(): |
| 500 | tests = [TestFTPClass, TestTimeouts] |
| 501 | if socket.has_ipv6: |
| 502 | try: |
| 503 | DummyFTPServer((HOST, 0), af=socket.AF_INET6) |
| 504 | except socket.error: |
| 505 | pass |
| 506 | else: |
| 507 | tests.append(TestIPv6Environment) |
| 508 | thread_info = support.threading_setup() |
| 509 | try: |
| 510 | support.run_unittest(*tests) |
| 511 | finally: |
| 512 | support.threading_cleanup(*thread_info) |
| 513 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 514 | |
| 515 | if __name__ == '__main__': |
| 516 | test_main() |