xref: /aosp_15_r20/external/libtracefs/utest/trace-utest.c (revision 287e80b3a36113050663245e7f2c00d274188f18)
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  * Copyright (C) 2020, VMware, Tzvetomir Stoyanov <[email protected]>
4  *
5  */
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <getopt.h>
9 #include <stdlib.h>
10 
11 #include <CUnit/CUnit.h>
12 #include <CUnit/Basic.h>
13 
14 #include "trace-utest.h"
15 
16 enum unit_tests {
17 	RUN_NONE	= 0,
18 	RUN_TRACEFS	= (1 << 0),
19 	RUN_ALL		= 0xFFFF
20 };
21 
print_help(char ** argv)22 static void print_help(char **argv)
23 {
24 	printf("Usage: %s [OPTIONS]\n", basename(argv[0]));
25 	printf("\t-s, --silent\tPrint test summary\n");
26 	printf("\t-r, --run test\tRun specific test:\n");
27 	printf("\t\t  tracefs   run libtracefs tests\n");
28 	printf("\t-h, --help\tPrint usage information\n");
29 	exit(0);
30 }
31 
main(int argc,char ** argv)32 int main(int argc, char **argv)
33 {
34 	CU_BasicRunMode verbose = CU_BRM_VERBOSE;
35 	enum unit_tests tests = RUN_NONE;
36 
37 	for (;;) {
38 		int c;
39 		int index = 0;
40 		const char *opts = "+hsr:";
41 		static struct option long_options[] = {
42 			{"silent", no_argument, NULL, 's'},
43 			{"run", required_argument, NULL, 'r'},
44 			{"help", no_argument, NULL, 'h'},
45 			{NULL, 0, NULL, 0}
46 		};
47 
48 		c = getopt_long (argc, argv, opts, long_options, &index);
49 		if (c == -1)
50 			break;
51 		switch (c) {
52 		case 'r':
53 			if (strcmp(optarg, "tracefs") == 0)
54 				tests |= RUN_TRACEFS;
55 			else
56 				print_help(argv);
57 			break;
58 		case 's':
59 			verbose = CU_BRM_SILENT;
60 			break;
61 		case 'h':
62 		default:
63 			print_help(argv);
64 			break;
65 		}
66 	}
67 
68 	if (tests == RUN_NONE)
69 		tests = RUN_ALL;
70 
71 	if (CU_initialize_registry() != CUE_SUCCESS) {
72 		printf("Test registry cannot be initialized\n");
73 		return -1;
74 	}
75 
76 	if (tests & RUN_TRACEFS)
77 		test_tracefs_lib();
78 
79 	CU_basic_set_mode(verbose);
80 	CU_basic_run_tests();
81 	CU_cleanup_registry();
82 	return 0;
83 }
84