9c5e7d0daec77d0f4908a0446d2ed39849c3fb99
[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_destroy mockup
297  *
298  * Verified parameters: device, sess.
299  */
300 static struct mock_session_destroy_data {
301         void *device;
302         struct rte_security_session *sess;
303
304         int ret;
305
306         int called;
307         int failed;
308 } mock_session_destroy_exp = {NULL, NULL, 0, 0, 0};
309
310 static int
311 mock_session_destroy(void *device, struct rte_security_session *sess)
312 {
313         mock_session_destroy_exp.called++;
314
315         MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_destroy_exp, device);
316         MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_destroy_exp, sess);
317
318         return mock_session_destroy_exp.ret;
319 }
320
321 /**
322  * empty_ops
323  *
324  * is an empty security operations set (all function pointers set to NULL)
325  */
326 struct rte_security_ops empty_ops = { NULL };
327
328 /**
329  * mock_ops
330  *
331  * is a security operations set using mockup functions
332  */
333 struct rte_security_ops mock_ops = {
334         .session_create = mock_session_create,
335         .session_update = mock_session_update,
336         .session_get_size = mock_session_get_size,
337         .session_destroy = mock_session_destroy,
338 };
339
340
341 /**
342  * Test suite and test cases setup and teardown functions.
343  */
344
345 /**
346  * struct security_testsuite_params defines parameters initialized once
347  * for whole tests suite.
348  * Currently the only stored parameter is session_mpool a mempool created
349  * once in testsuite_setup and released in testsuite_teardown.
350  * The instance of this structure is stored in testsuite_params variable.
351  */
352 static struct security_testsuite_params {
353         struct rte_mempool *session_mpool;
354 } testsuite_params = { NULL };
355
356 /**
357  * struct security_unittest_params defines parameters initialized
358  * for every test case. The parameters are initialized in ut_setup
359  * or ut_setup_with_session (depending on the testcase)
360  * and released in ut_teardown.
361  * The instance of this structure is stored in unittest_params variable.
362  */
363 static struct security_unittest_params {
364         struct rte_security_ctx ctx;
365         struct rte_security_session_conf conf;
366         struct rte_security_session *sess;
367 } unittest_params = {
368         .ctx = {
369                 .device = NULL,
370                 .ops = &mock_ops,
371                 .sess_cnt = 0,
372         },
373         .sess = NULL,
374 };
375
376 #define SECURITY_TEST_MEMPOOL_NAME "SecurityTestsMempoolName"
377 #define SECURITY_TEST_MEMPOOL_SIZE 15
378 #define SECURITY_TEST_SESSION_OBJECT_SIZE sizeof(struct rte_security_session)
379
380 /**
381  * testsuite_setup initializes whole test suite parameters.
382  * It creates a new mempool used in all test cases
383  * and verifies if it properly created.
384  */
385 static int
386 testsuite_setup(void)
387 {
388         struct security_testsuite_params *ts_params = &testsuite_params;
389         ts_params->session_mpool = rte_mempool_create(
390                         SECURITY_TEST_MEMPOOL_NAME,
391                         SECURITY_TEST_MEMPOOL_SIZE,
392                         SECURITY_TEST_SESSION_OBJECT_SIZE,
393                         0, 0, NULL, NULL, NULL, NULL,
394                         SOCKET_ID_ANY, 0);
395         TEST_ASSERT_NOT_NULL(ts_params->session_mpool,
396                         "Cannot create mempool %s\n", rte_strerror(rte_errno));
397         return TEST_SUCCESS;
398 }
399
400 /**
401  * testsuite_teardown releases test suite wide parameters.
402  */
403 static void
404 testsuite_teardown(void)
405 {
406         struct security_testsuite_params *ts_params = &testsuite_params;
407         if (ts_params->session_mpool) {
408                 rte_mempool_free(ts_params->session_mpool);
409                 ts_params->session_mpool = NULL;
410         }
411 }
412
413 /**
414  * ut_setup initializes test case parameters to default values.
415  * It resets also any .called and .failed statistics of mockup functions
416  * usage.
417  */
418 static int
419 ut_setup(void)
420 {
421         struct security_unittest_params *ut_params = &unittest_params;
422         ut_params->ctx.device = NULL;
423         ut_params->ctx.ops = &mock_ops;
424         ut_params->ctx.sess_cnt = 0;
425         ut_params->sess = NULL;
426
427         mock_session_create_exp.called = 0;
428         mock_session_update_exp.called = 0;
429         mock_session_get_size_exp.called = 0;
430         mock_session_destroy_exp.called = 0;
431
432         mock_session_create_exp.failed = 0;
433         mock_session_update_exp.failed = 0;
434         mock_session_get_size_exp.failed = 0;
435         mock_session_destroy_exp.failed = 0;
436
437         return TEST_SUCCESS;
438 }
439
440 /**
441  * destroy_session_with_check is a helper function releasing session
442  * created with rte_security_session_create and stored in test case parameters.
443  * It's used both to release sessions created in test cases' bodies
444  * which are assigned to ut_params->sess
445  * as well as sessions created in ut_setup_with_session.
446  */
447 static int
448 destroy_session_with_check(void)
449 {
450         struct security_unittest_params *ut_params = &unittest_params;
451         if (ut_params->sess != NULL) {
452                 /* Assure that mockup function for destroy operation is set. */
453                 ut_params->ctx.ops = &mock_ops;
454
455                 mock_session_destroy_exp.device = NULL;
456                 mock_session_destroy_exp.sess = ut_params->sess;
457                 mock_session_destroy_exp.ret = 0;
458                 mock_session_destroy_exp.called = 0;
459                 mock_session_destroy_exp.failed = 0;
460
461                 int ret = rte_security_session_destroy(&ut_params->ctx,
462                                 ut_params->sess);
463                 TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy,
464                                 ret, 0, "%d");
465                 TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 1);
466
467                 ut_params->sess = NULL;
468         }
469         return TEST_SUCCESS;
470 }
471
472 /**
473  * ut_teardown releases test case parameters.
474  */
475 static void
476 ut_teardown(void)
477 {
478         destroy_session_with_check();
479 }
480
481 /**
482  * ut_setup_with_session initializes test case parameters by
483  * - calling standard ut_setup,
484  * - creating a session that can be used in test case.
485  */
486 static int
487 ut_setup_with_session(void)
488 {
489         struct security_unittest_params *ut_params = &unittest_params;
490         struct security_testsuite_params *ts_params = &testsuite_params;
491         struct rte_security_session *sess;
492
493         int ret = ut_setup();
494         if (ret != TEST_SUCCESS)
495                 return ret;
496
497         mock_session_create_exp.device = NULL;
498         mock_session_create_exp.conf = &ut_params->conf;
499         mock_session_create_exp.mp = ts_params->session_mpool;
500         mock_session_create_exp.ret = 0;
501
502         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
503                         ts_params->session_mpool);
504         TEST_ASSERT_MOCK_FUNCTION_CALL_NOT_NULL(rte_security_session_create,
505                         sess);
506         TEST_ASSERT_EQUAL(sess, mock_session_create_exp.sess,
507                         "Expecting session_create to be called with %p sess"
508                         " parameter, but it's called %p sess parameter",
509                         sess, mock_session_create_exp.sess);
510         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 1);
511
512         /*
513          * Store created session in test case parameters, so it can be released
514          * after test case in ut_teardown by destroy_session_with_check.
515          */
516         ut_params->sess = sess;
517
518         return TEST_SUCCESS;
519 }
520
521
522 /**
523  * Test functions
524  *
525  * Each test function is related to a single test case.
526  * They are arranged by tested rte_security API function
527  * and by rte_security execution paths sequence in code.
528  */
529
530 /**
531  * rte_security_session_create tests
532  */
533
534 /**
535  * Test execution of rte_security_session_create with NULL instance
536  */
537 static int
538 test_session_create_inv_context(void)
539 {
540         struct security_testsuite_params *ts_params = &testsuite_params;
541         struct security_unittest_params *ut_params = &unittest_params;
542         struct rte_security_session *sess;
543
544         sess = rte_security_session_create(NULL, &ut_params->conf,
545                         ts_params->session_mpool);
546         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
547                         sess, NULL, "%p");
548         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
549         TEST_ASSERT_MEMPOOL_USAGE(0);
550         TEST_ASSERT_SESSION_COUNT(0);
551
552         return TEST_SUCCESS;
553 }
554
555 /**
556  * Test execution of rte_security_session_create with invalid
557  * security operations structure (NULL)
558  */
559 static int
560 test_session_create_inv_context_ops(void)
561 {
562         struct security_testsuite_params *ts_params = &testsuite_params;
563         struct security_unittest_params *ut_params = &unittest_params;
564         struct rte_security_session *sess;
565
566         ut_params->ctx.ops = NULL;
567
568         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
569                         ts_params->session_mpool);
570         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
571                         sess, NULL, "%p");
572         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
573         TEST_ASSERT_MEMPOOL_USAGE(0);
574         TEST_ASSERT_SESSION_COUNT(0);
575
576         return TEST_SUCCESS;
577 }
578
579 /**
580  * Test execution of rte_security_session_create with empty
581  * security operations
582  */
583 static int
584 test_session_create_inv_context_ops_fun(void)
585 {
586         struct security_testsuite_params *ts_params = &testsuite_params;
587         struct security_unittest_params *ut_params = &unittest_params;
588         struct rte_security_session *sess;
589
590         ut_params->ctx.ops = &empty_ops;
591
592         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
593                         ts_params->session_mpool);
594         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
595                         sess, NULL, "%p");
596         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
597         TEST_ASSERT_MEMPOOL_USAGE(0);
598         TEST_ASSERT_SESSION_COUNT(0);
599
600         return TEST_SUCCESS;
601 }
602
603 /**
604  * Test execution of rte_security_session_create with NULL conf parameter
605  */
606 static int
607 test_session_create_inv_configuration(void)
608 {
609         struct security_testsuite_params *ts_params = &testsuite_params;
610         struct security_unittest_params *ut_params = &unittest_params;
611         struct rte_security_session *sess;
612
613         sess = rte_security_session_create(&ut_params->ctx, NULL,
614                         ts_params->session_mpool);
615         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
616                         sess, NULL, "%p");
617         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
618         TEST_ASSERT_MEMPOOL_USAGE(0);
619         TEST_ASSERT_SESSION_COUNT(0);
620
621         return TEST_SUCCESS;
622 }
623
624 /**
625  * Test execution of rte_security_session_create with NULL mp parameter
626  */
627 static int
628 test_session_create_inv_mempool(void)
629 {
630         struct security_unittest_params *ut_params = &unittest_params;
631         struct rte_security_session *sess;
632
633         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
634                         NULL);
635         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
636                         sess, NULL, "%p");
637         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
638         TEST_ASSERT_MEMPOOL_USAGE(0);
639         TEST_ASSERT_SESSION_COUNT(0);
640
641         return TEST_SUCCESS;
642 }
643
644 /**
645  * Test execution of rte_security_session_create in case when mempool
646  * is fully used and no object can be got from it
647  */
648 static int
649 test_session_create_mempool_empty(void)
650 {
651         struct security_testsuite_params *ts_params = &testsuite_params;
652         struct security_unittest_params *ut_params = &unittest_params;
653         struct rte_security_session *tmp[SECURITY_TEST_MEMPOOL_SIZE];
654         struct rte_security_session *sess;
655
656         /* Get all available objects from mempool. */
657         int i, ret;
658         for (i = 0; i < SECURITY_TEST_MEMPOOL_SIZE; ++i) {
659                 ret = rte_mempool_get(ts_params->session_mpool,
660                                 (void **)(&tmp[i]));
661                 TEST_ASSERT_EQUAL(0, ret,
662                                 "Expect getting %d object from mempool"
663                                 " to succeed", i);
664         }
665         TEST_ASSERT_MEMPOOL_USAGE(SECURITY_TEST_MEMPOOL_SIZE);
666
667         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
668                         ts_params->session_mpool);
669         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
670                         sess, NULL, "%p");
671         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
672         TEST_ASSERT_MEMPOOL_USAGE(SECURITY_TEST_MEMPOOL_SIZE);
673         TEST_ASSERT_SESSION_COUNT(0);
674
675         /* Put objects back to the pool. */
676         for (i = 0; i < SECURITY_TEST_MEMPOOL_SIZE; ++i)
677                 rte_mempool_put(ts_params->session_mpool, (void *)(tmp[i]));
678         TEST_ASSERT_MEMPOOL_USAGE(0);
679
680         return TEST_SUCCESS;
681 }
682
683 /**
684  * Test execution of rte_security_session_create when session_create
685  * security operation fails
686  */
687 static int
688 test_session_create_ops_failure(void)
689 {
690         struct security_testsuite_params *ts_params = &testsuite_params;
691         struct security_unittest_params *ut_params = &unittest_params;
692         struct rte_security_session *sess;
693
694         mock_session_create_exp.device = NULL;
695         mock_session_create_exp.conf = &ut_params->conf;
696         mock_session_create_exp.mp = ts_params->session_mpool;
697         mock_session_create_exp.ret = -1;       /* Return failure status. */
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, 1);
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 in successful execution path
712  */
713 static int
714 test_session_create_success(void)
715 {
716         struct security_testsuite_params *ts_params = &testsuite_params;
717         struct security_unittest_params *ut_params = &unittest_params;
718         struct rte_security_session *sess;
719
720         mock_session_create_exp.device = NULL;
721         mock_session_create_exp.conf = &ut_params->conf;
722         mock_session_create_exp.mp = ts_params->session_mpool;
723         mock_session_create_exp.ret = 0;        /* Return success status. */
724
725         sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
726                         ts_params->session_mpool);
727         TEST_ASSERT_MOCK_FUNCTION_CALL_NOT_NULL(rte_security_session_create,
728                         sess);
729         TEST_ASSERT_EQUAL(sess, mock_session_create_exp.sess,
730                         "Expecting session_create to be called with %p sess"
731                         " parameter, but it's called %p sess parameter",
732                         sess, mock_session_create_exp.sess);
733         TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 1);
734         TEST_ASSERT_MEMPOOL_USAGE(1);
735         TEST_ASSERT_SESSION_COUNT(1);
736
737         /*
738          * Store created session in test case parameters, so it can be released
739          * after test case in ut_teardown by destroy_session_with_check.
740          */
741         ut_params->sess = sess;
742
743         return TEST_SUCCESS;
744 }
745
746
747 /**
748  * rte_security_session_update tests
749  */
750
751 /**
752  * Test execution of rte_security_session_update with NULL instance
753  */
754 static int
755 test_session_update_inv_context(void)
756 {
757         struct security_unittest_params *ut_params = &unittest_params;
758
759         int ret = rte_security_session_update(NULL, ut_params->sess,
760                         &ut_params->conf);
761         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
762                         ret, -EINVAL, "%d");
763         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
764
765         return TEST_SUCCESS;
766 }
767
768 /**
769  * Test execution of rte_security_session_update with invalid
770  * security operations structure (NULL)
771  */
772 static int
773 test_session_update_inv_context_ops(void)
774 {
775         struct security_unittest_params *ut_params = &unittest_params;
776         ut_params->ctx.ops = NULL;
777
778         int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
779                         &ut_params->conf);
780         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
781                         ret, -EINVAL, "%d");
782         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
783
784         return TEST_SUCCESS;
785 }
786
787 /**
788  * Test execution of rte_security_session_update with empty
789  * security operations
790  */
791 static int
792 test_session_update_inv_context_ops_fun(void)
793 {
794         struct security_unittest_params *ut_params = &unittest_params;
795         ut_params->ctx.ops = &empty_ops;
796
797         int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
798                         &ut_params->conf);
799         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
800                         ret, -ENOTSUP, "%d");
801         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
802
803         return TEST_SUCCESS;
804 }
805
806 /**
807  * Test execution of rte_security_session_update with NULL conf parameter
808  */
809 static int
810 test_session_update_inv_configuration(void)
811 {
812         struct security_unittest_params *ut_params = &unittest_params;
813
814         int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
815                         NULL);
816         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
817                         ret, -EINVAL, "%d");
818         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
819
820         return TEST_SUCCESS;
821 }
822
823 /**
824  * Test execution of rte_security_session_update with NULL sess parameter
825  */
826 static int
827 test_session_update_inv_session(void)
828 {
829         struct security_unittest_params *ut_params = &unittest_params;
830
831         int ret = rte_security_session_update(&ut_params->ctx, NULL,
832                         &ut_params->conf);
833         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
834                         ret, -EINVAL, "%d");
835         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
836
837         return TEST_SUCCESS;
838 }
839
840 /**
841  * Test execution of rte_security_session_update when session_update
842  * security operation fails
843  */
844 static int
845 test_session_update_ops_failure(void)
846 {
847         struct security_unittest_params *ut_params = &unittest_params;
848
849         mock_session_update_exp.device = NULL;
850         mock_session_update_exp.sess = ut_params->sess;
851         mock_session_update_exp.conf = &ut_params->conf;
852         mock_session_update_exp.ret = -1;       /* Return failure status. */
853
854         int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
855                         &ut_params->conf);
856         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
857                         ret, -1, "%d");
858         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 1);
859
860         return TEST_SUCCESS;
861 }
862
863 /**
864  * Test execution of rte_security_session_update in successful execution path
865  */
866 static int
867 test_session_update_success(void)
868 {
869         struct security_unittest_params *ut_params = &unittest_params;
870
871         mock_session_update_exp.device = NULL;
872         mock_session_update_exp.sess = ut_params->sess;
873         mock_session_update_exp.conf = &ut_params->conf;
874         mock_session_update_exp.ret = 0;        /* Return success status. */
875
876         int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
877                         &ut_params->conf);
878         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
879                         ret, 0, "%d");
880         TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 1);
881
882         return TEST_SUCCESS;
883 }
884
885
886 /**
887  * rte_security_session_get_size tests
888  */
889
890 /**
891  * Test execution of rte_security_session_get_size with NULL instance
892  */
893 static int
894 test_session_get_size_inv_context(void)
895 {
896         unsigned int ret = rte_security_session_get_size(NULL);
897         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
898                         ret, 0, "%u");
899         TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 0);
900
901         return TEST_SUCCESS;
902 }
903
904 /**
905  * Test execution of rte_security_session_get_size with invalid
906  * security operations structure (NULL)
907  */
908 static int
909 test_session_get_size_inv_context_ops(void)
910 {
911         struct security_unittest_params *ut_params = &unittest_params;
912         ut_params->ctx.ops = NULL;
913
914         unsigned int ret = rte_security_session_get_size(&ut_params->ctx);
915         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
916                         ret, 0, "%u");
917         TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 0);
918
919         return TEST_SUCCESS;
920 }
921
922 /**
923  * Test execution of rte_security_session_get_size with empty
924  * security operations
925  */
926 static int
927 test_session_get_size_inv_context_ops_fun(void)
928 {
929         struct security_unittest_params *ut_params = &unittest_params;
930         ut_params->ctx.ops = &empty_ops;
931
932         unsigned int ret = rte_security_session_get_size(&ut_params->ctx);
933         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
934                         ret, 0, "%u");
935         TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 0);
936
937         return TEST_SUCCESS;
938 }
939
940 /**
941  * Test execution of rte_security_session_get_size when session_get_size
942  * security operation fails
943  */
944 static int
945 test_session_get_size_ops_failure(void)
946 {
947         struct security_unittest_params *ut_params = &unittest_params;
948
949         mock_session_get_size_exp.device = NULL;
950         mock_session_get_size_exp.ret = 0;
951
952         unsigned int ret = rte_security_session_get_size(&ut_params->ctx);
953         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
954                         ret, 0, "%u");
955         TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 1);
956
957         return TEST_SUCCESS;
958 }
959
960 /**
961  * Test execution of rte_security_session_get_size in successful execution path
962  */
963 static int
964 test_session_get_size_success(void)
965 {
966         struct security_unittest_params *ut_params = &unittest_params;
967
968         mock_session_get_size_exp.device = NULL;
969         mock_session_get_size_exp.ret = 1024;
970
971         unsigned int ret = rte_security_session_get_size(&ut_params->ctx);
972         TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
973                         ret, 1024U, "%u");
974         TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 1);
975
976         return TEST_SUCCESS;
977 }
978
979
980 /**
981  * Declaration of testcases
982  */
983 static struct unit_test_suite security_testsuite  = {
984         .suite_name = "generic security",
985         .setup = testsuite_setup,
986         .teardown = testsuite_teardown,
987         .unit_test_cases = {
988                 TEST_CASE_ST(ut_setup, ut_teardown,
989                                 test_session_create_inv_context),
990                 TEST_CASE_ST(ut_setup, ut_teardown,
991                                 test_session_create_inv_context_ops),
992                 TEST_CASE_ST(ut_setup, ut_teardown,
993                                 test_session_create_inv_context_ops_fun),
994                 TEST_CASE_ST(ut_setup, ut_teardown,
995                                 test_session_create_inv_configuration),
996                 TEST_CASE_ST(ut_setup, ut_teardown,
997                                 test_session_create_inv_mempool),
998                 TEST_CASE_ST(ut_setup, ut_teardown,
999                                 test_session_create_mempool_empty),
1000                 TEST_CASE_ST(ut_setup, ut_teardown,
1001                                 test_session_create_ops_failure),
1002                 TEST_CASE_ST(ut_setup, ut_teardown,
1003                                 test_session_create_success),
1004
1005                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1006                                 test_session_update_inv_context),
1007                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1008                                 test_session_update_inv_context_ops),
1009                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1010                                 test_session_update_inv_context_ops_fun),
1011                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1012                                 test_session_update_inv_configuration),
1013                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1014                                 test_session_update_inv_session),
1015                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1016                                 test_session_update_ops_failure),
1017                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1018                                 test_session_update_success),
1019
1020                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1021                                 test_session_get_size_inv_context),
1022                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1023                                 test_session_get_size_inv_context_ops),
1024                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1025                                 test_session_get_size_inv_context_ops_fun),
1026                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1027                                 test_session_get_size_ops_failure),
1028                 TEST_CASE_ST(ut_setup_with_session, ut_teardown,
1029                                 test_session_get_size_success),
1030
1031                 TEST_CASES_END() /**< NULL terminate unit test array */
1032         }
1033 };
1034
1035 static int
1036 test_security(void)
1037 {
1038         rte_log_set_global_level(RTE_LOG_DEBUG);
1039         rte_log_set_level(RTE_LOGTYPE_EAL, RTE_LOG_DEBUG);
1040
1041         return unit_test_suite_runner(&security_testsuite);
1042 }
1043
1044 REGISTER_TEST_COMMAND(security_autotest, test_security);