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 | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 56 | ftp.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): |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 59 | # default -- use global socket timeout |
| 60 | self.assert_(socket.getdefaulttimeout() is None) |
| 61 | socket.setdefaulttimeout(30) |
| 62 | try: |
| 63 | ftp = ftplib.FTP("localhost") |
| 64 | finally: |
| 65 | socket.setdefaulttimeout(None) |
| 66 | self.assertEqual(ftp.sock.gettimeout(), 30) |
| 67 | self.evt.wait() |
| 68 | ftp.close() |
| 69 | |
| 70 | def testTimeoutNone(self): |
| 71 | # no timeout -- do not use global socket timeout |
| 72 | self.assert_(socket.getdefaulttimeout() is None) |
| 73 | socket.setdefaulttimeout(30) |
| 74 | try: |
| 75 | ftp = ftplib.FTP("localhost", timeout=None) |
| 76 | finally: |
| 77 | socket.setdefaulttimeout(None) |
Facundo Batista | 3f10099 | 2007-03-26 20:56:09 +0000 | [diff] [blame] | 78 | self.assertTrue(ftp.sock.gettimeout() is None) |
Neal Norwitz | b0917c1 | 2008-02-26 04:50:37 +0000 | [diff] [blame] | 79 | self.evt.wait() |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 80 | ftp.close() |
Neal Norwitz | 0d4c06e | 2007-04-25 06:30:05 +0000 | [diff] [blame] | 81 | |
Facundo Batista | 3f10099 | 2007-03-26 20:56:09 +0000 | [diff] [blame] | 82 | def testTimeoutValue(self): |
| 83 | # a value |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 84 | ftp = ftplib.FTP(HOST, timeout=30) |
Facundo Batista | 3f10099 | 2007-03-26 20:56:09 +0000 | [diff] [blame] | 85 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Neal Norwitz | b0917c1 | 2008-02-26 04:50:37 +0000 | [diff] [blame] | 86 | self.evt.wait() |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 87 | ftp.close() |
Facundo Batista | 3f10099 | 2007-03-26 20:56:09 +0000 | [diff] [blame] | 88 | |
Facundo Batista | 93c3368 | 2007-03-30 13:00:35 +0000 | [diff] [blame] | 89 | def testTimeoutConnect(self): |
| 90 | ftp = ftplib.FTP() |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 91 | ftp.connect(HOST, timeout=30) |
Facundo Batista | 93c3368 | 2007-03-30 13:00:35 +0000 | [diff] [blame] | 92 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Neal Norwitz | b0917c1 | 2008-02-26 04:50:37 +0000 | [diff] [blame] | 93 | self.evt.wait() |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 94 | ftp.close() |
Facundo Batista | 93c3368 | 2007-03-30 13:00:35 +0000 | [diff] [blame] | 95 | |
| 96 | def testTimeoutDifferentOrder(self): |
| 97 | ftp = ftplib.FTP(timeout=30) |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 98 | ftp.connect(HOST) |
Facundo Batista | 93c3368 | 2007-03-30 13:00:35 +0000 | [diff] [blame] | 99 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Neal Norwitz | b0917c1 | 2008-02-26 04:50:37 +0000 | [diff] [blame] | 100 | self.evt.wait() |
Facundo Batista | 4f1b1ed | 2008-05-29 16:39:26 +0000 | [diff] [blame] | 101 | ftp.close() |
Facundo Batista | 93c3368 | 2007-03-30 13:00:35 +0000 | [diff] [blame] | 102 | |
| 103 | def testTimeoutDirectAccess(self): |
| 104 | ftp = ftplib.FTP() |
| 105 | ftp.timeout = 30 |
Trent Nelson | e41b006 | 2008-04-08 23:47:30 +0000 | [diff] [blame] | 106 | ftp.connect(HOST) |
Facundo Batista | 93c3368 | 2007-03-30 13:00:35 +0000 | [diff] [blame] | 107 | self.assertEqual(ftp.sock.gettimeout(), 30) |
Neal Norwitz | b0917c1 | 2008-02-26 04:50:37 +0000 | [diff] [blame] | 108 | self.evt.wait() |
Facundo Batista | 3f10099 | 2007-03-26 20:56:09 +0000 | [diff] [blame] | 109 | ftp.close() |
| 110 | |
| 111 | |
Facundo Batista | 3f10099 | 2007-03-26 20:56:09 +0000 | [diff] [blame] | 112 | def test_main(verbose=None): |
| 113 | test_support.run_unittest(GeneralTests) |
| 114 | |
| 115 | if __name__ == '__main__': |
| 116 | test_main() |