1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved 3 */ 4 5 #include <rte_errno.h> 6 #include <rte_log.h> 7 #include <rte_memory.h> 8 #include <rte_mempool.h> 9 #include <rte_security.h> 10 #include <rte_security_driver.h> 11 12 /* Before including rte_test.h file you can define 13 * RTE_TEST_TRACE_FAILURE(_file, _line, _func) macro to better trace/debug test 14 * failures. Mostly useful in development phase. 15 */ 16 #ifndef RTE_TEST_TRACE_FAILURE 17 #define RTE_TEST_TRACE_FAILURE(_file, _line, _func) \ 18 RTE_LOG(DEBUG, EAL, "in %s:%d %s\n", _file, _line, _func) 19 #endif 20 21 #include <rte_test.h> 22 #include "test.h" 23 24 /** 25 * Security 26 * ======= 27 * 28 * Basic unit tests of the librte_security API. 29 * 30 * Structure of the file: 31 * - macros for making tests more readable; 32 * - mockup structures and functions for rte_security_ops; 33 * - test suite and test cases setup and teardown functions; 34 * - tests functions; 35 * - declaration of testcases. 36 */ 37 38 39 /** 40 * Macros 41 * 42 * Set of macros for making tests easier to read. 43 */ 44 45 /** 46 * Verify condition inside mocked up function. 47 * Mockup function cannot return a test error, so the failure 48 * of assertion increases counter and print logs. 49 * The counter can be verified later to check if test case should fail. 50 * 51 * @param fail_counter fail counter 52 * @param cond condition expected to be true 53 * @param msg printf style formatting string for custom message 54 */ 55 #define MOCK_TEST_ASSERT(fail_counter, cond, msg, ...) do { \ 56 if (!(cond)) { \ 57 fail_counter++; \ 58 RTE_LOG(DEBUG, EAL, "Test assert %s line %d failed: " \ 59 msg "\n", __func__, __LINE__, \ 60 ##__VA_ARGS__); \ 61 RTE_TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__); \ 62 } \ 63 } while (0) 64 65 /** 66 * Verify equality condition inside mocked up function. 67 * Mockup function cannot return a test error, so the failure 68 * of assertion increases counter and print logs. 69 * The counter can be verified later to check if test case should fail. 70 * 71 * @param fail_counter fail counter 72 * @param a first value of comparison 73 * @param b second value of comparison 74 * @param msg printf style formatting string for custom message 75 */ 76 #define MOCK_TEST_ASSERT_EQUAL(fail_counter, a, b, msg, ...) \ 77 MOCK_TEST_ASSERT(fail_counter, (a) == (b), msg, ##__VA_ARGS__) 78 79 /** 80 * Verify not null condition inside mocked up function. 81 * Mockup function cannot return a test error, so the failure 82 * of assertion increases counter and print logs. 83 * The counter can be verified later to check if test case should fail. 84 * 85 * @param fail_counter fail counter 86 * @param val value expected not to be NULL 87 * @param msg printf style formatting string for custom message 88 */ 89 #define MOCK_TEST_ASSERT_NOT_NULL(fail_counter, val, msg, ...) \ 90 MOCK_TEST_ASSERT(fail_counter, (val) != NULL, msg, ##__VA_ARGS__) 91 92 93 /** 94 * Verify if parameter of the mocked up function matches expected value. 95 * The expected value is stored in data structure in the field matching 96 * parameter name. 97 * 98 * @param data structure with expected values 99 * @param parameter name of the parameter (both field and parameter name) 100 * @param spec printf style spec for parameter 101 */ 102 #define MOCK_TEST_ASSERT_PARAMETER(data, parameter, spec) \ 103 MOCK_TEST_ASSERT_EQUAL(data.failed, data.parameter, parameter, \ 104 "Expecting parameter %s to be " spec \ 105 " but it's " spec, RTE_STR(parameter), \ 106 data.parameter, parameter) 107 108 /** 109 * Wrap for MOCK_TEST_ASSERT_PARAMETER macro for pointer type parameters. 110 * 111 * @param data structure with expected values 112 * @param parameter name of the parameter (both field and parameter name) 113 */ 114 #define MOCK_TEST_ASSERT_POINTER_PARAMETER(data, parameter) \ 115 MOCK_TEST_ASSERT_PARAMETER(data, parameter, "%p") 116 117 /** 118 * Wrap for MOCK_TEST_ASSERT_PARAMETER macro for uint64_t type parameters. 119 * 120 * @param data structure with expected values 121 * @param parameter name of the parameter (both field and parameter name) 122 */ 123 #define MOCK_TEST_ASSERT_U64_PARAMETER(data, parameter) \ 124 MOCK_TEST_ASSERT_PARAMETER(data, parameter, "%" PRIu64) 125 126 /** 127 * Verify number of calls of the mocked up function 128 * and check if there were any fails during execution. 129 * The fails statistics inside mocked up functions are collected 130 * as "failed" field in mockup structures. 131 * 132 * @param mock_data structure with statistics (called, failed) 133 * @param exp_calls expected number of mockup function calls 134 */ 135 #define TEST_ASSERT_MOCK_CALLS(mock_data, exp_calls) do { \ 136 TEST_ASSERT_EQUAL(exp_calls, mock_data.called, \ 137 "Expecting sub op to be called %d times, " \ 138 "but it's called %d times", \ 139 exp_calls, mock_data.called); \ 140 TEST_ASSERT_EQUAL(0, mock_data.failed, \ 141 "Expecting sub op asserts not to fail, " \ 142 "but they're failed %d times", \ 143 mock_data.failed); \ 144 } while (0) 145 146 /** 147 * Assert tested function result match expected value 148 * 149 * @param f_name name of tested function 150 * @param f_ret value returned by the function 151 * @param exp_ret expected returned value 152 * @param fmt printf style format for returned value 153 */ 154 #define TEST_ASSERT_MOCK_FUNCTION_CALL_RET(f_name, f_ret, exp_ret, fmt) \ 155 TEST_ASSERT_EQUAL(exp_ret, f_ret, "Expecting " RTE_STR(f_name) \ 156 " to return " fmt ", but it returned " fmt \ 157 "\n", exp_ret, f_ret) 158 159 /** 160 * Assert tested function result is not NULL 161 * 162 * @param f_name name of tested function 163 * @param f_ret value returned by the function 164 */ 165 #define TEST_ASSERT_MOCK_FUNCTION_CALL_NOT_NULL(f_name, f_ret) \ 166 TEST_ASSERT_NOT_NULL(f_ret, "Expecting " RTE_STR(f_name) \ 167 " to return not NULL\n") 168 169 /** 170 * Verify that sess_cnt counter value matches expected 171 * 172 * @param expected_sessions_count expected counter value 173 */ 174 #define TEST_ASSERT_SESSION_COUNT(expected_sessions_count) do { \ 175 struct security_unittest_params *ut_params = &unittest_params; \ 176 TEST_ASSERT_EQUAL(expected_sessions_count, \ 177 ut_params->ctx.sess_cnt, \ 178 "Expecting session counter to be %u," \ 179 " but it's %u", expected_sessions_count, \ 180 ut_params->ctx.sess_cnt); \ 181 } while (0) 182 183 /** 184 * Verify usage of mempool by checking if number of allocated objects matches 185 * expectations. The mempool is used to manage objects for sessions data. 186 * A single object is acquired from mempool during session_create 187 * and put back in session_destroy. 188 * 189 * @param expected_mempool_usage expected number of used mempool objects 190 */ 191 #define TEST_ASSERT_MEMPOOL_USAGE(expected_mempool_usage) do { \ 192 struct security_testsuite_params *ts_params = &testsuite_params;\ 193 unsigned int mempool_usage; \ 194 mempool_usage = rte_mempool_in_use_count( \ 195 ts_params->session_mpool); \ 196 TEST_ASSERT_EQUAL(expected_mempool_usage, mempool_usage, \ 197 "Expecting %u mempool allocations, " \ 198 "but there are %u allocated objects", \ 199 expected_mempool_usage, mempool_usage); \ 200 } while (0) 201 202 203 /** 204 * Mockup structures and functions for rte_security_ops; 205 * 206 * Set of structures for controlling mockup functions calls. 207 * Every mockup function X has its corresponding X_data structure 208 * and an instance of that structure X_exp. 209 * Structure contains parameters that a mockup function is expected 210 * to be called with, a value to return (.ret) and 2 statistics: 211 * .called (number of times the mockup function was called) 212 * and .failed (number of assertion fails during mockup function call). 213 * 214 * Mockup functions verify that the parameters they are called with match 215 * expected values. The expected values should be stored in corresponding 216 * structures prior to mockup functions call. Every failure of such 217 * verification increases .failed counter. Every call of mockup function 218 * increases .called counter. Function returns value stored in .ret field 219 * of the structure. 220 * In case of some parameters in some functions the expected value is unknown 221 * and cannot be detrmined prior to call. Such parameters are stored 222 * in structure and can be compared or analyzed later in test case code. 223 * 224 * Below structures and functions follow the rules just described. 225 * Additional remarks and exceptions are added in comments. 226 */ 227 228 /** 229 * session_create mockup 230 * 231 * Verified parameters: device, conf, mp. 232 * Saved, not verified parameters: sess. 233 */ 234 static struct mock_session_create_data { 235 void *device; 236 struct rte_security_session_conf *conf; 237 struct rte_security_session *sess; 238 struct rte_mempool *mp; 239 240 int ret; 241 242 int called; 243 int failed; 244 } mock_session_create_exp = {NULL, NULL, NULL, NULL, 0, 0, 0}; 245 246 static int 247 mock_session_create(void *device, 248 struct rte_security_session_conf *conf, 249 struct rte_security_session *sess, 250 struct rte_mempool *mp) 251 { 252 mock_session_create_exp.called++; 253 254 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_create_exp, device); 255 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_create_exp, conf); 256 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_create_exp, mp); 257 258 mock_session_create_exp.sess = sess; 259 260 return mock_session_create_exp.ret; 261 } 262 263 /** 264 * session_update mockup 265 * 266 * Verified parameters: device, sess, conf. 267 */ 268 static struct mock_session_update_data { 269 void *device; 270 struct rte_security_session *sess; 271 struct rte_security_session_conf *conf; 272 273 int ret; 274 275 int called; 276 int failed; 277 } mock_session_update_exp = {NULL, NULL, NULL, 0, 0, 0}; 278 279 static int 280 mock_session_update(void *device, 281 struct rte_security_session *sess, 282 struct rte_security_session_conf *conf) 283 { 284 mock_session_update_exp.called++; 285 286 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_update_exp, device); 287 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_update_exp, sess); 288 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_update_exp, conf); 289 290 return mock_session_update_exp.ret; 291 } 292 293 /** 294 * session_get_size mockup 295 * 296 * Verified parameters: device. 297 */ 298 static struct mock_session_get_size_data { 299 void *device; 300 301 unsigned int ret; 302 303 int called; 304 int failed; 305 } mock_session_get_size_exp = {NULL, 0U, 0, 0}; 306 307 static unsigned int 308 mock_session_get_size(void *device) 309 { 310 mock_session_get_size_exp.called++; 311 312 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_get_size_exp, device); 313 314 return mock_session_get_size_exp.ret; 315 } 316 317 /** 318 * session_stats_get mockup 319 * 320 * Verified parameters: device, sess, stats. 321 */ 322 static struct mock_session_stats_get_data { 323 void *device; 324 struct rte_security_session *sess; 325 struct rte_security_stats *stats; 326 327 int ret; 328 329 int called; 330 int failed; 331 } mock_session_stats_get_exp = {NULL, NULL, NULL, 0, 0, 0}; 332 333 static int 334 mock_session_stats_get(void *device, 335 struct rte_security_session *sess, 336 struct rte_security_stats *stats) 337 { 338 mock_session_stats_get_exp.called++; 339 340 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_stats_get_exp, device); 341 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_stats_get_exp, sess); 342 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_stats_get_exp, stats); 343 344 return mock_session_stats_get_exp.ret; 345 } 346 347 /** 348 * session_destroy mockup 349 * 350 * Verified parameters: device, sess. 351 */ 352 static struct mock_session_destroy_data { 353 void *device; 354 struct rte_security_session *sess; 355 356 int ret; 357 358 int called; 359 int failed; 360 } mock_session_destroy_exp = {NULL, NULL, 0, 0, 0}; 361 362 static int 363 mock_session_destroy(void *device, struct rte_security_session *sess) 364 { 365 mock_session_destroy_exp.called++; 366 367 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_destroy_exp, device); 368 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_destroy_exp, sess); 369 370 return mock_session_destroy_exp.ret; 371 } 372 373 /** 374 * set_pkt_metadata mockup 375 * 376 * Verified parameters: device, sess, m, params. 377 */ 378 static struct mock_set_pkt_metadata_data { 379 void *device; 380 struct rte_security_session *sess; 381 struct rte_mbuf *m; 382 void *params; 383 384 int ret; 385 386 int called; 387 int failed; 388 } mock_set_pkt_metadata_exp = {NULL, NULL, NULL, NULL, 0, 0, 0}; 389 390 static int 391 mock_set_pkt_metadata(void *device, 392 struct rte_security_session *sess, 393 struct rte_mbuf *m, 394 void *params) 395 { 396 mock_set_pkt_metadata_exp.called++; 397 398 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_set_pkt_metadata_exp, device); 399 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_set_pkt_metadata_exp, sess); 400 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_set_pkt_metadata_exp, m); 401 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_set_pkt_metadata_exp, params); 402 403 return mock_set_pkt_metadata_exp.ret; 404 } 405 406 /** 407 * get_userdata mockup 408 * 409 * Verified parameters: device, md. 410 * The userdata parameter works as an output parameter, so a passed address 411 * is verified not to be NULL and filled with userdata stored in structure. 412 */ 413 static struct mock_get_userdata_data { 414 void *device; 415 uint64_t md; 416 void *userdata; 417 418 int ret; 419 420 int called; 421 int failed; 422 } mock_get_userdata_exp = {NULL, 0UL, NULL, 0, 0, 0}; 423 424 static int 425 mock_get_userdata(void *device, 426 uint64_t md, 427 void **userdata) 428 { 429 mock_get_userdata_exp.called++; 430 431 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_get_userdata_exp, device); 432 MOCK_TEST_ASSERT_U64_PARAMETER(mock_get_userdata_exp, md); 433 434 MOCK_TEST_ASSERT_NOT_NULL(mock_get_userdata_exp.failed, 435 userdata, 436 "Expecting parameter userdata not to be NULL but it's %p", 437 userdata); 438 *userdata = mock_get_userdata_exp.userdata; 439 440 return mock_get_userdata_exp.ret; 441 } 442 443 /** 444 * capabilities_get mockup 445 * 446 * Verified parameters: device. 447 */ 448 static struct mock_capabilities_get_data { 449 void *device; 450 451 struct rte_security_capability *ret; 452 453 int called; 454 int failed; 455 } mock_capabilities_get_exp = {NULL, NULL, 0, 0}; 456 457 static const struct rte_security_capability * 458 mock_capabilities_get(void *device) 459 { 460 mock_capabilities_get_exp.called++; 461 462 MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_capabilities_get_exp, device); 463 464 return mock_capabilities_get_exp.ret; 465 } 466 467 /** 468 * empty_ops 469 * 470 * is an empty security operations set (all function pointers set to NULL) 471 */ 472 struct rte_security_ops empty_ops = { NULL }; 473 474 /** 475 * mock_ops 476 * 477 * is a security operations set using mockup functions 478 */ 479 struct rte_security_ops mock_ops = { 480 .session_create = mock_session_create, 481 .session_update = mock_session_update, 482 .session_get_size = mock_session_get_size, 483 .session_stats_get = mock_session_stats_get, 484 .session_destroy = mock_session_destroy, 485 .set_pkt_metadata = mock_set_pkt_metadata, 486 .get_userdata = mock_get_userdata, 487 .capabilities_get = mock_capabilities_get, 488 }; 489 490 491 /** 492 * Test suite and test cases setup and teardown functions. 493 */ 494 495 /** 496 * struct security_testsuite_params defines parameters initialized once 497 * for whole tests suite. 498 * Currently the only stored parameter is session_mpool a mempool created 499 * once in testsuite_setup and released in testsuite_teardown. 500 * The instance of this structure is stored in testsuite_params variable. 501 */ 502 static struct security_testsuite_params { 503 struct rte_mempool *session_mpool; 504 } testsuite_params = { NULL }; 505 506 /** 507 * struct security_unittest_params defines parameters initialized 508 * for every test case. The parameters are initialized in ut_setup 509 * or ut_setup_with_session (depending on the testcase) 510 * and released in ut_teardown. 511 * The instance of this structure is stored in unittest_params variable. 512 */ 513 static struct security_unittest_params { 514 struct rte_security_ctx ctx; 515 struct rte_security_session_conf conf; 516 struct rte_security_session *sess; 517 } unittest_params = { 518 .ctx = { 519 .device = NULL, 520 .ops = &mock_ops, 521 .sess_cnt = 0, 522 }, 523 .sess = NULL, 524 }; 525 526 #define SECURITY_TEST_MEMPOOL_NAME "SecurityTestsMempoolName" 527 #define SECURITY_TEST_MEMPOOL_SIZE 15 528 #define SECURITY_TEST_SESSION_OBJECT_SIZE sizeof(struct rte_security_session) 529 530 /** 531 * testsuite_setup initializes whole test suite parameters. 532 * It creates a new mempool used in all test cases 533 * and verifies if it properly created. 534 */ 535 static int 536 testsuite_setup(void) 537 { 538 struct security_testsuite_params *ts_params = &testsuite_params; 539 ts_params->session_mpool = rte_mempool_create( 540 SECURITY_TEST_MEMPOOL_NAME, 541 SECURITY_TEST_MEMPOOL_SIZE, 542 SECURITY_TEST_SESSION_OBJECT_SIZE, 543 0, 0, NULL, NULL, NULL, NULL, 544 SOCKET_ID_ANY, 0); 545 TEST_ASSERT_NOT_NULL(ts_params->session_mpool, 546 "Cannot create mempool %s\n", rte_strerror(rte_errno)); 547 return TEST_SUCCESS; 548 } 549 550 /** 551 * testsuite_teardown releases test suite wide parameters. 552 */ 553 static void 554 testsuite_teardown(void) 555 { 556 struct security_testsuite_params *ts_params = &testsuite_params; 557 if (ts_params->session_mpool) { 558 rte_mempool_free(ts_params->session_mpool); 559 ts_params->session_mpool = NULL; 560 } 561 } 562 563 /** 564 * ut_setup initializes test case parameters to default values. 565 * It resets also any .called and .failed statistics of mockup functions 566 * usage. 567 */ 568 static int 569 ut_setup(void) 570 { 571 struct security_unittest_params *ut_params = &unittest_params; 572 ut_params->ctx.device = NULL; 573 ut_params->ctx.ops = &mock_ops; 574 ut_params->ctx.sess_cnt = 0; 575 ut_params->sess = NULL; 576 577 mock_session_create_exp.called = 0; 578 mock_session_update_exp.called = 0; 579 mock_session_get_size_exp.called = 0; 580 mock_session_stats_get_exp.called = 0; 581 mock_session_destroy_exp.called = 0; 582 mock_set_pkt_metadata_exp.called = 0; 583 mock_get_userdata_exp.called = 0; 584 mock_capabilities_get_exp.called = 0; 585 586 mock_session_create_exp.failed = 0; 587 mock_session_update_exp.failed = 0; 588 mock_session_get_size_exp.failed = 0; 589 mock_session_stats_get_exp.failed = 0; 590 mock_session_destroy_exp.failed = 0; 591 mock_set_pkt_metadata_exp.failed = 0; 592 mock_get_userdata_exp.failed = 0; 593 mock_capabilities_get_exp.failed = 0; 594 595 return TEST_SUCCESS; 596 } 597 598 /** 599 * destroy_session_with_check is a helper function releasing session 600 * created with rte_security_session_create and stored in test case parameters. 601 * It's used both to release sessions created in test cases' bodies 602 * which are assigned to ut_params->sess 603 * as well as sessions created in ut_setup_with_session. 604 */ 605 static int 606 destroy_session_with_check(void) 607 { 608 struct security_unittest_params *ut_params = &unittest_params; 609 if (ut_params->sess != NULL) { 610 /* Assure that mockup function for destroy operation is set. */ 611 ut_params->ctx.ops = &mock_ops; 612 613 mock_session_destroy_exp.device = NULL; 614 mock_session_destroy_exp.sess = ut_params->sess; 615 mock_session_destroy_exp.ret = 0; 616 mock_session_destroy_exp.called = 0; 617 mock_session_destroy_exp.failed = 0; 618 619 int ret = rte_security_session_destroy(&ut_params->ctx, 620 ut_params->sess); 621 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy, 622 ret, 0, "%d"); 623 TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 1); 624 625 ut_params->sess = NULL; 626 } 627 return TEST_SUCCESS; 628 } 629 630 /** 631 * ut_teardown releases test case parameters. 632 */ 633 static void 634 ut_teardown(void) 635 { 636 destroy_session_with_check(); 637 } 638 639 /** 640 * ut_setup_with_session initializes test case parameters by 641 * - calling standard ut_setup, 642 * - creating a session that can be used in test case. 643 */ 644 static int 645 ut_setup_with_session(void) 646 { 647 struct security_unittest_params *ut_params = &unittest_params; 648 struct security_testsuite_params *ts_params = &testsuite_params; 649 struct rte_security_session *sess; 650 651 int ret = ut_setup(); 652 if (ret != TEST_SUCCESS) 653 return ret; 654 655 mock_session_create_exp.device = NULL; 656 mock_session_create_exp.conf = &ut_params->conf; 657 mock_session_create_exp.mp = ts_params->session_mpool; 658 mock_session_create_exp.ret = 0; 659 660 sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf, 661 ts_params->session_mpool); 662 TEST_ASSERT_MOCK_FUNCTION_CALL_NOT_NULL(rte_security_session_create, 663 sess); 664 TEST_ASSERT_EQUAL(sess, mock_session_create_exp.sess, 665 "Expecting session_create to be called with %p sess" 666 " parameter, but it's called %p sess parameter", 667 sess, mock_session_create_exp.sess); 668 TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 1); 669 670 /* 671 * Store created session in test case parameters, so it can be released 672 * after test case in ut_teardown by destroy_session_with_check. 673 */ 674 ut_params->sess = sess; 675 676 return TEST_SUCCESS; 677 } 678 679 680 /** 681 * Test functions 682 * 683 * Each test function is related to a single test case. 684 * They are arranged by tested rte_security API function 685 * and by rte_security execution paths sequence in code. 686 */ 687 688 /** 689 * rte_security_session_create tests 690 */ 691 692 /** 693 * Test execution of rte_security_session_create with NULL instance 694 */ 695 static int 696 test_session_create_inv_context(void) 697 { 698 struct security_testsuite_params *ts_params = &testsuite_params; 699 struct security_unittest_params *ut_params = &unittest_params; 700 struct rte_security_session *sess; 701 702 sess = rte_security_session_create(NULL, &ut_params->conf, 703 ts_params->session_mpool); 704 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create, 705 sess, NULL, "%p"); 706 TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0); 707 TEST_ASSERT_MEMPOOL_USAGE(0); 708 TEST_ASSERT_SESSION_COUNT(0); 709 710 return TEST_SUCCESS; 711 } 712 713 /** 714 * Test execution of rte_security_session_create with invalid 715 * security operations structure (NULL) 716 */ 717 static int 718 test_session_create_inv_context_ops(void) 719 { 720 struct security_testsuite_params *ts_params = &testsuite_params; 721 struct security_unittest_params *ut_params = &unittest_params; 722 struct rte_security_session *sess; 723 724 ut_params->ctx.ops = NULL; 725 726 sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf, 727 ts_params->session_mpool); 728 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create, 729 sess, NULL, "%p"); 730 TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0); 731 TEST_ASSERT_MEMPOOL_USAGE(0); 732 TEST_ASSERT_SESSION_COUNT(0); 733 734 return TEST_SUCCESS; 735 } 736 737 /** 738 * Test execution of rte_security_session_create with empty 739 * security operations 740 */ 741 static int 742 test_session_create_inv_context_ops_fun(void) 743 { 744 struct security_testsuite_params *ts_params = &testsuite_params; 745 struct security_unittest_params *ut_params = &unittest_params; 746 struct rte_security_session *sess; 747 748 ut_params->ctx.ops = &empty_ops; 749 750 sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf, 751 ts_params->session_mpool); 752 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create, 753 sess, NULL, "%p"); 754 TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0); 755 TEST_ASSERT_MEMPOOL_USAGE(0); 756 TEST_ASSERT_SESSION_COUNT(0); 757 758 return TEST_SUCCESS; 759 } 760 761 /** 762 * Test execution of rte_security_session_create with NULL conf parameter 763 */ 764 static int 765 test_session_create_inv_configuration(void) 766 { 767 struct security_testsuite_params *ts_params = &testsuite_params; 768 struct security_unittest_params *ut_params = &unittest_params; 769 struct rte_security_session *sess; 770 771 sess = rte_security_session_create(&ut_params->ctx, NULL, 772 ts_params->session_mpool); 773 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create, 774 sess, NULL, "%p"); 775 TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0); 776 TEST_ASSERT_MEMPOOL_USAGE(0); 777 TEST_ASSERT_SESSION_COUNT(0); 778 779 return TEST_SUCCESS; 780 } 781 782 /** 783 * Test execution of rte_security_session_create with NULL mp parameter 784 */ 785 static int 786 test_session_create_inv_mempool(void) 787 { 788 struct security_unittest_params *ut_params = &unittest_params; 789 struct rte_security_session *sess; 790 791 sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf, 792 NULL); 793 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create, 794 sess, NULL, "%p"); 795 TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0); 796 TEST_ASSERT_MEMPOOL_USAGE(0); 797 TEST_ASSERT_SESSION_COUNT(0); 798 799 return TEST_SUCCESS; 800 } 801 802 /** 803 * Test execution of rte_security_session_create in case when mempool 804 * is fully used and no object can be got from it 805 */ 806 static int 807 test_session_create_mempool_empty(void) 808 { 809 struct security_testsuite_params *ts_params = &testsuite_params; 810 struct security_unittest_params *ut_params = &unittest_params; 811 struct rte_security_session *tmp[SECURITY_TEST_MEMPOOL_SIZE]; 812 struct rte_security_session *sess; 813 814 /* Get all available objects from mempool. */ 815 int i, ret; 816 for (i = 0; i < SECURITY_TEST_MEMPOOL_SIZE; ++i) { 817 ret = rte_mempool_get(ts_params->session_mpool, 818 (void **)(&tmp[i])); 819 TEST_ASSERT_EQUAL(0, ret, 820 "Expect getting %d object from mempool" 821 " to succeed", i); 822 } 823 TEST_ASSERT_MEMPOOL_USAGE(SECURITY_TEST_MEMPOOL_SIZE); 824 825 sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf, 826 ts_params->session_mpool); 827 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create, 828 sess, NULL, "%p"); 829 TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0); 830 TEST_ASSERT_MEMPOOL_USAGE(SECURITY_TEST_MEMPOOL_SIZE); 831 TEST_ASSERT_SESSION_COUNT(0); 832 833 /* Put objects back to the pool. */ 834 for (i = 0; i < SECURITY_TEST_MEMPOOL_SIZE; ++i) 835 rte_mempool_put(ts_params->session_mpool, (void *)(tmp[i])); 836 TEST_ASSERT_MEMPOOL_USAGE(0); 837 838 return TEST_SUCCESS; 839 } 840 841 /** 842 * Test execution of rte_security_session_create when session_create 843 * security operation fails 844 */ 845 static int 846 test_session_create_ops_failure(void) 847 { 848 struct security_testsuite_params *ts_params = &testsuite_params; 849 struct security_unittest_params *ut_params = &unittest_params; 850 struct rte_security_session *sess; 851 852 mock_session_create_exp.device = NULL; 853 mock_session_create_exp.conf = &ut_params->conf; 854 mock_session_create_exp.mp = ts_params->session_mpool; 855 mock_session_create_exp.ret = -1; /* Return failure status. */ 856 857 sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf, 858 ts_params->session_mpool); 859 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create, 860 sess, NULL, "%p"); 861 TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 1); 862 TEST_ASSERT_MEMPOOL_USAGE(0); 863 TEST_ASSERT_SESSION_COUNT(0); 864 865 return TEST_SUCCESS; 866 } 867 868 /** 869 * Test execution of rte_security_session_create in successful execution path 870 */ 871 static int 872 test_session_create_success(void) 873 { 874 struct security_testsuite_params *ts_params = &testsuite_params; 875 struct security_unittest_params *ut_params = &unittest_params; 876 struct rte_security_session *sess; 877 878 mock_session_create_exp.device = NULL; 879 mock_session_create_exp.conf = &ut_params->conf; 880 mock_session_create_exp.mp = ts_params->session_mpool; 881 mock_session_create_exp.ret = 0; /* Return success status. */ 882 883 sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf, 884 ts_params->session_mpool); 885 TEST_ASSERT_MOCK_FUNCTION_CALL_NOT_NULL(rte_security_session_create, 886 sess); 887 TEST_ASSERT_EQUAL(sess, mock_session_create_exp.sess, 888 "Expecting session_create to be called with %p sess" 889 " parameter, but it's called %p sess parameter", 890 sess, mock_session_create_exp.sess); 891 TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 1); 892 TEST_ASSERT_MEMPOOL_USAGE(1); 893 TEST_ASSERT_SESSION_COUNT(1); 894 895 /* 896 * Store created session in test case parameters, so it can be released 897 * after test case in ut_teardown by destroy_session_with_check. 898 */ 899 ut_params->sess = sess; 900 901 return TEST_SUCCESS; 902 } 903 904 905 /** 906 * rte_security_session_update tests 907 */ 908 909 /** 910 * Test execution of rte_security_session_update with NULL instance 911 */ 912 static int 913 test_session_update_inv_context(void) 914 { 915 struct security_unittest_params *ut_params = &unittest_params; 916 917 int ret = rte_security_session_update(NULL, ut_params->sess, 918 &ut_params->conf); 919 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update, 920 ret, -EINVAL, "%d"); 921 TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0); 922 923 return TEST_SUCCESS; 924 } 925 926 /** 927 * Test execution of rte_security_session_update with invalid 928 * security operations structure (NULL) 929 */ 930 static int 931 test_session_update_inv_context_ops(void) 932 { 933 struct security_unittest_params *ut_params = &unittest_params; 934 ut_params->ctx.ops = NULL; 935 936 int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess, 937 &ut_params->conf); 938 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update, 939 ret, -EINVAL, "%d"); 940 TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0); 941 942 return TEST_SUCCESS; 943 } 944 945 /** 946 * Test execution of rte_security_session_update with empty 947 * security operations 948 */ 949 static int 950 test_session_update_inv_context_ops_fun(void) 951 { 952 struct security_unittest_params *ut_params = &unittest_params; 953 ut_params->ctx.ops = &empty_ops; 954 955 int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess, 956 &ut_params->conf); 957 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update, 958 ret, -ENOTSUP, "%d"); 959 TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0); 960 961 return TEST_SUCCESS; 962 } 963 964 /** 965 * Test execution of rte_security_session_update with NULL conf parameter 966 */ 967 static int 968 test_session_update_inv_configuration(void) 969 { 970 struct security_unittest_params *ut_params = &unittest_params; 971 972 int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess, 973 NULL); 974 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update, 975 ret, -EINVAL, "%d"); 976 TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0); 977 978 return TEST_SUCCESS; 979 } 980 981 /** 982 * Test execution of rte_security_session_update with NULL sess parameter 983 */ 984 static int 985 test_session_update_inv_session(void) 986 { 987 struct security_unittest_params *ut_params = &unittest_params; 988 989 int ret = rte_security_session_update(&ut_params->ctx, NULL, 990 &ut_params->conf); 991 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update, 992 ret, -EINVAL, "%d"); 993 TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0); 994 995 return TEST_SUCCESS; 996 } 997 998 /** 999 * Test execution of rte_security_session_update when session_update 1000 * security operation fails 1001 */ 1002 static int 1003 test_session_update_ops_failure(void) 1004 { 1005 struct security_unittest_params *ut_params = &unittest_params; 1006 1007 mock_session_update_exp.device = NULL; 1008 mock_session_update_exp.sess = ut_params->sess; 1009 mock_session_update_exp.conf = &ut_params->conf; 1010 mock_session_update_exp.ret = -1; /* Return failure status. */ 1011 1012 int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess, 1013 &ut_params->conf); 1014 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update, 1015 ret, -1, "%d"); 1016 TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 1); 1017 1018 return TEST_SUCCESS; 1019 } 1020 1021 /** 1022 * Test execution of rte_security_session_update in successful execution path 1023 */ 1024 static int 1025 test_session_update_success(void) 1026 { 1027 struct security_unittest_params *ut_params = &unittest_params; 1028 1029 mock_session_update_exp.device = NULL; 1030 mock_session_update_exp.sess = ut_params->sess; 1031 mock_session_update_exp.conf = &ut_params->conf; 1032 mock_session_update_exp.ret = 0; /* Return success status. */ 1033 1034 int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess, 1035 &ut_params->conf); 1036 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update, 1037 ret, 0, "%d"); 1038 TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 1); 1039 1040 return TEST_SUCCESS; 1041 } 1042 1043 1044 /** 1045 * rte_security_session_get_size tests 1046 */ 1047 1048 /** 1049 * Test execution of rte_security_session_get_size with NULL instance 1050 */ 1051 static int 1052 test_session_get_size_inv_context(void) 1053 { 1054 unsigned int ret = rte_security_session_get_size(NULL); 1055 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size, 1056 ret, 0, "%u"); 1057 TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 0); 1058 1059 return TEST_SUCCESS; 1060 } 1061 1062 /** 1063 * Test execution of rte_security_session_get_size with invalid 1064 * security operations structure (NULL) 1065 */ 1066 static int 1067 test_session_get_size_inv_context_ops(void) 1068 { 1069 struct security_unittest_params *ut_params = &unittest_params; 1070 ut_params->ctx.ops = NULL; 1071 1072 unsigned int ret = rte_security_session_get_size(&ut_params->ctx); 1073 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size, 1074 ret, 0, "%u"); 1075 TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 0); 1076 1077 return TEST_SUCCESS; 1078 } 1079 1080 /** 1081 * Test execution of rte_security_session_get_size with empty 1082 * security operations 1083 */ 1084 static int 1085 test_session_get_size_inv_context_ops_fun(void) 1086 { 1087 struct security_unittest_params *ut_params = &unittest_params; 1088 ut_params->ctx.ops = &empty_ops; 1089 1090 unsigned int ret = rte_security_session_get_size(&ut_params->ctx); 1091 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size, 1092 ret, 0, "%u"); 1093 TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 0); 1094 1095 return TEST_SUCCESS; 1096 } 1097 1098 /** 1099 * Test execution of rte_security_session_get_size when session_get_size 1100 * security operation fails 1101 */ 1102 static int 1103 test_session_get_size_ops_failure(void) 1104 { 1105 struct security_unittest_params *ut_params = &unittest_params; 1106 1107 mock_session_get_size_exp.device = NULL; 1108 mock_session_get_size_exp.ret = 0; 1109 1110 unsigned int ret = rte_security_session_get_size(&ut_params->ctx); 1111 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size, 1112 ret, 0, "%u"); 1113 TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 1); 1114 1115 return TEST_SUCCESS; 1116 } 1117 1118 /** 1119 * Test execution of rte_security_session_get_size in successful execution path 1120 */ 1121 static int 1122 test_session_get_size_success(void) 1123 { 1124 struct security_unittest_params *ut_params = &unittest_params; 1125 1126 mock_session_get_size_exp.device = NULL; 1127 mock_session_get_size_exp.ret = 1024; 1128 1129 unsigned int ret = rte_security_session_get_size(&ut_params->ctx); 1130 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size, 1131 ret, 1024U, "%u"); 1132 TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 1); 1133 1134 return TEST_SUCCESS; 1135 } 1136 1137 1138 /** 1139 * rte_security_session_stats_get tests 1140 */ 1141 1142 /** 1143 * Test execution of rte_security_session_stats_get with NULL instance 1144 */ 1145 static int 1146 test_session_stats_get_inv_context(void) 1147 { 1148 struct security_unittest_params *ut_params = &unittest_params; 1149 struct rte_security_stats stats; 1150 1151 int ret = rte_security_session_stats_get(NULL, ut_params->sess, &stats); 1152 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get, 1153 ret, -EINVAL, "%d"); 1154 TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 0); 1155 1156 return TEST_SUCCESS; 1157 } 1158 1159 /** 1160 * Test execution of rte_security_session_stats_get with invalid 1161 * security operations structure (NULL) 1162 */ 1163 static int 1164 test_session_stats_get_inv_context_ops(void) 1165 { 1166 struct security_unittest_params *ut_params = &unittest_params; 1167 struct rte_security_stats stats; 1168 ut_params->ctx.ops = NULL; 1169 1170 int ret = rte_security_session_stats_get(&ut_params->ctx, 1171 ut_params->sess, &stats); 1172 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get, 1173 ret, -EINVAL, "%d"); 1174 TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 0); 1175 1176 return TEST_SUCCESS; 1177 } 1178 1179 /** 1180 * Test execution of rte_security_session_stats_get with empty 1181 * security operations 1182 */ 1183 static int 1184 test_session_stats_get_inv_context_ops_fun(void) 1185 { 1186 struct security_unittest_params *ut_params = &unittest_params; 1187 struct rte_security_stats stats; 1188 ut_params->ctx.ops = &empty_ops; 1189 1190 int ret = rte_security_session_stats_get(&ut_params->ctx, 1191 ut_params->sess, &stats); 1192 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get, 1193 ret, -ENOTSUP, "%d"); 1194 TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 0); 1195 1196 return TEST_SUCCESS; 1197 } 1198 1199 /** 1200 * Test execution of rte_security_session_stats_get with NULL stats parameter 1201 */ 1202 static int 1203 test_session_stats_get_inv_stats(void) 1204 { 1205 struct security_unittest_params *ut_params = &unittest_params; 1206 1207 int ret = rte_security_session_stats_get(&ut_params->ctx, 1208 ut_params->sess, NULL); 1209 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get, 1210 ret, -EINVAL, "%d"); 1211 TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 0); 1212 1213 return TEST_SUCCESS; 1214 } 1215 1216 /** 1217 * Test execution of rte_security_session_stats_get when session_stats_get 1218 * security operation fails 1219 */ 1220 static int 1221 test_session_stats_get_ops_failure(void) 1222 { 1223 struct security_unittest_params *ut_params = &unittest_params; 1224 struct rte_security_stats stats; 1225 1226 mock_session_stats_get_exp.device = NULL; 1227 mock_session_stats_get_exp.sess = ut_params->sess; 1228 mock_session_stats_get_exp.stats = &stats; 1229 mock_session_stats_get_exp.ret = -1; 1230 1231 int ret = rte_security_session_stats_get(&ut_params->ctx, 1232 ut_params->sess, &stats); 1233 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get, 1234 ret, -1, "%d"); 1235 TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 1); 1236 1237 return TEST_SUCCESS; 1238 } 1239 1240 /** 1241 * Test execution of rte_security_session_stats_get in successful execution 1242 * path 1243 */ 1244 static int 1245 test_session_stats_get_success(void) 1246 { 1247 struct security_unittest_params *ut_params = &unittest_params; 1248 struct rte_security_stats stats; 1249 1250 mock_session_stats_get_exp.device = NULL; 1251 mock_session_stats_get_exp.sess = ut_params->sess; 1252 mock_session_stats_get_exp.stats = &stats; 1253 mock_session_stats_get_exp.ret = 0; 1254 1255 int ret = rte_security_session_stats_get(&ut_params->ctx, 1256 ut_params->sess, &stats); 1257 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get, 1258 ret, 0, "%d"); 1259 TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 1); 1260 1261 return TEST_SUCCESS; 1262 } 1263 1264 1265 /** 1266 * rte_security_session_destroy tests 1267 */ 1268 1269 /** 1270 * Test execution of rte_security_session_destroy with NULL instance 1271 */ 1272 static int 1273 test_session_destroy_inv_context(void) 1274 { 1275 struct security_unittest_params *ut_params = &unittest_params; 1276 1277 TEST_ASSERT_MEMPOOL_USAGE(1); 1278 TEST_ASSERT_SESSION_COUNT(1); 1279 1280 int ret = rte_security_session_destroy(NULL, ut_params->sess); 1281 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy, 1282 ret, -EINVAL, "%d"); 1283 TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 0); 1284 TEST_ASSERT_MEMPOOL_USAGE(1); 1285 TEST_ASSERT_SESSION_COUNT(1); 1286 1287 return TEST_SUCCESS; 1288 } 1289 1290 /** 1291 * Test execution of rte_security_session_destroy with invalid 1292 * security operations structure (NULL) 1293 */ 1294 static int 1295 test_session_destroy_inv_context_ops(void) 1296 { 1297 struct security_unittest_params *ut_params = &unittest_params; 1298 ut_params->ctx.ops = NULL; 1299 1300 TEST_ASSERT_MEMPOOL_USAGE(1); 1301 TEST_ASSERT_SESSION_COUNT(1); 1302 1303 int ret = rte_security_session_destroy(&ut_params->ctx, 1304 ut_params->sess); 1305 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy, 1306 ret, -EINVAL, "%d"); 1307 TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 0); 1308 TEST_ASSERT_MEMPOOL_USAGE(1); 1309 TEST_ASSERT_SESSION_COUNT(1); 1310 1311 return TEST_SUCCESS; 1312 } 1313 1314 /** 1315 * Test execution of rte_security_session_destroy with empty 1316 * security operations 1317 */ 1318 static int 1319 test_session_destroy_inv_context_ops_fun(void) 1320 { 1321 struct security_unittest_params *ut_params = &unittest_params; 1322 ut_params->ctx.ops = &empty_ops; 1323 1324 TEST_ASSERT_MEMPOOL_USAGE(1); 1325 TEST_ASSERT_SESSION_COUNT(1); 1326 1327 int ret = rte_security_session_destroy(&ut_params->ctx, 1328 ut_params->sess); 1329 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy, 1330 ret, -ENOTSUP, "%d"); 1331 TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 0); 1332 TEST_ASSERT_MEMPOOL_USAGE(1); 1333 TEST_ASSERT_SESSION_COUNT(1); 1334 1335 return TEST_SUCCESS; 1336 } 1337 1338 /** 1339 * Test execution of rte_security_session_destroy with NULL sess parameter 1340 */ 1341 static int 1342 test_session_destroy_inv_session(void) 1343 { 1344 struct security_unittest_params *ut_params = &unittest_params; 1345 1346 TEST_ASSERT_MEMPOOL_USAGE(1); 1347 TEST_ASSERT_SESSION_COUNT(1); 1348 1349 int ret = rte_security_session_destroy(&ut_params->ctx, NULL); 1350 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy, 1351 ret, -EINVAL, "%d"); 1352 TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 0); 1353 TEST_ASSERT_MEMPOOL_USAGE(1); 1354 TEST_ASSERT_SESSION_COUNT(1); 1355 1356 return TEST_SUCCESS; 1357 } 1358 1359 /** 1360 * Test execution of rte_security_session_destroy when session_destroy 1361 * security operation fails 1362 */ 1363 static int 1364 test_session_destroy_ops_failure(void) 1365 { 1366 struct security_unittest_params *ut_params = &unittest_params; 1367 1368 mock_session_destroy_exp.device = NULL; 1369 mock_session_destroy_exp.sess = ut_params->sess; 1370 mock_session_destroy_exp.ret = -1; 1371 1372 TEST_ASSERT_MEMPOOL_USAGE(1); 1373 TEST_ASSERT_SESSION_COUNT(1); 1374 1375 int ret = rte_security_session_destroy(&ut_params->ctx, 1376 ut_params->sess); 1377 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy, 1378 ret, -1, "%d"); 1379 TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 1); 1380 TEST_ASSERT_MEMPOOL_USAGE(1); 1381 TEST_ASSERT_SESSION_COUNT(1); 1382 1383 return TEST_SUCCESS; 1384 } 1385 1386 /** 1387 * Test execution of rte_security_session_destroy in successful execution path 1388 */ 1389 static int 1390 test_session_destroy_success(void) 1391 { 1392 struct security_unittest_params *ut_params = &unittest_params; 1393 1394 mock_session_destroy_exp.device = NULL; 1395 mock_session_destroy_exp.sess = ut_params->sess; 1396 mock_session_destroy_exp.ret = 0; 1397 TEST_ASSERT_MEMPOOL_USAGE(1); 1398 TEST_ASSERT_SESSION_COUNT(1); 1399 1400 int ret = rte_security_session_destroy(&ut_params->ctx, 1401 ut_params->sess); 1402 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy, 1403 ret, 0, "%d"); 1404 TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 1); 1405 TEST_ASSERT_MEMPOOL_USAGE(0); 1406 TEST_ASSERT_SESSION_COUNT(0); 1407 1408 /* 1409 * Remove session from test case parameters, so it won't be destroyed 1410 * during test case teardown. 1411 */ 1412 ut_params->sess = NULL; 1413 1414 return TEST_SUCCESS; 1415 } 1416 1417 1418 /** 1419 * rte_security_set_pkt_metadata tests 1420 */ 1421 1422 /** 1423 * Test execution of rte_security_set_pkt_metadata with NULL instance 1424 */ 1425 static int 1426 test_set_pkt_metadata_inv_context(void) 1427 { 1428 #ifdef RTE_DEBUG 1429 struct security_unittest_params *ut_params = &unittest_params; 1430 struct rte_mbuf m; 1431 int params; 1432 1433 int ret = rte_security_set_pkt_metadata(NULL, ut_params->sess, &m, 1434 ¶ms); 1435 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata, 1436 ret, -EINVAL, "%d"); 1437 TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 0); 1438 1439 return TEST_SUCCESS; 1440 #else 1441 return TEST_SKIPPED; 1442 #endif 1443 } 1444 1445 /** 1446 * Test execution of rte_security_set_pkt_metadata with invalid 1447 * security operations structure (NULL) 1448 */ 1449 static int 1450 test_set_pkt_metadata_inv_context_ops(void) 1451 { 1452 #ifdef RTE_DEBUG 1453 struct security_unittest_params *ut_params = &unittest_params; 1454 struct rte_mbuf m; 1455 int params; 1456 ut_params->ctx.ops = NULL; 1457 1458 int ret = rte_security_set_pkt_metadata(&ut_params->ctx, 1459 ut_params->sess, &m, ¶ms); 1460 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata, 1461 ret, -EINVAL, "%d"); 1462 TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 0); 1463 1464 return TEST_SUCCESS; 1465 #else 1466 return TEST_SKIPPED; 1467 #endif 1468 } 1469 1470 /** 1471 * Test execution of rte_security_set_pkt_metadata with empty 1472 * security operations 1473 */ 1474 static int 1475 test_set_pkt_metadata_inv_context_ops_fun(void) 1476 { 1477 struct security_unittest_params *ut_params = &unittest_params; 1478 struct rte_mbuf m; 1479 int params; 1480 ut_params->ctx.ops = &empty_ops; 1481 1482 int ret = rte_security_set_pkt_metadata(&ut_params->ctx, 1483 ut_params->sess, &m, ¶ms); 1484 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata, 1485 ret, -ENOTSUP, "%d"); 1486 TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 0); 1487 1488 return TEST_SUCCESS; 1489 } 1490 1491 /** 1492 * Test execution of rte_security_set_pkt_metadata with NULL sess parameter 1493 */ 1494 static int 1495 test_set_pkt_metadata_inv_session(void) 1496 { 1497 #ifdef RTE_DEBUG 1498 struct security_unittest_params *ut_params = &unittest_params; 1499 struct rte_mbuf m; 1500 int params; 1501 1502 int ret = rte_security_set_pkt_metadata(&ut_params->ctx, NULL, 1503 &m, ¶ms); 1504 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata, 1505 ret, -EINVAL, "%d"); 1506 TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 0); 1507 1508 return TEST_SUCCESS; 1509 #else 1510 return TEST_SKIPPED; 1511 #endif 1512 } 1513 1514 /** 1515 * Test execution of rte_security_set_pkt_metadata when set_pkt_metadata 1516 * security operation fails 1517 */ 1518 static int 1519 test_set_pkt_metadata_ops_failure(void) 1520 { 1521 struct security_unittest_params *ut_params = &unittest_params; 1522 struct rte_mbuf m; 1523 int params; 1524 1525 mock_set_pkt_metadata_exp.device = NULL; 1526 mock_set_pkt_metadata_exp.sess = ut_params->sess; 1527 mock_set_pkt_metadata_exp.m = &m; 1528 mock_set_pkt_metadata_exp.params = ¶ms; 1529 mock_set_pkt_metadata_exp.ret = -1; 1530 1531 int ret = rte_security_set_pkt_metadata(&ut_params->ctx, 1532 ut_params->sess, &m, ¶ms); 1533 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata, 1534 ret, -1, "%d"); 1535 TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 1); 1536 1537 return TEST_SUCCESS; 1538 } 1539 1540 /** 1541 * Test execution of rte_security_set_pkt_metadata in successful execution path 1542 */ 1543 static int 1544 test_set_pkt_metadata_success(void) 1545 { 1546 struct security_unittest_params *ut_params = &unittest_params; 1547 struct rte_mbuf m; 1548 int params; 1549 1550 mock_set_pkt_metadata_exp.device = NULL; 1551 mock_set_pkt_metadata_exp.sess = ut_params->sess; 1552 mock_set_pkt_metadata_exp.m = &m; 1553 mock_set_pkt_metadata_exp.params = ¶ms; 1554 mock_set_pkt_metadata_exp.ret = 0; 1555 1556 int ret = rte_security_set_pkt_metadata(&ut_params->ctx, 1557 ut_params->sess, &m, ¶ms); 1558 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata, 1559 ret, 0, "%d"); 1560 TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 1); 1561 1562 return TEST_SUCCESS; 1563 } 1564 1565 1566 /** 1567 * rte_security_get_userdata tests 1568 */ 1569 1570 /** 1571 * Test execution of rte_security_get_userdata with NULL instance 1572 */ 1573 static int 1574 test_get_userdata_inv_context(void) 1575 { 1576 #ifdef RTE_DEBUG 1577 uint64_t md = 0xDEADBEEF; 1578 1579 void *ret = rte_security_get_userdata(NULL, md); 1580 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_get_userdata, 1581 ret, NULL, "%p"); 1582 TEST_ASSERT_MOCK_CALLS(mock_get_userdata_exp, 0); 1583 1584 return TEST_SUCCESS; 1585 #else 1586 return TEST_SKIPPED; 1587 #endif 1588 } 1589 1590 /** 1591 * Test execution of rte_security_get_userdata with invalid 1592 * security operations structure (NULL) 1593 */ 1594 static int 1595 test_get_userdata_inv_context_ops(void) 1596 { 1597 #ifdef RTE_DEBUG 1598 struct security_unittest_params *ut_params = &unittest_params; 1599 uint64_t md = 0xDEADBEEF; 1600 ut_params->ctx.ops = NULL; 1601 1602 void *ret = rte_security_get_userdata(&ut_params->ctx, md); 1603 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_get_userdata, 1604 ret, NULL, "%p"); 1605 TEST_ASSERT_MOCK_CALLS(mock_get_userdata_exp, 0); 1606 1607 return TEST_SUCCESS; 1608 #else 1609 return TEST_SKIPPED; 1610 #endif 1611 } 1612 1613 /** 1614 * Test execution of rte_security_get_userdata with empty 1615 * security operations 1616 */ 1617 static int 1618 test_get_userdata_inv_context_ops_fun(void) 1619 { 1620 struct security_unittest_params *ut_params = &unittest_params; 1621 uint64_t md = 0xDEADBEEF; 1622 ut_params->ctx.ops = &empty_ops; 1623 1624 void *ret = rte_security_get_userdata(&ut_params->ctx, md); 1625 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_get_userdata, 1626 ret, NULL, "%p"); 1627 TEST_ASSERT_MOCK_CALLS(mock_get_userdata_exp, 0); 1628 1629 return TEST_SUCCESS; 1630 } 1631 1632 /** 1633 * Test execution of rte_security_get_userdata when get_userdata 1634 * security operation fails 1635 */ 1636 static int 1637 test_get_userdata_ops_failure(void) 1638 { 1639 struct security_unittest_params *ut_params = &unittest_params; 1640 uint64_t md = 0xDEADBEEF; 1641 void *userdata = (void *)0x7E577E57; 1642 1643 mock_get_userdata_exp.device = NULL; 1644 mock_get_userdata_exp.md = md; 1645 mock_get_userdata_exp.userdata = userdata; 1646 mock_get_userdata_exp.ret = -1; 1647 1648 void *ret = rte_security_get_userdata(&ut_params->ctx, md); 1649 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_get_userdata, 1650 ret, NULL, "%p"); 1651 TEST_ASSERT_MOCK_CALLS(mock_get_userdata_exp, 1); 1652 1653 return TEST_SUCCESS; 1654 } 1655 1656 /** 1657 * Test execution of rte_security_get_userdata in successful execution path 1658 */ 1659 static int 1660 test_get_userdata_success(void) 1661 { 1662 struct security_unittest_params *ut_params = &unittest_params; 1663 uint64_t md = 0xDEADBEEF; 1664 void *userdata = (void *)0x7E577E57; 1665 1666 mock_get_userdata_exp.device = NULL; 1667 mock_get_userdata_exp.md = md; 1668 mock_get_userdata_exp.userdata = userdata; 1669 mock_get_userdata_exp.ret = 0; 1670 1671 void *ret = rte_security_get_userdata(&ut_params->ctx, md); 1672 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_get_userdata, 1673 ret, userdata, "%p"); 1674 TEST_ASSERT_MOCK_CALLS(mock_get_userdata_exp, 1); 1675 1676 return TEST_SUCCESS; 1677 } 1678 1679 1680 /** 1681 * rte_security_capabilities_get tests 1682 */ 1683 1684 /** 1685 * Test execution of rte_security_capabilities_get with NULL instance 1686 */ 1687 static int 1688 test_capabilities_get_inv_context(void) 1689 { 1690 const struct rte_security_capability *ret; 1691 ret = rte_security_capabilities_get(NULL); 1692 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capabilities_get, 1693 ret, NULL, "%p"); 1694 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0); 1695 1696 return TEST_SUCCESS; 1697 } 1698 1699 /** 1700 * Test execution of rte_security_capabilities_get with invalid 1701 * security operations structure (NULL) 1702 */ 1703 static int 1704 test_capabilities_get_inv_context_ops(void) 1705 { 1706 struct security_unittest_params *ut_params = &unittest_params; 1707 ut_params->ctx.ops = NULL; 1708 1709 const struct rte_security_capability *ret; 1710 ret = rte_security_capabilities_get(&ut_params->ctx); 1711 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capabilities_get, 1712 ret, NULL, "%p"); 1713 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0); 1714 1715 return TEST_SUCCESS; 1716 } 1717 1718 /** 1719 * Test execution of rte_security_capabilities_get with empty 1720 * security operations 1721 */ 1722 static int 1723 test_capabilities_get_inv_context_ops_fun(void) 1724 { 1725 struct security_unittest_params *ut_params = &unittest_params; 1726 ut_params->ctx.ops = &empty_ops; 1727 1728 const struct rte_security_capability *ret; 1729 ret = rte_security_capabilities_get(&ut_params->ctx); 1730 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capabilities_get, 1731 ret, NULL, "%p"); 1732 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0); 1733 1734 return TEST_SUCCESS; 1735 } 1736 1737 /** 1738 * Test execution of rte_security_capabilities_get when capabilities_get 1739 * security operation fails 1740 */ 1741 static int 1742 test_capabilities_get_ops_failure(void) 1743 { 1744 struct security_unittest_params *ut_params = &unittest_params; 1745 1746 mock_capabilities_get_exp.device = NULL; 1747 mock_capabilities_get_exp.ret = NULL; 1748 1749 const struct rte_security_capability *ret; 1750 ret = rte_security_capabilities_get(&ut_params->ctx); 1751 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capabilities_get, 1752 ret, NULL, "%p"); 1753 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1); 1754 1755 return TEST_SUCCESS; 1756 } 1757 1758 /** 1759 * Test execution of rte_security_capabilities_get in successful execution path 1760 */ 1761 static int 1762 test_capabilities_get_success(void) 1763 { 1764 struct security_unittest_params *ut_params = &unittest_params; 1765 struct rte_security_capability capabilities; 1766 1767 mock_capabilities_get_exp.device = NULL; 1768 mock_capabilities_get_exp.ret = &capabilities; 1769 1770 const struct rte_security_capability *ret; 1771 ret = rte_security_capabilities_get(&ut_params->ctx); 1772 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capabilities_get, 1773 ret, &capabilities, "%p"); 1774 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1); 1775 1776 return TEST_SUCCESS; 1777 } 1778 1779 1780 /** 1781 * rte_security_capability_get tests 1782 */ 1783 1784 /** 1785 * Test execution of rte_security_capability_get with NULL instance 1786 */ 1787 static int 1788 test_capability_get_inv_context(void) 1789 { 1790 struct rte_security_capability_idx idx; 1791 1792 const struct rte_security_capability *ret; 1793 ret = rte_security_capability_get(NULL, &idx); 1794 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get, 1795 ret, NULL, "%p"); 1796 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0); 1797 1798 return TEST_SUCCESS; 1799 } 1800 1801 /** 1802 * Test execution of rte_security_capability_get with invalid 1803 * security operations structure (NULL) 1804 */ 1805 static int 1806 test_capability_get_inv_context_ops(void) 1807 { 1808 struct security_unittest_params *ut_params = &unittest_params; 1809 struct rte_security_capability_idx idx; 1810 ut_params->ctx.ops = NULL; 1811 1812 const struct rte_security_capability *ret; 1813 ret = rte_security_capability_get(&ut_params->ctx, &idx); 1814 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get, 1815 ret, NULL, "%p"); 1816 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0); 1817 1818 return TEST_SUCCESS; 1819 } 1820 1821 /** 1822 * Test execution of rte_security_capability_get with empty 1823 * security operations 1824 */ 1825 static int 1826 test_capability_get_inv_context_ops_fun(void) 1827 { 1828 struct security_unittest_params *ut_params = &unittest_params; 1829 struct rte_security_capability_idx idx; 1830 ut_params->ctx.ops = &empty_ops; 1831 1832 const struct rte_security_capability *ret; 1833 ret = rte_security_capability_get(&ut_params->ctx, &idx); 1834 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get, 1835 ret, NULL, "%p"); 1836 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0); 1837 1838 return TEST_SUCCESS; 1839 } 1840 1841 /** 1842 * Test execution of rte_security_capability_get with NULL idx parameter 1843 */ 1844 static int 1845 test_capability_get_inv_idx(void) 1846 { 1847 struct security_unittest_params *ut_params = &unittest_params; 1848 1849 const struct rte_security_capability *ret; 1850 ret = rte_security_capability_get(&ut_params->ctx, NULL); 1851 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get, 1852 ret, NULL, "%p"); 1853 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0); 1854 1855 return TEST_SUCCESS; 1856 } 1857 1858 /** 1859 * Test execution of rte_security_capability_get when capabilities_get 1860 * security operation fails 1861 */ 1862 static int 1863 test_capability_get_ops_failure(void) 1864 { 1865 struct security_unittest_params *ut_params = &unittest_params; 1866 struct rte_security_capability_idx idx; 1867 1868 mock_capabilities_get_exp.device = NULL; 1869 mock_capabilities_get_exp.ret = NULL; 1870 1871 const struct rte_security_capability *ret; 1872 ret = rte_security_capability_get(&ut_params->ctx, &idx); 1873 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get, 1874 ret, NULL, "%p"); 1875 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1); 1876 1877 return TEST_SUCCESS; 1878 } 1879 1880 /** 1881 * Test execution of rte_security_capability_get when capabilities table 1882 * is empty (contains only RTE_SECURITY_ACTION_TYPE_NONE ending entry) 1883 */ 1884 static int 1885 test_capability_get_empty_table(void) 1886 { 1887 struct security_unittest_params *ut_params = &unittest_params; 1888 struct rte_security_capability_idx idx; 1889 struct rte_security_capability capabilities[] = { 1890 { 1891 .action = RTE_SECURITY_ACTION_TYPE_NONE, 1892 }, 1893 }; 1894 1895 mock_capabilities_get_exp.device = NULL; 1896 mock_capabilities_get_exp.ret = capabilities; 1897 1898 const struct rte_security_capability *ret; 1899 ret = rte_security_capability_get(&ut_params->ctx, &idx); 1900 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get, 1901 ret, NULL, "%p"); 1902 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1); 1903 1904 return TEST_SUCCESS; 1905 } 1906 1907 /** 1908 * Test execution of rte_security_capability_get when capabilities table 1909 * does not contain entry with matching action 1910 */ 1911 static int 1912 test_capability_get_no_matching_action(void) 1913 { 1914 struct security_unittest_params *ut_params = &unittest_params; 1915 struct rte_security_capability_idx idx = { 1916 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 1917 }; 1918 struct rte_security_capability capabilities[] = { 1919 { 1920 .action = RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO, 1921 }, 1922 { 1923 .action = RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL, 1924 }, 1925 { 1926 .action = RTE_SECURITY_ACTION_TYPE_NONE, 1927 }, 1928 }; 1929 1930 mock_capabilities_get_exp.device = NULL; 1931 mock_capabilities_get_exp.ret = capabilities; 1932 1933 const struct rte_security_capability *ret; 1934 ret = rte_security_capability_get(&ut_params->ctx, &idx); 1935 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get, 1936 ret, NULL, "%p"); 1937 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1); 1938 1939 return TEST_SUCCESS; 1940 } 1941 1942 /** 1943 * Test execution of rte_security_capability_get when capabilities table 1944 * does not contain entry with matching protocol 1945 */ 1946 static int 1947 test_capability_get_no_matching_protocol(void) 1948 { 1949 struct security_unittest_params *ut_params = &unittest_params; 1950 struct rte_security_capability_idx idx = { 1951 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 1952 .protocol = RTE_SECURITY_PROTOCOL_IPSEC, 1953 }; 1954 struct rte_security_capability capabilities[] = { 1955 { 1956 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 1957 .protocol = RTE_SECURITY_PROTOCOL_MACSEC, 1958 }, 1959 { 1960 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 1961 .protocol = RTE_SECURITY_PROTOCOL_PDCP, 1962 }, 1963 { 1964 .action = RTE_SECURITY_ACTION_TYPE_NONE, 1965 }, 1966 }; 1967 1968 mock_capabilities_get_exp.device = NULL; 1969 mock_capabilities_get_exp.ret = capabilities; 1970 1971 const struct rte_security_capability *ret; 1972 ret = rte_security_capability_get(&ut_params->ctx, &idx); 1973 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get, 1974 ret, NULL, "%p"); 1975 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1); 1976 1977 return TEST_SUCCESS; 1978 } 1979 1980 /** 1981 * Test execution of rte_security_capability_get when macsec protocol 1982 * is searched and capabilities table contain proper entry. 1983 * However macsec records search is not supported in rte_security. 1984 */ 1985 static int 1986 test_capability_get_no_support_for_macsec(void) 1987 { 1988 struct security_unittest_params *ut_params = &unittest_params; 1989 struct rte_security_capability_idx idx = { 1990 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 1991 .protocol = RTE_SECURITY_PROTOCOL_MACSEC, 1992 }; 1993 struct rte_security_capability capabilities[] = { 1994 { 1995 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 1996 .protocol = RTE_SECURITY_PROTOCOL_MACSEC, 1997 }, 1998 { 1999 .action = RTE_SECURITY_ACTION_TYPE_NONE, 2000 }, 2001 }; 2002 2003 mock_capabilities_get_exp.device = NULL; 2004 mock_capabilities_get_exp.ret = capabilities; 2005 2006 const struct rte_security_capability *ret; 2007 ret = rte_security_capability_get(&ut_params->ctx, &idx); 2008 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get, 2009 ret, NULL, "%p"); 2010 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1); 2011 2012 return TEST_SUCCESS; 2013 } 2014 2015 /** 2016 * Test execution of rte_security_capability_get when capabilities table 2017 * does not contain entry with matching ipsec proto field 2018 */ 2019 static int 2020 test_capability_get_ipsec_mismatch_proto(void) 2021 { 2022 struct security_unittest_params *ut_params = &unittest_params; 2023 struct rte_security_capability_idx idx = { 2024 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 2025 .protocol = RTE_SECURITY_PROTOCOL_IPSEC, 2026 .ipsec = { 2027 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP, 2028 }, 2029 }; 2030 struct rte_security_capability capabilities[] = { 2031 { 2032 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 2033 .protocol = RTE_SECURITY_PROTOCOL_IPSEC, 2034 .ipsec = { 2035 .proto = RTE_SECURITY_IPSEC_SA_PROTO_AH, 2036 }, 2037 }, 2038 { 2039 .action = RTE_SECURITY_ACTION_TYPE_NONE, 2040 }, 2041 }; 2042 2043 mock_capabilities_get_exp.device = NULL; 2044 mock_capabilities_get_exp.ret = capabilities; 2045 2046 const struct rte_security_capability *ret; 2047 ret = rte_security_capability_get(&ut_params->ctx, &idx); 2048 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get, 2049 ret, NULL, "%p"); 2050 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1); 2051 2052 return TEST_SUCCESS; 2053 } 2054 2055 /** 2056 * Test execution of rte_security_capability_get when capabilities table 2057 * does not contain entry with matching ipsec mode field 2058 */ 2059 static int 2060 test_capability_get_ipsec_mismatch_mode(void) 2061 { 2062 struct security_unittest_params *ut_params = &unittest_params; 2063 struct rte_security_capability_idx idx = { 2064 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 2065 .protocol = RTE_SECURITY_PROTOCOL_IPSEC, 2066 .ipsec = { 2067 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP, 2068 .mode = RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT, 2069 }, 2070 }; 2071 struct rte_security_capability capabilities[] = { 2072 { 2073 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 2074 .protocol = RTE_SECURITY_PROTOCOL_IPSEC, 2075 .ipsec = { 2076 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP, 2077 .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL, 2078 }, 2079 }, 2080 { 2081 .action = RTE_SECURITY_ACTION_TYPE_NONE, 2082 }, 2083 }; 2084 2085 mock_capabilities_get_exp.device = NULL; 2086 mock_capabilities_get_exp.ret = capabilities; 2087 2088 const struct rte_security_capability *ret; 2089 ret = rte_security_capability_get(&ut_params->ctx, &idx); 2090 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get, 2091 ret, NULL, "%p"); 2092 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1); 2093 2094 return TEST_SUCCESS; 2095 } 2096 2097 /** 2098 * Test execution of rte_security_capability_get when capabilities table 2099 * does not contain entry with matching ipsec direction field 2100 */ 2101 static int 2102 test_capability_get_ipsec_mismatch_dir(void) 2103 { 2104 struct security_unittest_params *ut_params = &unittest_params; 2105 struct rte_security_capability_idx idx = { 2106 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 2107 .protocol = RTE_SECURITY_PROTOCOL_IPSEC, 2108 .ipsec = { 2109 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP, 2110 .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL, 2111 .direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS, 2112 }, 2113 }; 2114 struct rte_security_capability capabilities[] = { 2115 { 2116 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 2117 .protocol = RTE_SECURITY_PROTOCOL_IPSEC, 2118 .ipsec = { 2119 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP, 2120 .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL, 2121 .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS, 2122 }, 2123 }, 2124 { 2125 .action = RTE_SECURITY_ACTION_TYPE_NONE, 2126 }, 2127 }; 2128 2129 mock_capabilities_get_exp.device = NULL; 2130 mock_capabilities_get_exp.ret = capabilities; 2131 2132 const struct rte_security_capability *ret; 2133 ret = rte_security_capability_get(&ut_params->ctx, &idx); 2134 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get, 2135 ret, NULL, "%p"); 2136 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1); 2137 2138 return TEST_SUCCESS; 2139 } 2140 2141 /** 2142 * Test execution of rte_security_capability_get when capabilities table 2143 * contains matching ipsec entry 2144 */ 2145 static int 2146 test_capability_get_ipsec_match(void) 2147 { 2148 struct security_unittest_params *ut_params = &unittest_params; 2149 struct rte_security_capability_idx idx = { 2150 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 2151 .protocol = RTE_SECURITY_PROTOCOL_IPSEC, 2152 .ipsec = { 2153 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP, 2154 .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL, 2155 .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS, 2156 }, 2157 }; 2158 struct rte_security_capability capabilities[] = { 2159 { 2160 .action = RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO, 2161 }, 2162 { 2163 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 2164 .protocol = RTE_SECURITY_PROTOCOL_IPSEC, 2165 .ipsec = { 2166 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP, 2167 .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL, 2168 .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS, 2169 }, 2170 }, 2171 { 2172 .action = RTE_SECURITY_ACTION_TYPE_NONE, 2173 }, 2174 }; 2175 2176 mock_capabilities_get_exp.device = NULL; 2177 mock_capabilities_get_exp.ret = capabilities; 2178 2179 const struct rte_security_capability *ret; 2180 ret = rte_security_capability_get(&ut_params->ctx, &idx); 2181 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get, 2182 ret, &capabilities[1], "%p"); 2183 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1); 2184 2185 return TEST_SUCCESS; 2186 } 2187 2188 /** 2189 * Test execution of rte_security_capability_get when capabilities table 2190 * does not contain entry with matching pdcp domain field 2191 */ 2192 static int 2193 test_capability_get_pdcp_mismatch_domain(void) 2194 { 2195 struct security_unittest_params *ut_params = &unittest_params; 2196 struct rte_security_capability_idx idx = { 2197 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 2198 .protocol = RTE_SECURITY_PROTOCOL_PDCP, 2199 .pdcp = { 2200 .domain = RTE_SECURITY_PDCP_MODE_CONTROL, 2201 }, 2202 }; 2203 struct rte_security_capability capabilities[] = { 2204 { 2205 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 2206 .protocol = RTE_SECURITY_PROTOCOL_PDCP, 2207 .pdcp = { 2208 .domain = RTE_SECURITY_PDCP_MODE_DATA, 2209 }, 2210 }, 2211 { 2212 .action = RTE_SECURITY_ACTION_TYPE_NONE, 2213 }, 2214 }; 2215 2216 mock_capabilities_get_exp.device = NULL; 2217 mock_capabilities_get_exp.ret = capabilities; 2218 2219 const struct rte_security_capability *ret; 2220 ret = rte_security_capability_get(&ut_params->ctx, &idx); 2221 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get, 2222 ret, NULL, "%p"); 2223 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1); 2224 2225 return TEST_SUCCESS; 2226 } 2227 2228 /** 2229 * Test execution of rte_security_capability_get when capabilities table 2230 * contains matching pdcp entry 2231 */ 2232 static int 2233 test_capability_get_pdcp_match(void) 2234 { 2235 struct security_unittest_params *ut_params = &unittest_params; 2236 struct rte_security_capability_idx idx = { 2237 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 2238 .protocol = RTE_SECURITY_PROTOCOL_PDCP, 2239 .pdcp = { 2240 .domain = RTE_SECURITY_PDCP_MODE_CONTROL, 2241 }, 2242 }; 2243 struct rte_security_capability capabilities[] = { 2244 { 2245 .action = RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO, 2246 }, 2247 { 2248 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL, 2249 .protocol = RTE_SECURITY_PROTOCOL_PDCP, 2250 .pdcp = { 2251 .domain = RTE_SECURITY_PDCP_MODE_CONTROL, 2252 }, 2253 }, 2254 { 2255 .action = RTE_SECURITY_ACTION_TYPE_NONE, 2256 }, 2257 }; 2258 2259 mock_capabilities_get_exp.device = NULL; 2260 mock_capabilities_get_exp.ret = capabilities; 2261 2262 const struct rte_security_capability *ret; 2263 ret = rte_security_capability_get(&ut_params->ctx, &idx); 2264 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get, 2265 ret, &capabilities[1], "%p"); 2266 TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1); 2267 2268 return TEST_SUCCESS; 2269 } 2270 2271 /** 2272 * Declaration of testcases 2273 */ 2274 static struct unit_test_suite security_testsuite = { 2275 .suite_name = "generic security", 2276 .setup = testsuite_setup, 2277 .teardown = testsuite_teardown, 2278 .unit_test_cases = { 2279 TEST_CASE_ST(ut_setup, ut_teardown, 2280 test_session_create_inv_context), 2281 TEST_CASE_ST(ut_setup, ut_teardown, 2282 test_session_create_inv_context_ops), 2283 TEST_CASE_ST(ut_setup, ut_teardown, 2284 test_session_create_inv_context_ops_fun), 2285 TEST_CASE_ST(ut_setup, ut_teardown, 2286 test_session_create_inv_configuration), 2287 TEST_CASE_ST(ut_setup, ut_teardown, 2288 test_session_create_inv_mempool), 2289 TEST_CASE_ST(ut_setup, ut_teardown, 2290 test_session_create_mempool_empty), 2291 TEST_CASE_ST(ut_setup, ut_teardown, 2292 test_session_create_ops_failure), 2293 TEST_CASE_ST(ut_setup, ut_teardown, 2294 test_session_create_success), 2295 2296 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2297 test_session_update_inv_context), 2298 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2299 test_session_update_inv_context_ops), 2300 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2301 test_session_update_inv_context_ops_fun), 2302 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2303 test_session_update_inv_configuration), 2304 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2305 test_session_update_inv_session), 2306 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2307 test_session_update_ops_failure), 2308 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2309 test_session_update_success), 2310 2311 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2312 test_session_get_size_inv_context), 2313 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2314 test_session_get_size_inv_context_ops), 2315 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2316 test_session_get_size_inv_context_ops_fun), 2317 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2318 test_session_get_size_ops_failure), 2319 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2320 test_session_get_size_success), 2321 2322 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2323 test_session_stats_get_inv_context), 2324 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2325 test_session_stats_get_inv_context_ops), 2326 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2327 test_session_stats_get_inv_context_ops_fun), 2328 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2329 test_session_stats_get_inv_stats), 2330 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2331 test_session_stats_get_ops_failure), 2332 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2333 test_session_stats_get_success), 2334 2335 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2336 test_session_destroy_inv_context), 2337 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2338 test_session_destroy_inv_context_ops), 2339 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2340 test_session_destroy_inv_context_ops_fun), 2341 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2342 test_session_destroy_inv_session), 2343 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2344 test_session_destroy_ops_failure), 2345 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2346 test_session_destroy_success), 2347 2348 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2349 test_set_pkt_metadata_inv_context), 2350 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2351 test_set_pkt_metadata_inv_context_ops), 2352 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2353 test_set_pkt_metadata_inv_context_ops_fun), 2354 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2355 test_set_pkt_metadata_inv_session), 2356 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2357 test_set_pkt_metadata_ops_failure), 2358 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2359 test_set_pkt_metadata_success), 2360 2361 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2362 test_get_userdata_inv_context), 2363 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2364 test_get_userdata_inv_context_ops), 2365 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2366 test_get_userdata_inv_context_ops_fun), 2367 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2368 test_get_userdata_ops_failure), 2369 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2370 test_get_userdata_success), 2371 2372 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2373 test_capabilities_get_inv_context), 2374 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2375 test_capabilities_get_inv_context_ops), 2376 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2377 test_capabilities_get_inv_context_ops_fun), 2378 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2379 test_capabilities_get_ops_failure), 2380 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2381 test_capabilities_get_success), 2382 2383 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2384 test_capability_get_inv_context), 2385 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2386 test_capability_get_inv_context_ops), 2387 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2388 test_capability_get_inv_context_ops_fun), 2389 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2390 test_capability_get_inv_idx), 2391 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2392 test_capability_get_ops_failure), 2393 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2394 test_capability_get_empty_table), 2395 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2396 test_capability_get_no_matching_action), 2397 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2398 test_capability_get_no_matching_protocol), 2399 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2400 test_capability_get_no_support_for_macsec), 2401 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2402 test_capability_get_ipsec_mismatch_proto), 2403 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2404 test_capability_get_ipsec_mismatch_mode), 2405 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2406 test_capability_get_ipsec_mismatch_dir), 2407 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2408 test_capability_get_ipsec_match), 2409 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2410 test_capability_get_pdcp_mismatch_domain), 2411 TEST_CASE_ST(ut_setup_with_session, ut_teardown, 2412 test_capability_get_pdcp_match), 2413 2414 TEST_CASES_END() /**< NULL terminate unit test array */ 2415 } 2416 }; 2417 2418 static int 2419 test_security(void) 2420 { 2421 rte_log_set_global_level(RTE_LOG_DEBUG); 2422 rte_log_set_level(RTE_LOGTYPE_EAL, RTE_LOG_DEBUG); 2423 2424 return unit_test_suite_runner(&security_testsuite); 2425 } 2426 2427 REGISTER_TEST_COMMAND(security_autotest, test_security); 2428