xref: /dpdk/app/test/test.c (revision fc1f2750a3ec6da919e3c86e59d56f34ec97154b)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <string.h>
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <stdarg.h>
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <termios.h>
41 #include <ctype.h>
42 #include <sys/queue.h>
43 
44 #ifdef RTE_LIBRTE_CMDLINE
45 #include <cmdline_rdline.h>
46 #include <cmdline_parse.h>
47 #include <cmdline_socket.h>
48 #include <cmdline.h>
49 extern cmdline_parse_ctx_t main_ctx[];
50 #endif
51 
52 #include <rte_memory.h>
53 #include <rte_memzone.h>
54 #include <rte_tailq.h>
55 #include <rte_eal.h>
56 #include <rte_cycles.h>
57 #include <rte_log.h>
58 #include <rte_string_fns.h>
59 #ifdef RTE_LIBRTE_TIMER
60 #include <rte_timer.h>
61 #endif
62 
63 #include "test.h"
64 
65 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
66 
67 const char *prgname; /* to be set to argv[0] */
68 
69 #ifndef RTE_EXEC_ENV_BAREMETAL
70 static const char *recursive_call; /* used in linuxapp for MP and other tests */
71 
72 static int
73 no_action(void){ return 0; }
74 
75 static int
76 do_recursive_call(void)
77 {
78 	unsigned i;
79 	struct {
80 		const char *env_var;
81 		int (*action_fn)(void);
82 	} actions[] =  {
83 			{ "run_secondary_instances", test_mp_secondary },
84 			{ "test_missing_c_flag", no_action },
85 			{ "test_missing_n_flag", no_action },
86 			{ "test_no_hpet_flag", no_action },
87 			{ "test_whitelist_flag", no_action },
88 			{ "test_invalid_b_flag", no_action },
89 			{ "test_invalid_vdev_flag", no_action },
90 			{ "test_invalid_r_flag", no_action },
91 #ifdef RTE_LIBRTE_XEN_DOM0
92 			{ "test_dom0_misc_flags", no_action },
93 #else
94 			{ "test_misc_flags", no_action },
95 #endif
96 			{ "test_memory_flags", no_action },
97 			{ "test_file_prefix", no_action },
98 			{ "test_no_huge_flag", no_action },
99 #ifdef RTE_LIBRTE_IVSHMEM
100 			{ "test_ivshmem", test_ivshmem },
101 #endif
102 	};
103 
104 	if (recursive_call == NULL)
105 		return -1;
106 	for (i = 0; i < sizeof(actions)/sizeof(actions[0]); i++) {
107 		if (strcmp(actions[i].env_var, recursive_call) == 0)
108 			return (actions[i].action_fn)();
109 	}
110 	printf("ERROR - missing action to take for %s\n", recursive_call);
111 	return -1;
112 }
113 #endif
114 
115 int
116 main(int argc, char **argv)
117 {
118 #ifdef RTE_LIBRTE_CMDLINE
119 	struct cmdline *cl;
120 #endif
121 	int ret;
122 
123 	ret = rte_eal_init(argc, argv);
124 	if (ret < 0)
125 		return -1;
126 
127 #ifdef RTE_LIBRTE_TIMER
128 	rte_timer_subsystem_init();
129 #endif
130 
131 	if (commands_init() < 0)
132 		return -1;
133 
134 	argv += ret;
135 
136 	prgname = argv[0];
137 
138 #ifndef RTE_EXEC_ENV_BAREMETAL
139 	if ((recursive_call = getenv(RECURSIVE_ENV_VAR)) != NULL)
140 		return do_recursive_call();
141 #endif
142 
143 #ifdef RTE_LIBEAL_USE_HPET
144 	if (rte_eal_hpet_init(1) < 0)
145 #endif
146 		RTE_LOG(INFO, APP,
147 				"HPET is not enabled, using TSC as default timer\n");
148 
149 
150 #ifdef RTE_LIBRTE_CMDLINE
151 	cl = cmdline_stdin_new(main_ctx, "RTE>>");
152 	if (cl == NULL) {
153 		return -1;
154 	}
155 	cmdline_interact(cl);
156 	cmdline_stdin_exit(cl);
157 #endif
158 
159 	return 0;
160 }
161 
162 
163 int
164 unit_test_suite_runner(struct unit_test_suite *suite)
165 {
166 	int retval, i = 0;
167 
168 	if (suite->suite_name)
169 		printf("Test Suite : %s\n", suite->suite_name);
170 
171 	if (suite->setup)
172 		if (suite->setup() != 0)
173 			return -1;
174 
175 	while (suite->unit_test_cases[i].testcase) {
176 		/* Run test case setup */
177 		if (suite->unit_test_cases[i].setup) {
178 			retval = suite->unit_test_cases[i].setup();
179 			if (retval != 0)
180 				return retval;
181 		}
182 
183 		/* Run test case */
184 		if (suite->unit_test_cases[i].testcase() == 0) {
185 			printf("TestCase %2d: %s\n", i,
186 					suite->unit_test_cases[i].success_msg ?
187 					suite->unit_test_cases[i].success_msg :
188 					"passed");
189 		}
190 		else {
191 			printf("TestCase %2d: %s\n", i, suite->unit_test_cases[i].fail_msg ?
192 					suite->unit_test_cases[i].fail_msg :
193 					"failed");
194 			return -1;
195 		}
196 
197 		/* Run test case teardown */
198 		if (suite->unit_test_cases[i].teardown) {
199 			retval = suite->unit_test_cases[i].teardown();
200 			if (retval != 0)
201 				return retval;
202 		}
203 
204 		i++;
205 	}
206 
207 	/* Run test suite teardown */
208 	if (suite->teardown)
209 		if (suite->teardown() != 0)
210 			return -1;
211 
212 	return 0;
213 }
214