4 from vppapigen import VPPAPI, Option, ParseError, Union, foldup_crcs, global_types
8 # - test parsing of options, typedefs, enums, defines
9 # - test JSON, C output
12 class TestVersion(unittest.TestCase):
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))
22 class TestUnion(unittest.TestCase):
34 r = self.parser.parse_string(test_string)
35 self.assertTrue(isinstance(r[0], Union))
37 def test_union_vla(self):
43 autoreply define foo {
44 vl_api_foo_union_vla_t v;
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)
54 union foo_union_vla2 {
59 autoreply define foo2 {
60 vl_api_foo_union_vla2_t v;
63 self.assertRaises(ValueError, self.parser.parse_string, test_string2)
66 union foo_union_vla3 {
70 autoreply define foo3 {
71 vl_api_foo_union_vla3_t v;
75 self.assertRaises(ValueError, self.parser.parse_string, test_string3)
77 class TestTypedef(unittest.TestCase):
82 def test_duplicatetype(self):
84 typedef foo1 { u8 dummy; };
85 typedef foo1 { u8 dummy; };
87 self.assertRaises(KeyError, self.parser.parse_string, test_string)
90 class TestDefine(unittest.TestCase):
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))
101 test_string = 'define { u8 foo;};'
102 with self.assertRaises(ParseError) as ctx:
103 self.parser.parse_string(test_string)
105 def test_flags(self):
107 manual_print dont_trace manual_endian define foo { u8 foo; };
108 define foo_reply {u32 context; i32 retval; };
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']:
116 self.assertTrue(d.dont_trace)
117 self.assertTrue(d.manual_endian)
118 self.assertTrue(d.manual_print)
119 self.assertFalse(d.autoreply)
122 nonexisting_flag define foo { u8 foo; };
124 with self.assertRaises(ParseError):
125 self.parser.parse_string(test_string)
128 class TestService(unittest.TestCase):
131 cls.parser = VPPAPI()
133 def test_service(self):
135 autoreply define show_version { u8 foo;};
136 service { rpc show_version returns show_version_reply; };
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')
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]
154 class TestCRC(unittest.TestCase):
157 typedef list { u8 foo; };
158 autoreply define foo { u8 foo; vl_api_list_t l;};
160 crc = get_crc(test_string, 'foo')
162 # modify underlaying type
164 typedef list { u8 foo2; };
165 autoreply define foo { u8 foo; vl_api_list_t l;};
167 crc2 = get_crc(test_string, 'foo')
168 self.assertNotEqual(crc, crc2)
170 # two user-defined types
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;};
176 crc3 = get_crc(test_string, 'foo')
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;};
183 crc4 = get_crc(test_string, 'foo')
184 self.assertNotEqual(crc3, crc4)
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;};
191 crc5 = get_crc(test_string, 'foo')
192 self.assertNotEqual(crc4, crc5)
199 typedef srv6_sid_list
204 vl_api_ip6_address_t sids[16];
206 autoreply define sr_policy_add
210 vl_api_ip6_address_t bsid_addr;
215 vl_api_srv6_sid_list_t sids;
219 crc = get_crc(test_string, 'sr_policy_add')
226 typedef srv6_sid_list
230 vl_api_ip6_address_t sids[16];
232 autoreply define sr_policy_add
236 vl_api_ip6_address_t bsid_addr;
241 vl_api_srv6_sid_list_t sids;
244 crc2 = get_crc(test_string, 'sr_policy_add')
246 self.assertNotEqual(crc, crc2)
248 if __name__ == '__main__':
249 unittest.main(verbosity=2)