New upstream version 16.11.5
[deb_dpdk.git] / app / 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_memzone.h>
54 #include <rte_eal.h>
55 #include <rte_cycles.h>
56 #include <rte_log.h>
57 #include <rte_string_fns.h>
58 #ifdef RTE_LIBRTE_TIMER
59 #include <rte_timer.h>
60 #endif
61
62 #include "test.h"
63
64 #define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
65
66 const char *prgname; /* to be set to argv[0] */
67
68 static const char *recursive_call; /* used in linuxapp for MP and other tests */
69
70 static int
71 no_action(void){ return 0; }
72
73 static int
74 do_recursive_call(void)
75 {
76         unsigned i;
77         struct {
78                 const char *env_var;
79                 int (*action_fn)(void);
80         } actions[] =  {
81                         { "run_secondary_instances", test_mp_secondary },
82                         { "test_missing_c_flag", no_action },
83                         { "test_master_lcore_flag", no_action },
84                         { "test_invalid_n_flag", no_action },
85                         { "test_no_hpet_flag", no_action },
86                         { "test_whitelist_flag", no_action },
87                         { "test_invalid_b_flag", no_action },
88                         { "test_invalid_vdev_flag", no_action },
89                         { "test_invalid_r_flag", no_action },
90 #ifdef RTE_LIBRTE_XEN_DOM0
91                         { "test_dom0_misc_flags", no_action },
92 #else
93                         { "test_misc_flags", no_action },
94 #endif
95                         { "test_memory_flags", no_action },
96                         { "test_file_prefix", no_action },
97                         { "test_no_huge_flag", no_action },
98         };
99
100         if (recursive_call == NULL)
101                 return -1;
102         for (i = 0; i < sizeof(actions)/sizeof(actions[0]); i++) {
103                 if (strcmp(actions[i].env_var, recursive_call) == 0)
104                         return (actions[i].action_fn)();
105         }
106         printf("ERROR - missing action to take for %s\n", recursive_call);
107         return -1;
108 }
109
110 int
111 main(int argc, char **argv)
112 {
113 #ifdef RTE_LIBRTE_CMDLINE
114         struct cmdline *cl;
115 #endif
116         int ret;
117
118         ret = rte_eal_init(argc, argv);
119         if (ret < 0)
120                 return -1;
121
122 #ifdef RTE_LIBRTE_TIMER
123         rte_timer_subsystem_init();
124 #endif
125
126         if (commands_init() < 0)
127                 return -1;
128
129         argv += ret;
130
131         prgname = argv[0];
132
133         if ((recursive_call = getenv(RECURSIVE_ENV_VAR)) != NULL)
134                 return do_recursive_call();
135
136 #ifdef RTE_LIBEAL_USE_HPET
137         if (rte_eal_hpet_init(1) < 0)
138 #endif
139                 RTE_LOG(INFO, APP,
140                                 "HPET is not enabled, using TSC as default timer\n");
141
142
143 #ifdef RTE_LIBRTE_CMDLINE
144         cl = cmdline_stdin_new(main_ctx, "RTE>>");
145         if (cl == NULL) {
146                 return -1;
147         }
148         cmdline_interact(cl);
149         cmdline_stdin_exit(cl);
150 #endif
151
152         return 0;
153 }
154
155
156 int
157 unit_test_suite_runner(struct unit_test_suite *suite)
158 {
159         int test_success;
160         unsigned total = 0, executed = 0, skipped = 0, succeeded = 0, failed = 0;
161
162         if (suite->suite_name) {
163                 printf(" + ------------------------------------------------------- +\n");
164                 printf(" + Test Suite : %s\n", suite->suite_name);
165         }
166
167         if (suite->setup)
168                 if (suite->setup() != 0) {
169                         /*
170                          * setup failed, so count all enabled tests and mark
171                          * them as failed
172                          */
173                         while (suite->unit_test_cases[total].testcase) {
174                                 if (!suite->unit_test_cases[total].enabled)
175                                         skipped++;
176                                 else
177                                         failed++;
178                                 total++;
179                         }
180                         goto suite_summary;
181                 }
182
183         printf(" + ------------------------------------------------------- +\n");
184
185         while (suite->unit_test_cases[total].testcase) {
186                 if (!suite->unit_test_cases[total].enabled) {
187                         skipped++;
188                         total++;
189                         continue;
190                 } else {
191                         executed++;
192                 }
193
194                 /* run test case setup */
195                 if (suite->unit_test_cases[total].setup)
196                         test_success = suite->unit_test_cases[total].setup();
197                 else
198                         test_success = TEST_SUCCESS;
199
200                 if (test_success == TEST_SUCCESS) {
201                         /* run the test case */
202                         test_success = suite->unit_test_cases[total].testcase();
203                         if (test_success == TEST_SUCCESS)
204                                 succeeded++;
205                         else
206                                 failed++;
207                 } else {
208                         failed++;
209                 }
210
211                 /* run the test case teardown */
212                 if (suite->unit_test_cases[total].teardown)
213                         suite->unit_test_cases[total].teardown();
214
215                 if (test_success == TEST_SUCCESS)
216                         printf(" + TestCase [%2d] : %s\n", total,
217                                         suite->unit_test_cases[total].success_msg ?
218                                         suite->unit_test_cases[total].success_msg :
219                                         "passed");
220                 else
221                         printf(" + TestCase [%2d] : %s\n", total,
222                                         suite->unit_test_cases[total].fail_msg ?
223                                         suite->unit_test_cases[total].fail_msg :
224                                         "failed");
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 Passed :      %2d\n", succeeded);
242         printf(" + Tests Failed :      %2d\n", failed);
243         printf(" + ------------------------------------------------------- +\n");
244
245         if (failed)
246                 return -1;
247
248         return 0;
249 }