Add C++ API
[vpp.git] / test / ext / vapi_c_test.c
1 /*
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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  *------------------------------------------------------------------
16  */
17
18 #include <stdio.h>
19 #include <endian.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <assert.h>
23 #include <setjmp.h>
24 #include <check.h>
25 #include <vapi/vapi.h>
26 #include <vapi/vpe.api.vapi.h>
27 #include <vapi/interface.api.vapi.h>
28 #include <vapi/l2.api.vapi.h>
29 #include <vapi/stats.api.vapi.h>
30 #include <fake.api.vapi.h>
31
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_STATS_API_JSON;
36 DEFINE_VAPI_MSG_IDS_FAKE_API_JSON;
37
38 static char *app_name = NULL;
39 static char *api_prefix = NULL;
40 static const int max_outstanding_requests = 64;
41 static const int response_queue_size = 32;
42
43 START_TEST (test_invalid_values)
44 {
45   vapi_ctx_t ctx;
46   vapi_error_e rv = vapi_ctx_alloc (&ctx);
47   ck_assert_int_eq (VAPI_OK, rv);
48   vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
49   ck_assert_ptr_eq (NULL, sv);
50   rv = vapi_send (ctx, sv);
51   ck_assert_int_eq (VAPI_EINVAL, rv);
52   rv = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
53                      response_queue_size, VAPI_MODE_BLOCKING);
54   ck_assert_int_eq (VAPI_OK, rv);
55   rv = vapi_send (ctx, NULL);
56   ck_assert_int_eq (VAPI_EINVAL, rv);
57   rv = vapi_send (NULL, NULL);
58   ck_assert_int_eq (VAPI_EINVAL, rv);
59   rv = vapi_recv (NULL, NULL, NULL);
60   ck_assert_int_eq (VAPI_EINVAL, rv);
61   rv = vapi_recv (ctx, NULL, NULL);
62   ck_assert_int_eq (VAPI_EINVAL, rv);
63   vapi_msg_show_version_reply *reply;
64   rv = vapi_recv (ctx, (void **) &reply, NULL);
65   ck_assert_int_eq (VAPI_EINVAL, rv);
66   rv = vapi_disconnect (ctx);
67   ck_assert_int_eq (VAPI_OK, rv);
68   vapi_ctx_free (ctx);
69 }
70
71 END_TEST;
72
73 START_TEST (test_hton_1)
74 {
75   const u16 _vl_msg_id = 1;
76   vapi_type_msg_header1_t h;
77   h._vl_msg_id = _vl_msg_id;
78   vapi_type_msg_header1_t_hton (&h);
79   ck_assert_int_eq (be16toh (h._vl_msg_id), _vl_msg_id);
80 }
81
82 END_TEST;
83
84 START_TEST (test_hton_2)
85 {
86   const u16 _vl_msg_id = 1;
87   const u32 client_index = 3;
88   vapi_type_msg_header2_t h;
89   h._vl_msg_id = _vl_msg_id;
90   h.client_index = client_index;
91   vapi_type_msg_header2_t_hton (&h);
92   ck_assert_int_eq (be16toh (h._vl_msg_id), _vl_msg_id);
93   ck_assert_int_eq (h.client_index, client_index);
94 }
95
96 END_TEST;
97
98 START_TEST (test_hton_3)
99 {
100   const size_t data_size = 10;
101   vapi_msg_vnet_interface_combined_counters *m =
102     malloc (sizeof (vapi_msg_vnet_interface_combined_counters) +
103             data_size * sizeof (vapi_type_vlib_counter));
104   ck_assert_ptr_ne (NULL, m);
105   vapi_payload_vnet_interface_combined_counters *p = &m->payload;
106   const u16 _vl_msg_id = 1;
107   p->_vl_msg_id = _vl_msg_id;
108   const u32 first_sw_if_index = 2;
109   p->first_sw_if_index = first_sw_if_index;
110   p->count = data_size;
111   const u64 packets = 1234;
112   const u64 bytes = 2345;
113   int i;
114   for (i = 0; i < data_size; ++i)
115     {
116       p->data[i].packets = packets;
117       p->data[i].bytes = bytes;
118     }
119   vapi_msg_vnet_interface_combined_counters_hton (m);
120   ck_assert_int_eq (_vl_msg_id, be16toh (p->_vl_msg_id));
121   ck_assert_int_eq (first_sw_if_index, be32toh (p->first_sw_if_index));
122   ck_assert_int_eq (data_size, be32toh (p->count));
123   for (i = 0; i < data_size; ++i)
124     {
125       ck_assert_int_eq (packets, be64toh (p->data[i].packets));
126       ck_assert_int_eq (bytes, be64toh (p->data[i].bytes));
127     }
128   free (p);
129 }
130
131 END_TEST;
132
133 #define verify_hton_swap(expr, value)           \
134   if (4 == sizeof (expr))                       \
135     {                                           \
136       ck_assert_int_eq (expr, htobe32 (value)); \
137     }                                           \
138   else if (2 == sizeof (expr))                  \
139     {                                           \
140       ck_assert_int_eq (expr, htobe16 (value)); \
141     }                                           \
142   else                                          \
143     {                                           \
144       ck_assert_int_eq (expr, value);           \
145     }
146
147 START_TEST (test_hton_4)
148 {
149   const int vla_count = 3;
150   char x[sizeof (vapi_msg_bridge_domain_details) +
151          vla_count * sizeof (vapi_type_bridge_domain_sw_if)];
152   vapi_msg_bridge_domain_details *d = (void *) x;
153   int cnt = 1;
154   d->header._vl_msg_id = cnt++;
155   d->header.context = cnt++;
156   d->payload.bd_id = cnt++;
157   d->payload.flood = cnt++;
158   d->payload.uu_flood = cnt++;
159   d->payload.forward = cnt++;
160   d->payload.learn = cnt++;
161   d->payload.arp_term = cnt++;
162   d->payload.mac_age = cnt++;
163   d->payload.bvi_sw_if_index = cnt++;
164   d->payload.n_sw_ifs = vla_count;
165   int i;
166   for (i = 0; i < vla_count; ++i)
167     {
168       vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
169       det->context = cnt++;
170       det->sw_if_index = cnt++;
171       det->shg = cnt++;
172     }
173   ck_assert_int_eq (sizeof (x), vapi_calc_bridge_domain_details_msg_size (d));
174   vapi_msg_bridge_domain_details_hton (d);
175   int tmp = 1;
176   verify_hton_swap (d->header._vl_msg_id, tmp);
177   ++tmp;
178   ck_assert_int_eq (d->header.context, tmp);
179   ++tmp;
180   verify_hton_swap (d->payload.bd_id, tmp);
181   ++tmp;
182   verify_hton_swap (d->payload.flood, tmp);
183   ++tmp;
184   verify_hton_swap (d->payload.uu_flood, tmp);
185   ++tmp;
186   verify_hton_swap (d->payload.forward, tmp);
187   ++tmp;
188   verify_hton_swap (d->payload.learn, tmp);
189   ++tmp;
190   verify_hton_swap (d->payload.arp_term, tmp);
191   ++tmp;
192   verify_hton_swap (d->payload.mac_age, tmp);
193   ++tmp;
194   verify_hton_swap (d->payload.bvi_sw_if_index, tmp);
195   ++tmp;
196   ck_assert_int_eq (d->payload.n_sw_ifs, htobe32 (vla_count));
197   for (i = 0; i < vla_count; ++i)
198     {
199       vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
200       verify_hton_swap (det->context, tmp);
201       ++tmp;
202       verify_hton_swap (det->sw_if_index, tmp);
203       ++tmp;
204       verify_hton_swap (det->shg, tmp);
205       ++tmp;
206     }
207   vapi_msg_bridge_domain_details_ntoh (d);
208   tmp = 1;
209   ck_assert_int_eq (d->header._vl_msg_id, tmp);
210   ++tmp;
211   ck_assert_int_eq (d->header.context, tmp);
212   ++tmp;
213   ck_assert_int_eq (d->payload.bd_id, tmp);
214   ++tmp;
215   ck_assert_int_eq (d->payload.flood, tmp);
216   ++tmp;
217   ck_assert_int_eq (d->payload.uu_flood, tmp);
218   ++tmp;
219   ck_assert_int_eq (d->payload.forward, tmp);
220   ++tmp;
221   ck_assert_int_eq (d->payload.learn, tmp);
222   ++tmp;
223   ck_assert_int_eq (d->payload.arp_term, tmp);
224   ++tmp;
225   ck_assert_int_eq (d->payload.mac_age, tmp);
226   ++tmp;
227   ck_assert_int_eq (d->payload.bvi_sw_if_index, tmp);
228   ++tmp;
229   ck_assert_int_eq (d->payload.n_sw_ifs, vla_count);
230   for (i = 0; i < vla_count; ++i)
231     {
232       vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
233       ck_assert_int_eq (det->context, tmp);
234       ++tmp;
235       ck_assert_int_eq (det->sw_if_index, tmp);
236       ++tmp;
237       ck_assert_int_eq (det->shg, tmp);
238       ++tmp;
239     }
240   ck_assert_int_eq (sizeof (x), vapi_calc_bridge_domain_details_msg_size (d));
241 }
242
243 END_TEST;
244
245 START_TEST (test_ntoh_1)
246 {
247   const u16 _vl_msg_id = 1;
248   vapi_type_msg_header1_t h;
249   h._vl_msg_id = _vl_msg_id;
250   vapi_type_msg_header1_t_ntoh (&h);
251   ck_assert_int_eq (htobe16 (h._vl_msg_id), _vl_msg_id);
252 }
253
254 END_TEST;
255
256 START_TEST (test_ntoh_2)
257 {
258   const u16 _vl_msg_id = 1;
259   const u32 client_index = 3;
260   vapi_type_msg_header2_t h;
261   h._vl_msg_id = _vl_msg_id;
262   h.client_index = client_index;
263   vapi_type_msg_header2_t_ntoh (&h);
264   ck_assert_int_eq (htobe16 (h._vl_msg_id), _vl_msg_id);
265   ck_assert_int_eq (h.client_index, client_index);
266 }
267
268 END_TEST;
269
270 START_TEST (test_ntoh_3)
271 {
272   const size_t data_size = 10;
273   vapi_msg_vnet_interface_combined_counters *m =
274     malloc (sizeof (vapi_msg_vnet_interface_combined_counters) +
275             data_size * sizeof (vapi_type_vlib_counter));
276   ck_assert_ptr_ne (NULL, m);
277   vapi_payload_vnet_interface_combined_counters *p = &m->payload;
278   const u16 _vl_msg_id = 1;
279   p->_vl_msg_id = _vl_msg_id;
280   const u32 first_sw_if_index = 2;
281   p->first_sw_if_index = first_sw_if_index;
282   const size_t be_data_size = htobe32 (data_size);
283   p->count = be_data_size;
284   const u64 packets = 1234;
285   const u64 bytes = 2345;
286   int i;
287   for (i = 0; i < data_size; ++i)
288     {
289       p->data[i].packets = packets;
290       p->data[i].bytes = bytes;
291     }
292   vapi_msg_vnet_interface_combined_counters_ntoh (m);
293   ck_assert_int_eq (_vl_msg_id, be16toh (p->_vl_msg_id));
294   ck_assert_int_eq (first_sw_if_index, be32toh (p->first_sw_if_index));
295   ck_assert_int_eq (be_data_size, be32toh (p->count));
296   for (i = 0; i < data_size; ++i)
297     {
298       ck_assert_int_eq (packets, htobe64 (p->data[i].packets));
299       ck_assert_int_eq (bytes, htobe64 (p->data[i].bytes));
300     }
301   free (p);
302 }
303
304 END_TEST;
305
306 #define verify_ntoh_swap(expr, value)           \
307   if (4 == sizeof (expr))                       \
308     {                                           \
309       ck_assert_int_eq (expr, be32toh (value)); \
310     }                                           \
311   else if (2 == sizeof (expr))                  \
312     {                                           \
313       ck_assert_int_eq (expr, be16toh (value)); \
314     }                                           \
315   else                                          \
316     {                                           \
317       ck_assert_int_eq (expr, value);           \
318     }
319
320 START_TEST (test_ntoh_4)
321 {
322   const int vla_count = 3;
323   char x[sizeof (vapi_msg_bridge_domain_details) +
324          vla_count * sizeof (vapi_type_bridge_domain_sw_if)];
325   vapi_msg_bridge_domain_details *d = (void *) x;
326   int cnt = 1;
327   d->header._vl_msg_id = cnt++;
328   d->header.context = cnt++;
329   d->payload.bd_id = cnt++;
330   d->payload.flood = cnt++;
331   d->payload.uu_flood = cnt++;
332   d->payload.forward = cnt++;
333   d->payload.learn = cnt++;
334   d->payload.arp_term = cnt++;
335   d->payload.mac_age = cnt++;
336   d->payload.bvi_sw_if_index = cnt++;
337   d->payload.n_sw_ifs = htobe32 (vla_count);
338   int i;
339   for (i = 0; i < vla_count; ++i)
340     {
341       vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
342       det->context = cnt++;
343       det->sw_if_index = cnt++;
344       det->shg = cnt++;
345     }
346   vapi_msg_bridge_domain_details_ntoh (d);
347   ck_assert_int_eq (sizeof (x), vapi_calc_bridge_domain_details_msg_size (d));
348   int tmp = 1;
349   verify_ntoh_swap (d->header._vl_msg_id, tmp);
350   ++tmp;
351   ck_assert_int_eq (d->header.context, tmp);
352   ++tmp;
353   verify_ntoh_swap (d->payload.bd_id, tmp);
354   ++tmp;
355   verify_ntoh_swap (d->payload.flood, tmp);
356   ++tmp;
357   verify_ntoh_swap (d->payload.uu_flood, tmp);
358   ++tmp;
359   verify_ntoh_swap (d->payload.forward, tmp);
360   ++tmp;
361   verify_ntoh_swap (d->payload.learn, tmp);
362   ++tmp;
363   verify_ntoh_swap (d->payload.arp_term, tmp);
364   ++tmp;
365   verify_ntoh_swap (d->payload.mac_age, tmp);
366   ++tmp;
367   verify_ntoh_swap (d->payload.bvi_sw_if_index, tmp);
368   ++tmp;
369   ck_assert_int_eq (d->payload.n_sw_ifs, vla_count);
370   for (i = 0; i < vla_count; ++i)
371     {
372       vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
373       verify_ntoh_swap (det->context, tmp);
374       ++tmp;
375       verify_ntoh_swap (det->sw_if_index, tmp);
376       ++tmp;
377       verify_ntoh_swap (det->shg, tmp);
378       ++tmp;
379     }
380   vapi_msg_bridge_domain_details_hton (d);
381   tmp = 1;
382   ck_assert_int_eq (d->header._vl_msg_id, tmp);
383   ++tmp;
384   ck_assert_int_eq (d->header.context, tmp);
385   ++tmp;
386   ck_assert_int_eq (d->payload.bd_id, tmp);
387   ++tmp;
388   ck_assert_int_eq (d->payload.flood, tmp);
389   ++tmp;
390   ck_assert_int_eq (d->payload.uu_flood, tmp);
391   ++tmp;
392   ck_assert_int_eq (d->payload.forward, tmp);
393   ++tmp;
394   ck_assert_int_eq (d->payload.learn, tmp);
395   ++tmp;
396   ck_assert_int_eq (d->payload.arp_term, tmp);
397   ++tmp;
398   ck_assert_int_eq (d->payload.mac_age, tmp);
399   ++tmp;
400   ck_assert_int_eq (d->payload.bvi_sw_if_index, tmp);
401   ++tmp;
402   ck_assert_int_eq (d->payload.n_sw_ifs, htobe32 (vla_count));
403   for (i = 0; i < vla_count; ++i)
404     {
405       vapi_type_bridge_domain_sw_if *det = &d->payload.sw_if_details[i];
406       ck_assert_int_eq (det->context, tmp);
407       ++tmp;
408       ck_assert_int_eq (det->sw_if_index, tmp);
409       ++tmp;
410       ck_assert_int_eq (det->shg, tmp);
411       ++tmp;
412     }
413 }
414
415 END_TEST;
416
417 vapi_error_e
418 show_version_cb (vapi_ctx_t ctx, void *caller_ctx,
419                  vapi_error_e rv, bool is_last,
420                  vapi_payload_show_version_reply * p)
421 {
422   ck_assert_int_eq (VAPI_OK, rv);
423   ck_assert_int_eq (true, is_last);
424   ck_assert_str_eq ("vpe", (char *) p->program);
425   printf
426     ("show_version_reply: program: `%s', version: `%s', build directory: "
427      "`%s', build date: `%s'\n", p->program, p->version, p->build_directory,
428      p->build_date);
429   ++*(int *) caller_ctx;
430   return VAPI_OK;
431 }
432
433 typedef struct
434 {
435   int called;
436   int expected_retval;
437   u32 *sw_if_index_storage;
438 } test_create_loopback_ctx_t;
439
440 vapi_error_e
441 loopback_create_cb (vapi_ctx_t ctx, void *caller_ctx,
442                     vapi_error_e rv, bool is_last,
443                     vapi_payload_create_loopback_reply * p)
444 {
445   test_create_loopback_ctx_t *clc = caller_ctx;
446   ck_assert_int_eq (clc->expected_retval, p->retval);
447   *clc->sw_if_index_storage = p->sw_if_index;
448   ++clc->called;
449   return VAPI_OK;
450 }
451
452 typedef struct
453 {
454   int called;
455   int expected_retval;
456   u32 *sw_if_index_storage;
457 } test_delete_loopback_ctx_t;
458
459 vapi_error_e
460 loopback_delete_cb (vapi_ctx_t ctx, void *caller_ctx,
461                     vapi_error_e rv, bool is_last,
462                     vapi_payload_delete_loopback_reply * p)
463 {
464   test_delete_loopback_ctx_t *dlc = caller_ctx;
465   ck_assert_int_eq (dlc->expected_retval, p->retval);
466   ++dlc->called;
467   return VAPI_OK;
468 }
469
470 START_TEST (test_connect)
471 {
472   vapi_ctx_t ctx;
473   vapi_error_e rv = vapi_ctx_alloc (&ctx);
474   ck_assert_int_eq (VAPI_OK, rv);
475   rv = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
476                      response_queue_size, VAPI_MODE_BLOCKING);
477   ck_assert_int_eq (VAPI_OK, rv);
478   rv = vapi_disconnect (ctx);
479   ck_assert_int_eq (VAPI_OK, rv);
480   vapi_ctx_free (ctx);
481 }
482
483 END_TEST;
484
485 vapi_ctx_t ctx;
486
487 void
488 setup_blocking (void)
489 {
490   vapi_error_e rv = vapi_ctx_alloc (&ctx);
491   ck_assert_int_eq (VAPI_OK, rv);
492   rv = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
493                      response_queue_size, VAPI_MODE_BLOCKING);
494   ck_assert_int_eq (VAPI_OK, rv);
495 }
496
497 void
498 setup_nonblocking (void)
499 {
500   vapi_error_e rv = vapi_ctx_alloc (&ctx);
501   ck_assert_int_eq (VAPI_OK, rv);
502   rv = vapi_connect (ctx, app_name, api_prefix, max_outstanding_requests,
503                      response_queue_size, VAPI_MODE_NONBLOCKING);
504   ck_assert_int_eq (VAPI_OK, rv);
505 }
506
507 void
508 teardown (void)
509 {
510   vapi_disconnect (ctx);
511   vapi_ctx_free (ctx);
512 }
513
514 START_TEST (test_show_version_1)
515 {
516   printf ("--- Basic show version message - reply test ---\n");
517   vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
518   ck_assert_ptr_ne (NULL, sv);
519   vapi_msg_show_version_hton (sv);
520   vapi_error_e rv = vapi_send (ctx, sv);
521   ck_assert_int_eq (VAPI_OK, rv);
522   vapi_msg_show_version_reply *resp;
523   size_t size;
524   rv = vapi_recv (ctx, (void *) &resp, &size);
525   ck_assert_int_eq (VAPI_OK, rv);
526   int dummy;
527   show_version_cb (NULL, &dummy, VAPI_OK, true, &resp->payload);
528   vapi_msg_free (ctx, resp);
529 }
530
531 END_TEST;
532
533 START_TEST (test_show_version_2)
534 {
535   int called = 0;
536   printf ("--- Show version via blocking callback API ---\n");
537   const int attempts = response_queue_size * 4;
538   int i = 0;
539   for (i = 0; i < attempts; ++i)
540     {
541       vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
542       ck_assert_ptr_ne (NULL, sv);
543       vapi_error_e rv = vapi_show_version (ctx, sv, show_version_cb, &called);
544       ck_assert_int_eq (VAPI_OK, rv);
545     }
546   ck_assert_int_eq (attempts, called);
547 }
548
549 END_TEST;
550
551 typedef struct
552 {
553   bool last_called;
554   size_t num_ifs;
555   u32 *sw_if_indexes;
556   bool *seen;
557   int called;
558 } sw_interface_dump_ctx;
559
560 vapi_error_e
561 sw_interface_dump_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
562                       vapi_error_e rv, bool is_last,
563                       vapi_payload_sw_interface_details * reply)
564 {
565   sw_interface_dump_ctx *dctx = callback_ctx;
566   ck_assert_int_eq (false, dctx->last_called);
567   if (is_last)
568     {
569       ck_assert (NULL == reply);
570       dctx->last_called = true;
571     }
572   else
573     {
574       ck_assert (reply);
575       printf ("Interface dump entry: [%u]: %s\n", reply->sw_if_index,
576               reply->interface_name);
577       size_t i = 0;
578       for (i = 0; i < dctx->num_ifs; ++i)
579         {
580           if (dctx->sw_if_indexes[i] == reply->sw_if_index)
581             {
582               ck_assert_int_eq (false, dctx->seen[i]);
583               dctx->seen[i] = true;
584             }
585         }
586     }
587   ++dctx->called;
588   return VAPI_OK;
589 }
590
591 START_TEST (test_loopbacks_1)
592 {
593   printf ("--- Create/delete loopbacks using blocking API ---\n");
594   const size_t num_ifs = 5;
595   u8 mac_addresses[num_ifs][6];
596   memset (&mac_addresses, 0, sizeof (mac_addresses));
597   u32 sw_if_indexes[num_ifs];
598   memset (&sw_if_indexes, 0xff, sizeof (sw_if_indexes));
599   test_create_loopback_ctx_t clcs[num_ifs];
600   memset (&clcs, 0, sizeof (clcs));
601   test_delete_loopback_ctx_t dlcs[num_ifs];
602   memset (&dlcs, 0, sizeof (dlcs));
603   int i;
604   for (i = 0; i < num_ifs; ++i)
605     {
606       memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
607       mac_addresses[i][5] = i;
608       clcs[i].sw_if_index_storage = &sw_if_indexes[i];
609     }
610   for (i = 0; i < num_ifs; ++i)
611     {
612       vapi_msg_create_loopback *cl = vapi_alloc_create_loopback (ctx);
613       memcpy (cl->payload.mac_address, mac_addresses[i],
614               sizeof (cl->payload.mac_address));
615       vapi_error_e rv =
616         vapi_create_loopback (ctx, cl, loopback_create_cb, &clcs[i]);
617       ck_assert_int_eq (VAPI_OK, rv);
618     }
619   for (i = 0; i < num_ifs; ++i)
620     {
621       ck_assert_int_eq (1, clcs[i].called);
622       printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
623               "sw_if_index %u\n",
624               mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
625               mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
626               sw_if_indexes[i]);
627     }
628   bool seen[num_ifs];
629   sw_interface_dump_ctx dctx = { false, num_ifs, sw_if_indexes, seen, 0 };
630   vapi_msg_sw_interface_dump *dump;
631   vapi_error_e rv;
632   const int attempts = response_queue_size * 4;
633   for (i = 0; i < attempts; ++i)
634     {
635       dctx.last_called = false;
636       memset (&seen, 0, sizeof (seen));
637       dump = vapi_alloc_sw_interface_dump (ctx);
638       dump->payload.name_filter_valid = 0;
639       memset (dump->payload.name_filter, 0,
640               sizeof (dump->payload.name_filter));
641       while (VAPI_EAGAIN ==
642              (rv =
643               vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb,
644                                       &dctx)))
645         ;
646       ck_assert_int_eq (true, dctx.last_called);
647       int j = 0;
648       for (j = 0; j < num_ifs; ++j)
649         {
650           ck_assert_int_eq (true, seen[j]);
651         }
652     }
653   memset (&seen, 0, sizeof (seen));
654   for (i = 0; i < num_ifs; ++i)
655     {
656       vapi_msg_delete_loopback *dl = vapi_alloc_delete_loopback (ctx);
657       dl->payload.sw_if_index = sw_if_indexes[i];
658       vapi_error_e rv =
659         vapi_delete_loopback (ctx, dl, loopback_delete_cb, &dlcs[i]);
660       ck_assert_int_eq (VAPI_OK, rv);
661     }
662   for (i = 0; i < num_ifs; ++i)
663     {
664       ck_assert_int_eq (1, dlcs[i].called);
665       printf ("Deleted loopback with sw_if_index %u\n", sw_if_indexes[i]);
666     }
667   dctx.last_called = false;
668   memset (&seen, 0, sizeof (seen));
669   dump = vapi_alloc_sw_interface_dump (ctx);
670   dump->payload.name_filter_valid = 0;
671   memset (dump->payload.name_filter, 0, sizeof (dump->payload.name_filter));
672   while (VAPI_EAGAIN ==
673          (rv =
674           vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb, &dctx)))
675     ;
676   ck_assert_int_eq (true, dctx.last_called);
677   for (i = 0; i < num_ifs; ++i)
678     {
679       ck_assert_int_eq (false, seen[i]);
680     }
681 }
682
683 END_TEST;
684
685 START_TEST (test_show_version_3)
686 {
687   printf ("--- Show version via async callback ---\n");
688   int called = 0;
689   vapi_error_e rv;
690   vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
691   ck_assert_ptr_ne (NULL, sv);
692   while (VAPI_EAGAIN ==
693          (rv = vapi_show_version (ctx, sv, show_version_cb, &called)))
694     ;
695   ck_assert_int_eq (VAPI_OK, rv);
696   ck_assert_int_eq (0, called);
697   rv = vapi_dispatch (ctx);
698   ck_assert_int_eq (VAPI_OK, rv);
699   ck_assert_int_eq (1, called);
700   called = 0;
701   rv = vapi_dispatch (ctx);
702   ck_assert_int_eq (VAPI_OK, rv);
703   ck_assert_int_eq (0, called);
704 }
705
706 END_TEST;
707
708 START_TEST (test_show_version_4)
709 {
710   printf ("--- Show version via async callback - multiple messages ---\n");
711   vapi_error_e rv;
712   const size_t num_req = 5;
713   int contexts[num_req];
714   memset (contexts, 0, sizeof (contexts));
715   int i;
716   for (i = 0; i < num_req; ++i)
717     {
718       vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
719       ck_assert_ptr_ne (NULL, sv);
720       while (VAPI_EAGAIN ==
721              (rv =
722               vapi_show_version (ctx, sv, show_version_cb, &contexts[i])))
723         ;
724       ck_assert_int_eq (VAPI_OK, rv);
725       int j;
726       for (j = 0; j < num_req; ++j)
727         {
728           ck_assert_int_eq (0, contexts[j]);
729         }
730     }
731   rv = vapi_dispatch (ctx);
732   ck_assert_int_eq (VAPI_OK, rv);
733   for (i = 0; i < num_req; ++i)
734     {
735       ck_assert_int_eq (1, contexts[i]);
736     }
737   memset (contexts, 0, sizeof (contexts));
738   rv = vapi_dispatch (ctx);
739   ck_assert_int_eq (VAPI_OK, rv);
740   for (i = 0; i < num_req; ++i)
741     {
742       ck_assert_int_eq (0, contexts[i]);
743     }
744 }
745
746 END_TEST;
747
748 START_TEST (test_loopbacks_2)
749 {
750   printf ("--- Create/delete loopbacks using non-blocking API ---\n");
751   vapi_error_e rv;
752   const size_t num_ifs = 5;
753   u8 mac_addresses[num_ifs][6];
754   memset (&mac_addresses, 0, sizeof (mac_addresses));
755   u32 sw_if_indexes[num_ifs];
756   memset (&sw_if_indexes, 0xff, sizeof (sw_if_indexes));
757   test_create_loopback_ctx_t clcs[num_ifs];
758   memset (&clcs, 0, sizeof (clcs));
759   test_delete_loopback_ctx_t dlcs[num_ifs];
760   memset (&dlcs, 0, sizeof (dlcs));
761   int i;
762   for (i = 0; i < num_ifs; ++i)
763     {
764       memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
765       mac_addresses[i][5] = i;
766       clcs[i].sw_if_index_storage = &sw_if_indexes[i];
767     }
768   for (i = 0; i < num_ifs; ++i)
769     {
770       vapi_msg_create_loopback *cl = vapi_alloc_create_loopback (ctx);
771       memcpy (cl->payload.mac_address, mac_addresses[i],
772               sizeof (cl->payload.mac_address));
773       while (VAPI_EAGAIN ==
774              (rv =
775               vapi_create_loopback (ctx, cl, loopback_create_cb, &clcs[i])))
776         ;
777       ck_assert_int_eq (VAPI_OK, rv);
778     }
779   rv = vapi_dispatch (ctx);
780   ck_assert_int_eq (VAPI_OK, rv);
781   for (i = 0; i < num_ifs; ++i)
782     {
783       ck_assert_int_eq (1, clcs[i].called);
784       printf ("Loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
785               "sw_if_index %u\n",
786               mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
787               mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
788               sw_if_indexes[i]);
789     }
790   bool seen[num_ifs];
791   memset (&seen, 0, sizeof (seen));
792   sw_interface_dump_ctx dctx = { false, num_ifs, sw_if_indexes, seen, 0 };
793   vapi_msg_sw_interface_dump *dump = vapi_alloc_sw_interface_dump (ctx);
794   dump->payload.name_filter_valid = 0;
795   memset (dump->payload.name_filter, 0, sizeof (dump->payload.name_filter));
796   while (VAPI_EAGAIN ==
797          (rv =
798           vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb, &dctx)))
799     ;
800   for (i = 0; i < num_ifs; ++i)
801     {
802       ck_assert_int_eq (false, seen[i]);
803     }
804   memset (&seen, 0, sizeof (seen));
805   ck_assert_int_eq (false, dctx.last_called);
806   rv = vapi_dispatch (ctx);
807   ck_assert_int_eq (VAPI_OK, rv);
808   for (i = 0; i < num_ifs; ++i)
809     {
810       ck_assert_int_eq (true, seen[i]);
811     }
812   memset (&seen, 0, sizeof (seen));
813   ck_assert_int_eq (true, dctx.last_called);
814   for (i = 0; i < num_ifs; ++i)
815     {
816       vapi_msg_delete_loopback *dl = vapi_alloc_delete_loopback (ctx);
817       dl->payload.sw_if_index = sw_if_indexes[i];
818       while (VAPI_EAGAIN ==
819              (rv =
820               vapi_delete_loopback (ctx, dl, loopback_delete_cb, &dlcs[i])))
821         ;
822       ck_assert_int_eq (VAPI_OK, rv);
823     }
824   rv = vapi_dispatch (ctx);
825   ck_assert_int_eq (VAPI_OK, rv);
826   for (i = 0; i < num_ifs; ++i)
827     {
828       ck_assert_int_eq (1, dlcs[i].called);
829       printf ("Deleted loopback with sw_if_index %u\n", sw_if_indexes[i]);
830     }
831   memset (&seen, 0, sizeof (seen));
832   dctx.last_called = false;
833   dump = vapi_alloc_sw_interface_dump (ctx);
834   dump->payload.name_filter_valid = 0;
835   memset (dump->payload.name_filter, 0, sizeof (dump->payload.name_filter));
836   while (VAPI_EAGAIN ==
837          (rv =
838           vapi_sw_interface_dump (ctx, dump, sw_interface_dump_cb, &dctx)))
839     ;
840   rv = vapi_dispatch (ctx);
841   ck_assert_int_eq (VAPI_OK, rv);
842   for (i = 0; i < num_ifs; ++i)
843     {
844       ck_assert_int_eq (false, seen[i]);
845     }
846   memset (&seen, 0, sizeof (seen));
847   ck_assert_int_eq (true, dctx.last_called);
848 }
849
850 END_TEST;
851
852 vapi_error_e
853 interface_simple_stats_cb (vapi_ctx_t ctx, void *callback_ctx,
854                            vapi_error_e rv, bool is_last,
855                            vapi_payload_want_interface_simple_stats_reply *
856                            payload)
857 {
858   return VAPI_OK;
859 }
860
861 vapi_error_e
862 simple_counters_cb (vapi_ctx_t ctx, void *callback_ctx,
863                     vapi_payload_vnet_interface_simple_counters * payload)
864 {
865   int *called = callback_ctx;
866   ++*called;
867   printf ("simple counters: first_sw_if_index=%u\n",
868           payload->first_sw_if_index);
869   return VAPI_OK;
870 }
871
872 START_TEST (test_stats_1)
873 {
874   printf ("--- Receive stats using generic blocking API ---\n");
875   vapi_msg_want_interface_simple_stats *ws =
876     vapi_alloc_want_interface_simple_stats (ctx);
877   ws->payload.enable_disable = 1;
878   ws->payload.pid = getpid ();
879   vapi_error_e rv;
880   rv = vapi_want_interface_simple_stats (ctx, ws, interface_simple_stats_cb,
881                                          NULL);
882   ck_assert_int_eq (VAPI_OK, rv);
883   int called = 0;
884   vapi_set_event_cb (ctx, vapi_msg_id_vnet_interface_simple_counters,
885                      (vapi_event_cb) simple_counters_cb, &called);
886   rv = vapi_dispatch_one (ctx);
887   ck_assert_int_eq (VAPI_OK, rv);
888   ck_assert_int_eq (1, called);
889 }
890
891 END_TEST;
892
893 START_TEST (test_stats_2)
894 {
895   printf ("--- Receive stats using stat-specific blocking API ---\n");
896   vapi_msg_want_interface_simple_stats *ws =
897     vapi_alloc_want_interface_simple_stats (ctx);
898   ws->payload.enable_disable = 1;
899   ws->payload.pid = getpid ();
900   vapi_error_e rv;
901   rv = vapi_want_interface_simple_stats (ctx, ws, interface_simple_stats_cb,
902                                          NULL);
903   ck_assert_int_eq (VAPI_OK, rv);
904   int called = 0;
905   vapi_set_vapi_msg_vnet_interface_simple_counters_event_cb (ctx,
906                                                              simple_counters_cb,
907                                                              &called);
908   rv = vapi_dispatch_one (ctx);
909   ck_assert_int_eq (VAPI_OK, rv);
910   ck_assert_int_eq (1, called);
911 }
912
913 END_TEST;
914
915 vapi_error_e
916 generic_cb (vapi_ctx_t ctx, void *callback_ctx, vapi_msg_id_t id, void *msg)
917 {
918   int *called = callback_ctx;
919   ck_assert_int_eq (0, *called);
920   ++*called;
921   ck_assert_int_eq (id, vapi_msg_id_show_version_reply);
922   ck_assert_ptr_ne (NULL, msg);
923   vapi_msg_show_version_reply *reply = msg;
924   ck_assert_str_eq ("vpe", (char *) reply->payload.program);
925   return VAPI_OK;
926 }
927
928 START_TEST (test_show_version_5)
929 {
930   printf ("--- Receive show version using generic callback - nonblocking "
931           "API ---\n");
932   vapi_error_e rv;
933   vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
934   ck_assert_ptr_ne (NULL, sv);
935   vapi_msg_show_version_hton (sv);
936   while (VAPI_EAGAIN == (rv = vapi_send (ctx, sv)))
937     ;
938   ck_assert_int_eq (VAPI_OK, rv);
939   int called = 0;
940   vapi_set_generic_event_cb (ctx, generic_cb, &called);
941   ck_assert_int_eq (VAPI_OK, rv);
942   rv = vapi_dispatch_one (ctx);
943   ck_assert_int_eq (VAPI_OK, rv);
944   ck_assert_int_eq (1, called);
945   sv = vapi_alloc_show_version (ctx);
946   ck_assert_ptr_ne (NULL, sv);
947   vapi_msg_show_version_hton (sv);
948   while (VAPI_EAGAIN == (rv = vapi_send (ctx, sv)))
949     ;
950   ck_assert_int_eq (VAPI_OK, rv);
951   vapi_clear_generic_event_cb (ctx);
952   rv = vapi_dispatch_one (ctx);
953   ck_assert_int_eq (VAPI_OK, rv);
954   ck_assert_int_eq (1, called); /* needs to remain unchanged */
955 }
956
957 END_TEST;
958
959 vapi_error_e
960 combined_counters_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
961                       vapi_payload_vnet_interface_combined_counters * payload)
962 {
963   int *called = callback_ctx;
964   ++*called;
965   printf ("combined counters: first_sw_if_index=%u\n",
966           payload->first_sw_if_index);
967   return VAPI_OK;
968 }
969
970 vapi_error_e
971 stats_cb (vapi_ctx_t ctx, void *callback_ctx, vapi_error_e rv,
972           bool is_last, vapi_payload_want_stats_reply * payload)
973 {
974   return VAPI_OK;
975 }
976
977 START_TEST (test_stats_3)
978 {
979   printf ("--- Receive multiple stats using stat-specific non-blocking API "
980           "---\n");
981   vapi_msg_want_stats *ws = vapi_alloc_want_stats (ctx);
982   ws->payload.enable_disable = 1;
983   ws->payload.pid = getpid ();
984   vapi_error_e rv;
985   rv = vapi_want_stats (ctx, ws, stats_cb, NULL);
986   ck_assert_int_eq (VAPI_OK, rv);
987   int called = 0;
988   int called2 = 0;
989   vapi_set_vapi_msg_vnet_interface_simple_counters_event_cb (ctx,
990                                                              simple_counters_cb,
991                                                              &called);
992   vapi_set_vapi_msg_vnet_interface_combined_counters_event_cb (ctx,
993                                                                combined_counters_cb,
994                                                                &called2);
995   while (!called || !called2)
996     {
997       if (VAPI_EAGAIN != (rv = vapi_dispatch_one (ctx)))
998         {
999           ck_assert_int_eq (VAPI_OK, rv);
1000         }
1001     }
1002 }
1003
1004 END_TEST;
1005
1006 vapi_error_e
1007 show_version_no_cb (vapi_ctx_t ctx, void *caller_ctx,
1008                     vapi_error_e rv, bool is_last,
1009                     vapi_payload_show_version_reply * p)
1010 {
1011   ck_assert_int_eq (VAPI_ENORESP, rv);
1012   ck_assert_int_eq (true, is_last);
1013   ck_assert_ptr_eq (NULL, p);
1014   ++*(int *) caller_ctx;
1015   return VAPI_OK;
1016 }
1017
1018 START_TEST (test_no_response_1)
1019 {
1020   printf ("--- Simulate no response to regular message ---\n");
1021   vapi_error_e rv;
1022   vapi_msg_show_version *sv = vapi_alloc_show_version (ctx);
1023   ck_assert_ptr_ne (NULL, sv);
1024   sv->header._vl_msg_id = ~0;   /* malformed ID causes vpp to drop the msg */
1025   int called = 0;
1026   while (VAPI_EAGAIN ==
1027          (rv = vapi_show_version (ctx, sv, show_version_no_cb, &called)))
1028     ;
1029   ck_assert_int_eq (VAPI_OK, rv);
1030   sv = vapi_alloc_show_version (ctx);
1031   ck_assert_ptr_ne (NULL, sv);
1032   while (VAPI_EAGAIN ==
1033          (rv = vapi_show_version (ctx, sv, show_version_cb, &called)))
1034     ;
1035   ck_assert_int_eq (VAPI_OK, rv);
1036   rv = vapi_dispatch (ctx);
1037   ck_assert_int_eq (VAPI_OK, rv);
1038   ck_assert_int_eq (2, called);
1039 }
1040
1041 END_TEST;
1042
1043 vapi_error_e
1044 no_msg_cb (struct vapi_ctx_s *ctx, void *callback_ctx,
1045            vapi_error_e rv, bool is_last,
1046            vapi_payload_sw_interface_details * reply)
1047 {
1048   int *called = callback_ctx;
1049   ++*called;
1050   ck_assert_int_eq (VAPI_OK, rv);
1051   ck_assert_int_eq (true, is_last);
1052   ck_assert_ptr_eq (NULL, reply);
1053   return VAPI_OK;
1054 }
1055
1056 START_TEST (test_no_response_2)
1057 {
1058   printf ("--- Simulate no response to dump message ---\n");
1059   vapi_error_e rv;
1060   vapi_msg_sw_interface_dump *dump = vapi_alloc_sw_interface_dump (ctx);
1061   dump->header._vl_msg_id = ~0; /* malformed ID causes vpp to drop the msg */
1062   int no_called = 0;
1063   while (VAPI_EAGAIN ==
1064          (rv = vapi_sw_interface_dump (ctx, dump, no_msg_cb, &no_called)))
1065     ;
1066   ck_assert_int_eq (VAPI_OK, rv);
1067   rv = vapi_dispatch (ctx);
1068   ck_assert_int_eq (VAPI_OK, rv);
1069   ck_assert_int_eq (1, no_called);
1070 }
1071
1072 END_TEST;
1073
1074 START_TEST (test_unsupported)
1075 {
1076   printf ("--- Unsupported messages ---\n");
1077   bool available = vapi_is_msg_available (ctx, vapi_msg_id_test_fake_msg);
1078   ck_assert_int_eq (false, available);
1079 }
1080
1081 END_TEST;
1082
1083 Suite *
1084 test_suite (void)
1085 {
1086   Suite *s = suite_create ("VAPI test");
1087
1088   TCase *tc_negative = tcase_create ("Negative tests");
1089   tcase_add_test (tc_negative, test_invalid_values);
1090   suite_add_tcase (s, tc_negative);
1091
1092   TCase *tc_swap = tcase_create ("Byteswap tests");
1093   tcase_add_test (tc_swap, test_hton_1);
1094   tcase_add_test (tc_swap, test_hton_2);
1095   tcase_add_test (tc_swap, test_hton_3);
1096   tcase_add_test (tc_swap, test_hton_4);
1097   tcase_add_test (tc_swap, test_ntoh_1);
1098   tcase_add_test (tc_swap, test_ntoh_2);
1099   tcase_add_test (tc_swap, test_ntoh_3);
1100   tcase_add_test (tc_swap, test_ntoh_4);
1101   suite_add_tcase (s, tc_swap);
1102
1103   TCase *tc_connect = tcase_create ("Connect");
1104   tcase_add_test (tc_connect, test_connect);
1105   suite_add_tcase (s, tc_connect);
1106
1107   TCase *tc_block = tcase_create ("Blocking API");
1108   tcase_set_timeout (tc_block, 25);
1109   tcase_add_checked_fixture (tc_block, setup_blocking, teardown);
1110   tcase_add_test (tc_block, test_show_version_1);
1111   tcase_add_test (tc_block, test_show_version_2);
1112   tcase_add_test (tc_block, test_loopbacks_1);
1113   tcase_add_test (tc_block, test_stats_1);
1114   tcase_add_test (tc_block, test_stats_2);
1115   suite_add_tcase (s, tc_block);
1116
1117   TCase *tc_nonblock = tcase_create ("Nonblocking API");
1118   tcase_set_timeout (tc_nonblock, 25);
1119   tcase_add_checked_fixture (tc_nonblock, setup_nonblocking, teardown);
1120   tcase_add_test (tc_nonblock, test_show_version_3);
1121   tcase_add_test (tc_nonblock, test_show_version_4);
1122   tcase_add_test (tc_nonblock, test_show_version_5);
1123   tcase_add_test (tc_nonblock, test_loopbacks_2);
1124   tcase_add_test (tc_nonblock, test_stats_3);
1125   tcase_add_test (tc_nonblock, test_no_response_1);
1126   tcase_add_test (tc_nonblock, test_no_response_2);
1127   suite_add_tcase (s, tc_nonblock);
1128
1129   TCase *tc_unsupported = tcase_create ("Unsupported message");
1130   tcase_add_checked_fixture (tc_unsupported, setup_blocking, teardown);
1131   tcase_add_test (tc_unsupported, test_unsupported);
1132   suite_add_tcase (s, tc_unsupported);
1133
1134   return s;
1135 }
1136
1137 int
1138 main (int argc, char *argv[])
1139 {
1140   if (3 != argc)
1141     {
1142       printf ("Invalid argc==`%d'\n", argc);
1143       return EXIT_FAILURE;
1144     }
1145   app_name = argv[1];
1146   api_prefix = argv[2];
1147   printf ("App name: `%s', API prefix: `%s'\n", app_name, api_prefix);
1148
1149   int number_failed;
1150   Suite *s;
1151   SRunner *sr;
1152
1153   s = test_suite ();
1154   sr = srunner_create (s);
1155
1156   srunner_run_all (sr, CK_NORMAL);
1157   number_failed = srunner_ntests_failed (sr);
1158   srunner_free (sr);
1159   return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
1160 }
1161
1162 /*
1163  * fd.io coding-style-patch-verification: ON
1164  *
1165  * Local Variables:
1166  * eval: (c-set-style "gnu")
1167  * End:
1168  */