15129044dSC.J. Collier/*-
25129044dSC.J. Collier *   BSD LICENSE
35129044dSC.J. Collier *
45129044dSC.J. Collier *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
55129044dSC.J. Collier *   All rights reserved.
65129044dSC.J. Collier *
75129044dSC.J. Collier *   Redistribution and use in source and binary forms, with or without
85129044dSC.J. Collier *   modification, are permitted provided that the following conditions
95129044dSC.J. Collier *   are met:
105129044dSC.J. Collier *
115129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
125129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
135129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
145129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in
155129044dSC.J. Collier *       the documentation and/or other materials provided with the
165129044dSC.J. Collier *       distribution.
175129044dSC.J. Collier *     * Neither the name of Intel Corporation nor the names of its
185129044dSC.J. Collier *       contributors may be used to endorse or promote products derived
195129044dSC.J. Collier *       from this software without specific prior written permission.
205129044dSC.J. Collier *
215129044dSC.J. Collier *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225129044dSC.J. Collier *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235129044dSC.J. Collier *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245129044dSC.J. Collier *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255129044dSC.J. Collier *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265129044dSC.J. Collier *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275129044dSC.J. Collier *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285129044dSC.J. Collier *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295129044dSC.J. Collier *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305129044dSC.J. Collier *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315129044dSC.J. Collier *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325129044dSC.J. Collier */
335129044dSC.J. Collier
345129044dSC.J. Collier#ifndef _TEST_H_
355129044dSC.J. Collier#define _TEST_H_
363d9b7210SChristian Ehrhardt
375129044dSC.J. Collier#include <stddef.h>
385129044dSC.J. Collier#include <sys/queue.h>
393d9b7210SChristian Ehrhardt
403d9b7210SChristian Ehrhardt#include <rte_common.h>
413d9b7210SChristian Ehrhardt#include <rte_log.h>
425129044dSC.J. Collier
435129044dSC.J. Collier#define TEST_SUCCESS  (0)
445129044dSC.J. Collier#define TEST_FAILED  (-1)
455129044dSC.J. Collier
465129044dSC.J. Collier/* Before including test.h file you can define
475129044dSC.J. Collier * TEST_TRACE_FAILURE(_file, _line, _func) macro to better trace/debug test
485129044dSC.J. Collier * failures. Mostly useful in test development phase. */
495129044dSC.J. Collier#ifndef TEST_TRACE_FAILURE
505129044dSC.J. Collier# define TEST_TRACE_FAILURE(_file, _line, _func)
515129044dSC.J. Collier#endif
525129044dSC.J. Collier
535129044dSC.J. Collier#define TEST_ASSERT(cond, msg, ...) do {                         \
545129044dSC.J. Collier		if (!(cond)) {                                           \
555129044dSC.J. Collier			printf("TestCase %s() line %d failed: "              \
565129044dSC.J. Collier				msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
575129044dSC.J. Collier			TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
585129044dSC.J. Collier			return TEST_FAILED;                                  \
595129044dSC.J. Collier		}                                                        \
605129044dSC.J. Collier} while (0)
615129044dSC.J. Collier
625129044dSC.J. Collier#define TEST_ASSERT_EQUAL(a, b, msg, ...) do {                   \
635129044dSC.J. Collier		if (!(a == b)) {                                         \
645129044dSC.J. Collier			printf("TestCase %s() line %d failed: "              \
655129044dSC.J. Collier				msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
665129044dSC.J. Collier			TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
675129044dSC.J. Collier			return TEST_FAILED;                                  \
685129044dSC.J. Collier		}                                                        \
695129044dSC.J. Collier} while (0)
705129044dSC.J. Collier
71809f0800SChristian Ehrhardt/* Compare two buffers (length in bytes) */
725129044dSC.J. Collier#define TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, len,  msg, ...) do {	\
735129044dSC.J. Collier	if (memcmp(a, b, len)) {                                        \
745129044dSC.J. Collier		printf("TestCase %s() line %d failed: "              \
755129044dSC.J. Collier			msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
765129044dSC.J. Collier		TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
775129044dSC.J. Collier		return TEST_FAILED;                                  \
785129044dSC.J. Collier	}                                                        \
795129044dSC.J. Collier} while (0)
805129044dSC.J. Collier
81809f0800SChristian Ehrhardt/* Compare two buffers with offset (length and offset in bytes) */
82809f0800SChristian Ehrhardt#define TEST_ASSERT_BUFFERS_ARE_EQUAL_OFFSET(a, b, len, off, msg, ...) do { \
83809f0800SChristian Ehrhardt	const uint8_t *_a_with_off = (const uint8_t *)a + off;              \
84809f0800SChristian Ehrhardt	const uint8_t *_b_with_off = (const uint8_t *)b + off;              \
85809f0800SChristian Ehrhardt	TEST_ASSERT_BUFFERS_ARE_EQUAL(_a_with_off, _b_with_off, len, msg);  \
86809f0800SChristian Ehrhardt} while (0)
87809f0800SChristian Ehrhardt
88809f0800SChristian Ehrhardt/* Compare two buffers (length in bits) */
89809f0800SChristian Ehrhardt#define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(a, b, len, msg, ...) do {	\
90809f0800SChristian Ehrhardt	uint8_t _last_byte_a, _last_byte_b;                       \
91809f0800SChristian Ehrhardt	uint8_t _last_byte_mask, _last_byte_bits;                  \
92809f0800SChristian Ehrhardt	TEST_ASSERT_BUFFERS_ARE_EQUAL(a, b, (len >> 3), msg);     \
93809f0800SChristian Ehrhardt	if (len % 8) {                                              \
94809f0800SChristian Ehrhardt		_last_byte_bits = len % 8;                   \
95809f0800SChristian Ehrhardt		_last_byte_mask = ~((1 << (8 - _last_byte_bits)) - 1); \
96809f0800SChristian Ehrhardt		_last_byte_a = ((const uint8_t *)a)[len >> 3];            \
97809f0800SChristian Ehrhardt		_last_byte_b = ((const uint8_t *)b)[len >> 3];            \
98809f0800SChristian Ehrhardt		_last_byte_a &= _last_byte_mask;                     \
99809f0800SChristian Ehrhardt		_last_byte_b &= _last_byte_mask;                    \
100809f0800SChristian Ehrhardt		if (_last_byte_a != _last_byte_b) {                  \
101809f0800SChristian Ehrhardt			printf("TestCase %s() line %d failed: "              \
102809f0800SChristian Ehrhardt				msg "\n", __func__, __LINE__, ##__VA_ARGS__);\
103809f0800SChristian Ehrhardt			TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
104809f0800SChristian Ehrhardt			return TEST_FAILED;                                  \
105809f0800SChristian Ehrhardt		}                                                        \
106809f0800SChristian Ehrhardt	}                                                            \
107809f0800SChristian Ehrhardt} while (0)
108809f0800SChristian Ehrhardt
109809f0800SChristian Ehrhardt/* Compare two buffers with offset (length and offset in bits) */
110809f0800SChristian Ehrhardt#define TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT_OFFSET(a, b, len, off, msg, ...) do {	\
111809f0800SChristian Ehrhardt	uint8_t _first_byte_a, _first_byte_b;                                 \
112809f0800SChristian Ehrhardt	uint8_t _first_byte_mask, _first_byte_bits;                           \
113809f0800SChristian Ehrhardt	uint32_t _len_without_first_byte = (off % 8) ?                       \
114809f0800SChristian Ehrhardt				len - (8 - (off % 8)) :                       \
115809f0800SChristian Ehrhardt				len;                                          \
116809f0800SChristian Ehrhardt	uint32_t _off_in_bytes = (off % 8) ? (off >> 3) + 1 : (off >> 3);     \
117809f0800SChristian Ehrhardt	const uint8_t *_a_with_off = (const uint8_t *)a + _off_in_bytes;      \
118809f0800SChristian Ehrhardt	const uint8_t *_b_with_off = (const uint8_t *)b + _off_in_bytes;      \
119809f0800SChristian Ehrhardt	TEST_ASSERT_BUFFERS_ARE_EQUAL_BIT(_a_with_off, _b_with_off,           \
120809f0800SChristian Ehrhardt				_len_without_first_byte, msg);                \
121809f0800SChristian Ehrhardt	if (off % 8) {                                                        \
122809f0800SChristian Ehrhardt		_first_byte_bits = 8 - (off % 8);                             \
123809f0800SChristian Ehrhardt		_first_byte_mask = (1 << _first_byte_bits) - 1;               \
124809f0800SChristian Ehrhardt		_first_byte_a = *(_a_with_off - 1);                           \
125809f0800SChristian Ehrhardt		_first_byte_b = *(_b_with_off - 1);                           \
126809f0800SChristian Ehrhardt		_first_byte_a &= _first_byte_mask;                            \
127809f0800SChristian Ehrhardt		_first_byte_b &= _first_byte_mask;                            \
128809f0800SChristian Ehrhardt		if (_first_byte_a != _first_byte_b) {                         \
129809f0800SChristian Ehrhardt			printf("TestCase %s() line %d failed: "               \
130809f0800SChristian Ehrhardt				msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
131809f0800SChristian Ehrhardt			TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);     \
132809f0800SChristian Ehrhardt			return TEST_FAILED;                                   \
133809f0800SChristian Ehrhardt		}                                                             \
134809f0800SChristian Ehrhardt	}                                                                     \
135809f0800SChristian Ehrhardt} while (0)
1365129044dSC.J. Collier
1375129044dSC.J. Collier#define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do {               \
1385129044dSC.J. Collier		if (!(a != b)) {                                         \
1395129044dSC.J. Collier			printf("TestCase %s() line %d failed: "              \
1405129044dSC.J. Collier				msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
1415129044dSC.J. Collier			TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
1425129044dSC.J. Collier			return TEST_FAILED;                                  \
1435129044dSC.J. Collier		}                                                        \
1445129044dSC.J. Collier} while (0)
1455129044dSC.J. Collier
1465129044dSC.J. Collier#define TEST_ASSERT_SUCCESS(val, msg, ...) do {                  \
1475129044dSC.J. Collier		typeof(val) _val = (val);                                \
1485129044dSC.J. Collier		if (!(_val == 0)) {                                      \
1495129044dSC.J. Collier			printf("TestCase %s() line %d failed (err %d): "     \
1505129044dSC.J. Collier				msg "\n", __func__, __LINE__, _val,              \
1515129044dSC.J. Collier				##__VA_ARGS__);                                  \
1525129044dSC.J. Collier			TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
1535129044dSC.J. Collier			return TEST_FAILED;                                  \
1545129044dSC.J. Collier		}                                                        \
1555129044dSC.J. Collier} while (0)
1565129044dSC.J. Collier
1575129044dSC.J. Collier#define TEST_ASSERT_FAIL(val, msg, ...) do {                     \
1585129044dSC.J. Collier		if (!(val != 0)) {                                       \
1595129044dSC.J. Collier			printf("TestCase %s() line %d failed: "              \
1605129044dSC.J. Collier				msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
1615129044dSC.J. Collier			TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
1625129044dSC.J. Collier			return TEST_FAILED;                                  \
1635129044dSC.J. Collier		}                                                        \
1645129044dSC.J. Collier} while (0)
1655129044dSC.J. Collier
1665129044dSC.J. Collier#define TEST_ASSERT_NULL(val, msg, ...) do {                     \
1675129044dSC.J. Collier		if (!(val == NULL)) {                                    \
1685129044dSC.J. Collier			printf("TestCase %s() line %d failed: "              \
1695129044dSC.J. Collier				msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
1705129044dSC.J. Collier			TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
1715129044dSC.J. Collier			return TEST_FAILED;                                  \
1725129044dSC.J. Collier		}                                                        \
1735129044dSC.J. Collier} while (0)
1745129044dSC.J. Collier
1755129044dSC.J. Collier#define TEST_ASSERT_NOT_NULL(val, msg, ...) do {                 \
1765129044dSC.J. Collier		if (!(val != NULL)) {                                    \
1775129044dSC.J. Collier			printf("TestCase %s() line %d failed: "              \
1785129044dSC.J. Collier				msg "\n", __func__, __LINE__, ##__VA_ARGS__);    \
1795129044dSC.J. Collier			TEST_TRACE_FAILURE(__FILE__, __LINE__, __func__);    \
1805129044dSC.J. Collier			return TEST_FAILED;                                  \
1815129044dSC.J. Collier		}                                                        \
1825129044dSC.J. Collier} while (0)
1835129044dSC.J. Collier
1845129044dSC.J. Collierstruct unit_test_case {
1855129044dSC.J. Collier	int (*setup)(void);
1865129044dSC.J. Collier	void (*teardown)(void);
1875129044dSC.J. Collier	int (*testcase)(void);
1885129044dSC.J. Collier	const char *success_msg;
1895129044dSC.J. Collier	const char *fail_msg;
1905129044dSC.J. Collier	unsigned enabled;
1915129044dSC.J. Collier};
1925129044dSC.J. Collier
1935129044dSC.J. Collier#define TEST_CASE(fn) { NULL, NULL, fn, #fn " succeeded", #fn " failed", 1 }
1945129044dSC.J. Collier
1955129044dSC.J. Collier#define TEST_CASE_NAMED(name, fn) { NULL, NULL, fn, name " succeeded", \
1965129044dSC.J. Collier		name " failed", 1 }
1975129044dSC.J. Collier
1985129044dSC.J. Collier#define TEST_CASE_ST(setup, teardown, testcase)         \
1995129044dSC.J. Collier		{ setup, teardown, testcase, #testcase " succeeded",    \
2005129044dSC.J. Collier		#testcase " failed ", 1 }
2015129044dSC.J. Collier
2025129044dSC.J. Collier
2035129044dSC.J. Collier#define TEST_CASE_DISABLED(fn) { NULL, NULL, fn, #fn " succeeded", \
2045129044dSC.J. Collier	#fn " failed", 0 }
2055129044dSC.J. Collier
2065129044dSC.J. Collier#define TEST_CASE_ST_DISABLED(setup, teardown, testcase)         \
2075129044dSC.J. Collier		{ setup, teardown, testcase, #testcase " succeeded",    \
2085129044dSC.J. Collier		#testcase " failed ", 0 }
2095129044dSC.J. Collier
2105129044dSC.J. Collier#define TEST_CASES_END() { NULL, NULL, NULL, NULL, NULL, 0 }
2115129044dSC.J. Collier
212809f0800SChristian Ehrhardt#if RTE_LOG_LEVEL >= RTE_LOG_DEBUG
213809f0800SChristian Ehrhardt#define TEST_HEXDUMP(file, title, buf, len) rte_hexdump(file, title, buf, len)
214809f0800SChristian Ehrhardt#else
215809f0800SChristian Ehrhardt#define TEST_HEXDUMP(file, title, buf, len) do {} while (0)
216809f0800SChristian Ehrhardt#endif
217809f0800SChristian Ehrhardt
2185129044dSC.J. Collierstruct unit_test_suite {
2195129044dSC.J. Collier	const char *suite_name;
2205129044dSC.J. Collier	int (*setup)(void);
2215129044dSC.J. Collier	void (*teardown)(void);
2225129044dSC.J. Collier	struct unit_test_case unit_test_cases[];
2235129044dSC.J. Collier};
2245129044dSC.J. Collier
2255129044dSC.J. Collierint unit_test_suite_runner(struct unit_test_suite *suite);
2265129044dSC.J. Collier
2275129044dSC.J. Collier#define RECURSIVE_ENV_VAR "RTE_TEST_RECURSIVE"
2285129044dSC.J. Collier
2295129044dSC.J. Collier#include <cmdline_parse.h>
2305129044dSC.J. Collier#include <cmdline_parse_string.h>
2315129044dSC.J. Collier
2325129044dSC.J. Collierextern const char *prgname;
2335129044dSC.J. Collier
2345129044dSC.J. Collierint commands_init(void);
2355129044dSC.J. Collier
2365129044dSC.J. Collierint test_pci(void);
2375129044dSC.J. Collierint test_pci_run;
2385129044dSC.J. Collier
2395129044dSC.J. Collierint test_mp_secondary(void);
2405129044dSC.J. Collier
2415129044dSC.J. Collierint test_set_rxtx_conf(cmdline_fixed_string_t mode);
2425129044dSC.J. Collierint test_set_rxtx_anchor(cmdline_fixed_string_t type);
2435129044dSC.J. Collierint test_set_rxtx_sc(cmdline_fixed_string_t type);
2445129044dSC.J. Collier
2455129044dSC.J. Colliertypedef int (test_callback)(void);
2465129044dSC.J. CollierTAILQ_HEAD(test_commands_list, test_command);
2475129044dSC.J. Collierstruct test_command {
2485129044dSC.J. Collier	TAILQ_ENTRY(test_command) next;
2495129044dSC.J. Collier	const char *command;
2505129044dSC.J. Collier	test_callback *callback;
2515129044dSC.J. Collier};
2525129044dSC.J. Collier
2535129044dSC.J. Colliervoid add_test_command(struct test_command *t);
2545129044dSC.J. Collier
2559ecc306dSRicardo Salveti/* Register a test function with its command string */
2569ecc306dSRicardo Salveti#define REGISTER_TEST_COMMAND(cmd, func) \
2579ecc306dSRicardo Salveti	static struct test_command test_struct_##cmd = { \
2589ecc306dSRicardo Salveti		.command = RTE_STR(cmd), \
2599ecc306dSRicardo Salveti		.callback = func, \
2609ecc306dSRicardo Salveti	}; \
2619ecc306dSRicardo Salveti	static void __attribute__((constructor, used)) \
2629ecc306dSRicardo Salveti	test_register_##cmd(void) \
2639ecc306dSRicardo Salveti	{ \
2649ecc306dSRicardo Salveti		add_test_command(&test_struct_##cmd); \
2659ecc306dSRicardo Salveti	}
2665129044dSC.J. Collier
2675129044dSC.J. Collier#endif
268