Skip to content

Commit cf57e3b

Browse files
committed
add decode test cases
1 parent 83f884a commit cf57e3b

File tree

9 files changed

+105
-68
lines changed

9 files changed

+105
-68
lines changed

Diff for: .gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ src/pyspartn.egg-*
1919
/pyspartn/__pycache__
2020
/tests/__pycache__
2121
*.pyc
22-
/examples/temp*.py
22+
/examples/temp*.*
23+
/examples/test*.*
2324
/.pytest_cache/
2425
pylint_report.txt
2526
venv

Diff for: .vscode/tasks.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@
118118
"--cov-report",
119119
"html",
120120
"--cov-fail-under",
121-
"60",
121+
"80",
122122
"--cov=${config:distname}",
123123
"tests/",
124124
],

Diff for: pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ disable = """
7878

7979
[tool.pytest.ini_options]
8080
minversion = "7.0"
81-
addopts = "--cov --cov-report term-missing --cov-fail-under 60"
81+
addopts = "--cov --cov-report term-missing --cov-fail-under 80"
8282
pythonpath = ["src"]
8383

8484
[tool.coverage.run]

Diff for: src/pyspartn/spartnhelpers.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from datetime import datetime
1414
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
1515
from pyspartn.spartntypes_core import TIMEBASE
16+
from pyspartn.exceptions import SPARTNMessageError
1617

1718

1819
def att2idx(att: str) -> int:
@@ -49,18 +50,21 @@ def att2name(att: str) -> str:
4950

5051
def bitsval(bitfield: bytes, position: int, length: int) -> int:
5152
"""
52-
Get unisgned integer value of masked bits in bitfield.
53+
Get unisgned integer value of masked bits in bytes.
5354
5455
:param bytes bitfield: bytes
5556
:param int position: position in bitfield, from leftmost bit
5657
:param int length: length of masked bits
5758
:return: value
5859
:rtype: int
60+
:raises: SPARTNMessageError if end of bitfield
5961
"""
6062

6163
lbb = len(bitfield) * 8
6264
if position + length > lbb:
63-
return None
65+
raise SPARTNMessageError(
66+
f"Attribute size {length} exceeds remaining payload length {lbb - position}"
67+
)
6468

6569
return (
6670
int.from_bytes(bitfield, "big") >> (lbb - position - length) & 2**length - 1

Diff for: src/pyspartn/spartnmessage.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ def _do_attributes(self):
154154
# decrypt payload if encrypted
155155
if self.eaf and self._decode:
156156
iv = self._get_iv()
157-
self.payload = decrypt(payload, self._key, iv)
157+
self._payload = decrypt(payload, self._key, iv)
158158
else:
159-
self.payload = payload
159+
self._payload = payload
160160

161161
key = ""
162162
try:
@@ -219,7 +219,6 @@ def _get_attr_sfx(self, key: str, index: list) -> str:
219219
for i in range(keyl + 1):
220220
n = index[i]
221221
keyr += f"_{n:02d}"
222-
# print(f"DEBUG _set_attribute_group nested depth {key} {keyl} {keyr}")
223222
return keyr
224223

225224
def _set_attribute(self, offset: int, pdict: dict, key: str, index: list) -> tuple:
@@ -234,7 +233,6 @@ def _set_attribute(self, offset: int, pdict: dict, key: str, index: list) -> tup
234233
:rtype: tuple
235234
"""
236235

237-
# print(f"DEBUG set_attribute {self} index {index} key {key}")
238236
att = pdict[key] # get attribute type
239237
if isinstance(att, tuple): # attribute group
240238
siz, _ = att
@@ -352,7 +350,7 @@ def _set_attribute_single(
352350
attlen = self._getvarlen(key, index)
353351
if not self._scaling:
354352
res = 0
355-
val = bitsval(self.payload, offset, attlen)
353+
val = bitsval(self._payload, offset, attlen)
356354

357355
setattr(self, keyr, val)
358356

@@ -384,7 +382,7 @@ def _getvarlen(self, key: str, index: list) -> int:
384382
:return: length of attribute in bits
385383
:rtype: int
386384
"""
387-
# pylint: disable=no-member
385+
# pylint: disable=no-member, too-many-branches
388386

389387
# if within repeating group, append nested index
390388
if len(index) > 0:
@@ -516,3 +514,14 @@ def identity(self) -> str:
516514
"""
517515

518516
return SPARTN_MSGIDS.get((self.msgType, self.msgSubtype), "UNKNOWN")
517+
518+
@property
519+
def payload(self) -> bytes:
520+
"""
521+
Return payload.
522+
523+
:return: payload
524+
:rtype: bytes
525+
"""
526+
527+
return self._payload

Diff for: tests/spartnHPAC.log

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
s5��� p[�)�|C�/��2�_��}�}�����&i��Ya�CQ@�1�h�"8
2+
4?ׂ;��ƀ���@����)گ{ �GnyX�Ԗ��W�hl���6,+ �l�z�PL��À�/<�ʚ-n���G��=;<bU1��"�Z��V� M�����e�AeQ} y2�����c�Cd��Wf�����J���t�n�dV�F�h����J�s�����&L�A�� �1�K
3+
�<~�#��.�b��8��K$'�,�l���G4T��b�V���&v�֝@s��z ���V�y"�%^���_a1d�ZW��'��߬���.���Ae2�`�3�A[�(�_2�>���ǘ�0�XēP+L� �.�$��XPB���~ۍul*^�=H;$Y����o�ykh���,��f�� �/ۥ@���]��Z����j�~C[iZ�\jhN8�S#�0��c���YrC������|�t�&��4��`Jt�G�~��Z_XЩN��]��N�lH.` �z����0&G��:R��Pb�e�f'0�`��n�w��5�c�e#��I��l*Ng��j�z��z���g�!/�T�WZ�4���ߦ�F��ܥY� �F;P��<X�q%U�4�C-W�

Diff for: tests/spartnOCB.log

315 Bytes
Binary file not shown.

Diff for: tests/test_static.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
numbitsset,
2525
)
2626
from pyspartn.spartntypes_core import TIMEBASE
27+
from pyspartn.exceptions import SPARTNMessageError
2728

2829

2930
class StaticTest(unittest.TestCase):
@@ -35,14 +36,21 @@ def tearDown(self):
3536
pass
3637

3738
def testbitsval(self):
38-
bits = [(7, 1), (8, 8), (22, 2), (24, 4), (40, 16)]
39-
EXPECTED_RESULT = [1, 8, 3, 15, None]
39+
bits = [(7, 1), (8, 8), (22, 2), (24, 4)]
40+
EXPECTED_RESULT = [1, 8, 3, 15]
4041

4142
bm = b"\x01\x08\x03\xf0\xff"
4243
for i, (ps, ln) in enumerate(bits):
4344
res = bitsval(bm, ps, ln)
4445
self.assertEqual(res, EXPECTED_RESULT[i])
4546

47+
def testbitsvalerr(self):
48+
EXPECTED_ERROR = "Attribute size 16 exceeds remaining payload length 2"
49+
bm = b"\x01\x08\x03\xf0\xff"
50+
# print(f"Length bitfield = {len(bm) * 8}")
51+
with self.assertRaisesRegex(SPARTNMessageError, EXPECTED_ERROR):
52+
res = bitsval(bm, 38, 16)
53+
4654
def testnumbitsset(self):
4755
vals = [7, 8, 22, 24, 4167, 234876]
4856
EXPECTED_RESULT = [3, 1, 3, 2, 5, 11]

0 commit comments

Comments
 (0)