[LTP] [PATCH v2 15/31] testcases: sysfs: sys_leds01
Cyril Hrubis
chrubis@suse.cz
Wed Aug 19 18:16:18 CEST 2026
Add test for /sys/class/leds/*/* files.
Signed-off-by: Cyril Hrubis <chrubis@suse.cz>
---
runtest/sysfs | 1 +
testcases/kernel/sysfs/class/leds/.gitignore | 1 +
testcases/kernel/sysfs/class/leds/Makefile | 7 ++
.../kernel/sysfs/class/leds/sys_leds01.c | 70 +++++++++++++++++++
4 files changed, 79 insertions(+)
create mode 100644 testcases/kernel/sysfs/class/leds/.gitignore
create mode 100644 testcases/kernel/sysfs/class/leds/Makefile
create mode 100644 testcases/kernel/sysfs/class/leds/sys_leds01.c
diff --git a/runtest/sysfs b/runtest/sysfs
index 23c7adc7a..f515c4c1e 100644
--- a/runtest/sysfs
+++ b/runtest/sysfs
@@ -11,3 +11,4 @@ sys_clockevents01 sys_clockevents01
sys_ata01 sys_ata01
sys_bdi01 sys_bdi01
sys_hwmon01 sys_hwmon01
+sys_leds01 sys_leds01
diff --git a/testcases/kernel/sysfs/class/leds/.gitignore b/testcases/kernel/sysfs/class/leds/.gitignore
new file mode 100644
index 000000000..96e026b0d
--- /dev/null
+++ b/testcases/kernel/sysfs/class/leds/.gitignore
@@ -0,0 +1 @@
+/sys_leds01
diff --git a/testcases/kernel/sysfs/class/leds/Makefile b/testcases/kernel/sysfs/class/leds/Makefile
new file mode 100644
index 000000000..034038061
--- /dev/null
+++ b/testcases/kernel/sysfs/class/leds/Makefile
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: GPL-2.0-or-later
+# Copyright (c) 2026 Cyril Hrubis <chrubis@suse.cz>
+
+top_srcdir ?= ../../../../..
+
+include $(top_srcdir)/include/mk/testcases.mk
+include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/sysfs/class/leds/sys_leds01.c b/testcases/kernel/sysfs/class/leds/sys_leds01.c
new file mode 100644
index 000000000..710a6ed1e
--- /dev/null
+++ b/testcases/kernel/sysfs/class/leds/sys_leds01.c
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2026 Cyril Hrubis <chrubis@suse.cz>
+ */
+
+/*\
+ * Sanity checks for the LED class devices exported under
+ * /sys/class/leds/<led>/.
+ *
+ * For every LED the test verifies that:
+ *
+ * - max_brightness is greater than zero
+ * - brightness is in the range [0, max_brightness]
+ * - trigger is a bracketed-choice file listing the available triggers with
+ * exactly one of them selected via [] (the set of triggers is not checked
+ * as it is highly kernel- and hardware-dependent)
+ *
+ * The test skips with TCONF when no LED device is present.
+ */
+
+#include <limits.h>
+#include <dirent.h>
+#include "tst_test.h"
+#include "tst_sysfs_assert.h"
+
+#define LEDS "/sys/class/leds"
+
+static void check_led(const char *name)
+{
+ char trigger[64];
+
+ tst_res(TINFO, "Checking led '%s'", name);
+
+ TST_SYSFS_ASSERT_RANGELL(1, LONG_MAX, LEDS "/%s/max_brightness", name);
+
+ TST_SYSFS_ASSERT_RANGELF(0, "brightness", "max_brightness",
+ LEDS "/%s/", name);
+
+ TST_SYSFS_ASSERT_CHOICE(NULL, trigger, sizeof(trigger),
+ LEDS "/%s/trigger", name);
+}
+
+static void do_test(void)
+{
+ DIR *d;
+ struct dirent *ent;
+ int found = 0;
+
+ if (!tst_sysfs_exists(LEDS))
+ tst_brk(TCONF, LEDS ": not present");
+
+ d = SAFE_OPENDIR(LEDS);
+
+ while ((ent = SAFE_READDIR(d))) {
+ if (ent->d_name[0] == '.')
+ continue;
+
+ found = 1;
+ check_led(ent->d_name);
+ }
+
+ SAFE_CLOSEDIR(d);
+
+ if (!found)
+ tst_res(TCONF, "No LED device found");
+}
+
+static struct tst_test test = {
+ .test_all = do_test,
+};
--
2.54.0
More information about the ltp
mailing list