Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1 | import socket |
| 2 | import threading |
| 3 | import ftplib |
| 4 | import time |
| 5 | |
| 6 | from unittest import TestCase |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test import support |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 8 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | HOST = support.HOST |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 10 | |
| 11 | # This function sets the evt 3 times: |
| 12 | # 1) when the connection is ready to be accepted. |
| 13 | # 2) when it is safe for the caller to close the connection |
| 14 | # 3) when we have closed the socket |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 15 | def server(evt, serv): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 16 | serv.listen(5) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 17 | |
| 18 | # (1) Signal the caller that we are ready to accept the connection. |
| 19 | evt.set() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 20 | try: |
| 21 | conn, addr = serv.accept() |
| 22 | except socket.timeout: |
| 23 | pass |
| 24 | else: |
Guido van Rossum | 8a392d7 | 2007-11-21 22:09:45 +0000 | [diff] [blame] | 25 | conn.send(b"1 Hola mundo\n") |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 26 | # (2) Signal the caller that it is safe to close the socket. |
| 27 | evt.set() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 28 | conn.close() |
| 29 | finally: |
| 30 | serv.close() |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 31 | # (3) Signal the caller that we are done. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 32 | evt.set() |
| 33 | |
| 34 | class GeneralTests(TestCase): |
| 35 | |
| 36 | def setUp(self): |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 37 | self.evt = threading.Event() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 38 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 39 | self.sock.settimeout(3) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 40 | self.port = support.bind_port(self.sock) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 41 | threading.Thread(target=server, args=(self.evt,self.sock)).start() |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 42 | # Wait for the server to be ready. |
| 43 | self.evt.wait() |
| 44 | self.evt.clear() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 45 | ftplib.FTP.port = self.port |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 46 | |
| 47 | def tearDown(self): |
| 48 | self.evt.wait() |
| 49 | |
| 50 | def testBasic(self): |
| 51 | # do nothing |
| 52 | ftplib.FTP() |
| 53 | |
| 54 | # connects |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 55 | ftp = ftplib.FTP(HOST) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 56 | self.evt.wait() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 57 | ftp.sock.close() |
| 58 | |
| 59 | def testTimeoutDefault(self): |
| 60 | # default |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 61 | ftp = ftplib.FTP(HOST) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 62 | self.assertTrue(ftp.sock.gettimeout() is None) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 63 | self.evt.wait() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 64 | ftp.sock.close() |
| 65 | |
| 66 | def testTimeoutValue(self): |
| 67 | # a value |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 68 | ftp = ftplib.FTP(HOST, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 69 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 70 | self.evt.wait() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 71 | ftp.sock.close() |
| 72 | |
| 73 | def testTimeoutConnect(self): |
| 74 | ftp = ftplib.FTP() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 75 | ftp.connect(HOST, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 76 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 77 | self.evt.wait() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 78 | ftp.sock.close() |
| 79 | |
| 80 | def testTimeoutDifferentOrder(self): |
| 81 | ftp = ftplib.FTP(timeout=30) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 82 | ftp.connect(HOST) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 83 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 84 | self.evt.wait() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 85 | ftp.sock.close() |
| 86 | |
| 87 | def testTimeoutDirectAccess(self): |
| 88 | ftp = ftplib.FTP() |
| 89 | ftp.timeout = 30 |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 90 | ftp.connect(HOST) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 91 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 92 | self.evt.wait() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 93 | ftp.sock.close() |
| 94 | |
| 95 | def testTimeoutNone(self): |
| 96 | # None, having other default |
| 97 | previous = socket.getdefaulttimeout() |
| 98 | socket.setdefaulttimeout(30) |
| 99 | try: |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 100 | ftp = ftplib.FTP(HOST, timeout=None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 101 | finally: |
| 102 | socket.setdefaulttimeout(previous) |
| 103 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 104 | self.evt.wait() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 105 | ftp.close() |
| 106 | |
| 107 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 108 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 109 | support.run_unittest(GeneralTests) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 110 | |
| 111 | if __name__ == '__main__': |
| 112 | test_main() |