Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _unittest_can_media_filter_faults() -> None:
from pytest import raises
with raises(ValueError):
FilterConfiguration(0, -1, None)
with raises(ValueError):
FilterConfiguration(-1, 0, None)
for fmt in FrameFormat:
with raises(ValueError):
FilterConfiguration(2 ** int(fmt), 0, fmt)
with raises(ValueError):
FilterConfiguration(0, 2 ** int(fmt), fmt)
with raises(ValueError):
optimize_filter_configurations([], 0)
def _unittest_can_media_filter_str() -> None:
assert str(FilterConfiguration(0b10101010,
0b11101000,
FrameFormat.EXTENDED)) == 'ext:xxxxxxxxxxxxxxxxxxxxx101x1xxx'
assert str(FilterConfiguration(0b10101010101010101010101010101,
0b10111111111111111111111111111,
FrameFormat.EXTENDED)) == 'ext:1x101010101010101010101010101'
assert str(FilterConfiguration(0b10101010101, 0b11111111111, FrameFormat.BASE)) == 'bas:10101010101'
assert str(FilterConfiguration(123, 456, None)) == 'any:xxxxxxxxxxxxxxxxxxxx001xx1xxx'
assert str(FilterConfiguration.new_promiscuous()) == 'any:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
assert repr(FilterConfiguration(123, 456, None)) == 'FilterConfiguration(identifier=123, mask=456, format=None)'
def merge(self, other: FilterConfiguration) -> FilterConfiguration:
"""
This is a part of the CAN acceptance filter configuration optimization algorithm;
see :func:`optimize_filter_configurations`.
Given two filter configurations ``A`` and ``B``, where ``A`` accepts CAN frames whose identifiers
belong to ``Ca`` and likewise ``Cb`` for ``B``, the merge product of ``A`` and ``B`` would be a
new filter configuration that accepts CAN frames belonging to a new set which is a superset of
the union of ``Ca`` and ``Cb``.
"""
mask = self.mask & other.mask & ~(self.identifier ^ other.identifier)
identifier = self.identifier & mask
fmt = self.format if self.format == other.format else None
return FilterConfiguration(identifier=identifier, mask=mask, format=fmt)
print([str(r) for r in reduced])
reduced = FilterConfiguration.compact(generate_filter_configurations(reference_subject_ids, 0b1010101), 3)
assert reduced == [
ext(idn=0b_000_1_0_000000000_1010101_0000000_1,
msk=0b_000_1_0_000000000_1111111_0000000_1), # Services
ext(idn=0b_000_0_0_0000000000000000_1010101_1, # Loopback messages
msk=0b_000_1_0_0000000000000000_1111111_1),
ext(idn=0b_000_0_0_0000000000000000_0000000_1,
msk=0b_000_1_0_1111111111000000_0000000_1),
]
print([str(r) for r in reduced])
reduced = FilterConfiguration.compact(generate_filter_configurations(reference_subject_ids, 0b1010101), 1)
assert reduced == [
ext(idn=0b_000_0_0_000000000_0000000_0000000_1,
msk=0b_000_0_0_000000000_0000000_0000000_1), # Degenerates to checking only protocol version
]
print([str(r) for r in reduced])
def _unittest_can_media_filter_str() -> None:
assert str(FilterConfiguration(0b10101010,
0b11101000,
FrameFormat.EXTENDED)) == 'ext:xxxxxxxxxxxxxxxxxxxxx101x1xxx'
assert str(FilterConfiguration(0b10101010101010101010101010101,
0b10111111111111111111111111111,
FrameFormat.EXTENDED)) == 'ext:1x101010101010101010101010101'
assert str(FilterConfiguration(0b10101010101, 0b11111111111, FrameFormat.BASE)) == 'bas:10101010101'
assert str(FilterConfiguration(123, 456, None)) == 'any:xxxxxxxxxxxxxxxxxxxx001xx1xxx'
assert str(FilterConfiguration.new_promiscuous()) == 'any:xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
assert repr(FilterConfiguration(123, 456, None)) == 'FilterConfiguration(identifier=123, mask=456, format=None)'
def _unittest_can_media_filter_merge() -> None:
assert FilterConfiguration(123456, 0, None).rank == -29 # Worst rank
assert FilterConfiguration(123456, 0b110, None).rank == -27 # Two better
assert FilterConfiguration(1234, 0b110, FrameFormat.BASE).rank == 2
assert FilterConfiguration(0b111, 0b111, FrameFormat.EXTENDED).merge(
FilterConfiguration(0b111, 0b111, FrameFormat.BASE)).rank == -29 + 3
def _unittest_can_media_filter_merge() -> None:
assert FilterConfiguration(123456, 0, None).rank == -29 # Worst rank
assert FilterConfiguration(123456, 0b110, None).rank == -27 # Two better
assert FilterConfiguration(1234, 0b110, FrameFormat.BASE).rank == 2
assert FilterConfiguration(0b111, 0b111, FrameFormat.EXTENDED).merge(
FilterConfiguration(0b111, 0b111, FrameFormat.BASE)).rank == -29 + 3
def new_promiscuous() -> FilterConfiguration:
"""Returns a configuration that accepts all frames."""
return FilterConfiguration(identifier=0, mask=0, format=None)
def ext(idn: int, msk: int) -> FilterConfiguration:
assert idn < 2 ** 29 and msk < 2 ** 29
return FilterConfiguration(identifier=idn, mask=msk, format=Frame.Format.EXTENDED)