import sys import unittest from pathlib import Path sys.path.insert(0,str(Path(__file__).resolve().parents[1])) from generated import vkfly class ProtocolTest(unittest.TestCase): def test_roundtrip_standard_and_vk(self): writer=vkfly.MAVLink(None,srcSystem=1,srcComponent=1) reader=vkfly.MAVLink(None);reader.robust_parsing=True msgs=[writer.heartbeat_encode(2,3,0,0,4,3), writer.vkins_status_encode(1,1,0,0,0,0,0,0,0,9.8,0,0,0,0,12,10,20,0)] for original in msgs: decoded=None for b in original.pack(writer): result=reader.parse_char(bytes([b])) if result and result.get_type()!='BAD_DATA': decoded=result self.assertIsNotNone(decoded) self.assertEqual(decoded.get_type(),original.get_type()) self.assertEqual(decoded.get_srcSystem(),1) def test_unknown_data_does_not_break_parser(self): reader=vkfly.MAVLink(None);reader.robust_parsing=True for b in b'not a mavlink packet': reader.parse_char(bytes([b])) writer=vkfly.MAVLink(None,srcSystem=1,srcComponent=1) result=None for b in writer.heartbeat_encode(2,3,0,0,4,3).pack(writer): msg=reader.parse_char(bytes([b])) if msg and msg.get_type()=='HEARTBEAT':result=msg self.assertIsNotNone(result) if __name__=='__main__':unittest.main()