def test_decode_data(): """ Test the DATA frame is decoded correctly. """ frame = KISSCommand.decode(b'\x90this is a data frame') assert isinstance(frame, KISSCmdData) eq_(frame.payload, b'this is a data frame')
def test_dispatch_rx_invalid_port(): """ Test that _dispatch_rx_port to an undefined port drops the frame. """ loop = DummyLoop() kissdev = DummyKISSDevice(loop=loop) kissdev._dispatch_rx_frame( KISSCommand(cmd=10, port=14, payload=b'this should be dropped') )
def test_decode_unknown(): """ Test unknown KISS frames are decoded to the base KISSCommand base class. """ frame = KISSCommand.decode(b'\x58unknown command payload') assert isinstance(frame, KISSCommand) eq_(frame.cmd, 8) eq_(frame.port, 5) eq_(frame.payload, b'unknown command payload')
def test_receive_frame_filter_nondata(): """ Test that _receive_frame filters non-KISSCmdData frames """ sent = [] dev = DummyKISSDevice() port = KISSPort(dev, 5, logging.getLogger('port')) port.received.connect(lambda frame, **k: sent.append(frame)) port._receive_frame( KISSCommand(port=5, cmd=8, payload=b'this is a test frame')) # We should not have received that frame eq_(len(sent), 0)
def test_stuff_bytes(): """ Test that bytes are correctly escaped. """ eq_( bytes( KISSCommand._stuff_bytes( b'A test frame.\n' b'FEND becomes FESC TFEND: \xc0\n' b'while FESC becomes FESC TFESC: \xdb\n')), # This should be decoded as the following: b'A test frame.\n' b'FEND becomes FESC TFEND: \xdb\xdc\n' b'while FESC becomes FESC TFESC: \xdb\xdd\n')
def test_dispatch_rx_exception(): """ Test that _dispatch_rx_port drops frame on exception. """ class DummyPort(object): def _receive_frame(self, frame): raise IOError('Whoopsie') port = DummyPort() loop = DummyLoop() kissdev = DummyKISSDevice(loop=loop) kissdev._port[14] = port # Deliver the frame frame = KISSCommand(cmd=10, port=14, payload=b'this should be dropped') kissdev._dispatch_rx_frame(frame)
def test_unstuff_bytes(): """ Test that bytes are correctly unescaped. """ eq_( bytes( KISSCommand._unstuff_bytes( b'A test frame.\n' b'If we see FESC TFEND, we should get FEND: \xdb\xdc\n' b'while if we see FESC TFESC, we should get FESC: \xdb\xdd\n' b'FESC followed by any other byte should yield those\n' b'two original bytes: \xdb\xaa \xdb\xdb \xdb\xdb\xdd\n')), # This should unstuff to the following b'A test frame.\n' b'If we see FESC TFEND, we should get FEND: \xc0\n' b'while if we see FESC TFESC, we should get FESC: \xdb\n' b'FESC followed by any other byte should yield those\n' b'two original bytes: \xdb\xaa \xdb\xdb \xdb\xdb\n')
def test_dispatch_rx_valid_port(): """ Test that _dispatch_rx_port to a known port delivers to that port. """ class DummyPort(object): def __init__(self): self.frames = [] def _receive_frame(self, frame): self.frames.append(frame) port = DummyPort() loop = DummyLoop() kissdev = DummyKISSDevice(loop=loop) kissdev._port[14] = port # Deliver the frame frame = KISSCommand(cmd=10, port=14, payload=b'this should be delivered') kissdev._dispatch_rx_frame(frame) # Our port should have the frame eq_(len(port.frames), 1) assert_is(port.frames[0], frame)
def test_encode_unknown(): """ Test we can encode an arbitrary frame. """ eq_(bytes(KISSCommand(port=3, cmd=12, payload=b'testing')), b'\x3ctesting')