aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorHarald Welte <laforge@osmocom.org>2023-12-27 21:44:56 +0100
committerHarald Welte <laforge@osmocom.org>2023-12-29 18:51:21 +0100
commitf6fceb8684c6aaccdf2d0661663866a52bb0cc66 (patch)
tree17dd2a8a35081ef71d914dd91b1576a5c5fb15d2 /tests
parent842fbdb15d50588bc427e9ed75ad720e578efb38 (diff)
Implement convoluted encoding of UCS-2 as per TS 102 221 Annex A
TS 102 221 Annex A defines three variants of encoding UCS-2 characters into byte streams in files on UICC cards: One rather simplistic one, and two variants for optimizing memory utilization on the card. Let's impelement a construct "Ucs2Adapter" class for this. Change-Id: Ic8bc8f71079faec1bf0e538dc0dfa21403869c6d
Diffstat (limited to 'tests')
-rw-r--r--tests/test_construct.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/test_construct.py b/tests/test_construct.py
index f1bee5a..11822a8 100644
--- a/tests/test_construct.py
+++ b/tests/test_construct.py
@@ -33,5 +33,49 @@ class TestUtils(unittest.TestCase):
self.assertEqual(filter_dict(inp), out)
+class TestUcs2Adapter(unittest.TestCase):
+ # the three examples from TS 102 221 Annex A
+ EXAMPLE1 = b'\x80\x00\x30\x00\x31\x00\x32\x00\x33'
+ EXAMPLE2 = b'\x81\x05\x13\x53\x95\xa6\xa6\xff\xff'
+ EXAMPLE3 = b'\x82\x05\x05\x30\x2d\x82\xd3\x2d\x31'
+ ad = Ucs2Adapter(GreedyBytes)
+
+ def test_example1_decode(self):
+ dec = self.ad._decode(self.EXAMPLE1, None, None)
+ self.assertEqual(dec, "0123")
+
+ def test_example2_decode(self):
+ dec = self.ad._decode(self.EXAMPLE2, None, None)
+ self.assertEqual(dec, "S\u0995\u09a6\u09a6\u09ff")
+
+ def test_example3_decode(self):
+ dec = self.ad._decode(self.EXAMPLE3, None, None)
+ self.assertEqual(dec, "-\u0532\u0583-1")
+
+ testdata = [
+ # variant 2 with only GSM alphabet characters
+ ( "mahlzeit", '8108006d61686c7a656974' ),
+ # variant 2 with mixed GSM alphabet + UCS2
+ ( "mahlzeit\u099523", '810b136d61686c7a656974953233' ),
+ # variant 3 due to codepoint exceeding 8 bit
+ ( "mahl\u8023zeit", '820980236d61686c807a656974' ),
+ # variant 1 as there is no common codepoint pointer / prefix
+ ( "\u3000\u2000\u1000", '80300020001000' ),
+ ]
+
+ def test_data_decode(self):
+ for string, encoded_hex in self.testdata:
+ encoded = h2b(encoded_hex)
+ dec = self.ad._decode(encoded, None, None)
+ self.assertEqual(dec, string)
+
+ def test_data_encode(self):
+ for string, encoded_hex in self.testdata:
+ encoded = h2b(encoded_hex)
+ re_enc = self.ad._encode(string, None, None)
+ self.assertEqual(encoded, re_enc)
+
+
+
if __name__ == "__main__":
unittest.main()