Skip to content

Commit 785d484

Browse files
committed
utils: Fix bugs in DataObject encoders
The DataObject is some weird / rarely used different code than the normal TLV encoder/decoder. It has apparently so far only been used for decoding, without testing the encoding side, resulting in related bugs. Let's fix those that I encountered today, and add a test case. Change-Id: I31370066f43c22fc3ce9e2b9ee75986a652f6fc4
1 parent b7f35ac commit 785d484

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

pySim/utils.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ def to_tlv(self) -> bytes:
13331333
bytes encoded in TLV format.
13341334
"""
13351335
val = self.to_bytes()
1336-
return bytes(self._compute_tag()) + bytes(len(val)) + val
1336+
return bertlv_encode_tag(self._compute_tag()) + bertlv_encode_len(len(val)) + val
13371337

13381338
# 'codec' interface
13391339
def decode(self, binary: bytes) -> Tuple[dict, bytes]:
@@ -1481,7 +1481,8 @@ def decode(self, binary: bytes) -> Tuple[dict, bytes]:
14811481

14821482
# 'codec' interface
14831483
def encode(self, decoded) -> bytes:
1484-
obj = self.members_by_name(decoded[0])
1484+
obj = self.members_by_name[list(decoded)[0]]
1485+
obj.decoded = list(decoded.values())[0]
14851486
return obj.to_tlv()
14861487

14871488

tests/test_utils.py

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44
from pySim import utils
55
from pySim.ts_31_102 import EF_SUCI_Calc_Info
66

7+
# we don't really want to thest TS 102 221, but the underlying DataObject codebase
8+
from pySim.ts_102_221 import AM_DO_EF, AM_DO_DF, SC_DO
9+
10+
class DoTestCase(unittest.TestCase):
11+
12+
def testSeqOfChoices(self):
13+
"""A sequence of two choices with each a variety of DO/TLVs"""
14+
arr_seq = utils.DataObjectSequence('arr', sequence=[AM_DO_EF, SC_DO])
15+
# input data
16+
dec_in = [{'access_mode': ['update_erase', 'read_search_compare']}, {'control_reference_template':'PIN1'}]
17+
# encode it once
18+
encoded = arr_seq.encode(dec_in)
19+
# decode again
20+
re_decoded = arr_seq.decode(encoded)
21+
self.assertEqual(dec_in, re_decoded[0])
22+
723
class DecTestCase(unittest.TestCase):
824
# TS33.501 Annex C.4 test keys
925
hnet_pubkey_profile_b = "0272DA71976234CE833A6907425867B82E074D44EF907DFB4B3E21C1C2256EBCD1" # ID 27 in test file

0 commit comments

Comments
 (0)