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