-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit_test.c
610 lines (512 loc) · 12.1 KB
/
unit_test.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/select.h>
#include <unistd.h>
#include <getopt.h>
#include <linux/kut_mmzone.h>
#include <linux/kut_bug.h>
#include <linux/mmc/kut_host.h>
#include "unit_test.h"
/* unit_test.h should be included after the tested code's header files so that
* if it defines any of the following definitions it should get precedence */
#ifdef CONFIG_KUT_COLOURS
#undef C_CYAN
#define C_CYAN "\033[01;36m"
#undef C_RED
#define C_RED "\033[01;31m"
#undef C_GREEN
#define C_GREEN "\033[01;32m"
#undef C_BLUE
#define C_BLUE "\033[01;34m"
#undef C_GREY
#define C_GREY "\033[00;37m"
#undef C_NORMAL
#define C_NORMAL "\033[00;00;00m"
#undef C_HIGHLIGHT
#define C_HIGHLIGHT "\033[01m"
#undef C_ITALIC
#define C_ITALIC "\033[03m"
#undef C_UNDERLINE
#define C_UNDERLINE "\033[04m"
#else
#undef C_CYAN
#define C_CYAN ""
#undef C_RED
#define C_RED ""
#undef C_GREEN
#define C_GREEN ""
#undef C_BLUE
#define C_BLUE ""
#undef C_GREY
#define C_GREY ""
#undef C_NORMAL
#define C_NORMAL ""
#undef C_HIGHLIGHT
#define C_HIGHLIGHT ""
#undef C_UNDERLINE
#define C_UNDERLINE ""
#undef C_ITALIC
#define C_ITALIC ""
#endif
#ifndef CURSOR_POS_SAVE
#define CURSOR_POS_SAVE "\033[s"
#endif
#ifndef CURSOR_POS_RESTORE
#define CURSOR_POS_RESTORE "\033[u"
#endif
#ifndef CURSOR_MOV_UP
#define CURSOR_MOV_UP "\033[%dA"
#endif
#ifndef CURSOR_MOV_DOWN
#define CURSOR_MOV_DOWN "\033[%dB"
#endif
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MAX_APP_NAME_SZ 256
#define UT_DISABLED(tests, t) ({ \
int __ret; \
do { \
if ((tests)->is_test_disabled) \
__ret = (tests)->is_test_disabled((t)->disabled); \
else \
__ret = (t)->disabled; \
} while (0); \
__ret; \
})
#define LIST_TEST -1
#define RUN_ALL_TEST 0
typedef int (*vio_t)(const char *format, va_list ap);
struct unit_test_wrapper {
struct unit_test *ut;
unsigned long *map;
};
#define UNIT_TEST_DECLERATIONS
#include "tests.h"
#define UNIT_TEST_ENTRIES
static struct unit_test_wrapper all_tests[] = {
#include "tests.h"
};
/* io functionality */
int vscanf(const char *format, va_list ap);
static int first_comment;
int ask_user;
static int vio_colour(vio_t vfunc, char *colour, char *fmt, va_list va)
{
int ret;
if (!colour)
colour = C_NORMAL;
ret = printf("%s", colour);
ret += vfunc(fmt, va);
ret += printf("%s", C_NORMAL);
fflush(stdout);
return ret;
}
static int p_colour(char *colour, char *fmt, ...)
{
int ret;
va_list va;
va_start(va, fmt);
ret = vio_colour(vprintf, colour, fmt, va);
va_end(va);
return ret;
}
static struct unit_test_wrapper *module2ut(char *module)
{
int i;
for (i = 0; i < ARRAY_SZ(all_tests) &&
strcmp(module, all_tests[i].ut->module); i++);
if (i == ARRAY_SZ(all_tests)) {
printf("Error: '%s' is not a valid test modlue\n", module);
return NULL;
}
return all_tests + i;
}
static void p_test_summery(char *description, int total, int passed, int failed,
int known_issues, int disabled, char *summery_comment)
{
int i, len;
len = printf("\n%s Test Summery%s%s%s\n", description,
summery_comment ? " (" : "",
summery_comment ? summery_comment : "", summery_comment ?
")" : "");
for (i = 0; i < len - 2; i++)
printf("-");
printf("\n");
printf("%stotal: %i%s\n", C_HIGHLIGHT, total, C_NORMAL);
printf("passed: %i\n", passed);
printf("failed: %i\n", failed);
printf("known issues: %i\n", known_issues);
printf("disabled: %i\n", disabled);
}
static char *app_name(char *arg)
{
char *app_name;
app_name = strrchr(arg, '/');
if (app_name)
app_name += 1;
else
app_name = arg;
return app_name;
}
static void test_usage(char *path)
{
char *app = app_name(path);
printf(C_HIGHLIGHT "%s [OPTIONS]" C_NORMAL \
" - run kernel unit tests\n" \
"\n" \
" Where OPTIONS:\n"
"\n" \
C_HIGHLIGHT \
"-l, --list [module1 module2 ...]" \
C_NORMAL "\n" \
" List available unit tests.\n" \
"\n" \
" If no tests are specified then a summary of all " \
"available tests is listed.\n" \
"\n" \
" Specific module names may be selected to be listed in " \
"which case they will be listed in more detail.\n"
" The available test names are those listed when using " \
C_UNDERLINE "--list" C_NORMAL " without arguments.\n" \
"\n" \
C_HIGHLIGHT \
"-t, --test <module> [#1 [#2 [...]]]" \
C_NORMAL "\n"
" Specify a specific unit test to run.\n" \
"\n" \
" " C_ITALIC "module" C_NORMAL " is given by using the " \
C_UNDERLINE "--list" C_NORMAL " option without " \
"arguments.\n" \
" If no test ids are provided, then all the tests of " \
C_ITALIC "module" C_NORMAL " are run.\n" \
"\n" \
" Execution can be limited to running a subset of the " \
C_ITALIC "module" C_NORMAL "'s tests.\n"
" This is done by specific test numbers as are given by " \
C_UNDERLINE "--list " C_ITALIC "module" C_NORMAL ".\n" \
"\n" \
C_HIGHLIGHT "-h, --help" C_NORMAL "\n" \
" Print this help message and exit.\n" \
"\n" \
C_HIGHLIGHT \
"If no options are used, %s runs all the tests." \
C_NORMAL "\n", app, app);
}
static int test_pre_post(int pre, struct unit_test *ut)
{
int ret;
int (*func)(void) = pre? ut->pre_all_tests: ut->post_all_tests;
if (!func)
return 0;
if (!pre)
printf("\n");
p_colour(C_HIGHLIGHT, "%s %s\n", ut->description,
pre ? "Init" : "Uninit");
ret = func();
if (ret) {
printf(C_RED "%s failed\n" C_NORMAL,
pre ? "pre_all_tests" : "post_all_tests");
}
return ret;
}
static void init_common(void)
{
kut_bug_on_do_exit_set(false);
kut_mem_pressure_set(KUT_MEM_AVERAGE);
kut_host_index_reset();
srandom(0);
}
static int test_init(struct unit_test *ut)
{
init_common();
return test_pre_post(1, ut);
}
static int test_uninit(struct unit_test *ut)
{
return test_pre_post(0, ut);
}
static int pre_single_test(struct unit_test *ut)
{
init_common();
if (!ut->pre_single_test)
return 0;
return ut->pre_single_test();
}
static int post_single_test(struct unit_test *ut)
{
if (!ut->post_single_test)
return 0;
return ut->post_single_test();
}
static int setup_map(struct unit_test_wrapper *wrapper)
{
int size;
if (wrapper->map)
return 0;
size = (wrapper->ut->count + sizeof(unsigned long) - 1) /
sizeof(unsigned long);
if (!(wrapper->map = calloc(size, sizeof(unsigned long))))
return -1;
return 0;
}
static void teardown_map(struct unit_test_wrapper *wrapper)
{
free(wrapper->map);
wrapper->map = NULL;
}
static void map_set_get(unsigned long **map, int *shift)
{
int div = *shift/sizeof(unsigned long);
*map += div;
*shift -= div*sizeof(unsigned long);
}
static void map_set(unsigned long *map, int shift)
{
map_set_get(&map, &shift);
*map |= 1 << shift;
}
static int map_get(unsigned long *map, int shift)
{
map_set_get(&map, &shift);
return *map & 1 << shift ? 1 : 0;
}
static int unit_test(struct unit_test *ut, unsigned long *map)
{
int total = 0, disabled = 0, passed = 0, failed = 0, known_issues = 0;
int len, i;
/* print unit test description as header */
len = printf(C_HIGHLIGHT "%s Unit Tests\n", ut->description);
for (i = 0; i < len - 6; i++)
printf("-");
printf("\n" C_NORMAL);
if (test_init(ut))
return -1;
for (i = 0; i < ut->count; i++) {
struct single_test *t;
if (map && !(map_get(map, i)))
continue;
t = ut->tests + i;
first_comment = 1;
total++;
p_colour(C_HIGHLIGHT, "%i. %s ", i + 1, t->description);
if (!t->func) {
p_colour(C_CYAN, "function does not exist\n");
return -1;
}
printf("\n");
fflush(stdout);
if (UT_DISABLED(ut, t)) {
disabled++;
p_colour(C_CYAN, "disabled\n");
continue;
}
if (t->known_issue) {
p_colour(C_BLUE, "known issue: ");
p_colour(C_NORMAL, "%s\n", t->known_issue);
known_issues++;
continue;
}
if (pre_single_test(ut)) {
printf(C_RED "pre test failed, continuing...\n"
C_NORMAL);
continue;
}
if ((t->func())) {
p_colour(C_RED, "Failed");
failed++;
} else {
p_colour(C_GREEN, "OK");
passed++;
}
if (post_single_test(ut)) {
printf(C_RED "post test failed, continuing...\n"
C_NORMAL);
continue;
}
printf("\n");
}
if (test_uninit(ut))
return -1;
p_test_summery(ut->description, total, passed, failed, known_issues,
disabled, ut->summery_comment);
return 0;
}
static void list_tests_all(void)
{
int i;
/* print header */
printf(C_HIGHLIGHT "%-5s %-20s %s\n", "num", "module", "description");
printf(C_HIGHLIGHT "%-5s %-20s %s\n", "---", "------", "-----------");
/* list tests */
for (i = 0; i < ARRAY_SZ(all_tests); i++) {
printf("%3.d. %-20s %s\n", i+1, all_tests[i].ut->module,
all_tests[i].ut->description);
}
}
static void list_tests_single(struct unit_test *ut)
{
int i, count = ut->count;
char *list_comment = ut->list_comment;
struct single_test *tests = ut->tests;
p_colour(C_HIGHLIGHT, "%s Unit Tests%s%s%s\n", ut->description,
list_comment ? " (" : "", list_comment ? list_comment : "",
list_comment ? ")" : "");
for (i = 0; i < count; i++) {
struct single_test *t = &tests[i];
int is_test_disabled = UT_DISABLED(ut, t);
printf("%i. ", i + 1);
p_colour(is_test_disabled ? C_GREY : C_NORMAL, "%s",
t->description);
if (is_test_disabled) {
p_colour(C_CYAN, " (disabled)");
} else if (t->known_issue) {
p_colour(C_BLUE, " (known issue: ");
p_colour(C_GREY, t->known_issue);
p_colour(C_BLUE, ")");
}
printf("\n");
}
}
static void list_tests(struct unit_test *ut)
{
if (!ut)
list_tests_all();
else
list_tests_single(ut);
}
int main(int argc, char **argv)
{
int i, arg;
char *optstring = "hlt:";
struct option longopts[] = {
{
.name = "list",
.val = 'l',
.has_arg = no_argument,
.flag = NULL,
},
{
.name = "test",
.val = 't',
.has_arg = required_argument,
.flag = NULL,
},
{
.name = "help",
.val = 'h',
.has_arg = no_argument,
.flag = NULL,
},
{ 0 }
};
enum {
arg_none,
arg_list,
arg_test,
arg_help,
} argtype = arg_none;
struct unit_test_wrapper *wrapper;
while ((arg = getopt_long(argc, argv, optstring, longopts, NULL)) !=
-1) {
switch(arg) {
case 'l':
if (argtype != arg_none)
goto err_arg;
argtype = arg_list;
/* list all tests */
if (argc == 2) {
list_tests(NULL);
break;
}
/* list specific tests */
for (i = 2; i < argc; i++) {
wrapper = module2ut(argv[i]);
if (!wrapper) {
printf("Error: unknown unit test - "
"%s\n", argv[i]);
continue;
}
list_tests(wrapper->ut);
}
break;
case 't':
if (argtype != arg_none && argtype != arg_test)
goto err_arg;
if (argtype == arg_test)
printf("\n");
else
argtype = arg_test;
/* handle a specified unit test */
if (!(wrapper = module2ut(optarg))) {
printf("Error: unknown unit test - %s\n",
optarg);
break;
}
/* run a specified selection of its tests */
while (optind < argc) {
int test;
char *err;
test = strtol(argv[optind], &err, 10);
if (!*err) {
optind++;
if (!test ||
test > wrapper->ut->count) {
printf("Error: %s out of " \
"range - %d\n", optarg,
test);
continue;
}
if (setup_map(wrapper)) {
printf("Error: out of " \
"memory\n");
return -1;
}
map_set(wrapper->map, test - 1);
} else {
if (strcmp(argv[optind], "-t") &&
strncmp(argv[optind], "--test",
MAX(strlen(argv[optind]), 3))) {
printf("Error: not a test " \
"number: '%s'\n",
argv[optind]);
}
break;
}
}
unit_test(wrapper->ut, wrapper->map);
teardown_map(wrapper);
break;
case 'h':
if (argtype != arg_none)
goto err_arg;
argtype = arg_help;
test_usage(argv[0]);
return 0;
default:
test_usage(argv[0]);
return -1;
}
}
/* run all tests */
if (argc == 1) {
for (i = 0; i < ARRAY_SZ(all_tests); i++) {
if (i)
printf("\n");
unit_test(all_tests[i].ut, 0);
}
return 0;
} else if (argtype == arg_none) {
printf("Error: unknown arguments - ");
for (i = 1; i < argc ;i++)
printf("%s ", argv[i]);
printf("\n");
return -1;
}
return 0;
err_arg:
printf("Error: incompatible argument combination\n");
return -1;
}