vppapigen: missing crcs in user-defined types
[vpp.git] / src / tools / vppapigen / test_vppapigen.py
1 #!/usr/bin/env python3
2
3 import unittest
4 from vppapigen import VPPAPI, Option, ParseError, Union, foldup_crcs, global_types
5 import vppapigen
6
7 # TODO
8 # - test parsing of options, typedefs, enums, defines
9 # - test JSON, C output
10
11
12 class TestVersion(unittest.TestCase):
13     @classmethod
14     def setUpClass(cls):
15         cls.parser = VPPAPI()
16
17     def test_version(self):
18         version_string = 'option version = "1.0.0";'
19         r = self.parser.parse_string(version_string)
20         self.assertTrue(isinstance(r[0], Option))
21
22 class TestUnion(unittest.TestCase):
23     @classmethod
24     def setUpClass(cls):
25         cls.parser = VPPAPI()
26
27     def test_union(self):
28         test_string = '''
29         union foo_union {
30         u32 a;
31         u8 b;
32         };
33         '''
34         r = self.parser.parse_string(test_string)
35         self.assertTrue(isinstance(r[0], Union))
36
37     def test_union_vla(self):
38         test_string = '''
39         union foo_union_vla {
40         u32 a;
41         u8 b[a];
42         };
43         autoreply define foo {
44         vl_api_foo_union_vla_t v;
45         };
46         '''
47         r = self.parser.parse_string(test_string)
48         self.assertTrue(isinstance(r[0], Union))
49         self.assertTrue(r[0].vla)
50         s = self.parser.process(r)
51
52
53         test_string2 = '''
54         union foo_union_vla2 {
55         u32 a;
56         u8 b[a];
57         u32 c;
58         };
59         autoreply define foo2 {
60         vl_api_foo_union_vla2_t v;
61         };
62         '''
63         self.assertRaises(ValueError, self.parser.parse_string, test_string2)
64
65         test_string3 = '''
66         union foo_union_vla3 {
67         u32 a;
68         u8 b[a];
69         };
70         autoreply define foo3 {
71         vl_api_foo_union_vla3_t v;
72         u32 x;
73         };
74         '''
75         self.assertRaises(ValueError, self.parser.parse_string, test_string3)
76
77 class TestTypedef(unittest.TestCase):
78     @classmethod
79     def setUpClass(cls):
80         cls.parser = VPPAPI()
81
82     def test_duplicatetype(self):
83         test_string = '''
84         typedef foo1 { u8 dummy; };
85         typedef foo1 { u8 dummy; };
86         '''
87         self.assertRaises(KeyError, self.parser.parse_string, test_string)
88
89
90 class TestDefine(unittest.TestCase):
91     @classmethod
92     def setUpClass(cls):
93         cls.parser = VPPAPI()
94
95     def test_unknowntype(self):
96         test_string = 'define foo { foobar foo;};'
97         with self.assertRaises(ParseError) as ctx:
98             self.parser.parse_string(test_string)
99         self.assertIn('Undefined type: foobar', str(ctx.exception))
100
101         test_string = 'define { u8 foo;};'
102         with self.assertRaises(ParseError) as ctx:
103             self.parser.parse_string(test_string)
104
105     def test_flags(self):
106         test_string = '''
107           manual_print dont_trace manual_endian define foo { u8 foo; };
108           define foo_reply {u32 context; i32 retval; };
109         '''
110         r = self.parser.parse_string(test_string)
111         self.assertIsNotNone(r)
112         s = self.parser.process(r)
113         self.assertIsNotNone(s)
114         for d in s['Define']:
115             if d.name == 'foo':
116                 self.assertTrue(d.dont_trace)
117                 self.assertTrue(d.manual_endian)
118                 self.assertTrue(d.manual_print)
119                 self.assertFalse(d.autoreply)
120
121         test_string = '''
122           nonexisting_flag define foo { u8 foo; };
123         '''
124         with self.assertRaises(ParseError):
125             self.parser.parse_string(test_string)
126
127
128 class TestService(unittest.TestCase):
129     @classmethod
130     def setUpClass(cls):
131         cls.parser = VPPAPI()
132
133     def test_service(self):
134         test_string = '''
135          autoreply define show_version { u8 foo;};
136          service { rpc show_version returns show_version_reply; };
137         '''
138         r = self.parser.parse_string(test_string)
139         s = self.parser.process(r)
140         self.assertEqual(s['Service'][0].caller, 'show_version')
141         self.assertEqual(s['Service'][0].reply, 'show_version_reply')
142
143
144 def get_crc(apistring, name):
145     vppapigen.global_types = {}
146     parser = vppapigen.VPPAPI()
147     r = parser.parse_string(apistring)
148     s = parser.process(r)
149     foldup_crcs(s['Define'])
150     d = [f for f in s['Define'] if f.name == name]
151     return d[0].crc
152
153
154 class TestCRC(unittest.TestCase):
155     def test_crc(self):
156         test_string = '''
157          typedef list { u8 foo; };
158          autoreply define foo { u8 foo; vl_api_list_t l;};
159         '''
160         crc = get_crc(test_string, 'foo')
161
162         # modify underlaying type
163         test_string = '''
164          typedef list { u8 foo2; };
165          autoreply define foo { u8 foo;  vl_api_list_t l;};
166         '''
167         crc2 = get_crc(test_string, 'foo')
168         self.assertNotEqual(crc, crc2)
169
170         # two user-defined types
171         test_string = '''
172          typedef address { u8 foo2; };
173          typedef list { u8 foo2; vl_api_address_t add; };
174          autoreply define foo { u8 foo;  vl_api_list_t l;};
175         '''
176         crc3 = get_crc(test_string, 'foo')
177
178         test_string = '''
179          typedef address { u8 foo3; };
180          typedef list { u8 foo2; vl_api_address_t add; };
181          autoreply define foo { u8 foo;  vl_api_list_t l;};
182         '''
183         crc4 = get_crc(test_string, 'foo')
184         self.assertNotEqual(crc3, crc4)
185
186         test_string = '''
187          typedef address { u8 foo3; };
188          typedef list { u8 foo2; vl_api_address_t add; u8 foo3; };
189          autoreply define foo { u8 foo;  vl_api_list_t l;};
190         '''
191         crc5 = get_crc(test_string, 'foo')
192         self.assertNotEqual(crc4, crc5)
193
194         test_string = '''
195 typedef ip6_address
196 {
197   u8 foo;
198 };
199 typedef srv6_sid_list
200 {
201   u8 num_sids;
202   u32 weight;
203   u32 sl_index;
204   vl_api_ip6_address_t sids[16];
205 };
206 autoreply define sr_policy_add
207 {
208   u32 client_index;
209   u32 context;
210   vl_api_ip6_address_t bsid_addr;
211   u32 weight;
212   bool is_encap;
213   bool is_spray;
214   u32 fib_table;
215   vl_api_srv6_sid_list_t sids;
216 };
217 '''
218
219         crc = get_crc(test_string, 'sr_policy_add')
220
221         test_string = '''
222 typedef ip6_address
223 {
224   u8 foo;
225 };
226 typedef srv6_sid_list
227 {
228   u8 num_sids;
229   u32 weight;
230   vl_api_ip6_address_t sids[16];
231 };
232 autoreply define sr_policy_add
233 {
234   u32 client_index;
235   u32 context;
236   vl_api_ip6_address_t bsid_addr;
237   u32 weight;
238   bool is_encap;
239   bool is_spray;
240   u32 fib_table;
241   vl_api_srv6_sid_list_t sids;
242 };
243 '''
244         crc2 = get_crc(test_string, 'sr_policy_add')
245
246         self.assertNotEqual(crc, crc2)
247
248 if __name__ == '__main__':
249     unittest.main(verbosity=2)