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