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