tests: replace pycodestyle with black
[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
8 # TODO
9 # - test parsing of options, typedefs, enums, defines
10 # - test JSON, C output
11
12
13 class TestVersion(unittest.TestCase):
14     @classmethod
15     def setUpClass(cls):
16         cls.parser = VPPAPI()
17
18     def test_version(self):
19         version_string = 'option version = "1.0.0";'
20         r = self.parser.parse_string(version_string)
21         self.assertTrue(isinstance(r[0], Option))
22
23
24 class TestUnion(unittest.TestCase):
25     @classmethod
26     def setUpClass(cls):
27         cls.parser = VPPAPI()
28
29     def test_union(self):
30         test_string = """
31         union foo_union {
32         u32 a;
33         u8 b;
34         };
35         """
36         r = self.parser.parse_string(test_string)
37         self.assertTrue(isinstance(r[0], Union))
38
39     def test_union_vla(self):
40         test_string = """
41         union foo_union_vla {
42         u32 a;
43         u8 b[a];
44         };
45         autoreply define foo {
46         vl_api_foo_union_vla_t v;
47         };
48         """
49         r = self.parser.parse_string(test_string)
50         self.assertTrue(isinstance(r[0], Union))
51         self.assertTrue(r[0].vla)
52         s = self.parser.process(r)
53
54         test_string2 = """
55         union foo_union_vla2 {
56         u32 a;
57         u8 b[a];
58         u32 c;
59         };
60         autoreply define foo2 {
61         vl_api_foo_union_vla2_t v;
62         };
63         """
64         self.assertRaises(ValueError, self.parser.parse_string, test_string2)
65
66         test_string3 = """
67         union foo_union_vla3 {
68         u32 a;
69         u8 b[a];
70         };
71         autoreply define foo3 {
72         vl_api_foo_union_vla3_t v;
73         u32 x;
74         };
75         """
76         self.assertRaises(ValueError, self.parser.parse_string, test_string3)
77
78
79 class TestTypedef(unittest.TestCase):
80     @classmethod
81     def setUpClass(cls):
82         cls.parser = VPPAPI()
83
84     def test_duplicatetype(self):
85         test_string = """
86         typedef foo1 { u8 dummy; };
87         typedef foo1 { u8 dummy; };
88         """
89         self.assertRaises(KeyError, self.parser.parse_string, test_string)
90
91
92 class TestDefine(unittest.TestCase):
93     @classmethod
94     def setUpClass(cls):
95         cls.parser = VPPAPI()
96
97     def test_unknowntype(self):
98         test_string = "define foo { foobar foo;};"
99         with self.assertRaises(ParseError) as ctx:
100             self.parser.parse_string(test_string)
101         self.assertIn("Undefined type: foobar", str(ctx.exception))
102
103         test_string = "define { u8 foo;};"
104         with self.assertRaises(ParseError) as ctx:
105             self.parser.parse_string(test_string)
106
107     def test_flags(self):
108         test_string = """
109           manual_print dont_trace manual_endian define foo { u8 foo; };
110           define foo_reply {u32 context; i32 retval; };
111         """
112         r = self.parser.parse_string(test_string)
113         self.assertIsNotNone(r)
114         s = self.parser.process(r)
115         self.assertIsNotNone(s)
116         for d in s["Define"]:
117             if d.name == "foo":
118                 self.assertTrue(d.dont_trace)
119                 self.assertTrue(d.manual_endian)
120                 self.assertTrue(d.manual_print)
121                 self.assertFalse(d.autoreply)
122
123         test_string = """
124           nonexisting_flag define foo { u8 foo; };
125         """
126         with self.assertRaises(ParseError):
127             self.parser.parse_string(test_string)
128
129     def test_options(self):
130         test_string = """
131           define foo { option deprecated; u8 foo; };
132           define foo_reply {u32 context; i32 retval; };
133         """
134         r = self.parser.parse_string(test_string)
135         self.assertIsNotNone(r)
136         s = self.parser.process(r)
137         self.assertIsNotNone(s)
138
139
140 class TestService(unittest.TestCase):
141     @classmethod
142     def setUpClass(cls):
143         cls.parser = VPPAPI()
144
145     def test_service(self):
146         test_string = """
147          autoreply define show_version { u8 foo;};
148          service { rpc show_version returns show_version_reply; };
149         """
150         r = self.parser.parse_string(test_string)
151         s = self.parser.process(r)
152         self.assertEqual(s["Service"][0].caller, "show_version")
153         self.assertEqual(s["Service"][0].reply, "show_version_reply")
154
155
156 def get_crc(apistring, name):
157     vppapigen.global_types = {}
158     parser = vppapigen.VPPAPI()
159     r = parser.parse_string(apistring)
160     s = parser.process(r)
161     foldup_crcs(s["Define"])
162     d = [f for f in s["Define"] if f.name == name]
163     return d[0].crc
164
165
166 class TestCRC(unittest.TestCase):
167     def test_crc(self):
168         test_string = """
169          typedef list { u8 foo; };
170          autoreply define foo { u8 foo; vl_api_list_t l;};
171         """
172         crc = get_crc(test_string, "foo")
173
174         # modify underlying type
175         test_string = """
176          typedef list { u8 foo2; };
177          autoreply define foo { u8 foo;  vl_api_list_t l;};
178         """
179         crc2 = get_crc(test_string, "foo")
180         self.assertNotEqual(crc, crc2)
181
182         # two user-defined types
183         test_string = """
184          typedef address { u8 foo2; };
185          typedef list { u8 foo2; vl_api_address_t add; };
186          autoreply define foo { u8 foo;  vl_api_list_t l;};
187         """
188         crc3 = get_crc(test_string, "foo")
189
190         test_string = """
191          typedef address { u8 foo3; };
192          typedef list { u8 foo2; vl_api_address_t add; };
193          autoreply define foo { u8 foo;  vl_api_list_t l;};
194         """
195         crc4 = get_crc(test_string, "foo")
196         self.assertNotEqual(crc3, crc4)
197
198         test_string = """
199          typedef address { u8 foo3; };
200          typedef list { u8 foo2; vl_api_address_t add; u8 foo3; };
201          autoreply define foo { u8 foo;  vl_api_list_t l;};
202         """
203         crc5 = get_crc(test_string, "foo")
204         self.assertNotEqual(crc4, crc5)
205
206         test_string = """
207 typedef ip6_address
208 {
209   u8 foo;
210 };
211 typedef srv6_sid_list
212 {
213   u8 num_sids;
214   u32 weight;
215   u32 sl_index;
216   vl_api_ip6_address_t sids[16];
217 };
218 autoreply define sr_policy_add
219 {
220   u32 client_index;
221   u32 context;
222   vl_api_ip6_address_t bsid_addr;
223   u32 weight;
224   bool is_encap;
225   bool is_spray;
226   u32 fib_table;
227   vl_api_srv6_sid_list_t sids;
228 };
229 """
230
231         crc = get_crc(test_string, "sr_policy_add")
232
233         test_string = """
234 typedef ip6_address
235 {
236   u8 foo;
237 };
238 typedef srv6_sid_list
239 {
240   u8 num_sids;
241   u32 weight;
242   vl_api_ip6_address_t sids[16];
243 };
244 autoreply define sr_policy_add
245 {
246   u32 client_index;
247   u32 context;
248   vl_api_ip6_address_t bsid_addr;
249   u32 weight;
250   bool is_encap;
251   bool is_spray;
252   u32 fib_table;
253   vl_api_srv6_sid_list_t sids;
254 };
255 """
256         crc2 = get_crc(test_string, "sr_policy_add")
257
258         self.assertNotEqual(crc, crc2)
259
260
261 class TestEnum(unittest.TestCase):
262     @classmethod
263     def setUpClass(cls):
264         cls.parser = VPPAPI()
265
266     def test_enum_as_enum(self):
267         test_string = """\
268 enum tunnel_mode : u8
269 {
270   /** point-to-point */
271   TUNNEL_API_MODE_P2P = 0,
272   /** multi-point */
273   TUNNEL_API_MODE_MP,
274 };
275 """
276         r = self.parser.parse_string(test_string)
277         self.assertIsNotNone(r)
278         s = self.parser.process(r)
279         for o in s["types"]:
280             if o.type == "Enum":
281                 self.assertEqual(o.name, "tunnel_mode")
282                 break
283         else:
284             self.fail()
285
286     def test_enumflag_as_enum(self):
287         test_string = """\
288 enum virtio_flags {
289         VIRTIO_API_FLAG_GSO = 1, /* enable gso on the interface */
290         VIRTIO_API_FLAG_CSUM_OFFLOAD = 2, /* enable checksum offload without gso on the interface */
291         VIRTIO_API_FLAG_GRO_COALESCE = 4, /* enable packet coalescing on tx side, provided gso enabled */
292         VIRTIO_API_FLAG_PACKED = 8, /* enable packed ring support, provided it is available from backend */
293         VIRTIO_API_FLAG_IN_ORDER = 16, /* enable in order support, provided it is available from backend */
294         VIRTIO_API_FLAG_BUFFERING = 32 [backwards_compatible], /* enable buffering to handle backend jitter/delays */
295 };"""
296         r = self.parser.parse_string(test_string)
297         self.assertIsNotNone(r)
298         s = self.parser.process(r)
299         for o in s["types"]:
300             if o.type == "Enum":
301                 self.assertEqual(o.name, "virtio_flags")
302                 break
303         else:
304             self.fail()
305
306
307 class TestEnumFlag(unittest.TestCase):
308     @classmethod
309     def setUpClass(cls):
310         cls.parser = VPPAPI()
311
312     def test_enum_as_enumflag(self):
313         test_string = """\
314 enumflag tunnel_mode_ef : u8
315 {
316   /** point-to-point */
317   TUNNEL_API_MODE_P2P = 0,
318   /** multi-point */
319   TUNNEL_API_MODE_MP,
320   TUNNEL_API_MODE_FOO,
321   TUNNEL_API_MODE_BAR,
322 };"""
323         with self.assertRaises(TypeError) as ctx:
324             r = self.parser.parse_string(test_string)
325
326         self.assertTrue(
327             str(ctx.exception).startswith("tunnel_mode_ef is not a flag enum.")
328         )
329
330     def test_enumflag_as_enumflag(self):
331         test_string = """\
332 enumflag virtio_flags_ef {
333         VIRTIO_API_FLAG_GSO = 1, /* enable gso on the interface */
334         VIRTIO_API_FLAG_CSUM_OFFLOAD = 2, /* enable checksum offload without gso on the interface */
335         VIRTIO_API_FLAG_GRO_COALESCE = 4, /* enable packet coalescing on tx side, provided gso enabled */
336         VIRTIO_API_FLAG_PACKED = 8, /* enable packed ring support, provided it is available from backend */
337         VIRTIO_API_FLAG_IN_ORDER = 16, /* enable in order support, provided it is available from backend */
338         VIRTIO_API_FLAG_BUFFERING = 32 [backwards_compatible], /* enable buffering to handle backend jitter/delays */
339 };"""
340         r = self.parser.parse_string(test_string)
341         self.assertIsNotNone(r)
342         s = self.parser.process(r)
343         for o in s["types"]:
344             if o.type == "EnumFlag":
345                 self.assertEqual(o.name, "virtio_flags_ef")
346                 break
347         else:
348             self.fail()
349
350
351 if __name__ == "__main__":
352     unittest.main(verbosity=2)