@@ -165,8 +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 (
169+ socket .socket (socket .AF_INET , socket .SOCK_DGRAM ) as sock ,
170170 pytest .raises (OSError ),
171171 patch .object (netutils .log , "error" , _log_error ),
172172 patch ("socket.socket.setsockopt" , side_effect = OSError ),
@@ -182,100 +182,117 @@ def _log_error(*args):
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 ):
196+ with (
197+ socket .socket (socket .AF_INET , socket .SOCK_DGRAM ) as sock ,
198+ patch ("socket.socket.setsockopt" , side_effect = OSError ),
199+ ):
198200 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 )
206-
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 )
210-
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 )
214-
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 )
207+ with socket .socket (socket .AF_INET , socket .SOCK_DGRAM ) as sock :
208+ # Should raise on EPERM always
209+ with (
210+ pytest .raises (OSError ),
211+ patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EPERM , None )),
212+ ):
213+ netutils .set_mdns_port_socket_options_for_ip_version (sock , ("" ,), r .IPVersion .V4Only )
214+
215+ # Should raise on EINVAL always when bind address is not ''
216+ with (
217+ pytest .raises (OSError ),
218+ patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EINVAL , None )),
219+ ):
220+ netutils .set_mdns_port_socket_options_for_ip_version (sock , ("127.0.0.1" ,), r .IPVersion .V4Only )
221+
222+ # Should not raise on EINVAL when bind address is ''
223+ with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EINVAL , None )):
224+ netutils .set_mdns_port_socket_options_for_ip_version (sock , ("" ,), r .IPVersion .V4Only )
218225
219226
220227def 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
228+ with socket .socket (socket .AF_INET , socket .SOCK_DGRAM ) as sock :
229+ interface = "127.0.0.1"
230+
231+ # EPERM should always raise
232+ with (
233+ pytest .raises (OSError ),
234+ patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EPERM , None )),
235+ ):
236+ netutils .add_multicast_member (sock , interface )
237+
238+ # EADDRINUSE should return False
239+ with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EADDRINUSE , None )):
240+ assert netutils .add_multicast_member (sock , interface ) is False
241+
242+ # EADDRNOTAVAIL should return False
243+ with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EADDRNOTAVAIL , None )):
244+ assert netutils .add_multicast_member (sock , interface ) is False
245+
246+ # EINVAL should return False
247+ with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .EINVAL , None )):
248+ assert netutils .add_multicast_member (sock , interface ) is False
249+
250+ # ENOPROTOOPT should return False
251+ with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .ENOPROTOOPT , None )):
252+ assert netutils .add_multicast_member (sock , interface ) is False
253+
254+ # ENODEV should raise for ipv4
255+ with (
256+ pytest .raises (OSError ),
257+ patch ("socket.socket.setsockopt" , side_effect = OSError (errno .ENODEV , None )),
258+ ):
259+ assert netutils .add_multicast_member (sock , interface ) is False
260+
261+ # ENODEV should return False for ipv6
262+ with patch ("socket.socket.setsockopt" , side_effect = OSError (errno .ENODEV , None )):
263+ assert netutils .add_multicast_member (sock , ("2001:db8::" , 1 , 1 )) is False # type: ignore[arg-type]
264+
265+ # No IPv6 support should return False for IPv6
266+ with patch ("socket.inet_pton" , side_effect = OSError ()):
267+ assert netutils .add_multicast_member (sock , ("2001:db8::" , 1 , 1 )) is False # type: ignore[arg-type]
268+
269+ # No error should return True
270+ with patch ("socket.socket.setsockopt" ):
271+ assert netutils .add_multicast_member (sock , interface ) is True
272+
273+ # Ran out of IGMP memberships is forgiving and logs about igmp_max_memberships on linux
274+ caplog .clear ()
275+ with (
276+ patch .object (sys , "platform" , "linux" ),
277+ patch (
278+ "socket.socket.setsockopt" , side_effect = OSError (errno .ENOBUFS , "No buffer space available" )
279+ ),
280+ ):
281+ assert netutils .add_multicast_member (sock , interface ) is False
282+ assert "No buffer space available" in caplog .text
283+ assert "net.ipv4.igmp_max_memberships" in caplog .text
284+
285+ # Ran out of IGMP memberships is forgiving and logs
286+ caplog .clear ()
287+ with (
288+ patch .object (sys , "platform" , "darwin" ),
289+ patch (
290+ "socket.socket.setsockopt" , side_effect = OSError (errno .ENOBUFS , "No buffer space available" )
291+ ),
292+ ):
293+ assert netutils .add_multicast_member (sock , interface ) is False
294+ assert "No buffer space available" in caplog .text
295+ assert "net.ipv4.igmp_max_memberships" not in caplog .text
279296
280297
281298def test_bind_raises_skips_address ():
0 commit comments