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) |
| 217 | |
| 218 | def writable(self): |
| 219 | return 0 |
| 220 | |
| 221 | def handle_error(self): |
| 222 | raise |
| 223 | |
| 224 | |
| 225 | class TestFTPClass(TestCase): |
| 226 | |
| 227 | def setUp(self): |
| 228 | self.server = DummyFTPServer((HOST, 0)) |
| 229 | self.server.start() |
| 230 | self.client = ftplib.FTP(timeout=2) |
| 231 | self.client.connect(self.server.host, self.server.port) |
| 232 | |
| 233 | def tearDown(self): |
| 234 | self.client.close() |
| 235 | self.server.stop() |
| 236 | |
| 237 | def test_getwelcome(self): |
| 238 | self.assertEqual(self.client.getwelcome(), '220 welcome') |
| 239 | |
| 240 | def test_sanitize(self): |
| 241 | self.assertEqual(self.client.sanitize('foo'), repr('foo')) |
| 242 | self.assertEqual(self.client.sanitize('pass 12345'), repr('pass *****')) |
| 243 | self.assertEqual(self.client.sanitize('PASS 12345'), repr('PASS *****')) |
| 244 | |
| 245 | def test_exceptions(self): |
| 246 | self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 400') |
| 247 | self.assertRaises(ftplib.error_temp, self.client.sendcmd, 'echo 499') |
| 248 | self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 500') |
| 249 | self.assertRaises(ftplib.error_perm, self.client.sendcmd, 'echo 599') |
| 250 | self.assertRaises(ftplib.error_proto, self.client.sendcmd, 'echo 999') |
| 251 | |
| 252 | def test_all_errors(self): |
| 253 | exceptions = (ftplib.error_reply, ftplib.error_temp, ftplib.error_perm, |
| 254 | ftplib.error_proto, ftplib.Error, IOError, EOFError) |
| 255 | for x in exceptions: |
| 256 | try: |
| 257 | raise x('exception not included in all_errors set') |
| 258 | except ftplib.all_errors: |
| 259 | pass |
| 260 | |
| 261 | def test_set_pasv(self): |
| 262 | # passive mode is supposed to be enabled by default |
| 263 | self.assertTrue(self.client.passiveserver) |
| 264 | self.client.set_pasv(True) |
| 265 | self.assertTrue(self.client.passiveserver) |
| 266 | self.client.set_pasv(False) |
| 267 | self.assertFalse(self.client.passiveserver) |
| 268 | |
| 269 | def test_voidcmd(self): |
| 270 | self.client.voidcmd('echo 200') |
| 271 | self.client.voidcmd('echo 299') |
| 272 | self.assertRaises(ftplib.error_reply, self.client.voidcmd, 'echo 199') |
| 273 | self.assertRaises(ftplib.error_reply, self.client.voidcmd, 'echo 300') |
| 274 | |
| 275 | def test_login(self): |
| 276 | self.client.login() |
| 277 | |
| 278 | def test_acct(self): |
| 279 | self.client.acct('passwd') |
| 280 | |
| 281 | def test_rename(self): |
| 282 | self.client.rename('a', 'b') |
| 283 | self.server.handler.next_response = '200' |
| 284 | self.assertRaises(ftplib.error_reply, self.client.rename, 'a', 'b') |
| 285 | |
| 286 | def test_delete(self): |
| 287 | self.client.delete('foo') |
| 288 | self.server.handler.next_response = '199' |
| 289 | self.assertRaises(ftplib.error_reply, self.client.delete, 'foo') |
| 290 | |
| 291 | def test_size(self): |
| 292 | self.client.size('foo') |
| 293 | |
| 294 | def test_mkd(self): |
| 295 | dir = self.client.mkd('/foo') |
| 296 | self.assertEqual(dir, '/foo') |
| 297 | |
| 298 | def test_rmd(self): |
| 299 | self.client.rmd('foo') |
| 300 | |
| 301 | def test_pwd(self): |
| 302 | dir = self.client.pwd() |
| 303 | self.assertEqual(dir, 'pwd ok') |
| 304 | |
| 305 | def test_quit(self): |
| 306 | self.assertEqual(self.client.quit(), '221 quit ok') |
| 307 | # Ensure the connection gets closed; sock attribute should be None |
| 308 | self.assertEqual(self.client.sock, None) |
| 309 | |
| 310 | def test_retrbinary(self): |
| 311 | def callback(data): |
| 312 | received.append(data.decode('ascii')) |
| 313 | received = [] |
| 314 | self.client.retrbinary('retr', callback) |
| 315 | self.assertEqual(''.join(received), RETR_DATA) |
| 316 | |
| 317 | def test_retrlines(self): |
| 318 | received = [] |
| 319 | self.client.retrlines('retr', received.append) |
| 320 | self.assertEqual(''.join(received), RETR_DATA.replace('\r\n', '')) |
| 321 | |
| 322 | def test_storbinary(self): |
| 323 | f = io.BytesIO(RETR_DATA.encode('ascii')) |
| 324 | self.client.storbinary('stor', f) |
| 325 | self.assertEqual(self.server.handler.last_received_data, RETR_DATA) |
| 326 | # test new callback arg |
| 327 | flag = [] |
| 328 | f.seek(0) |
| 329 | self.client.storbinary('stor', f, callback=lambda x: flag.append(None)) |
| 330 | self.assertTrue(flag) |
| 331 | |
| 332 | def test_storlines(self): |
| 333 | f = io.BytesIO(RETR_DATA.replace('\r\n', '\n').encode('ascii')) |
| 334 | self.client.storlines('stor', f) |
| 335 | self.assertEqual(self.server.handler.last_received_data, RETR_DATA) |
| 336 | # test new callback arg |
| 337 | flag = [] |
| 338 | f.seek(0) |
| 339 | self.client.storlines('stor foo', f, callback=lambda x: flag.append(None)) |
| 340 | self.assertTrue(flag) |
| 341 | |
| 342 | def test_nlst(self): |
| 343 | self.client.nlst() |
| 344 | self.assertEqual(self.client.nlst(), NLST_DATA.split('\r\n')[:-1]) |
| 345 | |
| 346 | def test_dir(self): |
| 347 | l = [] |
| 348 | self.client.dir(lambda x: l.append(x)) |
| 349 | self.assertEqual(''.join(l), LIST_DATA.replace('\r\n', '')) |
| 350 | |
Benjamin Peterson | 3a53fbb | 2008-09-27 22:04:16 +0000 | [diff] [blame^] | 351 | def test_makeport(self): |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 352 | self.client.makeport() |
| 353 | # IPv4 is in use, just make sure send_eprt has not been used |
| 354 | self.assertEqual(self.server.handler.last_received_cmd, 'port') |
| 355 | |
| 356 | def test_makepasv(self): |
| 357 | host, port = self.client.makepasv() |
| 358 | conn = socket.create_connection((host, port), 2) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 359 | conn.close() |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 360 | # IPv4 is in use, just make sure send_epsv has not been used |
| 361 | self.assertEqual(self.server.handler.last_received_cmd, 'pasv') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 362 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 363 | |
| 364 | class TestIPv6Environment(TestCase): |
| 365 | |
| 366 | def setUp(self): |
| 367 | self.server = DummyFTPServer((HOST, 0), af=socket.AF_INET6) |
| 368 | self.server.start() |
| 369 | self.client = ftplib.FTP() |
| 370 | self.client.connect(self.server.host, self.server.port) |
| 371 | |
| 372 | def tearDown(self): |
| 373 | self.client.close() |
| 374 | self.server.stop() |
| 375 | |
| 376 | def test_af(self): |
| 377 | self.assertEqual(self.client.af, socket.AF_INET6) |
| 378 | |
| 379 | def test_makeport(self): |
| 380 | self.client.makeport() |
| 381 | self.assertEqual(self.server.handler.last_received_cmd, 'eprt') |
| 382 | |
| 383 | def test_makepasv(self): |
| 384 | host, port = self.client.makepasv() |
| 385 | conn = socket.create_connection((host, port), 2) |
| 386 | conn.close() |
| 387 | self.assertEqual(self.server.handler.last_received_cmd, 'epsv') |
| 388 | |
| 389 | def test_transfer(self): |
| 390 | def retr(): |
| 391 | def callback(data): |
| 392 | received.append(data.decode('ascii')) |
| 393 | received = [] |
| 394 | self.client.retrbinary('retr', callback) |
| 395 | self.assertEqual(''.join(received), RETR_DATA) |
| 396 | self.client.set_pasv(True) |
| 397 | retr() |
| 398 | self.client.set_pasv(False) |
| 399 | retr() |
| 400 | |
| 401 | |
| 402 | class TestTimeouts(TestCase): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 403 | |
| 404 | def setUp(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 405 | self.evt = threading.Event() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 406 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 407 | self.sock.settimeout(3) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 408 | self.port = support.bind_port(self.sock) |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 409 | threading.Thread(target=self.server, args=(self.evt,self.sock)).start() |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 410 | # Wait for the server to be ready. |
| 411 | self.evt.wait() |
| 412 | self.evt.clear() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 413 | ftplib.FTP.port = self.port |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 414 | |
| 415 | def tearDown(self): |
| 416 | self.evt.wait() |
| 417 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 418 | def server(self, evt, serv): |
| 419 | # This method sets the evt 3 times: |
| 420 | # 1) when the connection is ready to be accepted. |
| 421 | # 2) when it is safe for the caller to close the connection |
| 422 | # 3) when we have closed the socket |
| 423 | serv.listen(5) |
| 424 | # (1) Signal the caller that we are ready to accept the connection. |
| 425 | evt.set() |
| 426 | try: |
| 427 | conn, addr = serv.accept() |
| 428 | except socket.timeout: |
| 429 | pass |
| 430 | else: |
| 431 | conn.send(b"1 Hola mundo\n") |
| 432 | # (2) Signal the caller that it is safe to close the socket. |
| 433 | evt.set() |
| 434 | conn.close() |
| 435 | finally: |
| 436 | serv.close() |
| 437 | # (3) Signal the caller that we are done. |
| 438 | evt.set() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 439 | |
| 440 | def testTimeoutDefault(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 441 | # default -- use global socket timeout |
| 442 | self.assert_(socket.getdefaulttimeout() is None) |
| 443 | socket.setdefaulttimeout(30) |
| 444 | try: |
| 445 | ftp = ftplib.FTP("localhost") |
| 446 | finally: |
| 447 | socket.setdefaulttimeout(None) |
| 448 | self.assertEqual(ftp.sock.gettimeout(), 30) |
| 449 | self.evt.wait() |
| 450 | ftp.close() |
| 451 | |
| 452 | def testTimeoutNone(self): |
| 453 | # no timeout -- do not use global socket timeout |
| 454 | self.assert_(socket.getdefaulttimeout() is None) |
| 455 | socket.setdefaulttimeout(30) |
| 456 | try: |
| 457 | ftp = ftplib.FTP("localhost", timeout=None) |
| 458 | finally: |
| 459 | socket.setdefaulttimeout(None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 460 | self.assertTrue(ftp.sock.gettimeout() is None) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 461 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 462 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 463 | |
| 464 | def testTimeoutValue(self): |
| 465 | # a value |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 466 | ftp = ftplib.FTP(HOST, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 467 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 468 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 469 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 470 | |
| 471 | def testTimeoutConnect(self): |
| 472 | ftp = ftplib.FTP() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 473 | ftp.connect(HOST, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 474 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 475 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 476 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 477 | |
| 478 | def testTimeoutDifferentOrder(self): |
| 479 | ftp = ftplib.FTP(timeout=30) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 480 | ftp.connect(HOST) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 481 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 482 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 483 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 484 | |
| 485 | def testTimeoutDirectAccess(self): |
| 486 | ftp = ftplib.FTP() |
| 487 | ftp.timeout = 30 |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 488 | ftp.connect(HOST) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 489 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 490 | self.evt.wait() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 491 | ftp.close() |
| 492 | |
| 493 | |
Benjamin Peterson | be17a11 | 2008-09-27 21:49:47 +0000 | [diff] [blame] | 494 | def test_main(): |
| 495 | tests = [TestFTPClass, TestTimeouts] |
| 496 | if socket.has_ipv6: |
| 497 | try: |
| 498 | DummyFTPServer((HOST, 0), af=socket.AF_INET6) |
| 499 | except socket.error: |
| 500 | pass |
| 501 | else: |
| 502 | tests.append(TestIPv6Environment) |
| 503 | thread_info = support.threading_setup() |
| 504 | try: |
| 505 | support.run_unittest(*tests) |
| 506 | finally: |
| 507 | support.threading_cleanup(*thread_info) |
| 508 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 509 | |
| 510 | if __name__ == '__main__': |
| 511 | test_main() |