xref: /dpdk/app/test/test_security.c (revision e0a8442ccd15bafbb7eb150c35331c8e3b828c53)
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_ether.h>
10 #include <rte_security.h>
11 #include <rte_security_driver.h>
12 
13 /* Before including rte_test.h file you can define
14  * RTE_TEST_TRACE_FAILURE(_file, _line, _func) macro to better trace/debug test
15  * failures. Mostly useful in development phase.
16  */
17 #ifndef RTE_TEST_TRACE_FAILURE
18 #define RTE_TEST_TRACE_FAILURE(_file, _line, _func) \
19 	RTE_LOG(DEBUG, EAL, "in %s:%d %s\n", _file, _line, _func)
20 #endif
21 
22 #include <rte_test.h>
23 #include "test.h"
24 
25 /**
26  * Security
27  * =======
28  *
29  * Basic unit tests of the librte_security API.
30  *
31  * Structure of the file:
32  * - macros for making tests more readable;
33  * - mockup structures and functions for rte_security_ops;
34  * - test suite and test cases setup and teardown functions;
35  * - tests functions;
36  * - declaration of testcases.
37  */
38 
39 
40 /**
41  * Macros
42  *
43  * Set of macros for making tests easier to read.
44  */
45 
46 /**
47  * Verify condition inside mocked up function.
48  * Mockup function cannot return a test error, so the failure
49  * of assertion increases counter and print logs.
50  * The counter can be verified later to check if test case should fail.
51  *
52  * @param   fail_counter	fail counter
53  * @param   cond	condition expected to be true
54  * @param   msg	printf style formatting string for custom message
55  */
56 #define MOCK_TEST_ASSERT(fail_counter, cond, msg, ...) do {		\
57 	if (!(cond)) {							\
58 		fail_counter++;						\
59 		RTE_LOG(DEBUG, EAL, "Test assert %s line %d failed: "	\
60 				msg "\n", __func__, __LINE__,		\
61 				 ##__VA_ARGS__);			\
62 		RTE_TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);	\
63 	}								\
64 } while (0)
65 
66 /**
67  * Verify equality condition inside mocked up function.
68  * Mockup function cannot return a test error, so the failure
69  * of assertion increases counter and print logs.
70  * The counter can be verified later to check if test case should fail.
71  *
72  * @param   fail_counter	fail counter
73  * @param   a	first value of comparison
74  * @param   b	second value of comparison
75  * @param   msg	printf style formatting string for custom message
76  */
77 #define MOCK_TEST_ASSERT_EQUAL(fail_counter, a, b, msg, ...)	\
78 	MOCK_TEST_ASSERT(fail_counter, (a) == (b), msg, ##__VA_ARGS__)
79 
80 /**
81  * Verify not null condition inside mocked up function.
82  * Mockup function cannot return a test error, so the failure
83  * of assertion increases counter and print logs.
84  * The counter can be verified later to check if test case should fail.
85  *
86  * @param   fail_counter	fail counter
87  * @param   val	value expected not to be NULL
88  * @param   msg	printf style formatting string for custom message
89  */
90 #define MOCK_TEST_ASSERT_NOT_NULL(fail_counter, val, msg, ...)	\
91 	MOCK_TEST_ASSERT(fail_counter, (val) != NULL, msg, ##__VA_ARGS__)
92 
93 
94 /**
95  * Verify if parameter of the mocked up function matches expected value.
96  * The expected value is stored in data structure in the field matching
97  * parameter name.
98  *
99  * @param   data	structure with expected values
100  * @param   parameter	name of the parameter (both field and parameter name)
101  * @param   spec	printf style spec for parameter
102  */
103 #define MOCK_TEST_ASSERT_PARAMETER(data, parameter, spec)		\
104 	MOCK_TEST_ASSERT_EQUAL(data.failed, data.parameter, parameter,	\
105 			"Expecting parameter %s to be " spec		\
106 			" but it's " spec, RTE_STR(parameter),		\
107 			data.parameter, parameter)
108 
109 /**
110  * Wrap for MOCK_TEST_ASSERT_PARAMETER macro for pointer type parameters.
111  *
112  * @param   data	structure with expected values
113  * @param   parameter	name of the parameter (both field and parameter name)
114  */
115 #define MOCK_TEST_ASSERT_POINTER_PARAMETER(data, parameter)	\
116 	MOCK_TEST_ASSERT_PARAMETER(data, parameter, "%p")
117 
118 /**
119  * Wrap for MOCK_TEST_ASSERT_PARAMETER macro for uint64_t type parameters.
120  *
121  * @param   data	structure with expected values
122  * @param   parameter	name of the parameter (both field and parameter name)
123  */
124 #define MOCK_TEST_ASSERT_U64_PARAMETER(data, parameter)	\
125 	MOCK_TEST_ASSERT_PARAMETER(data, parameter, "%" PRIu64)
126 
127 /**
128  * Verify number of calls of the mocked up function
129  * and check if there were any fails during execution.
130  * The fails statistics inside mocked up functions are collected
131  * as "failed" field in mockup structures.
132  *
133  * @param   mock_data	structure with statistics (called, failed)
134  * @param   exp_calls	expected number of mockup function calls
135  */
136 #define TEST_ASSERT_MOCK_CALLS(mock_data, exp_calls) do {		\
137 	TEST_ASSERT_EQUAL(exp_calls, mock_data.called,			\
138 			"Expecting sub op to be called %d times, "	\
139 			"but it's called %d times",			\
140 			exp_calls, mock_data.called);			\
141 	TEST_ASSERT_EQUAL(0, mock_data.failed,				\
142 			"Expecting sub op asserts not to fail, "	\
143 			"but they're failed %d times",			\
144 			mock_data.failed);				\
145 } while (0)
146 
147 /**
148  * Assert tested function result match expected value
149  *
150  * @param   f_name	name of tested function
151  * @param   f_ret	value returned by the function
152  * @param   exp_ret	expected returned value
153  * @param   fmt		printf style format for returned value
154  */
155 #define TEST_ASSERT_MOCK_FUNCTION_CALL_RET(f_name, f_ret, exp_ret, fmt)	\
156 	TEST_ASSERT_EQUAL(exp_ret, f_ret, "Expecting " RTE_STR(f_name)	\
157 			" to return " fmt ", but it returned " fmt	\
158 			"\n", exp_ret, f_ret)
159 
160 /**
161  * Assert tested function result is not NULL
162  *
163  * @param   f_name	name of tested function
164  * @param   f_ret	value returned by the function
165  */
166 #define TEST_ASSERT_MOCK_FUNCTION_CALL_NOT_NULL(f_name, f_ret)		\
167 	TEST_ASSERT_NOT_NULL(f_ret, "Expecting " RTE_STR(f_name)	\
168 			" to return not NULL\n")
169 
170 /**
171  * Verify that sess_cnt counter value matches expected
172  *
173  * @param   expected_sessions_count	expected counter value
174  */
175 #define TEST_ASSERT_SESSION_COUNT(expected_sessions_count) do {		\
176 	struct security_unittest_params *ut_params = &unittest_params;	\
177 	TEST_ASSERT_EQUAL(expected_sessions_count,			\
178 			ut_params->ctx.sess_cnt,			\
179 			"Expecting session counter to be %u,"		\
180 			" but it's %u",	expected_sessions_count,	\
181 			ut_params->ctx.sess_cnt);			\
182 } while (0)
183 
184 /**
185  * Verify usage of mempool by checking if number of allocated objects matches
186  * expectations. The mempool is used to manage objects for sessions data.
187  * A single object is acquired from mempool during session_create
188  * and put back in session_destroy.
189  *
190  * @param   expected_mempool_usage	expected number of used mempool objects
191  */
192 #define TEST_ASSERT_MEMPOOL_USAGE(expected_mempool_usage) do {		\
193 	struct security_testsuite_params *ts_params = &testsuite_params;\
194 	unsigned int mempool_usage;					\
195 	mempool_usage = rte_mempool_in_use_count(			\
196 			ts_params->session_mpool);			\
197 	TEST_ASSERT_EQUAL(expected_mempool_usage, mempool_usage,	\
198 			"Expecting %u mempool allocations, "		\
199 			"but there are %u allocated objects",		\
200 			expected_mempool_usage, mempool_usage);		\
201 } while (0)
202 
203 /**
204  * Mockup structures and functions for rte_security_ops;
205  *
206  * Set of structures for controlling mockup functions calls.
207  * Every mockup function X has its corresponding X_data structure
208  * and an instance of that structure X_exp.
209  * Structure contains parameters that a mockup function is expected
210  * to be called with, a value to return (.ret) and 2 statistics:
211  * .called (number of times the mockup function was called)
212  * and .failed (number of assertion fails during mockup function call).
213  *
214  * Mockup functions verify that the parameters they are called with match
215  * expected values. The expected values should be stored in corresponding
216  * structures prior to mockup functions call. Every failure of such
217  * verification increases .failed counter. Every call of mockup function
218  * increases .called counter. Function returns value stored in .ret field
219  * of the structure.
220  * In case of some parameters in some functions the expected value is unknown
221  * and cannot be determined prior to call. Such parameters are stored
222  * in structure and can be compared or analyzed later in test case code.
223  *
224  * Below structures and functions follow the rules just described.
225  * Additional remarks and exceptions are added in comments.
226  */
227 
228 /**
229  * session_create mockup
230  *
231  * Verified parameters: device, conf, mp.
232  * Saved, not verified parameters: sess.
233  */
234 static struct mock_session_create_data {
235 	void *device;
236 	struct rte_security_session_conf *conf;
237 	void *sess;
238 	struct rte_mempool *mp;
239 	struct rte_mempool *priv_mp;
240 
241 	int ret;
242 
243 	int called;
244 	int failed;
245 } mock_session_create_exp = {NULL, NULL, NULL, NULL, NULL, 0, 0, 0};
246 
247 static int
mock_session_create(void * device,struct rte_security_session_conf * conf,struct rte_security_session * sess)248 mock_session_create(void *device,
249 		struct rte_security_session_conf *conf,
250 		struct rte_security_session *sess)
251 {
252 	mock_session_create_exp.called++;
253 
254 	MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_create_exp, device);
255 	MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_create_exp, conf);
256 
257 	if (mock_session_create_exp.ret == 0) {
258 		mock_session_create_exp.sess = sess;
259 	}
260 
261 	return mock_session_create_exp.ret;
262 }
263 
264 /**
265  * session_update mockup
266  *
267  * Verified parameters: device, sess, conf.
268  */
269 static struct mock_session_update_data {
270 	void *device;
271 	void *sess;
272 	struct rte_security_session_conf *conf;
273 
274 	int ret;
275 
276 	int called;
277 	int failed;
278 } mock_session_update_exp = {NULL, NULL, NULL, 0, 0, 0};
279 
280 static int
mock_session_update(void * device,struct rte_security_session * sess,struct rte_security_session_conf * conf)281 mock_session_update(void *device,
282 		struct rte_security_session *sess,
283 		struct rte_security_session_conf *conf)
284 {
285 	mock_session_update_exp.called++;
286 
287 	MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_update_exp, device);
288 	MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_update_exp, sess);
289 	MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_update_exp, conf);
290 
291 	return mock_session_update_exp.ret;
292 }
293 
294 /**
295  * session_get_size mockup
296  *
297  * Verified parameters: device.
298  */
299 static struct mock_session_get_size_data {
300 	void *device;
301 
302 	unsigned int ret;
303 
304 	int called;
305 	int failed;
306 } mock_session_get_size_exp = {NULL, 0U, 0, 0};
307 
308 static unsigned int
mock_session_get_size(void * device)309 mock_session_get_size(void *device)
310 {
311 	mock_session_get_size_exp.called++;
312 
313 	MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_get_size_exp, device);
314 
315 	return mock_session_get_size_exp.ret;
316 }
317 
318 /**
319  * session_stats_get mockup
320  *
321  * Verified parameters: device, sess, stats.
322  */
323 static struct mock_session_stats_get_data {
324 	void *device;
325 	void *sess;
326 	struct rte_security_stats *stats;
327 
328 	int ret;
329 
330 	int called;
331 	int failed;
332 } mock_session_stats_get_exp = {NULL, NULL, NULL, 0, 0, 0};
333 
334 static int
mock_session_stats_get(void * device,struct rte_security_session * sess,struct rte_security_stats * stats)335 mock_session_stats_get(void *device,
336 		struct rte_security_session *sess,
337 		struct rte_security_stats *stats)
338 {
339 	mock_session_stats_get_exp.called++;
340 
341 	MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_stats_get_exp, device);
342 	MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_stats_get_exp, sess);
343 	MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_stats_get_exp, stats);
344 
345 	return mock_session_stats_get_exp.ret;
346 }
347 
348 /**
349  * session_destroy mockup
350  *
351  * Verified parameters: device, sess.
352  */
353 static struct mock_session_destroy_data {
354 	void *device;
355 	void *sess;
356 
357 	int ret;
358 
359 	int called;
360 	int failed;
361 } mock_session_destroy_exp = {NULL, NULL, 0, 0, 0};
362 
363 static int
mock_session_destroy(void * device,struct rte_security_session * sess)364 mock_session_destroy(void *device, struct rte_security_session *sess)
365 {
366 	mock_session_destroy_exp.called++;
367 	MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_destroy_exp, device);
368 	MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_session_destroy_exp, sess);
369 
370 	return mock_session_destroy_exp.ret;
371 }
372 
373 /**
374  * set_pkt_metadata mockup
375  *
376  * Verified parameters: device, sess, m, params.
377  */
378 static struct mock_set_pkt_metadata_data {
379 	void *device;
380 	void *sess;
381 	struct rte_mbuf *m;
382 	void *params;
383 
384 	int ret;
385 
386 	int called;
387 	int failed;
388 } mock_set_pkt_metadata_exp = {NULL, NULL, NULL, NULL, 0, 0, 0};
389 
390 static int
mock_set_pkt_metadata(void * device,struct rte_security_session * sess,struct rte_mbuf * m,void * params)391 mock_set_pkt_metadata(void *device,
392 		struct rte_security_session *sess,
393 		struct rte_mbuf *m,
394 		void *params)
395 {
396 	mock_set_pkt_metadata_exp.called++;
397 
398 	MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_set_pkt_metadata_exp, device);
399 	MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_set_pkt_metadata_exp, sess);
400 	MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_set_pkt_metadata_exp, m);
401 	MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_set_pkt_metadata_exp, params);
402 
403 	return mock_set_pkt_metadata_exp.ret;
404 }
405 
406 /**
407  * capabilities_get mockup
408  *
409  * Verified parameters: device.
410  */
411 static struct mock_capabilities_get_data {
412 	void *device;
413 
414 	struct rte_security_capability *ret;
415 
416 	int called;
417 	int failed;
418 } mock_capabilities_get_exp = {NULL, NULL, 0, 0};
419 
420 static const struct rte_security_capability *
mock_capabilities_get(void * device)421 mock_capabilities_get(void *device)
422 {
423 	mock_capabilities_get_exp.called++;
424 
425 	MOCK_TEST_ASSERT_POINTER_PARAMETER(mock_capabilities_get_exp, device);
426 
427 	return mock_capabilities_get_exp.ret;
428 }
429 
430 /**
431  * empty_ops
432  *
433  * is an empty security operations set (all function pointers set to NULL)
434  */
435 struct rte_security_ops empty_ops = { NULL };
436 
437 /**
438  * mock_ops
439  *
440  * is a security operations set using mockup functions
441  */
442 struct rte_security_ops mock_ops = {
443 	.session_create = mock_session_create,
444 	.session_update = mock_session_update,
445 	.session_get_size = mock_session_get_size,
446 	.session_stats_get = mock_session_stats_get,
447 	.session_destroy = mock_session_destroy,
448 	.set_pkt_metadata = mock_set_pkt_metadata,
449 	.capabilities_get = mock_capabilities_get,
450 };
451 
452 
453 /**
454  * Test suite and test cases setup and teardown functions.
455  */
456 
457 /**
458  * struct security_testsuite_params defines parameters initialized once
459  * for whole tests suite.
460  * Currently the only stored parameter is session_mpool a mempool created
461  * once in testsuite_setup and released in testsuite_teardown.
462  * The instance of this structure is stored in testsuite_params variable.
463  */
464 static struct security_testsuite_params {
465 	struct rte_mempool *session_mpool;
466 } testsuite_params = { NULL };
467 
468 /**
469  * struct security_unittest_params defines parameters initialized
470  * for every test case. The parameters are initialized in ut_setup
471  * or ut_setup_with_session (depending on the testcase)
472  * and released in ut_teardown.
473  * The instance of this structure is stored in unittest_params variable.
474  */
475 static struct security_unittest_params {
476 	struct rte_security_ctx ctx;
477 	struct rte_security_session_conf conf;
478 	void *sess;
479 } unittest_params = {
480 	.ctx = {
481 		.device = NULL,
482 		.ops = &mock_ops,
483 		.sess_cnt = 0,
484 	},
485 	.sess = NULL,
486 };
487 
488 #define SECURITY_TEST_MEMPOOL_NAME "SecurityTestMp"
489 #define SECURITY_TEST_PRIV_MEMPOOL_NAME "SecurityTestPrivMp"
490 #define SECURITY_TEST_MEMPOOL_SIZE 15
491 #define SECURITY_TEST_SESSION_PRIV_OBJ_SZ 64
492 #define SECURITY_TEST_SESSION_OBJ_SZ (sizeof(struct rte_security_session) + \
493 					SECURITY_TEST_SESSION_PRIV_OBJ_SZ)
494 
495 /**
496  * testsuite_setup initializes whole test suite parameters.
497  * It creates a new mempool used in all test cases
498  * and verifies if it properly created.
499  */
500 static int
testsuite_setup(void)501 testsuite_setup(void)
502 {
503 	struct security_testsuite_params *ts_params = &testsuite_params;
504 	ts_params->session_mpool = rte_mempool_create(
505 			SECURITY_TEST_MEMPOOL_NAME,
506 			SECURITY_TEST_MEMPOOL_SIZE,
507 			SECURITY_TEST_SESSION_OBJ_SZ,
508 			0, 0, NULL, NULL, NULL, NULL,
509 			SOCKET_ID_ANY, 0);
510 	TEST_ASSERT_NOT_NULL(ts_params->session_mpool,
511 			"Cannot create mempool %s\n", rte_strerror(rte_errno));
512 
513 	return TEST_SUCCESS;
514 }
515 
516 /**
517  * testsuite_teardown releases test suite wide parameters.
518  */
519 static void
testsuite_teardown(void)520 testsuite_teardown(void)
521 {
522 	struct security_testsuite_params *ts_params = &testsuite_params;
523 	if (ts_params->session_mpool) {
524 		rte_mempool_free(ts_params->session_mpool);
525 		ts_params->session_mpool = NULL;
526 	}
527 }
528 
529 /**
530  * ut_setup initializes test case parameters to default values.
531  * It resets also any .called and .failed statistics of mockup functions
532  * usage.
533  */
534 static int
ut_setup(void)535 ut_setup(void)
536 {
537 	struct security_unittest_params *ut_params = &unittest_params;
538 	ut_params->ctx.device = NULL;
539 	ut_params->ctx.ops = &mock_ops;
540 	ut_params->ctx.sess_cnt = 0;
541 	ut_params->sess = NULL;
542 
543 	mock_session_create_exp.called = 0;
544 	mock_session_update_exp.called = 0;
545 	mock_session_get_size_exp.called = 0;
546 	mock_session_stats_get_exp.called = 0;
547 	mock_session_destroy_exp.called = 0;
548 	mock_set_pkt_metadata_exp.called = 0;
549 	mock_capabilities_get_exp.called = 0;
550 
551 	mock_session_create_exp.failed = 0;
552 	mock_session_update_exp.failed = 0;
553 	mock_session_get_size_exp.failed = 0;
554 	mock_session_stats_get_exp.failed = 0;
555 	mock_session_destroy_exp.failed = 0;
556 	mock_set_pkt_metadata_exp.failed = 0;
557 	mock_capabilities_get_exp.failed = 0;
558 
559 	return TEST_SUCCESS;
560 }
561 
562 /**
563  * destroy_session_with_check is a helper function releasing session
564  * created with rte_security_session_create and stored in test case parameters.
565  * It's used both to release sessions created in test cases' bodies
566  * which are assigned to ut_params->sess
567  * as well as sessions created in ut_setup_with_session.
568  */
569 static int
destroy_session_with_check(void)570 destroy_session_with_check(void)
571 {
572 	struct security_unittest_params *ut_params = &unittest_params;
573 	if (ut_params->sess != NULL) {
574 		/* Assure that mockup function for destroy operation is set. */
575 		ut_params->ctx.ops = &mock_ops;
576 
577 		mock_session_destroy_exp.device = NULL;
578 		mock_session_destroy_exp.sess = ut_params->sess;
579 		mock_session_destroy_exp.ret = 0;
580 		mock_session_destroy_exp.called = 0;
581 		mock_session_destroy_exp.failed = 0;
582 
583 		int ret = rte_security_session_destroy(&ut_params->ctx,
584 				ut_params->sess);
585 		TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy,
586 				ret, 0, "%d");
587 		TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 1);
588 
589 		ut_params->sess = NULL;
590 	}
591 	return TEST_SUCCESS;
592 }
593 
594 /**
595  * ut_teardown releases test case parameters.
596  */
597 static void
ut_teardown(void)598 ut_teardown(void)
599 {
600 	destroy_session_with_check();
601 }
602 
603 /**
604  * ut_setup_with_session initializes test case parameters by
605  * - calling standard ut_setup,
606  * - creating a session that can be used in test case.
607  */
608 static int
ut_setup_with_session(void)609 ut_setup_with_session(void)
610 {
611 	struct security_unittest_params *ut_params = &unittest_params;
612 	struct security_testsuite_params *ts_params = &testsuite_params;
613 	void *sess;
614 
615 	int ret = ut_setup();
616 	if (ret != TEST_SUCCESS)
617 		return ret;
618 
619 	mock_session_create_exp.device = NULL;
620 	mock_session_create_exp.conf = &ut_params->conf;
621 	mock_session_create_exp.mp = ts_params->session_mpool;
622 	mock_session_create_exp.ret = 0;
623 
624 	sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
625 			ts_params->session_mpool);
626 	TEST_ASSERT_MOCK_FUNCTION_CALL_NOT_NULL(rte_security_session_create,
627 			sess);
628 	TEST_ASSERT_EQUAL(sess, mock_session_create_exp.sess,
629 			"Expecting session_create to be called with %p sess"
630 			" parameter, but it's called %p sess parameter",
631 			sess, mock_session_create_exp.sess);
632 	TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 1);
633 
634 	/*
635 	 * Store created session in test case parameters, so it can be released
636 	 * after test case in ut_teardown by destroy_session_with_check.
637 	 */
638 	ut_params->sess = sess;
639 
640 	return TEST_SUCCESS;
641 }
642 
643 
644 /**
645  * Test functions
646  *
647  * Each test function is related to a single test case.
648  * They are arranged by tested rte_security API function
649  * and by rte_security execution paths sequence in code.
650  */
651 
652 /**
653  * rte_security_session_create tests
654  */
655 
656 /**
657  * Test execution of rte_security_session_create with NULL instance
658  */
659 static int
test_session_create_inv_context(void)660 test_session_create_inv_context(void)
661 {
662 	struct security_testsuite_params *ts_params = &testsuite_params;
663 	struct security_unittest_params *ut_params = &unittest_params;
664 	void *sess;
665 
666 	sess = rte_security_session_create(NULL, &ut_params->conf,
667 			ts_params->session_mpool);
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 with invalid
679  * security operations structure (NULL)
680  */
681 static int
test_session_create_inv_context_ops(void)682 test_session_create_inv_context_ops(void)
683 {
684 	struct security_testsuite_params *ts_params = &testsuite_params;
685 	struct security_unittest_params *ut_params = &unittest_params;
686 	void *sess;
687 
688 	ut_params->ctx.ops = NULL;
689 
690 	sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
691 			ts_params->session_mpool);
692 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
693 			sess, NULL, "%p");
694 	TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
695 	TEST_ASSERT_MEMPOOL_USAGE(0);
696 	TEST_ASSERT_SESSION_COUNT(0);
697 
698 	return TEST_SUCCESS;
699 }
700 
701 /**
702  * Test execution of rte_security_session_create with empty
703  * security operations
704  */
705 static int
test_session_create_inv_context_ops_fun(void)706 test_session_create_inv_context_ops_fun(void)
707 {
708 	struct security_testsuite_params *ts_params = &testsuite_params;
709 	struct security_unittest_params *ut_params = &unittest_params;
710 	void *sess;
711 
712 	ut_params->ctx.ops = &empty_ops;
713 
714 	sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
715 			ts_params->session_mpool);
716 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
717 			sess, NULL, "%p");
718 	TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
719 	TEST_ASSERT_MEMPOOL_USAGE(0);
720 	TEST_ASSERT_SESSION_COUNT(0);
721 
722 	return TEST_SUCCESS;
723 }
724 
725 /**
726  * Test execution of rte_security_session_create with NULL conf parameter
727  */
728 static int
test_session_create_inv_configuration(void)729 test_session_create_inv_configuration(void)
730 {
731 	struct security_testsuite_params *ts_params = &testsuite_params;
732 	struct security_unittest_params *ut_params = &unittest_params;
733 	void *sess;
734 
735 	sess = rte_security_session_create(&ut_params->ctx, NULL,
736 			ts_params->session_mpool);
737 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
738 			sess, NULL, "%p");
739 	TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
740 	TEST_ASSERT_MEMPOOL_USAGE(0);
741 	TEST_ASSERT_SESSION_COUNT(0);
742 
743 	return TEST_SUCCESS;
744 }
745 
746 /**
747  * Test execution of rte_security_session_create with NULL session
748  * mempool
749  */
750 static int
test_session_create_inv_mempool(void)751 test_session_create_inv_mempool(void)
752 {
753 	struct security_unittest_params *ut_params = &unittest_params;
754 	void *sess;
755 
756 	sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf, NULL);
757 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
758 			sess, NULL, "%p");
759 	TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
760 	TEST_ASSERT_MEMPOOL_USAGE(0);
761 	TEST_ASSERT_SESSION_COUNT(0);
762 
763 	return TEST_SUCCESS;
764 }
765 
766 /**
767  * Test execution of rte_security_session_create in case when mempool
768  * is fully used and no object can be got from it
769  */
770 static int
test_session_create_mempool_empty(void)771 test_session_create_mempool_empty(void)
772 {
773 	struct security_testsuite_params *ts_params = &testsuite_params;
774 	struct security_unittest_params *ut_params = &unittest_params;
775 	void *tmp[SECURITY_TEST_MEMPOOL_SIZE];
776 	void *sess;
777 
778 	/* Get all available objects from mempool. */
779 	int i, ret;
780 	for (i = 0; i < SECURITY_TEST_MEMPOOL_SIZE; ++i) {
781 		ret = rte_mempool_get(ts_params->session_mpool,
782 				(void **)(&tmp[i]));
783 		TEST_ASSERT_EQUAL(0, ret,
784 				"Expect getting %d object from mempool"
785 				" to succeed", i);
786 	}
787 	TEST_ASSERT_MEMPOOL_USAGE(SECURITY_TEST_MEMPOOL_SIZE);
788 
789 	sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
790 			ts_params->session_mpool);
791 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
792 			sess, NULL, "%p");
793 	TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 0);
794 	TEST_ASSERT_MEMPOOL_USAGE(SECURITY_TEST_MEMPOOL_SIZE);
795 	TEST_ASSERT_SESSION_COUNT(0);
796 
797 	/* Put objects back to the pool. */
798 	for (i = 0; i < SECURITY_TEST_MEMPOOL_SIZE; ++i) {
799 		rte_mempool_put(ts_params->session_mpool,
800 				(void *)(tmp[i]));
801 	}
802 	TEST_ASSERT_MEMPOOL_USAGE(0);
803 
804 	return TEST_SUCCESS;
805 }
806 
807 /**
808  * Test execution of rte_security_session_create when session_create
809  * security operation fails
810  */
811 static int
test_session_create_ops_failure(void)812 test_session_create_ops_failure(void)
813 {
814 	struct security_testsuite_params *ts_params = &testsuite_params;
815 	struct security_unittest_params *ut_params = &unittest_params;
816 	void *sess;
817 
818 	mock_session_create_exp.device = NULL;
819 	mock_session_create_exp.conf = &ut_params->conf;
820 	mock_session_create_exp.mp = ts_params->session_mpool;
821 	mock_session_create_exp.ret = -1;	/* Return failure status. */
822 
823 	sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
824 			ts_params->session_mpool);
825 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_create,
826 			sess, NULL, "%p");
827 	TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 1);
828 	TEST_ASSERT_MEMPOOL_USAGE(0);
829 	TEST_ASSERT_SESSION_COUNT(0);
830 
831 	return TEST_SUCCESS;
832 }
833 
834 /**
835  * Test execution of rte_security_session_create in successful execution path
836  */
837 static int
test_session_create_success(void)838 test_session_create_success(void)
839 {
840 	struct security_testsuite_params *ts_params = &testsuite_params;
841 	struct security_unittest_params *ut_params = &unittest_params;
842 	void *sess;
843 
844 	mock_session_create_exp.device = NULL;
845 	mock_session_create_exp.conf = &ut_params->conf;
846 	mock_session_create_exp.mp = ts_params->session_mpool;
847 	mock_session_create_exp.ret = 0;	/* Return success status. */
848 
849 	sess = rte_security_session_create(&ut_params->ctx, &ut_params->conf,
850 			ts_params->session_mpool);
851 	TEST_ASSERT_MOCK_FUNCTION_CALL_NOT_NULL(rte_security_session_create,
852 			sess);
853 	TEST_ASSERT_EQUAL(sess, mock_session_create_exp.sess,
854 			"Expecting session_create to be called with %p sess"
855 			" parameter, but it's called %p sess parameter",
856 			sess, mock_session_create_exp.sess);
857 	TEST_ASSERT_MOCK_CALLS(mock_session_create_exp, 1);
858 	TEST_ASSERT_MEMPOOL_USAGE(1);
859 	TEST_ASSERT_SESSION_COUNT(1);
860 
861 	/*
862 	 * Store created session in test case parameters, so it can be released
863 	 * after test case in ut_teardown by destroy_session_with_check.
864 	 */
865 	ut_params->sess = sess;
866 
867 	return TEST_SUCCESS;
868 }
869 
870 
871 /**
872  * rte_security_session_update tests
873  */
874 
875 /**
876  * Test execution of rte_security_session_update with NULL instance
877  */
878 static int
test_session_update_inv_context(void)879 test_session_update_inv_context(void)
880 {
881 	struct security_unittest_params *ut_params = &unittest_params;
882 
883 	int ret = rte_security_session_update(NULL, ut_params->sess,
884 			&ut_params->conf);
885 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
886 			ret, -EINVAL, "%d");
887 	TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
888 
889 	return TEST_SUCCESS;
890 }
891 
892 /**
893  * Test execution of rte_security_session_update with invalid
894  * security operations structure (NULL)
895  */
896 static int
test_session_update_inv_context_ops(void)897 test_session_update_inv_context_ops(void)
898 {
899 	struct security_unittest_params *ut_params = &unittest_params;
900 	ut_params->ctx.ops = NULL;
901 
902 	int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
903 			&ut_params->conf);
904 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
905 			ret, -EINVAL, "%d");
906 	TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
907 
908 	return TEST_SUCCESS;
909 }
910 
911 /**
912  * Test execution of rte_security_session_update with empty
913  * security operations
914  */
915 static int
test_session_update_inv_context_ops_fun(void)916 test_session_update_inv_context_ops_fun(void)
917 {
918 	struct security_unittest_params *ut_params = &unittest_params;
919 	ut_params->ctx.ops = &empty_ops;
920 
921 	int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
922 			&ut_params->conf);
923 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
924 			ret, -ENOTSUP, "%d");
925 	TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
926 
927 	return TEST_SUCCESS;
928 }
929 
930 /**
931  * Test execution of rte_security_session_update with NULL conf parameter
932  */
933 static int
test_session_update_inv_configuration(void)934 test_session_update_inv_configuration(void)
935 {
936 	struct security_unittest_params *ut_params = &unittest_params;
937 
938 	int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
939 			NULL);
940 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
941 			ret, -EINVAL, "%d");
942 	TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
943 
944 	return TEST_SUCCESS;
945 }
946 
947 /**
948  * Test execution of rte_security_session_update with NULL sess parameter
949  */
950 static int
test_session_update_inv_session(void)951 test_session_update_inv_session(void)
952 {
953 	struct security_unittest_params *ut_params = &unittest_params;
954 
955 	int ret = rte_security_session_update(&ut_params->ctx, NULL,
956 			&ut_params->conf);
957 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
958 			ret, -EINVAL, "%d");
959 	TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 0);
960 
961 	return TEST_SUCCESS;
962 }
963 
964 /**
965  * Test execution of rte_security_session_update when session_update
966  * security operation fails
967  */
968 static int
test_session_update_ops_failure(void)969 test_session_update_ops_failure(void)
970 {
971 	struct security_unittest_params *ut_params = &unittest_params;
972 
973 	mock_session_update_exp.device = NULL;
974 	mock_session_update_exp.sess = ut_params->sess;
975 	mock_session_update_exp.conf = &ut_params->conf;
976 	mock_session_update_exp.ret = -1;	/* Return failure status. */
977 
978 	int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
979 			&ut_params->conf);
980 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
981 			ret, -1, "%d");
982 	TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 1);
983 
984 	return TEST_SUCCESS;
985 }
986 
987 /**
988  * Test execution of rte_security_session_update in successful execution path
989  */
990 static int
test_session_update_success(void)991 test_session_update_success(void)
992 {
993 	struct security_unittest_params *ut_params = &unittest_params;
994 
995 	mock_session_update_exp.device = NULL;
996 	mock_session_update_exp.sess = ut_params->sess;
997 	mock_session_update_exp.conf = &ut_params->conf;
998 	mock_session_update_exp.ret = 0;	/* Return success status. */
999 
1000 	int ret = rte_security_session_update(&ut_params->ctx, ut_params->sess,
1001 			&ut_params->conf);
1002 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_update,
1003 			ret, 0, "%d");
1004 	TEST_ASSERT_MOCK_CALLS(mock_session_update_exp, 1);
1005 
1006 	return TEST_SUCCESS;
1007 }
1008 
1009 
1010 /**
1011  * rte_security_session_get_size tests
1012  */
1013 
1014 /**
1015  * Test execution of rte_security_session_get_size with NULL instance
1016  */
1017 static int
test_session_get_size_inv_context(void)1018 test_session_get_size_inv_context(void)
1019 {
1020 	unsigned int ret = rte_security_session_get_size(NULL);
1021 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
1022 			ret, 0, "%u");
1023 	TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 1);
1024 
1025 	return TEST_SUCCESS;
1026 }
1027 
1028 /**
1029  * Test execution of rte_security_session_get_size with invalid
1030  * security operations structure (NULL)
1031  */
1032 static int
test_session_get_size_inv_context_ops(void)1033 test_session_get_size_inv_context_ops(void)
1034 {
1035 	struct security_unittest_params *ut_params = &unittest_params;
1036 	ut_params->ctx.ops = NULL;
1037 
1038 	unsigned int ret = rte_security_session_get_size(&ut_params->ctx);
1039 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
1040 			ret, 0, "%u");
1041 	TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 1);
1042 
1043 	return TEST_SUCCESS;
1044 }
1045 
1046 /**
1047  * Test execution of rte_security_session_get_size with empty
1048  * security operations
1049  */
1050 static int
test_session_get_size_inv_context_ops_fun(void)1051 test_session_get_size_inv_context_ops_fun(void)
1052 {
1053 	struct security_unittest_params *ut_params = &unittest_params;
1054 	ut_params->ctx.ops = &empty_ops;
1055 
1056 	unsigned int ret = rte_security_session_get_size(&ut_params->ctx);
1057 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
1058 			ret, 0, "%u");
1059 	TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 1);
1060 
1061 	return TEST_SUCCESS;
1062 }
1063 
1064 /**
1065  * Test execution of rte_security_session_get_size when session_get_size
1066  * security operation fails
1067  */
1068 static int
test_session_get_size_ops_failure(void)1069 test_session_get_size_ops_failure(void)
1070 {
1071 	struct security_unittest_params *ut_params = &unittest_params;
1072 
1073 	mock_session_get_size_exp.device = NULL;
1074 	mock_session_get_size_exp.ret = 0;
1075 
1076 	unsigned int ret = rte_security_session_get_size(&ut_params->ctx);
1077 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
1078 			ret, 64, "%u");
1079 	TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 2);
1080 
1081 	return TEST_SUCCESS;
1082 }
1083 
1084 /**
1085  * Test execution of rte_security_session_get_size in successful execution path
1086  */
1087 static int
test_session_get_size_success(void)1088 test_session_get_size_success(void)
1089 {
1090 	struct security_unittest_params *ut_params = &unittest_params;
1091 
1092 	mock_session_get_size_exp.device = NULL;
1093 	mock_session_get_size_exp.ret = 64;
1094 
1095 	unsigned int ret = rte_security_session_get_size(&ut_params->ctx);
1096 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_get_size,
1097 			ret, 128U, "%u");
1098 	TEST_ASSERT_MOCK_CALLS(mock_session_get_size_exp, 2);
1099 
1100 	return TEST_SUCCESS;
1101 }
1102 
1103 
1104 /**
1105  * rte_security_session_stats_get tests
1106  */
1107 
1108 /**
1109  * Test execution of rte_security_session_stats_get with NULL instance
1110  */
1111 static int
test_session_stats_get_inv_context(void)1112 test_session_stats_get_inv_context(void)
1113 {
1114 	struct security_unittest_params *ut_params = &unittest_params;
1115 	struct rte_security_stats stats;
1116 
1117 	int ret = rte_security_session_stats_get(NULL, ut_params->sess, &stats);
1118 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1119 			ret, -EINVAL, "%d");
1120 	TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 0);
1121 
1122 	return TEST_SUCCESS;
1123 }
1124 
1125 /**
1126  * Test execution of rte_security_session_stats_get with invalid
1127  * security operations structure (NULL)
1128  */
1129 static int
test_session_stats_get_inv_context_ops(void)1130 test_session_stats_get_inv_context_ops(void)
1131 {
1132 	struct security_unittest_params *ut_params = &unittest_params;
1133 	struct rte_security_stats stats;
1134 	ut_params->ctx.ops = NULL;
1135 
1136 	int ret = rte_security_session_stats_get(&ut_params->ctx,
1137 			ut_params->sess, &stats);
1138 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1139 			ret, -EINVAL, "%d");
1140 	TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 0);
1141 
1142 	return TEST_SUCCESS;
1143 }
1144 
1145 /**
1146  * Test execution of rte_security_session_stats_get with empty
1147  * security operations
1148  */
1149 static int
test_session_stats_get_inv_context_ops_fun(void)1150 test_session_stats_get_inv_context_ops_fun(void)
1151 {
1152 	struct security_unittest_params *ut_params = &unittest_params;
1153 	struct rte_security_stats stats;
1154 	ut_params->ctx.ops = &empty_ops;
1155 
1156 	int ret = rte_security_session_stats_get(&ut_params->ctx,
1157 			ut_params->sess, &stats);
1158 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1159 			ret, -ENOTSUP, "%d");
1160 	TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 0);
1161 
1162 	return TEST_SUCCESS;
1163 }
1164 
1165 /**
1166  * Test execution of rte_security_session_stats_get with NULL stats parameter
1167  */
1168 static int
test_session_stats_get_inv_stats(void)1169 test_session_stats_get_inv_stats(void)
1170 {
1171 	struct security_unittest_params *ut_params = &unittest_params;
1172 
1173 	int ret = rte_security_session_stats_get(&ut_params->ctx,
1174 			ut_params->sess, NULL);
1175 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1176 			ret, -EINVAL, "%d");
1177 	TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 0);
1178 
1179 	return TEST_SUCCESS;
1180 }
1181 
1182 /**
1183  * Test execution of rte_security_session_stats_get when session_stats_get
1184  * security operation fails
1185  */
1186 static int
test_session_stats_get_ops_failure(void)1187 test_session_stats_get_ops_failure(void)
1188 {
1189 	struct security_unittest_params *ut_params = &unittest_params;
1190 	struct rte_security_stats stats;
1191 
1192 	mock_session_stats_get_exp.device = NULL;
1193 	mock_session_stats_get_exp.sess = ut_params->sess;
1194 	mock_session_stats_get_exp.stats = &stats;
1195 	mock_session_stats_get_exp.ret = -1;
1196 
1197 	int ret = rte_security_session_stats_get(&ut_params->ctx,
1198 			ut_params->sess, &stats);
1199 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1200 			ret, -1, "%d");
1201 	TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 1);
1202 
1203 	return TEST_SUCCESS;
1204 }
1205 
1206 /**
1207  * Test execution of rte_security_session_stats_get in successful execution
1208  * path
1209  */
1210 static int
test_session_stats_get_success(void)1211 test_session_stats_get_success(void)
1212 {
1213 	struct security_unittest_params *ut_params = &unittest_params;
1214 	struct rte_security_stats stats;
1215 
1216 	mock_session_stats_get_exp.device = NULL;
1217 	mock_session_stats_get_exp.sess = ut_params->sess;
1218 	mock_session_stats_get_exp.stats = &stats;
1219 	mock_session_stats_get_exp.ret = 0;
1220 
1221 	int ret = rte_security_session_stats_get(&ut_params->ctx,
1222 			ut_params->sess, &stats);
1223 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_stats_get,
1224 			ret, 0, "%d");
1225 	TEST_ASSERT_MOCK_CALLS(mock_session_stats_get_exp, 1);
1226 
1227 	return TEST_SUCCESS;
1228 }
1229 
1230 
1231 /**
1232  * rte_security_session_destroy tests
1233  */
1234 
1235 /**
1236  * Test execution of rte_security_session_destroy with NULL instance
1237  */
1238 static int
test_session_destroy_inv_context(void)1239 test_session_destroy_inv_context(void)
1240 {
1241 	struct security_unittest_params *ut_params = &unittest_params;
1242 
1243 	TEST_ASSERT_MEMPOOL_USAGE(1);
1244 	TEST_ASSERT_SESSION_COUNT(1);
1245 
1246 	int ret = rte_security_session_destroy(NULL, ut_params->sess);
1247 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy,
1248 			ret, -EINVAL, "%d");
1249 	TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 0);
1250 	TEST_ASSERT_MEMPOOL_USAGE(1);
1251 	TEST_ASSERT_SESSION_COUNT(1);
1252 
1253 	return TEST_SUCCESS;
1254 }
1255 
1256 /**
1257  * Test execution of rte_security_session_destroy with invalid
1258  * security operations structure (NULL)
1259  */
1260 static int
test_session_destroy_inv_context_ops(void)1261 test_session_destroy_inv_context_ops(void)
1262 {
1263 	struct security_unittest_params *ut_params = &unittest_params;
1264 	ut_params->ctx.ops = NULL;
1265 
1266 	TEST_ASSERT_MEMPOOL_USAGE(1);
1267 	TEST_ASSERT_SESSION_COUNT(1);
1268 
1269 	int ret = rte_security_session_destroy(&ut_params->ctx,
1270 			ut_params->sess);
1271 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy,
1272 			ret, -EINVAL, "%d");
1273 	TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 0);
1274 	TEST_ASSERT_MEMPOOL_USAGE(1);
1275 	TEST_ASSERT_SESSION_COUNT(1);
1276 
1277 	return TEST_SUCCESS;
1278 }
1279 
1280 /**
1281  * Test execution of rte_security_session_destroy with empty
1282  * security operations
1283  */
1284 static int
test_session_destroy_inv_context_ops_fun(void)1285 test_session_destroy_inv_context_ops_fun(void)
1286 {
1287 	struct security_unittest_params *ut_params = &unittest_params;
1288 	ut_params->ctx.ops = &empty_ops;
1289 
1290 	TEST_ASSERT_MEMPOOL_USAGE(1);
1291 	TEST_ASSERT_SESSION_COUNT(1);
1292 
1293 	int ret = rte_security_session_destroy(&ut_params->ctx,
1294 			ut_params->sess);
1295 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy,
1296 			ret, -ENOTSUP, "%d");
1297 	TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 0);
1298 	TEST_ASSERT_MEMPOOL_USAGE(1);
1299 	TEST_ASSERT_SESSION_COUNT(1);
1300 
1301 	return TEST_SUCCESS;
1302 }
1303 
1304 /**
1305  * Test execution of rte_security_session_destroy with NULL sess parameter
1306  */
1307 static int
test_session_destroy_inv_session(void)1308 test_session_destroy_inv_session(void)
1309 {
1310 	struct security_unittest_params *ut_params = &unittest_params;
1311 
1312 	TEST_ASSERT_MEMPOOL_USAGE(1);
1313 	TEST_ASSERT_SESSION_COUNT(1);
1314 
1315 	int ret = rte_security_session_destroy(&ut_params->ctx, NULL);
1316 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy,
1317 			ret, -EINVAL, "%d");
1318 	TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 0);
1319 	TEST_ASSERT_MEMPOOL_USAGE(1);
1320 	TEST_ASSERT_SESSION_COUNT(1);
1321 
1322 	return TEST_SUCCESS;
1323 }
1324 
1325 /**
1326  * Test execution of rte_security_session_destroy when session_destroy
1327  * security operation fails
1328  */
1329 static int
test_session_destroy_ops_failure(void)1330 test_session_destroy_ops_failure(void)
1331 {
1332 	struct security_unittest_params *ut_params = &unittest_params;
1333 
1334 	mock_session_destroy_exp.device = NULL;
1335 	mock_session_destroy_exp.sess = ut_params->sess;
1336 	mock_session_destroy_exp.ret = -1;
1337 
1338 	TEST_ASSERT_MEMPOOL_USAGE(1);
1339 	TEST_ASSERT_SESSION_COUNT(1);
1340 
1341 	int ret = rte_security_session_destroy(&ut_params->ctx,
1342 			ut_params->sess);
1343 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy,
1344 			ret, -1, "%d");
1345 	TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 1);
1346 	TEST_ASSERT_MEMPOOL_USAGE(1);
1347 	TEST_ASSERT_SESSION_COUNT(1);
1348 
1349 	return TEST_SUCCESS;
1350 }
1351 
1352 /**
1353  * Test execution of rte_security_session_destroy in successful execution path
1354  */
1355 static int
test_session_destroy_success(void)1356 test_session_destroy_success(void)
1357 {
1358 	struct security_unittest_params *ut_params = &unittest_params;
1359 
1360 	mock_session_destroy_exp.device = NULL;
1361 	mock_session_destroy_exp.sess = ut_params->sess;
1362 	mock_session_destroy_exp.ret = 0;
1363 	TEST_ASSERT_MEMPOOL_USAGE(1);
1364 	TEST_ASSERT_SESSION_COUNT(1);
1365 
1366 	int ret = rte_security_session_destroy(&ut_params->ctx,
1367 			ut_params->sess);
1368 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_session_destroy,
1369 			ret, 0, "%d");
1370 	TEST_ASSERT_MOCK_CALLS(mock_session_destroy_exp, 1);
1371 	TEST_ASSERT_MEMPOOL_USAGE(0);
1372 	TEST_ASSERT_SESSION_COUNT(0);
1373 
1374 	/*
1375 	 * Remove session from test case parameters, so it won't be destroyed
1376 	 * during test case teardown.
1377 	 */
1378 	ut_params->sess = NULL;
1379 
1380 	return TEST_SUCCESS;
1381 }
1382 
1383 
1384 /**
1385  * rte_security_set_pkt_metadata tests
1386  */
1387 
1388 /**
1389  * Test execution of rte_security_set_pkt_metadata with NULL instance
1390  */
1391 static int
test_set_pkt_metadata_inv_context(void)1392 test_set_pkt_metadata_inv_context(void)
1393 {
1394 #ifdef RTE_DEBUG
1395 	struct security_unittest_params *ut_params = &unittest_params;
1396 	struct rte_mbuf m;
1397 	int params;
1398 
1399 	int ret = rte_security_set_pkt_metadata(NULL, ut_params->sess, &m,
1400 			&params);
1401 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata,
1402 			ret, -EINVAL, "%d");
1403 	TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 0);
1404 
1405 	return TEST_SUCCESS;
1406 #else
1407 	return TEST_SKIPPED;
1408 #endif
1409 }
1410 
1411 /**
1412  * Test execution of rte_security_set_pkt_metadata with invalid
1413  * security operations structure (NULL)
1414  */
1415 static int
test_set_pkt_metadata_inv_context_ops(void)1416 test_set_pkt_metadata_inv_context_ops(void)
1417 {
1418 #ifdef RTE_DEBUG
1419 	struct security_unittest_params *ut_params = &unittest_params;
1420 	struct rte_mbuf m;
1421 	int params;
1422 	ut_params->ctx.ops = NULL;
1423 
1424 	int ret = rte_security_set_pkt_metadata(&ut_params->ctx,
1425 			ut_params->sess, &m, &params);
1426 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata,
1427 			ret, -EINVAL, "%d");
1428 	TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 0);
1429 
1430 	return TEST_SUCCESS;
1431 #else
1432 	return TEST_SKIPPED;
1433 #endif
1434 }
1435 
1436 /**
1437  * Test execution of rte_security_set_pkt_metadata with empty
1438  * security operations
1439  */
1440 static int
test_set_pkt_metadata_inv_context_ops_fun(void)1441 test_set_pkt_metadata_inv_context_ops_fun(void)
1442 {
1443 	struct security_unittest_params *ut_params = &unittest_params;
1444 	struct rte_mbuf m;
1445 	int params;
1446 	ut_params->ctx.ops = &empty_ops;
1447 
1448 	int ret = rte_security_set_pkt_metadata(&ut_params->ctx,
1449 			ut_params->sess, &m, &params);
1450 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata,
1451 			ret, -ENOTSUP, "%d");
1452 	TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 0);
1453 
1454 	return TEST_SUCCESS;
1455 }
1456 
1457 /**
1458  * Test execution of rte_security_set_pkt_metadata with NULL sess parameter
1459  */
1460 static int
test_set_pkt_metadata_inv_session(void)1461 test_set_pkt_metadata_inv_session(void)
1462 {
1463 #ifdef RTE_DEBUG
1464 	struct security_unittest_params *ut_params = &unittest_params;
1465 	struct rte_mbuf m;
1466 	int params;
1467 
1468 	int ret = rte_security_set_pkt_metadata(&ut_params->ctx, NULL,
1469 			&m, &params);
1470 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata,
1471 			ret, -EINVAL, "%d");
1472 	TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 0);
1473 
1474 	return TEST_SUCCESS;
1475 #else
1476 	return TEST_SKIPPED;
1477 #endif
1478 }
1479 
1480 /**
1481  * Test execution of rte_security_set_pkt_metadata when set_pkt_metadata
1482  * security operation fails
1483  */
1484 static int
test_set_pkt_metadata_ops_failure(void)1485 test_set_pkt_metadata_ops_failure(void)
1486 {
1487 	struct security_unittest_params *ut_params = &unittest_params;
1488 	struct rte_mbuf m;
1489 	int params;
1490 
1491 	mock_set_pkt_metadata_exp.device = NULL;
1492 	mock_set_pkt_metadata_exp.sess = ut_params->sess;
1493 	mock_set_pkt_metadata_exp.m = &m;
1494 	mock_set_pkt_metadata_exp.params = &params;
1495 	mock_set_pkt_metadata_exp.ret = -1;
1496 
1497 	int ret = rte_security_set_pkt_metadata(&ut_params->ctx,
1498 			ut_params->sess, &m, &params);
1499 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata,
1500 			ret, -1, "%d");
1501 	TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 1);
1502 
1503 	return TEST_SUCCESS;
1504 }
1505 
1506 /**
1507  * Test execution of rte_security_set_pkt_metadata in successful execution path
1508  */
1509 static int
test_set_pkt_metadata_success(void)1510 test_set_pkt_metadata_success(void)
1511 {
1512 	struct security_unittest_params *ut_params = &unittest_params;
1513 	struct rte_mbuf m;
1514 	int params;
1515 
1516 	mock_set_pkt_metadata_exp.device = NULL;
1517 	mock_set_pkt_metadata_exp.sess = ut_params->sess;
1518 	mock_set_pkt_metadata_exp.m = &m;
1519 	mock_set_pkt_metadata_exp.params = &params;
1520 	mock_set_pkt_metadata_exp.ret = 0;
1521 
1522 	int ret = rte_security_set_pkt_metadata(&ut_params->ctx,
1523 			ut_params->sess, &m, &params);
1524 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_set_pkt_metadata,
1525 			ret, 0, "%d");
1526 	TEST_ASSERT_MOCK_CALLS(mock_set_pkt_metadata_exp, 1);
1527 
1528 	return TEST_SUCCESS;
1529 }
1530 
1531 /**
1532  * rte_security_capabilities_get tests
1533  */
1534 
1535 /**
1536  * Test execution of rte_security_capabilities_get with NULL instance
1537  */
1538 static int
test_capabilities_get_inv_context(void)1539 test_capabilities_get_inv_context(void)
1540 {
1541 	const struct rte_security_capability *ret;
1542 	ret = rte_security_capabilities_get(NULL);
1543 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capabilities_get,
1544 			ret, NULL, "%p");
1545 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0);
1546 
1547 	return TEST_SUCCESS;
1548 }
1549 
1550 /**
1551  * Test execution of rte_security_capabilities_get with invalid
1552  * security operations structure (NULL)
1553  */
1554 static int
test_capabilities_get_inv_context_ops(void)1555 test_capabilities_get_inv_context_ops(void)
1556 {
1557 	struct security_unittest_params *ut_params = &unittest_params;
1558 	ut_params->ctx.ops = NULL;
1559 
1560 	const struct rte_security_capability *ret;
1561 	ret = rte_security_capabilities_get(&ut_params->ctx);
1562 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capabilities_get,
1563 			ret, NULL, "%p");
1564 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0);
1565 
1566 	return TEST_SUCCESS;
1567 }
1568 
1569 /**
1570  * Test execution of rte_security_capabilities_get with empty
1571  * security operations
1572  */
1573 static int
test_capabilities_get_inv_context_ops_fun(void)1574 test_capabilities_get_inv_context_ops_fun(void)
1575 {
1576 	struct security_unittest_params *ut_params = &unittest_params;
1577 	ut_params->ctx.ops = &empty_ops;
1578 
1579 	const struct rte_security_capability *ret;
1580 	ret = rte_security_capabilities_get(&ut_params->ctx);
1581 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capabilities_get,
1582 			ret, NULL, "%p");
1583 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0);
1584 
1585 	return TEST_SUCCESS;
1586 }
1587 
1588 /**
1589  * Test execution of rte_security_capabilities_get when capabilities_get
1590  * security operation fails
1591  */
1592 static int
test_capabilities_get_ops_failure(void)1593 test_capabilities_get_ops_failure(void)
1594 {
1595 	struct security_unittest_params *ut_params = &unittest_params;
1596 
1597 	mock_capabilities_get_exp.device = NULL;
1598 	mock_capabilities_get_exp.ret = NULL;
1599 
1600 	const struct rte_security_capability *ret;
1601 	ret = rte_security_capabilities_get(&ut_params->ctx);
1602 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capabilities_get,
1603 			ret, NULL, "%p");
1604 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
1605 
1606 	return TEST_SUCCESS;
1607 }
1608 
1609 /**
1610  * Test execution of rte_security_capabilities_get in successful execution path
1611  */
1612 static int
test_capabilities_get_success(void)1613 test_capabilities_get_success(void)
1614 {
1615 	struct security_unittest_params *ut_params = &unittest_params;
1616 	struct rte_security_capability capabilities;
1617 
1618 	mock_capabilities_get_exp.device = NULL;
1619 	mock_capabilities_get_exp.ret = &capabilities;
1620 
1621 	const struct rte_security_capability *ret;
1622 	ret = rte_security_capabilities_get(&ut_params->ctx);
1623 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capabilities_get,
1624 			ret, &capabilities, "%p");
1625 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
1626 
1627 	return TEST_SUCCESS;
1628 }
1629 
1630 
1631 /**
1632  * rte_security_capability_get tests
1633  */
1634 
1635 /**
1636  * Test execution of rte_security_capability_get with NULL instance
1637  */
1638 static int
test_capability_get_inv_context(void)1639 test_capability_get_inv_context(void)
1640 {
1641 	struct rte_security_capability_idx idx;
1642 
1643 	const struct rte_security_capability *ret;
1644 	ret = rte_security_capability_get(NULL, &idx);
1645 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1646 			ret, NULL, "%p");
1647 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0);
1648 
1649 	return TEST_SUCCESS;
1650 }
1651 
1652 /**
1653  * Test execution of rte_security_capability_get with invalid
1654  * security operations structure (NULL)
1655  */
1656 static int
test_capability_get_inv_context_ops(void)1657 test_capability_get_inv_context_ops(void)
1658 {
1659 	struct security_unittest_params *ut_params = &unittest_params;
1660 	struct rte_security_capability_idx idx;
1661 	ut_params->ctx.ops = NULL;
1662 
1663 	const struct rte_security_capability *ret;
1664 	ret = rte_security_capability_get(&ut_params->ctx, &idx);
1665 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1666 			ret, NULL, "%p");
1667 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0);
1668 
1669 	return TEST_SUCCESS;
1670 }
1671 
1672 /**
1673  * Test execution of rte_security_capability_get with empty
1674  * security operations
1675  */
1676 static int
test_capability_get_inv_context_ops_fun(void)1677 test_capability_get_inv_context_ops_fun(void)
1678 {
1679 	struct security_unittest_params *ut_params = &unittest_params;
1680 	struct rte_security_capability_idx idx;
1681 	ut_params->ctx.ops = &empty_ops;
1682 
1683 	const struct rte_security_capability *ret;
1684 	ret = rte_security_capability_get(&ut_params->ctx, &idx);
1685 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1686 			ret, NULL, "%p");
1687 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0);
1688 
1689 	return TEST_SUCCESS;
1690 }
1691 
1692 /**
1693  * Test execution of rte_security_capability_get with NULL idx parameter
1694  */
1695 static int
test_capability_get_inv_idx(void)1696 test_capability_get_inv_idx(void)
1697 {
1698 	struct security_unittest_params *ut_params = &unittest_params;
1699 
1700 	const struct rte_security_capability *ret;
1701 	ret = rte_security_capability_get(&ut_params->ctx, NULL);
1702 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1703 			ret, NULL, "%p");
1704 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 0);
1705 
1706 	return TEST_SUCCESS;
1707 }
1708 
1709 /**
1710  * Test execution of rte_security_capability_get when capabilities_get
1711  * security operation fails
1712  */
1713 static int
test_capability_get_ops_failure(void)1714 test_capability_get_ops_failure(void)
1715 {
1716 	struct security_unittest_params *ut_params = &unittest_params;
1717 	struct rte_security_capability_idx idx;
1718 
1719 	mock_capabilities_get_exp.device = NULL;
1720 	mock_capabilities_get_exp.ret = NULL;
1721 
1722 	const struct rte_security_capability *ret;
1723 	ret = rte_security_capability_get(&ut_params->ctx, &idx);
1724 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1725 			ret, NULL, "%p");
1726 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
1727 
1728 	return TEST_SUCCESS;
1729 }
1730 
1731 /**
1732  * Test execution of rte_security_capability_get when capabilities table
1733  * is empty (contains only RTE_SECURITY_ACTION_TYPE_NONE ending entry)
1734  */
1735 static int
test_capability_get_empty_table(void)1736 test_capability_get_empty_table(void)
1737 {
1738 	struct security_unittest_params *ut_params = &unittest_params;
1739 	struct rte_security_capability_idx idx;
1740 	struct rte_security_capability capabilities[] = {
1741 		{
1742 			.action = RTE_SECURITY_ACTION_TYPE_NONE,
1743 		},
1744 	};
1745 
1746 	mock_capabilities_get_exp.device = NULL;
1747 	mock_capabilities_get_exp.ret = capabilities;
1748 
1749 	const struct rte_security_capability *ret;
1750 	ret = rte_security_capability_get(&ut_params->ctx, &idx);
1751 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1752 			ret, NULL, "%p");
1753 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
1754 
1755 	return TEST_SUCCESS;
1756 }
1757 
1758 /**
1759  * Test execution of rte_security_capability_get when capabilities table
1760  * does not contain entry with matching action
1761  */
1762 static int
test_capability_get_no_matching_action(void)1763 test_capability_get_no_matching_action(void)
1764 {
1765 	struct security_unittest_params *ut_params = &unittest_params;
1766 	struct rte_security_capability_idx idx = {
1767 		.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1768 	};
1769 	struct rte_security_capability capabilities[] = {
1770 		{
1771 			.action = RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
1772 		},
1773 		{
1774 			.action = RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL,
1775 		},
1776 		{
1777 			.action = RTE_SECURITY_ACTION_TYPE_NONE,
1778 		},
1779 	};
1780 
1781 	mock_capabilities_get_exp.device = NULL;
1782 	mock_capabilities_get_exp.ret = capabilities;
1783 
1784 	const struct rte_security_capability *ret;
1785 	ret = rte_security_capability_get(&ut_params->ctx, &idx);
1786 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1787 			ret, NULL, "%p");
1788 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
1789 
1790 	return TEST_SUCCESS;
1791 }
1792 
1793 /**
1794  * Test execution of rte_security_capability_get when capabilities table
1795  * does not contain entry with matching protocol
1796  */
1797 static int
test_capability_get_no_matching_protocol(void)1798 test_capability_get_no_matching_protocol(void)
1799 {
1800 	struct security_unittest_params *ut_params = &unittest_params;
1801 	struct rte_security_capability_idx idx = {
1802 		.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1803 		.protocol = RTE_SECURITY_PROTOCOL_IPSEC,
1804 	};
1805 	struct rte_security_capability capabilities[] = {
1806 		{
1807 			.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1808 			.protocol = RTE_SECURITY_PROTOCOL_MACSEC,
1809 		},
1810 		{
1811 			.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1812 			.protocol = RTE_SECURITY_PROTOCOL_PDCP,
1813 		},
1814 		{
1815 			.action = RTE_SECURITY_ACTION_TYPE_NONE,
1816 		},
1817 	};
1818 
1819 	mock_capabilities_get_exp.device = NULL;
1820 	mock_capabilities_get_exp.ret = capabilities;
1821 
1822 	const struct rte_security_capability *ret;
1823 	ret = rte_security_capability_get(&ut_params->ctx, &idx);
1824 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1825 			ret, NULL, "%p");
1826 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
1827 
1828 	return TEST_SUCCESS;
1829 }
1830 
1831 /**
1832  * Test execution of rte_security_capability_get when capabilities table
1833  * does not contain entry with matching ipsec proto field
1834  */
1835 static int
test_capability_get_ipsec_mismatch_proto(void)1836 test_capability_get_ipsec_mismatch_proto(void)
1837 {
1838 	struct security_unittest_params *ut_params = &unittest_params;
1839 	struct rte_security_capability_idx idx = {
1840 		.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1841 		.protocol = RTE_SECURITY_PROTOCOL_IPSEC,
1842 		.ipsec = {
1843 			.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
1844 		},
1845 	};
1846 	struct rte_security_capability capabilities[] = {
1847 		{
1848 			.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1849 			.protocol = RTE_SECURITY_PROTOCOL_IPSEC,
1850 			.ipsec = {
1851 				.proto = RTE_SECURITY_IPSEC_SA_PROTO_AH,
1852 			},
1853 		},
1854 		{
1855 			.action = RTE_SECURITY_ACTION_TYPE_NONE,
1856 		},
1857 	};
1858 
1859 	mock_capabilities_get_exp.device = NULL;
1860 	mock_capabilities_get_exp.ret = capabilities;
1861 
1862 	const struct rte_security_capability *ret;
1863 	ret = rte_security_capability_get(&ut_params->ctx, &idx);
1864 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1865 			ret, NULL, "%p");
1866 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
1867 
1868 	return TEST_SUCCESS;
1869 }
1870 
1871 /**
1872  * Test execution of rte_security_capability_get when capabilities table
1873  * does not contain entry with matching ipsec mode field
1874  */
1875 static int
test_capability_get_ipsec_mismatch_mode(void)1876 test_capability_get_ipsec_mismatch_mode(void)
1877 {
1878 	struct security_unittest_params *ut_params = &unittest_params;
1879 	struct rte_security_capability_idx idx = {
1880 		.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1881 		.protocol = RTE_SECURITY_PROTOCOL_IPSEC,
1882 		.ipsec = {
1883 			.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
1884 			.mode = RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
1885 		},
1886 	};
1887 	struct rte_security_capability capabilities[] = {
1888 		{
1889 			.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1890 			.protocol = RTE_SECURITY_PROTOCOL_IPSEC,
1891 			.ipsec = {
1892 				.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
1893 				.mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
1894 			},
1895 		},
1896 		{
1897 			.action = RTE_SECURITY_ACTION_TYPE_NONE,
1898 		},
1899 	};
1900 
1901 	mock_capabilities_get_exp.device = NULL;
1902 	mock_capabilities_get_exp.ret = capabilities;
1903 
1904 	const struct rte_security_capability *ret;
1905 	ret = rte_security_capability_get(&ut_params->ctx, &idx);
1906 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1907 			ret, NULL, "%p");
1908 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
1909 
1910 	return TEST_SUCCESS;
1911 }
1912 
1913 /**
1914  * Test execution of rte_security_capability_get when capabilities table
1915  * does not contain entry with matching ipsec direction field
1916  */
1917 static int
test_capability_get_ipsec_mismatch_dir(void)1918 test_capability_get_ipsec_mismatch_dir(void)
1919 {
1920 	struct security_unittest_params *ut_params = &unittest_params;
1921 	struct rte_security_capability_idx idx = {
1922 		.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1923 		.protocol = RTE_SECURITY_PROTOCOL_IPSEC,
1924 		.ipsec = {
1925 			.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
1926 			.mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
1927 			.direction = RTE_SECURITY_IPSEC_SA_DIR_EGRESS,
1928 		},
1929 	};
1930 	struct rte_security_capability capabilities[] = {
1931 		{
1932 			.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1933 			.protocol = RTE_SECURITY_PROTOCOL_IPSEC,
1934 			.ipsec = {
1935 				.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
1936 				.mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
1937 				.direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
1938 			},
1939 		},
1940 		{
1941 			.action = RTE_SECURITY_ACTION_TYPE_NONE,
1942 		},
1943 	};
1944 
1945 	mock_capabilities_get_exp.device = NULL;
1946 	mock_capabilities_get_exp.ret = capabilities;
1947 
1948 	const struct rte_security_capability *ret;
1949 	ret = rte_security_capability_get(&ut_params->ctx, &idx);
1950 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1951 			ret, NULL, "%p");
1952 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
1953 
1954 	return TEST_SUCCESS;
1955 }
1956 
1957 /**
1958  * Test execution of rte_security_capability_get when capabilities table
1959  * contains matching ipsec entry
1960  */
1961 static int
test_capability_get_ipsec_match(void)1962 test_capability_get_ipsec_match(void)
1963 {
1964 	struct security_unittest_params *ut_params = &unittest_params;
1965 	struct rte_security_capability_idx idx = {
1966 		.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1967 		.protocol = RTE_SECURITY_PROTOCOL_IPSEC,
1968 		.ipsec = {
1969 			.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
1970 			.mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
1971 			.direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
1972 		},
1973 	};
1974 	struct rte_security_capability capabilities[] = {
1975 		{
1976 			.action = RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
1977 		},
1978 		{
1979 			.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
1980 			.protocol = RTE_SECURITY_PROTOCOL_IPSEC,
1981 			.ipsec = {
1982 				.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
1983 				.mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL,
1984 				.direction = RTE_SECURITY_IPSEC_SA_DIR_INGRESS,
1985 			},
1986 		},
1987 		{
1988 			.action = RTE_SECURITY_ACTION_TYPE_NONE,
1989 		},
1990 	};
1991 
1992 	mock_capabilities_get_exp.device = NULL;
1993 	mock_capabilities_get_exp.ret = capabilities;
1994 
1995 	const struct rte_security_capability *ret;
1996 	ret = rte_security_capability_get(&ut_params->ctx, &idx);
1997 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
1998 			ret, &capabilities[1], "%p");
1999 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
2000 
2001 	return TEST_SUCCESS;
2002 }
2003 
2004 /**
2005  * Test execution of rte_security_capability_get when capabilities table
2006  * does not contain entry with matching pdcp domain field
2007  */
2008 static int
test_capability_get_pdcp_mismatch_domain(void)2009 test_capability_get_pdcp_mismatch_domain(void)
2010 {
2011 	struct security_unittest_params *ut_params = &unittest_params;
2012 	struct rte_security_capability_idx idx = {
2013 		.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2014 		.protocol = RTE_SECURITY_PROTOCOL_PDCP,
2015 		.pdcp = {
2016 			.domain = RTE_SECURITY_PDCP_MODE_CONTROL,
2017 		},
2018 	};
2019 	struct rte_security_capability capabilities[] = {
2020 		{
2021 			.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2022 			.protocol = RTE_SECURITY_PROTOCOL_PDCP,
2023 			.pdcp = {
2024 				.domain = RTE_SECURITY_PDCP_MODE_DATA,
2025 			},
2026 		},
2027 		{
2028 			.action = RTE_SECURITY_ACTION_TYPE_NONE,
2029 		},
2030 	};
2031 
2032 	mock_capabilities_get_exp.device = NULL;
2033 	mock_capabilities_get_exp.ret = capabilities;
2034 
2035 	const struct rte_security_capability *ret;
2036 	ret = rte_security_capability_get(&ut_params->ctx, &idx);
2037 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
2038 			ret, NULL, "%p");
2039 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
2040 
2041 	return TEST_SUCCESS;
2042 }
2043 
2044 /**
2045  * Test execution of rte_security_capability_get when capabilities table
2046  * contains matching pdcp entry
2047  */
2048 static int
test_capability_get_pdcp_match(void)2049 test_capability_get_pdcp_match(void)
2050 {
2051 	struct security_unittest_params *ut_params = &unittest_params;
2052 	struct rte_security_capability_idx idx = {
2053 		.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2054 		.protocol = RTE_SECURITY_PROTOCOL_PDCP,
2055 		.pdcp = {
2056 			.domain = RTE_SECURITY_PDCP_MODE_CONTROL,
2057 		},
2058 	};
2059 	struct rte_security_capability capabilities[] = {
2060 		{
2061 			.action = RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
2062 		},
2063 		{
2064 			.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2065 			.protocol = RTE_SECURITY_PROTOCOL_PDCP,
2066 			.pdcp = {
2067 				.domain = RTE_SECURITY_PDCP_MODE_CONTROL,
2068 			},
2069 		},
2070 		{
2071 			.action = RTE_SECURITY_ACTION_TYPE_NONE,
2072 		},
2073 	};
2074 
2075 	mock_capabilities_get_exp.device = NULL;
2076 	mock_capabilities_get_exp.ret = capabilities;
2077 
2078 	const struct rte_security_capability *ret;
2079 	ret = rte_security_capability_get(&ut_params->ctx, &idx);
2080 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
2081 			ret, &capabilities[1], "%p");
2082 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
2083 
2084 	return TEST_SUCCESS;
2085 }
2086 
2087 /**
2088  * Test execution of rte_security_capability_get when capabilities table
2089  * does not contain entry with matching DOCSIS direction field
2090  */
2091 static int
test_capability_get_docsis_mismatch_direction(void)2092 test_capability_get_docsis_mismatch_direction(void)
2093 {
2094 	struct security_unittest_params *ut_params = &unittest_params;
2095 	struct rte_security_capability_idx idx = {
2096 		.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2097 		.protocol = RTE_SECURITY_PROTOCOL_DOCSIS,
2098 		.docsis = {
2099 			.direction = RTE_SECURITY_DOCSIS_DOWNLINK
2100 		},
2101 	};
2102 	struct rte_security_capability capabilities[] = {
2103 		{
2104 			.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2105 			.protocol = RTE_SECURITY_PROTOCOL_DOCSIS,
2106 			.docsis = {
2107 				.direction = RTE_SECURITY_DOCSIS_UPLINK
2108 			},
2109 		},
2110 		{
2111 			.action = RTE_SECURITY_ACTION_TYPE_NONE,
2112 		},
2113 	};
2114 
2115 	mock_capabilities_get_exp.device = NULL;
2116 	mock_capabilities_get_exp.ret = capabilities;
2117 
2118 	const struct rte_security_capability *ret;
2119 	ret = rte_security_capability_get(&ut_params->ctx, &idx);
2120 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
2121 			ret, NULL, "%p");
2122 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
2123 
2124 	return TEST_SUCCESS;
2125 }
2126 
2127 /**
2128  * Test execution of rte_security_capability_get when capabilities table
2129  * contains matching DOCSIS entry
2130  */
2131 static int
test_capability_get_docsis_match(void)2132 test_capability_get_docsis_match(void)
2133 {
2134 	struct security_unittest_params *ut_params = &unittest_params;
2135 	struct rte_security_capability_idx idx = {
2136 		.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2137 		.protocol = RTE_SECURITY_PROTOCOL_DOCSIS,
2138 		.docsis = {
2139 			.direction = RTE_SECURITY_DOCSIS_UPLINK
2140 		},
2141 	};
2142 	struct rte_security_capability capabilities[] = {
2143 		{
2144 			.action = RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO,
2145 		},
2146 		{
2147 			.action = RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL,
2148 			.protocol = RTE_SECURITY_PROTOCOL_DOCSIS,
2149 			.docsis = {
2150 				.direction = RTE_SECURITY_DOCSIS_UPLINK
2151 			},
2152 		},
2153 		{
2154 			.action = RTE_SECURITY_ACTION_TYPE_NONE,
2155 		},
2156 	};
2157 
2158 	mock_capabilities_get_exp.device = NULL;
2159 	mock_capabilities_get_exp.ret = capabilities;
2160 
2161 	const struct rte_security_capability *ret;
2162 	ret = rte_security_capability_get(&ut_params->ctx, &idx);
2163 	TEST_ASSERT_MOCK_FUNCTION_CALL_RET(rte_security_capability_get,
2164 			ret, &capabilities[1], "%p");
2165 	TEST_ASSERT_MOCK_CALLS(mock_capabilities_get_exp, 1);
2166 
2167 	return TEST_SUCCESS;
2168 }
2169 
2170 /**
2171  * Declaration of testcases
2172  */
2173 static struct unit_test_suite security_testsuite  = {
2174 	.suite_name = "generic security",
2175 	.setup = testsuite_setup,
2176 	.teardown = testsuite_teardown,
2177 	.unit_test_cases = {
2178 		TEST_CASE_ST(ut_setup, ut_teardown,
2179 				test_session_create_inv_context),
2180 		TEST_CASE_ST(ut_setup, ut_teardown,
2181 				test_session_create_inv_context_ops),
2182 		TEST_CASE_ST(ut_setup, ut_teardown,
2183 				test_session_create_inv_context_ops_fun),
2184 		TEST_CASE_ST(ut_setup, ut_teardown,
2185 				test_session_create_inv_configuration),
2186 		TEST_CASE_ST(ut_setup, ut_teardown,
2187 				test_session_create_inv_mempool),
2188 		TEST_CASE_ST(ut_setup, ut_teardown,
2189 				test_session_create_mempool_empty),
2190 		TEST_CASE_ST(ut_setup, ut_teardown,
2191 				test_session_create_ops_failure),
2192 		TEST_CASE_ST(ut_setup, ut_teardown,
2193 				test_session_create_success),
2194 
2195 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2196 				test_session_update_inv_context),
2197 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2198 				test_session_update_inv_context_ops),
2199 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2200 				test_session_update_inv_context_ops_fun),
2201 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2202 				test_session_update_inv_configuration),
2203 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2204 				test_session_update_inv_session),
2205 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2206 				test_session_update_ops_failure),
2207 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2208 				test_session_update_success),
2209 
2210 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2211 				test_session_get_size_inv_context),
2212 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2213 				test_session_get_size_inv_context_ops),
2214 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2215 				test_session_get_size_inv_context_ops_fun),
2216 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2217 				test_session_get_size_ops_failure),
2218 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2219 				test_session_get_size_success),
2220 
2221 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2222 				test_session_stats_get_inv_context),
2223 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2224 				test_session_stats_get_inv_context_ops),
2225 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2226 				test_session_stats_get_inv_context_ops_fun),
2227 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2228 				test_session_stats_get_inv_stats),
2229 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2230 				test_session_stats_get_ops_failure),
2231 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2232 				test_session_stats_get_success),
2233 
2234 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2235 				test_session_destroy_inv_context),
2236 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2237 				test_session_destroy_inv_context_ops),
2238 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2239 				test_session_destroy_inv_context_ops_fun),
2240 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2241 				test_session_destroy_inv_session),
2242 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2243 				test_session_destroy_ops_failure),
2244 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2245 				test_session_destroy_success),
2246 
2247 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2248 				test_set_pkt_metadata_inv_context),
2249 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2250 				test_set_pkt_metadata_inv_context_ops),
2251 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2252 				test_set_pkt_metadata_inv_context_ops_fun),
2253 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2254 				test_set_pkt_metadata_inv_session),
2255 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2256 				test_set_pkt_metadata_ops_failure),
2257 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2258 				test_set_pkt_metadata_success),
2259 
2260 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2261 				test_capabilities_get_inv_context),
2262 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2263 				test_capabilities_get_inv_context_ops),
2264 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2265 				test_capabilities_get_inv_context_ops_fun),
2266 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2267 				test_capabilities_get_ops_failure),
2268 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2269 				test_capabilities_get_success),
2270 
2271 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2272 				test_capability_get_inv_context),
2273 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2274 				test_capability_get_inv_context_ops),
2275 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2276 				test_capability_get_inv_context_ops_fun),
2277 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2278 				test_capability_get_inv_idx),
2279 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2280 				test_capability_get_ops_failure),
2281 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2282 				test_capability_get_empty_table),
2283 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2284 				test_capability_get_no_matching_action),
2285 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2286 				test_capability_get_no_matching_protocol),
2287 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2288 				test_capability_get_ipsec_mismatch_proto),
2289 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2290 				test_capability_get_ipsec_mismatch_mode),
2291 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2292 				test_capability_get_ipsec_mismatch_dir),
2293 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2294 				test_capability_get_ipsec_match),
2295 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2296 				test_capability_get_pdcp_mismatch_domain),
2297 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2298 				test_capability_get_pdcp_match),
2299 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2300 				test_capability_get_docsis_mismatch_direction),
2301 		TEST_CASE_ST(ut_setup_with_session, ut_teardown,
2302 				test_capability_get_docsis_match),
2303 
2304 		TEST_CASES_END() /**< NULL terminate unit test array */
2305 	}
2306 };
2307 
2308 static int
test_security(void)2309 test_security(void)
2310 {
2311 	rte_log_set_global_level(RTE_LOG_DEBUG);
2312 	rte_log_set_level(RTE_LOGTYPE_EAL, RTE_LOG_DEBUG);
2313 
2314 	return unit_test_suite_runner(&security_testsuite);
2315 }
2316 
2317 REGISTER_FAST_TEST(security_autotest, false, true, test_security);
2318