blob: fe520fb89789c6848a734896b808e1d6ca44cc9a [file] [log] [blame]
Harald Welte865eea62023-01-27 19:26:12 +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 *
22from pySim.filesystem 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
Harald Weltef9e2df12023-07-11 21:03:54 +020032import pySim.cdma_ruim
Harald Welte865eea62023-01-27 19:26:12 +010033
34def get_qualified_name(c):
35 """return the qualified (by module) name of a class."""
36 return "%s.%s" % (c.__module__, c.__name__)
37
38class LinFixed_Test(unittest.TestCase):
39 classes = all_subclasses(LinFixedEF)
40
41 def test_decode_record(self):
42 """Test the decoder for a linear-fixed EF. Requires the given LinFixedEF subclass
43 to have an '_test_decode' attribute, containing a list of tuples. Each tuple can
44 either be a
45 * 2-tuple (hexstring, decoded_dict) or a
46 * 3-tuple (hexstring, record_nr, decoded_dict)
47 """
48 for c in self.classes:
49 name = get_qualified_name(c)
50 if hasattr(c, '_test_decode'):
51 for t in c._test_decode:
52 with self.subTest(name, test_decode=t):
53 inst = c()
54 if len(t) == 2:
55 encoded = t[0]
56 rec_num = 1
57 decoded = t[1]
58 else:
59 encoded = t[0]
60 rec_num = t[1]
61 decoded = t[2]
62 logging.debug("Testing decode of %s", name)
63 re_dec = inst.decode_record_hex(encoded, rec_num)
64 self.assertEqual(decoded, re_dec)
65
66 def test_encode_record(self):
67 """Test the encoder for a linear-fixed EF. Requires the given LinFixedEF subclass
68 to have an '_test_encode' attribute, containing a list of tuples. Each tuple can
69 either be a
70 * 2-tuple (hexstring, decoded_dict) or a
71 * 3-tuple (hexstring, record_nr, decoded_dict)
72 """
73 for c in self.classes:
74 name = get_qualified_name(c)
75 if hasattr(c, '_test_encode'):
76 for t in c._test_encode:
77 with self.subTest(name, test_encode=t):
78 inst = c()
79 if len(t) == 2:
80 encoded = t[0]
81 rec_num = 1
82 decoded = t[1]
83 else:
84 encoded = t[0]
85 rec_num = t[1]
86 decoded = t[2]
87 logging.debug("Testing encode of %s", name)
88 re_enc = inst.encode_record_hex(decoded, rec_num)
89 self.assertEqual(encoded, re_enc)
90
91 def test_de_encode_record(self):
92 """Test the decoder and encoder for a linear-fixed EF. Performs first a decoder
93 test, and then re-encodes the decoded data, comparing the re-encoded data with the
94 initial input data.
95
96 Requires the given LinFixedEF subclass to have a '_test_de_encode' attribute,
97 containing a list of tuples. Each tuple can
98 either be a
99 * 2-tuple (hexstring, decoded_dict) or a
100 * 3-tuple (hexstring, record_nr, 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 if len(t) == 2:
109 encoded = t[0]
110 rec_num = 1
111 decoded = t[1]
112 else:
113 encoded = t[0]
114 rec_num = t[1]
115 decoded = t[2]
116 logging.debug("Testing decode of %s", name)
117 re_dec = inst.decode_record_hex(encoded, rec_num)
118 self.assertEqual(decoded, re_dec)
119 # re-encode the decoded data
120 logging.debug("Testing re-encode of %s", name)
121 re_enc = inst.encode_record_hex(re_dec, rec_num)
122 self.assertEqual(encoded, re_enc)
123
124
125class TransRecEF_Test(unittest.TestCase):
126 classes = all_subclasses(TransRecEF)
127
128 def test_decode_record(self):
129 """Test the decoder for a transparent record-oriented EF. Requires the given TransRecEF subclass
130 to have an '_test_decode' attribute, containing a list of tuples. Each tuple has to be a
131 2-tuple (hexstring, decoded_dict).
132 """
133 for c in self.classes:
134 name = get_qualified_name(c)
135 if hasattr(c, '_test_decode'):
136 for t in c._test_decode:
137 with self.subTest(name, test_decode=t):
138 inst = c()
139 encoded = t[0]
140 decoded = t[1]
141 logging.debug("Testing decode of %s", name)
142 re_dec = inst.decode_record_hex(encoded)
143 self.assertEqual(decoded, re_dec)
144
145 def test_encode_record(self):
146 """Test the encoder for a transparent record-oriented EF. Requires the given TransRecEF subclass
147 to have an '_test_encode' attribute, containing a list of tuples. Each tuple has to be a
148 2-tuple (hexstring, decoded_dict).
149 """
150 for c in self.classes:
151 name = get_qualified_name(c)
152 if hasattr(c, '_test_decode'):
153 for t in c._test_decode:
154 with self.subTest(name, test_decode=t):
155 inst = c()
156 encoded = t[0]
157 decoded = t[1]
158 logging.debug("Testing decode of %s", name)
159 re_dec = inst.decode_record_hex(encoded)
160 self.assertEqual(decoded, re_dec)
161
162
163 def test_de_encode_record(self):
164 """Test the decoder and encoder for a transparent record-oriented EF. Performs first a decoder
165 test, and then re-encodes the decoded data, comparing the re-encoded data with the
166 initial input data.
167
168 Requires the given TransRecEF subclass to have a '_test_de_encode' attribute,
169 containing a list of tuples. Each tuple has to be a 2-tuple (hexstring, decoded_dict).
170 """
171 for c in self.classes:
172 name = get_qualified_name(c)
173 if hasattr(c, '_test_de_encode'):
174 for t in c._test_de_encode:
175 with self.subTest(name, test_de_encode=t):
176 inst = c()
177 encoded = t[0]
178 decoded = t[1]
179 logging.debug("Testing decode of %s", name)
180 re_dec = inst.decode_record_hex(encoded)
181 self.assertEqual(decoded, re_dec)
182 # re-encode the decoded data
183 logging.debug("Testing re-encode of %s", name)
184 re_enc = inst.encode_record_hex(re_dec)
185 self.assertEqual(encoded, re_enc)
186
187
188class TransparentEF_Test(unittest.TestCase):
189 @classmethod
190 def get_classes(cls):
191 """get list of TransparentEF sub-classes which are not a TransRecEF subclass."""
192 classes = all_subclasses(TransparentEF)
193 trans_rec_classes = all_subclasses(TransRecEF)
194 return filter(lambda c: c not in trans_rec_classes, classes)
195
196 @classmethod
197 def setUpClass(cls):
198 """set-up method called once for this class by unittest framework"""
199 cls.classes = cls.get_classes()
200
201 def test_decode_file(self):
202 """Test the decoder for a transparent EF. Requires the given TransparentEF subclass
203 to have a '_test_decode' attribute, containing a list of tuples. Each tuple
204 is a 2-tuple (hexstring, decoded_dict).
205 """
206 for c in self.classes:
207 name = get_qualified_name(c)
208 if hasattr(c, '_test_decode'):
209 for t in c._test_decode:
210 with self.subTest(name, test_decode=t):
211 inst = c()
212 encoded = t[0]
213 decoded = t[1]
214 logging.debug("Testing decode of %s", name)
215 re_dec = inst.decode_hex(encoded)
216 self.assertEqual(decoded, re_dec)
217
218 def test_encode_file(self):
219 """Test the encoder for a transparent EF. Requires the given TransparentEF subclass
220 to have a '_test_encode' attribute, containing a list of tuples. Each tuple
221 is a 2-tuple (hexstring, decoded_dict).
222 """
223 for c in self.classes:
224 name = get_qualified_name(c)
225 if hasattr(c, '_test_encode'):
226 for t in c._test_encode:
227 with self.subTest(name, test_encode=t):
228 inst = c()
229 encoded = t[0]
230 decoded = t[1]
231 logging.debug("Testing encode of %s", name)
232 re_dec = inst.decode_hex(encoded)
233 self.assertEqual(decoded, re_dec)
234
235 def test_de_encode_file(self):
236 """Test the decoder and encoder for a transparent EF. Performs first a decoder
237 test, and then re-encodes the decoded data, comparing the re-encoded data with the
238 initial input data.
239
240 Requires the given TransparentEF subclass to have a '_test_de_encode' attribute,
241 containing a list of tuples. Each tuple is a 2-tuple (hexstring, decoded_dict).
242 """
243 for c in self.classes:
244 name = get_qualified_name(c)
245 if hasattr(c, '_test_de_encode'):
246 for t in c._test_de_encode:
247 with self.subTest(name, test_de_encode=t):
248 inst = c()
249 encoded = t[0]
250 decoded = t[1]
251 logging.debug("Testing decode of %s", name)
252 re_dec = inst.decode_hex(encoded)
253 self.assertEqual(decoded, re_dec)
254 logging.debug("Testing re-encode of %s", name)
255 re_dec = inst.decode_hex(encoded)
256 re_enc = inst.encode_hex(re_dec)
257 self.assertEqual(encoded, re_enc)
258
259
260if __name__ == '__main__':
261 logger = logging.getLogger()
262 logger.setLevel(logging.DEBUG)
263 unittest.main()