blob: 284b211eb8135db5b0cfbd01b9631b24b03b3929 [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);
149 auto &p = d.get_request ().get_payload ();
150 p.name_filter_valid = 0;
Jakub Grajciar053204a2019-03-18 13:17:53 +0100151 memset (p.name_filter.buf, 0, p.name_filter.length);
Klement Sekeradc15be22017-06-12 06:49:33 +0200152 auto rv = d.execute ();
153 ck_assert_int_eq (VAPI_OK, rv);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100154 WAIT_FOR_RESPONSE (d, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200155 ck_assert_int_eq (VAPI_OK, rv);
156 auto &rs = d.get_result_set ();
157 for (auto &r : rs)
158 {
159 auto &p = r.get_payload ();
160 for (int i = 0; i < num_ifs; ++i)
161 {
162 if (sw_if_indexes[i] == p.sw_if_index)
163 {
164 ck_assert_int_eq (0, seen[i]);
165 seen[i] = true;
166 }
167 }
168 }
169 for (int i = 0; i < num_ifs; ++i)
170 {
171 ck_assert_int_eq (1, seen[i]);
172 }
173 }
174
175 for (int i = 0; i < num_ifs; ++i)
176 {
177 Delete_loopback dl (con);
178 dl.get_request ().get_payload ().sw_if_index = sw_if_indexes[i];
179 auto rv = dl.execute ();
180 ck_assert_int_eq (VAPI_OK, rv);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100181 WAIT_FOR_RESPONSE (dl, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200182 ck_assert_int_eq (VAPI_OK, rv);
183 auto &response = dl.get_response ();
184 auto rp = response.get_payload ();
185 ck_assert_int_eq (0, rp.retval);
186 printf ("Deleted loopback with sw_if_index %u\n", sw_if_indexes[i]);
187 }
188
189 { // new context
190 Sw_interface_dump d (con);
191 auto &p = d.get_request ().get_payload ();
192 p.name_filter_valid = 0;
Jakub Grajciar053204a2019-03-18 13:17:53 +0100193 memset (p.name_filter.buf, 0, p.name_filter.length);
Klement Sekeradc15be22017-06-12 06:49:33 +0200194 auto rv = d.execute ();
195 ck_assert_int_eq (VAPI_OK, rv);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100196 WAIT_FOR_RESPONSE (d, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200197 ck_assert_int_eq (VAPI_OK, rv);
198 auto &rs = d.get_result_set ();
199 for (auto &r : rs)
200 {
201 auto &p = r.get_payload ();
202 for (int i = 0; i < num_ifs; ++i)
203 {
204 ck_assert_int_ne (sw_if_indexes[i], p.sw_if_index);
205 }
206 }
207 }
208}
209
210END_TEST;
211
212struct Create_loopback_cb
213{
214 Create_loopback_cb () : called{0}, sw_if_index{0} {};
215 int called;
216 u32 sw_if_index;
217 bool seen;
218 vapi_error_e operator() (Create_loopback &cl)
219 {
220 auto &r = cl.get_response ();
221 sw_if_index = r.get_payload ().sw_if_index;
222 ++called;
223 return VAPI_OK;
224 }
225};
226
227struct Delete_loopback_cb
228{
229 Delete_loopback_cb () : called{0}, sw_if_index{0} {};
230 int called;
231 u32 sw_if_index;
232 bool seen;
233 vapi_error_e operator() (Delete_loopback &dl)
234 {
235 auto &r = dl.get_response ();
236 ck_assert_int_eq (0, r.get_payload ().retval);
237 ++called;
238 return VAPI_OK;
239 }
240};
241
242template <int num_ifs> struct Sw_interface_dump_cb
243{
244 Sw_interface_dump_cb (std::array<Create_loopback_cb, num_ifs> &cbs)
245 : called{0}, cbs{cbs} {};
246 int called;
247 std::array<Create_loopback_cb, num_ifs> &cbs;
248 vapi_error_e operator() (Sw_interface_dump &d)
249 {
250 for (auto &y : cbs)
251 {
252 y.seen = false;
253 }
254 for (auto &x : d.get_result_set ())
255 {
256 auto &p = x.get_payload ();
257 for (auto &y : cbs)
258 {
259 if (p.sw_if_index == y.sw_if_index)
260 {
261 y.seen = true;
262 }
263 }
264 }
265 for (auto &y : cbs)
266 {
267 ck_assert_int_eq (true, y.seen);
268 }
269 ++called;
270 return VAPI_OK;
271 }
272};
273
274START_TEST (test_loopbacks_2)
275{
276 printf ("--- Create/delete loopbacks by getting a callback ---\n");
277 const auto num_ifs = 5;
278 u8 mac_addresses[num_ifs][6];
279 memset (&mac_addresses, 0, sizeof (mac_addresses));
280 for (int i = 0; i < num_ifs; ++i)
281 {
282 memcpy (&mac_addresses[i], "\1\2\3\4\5\6", 6);
283 mac_addresses[i][5] = i;
284 }
285 std::array<Create_loopback_cb, num_ifs> ccbs;
286 std::array<std::unique_ptr<Create_loopback>, num_ifs> clcs;
287 for (int i = 0; i < num_ifs; ++i)
288 {
289 Create_loopback *cl = new Create_loopback (con, std::ref (ccbs[i]));
290 clcs[i].reset (cl);
291 auto &p = cl->get_request ().get_payload ();
292 memcpy (p.mac_address, mac_addresses[i], sizeof (p.mac_address));
293 auto e = cl->execute ();
294 ck_assert_int_eq (VAPI_OK, e);
295 }
296 con.dispatch ();
297 for (int i = 0; i < num_ifs; ++i)
298 {
299 ck_assert_int_eq (1, ccbs[i].called);
300 printf ("Created loopback with MAC %02x:%02x:%02x:%02x:%02x:%02x --> "
301 "sw_if_index %u\n",
302 mac_addresses[i][0], mac_addresses[i][1], mac_addresses[i][2],
303 mac_addresses[i][3], mac_addresses[i][4], mac_addresses[i][5],
304 ccbs[i].sw_if_index);
305 }
306
307 Sw_interface_dump_cb<num_ifs> swdcb (ccbs);
308 Sw_interface_dump d (con, std::ref (swdcb));
309 auto &p = d.get_request ().get_payload ();
310 p.name_filter_valid = 0;
Jakub Grajciar053204a2019-03-18 13:17:53 +0100311 memset (p.name_filter.buf, 0, p.name_filter.length);
Klement Sekeradc15be22017-06-12 06:49:33 +0200312 auto rv = d.execute ();
313 ck_assert_int_eq (VAPI_OK, rv);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100314 WAIT_FOR_RESPONSE (d, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200315 ck_assert_int_eq (VAPI_OK, rv);
316 ck_assert_int_ne (0, swdcb.called);
317 std::array<Delete_loopback_cb, num_ifs> dcbs;
318 std::array<std::unique_ptr<Delete_loopback>, num_ifs> dlcs;
319 for (int i = 0; i < num_ifs; ++i)
320 {
321 Delete_loopback *dl = new Delete_loopback (con, std::ref (dcbs[i]));
322 dlcs[i].reset (dl);
323 auto &p = dl->get_request ().get_payload ();
324 p.sw_if_index = ccbs[i].sw_if_index;
325 dcbs[i].sw_if_index = ccbs[i].sw_if_index;
326 auto e = dl->execute ();
327 ck_assert_int_eq (VAPI_OK, e);
328 }
329 con.dispatch ();
330 for (auto &x : dcbs)
331 {
332 ck_assert_int_eq (true, x.called);
333 printf ("Deleted loopback with sw_if_index %u\n", x.sw_if_index);
334 }
335
336 { // new context
337 Sw_interface_dump d (con);
338 auto &p = d.get_request ().get_payload ();
339 p.name_filter_valid = 0;
Jakub Grajciar053204a2019-03-18 13:17:53 +0100340 memset (p.name_filter.buf, 0, p.name_filter.length);
Klement Sekeradc15be22017-06-12 06:49:33 +0200341 auto rv = d.execute ();
342 ck_assert_int_eq (VAPI_OK, rv);
Mohsin Kazmi3fca5672018-01-04 18:57:26 +0100343 WAIT_FOR_RESPONSE (d, rv);
Klement Sekeradc15be22017-06-12 06:49:33 +0200344 ck_assert_int_eq (VAPI_OK, rv);
345 auto &rs = d.get_result_set ();
346 for (auto &r : rs)
347 {
348 auto &p = r.get_payload ();
349 for (int i = 0; i < num_ifs; ++i)
350 {
351 ck_assert_int_ne (ccbs[i].sw_if_index, p.sw_if_index);
352 }
353 }
354 }
355}
356
357END_TEST;
358
Klement Sekeradc15be22017-06-12 06:49:33 +0200359START_TEST (test_unsupported)
360{
361 printf ("--- Unsupported messages ---\n");
362 bool thrown = false;
363 try
364 {
365 Test_fake_msg fake (con);
366 }
367 catch (const Msg_not_available_exception &)
368 {
369 thrown = true;
370 printf ("Constructing unsupported msg not possible - test pass.\n");
371 }
372 ck_assert_int_eq (true, thrown);
373 thrown = false;
374 try
375 {
376 Test_fake_dump fake (con);
377 }
378 catch (const Msg_not_available_exception &)
379 {
380 thrown = true;
381 printf ("Constructing unsupported dump not possible - test pass.\n");
382 }
383 ck_assert_int_eq (true, thrown);
384 thrown = false;
385 try
386 {
387 Event_registration<Test_fake_details> fake (con);
388 }
389 catch (const Msg_not_available_exception &)
390 {
391 thrown = true;
392 printf ("Constructing unsupported event registration not possible - "
393 "test pass.\n");
394 }
395 ck_assert_int_eq (true, thrown);
396}
397
398END_TEST;
399
400Suite *test_suite (void)
401{
402 Suite *s = suite_create ("VAPI test");
403
404 TCase *tc_cpp_api = tcase_create ("C++ API");
405 tcase_set_timeout (tc_cpp_api, 25);
406 tcase_add_checked_fixture (tc_cpp_api, setup, teardown);
407 tcase_add_test (tc_cpp_api, test_show_version_1);
408 tcase_add_test (tc_cpp_api, test_show_version_2);
409 tcase_add_test (tc_cpp_api, test_loopbacks_1);
410 tcase_add_test (tc_cpp_api, test_loopbacks_2);
Klement Sekeradc15be22017-06-12 06:49:33 +0200411 tcase_add_test (tc_cpp_api, test_unsupported);
412 suite_add_tcase (s, tc_cpp_api);
413
414 return s;
415}
416
417int main (int argc, char *argv[])
418{
419 if (3 != argc)
420 {
421 printf ("Invalid argc==`%d'\n", argc);
422 return EXIT_FAILURE;
423 }
424 app_name = argv[1];
425 api_prefix = argv[2];
426 printf ("App name: `%s', API prefix: `%s'\n", app_name, api_prefix);
427
428 int number_failed;
429 Suite *s;
430 SRunner *sr;
431
432 s = test_suite ();
433 sr = srunner_create (s);
434
435 srunner_run_all (sr, CK_NORMAL);
436 number_failed = srunner_ntests_failed (sr);
437 srunner_free (sr);
438 return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
439}
440
441/*
442 * fd.io coding-style-patch-verification: ON
443 *
444 * Local Variables:
445 * eval: (c-set-style "gnu")
446 * End:
447 */