test/security: check userdata get
[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  * empty_ops
445  *
446  * is an empty security operations set (all function pointers set to NULL)
447  */
448 struct rte_security_ops empty_ops = { NULL };
449
450 /**
451  * mock_ops
452  *
453  * is a security operations set using mockup functions
454  */
455 struct rte_security_ops mock_ops = {
456         .session_create = mock_session_create,
457         .session_update = mock_session_update,
458         .session_get_size = mock_session_get_size,
459         .session_stats_get = mock_session_stats_get,
460         .session_destroy = mock_session_destroy,
461         .set_pkt_metadata = mock_set_pkt_metadata,
462         .get_userdata = mock_get_userdata,
463 };
464
465
466 /**
467  * Test suite and test cases setup and teardown functions.
468  */
469
470 /**
471  * struct security_testsuite_params defines parameters initialized once
472  * for whole tests suite.
473  * Currently the only stored parameter is session_mpool a mempool created
474  * once in testsuite_setup and released in testsuite_teardown.
475  * The instance of this structure is stored in testsuite_params variable.
476  */
477 static struct security_testsuite_params {
478         struct rte_mempool *session_mpool;
479 } testsuite_params = { NULL };
480
481 /**
482  * struct security_unittest_params defines parameters initialized
483  * for every test case. The parameters are initialized in ut_setup
484  * or ut_setup_with_session (depending on the testcase)
485  * and released in ut_teardown.
486  * The instance of this structure is stored in unittest_params variable.
487  */
488 static struct security_unittest_params {
489         struct rte_security_ctx ctx;
490         struct rte_security_session_conf conf;
491         struct rte_security_session *sess;
492 } unittest_params = {
493         .ctx = {
494                 .device = NULL,
495                 .ops = &mock_ops,
496                 .sess_cnt = 0,
497         },
498         .sess = NULL,
499 };
500
501 #define SECURITY_TEST_MEMPOOL_NAME "SecurityTestsMempoolName"
502 #define SECURITY_TEST_MEMPOOL_SIZE 15
503 #define SECURITY_TEST_SESSION_OBJECT_SIZE sizeof(struct rte_security_session)
504
505 /**
506  * testsuite_setup initializes whole test suite parameters.
507  * It creates a new mempool used in all test cases
508  * and verifies if it properly created.
509  */
510 static int
511 testsuite_setup(void)
512 {
513         struct security_testsuite_params *ts_params = &testsuite_params;
514         ts_params->session_mpool = rte_mempool_create(
515                         SECURITY_TEST_MEMPOOL_NAME,
516                         SECURITY_TEST_MEMPOOL_SIZE,
517                         SECURITY_TEST_SESSION_OBJECT_SIZE,
518                         0, 0, NULL, NULL, NULL, NULL,
519                         SOCKET_ID_ANY, 0);
520         TEST_ASSERT_NOT_NULL(ts_params->session_mpool,
521                         "Cannot create mempool %s\n", rte_strerror(rte_errno));
522         return TEST_SUCCESS;
523 }
524
525 /**
526  * testsuite_teardown releases test suite wide parameters.
527  */
528 static void
529 testsuite_teardown(void)
530 {
531         struct security_testsuite_params *ts_params = &testsuite_params;
532         if (ts_params->session_mpool) {
533                 rte_mempool_free(ts_params->session_mpool);
534                 ts_params->session_mpool = NULL;
535         }
536 }
537
538 /**
539  * ut_setup initializes test case parameters to default values.
540  * It resets also any .called and .failed statistics of mockup functions
541  * usage.
542  */
543 static int
544 ut_setup(void)
545 {
546         struct security_unittest_params *ut_params = &unittest_params;
547         ut_params->ctx.device = NULL;
548         ut_params->ctx.ops = &mock_ops;
549         ut_params->ctx.sess_cnt = 0;
550         ut_params->sess = NULL;
551
552         mock_session_create_exp.called = 0;
553         mock_session_update_exp.called = 0;
554         mock_session_get_size_exp.called = 0;
555         mock_session_stats_get_exp.called = 0;
556         mock_session_destroy_exp.called = 0;
557         mock_set_pkt_metadata_exp.called = 0;
558         mock_get_userdata_exp.called = 0;
559
560         mock_session_create_exp.failed = 0;
561         mock_session_update_exp.failed = 0;
562         mock_session_get_size_exp.failed = 0;
563         mock_session_stats_get_exp.failed = 0;
564         mock_session_destroy_exp.failed = 0;
565         mock_set_pkt_metadata_exp.failed = 0;
566         mock_get_userdata_exp.failed = 0;
567
568         return TEST_SUCCESS;
569 }
570
571 /**
572  * destroy_session_with_check is a helper function releasing session
573  * created with rte_security_session_create and stored in test case parameters.
574  * It's used both to release sessions created in test cases' bodies
575  * which are assigned to ut_params->sess
576  * as well as sessions created in ut_setup_with_session.
577  */
578 static int
579 destroy_session_with_check(void)
580 {
581         struct security_unittest_params *ut_params = &unittest_params;
582         if (ut_params->sess != NULL) {
583                 /* Assure that mockup function for destroy operation is set. */
584                 ut_params->ctx.ops = &mock_ops;
585
586                 mock_session_destroy_exp.device = NULL;
587                 mock_session_destroy_exp.sess = ut_params->sess;
588                 mock_session_destroy_exp.ret = 0;
589                 mock_session_destroy_exp.called = 0;
590                 mock_session_destroy_exp.failed = 0;
591
592                 int ret = rte_security_session_destroy(&ut_params->ctx,
593                                 ut_params->sess);
594                 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy,
595                                 ret, 0, "%d");
596                 TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 1);
597
598                 ut_params->sess = NULL;
599         }
600         return TEST_SUCCESS;
601 }
602
603 /**
604  * ut_teardown releases test case parameters.
605  */
606 static void
607 ut_teardown(void)
608 {
609         destroy_session_with_check();
610 }
611
612 /**
613  * ut_setup_with_session initializes test case parameters by
614  * - calling standard ut_setup,
615  * - creating a session that can be used in test case.
616  */
617 static int
618 ut_setup_with_session(void)
619 {
620         struct security_unittest_params *ut_params = &unittest_params;
621         struct security_testsuite_params *ts_params = &testsuite_params;
622         struct rte_security_session *sess;
623
624         int ret = ut_setup();
625         if (ret != TEST_SUCCESS)
626                 return ret;
627
628         mock_session_create_exp.device = NULL;
629         mock_session_create_exp.conf = &ut_params->conf;
630         mock_session_create_exp.mp = ts_params->session_mpool;
631         mock_session_create_exp.ret = 0;
632
633         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
634                         ts_params->session_mpool);
635         TEST_ASSERT_MOCK_FUNCTION_CALL_NOT_NULL(rte_security_session_create,
636                         sess);
637         TEST_ASSERT_EQUAL(sess, mock_session_create_exp.sess,
638                         "Expecting session_create to be called with %p sess"
639                         " parameter, but it's called %p sess parameter",
640                         sess, mock_session_create_exp.sess);
641         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 1);
642
643         /*
644          * Store created session in test case parameters, so it can be released
645          * after test case in ut_teardown by destroy_session_with_check.
646          */
647         ut_params->sess = sess;
648
649         return TEST_SUCCESS;
650 }
651
652
653 /**
654  * Test functions
655  *
656  * Each test function is related to a single test case.
657  * They are arranged by tested rte_security API function
658  * and by rte_security execution paths sequence in code.
659  */
660
661 /**
662  * rte_security_session_create tests
663  */
664
665 /**
666  * Test execution of rte_security_session_create with NULL instance
667  */
668 static int
669 test_session_create_inv_context(void)
670 {
671         struct security_testsuite_params *ts_params = &testsuite_params;
672         struct security_unittest_params *ut_params = &unittest_params;
673         struct rte_security_session *sess;
674
675         sess = rte_security_session_create(NULL, &ut_params->conf,
676                         ts_params->session_mpool);
677         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
678                         sess, NULL, "%p");
679         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
680         TEST_ASSERT_MEMPOOL_USAGE(0);
681         TEST_ASSERT_SESSION_COUNT(0);
682
683         return TEST_SUCCESS;
684 }
685
686 /**
687  * Test execution of rte_security_session_create with invalid
688  * security operations structure (NULL)
689  */
690 static int
691 test_session_create_inv_context_ops(void)
692 {
693         struct security_testsuite_params *ts_params = &testsuite_params;
694         struct security_unittest_params *ut_params = &unittest_params;
695         struct rte_security_session *sess;
696
697         ut_params->ctx.ops = NULL;
698
699         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
700                         ts_params->session_mpool);
701         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
702                         sess, NULL, "%p");
703         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
704         TEST_ASSERT_MEMPOOL_USAGE(0);
705         TEST_ASSERT_SESSION_COUNT(0);
706
707         return TEST_SUCCESS;
708 }
709
710 /**
711  * Test execution of rte_security_session_create with empty
712  * security operations
713  */
714 static int
715 test_session_create_inv_context_ops_fun(void)
716 {
717         struct security_testsuite_params *ts_params = &testsuite_params;
718         struct security_unittest_params *ut_params = &unittest_params;
719         struct rte_security_session *sess;
720
721         ut_params->ctx.ops = &empty_ops;
722
723         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
724                         ts_params->session_mpool);
725         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
726                         sess, NULL, "%p");
727         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
728         TEST_ASSERT_MEMPOOL_USAGE(0);
729         TEST_ASSERT_SESSION_COUNT(0);
730
731         return TEST_SUCCESS;
732 }
733
734 /**
735  * Test execution of rte_security_session_create with NULL conf parameter
736  */
737 static int
738 test_session_create_inv_configuration(void)
739 {
740         struct security_testsuite_params *ts_params = &testsuite_params;
741         struct security_unittest_params *ut_params = &unittest_params;
742         struct rte_security_session *sess;
743
744         sess = rte_security_session_create(&ut_params->ctx, NULL,
745                         ts_params->session_mpool);
746         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
747                         sess, NULL, "%p");
748         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
749         TEST_ASSERT_MEMPOOL_USAGE(0);
750         TEST_ASSERT_SESSION_COUNT(0);
751
752         return TEST_SUCCESS;
753 }
754
755 /**
756  * Test execution of rte_security_session_create with NULL mp parameter
757  */
758 static int
759 test_session_create_inv_mempool(void)
760 {
761         struct security_unittest_params *ut_params = &unittest_params;
762         struct rte_security_session *sess;
763
764         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
765                         NULL);
766         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
767                         sess, NULL, "%p");
768         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
769         TEST_ASSERT_MEMPOOL_USAGE(0);
770         TEST_ASSERT_SESSION_COUNT(0);
771
772         return TEST_SUCCESS;
773 }
774
775 /**
776  * Test execution of rte_security_session_create in case when mempool
777  * is fully used and no object can be got from it
778  */
779 static int
780 test_session_create_mempool_empty(void)
781 {
782         struct security_testsuite_params *ts_params = &testsuite_params;
783         struct security_unittest_params *ut_params = &unittest_params;
784         struct rte_security_session *tmp[SECURITY_TEST_MEMPOOL_SIZE];
785         struct rte_security_session *sess;
786
787         /* Get all available objects from mempool. */
788         int i, ret;
789         for (i = 0; i < SECURITY_TEST_MEMPOOL_SIZE; ++i) {
790                 ret = rte_mempool_get(ts_params->session_mpool,
791                                 (void **)(&tmp[i]));
792                 TEST_ASSERT_EQUAL(0, ret,
793                                 "Expect getting %d object from mempool"
794                                 " to succeed", i);
795         }
796         TEST_ASSERT_MEMPOOL_USAGE(SECURITY_TEST_MEMPOOL_SIZE);
797
798         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
799                         ts_params->session_mpool);
800         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
801                         sess, NULL, "%p");
802         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
803         TEST_ASSERT_MEMPOOL_USAGE(SECURITY_TEST_MEMPOOL_SIZE);
804         TEST_ASSERT_SESSION_COUNT(0);
805
806         /* Put objects back to the pool. */
807         for (i = 0; i < SECURITY_TEST_MEMPOOL_SIZE; ++i)
808                 rte_mempool_put(ts_params->session_mpool, (void *)(tmp[i]));
809         TEST_ASSERT_MEMPOOL_USAGE(0);
810
811         return TEST_SUCCESS;
812 }
813
814 /**
815  * Test execution of rte_security_session_create when session_create
816  * security operation fails
817  */
818 static int
819 test_session_create_ops_failure(void)
820 {
821         struct security_testsuite_params *ts_params = &testsuite_params;
822         struct security_unittest_params *ut_params = &unittest_params;
823         struct rte_security_session *sess;
824
825         mock_session_create_exp.device = NULL;
826         mock_session_create_exp.conf = &ut_params->conf;
827         mock_session_create_exp.mp = ts_params->session_mpool;
828         mock_session_create_exp.ret = -1;       /* Return failure status. */
829
830         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
831                         ts_params->session_mpool);
832         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
833                         sess, NULL, "%p");
834         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 1);
835         TEST_ASSERT_MEMPOOL_USAGE(0);
836         TEST_ASSERT_SESSION_COUNT(0);
837
838         return TEST_SUCCESS;
839 }
840
841 /**
842  * Test execution of rte_security_session_create in successful execution path
843  */
844 static int
845 test_session_create_success(void)
846 {
847         struct security_testsuite_params *ts_params = &testsuite_params;
848         struct security_unittest_params *ut_params = &unittest_params;
849         struct rte_security_session *sess;
850
851         mock_session_create_exp.device = NULL;
852         mock_session_create_exp.conf = &ut_params->conf;
853         mock_session_create_exp.mp = ts_params->session_mpool;
854         mock_session_create_exp.ret = 0;        /* Return success status. */
855
856         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
857                         ts_params->session_mpool);
858         TEST_ASSERT_MOCK_FUNCTION_CALL_NOT_NULL(rte_security_session_create,
859                         sess);
860         TEST_ASSERT_EQUAL(sess, mock_session_create_exp.sess,
861                         "Expecting session_create to be called with %p sess"
862                         " parameter, but it's called %p sess parameter",
863                         sess, mock_session_create_exp.sess);
864         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 1);
865         TEST_ASSERT_MEMPOOL_USAGE(1);
866         TEST_ASSERT_SESSION_COUNT(1);
867
868         /*
869          * Store created session in test case parameters, so it can be released
870          * after test case in ut_teardown by destroy_session_with_check.
871          */
872         ut_params->sess = sess;
873
874         return TEST_SUCCESS;
875 }
876
877
878 /**
879  * rte_security_session_update tests
880  */
881
882 /**
883  * Test execution of rte_security_session_update with NULL instance
884  */
885 static int
886 test_session_update_inv_context(void)
887 {
888         struct security_unittest_params *ut_params = &unittest_params;
889
890         int ret = rte_security_session_update(NULL, ut_params->sess,
891                         &ut_params->conf);
892         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
893                         ret, -EINVAL, "%d");
894         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
895
896         return TEST_SUCCESS;
897 }
898
899 /**
900  * Test execution of rte_security_session_update with invalid
901  * security operations structure (NULL)
902  */
903 static int
904 test_session_update_inv_context_ops(void)
905 {
906         struct security_unittest_params *ut_params = &unittest_params;
907         ut_params->ctx.ops = NULL;
908
909         int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
910                         &ut_params->conf);
911         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
912                         ret, -EINVAL, "%d");
913         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
914
915         return TEST_SUCCESS;
916 }
917
918 /**
919  * Test execution of rte_security_session_update with empty
920  * security operations
921  */
922 static int
923 test_session_update_inv_context_ops_fun(void)
924 {
925         struct security_unittest_params *ut_params = &unittest_params;
926         ut_params->ctx.ops = &empty_ops;
927
928         int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
929                         &ut_params->conf);
930         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
931                         ret, -ENOTSUP, "%d");
932         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
933
934         return TEST_SUCCESS;
935 }
936
937 /**
938  * Test execution of rte_security_session_update with NULL conf parameter
939  */
940 static int
941 test_session_update_inv_configuration(void)
942 {
943         struct security_unittest_params *ut_params = &unittest_params;
944
945         int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
946                         NULL);
947         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
948                         ret, -EINVAL, "%d");
949         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
950
951         return TEST_SUCCESS;
952 }
953
954 /**
955  * Test execution of rte_security_session_update with NULL sess parameter
956  */
957 static int
958 test_session_update_inv_session(void)
959 {
960         struct security_unittest_params *ut_params = &unittest_params;
961
962         int ret = rte_security_session_update(&ut_params->ctx, NULL,
963                         &ut_params->conf);
964         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
965                         ret, -EINVAL, "%d");
966         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
967
968         return TEST_SUCCESS;
969 }
970
971 /**
972  * Test execution of rte_security_session_update when session_update
973  * security operation fails
974  */
975 static int
976 test_session_update_ops_failure(void)
977 {
978         struct security_unittest_params *ut_params = &unittest_params;
979
980         mock_session_update_exp.device = NULL;
981         mock_session_update_exp.sess = ut_params->sess;
982         mock_session_update_exp.conf = &ut_params->conf;
983         mock_session_update_exp.ret = -1;       /* Return failure status. */
984
985         int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
986                         &ut_params->conf);
987         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
988                         ret, -1, "%d");
989         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 1);
990
991         return TEST_SUCCESS;
992 }
993
994 /**
995  * Test execution of rte_security_session_update in successful execution path
996  */
997 static int
998 test_session_update_success(void)
999 {
1000         struct security_unittest_params *ut_params = &unittest_params;
1001
1002         mock_session_update_exp.device = NULL;
1003         mock_session_update_exp.sess = ut_params->sess;
1004         mock_session_update_exp.conf = &ut_params->conf;
1005         mock_session_update_exp.ret = 0;        /* Return success status. */
1006
1007         int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
1008                         &ut_params->conf);
1009         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
1010                         ret, 0, "%d");
1011         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 1);
1012
1013         return TEST_SUCCESS;
1014 }
1015
1016
1017 /**
1018  * rte_security_session_get_size tests
1019  */
1020
1021 /**
1022  * Test execution of rte_security_session_get_size with NULL instance
1023  */
1024 static int
1025 test_session_get_size_inv_context(void)
1026 {
1027         unsigned int ret = rte_security_session_get_size(NULL);
1028         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
1029                         ret, 0, "%u");
1030         TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 0);
1031
1032         return TEST_SUCCESS;
1033 }
1034
1035 /**
1036  * Test execution of rte_security_session_get_size with invalid
1037  * security operations structure (NULL)
1038  */
1039 static int
1040 test_session_get_size_inv_context_ops(void)
1041 {
1042         struct security_unittest_params *ut_params = &unittest_params;
1043         ut_params->ctx.ops = NULL;
1044
1045         unsigned int ret = rte_security_session_get_size(&ut_params->ctx);
1046         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
1047                         ret, 0, "%u");
1048         TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 0);
1049
1050         return TEST_SUCCESS;
1051 }
1052
1053 /**
1054  * Test execution of rte_security_session_get_size with empty
1055  * security operations
1056  */
1057 static int
1058 test_session_get_size_inv_context_ops_fun(void)
1059 {
1060         struct security_unittest_params *ut_params = &unittest_params;
1061         ut_params->ctx.ops = &empty_ops;
1062
1063         unsigned int ret = rte_security_session_get_size(&ut_params->ctx);
1064         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
1065                         ret, 0, "%u");
1066         TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 0);
1067
1068         return TEST_SUCCESS;
1069 }
1070
1071 /**
1072  * Test execution of rte_security_session_get_size when session_get_size
1073  * security operation fails
1074  */
1075 static int
1076 test_session_get_size_ops_failure(void)
1077 {
1078         struct security_unittest_params *ut_params = &unittest_params;
1079
1080         mock_session_get_size_exp.device = NULL;
1081         mock_session_get_size_exp.ret = 0;
1082
1083         unsigned int ret = rte_security_session_get_size(&ut_params->ctx);
1084         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
1085                         ret, 0, "%u");
1086         TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 1);
1087
1088         return TEST_SUCCESS;
1089 }
1090
1091 /**
1092  * Test execution of rte_security_session_get_size in successful execution path
1093  */
1094 static int
1095 test_session_get_size_success(void)
1096 {
1097         struct security_unittest_params *ut_params = &unittest_params;
1098
1099         mock_session_get_size_exp.device = NULL;
1100         mock_session_get_size_exp.ret = 1024;
1101
1102         unsigned int ret = rte_security_session_get_size(&ut_params->ctx);
1103         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
1104                         ret, 1024U, "%u");
1105         TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 1);
1106
1107         return TEST_SUCCESS;
1108 }
1109
1110
1111 /**
1112  * rte_security_session_stats_get tests
1113  */
1114
1115 /**
1116  * Test execution of rte_security_session_stats_get with NULL instance
1117  */
1118 static int
1119 test_session_stats_get_inv_context(void)
1120 {
1121         struct security_unittest_params *ut_params = &unittest_params;
1122         struct rte_security_stats stats;
1123
1124         int ret = rte_security_session_stats_get(NULL, ut_params->sess, &stats);
1125         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1126                         ret, -EINVAL, "%d");
1127         TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 0);
1128
1129         return TEST_SUCCESS;
1130 }
1131
1132 /**
1133  * Test execution of rte_security_session_stats_get with invalid
1134  * security operations structure (NULL)
1135  */
1136 static int
1137 test_session_stats_get_inv_context_ops(void)
1138 {
1139         struct security_unittest_params *ut_params = &unittest_params;
1140         struct rte_security_stats stats;
1141         ut_params->ctx.ops = NULL;
1142
1143         int ret = rte_security_session_stats_get(&ut_params->ctx,
1144                         ut_params->sess, &stats);
1145         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1146                         ret, -EINVAL, "%d");
1147         TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 0);
1148
1149         return TEST_SUCCESS;
1150 }
1151
1152 /**
1153  * Test execution of rte_security_session_stats_get with empty
1154  * security operations
1155  */
1156 static int
1157 test_session_stats_get_inv_context_ops_fun(void)
1158 {
1159         struct security_unittest_params *ut_params = &unittest_params;
1160         struct rte_security_stats stats;
1161         ut_params->ctx.ops = &empty_ops;
1162
1163         int ret = rte_security_session_stats_get(&ut_params->ctx,
1164                         ut_params->sess, &stats);
1165         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1166                         ret, -ENOTSUP, "%d");
1167         TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 0);
1168
1169         return TEST_SUCCESS;
1170 }
1171
1172 /**
1173  * Test execution of rte_security_session_stats_get with NULL stats parameter
1174  */
1175 static int
1176 test_session_stats_get_inv_stats(void)
1177 {
1178         struct security_unittest_params *ut_params = &unittest_params;
1179
1180         int ret = rte_security_session_stats_get(&ut_params->ctx,
1181                         ut_params->sess, NULL);
1182         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1183                         ret, -EINVAL, "%d");
1184         TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 0);
1185
1186         return TEST_SUCCESS;
1187 }
1188
1189 /**
1190  * Test execution of rte_security_session_stats_get when session_stats_get
1191  * security operation fails
1192  */
1193 static int
1194 test_session_stats_get_ops_failure(void)
1195 {
1196         struct security_unittest_params *ut_params = &unittest_params;
1197         struct rte_security_stats stats;
1198
1199         mock_session_stats_get_exp.device = NULL;
1200         mock_session_stats_get_exp.sess = ut_params->sess;
1201         mock_session_stats_get_exp.stats = &stats;
1202         mock_session_stats_get_exp.ret = -1;
1203
1204         int ret = rte_security_session_stats_get(&ut_params->ctx,
1205                         ut_params->sess, &stats);
1206         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1207                         ret, -1, "%d");
1208         TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 1);
1209
1210         return TEST_SUCCESS;
1211 }
1212
1213 /**
1214  * Test execution of rte_security_session_stats_get in successful execution
1215  * path
1216  */
1217 static int
1218 test_session_stats_get_success(void)
1219 {
1220         struct security_unittest_params *ut_params = &unittest_params;
1221         struct rte_security_stats stats;
1222
1223         mock_session_stats_get_exp.device = NULL;
1224         mock_session_stats_get_exp.sess = ut_params->sess;
1225         mock_session_stats_get_exp.stats = &stats;
1226         mock_session_stats_get_exp.ret = 0;
1227
1228         int ret = rte_security_session_stats_get(&ut_params->ctx,
1229                         ut_params->sess, &stats);
1230         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1231                         ret, 0, "%d");
1232         TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 1);
1233
1234         return TEST_SUCCESS;
1235 }
1236
1237
1238 /**
1239  * rte_security_session_destroy tests
1240  */
1241
1242 /**
1243  * Test execution of rte_security_session_destroy with NULL instance
1244  */
1245 static int
1246 test_session_destroy_inv_context(void)
1247 {
1248         struct security_unittest_params *ut_params = &unittest_params;
1249
1250         TEST_ASSERT_MEMPOOL_USAGE(1);
1251         TEST_ASSERT_SESSION_COUNT(1);
1252
1253         int ret = rte_security_session_destroy(NULL, ut_params->sess);
1254         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy,
1255                         ret, -EINVAL, "%d");
1256         TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 0);
1257         TEST_ASSERT_MEMPOOL_USAGE(1);
1258         TEST_ASSERT_SESSION_COUNT(1);
1259
1260         return TEST_SUCCESS;
1261 }
1262
1263 /**
1264  * Test execution of rte_security_session_destroy with invalid
1265  * security operations structure (NULL)
1266  */
1267 static int
1268 test_session_destroy_inv_context_ops(void)
1269 {
1270         struct security_unittest_params *ut_params = &unittest_params;
1271         ut_params->ctx.ops = NULL;
1272
1273         TEST_ASSERT_MEMPOOL_USAGE(1);
1274         TEST_ASSERT_SESSION_COUNT(1);
1275
1276         int ret = rte_security_session_destroy(&ut_params->ctx,
1277                         ut_params->sess);
1278         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy,
1279                         ret, -EINVAL, "%d");
1280         TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 0);
1281         TEST_ASSERT_MEMPOOL_USAGE(1);
1282         TEST_ASSERT_SESSION_COUNT(1);
1283
1284         return TEST_SUCCESS;
1285 }
1286
1287 /**
1288  * Test execution of rte_security_session_destroy with empty
1289  * security operations
1290  */
1291 static int
1292 test_session_destroy_inv_context_ops_fun(void)
1293 {
1294         struct security_unittest_params *ut_params = &unittest_params;
1295         ut_params->ctx.ops = &empty_ops;
1296
1297         TEST_ASSERT_MEMPOOL_USAGE(1);
1298         TEST_ASSERT_SESSION_COUNT(1);
1299
1300         int ret = rte_security_session_destroy(&ut_params->ctx,
1301                         ut_params->sess);
1302         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy,
1303                         ret, -ENOTSUP, "%d");
1304         TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 0);
1305         TEST_ASSERT_MEMPOOL_USAGE(1);
1306         TEST_ASSERT_SESSION_COUNT(1);
1307
1308         return TEST_SUCCESS;
1309 }
1310
1311 /**
1312  * Test execution of rte_security_session_destroy with NULL sess parameter
1313  */
1314 static int
1315 test_session_destroy_inv_session(void)
1316 {
1317         struct security_unittest_params *ut_params = &unittest_params;
1318
1319         TEST_ASSERT_MEMPOOL_USAGE(1);
1320         TEST_ASSERT_SESSION_COUNT(1);
1321
1322         int ret = rte_security_session_destroy(&ut_params->ctx, NULL);
1323         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy,
1324                         ret, -EINVAL, "%d");
1325         TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 0);
1326         TEST_ASSERT_MEMPOOL_USAGE(1);
1327         TEST_ASSERT_SESSION_COUNT(1);
1328
1329         return TEST_SUCCESS;
1330 }
1331
1332 /**
1333  * Test execution of rte_security_session_destroy when session_destroy
1334  * security operation fails
1335  */
1336 static int
1337 test_session_destroy_ops_failure(void)
1338 {
1339         struct security_unittest_params *ut_params = &unittest_params;
1340
1341         mock_session_destroy_exp.device = NULL;
1342         mock_session_destroy_exp.sess = ut_params->sess;
1343         mock_session_destroy_exp.ret = -1;
1344
1345         TEST_ASSERT_MEMPOOL_USAGE(1);
1346         TEST_ASSERT_SESSION_COUNT(1);
1347
1348         int ret = rte_security_session_destroy(&ut_params->ctx,
1349                         ut_params->sess);
1350         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy,
1351                         ret, -1, "%d");
1352         TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 1);
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 in successful execution path
1361  */
1362 static int
1363 test_session_destroy_success(void)
1364 {
1365         struct security_unittest_params *ut_params = &unittest_params;
1366
1367         mock_session_destroy_exp.device = NULL;
1368         mock_session_destroy_exp.sess = ut_params->sess;
1369         mock_session_destroy_exp.ret = 0;
1370         TEST_ASSERT_MEMPOOL_USAGE(1);
1371         TEST_ASSERT_SESSION_COUNT(1);
1372
1373         int ret = rte_security_session_destroy(&ut_params->ctx,
1374                         ut_params->sess);
1375         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy,
1376                         ret, 0, "%d");
1377         TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 1);
1378         TEST_ASSERT_MEMPOOL_USAGE(0);
1379         TEST_ASSERT_SESSION_COUNT(0);
1380
1381         /*
1382          * Remove session from test case parameters, so it won't be destroyed
1383          * during test case teardown.
1384          */
1385         ut_params->sess = NULL;
1386
1387         return TEST_SUCCESS;
1388 }
1389
1390
1391 /**
1392  * rte_security_set_pkt_metadata tests
1393  */
1394
1395 /**
1396  * Test execution of rte_security_set_pkt_metadata with NULL instance
1397  */
1398 static int
1399 test_set_pkt_metadata_inv_context(void)
1400 {
1401 #ifdef RTE_DEBUG
1402         struct security_unittest_params *ut_params = &unittest_params;
1403         struct rte_mbuf m;
1404         int params;
1405
1406         int ret = rte_security_set_pkt_metadata(NULL, ut_params->sess, &m,
1407                         &params);
1408         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata,
1409                         ret, -EINVAL, "%d");
1410         TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 0);
1411
1412         return TEST_SUCCESS;
1413 #else
1414         return TEST_SKIPPED;
1415 #endif
1416 }
1417
1418 /**
1419  * Test execution of rte_security_set_pkt_metadata with invalid
1420  * security operations structure (NULL)
1421  */
1422 static int
1423 test_set_pkt_metadata_inv_context_ops(void)
1424 {
1425 #ifdef RTE_DEBUG
1426         struct security_unittest_params *ut_params = &unittest_params;
1427         struct rte_mbuf m;
1428         int params;
1429         ut_params->ctx.ops = NULL;
1430
1431         int ret = rte_security_set_pkt_metadata(&ut_params->ctx,
1432                         ut_params->sess, &m, &params);
1433         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata,
1434                         ret, -EINVAL, "%d");
1435         TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 0);
1436
1437         return TEST_SUCCESS;
1438 #else
1439         return TEST_SKIPPED;
1440 #endif
1441 }
1442
1443 /**
1444  * Test execution of rte_security_set_pkt_metadata with empty
1445  * security operations
1446  */
1447 static int
1448 test_set_pkt_metadata_inv_context_ops_fun(void)
1449 {
1450 #ifdef RTE_DEBUG
1451         struct security_unittest_params *ut_params = &unittest_params;
1452         struct rte_mbuf m;
1453         int params;
1454         ut_params->ctx.ops = &empty_ops;
1455
1456         int ret = rte_security_set_pkt_metadata(&ut_params->ctx,
1457                         ut_params->sess, &m, &params);
1458         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata,
1459                         ret, -ENOTSUP, "%d");
1460         TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 0);
1461
1462         return TEST_SUCCESS;
1463 #else
1464         return TEST_SKIPPED;
1465 #endif
1466 }
1467
1468 /**
1469  * Test execution of rte_security_set_pkt_metadata with NULL sess parameter
1470  */
1471 static int
1472 test_set_pkt_metadata_inv_session(void)
1473 {
1474 #ifdef RTE_DEBUG
1475         struct security_unittest_params *ut_params = &unittest_params;
1476         struct rte_mbuf m;
1477         int params;
1478
1479         int ret = rte_security_set_pkt_metadata(&ut_params->ctx, NULL,
1480                         &m, &params);
1481         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata,
1482                         ret, -EINVAL, "%d");
1483         TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 0);
1484
1485         return TEST_SUCCESS;
1486 #else
1487         return TEST_SKIPPED;
1488 #endif
1489 }
1490
1491 /**
1492  * Test execution of rte_security_set_pkt_metadata when set_pkt_metadata
1493  * security operation fails
1494  */
1495 static int
1496 test_set_pkt_metadata_ops_failure(void)
1497 {
1498         struct security_unittest_params *ut_params = &unittest_params;
1499         struct rte_mbuf m;
1500         int params;
1501
1502         mock_set_pkt_metadata_exp.device = NULL;
1503         mock_set_pkt_metadata_exp.sess = ut_params->sess;
1504         mock_set_pkt_metadata_exp.m = &m;
1505         mock_set_pkt_metadata_exp.params = &params;
1506         mock_set_pkt_metadata_exp.ret = -1;
1507
1508         int ret = rte_security_set_pkt_metadata(&ut_params->ctx,
1509                         ut_params->sess, &m, &params);
1510         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata,
1511                         ret, -1, "%d");
1512         TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 1);
1513
1514         return TEST_SUCCESS;
1515 }
1516
1517 /**
1518  * Test execution of rte_security_set_pkt_metadata in successful execution path
1519  */
1520 static int
1521 test_set_pkt_metadata_success(void)
1522 {
1523         struct security_unittest_params *ut_params = &unittest_params;
1524         struct rte_mbuf m;
1525         int params;
1526
1527         mock_set_pkt_metadata_exp.device = NULL;
1528         mock_set_pkt_metadata_exp.sess = ut_params->sess;
1529         mock_set_pkt_metadata_exp.m = &m;
1530         mock_set_pkt_metadata_exp.params = &params;
1531         mock_set_pkt_metadata_exp.ret = 0;
1532
1533         int ret = rte_security_set_pkt_metadata(&ut_params->ctx,
1534                         ut_params->sess, &m, &params);
1535         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata,
1536                         ret, 0, "%d");
1537         TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 1);
1538
1539         return TEST_SUCCESS;
1540 }
1541
1542
1543 /**
1544  * rte_security_get_userdata tests
1545  */
1546
1547 /**
1548  * Test execution of rte_security_get_userdata with NULL instance
1549  */
1550 static int
1551 test_get_userdata_inv_context(void)
1552 {
1553 #ifdef RTE_DEBUG
1554         uint64_t md = 0xDEADBEEF;
1555
1556         void *ret = rte_security_get_userdata(NULL, md);
1557         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_get_userdata,
1558                         ret, NULL, "%p");
1559         TEST_ASSERT_MOCK_CALLS(mock_get_userdata_exp, 0);
1560
1561         return TEST_SUCCESS;
1562 #else
1563         return TEST_SKIPPED;
1564 #endif
1565 }
1566
1567 /**
1568  * Test execution of rte_security_get_userdata with invalid
1569  * security operations structure (NULL)
1570  */
1571 static int
1572 test_get_userdata_inv_context_ops(void)
1573 {
1574 #ifdef RTE_DEBUG
1575         struct security_unittest_params *ut_params = &unittest_params;
1576         uint64_t md = 0xDEADBEEF;
1577         ut_params->ctx.ops = NULL;
1578
1579         void *ret = rte_security_get_userdata(&ut_params->ctx, md);
1580         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_get_userdata,
1581                         ret, NULL, "%p");
1582         TEST_ASSERT_MOCK_CALLS(mock_get_userdata_exp, 0);
1583
1584         return TEST_SUCCESS;
1585 #else
1586         return TEST_SKIPPED;
1587 #endif
1588 }
1589
1590 /**
1591  * Test execution of rte_security_get_userdata with empty
1592  * security operations
1593  */
1594 static int
1595 test_get_userdata_inv_context_ops_fun(void)
1596 {
1597 #ifdef RTE_DEBUG
1598         struct security_unittest_params *ut_params = &unittest_params;
1599         uint64_t md = 0xDEADBEEF;
1600         ut_params->ctx.ops = &empty_ops;
1601
1602         void *ret = rte_security_get_userdata(&ut_params->ctx, md);
1603         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_get_userdata,
1604                         ret, NULL, "%p");
1605         TEST_ASSERT_MOCK_CALLS(mock_get_userdata_exp, 0);
1606
1607         return TEST_SUCCESS;
1608 #else
1609         return TEST_SKIPPED;
1610 #endif
1611 }
1612
1613 /**
1614  * Test execution of rte_security_get_userdata when get_userdata
1615  * security operation fails
1616  */
1617 static int
1618 test_get_userdata_ops_failure(void)
1619 {
1620         struct security_unittest_params *ut_params = &unittest_params;
1621         uint64_t md = 0xDEADBEEF;
1622         void *userdata = (void *)0x7E577E57;
1623
1624         mock_get_userdata_exp.device = NULL;
1625         mock_get_userdata_exp.md = md;
1626         mock_get_userdata_exp.userdata = userdata;
1627         mock_get_userdata_exp.ret = -1;
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, 1);
1633
1634         return TEST_SUCCESS;
1635 }
1636
1637 /**
1638  * Test execution of rte_security_get_userdata in successful execution path
1639  */
1640 static int
1641 test_get_userdata_success(void)
1642 {
1643         struct security_unittest_params *ut_params = &unittest_params;
1644         uint64_t md = 0xDEADBEEF;
1645         void *userdata = (void *)0x7E577E57;
1646
1647         mock_get_userdata_exp.device = NULL;
1648         mock_get_userdata_exp.md = md;
1649         mock_get_userdata_exp.userdata = userdata;
1650         mock_get_userdata_exp.ret = 0;
1651
1652         void *ret = rte_security_get_userdata(&ut_params->ctx, md);
1653         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_get_userdata,
1654                         ret, userdata, "%p");
1655         TEST_ASSERT_MOCK_CALLS(mock_get_userdata_exp, 1);
1656
1657         return TEST_SUCCESS;
1658 }
1659
1660
1661 /**
1662  * Declaration of testcases
1663  */
1664 static struct unit_test_suite security_testsuite  = {
1665         .suite_name = "generic security",
1666         .setup = testsuite_setup,
1667         .teardown = testsuite_teardown,
1668         .unit_test_cases = {
1669                 TEST_CASE_ST(ut_setup, ut_teardown,
1670                                 test_session_create_inv_context),
1671                 TEST_CASE_ST(ut_setup, ut_teardown,
1672                                 test_session_create_inv_context_ops),
1673                 TEST_CASE_ST(ut_setup, ut_teardown,
1674                                 test_session_create_inv_context_ops_fun),
1675                 TEST_CASE_ST(ut_setup, ut_teardown,
1676                                 test_session_create_inv_configuration),
1677                 TEST_CASE_ST(ut_setup, ut_teardown,
1678                                 test_session_create_inv_mempool),
1679                 TEST_CASE_ST(ut_setup, ut_teardown,
1680                                 test_session_create_mempool_empty),
1681                 TEST_CASE_ST(ut_setup, ut_teardown,
1682                                 test_session_create_ops_failure),
1683                 TEST_CASE_ST(ut_setup, ut_teardown,
1684                                 test_session_create_success),
1685
1686                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1687                                 test_session_update_inv_context),
1688                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1689                                 test_session_update_inv_context_ops),
1690                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1691                                 test_session_update_inv_context_ops_fun),
1692                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1693                                 test_session_update_inv_configuration),
1694                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1695                                 test_session_update_inv_session),
1696                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1697                                 test_session_update_ops_failure),
1698                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1699                                 test_session_update_success),
1700
1701                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1702                                 test_session_get_size_inv_context),
1703                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1704                                 test_session_get_size_inv_context_ops),
1705                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1706                                 test_session_get_size_inv_context_ops_fun),
1707                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1708                                 test_session_get_size_ops_failure),
1709                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1710                                 test_session_get_size_success),
1711
1712                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1713                                 test_session_stats_get_inv_context),
1714                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1715                                 test_session_stats_get_inv_context_ops),
1716                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1717                                 test_session_stats_get_inv_context_ops_fun),
1718                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1719                                 test_session_stats_get_inv_stats),
1720                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1721                                 test_session_stats_get_ops_failure),
1722                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1723                                 test_session_stats_get_success),
1724
1725                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1726                                 test_session_destroy_inv_context),
1727                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1728                                 test_session_destroy_inv_context_ops),
1729                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1730                                 test_session_destroy_inv_context_ops_fun),
1731                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1732                                 test_session_destroy_inv_session),
1733                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1734                                 test_session_destroy_ops_failure),
1735                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1736                                 test_session_destroy_success),
1737
1738                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1739                                 test_set_pkt_metadata_inv_context),
1740                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1741                                 test_set_pkt_metadata_inv_context_ops),
1742                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1743                                 test_set_pkt_metadata_inv_context_ops_fun),
1744                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1745                                 test_set_pkt_metadata_inv_session),
1746                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1747                                 test_set_pkt_metadata_ops_failure),
1748                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1749                                 test_set_pkt_metadata_success),
1750
1751                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1752                                 test_get_userdata_inv_context),
1753                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1754                                 test_get_userdata_inv_context_ops),
1755                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1756                                 test_get_userdata_inv_context_ops_fun),
1757                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1758                                 test_get_userdata_ops_failure),
1759                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1760                                 test_get_userdata_success),
1761
1762                 TEST_CASES_END() /**< NULL terminate unit test array */
1763         }
1764 };
1765
1766 static int
1767 test_security(void)
1768 {
1769         rte_log_set_global_level(RTE_LOG_DEBUG);
1770         rte_log_set_level(RTE_LOGTYPE_EAL, RTE_LOG_DEBUG);
1771
1772         return unit_test_suite_runner(&security_testsuite);
1773 }
1774
1775 REGISTER_TEST_COMMAND(security_autotest, test_security);