Skip to content

Commit eb58933

Browse files
committed
leds: led-test: Fill out the registration test to cover more test cases
Upon successful LED class device registration, it is expected that certain attributes are filled out in pre-defined ways. For instance, if provided, the .brightness_get() call-back should be called to populate the class device 'brightness' attribute, 'max_brightness' should be initialised as LED_FULL (at least until we can rid these pesky enums) and the sysfs group should be created with the class device name supplied by the caller. If subsequent registrations take place that would result in name conflicts, various outcomes are expected depending on which flags are set. If LED_REJECT_NAME_CONFLICT is disabled, registration should succeed resulting in an iteration on the provided name. Conversely, if it's enabled, then registration is expected to fail outright. We test for all of these scenarios here. Link: https://lore.kernel.org/r/20250501081918.3621432-2-lee@kernel.org Signed-off-by: Lee Jones <lee@kernel.org>
1 parent 1d7f254 commit eb58933

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

drivers/leds/led-test.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,51 @@
1010
#include <linux/device.h>
1111
#include <linux/leds.h>
1212

13+
#define LED_TEST_POST_REG_BRIGHTNESS 10
14+
1315
struct led_test_ddata {
1416
struct led_classdev cdev;
1517
struct device *dev;
1618
};
1719

20+
static enum led_brightness led_test_brightness_get(struct led_classdev *cdev)
21+
{
22+
return LED_TEST_POST_REG_BRIGHTNESS;
23+
}
24+
1825
static void led_test_class_register(struct kunit *test)
1926
{
2027
struct led_test_ddata *ddata = test->priv;
21-
struct led_classdev *cdev = &ddata->cdev;
28+
struct led_classdev *cdev_clash, *cdev = &ddata->cdev;
2229
struct device *dev = ddata->dev;
2330
int ret;
2431

32+
/* Register a LED class device */
2533
cdev->name = "led-test";
34+
cdev->brightness_get = led_test_brightness_get;
35+
cdev->brightness = 0;
2636

2737
ret = devm_led_classdev_register(dev, cdev);
2838
KUNIT_ASSERT_EQ(test, ret, 0);
39+
40+
KUNIT_EXPECT_EQ(test, cdev->max_brightness, LED_FULL);
41+
KUNIT_EXPECT_EQ(test, cdev->brightness, LED_TEST_POST_REG_BRIGHTNESS);
42+
KUNIT_EXPECT_STREQ(test, cdev->dev->kobj.name, "led-test");
43+
44+
/* Register again with the same name - expect it to pass with the LED renamed */
45+
cdev_clash = devm_kmemdup(dev, cdev, sizeof(*cdev), GFP_KERNEL);
46+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, cdev_clash);
47+
48+
ret = devm_led_classdev_register(dev, cdev_clash);
49+
KUNIT_ASSERT_EQ(test, ret, 0);
50+
51+
KUNIT_EXPECT_STREQ(test, cdev_clash->dev->kobj.name, "led-test_1");
52+
KUNIT_EXPECT_STREQ(test, cdev_clash->name, "led-test");
53+
54+
/* Enable name conflict rejection and register with the same name again - expect failure */
55+
cdev_clash->flags |= LED_REJECT_NAME_CONFLICT;
56+
ret = devm_led_classdev_register(dev, cdev_clash);
57+
KUNIT_EXPECT_EQ(test, ret, -EEXIST);
2958
}
3059

3160
static struct kunit_case led_test_cases[] = {

0 commit comments

Comments
 (0)