Harald Welte | 81f4b40 | 2022-02-12 10:31:27 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # (C) 2022 by Harald Welte <laforge@osmocom.org> |
| 4 | # All Rights Reserved |
| 5 | # |
| 6 | # This program is free software: you can redistribute it and/or modify |
| 7 | # it under the terms of the GNU General Public License as published by |
| 8 | # the Free Software Foundation, either version 2 of the License, or |
| 9 | # (at your option) any later version. |
| 10 | # |
| 11 | # This program is distributed in the hope that it will be useful, |
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | # GNU General Public License for more details. |
| 15 | # |
| 16 | # You should have received a copy of the GNU General Public License |
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | |
| 19 | import unittest |
| 20 | from pySim.tlv import * |
| 21 | |
| 22 | class TestUtils(unittest.TestCase): |
| 23 | def test_camel_to_snake(self): |
| 24 | cases = [ |
| 25 | ('CamelCase', 'camel_case'), |
| 26 | ('CamelCaseUPPER', 'camel_case_upper'), |
| 27 | ('Camel_CASE_underSCORE', 'camel_case_under_score'), |
| 28 | ] |
| 29 | for c in cases: |
| 30 | self.assertEqual(camel_to_snake(c[0]), c[1]) |
| 31 | |
| 32 | def test_flatten_dict_lists(self): |
| 33 | inp = [ |
| 34 | { 'first': 1 }, |
| 35 | { 'second': 2 }, |
| 36 | { 'third': 3 }, |
| 37 | ] |
| 38 | out = { 'first': 1, 'second':2, 'third': 3} |
| 39 | self.assertEqual(flatten_dict_lists(inp), out) |
| 40 | |
| 41 | def test_flatten_dict_lists_nodict(self): |
| 42 | inp = [ |
| 43 | { 'first': 1 }, |
| 44 | { 'second': 2 }, |
| 45 | { 'third': 3 }, |
| 46 | 4, |
| 47 | ] |
| 48 | self.assertEqual(flatten_dict_lists(inp), inp) |
| 49 | |
| 50 | def test_flatten_dict_lists_nested(self): |
| 51 | inp = {'top': [ |
| 52 | { 'first': 1 }, |
| 53 | { 'second': 2 }, |
| 54 | { 'third': 3 }, |
| 55 | ] } |
| 56 | out = {'top': { 'first': 1, 'second':2, 'third': 3 } } |
| 57 | self.assertEqual(flatten_dict_lists(inp), out) |
| 58 | |
| 59 | class TestTranscodable(unittest.TestCase): |
| 60 | class XC_constr_class(Transcodable): |
| 61 | _construct = Int8ub |
| 62 | def __init__(self): |
| 63 | super().__init__(); |
| 64 | |
| 65 | def test_XC_constr_class(self): |
| 66 | """Transcodable derived class with _construct class variable""" |
| 67 | xc = TestTranscodable.XC_constr_class() |
| 68 | self.assertEqual(xc.from_bytes(b'\x23'), 35) |
| 69 | self.assertEqual(xc.to_bytes(), b'\x23') |
| 70 | |
| 71 | class XC_constr_instance(Transcodable): |
| 72 | def __init__(self): |
| 73 | super().__init__(); |
| 74 | self._construct = Int8ub |
| 75 | |
| 76 | def test_XC_constr_instance(self): |
| 77 | """Transcodable derived class with _construct instance variable""" |
| 78 | xc = TestTranscodable.XC_constr_instance() |
| 79 | self.assertEqual(xc.from_bytes(b'\x23'), 35) |
| 80 | self.assertEqual(xc.to_bytes(), b'\x23') |
| 81 | |
| 82 | class XC_method_instance(Transcodable): |
| 83 | def __init__(self): |
| 84 | super().__init__(); |
| 85 | def _from_bytes(self, do): |
| 86 | return ('decoded', do) |
| 87 | def _to_bytes(self): |
| 88 | return self.decoded[1] |
| 89 | |
| 90 | def test_XC_method_instance(self): |
| 91 | """Transcodable derived class with _{from,to}_bytes() methods""" |
| 92 | xc = TestTranscodable.XC_method_instance() |
| 93 | self.assertEqual(xc.to_bytes(), b'') |
| 94 | self.assertEqual(xc.from_bytes(b''), None) |
| 95 | self.assertEqual(xc.from_bytes(b'\x23'), ('decoded', b'\x23')) |
| 96 | self.assertEqual(xc.to_bytes(), b'\x23') |
| 97 | |
| 98 | class TestIE(unittest.TestCase): |
| 99 | class MyIE(IE, tag=0x23, desc='My IE description'): |
| 100 | _construct = Int8ub |
| 101 | def to_ie(self): |
| 102 | return self.to_bytes() |
| 103 | |
| 104 | def test_IE_empty(self): |
| 105 | ie = TestIE.MyIE() |
| 106 | self.assertEqual(ie.to_dict(), {'my_ie': None}) |
| 107 | self.assertEqual(repr(ie), 'MyIE(None)') |
| 108 | self.assertEqual(ie.is_constructed(), False) |
| 109 | |
| 110 | def test_IE_from_bytes(self): |
| 111 | ie = TestIE.MyIE() |
| 112 | ie.from_bytes(b'\x42') |
| 113 | self.assertEqual(ie.to_dict(), {'my_ie': 66}) |
| 114 | self.assertEqual(repr(ie), 'MyIE(66)') |
| 115 | self.assertEqual(ie.is_constructed(), False) |
| 116 | self.assertEqual(ie.to_bytes(), b'\x42') |
| 117 | self.assertEqual(ie.to_ie(), b'\x42') |
| 118 | |
| 119 | if __name__ == "__main__": |
| 120 | unittest.main() |