blob: c7cd3a5b1a6e09d28562ac016292649ee7061e33 [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
Trent Nelsone41b0062008-04-08 23:47:30 +00009HOST = test_support.HOST
Neal Norwitzb0917c12008-02-26 04:50:37 +000010
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 Nelsone41b0062008-04-08 23:47:30 +000015def server(evt, serv):
Facundo Batista3f100992007-03-26 20:56:09 +000016 serv.listen(5)
Neal Norwitzb0917c12008-02-26 04:50:37 +000017 # (1) Signal the caller that we are ready to accept the connection.
18 evt.set()
Facundo Batista93c33682007-03-30 13:00:35 +000019 try:
20 conn, addr = serv.accept()
21 except socket.timeout:
22 pass
23 else:
24 conn.send("1 Hola mundo\n")
Neal Norwitzb0917c12008-02-26 04:50:37 +000025 # (2) Signal the caller that it is safe to close the socket.
26 evt.set()
Facundo Batista93c33682007-03-30 13:00:35 +000027 conn.close()
28 finally:
29 serv.close()
Neal Norwitzb0917c12008-02-26 04:50:37 +000030 # (3) Signal the caller that we are done.
Facundo Batista93c33682007-03-30 13:00:35 +000031 evt.set()
Facundo Batista3f100992007-03-26 20:56:09 +000032
33class GeneralTests(TestCase):
Neal Norwitz0d4c06e2007-04-25 06:30:05 +000034
Facundo Batista3f100992007-03-26 20:56:09 +000035 def setUp(self):
Facundo Batista3f100992007-03-26 20:56:09 +000036 self.evt = threading.Event()
Trent Nelsone41b0062008-04-08 23:47:30 +000037 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 Norwitzb0917c12008-02-26 04:50:37 +000041 # Wait for the server to be ready.
42 self.evt.wait()
43 self.evt.clear()
Trent Nelsone41b0062008-04-08 23:47:30 +000044 ftplib.FTP.port = self.port
Facundo Batista3f100992007-03-26 20:56:09 +000045
46 def tearDown(self):
47 self.evt.wait()
48
49 def testBasic(self):
50 # do nothing
51 ftplib.FTP()
52
53 # connects
Trent Nelsone41b0062008-04-08 23:47:30 +000054 ftp = ftplib.FTP(HOST)
Neal Norwitzb0917c12008-02-26 04:50:37 +000055 self.evt.wait()
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000056 ftp.close()
Neal Norwitz0d4c06e2007-04-25 06:30:05 +000057
Facundo Batista3f100992007-03-26 20:56:09 +000058 def testTimeoutDefault(self):
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000059 # 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 Batista3f100992007-03-26 20:56:09 +000078 self.assertTrue(ftp.sock.gettimeout() is None)
Neal Norwitzb0917c12008-02-26 04:50:37 +000079 self.evt.wait()
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000080 ftp.close()
Neal Norwitz0d4c06e2007-04-25 06:30:05 +000081
Facundo Batista3f100992007-03-26 20:56:09 +000082 def testTimeoutValue(self):
83 # a value
Trent Nelsone41b0062008-04-08 23:47:30 +000084 ftp = ftplib.FTP(HOST, timeout=30)
Facundo Batista3f100992007-03-26 20:56:09 +000085 self.assertEqual(ftp.sock.gettimeout(), 30)
Neal Norwitzb0917c12008-02-26 04:50:37 +000086 self.evt.wait()
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000087 ftp.close()
Facundo Batista3f100992007-03-26 20:56:09 +000088
Facundo Batista93c33682007-03-30 13:00:35 +000089 def testTimeoutConnect(self):
90 ftp = ftplib.FTP()
Trent Nelsone41b0062008-04-08 23:47:30 +000091 ftp.connect(HOST, timeout=30)
Facundo Batista93c33682007-03-30 13:00:35 +000092 self.assertEqual(ftp.sock.gettimeout(), 30)
Neal Norwitzb0917c12008-02-26 04:50:37 +000093 self.evt.wait()
Facundo Batista4f1b1ed2008-05-29 16:39:26 +000094 ftp.close()
Facundo Batista93c33682007-03-30 13:00:35 +000095
96 def testTimeoutDifferentOrder(self):
97 ftp = ftplib.FTP(timeout=30)
Trent Nelsone41b0062008-04-08 23:47:30 +000098 ftp.connect(HOST)
Facundo Batista93c33682007-03-30 13:00:35 +000099 self.assertEqual(ftp.sock.gettimeout(), 30)
Neal Norwitzb0917c12008-02-26 04:50:37 +0000100 self.evt.wait()
Facundo Batista4f1b1ed2008-05-29 16:39:26 +0000101 ftp.close()
Facundo Batista93c33682007-03-30 13:00:35 +0000102
103 def testTimeoutDirectAccess(self):
104 ftp = ftplib.FTP()
105 ftp.timeout = 30
Trent Nelsone41b0062008-04-08 23:47:30 +0000106 ftp.connect(HOST)
Facundo Batista93c33682007-03-30 13:00:35 +0000107 self.assertEqual(ftp.sock.gettimeout(), 30)
Neal Norwitzb0917c12008-02-26 04:50:37 +0000108 self.evt.wait()
Facundo Batista3f100992007-03-26 20:56:09 +0000109 ftp.close()
110
111
Facundo Batista3f100992007-03-26 20:56:09 +0000112def test_main(verbose=None):
113 test_support.run_unittest(GeneralTests)
114
115if __name__ == '__main__':
116 test_main()