blob: a0dd1076650177715cf246f3f54bf84f35c91fb3 [file] [log] [blame]
Harald Welte2a36c1b2023-12-23 12:49:09 +01001#!/usr/bin/env python3
2
3# (C) 2023 by Harald Welte <laforge@osmocom.org>
4#
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18import unittest
19import logging
20
21from pySim.utils import b2h, h2b, all_subclasses
22from pySim.tlv import *
23
24import pySim.iso7816_4
25import pySim.ts_102_221
26import pySim.ts_102_222
27import pySim.ts_31_102
28import pySim.ts_31_103
29import pySim.ts_51_011
30import pySim.sysmocom_sja2
31import pySim.gsm_r
32import pySim.cdma_ruim
Harald Welte45626272023-12-23 21:02:44 +010033import pySim.global_platform
Harald Welte2a36c1b2023-12-23 12:49:09 +010034
35if 'unittest.util' in __import__('sys').modules:
36 # Show full diff in self.assertEqual.
37 __import__('sys').modules['unittest.util']._MAX_LENGTH = 999999999
38
39def get_qualified_name(c):
40 """return the qualified (by module) name of a class."""
41 return "%s.%s" % (c.__module__, c.__name__)
42
43class TLV_IE_Test(unittest.TestCase):
44 maxDiff = None
45
46 @classmethod
47 def get_classes(cls):
48 """get list of TLV_IE sub-classes."""
49 return all_subclasses(TLV_IE)
50
51 @classmethod
52 def setUpClass(cls):
53 """set-up method called once for this class by unittest framework"""
54 cls.classes = cls.get_classes()
55
56 def test_decode_tlv(self):
57 """Test the decoder for a TLV_IE. Requires the given TLV_IE subclass
58 to have a '_test_decode' attribute, containing a list of tuples. Each tuple
59 is a 2-tuple (hexstring, decoded_dict).
60 """
61 for c in self.classes:
62 name = get_qualified_name(c)
63 if hasattr(c, '_test_decode'):
64 for t in c._test_decode:
65 with self.subTest(name, test_decode=t):
66 inst = c()
67 encoded = t[0]
68 decoded = { camel_to_snake(c.__name__): t[1] }
69 context = t[2] if len(t) == 3 else {}
70 logging.debug("Testing decode of %s", name)
71 inst.from_tlv(h2b(encoded), context=context)
72 re_dec = inst.to_dict()
73 self.assertEqual(decoded, re_dec)
74
75 def test_encode_tlv(self):
76 """Test the encoder for a TLV_IE. Requires the given TLV_IE subclass
77 to have a '_test_encode' attribute, containing a list of tuples. Each tuple
78 is a 2-tuple (hexstring, decoded_dict).
79 """
80 for c in self.classes:
81 name = get_qualified_name(c)
82 if hasattr(c, '_test_encode'):
83 for t in c._test_encode:
84 with self.subTest(name, test_encode=t):
85 inst = c()
86 encoded = t[0]
87 decoded = { camel_to_snake(c.__name__): t[1] }
88 context = t[2] if len(t) == 3 else {}
89 logging.debug("Testing encode of %s", name)
90 inst.from_dict(decoded)
91 re_enc = b2h(inst.to_tlv(context))
92 self.assertEqual(encoded.upper(), re_enc.upper())
93
94 def test_de_encode_tlv(self):
95 """Test the decoder and encoder for a TLV_IE. Performs first a decoder
96 test, and then re-encodes the decoded data, comparing the re-encoded data with the
97 initial input data.
98
99 Requires the given TLV_IE subclass to have a '_test_de_encode' attribute,
100 containing a list of tuples. Each tuple is a 2-tuple (hexstring, decoded_dict).
101 """
102 for c in self.classes:
103 name = get_qualified_name(c)
104 if hasattr(c, '_test_de_encode'):
105 for t in c._test_de_encode:
106 with self.subTest(name, test_de_encode=t):
107 inst = c()
108 encoded = t[0]
109 decoded = { camel_to_snake(c.__name__): t[1] }
110 context = t[2] if len(t) == 3 else {}
111 logging.debug("Testing decode of %s", name)
112 inst.from_tlv(h2b(encoded), context=context)
113 re_dec = inst.to_dict()
114 self.assertEqual(decoded, re_dec)
115 logging.debug("Testing re-encode of %s", name)
116 re_enc = b2h(inst.to_tlv(context=context))
117 self.assertEqual(encoded.upper(), re_enc.upper())
118
119
120if __name__ == '__main__':
121 logger = logging.getLogger()
122 logger.setLevel(logging.DEBUG)
123 unittest.main()