|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
| 2 | +/* |
| 3 | + * Copyright (C) 2025 Google LLC |
| 4 | + * |
| 5 | + * Author: Lee Jones <lee@kernel.org> |
| 6 | + */ |
| 7 | + |
| 8 | +#include <kunit/device.h> |
| 9 | +#include <kunit/test.h> |
| 10 | +#include <linux/device.h> |
| 11 | +#include <linux/leds.h> |
| 12 | + |
| 13 | +struct led_test_ddata { |
| 14 | + struct led_classdev cdev; |
| 15 | + struct device *dev; |
| 16 | +}; |
| 17 | + |
| 18 | +static void led_test_class_register(struct kunit *test) |
| 19 | +{ |
| 20 | + struct led_test_ddata *ddata = test->priv; |
| 21 | + struct led_classdev *cdev = &ddata->cdev; |
| 22 | + struct device *dev = ddata->dev; |
| 23 | + int ret; |
| 24 | + |
| 25 | + cdev->name = "led-test"; |
| 26 | + |
| 27 | + ret = devm_led_classdev_register(dev, cdev); |
| 28 | + KUNIT_ASSERT_EQ(test, ret, 0); |
| 29 | + if (ret) |
| 30 | + return; |
| 31 | +} |
| 32 | + |
| 33 | +static struct kunit_case led_test_cases[] = { |
| 34 | + KUNIT_CASE(led_test_class_register), |
| 35 | + { } |
| 36 | +}; |
| 37 | + |
| 38 | +static int led_test_init(struct kunit *test) |
| 39 | +{ |
| 40 | + struct led_test_ddata *ddata; |
| 41 | + struct device *dev; |
| 42 | + |
| 43 | + ddata = kunit_kzalloc(test, sizeof(*ddata), GFP_KERNEL); |
| 44 | + if (!ddata) |
| 45 | + return -ENOMEM; |
| 46 | + |
| 47 | + test->priv = ddata; |
| 48 | + |
| 49 | + dev = kunit_device_register(test, "led_test"); |
| 50 | + if (IS_ERR(dev)) |
| 51 | + return PTR_ERR(dev); |
| 52 | + |
| 53 | + ddata->dev = get_device(dev); |
| 54 | + |
| 55 | + return 0; |
| 56 | +} |
| 57 | + |
| 58 | +static void led_test_exit(struct kunit *test) |
| 59 | +{ |
| 60 | + struct led_test_ddata *ddata = test->priv; |
| 61 | + |
| 62 | + if (ddata && ddata->dev) |
| 63 | + put_device(ddata->dev); |
| 64 | +} |
| 65 | + |
| 66 | +static struct kunit_suite led_test_suite = { |
| 67 | + .name = "led", |
| 68 | + .init = led_test_init, |
| 69 | + .exit = led_test_exit, |
| 70 | + .test_cases = led_test_cases, |
| 71 | +}; |
| 72 | +kunit_test_suite(led_test_suite); |
| 73 | + |
| 74 | +MODULE_AUTHOR("Lee Jones <lee@kernel.org>"); |
| 75 | +MODULE_DESCRIPTION("KUnit tests for the LED framework"); |
| 76 | +MODULE_LICENSE("GPL"); |
0 commit comments