New upstream version 18.11-rc2
[deb_dpdk.git] / test / test / test.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <string.h>
6 #include <stdio.h>
7 #include <stdint.h>
8 #include <stdarg.h>
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <termios.h>
12 #include <ctype.h>
13 #include <sys/queue.h>
14
15 #ifdef RTE_LIBRTE_CMDLINE
16 #include <cmdline_rdline.h>
17 #include <cmdline_parse.h>
18 #include <cmdline_socket.h>
19 #include <cmdline.h>
20 extern cmdline_parse_ctx_t main_ctx[];
21 #endif
22
23 #include <rte_memory.h>
24 #include <rte_eal.h>
25 #include <rte_cycles.h>
26 #include <rte_log.h>
27 #include <rte_string_fns.h>
28 #ifdef RTE_LIBRTE_TIMER
29 #include <rte_timer.h>
30 #endif
31
32 #include "test.h"
33
34 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
35
36 const char *prgname; /* to be set to argv[0] */
37
38 static const char *recursive_call; /* used in linuxapp for MP and other tests */
39
40 static int
41 no_action(void){ return 0; }
42
43 static int
44 do_recursive_call(void)
45 {
46         unsigned i;
47         struct {
48                 const char *env_var;
49                 int (*action_fn)(void);
50         } actions[] =  {
51                         { "run_secondary_instances", test_mp_secondary },
52                         { "test_missing_c_flag", no_action },
53                         { "test_master_lcore_flag", no_action },
54                         { "test_invalid_n_flag", no_action },
55                         { "test_no_hpet_flag", no_action },
56                         { "test_whitelist_flag", no_action },
57                         { "test_invalid_b_flag", no_action },
58                         { "test_invalid_vdev_flag", no_action },
59                         { "test_invalid_r_flag", no_action },
60                         { "test_misc_flags", no_action },
61                         { "test_memory_flags", no_action },
62                         { "test_file_prefix", no_action },
63                         { "test_no_huge_flag", no_action },
64         };
65
66         if (recursive_call == NULL)
67                 return -1;
68         for (i = 0; i < sizeof(actions)/sizeof(actions[0]); i++) {
69                 if (strcmp(actions[i].env_var, recursive_call) == 0)
70                         return (actions[i].action_fn)();
71         }
72         printf("ERROR - missing action to take for %s\n", recursive_call);
73         return -1;
74 }
75
76 int last_test_result;
77
78 #define MAX_EXTRA_ARGS 32
79
80 int
81 main(int argc, char **argv)
82 {
83 #ifdef RTE_LIBRTE_CMDLINE
84         struct cmdline *cl;
85 #endif
86         char *extra_args;
87         int ret;
88
89         extra_args = getenv("DPDK_TEST_PARAMS");
90         if (extra_args != NULL && strlen(extra_args) > 0) {
91                 char **all_argv;
92                 char *eargv[MAX_EXTRA_ARGS];
93                 int all_argc;
94                 int eargc;
95                 int i;
96
97                 RTE_LOG(INFO, APP, "Using additional DPDK_TEST_PARAMS: '%s'\n",
98                                 extra_args);
99                 eargc = rte_strsplit(extra_args, strlen(extra_args),
100                                 eargv, MAX_EXTRA_ARGS, ' ');
101
102                 /* merge argc/argv and the environment args */
103                 all_argc = argc + eargc;
104                 all_argv = malloc(sizeof(*all_argv) * (all_argc + 1));
105                 if (all_argv == NULL)
106                         return -1;
107
108                 for (i = 0; i < argc; i++)
109                         all_argv[i] = argv[i];
110                 for (i = 0; i < eargc; i++)
111                         all_argv[argc + i] = eargv[i];
112                 all_argv[all_argc] = NULL;
113
114                 /* call eal_init with combined args */
115                 ret = rte_eal_init(all_argc, all_argv);
116                 free(all_argv);
117         } else
118                 ret = rte_eal_init(argc, argv);
119         if (ret < 0) {
120                 ret = -1;
121                 goto out;
122         }
123
124 #ifdef RTE_LIBRTE_TIMER
125         rte_timer_subsystem_init();
126 #endif
127
128         if (commands_init() < 0) {
129                 ret = -1;
130                 goto out;
131         }
132
133         argv += ret;
134
135         prgname = argv[0];
136
137         recursive_call = getenv(RECURSIVE_ENV_VAR);
138         if (recursive_call != NULL) {
139                 ret = do_recursive_call();
140                 goto out;
141         }
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                 ret = -1;
154                 goto out;
155         }
156
157         char *dpdk_test = getenv("DPDK_TEST");
158         if (dpdk_test && strlen(dpdk_test)) {
159                 char buf[1024];
160                 snprintf(buf, sizeof(buf), "%s\n", dpdk_test);
161                 if (cmdline_in(cl, buf, strlen(buf)) < 0) {
162                         printf("error on cmdline input\n");
163                         ret = -1;
164                         goto out;
165                 }
166
167                 cmdline_stdin_exit(cl);
168                 ret = last_test_result;
169                 goto out;
170         }
171         /* if no DPDK_TEST env variable, go interactive */
172         cmdline_interact(cl);
173         cmdline_stdin_exit(cl);
174 #endif
175         ret = 0;
176
177 out:
178         rte_eal_cleanup();
179         return ret;
180 }
181
182
183 int
184 unit_test_suite_runner(struct unit_test_suite *suite)
185 {
186         int test_success;
187         unsigned int total = 0, executed = 0, skipped = 0;
188         unsigned int succeeded = 0, failed = 0, unsupported = 0;
189         const char *status;
190
191         if (suite->suite_name) {
192                 printf(" + ------------------------------------------------------- +\n");
193                 printf(" + Test Suite : %s\n", suite->suite_name);
194         }
195
196         if (suite->setup)
197                 if (suite->setup() != 0) {
198                         /*
199                          * setup failed, so count all enabled tests and mark
200                          * them as failed
201                          */
202                         while (suite->unit_test_cases[total].testcase) {
203                                 if (!suite->unit_test_cases[total].enabled)
204                                         skipped++;
205                                 else
206                                         failed++;
207                                 total++;
208                         }
209                         goto suite_summary;
210                 }
211
212         printf(" + ------------------------------------------------------- +\n");
213
214         while (suite->unit_test_cases[total].testcase) {
215                 if (!suite->unit_test_cases[total].enabled) {
216                         skipped++;
217                         total++;
218                         continue;
219                 } else {
220                         executed++;
221                 }
222
223                 /* run test case setup */
224                 if (suite->unit_test_cases[total].setup)
225                         test_success = suite->unit_test_cases[total].setup();
226                 else
227                         test_success = TEST_SUCCESS;
228
229                 if (test_success == TEST_SUCCESS) {
230                         /* run the test case */
231                         test_success = suite->unit_test_cases[total].testcase();
232                         if (test_success == TEST_SUCCESS)
233                                 succeeded++;
234                         else if (test_success == -ENOTSUP)
235                                 unsupported++;
236                         else
237                                 failed++;
238                 } else if (test_success == -ENOTSUP) {
239                         unsupported++;
240                 } else {
241                         failed++;
242                 }
243
244                 /* run the test case teardown */
245                 if (suite->unit_test_cases[total].teardown)
246                         suite->unit_test_cases[total].teardown();
247
248                 if (test_success == TEST_SUCCESS)
249                         status = "succeeded";
250                 else if (test_success == -ENOTSUP)
251                         status = "unsupported";
252                 else
253                         status = "failed";
254
255                 printf(" + TestCase [%2d] : %s %s\n", total,
256                                 suite->unit_test_cases[total].name, status);
257
258                 total++;
259         }
260
261         /* Run test suite teardown */
262         if (suite->teardown)
263                 suite->teardown();
264
265         goto suite_summary;
266
267 suite_summary:
268         printf(" + ------------------------------------------------------- +\n");
269         printf(" + Test Suite Summary \n");
270         printf(" + Tests Total :       %2d\n", total);
271         printf(" + Tests Skipped :     %2d\n", skipped);
272         printf(" + Tests Executed :    %2d\n", executed);
273         printf(" + Tests Unsupported:  %2d\n", unsupported);
274         printf(" + Tests Passed :      %2d\n", succeeded);
275         printf(" + Tests Failed :      %2d\n", failed);
276         printf(" + ------------------------------------------------------- +\n");
277
278         last_test_result = failed;
279
280         if (failed)
281                 return -1;
282
283         return 0;
284 }