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() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 57 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 58 | |
| 59 | def testTimeoutDefault(self): |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 60 | # default -- use global socket timeout |
| 61 | self.assert_(socket.getdefaulttimeout() is None) |
| 62 | socket.setdefaulttimeout(30) |
| 63 | try: |
| 64 | ftp = ftplib.FTP("localhost") |
| 65 | finally: |
| 66 | socket.setdefaulttimeout(None) |
| 67 | self.assertEqual(ftp.sock.gettimeout(), 30) |
| 68 | self.evt.wait() |
| 69 | ftp.close() |
| 70 | |
| 71 | def testTimeoutNone(self): |
| 72 | # no timeout -- do not use global socket timeout |
| 73 | self.assert_(socket.getdefaulttimeout() is None) |
| 74 | socket.setdefaulttimeout(30) |
| 75 | try: |
| 76 | ftp = ftplib.FTP("localhost", timeout=None) |
| 77 | finally: |
| 78 | socket.setdefaulttimeout(None) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 79 | self.assertTrue(ftp.sock.gettimeout() is None) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 80 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 81 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 82 | |
| 83 | def testTimeoutValue(self): |
| 84 | # a value |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 85 | ftp = ftplib.FTP(HOST, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 86 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 87 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 88 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 89 | |
| 90 | def testTimeoutConnect(self): |
| 91 | ftp = ftplib.FTP() |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 92 | ftp.connect(HOST, timeout=30) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 93 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 94 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 95 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 96 | |
| 97 | def testTimeoutDifferentOrder(self): |
| 98 | ftp = ftplib.FTP(timeout=30) |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 99 | ftp.connect(HOST) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 100 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 101 | self.evt.wait() |
Georg Brandl | f78e02b | 2008-06-10 17:40:04 +0000 | [diff] [blame] | 102 | ftp.close() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 103 | |
| 104 | def testTimeoutDirectAccess(self): |
| 105 | ftp = ftplib.FTP() |
| 106 | ftp.timeout = 30 |
Christian Heimes | 5e69685 | 2008-04-09 08:37:03 +0000 | [diff] [blame] | 107 | ftp.connect(HOST) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 108 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 109 | self.evt.wait() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 110 | ftp.close() |
| 111 | |
| 112 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 113 | def test_main(verbose=None): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 114 | support.run_unittest(GeneralTests) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 115 | |
| 116 | if __name__ == '__main__': |
| 117 | test_main() |