def test_listener_deinit(can=builtin_bus_factory): with can() as b: extended_matches = [ Match(0x1FFFCAFE, mask=0xFFFF, extended=True), Match(0x0BEEF000, mask=0x1FFFF000, extended=True), ] standard_matches = [ Match(0x400, mask=0x7F0), # should allow 0x40* Match(0x70A, mask=0x70F), # should allow 0x7*A ] with b.listen(extended_matches, timeout=0.1) as l: # mask matching mo = Message(id=0x0BEEF000, extended=True, data=b"") b.send(mo) mi = l.receive() assert mi # non-matching mo = Message(id=0x1FFFFAFF, extended=True, data=b"") b.send(mo) mi = l.receive() assert not mi with b.listen(standard_matches, timeout=0.1) as l: # non-matching mo = RemoteTransmissionRequest(id=0x418, length=0) b.send(mo) mi = l.receive() assert not mi, "ID 0x418 not blocked by masked filter" # masked matching mo = RemoteTransmissionRequest(id=0x7FA, length=0) b.send(mo) mi = l.receive() assert mi, "ID 0x7FA blocked by masked filter"
def test_mcp_standard_id_masked_filters(can=builtin_bus_factory): print("Test MCP Standard ID masked filters") standard_matches = [ Match(0x400, mask=0x7F0), # should allow 0x40* Match(0x70A, mask=0x70F), # should allow 0x7*A ] with can() as b, b.listen(standard_matches, timeout=0.1) as l: # masked matching mo = RemoteTransmissionRequest(id=0x408, length=0) b.send(mo) mi = l.receive() assert mi, "ID 0x408 not blocked by masked filter" # # non-matching mo = RemoteTransmissionRequest(id=0x418, length=0) b.send(mo) mi = l.receive() assert not mi, "ID 0x418 not blocked by masked filter" # masked matching mo = RemoteTransmissionRequest(id=0x7FA, length=0) b.send(mo) mi = l.receive() assert mi, "ID 0x7FA blocked by masked filter" # non-matching mo = RemoteTransmissionRequest(id=0x7FB, length=0) b.send(mo) mi = l.receive() assert not mi, "ID 0x7FB not blocked by masked filter"
def test_mcp_standard_id_exact_filters(can=builtin_bus_factory): print("Test MCP Standard ID filters") standard_matches = [ Match(0x408), Match(max_standard_id), ] with can() as b, b.listen(standard_matches, timeout=0.1) as l: # exact ID matching mo = RemoteTransmissionRequest(id=0x408, length=0) b.send(mo) mi = l.receive() assert mi # # mask matching mo = RemoteTransmissionRequest(id=max_standard_id, length=0) b.send(mo) mi = l.receive() assert mi # non-matching mo = RemoteTransmissionRequest(id=0x409, length=0) b.send(mo) mi = l.receive() assert not mi, "ID 0x409 not blocked by filters & masks" # non-matching mo = RemoteTransmissionRequest(id=(max_standard_id - 1), length=0) b.send(mo) mi = l.receive() assert not mi, "ID %x not blocked by filters & masks" % (max_standard_id - 1)
def test_filters2(can=builtin_bus_factory): matches = [ Match(0x408), Match(0x700, mask=0x7F0), Match(0x4081975, extended=True), Match(0x888800, mask=0xFFFFF0, extended=True), ] print("Test filters 2") with can() as b, b.listen(matches[0:2], timeout=0.1) as l1, b.listen( matches[2:4], timeout=0.1 ) as l2: # exact ID matching mo = RemoteTransmissionRequest(id=0x408, length=0) b.send(mo) mi1 = l1.receive() mi2 = l2.receive() assert mi1 assert not mi2 # mask matching mo = RemoteTransmissionRequest(id=0x704, length=0) b.send(mo) mi1 = l1.receive() mi2 = l2.receive() assert mi1 assert not mi2 # non-matching mo = RemoteTransmissionRequest(id=0x409, length=0) b.send(mo) mi1 = l1.receive() mi2 = l2.receive() assert not mi1 assert not mi2 # Extended # exact ID matching mo = Message(id=0x4081975, extended=True, data=b"") b.send(mo) mi1 = l1.receive() mi2 = l2.receive() assert not mi1 assert mi2 # mask matching mo = Message(id=0x888804, extended=True, data=b"") b.send(mo) mi1 = l1.receive() mi2 = l2.receive() assert not mi1 assert mi2 # non-matching mo = Message(id=0x4081985, extended=True, data=b"") b.send(mo) mi1 = l1.receive() mi2 = l2.receive() assert not mi1 assert not mi2
def test_filters1(can=builtin_bus_factory): matches = [ Match(0x408), Match(0x700, mask=0x7F0), Match(0x4081975, extended=True), Match(0x888800, mask=0xFFFFF0, extended=True), ] print("Test filters") with can() as b, b.listen(matches, timeout=0.1) as l: # exact ID matching mo = RemoteTransmissionRequest(id=0x408, length=0) b.send(mo) mi = l.receive() assert mi # mask matching mo = RemoteTransmissionRequest(id=0x704, length=0) b.send(mo) mi = l.receive() assert mi # non-matching # 0x409 => 0b 0000 0100 0000 1001 mo = RemoteTransmissionRequest(id=0x409, length=0) b.send(mo) mi = l.receive() assert not mi, "ID 0x409 not blocked by filters & masks" # Extended # exact ID matching mo = Message(id=0x4081975, extended=True, data=b"") b.send(mo) mi = l.receive() assert mi # mask matching mo = Message(id=0x888804, extended=True, data=b"") b.send(mo) mi = l.receive() assert mi # non-matching mo = Message(id=0x4081985, extended=True, data=b"") b.send(mo) mi = l.receive() assert not mi
def test_rtr_constructor(): print("Testing RemoteTransmissionRequest") assert RemoteTransmissionRequest(id=0, length=0).id == 0 assert RemoteTransmissionRequest(id=1, length=0).id == 1 for i in lengths: assert RemoteTransmissionRequest(id=0, length=i).length == i assert not RemoteTransmissionRequest(id=0, length=1).extended assert not RemoteTransmissionRequest(id=0, extended=False, length=1).extended assert RemoteTransmissionRequest(id=0, extended=True, length=1).extended
def test_rtr_receive(can=builtin_bus_factory): with can() as b, b.listen(timeout=0.1) as l: for length in lengths: print("Test messages of length", length) mo = RemoteTransmissionRequest(id=0x5555555, extended=True, length=length) b.send(mo) mi = l.receive() assert mi assert isinstance(mi, RemoteTransmissionRequest) assert mi.id == 0x5555555, "Extended ID does not match: 0x{:07X}".format( mi.id ) assert mi.extended assert mi.length == length mo = RemoteTransmissionRequest(id=max_standard_id, length=length) b.send(mo) mi = l.receive() assert mi assert isinstance(mi, RemoteTransmissionRequest) assert ( mi.id == max_standard_id ), "Max standard ID not sent/received properly: %s" % hex(mi.id) assert mi.length == length mo = RemoteTransmissionRequest(id=0x555, length=length) b.send(mo) mi = l.receive() assert mi # this fails every other run! assert isinstance(mi, RemoteTransmissionRequest) assert mi.id == 0x555 assert mi.length == length mo = RemoteTransmissionRequest( id=max_extended_id, extended=True, length=length ) assert mo.extended b.send(mo) mi = l.receive() assert mi assert isinstance(mi, RemoteTransmissionRequest) assert ( mi.id == max_extended_id ), "Max extended ID not sent/received properly" assert mi.length == length data = bytes(range(length)) mo = Message(id=0, data=data) b.send(mo) mi = l.receive() assert mi assert isinstance(mi, Message) assert mi.data == data mo = Message(id=max_extended_id, extended=True, data=data) b.send(mo) mi = l.receive() assert mi assert isinstance(mi, Message) assert mi.data == data