50
50
#ifndef __KSELFTEST_HARNESS_H
51
51
#define __KSELFTEST_HARNESS_H
52
52
53
+ #ifndef _GNU_SOURCE
53
54
#define _GNU_SOURCE
55
+ #endif
54
56
#include <asm/types.h>
55
57
#include <errno.h>
56
58
#include <stdbool.h>
62
64
#include <sys/wait.h>
63
65
#include <unistd.h>
64
66
67
+ #include "kselftest.h"
68
+
65
69
#define TEST_TIMEOUT_DEFAULT 30
66
70
67
71
/* Utilities exposed to the test definitions */
104
108
105
109
/* Unconditional logger for internal use. */
106
110
#define __TH_LOG (fmt , ...) \
107
- fprintf(TH_LOG_STREAM, "%s:%d:%s:" fmt "\n", \
111
+ fprintf(TH_LOG_STREAM, "# %s:%d:%s:" fmt "\n", \
108
112
__FILE__, __LINE__, _metadata->name, ##__VA_ARGS__)
109
113
110
114
/**
119
123
*/
120
124
#define XFAIL (statement , fmt , ...) do { \
121
125
if (TH_LOG_ENABLED) { \
122
- fprintf(TH_LOG_STREAM, "[ XFAIL! ] " fmt "\n", \
126
+ fprintf(TH_LOG_STREAM, "# XFAIL " fmt "\n", \
123
127
##__VA_ARGS__); \
124
128
} \
125
129
/* 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)
813
817
/* Sanity check handler execution environment. */
814
818
if (!t ) {
815
819
fprintf (TH_LOG_STREAM ,
816
- "no active test in SIGALRM handler!?\n" );
820
+ "# no active test in SIGALRM handler!?\n" );
817
821
abort ();
818
822
}
819
823
if (sig != SIGALRM || sig != info -> si_signo ) {
820
824
fprintf (TH_LOG_STREAM ,
821
- "%s: SIGALRM handler caught signal %d!?\n" ,
825
+ "# %s: SIGALRM handler caught signal %d!?\n" ,
822
826
t -> name , sig != SIGALRM ? sig : info -> si_signo );
823
827
abort ();
824
828
}
@@ -839,7 +843,7 @@ void __wait_for_test(struct __test_metadata *t)
839
843
if (sigaction (SIGALRM , & action , & saved_action )) {
840
844
t -> passed = 0 ;
841
845
fprintf (TH_LOG_STREAM ,
842
- "%s: unable to install SIGALRM handler\n" ,
846
+ "# %s: unable to install SIGALRM handler\n" ,
843
847
t -> name );
844
848
return ;
845
849
}
@@ -851,7 +855,7 @@ void __wait_for_test(struct __test_metadata *t)
851
855
if (sigaction (SIGALRM , & saved_action , NULL )) {
852
856
t -> passed = 0 ;
853
857
fprintf (TH_LOG_STREAM ,
854
- "%s: unable to uninstall SIGALRM handler\n" ,
858
+ "# %s: unable to uninstall SIGALRM handler\n" ,
855
859
t -> name );
856
860
return ;
857
861
}
@@ -860,39 +864,37 @@ void __wait_for_test(struct __test_metadata *t)
860
864
if (t -> timed_out ) {
861
865
t -> passed = 0 ;
862
866
fprintf (TH_LOG_STREAM ,
863
- "%s: Test terminated by timeout\n" , t -> name );
867
+ "# %s: Test terminated by timeout\n" , t -> name );
864
868
} else if (WIFEXITED (status )) {
865
869
t -> passed = t -> termsig == -1 ? !WEXITSTATUS (status ) : 0 ;
866
870
if (t -> termsig != -1 ) {
867
871
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" ,
870
873
t -> name ,
871
874
WEXITSTATUS (status ));
872
875
} else if (!t -> passed ) {
873
876
fprintf (TH_LOG_STREAM ,
874
- "%s: Test failed at step #%d\n" ,
877
+ "# %s: Test failed at step #%d\n" ,
875
878
t -> name ,
876
879
WEXITSTATUS (status ));
877
880
}
878
881
} else if (WIFSIGNALED (status )) {
879
882
t -> passed = 0 ;
880
883
if (WTERMSIG (status ) == SIGABRT ) {
881
884
fprintf (TH_LOG_STREAM ,
882
- "%s: Test terminated by assertion\n" ,
885
+ "# %s: Test terminated by assertion\n" ,
883
886
t -> name );
884
887
} else if (WTERMSIG (status ) == t -> termsig ) {
885
888
t -> passed = 1 ;
886
889
} else {
887
890
fprintf (TH_LOG_STREAM ,
888
- "%s: Test terminated unexpectedly "
889
- "by signal %d\n" ,
891
+ "# %s: Test terminated unexpectedly by signal %d\n" ,
890
892
t -> name ,
891
893
WTERMSIG (status ));
892
894
}
893
895
} else {
894
896
fprintf (TH_LOG_STREAM ,
895
- "%s: Test ended in some other way [%u]\n" ,
897
+ "# %s: Test ended in some other way [%u]\n" ,
896
898
t -> name ,
897
899
status );
898
900
}
@@ -908,11 +910,11 @@ void __run_test(struct __fixture_metadata *f,
908
910
t -> step = 0 ;
909
911
t -> no_print = 0 ;
910
912
911
- printf ( "[ RUN ] %s%s%s.%s\n" ,
913
+ ksft_print_msg ( " RUN %s%s%s.%s ... \n" ,
912
914
f -> name , variant -> name [0 ] ? "." : "" , variant -> name , t -> name );
913
915
t -> pid = fork ();
914
916
if (t -> pid < 0 ) {
915
- printf ("ERROR SPAWNING TEST CHILD\n" );
917
+ ksft_print_msg ("ERROR SPAWNING TEST CHILD\n" );
916
918
t -> passed = 0 ;
917
919
} else if (t -> pid == 0 ) {
918
920
t -> fn (t , variant );
@@ -921,7 +923,9 @@ void __run_test(struct __fixture_metadata *f,
921
923
} else {
922
924
__wait_for_test (t );
923
925
}
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" ,
925
929
f -> name , variant -> name [0 ] ? "." : "" , variant -> name , t -> name );
926
930
}
927
931
@@ -945,8 +949,9 @@ static int test_harness_run(int __attribute__((unused)) argc,
945
949
}
946
950
}
947
951
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" ,
950
955
test_count , case_count );
951
956
for (f = __fixture_list ; f ; f = f -> next ) {
952
957
for (v = f -> variant ?: & no_variant ; v ; v = v -> next ) {
@@ -960,9 +965,12 @@ static int test_harness_run(int __attribute__((unused)) argc,
960
965
}
961
966
}
962
967
}
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 ;
966
974
}
967
975
968
976
static void __attribute__((constructor )) __constructor_order_first (void )
0 commit comments