test/security: check session stats
[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 /**
81  * Verify if parameter of the mocked up function matches expected value.
82  * The expected value is stored in data structure in the field matching
83  * parameter name.
84  *
85  * @param   data        structure with expected values
86  * @param   parameter   name of the parameter (both field and parameter name)
87  * @param   spec        printf style spec for parameter
88  */
89 #define MOCK_TEST_ASSERT_PARAMETER(data, parameter, spec)               \
90         MOCK_TEST_ASSERT_EQUAL(data.failed, data.parameter, parameter,  \
91                         "Expecting parameter %s to be " spec            \
92                         " but it's " spec, RTE_STR(parameter),          \
93                         data.parameter, parameter)
94
95 /**
96  * Wrap for MOCK_TEST_ASSERT_PARAMETER macro for pointer type parameters.
97  *
98  * @param   data        structure with expected values
99  * @param   parameter   name of the parameter (both field and parameter name)
100  */
101 #define MOCK_TEST_ASSERT_POINTER_PARAMETER(data, parameter)     \
102         MOCK_TEST_ASSERT_PARAMETER(data, parameter, "%p")
103
104 /**
105  * Verify number of calls of the mocked up function
106  * and check if there were any fails during execution.
107  * The fails statistics inside mocked up functions are collected
108  * as "failed" field in mockup structures.
109  *
110  * @param   mock_data   structure with statistics (called, failed)
111  * @param   exp_calls   expected number of mockup function calls
112  */
113 #define TEST_ASSERT_MOCK_CALLS(mock_data, exp_calls) do {               \
114         TEST_ASSERT_EQUAL(exp_calls, mock_data.called,                  \
115                         "Expecting sub op to be called %d times, "      \
116                         "but it's called %d times",                     \
117                         exp_calls, mock_data.called);                   \
118         TEST_ASSERT_EQUAL(0, mock_data.failed,                          \
119                         "Expecting sub op asserts not to fail, "        \
120                         "but they're failed %d times",                  \
121                         mock_data.failed);                              \
122 } while (0)
123
124 /**
125  * Assert tested function result match expected value
126  *
127  * @param   f_name      name of tested function
128  * @param   f_ret       value returned by the function
129  * @param   exp_ret     expected returned value
130  * @param   fmt         printf style format for returned value
131  */
132 #define TEST_ASSERT_MOCK_FUNCTION_CALL_RET(f_name, f_ret, exp_ret, fmt) \
133         TEST_ASSERT_EQUAL(exp_ret, f_ret, "Expecting " RTE_STR(f_name)  \
134                         " to return " fmt ", but it returned " fmt      \
135                         "\n", exp_ret, f_ret)
136
137 /**
138  * Assert tested function result is not NULL
139  *
140  * @param   f_name      name of tested function
141  * @param   f_ret       value returned by the function
142  */
143 #define TEST_ASSERT_MOCK_FUNCTION_CALL_NOT_NULL(f_name, f_ret)          \
144         TEST_ASSERT_NOT_NULL(f_ret, "Expecting " RTE_STR(f_name)        \
145                         " to return not NULL\n")
146
147 /**
148  * Verify that sess_cnt counter value matches expected
149  *
150  * @param   expected_sessions_count     expected counter value
151  */
152 #define TEST_ASSERT_SESSION_COUNT(expected_sessions_count) do {         \
153         struct security_unittest_params *ut_params = &unittest_params;  \
154         TEST_ASSERT_EQUAL(expected_sessions_count,                      \
155                         ut_params->ctx.sess_cnt,                        \
156                         "Expecting session counter to be %u,"           \
157                         " but it's %u", expected_sessions_count,        \
158                         ut_params->ctx.sess_cnt);                       \
159 } while (0)
160
161 /**
162  * Verify usage of mempool by checking if number of allocated objects matches
163  * expectations. The mempool is used to manage objects for sessions data.
164  * A single object is acquired from mempool during session_create
165  * and put back in session_destroy.
166  *
167  * @param   expected_mempool_usage      expected number of used mempool objects
168  */
169 #define TEST_ASSERT_MEMPOOL_USAGE(expected_mempool_usage) do {          \
170         struct security_testsuite_params *ts_params = &testsuite_params;\
171         unsigned int mempool_usage;                                     \
172         mempool_usage = rte_mempool_in_use_count(                       \
173                         ts_params->session_mpool);                      \
174         TEST_ASSERT_EQUAL(expected_mempool_usage, mempool_usage,        \
175                         "Expecting %u mempool allocations, "            \
176                         "but there are %u allocated objects",           \
177                         expected_mempool_usage, mempool_usage);         \
178 } while (0)
179
180
181 /**
182  * Mockup structures and functions for rte_security_ops;
183  *
184  * Set of structures for controlling mockup functions calls.
185  * Every mockup function X has its corresponding X_data structure
186  * and an instance of that structure X_exp.
187  * Structure contains parameters that a mockup function is expected
188  * to be called with, a value to return (.ret) and 2 statistics:
189  * .called (number of times the mockup function was called)
190  * and .failed (number of assertion fails during mockup function call).
191  *
192  * Mockup functions verify that the parameters they are called with match
193  * expected values. The expected values should be stored in corresponding
194  * structures prior to mockup functions call. Every failure of such
195  * verification increases .failed counter. Every call of mockup function
196  * increases .called counter. Function returns value stored in .ret field
197  * of the structure.
198  * In case of some parameters in some functions the expected value is unknown
199  * and cannot be detrmined prior to call. Such parameters are stored
200  * in structure and can be compared or analyzed later in test case code.
201  *
202  * Below structures and functions follow the rules just described.
203  * Additional remarks and exceptions are added in comments.
204  */
205
206 /**
207  * session_create mockup
208  *
209  * Verified parameters: device, conf, mp.
210  * Saved, not verified parameters: sess.
211  */
212 static struct mock_session_create_data {
213         void *device;
214         struct rte_security_session_conf *conf;
215         struct rte_security_session *sess;
216         struct rte_mempool *mp;
217
218         int ret;
219
220         int called;
221         int failed;
222 } mock_session_create_exp = {NULL, NULL, NULL, NULL, 0, 0, 0};
223
224 static int
225 mock_session_create(void *device,
226                 struct rte_security_session_conf *conf,
227                 struct rte_security_session *sess,
228                 struct rte_mempool *mp)
229 {
230         mock_session_create_exp.called++;
231
232         MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_create_exp, device);
233         MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_create_exp, conf);
234         MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_create_exp, mp);
235
236         mock_session_create_exp.sess = sess;
237
238         return mock_session_create_exp.ret;
239 }
240
241 /**
242  * session_update mockup
243  *
244  * Verified parameters: device, sess, conf.
245  */
246 static struct mock_session_update_data {
247         void *device;
248         struct rte_security_session *sess;
249         struct rte_security_session_conf *conf;
250
251         int ret;
252
253         int called;
254         int failed;
255 } mock_session_update_exp = {NULL, NULL, NULL, 0, 0, 0};
256
257 static int
258 mock_session_update(void *device,
259                 struct rte_security_session *sess,
260                 struct rte_security_session_conf *conf)
261 {
262         mock_session_update_exp.called++;
263
264         MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_update_exp, device);
265         MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_update_exp, sess);
266         MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_update_exp, conf);
267
268         return mock_session_update_exp.ret;
269 }
270
271 /**
272  * session_get_size mockup
273  *
274  * Verified parameters: device.
275  */
276 static struct mock_session_get_size_data {
277         void *device;
278
279         unsigned int ret;
280
281         int called;
282         int failed;
283 } mock_session_get_size_exp = {NULL, 0U, 0, 0};
284
285 static unsigned int
286 mock_session_get_size(void *device)
287 {
288         mock_session_get_size_exp.called++;
289
290         MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_get_size_exp, device);
291
292         return mock_session_get_size_exp.ret;
293 }
294
295 /**
296  * session_stats_get mockup
297  *
298  * Verified parameters: device, sess, stats.
299  */
300 static struct mock_session_stats_get_data {
301         void *device;
302         struct rte_security_session *sess;
303         struct rte_security_stats *stats;
304
305         int ret;
306
307         int called;
308         int failed;
309 } mock_session_stats_get_exp = {NULL, NULL, NULL, 0, 0, 0};
310
311 static int
312 mock_session_stats_get(void *device,
313                 struct rte_security_session *sess,
314                 struct rte_security_stats *stats)
315 {
316         mock_session_stats_get_exp.called++;
317
318         MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_stats_get_exp, device);
319         MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_stats_get_exp, sess);
320         MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_stats_get_exp, stats);
321
322         return mock_session_stats_get_exp.ret;
323 }
324
325 /**
326  * session_destroy mockup
327  *
328  * Verified parameters: device, sess.
329  */
330 static struct mock_session_destroy_data {
331         void *device;
332         struct rte_security_session *sess;
333
334         int ret;
335
336         int called;
337         int failed;
338 } mock_session_destroy_exp = {NULL, NULL, 0, 0, 0};
339
340 static int
341 mock_session_destroy(void *device, struct rte_security_session *sess)
342 {
343         mock_session_destroy_exp.called++;
344
345         MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_destroy_exp, device);
346         MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_destroy_exp, sess);
347
348         return mock_session_destroy_exp.ret;
349 }
350
351 /**
352  * empty_ops
353  *
354  * is an empty security operations set (all function pointers set to NULL)
355  */
356 struct rte_security_ops empty_ops = { NULL };
357
358 /**
359  * mock_ops
360  *
361  * is a security operations set using mockup functions
362  */
363 struct rte_security_ops mock_ops = {
364         .session_create = mock_session_create,
365         .session_update = mock_session_update,
366         .session_get_size = mock_session_get_size,
367         .session_stats_get = mock_session_stats_get,
368         .session_destroy = mock_session_destroy,
369 };
370
371
372 /**
373  * Test suite and test cases setup and teardown functions.
374  */
375
376 /**
377  * struct security_testsuite_params defines parameters initialized once
378  * for whole tests suite.
379  * Currently the only stored parameter is session_mpool a mempool created
380  * once in testsuite_setup and released in testsuite_teardown.
381  * The instance of this structure is stored in testsuite_params variable.
382  */
383 static struct security_testsuite_params {
384         struct rte_mempool *session_mpool;
385 } testsuite_params = { NULL };
386
387 /**
388  * struct security_unittest_params defines parameters initialized
389  * for every test case. The parameters are initialized in ut_setup
390  * or ut_setup_with_session (depending on the testcase)
391  * and released in ut_teardown.
392  * The instance of this structure is stored in unittest_params variable.
393  */
394 static struct security_unittest_params {
395         struct rte_security_ctx ctx;
396         struct rte_security_session_conf conf;
397         struct rte_security_session *sess;
398 } unittest_params = {
399         .ctx = {
400                 .device = NULL,
401                 .ops = &mock_ops,
402                 .sess_cnt = 0,
403         },
404         .sess = NULL,
405 };
406
407 #define SECURITY_TEST_MEMPOOL_NAME "SecurityTestsMempoolName"
408 #define SECURITY_TEST_MEMPOOL_SIZE 15
409 #define SECURITY_TEST_SESSION_OBJECT_SIZE sizeof(struct rte_security_session)
410
411 /**
412  * testsuite_setup initializes whole test suite parameters.
413  * It creates a new mempool used in all test cases
414  * and verifies if it properly created.
415  */
416 static int
417 testsuite_setup(void)
418 {
419         struct security_testsuite_params *ts_params = &testsuite_params;
420         ts_params->session_mpool = rte_mempool_create(
421                         SECURITY_TEST_MEMPOOL_NAME,
422                         SECURITY_TEST_MEMPOOL_SIZE,
423                         SECURITY_TEST_SESSION_OBJECT_SIZE,
424                         0, 0, NULL, NULL, NULL, NULL,
425                         SOCKET_ID_ANY, 0);
426         TEST_ASSERT_NOT_NULL(ts_params->session_mpool,
427                         "Cannot create mempool %s\n", rte_strerror(rte_errno));
428         return TEST_SUCCESS;
429 }
430
431 /**
432  * testsuite_teardown releases test suite wide parameters.
433  */
434 static void
435 testsuite_teardown(void)
436 {
437         struct security_testsuite_params *ts_params = &testsuite_params;
438         if (ts_params->session_mpool) {
439                 rte_mempool_free(ts_params->session_mpool);
440                 ts_params->session_mpool = NULL;
441         }
442 }
443
444 /**
445  * ut_setup initializes test case parameters to default values.
446  * It resets also any .called and .failed statistics of mockup functions
447  * usage.
448  */
449 static int
450 ut_setup(void)
451 {
452         struct security_unittest_params *ut_params = &unittest_params;
453         ut_params->ctx.device = NULL;
454         ut_params->ctx.ops = &mock_ops;
455         ut_params->ctx.sess_cnt = 0;
456         ut_params->sess = NULL;
457
458         mock_session_create_exp.called = 0;
459         mock_session_update_exp.called = 0;
460         mock_session_get_size_exp.called = 0;
461         mock_session_stats_get_exp.called = 0;
462         mock_session_destroy_exp.called = 0;
463
464         mock_session_create_exp.failed = 0;
465         mock_session_update_exp.failed = 0;
466         mock_session_get_size_exp.failed = 0;
467         mock_session_stats_get_exp.failed = 0;
468         mock_session_destroy_exp.failed = 0;
469
470         return TEST_SUCCESS;
471 }
472
473 /**
474  * destroy_session_with_check is a helper function releasing session
475  * created with rte_security_session_create and stored in test case parameters.
476  * It's used both to release sessions created in test cases' bodies
477  * which are assigned to ut_params->sess
478  * as well as sessions created in ut_setup_with_session.
479  */
480 static int
481 destroy_session_with_check(void)
482 {
483         struct security_unittest_params *ut_params = &unittest_params;
484         if (ut_params->sess != NULL) {
485                 /* Assure that mockup function for destroy operation is set. */
486                 ut_params->ctx.ops = &mock_ops;
487
488                 mock_session_destroy_exp.device = NULL;
489                 mock_session_destroy_exp.sess = ut_params->sess;
490                 mock_session_destroy_exp.ret = 0;
491                 mock_session_destroy_exp.called = 0;
492                 mock_session_destroy_exp.failed = 0;
493
494                 int ret = rte_security_session_destroy(&ut_params->ctx,
495                                 ut_params->sess);
496                 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy,
497                                 ret, 0, "%d");
498                 TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 1);
499
500                 ut_params->sess = NULL;
501         }
502         return TEST_SUCCESS;
503 }
504
505 /**
506  * ut_teardown releases test case parameters.
507  */
508 static void
509 ut_teardown(void)
510 {
511         destroy_session_with_check();
512 }
513
514 /**
515  * ut_setup_with_session initializes test case parameters by
516  * - calling standard ut_setup,
517  * - creating a session that can be used in test case.
518  */
519 static int
520 ut_setup_with_session(void)
521 {
522         struct security_unittest_params *ut_params = &unittest_params;
523         struct security_testsuite_params *ts_params = &testsuite_params;
524         struct rte_security_session *sess;
525
526         int ret = ut_setup();
527         if (ret != TEST_SUCCESS)
528                 return ret;
529
530         mock_session_create_exp.device = NULL;
531         mock_session_create_exp.conf = &ut_params->conf;
532         mock_session_create_exp.mp = ts_params->session_mpool;
533         mock_session_create_exp.ret = 0;
534
535         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
536                         ts_params->session_mpool);
537         TEST_ASSERT_MOCK_FUNCTION_CALL_NOT_NULL(rte_security_session_create,
538                         sess);
539         TEST_ASSERT_EQUAL(sess, mock_session_create_exp.sess,
540                         "Expecting session_create to be called with %p sess"
541                         " parameter, but it's called %p sess parameter",
542                         sess, mock_session_create_exp.sess);
543         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 1);
544
545         /*
546          * Store created session in test case parameters, so it can be released
547          * after test case in ut_teardown by destroy_session_with_check.
548          */
549         ut_params->sess = sess;
550
551         return TEST_SUCCESS;
552 }
553
554
555 /**
556  * Test functions
557  *
558  * Each test function is related to a single test case.
559  * They are arranged by tested rte_security API function
560  * and by rte_security execution paths sequence in code.
561  */
562
563 /**
564  * rte_security_session_create tests
565  */
566
567 /**
568  * Test execution of rte_security_session_create with NULL instance
569  */
570 static int
571 test_session_create_inv_context(void)
572 {
573         struct security_testsuite_params *ts_params = &testsuite_params;
574         struct security_unittest_params *ut_params = &unittest_params;
575         struct rte_security_session *sess;
576
577         sess = rte_security_session_create(NULL, &ut_params->conf,
578                         ts_params->session_mpool);
579         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
580                         sess, NULL, "%p");
581         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
582         TEST_ASSERT_MEMPOOL_USAGE(0);
583         TEST_ASSERT_SESSION_COUNT(0);
584
585         return TEST_SUCCESS;
586 }
587
588 /**
589  * Test execution of rte_security_session_create with invalid
590  * security operations structure (NULL)
591  */
592 static int
593 test_session_create_inv_context_ops(void)
594 {
595         struct security_testsuite_params *ts_params = &testsuite_params;
596         struct security_unittest_params *ut_params = &unittest_params;
597         struct rte_security_session *sess;
598
599         ut_params->ctx.ops = NULL;
600
601         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
602                         ts_params->session_mpool);
603         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
604                         sess, NULL, "%p");
605         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
606         TEST_ASSERT_MEMPOOL_USAGE(0);
607         TEST_ASSERT_SESSION_COUNT(0);
608
609         return TEST_SUCCESS;
610 }
611
612 /**
613  * Test execution of rte_security_session_create with empty
614  * security operations
615  */
616 static int
617 test_session_create_inv_context_ops_fun(void)
618 {
619         struct security_testsuite_params *ts_params = &testsuite_params;
620         struct security_unittest_params *ut_params = &unittest_params;
621         struct rte_security_session *sess;
622
623         ut_params->ctx.ops = &empty_ops;
624
625         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
626                         ts_params->session_mpool);
627         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
628                         sess, NULL, "%p");
629         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
630         TEST_ASSERT_MEMPOOL_USAGE(0);
631         TEST_ASSERT_SESSION_COUNT(0);
632
633         return TEST_SUCCESS;
634 }
635
636 /**
637  * Test execution of rte_security_session_create with NULL conf parameter
638  */
639 static int
640 test_session_create_inv_configuration(void)
641 {
642         struct security_testsuite_params *ts_params = &testsuite_params;
643         struct security_unittest_params *ut_params = &unittest_params;
644         struct rte_security_session *sess;
645
646         sess = rte_security_session_create(&ut_params->ctx, NULL,
647                         ts_params->session_mpool);
648         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
649                         sess, NULL, "%p");
650         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
651         TEST_ASSERT_MEMPOOL_USAGE(0);
652         TEST_ASSERT_SESSION_COUNT(0);
653
654         return TEST_SUCCESS;
655 }
656
657 /**
658  * Test execution of rte_security_session_create with NULL mp parameter
659  */
660 static int
661 test_session_create_inv_mempool(void)
662 {
663         struct security_unittest_params *ut_params = &unittest_params;
664         struct rte_security_session *sess;
665
666         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
667                         NULL);
668         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
669                         sess, NULL, "%p");
670         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
671         TEST_ASSERT_MEMPOOL_USAGE(0);
672         TEST_ASSERT_SESSION_COUNT(0);
673
674         return TEST_SUCCESS;
675 }
676
677 /**
678  * Test execution of rte_security_session_create in case when mempool
679  * is fully used and no object can be got from it
680  */
681 static int
682 test_session_create_mempool_empty(void)
683 {
684         struct security_testsuite_params *ts_params = &testsuite_params;
685         struct security_unittest_params *ut_params = &unittest_params;
686         struct rte_security_session *tmp[SECURITY_TEST_MEMPOOL_SIZE];
687         struct rte_security_session *sess;
688
689         /* Get all available objects from mempool. */
690         int i, ret;
691         for (i = 0; i < SECURITY_TEST_MEMPOOL_SIZE; ++i) {
692                 ret = rte_mempool_get(ts_params->session_mpool,
693                                 (void **)(&tmp[i]));
694                 TEST_ASSERT_EQUAL(0, ret,
695                                 "Expect getting %d object from mempool"
696                                 " to succeed", i);
697         }
698         TEST_ASSERT_MEMPOOL_USAGE(SECURITY_TEST_MEMPOOL_SIZE);
699
700         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
701                         ts_params->session_mpool);
702         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
703                         sess, NULL, "%p");
704         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
705         TEST_ASSERT_MEMPOOL_USAGE(SECURITY_TEST_MEMPOOL_SIZE);
706         TEST_ASSERT_SESSION_COUNT(0);
707
708         /* Put objects back to the pool. */
709         for (i = 0; i < SECURITY_TEST_MEMPOOL_SIZE; ++i)
710                 rte_mempool_put(ts_params->session_mpool, (void *)(tmp[i]));
711         TEST_ASSERT_MEMPOOL_USAGE(0);
712
713         return TEST_SUCCESS;
714 }
715
716 /**
717  * Test execution of rte_security_session_create when session_create
718  * security operation fails
719  */
720 static int
721 test_session_create_ops_failure(void)
722 {
723         struct security_testsuite_params *ts_params = &testsuite_params;
724         struct security_unittest_params *ut_params = &unittest_params;
725         struct rte_security_session *sess;
726
727         mock_session_create_exp.device = NULL;
728         mock_session_create_exp.conf = &ut_params->conf;
729         mock_session_create_exp.mp = ts_params->session_mpool;
730         mock_session_create_exp.ret = -1;       /* Return failure status. */
731
732         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
733                         ts_params->session_mpool);
734         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
735                         sess, NULL, "%p");
736         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 1);
737         TEST_ASSERT_MEMPOOL_USAGE(0);
738         TEST_ASSERT_SESSION_COUNT(0);
739
740         return TEST_SUCCESS;
741 }
742
743 /**
744  * Test execution of rte_security_session_create in successful execution path
745  */
746 static int
747 test_session_create_success(void)
748 {
749         struct security_testsuite_params *ts_params = &testsuite_params;
750         struct security_unittest_params *ut_params = &unittest_params;
751         struct rte_security_session *sess;
752
753         mock_session_create_exp.device = NULL;
754         mock_session_create_exp.conf = &ut_params->conf;
755         mock_session_create_exp.mp = ts_params->session_mpool;
756         mock_session_create_exp.ret = 0;        /* Return success status. */
757
758         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
759                         ts_params->session_mpool);
760         TEST_ASSERT_MOCK_FUNCTION_CALL_NOT_NULL(rte_security_session_create,
761                         sess);
762         TEST_ASSERT_EQUAL(sess, mock_session_create_exp.sess,
763                         "Expecting session_create to be called with %p sess"
764                         " parameter, but it's called %p sess parameter",
765                         sess, mock_session_create_exp.sess);
766         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 1);
767         TEST_ASSERT_MEMPOOL_USAGE(1);
768         TEST_ASSERT_SESSION_COUNT(1);
769
770         /*
771          * Store created session in test case parameters, so it can be released
772          * after test case in ut_teardown by destroy_session_with_check.
773          */
774         ut_params->sess = sess;
775
776         return TEST_SUCCESS;
777 }
778
779
780 /**
781  * rte_security_session_update tests
782  */
783
784 /**
785  * Test execution of rte_security_session_update with NULL instance
786  */
787 static int
788 test_session_update_inv_context(void)
789 {
790         struct security_unittest_params *ut_params = &unittest_params;
791
792         int ret = rte_security_session_update(NULL, ut_params->sess,
793                         &ut_params->conf);
794         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
795                         ret, -EINVAL, "%d");
796         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
797
798         return TEST_SUCCESS;
799 }
800
801 /**
802  * Test execution of rte_security_session_update with invalid
803  * security operations structure (NULL)
804  */
805 static int
806 test_session_update_inv_context_ops(void)
807 {
808         struct security_unittest_params *ut_params = &unittest_params;
809         ut_params->ctx.ops = NULL;
810
811         int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
812                         &ut_params->conf);
813         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
814                         ret, -EINVAL, "%d");
815         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
816
817         return TEST_SUCCESS;
818 }
819
820 /**
821  * Test execution of rte_security_session_update with empty
822  * security operations
823  */
824 static int
825 test_session_update_inv_context_ops_fun(void)
826 {
827         struct security_unittest_params *ut_params = &unittest_params;
828         ut_params->ctx.ops = &empty_ops;
829
830         int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
831                         &ut_params->conf);
832         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
833                         ret, -ENOTSUP, "%d");
834         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
835
836         return TEST_SUCCESS;
837 }
838
839 /**
840  * Test execution of rte_security_session_update with NULL conf parameter
841  */
842 static int
843 test_session_update_inv_configuration(void)
844 {
845         struct security_unittest_params *ut_params = &unittest_params;
846
847         int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
848                         NULL);
849         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
850                         ret, -EINVAL, "%d");
851         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
852
853         return TEST_SUCCESS;
854 }
855
856 /**
857  * Test execution of rte_security_session_update with NULL sess parameter
858  */
859 static int
860 test_session_update_inv_session(void)
861 {
862         struct security_unittest_params *ut_params = &unittest_params;
863
864         int ret = rte_security_session_update(&ut_params->ctx, NULL,
865                         &ut_params->conf);
866         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
867                         ret, -EINVAL, "%d");
868         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
869
870         return TEST_SUCCESS;
871 }
872
873 /**
874  * Test execution of rte_security_session_update when session_update
875  * security operation fails
876  */
877 static int
878 test_session_update_ops_failure(void)
879 {
880         struct security_unittest_params *ut_params = &unittest_params;
881
882         mock_session_update_exp.device = NULL;
883         mock_session_update_exp.sess = ut_params->sess;
884         mock_session_update_exp.conf = &ut_params->conf;
885         mock_session_update_exp.ret = -1;       /* Return failure status. */
886
887         int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
888                         &ut_params->conf);
889         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
890                         ret, -1, "%d");
891         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 1);
892
893         return TEST_SUCCESS;
894 }
895
896 /**
897  * Test execution of rte_security_session_update in successful execution path
898  */
899 static int
900 test_session_update_success(void)
901 {
902         struct security_unittest_params *ut_params = &unittest_params;
903
904         mock_session_update_exp.device = NULL;
905         mock_session_update_exp.sess = ut_params->sess;
906         mock_session_update_exp.conf = &ut_params->conf;
907         mock_session_update_exp.ret = 0;        /* Return success status. */
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, 0, "%d");
913         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 1);
914
915         return TEST_SUCCESS;
916 }
917
918
919 /**
920  * rte_security_session_get_size tests
921  */
922
923 /**
924  * Test execution of rte_security_session_get_size with NULL instance
925  */
926 static int
927 test_session_get_size_inv_context(void)
928 {
929         unsigned int ret = rte_security_session_get_size(NULL);
930         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
931                         ret, 0, "%u");
932         TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 0);
933
934         return TEST_SUCCESS;
935 }
936
937 /**
938  * Test execution of rte_security_session_get_size with invalid
939  * security operations structure (NULL)
940  */
941 static int
942 test_session_get_size_inv_context_ops(void)
943 {
944         struct security_unittest_params *ut_params = &unittest_params;
945         ut_params->ctx.ops = NULL;
946
947         unsigned int ret = rte_security_session_get_size(&ut_params->ctx);
948         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
949                         ret, 0, "%u");
950         TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 0);
951
952         return TEST_SUCCESS;
953 }
954
955 /**
956  * Test execution of rte_security_session_get_size with empty
957  * security operations
958  */
959 static int
960 test_session_get_size_inv_context_ops_fun(void)
961 {
962         struct security_unittest_params *ut_params = &unittest_params;
963         ut_params->ctx.ops = &empty_ops;
964
965         unsigned int ret = rte_security_session_get_size(&ut_params->ctx);
966         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
967                         ret, 0, "%u");
968         TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 0);
969
970         return TEST_SUCCESS;
971 }
972
973 /**
974  * Test execution of rte_security_session_get_size when session_get_size
975  * security operation fails
976  */
977 static int
978 test_session_get_size_ops_failure(void)
979 {
980         struct security_unittest_params *ut_params = &unittest_params;
981
982         mock_session_get_size_exp.device = NULL;
983         mock_session_get_size_exp.ret = 0;
984
985         unsigned int ret = rte_security_session_get_size(&ut_params->ctx);
986         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
987                         ret, 0, "%u");
988         TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 1);
989
990         return TEST_SUCCESS;
991 }
992
993 /**
994  * Test execution of rte_security_session_get_size in successful execution path
995  */
996 static int
997 test_session_get_size_success(void)
998 {
999         struct security_unittest_params *ut_params = &unittest_params;
1000
1001         mock_session_get_size_exp.device = NULL;
1002         mock_session_get_size_exp.ret = 1024;
1003
1004         unsigned int ret = rte_security_session_get_size(&ut_params->ctx);
1005         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
1006                         ret, 1024U, "%u");
1007         TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 1);
1008
1009         return TEST_SUCCESS;
1010 }
1011
1012
1013 /**
1014  * rte_security_session_stats_get tests
1015  */
1016
1017 /**
1018  * Test execution of rte_security_session_stats_get with NULL instance
1019  */
1020 static int
1021 test_session_stats_get_inv_context(void)
1022 {
1023         struct security_unittest_params *ut_params = &unittest_params;
1024         struct rte_security_stats stats;
1025
1026         int ret = rte_security_session_stats_get(NULL, ut_params->sess, &stats);
1027         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1028                         ret, -EINVAL, "%d");
1029         TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 0);
1030
1031         return TEST_SUCCESS;
1032 }
1033
1034 /**
1035  * Test execution of rte_security_session_stats_get with invalid
1036  * security operations structure (NULL)
1037  */
1038 static int
1039 test_session_stats_get_inv_context_ops(void)
1040 {
1041         struct security_unittest_params *ut_params = &unittest_params;
1042         struct rte_security_stats stats;
1043         ut_params->ctx.ops = NULL;
1044
1045         int ret = rte_security_session_stats_get(&ut_params->ctx,
1046                         ut_params->sess, &stats);
1047         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1048                         ret, -EINVAL, "%d");
1049         TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 0);
1050
1051         return TEST_SUCCESS;
1052 }
1053
1054 /**
1055  * Test execution of rte_security_session_stats_get with empty
1056  * security operations
1057  */
1058 static int
1059 test_session_stats_get_inv_context_ops_fun(void)
1060 {
1061         struct security_unittest_params *ut_params = &unittest_params;
1062         struct rte_security_stats stats;
1063         ut_params->ctx.ops = &empty_ops;
1064
1065         int ret = rte_security_session_stats_get(&ut_params->ctx,
1066                         ut_params->sess, &stats);
1067         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1068                         ret, -ENOTSUP, "%d");
1069         TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 0);
1070
1071         return TEST_SUCCESS;
1072 }
1073
1074 /**
1075  * Test execution of rte_security_session_stats_get with NULL stats parameter
1076  */
1077 static int
1078 test_session_stats_get_inv_stats(void)
1079 {
1080         struct security_unittest_params *ut_params = &unittest_params;
1081
1082         int ret = rte_security_session_stats_get(&ut_params->ctx,
1083                         ut_params->sess, NULL);
1084         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1085                         ret, -EINVAL, "%d");
1086         TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 0);
1087
1088         return TEST_SUCCESS;
1089 }
1090
1091 /**
1092  * Test execution of rte_security_session_stats_get when session_stats_get
1093  * security operation fails
1094  */
1095 static int
1096 test_session_stats_get_ops_failure(void)
1097 {
1098         struct security_unittest_params *ut_params = &unittest_params;
1099         struct rte_security_stats stats;
1100
1101         mock_session_stats_get_exp.device = NULL;
1102         mock_session_stats_get_exp.sess = ut_params->sess;
1103         mock_session_stats_get_exp.stats = &stats;
1104         mock_session_stats_get_exp.ret = -1;
1105
1106         int ret = rte_security_session_stats_get(&ut_params->ctx,
1107                         ut_params->sess, &stats);
1108         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1109                         ret, -1, "%d");
1110         TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 1);
1111
1112         return TEST_SUCCESS;
1113 }
1114
1115 /**
1116  * Test execution of rte_security_session_stats_get in successful execution
1117  * path
1118  */
1119 static int
1120 test_session_stats_get_success(void)
1121 {
1122         struct security_unittest_params *ut_params = &unittest_params;
1123         struct rte_security_stats stats;
1124
1125         mock_session_stats_get_exp.device = NULL;
1126         mock_session_stats_get_exp.sess = ut_params->sess;
1127         mock_session_stats_get_exp.stats = &stats;
1128         mock_session_stats_get_exp.ret = 0;
1129
1130         int ret = rte_security_session_stats_get(&ut_params->ctx,
1131                         ut_params->sess, &stats);
1132         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1133                         ret, 0, "%d");
1134         TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 1);
1135
1136         return TEST_SUCCESS;
1137 }
1138
1139
1140 /**
1141  * Declaration of testcases
1142  */
1143 static struct unit_test_suite security_testsuite  = {
1144         .suite_name = "generic security",
1145         .setup = testsuite_setup,
1146         .teardown = testsuite_teardown,
1147         .unit_test_cases = {
1148                 TEST_CASE_ST(ut_setup, ut_teardown,
1149                                 test_session_create_inv_context),
1150                 TEST_CASE_ST(ut_setup, ut_teardown,
1151                                 test_session_create_inv_context_ops),
1152                 TEST_CASE_ST(ut_setup, ut_teardown,
1153                                 test_session_create_inv_context_ops_fun),
1154                 TEST_CASE_ST(ut_setup, ut_teardown,
1155                                 test_session_create_inv_configuration),
1156                 TEST_CASE_ST(ut_setup, ut_teardown,
1157                                 test_session_create_inv_mempool),
1158                 TEST_CASE_ST(ut_setup, ut_teardown,
1159                                 test_session_create_mempool_empty),
1160                 TEST_CASE_ST(ut_setup, ut_teardown,
1161                                 test_session_create_ops_failure),
1162                 TEST_CASE_ST(ut_setup, ut_teardown,
1163                                 test_session_create_success),
1164
1165                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1166                                 test_session_update_inv_context),
1167                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1168                                 test_session_update_inv_context_ops),
1169                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1170                                 test_session_update_inv_context_ops_fun),
1171                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1172                                 test_session_update_inv_configuration),
1173                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1174                                 test_session_update_inv_session),
1175                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1176                                 test_session_update_ops_failure),
1177                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1178                                 test_session_update_success),
1179
1180                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1181                                 test_session_get_size_inv_context),
1182                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1183                                 test_session_get_size_inv_context_ops),
1184                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1185                                 test_session_get_size_inv_context_ops_fun),
1186                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1187                                 test_session_get_size_ops_failure),
1188                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1189                                 test_session_get_size_success),
1190
1191                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1192                                 test_session_stats_get_inv_context),
1193                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1194                                 test_session_stats_get_inv_context_ops),
1195                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1196                                 test_session_stats_get_inv_context_ops_fun),
1197                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1198                                 test_session_stats_get_inv_stats),
1199                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1200                                 test_session_stats_get_ops_failure),
1201                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1202                                 test_session_stats_get_success),
1203
1204                 TEST_CASES_END() /**< NULL terminate unit test array */
1205         }
1206 };
1207
1208 static int
1209 test_security(void)
1210 {
1211         rte_log_set_global_level(RTE_LOG_DEBUG);
1212         rte_log_set_level(RTE_LOGTYPE_EAL, RTE_LOG_DEBUG);
1213
1214         return unit_test_suite_runner(&security_testsuite);
1215 }
1216
1217 REGISTER_TEST_COMMAND(security_autotest, test_security);