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