blob: c0e0cdc3ab80375a3594f557b6be25bf641c8f7c [file] [log] [blame]
Klement Sekeradc15be22017-06-12 06:49:33 +02001/*
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>
Filip Tehlarf0e67d72021-07-23 22:03:05 +000024#include <vapi/memclnt.api.vapi.h>
Klement Sekeradc15be22017-06-12 06:49:33 +020025#include <vapi/vapi.hpp>
26#include <vapi/vpe.api.vapi.hpp>
27#include <vapi/interface.api.vapi.hpp>
Klement Sekeradc15be22017-06-12 06:49:33 +020028#include <fake.api.vapi.hpp>
29
30DEFINE_VAPI_MSG_IDS_VPE_API_JSON;
31DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON;
Klement Sekeradc15be22017-06-12 06:49:33 +020032DEFINE_VAPI_MSG_IDS_FAKE_API_JSON;
33
34static char *app_name = nullptr;
35static char *api_prefix = nullptr;
36static const int max_outstanding_requests = 32;
37static const int response_queue_size = 32;
38
Mohsin Kazmi3fca5672018-01-04 18:57:26 +010039#define WAIT_FOR_RESPONSE(param, ret) \
40 do \
41 { \
42 ret = con.wait_for_response (param); \
43 } \
44 while (ret == VAPI_EAGAIN)
45
Klement Sekeradc15be22017-06-12 06:49:33 +020046using namespace vapi;
47
48void verify_show_version_reply (const Show_version_reply &r)
49{
50 auto &p = r.get_payload ();
51 printf ("show_version_reply: program: `%s', version: `%s', build directory: "
52 "`%s', build date: `%s'\n",
Ole Troane5ff5a32019-08-23 22:55:18 +020053 p.program, p.version, p.build_directory, p.build_date);
54 ck_assert_str_eq ("vpe", (char *)p.program);
Klement Sekeradc15be22017-06-12 06:49:33 +020055}
56
57Connection con;
58
59void setup (void)
60{
61 vapi_error_e rv = con.connect (
62 app_name, api_prefix, max_outstanding_requests, response_queue_size);
63 ck_assert_int_eq (VAPI_OK, rv);
64}
65
66void teardown (void)
67{
68 con.disconnect ();
69}
70
71START_TEST (test_show_version_1)
72{
73 printf ("--- Show version by reading response associated to request ---\n");
74 Show_version sv (con);
75 vapi_error_e rv = sv.execute ();
76 ck_assert_int_eq (VAPI_OK, rv);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +010077 WAIT_FOR_RESPONSE (sv, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +020078 ck_assert_int_eq (VAPI_OK, rv);
79 auto &r = sv.get_response ();
80 verify_show_version_reply (r);
81}
82
83END_TEST;
84
85struct Show_version_cb
86{
87 Show_version_cb () : called{0} {};
88 int called;
89 vapi_error_e operator() (Show_version &sv)
90 {
91 auto &r = sv.get_response ();
92 verify_show_version_reply (r);
93 ++called;
94 return VAPI_OK;
95 }
96};
97
98START_TEST (test_show_version_2)
99{
100 printf ("--- Show version by getting a callback ---\n");
101 Show_version_cb cb;
102 Show_version sv (con, std::ref (cb));
103 vapi_error_e rv = sv.execute ();
104 ck_assert_int_eq (VAPI_OK, rv);
105 con.dispatch (sv);
106 ck_assert_int_eq (1, cb.called);
107}
108
109END_TEST;
110
111START_TEST (test_loopbacks_1)
112{
113 printf ("--- Create/delete loopbacks by waiting for response ---\n");
114 const auto num_ifs = 5;
115 u8 mac_addresses[num_ifs][6];
116 memset (&mac_addresses, 0, sizeof (mac_addresses));
117 u32 sw_if_indexes[num_ifs];
118 memset (&sw_if_indexes, 0xff, sizeof (sw_if_indexes));
119 for (int i = 0; i < num_ifs; ++i)
120 {
121 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
122 mac_addresses[i][5] = i;
123 }
124 for (int i = 0; i < num_ifs; ++i)
125 {
126 Create_loopback cl (con);
127 auto &p = cl.get_request ().get_payload ();
128 memcpy (p.mac_address, mac_addresses[i], sizeof (p.mac_address));
129 auto e = cl.execute ();
130 ck_assert_int_eq (VAPI_OK, e);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100131 vapi_error_e rv;
132 WAIT_FOR_RESPONSE (cl, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200133 ck_assert_int_eq (VAPI_OK, rv);
134 auto &rp = cl.get_response ().get_payload ();
135 ck_assert_int_eq (0, rp.retval);
136 sw_if_indexes[i] = rp.sw_if_index;
137 }
138 for (int i = 0; i < num_ifs; ++i)
139 {
140 printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
141 "sw_if_index %u\n",
142 mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
143 mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
144 sw_if_indexes[i]);
145 }
146
147 { // new context
148 bool seen[num_ifs] = {0};
149 Sw_interface_dump d (con);
Klement Sekeradc15be22017-06-12 06:49:33 +0200150 auto rv = d.execute ();
151 ck_assert_int_eq (VAPI_OK, rv);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100152 WAIT_FOR_RESPONSE (d, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200153 ck_assert_int_eq (VAPI_OK, rv);
154 auto &rs = d.get_result_set ();
155 for (auto &r : rs)
156 {
157 auto &p = r.get_payload ();
158 for (int i = 0; i < num_ifs; ++i)
159 {
160 if (sw_if_indexes[i] == p.sw_if_index)
161 {
162 ck_assert_int_eq (0, seen[i]);
163 seen[i] = true;
164 }
165 }
166 }
167 for (int i = 0; i < num_ifs; ++i)
168 {
169 ck_assert_int_eq (1, seen[i]);
170 }
171 }
172
173 for (int i = 0; i < num_ifs; ++i)
174 {
175 Delete_loopback dl (con);
176 dl.get_request ().get_payload ().sw_if_index = sw_if_indexes[i];
177 auto rv = dl.execute ();
178 ck_assert_int_eq (VAPI_OK, rv);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100179 WAIT_FOR_RESPONSE (dl, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200180 ck_assert_int_eq (VAPI_OK, rv);
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]);
185 }
186
187 { // new context
188 Sw_interface_dump d (con);
Klement Sekeradc15be22017-06-12 06:49:33 +0200189 auto rv = d.execute ();
190 ck_assert_int_eq (VAPI_OK, rv);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100191 WAIT_FOR_RESPONSE (d, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200192 ck_assert_int_eq (VAPI_OK, rv);
193 auto &rs = d.get_result_set ();
194 for (auto &r : rs)
195 {
196 auto &p = r.get_payload ();
197 for (int i = 0; i < num_ifs; ++i)
198 {
199 ck_assert_int_ne (sw_if_indexes[i], p.sw_if_index);
200 }
201 }
202 }
203}
204
205END_TEST;
206
207struct Create_loopback_cb
208{
Klement Sekeraa0f55c92021-10-13 21:45:42 +0200209 Create_loopback_cb () : called{ 0 }, sw_if_index{ 0 }, seen{ false } {};
Klement Sekeradc15be22017-06-12 06:49:33 +0200210 int called;
211 u32 sw_if_index;
212 bool seen;
213 vapi_error_e operator() (Create_loopback &cl)
214 {
215 auto &r = cl.get_response ();
216 sw_if_index = r.get_payload ().sw_if_index;
217 ++called;
218 return VAPI_OK;
219 }
220};
221
222struct Delete_loopback_cb
223{
Klement Sekeraa0f55c92021-10-13 21:45:42 +0200224 Delete_loopback_cb () : called{ 0 }, sw_if_index{ 0 }, seen{ false } {};
Klement Sekeradc15be22017-06-12 06:49:33 +0200225 int called;
226 u32 sw_if_index;
227 bool seen;
228 vapi_error_e operator() (Delete_loopback &dl)
229 {
230 auto &r = dl.get_response ();
231 ck_assert_int_eq (0, r.get_payload ().retval);
232 ++called;
233 return VAPI_OK;
234 }
235};
236
237template <int num_ifs> struct Sw_interface_dump_cb
238{
239 Sw_interface_dump_cb (std::array<Create_loopback_cb, num_ifs> &cbs)
240 : called{0}, cbs{cbs} {};
241 int called;
242 std::array<Create_loopback_cb, num_ifs> &cbs;
243 vapi_error_e operator() (Sw_interface_dump &d)
244 {
245 for (auto &y : cbs)
246 {
247 y.seen = false;
248 }
249 for (auto &x : d.get_result_set ())
250 {
251 auto &p = x.get_payload ();
252 for (auto &y : cbs)
253 {
254 if (p.sw_if_index == y.sw_if_index)
255 {
256 y.seen = true;
257 }
258 }
259 }
260 for (auto &y : cbs)
261 {
262 ck_assert_int_eq (true, y.seen);
263 }
264 ++called;
265 return VAPI_OK;
266 }
267};
268
269START_TEST (test_loopbacks_2)
270{
271 printf ("--- Create/delete loopbacks by getting a callback ---\n");
272 const auto num_ifs = 5;
273 u8 mac_addresses[num_ifs][6];
274 memset (&mac_addresses, 0, sizeof (mac_addresses));
275 for (int i = 0; i < num_ifs; ++i)
276 {
277 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
278 mac_addresses[i][5] = i;
279 }
280 std::array<Create_loopback_cb, num_ifs> ccbs;
281 std::array<std::unique_ptr<Create_loopback>, num_ifs> clcs;
282 for (int i = 0; i < num_ifs; ++i)
283 {
284 Create_loopback *cl = new Create_loopback (con, std::ref (ccbs[i]));
285 clcs[i].reset (cl);
286 auto &p = cl->get_request ().get_payload ();
287 memcpy (p.mac_address, mac_addresses[i], sizeof (p.mac_address));
288 auto e = cl->execute ();
289 ck_assert_int_eq (VAPI_OK, e);
290 }
291 con.dispatch ();
292 for (int i = 0; i < num_ifs; ++i)
293 {
294 ck_assert_int_eq (1, ccbs[i].called);
295 printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
296 "sw_if_index %u\n",
297 mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
298 mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
299 ccbs[i].sw_if_index);
300 }
301
302 Sw_interface_dump_cb<num_ifs> swdcb (ccbs);
303 Sw_interface_dump d (con, std::ref (swdcb));
Klement Sekeradc15be22017-06-12 06:49:33 +0200304 auto rv = d.execute ();
305 ck_assert_int_eq (VAPI_OK, rv);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100306 WAIT_FOR_RESPONSE (d, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200307 ck_assert_int_eq (VAPI_OK, rv);
308 ck_assert_int_ne (0, swdcb.called);
309 std::array<Delete_loopback_cb, num_ifs> dcbs;
310 std::array<std::unique_ptr<Delete_loopback>, num_ifs> dlcs;
311 for (int i = 0; i < num_ifs; ++i)
312 {
313 Delete_loopback *dl = new Delete_loopback (con, std::ref (dcbs[i]));
314 dlcs[i].reset (dl);
315 auto &p = dl->get_request ().get_payload ();
316 p.sw_if_index = ccbs[i].sw_if_index;
317 dcbs[i].sw_if_index = ccbs[i].sw_if_index;
318 auto e = dl->execute ();
319 ck_assert_int_eq (VAPI_OK, e);
320 }
321 con.dispatch ();
322 for (auto &x : dcbs)
323 {
324 ck_assert_int_eq (true, x.called);
325 printf ("Deleted loopback with sw_if_index %u\n", x.sw_if_index);
326 }
327
328 { // new context
329 Sw_interface_dump d (con);
Klement Sekeradc15be22017-06-12 06:49:33 +0200330 auto rv = d.execute ();
331 ck_assert_int_eq (VAPI_OK, rv);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100332 WAIT_FOR_RESPONSE (d, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200333 ck_assert_int_eq (VAPI_OK, rv);
334 auto &rs = d.get_result_set ();
335 for (auto &r : rs)
336 {
337 auto &p = r.get_payload ();
338 for (int i = 0; i < num_ifs; ++i)
339 {
340 ck_assert_int_ne (ccbs[i].sw_if_index, p.sw_if_index);
341 }
342 }
343 }
344}
345
346END_TEST;
347
Klement Sekeradc15be22017-06-12 06:49:33 +0200348START_TEST (test_unsupported)
349{
350 printf ("--- Unsupported messages ---\n");
351 bool thrown = false;
352 try
353 {
354 Test_fake_msg fake (con);
355 }
356 catch (const Msg_not_available_exception &)
357 {
358 thrown = true;
359 printf ("Constructing unsupported msg not possible - test pass.\n");
360 }
361 ck_assert_int_eq (true, thrown);
362 thrown = false;
363 try
364 {
365 Test_fake_dump fake (con);
366 }
367 catch (const Msg_not_available_exception &)
368 {
369 thrown = true;
370 printf ("Constructing unsupported dump not possible - test pass.\n");
371 }
372 ck_assert_int_eq (true, thrown);
373 thrown = false;
374 try
375 {
376 Event_registration<Test_fake_details> fake (con);
377 }
378 catch (const Msg_not_available_exception &)
379 {
380 thrown = true;
381 printf ("Constructing unsupported event registration not possible - "
382 "test pass.\n");
383 }
384 ck_assert_int_eq (true, thrown);
385}
386
387END_TEST;
388
389Suite *test_suite (void)
390{
391 Suite *s = suite_create ("VAPI test");
392
393 TCase *tc_cpp_api = tcase_create ("C++ API");
394 tcase_set_timeout (tc_cpp_api, 25);
395 tcase_add_checked_fixture (tc_cpp_api, setup, teardown);
396 tcase_add_test (tc_cpp_api, test_show_version_1);
397 tcase_add_test (tc_cpp_api, test_show_version_2);
398 tcase_add_test (tc_cpp_api, test_loopbacks_1);
399 tcase_add_test (tc_cpp_api, test_loopbacks_2);
Klement Sekeradc15be22017-06-12 06:49:33 +0200400 tcase_add_test (tc_cpp_api, test_unsupported);
401 suite_add_tcase (s, tc_cpp_api);
402
403 return s;
404}
405
406int main (int argc, char *argv[])
407{
408 if (3 != argc)
409 {
410 printf ("Invalid argc==`%d'\n", argc);
411 return EXIT_FAILURE;
412 }
413 app_name = argv[1];
414 api_prefix = argv[2];
415 printf ("App name: `%s', API prefix: `%s'\n", app_name, api_prefix);
416
417 int number_failed;
418 Suite *s;
419 SRunner *sr;
420
421 s = test_suite ();
422 sr = srunner_create (s);
423
424 srunner_run_all (sr, CK_NORMAL);
425 number_failed = srunner_ntests_failed (sr);
426 srunner_free (sr);
427 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
428}
429
430/*
431 * fd.io coding-style-patch-verification: ON
432 *
433 * Local Variables:
434 * eval: (c-set-style "gnu")
435 * End:
436 */