Skip to content

Commit 0ef67a8

Browse files
keesshuahkh
authored andcommitted
selftests/harness: Report skip reason
Use a share memory segment to pass string information between forked test and the test runner for the skip reason. Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent d088c92 commit 0ef67a8

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

tools/testing/selftests/kselftest_harness.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include <stdio.h>
6161
#include <stdlib.h>
6262
#include <string.h>
63+
#include <sys/mman.h>
6364
#include <sys/types.h>
6465
#include <sys/wait.h>
6566
#include <unistd.h>
@@ -122,9 +123,11 @@
122123
* and runs "statement", which is usually "return" or "goto skip".
123124
*/
124125
#define SKIP(statement, fmt, ...) do { \
126+
snprintf(_metadata->results->reason, \
127+
sizeof(_metadata->results->reason), fmt, ##__VA_ARGS__); \
125128
if (TH_LOG_ENABLED) { \
126-
fprintf(TH_LOG_STREAM, "# SKIP " fmt "\n", \
127-
##__VA_ARGS__); \
129+
fprintf(TH_LOG_STREAM, "# SKIP %s\n", \
130+
_metadata->results->reason); \
128131
} \
129132
_metadata->passed = 1; \
130133
_metadata->skip = 1; \
@@ -762,6 +765,10 @@
762765
} \
763766
}
764767

768+
struct __test_results {
769+
char reason[1024]; /* Reason for test result */
770+
};
771+
765772
struct __test_metadata;
766773
struct __fixture_variant_metadata;
767774

@@ -815,6 +822,7 @@ struct __test_metadata {
815822
bool timed_out; /* did this test timeout instead of exiting? */
816823
__u8 step;
817824
bool no_print; /* manual trigger when TH_LOG_STREAM is not available */
825+
struct __test_results *results;
818826
struct __test_metadata *prev, *next;
819827
};
820828

@@ -957,6 +965,7 @@ void __run_test(struct __fixture_metadata *f,
957965
t->trigger = 0;
958966
t->step = 0;
959967
t->no_print = 0;
968+
memset(t->results->reason, 0, sizeof(t->results->reason));
960969

961970
ksft_print_msg(" RUN %s%s%s.%s ...\n",
962971
f->name, variant->name[0] ? "." : "", variant->name, t->name);
@@ -986,8 +995,8 @@ void __run_test(struct __fixture_metadata *f,
986995
f->name, variant->name[0] ? "." : "", variant->name, t->name);
987996

988997
if (t->skip)
989-
ksft_test_result_skip("%s%s%s.%s\n",
990-
f->name, variant->name[0] ? "." : "", variant->name, t->name);
998+
ksft_test_result_skip("%s\n", t->results->reason[0] ?
999+
t->results->reason : "unknown");
9911000
else
9921001
ksft_test_result(t->passed, "%s%s%s.%s\n",
9931002
f->name, variant->name[0] ? "." : "", variant->name, t->name);
@@ -999,6 +1008,7 @@ static int test_harness_run(int __attribute__((unused)) argc,
9991008
struct __fixture_variant_metadata no_variant = { .name = "", };
10001009
struct __fixture_variant_metadata *v;
10011010
struct __fixture_metadata *f;
1011+
struct __test_results *results;
10021012
struct __test_metadata *t;
10031013
int ret = 0;
10041014
unsigned int case_count = 0, test_count = 0;
@@ -1013,6 +1023,9 @@ static int test_harness_run(int __attribute__((unused)) argc,
10131023
}
10141024
}
10151025

1026+
results = mmap(NULL, sizeof(*results), PROT_READ | PROT_WRITE,
1027+
MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1028+
10161029
ksft_print_header();
10171030
ksft_set_plan(test_count);
10181031
ksft_print_msg("Starting %u tests from %u test cases.\n",
@@ -1021,14 +1034,18 @@ static int test_harness_run(int __attribute__((unused)) argc,
10211034
for (v = f->variant ?: &no_variant; v; v = v->next) {
10221035
for (t = f->tests; t; t = t->next) {
10231036
count++;
1037+
t->results = results;
10241038
__run_test(f, v, t);
1039+
t->results = NULL;
10251040
if (t->passed)
10261041
pass_count++;
10271042
else
10281043
ret = 1;
10291044
}
10301045
}
10311046
}
1047+
munmap(results, sizeof(*results));
1048+
10321049
ksft_print_msg("%s: %u / %u tests passed.\n", ret ? "FAILED" : "PASSED",
10331050
pass_count, count);
10341051
ksft_exit(ret == 0);

0 commit comments

Comments
 (0)