/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
*
* kselftest_harness.h: simple C unit test helper.
*
* See documentation in Documentation/dev-tools/kselftest.rst
*
* API inspired by code.google.com/p/googletest
*/
/**
* DOC: example
*
* .. code-block:: c
*
* #include "../kselftest_harness.h"
*
* TEST(standalone_test) {
* do_some_stuff;
* EXPECT_GT(10, stuff) {
* stuff_state_t state;
* enumerate_stuff_state(&state);
* TH_LOG("expectation failed with state: %s", state.msg);
* }
* more_stuff;
* ASSERT_NE(some_stuff, NULL) TH_LOG("how did it happen?!");
* last_stuff;
* EXPECT_EQ(0, last_stuff);
* }
*
* FIXTURE(my_fixture) {
* mytype_t *data;
* int awesomeness_level;
* };
* FIXTURE_SETUP(my_fixture) {
* self->data = mytype_new();
* ASSERT_NE(NULL, self->data);
* }
* FIXTURE_TEARDOWN(my_fixture) {
* mytype_free(self->data);
* }
* TEST_F(my_fixture, data_is_good) {
* EXPECT_EQ(1, is_my_data_good(self->data));
* }
*
* TEST_HARNESS_MAIN
*/
#ifndef __KSELFTEST_HARNESS_H
#define __KSELFTEST_HARNESS_H
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <asm/types.h>
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <setjmp.h>
#include "kselftest.h"
#define TEST_TIMEOUT_DEFAULT 30
/* Utilities exposed to the test definitions */
#ifndef TH_LOG_STREAM
# define TH_LOG_STREAM stderr
#endif
#ifndef TH_LOG_ENABLED
# define TH_LOG_ENABLED 1
#endif
/**
* TH_LOG()
*
* @fmt: format string
* @...: optional arguments
*
* .. code-block:: c
*
* TH_LOG(format, ...)
*
* Optional debug logging function available for use in tests.
* Logging may be enabled or disabled by defining TH_LOG_ENABLED.
* E.g., #define TH_LOG_ENABLED 1
*
* If no definition is provided, logging is enabled by default.
*/
#define TH_LOG(fmt, ...) do { \
if (TH_LOG_ENABLED) \
__TH_LOG(fmt, ##__VA_ARGS__); \
} while (0)
/* Unconditional logger for internal use. */
#define __TH_LOG(fmt, ...) \
fprintf(TH_LOG_STREAM, "# %s:%d:%s:" fmt "\n", \
__FILE__, __LINE__, _metadata->name, ##__VA_ARGS__)
/**
* SKIP()
*
* @statement: statement to run after reporting SKIP
* @fmt: format string
* @...: optional arguments
*
* .. code-block:: c
*
* SKIP(statement, fmt, ...);
*
* This forces a "pass" after reporting why something is being skipped
* and runs "statement", which is usually "return" or "goto skip".
*/
#define SKIP(statement, fmt, ...) do { \
snprintf(_metadata->results->reason, \
sizeof(_metadata->results->reason), fmt, ##__VA_ARGS__); \
if (TH_LOG_ENABLED) { \
fprintf(TH_LOG_STREAM, "# SKIP %s\n", \
_metadata->results->reason); \
} \
_metadata->exit_code = KSFT_SKIP; \
_metadata->trigger = 0; \
statement; \
} while (0)
/**
* TEST() - Defines the test function and creates the registration
* stub
*
* @test_name: test name
*
* .. code-block:: c
*
* TEST(name) { implementation }
*
* Defines a test by name.
* Names must be unique and tests must not be run in parallel. The
* implementation containing block is a function and scoping should be treated
* as such. Returning early may be performed with a bare "return;" statement.
*
* EXPECT_* and ASSERT_* are valid in a TEST() { } context.
*/
#define TEST(test_name) __TEST_IMPL(test_name, -1)
/**
* TEST_SIGNAL()
*
* @test_name: test name
* @signal: signal number
*
* .. code-block:: c
*
* TEST_SIGNAL(name, signal) { implementation }
*
* Defines a test by name and the expected term signal.
* Names must be unique and tests must not be run in parallel. The
* implementation containing block is a function and scoping should be treated
* as such. Returning early may be performed with a bare "return;" statement.
*
* EXPECT_* and ASSERT_* are valid in a TEST() { } context.
*/
#define TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal)
#define __TEST_IMPL(test_name, _signal) \
static void test_name(struct __test_metadata *_metadata); \
static inline void wrapper_##test_name( \
struct __test_metadata *_metadata, \
struct __fixture_variant_metadata *variant) \
{ \
_metadata->setup_completed = true; \
if (setjmp(_metadata->env) == 0) \
test_name(_metadata); \
__test_check_assert(_metadata); \
} \
static struct __test_metadata _##test_name##_object = \
{ .name = #test_name, \
.fn = &wrapper_##test_name, \
.fixture = &_fixture_global, \
.termsig = _signal, \
.timeout = TEST_TIMEOUT_DEFAULT, }; \
static void __attribute__((constructor)) _register_##test_name(void) \
{ \
__register_test(&_##test_name##_object); \
} \
static void test_name( \
struct __test_metadata __attribute__((unused)) *_metadata)
/**
* FIXTURE_DATA() - Wraps the struct name so we have one less
* argument to pass around
*