laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/35441?usp=email )
Change subject: data-driven TLV unit data test support
......................................................................
data-driven TLV unit data test support
While we do have the _test_de_encode data driven tests for file
definitions, we don't yet have something similar for derived classes of
BER_TLV_IE. This means that TLVs used outside of the filesystem context
(for example, decoding the SELECT/STATUS response, but also eUICC and
other stuff) do not yet have test coverage.
This commit just adds the related test code, but no test data yet.
Related: OS#6317
Change-Id: Ied85f292bb57fde11dc188be84e3384dc3ff1601
---
A tests/test_tlvs.py
1 file changed, 140 insertions(+), 0 deletions(-)
Approvals:
laforge: Looks good to me, approved
Jenkins Builder: Verified
diff --git a/tests/test_tlvs.py b/tests/test_tlvs.py
new file mode 100755
index 0000000..b0ad48a
--- /dev/null
+++ b/tests/test_tlvs.py
@@ -0,0 +1,122 @@
+#!/usr/bin/env python3
+
+# (C) 2023 by Harald Welte <laforge(a)osmocom.org>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+import unittest
+import logging
+
+from pySim.utils import b2h, h2b, all_subclasses
+from pySim.tlv import *
+
+import pySim.iso7816_4
+import pySim.ts_102_221
+import pySim.ts_102_222
+import pySim.ts_31_102
+import pySim.ts_31_103
+import pySim.ts_51_011
+import pySim.sysmocom_sja2
+import pySim.gsm_r
+import pySim.cdma_ruim
+
+if 'unittest.util' in __import__('sys').modules:
+ # Show full diff in self.assertEqual.
+ __import__('sys').modules['unittest.util']._MAX_LENGTH = 999999999
+
+def get_qualified_name(c):
+ """return the qualified (by module) name of a class."""
+ return "%s.%s" % (c.__module__, c.__name__)
+
+class TLV_IE_Test(unittest.TestCase):
+ maxDiff = None
+
+ @classmethod
+ def get_classes(cls):
+ """get list of TLV_IE sub-classes."""
+ return all_subclasses(TLV_IE)
+
+ @classmethod
+ def setUpClass(cls):
+ """set-up method called once for this class by unittest framework"""
+ cls.classes = cls.get_classes()
+
+ def test_decode_tlv(self):
+ """Test the decoder for a TLV_IE. Requires the given TLV_IE subclass
+ to have a '_test_decode' attribute, containing a list of tuples. Each tuple
+ is a 2-tuple (hexstring, decoded_dict).
+ """
+ for c in self.classes:
+ name = get_qualified_name(c)
+ if hasattr(c, '_test_decode'):
+ for t in c._test_decode:
+ with self.subTest(name, test_decode=t):
+ inst = c()
+ encoded = t[0]
+ decoded = { camel_to_snake(c.__name__): t[1] }
+ context = t[2] if len(t) == 3 else {}
+ logging.debug("Testing decode of %s", name)
+ inst.from_tlv(h2b(encoded), context=context)
+ re_dec = inst.to_dict()
+ self.assertEqual(decoded, re_dec)
+
+ def test_encode_tlv(self):
+ """Test the encoder for a TLV_IE. Requires the given TLV_IE subclass
+ to have a '_test_encode' attribute, containing a list of tuples. Each tuple
+ is a 2-tuple (hexstring, decoded_dict).
+ """
+ for c in self.classes:
+ name = get_qualified_name(c)
+ if hasattr(c, '_test_encode'):
+ for t in c._test_encode:
+ with self.subTest(name, test_encode=t):
+ inst = c()
+ encoded = t[0]
+ decoded = { camel_to_snake(c.__name__): t[1] }
+ context = t[2] if len(t) == 3 else {}
+ logging.debug("Testing encode of %s", name)
+ inst.from_dict(decoded)
+ re_enc = b2h(inst.to_tlv(context))
+ self.assertEqual(encoded.upper(), re_enc.upper())
+
+ def test_de_encode_tlv(self):
+ """Test the decoder and encoder for a TLV_IE. Performs first a decoder
+ test, and then re-encodes the decoded data, comparing the re-encoded data with the
+ initial input data.
+
+ Requires the given TLV_IE subclass to have a '_test_de_encode' attribute,
+ containing a list of tuples. Each tuple is a 2-tuple (hexstring, decoded_dict).
+ """
+ for c in self.classes:
+ name = get_qualified_name(c)
+ if hasattr(c, '_test_de_encode'):
+ for t in c._test_de_encode:
+ with self.subTest(name, test_de_encode=t):
+ inst = c()
+ encoded = t[0]
+ decoded = { camel_to_snake(c.__name__): t[1] }
+ context = t[2] if len(t) == 3 else {}
+ logging.debug("Testing decode of %s", name)
+ inst.from_tlv(h2b(encoded), context=context)
+ re_dec = inst.to_dict()
+ self.assertEqual(decoded, re_dec)
+ logging.debug("Testing re-encode of %s", name)
+ re_enc = b2h(inst.to_tlv(context=context))
+ self.assertEqual(encoded.upper(), re_enc.upper())
+
+
+if __name__ == '__main__':
+ logger = logging.getLogger()
+ logger.setLevel(logging.DEBUG)
+ unittest.main()
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/35441?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ied85f292bb57fde11dc188be84e3384dc3ff1601
Gerrit-Change-Number: 35441
Gerrit-PatchSet: 3
Gerrit-Owner: laforge <laforge(a)osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier(a)sysmocom.de>
Gerrit-Reviewer: laforge <laforge(a)osmocom.org>
Gerrit-MessageType: merged
Attention is currently required from: dexter.
laforge has posted comments on this change. ( https://gerrit.osmocom.org/c/pysim/+/35441?usp=email )
Change subject: data-driven TLV unit data test support
......................................................................
Patch Set 3: Code-Review+2
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/35441?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ied85f292bb57fde11dc188be84e3384dc3ff1601
Gerrit-Change-Number: 35441
Gerrit-PatchSet: 3
Gerrit-Owner: laforge <laforge(a)osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier(a)sysmocom.de>
Gerrit-Reviewer: laforge <laforge(a)osmocom.org>
Gerrit-Attention: dexter <pmaier(a)sysmocom.de>
Gerrit-Comment-Date: Wed, 27 Dec 2023 15:27:10 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment
Attention is currently required from: dexter, fixeria.
laforge has posted comments on this change. ( https://gerrit.osmocom.org/c/pysim/+/35440?usp=email )
Change subject: Fix enumeration of GlobbalPlatformISDR during card_init()
......................................................................
Patch Set 1:
(1 comment)
File pySim/global_platform.py:
https://gerrit.osmocom.org/c/pysim/+/35440/comment/64b31f36_d293ac57
PS1, Line 262: intermediate = False
it's rather ugly that we have to explicitly set this to False in the "leaf" class[es]. Ideally we'd have some kind of attribute that is not inheritable. Something that only exists in CardApplicationSD but not any derived classes.
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/35440?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I7fb1637f8f7a149b536c4d77dac92736c526aa6c
Gerrit-Change-Number: 35440
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge(a)osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier(a)sysmocom.de>
Gerrit-Reviewer: fixeria <vyanitskiy(a)sysmocom.de>
Gerrit-Attention: fixeria <vyanitskiy(a)sysmocom.de>
Gerrit-Attention: dexter <pmaier(a)sysmocom.de>
Gerrit-Comment-Date: Wed, 27 Dec 2023 15:12:14 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment
Attention is currently required from: dexter, fixeria.
laforge has posted comments on this change. ( https://gerrit.osmocom.org/c/pysim/+/35440?usp=email )
Change subject: Fix enumeration of GlobbalPlatformISDR during card_init()
......................................................................
Patch Set 1:
(1 comment)
Patchset:
PS1:
not sure if there's a more elegant way to resolve this other than the manual/explicit 'intermediate' class atrribute
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/35440?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I7fb1637f8f7a149b536c4d77dac92736c526aa6c
Gerrit-Change-Number: 35440
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge(a)osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier(a)sysmocom.de>
Gerrit-Reviewer: fixeria <vyanitskiy(a)sysmocom.de>
Gerrit-Attention: fixeria <vyanitskiy(a)sysmocom.de>
Gerrit-Attention: dexter <pmaier(a)sysmocom.de>
Gerrit-Comment-Date: Wed, 27 Dec 2023 15:09:52 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment
laforge has uploaded this change for review. ( https://gerrit.osmocom.org/c/pysim/+/35447?usp=email )
Change subject: tlv: Fix from_dict() symmetry
......................................................................
tlv: Fix from_dict() symmetry
the to_dict() method generates a {class_name: value} dictionary,
for both the nested and non-nested case. However, before this patch,
the from_dict() method expects a plain list of child IE dicts
in the nested case. This is illogical.
Let's make sure from_dict always expectes a {class_name: value} dict
for both nested and non-nested situations.
Change-Id: I07e4feb3800b420d8be7aae8911f828f1da9dab8
---
M pySim/tlv.py
1 file changed, 21 insertions(+), 4 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/pysim refs/changes/47/35447/1
diff --git a/pySim/tlv.py b/pySim/tlv.py
index 6ea7dd1..4ac5ebf 100644
--- a/pySim/tlv.py
+++ b/pySim/tlv.py
@@ -157,13 +157,13 @@
def from_dict(self, decoded: dict):
"""Set the IE internal decoded representation to data from the argument.
If this is a nested IE, the child IE instance list is re-created."""
+ expected_key_name = camel_to_snake(type(self).__name__)
+ if not expected_key_name in decoded:
+ raise ValueError("Dict %s doesn't contain expected key %s" % (decoded, expected_key_name))
if self.nested_collection:
- self.children = self.nested_collection.from_dict(decoded)
+ self.children = self.nested_collection.from_dict(decoded[expected_key_name])
else:
self.children = []
- expected_key_name = camel_to_snake(type(self).__name__)
- if not expected_key_name in decoded:
- raise ValueError("Dict %s doesn't contain expected key %s" % (decoded, expected_key_name))
self.decoded = decoded[expected_key_name]
def is_constructed(self):
--
To view, visit https://gerrit.osmocom.org/c/pysim/+/35447?usp=email
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings
Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I07e4feb3800b420d8be7aae8911f828f1da9dab8
Gerrit-Change-Number: 35447
Gerrit-PatchSet: 1
Gerrit-Owner: laforge <laforge(a)osmocom.org>
Gerrit-MessageType: newchange