From 219c4e7686708c06ce7df5ce92ae8002078be154 Mon Sep 17 00:00:00 2001 From: Mark Jessop Date: Sat, 30 Jan 2021 10:44:46 +1030 Subject: [PATCH] Add additional checks on some fields. --- horusdemodlib/__init__.py | 2 +- horusdemodlib/decoder.py | 30 ++++++++++++++++++++++++++++-- horusdemodlib/delegates.py | 16 +++++++++++++++- pyproject.toml | 2 +- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/horusdemodlib/__init__.py b/horusdemodlib/__init__.py index 8754a47..f4bd716 100755 --- a/horusdemodlib/__init__.py +++ b/horusdemodlib/__init__.py @@ -1 +1 @@ -__version__ = "0.1.20" +__version__ = "0.1.21" diff --git a/horusdemodlib/decoder.py b/horusdemodlib/decoder.py index 9ea7fd0..49229ef 100644 --- a/horusdemodlib/decoder.py +++ b/horusdemodlib/decoder.py @@ -80,7 +80,7 @@ 32: 'horus_binary_v2_32byte' } -def decode_packet(data:bytes, packet_format:dict = None) -> dict: +def decode_packet(data:bytes, packet_format:dict = None, ignore_crc:bool = False) -> dict: """ Attempt to decode a set of bytes based on a provided packet format. @@ -113,7 +113,7 @@ def decode_packet(data:bytes, packet_format:dict = None) -> dict: # Check the Checksum _crc_ok = check_packet_crc(data, checksum=packet_format['checksum']) - if not _crc_ok: + if (not _crc_ok) and (not ignore_crc): raise ValueError("Decoder - CRC Failure.") else: _output['crc_ok'] = True @@ -321,6 +321,32 @@ def parse_ukhas_string(sentence:str) -> dict: print(f"Input ({_format}): {str(_input)} - Caught Error: {str(e)}") assert(_output == 'error') + # Binary packet tests that break various fields + tests = [ + # ID Seq---| HH MM SS Lat----------| Lon-----------| Alt---| + ['horus_binary_v1', b'\x01\x12\x00\x00\x00\x23\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1C\x9A\x95\x45', ''], + ['horus_binary_v1', b'\x01\x12\x00\x18\x00\x23\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1C\x9A\x95\x45', 'error'], + ['horus_binary_v1', b'\x01\x12\x00\x00\x3c\x23\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1C\x9A\x95\x45', 'error'], + ['horus_binary_v1', b'\x01\x12\x00\x00\x00\x3c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1C\x9A\x95\x45', 'error'], + ['horus_binary_v1', b'\x01\x12\x00\x00\x00\x23\x00\x00\x35\x43\x00\x00\x00\x00\x00\x00\x00\x00\x1C\x9A\x95\x45', 'error'], + ['horus_binary_v1', b'\x01\x12\x00\x00\x00\x23\x00\x80\x34\xc3\x00\x00\x00\x00\x00\x00\x00\x00\x1C\x9A\x95\x45', 'error'], + ] + + for _test in tests: + _format = _test[0] + _input = _test[1] + _output = _test[2] + + try: + _decoded = decode_packet(_input, ignore_crc=True) + print(f"Input ({_format}): {str(_input)} - Output: {_decoded['ukhas_str']}") + print(_decoded) + # Insert assert checks here. + + except ValueError as e: + print(f"Input ({_format}): {str(_input)} - Caught Error: {str(e)}") + assert(_output == 'error') + # RTTY Decoder Tests tests = [ '$$HORUS,6,06:43:16,0.000000,0.000000,0,0,0,1801,20*1DA2', diff --git a/horusdemodlib/delegates.py b/horusdemodlib/delegates.py index 89578c2..8b5d900 100644 --- a/horusdemodlib/delegates.py +++ b/horusdemodlib/delegates.py @@ -40,8 +40,16 @@ def decode_time_hms(data: bytes) -> str: raise ValueError(f"time_hms - Input has incorrect length ({len(data)}), should be 3.") _hour = int(data[0]) + if _hour >= 24: + raise ValueError(f"time_hms - Hour value ({_hour}) out of range 0-23.") + _minute = int(data[1]) + if _minute >= 60: + raise ValueError(f"time_hms - Minute value ({_minute}) out of range 0-59.") + _second = int(data[2]) + if _second >= 60: + raise ValueError(f"time_hms - Second value ({_second}) out of range 0-59.") _str = f"{_hour:02d}:{_minute:02d}:{_second:02d}" @@ -65,7 +73,7 @@ def decode_time_biseconds(data:int) -> str: raise ValueError("time_biseconds - Invalid input type.") if (data < 0) or data > 43200: - raise ValueError("time_biseconds - Input of of range (0-43200)") + raise ValueError("time_biseconds - Input out of range (0-43200)") _str = time.strftime("%H:%M:%S", time.gmtime(data*2)) @@ -81,6 +89,9 @@ def decode_degree_float(data:float) -> str: if type(data) != float: raise ValueError("decimal_degrees - Invalid input type.") + if (data < -180.0) or (data > 180.0): + raise ValueError(f"decimal_degrees - Value ({data}) out of range -180 - 180.") + return (data, f"{data:.5f}") @@ -110,6 +121,9 @@ def decode_degree_fixed3(data:bytes) -> str: _value = struct.unpack(' 180.0): + raise ValueError(f"degree_fixed3 - Value ({_value_degrees}) out of range -180 - 180.") + return (_value_degrees, f"{_value_degrees:.5f}") diff --git a/pyproject.toml b/pyproject.toml index 62017e8..e4b501a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "horusdemodlib" -version = "0.1.20" +version = "0.1.21" description = "Project Horus HAB Telemetry Demodulators" authors = ["Mark Jessop"] license = "LGPL-2.1-or-later"