@@ -165,9 +165,8 @@ def test_disable_ipv6_only_or_raise():
165165 def _log_error (* args ):
166166 errors_logged .append (args )
167167
168- sock = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
169168 with (
170- pytest .raises (OSError ),
169+ socket . socket ( socket . AF_INET , socket . SOCK_DGRAM ) as sock , pytest .raises (OSError ),
171170 patch .object (netutils .log , "error" , _log_error ),
172171 patch ("socket.socket.setsockopt" , side_effect = OSError ),
173172 ):
@@ -179,103 +178,106 @@ def _log_error(*args):
179178 )
180179
181180
181+
182182@pytest .mark .skipif (not hasattr (socket , "SO_REUSEPORT" ), reason = "System does not have SO_REUSEPORT" )
183183def test_set_so_reuseport_if_available_is_present ():
184184 """Test that setting socket.SO_REUSEPORT only OSError errno.ENOPROTOOPT is trapped."""
185- sock = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
186- with pytest .raises (OSError ), patch ("socket.socket.setsockopt" , side_effect = OSError ):
187- netutils .set_so_reuseport_if_available (sock )
185+ with socket .socket (socket .AF_INET , socket .SOCK_DGRAM ) as sock :
186+ with pytest .raises (OSError ), patch ("socket.socket.setsockopt" , side_effect = OSError ):
187+ netutils .set_so_reuseport_if_available (sock )
188188
189- with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .ENOPROTOOPT , None )):
190- netutils .set_so_reuseport_if_available (sock )
189+ with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .ENOPROTOOPT , None )):
190+ netutils .set_so_reuseport_if_available (sock )
191191
192192
193193@pytest .mark .skipif (hasattr (socket , "SO_REUSEPORT" ), reason = "System has SO_REUSEPORT" )
194194def test_set_so_reuseport_if_available_not_present ():
195195 """Test that we do not try to set SO_REUSEPORT if it is not present."""
196- sock = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
197- with patch ("socket.socket.setsockopt" , side_effect = OSError ):
198- netutils .set_so_reuseport_if_available (sock )
196+ with (
197+ socket .socket (socket .AF_INET , socket .SOCK_DGRAM ) as sock ,
198+ patch ("socket.socket.setsockopt" , side_effect = OSError ),
199+ ):
200+ netutils .set_so_reuseport_if_available (sock )
199201
200202
201203def test_set_mdns_port_socket_options_for_ip_version ():
202204 """Test OSError with errno with EINVAL and bind address ''.
203205
204206 from setsockopt IP_MULTICAST_TTL does not raise."""
205- sock = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
207+ with socket .socket (socket .AF_INET , socket .SOCK_DGRAM ) as sock :
206208
207- # Should raise on EPERM always
208- with pytest .raises (OSError ), patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EPERM , None )):
209- netutils .set_mdns_port_socket_options_for_ip_version (sock , ("" ,), r .IPVersion .V4Only )
209+ # Should raise on EPERM always
210+ with pytest .raises (OSError ), patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EPERM , None )):
211+ netutils .set_mdns_port_socket_options_for_ip_version (sock , ("" ,), r .IPVersion .V4Only )
210212
211- # Should raise on EINVAL always when bind address is not ''
212- with pytest .raises (OSError ), patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EINVAL , None )):
213- netutils .set_mdns_port_socket_options_for_ip_version (sock , ("127.0.0.1" ,), r .IPVersion .V4Only )
213+ # Should raise on EINVAL always when bind address is not ''
214+ with pytest .raises (OSError ), patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EINVAL , None )):
215+ netutils .set_mdns_port_socket_options_for_ip_version (sock , ("127.0.0.1" ,), r .IPVersion .V4Only )
214216
215- # Should not raise on EINVAL when bind address is ''
216- with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EINVAL , None )):
217- netutils .set_mdns_port_socket_options_for_ip_version (sock , ("" ,), r .IPVersion .V4Only )
217+ # Should not raise on EINVAL when bind address is ''
218+ with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EINVAL , None )):
219+ netutils .set_mdns_port_socket_options_for_ip_version (sock , ("" ,), r .IPVersion .V4Only )
218220
219221
220222def test_add_multicast_member (caplog : pytest .LogCaptureFixture ) -> None :
221- sock = socket .socket (socket .AF_INET , socket .SOCK_DGRAM )
222- interface = "127.0.0.1"
223-
224- # EPERM should always raise
225- with pytest .raises (OSError ), patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EPERM , None )):
226- netutils .add_multicast_member (sock , interface )
227-
228- # EADDRINUSE should return False
229- with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EADDRINUSE , None )):
230- assert netutils .add_multicast_member (sock , interface ) is False
231-
232- # EADDRNOTAVAIL should return False
233- with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EADDRNOTAVAIL , None )):
234- assert netutils .add_multicast_member (sock , interface ) is False
235-
236- # EINVAL should return False
237- with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EINVAL , None )):
238- assert netutils .add_multicast_member (sock , interface ) is False
239-
240- # ENOPROTOOPT should return False
241- with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .ENOPROTOOPT , None )):
242- assert netutils .add_multicast_member (sock , interface ) is False
243-
244- # ENODEV should raise for ipv4
245- with pytest .raises (OSError ), patch ("socket.socket.setsockopt" , side_effect = OSError (errno .ENODEV , None )):
246- assert netutils .add_multicast_member (sock , interface ) is False
247-
248- # ENODEV should return False for ipv6
249- with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .ENODEV , None )):
250- assert netutils .add_multicast_member (sock , ("2001:db8::" , 1 , 1 )) is False # type: ignore[arg-type]
251-
252- # No IPv6 support should return False for IPv6
253- with patch ("socket.inet_pton" , side_effect = OSError ()):
254- assert netutils .add_multicast_member (sock , ("2001:db8::" , 1 , 1 )) is False # type: ignore[arg-type]
255-
256- # No error should return True
257- with patch ("socket.socket.setsockopt" ):
258- assert netutils .add_multicast_member (sock , interface ) is True
259-
260- # Ran out of IGMP memberships is forgiving and logs about igmp_max_memberships on linux
261- caplog .clear ()
262- with (
263- patch .object (sys , "platform" , "linux" ),
264- patch ("socket.socket.setsockopt" , side_effect = OSError (errno .ENOBUFS , "No buffer space available" )),
265- ):
266- assert netutils .add_multicast_member (sock , interface ) is False
267- assert "No buffer space available" in caplog .text
268- assert "net.ipv4.igmp_max_memberships" in caplog .text
269-
270- # Ran out of IGMP memberships is forgiving and logs
271- caplog .clear ()
272- with (
273- patch .object (sys , "platform" , "darwin" ),
274- patch ("socket.socket.setsockopt" , side_effect = OSError (errno .ENOBUFS , "No buffer space available" )),
275- ):
276- assert netutils .add_multicast_member (sock , interface ) is False
277- assert "No buffer space available" in caplog .text
278- assert "net.ipv4.igmp_max_memberships" not in caplog .text
223+ with socket .socket (socket .AF_INET , socket .SOCK_DGRAM ) as sock :
224+ interface = "127.0.0.1"
225+
226+ # EPERM should always raise
227+ with pytest .raises (OSError ), patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EPERM , None )):
228+ netutils .add_multicast_member (sock , interface )
229+
230+ # EADDRINUSE should return False
231+ with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EADDRINUSE , None )):
232+ assert netutils .add_multicast_member (sock , interface ) is False
233+
234+ # EADDRNOTAVAIL should return False
235+ with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EADDRNOTAVAIL , None )):
236+ assert netutils .add_multicast_member (sock , interface ) is False
237+
238+ # EINVAL should return False
239+ with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EINVAL , None )):
240+ assert netutils .add_multicast_member (sock , interface ) is False
241+
242+ # ENOPROTOOPT should return False
243+ with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .ENOPROTOOPT , None )):
244+ assert netutils .add_multicast_member (sock , interface ) is False
245+
246+ # ENODEV should raise for ipv4
247+ with pytest .raises (OSError ), patch ("socket.socket.setsockopt" , side_effect = OSError (errno .ENODEV , None )):
248+ assert netutils .add_multicast_member (sock , interface ) is False
249+
250+ # ENODEV should return False for ipv6
251+ with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .ENODEV , None )):
252+ assert netutils .add_multicast_member (sock , ("2001:db8::" , 1 , 1 )) is False # type: ignore[arg-type]
253+
254+ # No IPv6 support should return False for IPv6
255+ with patch ("socket.inet_pton" , side_effect = OSError ()):
256+ assert netutils .add_multicast_member (sock , ("2001:db8::" , 1 , 1 )) is False # type: ignore[arg-type]
257+
258+ # No error should return True
259+ with patch ("socket.socket.setsockopt" ):
260+ assert netutils .add_multicast_member (sock , interface ) is True
261+
262+ # Ran out of IGMP memberships is forgiving and logs about igmp_max_memberships on linux
263+ caplog .clear ()
264+ with (
265+ patch .object (sys , "platform" , "linux" ),
266+ patch ("socket.socket.setsockopt" , side_effect = OSError (errno .ENOBUFS , "No buffer space available" )),
267+ ):
268+ assert netutils .add_multicast_member (sock , interface ) is False
269+ assert "No buffer space available" in caplog .text
270+ assert "net.ipv4.igmp_max_memberships" in caplog .text
271+
272+ # Ran out of IGMP memberships is forgiving and logs
273+ caplog .clear ()
274+ with (
275+ patch .object (sys , "platform" , "darwin" ),
276+ patch ("socket.socket.setsockopt" , side_effect = OSError (errno .ENOBUFS , "No buffer space available" )),
277+ ):
278+ assert netutils .add_multicast_member (sock , interface ) is False
279+ assert "No buffer space available" in caplog .text
280+ assert "net.ipv4.igmp_max_memberships" not in caplog .text
279281
280282
281283def test_bind_raises_skips_address ():
0 commit comments