56ebb39cb580f206a5f7bb5092b01f36e385dc19
[vpp.git] / src / vpp-api / vapi / vapi_cpp_test.cpp
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 <memory>
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <assert.h>
22 #include <setjmp.h>
23 #include <check.h>
24 #include <vapi/memclnt.api.vapi.h>
25 #include <vapi/vapi.hpp>
26 #include <vapi/vpe.api.vapi.hpp>
27 #include <vapi/interface.api.vapi.hpp>
28 #include <vapi/mss_clamp.api.vapi.hpp>
29 #include <fake.api.vapi.hpp>
30
31 DEFINE_VAPI_MSG_IDS_VPE_API_JSON;
32 DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON;
33 DEFINE_VAPI_MSG_IDS_MSS_CLAMP_API_JSON;
34 DEFINE_VAPI_MSG_IDS_FAKE_API_JSON;
35
36 static char *app_name = nullptr;
37 static char *api_prefix = nullptr;
38 static const int max_outstanding_requests = 32;
39 static const int response_queue_size = 32;
40
41 #define WAIT_FOR_RESPONSE(param, ret)      \
42   do                                       \
43     {                                      \
44       ret = con.wait_for_response (param); \
45     }                                      \
46   while (ret == VAPI_EAGAIN)
47
48 using namespace vapi;
49
50 void verify_show_version_reply (const Show_version_reply &r)
51 {
52   auto &p = r.get_payload ();
53   printf ("show_version_reply: program: `%s', version: `%s', build directory: "
54           "`%s', build date: `%s'\n",
55           p.program, p.version, p.build_directory, p.build_date);
56   ck_assert_str_eq ("vpe", (char *)p.program);
57 }
58
59 Connection con;
60
61 void setup (void)
62 {
63   vapi_error_e rv = con.connect (
64       app_name, api_prefix, max_outstanding_requests, response_queue_size);
65   ck_assert_int_eq (VAPI_OK, rv);
66 }
67
68 void teardown (void)
69 {
70   con.disconnect ();
71 }
72
73 START_TEST (test_show_version_1)
74 {
75   printf ("--- Show version by reading response associated to request ---\n");
76   Show_version sv (con);
77   vapi_error_e rv = sv.execute ();
78   ck_assert_int_eq (VAPI_OK, rv);
79   WAIT_FOR_RESPONSE (sv, rv);
80   ck_assert_int_eq (VAPI_OK, rv);
81   auto &r = sv.get_response ();
82   verify_show_version_reply (r);
83 }
84
85 END_TEST;
86
87 struct Show_version_cb
88 {
89   Show_version_cb () : called{0} {};
90   int called;
91   vapi_error_e operator() (Show_version &sv)
92   {
93     auto &r = sv.get_response ();
94     verify_show_version_reply (r);
95     ++called;
96     return VAPI_OK;
97   }
98 };
99
100 START_TEST (test_show_version_2)
101 {
102   printf ("--- Show version by getting a callback ---\n");
103   Show_version_cb cb;
104   Show_version sv (con, std::ref (cb));
105   vapi_error_e rv = sv.execute ();
106   ck_assert_int_eq (VAPI_OK, rv);
107   con.dispatch (sv);
108   ck_assert_int_eq (1, cb.called);
109 }
110
111 END_TEST;
112
113 START_TEST (test_loopbacks_1)
114 {
115   printf ("--- Create/delete loopbacks by waiting for response ---\n");
116   const auto num_ifs = 5;
117   u8 mac_addresses[num_ifs][6];
118   memset (&mac_addresses, 0, sizeof (mac_addresses));
119   u32 sw_if_indexes[num_ifs];
120   memset (&sw_if_indexes, 0xff, sizeof (sw_if_indexes));
121   for (int i = 0; i < num_ifs; ++i)
122     {
123       memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
124       mac_addresses[i][5] = i;
125     }
126   for (int i = 0; i < num_ifs; ++i)
127     {
128       Create_loopback cl (con);
129       auto &p = cl.get_request ().get_payload ();
130       memcpy (p.mac_address, mac_addresses[i], sizeof (p.mac_address));
131       auto e = cl.execute ();
132       ck_assert_int_eq (VAPI_OK, e);
133       vapi_error_e rv;
134       WAIT_FOR_RESPONSE (cl, rv);
135       ck_assert_int_eq (VAPI_OK, rv);
136       auto &rp = cl.get_response ().get_payload ();
137       ck_assert_int_eq (0, rp.retval);
138       sw_if_indexes[i] = rp.sw_if_index;
139     }
140   for (int i = 0; i < num_ifs; ++i)
141     {
142       printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
143               "sw_if_index %u\n",
144               mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
145               mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
146               sw_if_indexes[i]);
147     }
148
149   { // new context
150     for (int i = 0; i < num_ifs; ++i)
151       {
152         Mss_clamp_enable_disable d (con);
153         auto &req = d.get_request ().get_payload ();
154         req.sw_if_index = sw_if_indexes[i];
155         req.ipv4_mss = 1420;
156         req.ipv4_direction = vapi_enum_mss_clamp_dir::MSS_CLAMP_DIR_RX;
157         auto rv = d.execute ();
158         ck_assert_int_eq (VAPI_OK, rv);
159         WAIT_FOR_RESPONSE (d, rv);
160         ck_assert_int_eq (VAPI_OK, rv);
161       }
162   }
163
164   { // new context
165     bool seen[num_ifs] = { 0 };
166     Mss_clamp_get d (con);
167     d.get_request ().get_payload ().sw_if_index = ~0;
168     auto rv = d.execute ();
169     ck_assert_int_eq (VAPI_OK, rv);
170     WAIT_FOR_RESPONSE (d, rv);
171     ck_assert_int_eq (VAPI_OK, rv);
172     auto &rs = d.get_result_set ();
173     for (auto &r : rs)
174       {
175         auto &p = r.get_payload ();
176         ck_assert_int_eq (p.ipv4_mss, 1420);
177         printf ("tcp-clamp: sw_if_idx %u ip4-mss %d dir %d\n", p.sw_if_index,
178                 p.ipv4_mss, p.ipv4_direction);
179         for (int i = 0; i < num_ifs; ++i)
180           {
181             if (sw_if_indexes[i] == p.sw_if_index)
182               {
183                 ck_assert_int_eq (0, seen[i]);
184                 seen[i] = true;
185               }
186           }
187       }
188     for (int i = 0; i < num_ifs; ++i)
189       {
190         ck_assert_int_eq (1, seen[i]);
191       }
192   }
193
194   { // new context
195     bool seen[num_ifs] = {0};
196     Sw_interface_dump d (con, 0);
197     auto rv = d.execute ();
198     ck_assert_int_eq (VAPI_OK, rv);
199     WAIT_FOR_RESPONSE (d, rv);
200     ck_assert_int_eq (VAPI_OK, rv);
201     auto &rs = d.get_result_set ();
202     for (auto &r : rs)
203       {
204         auto &p = r.get_payload ();
205         for (int i = 0; i < num_ifs; ++i)
206           {
207             if (sw_if_indexes[i] == p.sw_if_index)
208               {
209                 ck_assert_int_eq (0, seen[i]);
210                 seen[i] = true;
211               }
212           }
213       }
214     for (int i = 0; i < num_ifs; ++i)
215       {
216         ck_assert_int_eq (1, seen[i]);
217       }
218   }
219
220   for (int i = 0; i < num_ifs; ++i)
221     {
222       Delete_loopback dl (con);
223       dl.get_request ().get_payload ().sw_if_index = sw_if_indexes[i];
224       auto rv = dl.execute ();
225       ck_assert_int_eq (VAPI_OK, rv);
226       WAIT_FOR_RESPONSE (dl, rv);
227       ck_assert_int_eq (VAPI_OK, rv);
228       auto &response = dl.get_response ();
229       auto rp = response.get_payload ();
230       ck_assert_int_eq (0, rp.retval);
231       printf ("Deleted loopback with sw_if_index %u\n", sw_if_indexes[i]);
232     }
233
234   { // new context
235     Sw_interface_dump d (con, 0);
236     auto rv = d.execute ();
237     ck_assert_int_eq (VAPI_OK, rv);
238     WAIT_FOR_RESPONSE (d, rv);
239     ck_assert_int_eq (VAPI_OK, rv);
240     auto &rs = d.get_result_set ();
241     for (auto &r : rs)
242       {
243         auto &p = r.get_payload ();
244         for (int i = 0; i < num_ifs; ++i)
245           {
246             ck_assert_int_ne (sw_if_indexes[i], p.sw_if_index);
247           }
248       }
249   }
250 }
251
252 END_TEST;
253
254 struct Create_loopback_cb
255 {
256   Create_loopback_cb () : called{ 0 }, sw_if_index{ 0 }, seen{ false } {};
257   int called;
258   u32 sw_if_index;
259   bool seen;
260   vapi_error_e operator() (Create_loopback &cl)
261   {
262     auto &r = cl.get_response ();
263     sw_if_index = r.get_payload ().sw_if_index;
264     ++called;
265     return VAPI_OK;
266   }
267 };
268
269 struct Delete_loopback_cb
270 {
271   Delete_loopback_cb () : called{ 0 }, sw_if_index{ 0 }, seen{ false } {};
272   int called;
273   u32 sw_if_index;
274   bool seen;
275   vapi_error_e operator() (Delete_loopback &dl)
276   {
277     auto &r = dl.get_response ();
278     ck_assert_int_eq (0, r.get_payload ().retval);
279     ++called;
280     return VAPI_OK;
281   }
282 };
283
284 template <int num_ifs> struct Sw_interface_dump_cb
285 {
286   Sw_interface_dump_cb (std::array<Create_loopback_cb, num_ifs> &cbs)
287       : called{0}, cbs{cbs} {};
288   int called;
289   std::array<Create_loopback_cb, num_ifs> &cbs;
290   vapi_error_e operator() (Sw_interface_dump &d)
291   {
292     for (auto &y : cbs)
293       {
294         y.seen = false;
295       }
296     for (auto &x : d.get_result_set ())
297       {
298         auto &p = x.get_payload ();
299         for (auto &y : cbs)
300           {
301             if (p.sw_if_index == y.sw_if_index)
302               {
303                 y.seen = true;
304               }
305           }
306       }
307     for (auto &y : cbs)
308       {
309         ck_assert_int_eq (true, y.seen);
310       }
311     ++called;
312     return VAPI_OK;
313   }
314 };
315
316 START_TEST (test_loopbacks_2)
317 {
318   printf ("--- Create/delete loopbacks by getting a callback ---\n");
319   const auto num_ifs = 5;
320   u8 mac_addresses[num_ifs][6];
321   memset (&mac_addresses, 0, sizeof (mac_addresses));
322   for (int i = 0; i < num_ifs; ++i)
323     {
324       memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
325       mac_addresses[i][5] = i;
326     }
327   std::array<Create_loopback_cb, num_ifs> ccbs;
328   std::array<std::unique_ptr<Create_loopback>, num_ifs> clcs;
329   for (int i = 0; i < num_ifs; ++i)
330     {
331       Create_loopback *cl = new Create_loopback (con, std::ref (ccbs[i]));
332       clcs[i].reset (cl);
333       auto &p = cl->get_request ().get_payload ();
334       memcpy (p.mac_address, mac_addresses[i], sizeof (p.mac_address));
335       auto e = cl->execute ();
336       ck_assert_int_eq (VAPI_OK, e);
337     }
338   con.dispatch ();
339   for (int i = 0; i < num_ifs; ++i)
340     {
341       ck_assert_int_eq (1, ccbs[i].called);
342       printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
343               "sw_if_index %u\n",
344               mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
345               mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
346               ccbs[i].sw_if_index);
347     }
348
349   Sw_interface_dump_cb<num_ifs> swdcb (ccbs);
350   Sw_interface_dump d (con, 0, std::ref (swdcb));
351   auto rv = d.execute ();
352   ck_assert_int_eq (VAPI_OK, rv);
353   WAIT_FOR_RESPONSE (d, rv);
354   ck_assert_int_eq (VAPI_OK, rv);
355   ck_assert_int_ne (0, swdcb.called);
356   std::array<Delete_loopback_cb, num_ifs> dcbs;
357   std::array<std::unique_ptr<Delete_loopback>, num_ifs> dlcs;
358   for (int i = 0; i < num_ifs; ++i)
359     {
360       Delete_loopback *dl = new Delete_loopback (con, std::ref (dcbs[i]));
361       dlcs[i].reset (dl);
362       auto &p = dl->get_request ().get_payload ();
363       p.sw_if_index = ccbs[i].sw_if_index;
364       dcbs[i].sw_if_index = ccbs[i].sw_if_index;
365       auto e = dl->execute ();
366       ck_assert_int_eq (VAPI_OK, e);
367     }
368   con.dispatch ();
369   for (auto &x : dcbs)
370     {
371       ck_assert_int_eq (true, x.called);
372       printf ("Deleted loopback with sw_if_index %u\n", x.sw_if_index);
373     }
374
375   { // new context
376     Sw_interface_dump d (con, 0);
377     auto rv = d.execute ();
378     ck_assert_int_eq (VAPI_OK, rv);
379     WAIT_FOR_RESPONSE (d, rv);
380     ck_assert_int_eq (VAPI_OK, rv);
381     auto &rs = d.get_result_set ();
382     for (auto &r : rs)
383       {
384         auto &p = r.get_payload ();
385         for (int i = 0; i < num_ifs; ++i)
386           {
387             ck_assert_int_ne (ccbs[i].sw_if_index, p.sw_if_index);
388           }
389       }
390   }
391 }
392
393 END_TEST;
394
395 START_TEST (test_unsupported)
396 {
397   printf ("--- Unsupported messages ---\n");
398   bool thrown = false;
399   try
400     {
401       Test_fake_msg fake (con);
402     }
403   catch (const Msg_not_available_exception &)
404     {
405       thrown = true;
406       printf ("Constructing unsupported msg not possible - test pass.\n");
407     }
408   ck_assert_int_eq (true, thrown);
409   thrown = false;
410   try
411     {
412       Test_fake_dump fake (con);
413     }
414   catch (const Msg_not_available_exception &)
415     {
416       thrown = true;
417       printf ("Constructing unsupported dump not possible - test pass.\n");
418     }
419   ck_assert_int_eq (true, thrown);
420   thrown = false;
421   try
422     {
423       Event_registration<Test_fake_details> fake (con);
424     }
425   catch (const Msg_not_available_exception &)
426     {
427       thrown = true;
428       printf ("Constructing unsupported event registration not possible - "
429               "test pass.\n");
430     }
431   ck_assert_int_eq (true, thrown);
432 }
433
434 END_TEST;
435
436 Suite *test_suite (void)
437 {
438   Suite *s = suite_create ("VAPI test");
439
440   TCase *tc_cpp_api = tcase_create ("C++ API");
441   tcase_set_timeout (tc_cpp_api, 25);
442   tcase_add_checked_fixture (tc_cpp_api, setup, teardown);
443   tcase_add_test (tc_cpp_api, test_show_version_1);
444   tcase_add_test (tc_cpp_api, test_show_version_2);
445   tcase_add_test (tc_cpp_api, test_loopbacks_1);
446   tcase_add_test (tc_cpp_api, test_loopbacks_2);
447   tcase_add_test (tc_cpp_api, test_unsupported);
448   suite_add_tcase (s, tc_cpp_api);
449
450   return s;
451 }
452
453 int main (int argc, char *argv[])
454 {
455   if (3 != argc)
456     {
457       printf ("Invalid argc==`%d'\n", argc);
458       return EXIT_FAILURE;
459     }
460   app_name = argv[1];
461   api_prefix = argv[2];
462   printf ("App name: `%s', API prefix: `%s'\n", app_name, api_prefix);
463
464   int number_failed;
465   Suite *s;
466   SRunner *sr;
467
468   s = test_suite ();
469   sr = srunner_create (s);
470
471   srunner_run_all (sr, CK_NORMAL);
472   number_failed = srunner_ntests_failed (sr);
473   srunner_free (sr);
474   return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
475 }
476
477 /*
478  * fd.io coding-style-patch-verification: ON
479  *
480  * Local Variables:
481  * eval: (c-set-style "gnu")
482  * End:
483  */