aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@osmocom.org>2022-04-05 14:24:22 +0200
committerHarald Welte <laforge@osmocom.org>2022-04-05 14:33:00 +0200
commit785d48470930643e2a50157dd5e7fb756eb69106 (patch)
treed5ac4ca09416b244115b40502599f2dfcb65fc50
parentb7f35ac1638a95d2e1e1ed89da83a6930939b9ac (diff)
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
-rw-r--r--pySim/utils.py5
-rwxr-xr-xtests/test_utils.py16
2 files changed, 19 insertions, 2 deletions
diff --git a/pySim/utils.py b/pySim/utils.py
index 555aa82..8a7fcdb 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -1333,7 +1333,7 @@ class DataObject(abc.ABC):
bytes encoded in TLV format.
"""
val = self.to_bytes()
- return bytes(self._compute_tag()) + bytes(len(val)) + val
+ return bertlv_encode_tag(self._compute_tag()) + bertlv_encode_len(len(val)) + val
# 'codec' interface
def decode(self, binary: bytes) -> Tuple[dict, bytes]:
@@ -1481,7 +1481,8 @@ class DataObjectChoice(DataObjectCollection):
# 'codec' interface
def encode(self, decoded) -> bytes:
- obj = self.members_by_name(decoded[0])
+ obj = self.members_by_name[list(decoded)[0]]
+ obj.decoded = list(decoded.values())[0]
return obj.to_tlv()
diff --git a/tests/test_utils.py b/tests/test_utils.py
index ae23461..b7f790d 100755
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -4,6 +4,22 @@ import unittest
from pySim import utils
from pySim.ts_31_102 import EF_SUCI_Calc_Info
+# we don't really want to thest TS 102 221, but the underlying DataObject codebase
+from pySim.ts_102_221 import AM_DO_EF, AM_DO_DF, SC_DO
+
+class DoTestCase(unittest.TestCase):
+
+ def testSeqOfChoices(self):
+ """A sequence of two choices with each a variety of DO/TLVs"""
+ arr_seq = utils.DataObjectSequence('arr', sequence=[AM_DO_EF, SC_DO])
+ # input data
+ dec_in = [{'access_mode': ['update_erase', 'read_search_compare']}, {'control_reference_template':'PIN1'}]
+ # encode it once
+ encoded = arr_seq.encode(dec_in)
+ # decode again
+ re_decoded = arr_seq.decode(encoded)
+ self.assertEqual(dec_in, re_decoded[0])
+
class DecTestCase(unittest.TestCase):
# TS33.501 Annex C.4 test keys
hnet_pubkey_profile_b = "0272DA71976234CE833A6907425867B82E074D44EF907DFB4B3E21C1C2256EBCD1" # ID 27 in test file