utils: Add function to verify Luhn check digits and to sanitize ICCIDs

Change-Id: I7812420cf97984dd834fca6a38c5e5ae113243cb
This commit is contained in:
Harald Welte
2024-02-18 11:05:45 +01:00
parent 979c837286
commit efdf423a7f
2 changed files with 47 additions and 1 deletions

View File

@@ -236,5 +236,22 @@ class TestDgiTlv(unittest.TestCase):
self.assertEqual(utils.dgi_parse_len(b'\xfe\x0b'), (254, b'\x0b'))
self.assertEqual(utils.dgi_parse_len(b'\xff\x00\xff\x0b'), (255, b'\x0b'))
class TestLuhn(unittest.TestCase):
def test_verify(self):
utils.verify_luhn('8988211000000530082')
def test_encode(self):
self.assertEqual(utils.calculate_luhn('898821100000053008'), 2)
def test_sanitize_iccid(self):
# 19 digits with correct luhn; we expect no change
self.assertEqual(utils.sanitize_iccid('8988211000000530082'), '8988211000000530082')
# 20 digits with correct luhn; we expect no change
self.assertEqual(utils.sanitize_iccid('89882110000005300811'), '89882110000005300811')
# 19 digits without correct luhn; we expect check digit to be added
self.assertEqual(utils.sanitize_iccid('8988211000000530081'), '89882110000005300811')
# 18 digits; we expect luhn check digit to be added
self.assertEqual(utils.sanitize_iccid('898821100000053008'), '8988211000000530082')
if __name__ == "__main__":
unittest.main()