Skip to content

Commit

Permalink
tests/ztest: Add ztest_test_pass()
Browse files Browse the repository at this point in the history
ztest provides a ztest_test_fail() interface to fail the currently
running test, but does not provide an equivalent ztest_test_pass().
Normally a test passes just by returning without an assertion failure
or other call to ztest_test_fail().  However, if the correct behavior
for a test is to trigger a fatal fault (as with tests/kernel/fatal or
protection or MPU tests), then we need a way for the test to pass the
currently running test before aborting the current thread.
Otherwise, ztest hangs forever in run_test() on the
k_sem_take(&test_end_signal, K_FOREVER) call.  Add
a ztest_test_pass() interface and implement it for kernel and
userspace variants of ztest.  This interface will be used in the
protection tests.

Signed-off-by: Stephen Smalley <[email protected]>
  • Loading branch information
stephensmalley authored and Anas Nashif committed Jun 22, 2017
1 parent 60e752a commit 083fdf3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/ztest/include/ztest_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ void _ztest_run_test_suite(const char *name, struct unit_test *suite);
*/
void ztest_test_fail(void);

/**
* @brief Pass the currently running test.
*
* Normally a test passes just by returning without an assertion failure.
* However, if the success case for your test involves a fatal fault,
* you can call this function from _SysFatalErrorHandler to indicate that
* the test passed before aborting the thread.
*/
void ztest_test_pass(void);

/**
* @brief Do nothing, successfully.
*
Expand Down
18 changes: 18 additions & 0 deletions tests/ztest/src/ztest.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,19 @@ static void run_test_functions(struct unit_test *test)
#define FAIL_FAST 0

static jmp_buf test_fail;
static jmp_buf test_pass;
static jmp_buf stack_fail;

void ztest_test_fail(void)
{
raise(SIGABRT);
}

void ztest_test_pass(void)
{
longjmp(test_pass, 1);
}

static void handle_signal(int sig)
{
static const char *const phase_str[] = {
Expand Down Expand Up @@ -106,6 +112,11 @@ static int run_test(struct unit_test *test)
goto out;
}

if (setjmp(test_pass)) {
ret = TC_PASS;
goto out;
}

run_test_functions(test);
out:
ret |= cleanup_test(test);
Expand Down Expand Up @@ -142,6 +153,13 @@ void ztest_test_fail(void)
k_thread_abort(k_current_get());
}

void ztest_test_pass(void)
{
test_result = 0;
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);
Expand Down

0 comments on commit 083fdf3

Please sign in to comment.