New upstream version 17.11.1
[deb_dpdk.git] / test / test / test.c
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_eal.h>
54 #include <rte_cycles.h>
55 #include <rte_log.h>
56 #include <rte_string_fns.h>
57 #ifdef RTE_LIBRTE_TIMER
58 #include <rte_timer.h>
59 #endif
60
61 #include "test.h"
62
63 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
64
65 const char *prgname; /* to be set to argv[0] */
66
67 static const char *recursive_call; /* used in linuxapp for MP and other tests */
68
69 static int
70 no_action(void){ return 0; }
71
72 static int
73 do_recursive_call(void)
74 {
75         unsigned i;
76         struct {
77                 const char *env_var;
78                 int (*action_fn)(void);
79         } actions[] =  {
80                         { "run_secondary_instances", test_mp_secondary },
81                         { "test_missing_c_flag", no_action },
82                         { "test_master_lcore_flag", no_action },
83                         { "test_invalid_n_flag", no_action },
84                         { "test_no_hpet_flag", no_action },
85                         { "test_whitelist_flag", no_action },
86                         { "test_invalid_b_flag", no_action },
87                         { "test_invalid_vdev_flag", no_action },
88                         { "test_invalid_r_flag", no_action },
89                         { "test_misc_flags", no_action },
90                         { "test_memory_flags", no_action },
91                         { "test_file_prefix", no_action },
92                         { "test_no_huge_flag", no_action },
93         };
94
95         if (recursive_call == NULL)
96                 return -1;
97         for (i = 0; i < sizeof(actions)/sizeof(actions[0]); i++) {
98                 if (strcmp(actions[i].env_var, recursive_call) == 0)
99                         return (actions[i].action_fn)();
100         }
101         printf("ERROR - missing action to take for %s\n", recursive_call);
102         return -1;
103 }
104
105 int
106 main(int argc, char **argv)
107 {
108 #ifdef RTE_LIBRTE_CMDLINE
109         struct cmdline *cl;
110 #endif
111         int ret;
112
113         ret = rte_eal_init(argc, argv);
114         if (ret < 0)
115                 return -1;
116
117 #ifdef RTE_LIBRTE_TIMER
118         rte_timer_subsystem_init();
119 #endif
120
121         if (commands_init() < 0)
122                 return -1;
123
124         argv += ret;
125
126         prgname = argv[0];
127
128         if ((recursive_call = getenv(RECURSIVE_ENV_VAR)) != NULL)
129                 return do_recursive_call();
130
131 #ifdef RTE_LIBEAL_USE_HPET
132         if (rte_eal_hpet_init(1) < 0)
133 #endif
134                 RTE_LOG(INFO, APP,
135                                 "HPET is not enabled, using TSC as default timer\n");
136
137
138 #ifdef RTE_LIBRTE_CMDLINE
139         cl = cmdline_stdin_new(main_ctx, "RTE>>");
140         if (cl == NULL) {
141                 return -1;
142         }
143         cmdline_interact(cl);
144         cmdline_stdin_exit(cl);
145 #endif
146
147         return 0;
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         if (failed)
247                 return -1;
248
249         return 0;
250 }