Skip to contentSkip to conversation

selftests: rtc: Support harness test filtering and arguments

PATCH Superseded Wake Liu 3 participants 8 messages Sep 10 – Nov 10

Subsystems (2)
lore
Share & tools
lore

Wake Liu

author
#0 lore raw

PATCH 1/1 selftests: rtc: Support harness test filtering and arguments

rtctest implements its own main() function with a rigid switch (argc) check that only accepts an optional single argument for the RTC device node path. This prevents kselftest_harness standard arguments from working properly:

1. Passing filter options like -t <name> or -T <name> triggers a usage error because argc is greater than 2.
2. Passing -l or -h causes rtctest to treat "-l" or "-h" as the device node path and fail with access() errors before ever reaching the harness argument checker.

Parse command line arguments with getopt() to pass all kselftest harness options (such as -t, -T, -v, -V, -f, -F, -r, -d, -h, and -l) through to test_harness_run(), while preserving the ability to specify an optional RTC device node path as a non-option argument. Also ensure optind is reset before invoking test_harness_run() so the harness can inspect the options, and bypass device accessibility checks when help or test listing is requested.

---
 tools/testing/selftests/rtc/rtctest.c | 44 +++++++++++++++++++++------
 1 file changed, 35 insertions(+), 9 deletions(-)
tools/testing/selftests/rtc/rtctest.c +35 -9
index 8047d9879039..57dda87ba813 100644
@@ -486,18 +486,44 @@ TEST_F_TIMEOUT(rtc, alarm_wkalm_set_minute, 65) {
 int main(int argc, char **argv)
 {
 	int ret = -1;
+	int opt, i;
+	int non_opt = 0;
+
+	while ((opt = getopt(argc, argv, "dhlF:f:V:v:t:T:r:")) != -1) {
+		switch (opt) {
+		case 'h':
+			fprintf(stderr,
+				"Usage: %s [-h|-l|-d] [-t|-T|-v|-V|-f|-F|-r name] [rtcdev]\n\n",
+				argv[0]);
+			optind = 1;
+			return test_harness_run(argc, argv);
+		case 'l':
+			optind = 1;
+			return test_harness_run(argc, argv);
+		case '?':
+			fprintf(stderr,
+				"Usage: %s [-h|-l|-d] [-t|-T|-v|-V|-f|-F|-r name] [rtcdev]\n",
+				argv[0]);
+			return KSFT_FAIL;
+		default:
+			break;
+		}
+	}
 
-	switch (argc) {
-	case 2:
-		rtc_file = argv[1];
-		/* FALLTHROUGH */
-	case 1:
-		break;
-	default:
-		fprintf(stderr, "usage: %s [rtcdev]\n", argv[0]);
-		return 1;
+	for (i = optind; i < argc; i++) {
+		if (non_opt == 0) {
+			rtc_file = argv[i];
+			non_opt++;
+		} else {
+			fprintf(stderr,
+				"Usage: %s [-h|-l|-d] [-t|-T|-v|-V|-f|-F|-r name] [rtcdev]\n",
+				argv[0]);
+			return KSFT_FAIL;
+		}
 	}
 
+	optind = 1;
+
 	/* Run the test if rtc_file is accessible */
 	if (access(rtc_file, R_OK) == 0)
 		ret = test_harness_run(argc, argv);
Signature · 1 line
2.55.0.979.g7e5102b832-goog
#1 lore raw
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 19 lines

rtctest implements its own main() function with a rigid switch (argc) check that only accepts an optional single argument for the RTC device node path. This prevents kselftest_harness standard arguments from working properly:

1. Passing filter options like -t <name> or -T <name> triggers a usage error because argc is greater than 2.
2. Passing -l or -h causes rtctest to treat "-l" or "-h" as the device node path and fail with access() errors before ever reaching the harness argument checker.

Parse command line arguments with getopt() to pass all kselftest harness options (such as -t, -T, -v, -V, -f, -F, -r, -d, -h, and -l) through to test_harness_run(), while preserving the ability to specify an optional RTC device node path as a non-option argument. Also ensure optind is reset before invoking test_harness_run() so the harness can inspect the options, and bypass device accessibility checks when help or test listing is requested.

Quoted 1 line

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Acked-by on v1 1/1
#2 lore raw
In reply to Wake Liu
Quoted 19 lines · Wake Liu

rtctest implements its own main() function with a rigid switch (argc) check that only accepts an optional single argument for the RTC device node path. This prevents kselftest_harness standard arguments from working properly:

1. Passing filter options like -t <name> or -T <name> triggers a usage error because argc is greater than 2.
2. Passing -l or -h causes rtctest to treat "-l" or "-h" as the device node path and fail with access() errors before ever reaching the harness argument checker.

Parse command line arguments with getopt() to pass all kselftest harness options (such as -t, -T, -v, -V, -f, -F, -r, -d, -h, and -l) through to test_harness_run(), while preserving the ability to specify an optional RTC device node path as a non-option argument. Also ensure optind is reset before invoking test_harness_run() so the harness can inspect the options, and bypass device accessibility checks when help or test listing is requested.

Quoted 1 line
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
author
#3 lore raw
In reply to Wake Liu

Hi Shuah,

Gentle ping on this patch. Alexandre has acked it from the RTC side [1], and it still applies cleanly on top of both kselftest/next and v7.3-rc6.

With this change, rtctest accepts the standard kselftest harness options, so a test runner can select a subset of the cases with e.g. "rtctest -t date_read -t date_read_loop". This would let Android VTS drop a local #ifdef that it currently carries in rtctest.c.

There is no rush; picking it up for the next cycle is perfectly fine. Alexandre, if you would rather take it through the rtc tree, that works for me as well.

FWIW, the pending date_read_loop fix [2] touches the same file. The two patches are independent and apply in either order.

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

Thanks,
Wake

author
#4 lore raw

PATCH v2 1/1 selftests: rtc: Support harness test filtering and arguments

In reply to Wake Liu

rtctest implements its own main() function with a rigid switch (argc) check that only accepts an optional single argument for the RTC device node path. This prevents kselftest_harness standard arguments from working properly:

1. Passing filter options like -t <name> or -T <name> triggers a usage error because argc is greater than 2.
2. Passing -l or -h causes rtctest to treat "-l" or "-h" as the device node path and fail with access() errors before ever reaching the harness argument checker.

Parse command line arguments with getopt() to pass all kselftest harness options (such as -t, -T, -v, -V, -f, -F, -r, -d, -h, and -l) through to test_harness_run(), while preserving the ability to specify an optional RTC device node path as a non-option argument. Also ensure optind is reset before invoking test_harness_run() so the harness can inspect the options, and bypass device accessibility checks when help or test listing is requested.

---
v2:
- Rebased onto v7.4-rc1, no code changes.
- Added Alexandre's Acked-by.

Alexandre, the other rtctest changes for v7.4 went through the RTC tree. Would you be willing to queue this one there as well? Shuah, please let me know if you would rather take it via kselftest.

v1: https://lore.kernel.org/all/[email protected]/

 tools/testing/selftests/rtc/rtctest.c | 44 +++++++++++++++++++++------
 1 file changed, 35 insertions(+), 9 deletions(-)
tools/testing/selftests/rtc/rtctest.c +35 -9
index 552191fe1ca0..eb26d957835d 100644
@@ -487,18 +487,44 @@ TEST_F_TIMEOUT(rtc, alarm_wkalm_set_minute, 65) {
 int main(int argc, char **argv)
 {
 	int ret = -1;
+	int opt, i;
+	int non_opt = 0;
+
+	while ((opt = getopt(argc, argv, "dhlF:f:V:v:t:T:r:")) != -1) {
+		switch (opt) {
+		case 'h':
+			fprintf(stderr,
+				"Usage: %s [-h|-l|-d] [-t|-T|-v|-V|-f|-F|-r name] [rtcdev]\n\n",
+				argv[0]);
+			optind = 1;
+			return test_harness_run(argc, argv);
+		case 'l':
+			optind = 1;
+			return test_harness_run(argc, argv);
+		case '?':
+			fprintf(stderr,
+				"Usage: %s [-h|-l|-d] [-t|-T|-v|-V|-f|-F|-r name] [rtcdev]\n",
+				argv[0]);
+			return KSFT_FAIL;
+		default:
+			break;
+		}
+	}
 
-	switch (argc) {
-	case 2:
-		rtc_file = argv[1];
-		/* FALLTHROUGH */
-	case 1:
-		break;
-	default:
-		fprintf(stderr, "usage: %s [rtcdev]\n", argv[0]);
-		return 1;
+	for (i = optind; i < argc; i++) {
+		if (non_opt == 0) {
+			rtc_file = argv[i];
+			non_opt++;
+		} else {
+			fprintf(stderr,
+				"Usage: %s [-h|-l|-d] [-t|-T|-v|-V|-f|-F|-r name] [rtcdev]\n",
+				argv[0]);
+			return KSFT_FAIL;
+		}
 	}
 
+	optind = 1;
+
 	/* Run the test if rtc_file is accessible */
 	if (access(rtc_file, R_OK) == 0)
 		ret = test_harness_run(argc, argv);

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.56.0.398.g882cbad275-goog
#5 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] selftests: rtc: incomplete getopt() reset on -h/-l in main() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] selftests: rtc: incomplete getopt() reset on -h/-l in main()
--

commit 9bed5ee7eee853de78b5dce8eb5280f5ed1c8688
Author: Wake Liu <[email protected]>

selftests: rtc: Support harness test filtering and arguments

This commit updates main() in rtctest.c to parse command-line options with getopt() so kselftest harness flags can be passed alongside the optional RTC device node path.

Quoted 12 lines
tools/testing/selftests/rtc/rtctest.c +4 -0
index 552191fe1ca09..eb26d957835d7 100644
@@ -487,18 +487,44 @@ TEST_F_TIMEOUT(rtc, alarm_wkalm_set_minute, 65) {
 int main(int argc, char **argv)
 {
 	int ret = -1;
+	int opt, i;
+	int non_opt = 0;
+
+	while ((opt = getopt(argc, argv, "dhlF:f:V:v:t:T:r:")) != -1) {

tools/testing/selftests/rtc/rtctest.c TEST_F_TIMEOUT(rtc, alarm_wkalm_set_minute, 65) { · line 503

Quoted 10 lines
tools/testing/selftests/rtc/rtctest.c +10 -0
+		switch (opt) {
+		case 'h':
+			fprintf(stderr,
+				"Usage: %s [-h|-l|-d] [-t|-T|-v|-V|-f|-F|-r name] [rtcdev]\n\n",
+				argv[0]);
+			optind = 1;
+			return test_harness_run(argc, argv);
+		case 'l':
+			optind = 1;
+			return test_harness_run(argc, argv);

[Severity: Medium]
What happens if -h or -l is passed as the first flag in a clustered short option argument (such as -hd or -ld)?

When getopt() parses the first character in argv[1], optind is still 1 and getopt()'s internal within-argument character cursor points to the second character ('d'). Returning early from inside the getopt() loop after setting optind = 1 does not reset that internal cursor when getopt() has not finished consuming the current argument.

When main() calls test_harness_run() -> test_harness_argv_check():

tools/testing/selftests/kselftest_harness.h:test_harness_argv_check() {
    ...
    while ((opt = getopt(argc, argv, "dhlF:f:V:v:t:T:r:")) != -1) {
        switch (opt) {
        ...
        case 'h':
            test_harness_usage(argv[0]);
            return KSFT_FAIL;
        case 'l':
            __test_harness_list_tests();
            return KSFT_FAIL;
        ...
        }
    }
    return KSFT_PASS;
}

getopt() in test_harness_argv_check() resumes at 'd' instead of re-reading 'h' or 'l', returns KSFT_PASS, and runs the full RTC test suite (while also having bypassed the access(rtc_file, R_OK) check in main()).

Would it be better to let the getopt() loop finish scanning all options (for example by recording a flag for -h/-l or resetting optind = 0 where supported) before calling test_harness_run()?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#6 lore raw

PATCH v3 1/1 selftests: rtc: Support harness test filtering and arguments

In reply to Wake Liu

rtctest implements its own main() function with a rigid switch (argc) check that only accepts an optional single argument for the RTC device node path. This prevents kselftest_harness standard arguments from working properly:

1. Passing filter options like -t <name> or -T <name> triggers a usage error because argc is greater than 2.
2. Passing -l or -h causes rtctest to treat "-l" or "-h" as the device node path and fail with access() errors before ever reaching the harness argument checker.

Parse command line arguments with getopt() to pass all kselftest harness options (such as -t, -T, -v, -V, -f, -F, -r, -d, -h, and -l) through to test_harness_run(), while preserving the ability to specify an optional RTC device node path as a non-option argument. Only reset optind once all options have been parsed, so the harness re-parses them from a clean getopt() state, and bypass device accessibility checks when help or test listing is requested.

---
v3:
- Don't call test_harness_run() from inside the getopt() loop for -h and -l. getopt() keeps its position inside grouped options, so with e.g. "-ld" the harness resumed at 'd', missed -l/-h and ran the whole suite without the device check. Record -h/-l instead and reset optind only after all options have been parsed. Reported by Sashiko AI review.
- Kept Alexandre's Acked-by as the change is limited to the -h/-l handling. Alexandre, please let me know if you'd rather re-check.

v2: https://lore.kernel.org/all/[email protected]/
- Rebased onto v7.4-rc1, no code changes.
- Added Alexandre's Acked-by.

v1: https://lore.kernel.org/all/[email protected]/

Alexandre, if you're still fine with it, could you queue this via the RTC tree along with the other rtctest changes? Shuah, please let me know if you would rather take it via kselftest.

 tools/testing/selftests/rtc/rtctest.c | 54 ++++++++++++++++++++++-----
 1 file changed, 45 insertions(+), 9 deletions(-)
tools/testing/selftests/rtc/rtctest.c +45 -9
index 552191fe1ca0..21c37764f9d3 100644
@@ -487,18 +487,54 @@ TEST_F_TIMEOUT(rtc, alarm_wkalm_set_minute, 65) {
 int main(int argc, char **argv)
 {
 	int ret = -1;
+	int opt, i;
+	int non_opt = 0;
+	bool skip_dev_check = false;
+
+	while ((opt = getopt(argc, argv, "dhlF:f:V:v:t:T:r:")) != -1) {
+		switch (opt) {
+		case 'h':
+			fprintf(stderr,
+				"Usage: %s [-h|-l|-d] [-t|-T|-v|-V|-f|-F|-r name] [rtcdev]\n\n",
+				argv[0]);
+			skip_dev_check = true;
+			break;
+		case 'l':
+			skip_dev_check = true;
+			break;
+		case '?':
+			fprintf(stderr,
+				"Usage: %s [-h|-l|-d] [-t|-T|-v|-V|-f|-F|-r name] [rtcdev]\n",
+				argv[0]);
+			return KSFT_FAIL;
+		default:
+			break;
+		}
+	}
 
-	switch (argc) {
-	case 2:
-		rtc_file = argv[1];
-		/* FALLTHROUGH */
-	case 1:
-		break;
-	default:
-		fprintf(stderr, "usage: %s [rtcdev]\n", argv[0]);
-		return 1;
+	for (i = optind; i < argc; i++) {
+		if (non_opt == 0) {
+			rtc_file = argv[i];
+			non_opt++;
+		} else {
+			fprintf(stderr,
+				"Usage: %s [-h|-l|-d] [-t|-T|-v|-V|-f|-F|-r name] [rtcdev]\n",
+				argv[0]);
+			return KSFT_FAIL;
+		}
 	}
 
+	/*
+	 * Rewind getopt() so test_harness_run() can parse the options again.
+	 * Only do this after the loop above has consumed every option, as
+	 * getopt() keeps its position within grouped options (e.g. "-ld").
+	 */
+	optind = 1;
+
+	/* -h and -l are handled by the harness and need no RTC device */
+	if (skip_dev_check)
+		return test_harness_run(argc, argv);
+
 	/* Run the test if rtc_file is accessible */
 	if (access(rtc_file, R_OK) == 0)
 		ret = test_harness_run(argc, argv);

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4

Signature · 1 line
2.56.0.542.g3ef89b7020-goog
#7 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 19 lines

rtctest implements its own main() function with a rigid switch (argc) check that only accepts an optional single argument for the RTC device node path. This prevents kselftest_harness standard arguments from working properly:

1. Passing filter options like -t <name> or -T <name> triggers a usage error because argc is greater than 2.
2. Passing -l or -h causes rtctest to treat "-l" or "-h" as the device node path and fail with access() errors before ever reaching the harness argument checker.

Parse command line arguments with getopt() to pass all kselftest harness options (such as -t, -T, -v, -V, -f, -F, -r, -d, -h, and -l) through to test_harness_run(), while preserving the ability to specify an optional RTC device node path as a non-option argument. Only reset optind once all options have been parsed, so the harness re-parses them from a clean getopt() state, and bypass device accessibility checks when help or test listing is requested.

Quoted 1 line

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1