Change in pysim[master]: pySim/utils.py: fix 3-digit MNC encoding in enc_plmn()

laforge gerrit-no-reply at lists.osmocom.org
Fri Mar 12 07:26:34 UTC 2021


laforge has submitted this change. ( https://gerrit.osmocom.org/c/pysim/+/23329 )

Change subject: pySim/utils.py: fix 3-digit MNC encoding in enc_plmn()
......................................................................

pySim/utils.py: fix 3-digit MNC encoding in enc_plmn()

The bug that was attempted to be fixed in [1] actually was in the
encoding API - pySim.utils.enc_plmn().  According to 3GPP TS 31.102,
which points to TS 24.008, the three-digit (E)HPLMN shall be encoded
as shown below (ASCII-art interpretation):

    0   1   2   3   4   5   6   7
  +---+---+---+---+---+---+---+---+
  |  MCC Digit 2  |  MCC Digit 1  |
  +---+---+---+---+---+---+---+---+
  |  MNC Digit 3  |  MCC Digit 3  |
  +---+---+---+---+---+---+---+---+
  |  MNC Digit 2  |  MNC Digit 1  |
  +---+---+---+---+---+---+---+---+

while pySim.utils.enc_plmn() would produce the following:

    0   1   2   3   4   5   6   7
  +---+---+---+---+---+---+---+---+
  |  MCC Digit 2  |  MCC Digit 1  |
  +---+---+---+---+---+---+---+---+
  |  MNC Digit 1  |  MCC Digit 3  |
  +---+---+---+---+---+---+---+---+
  |  MNC Digit 3  |  MNC Digit 2  |
  +---+---+---+---+---+---+---+---+

Initially the _decoding_ API was correct, but then got changed in
[1] to follow buggy pySim's encoding API.  As a result, a (E)HPLMN
programmed with pySim-prog.py would look correct if verified by
pySim-read.py, but the actual file content would be wrong.

This situation shows that our 'program-read-match' build verification
approach alone is insignificant.  The lack of unit test coverage,
at least for the core parts of the project, makes it possible to have
symmetrical bugs in both encoding and decoding API parts unnoticed.

This problem was found while trying to enable dead unit tests in [3].
Change [1] that introduced a symmetrical bug is reverted in [2].

Change-Id: Ic7612502e1bb0d280133dabbcb5cb146fc6997e5
Related: [1] I799469206f87e930d8888367890babcb8ebe23a9
Related: [2] If6bf5383988ad442e275efc7c5a159327d104879
Related: [3] I4d4facfabc75187acd5238ff4d0f26022bd58f82
---
M pySim/utils.py
M tests/test_utils.py
2 files changed, 12 insertions(+), 2 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved



diff --git a/pySim/utils.py b/pySim/utils.py
index b49a437..d75a000 100644
--- a/pySim/utils.py
+++ b/pySim/utils.py
@@ -109,8 +109,8 @@
 def enc_plmn(mcc, mnc):
 	"""Converts integer MCC/MNC into 3 bytes for EF"""
 	if len(mnc) == 2:
-		mnc = "F%s" % mnc
-	return swap_nibbles("%s%s" % (mcc, mnc))
+		mnc += "F" # pad to 3 digits if needed
+	return (mcc[1] + mcc[0]) + (mnc[2] + mcc[2]) + (mnc[1] + mnc[0])
 
 def dec_spn(ef):
 	byte1 = int(ef[0:2])
diff --git a/tests/test_utils.py b/tests/test_utils.py
index 44fe631..0df205a 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -29,6 +29,16 @@
 	def testDecMNCfromPLMN_unused(self):
 		self.assertEqual(utils.dec_mnc_from_plmn("00f0ff"), 4095)
 
+	def test_enc_plmn(self):
+		with self.subTest("2-digit MCC"):
+			self.assertEqual(utils.enc_plmn("001", "01F"), "00F110")
+			self.assertEqual(utils.enc_plmn("001", "01"), "00F110")
+			self.assertEqual(utils.enc_plmn("295", "10"), "92F501")
+
+		with self.subTest("3-digit MCC"):
+			self.assertEqual(utils.enc_plmn("001", "001"), "001100")
+			self.assertEqual(utils.enc_plmn("302", "361"), "031263")
+
 	def testDecAct_noneSet(self):
 		self.assertEqual(utils.dec_act("0000"), [])
 

-- 
To view, visit https://gerrit.osmocom.org/c/pysim/+/23329
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: Ic7612502e1bb0d280133dabbcb5cb146fc6997e5
Gerrit-Change-Number: 23329
Gerrit-PatchSet: 1
Gerrit-Owner: fixeria <vyanitskiy at sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: herlesupreeth <herlesupreeth at gmail.com>
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210312/20786c31/attachment.htm>


More information about the gerrit-log mailing list