projects/linux·blob master · refs

73 lines · 2.4 KiB

1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * Copyright (C) 2026-2029 Red Hat, Inc. Gabriele Monaco <[email protected]>
4 *
5 * Declaration of wrappers to allow mocking core functionality, like current,
6 * and other testing utilities.
7 * Necessary only when mocking may be needed. If the RV KUnit test is
8 * enabled, the wrappers incur an additional function call overhead.
9 */
10
11#ifndef _RV_KUNIT_H
12#define _RV_KUNIT_H
13
14#if IS_ENABLED(CONFIG_RV_MONITORS_KUNIT_TEST)
15
16#include <kunit/test.h>
17#include <kunit/test-bug.h>
18#include <linux/delay.h>
19
20int rv_set_testing(struct kunit_suite *suite);
21void rv_clear_testing(struct kunit_suite *suite);
22
23#define RV_KUNIT_MAX_MOCK_TASKS 8
24
25struct rv_kunit_ctx {
26 int reactions, expected;
27 int mock_task_count;
28 struct task_struct *mock_tasks[RV_KUNIT_MAX_MOCK_TASKS];
29};
30
31#define RV_KUNIT_EXPECT_REACTION(test, ctx) \
32 do { \
33 KUNIT_EXPECT_EQ(test, ctx->reactions, ++ctx->expected); \
34 if (ctx->reactions != ctx->expected) \
35 ctx->expected = ctx->reactions; \
36 } while (0)
37
38#define RV_KUNIT_EXPECT_NO_REACTION(test, ctx) \
39 do { \
40 KUNIT_EXPECT_EQ(test, ctx->reactions, ctx->expected); \
41 if (ctx->reactions != ctx->expected) \
42 ctx->expected = ctx->reactions; \
43 } while (0)
44
45#define RV_KUNIT_EXPECT_REACTION_HERE(test, ctx) \
46 for (int __done = ({ RV_KUNIT_EXPECT_NO_REACTION(test, ctx); 0; }); \
47 !__done; \
48 __done = ({ RV_KUNIT_EXPECT_REACTION(test, ctx); 1; }))
49
50struct rv_kunit_mon {
51 struct rv_monitor *rv_this;
52 int (*monitor_init)(void);
53 void (*monitor_destroy)(void);
54 bool is_per_task;
55 int *task_slot;
56 void (*task_reset)(struct task_struct *task);
57};
58
59void prepare_test(struct kunit *test, const struct rv_kunit_mon *mon);
60void teardown_test(void *arg);
61struct task_struct *rv_kunit_alloc_mock_task(struct kunit *test);
62
63void rv_mock_current(struct task_struct *tsk);
64struct task_struct *rv_get_mock_current(void);
65
66#define rv_get_current() (unlikely(kunit_get_current_test()) ? rv_get_mock_current() : current)
67
68#else /* !CONFIG_RV_MONITORS_KUNIT_TEST */
69
70#define rv_get_current() current
71
72#endif /* CONFIG_RV_MONITORS_KUNIT_TEST */
73#endif /* _RV_KUNIT_H */