test_protocol.py 1.4 KB

123456789101112131415161718192021222324252627282930
  1. import sys
  2. import unittest
  3. from pathlib import Path
  4. sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
  5. from generated import vkfly
  6. class ProtocolTest(unittest.TestCase):
  7. def test_roundtrip_standard_and_vk(self):
  8. writer=vkfly.MAVLink(None,srcSystem=1,srcComponent=1)
  9. reader=vkfly.MAVLink(None);reader.robust_parsing=True
  10. 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)]
  11. for original in msgs:
  12. decoded=None
  13. for b in original.pack(writer):
  14. result=reader.parse_char(bytes([b]))
  15. if result and result.get_type()!='BAD_DATA': decoded=result
  16. self.assertIsNotNone(decoded)
  17. self.assertEqual(decoded.get_type(),original.get_type())
  18. self.assertEqual(decoded.get_srcSystem(),1)
  19. def test_unknown_data_does_not_break_parser(self):
  20. reader=vkfly.MAVLink(None);reader.robust_parsing=True
  21. for b in b'not a mavlink packet': reader.parse_char(bytes([b]))
  22. writer=vkfly.MAVLink(None,srcSystem=1,srcComponent=1)
  23. result=None
  24. for b in writer.heartbeat_encode(2,3,0,0,4,3).pack(writer):
  25. msg=reader.parse_char(bytes([b]))
  26. if msg and msg.get_type()=='HEARTBEAT':result=msg
  27. self.assertIsNotNone(result)
  28. if __name__=='__main__':unittest.main()