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)
127 def test_options(self):
129 define foo { option deprecated; u8 foo; };
130 define foo_reply {u32 context; i32 retval; };
132 r = self.parser.parse_string(test_string)
133 self.assertIsNotNone(r)
134 s = self.parser.process(r)
135 self.assertIsNotNone(s)
138 class TestService(unittest.TestCase):
141 cls.parser = VPPAPI()
143 def test_service(self):
145 autoreply define show_version { u8 foo;};
146 service { rpc show_version returns show_version_reply; };
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')
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]
164 class TestCRC(unittest.TestCase):
167 typedef list { u8 foo; };
168 autoreply define foo { u8 foo; vl_api_list_t l;};
170 crc = get_crc(test_string, 'foo')
172 # modify underlaying type
174 typedef list { u8 foo2; };
175 autoreply define foo { u8 foo; vl_api_list_t l;};
177 crc2 = get_crc(test_string, 'foo')
178 self.assertNotEqual(crc, crc2)
180 # two user-defined types
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;};
186 crc3 = get_crc(test_string, 'foo')
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;};
193 crc4 = get_crc(test_string, 'foo')
194 self.assertNotEqual(crc3, crc4)
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;};
201 crc5 = get_crc(test_string, 'foo')
202 self.assertNotEqual(crc4, crc5)
209 typedef srv6_sid_list
214 vl_api_ip6_address_t sids[16];
216 autoreply define sr_policy_add
220 vl_api_ip6_address_t bsid_addr;
225 vl_api_srv6_sid_list_t sids;
229 crc = get_crc(test_string, 'sr_policy_add')
236 typedef srv6_sid_list
240 vl_api_ip6_address_t sids[16];
242 autoreply define sr_policy_add
246 vl_api_ip6_address_t bsid_addr;
251 vl_api_srv6_sid_list_t sids;
254 crc2 = get_crc(test_string, 'sr_policy_add')
256 self.assertNotEqual(crc, crc2)
258 if __name__ == '__main__':
259 unittest.main(verbosity=2)