| From 0c730ea49928beabf00183f74a83532a829aea5d Mon Sep 17 00:00:00 2001 |
| From: Developer <developer@example.com> |
| Date: Fri, 27 Sep 2024 21:09:07 +0000 |
| Subject: [PATCH 1/2] Create an example KUnit test |
| |
| Following the "Writing Your First Test" guide from the KUnit |
| documentation, adds an example driver and KUnit test to drivers/misc. |
| |
| Link: https://docs.kernel.org/dev-tools/kunit/start.html#writing-your-first-test |
| Test: tools/testing/kunit/kunit.py run --kunitconfig drivers/misc |
| Change-Id: I2f9570b7815bb600e83b1871852454c29ac42e0a |
| Signed-off-by: Developer <developer@example.com> |
| --- |
| drivers/misc/.kunitconfig | 4 +++ |
| drivers/misc/Kconfig | 8 +++++ |
| drivers/misc/Makefile | 2 ++ |
| drivers/misc/example.c | 13 +++++++ |
| drivers/misc/example.h | 5 +++ |
| drivers/misc/example_test.c | 37 ++++++++++++++++++++ |
| tools/testing/kunit/configs/all_tests.config | 2 ++ |
| 7 files changed, 71 insertions(+) |
| create mode 100644 drivers/misc/.kunitconfig |
| create mode 100644 drivers/misc/example.c |
| create mode 100644 drivers/misc/example.h |
| create mode 100644 drivers/misc/example_test.c |
| |
| diff --git a/drivers/misc/.kunitconfig b/drivers/misc/.kunitconfig |
| new file mode 100644 |
| index 000000000000..beaba9037670 |
| --- /dev/null |
| +++ b/drivers/misc/.kunitconfig |
| @@ -0,0 +1,4 @@ |
| +CONFIG_KUNIT=y |
| +CONFIG_MISC_EXAMPLE=y |
| +CONFIG_MISC_EXAMPLE_TEST=y |
| + |
| diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig |
| index d118aeeb1049..4af45d56cb04 100644 |
| --- a/drivers/misc/Kconfig |
| +++ b/drivers/misc/Kconfig |
| @@ -5,6 +5,14 @@ |
| |
| menu "Misc devices" |
| |
| +config MISC_EXAMPLE |
| + bool "My example" |
| + |
| +config MISC_EXAMPLE_TEST |
| + tristate "Test for my example" if !KUNIT_ALL_TESTS |
| + depends on MISC_EXAMPLE && KUNIT |
| + default KUNIT_ALL_TESTS |
| + |
| config SENSORS_LIS3LV02D |
| tristate |
| depends on INPUT |
| diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile |
| index 35c790e23072..c0bdb54f3af8 100644 |
| --- a/drivers/misc/Makefile |
| +++ b/drivers/misc/Makefile |
| @@ -3,6 +3,8 @@ |
| # Makefile for misc devices that really don't fit anywhere else. |
| # |
| |
| +obj-$(CONFIG_MISC_EXAMPLE) += example.o |
| +obj-$(CONFIG_MISC_EXAMPLE_TEST) += example_test.o |
| obj-$(CONFIG_IBM_ASM) += ibmasm/ |
| obj-$(CONFIG_IBMVMC) += ibmvmc.o |
| obj-$(CONFIG_AD525X_DPOT) += ad525x_dpot.o |
| diff --git a/drivers/misc/example.c b/drivers/misc/example.c |
| new file mode 100644 |
| index 000000000000..75a1d8984f7c |
| --- /dev/null |
| +++ b/drivers/misc/example.c |
| @@ -0,0 +1,13 @@ |
| +// SPDX-License-Identifier: GPL-2.0 |
| +/* |
| + * Copyright 2025 Google LLC. |
| + */ |
| +#include <linux/module.h> |
| + |
| +#include "example.h" |
| + |
| +int misc_example_add(int left, int right) |
| +{ |
| + return left + right; |
| +} |
| +EXPORT_SYMBOL_GPL(misc_example_add); |
| diff --git a/drivers/misc/example.h b/drivers/misc/example.h |
| new file mode 100644 |
| index 000000000000..698a31ac54cd |
| --- /dev/null |
| +++ b/drivers/misc/example.h |
| @@ -0,0 +1,5 @@ |
| +// SPDX-License-Identifier: GPL-2.0 |
| +/* |
| + * Copyright 2025 Google LLC. |
| + */ |
| +int misc_example_add(int left, int right); |
| diff --git a/drivers/misc/example_test.c b/drivers/misc/example_test.c |
| new file mode 100644 |
| index 000000000000..77ffcb863ccc |
| --- /dev/null |
| +++ b/drivers/misc/example_test.c |
| @@ -0,0 +1,37 @@ |
| +// SPDX-License-Identifier: GPL-2.0 |
| +/* |
| + * Copyright 2025 Google LLC. |
| + */ |
| +#include <kunit/test.h> |
| +#include "example.h" |
| + |
| +/* Define the test cases. */ |
| + |
| +static void misc_example_add_test_basic(struct kunit *test) |
| +{ |
| + KUNIT_EXPECT_EQ(test, 1, misc_example_add(1, 0)); |
| + KUNIT_EXPECT_EQ(test, 2, misc_example_add(1, 1)); |
| + KUNIT_EXPECT_EQ(test, 0, misc_example_add(-1, 1)); |
| + KUNIT_EXPECT_EQ(test, INT_MAX, misc_example_add(0, INT_MAX)); |
| + KUNIT_EXPECT_EQ(test, -1, misc_example_add(INT_MAX, INT_MIN)); |
| +} |
| + |
| +static void misc_example_test_failure(struct kunit *test) |
| +{ |
| + KUNIT_FAIL(test, "This test never passes."); |
| +} |
| + |
| +static struct kunit_case misc_example_test_cases[] = { |
| + KUNIT_CASE(misc_example_add_test_basic), |
| + KUNIT_CASE(misc_example_test_failure), |
| + {} |
| +}; |
| + |
| +static struct kunit_suite misc_example_test_suite = { |
| + .name = "misc-example", |
| + .test_cases = misc_example_test_cases, |
| +}; |
| +kunit_test_suite(misc_example_test_suite); |
| + |
| +MODULE_DESCRIPTION("KUnit test for misc_example_add"); |
| +MODULE_LICENSE("GPL"); |
| diff --git a/tools/testing/kunit/configs/all_tests.config b/tools/testing/kunit/configs/all_tests.config |
| index 422e186cf3cf..08b31354ce97 100644 |
| --- a/tools/testing/kunit/configs/all_tests.config |
| +++ b/tools/testing/kunit/configs/all_tests.config |
| @@ -29,6 +29,8 @@ CONFIG_MCTP_FLOWS=y |
| CONFIG_INET=y |
| CONFIG_MPTCP=y |
| |
| +CONFIG_MISC_EXAMPLE=y |
| + |
| CONFIG_NETDEVICES=y |
| CONFIG_WLAN=y |
| CONFIG_CFG80211=y |