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