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