Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions test/back2back_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@

from .config import (
IS_CI,
IS_OSX,
IS_PYPY,
TEST_CAN_FD,
TEST_INTERFACE_SOCKETCAN,
TEST_UDP_MULTICAST_IPv4,
TEST_UDP_MULTICAST_IPv6,
)


Expand Down Expand Up @@ -291,16 +292,14 @@ class BasicTestSocketCan(Back2BackTestCase):
CHANNEL_2 = "vcan0"


# this doesn't even work on Travis CI for macOS; for example, see
# https://travis-ci.org/github/hardbyte/python-can/jobs/745389871
@unittest.skipIf(
IS_CI and IS_OSX,
"not supported for macOS CI",
)
@unittest.skipUnless(
is_msgpack_installed(raise_exception=False),
"msgpack not installed",
)
@unittest.skipUnless(
TEST_UDP_MULTICAST_IPv4,
"IPv4 UDP multicast not available on this host",
)
class BasicTestUdpMulticastBusIPv4(Back2BackTestCase):
INTERFACE_1 = "udp_multicast"
CHANNEL_1 = UdpMulticastBus.DEFAULT_GROUP_IPv4
Expand All @@ -312,16 +311,14 @@ def test_unique_message_instances(self):
super().test_unique_message_instances()


# this doesn't even work for loopback multicast addresses on Travis CI; for example, see
# https://travis-ci.org/github/hardbyte/python-can/builds/745065503
@unittest.skipIf(
IS_CI and IS_OSX,
"not supported for macOS CI",
)
@unittest.skipUnless(
is_msgpack_installed(raise_exception=False),
"msgpack not installed",
)
@unittest.skipUnless(
TEST_UDP_MULTICAST_IPv6,
"IPv6 UDP multicast not available on this host",
)
class BasicTestUdpMulticastBusIPv6(Back2BackTestCase):
HOST_LOCAL_MCAST_GROUP_IPv6 = "ff11:7079:7468:6f6e:6465:6d6f:6d63:6173"

Expand Down
36 changes: 36 additions & 0 deletions test/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"""

import platform
import socket
import struct
from os import environ as environment


Expand Down Expand Up @@ -59,3 +61,37 @@ def env(name: str) -> bool:
TEST_CAN_FD = True

TEST_INTERFACE_SOCKETCAN = IS_LINUX and env("TEST_SOCKETCAN")


def _can_use_multicast(group: str) -> bool:
"""Check whether the host can send a datagram to the multicast ``group`` and receive it back.

This mirrors what :class:`~can.interfaces.udp_multicast.UdpMulticastBus` does: join the group
on an interface chosen by the kernel and rely on multicast loopback. It fails, for example, in
containers without an IPv6 multicast route or on hosts whose firewall drops incoming multicast.
"""
is_ipv6 = ":" in group
family = socket.AF_INET6 if is_ipv6 else socket.AF_INET
probe = b"python-can multicast probe"
try:
with socket.socket(family, socket.SOCK_DGRAM) as sock:
sock.settimeout(0.5)
sock.bind(("", 0))
port = sock.getsockname()[1]
if is_ipv6:
# interface index 0 lets the kernel choose the interface by route lookup
request = socket.inet_pton(family, group) + struct.pack("@I", 0)
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, request)
else:
request = socket.inet_aton(group) + struct.pack("@I", socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, request)
sock.sendto(probe, (group, port))
return sock.recv(len(probe)) == probe
except OSError: # includes the timeout
return False


# The probe groups are arbitrary: the kernel picks the interface by a route lookup for the whole
# multicast range, so every group of the same family and scope gives the same result.
TEST_UDP_MULTICAST_IPv4 = _can_use_multicast("239.255.0.1")
TEST_UDP_MULTICAST_IPv6 = _can_use_multicast("ff11::1")
Loading