blob: 7c1f622ce05cf45b17b01e6640464aea39f8332c [file] [log] [blame]
Facundo Batista3f100992007-03-26 20:56:09 +00001import socket
2import threading
3import ftplib
4import time
5
6from unittest import TestCase
7from test import test_support
8
Neal Norwitzb0917c12008-02-26 04:50:37 +00009server_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 Batista3f100992007-03-26 20:56:09 +000015def server(evt):
Neal Norwitzb0917c12008-02-26 04:50:37 +000016 global server_port
Facundo Batista3f100992007-03-26 20:56:09 +000017 serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Facundo Batista93c33682007-03-30 13:00:35 +000018 serv.settimeout(3)
Facundo Batista3f100992007-03-26 20:56:09 +000019 serv.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Neal Norwitzb0917c12008-02-26 04:50:37 +000020 server_port = test_support.bind_port(serv, "", 9091)
Facundo Batista3f100992007-03-26 20:56:09 +000021 serv.listen(5)
Neal Norwitzb0917c12008-02-26 04:50:37 +000022 # (1) Signal the caller that we are ready to accept the connection.
23 evt.set()
Facundo Batista93c33682007-03-30 13:00:35 +000024 try:
25 conn, addr = serv.accept()
26 except socket.timeout:
27 pass
28 else:
29 conn.send("1 Hola mundo\n")
Neal Norwitzb0917c12008-02-26 04:50:37 +000030 # (2) Signal the caller that it is safe to close the socket.
31 evt.set()
Facundo Batista93c33682007-03-30 13:00:35 +000032 conn.close()
33 finally:
34 serv.close()
Neal Norwitzb0917c12008-02-26 04:50:37 +000035 # (3) Signal the caller that we are done.
Facundo Batista93c33682007-03-30 13:00:35 +000036 evt.set()
Facundo Batista3f100992007-03-26 20:56:09 +000037
38class GeneralTests(TestCase):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +000039
Facundo Batista3f100992007-03-26 20:56:09 +000040 def setUp(self):
Facundo Batista3f100992007-03-26 20:56:09 +000041 self.evt = threading.Event()
42 threading.Thread(target=server, args=(self.evt,)).start()
Neal Norwitzb0917c12008-02-26 04:50:37 +000043 # Wait for the server to be ready.
44 self.evt.wait()
45 self.evt.clear()
46 ftplib.FTP.port = server_port
Facundo Batista3f100992007-03-26 20:56:09 +000047
48 def tearDown(self):
Neal Norwitzb0917c12008-02-26 04:50:37 +000049 # Wait on the closing of the socket (this shouldn't be necessary).
Facundo Batista3f100992007-03-26 20:56:09 +000050 self.evt.wait()
51
52 def testBasic(self):
53 # do nothing
54 ftplib.FTP()
55
56 # connects
57 ftp = ftplib.FTP("localhost")
Neal Norwitzb0917c12008-02-26 04:50:37 +000058 self.evt.wait()
Facundo Batista3f100992007-03-26 20:56:09 +000059 ftp.sock.close()
Neal Norwitz0d4c06e2007-04-25 06:30:05 +000060
Facundo Batista3f100992007-03-26 20:56:09 +000061 def testTimeoutDefault(self):
62 # default
63 ftp = ftplib.FTP("localhost")
64 self.assertTrue(ftp.sock.gettimeout() is None)
Neal Norwitzb0917c12008-02-26 04:50:37 +000065 self.evt.wait()
Facundo Batista3f100992007-03-26 20:56:09 +000066 ftp.sock.close()
Neal Norwitz0d4c06e2007-04-25 06:30:05 +000067
Facundo Batista3f100992007-03-26 20:56:09 +000068 def testTimeoutValue(self):
69 # a value
70 ftp = ftplib.FTP("localhost", timeout=30)
71 self.assertEqual(ftp.sock.gettimeout(), 30)
Neal Norwitzb0917c12008-02-26 04:50:37 +000072 self.evt.wait()
Facundo Batista3f100992007-03-26 20:56:09 +000073 ftp.sock.close()
74
Facundo Batista93c33682007-03-30 13:00:35 +000075 def testTimeoutConnect(self):
76 ftp = ftplib.FTP()
77 ftp.connect("localhost", timeout=30)
78 self.assertEqual(ftp.sock.gettimeout(), 30)
Neal Norwitzb0917c12008-02-26 04:50:37 +000079 self.evt.wait()
Facundo Batista93c33682007-03-30 13:00:35 +000080 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 Norwitzb0917c12008-02-26 04:50:37 +000086 self.evt.wait()
Facundo Batista93c33682007-03-30 13:00:35 +000087 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 Norwitzb0917c12008-02-26 04:50:37 +000094 self.evt.wait()
Facundo Batista93c33682007-03-30 13:00:35 +000095 ftp.sock.close()
96
Facundo Batista3f100992007-03-26 20:56:09 +000097 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 Norwitzb0917c12008-02-26 04:50:37 +0000106 self.evt.wait()
Facundo Batista3f100992007-03-26 20:56:09 +0000107 ftp.close()
108
109
Facundo Batista3f100992007-03-26 20:56:09 +0000110def test_main(verbose=None):
111 test_support.run_unittest(GeneralTests)
112
113if __name__ == '__main__':
114 test_main()