blob: 5f961d2de3d252a15324a366b3d6de0b24e385bd [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 Batista3f100992007-03-26 20:56:09 +000056 ftp.sock.close()
Neal Norwitz0d4c06e2007-04-25 06:30:05 +000057
Facundo Batista3f100992007-03-26 20:56:09 +000058 def testTimeoutDefault(self):
59 # default
Trent Nelsone41b0062008-04-08 23:47:30 +000060 ftp = ftplib.FTP(HOST)
Facundo Batista3f100992007-03-26 20:56:09 +000061 self.assertTrue(ftp.sock.gettimeout() is None)
Neal Norwitzb0917c12008-02-26 04:50:37 +000062 self.evt.wait()
Facundo Batista3f100992007-03-26 20:56:09 +000063 ftp.sock.close()
Neal Norwitz0d4c06e2007-04-25 06:30:05 +000064
Facundo Batista3f100992007-03-26 20:56:09 +000065 def testTimeoutValue(self):
66 # a value
Trent Nelsone41b0062008-04-08 23:47:30 +000067 ftp = ftplib.FTP(HOST, timeout=30)
Facundo Batista3f100992007-03-26 20:56:09 +000068 self.assertEqual(ftp.sock.gettimeout(), 30)
Neal Norwitzb0917c12008-02-26 04:50:37 +000069 self.evt.wait()
Facundo Batista3f100992007-03-26 20:56:09 +000070 ftp.sock.close()
71
Facundo Batista93c33682007-03-30 13:00:35 +000072 def testTimeoutConnect(self):
73 ftp = ftplib.FTP()
Trent Nelsone41b0062008-04-08 23:47:30 +000074 ftp.connect(HOST, timeout=30)
Facundo Batista93c33682007-03-30 13:00:35 +000075 self.assertEqual(ftp.sock.gettimeout(), 30)
Neal Norwitzb0917c12008-02-26 04:50:37 +000076 self.evt.wait()
Facundo Batista93c33682007-03-30 13:00:35 +000077 ftp.sock.close()
78
79 def testTimeoutDifferentOrder(self):
80 ftp = ftplib.FTP(timeout=30)
Trent Nelsone41b0062008-04-08 23:47:30 +000081 ftp.connect(HOST)
Facundo Batista93c33682007-03-30 13:00:35 +000082 self.assertEqual(ftp.sock.gettimeout(), 30)
Neal Norwitzb0917c12008-02-26 04:50:37 +000083 self.evt.wait()
Facundo Batista93c33682007-03-30 13:00:35 +000084 ftp.sock.close()
85
86 def testTimeoutDirectAccess(self):
87 ftp = ftplib.FTP()
88 ftp.timeout = 30
Trent Nelsone41b0062008-04-08 23:47:30 +000089 ftp.connect(HOST)
Facundo Batista93c33682007-03-30 13:00:35 +000090 self.assertEqual(ftp.sock.gettimeout(), 30)
Neal Norwitzb0917c12008-02-26 04:50:37 +000091 self.evt.wait()
Facundo Batista93c33682007-03-30 13:00:35 +000092 ftp.sock.close()
93
Facundo Batista3f100992007-03-26 20:56:09 +000094 def testTimeoutNone(self):
95 # None, having other default
96 previous = socket.getdefaulttimeout()
97 socket.setdefaulttimeout(30)
98 try:
Trent Nelsone41b0062008-04-08 23:47:30 +000099 ftp = ftplib.FTP(HOST, timeout=None)
Facundo Batista3f100992007-03-26 20:56:09 +0000100 finally:
101 socket.setdefaulttimeout(previous)
102 self.assertEqual(ftp.sock.gettimeout(), 30)
Neal Norwitzb0917c12008-02-26 04:50:37 +0000103 self.evt.wait()
Facundo Batista3f100992007-03-26 20:56:09 +0000104 ftp.close()
105
106
Facundo Batista3f100992007-03-26 20:56:09 +0000107def test_main(verbose=None):
108 test_support.run_unittest(GeneralTests)
109
110if __name__ == '__main__':
111 test_main()