Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 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/vapi.hpp> |
| 25 | #include <vapi/vpe.api.vapi.hpp> |
| 26 | #include <vapi/interface.api.vapi.hpp> |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 27 | #include <fake.api.vapi.hpp> |
| 28 | |
| 29 | DEFINE_VAPI_MSG_IDS_VPE_API_JSON; |
| 30 | DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON; |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 31 | DEFINE_VAPI_MSG_IDS_FAKE_API_JSON; |
| 32 | |
| 33 | static char *app_name = nullptr; |
| 34 | static char *api_prefix = nullptr; |
| 35 | static const int max_outstanding_requests = 32; |
| 36 | static const int response_queue_size = 32; |
| 37 | |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 38 | #define WAIT_FOR_RESPONSE(param, ret) \ |
| 39 | do \ |
| 40 | { \ |
| 41 | ret = con.wait_for_response (param); \ |
| 42 | } \ |
| 43 | while (ret == VAPI_EAGAIN) |
| 44 | |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 45 | using namespace vapi; |
| 46 | |
| 47 | void 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 Troan | e5ff5a3 | 2019-08-23 22:55:18 +0200 | [diff] [blame] | 52 | p.program, p.version, p.build_directory, p.build_date); |
| 53 | ck_assert_str_eq ("vpe", (char *)p.program); |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | Connection con; |
| 57 | |
| 58 | void 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 | |
| 65 | void teardown (void) |
| 66 | { |
| 67 | con.disconnect (); |
| 68 | } |
| 69 | |
| 70 | START_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 Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 76 | WAIT_FOR_RESPONSE (sv, rv); |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 77 | ck_assert_int_eq (VAPI_OK, rv); |
| 78 | auto &r = sv.get_response (); |
| 79 | verify_show_version_reply (r); |
| 80 | } |
| 81 | |
| 82 | END_TEST; |
| 83 | |
| 84 | struct 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 | |
| 97 | START_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 | |
| 108 | END_TEST; |
| 109 | |
| 110 | START_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 Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 130 | vapi_error_e rv; |
| 131 | WAIT_FOR_RESPONSE (cl, rv); |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 132 | 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 Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 149 | auto rv = d.execute (); |
| 150 | ck_assert_int_eq (VAPI_OK, rv); |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 151 | WAIT_FOR_RESPONSE (d, rv); |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 152 | 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 Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 178 | WAIT_FOR_RESPONSE (dl, rv); |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 179 | 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 Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 188 | auto rv = d.execute (); |
| 189 | ck_assert_int_eq (VAPI_OK, rv); |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 190 | WAIT_FOR_RESPONSE (d, rv); |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 191 | 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 | |
| 204 | END_TEST; |
| 205 | |
| 206 | struct Create_loopback_cb |
| 207 | { |
Klement Sekera | a0f55c9 | 2021-10-13 21:45:42 +0200 | [diff] [blame^] | 208 | Create_loopback_cb () : called{ 0 }, sw_if_index{ 0 }, seen{ false } {}; |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 209 | 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 | |
| 221 | struct Delete_loopback_cb |
| 222 | { |
Klement Sekera | a0f55c9 | 2021-10-13 21:45:42 +0200 | [diff] [blame^] | 223 | Delete_loopback_cb () : called{ 0 }, sw_if_index{ 0 }, seen{ false } {}; |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 224 | 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 | |
| 236 | template <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 | |
| 268 | START_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 Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 303 | auto rv = d.execute (); |
| 304 | ck_assert_int_eq (VAPI_OK, rv); |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 305 | WAIT_FOR_RESPONSE (d, rv); |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 306 | 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 Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 329 | auto rv = d.execute (); |
| 330 | ck_assert_int_eq (VAPI_OK, rv); |
Mohsin Kazmi | 3fca567 | 2018-01-04 18:57:26 +0100 | [diff] [blame] | 331 | WAIT_FOR_RESPONSE (d, rv); |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 332 | 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 | |
| 345 | END_TEST; |
| 346 | |
Klement Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 347 | START_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 | |
| 386 | END_TEST; |
| 387 | |
| 388 | Suite *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 Sekera | dc15be2 | 2017-06-12 06:49:33 +0200 | [diff] [blame] | 399 | tcase_add_test (tc_cpp_api, test_unsupported); |
| 400 | suite_add_tcase (s, tc_cpp_api); |
| 401 | |
| 402 | return s; |
| 403 | } |
| 404 | |
| 405 | int 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 | */ |