2 *------------------------------------------------------------------
3 * Copyright (c) 2017 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
25 #include <vppinfra/string.h>
26 #include <vapi/vapi.h>
27 #include <vapi/vpe.api.vapi.h>
28 #include <vapi/interface.api.vapi.h>
29 #include <vapi/l2.api.vapi.h>
30 #include <fake.api.vapi.h>
32 DEFINE_VAPI_MSG_IDS_VPE_API_JSON;
33 DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON;
34 DEFINE_VAPI_MSG_IDS_L2_API_JSON;
35 DEFINE_VAPI_MSG_IDS_FAKE_API_JSON;
37 static char *app_name = NULL;
38 static char *api_prefix = NULL;
39 static const int max_outstanding_requests = 64;
40 static const int response_queue_size = 32;
42 /* centos has ancient check so we hack our way around here
43 * to make it work somehow */
44 #ifndef ck_assert_ptr_eq
45 #define ck_assert_ptr_eq(X,Y) ck_assert_int_eq((long)X, (long)Y)
48 #ifndef ck_assert_ptr_ne
49 #define ck_assert_ptr_ne(X,Y) ck_assert_int_ne((long)X, (long)Y)
52 START_TEST (test_invalid_values)
55 vapi_error_e rv = vapi_ctx_alloc (&ctx);
56 ck_assert_int_eq (VAPI_OK, rv);
57 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
58 ck_assert_ptr_eq (NULL, sv);
59 rv = vapi_send (ctx, sv);
60 ck_assert_int_eq (VAPI_EINVAL, rv);
61 rv = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
62 response_queue_size, VAPI_MODE_BLOCKING, true);
63 ck_assert_int_eq (VAPI_OK, rv);
64 rv = vapi_send (ctx, NULL);
65 ck_assert_int_eq (VAPI_EINVAL, rv);
66 rv = vapi_send (NULL, NULL);
67 ck_assert_int_eq (VAPI_EINVAL, rv);
68 rv = vapi_recv (NULL, NULL, NULL, 0, 0);
69 ck_assert_int_eq (VAPI_EINVAL, rv);
70 rv = vapi_recv (ctx, NULL, NULL, 0, 0);
71 ck_assert_int_eq (VAPI_EINVAL, rv);
72 vapi_msg_show_version_reply *reply;
73 rv = vapi_recv (ctx, (void **) &reply, NULL, 0, 0);
74 ck_assert_int_eq (VAPI_EINVAL, rv);
75 rv = vapi_disconnect (ctx);
76 ck_assert_int_eq (VAPI_OK, rv);
82 START_TEST (test_hton_1)
84 const u16 _vl_msg_id = 1;
85 vapi_type_msg_header1_t h;
86 h._vl_msg_id = _vl_msg_id;
87 vapi_type_msg_header1_t_hton (&h);
88 ck_assert_int_eq (be16toh (h._vl_msg_id), _vl_msg_id);
93 START_TEST (test_hton_2)
95 const u16 _vl_msg_id = 1;
96 const u32 client_index = 3;
97 vapi_type_msg_header2_t h;
98 h._vl_msg_id = _vl_msg_id;
99 h.client_index = client_index;
100 vapi_type_msg_header2_t_hton (&h);
101 ck_assert_int_eq (be16toh (h._vl_msg_id), _vl_msg_id);
102 ck_assert_int_eq (h.client_index, client_index);
107 #define verify_hton_swap(expr, value) \
108 if (4 == sizeof (expr)) \
110 ck_assert_int_eq (expr, htobe32 (value)); \
112 else if (2 == sizeof (expr)) \
114 ck_assert_int_eq (expr, htobe16 (value)); \
118 ck_assert_int_eq (expr, value); \
121 START_TEST (test_hton_4)
123 const int vla_count = 3;
124 char x[sizeof (vapi_msg_bridge_domain_details) +
125 vla_count * sizeof (vapi_type_bridge_domain_sw_if)];
126 vapi_msg_bridge_domain_details *d = (void *) x;
128 d->header._vl_msg_id = cnt++;
129 d->header.context = cnt++;
130 d->payload.bd_id = cnt++;
131 d->payload.flood = cnt++;
132 d->payload.uu_flood = cnt++;
133 d->payload.forward = cnt++;
134 d->payload.learn = cnt++;
135 d->payload.arp_term = cnt++;
136 d->payload.mac_age = cnt++;
137 d->payload.bvi_sw_if_index = cnt++;
138 d->payload.n_sw_ifs = vla_count;
140 for (i = 0; i < vla_count; ++i)
142 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
143 det->context = cnt++;
144 det->sw_if_index = cnt++;
147 ck_assert_int_eq (sizeof (x), vapi_calc_bridge_domain_details_msg_size (d));
148 vapi_msg_bridge_domain_details_hton (d);
150 verify_hton_swap (d->header._vl_msg_id, tmp);
152 ck_assert_int_eq (d->header.context, tmp);
154 verify_hton_swap (d->payload.bd_id, tmp);
156 verify_hton_swap (d->payload.flood, tmp);
158 verify_hton_swap (d->payload.uu_flood, tmp);
160 verify_hton_swap (d->payload.forward, tmp);
162 verify_hton_swap (d->payload.learn, tmp);
164 verify_hton_swap (d->payload.arp_term, tmp);
166 verify_hton_swap (d->payload.mac_age, tmp);
168 verify_hton_swap (d->payload.bvi_sw_if_index, tmp);
170 ck_assert_int_eq (d->payload.n_sw_ifs, htobe32 (vla_count));
171 for (i = 0; i < vla_count; ++i)
173 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
174 verify_hton_swap (det->context, tmp);
176 verify_hton_swap (det->sw_if_index, tmp);
178 verify_hton_swap (det->shg, tmp);
181 vapi_msg_bridge_domain_details_ntoh (d);
183 ck_assert_int_eq (d->header._vl_msg_id, tmp);
185 ck_assert_int_eq (d->header.context, tmp);
187 ck_assert_int_eq (d->payload.bd_id, tmp);
189 ck_assert_int_eq (d->payload.flood, tmp);
191 ck_assert_int_eq (d->payload.uu_flood, tmp);
193 ck_assert_int_eq (d->payload.forward, tmp);
195 ck_assert_int_eq (d->payload.learn, tmp);
197 ck_assert_int_eq (d->payload.arp_term, tmp);
199 ck_assert_int_eq (d->payload.mac_age, tmp);
201 ck_assert_int_eq (d->payload.bvi_sw_if_index, tmp);
203 ck_assert_int_eq (d->payload.n_sw_ifs, vla_count);
204 for (i = 0; i < vla_count; ++i)
206 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
207 ck_assert_int_eq (det->context, tmp);
209 ck_assert_int_eq (det->sw_if_index, tmp);
211 ck_assert_int_eq (det->shg, tmp);
214 ck_assert_int_eq (sizeof (x), vapi_calc_bridge_domain_details_msg_size (d));
219 START_TEST (test_ntoh_1)
221 const u16 _vl_msg_id = 1;
222 vapi_type_msg_header1_t h;
223 h._vl_msg_id = _vl_msg_id;
224 vapi_type_msg_header1_t_ntoh (&h);
225 ck_assert_int_eq (htobe16 (h._vl_msg_id), _vl_msg_id);
230 START_TEST (test_ntoh_2)
232 const u16 _vl_msg_id = 1;
233 const u32 client_index = 3;
234 vapi_type_msg_header2_t h;
235 h._vl_msg_id = _vl_msg_id;
236 h.client_index = client_index;
237 vapi_type_msg_header2_t_ntoh (&h);
238 ck_assert_int_eq (htobe16 (h._vl_msg_id), _vl_msg_id);
239 ck_assert_int_eq (h.client_index, client_index);
244 #define verify_ntoh_swap(expr, value) \
245 if (4 == sizeof (expr)) \
247 ck_assert_int_eq (expr, be32toh (value)); \
249 else if (2 == sizeof (expr)) \
251 ck_assert_int_eq (expr, be16toh (value)); \
255 ck_assert_int_eq (expr, value); \
258 START_TEST (test_ntoh_4)
260 const int vla_count = 3;
261 char x[sizeof (vapi_msg_bridge_domain_details) +
262 vla_count * sizeof (vapi_type_bridge_domain_sw_if)];
263 vapi_msg_bridge_domain_details *d = (void *) x;
265 d->header._vl_msg_id = cnt++;
266 d->header.context = cnt++;
267 d->payload.bd_id = cnt++;
268 d->payload.flood = cnt++;
269 d->payload.uu_flood = cnt++;
270 d->payload.forward = cnt++;
271 d->payload.learn = cnt++;
272 d->payload.arp_term = cnt++;
273 d->payload.mac_age = cnt++;
274 d->payload.bvi_sw_if_index = cnt++;
275 d->payload.n_sw_ifs = htobe32 (vla_count);
277 for (i = 0; i < vla_count; ++i)
279 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
280 det->context = cnt++;
281 det->sw_if_index = cnt++;
284 vapi_msg_bridge_domain_details_ntoh (d);
285 ck_assert_int_eq (sizeof (x), vapi_calc_bridge_domain_details_msg_size (d));
287 verify_ntoh_swap (d->header._vl_msg_id, tmp);
289 ck_assert_int_eq (d->header.context, tmp);
291 verify_ntoh_swap (d->payload.bd_id, tmp);
293 verify_ntoh_swap (d->payload.flood, tmp);
295 verify_ntoh_swap (d->payload.uu_flood, tmp);
297 verify_ntoh_swap (d->payload.forward, tmp);
299 verify_ntoh_swap (d->payload.learn, tmp);
301 verify_ntoh_swap (d->payload.arp_term, tmp);
303 verify_ntoh_swap (d->payload.mac_age, tmp);
305 verify_ntoh_swap (d->payload.bvi_sw_if_index, tmp);
307 ck_assert_int_eq (d->payload.n_sw_ifs, vla_count);
308 for (i = 0; i < vla_count; ++i)
310 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
311 verify_ntoh_swap (det->context, tmp);
313 verify_ntoh_swap (det->sw_if_index, tmp);
315 verify_ntoh_swap (det->shg, tmp);
318 vapi_msg_bridge_domain_details_hton (d);
320 ck_assert_int_eq (d->header._vl_msg_id, tmp);
322 ck_assert_int_eq (d->header.context, tmp);
324 ck_assert_int_eq (d->payload.bd_id, tmp);
326 ck_assert_int_eq (d->payload.flood, tmp);
328 ck_assert_int_eq (d->payload.uu_flood, tmp);
330 ck_assert_int_eq (d->payload.forward, tmp);
332 ck_assert_int_eq (d->payload.learn, tmp);
334 ck_assert_int_eq (d->payload.arp_term, tmp);
336 ck_assert_int_eq (d->payload.mac_age, tmp);
338 ck_assert_int_eq (d->payload.bvi_sw_if_index, tmp);
340 ck_assert_int_eq (d->payload.n_sw_ifs, htobe32 (vla_count));
341 for (i = 0; i < vla_count; ++i)
343 vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
344 ck_assert_int_eq (det->context, tmp);
346 ck_assert_int_eq (det->sw_if_index, tmp);
348 ck_assert_int_eq (det->shg, tmp);
356 show_version_cb (vapi_ctx_t ctx, void *caller_ctx,
357 vapi_error_e rv, bool is_last,
358 vapi_payload_show_version_reply * p)
360 ck_assert_int_eq (VAPI_OK, rv);
361 ck_assert_int_eq (true, is_last);
362 ck_assert_str_eq ("vpe", (char *) p->program);
364 ("show_version_reply: program: `%s', version: `%s', build directory: "
365 "`%s', build date: `%s'\n", p->program, p->version, p->build_directory,
367 ++*(int *) caller_ctx;
375 u32 *sw_if_index_storage;
376 } test_create_loopback_ctx_t;
379 loopback_create_cb (vapi_ctx_t ctx, void *caller_ctx,
380 vapi_error_e rv, bool is_last,
381 vapi_payload_create_loopback_reply * p)
383 test_create_loopback_ctx_t *clc = caller_ctx;
384 ck_assert_int_eq (clc->expected_retval, p->retval);
385 *clc->sw_if_index_storage = p->sw_if_index;
394 u32 *sw_if_index_storage;
395 } test_delete_loopback_ctx_t;
398 loopback_delete_cb (vapi_ctx_t ctx, void *caller_ctx,
399 vapi_error_e rv, bool is_last,
400 vapi_payload_delete_loopback_reply * p)
402 test_delete_loopback_ctx_t *dlc = caller_ctx;
403 ck_assert_int_eq (dlc->expected_retval, p->retval);
408 START_TEST (test_connect)
411 vapi_error_e rv = vapi_ctx_alloc (&ctx);
412 ck_assert_int_eq (VAPI_OK, rv);
413 rv = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
414 response_queue_size, VAPI_MODE_BLOCKING, true);
415 ck_assert_int_eq (VAPI_OK, rv);
416 rv = vapi_disconnect (ctx);
417 ck_assert_int_eq (VAPI_OK, rv);
426 setup_blocking (void)
428 vapi_error_e rv = vapi_ctx_alloc (&ctx);
429 ck_assert_int_eq (VAPI_OK, rv);
430 rv = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
431 response_queue_size, VAPI_MODE_BLOCKING, true);
432 ck_assert_int_eq (VAPI_OK, rv);
436 setup_nonblocking (void)
438 vapi_error_e rv = vapi_ctx_alloc (&ctx);
439 ck_assert_int_eq (VAPI_OK, rv);
440 rv = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
441 response_queue_size, VAPI_MODE_NONBLOCKING, true);
442 ck_assert_int_eq (VAPI_OK, rv);
448 vapi_disconnect (ctx);
452 START_TEST (test_show_version_1)
454 printf ("--- Basic show version message - reply test ---\n");
455 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
456 ck_assert_ptr_ne (NULL, sv);
457 vapi_msg_show_version_hton (sv);
458 vapi_error_e rv = vapi_send (ctx, sv);
459 ck_assert_int_eq (VAPI_OK, rv);
460 vapi_msg_show_version_reply *resp;
462 rv = vapi_recv (ctx, (void *) &resp, &size, 0, 0);
463 ck_assert_int_eq (VAPI_OK, rv);
465 show_version_cb (NULL, &dummy, VAPI_OK, true, &resp->payload);
466 vapi_msg_free (ctx, resp);
471 START_TEST (test_show_version_2)
474 printf ("--- Show version via blocking callback API ---\n");
475 const int attempts = response_queue_size * 4;
477 for (i = 0; i < attempts; ++i)
479 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
480 ck_assert_ptr_ne (NULL, sv);
481 vapi_error_e rv = vapi_show_version (ctx, sv, show_version_cb, &called);
482 ck_assert_int_eq (VAPI_OK, rv);
484 ck_assert_int_eq (attempts, called);
496 } sw_interface_dump_ctx;
499 sw_interface_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
500 vapi_error_e rv, bool is_last,
501 vapi_payload_sw_interface_details * reply)
503 sw_interface_dump_ctx *dctx = callback_ctx;
504 ck_assert_int_eq (false, dctx->last_called);
507 ck_assert (NULL == reply);
508 dctx->last_called = true;
512 ck_assert (NULL != reply);
513 printf ("Interface dump entry: [%u]: %s\n", reply->sw_if_index,
514 reply->interface_name);
516 for (i = 0; i < dctx->num_ifs; ++i)
518 if (dctx->sw_if_indexes[i] == reply->sw_if_index)
520 ck_assert_int_eq (false, dctx->seen[i]);
521 dctx->seen[i] = true;
529 START_TEST (test_loopbacks_1)
531 printf ("--- Create/delete loopbacks using blocking API ---\n");
532 const size_t num_ifs = 5;
533 u8 mac_addresses[num_ifs][6];
534 clib_memset (&mac_addresses, 0, sizeof (mac_addresses));
535 u32 sw_if_indexes[num_ifs];
536 clib_memset (&sw_if_indexes, 0xff, sizeof (sw_if_indexes));
537 test_create_loopback_ctx_t clcs[num_ifs];
538 clib_memset (&clcs, 0, sizeof (clcs));
539 test_delete_loopback_ctx_t dlcs[num_ifs];
540 clib_memset (&dlcs, 0, sizeof (dlcs));
542 for (i = 0; i < num_ifs; ++i)
544 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
545 mac_addresses[i][5] = i;
546 clcs[i].sw_if_index_storage = &sw_if_indexes[i];
548 for (i = 0; i < num_ifs; ++i)
550 vapi_msg_create_loopback *cl = vapi_alloc_create_loopback (ctx);
551 memcpy (cl->payload.mac_address, mac_addresses[i],
552 sizeof (cl->payload.mac_address));
554 vapi_create_loopback (ctx, cl, loopback_create_cb, &clcs[i]);
555 ck_assert_int_eq (VAPI_OK, rv);
557 for (i = 0; i < num_ifs; ++i)
559 ck_assert_int_eq (1, clcs[i].called);
560 printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
562 mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
563 mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
567 sw_interface_dump_ctx dctx = { false, num_ifs, sw_if_indexes, seen, 0 };
568 vapi_msg_sw_interface_dump *dump;
570 const int attempts = response_queue_size * 4;
571 for (i = 0; i < attempts; ++i)
573 dctx.last_called = false;
574 clib_memset (&seen, 0, sizeof (seen));
575 dump = vapi_alloc_sw_interface_dump (ctx);
576 dump->payload.name_filter_valid = 0;
577 clib_memset (dump->payload.name_filter.buf, 0,
578 dump->payload.name_filter.length);
579 while (VAPI_EAGAIN ==
581 vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb,
584 ck_assert_int_eq (true, dctx.last_called);
586 for (j = 0; j < num_ifs; ++j)
588 ck_assert_int_eq (true, seen[j]);
591 clib_memset (&seen, 0, sizeof (seen));
592 for (i = 0; i < num_ifs; ++i)
594 vapi_msg_delete_loopback *dl = vapi_alloc_delete_loopback (ctx);
595 dl->payload.sw_if_index = sw_if_indexes[i];
597 vapi_delete_loopback (ctx, dl, loopback_delete_cb, &dlcs[i]);
598 ck_assert_int_eq (VAPI_OK, rv);
600 for (i = 0; i < num_ifs; ++i)
602 ck_assert_int_eq (1, dlcs[i].called);
603 printf ("Deleted loopback with sw_if_index %u\n", sw_if_indexes[i]);
605 dctx.last_called = false;
606 clib_memset (&seen, 0, sizeof (seen));
607 dump = vapi_alloc_sw_interface_dump (ctx);
608 dump->payload.name_filter_valid = 0;
609 clib_memset (dump->payload.name_filter.buf, 0,
610 dump->payload.name_filter.length);
611 while (VAPI_EAGAIN ==
613 vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb, &dctx)))
615 ck_assert_int_eq (true, dctx.last_called);
616 for (i = 0; i < num_ifs; ++i)
618 ck_assert_int_eq (false, seen[i]);
624 START_TEST (test_show_version_3)
626 printf ("--- Show version via async callback ---\n");
629 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
630 ck_assert_ptr_ne (NULL, sv);
631 while (VAPI_EAGAIN ==
632 (rv = vapi_show_version (ctx, sv, show_version_cb, &called)))
634 ck_assert_int_eq (VAPI_OK, rv);
635 ck_assert_int_eq (0, called);
636 rv = vapi_dispatch (ctx);
637 ck_assert_int_eq (VAPI_OK, rv);
638 ck_assert_int_eq (1, called);
640 rv = vapi_dispatch (ctx);
641 ck_assert_int_eq (VAPI_OK, rv);
642 ck_assert_int_eq (0, called);
647 START_TEST (test_show_version_4)
649 printf ("--- Show version via async callback - multiple messages ---\n");
651 const size_t num_req = 5;
652 int contexts[num_req];
653 clib_memset (contexts, 0, sizeof (contexts));
655 for (i = 0; i < num_req; ++i)
657 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
658 ck_assert_ptr_ne (NULL, sv);
659 while (VAPI_EAGAIN ==
661 vapi_show_version (ctx, sv, show_version_cb, &contexts[i])))
663 ck_assert_int_eq (VAPI_OK, rv);
665 for (j = 0; j < num_req; ++j)
667 ck_assert_int_eq (0, contexts[j]);
670 rv = vapi_dispatch (ctx);
671 ck_assert_int_eq (VAPI_OK, rv);
672 for (i = 0; i < num_req; ++i)
674 ck_assert_int_eq (1, contexts[i]);
676 clib_memset (contexts, 0, sizeof (contexts));
677 rv = vapi_dispatch (ctx);
678 ck_assert_int_eq (VAPI_OK, rv);
679 for (i = 0; i < num_req; ++i)
681 ck_assert_int_eq (0, contexts[i]);
687 START_TEST (test_loopbacks_2)
689 printf ("--- Create/delete loopbacks using non-blocking API ---\n");
691 const size_t num_ifs = 5;
692 u8 mac_addresses[num_ifs][6];
693 clib_memset (&mac_addresses, 0, sizeof (mac_addresses));
694 u32 sw_if_indexes[num_ifs];
695 clib_memset (&sw_if_indexes, 0xff, sizeof (sw_if_indexes));
696 test_create_loopback_ctx_t clcs[num_ifs];
697 clib_memset (&clcs, 0, sizeof (clcs));
698 test_delete_loopback_ctx_t dlcs[num_ifs];
699 clib_memset (&dlcs, 0, sizeof (dlcs));
701 for (i = 0; i < num_ifs; ++i)
703 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
704 mac_addresses[i][5] = i;
705 clcs[i].sw_if_index_storage = &sw_if_indexes[i];
707 for (i = 0; i < num_ifs; ++i)
709 vapi_msg_create_loopback *cl = vapi_alloc_create_loopback (ctx);
710 memcpy (cl->payload.mac_address, mac_addresses[i],
711 sizeof (cl->payload.mac_address));
712 while (VAPI_EAGAIN ==
714 vapi_create_loopback (ctx, cl, loopback_create_cb, &clcs[i])))
716 ck_assert_int_eq (VAPI_OK, rv);
718 rv = vapi_dispatch (ctx);
719 ck_assert_int_eq (VAPI_OK, rv);
720 for (i = 0; i < num_ifs; ++i)
722 ck_assert_int_eq (1, clcs[i].called);
723 printf ("Loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
725 mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
726 mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
730 clib_memset (&seen, 0, sizeof (seen));
731 sw_interface_dump_ctx dctx = { false, num_ifs, sw_if_indexes, seen, 0 };
732 vapi_msg_sw_interface_dump *dump = vapi_alloc_sw_interface_dump (ctx);
733 dump->payload.name_filter_valid = 0;
734 clib_memset (dump->payload.name_filter.buf, 0,
735 dump->payload.name_filter.length);
736 while (VAPI_EAGAIN ==
738 vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb, &dctx)))
740 for (i = 0; i < num_ifs; ++i)
742 ck_assert_int_eq (false, seen[i]);
744 clib_memset (&seen, 0, sizeof (seen));
745 ck_assert_int_eq (false, dctx.last_called);
746 rv = vapi_dispatch (ctx);
747 ck_assert_int_eq (VAPI_OK, rv);
748 for (i = 0; i < num_ifs; ++i)
750 ck_assert_int_eq (true, seen[i]);
752 clib_memset (&seen, 0, sizeof (seen));
753 ck_assert_int_eq (true, dctx.last_called);
754 for (i = 0; i < num_ifs; ++i)
756 vapi_msg_delete_loopback *dl = vapi_alloc_delete_loopback (ctx);
757 dl->payload.sw_if_index = sw_if_indexes[i];
758 while (VAPI_EAGAIN ==
760 vapi_delete_loopback (ctx, dl, loopback_delete_cb, &dlcs[i])))
762 ck_assert_int_eq (VAPI_OK, rv);
764 rv = vapi_dispatch (ctx);
765 ck_assert_int_eq (VAPI_OK, rv);
766 for (i = 0; i < num_ifs; ++i)
768 ck_assert_int_eq (1, dlcs[i].called);
769 printf ("Deleted loopback with sw_if_index %u\n", sw_if_indexes[i]);
771 clib_memset (&seen, 0, sizeof (seen));
772 dctx.last_called = false;
773 dump = vapi_alloc_sw_interface_dump (ctx);
774 dump->payload.name_filter_valid = 0;
775 clib_memset (dump->payload.name_filter.buf, 0,
776 dump->payload.name_filter.length);
777 while (VAPI_EAGAIN ==
779 vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb, &dctx)))
781 rv = vapi_dispatch (ctx);
782 ck_assert_int_eq (VAPI_OK, rv);
783 for (i = 0; i < num_ifs; ++i)
785 ck_assert_int_eq (false, seen[i]);
787 clib_memset (&seen, 0, sizeof (seen));
788 ck_assert_int_eq (true, dctx.last_called);
794 generic_cb (vapi_ctx_t ctx, void *callback_ctx, vapi_msg_id_t id, void *msg)
796 int *called = callback_ctx;
797 ck_assert_int_eq (0, *called);
799 ck_assert_int_eq (id, vapi_msg_id_show_version_reply);
800 ck_assert_ptr_ne (NULL, msg);
801 vapi_msg_show_version_reply *reply = msg;
802 ck_assert_str_eq ("vpe", (char *) reply->payload.program);
806 START_TEST (test_show_version_5)
808 printf ("--- Receive show version using generic callback - nonblocking "
811 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
812 ck_assert_ptr_ne (NULL, sv);
813 vapi_msg_show_version_hton (sv);
814 while (VAPI_EAGAIN == (rv = vapi_send (ctx, sv)))
816 ck_assert_int_eq (VAPI_OK, rv);
818 vapi_set_generic_event_cb (ctx, generic_cb, &called);
819 ck_assert_int_eq (VAPI_OK, rv);
820 rv = vapi_dispatch_one (ctx);
821 ck_assert_int_eq (VAPI_OK, rv);
822 ck_assert_int_eq (1, called);
823 sv = vapi_alloc_show_version (ctx);
824 ck_assert_ptr_ne (NULL, sv);
825 vapi_msg_show_version_hton (sv);
826 while (VAPI_EAGAIN == (rv = vapi_send (ctx, sv)))
828 ck_assert_int_eq (VAPI_OK, rv);
829 vapi_clear_generic_event_cb (ctx);
830 rv = vapi_dispatch_one (ctx);
831 ck_assert_int_eq (VAPI_OK, rv);
832 ck_assert_int_eq (1, called); /* needs to remain unchanged */
838 show_version_no_cb (vapi_ctx_t ctx, void *caller_ctx,
839 vapi_error_e rv, bool is_last,
840 vapi_payload_show_version_reply * p)
842 ck_assert_int_eq (VAPI_ENORESP, rv);
843 ck_assert_int_eq (true, is_last);
844 ck_assert_ptr_eq (NULL, p);
845 ++*(int *) caller_ctx;
849 START_TEST (test_no_response_1)
851 printf ("--- Simulate no response to regular message ---\n");
853 vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
854 ck_assert_ptr_ne (NULL, sv);
855 sv->header._vl_msg_id = ~0; /* malformed ID causes vpp to drop the msg */
857 while (VAPI_EAGAIN ==
858 (rv = vapi_show_version (ctx, sv, show_version_no_cb, &called)))
860 ck_assert_int_eq (VAPI_OK, rv);
861 sv = vapi_alloc_show_version (ctx);
862 ck_assert_ptr_ne (NULL, sv);
863 while (VAPI_EAGAIN ==
864 (rv = vapi_show_version (ctx, sv, show_version_cb, &called)))
866 ck_assert_int_eq (VAPI_OK, rv);
867 rv = vapi_dispatch (ctx);
868 ck_assert_int_eq (VAPI_OK, rv);
869 ck_assert_int_eq (2, called);
875 no_msg_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
876 vapi_error_e rv, bool is_last,
877 vapi_payload_sw_interface_details * reply)
879 int *called = callback_ctx;
881 ck_assert_int_eq (VAPI_OK, rv);
882 ck_assert_int_eq (true, is_last);
883 ck_assert_ptr_eq (NULL, reply);
887 START_TEST (test_no_response_2)
889 printf ("--- Simulate no response to dump message ---\n");
891 vapi_msg_sw_interface_dump *dump = vapi_alloc_sw_interface_dump (ctx);
892 dump->header._vl_msg_id = ~0; /* malformed ID causes vpp to drop the msg */
894 while (VAPI_EAGAIN ==
895 (rv = vapi_sw_interface_dump (ctx, dump, no_msg_cb, &no_called)))
897 ck_assert_int_eq (VAPI_OK, rv);
898 rv = vapi_dispatch (ctx);
899 ck_assert_int_eq (VAPI_OK, rv);
900 ck_assert_int_eq (1, no_called);
905 START_TEST (test_unsupported)
907 printf ("--- Unsupported messages ---\n");
908 bool available = vapi_is_msg_available (ctx, vapi_msg_id_test_fake_msg);
909 ck_assert_int_eq (false, available);
917 Suite *s = suite_create ("VAPI test");
919 TCase *tc_negative = tcase_create ("Negative tests");
920 tcase_add_test (tc_negative, test_invalid_values);
921 suite_add_tcase (s, tc_negative);
923 TCase *tc_swap = tcase_create ("Byteswap tests");
924 tcase_add_test (tc_swap, test_hton_1);
925 tcase_add_test (tc_swap, test_hton_2);
926 tcase_add_test (tc_swap, test_hton_4);
927 tcase_add_test (tc_swap, test_ntoh_1);
928 tcase_add_test (tc_swap, test_ntoh_2);
929 tcase_add_test (tc_swap, test_ntoh_4);
930 suite_add_tcase (s, tc_swap);
932 TCase *tc_connect = tcase_create ("Connect");
933 tcase_add_test (tc_connect, test_connect);
934 suite_add_tcase (s, tc_connect);
936 TCase *tc_block = tcase_create ("Blocking API");
937 tcase_set_timeout (tc_block, 25);
938 tcase_add_checked_fixture (tc_block, setup_blocking, teardown);
939 tcase_add_test (tc_block, test_show_version_1);
940 tcase_add_test (tc_block, test_show_version_2);
941 tcase_add_test (tc_block, test_loopbacks_1);
942 suite_add_tcase (s, tc_block);
944 TCase *tc_nonblock = tcase_create ("Nonblocking API");
945 tcase_set_timeout (tc_nonblock, 25);
946 tcase_add_checked_fixture (tc_nonblock, setup_nonblocking, teardown);
947 tcase_add_test (tc_nonblock, test_show_version_3);
948 tcase_add_test (tc_nonblock, test_show_version_4);
949 tcase_add_test (tc_nonblock, test_show_version_5);
950 tcase_add_test (tc_nonblock, test_loopbacks_2);
951 tcase_add_test (tc_nonblock, test_no_response_1);
952 tcase_add_test (tc_nonblock, test_no_response_2);
953 suite_add_tcase (s, tc_nonblock);
955 TCase *tc_unsupported = tcase_create ("Unsupported message");
956 tcase_add_checked_fixture (tc_unsupported, setup_blocking, teardown);
957 tcase_add_test (tc_unsupported, test_unsupported);
958 suite_add_tcase (s, tc_unsupported);
964 main (int argc, char *argv[])
968 printf ("Invalid argc==`%d'\n", argc);
972 api_prefix = argv[2];
973 printf ("App name: `%s', API prefix: `%s'\n", app_name, api_prefix);
980 sr = srunner_create (s);
982 srunner_run_all (sr, CK_NORMAL);
983 number_failed = srunner_ntests_failed (sr);
985 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
989 * fd.io coding-style-patch-verification: ON
992 * eval: (c-set-style "gnu")