blob: 9772875d8f2b7c61071b60e4684dad3f8951cb73 [file] [log] [blame]
Kevin Peng62a87112020-07-07 15:07:46 +08001/*
2 * Copyright (c) 2017-2019, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#ifndef __TEST_FRAMEWORK_H__
9#define __TEST_FRAMEWORK_H__
10
11#include <stdarg.h>
12#include <stdint.h>
13#include <stdio.h>
14
15#include "log/tfm_log_raw.h"
16#include "test_framework_helpers.h"
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22enum test_status_t {
23 TEST_PASSED = 0, /*!< Test has passed */
24 TEST_FAILED = 1, /*!< Test has failed */
25};
26
27struct test_result_t {
28 enum test_status_t val; /*!< Test result \ref test_status_t */
29 const char *info_msg; /*!< Information message to show in case of
30 * failure
31 */
32 const char *filename; /*!< Filename where the failure has occured */
33 uint32_t line; /*!< Line where the failure has occured */
34};
35
36/**
37 * \brief Runs the test.
38 *
39 * \param[out] ret Test result value
40 */
41typedef void TEST_FUN(struct test_result_t *ret);
42
43struct test_t {
44 TEST_FUN * const test; /*!< Test function to call */
45 const char *name; /*!< Test name */
46 const char *desc; /*!< Test description */
47 struct test_result_t ret; /*!< Test result */
48};
49
50struct test_suite_t;
51
52/**
53 * \brief Registers test in the testsuite structure and sets the name.
54 *
55 * \param[in] p_test_suite Pointer to the p_test_suite_location.
56 */
57typedef void TESTSUITE_REG(struct test_suite_t *p_test_suite);
58
59struct test_suite_t {
60 TESTSUITE_REG * const freg; /*!< Function to set all follow fields
61 * of the current test suite
62 */
63 struct test_t *test_list; /*!< List of tests */
64 uint32_t list_size; /*!< List size */
65 const char *name; /*!< Test suite name */
66 enum test_status_t val; /*!< Test suite result \ref test_result_t */
67};
68
69enum test_suite_err_t {
70 TEST_SUITE_ERR_NO_ERROR = 0, /*!< No error */
71 TEST_SUITE_ERR_INVALID_DATA = 1, /*!< Invalid test suite if any of the
72 * pointers is NULL
73 */
74 TEST_SUITE_ERR_INVALID_TEST_DATA = 2, /*!< Invalid test if any of the
75 * pointers is NULL
76 */
77 TEST_SUITE_ERR_TEST_FAILED = 3, /*!< Last executed test has failed */
78};
79
80/**
81 * \brief Translates the test suite error into a string.
82 *
83 * \param[in] err Error value \ref test_suite_err_t
84 *
85 * \returns error as string.
86 */
87const char *test_err_to_str(enum test_suite_err_t err);
88
89/**
90 * \brief Sets test suite parameters.
91 *
92 * \param[in] name Test suite name
93 * \param[in] test_list Pointer to the test list
94 * \param[in] size Test list size
95 * \param[in,out] p_ts Pointer to test suite object to fill in the
96 * parameters
97 *
98 * \returns Returns error code as specified in \ref test_suite_err_t
99 */
100enum test_suite_err_t set_testsuite(const char *name,
101 struct test_t *test_list, uint32_t size,
102 struct test_suite_t *p_ts);
103
104/**
105 * \brief Runs the given test suite.
106 *
107 * \param[in,out] test_suite Test suite to run the list of tests and
108 * store test results.
109 *
110 * \returns Returns error code as specified in \ref test_suite_err_t
111 */
112enum test_suite_err_t run_testsuite(struct test_suite_t *test_suite);
113
114/**
115 * \brief Prints all test in the the given test suite.
116 *
117 * \param[in] ts Test suite to print the list of tests
118 */
119void show_tests(const struct test_suite_t *ts);
120
121/**
122 * \brief Sets test failure state and information in the \ref test_result_t
123 * structure.
124 *
125 * \param[in] info_msg Information message to show
126 * \param[in] filename Filename where the error has ocurred
127 * \param[in] line Line in the file where the error has ocurred
128 * \param[out] ret Pointer to \ref test_result_t structure to
129 * set the values
130 *
131 * \note: If info_msg is "" or , info message is not shown. If filename is "",
132 * filename and line are not shown.
133 */
134void set_test_failed(const char *info_msg, const char *filename, uint32_t line,
135 struct test_result_t *ret);
136
137#define TEST_FAIL(info_msg) set_test_failed(info_msg, __FILE__, __LINE__, ret)
138
139#define TEST_LOG(...) tfm_log_printf(__VA_ARGS__)
140
141#ifdef __cplusplus
142}
143#endif
144
145#endif /* __TEST_FRAMEWORK_H__ */