Skip to content

Commit 86a5f32

Browse files
computersforpeacegregkh
authored andcommitted
drivers: base: test: Add ...find_device_by...(... NULL) tests
We recently updated these device_match*() (and therefore, various *find_device_by*()) functions to return a consistent 'false' value when trying to match a NULL handle. Add tests for this. This provides regression-testing coverage for the sorts of bugs that underly commit 5c8418c ("PCI/pwrctrl: Unregister platform device only if one actually exists"). Reviewed-by: Maxime Ripard <mripard@kernel.org> Signed-off-by: Brian Norris <briannorris@chromium.org> Acked-by: Shuah Khan <skhan@linuxfoundation.org> Reviewed-by: David Gow <davidgow@google.com> Link: https://lore.kernel.org/r/20241216201148.535115-4-briannorris@chromium.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 55b7aee commit 86a5f32

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

drivers/base/test/platform-device-test.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3+
#include <kunit/platform_device.h>
34
#include <kunit/resource.h>
45

56
#include <linux/device.h>
7+
#include <linux/device/bus.h>
8+
#include <linux/of_platform.h>
69
#include <linux/platform_device.h>
710

811
#define DEVICE_NAME "test"
@@ -217,7 +220,43 @@ static struct kunit_suite platform_device_devm_test_suite = {
217220
.test_cases = platform_device_devm_tests,
218221
};
219222

220-
kunit_test_suite(platform_device_devm_test_suite);
223+
static void platform_device_find_by_null_test(struct kunit *test)
224+
{
225+
struct platform_device *pdev;
226+
int ret;
227+
228+
pdev = kunit_platform_device_alloc(test, DEVICE_NAME, PLATFORM_DEVID_NONE);
229+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pdev);
230+
231+
ret = kunit_platform_device_add(test, pdev);
232+
KUNIT_ASSERT_EQ(test, ret, 0);
233+
234+
KUNIT_EXPECT_PTR_EQ(test, of_find_device_by_node(NULL), NULL);
235+
236+
KUNIT_EXPECT_PTR_EQ(test, bus_find_device_by_of_node(&platform_bus_type, NULL), NULL);
237+
KUNIT_EXPECT_PTR_EQ(test, bus_find_device_by_fwnode(&platform_bus_type, NULL), NULL);
238+
KUNIT_EXPECT_PTR_EQ(test, bus_find_device_by_acpi_dev(&platform_bus_type, NULL), NULL);
239+
240+
KUNIT_EXPECT_FALSE(test, device_match_of_node(&pdev->dev, NULL));
241+
KUNIT_EXPECT_FALSE(test, device_match_fwnode(&pdev->dev, NULL));
242+
KUNIT_EXPECT_FALSE(test, device_match_acpi_dev(&pdev->dev, NULL));
243+
KUNIT_EXPECT_FALSE(test, device_match_acpi_handle(&pdev->dev, NULL));
244+
}
245+
246+
static struct kunit_case platform_device_match_tests[] = {
247+
KUNIT_CASE(platform_device_find_by_null_test),
248+
{}
249+
};
250+
251+
static struct kunit_suite platform_device_match_test_suite = {
252+
.name = "platform-device-match",
253+
.test_cases = platform_device_match_tests,
254+
};
255+
256+
kunit_test_suites(
257+
&platform_device_devm_test_suite,
258+
&platform_device_match_test_suite,
259+
);
221260

222261
MODULE_DESCRIPTION("Test module for platform devices");
223262
MODULE_AUTHOR("Maxime Ripard <mripard@kernel.org>");

0 commit comments

Comments
 (0)