Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | /*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <ztest.h>
#include <stdio.h>
enum {
TEST_PHASE_SETUP,
TEST_PHASE_TEST,
TEST_PHASE_TEARDOWN,
TEST_PHASE_FRAMEWORK
} phase = TEST_PHASE_FRAMEWORK;
static int test_status;
static int cleanup_test(struct unit_test *test)
{
int ret = TC_PASS;
int mock_status;
mock_status = _cleanup_mock();
if (!ret && mock_status == 1) {
PRINT("Test %s failed: Unused mock parameter values\n",
test->name);
ret = TC_FAIL;
} else if (!ret && mock_status == 2) {
PRINT("Test %s failed: Unused mock return values\n",
test->name);
ret = TC_FAIL;
}
return ret;
}
static void run_test_functions(struct unit_test *test)
{
phase = TEST_PHASE_SETUP;
test->setup();
phase = TEST_PHASE_TEST;
test->test();
phase = TEST_PHASE_TEARDOWN;
test->teardown();
phase = TEST_PHASE_FRAMEWORK;
}
#ifndef KERNEL
#include <setjmp.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#define FAIL_FAST 0
static jmp_buf test_fail;
static jmp_buf stack_fail;
void ztest_test_fail(void)
{
raise(SIGABRT);
}
static void handle_signal(int sig)
{
static const char *const phase_str[] = {
"setup",
"unit test",
"teardown",
};
PRINT(" %s", strsignal(sig));
switch (phase) {
case TEST_PHASE_SETUP:
case TEST_PHASE_TEST:
case TEST_PHASE_TEARDOWN:
PRINT(" at %s function\n", phase_str[phase]);
longjmp(test_fail, 1);
case TEST_PHASE_FRAMEWORK:
PRINT("\n");
longjmp(stack_fail, 1);
}
}
static void init_testing(void)
{
signal(SIGABRT, handle_signal);
signal(SIGSEGV, handle_signal);
if (setjmp(stack_fail)) {
PRINT("Test suite crashed.");
exit(1);
}
}
static int run_test(struct unit_test *test)
{
int ret = TC_PASS;
TC_START(test->name);
if (setjmp(test_fail)) {
ret = TC_FAIL;
goto out;
}
run_test_functions(test);
out:
ret |= cleanup_test(test);
_TC_END_RESULT(ret, test->name);
return ret;
}
#else /* KERNEL */
/* Zephyr's probably going to cause all tests to fail if one test fails, so
* skip the rest of tests if one of them fails
*/
#ifdef CONFIG_ZTEST_FAIL_FAST
#define FAIL_FAST 1
#else
#define FAIL_FAST 0
#endif
#if CONFIG_ZTEST_STACKSIZE & (STACK_ALIGN - 1)
#error "CONFIG_ZTEST_STACKSIZE must be a multiple of the stack alignment"
#endif
static struct k_thread ztest_thread;
static char __stack __noinit thread_stack[CONFIG_ZTEST_STACKSIZE +
CONFIG_TEST_EXTRA_STACKSIZE];
static int test_result;
static struct k_sem test_end_signal;
void ztest_test_fail(void)
{
test_result = -1;
k_sem_give(&test_end_signal);
k_thread_abort(k_current_get());
}
static void init_testing(void)
{
k_sem_init(&test_end_signal, 0, 1);
}
static void test_cb(void *a, void *dummy2, void *dummy)
{
struct unit_test *test = (struct unit_test *)a;
ARG_UNUSED(dummy2);
ARG_UNUSED(dummy);
test_result = 1;
run_test_functions(test);
test_result = 0;
k_sem_give(&test_end_signal);
}
static int run_test(struct unit_test *test)
{
int ret = TC_PASS;
TC_START(test->name);
k_thread_create(&ztest_thread, thread_stack, sizeof(thread_stack),
(k_thread_entry_t) test_cb, (struct unit_test *)test,
NULL, NULL, -1, 0, 0);
/*
* There is an implicit expectation here that the thread that was
* spawned is still higher priority than the current thread.
*
* If that is not the case, it will have given the semaphore, which
* will have caused the current thread to run, _if_ the test case
* thread is preemptible, since it is higher priority. If there is
* another test case to be run after the current one finishes, the
* thread_stack will be reused for that new test case while the current
* test case has not finished running yet (it has given the semaphore,
* but has _not_ gone back to _thread_entry() and completed it's "abort
* phase": this will corrupt the kernel ready queue.
*/
k_sem_take(&test_end_signal, K_FOREVER);
if (test_result) {
ret = TC_FAIL;
}
if (!test_result || !FAIL_FAST) {
ret |= cleanup_test(test);
}
_TC_END_RESULT(ret, test->name);
return ret;
}
#endif /* !KERNEL */
void _ztest_run_test_suite(const char *name, struct unit_test *suite)
{
int fail = 0;
if (test_status < 0) {
return;
}
init_testing();
PRINT("Running test suite %s\n", name);
while (suite->test) {
fail += run_test(suite);
suite++;
if (fail && FAIL_FAST) {
break;
}
}
if (fail) {
TC_END_REPORT(TC_FAIL);
} else {
TC_END_REPORT(TC_PASS);
}
test_status = (test_status || fail) ? 1 : 0;
}
void test_main(void);
#ifndef KERNEL
int main(void)
{
_init_mock();
test_main();
return test_status;
}
#else
void main(void)
{
_init_mock();
test_main();
}
#endif
|