[OSCBS-41] Update to Zephyr v3.2.0 (#382)

Updating to integrate with Zephyr v3.2.0 required:

- Update `west.yml` to import Zephyr v3.2.0 manifest
- Prefix include pathname of ztest.h with `zephyr/`
- Prefix every Zephyr header included pathname with `zephyr/`
- Change all Zephyr tests/samples to use `find_package`
- For unit_testing, use a distinct prj.conf which only references
  Kconfigs defined in the Zephyr repo. (Zephyr constraint.)
- Move ztest headers into a zephyr-prefixed pathname

Co-authored-by: Gregory Shue <gregory.shue@legrand.com>
This commit is contained in:
Greg Shue
2023-01-18 08:50:31 -08:00
committed by GitHub
parent 1a7a810f1a
commit eb36033fd8
262 changed files with 580 additions and 285 deletions
+24
View File
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2015 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_ZEPHYR_H_
#define ZEPHYR_INCLUDE_ZEPHYR_H_
/*
* Applications can identify whether they are built for Zephyr by
* macro below. (It may be already defined by a makefile or toolchain.)
*/
#ifndef __ZEPHYR__
#define __ZEPHYR__
#endif
//GAS: rm? #include <kernel.h>
//GAS: add?
#define Z_STRINGIFY(x) #x
#define STRINGIFY(s) Z_STRINGIFY(s)
#endif /* ZEPHYR_INCLUDE_ZEPHYR_H_ */
+77
View File
@@ -0,0 +1,77 @@
/* printk.h - low-level debug output */
/*
* Copyright (c) 2010-2012, 2014 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*
* Modified from zephyr_v2.2.0 include/sys/printk.h
* because:
* 1. This port will never be run in the Zephyr kernel.
* This repository is extended to be a Zephyr module for that.
*
* Modifications:
* a. Code conditionally compiled on the following CPP symbols were deleted
* (as they were kernel-specific):
* CONFIG_PRINTK
* KERNEL
* b. Inclusion of The following header files were removed as irrelevant.
* <toolchain.h>
* <inttypes.h>
*/
#ifndef ZEPHYR_INCLUDE_SYS_PRINTK_H_
#define ZEPHYR_INCLUDE_SYS_PRINTK_H_
#include <stddef.h>
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
*
* @brief Print kernel debugging message.
*
* This routine prints a kernel debugging message to the system console.
* Output is send immediately, without any mutual exclusion or buffering.
*
* A basic set of conversion specifier characters are supported:
* - signed decimal: \%d, \%i
* - unsigned decimal: \%u
* - unsigned hexadecimal: \%x (\%X is treated as \%x)
* - pointer: \%p
* - string: \%s
* - character: \%c
* - percent: \%\%
*
* Field width (with or without leading zeroes) is supported.
* Length attributes h, hh, l, ll and z are supported. However, integral
* values with %lld and %lli are only printed if they fit in a long
* otherwise 'ERR' is printed. Full 64-bit values may be printed with %llx.
* Flags and precision attributes are not supported.
*
* @param fmt Format string.
* @param ... Optional list of format arguments.
*
* @return N/A
*/
extern void printk(const char *fmt, ...);
extern void vprintk(const char *fmt, va_list ap);
//GAS: useful? #warning "z_vprintk is not supported in this port of ztest"
static inline void z_vprintk(int (*out)(int f, void *c), void *ctx,
const char *fmt, va_list ap)
{
(void)(out);
(void)(ctx);
(void)(fmt);
(void)(ap);
}
#ifdef __cplusplus
}
#endif
#endif
+223
View File
@@ -0,0 +1,223 @@
/* tc_utilities.h - testcase utilities header file */
/*
* Copyright (c) 2012-2015 Wind River Systems, Inc.
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_TESTSUITE_INCLUDE_TC_UTIL_H_
#define ZEPHYR_TESTSUITE_INCLUDE_TC_UTIL_H_
#include <zephyr/kernel.h>
#include <string.h>
#ifdef CONFIG_SHELL
#include <zephyr/shell/shell.h>
#endif
#include <zephyr/sys/printk.h>
#if defined CONFIG_ZTEST_TC_UTIL_USER_OVERRIDE
#include <tc_util_user_override.h>
#endif
#ifndef PRINT_DATA
#define PRINT_DATA(fmt, ...) printk(fmt, ##__VA_ARGS__)
#endif
#if defined CONFIG_ARCH_POSIX
#include "posix_board_if.h"
#endif
/**
* @def TC_PRINT_RUNID
* @brief Report a Run ID
*
* When the CPP symbol \c TC_RUNID is defined (for example, from the
* compile environment), print the defined string ``RunID:
* <TC_RUNID>`` when called (TC_END_REPORT() will also call it).
*
* This is used mainly when automating the execution and running of
* multiple test cases, to verify that the expected image is being
* executed (as sometimes the targets fail to flash or reset
* properly).
*
* TC_RUNID is any string, that will be converted to a string literal.
*/
#define TC_STR_HELPER(x) #x
#define TC_STR(x) TC_STR_HELPER(x)
#ifdef TC_RUNID
#define TC_PRINT_RUNID PRINT_DATA("RunID: " TC_STR(TC_RUNID) "\n")
#else
#define TC_PRINT_RUNID do {} while (0)
#endif
#ifndef PRINT_LINE
#define PRINT_LINE \
PRINT_DATA( \
"============================================================" \
"=======\n")
#endif
/* stack size and priority for test suite task */
#define TASK_STACK_SIZE (1024 * 2)
#define FMT_ERROR "%s - %s@%d. "
#define TC_PASS 0
#define TC_FAIL 1
#define TC_SKIP 2
#ifndef TC_PASS_STR
#define TC_PASS_STR "PASS"
#endif
#ifndef TC_FAIL_STR
#define TC_FAIL_STR "FAIL"
#endif
#ifndef TC_SKIP_STR
#define TC_SKIP_STR "SKIP"
#endif
static inline const char *TC_RESULT_TO_STR(int result)
{
switch (result) {
case TC_PASS:
return TC_PASS_STR;
case TC_FAIL:
return TC_FAIL_STR;
case TC_SKIP:
return TC_SKIP_STR;
default:
return "?";
}
}
#if 0
static uint32_t tc_start_time;
#endif
static uint32_t tc_spend_time;
static inline void get_start_time_cyc(void)
{
/* Besides the ztest framework, some testcase will also call
* TC_START() in their code. But the caller thread cannot be
* in userspace.
*/
#if 0
if (!k_is_user_context()) {
tc_start_time = k_cycle_get_32();
}
#endif
}
static inline void test_time_ms(void)
{
#if 0
uint32_t spend_cycle = k_cycle_get_32() - tc_start_time;
tc_spend_time = k_cyc_to_ms_ceil32(spend_cycle);
#endif
}
#ifndef TC_ERROR
#define TC_ERROR(fmt, ...) \
do { \
PRINT_DATA(FMT_ERROR, "FAIL", __func__, __LINE__); \
PRINT_DATA(fmt, ##__VA_ARGS__); \
} while (0)
#endif
#ifndef TC_PRINT
#define TC_PRINT(fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
#endif
#ifndef TC_START
#define TC_START(name) \
do { \
PRINT_DATA("START - %s\n", name); \
get_start_time_cyc(); \
} while (0)
#endif
#ifndef TC_END
#define TC_END(result, fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
#endif
#ifndef Z_TC_END_RESULT
/* prints result and the function name */
#define Z_TC_END_RESULT(result, func) \
do { \
test_time_ms(); \
TC_END(result, " %s - %s in %u.%u seconds\n", \
TC_RESULT_TO_STR(result), func, tc_spend_time/1000, \
tc_spend_time%1000); \
PRINT_LINE; \
} while (0)
#endif
#ifndef TC_END_RESULT
#define TC_END_RESULT(result) \
Z_TC_END_RESULT((result), __func__)
#endif
#ifndef TC_SUITE_START
#define TC_SUITE_START(name) \
do { \
TC_PRINT("Running test suite %s\n", name); \
PRINT_LINE; \
} while (0)
#endif
#ifndef TC_SUITE_END
#define TC_SUITE_END(name, result) \
do { \
if (result == TC_PASS) { \
TC_PRINT("Test suite %s succeeded\n", name); \
} else { \
TC_PRINT("Test suite %s failed.\n", name); \
} \
} while (0)
#endif
#if defined(CONFIG_ARCH_POSIX)
#define TC_END_POST(result) posix_exit(result)
#else
#define TC_END_POST(result)
#endif /* CONFIG_ARCH_POSIX */
#ifndef TC_END_REPORT
#define TC_END_REPORT(result) \
do { \
PRINT_LINE; \
TC_PRINT_RUNID; \
TC_END(result, \
"PROJECT EXECUTION %s\n", \
(result) == TC_PASS ? "SUCCESSFUL" : "FAILED"); \
TC_END_POST(result); \
} while (0)
#endif
#if defined(CONFIG_SHELL)
#define TC_CMD_DEFINE(name) \
static int cmd_##name(const struct shell *shell, size_t argc, \
char **argv) \
{ \
TC_START(__func__); \
name(); \
TC_END_RESULT(TC_PASS); \
return 0; \
}
#define TC_CMD_ITEM(name) cmd_##name
#else
#define TC_CMD_DEFINE(name) \
int cmd_##name(int argc, char *argv[]) \
{ \
TC_START(__func__); \
name(); \
TC_END_RESULT(TC_PASS); \
return 0; \
}
#define TC_CMD_ITEM(name) {STRINGIFY(name), cmd_##name, "none"}
#endif
#endif /* ZEPHYR_TESTSUITE_INCLUDE_TC_UTIL_H_ */
+85
View File
@@ -0,0 +1,85 @@
/* test_utils.h - TinyCrypt interface to common functions for tests */
/*
* Copyright (C) 2015 by Intel Corporation, All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __TEST_UTILS_H__
#define __TEST_UTILS_H__
#include <zephyr/tc_util.h>
#include <tinycrypt/constants.h>
static inline void show_str(const char *label, const uint8_t *s, size_t len)
{
uint32_t i;
TC_PRINT("%s = ", label);
for (i = 0U; i < (uint32_t)len; ++i) {
TC_PRINT("%02x", s[i]);
}
TC_PRINT("\n");
}
static inline
void fatal(uint32_t testnum, const void *expected, size_t expectedlen,
const void *computed, size_t computedlen)
{
TC_ERROR("\tTest #%d Failed!\n", testnum);
show_str("\t\tExpected", expected, expectedlen);
show_str("\t\tComputed ", computed, computedlen);
TC_PRINT("\n");
}
static inline
uint32_t check_result(uint32_t testnum, const void *expected,
size_t expectedlen, const void *computed,
size_t computedlen, uint32_t verbose)
{
uint32_t result = TC_PASS;
ARG_UNUSED(verbose);
if (expectedlen != computedlen) {
TC_ERROR("The length of the computed buffer (%zu)",
computedlen);
TC_ERROR("does not match the expected length (%zu).",
expectedlen);
result = TC_FAIL;
} else {
if (memcmp(computed, expected, computedlen) != 0) {
fatal(testnum, expected, expectedlen,
computed, computedlen);
result = TC_FAIL;
}
}
return result;
}
#endif
+81
View File
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
*
* @brief Zephyr Testsuite
*
* From zephyr_v2.6.0 subsys/testsuite/ztest/include/ztest.h
* Note:
* 1. This port will never be run in the Zephyr kernel.
* This repository is extended to be a Zephyr module for that.
* 2. This port will not support multiple CPUs or toolchains.
*/
/**
* @brief Zephyr Tests (ZTest)
* @defgroup all_tests Zephyr Tests
* @{
* @}
*/
#ifndef ZEPHYR_TESTSUITE_INCLUDE_ZTEST_H_
#define ZEPHYR_TESTSUITE_INCLUDE_ZTEST_H_
/**
* @defgroup ztest Zephyr testing suite
*/
#if !defined(CONFIG_ZTEST) && !defined(ZTEST_UNITTEST)
#error "You need to add CONFIG_ZTEST to your config file."
#endif
#ifndef KERNEL
#define CONFIG_STDOUT_CONSOLE 1
#define CONFIG_ZTEST_ASSERT_VERBOSE 1
#define CONFIG_ZTEST_MOCKING
#define CONFIG_NUM_COOP_PRIORITIES 16
#define CONFIG_COOP_ENABLED 1
#define CONFIG_PREEMPT_ENABLED 1
#define CONFIG_MP_NUM_CPUS 1
#define CONFIG_SYS_CLOCK_TICKS_PER_SEC 100
#define CONFIG_SYS_CLOCK_HW_CYCLES_PER_SEC 10000000
#define ARCH_STACK_PTR_ALIGN 8
/* FIXME: Properly integrate with Zephyr's arch specific code */
#define CONFIG_X86 1
#define CONFIG_PRINTK 1
#ifdef __cplusplus
extern "C" {
#endif
struct esf;
typedef struct esf z_arch_esf_t;
#ifdef __cplusplus
}
#endif
#endif /* KERNEL */
#include <zephyr/sys/printk.h>
#define PRINT printk
#include <zephyr/kernel.h>
#include <zephyr/ztest_assert.h>
#include <zephyr/ztest_mock.h>
#include <zephyr/ztest_test.h>
#include <zephyr/tc_util.h>
#ifdef __cplusplus
extern "C" {
#endif
void test_main(void);
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_TESTSUITE_INCLUDE_ZTEST_H_ */
+244
View File
@@ -0,0 +1,244 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
*
* @brief Zephyr testing framework assertion macros
*/
#ifndef ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_
#define ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_
#include <zephyr/ztest.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
const char *ztest_relative_filename(const char *file);
void ztest_test_fail(void);
#if CONFIG_ZTEST_ASSERT_VERBOSE == 0
static inline bool z_zassert_(bool cond, const char *file, int line)
{
if (cond == false) {
PRINT("\n Assertion failed at %s:%d\n",
ztest_relative_filename(file), line);
ztest_test_fail();
return false;
}
return true;
}
#define z_zassert(cond, default_msg, file, line, func, msg, ...) \
z_zassert_(cond, file, line)
#else /* CONFIG_ZTEST_ASSERT_VERBOSE != 0 */
static inline bool z_zassert(bool cond,
const char *default_msg,
const char *file,
int line, const char *func,
const char *msg, ...)
{
if (cond == false) {
va_list vargs;
va_start(vargs, msg);
PRINT("\n Assertion failed at %s:%d: %s: %s\n",
ztest_relative_filename(file), line, func, default_msg);
vprintk(msg, vargs);
printk("\n");
va_end(vargs);
ztest_test_fail();
return false;
}
#if CONFIG_ZTEST_ASSERT_VERBOSE == 2
else {
PRINT("\n Assertion succeeded at %s:%d (%s)\n",
ztest_relative_filename(file), line, func);
}
#endif
return true;
}
#endif /* CONFIG_ZTEST_ASSERT_VERBOSE */
/**
* @defgroup ztest_assert Ztest assertion macros
* @ingroup ztest
*
* This module provides assertions when using Ztest.
*
* @{
*/
/**
* @brief Fail the test, if @a cond is false
*
* You probably don't need to call this macro directly. You should
* instead use zassert_{condition} macros below.
*
* Note that when CONFIG_MULTITHREADING=n macro returns from the function. It is
* then expected that in that case ztest asserts will be used only in the
* context of the test function.
*
* @param cond Condition to check
* @param msg Optional, can be NULL. Message to print if @a cond is false.
* @param default_msg Message to print if @a cond is false
*/
#define zassert(cond, default_msg, msg, ...) do { \
bool _ret = z_zassert(cond, msg ? ("(" default_msg ")") : (default_msg), \
__FILE__, __LINE__, __func__, \
msg ? msg : "", ##__VA_ARGS__); \
} while (0)
/**
* @brief Assert that this function call won't be reached
* @param msg Optional message to print if the assertion fails
*/
#define zassert_unreachable(msg, ...) zassert(0, "Reached unreachable code", \
msg, ##__VA_ARGS__)
/**
* @brief Assert that @a cond is true
* @param cond Condition to check
* @param msg Optional message to print if the assertion fails
*/
#define zassert_true(cond, msg, ...) zassert(cond, #cond " is false", \
msg, ##__VA_ARGS__)
/**
* @brief Assert that @a cond is false
* @param cond Condition to check
* @param msg Optional message to print if the assertion fails
*/
#define zassert_false(cond, msg, ...) zassert(!(cond), #cond " is true", \
msg, ##__VA_ARGS__)
/**
* @brief Assert that @a cond is 0 (success)
* @param cond Condition to check
* @param msg Optional message to print if the assertion fails
*/
#define zassert_ok(cond, msg, ...) zassert(!(cond), #cond " is non-zero", \
msg, ##__VA_ARGS__)
/**
* @brief Assert that @a ptr is NULL
* @param ptr Pointer to compare
* @param msg Optional message to print if the assertion fails
*/
#define zassert_is_null(ptr, msg, ...) zassert((ptr) == NULL, \
#ptr " is not NULL", \
msg, ##__VA_ARGS__)
/**
* @brief Assert that @a ptr is not NULL
* @param ptr Pointer to compare
* @param msg Optional message to print if the assertion fails
*/
#define zassert_not_null(ptr, msg, ...) zassert((ptr) != NULL, \
#ptr " is NULL", msg, \
##__VA_ARGS__)
/**
* @brief Assert that @a a equals @a b
*
* @a a and @a b won't be converted and will be compared directly.
*
* @param a Value to compare
* @param b Value to compare
* @param msg Optional message to print if the assertion fails
*/
#define zassert_equal(a, b, msg, ...) zassert((a) == (b), \
#a " not equal to " #b, \
msg, ##__VA_ARGS__)
/**
* @brief Assert that @a a does not equal @a b
*
* @a a and @a b won't be converted and will be compared directly.
*
* @param a Value to compare
* @param b Value to compare
* @param msg Optional message to print if the assertion fails
*/
#define zassert_not_equal(a, b, msg, ...) zassert((a) != (b), \
#a " equal to " #b, \
msg, ##__VA_ARGS__)
/**
* @brief Assert that @a a equals @a b
*
* @a a and @a b will be converted to `void *` before comparing.
*
* @param a Value to compare
* @param b Value to compare
* @param msg Optional message to print if the assertion fails
*/
#define zassert_equal_ptr(a, b, msg, ...) \
zassert((void *)(a) == (void *)(b), #a " not equal to " #b, \
msg, ##__VA_ARGS__)
/**
* @brief Assert that @a a is within @a b with delta @a d
*
* @param a Value to compare
* @param b Value to compare
* @param d Delta
* @param msg Optional message to print if the assertion fails
*/
#define zassert_within(a, b, d, msg, ...) \
zassert(((a) >= ((b) - (d))) && ((a) <= ((b) + (d))), \
#a " not within " #b " +/- " #d, \
msg, ##__VA_ARGS__)
/**
* @brief Assert that 2 memory buffers have the same contents
*
* This macro calls the final memory comparison assertion macro.
* Using double expansion allows providing some arguments by macros that
* would expand to more than one values (ANSI-C99 defines that all the macro
* arguments have to be expanded before macro call).
*
* @param ... Arguments, see @ref zassert_mem_equal__
* for real arguments accepted.
*/
#define zassert_mem_equal(...) \
zassert_mem_equal__(__VA_ARGS__)
/**
* @brief Internal assert that 2 memory buffers have the same contents
*
* @note This is internal macro, to be used as a second expansion.
* See @ref zassert_mem_equal.
*
* @param buf Buffer to compare
* @param exp Buffer with expected contents
* @param size Size of buffers
* @param msg Optional message to print if the assertion fails
*/
#define zassert_mem_equal__(buf, exp, size, msg, ...) \
zassert(memcmp(buf, exp, size) == 0, #buf " not equal to " #exp, \
msg, ##__VA_ARGS__)
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_TESTSUITE_ZTEST_ASSERT_H_ */
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2020 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef ZEPHYR_INCLUDE_ZTEST_FATAL_HOOK_H_
#define ZEPHYR_INCLUDE_ZTEST_FATAL_HOOK_H_
#include <zephyr/kernel.h>
#if defined(CONFIG_ZTEST_FATAL_HOOK)
/**
* @brief Set the flag that treat fatal error happened as expected
*
* @details This is used for negative test cases which triggers a fatal
* error. Set the param true will still pass the test case when expected
* fatal error happened. For normal test case, set it false makes it back
* to normal behavior.
*
* @param valid flag indicate fault is expected
*/
__syscall void ztest_set_fault_valid(bool valid);
/* @brief A hook after fatal error handler
*
* @details This is a test case hook that can run code from test case, in
* order to deal with some special case when catching the expected fatal
* error.
*
* Usage: Define your own hook function in your test case code, and do what
* you want to do after fatal error handler.
*
* By default, it will do nothing before leaving error handler.
*/
void ztest_post_fatal_error_hook(unsigned int reason,
const z_arch_esf_t *pEsf);
#endif
#if defined(CONFIG_ZTEST_ASSERT_HOOK)
/**
* @brief Set the flag that treat assert fail happened as expected
*
* @details This is used for negative test cases which triggers a assert
* fail. Set the param true will still pass the test case when expected
* assert fail happened. For normal test case, set it false make it back
* to normal behavior.
*
* @param valid flag indicate assert is expected
*/
__syscall void ztest_set_assert_valid(bool valid);
/* @brief A hook after assert fault handler
*
* @details This is a test case hook that can run code from test case, in
* order to deal with some special case when catching the expected assert
* failed.
*
* Usage: Define your own hook function in your test case code, and do what
* you want to do after assert handler.
*
* By default, it will abort the thread which assert failed.
*/
void ztest_post_assert_fail_hook(void);
#endif
#if defined(CONFIG_ZTEST_FATAL_HOOK) || defined(CONFIG_ZTEST_ASSERT_HOOK)
#include <syscalls/ztest_error_hook.h>
#endif
#endif /* ZEPHYR_INCLUDE_ZTEST_FATAL_HOOK_H_ */
+184
View File
@@ -0,0 +1,184 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
*
* @brief Ztest mocking support
*/
#ifndef ZEPHYR_TESTSUITE_ZTEST_MOCK_H_
#define ZEPHYR_TESTSUITE_ZTEST_MOCK_H_
/**
* @defgroup ztest_mock Ztest mocking support
* @ingroup ztest
*
* This module provides simple mocking functions for unit testing. These
* need CONFIG_ZTEST_MOCKING=y.
*
* @{
*/
/**
* @brief Tell function @a func to expect the value @a value for @a param
*
* When using ztest_check_expected_value(), tell that the value of @a param
* should be @a value. The value will internally be stored as an `uintptr_t`.
*
* @param func Function in question
* @param param Parameter for which the value should be set
* @param value Value for @a param
*/
#define ztest_expect_value(func, param, value) \
z_ztest_expect_value(STRINGIFY(func), STRINGIFY(param), \
(uintptr_t)(value))
/**
* @brief If @a param doesn't match the value set by ztest_expect_value(),
* fail the test
*
* This will first check that does @a param have a value to be expected, and
* then checks whether the value of the parameter is equal to the expected
* value. If either of these checks fail, the current test will fail. This
* must be called from the called function.
*
* @param param Parameter to check
*/
#define ztest_check_expected_value(param) \
z_ztest_check_expected_value(__func__, STRINGIFY(param), \
(uintptr_t)(param))
/**
* @brief Tell function @a func to expect the data @a data for @a param
*
* When using ztest_check_expected_data(), the data pointed to by
* @a param should be same @a data in this function. Only data pointer is stored
* by this function, so it must still be valid when ztest_check_expected_data is
* called.
*
* @param func Function in question
* @param param Parameter for which the data should be set
* @param data pointer for the data for parameter @a param
*/
#define ztest_expect_data(func, param, data) \
z_ztest_expect_data(STRINGIFY(func), STRINGIFY(param), (void *)(data))
/**
* @brief If data pointed by @a param don't match the data set by
* ztest_expect_data(), fail the test
*
* This will first check that @a param is expected to be null or non-null and
* then check whether the data pointed by parameter is equal to expected data.
* If either of these checks fail, the current test will fail. This
* must be called from the called function.
*
* @param param Parameter to check
* @param length Length of the data to compare
*/
#define ztest_check_expected_data(param, length) \
z_ztest_check_expected_data(__func__, STRINGIFY(param), \
(void *)(param), (length))
/**
* @brief Tell function @a func to return the data @a data for @a param
*
* When using ztest_return_data(), the data pointed to by @a param should be
* same @a data in this function. Only data pointer is stored by this function,
* so it must still be valid when ztest_copy_return_data is called.
*
* @param func Function in question
* @param param Parameter for which the data should be set
* @param data pointer for the data for parameter @a param
*/
#define ztest_return_data(func, param, data) \
z_ztest_return_data(STRINGIFY(func), STRINGIFY(param), (void *)(data))
/**
* @brief Copy the data set by ztest_return_data to the memory pointed by
* @a param
*
* This will first check that @a param is not null and then copy the data.
* This must be called from the called function.
*
* @param param Parameter to return data for
* @param length Length of the data to return
*/
#define ztest_copy_return_data(param, length) \
z_ztest_copy_return_data(__func__, STRINGIFY(param), \
(void *)(param), (length))
/**
* @brief Tell @a func that it should return @a value
*
* @param func Function that should return @a value
* @param value Value to return from @a func
*/
#define ztest_returns_value(func, value) \
z_ztest_returns_value(STRINGIFY(func), (uintptr_t)(value))
/**
* @brief Get the return value for current function
*
* The return value must have been set previously with ztest_returns_value().
* If no return value exists, the current test will fail.
*
* @returns The value the current function should return
*/
#define ztest_get_return_value() z_ztest_get_return_value(__func__)
/**
* @brief Get the return value as a pointer for current function
*
* The return value must have been set previously with ztest_returns_value().
* If no return value exists, the current test will fail.
*
* @returns The value the current function should return as a `void *`
*/
#define ztest_get_return_value_ptr() \
((void *)z_ztest_get_return_value(__func__))
/**
* @}
*/
#ifdef CONFIG_ZTEST_MOCKING
#include <zephyr/types.h>
#ifdef __cplusplus
extern "C" {
#endif
void z_init_mock(void);
int z_cleanup_mock(void);
void z_ztest_expect_value(const char *fn, const char *name, uintptr_t value);
void z_ztest_check_expected_value(const char *fn, const char *param,
uintptr_t value);
void z_ztest_expect_data(const char *fn, const char *name, void *val);
void z_ztest_check_expected_data(const char *fn, const char *name, void *data,
uint32_t length);
void z_ztest_return_data(const char *fn, const char *name, void *val);
void z_ztest_copy_return_data(const char *fn, const char *name, void *data,
uint32_t length);
void z_ztest_returns_value(const char *fn, uintptr_t value);
uintptr_t z_ztest_get_return_value(const char *fn);
#ifdef __cplusplus
}
#endif
#else /* !CONFIG_ZTEST_MOCKING */
#define z_init_mock()
#define z_cleanup_mock() 0
#endif /* CONFIG_ZTEST_MOCKING */
#endif /* ZEPHYR_TESTSUITE_ZTEST_MOCK_H_ */
+233
View File
@@ -0,0 +1,233 @@
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
*
* @brief Zephyr testing framework _test.
*
* From zephyr_v2.6.0 subsys/testsuite/ztest/src/ztest.c
* Note:
* 1. This port will never be run in the Zephyr kernel.
* This repository is extended to be a Zephyr module for that.
* 2. This port will not support multiple CPUs or toolchains.
*
* Modifications:
* 1. Added __ZEPHYR__ conditionals around:
* a. #include <app_memory/app_memdomain.h>
* b. syscall declarations and inclusions.
*/
#ifndef ZEPHYR_TESTSUITE_ZTEST_TEST_H_
#define ZEPHYR_TESTSUITE_ZTEST_TEST_H_
#if 0 /* __ZEPHYR__ */
#include <app_memory/app_memdomain.h>
#endif /* __ZEPHYR__ */
#ifdef __cplusplus
extern "C" {
#endif
struct unit_test {
const char *name;
void (*test)(void);
void (*setup)(void);
void (*teardown)(void);
uint32_t thread_options;
};
void z_ztest_run_test_suite(const char *name, struct unit_test *suite);
/**
* @defgroup ztest_test Ztest testing macros
* @ingroup ztest
*
* This module eases the testing process by providing helpful macros and other
* testing structures.
*
* @{
*/
/**
* @brief Fail the currently running test.
*
* This is the function called from failed assertions and the like. You
* probably don't need to call it yourself.
*/
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 k_sys_fatal_error_handler to indicate that
* the test passed before aborting the thread.
*/
void ztest_test_pass(void);
/**
* @brief Skip the current test.
*
*/
void ztest_test_skip(void);
/**
* @brief Do nothing, successfully.
*
* Unit test / setup function / teardown function that does
* nothing, successfully. Can be used as a parameter to
* ztest_unit_test_setup_teardown().
*/
static inline void unit_test_noop(void)
{
}
/**
* @brief Define a test with setup and teardown functions
*
* This should be called as an argument to ztest_test_suite. The test will
* be run in the following order: @a setup, @a fn, @a teardown.
*
* @param fn Main test function
* @param setup Setup function
* @param teardown Teardown function
*/
#define ztest_unit_test_setup_teardown(fn, setup, teardown) { \
STRINGIFY(fn), fn, setup, teardown, 0 \
}
/**
* @brief Define a user mode test with setup and teardown functions
*
* This should be called as an argument to ztest_test_suite. The test will
* be run in the following order: @a setup, @a fn, @a teardown. ALL
* test functions will be run in user mode, and only if CONFIG_USERSPACE
* is enabled, otherwise this is the same as ztest_unit_test_setup_teardown().
*
* @param fn Main test function
* @param setup Setup function
* @param teardown Teardown function
*/
#define ztest_user_unit_test_setup_teardown(fn, setup, teardown) { \
STRINGIFY(fn), fn, setup, teardown, K_USER \
}
/**
* @brief Define a test function
*
* This should be called as an argument to ztest_test_suite.
*
* @param fn Test function
*/
#define ztest_unit_test(fn) \
ztest_unit_test_setup_teardown(fn, unit_test_noop, unit_test_noop)
/**
* @brief Define a test function that should run as a user thread
*
* This should be called as an argument to ztest_test_suite.
* If CONFIG_USERSPACE is not enabled, this is functionally identical to
* ztest_unit_test().
*
* @param fn Test function
*/
#define ztest_user_unit_test(fn) \
ztest_user_unit_test_setup_teardown(fn, unit_test_noop, unit_test_noop)
#if 0 /* __ZEPHYR__ */
__syscall void z_test_1cpu_start(void);
__syscall void z_test_1cpu_stop(void);
#endif /* __ZEPHYR__ */
/**
* @brief Define a SMP-unsafe test function
*
* As ztest_unit_test(), but ensures all test code runs on only
* one CPU when in SMP.
*
* @param fn Test function
*/
#ifdef CONFIG_SMP
#define ztest_1cpu_unit_test(fn) \
ztest_unit_test_setup_teardown(fn, z_test_1cpu_start, z_test_1cpu_stop)
#else
#define ztest_1cpu_unit_test(fn) ztest_unit_test(fn)
#endif
/**
* @brief Define a SMP-unsafe test function that should run as a user thread
*
* As ztest_user_unit_test(), but ensures all test code runs on only
* one CPU when in SMP.
*
* @param fn Test function
*/
#ifdef CONFIG_SMP
#define ztest_1cpu_user_unit_test(fn) \
ztest_user_unit_test_setup_teardown(fn, z_test_1cpu_start, z_test_1cpu_stop)
#else
#define ztest_1cpu_user_unit_test(fn) ztest_user_unit_test(fn)
#endif
/* definitions for use with testing application shared memory */
#ifdef CONFIG_USERSPACE
#define ZTEST_DMEM K_APP_DMEM(ztest_mem_partition)
#define ZTEST_BMEM K_APP_BMEM(ztest_mem_partition)
#define ZTEST_SECTION K_APP_DMEM_SECTION(ztest_mem_partition)
extern struct k_mem_partition ztest_mem_partition;
#else
#define ZTEST_DMEM
#define ZTEST_BMEM
#define ZTEST_SECTION .data
#endif
/**
* @brief Define a test suite
*
* This function should be called in the following fashion:
* ```{.c}
* ztest_test_suite(test_suite_name,
* ztest_unit_test(test_function),
* ztest_unit_test(test_other_function)
* );
*
* ztest_run_test_suite(test_suite_name);
* ```
*
* @param suite Name of the testing suite
*/
#define ztest_test_suite(suite, ...) \
static ZTEST_DMEM struct unit_test _##suite[] = { \
__VA_ARGS__, { 0 } \
}
/**
* @brief Run the specified test suite.
*
* @param suite Test suite to run.
*/
#define ztest_run_test_suite(suite) \
z_ztest_run_test_suite(#suite, _##suite)
/**
* @}
*/
#ifndef ZTEST_UNITTEST
#if 0 /* __ZEPHYR__ */
#include <syscalls/ztest_test.h>
#endif /* _ZEPHYR__ */
#endif
#ifdef __cplusplus
}
#endif
#endif /* ZEPHYR_TESTSUITE_ZTEST_TEST_H_ */