New upstream version 18.11-rc1
[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 int
79 main(int argc, char **argv)
80 {
81 #ifdef RTE_LIBRTE_CMDLINE
82         struct cmdline *cl;
83 #endif
84         int ret;
85
86         ret = rte_eal_init(argc, argv);
87         if (ret < 0) {
88                 ret = -1;
89                 goto out;
90         }
91
92 #ifdef RTE_LIBRTE_TIMER
93         rte_timer_subsystem_init();
94 #endif
95
96         if (commands_init() < 0) {
97                 ret = -1;
98                 goto out;
99         }
100
101         argv += ret;
102
103         prgname = argv[0];
104
105         recursive_call = getenv(RECURSIVE_ENV_VAR);
106         if (recursive_call != NULL) {
107                 ret = do_recursive_call();
108                 goto out;
109         }
110
111 #ifdef RTE_LIBEAL_USE_HPET
112         if (rte_eal_hpet_init(1) < 0)
113 #endif
114                 RTE_LOG(INFO, APP,
115                                 "HPET is not enabled, using TSC as default timer\n");
116
117
118 #ifdef RTE_LIBRTE_CMDLINE
119         cl = cmdline_stdin_new(main_ctx, "RTE>>");
120         if (cl == NULL) {
121                 ret = -1;
122                 goto out;
123         }
124
125         char *dpdk_test = getenv("DPDK_TEST");
126         if (dpdk_test && strlen(dpdk_test)) {
127                 char buf[1024];
128                 snprintf(buf, sizeof(buf), "%s\n", dpdk_test);
129                 if (cmdline_in(cl, buf, strlen(buf)) < 0) {
130                         printf("error on cmdline input\n");
131                         ret = -1;
132                         goto out;
133                 }
134
135                 cmdline_stdin_exit(cl);
136                 ret = last_test_result;
137                 goto out;
138         }
139         /* if no DPDK_TEST env variable, go interactive */
140         cmdline_interact(cl);
141         cmdline_stdin_exit(cl);
142 #endif
143         ret = 0;
144
145 out:
146         rte_eal_cleanup();
147         return ret;
148 }
149
150
151 int
152 unit_test_suite_runner(struct unit_test_suite *suite)
153 {
154         int test_success;
155         unsigned int total = 0, executed = 0, skipped = 0;
156         unsigned int succeeded = 0, failed = 0, unsupported = 0;
157         const char *status;
158
159         if (suite->suite_name) {
160                 printf(" + ------------------------------------------------------- +\n");
161                 printf(" + Test Suite : %s\n", suite->suite_name);
162         }
163
164         if (suite->setup)
165                 if (suite->setup() != 0) {
166                         /*
167                          * setup failed, so count all enabled tests and mark
168                          * them as failed
169                          */
170                         while (suite->unit_test_cases[total].testcase) {
171                                 if (!suite->unit_test_cases[total].enabled)
172                                         skipped++;
173                                 else
174                                         failed++;
175                                 total++;
176                         }
177                         goto suite_summary;
178                 }
179
180         printf(" + ------------------------------------------------------- +\n");
181
182         while (suite->unit_test_cases[total].testcase) {
183                 if (!suite->unit_test_cases[total].enabled) {
184                         skipped++;
185                         total++;
186                         continue;
187                 } else {
188                         executed++;
189                 }
190
191                 /* run test case setup */
192                 if (suite->unit_test_cases[total].setup)
193                         test_success = suite->unit_test_cases[total].setup();
194                 else
195                         test_success = TEST_SUCCESS;
196
197                 if (test_success == TEST_SUCCESS) {
198                         /* run the test case */
199                         test_success = suite->unit_test_cases[total].testcase();
200                         if (test_success == TEST_SUCCESS)
201                                 succeeded++;
202                         else if (test_success == -ENOTSUP)
203                                 unsupported++;
204                         else
205                                 failed++;
206                 } else if (test_success == -ENOTSUP) {
207                         unsupported++;
208                 } else {
209                         failed++;
210                 }
211
212                 /* run the test case teardown */
213                 if (suite->unit_test_cases[total].teardown)
214                         suite->unit_test_cases[total].teardown();
215
216                 if (test_success == TEST_SUCCESS)
217                         status = "succeeded";
218                 else if (test_success == -ENOTSUP)
219                         status = "unsupported";
220                 else
221                         status = "failed";
222
223                 printf(" + TestCase [%2d] : %s %s\n", total,
224                                 suite->unit_test_cases[total].name, status);
225
226                 total++;
227         }
228
229         /* Run test suite teardown */
230         if (suite->teardown)
231                 suite->teardown();
232
233         goto suite_summary;
234
235 suite_summary:
236         printf(" + ------------------------------------------------------- +\n");
237         printf(" + Test Suite Summary \n");
238         printf(" + Tests Total :       %2d\n", total);
239         printf(" + Tests Skipped :     %2d\n", skipped);
240         printf(" + Tests Executed :    %2d\n", executed);
241         printf(" + Tests Unsupported:  %2d\n", unsupported);
242         printf(" + Tests Passed :      %2d\n", succeeded);
243         printf(" + Tests Failed :      %2d\n", failed);
244         printf(" + ------------------------------------------------------- +\n");
245
246         last_test_result = failed;
247
248         if (failed)
249                 return -1;
250
251         return 0;
252 }