Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 1 | """Unit tests for socket timeout feature.""" |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 2 | |
| 3 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 5 | |
Neal Norwitz | 55b61d2 | 2003-02-28 19:57:03 +0000 | [diff] [blame] | 6 | # This requires the 'network' resource as given on the regrtest command line. |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | skip_expected = not support.is_resource_enabled('network') |
Neal Norwitz | 55b61d2 | 2003-02-28 19:57:03 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 9 | import time |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 10 | import errno |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 11 | import socket |
| 12 | |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 14 | class CreationTestCase(unittest.TestCase): |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 15 | """Test case for socket.gettimeout() and socket.settimeout()""" |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 17 | def setUp(self): |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 18 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 19 | |
| 20 | def tearDown(self): |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 21 | self.sock.close() |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 22 | |
| 23 | def testObjectCreation(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 24 | # Test Socket creation |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 25 | self.assertEqual(self.sock.gettimeout(), None, |
| 26 | "timeout not disabled by default") |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 27 | |
| 28 | def testFloatReturnValue(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 29 | # Test return value of gettimeout() |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 30 | self.sock.settimeout(7.345) |
| 31 | self.assertEqual(self.sock.gettimeout(), 7.345) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 33 | self.sock.settimeout(3) |
| 34 | self.assertEqual(self.sock.gettimeout(), 3) |
| 35 | |
| 36 | self.sock.settimeout(None) |
| 37 | self.assertEqual(self.sock.gettimeout(), None) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 38 | |
| 39 | def testReturnType(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 40 | # Test return type of gettimeout() |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 41 | self.sock.settimeout(1) |
| 42 | self.assertEqual(type(self.sock.gettimeout()), type(1.0)) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 43 | |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 44 | self.sock.settimeout(3.9) |
| 45 | self.assertEqual(type(self.sock.gettimeout()), type(1.0)) |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 46 | |
| 47 | def testTypeCheck(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 48 | # Test type checking by settimeout() |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 49 | self.sock.settimeout(0) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 50 | self.sock.settimeout(0) |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 51 | self.sock.settimeout(0.0) |
| 52 | self.sock.settimeout(None) |
| 53 | self.assertRaises(TypeError, self.sock.settimeout, "") |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 54 | self.assertRaises(TypeError, self.sock.settimeout, "") |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 55 | self.assertRaises(TypeError, self.sock.settimeout, ()) |
| 56 | self.assertRaises(TypeError, self.sock.settimeout, []) |
| 57 | self.assertRaises(TypeError, self.sock.settimeout, {}) |
| 58 | self.assertRaises(TypeError, self.sock.settimeout, 0j) |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 59 | |
| 60 | def testRangeCheck(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 61 | # Test range checking by settimeout() |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 62 | self.assertRaises(ValueError, self.sock.settimeout, -1) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 63 | self.assertRaises(ValueError, self.sock.settimeout, -1) |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 64 | self.assertRaises(ValueError, self.sock.settimeout, -1.0) |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 66 | def testTimeoutThenBlocking(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 67 | # Test settimeout() followed by setblocking() |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 68 | self.sock.settimeout(10) |
| 69 | self.sock.setblocking(1) |
| 70 | self.assertEqual(self.sock.gettimeout(), None) |
| 71 | self.sock.setblocking(0) |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 72 | self.assertEqual(self.sock.gettimeout(), 0.0) |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 74 | self.sock.settimeout(10) |
| 75 | self.sock.setblocking(0) |
Guido van Rossum | 11ba094 | 2002-06-13 15:07:44 +0000 | [diff] [blame] | 76 | self.assertEqual(self.sock.gettimeout(), 0.0) |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 77 | self.sock.setblocking(1) |
| 78 | self.assertEqual(self.sock.gettimeout(), None) |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 79 | |
| 80 | def testBlockingThenTimeout(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 81 | # Test setblocking() followed by settimeout() |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 82 | self.sock.setblocking(0) |
| 83 | self.sock.settimeout(1) |
| 84 | self.assertEqual(self.sock.gettimeout(), 1) |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 86 | self.sock.setblocking(1) |
| 87 | self.sock.settimeout(1) |
| 88 | self.assertEqual(self.sock.gettimeout(), 1) |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 90 | |
Guido van Rossum | 24e4af8 | 2002-06-12 19:18:08 +0000 | [diff] [blame] | 91 | class TimeoutTestCase(unittest.TestCase): |
Tim Peters | 14821c5 | 2003-02-21 16:45:41 +0000 | [diff] [blame] | 92 | # There are a number of tests here trying to make sure that an operation |
| 93 | # doesn't take too much longer than expected. But competing machine |
| 94 | # activity makes it inevitable that such tests will fail at times. |
| 95 | # When fuzz was at 1.0, I (tim) routinely saw bogus failures on Win2K |
| 96 | # and Win98SE. Boosting it to 2.0 helped a lot, but isn't a real |
| 97 | # solution. |
| 98 | fuzz = 2.0 |
Guido van Rossum | aa6a664 | 2002-06-12 19:57:18 +0000 | [diff] [blame] | 99 | |
Victor Stinner | 5c85e3f | 2011-01-03 14:30:41 +0000 | [diff] [blame] | 100 | localhost = '127.0.0.1' |
| 101 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 102 | def setUp(self): |
Victor Stinner | 5c85e3f | 2011-01-03 14:30:41 +0000 | [diff] [blame] | 103 | raise NotImplementedError() |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 104 | |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 105 | tearDown = setUp |
| 106 | |
| 107 | def _sock_operation(self, count, timeout, method, *args): |
| 108 | """ |
| 109 | Test the specified socket method. |
| 110 | |
| 111 | The method is run at most `count` times and must raise a socket.timeout |
| 112 | within `timeout` + self.fuzz seconds. |
| 113 | """ |
| 114 | self.sock.settimeout(timeout) |
| 115 | method = getattr(self.sock, method) |
| 116 | for i in range(count): |
| 117 | t1 = time.time() |
| 118 | try: |
| 119 | method(*args) |
| 120 | except socket.timeout as e: |
| 121 | delta = time.time() - t1 |
| 122 | break |
| 123 | else: |
| 124 | self.fail('socket.timeout was not raised') |
| 125 | # These checks should account for timing unprecision |
| 126 | self.assertLess(delta, timeout + self.fuzz) |
| 127 | self.assertGreater(delta, timeout - 1.0) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 128 | |
Victor Stinner | 5c85e3f | 2011-01-03 14:30:41 +0000 | [diff] [blame] | 129 | |
| 130 | class TCPTimeoutTestCase(TimeoutTestCase): |
| 131 | """TCP test case for socket.socket() timeout functions""" |
| 132 | |
| 133 | def setUp(self): |
| 134 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 135 | self.addr_remote = ('www.python.org.', 80) |
| 136 | |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 137 | def tearDown(self): |
| 138 | self.sock.close() |
| 139 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 140 | def testConnectTimeout(self): |
Trent Nelson | 514dbb0 | 2012-08-20 21:22:59 -0400 | [diff] [blame] | 141 | # Testing connect timeout is tricky: we need to have IP connectivity |
| 142 | # to a host that silently drops our packets. We can't simulate this |
| 143 | # from Python because it's a function of the underlying TCP/IP stack. |
| 144 | # So, the following Snakebite host has been defined: |
| 145 | blackhole = ('blackhole.snakebite.net', 56666) |
| 146 | |
| 147 | # Blackhole has been configured to silently drop any incoming packets. |
| 148 | # No RSTs (for TCP) or ICMP UNREACH (for UDP/ICMP) will be sent back |
| 149 | # to hosts that attempt to connect to this address: which is exactly |
| 150 | # what we need to confidently test connect timeout. |
| 151 | |
| 152 | # However, we want to prevent false positives. It's not unreasonable |
| 153 | # to expect certain hosts may not be able to reach the blackhole, due |
| 154 | # to firewalling or general network configuration. In order to improve |
| 155 | # our confidence in testing the blackhole, a corresponding 'whitehole' |
| 156 | # has also been set up using one port higher: |
| 157 | whitehole = ('whitehole.snakebite.net', 56667) |
| 158 | |
| 159 | # This address has been configured to immediately drop any incoming |
| 160 | # packets as well, but it does it respectfully with regards to the |
| 161 | # incoming protocol. RSTs are sent for TCP packets, and ICMP UNREACH |
| 162 | # is sent for UDP/ICMP packets. This means our attempts to connect to |
| 163 | # it should be met immediately with ECONNREFUSED. The test case has |
| 164 | # been structured around this premise: if we get an ECONNREFUSED from |
| 165 | # the whitehole, we proceed with testing connect timeout against the |
| 166 | # blackhole. If we don't, we skip the test (with a message about not |
| 167 | # getting the required RST from the whitehole within the required |
| 168 | # timeframe). |
| 169 | |
| 170 | # For the records, the whitehole/blackhole configuration has been set |
| 171 | # up using the 'pf' firewall (available on BSDs), using the following: |
| 172 | # |
| 173 | # ext_if="bge0" |
| 174 | # |
| 175 | # blackhole_ip="35.8.247.6" |
| 176 | # whitehole_ip="35.8.247.6" |
| 177 | # blackhole_port="56666" |
| 178 | # whitehole_port="56667" |
| 179 | # |
| 180 | # block return in log quick on $ext_if proto { tcp udp } \ |
| 181 | # from any to $whitehole_ip port $whitehole_port |
| 182 | # block drop in log quick on $ext_if proto { tcp udp } \ |
| 183 | # from any to $blackhole_ip port $blackhole_port |
| 184 | # |
| 185 | |
| 186 | skip = True |
| 187 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 188 | # Use a timeout of 3 seconds. Why 3? Because it's more than 1, and |
| 189 | # less than 5. i.e. no particular reason. Feel free to tweak it if |
| 190 | # you feel a different value would be more appropriate. |
| 191 | timeout = 3 |
| 192 | sock.settimeout(timeout) |
| 193 | try: |
| 194 | sock.connect((whitehole)) |
| 195 | except socket.timeout: |
| 196 | pass |
| 197 | except IOError as err: |
| 198 | if err.errno == errno.ECONNREFUSED: |
| 199 | skip = False |
| 200 | finally: |
| 201 | sock.close() |
| 202 | del sock |
| 203 | |
| 204 | if skip: |
| 205 | self.skipTest( |
| 206 | "We didn't receive a connection reset (RST) packet from " |
| 207 | "{}:{} within {} seconds, so we're unable to test connect " |
| 208 | "timeout against the corresponding {}:{} (which is " |
| 209 | "configured to silently drop packets)." |
| 210 | .format( |
| 211 | whitehole[0], |
| 212 | whitehole[1], |
| 213 | timeout, |
| 214 | blackhole[0], |
| 215 | blackhole[1], |
| 216 | ) |
| 217 | ) |
| 218 | |
| 219 | # All that hard work just to test if connect times out in 0.001s ;-) |
| 220 | self.addr_remote = blackhole |
| 221 | with support.transient_internet(self.addr_remote[0]): |
| 222 | self._sock_operation(1, 0.001, 'connect', self.addr_remote) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 223 | |
| 224 | def testRecvTimeout(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 225 | # Test recv() timeout |
Antoine Pitrou | 83432ba | 2010-10-29 18:15:33 +0000 | [diff] [blame] | 226 | with support.transient_internet(self.addr_remote[0]): |
| 227 | self.sock.connect(self.addr_remote) |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 228 | self._sock_operation(1, 1.5, 'recv', 1024) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 229 | |
| 230 | def testAcceptTimeout(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 231 | # Test accept() timeout |
R. David Murray | e9e9593 | 2010-02-06 05:00:15 +0000 | [diff] [blame] | 232 | support.bind_port(self.sock, self.localhost) |
Guido van Rossum | 28774da | 2002-06-12 20:22:49 +0000 | [diff] [blame] | 233 | self.sock.listen(5) |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 234 | self._sock_operation(1, 1.5, 'accept') |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 235 | |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 236 | def testSend(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 237 | # Test send() timeout |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 238 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv: |
| 239 | support.bind_port(serv, self.localhost) |
| 240 | serv.listen(5) |
| 241 | self.sock.connect(serv.getsockname()) |
| 242 | # Send a lot of data in order to bypass buffering in the TCP stack. |
| 243 | self._sock_operation(100, 1.5, 'send', b"X" * 200000) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 244 | |
| 245 | def testSendto(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 246 | # Test sendto() timeout |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 247 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv: |
| 248 | support.bind_port(serv, self.localhost) |
| 249 | serv.listen(5) |
| 250 | self.sock.connect(serv.getsockname()) |
| 251 | # The address argument is ignored since we already connected. |
| 252 | self._sock_operation(100, 1.5, 'sendto', b"X" * 200000, |
| 253 | serv.getsockname()) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 254 | |
| 255 | def testSendall(self): |
Guido van Rossum | ae46931 | 2002-08-22 20:22:16 +0000 | [diff] [blame] | 256 | # Test sendall() timeout |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 257 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as serv: |
| 258 | support.bind_port(serv, self.localhost) |
| 259 | serv.listen(5) |
| 260 | self.sock.connect(serv.getsockname()) |
| 261 | # Send a lot of data in order to bypass buffering in the TCP stack. |
| 262 | self._sock_operation(100, 1.5, 'sendall', b"X" * 200000) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 263 | |
| 264 | |
Victor Stinner | 5c85e3f | 2011-01-03 14:30:41 +0000 | [diff] [blame] | 265 | class UDPTimeoutTestCase(TimeoutTestCase): |
| 266 | """UDP test case for socket.socket() timeout functions""" |
| 267 | |
| 268 | def setUp(self): |
| 269 | self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 270 | |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 271 | def tearDown(self): |
| 272 | self.sock.close() |
| 273 | |
Victor Stinner | 5c85e3f | 2011-01-03 14:30:41 +0000 | [diff] [blame] | 274 | def testRecvfromTimeout(self): |
| 275 | # Test recvfrom() timeout |
Victor Stinner | 5c85e3f | 2011-01-03 14:30:41 +0000 | [diff] [blame] | 276 | # Prevent "Address already in use" socket exceptions |
| 277 | support.bind_port(self.sock, self.localhost) |
Antoine Pitrou | ca023ca | 2011-01-06 09:05:22 +0000 | [diff] [blame] | 278 | self._sock_operation(1, 1.5, 'recvfrom', 1024) |
Victor Stinner | 5c85e3f | 2011-01-03 14:30:41 +0000 | [diff] [blame] | 279 | |
| 280 | |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 281 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 282 | support.requires('network') |
Victor Stinner | 5c85e3f | 2011-01-03 14:30:41 +0000 | [diff] [blame] | 283 | support.run_unittest( |
| 284 | CreationTestCase, |
| 285 | TCPTimeoutTestCase, |
| 286 | UDPTimeoutTestCase, |
| 287 | ) |
Guido van Rossum | 67f7a38 | 2002-06-06 21:08:16 +0000 | [diff] [blame] | 288 | |
| 289 | if __name__ == "__main__": |
Neal Norwitz | 996acf1 | 2003-02-17 14:51:41 +0000 | [diff] [blame] | 290 | test_main() |