25 #include <vapi/vpe.api.vapi.hpp>
26 #include <vapi/interface.api.vapi.hpp>
27 #include <fake.api.vapi.hpp>
38 #define WAIT_FOR_RESPONSE(param, ret) \
41 ret = con.wait_for_response (param); \
43 while (ret == VAPI_EAGAIN)
49 auto &p =
r.get_payload ();
50 printf (
"show_version_reply: program: `%s', version: `%s', build directory: "
51 "`%s', build date: `%s'\n",
52 p.program, p.version, p.build_directory, p.build_date);
53 ck_assert_str_eq (
"vpe", (
char *)p.program);
72 printf (
"--- Show version by reading response associated to request ---\n");
73 Show_version sv (
con);
78 auto &
r = sv.get_response ();
90 auto &
r = sv.get_response ();
99 printf (
"--- Show version by getting a callback ---\n");
101 Show_version sv (
con, std::ref (cb));
105 ck_assert_int_eq (1, cb.
called);
112 printf (
"--- Create/delete loopbacks by waiting for response ---\n");
113 const auto num_ifs = 5;
114 u8 mac_addresses[num_ifs][6];
115 memset (&mac_addresses, 0,
sizeof (mac_addresses));
116 u32 sw_if_indexes[num_ifs];
117 memset (&sw_if_indexes, 0xff,
sizeof (sw_if_indexes));
118 for (
int i = 0;
i < num_ifs; ++
i)
120 memcpy (&mac_addresses[
i],
"\1\2\3\4\5\6", 6);
121 mac_addresses[
i][5] =
i;
123 for (
int i = 0;
i < num_ifs; ++
i)
125 Create_loopback cl (
con);
126 auto &p = cl.get_request ().get_payload ();
127 memcpy (p.mac_address, mac_addresses[
i], sizeof (p.mac_address));
128 auto e = cl.execute ();
133 auto &rp = cl.get_response ().get_payload ();
134 ck_assert_int_eq (0, rp.retval);
135 sw_if_indexes[
i] = rp.sw_if_index;
137 for (
int i = 0;
i < num_ifs; ++
i)
139 printf (
"Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
141 mac_addresses[
i][0], mac_addresses[
i][1], mac_addresses[
i][2],
142 mac_addresses[
i][3], mac_addresses[
i][4], mac_addresses[
i][5],
147 bool seen[num_ifs] = {0};
148 Sw_interface_dump d (
con);
149 d.get_request ().get_payload ();
150 auto rv = d.execute ();
154 auto &rs = d.get_result_set ();
157 auto &p =
r.get_payload ();
158 for (
int i = 0;
i < num_ifs; ++
i)
160 if (sw_if_indexes[
i] == p.sw_if_index)
162 ck_assert_int_eq (0, seen[
i]);
167 for (
int i = 0;
i < num_ifs; ++
i)
169 ck_assert_int_eq (1, seen[
i]);
173 for (
int i = 0;
i < num_ifs; ++
i)
175 Delete_loopback dl (
con);
176 dl.get_request ().get_payload ().sw_if_index = sw_if_indexes[
i];
177 auto rv = dl.execute ();
181 auto &response = dl.get_response ();
182 auto rp = response.get_payload ();
183 ck_assert_int_eq (0, rp.retval);
184 printf (
"Deleted loopback with sw_if_index %u\n", sw_if_indexes[
i]);
188 Sw_interface_dump d (
con);
189 d.get_request ().get_payload ();
190 auto rv = d.execute ();
194 auto &rs = d.get_result_set ();
197 auto &p =
r.get_payload ();
198 for (
int i = 0;
i < num_ifs; ++
i)
200 ck_assert_int_ne (sw_if_indexes[
i], p.sw_if_index);
216 auto &
r = cl.get_response ();
231 auto &
r = dl.get_response ();
232 ck_assert_int_eq (0,
r.get_payload ().retval);
241 : called{0}, cbs{cbs} {};
243 std::array<Create_loopback_cb, num_ifs> &
cbs;
250 for (
auto &x : d.get_result_set ())
252 auto &p = x.get_payload ();
255 if (p.sw_if_index == y.sw_if_index)
263 ck_assert_int_eq (
true, y.seen);
272 printf (
"--- Create/delete loopbacks by getting a callback ---\n");
273 const auto num_ifs = 5;
274 u8 mac_addresses[num_ifs][6];
275 memset (&mac_addresses, 0,
sizeof (mac_addresses));
276 for (
int i = 0;
i < num_ifs; ++
i)
278 memcpy (&mac_addresses[
i],
"\1\2\3\4\5\6", 6);
279 mac_addresses[
i][5] =
i;
281 std::array<Create_loopback_cb, num_ifs> ccbs;
282 std::array<std::unique_ptr<Create_loopback>, num_ifs> clcs;
283 for (
int i = 0;
i < num_ifs; ++
i)
285 Create_loopback *cl =
new Create_loopback (
con, std::ref (ccbs[
i]));
287 auto &p = cl->get_request ().get_payload ();
288 memcpy (p.mac_address, mac_addresses[
i], sizeof (p.mac_address));
289 auto e = cl->execute ();
293 for (
int i = 0;
i < num_ifs; ++
i)
295 ck_assert_int_eq (1, ccbs[
i].called);
296 printf (
"Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
298 mac_addresses[
i][0], mac_addresses[
i][1], mac_addresses[
i][2],
299 mac_addresses[
i][3], mac_addresses[
i][4], mac_addresses[
i][5],
304 Sw_interface_dump d (
con, std::ref (swdcb));
305 d.get_request ().get_payload ();
306 auto rv = d.execute ();
310 ck_assert_int_ne (0, swdcb.
called);
311 std::array<Delete_loopback_cb, num_ifs> dcbs;
312 std::array<std::unique_ptr<Delete_loopback>, num_ifs> dlcs;
313 for (
int i = 0;
i < num_ifs; ++
i)
315 Delete_loopback *dl =
new Delete_loopback (
con, std::ref (dcbs[
i]));
317 auto &p = dl->get_request ().get_payload ();
318 p.sw_if_index = ccbs[
i].sw_if_index;
319 dcbs[
i].sw_if_index = ccbs[
i].sw_if_index;
320 auto e = dl->execute ();
326 ck_assert_int_eq (
true, x.called);
327 printf (
"Deleted loopback with sw_if_index %u\n", x.sw_if_index);
331 Sw_interface_dump d (
con);
332 d.get_request ().get_payload ();
333 auto rv = d.execute ();
337 auto &rs = d.get_result_set ();
340 auto &p =
r.get_payload ();
341 for (
int i = 0;
i < num_ifs; ++
i)
353 printf (
"--- Unsupported messages ---\n");
357 Test_fake_msg fake (
con);
362 printf (
"Constructing unsupported msg not possible - test pass.\n");
364 ck_assert_int_eq (
true, thrown);
368 Test_fake_dump fake (
con);
373 printf (
"Constructing unsupported dump not possible - test pass.\n");
375 ck_assert_int_eq (
true, thrown);
384 printf (
"Constructing unsupported event registration not possible - "
387 ck_assert_int_eq (
true, thrown);
394 Suite *s = suite_create (
"VAPI test");
396 TCase *tc_cpp_api = tcase_create (
"C++ API");
397 tcase_set_timeout (tc_cpp_api, 25);
399 tcase_add_test (tc_cpp_api, test_show_version_1);
400 tcase_add_test (tc_cpp_api, test_show_version_2);
401 tcase_add_test (tc_cpp_api, test_loopbacks_1);
402 tcase_add_test (tc_cpp_api, test_loopbacks_2);
403 tcase_add_test (tc_cpp_api, test_unsupported);
404 suite_add_tcase (s, tc_cpp_api);
409 int main (
int argc,
char *argv[])
413 printf (
"Invalid argc==`%d'\n", argc);
425 sr = srunner_create (s);
427 srunner_run_all (sr, CK_NORMAL);
428 number_failed = srunner_ntests_failed (sr);
430 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;