1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50#ifndef __KSELFTEST_HARNESS_H
51#define __KSELFTEST_HARNESS_H
52
53#ifndef _GNU_SOURCE
54#define _GNU_SOURCE
55#endif
56#include <asm/types.h>
57#include <errno.h>
58#include <stdbool.h>
59#include <stdint.h>
60#include <stdio.h>
61#include <stdlib.h>
62#include <string.h>
63#include <sys/mman.h>
64#include <sys/types.h>
65#include <sys/wait.h>
66#include <unistd.h>
67
68#include "kselftest.h"
69
70#define TEST_TIMEOUT_DEFAULT 30
71
72
73#ifndef TH_LOG_STREAM
74# define TH_LOG_STREAM stderr
75#endif
76
77#ifndef TH_LOG_ENABLED
78# define TH_LOG_ENABLED 1
79#endif
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105#define TH_LOG(fmt, ...) do { \
106 if (TH_LOG_ENABLED) \
107 __TH_LOG(fmt, ##__VA_ARGS__); \
108} while (0)
109
110
111#define __TH_LOG(fmt, ...) \
112 fprintf(TH_LOG_STREAM, "# %s:%d:%s:" fmt "\n", \
113 __FILE__, __LINE__, _metadata->name, ##__VA_ARGS__)
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129#define SKIP(statement, fmt, ...) do { \
130 snprintf(_metadata->results->reason, \
131 sizeof(_metadata->results->reason), fmt, ##__VA_ARGS__); \
132 if (TH_LOG_ENABLED) { \
133 fprintf(TH_LOG_STREAM, "# SKIP %s\n", \
134 _metadata->results->reason); \
135 } \
136 _metadata->passed = 1; \
137 _metadata->skip = 1; \
138 _metadata->trigger = 0; \
139 statement; \
140} while (0)
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159#define TEST(test_name) __TEST_IMPL(test_name, -1)
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178#define TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal)
179
180#define __TEST_IMPL(test_name, _signal) \
181 static void test_name(struct __test_metadata *_metadata); \
182 static inline void wrapper_##test_name( \
183 struct __test_metadata *_metadata, \
184 struct __fixture_variant_metadata *variant) \
185 { \
186 test_name(_metadata); \
187 } \
188 static struct __test_metadata _##test_name##_object = \
189 { .name = #test_name, \
190 .fn = &wrapper_##test_name, \
191 .fixture = &_fixture_global, \
192 .termsig = _signal, \
193 .timeout = TEST_TIMEOUT_DEFAULT, }; \
194 static void __attribute__((constructor)) _register_##test_name(void) \
195 { \
196 __register_test(&_##test_name##_object); \
197 } \
198 static void test_name( \
199 struct __test_metadata __attribute__((unused)) *_metadata)
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216#define FIXTURE_DATA(datatype_name) struct _test_data_##datatype_name
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234#define FIXTURE(fixture_name) \
235 FIXTURE_VARIANT(fixture_name); \
236 static struct __fixture_metadata _##fixture_name##_fixture_object = \
237 { .name = #fixture_name, }; \
238 static void __attribute__((constructor)) \
239 _register_##fixture_name##_data(void) \
240 { \
241 __register_fixture(&_##fixture_name##_fixture_object); \
242 } \
243 FIXTURE_DATA(fixture_name)
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264#define FIXTURE_SETUP(fixture_name) \
265 void fixture_name##_setup( \
266 struct __test_metadata __attribute__((unused)) *_metadata, \
267 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
268 const FIXTURE_VARIANT(fixture_name) \
269 __attribute__((unused)) *variant)
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287#define FIXTURE_TEARDOWN(fixture_name) \
288 void fixture_name##_teardown( \
289 struct __test_metadata __attribute__((unused)) *_metadata, \
290 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self)
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309#define FIXTURE_VARIANT(fixture_name) struct _fixture_variant_##fixture_name
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329#define FIXTURE_VARIANT_ADD(fixture_name, variant_name) \
330 extern FIXTURE_VARIANT(fixture_name) \
331 _##fixture_name##_##variant_name##_variant; \
332 static struct __fixture_variant_metadata \
333 _##fixture_name##_##variant_name##_object = \
334 { .name = #variant_name, \
335 .data = &_##fixture_name##_##variant_name##_variant}; \
336 static void __attribute__((constructor)) \
337 _register_##fixture_name##_##variant_name(void) \
338 { \
339 __register_fixture_variant(&_##fixture_name##_fixture_object, \
340 &_##fixture_name##_##variant_name##_object); \
341 } \
342 FIXTURE_VARIANT(fixture_name) \
343 _##fixture_name##_##variant_name##_variant =
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363#define TEST_F(fixture_name, test_name) \
364 __TEST_F_IMPL(fixture_name, test_name, -1, TEST_TIMEOUT_DEFAULT)
365
366#define TEST_F_SIGNAL(fixture_name, test_name, signal) \
367 __TEST_F_IMPL(fixture_name, test_name, signal, TEST_TIMEOUT_DEFAULT)
368
369#define TEST_F_TIMEOUT(fixture_name, test_name, timeout) \
370 __TEST_F_IMPL(fixture_name, test_name, -1, timeout)
371
372#define __TEST_F_IMPL(fixture_name, test_name, signal, tmout) \
373 static void fixture_name##_##test_name( \
374 struct __test_metadata *_metadata, \
375 FIXTURE_DATA(fixture_name) *self, \
376 const FIXTURE_VARIANT(fixture_name) *variant); \
377 static inline void wrapper_##fixture_name##_##test_name( \
378 struct __test_metadata *_metadata, \
379 struct __fixture_variant_metadata *variant) \
380 { \
381 \
382 FIXTURE_DATA(fixture_name) self; \
383 memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \
384 fixture_name##_setup(_metadata, &self, variant->data); \
385 \
386 if (!_metadata->passed) \
387 return; \
388 fixture_name##_##test_name(_metadata, &self, variant->data); \
389 fixture_name##_teardown(_metadata, &self); \
390 } \
391 static struct __test_metadata \
392 _##fixture_name##_##test_name##_object = { \
393 .name = #test_name, \
394 .fn = &wrapper_##fixture_name##_##test_name, \
395 .fixture = &_##fixture_name##_fixture_object, \
396 .termsig = signal, \
397 .timeout = tmout, \
398 }; \
399 static void __attribute__((constructor)) \
400 _register_##fixture_name##_##test_name(void) \
401 { \
402 __register_test(&_##fixture_name##_##test_name##_object); \
403 } \
404 static void fixture_name##_##test_name( \
405 struct __test_metadata __attribute__((unused)) *_metadata, \
406 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
407 const FIXTURE_VARIANT(fixture_name) \
408 __attribute__((unused)) *variant)
409
410
411
412
413
414
415
416
417
418
419#define TEST_HARNESS_MAIN \
420 static void __attribute__((constructor)) \
421 __constructor_order_last(void) \
422 { \
423 if (!__constructor_order) \
424 __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \
425 } \
426 int main(int argc, char **argv) { \
427 return test_harness_run(argc, argv); \
428 }
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446#define ASSERT_EQ(expected, seen) \
447 __EXPECT(expected, #expected, seen, #seen, ==, 1)
448
449
450
451
452
453
454
455
456
457#define ASSERT_NE(expected, seen) \
458 __EXPECT(expected, #expected, seen, #seen, !=, 1)
459
460
461
462
463
464
465
466
467
468#define ASSERT_LT(expected, seen) \
469 __EXPECT(expected, #expected, seen, #seen, <, 1)
470
471
472
473
474
475
476
477
478
479#define ASSERT_LE(expected, seen) \
480 __EXPECT(expected, #expected, seen, #seen, <=, 1)
481
482
483
484
485
486
487
488
489
490#define ASSERT_GT(expected, seen) \
491 __EXPECT(expected, #expected, seen, #seen, >, 1)
492
493
494
495
496
497
498
499
500
501#define ASSERT_GE(expected, seen) \
502 __EXPECT(expected, #expected, seen, #seen, >=, 1)
503
504
505
506
507
508
509
510
511#define ASSERT_NULL(seen) \
512 __EXPECT(NULL, "NULL", seen, #seen, ==, 1)
513
514
515
516
517
518
519
520
521#define ASSERT_TRUE(seen) \
522 __EXPECT(0, "0", seen, #seen, !=, 1)
523
524
525
526
527
528
529
530
531#define ASSERT_FALSE(seen) \
532 __EXPECT(0, "0", seen, #seen, ==, 1)
533
534
535
536
537
538
539
540
541
542#define ASSERT_STREQ(expected, seen) \
543 __EXPECT_STR(expected, seen, ==, 1)
544
545
546
547
548
549
550
551
552
553#define ASSERT_STRNE(expected, seen) \
554 __EXPECT_STR(expected, seen, !=, 1)
555
556
557
558
559
560
561
562
563
564#define EXPECT_EQ(expected, seen) \
565 __EXPECT(expected, #expected, seen, #seen, ==, 0)
566
567
568
569
570
571
572
573
574
575#define EXPECT_NE(expected, seen) \
576 __EXPECT(expected, #expected, seen, #seen, !=, 0)
577
578
579
580
581
582
583
584
585
586#define EXPECT_LT(expected, seen) \
587 __EXPECT(expected, #expected, seen, #seen, <, 0)
588
589
590
591
592
593
594
595
596
597#define EXPECT_LE(expected, seen) \
598 __EXPECT(expected, #expected, seen, #seen, <=, 0)
599
600
601
602
603
604
605
606
607
608#define EXPECT_GT(expected, seen) \
609 __EXPECT(expected, #expected, seen, #seen, >, 0)
610
611
612
613
614
615
616
617
618
619#define EXPECT_GE(expected, seen) \
620 __EXPECT(expected, #expected, seen, #seen, >=, 0)
621
622
623
624
625
626
627
628
629#define EXPECT_NULL(seen) \
630 __EXPECT(NULL, "NULL", seen, #seen, ==, 0)
631
632
633
634
635
636
637
638
639#define EXPECT_TRUE(seen) \
640 __EXPECT(0, "0", seen, #seen, !=, 0)
641
642
643
644
645
646
647
648
649#define EXPECT_FALSE(seen) \
650 __EXPECT(0, "0", seen, #seen, ==, 0)
651
652
653
654
655
656
657
658
659
660#define EXPECT_STREQ(expected, seen) \
661 __EXPECT_STR(expected, seen, ==, 0)
662
663
664
665
666
667
668
669
670
671#define EXPECT_STRNE(expected, seen) \
672 __EXPECT_STR(expected, seen, !=, 0)
673
674#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
675
676
677
678
679
680
681
682#define OPTIONAL_HANDLER(_assert) \
683 for (; _metadata->trigger; _metadata->trigger = \
684 __bail(_assert, _metadata->no_print, _metadata->step))
685
686#define __INC_STEP(_metadata) \
687 \
688 if (_metadata->passed && _metadata->step < 253) \
689 _metadata->step++;
690
691#define is_signed_type(var) (!!(((__typeof__(var))(-1)) < (__typeof__(var))1))
692
693#define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \
694 \
695 __typeof__(_expected) __exp = (_expected); \
696 __typeof__(_seen) __seen = (_seen); \
697 if (_assert) __INC_STEP(_metadata); \
698 if (!(__exp _t __seen)) { \
699 \
700 switch (is_signed_type(__exp) * 2 + is_signed_type(__seen)) { \
701 case 0: { \
702 unsigned long long __exp_print = (uintptr_t)__exp; \
703 unsigned long long __seen_print = (uintptr_t)__seen; \
704 __TH_LOG("Expected %s (%llu) %s %s (%llu)", \
705 _expected_str, __exp_print, #_t, \
706 _seen_str, __seen_print); \
707 break; \
708 } \
709 case 1: { \
710 unsigned long long __exp_print = (uintptr_t)__exp; \
711 long long __seen_print = (intptr_t)__seen; \
712 __TH_LOG("Expected %s (%llu) %s %s (%lld)", \
713 _expected_str, __exp_print, #_t, \
714 _seen_str, __seen_print); \
715 break; \
716 } \
717 case 2: { \
718 long long __exp_print = (intptr_t)__exp; \
719 unsigned long long __seen_print = (uintptr_t)__seen; \
720 __TH_LOG("Expected %s (%lld) %s %s (%llu)", \
721 _expected_str, __exp_print, #_t, \
722 _seen_str, __seen_print); \
723 break; \
724 } \
725 case 3: { \
726 long long __exp_print = (intptr_t)__exp; \
727 long long __seen_print = (intptr_t)__seen; \
728 __TH_LOG("Expected %s (%lld) %s %s (%lld)", \
729 _expected_str, __exp_print, #_t, \
730 _seen_str, __seen_print); \
731 break; \
732 } \
733 } \
734 _metadata->passed = 0; \
735 \
736 _metadata->trigger = 1; \
737 } \
738} while (0); OPTIONAL_HANDLER(_assert)
739
740#define __EXPECT_STR(_expected, _seen, _t, _assert) do { \
741 const char *__exp = (_expected); \
742 const char *__seen = (_seen); \
743 if (_assert) __INC_STEP(_metadata); \
744 if (!(strcmp(__exp, __seen) _t 0)) { \
745 __TH_LOG("Expected '%s' %s '%s'.", __exp, #_t, __seen); \
746 _metadata->passed = 0; \
747 _metadata->trigger = 1; \
748 } \
749} while (0); OPTIONAL_HANDLER(_assert)
750
751
752#define __LIST_APPEND(head, item) \
753{ \
754 \
755 if (head == NULL) { \
756 head = item; \
757 item->next = NULL; \
758 item->prev = item; \
759 return; \
760 } \
761 if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \
762 item->next = NULL; \
763 item->prev = head->prev; \
764 item->prev->next = item; \
765 head->prev = item; \
766 } else { \
767 item->next = head; \
768 item->next->prev = item; \
769 item->prev = item; \
770 head = item; \
771 } \
772}
773
774struct __test_results {
775 char reason[1024];
776};
777
778struct __test_metadata;
779struct __fixture_variant_metadata;
780
781
782struct __fixture_metadata {
783 const char *name;
784 struct __test_metadata *tests;
785 struct __fixture_variant_metadata *variant;
786 struct __fixture_metadata *prev, *next;
787} _fixture_global __attribute__((unused)) = {
788 .name = "global",
789 .prev = &_fixture_global,
790};
791
792static struct __fixture_metadata *__fixture_list = &_fixture_global;
793static int __constructor_order;
794
795#define _CONSTRUCTOR_ORDER_FORWARD 1
796#define _CONSTRUCTOR_ORDER_BACKWARD -1
797
798static inline void __register_fixture(struct __fixture_metadata *f)
799{
800 __LIST_APPEND(__fixture_list, f);
801}
802
803struct __fixture_variant_metadata {
804 const char *name;
805 const void *data;
806 struct __fixture_variant_metadata *prev, *next;
807};
808
809static inline void
810__register_fixture_variant(struct __fixture_metadata *f,
811 struct __fixture_variant_metadata *variant)
812{
813 __LIST_APPEND(f->variant, variant);
814}
815
816
817struct __test_metadata {
818 const char *name;
819 void (*fn)(struct __test_metadata *,
820 struct __fixture_variant_metadata *);
821 pid_t pid;
822 struct __fixture_metadata *fixture;
823 int termsig;
824 int passed;
825 int skip;
826 int trigger;
827 int timeout;
828 bool timed_out;
829 __u8 step;
830 bool no_print;
831 struct __test_results *results;
832 struct __test_metadata *prev, *next;
833};
834
835
836
837
838
839
840
841
842
843
844static inline void __register_test(struct __test_metadata *t)
845{
846 __LIST_APPEND(t->fixture->tests, t);
847}
848
849static inline int __bail(int for_realz, bool no_print, __u8 step)
850{
851 if (for_realz) {
852 if (no_print)
853 _exit(step);
854 abort();
855 }
856 return 0;
857}
858
859struct __test_metadata *__active_test;
860static void __timeout_handler(int sig, siginfo_t *info, void *ucontext)
861{
862 struct __test_metadata *t = __active_test;
863
864
865 if (!t) {
866 fprintf(TH_LOG_STREAM,
867 "# no active test in SIGALRM handler!?\n");
868 abort();
869 }
870 if (sig != SIGALRM || sig != info->si_signo) {
871 fprintf(TH_LOG_STREAM,
872 "# %s: SIGALRM handler caught signal %d!?\n",
873 t->name, sig != SIGALRM ? sig : info->si_signo);
874 abort();
875 }
876
877 t->timed_out = true;
878 kill(t->pid, SIGKILL);
879}
880
881void __wait_for_test(struct __test_metadata *t)
882{
883 struct sigaction action = {
884 .sa_sigaction = __timeout_handler,
885 .sa_flags = SA_SIGINFO,
886 };
887 struct sigaction saved_action;
888 int status;
889
890 if (sigaction(SIGALRM, &action, &saved_action)) {
891 t->passed = 0;
892 fprintf(TH_LOG_STREAM,
893 "# %s: unable to install SIGALRM handler\n",
894 t->name);
895 return;
896 }
897 __active_test = t;
898 t->timed_out = false;
899 alarm(t->timeout);
900 waitpid(t->pid, &status, 0);
901 alarm(0);
902 if (sigaction(SIGALRM, &saved_action, NULL)) {
903 t->passed = 0;
904 fprintf(TH_LOG_STREAM,
905 "# %s: unable to uninstall SIGALRM handler\n",
906 t->name);
907 return;
908 }
909 __active_test = NULL;
910
911 if (t->timed_out) {
912 t->passed = 0;
913 fprintf(TH_LOG_STREAM,
914 "# %s: Test terminated by timeout\n", t->name);
915 } else if (WIFEXITED(status)) {
916 if (t->termsig != -1) {
917 t->passed = 0;
918 fprintf(TH_LOG_STREAM,
919 "# %s: Test exited normally instead of by signal (code: %d)\n",
920 t->name,
921 WEXITSTATUS(status));
922 } else {
923 switch (WEXITSTATUS(status)) {
924
925 case 0:
926 t->passed = 1;
927 break;
928
929 case 255:
930 t->passed = 1;
931 t->skip = 1;
932 break;
933
934 default:
935 t->passed = 0;
936 fprintf(TH_LOG_STREAM,
937 "# %s: Test failed at step #%d\n",
938 t->name,
939 WEXITSTATUS(status));
940 }
941 }
942 } else if (WIFSIGNALED(status)) {
943 t->passed = 0;
944 if (WTERMSIG(status) == SIGABRT) {
945 fprintf(TH_LOG_STREAM,
946 "# %s: Test terminated by assertion\n",
947 t->name);
948 } else if (WTERMSIG(status) == t->termsig) {
949 t->passed = 1;
950 } else {
951 fprintf(TH_LOG_STREAM,
952 "# %s: Test terminated unexpectedly by signal %d\n",
953 t->name,
954 WTERMSIG(status));
955 }
956 } else {
957 fprintf(TH_LOG_STREAM,
958 "# %s: Test ended in some other way [%u]\n",
959 t->name,
960 status);
961 }
962}
963
964void __run_test(struct __fixture_metadata *f,
965 struct __fixture_variant_metadata *variant,
966 struct __test_metadata *t)
967{
968
969 t->passed = 1;
970 t->skip = 0;
971 t->trigger = 0;
972 t->step = 0;
973 t->no_print = 0;
974 memset(t->results->reason, 0, sizeof(t->results->reason));
975
976 ksft_print_msg(" RUN %s%s%s.%s ...\n",
977 f->name, variant->name[0] ? "." : "", variant->name, t->name);
978
979
980 fflush(stdout);
981 fflush(stderr);
982
983 t->pid = fork();
984 if (t->pid < 0) {
985 ksft_print_msg("ERROR SPAWNING TEST CHILD\n");
986 t->passed = 0;
987 } else if (t->pid == 0) {
988 t->fn(t, variant);
989 if (t->skip)
990 _exit(255);
991
992 if (t->passed)
993 _exit(0);
994
995 _exit(t->step);
996 } else {
997 __wait_for_test(t);
998 }
999 ksft_print_msg(" %4s %s%s%s.%s\n", t->passed ? "OK" : "FAIL",
1000 f->name, variant->name[0] ? "." : "", variant->name, t->name);
1001
1002 if (t->skip)
1003 ksft_test_result_skip("%s\n", t->results->reason[0] ?
1004 t->results->reason : "unknown");
1005 else
1006 ksft_test_result(t->passed, "%s%s%s.%s\n",
1007 f->name, variant->name[0] ? "." : "", variant->name, t->name);
1008}
1009
1010static int test_harness_run(int __attribute__((unused)) argc,
1011 char __attribute__((unused)) **argv)
1012{
1013 struct __fixture_variant_metadata no_variant = { .name = "", };
1014 struct __fixture_variant_metadata *v;
1015 struct __fixture_metadata *f;
1016 struct __test_results *results;
1017 struct __test_metadata *t;
1018 int ret = 0;
1019 unsigned int case_count = 0, test_count = 0;
1020 unsigned int count = 0;
1021 unsigned int pass_count = 0;
1022
1023 for (f = __fixture_list; f; f = f->next) {
1024 for (v = f->variant ?: &no_variant; v; v = v->next) {
1025 case_count++;
1026 for (t = f->tests; t; t = t->next)
1027 test_count++;
1028 }
1029 }
1030
1031 results = mmap(NULL, sizeof(*results), PROT_READ | PROT_WRITE,
1032 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1033
1034 ksft_print_header();
1035 ksft_set_plan(test_count);
1036 ksft_print_msg("Starting %u tests from %u test cases.\n",
1037 test_count, case_count);
1038 for (f = __fixture_list; f; f = f->next) {
1039 for (v = f->variant ?: &no_variant; v; v = v->next) {
1040 for (t = f->tests; t; t = t->next) {
1041 count++;
1042 t->results = results;
1043 __run_test(f, v, t);
1044 t->results = NULL;
1045 if (t->passed)
1046 pass_count++;
1047 else
1048 ret = 1;
1049 }
1050 }
1051 }
1052 munmap(results, sizeof(*results));
1053
1054 ksft_print_msg("%s: %u / %u tests passed.\n", ret ? "FAILED" : "PASSED",
1055 pass_count, count);
1056 ksft_exit(ret == 0);
1057
1058
1059 return KSFT_FAIL;
1060}
1061
1062static void __attribute__((constructor)) __constructor_order_first(void)
1063{
1064 if (!__constructor_order)
1065 __constructor_order = _CONSTRUCTOR_ORDER_FORWARD;
1066}
1067
1068#endif
1069