blob: 0e73ab1f9bb756bcd10b489d2c20c9b6c1c48560 [file] [log] [blame]
Harald Welte81f4b402022-02-12 10:31:27 +01001#!/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
19import unittest
Harald Welte57f73f82024-02-04 19:33:17 +010020from construct import Int8ub
Harald Welte81f4b402022-02-12 10:31:27 +010021from pySim.tlv import *
22
23class TestUtils(unittest.TestCase):
24 def test_camel_to_snake(self):
25 cases = [
26 ('CamelCase', 'camel_case'),
27 ('CamelCaseUPPER', 'camel_case_upper'),
28 ('Camel_CASE_underSCORE', 'camel_case_under_score'),
29 ]
30 for c in cases:
31 self.assertEqual(camel_to_snake(c[0]), c[1])
32
33 def test_flatten_dict_lists(self):
34 inp = [
35 { 'first': 1 },
36 { 'second': 2 },
37 { 'third': 3 },
38 ]
39 out = { 'first': 1, 'second':2, 'third': 3}
40 self.assertEqual(flatten_dict_lists(inp), out)
41
42 def test_flatten_dict_lists_nodict(self):
43 inp = [
44 { 'first': 1 },
45 { 'second': 2 },
46 { 'third': 3 },
47 4,
48 ]
49 self.assertEqual(flatten_dict_lists(inp), inp)
50
51 def test_flatten_dict_lists_nested(self):
52 inp = {'top': [
53 { 'first': 1 },
54 { 'second': 2 },
55 { 'third': 3 },
56 ] }
57 out = {'top': { 'first': 1, 'second':2, 'third': 3 } }
58 self.assertEqual(flatten_dict_lists(inp), out)
59
60class TestTranscodable(unittest.TestCase):
61 class XC_constr_class(Transcodable):
62 _construct = Int8ub
63 def __init__(self):
64 super().__init__();
65
66 def test_XC_constr_class(self):
67 """Transcodable derived class with _construct class variable"""
68 xc = TestTranscodable.XC_constr_class()
69 self.assertEqual(xc.from_bytes(b'\x23'), 35)
70 self.assertEqual(xc.to_bytes(), b'\x23')
71
72 class XC_constr_instance(Transcodable):
73 def __init__(self):
74 super().__init__();
75 self._construct = Int8ub
76
77 def test_XC_constr_instance(self):
78 """Transcodable derived class with _construct instance variable"""
79 xc = TestTranscodable.XC_constr_instance()
80 self.assertEqual(xc.from_bytes(b'\x23'), 35)
81 self.assertEqual(xc.to_bytes(), b'\x23')
82
83 class XC_method_instance(Transcodable):
84 def __init__(self):
85 super().__init__();
86 def _from_bytes(self, do):
87 return ('decoded', do)
88 def _to_bytes(self):
89 return self.decoded[1]
90
91 def test_XC_method_instance(self):
92 """Transcodable derived class with _{from,to}_bytes() methods"""
93 xc = TestTranscodable.XC_method_instance()
94 self.assertEqual(xc.to_bytes(), b'')
95 self.assertEqual(xc.from_bytes(b''), None)
96 self.assertEqual(xc.from_bytes(b'\x23'), ('decoded', b'\x23'))
97 self.assertEqual(xc.to_bytes(), b'\x23')
98
99class TestIE(unittest.TestCase):
100 class MyIE(IE, tag=0x23, desc='My IE description'):
101 _construct = Int8ub
102 def to_ie(self):
103 return self.to_bytes()
104
105 def test_IE_empty(self):
106 ie = TestIE.MyIE()
107 self.assertEqual(ie.to_dict(), {'my_ie': None})
108 self.assertEqual(repr(ie), 'MyIE(None)')
109 self.assertEqual(ie.is_constructed(), False)
110
111 def test_IE_from_bytes(self):
112 ie = TestIE.MyIE()
113 ie.from_bytes(b'\x42')
114 self.assertEqual(ie.to_dict(), {'my_ie': 66})
115 self.assertEqual(repr(ie), 'MyIE(66)')
116 self.assertEqual(ie.is_constructed(), False)
117 self.assertEqual(ie.to_bytes(), b'\x42')
118 self.assertEqual(ie.to_ie(), b'\x42')
119
120if __name__ == "__main__":
121 unittest.main()