vppapigen: add support for empty options
[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     def test_options(self):
128         test_string = '''
129           define foo { option deprecated; u8 foo; };
130           define foo_reply {u32 context; i32 retval; };
131         '''
132         r = self.parser.parse_string(test_string)
133         self.assertIsNotNone(r)
134         s = self.parser.process(r)
135         self.assertIsNotNone(s)
136
137
138 class TestService(unittest.TestCase):
139     @classmethod
140     def setUpClass(cls):
141         cls.parser = VPPAPI()
142
143     def test_service(self):
144         test_string = '''
145          autoreply define show_version { u8 foo;};
146          service { rpc show_version returns show_version_reply; };
147         '''
148         r = self.parser.parse_string(test_string)
149         s = self.parser.process(r)
150         self.assertEqual(s['Service'][0].caller, 'show_version')
151         self.assertEqual(s['Service'][0].reply, 'show_version_reply')
152
153
154 def get_crc(apistring, name):
155     vppapigen.global_types = {}
156     parser = vppapigen.VPPAPI()
157     r = parser.parse_string(apistring)
158     s = parser.process(r)
159     foldup_crcs(s['Define'])
160     d = [f for f in s['Define'] if f.name == name]
161     return d[0].crc
162
163
164 class TestCRC(unittest.TestCase):
165     def test_crc(self):
166         test_string = '''
167          typedef list { u8 foo; };
168          autoreply define foo { u8 foo; vl_api_list_t l;};
169         '''
170         crc = get_crc(test_string, 'foo')
171
172         # modify underlaying type
173         test_string = '''
174          typedef list { u8 foo2; };
175          autoreply define foo { u8 foo;  vl_api_list_t l;};
176         '''
177         crc2 = get_crc(test_string, 'foo')
178         self.assertNotEqual(crc, crc2)
179
180         # two user-defined types
181         test_string = '''
182          typedef address { u8 foo2; };
183          typedef list { u8 foo2; vl_api_address_t add; };
184          autoreply define foo { u8 foo;  vl_api_list_t l;};
185         '''
186         crc3 = get_crc(test_string, 'foo')
187
188         test_string = '''
189          typedef address { u8 foo3; };
190          typedef list { u8 foo2; vl_api_address_t add; };
191          autoreply define foo { u8 foo;  vl_api_list_t l;};
192         '''
193         crc4 = get_crc(test_string, 'foo')
194         self.assertNotEqual(crc3, crc4)
195
196         test_string = '''
197          typedef address { u8 foo3; };
198          typedef list { u8 foo2; vl_api_address_t add; u8 foo3; };
199          autoreply define foo { u8 foo;  vl_api_list_t l;};
200         '''
201         crc5 = get_crc(test_string, 'foo')
202         self.assertNotEqual(crc4, crc5)
203
204         test_string = '''
205 typedef ip6_address
206 {
207   u8 foo;
208 };
209 typedef srv6_sid_list
210 {
211   u8 num_sids;
212   u32 weight;
213   u32 sl_index;
214   vl_api_ip6_address_t sids[16];
215 };
216 autoreply define sr_policy_add
217 {
218   u32 client_index;
219   u32 context;
220   vl_api_ip6_address_t bsid_addr;
221   u32 weight;
222   bool is_encap;
223   bool is_spray;
224   u32 fib_table;
225   vl_api_srv6_sid_list_t sids;
226 };
227 '''
228
229         crc = get_crc(test_string, 'sr_policy_add')
230
231         test_string = '''
232 typedef ip6_address
233 {
234   u8 foo;
235 };
236 typedef srv6_sid_list
237 {
238   u8 num_sids;
239   u32 weight;
240   vl_api_ip6_address_t sids[16];
241 };
242 autoreply define sr_policy_add
243 {
244   u32 client_index;
245   u32 context;
246   vl_api_ip6_address_t bsid_addr;
247   u32 weight;
248   bool is_encap;
249   bool is_spray;
250   u32 fib_table;
251   vl_api_srv6_sid_list_t sids;
252 };
253 '''
254         crc2 = get_crc(test_string, 'sr_policy_add')
255
256         self.assertNotEqual(crc, crc2)
257
258 if __name__ == '__main__':
259     unittest.main(verbosity=2)