1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * KUnit API providing hooks for non-test code to interact with tests. 4 * 5 * Copyright (C) 2020, Google LLC. 6 * Author: Uriel Guajardo <urielguajardo@google.com> 7 */ 8 9 #ifndef _KUNIT_TEST_BUG_H 10 #define _KUNIT_TEST_BUG_H 11 12 #include <linux/stddef.h> /* for NULL */ 13 #include <linux/types.h> /* for bool */ 14 15 #if IS_ENABLED(CONFIG_KUNIT) 16 17 #include <linux/jump_label.h> /* For static branch */ 18 #include <linux/sched.h> 19 20 /* Static key if KUnit is running any tests. */ 21 DECLARE_STATIC_KEY_FALSE(kunit_running); 22 23 /* Hooks table: a table of function pointers filled in when kunit loads */ 24 extern struct kunit_hooks_table { 25 __printf(3, 4) void (*fail_current_test)(const char*, int, const char*, ...); 26 void *(*get_static_stub_address)(struct kunit *test, void *real_fn_addr); 27 bool (*is_suppressed_warning)(bool count); 28 } kunit_hooks; 29 30 /** 31 * kunit_get_current_test() - Return a pointer to the currently running 32 * KUnit test. 33 * 34 * If a KUnit test is running in the current task, returns a pointer to its 35 * associated struct kunit. This pointer can then be passed to any KUnit 36 * function or assertion. If no test is running (or a test is running in a 37 * different task), returns NULL. 38 * 39 * This function is safe to call even when KUnit is disabled. If CONFIG_KUNIT 40 * is not enabled, it will compile down to nothing and will return quickly no 41 * test is running. 42 */ 43 static inline struct kunit *kunit_get_current_test(void) 44 { 45 if (!static_branch_unlikely(&kunit_running)) 46 return NULL; 47 48 return current->kunit_test; 49 } 50 51 52 /** 53 * kunit_fail_current_test() - If a KUnit test is running, fail it. 54 * 55 * If a KUnit test is running in the current task, mark that test as failed. 56 */ 57 #define kunit_fail_current_test(fmt, ...) do { \ 58 if (static_branch_unlikely(&kunit_running)) { \ 59 /* Guaranteed to be non-NULL when kunit_running true*/ \ 60 kunit_hooks.fail_current_test(__FILE__, __LINE__, \ 61 fmt, ##__VA_ARGS__); \ 62 } \ 63 } while (0) 64 65 /** 66 * kunit_is_suppressed_warning() - Check if warnings are being suppressed 67 * by the current KUnit test. 68 * @count: if true, increment the suppression counter on match. 69 * 70 * Returns true if the current task has active warning suppression. 71 * Uses the kunit_running static branch for zero overhead when no tests run. 72 * 73 * A single WARN*() may traverse multiple call sites in the warning path 74 * (e.g., __warn_printk() and __report_bug()). Pass @count = true at the 75 * primary suppression point to count each warning exactly once, and 76 * @count = false at secondary points to suppress output without 77 * inflating the count. 78 */ 79 static inline bool kunit_is_suppressed_warning(bool count) 80 { 81 if (!static_branch_unlikely(&kunit_running)) 82 return false; 83 84 return kunit_hooks.is_suppressed_warning && 85 kunit_hooks.is_suppressed_warning(count); 86 } 87 88 #else 89 90 static inline struct kunit *kunit_get_current_test(void) { return NULL; } 91 static inline bool kunit_is_suppressed_warning(bool count) { return false; } 92 93 #define kunit_fail_current_test(fmt, ...) do {} while (0) 94 95 #endif 96 97 #endif /* _KUNIT_TEST_BUG_H */ 98