dexter has submitted this change. (
https://gerrit.osmocom.org/c/python/pyosmocom/+/38687?usp=email )
Change subject: Fixing 3-digit MNC PlmnAdapter encoding & decoding, and related
tests.
......................................................................
Fixing 3-digit MNC PlmnAdapter encoding & decoding, and related tests.
Related: pysim.git Ib2b586cb570dbe74a617c45c0fca276b08bb075e
Change-Id: I3811b227d629bd4e051a480c9622967e31f8a376
Depends: pysim.git: I3811b227d629bd4e051a480c9622967e31f8a376
Related: OS#6598
---
M src/osmocom/construct.py
M tests/test_construct.py
2 files changed, 19 insertions(+), 3 deletions(-)
Approvals:
dexter: Looks good to me, approved; Verified
diff --git a/src/osmocom/construct.py b/src/osmocom/construct.py
index 5a78246..c69b44f 100644
--- a/src/osmocom/construct.py
+++ b/src/osmocom/construct.py
@@ -262,20 +262,20 @@
return super()._encode(obj, context, path)
class PlmnAdapter(BcdAdapter):
- """convert a bytes(3) type to BCD string like 262-02 or
262-002."""
+ """convert a bytes(3) type to BCD string like 262-02 or 262-002 as
specified in 3GPP TS 24.008 § 10.5.1.3."""
def _decode(self, obj, context, path):
bcd = super()._decode(obj, context, path)
if bcd[3] == 'f':
return '-'.join([bcd[:3], bcd[4:]])
else:
- return '-'.join([bcd[:3], bcd[3:]])
+ return '-'.join([bcd[:3], bcd[4:6]+bcd[3]])
def _encode(self, obj, context, path):
l = obj.split('-')
if len(l[1]) == 2:
bcd = l[0] + 'f' + l[1]
else:
- bcd = l[0] + l[1]
+ bcd = l[0] + l[1][2] + l[1][0:2]
return super()._encode(bcd, context, path)
class InvertAdapter(Adapter):
diff --git a/tests/test_construct.py b/tests/test_construct.py
index ba4f8f4..bcb2cf8 100755
--- a/tests/test_construct.py
+++ b/tests/test_construct.py
@@ -113,6 +113,22 @@
re_enc = ad._encode(exp_dec, None, None)
self.assertEqual(re_enc, enc)
+ def test_plmn_adapter(self):
+ ad = PlmnAdapter(Bytes(3))
+ td = [
+ (bytes.fromhex('62f210'),'262-01'),
+ (bytes.fromhex('21f354'),"123-45"),
+ (bytes.fromhex('216354'),"123-456"),
+ (bytes.fromhex('030251'),"302-150")
+ ]
+ for enc, exp_dec in td:
+ with self.subTest(type='decode', name=exp_dec):
+ dec = ad._decode(enc, None, None)
+ self.assertEqual(dec, exp_dec)
+ with self.subTest(type='encode', name=exp_dec):
+ re_enc = ad._encode(exp_dec, None, None)
+ self.assertEqual(re_enc, enc)
+
class TestAsn1DerInteger(unittest.TestCase):
--
To view, visit
https://gerrit.osmocom.org/c/python/pyosmocom/+/38687?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.osmocom.org/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: python/pyosmocom
Gerrit-Branch: master
Gerrit-Change-Id: I3811b227d629bd4e051a480c9622967e31f8a376
Gerrit-Change-Number: 38687
Gerrit-PatchSet: 8
Gerrit-Owner: JPM <jean-pierre.marcotte.1(a)ens.etsmtl.ca>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: dexter <pmaier(a)sysmocom.de>
Gerrit-Reviewer: fixeria <vyanitskiy(a)sysmocom.de>
Gerrit-Reviewer: laforge <laforge(a)osmocom.org>