kvargs: fix invalid token parsing on FreeBSD
[dpdk.git] / app / test / test_security.c
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                         &params);
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, &params);
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 #ifdef RTE_DEBUG
1478         struct security_unittest_params *ut_params = &unittest_params;
1479         struct rte_mbuf m;
1480         int params;
1481         ut_params->ctx.ops = &empty_ops;
1482
1483         int ret = rte_security_set_pkt_metadata(&ut_params->ctx,
1484                         ut_params->sess, &m, &params);
1485         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata,
1486                         ret, -ENOTSUP, "%d");
1487         TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 0);
1488
1489         return TEST_SUCCESS;
1490 #else
1491         return TEST_SKIPPED;
1492 #endif
1493 }
1494
1495 /**
1496  * Test execution of rte_security_set_pkt_metadata with NULL sess parameter
1497  */
1498 static int
1499 test_set_pkt_metadata_inv_session(void)
1500 {
1501 #ifdef RTE_DEBUG
1502         struct security_unittest_params *ut_params = &unittest_params;
1503         struct rte_mbuf m;
1504         int params;
1505
1506         int ret = rte_security_set_pkt_metadata(&ut_params->ctx, NULL,
1507                         &m, &params);
1508         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata,
1509                         ret, -EINVAL, "%d");
1510         TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 0);
1511
1512         return TEST_SUCCESS;
1513 #else
1514         return TEST_SKIPPED;
1515 #endif
1516 }
1517
1518 /**
1519  * Test execution of rte_security_set_pkt_metadata when set_pkt_metadata
1520  * security operation fails
1521  */
1522 static int
1523 test_set_pkt_metadata_ops_failure(void)
1524 {
1525         struct security_unittest_params *ut_params = &unittest_params;
1526         struct rte_mbuf m;
1527         int params;
1528
1529         mock_set_pkt_metadata_exp.device = NULL;
1530         mock_set_pkt_metadata_exp.sess = ut_params->sess;
1531         mock_set_pkt_metadata_exp.m = &m;
1532         mock_set_pkt_metadata_exp.params = &params;
1533         mock_set_pkt_metadata_exp.ret = -1;
1534
1535         int ret = rte_security_set_pkt_metadata(&ut_params->ctx,
1536                         ut_params->sess, &m, &params);
1537         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata,
1538                         ret, -1, "%d");
1539         TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 1);
1540
1541         return TEST_SUCCESS;
1542 }
1543
1544 /**
1545  * Test execution of rte_security_set_pkt_metadata in successful execution path
1546  */
1547 static int
1548 test_set_pkt_metadata_success(void)
1549 {
1550         struct security_unittest_params *ut_params = &unittest_params;
1551         struct rte_mbuf m;
1552         int params;
1553
1554         mock_set_pkt_metadata_exp.device = NULL;
1555         mock_set_pkt_metadata_exp.sess = ut_params->sess;
1556         mock_set_pkt_metadata_exp.m = &m;
1557         mock_set_pkt_metadata_exp.params = &params;
1558         mock_set_pkt_metadata_exp.ret = 0;
1559
1560         int ret = rte_security_set_pkt_metadata(&ut_params->ctx,
1561                         ut_params->sess, &m, &params);
1562         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata,
1563                         ret, 0, "%d");
1564         TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 1);
1565
1566         return TEST_SUCCESS;
1567 }
1568
1569
1570 /**
1571  * rte_security_get_userdata tests
1572  */
1573
1574 /**
1575  * Test execution of rte_security_get_userdata with NULL instance
1576  */
1577 static int
1578 test_get_userdata_inv_context(void)
1579 {
1580 #ifdef RTE_DEBUG
1581         uint64_t md = 0xDEADBEEF;
1582
1583         void *ret = rte_security_get_userdata(NULL, md);
1584         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_get_userdata,
1585                         ret, NULL, "%p");
1586         TEST_ASSERT_MOCK_CALLS(mock_get_userdata_exp, 0);
1587
1588         return TEST_SUCCESS;
1589 #else
1590         return TEST_SKIPPED;
1591 #endif
1592 }
1593
1594 /**
1595  * Test execution of rte_security_get_userdata with invalid
1596  * security operations structure (NULL)
1597  */
1598 static int
1599 test_get_userdata_inv_context_ops(void)
1600 {
1601 #ifdef RTE_DEBUG
1602         struct security_unittest_params *ut_params = &unittest_params;
1603         uint64_t md = 0xDEADBEEF;
1604         ut_params->ctx.ops = NULL;
1605
1606         void *ret = rte_security_get_userdata(&ut_params->ctx, md);
1607         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_get_userdata,
1608                         ret, NULL, "%p");
1609         TEST_ASSERT_MOCK_CALLS(mock_get_userdata_exp, 0);
1610
1611         return TEST_SUCCESS;
1612 #else
1613         return TEST_SKIPPED;
1614 #endif
1615 }
1616
1617 /**
1618  * Test execution of rte_security_get_userdata with empty
1619  * security operations
1620  */
1621 static int
1622 test_get_userdata_inv_context_ops_fun(void)
1623 {
1624 #ifdef RTE_DEBUG
1625         struct security_unittest_params *ut_params = &unittest_params;
1626         uint64_t md = 0xDEADBEEF;
1627         ut_params->ctx.ops = &empty_ops;
1628
1629         void *ret = rte_security_get_userdata(&ut_params->ctx, md);
1630         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_get_userdata,
1631                         ret, NULL, "%p");
1632         TEST_ASSERT_MOCK_CALLS(mock_get_userdata_exp, 0);
1633
1634         return TEST_SUCCESS;
1635 #else
1636         return TEST_SKIPPED;
1637 #endif
1638 }
1639
1640 /**
1641  * Test execution of rte_security_get_userdata when get_userdata
1642  * security operation fails
1643  */
1644 static int
1645 test_get_userdata_ops_failure(void)
1646 {
1647         struct security_unittest_params *ut_params = &unittest_params;
1648         uint64_t md = 0xDEADBEEF;
1649         void *userdata = (void *)0x7E577E57;
1650
1651         mock_get_userdata_exp.device = NULL;
1652         mock_get_userdata_exp.md = md;
1653         mock_get_userdata_exp.userdata = userdata;
1654         mock_get_userdata_exp.ret = -1;
1655
1656         void *ret = rte_security_get_userdata(&ut_params->ctx, md);
1657         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_get_userdata,
1658                         ret, NULL, "%p");
1659         TEST_ASSERT_MOCK_CALLS(mock_get_userdata_exp, 1);
1660
1661         return TEST_SUCCESS;
1662 }
1663
1664 /**
1665  * Test execution of rte_security_get_userdata in successful execution path
1666  */
1667 static int
1668 test_get_userdata_success(void)
1669 {
1670         struct security_unittest_params *ut_params = &unittest_params;
1671         uint64_t md = 0xDEADBEEF;
1672         void *userdata = (void *)0x7E577E57;
1673
1674         mock_get_userdata_exp.device = NULL;
1675         mock_get_userdata_exp.md = md;
1676         mock_get_userdata_exp.userdata = userdata;
1677         mock_get_userdata_exp.ret = 0;
1678
1679         void *ret = rte_security_get_userdata(&ut_params->ctx, md);
1680         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_get_userdata,
1681                         ret, userdata, "%p");
1682         TEST_ASSERT_MOCK_CALLS(mock_get_userdata_exp, 1);
1683
1684         return TEST_SUCCESS;
1685 }
1686
1687
1688 /**
1689  * rte_security_capabilities_get tests
1690  */
1691
1692 /**
1693  * Test execution of rte_security_capabilities_get with NULL instance
1694  */
1695 static int
1696 test_capabilities_get_inv_context(void)
1697 {
1698         const struct rte_security_capability *ret;
1699         ret = rte_security_capabilities_get(NULL);
1700         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capabilities_get,
1701                         ret, NULL, "%p");
1702         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0);
1703
1704         return TEST_SUCCESS;
1705 }
1706
1707 /**
1708  * Test execution of rte_security_capabilities_get with invalid
1709  * security operations structure (NULL)
1710  */
1711 static int
1712 test_capabilities_get_inv_context_ops(void)
1713 {
1714         struct security_unittest_params *ut_params = &unittest_params;
1715         ut_params->ctx.ops = NULL;
1716
1717         const struct rte_security_capability *ret;
1718         ret = rte_security_capabilities_get(&ut_params->ctx);
1719         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capabilities_get,
1720                         ret, NULL, "%p");
1721         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0);
1722
1723         return TEST_SUCCESS;
1724 }
1725
1726 /**
1727  * Test execution of rte_security_capabilities_get with empty
1728  * security operations
1729  */
1730 static int
1731 test_capabilities_get_inv_context_ops_fun(void)
1732 {
1733         struct security_unittest_params *ut_params = &unittest_params;
1734         ut_params->ctx.ops = &empty_ops;
1735
1736         const struct rte_security_capability *ret;
1737         ret = rte_security_capabilities_get(&ut_params->ctx);
1738         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capabilities_get,
1739                         ret, NULL, "%p");
1740         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0);
1741
1742         return TEST_SUCCESS;
1743 }
1744
1745 /**
1746  * Test execution of rte_security_capabilities_get when capabilities_get
1747  * security operation fails
1748  */
1749 static int
1750 test_capabilities_get_ops_failure(void)
1751 {
1752         struct security_unittest_params *ut_params = &unittest_params;
1753
1754         mock_capabilities_get_exp.device = NULL;
1755         mock_capabilities_get_exp.ret = NULL;
1756
1757         const struct rte_security_capability *ret;
1758         ret = rte_security_capabilities_get(&ut_params->ctx);
1759         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capabilities_get,
1760                         ret, NULL, "%p");
1761         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
1762
1763         return TEST_SUCCESS;
1764 }
1765
1766 /**
1767  * Test execution of rte_security_capabilities_get in successful execution path
1768  */
1769 static int
1770 test_capabilities_get_success(void)
1771 {
1772         struct security_unittest_params *ut_params = &unittest_params;
1773         struct rte_security_capability capabilities;
1774
1775         mock_capabilities_get_exp.device = NULL;
1776         mock_capabilities_get_exp.ret = &capabilities;
1777
1778         const struct rte_security_capability *ret;
1779         ret = rte_security_capabilities_get(&ut_params->ctx);
1780         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capabilities_get,
1781                         ret, &capabilities, "%p");
1782         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
1783
1784         return TEST_SUCCESS;
1785 }
1786
1787
1788 /**
1789  * rte_security_capability_get tests
1790  */
1791
1792 /**
1793  * Test execution of rte_security_capability_get with NULL instance
1794  */
1795 static int
1796 test_capability_get_inv_context(void)
1797 {
1798         struct rte_security_capability_idx idx;
1799
1800         const struct rte_security_capability *ret;
1801         ret = rte_security_capability_get(NULL, &idx);
1802         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1803                         ret, NULL, "%p");
1804         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0);
1805
1806         return TEST_SUCCESS;
1807 }
1808
1809 /**
1810  * Test execution of rte_security_capability_get with invalid
1811  * security operations structure (NULL)
1812  */
1813 static int
1814 test_capability_get_inv_context_ops(void)
1815 {
1816         struct security_unittest_params *ut_params = &unittest_params;
1817         struct rte_security_capability_idx idx;
1818         ut_params->ctx.ops = NULL;
1819
1820         const struct rte_security_capability *ret;
1821         ret = rte_security_capability_get(&ut_params->ctx, &idx);
1822         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1823                         ret, NULL, "%p");
1824         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0);
1825
1826         return TEST_SUCCESS;
1827 }
1828
1829 /**
1830  * Test execution of rte_security_capability_get with empty
1831  * security operations
1832  */
1833 static int
1834 test_capability_get_inv_context_ops_fun(void)
1835 {
1836         struct security_unittest_params *ut_params = &unittest_params;
1837         struct rte_security_capability_idx idx;
1838         ut_params->ctx.ops = &empty_ops;
1839
1840         const struct rte_security_capability *ret;
1841         ret = rte_security_capability_get(&ut_params->ctx, &idx);
1842         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1843                         ret, NULL, "%p");
1844         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0);
1845
1846         return TEST_SUCCESS;
1847 }
1848
1849 /**
1850  * Test execution of rte_security_capability_get with NULL idx parameter
1851  */
1852 static int
1853 test_capability_get_inv_idx(void)
1854 {
1855         struct security_unittest_params *ut_params = &unittest_params;
1856
1857         const struct rte_security_capability *ret;
1858         ret = rte_security_capability_get(&ut_params->ctx, NULL);
1859         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1860                         ret, NULL, "%p");
1861         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0);
1862
1863         return TEST_SUCCESS;
1864 }
1865
1866 /**
1867  * Test execution of rte_security_capability_get when capabilities_get
1868  * security operation fails
1869  */
1870 static int
1871 test_capability_get_ops_failure(void)
1872 {
1873         struct security_unittest_params *ut_params = &unittest_params;
1874         struct rte_security_capability_idx idx;
1875
1876         mock_capabilities_get_exp.device = NULL;
1877         mock_capabilities_get_exp.ret = NULL;
1878
1879         const struct rte_security_capability *ret;
1880         ret = rte_security_capability_get(&ut_params->ctx, &idx);
1881         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1882                         ret, NULL, "%p");
1883         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
1884
1885         return TEST_SUCCESS;
1886 }
1887
1888 /**
1889  * Test execution of rte_security_capability_get when capabilities table
1890  * is empty (contains only RTE_SECURITY_ACTION_TYPE_NONE ending entry)
1891  */
1892 static int
1893 test_capability_get_empty_table(void)
1894 {
1895         struct security_unittest_params *ut_params = &unittest_params;
1896         struct rte_security_capability_idx idx;
1897         struct rte_security_capability capabilities[] = {
1898                 {
1899                         .action = RTE_SECURITY_ACTION_TYPE_NONE,
1900                 },
1901         };
1902
1903         mock_capabilities_get_exp.device = NULL;
1904         mock_capabilities_get_exp.ret = capabilities;
1905
1906         const struct rte_security_capability *ret;
1907         ret = rte_security_capability_get(&ut_params->ctx, &idx);
1908         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1909                         ret, NULL, "%p");
1910         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
1911
1912         return TEST_SUCCESS;
1913 }
1914
1915 /**
1916  * Test execution of rte_security_capability_get when capabilities table
1917  * does not contain entry with matching action
1918  */
1919 static int
1920 test_capability_get_no_matching_action(void)
1921 {
1922         struct security_unittest_params *ut_params = &unittest_params;
1923         struct rte_security_capability_idx idx = {
1924                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1925         };
1926         struct rte_security_capability capabilities[] = {
1927                 {
1928                         .action = RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
1929                 },
1930                 {
1931                         .action = RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
1932                 },
1933                 {
1934                         .action = RTE_SECURITY_ACTION_TYPE_NONE,
1935                 },
1936         };
1937
1938         mock_capabilities_get_exp.device = NULL;
1939         mock_capabilities_get_exp.ret = capabilities;
1940
1941         const struct rte_security_capability *ret;
1942         ret = rte_security_capability_get(&ut_params->ctx, &idx);
1943         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1944                         ret, NULL, "%p");
1945         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
1946
1947         return TEST_SUCCESS;
1948 }
1949
1950 /**
1951  * Test execution of rte_security_capability_get when capabilities table
1952  * does not contain entry with matching protocol
1953  */
1954 static int
1955 test_capability_get_no_matching_protocol(void)
1956 {
1957         struct security_unittest_params *ut_params = &unittest_params;
1958         struct rte_security_capability_idx idx = {
1959                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1960                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
1961         };
1962         struct rte_security_capability capabilities[] = {
1963                 {
1964                         .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1965                         .protocol = RTE_SECURITY_PROTOCOL_MACSEC,
1966                 },
1967                 {
1968                         .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1969                         .protocol = RTE_SECURITY_PROTOCOL_PDCP,
1970                 },
1971                 {
1972                         .action = RTE_SECURITY_ACTION_TYPE_NONE,
1973                 },
1974         };
1975
1976         mock_capabilities_get_exp.device = NULL;
1977         mock_capabilities_get_exp.ret = capabilities;
1978
1979         const struct rte_security_capability *ret;
1980         ret = rte_security_capability_get(&ut_params->ctx, &idx);
1981         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1982                         ret, NULL, "%p");
1983         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
1984
1985         return TEST_SUCCESS;
1986 }
1987
1988 /**
1989  * Test execution of rte_security_capability_get when macsec protocol
1990  * is searched and capabilities table contain proper entry.
1991  * However macsec records search is not supported in rte_security.
1992  */
1993 static int
1994 test_capability_get_no_support_for_macsec(void)
1995 {
1996         struct security_unittest_params *ut_params = &unittest_params;
1997         struct rte_security_capability_idx idx = {
1998                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1999                 .protocol = RTE_SECURITY_PROTOCOL_MACSEC,
2000         };
2001         struct rte_security_capability capabilities[] = {
2002                 {
2003                         .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2004                         .protocol = RTE_SECURITY_PROTOCOL_MACSEC,
2005                 },
2006                 {
2007                         .action = RTE_SECURITY_ACTION_TYPE_NONE,
2008                 },
2009         };
2010
2011         mock_capabilities_get_exp.device = NULL;
2012         mock_capabilities_get_exp.ret = capabilities;
2013
2014         const struct rte_security_capability *ret;
2015         ret = rte_security_capability_get(&ut_params->ctx, &idx);
2016         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
2017                         ret, NULL, "%p");
2018         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
2019
2020         return TEST_SUCCESS;
2021 }
2022
2023 /**
2024  * Test execution of rte_security_capability_get when capabilities table
2025  * does not contain entry with matching ipsec proto field
2026  */
2027 static int
2028 test_capability_get_ipsec_mismatch_proto(void)
2029 {
2030         struct security_unittest_params *ut_params = &unittest_params;
2031         struct rte_security_capability_idx idx = {
2032                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2033                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
2034                 .ipsec = {
2035                         .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
2036                 },
2037         };
2038         struct rte_security_capability capabilities[] = {
2039                 {
2040                         .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2041                         .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
2042                         .ipsec = {
2043                                 .proto = RTE_SECURITY_IPSEC_SA_PROTO_AH,
2044                         },
2045                 },
2046                 {
2047                         .action = RTE_SECURITY_ACTION_TYPE_NONE,
2048                 },
2049         };
2050
2051         mock_capabilities_get_exp.device = NULL;
2052         mock_capabilities_get_exp.ret = capabilities;
2053
2054         const struct rte_security_capability *ret;
2055         ret = rte_security_capability_get(&ut_params->ctx, &idx);
2056         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
2057                         ret, NULL, "%p");
2058         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
2059
2060         return TEST_SUCCESS;
2061 }
2062
2063 /**
2064  * Test execution of rte_security_capability_get when capabilities table
2065  * does not contain entry with matching ipsec mode field
2066  */
2067 static int
2068 test_capability_get_ipsec_mismatch_mode(void)
2069 {
2070         struct security_unittest_params *ut_params = &unittest_params;
2071         struct rte_security_capability_idx idx = {
2072                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2073                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
2074                 .ipsec = {
2075                         .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
2076                         .mode = RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
2077                 },
2078         };
2079         struct rte_security_capability capabilities[] = {
2080                 {
2081                         .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2082                         .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
2083                         .ipsec = {
2084                                 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
2085                                 .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
2086                         },
2087                 },
2088                 {
2089                         .action = RTE_SECURITY_ACTION_TYPE_NONE,
2090                 },
2091         };
2092
2093         mock_capabilities_get_exp.device = NULL;
2094         mock_capabilities_get_exp.ret = capabilities;
2095
2096         const struct rte_security_capability *ret;
2097         ret = rte_security_capability_get(&ut_params->ctx, &idx);
2098         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
2099                         ret, NULL, "%p");
2100         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
2101
2102         return TEST_SUCCESS;
2103 }
2104
2105 /**
2106  * Test execution of rte_security_capability_get when capabilities table
2107  * does not contain entry with matching ipsec direction field
2108  */
2109 static int
2110 test_capability_get_ipsec_mismatch_dir(void)
2111 {
2112         struct security_unittest_params *ut_params = &unittest_params;
2113         struct rte_security_capability_idx idx = {
2114                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2115                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
2116                 .ipsec = {
2117                         .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
2118                         .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
2119                         .direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
2120                 },
2121         };
2122         struct rte_security_capability capabilities[] = {
2123                 {
2124                         .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2125                         .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
2126                         .ipsec = {
2127                                 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
2128                                 .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
2129                                 .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
2130                         },
2131                 },
2132                 {
2133                         .action = RTE_SECURITY_ACTION_TYPE_NONE,
2134                 },
2135         };
2136
2137         mock_capabilities_get_exp.device = NULL;
2138         mock_capabilities_get_exp.ret = capabilities;
2139
2140         const struct rte_security_capability *ret;
2141         ret = rte_security_capability_get(&ut_params->ctx, &idx);
2142         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
2143                         ret, NULL, "%p");
2144         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
2145
2146         return TEST_SUCCESS;
2147 }
2148
2149 /**
2150  * Test execution of rte_security_capability_get when capabilities table
2151  * contains matching ipsec entry
2152  */
2153 static int
2154 test_capability_get_ipsec_match(void)
2155 {
2156         struct security_unittest_params *ut_params = &unittest_params;
2157         struct rte_security_capability_idx idx = {
2158                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2159                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
2160                 .ipsec = {
2161                         .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
2162                         .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
2163                         .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
2164                 },
2165         };
2166         struct rte_security_capability capabilities[] = {
2167                 {
2168                         .action = RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
2169                 },
2170                 {
2171                         .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2172                         .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
2173                         .ipsec = {
2174                                 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
2175                                 .mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
2176                                 .direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
2177                         },
2178                 },
2179                 {
2180                         .action = RTE_SECURITY_ACTION_TYPE_NONE,
2181                 },
2182         };
2183
2184         mock_capabilities_get_exp.device = NULL;
2185         mock_capabilities_get_exp.ret = capabilities;
2186
2187         const struct rte_security_capability *ret;
2188         ret = rte_security_capability_get(&ut_params->ctx, &idx);
2189         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
2190                         ret, &capabilities[1], "%p");
2191         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
2192
2193         return TEST_SUCCESS;
2194 }
2195
2196 /**
2197  * Test execution of rte_security_capability_get when capabilities table
2198  * does not contain entry with matching pdcp domain field
2199  */
2200 static int
2201 test_capability_get_pdcp_mismatch_domain(void)
2202 {
2203         struct security_unittest_params *ut_params = &unittest_params;
2204         struct rte_security_capability_idx idx = {
2205                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2206                 .protocol = RTE_SECURITY_PROTOCOL_PDCP,
2207                 .pdcp = {
2208                         .domain = RTE_SECURITY_PDCP_MODE_CONTROL,
2209                 },
2210         };
2211         struct rte_security_capability capabilities[] = {
2212                 {
2213                         .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2214                         .protocol = RTE_SECURITY_PROTOCOL_PDCP,
2215                         .pdcp = {
2216                                 .domain = RTE_SECURITY_PDCP_MODE_DATA,
2217                         },
2218                 },
2219                 {
2220                         .action = RTE_SECURITY_ACTION_TYPE_NONE,
2221                 },
2222         };
2223
2224         mock_capabilities_get_exp.device = NULL;
2225         mock_capabilities_get_exp.ret = capabilities;
2226
2227         const struct rte_security_capability *ret;
2228         ret = rte_security_capability_get(&ut_params->ctx, &idx);
2229         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
2230                         ret, NULL, "%p");
2231         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
2232
2233         return TEST_SUCCESS;
2234 }
2235
2236 /**
2237  * Test execution of rte_security_capability_get when capabilities table
2238  * contains matching pdcp entry
2239  */
2240 static int
2241 test_capability_get_pdcp_match(void)
2242 {
2243         struct security_unittest_params *ut_params = &unittest_params;
2244         struct rte_security_capability_idx idx = {
2245                 .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2246                 .protocol = RTE_SECURITY_PROTOCOL_PDCP,
2247                 .pdcp = {
2248                         .domain = RTE_SECURITY_PDCP_MODE_CONTROL,
2249                 },
2250         };
2251         struct rte_security_capability capabilities[] = {
2252                 {
2253                         .action = RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
2254                 },
2255                 {
2256                         .action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2257                         .protocol = RTE_SECURITY_PROTOCOL_PDCP,
2258                         .pdcp = {
2259                                 .domain = RTE_SECURITY_PDCP_MODE_CONTROL,
2260                         },
2261                 },
2262                 {
2263                         .action = RTE_SECURITY_ACTION_TYPE_NONE,
2264                 },
2265         };
2266
2267         mock_capabilities_get_exp.device = NULL;
2268         mock_capabilities_get_exp.ret = capabilities;
2269
2270         const struct rte_security_capability *ret;
2271         ret = rte_security_capability_get(&ut_params->ctx, &idx);
2272         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
2273                         ret, &capabilities[1], "%p");
2274         TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
2275
2276         return TEST_SUCCESS;
2277 }
2278
2279 /**
2280  * Declaration of testcases
2281  */
2282 static struct unit_test_suite security_testsuite  = {
2283         .suite_name = "generic security",
2284         .setup = testsuite_setup,
2285         .teardown = testsuite_teardown,
2286         .unit_test_cases = {
2287                 TEST_CASE_ST(ut_setup, ut_teardown,
2288                                 test_session_create_inv_context),
2289                 TEST_CASE_ST(ut_setup, ut_teardown,
2290                                 test_session_create_inv_context_ops),
2291                 TEST_CASE_ST(ut_setup, ut_teardown,
2292                                 test_session_create_inv_context_ops_fun),
2293                 TEST_CASE_ST(ut_setup, ut_teardown,
2294                                 test_session_create_inv_configuration),
2295                 TEST_CASE_ST(ut_setup, ut_teardown,
2296                                 test_session_create_inv_mempool),
2297                 TEST_CASE_ST(ut_setup, ut_teardown,
2298                                 test_session_create_mempool_empty),
2299                 TEST_CASE_ST(ut_setup, ut_teardown,
2300                                 test_session_create_ops_failure),
2301                 TEST_CASE_ST(ut_setup, ut_teardown,
2302                                 test_session_create_success),
2303
2304                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2305                                 test_session_update_inv_context),
2306                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2307                                 test_session_update_inv_context_ops),
2308                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2309                                 test_session_update_inv_context_ops_fun),
2310                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2311                                 test_session_update_inv_configuration),
2312                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2313                                 test_session_update_inv_session),
2314                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2315                                 test_session_update_ops_failure),
2316                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2317                                 test_session_update_success),
2318
2319                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2320                                 test_session_get_size_inv_context),
2321                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2322                                 test_session_get_size_inv_context_ops),
2323                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2324                                 test_session_get_size_inv_context_ops_fun),
2325                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2326                                 test_session_get_size_ops_failure),
2327                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2328                                 test_session_get_size_success),
2329
2330                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2331                                 test_session_stats_get_inv_context),
2332                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2333                                 test_session_stats_get_inv_context_ops),
2334                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2335                                 test_session_stats_get_inv_context_ops_fun),
2336                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2337                                 test_session_stats_get_inv_stats),
2338                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2339                                 test_session_stats_get_ops_failure),
2340                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2341                                 test_session_stats_get_success),
2342
2343                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2344                                 test_session_destroy_inv_context),
2345                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2346                                 test_session_destroy_inv_context_ops),
2347                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2348                                 test_session_destroy_inv_context_ops_fun),
2349                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2350                                 test_session_destroy_inv_session),
2351                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2352                                 test_session_destroy_ops_failure),
2353                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2354                                 test_session_destroy_success),
2355
2356                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2357                                 test_set_pkt_metadata_inv_context),
2358                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2359                                 test_set_pkt_metadata_inv_context_ops),
2360                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2361                                 test_set_pkt_metadata_inv_context_ops_fun),
2362                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2363                                 test_set_pkt_metadata_inv_session),
2364                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2365                                 test_set_pkt_metadata_ops_failure),
2366                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2367                                 test_set_pkt_metadata_success),
2368
2369                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2370                                 test_get_userdata_inv_context),
2371                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2372                                 test_get_userdata_inv_context_ops),
2373                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2374                                 test_get_userdata_inv_context_ops_fun),
2375                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2376                                 test_get_userdata_ops_failure),
2377                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2378                                 test_get_userdata_success),
2379
2380                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2381                                 test_capabilities_get_inv_context),
2382                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2383                                 test_capabilities_get_inv_context_ops),
2384                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2385                                 test_capabilities_get_inv_context_ops_fun),
2386                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2387                                 test_capabilities_get_ops_failure),
2388                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2389                                 test_capabilities_get_success),
2390
2391                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2392                                 test_capability_get_inv_context),
2393                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2394                                 test_capability_get_inv_context_ops),
2395                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2396                                 test_capability_get_inv_context_ops_fun),
2397                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2398                                 test_capability_get_inv_idx),
2399                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2400                                 test_capability_get_ops_failure),
2401                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2402                                 test_capability_get_empty_table),
2403                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2404                                 test_capability_get_no_matching_action),
2405                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2406                                 test_capability_get_no_matching_protocol),
2407                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2408                                 test_capability_get_no_support_for_macsec),
2409                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2410                                 test_capability_get_ipsec_mismatch_proto),
2411                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2412                                 test_capability_get_ipsec_mismatch_mode),
2413                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2414                                 test_capability_get_ipsec_mismatch_dir),
2415                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2416                                 test_capability_get_ipsec_match),
2417                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2418                                 test_capability_get_pdcp_mismatch_domain),
2419                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2420                                 test_capability_get_pdcp_match),
2421
2422                 TEST_CASES_END() /**< NULL terminate unit test array */
2423         }
2424 };
2425
2426 static int
2427 test_security(void)
2428 {
2429         rte_log_set_global_level(RTE_LOG_DEBUG);
2430         rte_log_set_level(RTE_LOGTYPE_EAL, RTE_LOG_DEBUG);
2431
2432         return unit_test_suite_runner(&security_testsuite);
2433 }
2434
2435 REGISTER_TEST_COMMAND(security_autotest, test_security);