Skip to content

Commit 219c4e7

Browse files
committed
Add additional checks on some fields.
1 parent 975d50a commit 219c4e7

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

horusdemodlib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.1.20"
1+
__version__ = "0.1.21"

horusdemodlib/decoder.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
32: 'horus_binary_v2_32byte'
8181
}
8282

83-
def decode_packet(data:bytes, packet_format:dict = None) -> dict:
83+
def decode_packet(data:bytes, packet_format:dict = None, ignore_crc:bool = False) -> dict:
8484
"""
8585
Attempt to decode a set of bytes based on a provided packet format.
8686
@@ -113,7 +113,7 @@ def decode_packet(data:bytes, packet_format:dict = None) -> dict:
113113
# Check the Checksum
114114
_crc_ok = check_packet_crc(data, checksum=packet_format['checksum'])
115115

116-
if not _crc_ok:
116+
if (not _crc_ok) and (not ignore_crc):
117117
raise ValueError("Decoder - CRC Failure.")
118118
else:
119119
_output['crc_ok'] = True
@@ -321,6 +321,32 @@ def parse_ukhas_string(sentence:str) -> dict:
321321
print(f"Input ({_format}): {str(_input)} - Caught Error: {str(e)}")
322322
assert(_output == 'error')
323323

324+
# Binary packet tests that break various fields
325+
tests = [
326+
# ID Seq---| HH MM SS Lat----------| Lon-----------| Alt---|
327+
['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', ''],
328+
['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'],
329+
['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'],
330+
['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'],
331+
['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'],
332+
['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'],
333+
]
334+
335+
for _test in tests:
336+
_format = _test[0]
337+
_input = _test[1]
338+
_output = _test[2]
339+
340+
try:
341+
_decoded = decode_packet(_input, ignore_crc=True)
342+
print(f"Input ({_format}): {str(_input)} - Output: {_decoded['ukhas_str']}")
343+
print(_decoded)
344+
# Insert assert checks here.
345+
346+
except ValueError as e:
347+
print(f"Input ({_format}): {str(_input)} - Caught Error: {str(e)}")
348+
assert(_output == 'error')
349+
324350
# RTTY Decoder Tests
325351
tests = [
326352
'$$HORUS,6,06:43:16,0.000000,0.000000,0,0,0,1801,20*1DA2',

horusdemodlib/delegates.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,16 @@ def decode_time_hms(data: bytes) -> str:
4040
raise ValueError(f"time_hms - Input has incorrect length ({len(data)}), should be 3.")
4141

4242
_hour = int(data[0])
43+
if _hour >= 24:
44+
raise ValueError(f"time_hms - Hour value ({_hour}) out of range 0-23.")
45+
4346
_minute = int(data[1])
47+
if _minute >= 60:
48+
raise ValueError(f"time_hms - Minute value ({_minute}) out of range 0-59.")
49+
4450
_second = int(data[2])
51+
if _second >= 60:
52+
raise ValueError(f"time_hms - Second value ({_second}) out of range 0-59.")
4553

4654
_str = f"{_hour:02d}:{_minute:02d}:{_second:02d}"
4755

@@ -65,7 +73,7 @@ def decode_time_biseconds(data:int) -> str:
6573
raise ValueError("time_biseconds - Invalid input type.")
6674

6775
if (data < 0) or data > 43200:
68-
raise ValueError("time_biseconds - Input of of range (0-43200)")
76+
raise ValueError("time_biseconds - Input out of range (0-43200)")
6977

7078
_str = time.strftime("%H:%M:%S", time.gmtime(data*2))
7179

@@ -81,6 +89,9 @@ def decode_degree_float(data:float) -> str:
8189
if type(data) != float:
8290
raise ValueError("decimal_degrees - Invalid input type.")
8391

92+
if (data < -180.0) or (data > 180.0):
93+
raise ValueError(f"decimal_degrees - Value ({data}) out of range -180 - 180.")
94+
8495
return (data, f"{data:.5f}")
8596

8697

@@ -110,6 +121,9 @@ def decode_degree_fixed3(data:bytes) -> str:
110121
_value = struct.unpack('<i', _temp)[0]
111122
_value_degrees = _value * 1e-7
112123

124+
if (_value_degrees < -180.0) or (_value_degrees > 180.0):
125+
raise ValueError(f"degree_fixed3 - Value ({_value_degrees}) out of range -180 - 180.")
126+
113127
return (_value_degrees, f"{_value_degrees:.5f}")
114128

115129

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "horusdemodlib"
3-
version = "0.1.20"
3+
version = "0.1.21"
44
description = "Project Horus HAB Telemetry Demodulators"
55
authors = ["Mark Jessop"]
66
license = "LGPL-2.1-or-later"

0 commit comments

Comments
 (0)