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