162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * KUnit API providing hooks for non-test code to interact with tests. 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2020, Google LLC. 662306a36Sopenharmony_ci * Author: Uriel Guajardo <urielguajardo@google.com> 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#ifndef _KUNIT_TEST_BUG_H 1062306a36Sopenharmony_ci#define _KUNIT_TEST_BUG_H 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci#include <linux/stddef.h> /* for NULL */ 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_KUNIT) 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ci#include <linux/jump_label.h> /* For static branch */ 1762306a36Sopenharmony_ci#include <linux/sched.h> 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci/* Static key if KUnit is running any tests. */ 2062306a36Sopenharmony_ciDECLARE_STATIC_KEY_FALSE(kunit_running); 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci/* Hooks table: a table of function pointers filled in when kunit loads */ 2362306a36Sopenharmony_ciextern struct kunit_hooks_table { 2462306a36Sopenharmony_ci __printf(3, 4) void (*fail_current_test)(const char*, int, const char*, ...); 2562306a36Sopenharmony_ci void *(*get_static_stub_address)(struct kunit *test, void *real_fn_addr); 2662306a36Sopenharmony_ci} kunit_hooks; 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci/** 2962306a36Sopenharmony_ci * kunit_get_current_test() - Return a pointer to the currently running 3062306a36Sopenharmony_ci * KUnit test. 3162306a36Sopenharmony_ci * 3262306a36Sopenharmony_ci * If a KUnit test is running in the current task, returns a pointer to its 3362306a36Sopenharmony_ci * associated struct kunit. This pointer can then be passed to any KUnit 3462306a36Sopenharmony_ci * function or assertion. If no test is running (or a test is running in a 3562306a36Sopenharmony_ci * different task), returns NULL. 3662306a36Sopenharmony_ci * 3762306a36Sopenharmony_ci * This function is safe to call even when KUnit is disabled. If CONFIG_KUNIT 3862306a36Sopenharmony_ci * is not enabled, it will compile down to nothing and will return quickly no 3962306a36Sopenharmony_ci * test is running. 4062306a36Sopenharmony_ci */ 4162306a36Sopenharmony_cistatic inline struct kunit *kunit_get_current_test(void) 4262306a36Sopenharmony_ci{ 4362306a36Sopenharmony_ci if (!static_branch_unlikely(&kunit_running)) 4462306a36Sopenharmony_ci return NULL; 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci return current->kunit_test; 4762306a36Sopenharmony_ci} 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci/** 5162306a36Sopenharmony_ci * kunit_fail_current_test() - If a KUnit test is running, fail it. 5262306a36Sopenharmony_ci * 5362306a36Sopenharmony_ci * If a KUnit test is running in the current task, mark that test as failed. 5462306a36Sopenharmony_ci */ 5562306a36Sopenharmony_ci#define kunit_fail_current_test(fmt, ...) do { \ 5662306a36Sopenharmony_ci if (static_branch_unlikely(&kunit_running)) { \ 5762306a36Sopenharmony_ci /* Guaranteed to be non-NULL when kunit_running true*/ \ 5862306a36Sopenharmony_ci kunit_hooks.fail_current_test(__FILE__, __LINE__, \ 5962306a36Sopenharmony_ci fmt, ##__VA_ARGS__); \ 6062306a36Sopenharmony_ci } \ 6162306a36Sopenharmony_ci } while (0) 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci#else 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_cistatic inline struct kunit *kunit_get_current_test(void) { return NULL; } 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci#define kunit_fail_current_test(fmt, ...) do {} while (0) 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci#endif 7062306a36Sopenharmony_ci 7162306a36Sopenharmony_ci#endif /* _KUNIT_TEST_BUG_H */ 72