Skip to content

Commit e80068b

Browse files
keesshuahkh
authored andcommitted
selftests/harness: Switch to TAP output
Using the kselftest_harness.h would result in non-TAP test reporting, which didn't make much sense given that all the requirements for using the low-level API were met. Switch to using ksft_*() helpers while retaining as much of a human-readability as possible. Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 245dd60 commit e80068b

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

tools/testing/selftests/kselftest.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
/*
3-
* kselftest.h: kselftest framework return codes to include from
4-
* selftests.
3+
* kselftest.h: low-level kselftest framework to include from
4+
* selftest programs. When possible, please use
5+
* kselftest_harness.h instead.
56
*
67
* Copyright (c) 2014 Shuah Khan <[email protected]>
78
* Copyright (c) 2014 Samsung Electronics Co., Ltd.

tools/testing/selftests/kselftest_harness.h

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@
5050
#ifndef __KSELFTEST_HARNESS_H
5151
#define __KSELFTEST_HARNESS_H
5252

53+
#ifndef _GNU_SOURCE
5354
#define _GNU_SOURCE
55+
#endif
5456
#include <asm/types.h>
5557
#include <errno.h>
5658
#include <stdbool.h>
@@ -62,6 +64,8 @@
6264
#include <sys/wait.h>
6365
#include <unistd.h>
6466

67+
#include "kselftest.h"
68+
6569
#define TEST_TIMEOUT_DEFAULT 30
6670

6771
/* Utilities exposed to the test definitions */
@@ -104,7 +108,7 @@
104108

105109
/* Unconditional logger for internal use. */
106110
#define __TH_LOG(fmt, ...) \
107-
fprintf(TH_LOG_STREAM, "%s:%d:%s:" fmt "\n", \
111+
fprintf(TH_LOG_STREAM, "# %s:%d:%s:" fmt "\n", \
108112
__FILE__, __LINE__, _metadata->name, ##__VA_ARGS__)
109113

110114
/**
@@ -119,7 +123,7 @@
119123
*/
120124
#define XFAIL(statement, fmt, ...) do { \
121125
if (TH_LOG_ENABLED) { \
122-
fprintf(TH_LOG_STREAM, "[ XFAIL! ] " fmt "\n", \
126+
fprintf(TH_LOG_STREAM, "# XFAIL " fmt "\n", \
123127
##__VA_ARGS__); \
124128
} \
125129
/* TODO: find a way to pass xfail to test runner process. */ \
@@ -813,12 +817,12 @@ static void __timeout_handler(int sig, siginfo_t *info, void *ucontext)
813817
/* Sanity check handler execution environment. */
814818
if (!t) {
815819
fprintf(TH_LOG_STREAM,
816-
"no active test in SIGALRM handler!?\n");
820+
"# no active test in SIGALRM handler!?\n");
817821
abort();
818822
}
819823
if (sig != SIGALRM || sig != info->si_signo) {
820824
fprintf(TH_LOG_STREAM,
821-
"%s: SIGALRM handler caught signal %d!?\n",
825+
"# %s: SIGALRM handler caught signal %d!?\n",
822826
t->name, sig != SIGALRM ? sig : info->si_signo);
823827
abort();
824828
}
@@ -839,7 +843,7 @@ void __wait_for_test(struct __test_metadata *t)
839843
if (sigaction(SIGALRM, &action, &saved_action)) {
840844
t->passed = 0;
841845
fprintf(TH_LOG_STREAM,
842-
"%s: unable to install SIGALRM handler\n",
846+
"# %s: unable to install SIGALRM handler\n",
843847
t->name);
844848
return;
845849
}
@@ -851,7 +855,7 @@ void __wait_for_test(struct __test_metadata *t)
851855
if (sigaction(SIGALRM, &saved_action, NULL)) {
852856
t->passed = 0;
853857
fprintf(TH_LOG_STREAM,
854-
"%s: unable to uninstall SIGALRM handler\n",
858+
"# %s: unable to uninstall SIGALRM handler\n",
855859
t->name);
856860
return;
857861
}
@@ -860,39 +864,37 @@ void __wait_for_test(struct __test_metadata *t)
860864
if (t->timed_out) {
861865
t->passed = 0;
862866
fprintf(TH_LOG_STREAM,
863-
"%s: Test terminated by timeout\n", t->name);
867+
"# %s: Test terminated by timeout\n", t->name);
864868
} else if (WIFEXITED(status)) {
865869
t->passed = t->termsig == -1 ? !WEXITSTATUS(status) : 0;
866870
if (t->termsig != -1) {
867871
fprintf(TH_LOG_STREAM,
868-
"%s: Test exited normally "
869-
"instead of by signal (code: %d)\n",
872+
"# %s: Test exited normally instead of by signal (code: %d)\n",
870873
t->name,
871874
WEXITSTATUS(status));
872875
} else if (!t->passed) {
873876
fprintf(TH_LOG_STREAM,
874-
"%s: Test failed at step #%d\n",
877+
"# %s: Test failed at step #%d\n",
875878
t->name,
876879
WEXITSTATUS(status));
877880
}
878881
} else if (WIFSIGNALED(status)) {
879882
t->passed = 0;
880883
if (WTERMSIG(status) == SIGABRT) {
881884
fprintf(TH_LOG_STREAM,
882-
"%s: Test terminated by assertion\n",
885+
"# %s: Test terminated by assertion\n",
883886
t->name);
884887
} else if (WTERMSIG(status) == t->termsig) {
885888
t->passed = 1;
886889
} else {
887890
fprintf(TH_LOG_STREAM,
888-
"%s: Test terminated unexpectedly "
889-
"by signal %d\n",
891+
"# %s: Test terminated unexpectedly by signal %d\n",
890892
t->name,
891893
WTERMSIG(status));
892894
}
893895
} else {
894896
fprintf(TH_LOG_STREAM,
895-
"%s: Test ended in some other way [%u]\n",
897+
"# %s: Test ended in some other way [%u]\n",
896898
t->name,
897899
status);
898900
}
@@ -908,11 +910,11 @@ void __run_test(struct __fixture_metadata *f,
908910
t->step = 0;
909911
t->no_print = 0;
910912

911-
printf("[ RUN ] %s%s%s.%s\n",
913+
ksft_print_msg(" RUN %s%s%s.%s ...\n",
912914
f->name, variant->name[0] ? "." : "", variant->name, t->name);
913915
t->pid = fork();
914916
if (t->pid < 0) {
915-
printf("ERROR SPAWNING TEST CHILD\n");
917+
ksft_print_msg("ERROR SPAWNING TEST CHILD\n");
916918
t->passed = 0;
917919
} else if (t->pid == 0) {
918920
t->fn(t, variant);
@@ -921,7 +923,9 @@ void __run_test(struct __fixture_metadata *f,
921923
} else {
922924
__wait_for_test(t);
923925
}
924-
printf("[ %4s ] %s%s%s.%s\n", (t->passed ? "OK" : "FAIL"),
926+
ksft_print_msg(" %4s %s%s%s.%s\n", t->passed ? "OK" : "FAIL",
927+
f->name, variant->name[0] ? "." : "", variant->name, t->name);
928+
ksft_test_result(t->passed, "%s%s%s.%s\n",
925929
f->name, variant->name[0] ? "." : "", variant->name, t->name);
926930
}
927931

@@ -945,8 +949,9 @@ static int test_harness_run(int __attribute__((unused)) argc,
945949
}
946950
}
947951

948-
/* TODO(wad) add optional arguments similar to gtest. */
949-
printf("[==========] Running %u tests from %u test cases.\n",
952+
ksft_print_header();
953+
ksft_set_plan(test_count);
954+
ksft_print_msg("Starting %u tests from %u test cases.\n",
950955
test_count, case_count);
951956
for (f = __fixture_list; f; f = f->next) {
952957
for (v = f->variant ?: &no_variant; v; v = v->next) {
@@ -960,9 +965,12 @@ static int test_harness_run(int __attribute__((unused)) argc,
960965
}
961966
}
962967
}
963-
printf("[==========] %u / %u tests passed.\n", pass_count, count);
964-
printf("[ %s ]\n", (ret ? "FAILED" : "PASSED"));
965-
return ret;
968+
ksft_print_msg("%s: %u / %u tests passed.\n", ret ? "FAILED" : "PASSED",
969+
pass_count, count);
970+
ksft_exit(ret == 0);
971+
972+
/* unreachable */
973+
return KSFT_FAIL;
966974
}
967975

968976
static void __attribute__((constructor)) __constructor_order_first(void)

0 commit comments

Comments
 (0)