17da9f76bbf07c2139e6a0cf8a2d03823c97e884
[dpdk.git] / app / test / test_cmdline_lib.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2012 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
35 #include <string.h>
36 #include <stdio.h>
37 #include <stdint.h>
38 #include <stdarg.h>
39 #include <stdlib.h>
40 #include <errno.h>
41 #include <termios.h>
42 #include <ctype.h>
43 #include <sys/queue.h>
44
45 #include <cmdline_vt100.h>
46 #include <cmdline_rdline.h>
47 #include <cmdline_parse.h>
48 #include <cmdline_socket.h>
49 #include <cmdline.h>
50
51 #include "test_cmdline.h"
52
53 /****************************************************************/
54 /* static functions required for some tests */
55 static void
56 valid_buffer(__attribute__((unused))struct rdline *rdl,
57                         __attribute__((unused))const char *buf,
58                         __attribute__((unused)) unsigned int size)
59 {
60 }
61
62 static int
63 complete_buffer(__attribute__((unused)) struct rdline *rdl,
64                         __attribute__((unused)) const char *buf,
65                         __attribute__((unused)) char *dstbuf,
66                         __attribute__((unused)) unsigned int dstsize,
67                         __attribute__((unused)) int *state)
68 {
69         return 0;
70 }
71
72 /****************************************************************/
73
74 static int
75 test_cmdline_parse_fns(void)
76 {
77         struct cmdline cl;
78         int i = 0;
79         char dst[CMDLINE_TEST_BUFSIZE];
80
81         if (cmdline_parse(NULL, "buffer") >= 0)
82                 goto error;
83         if (cmdline_parse(&cl, NULL) >= 0)
84                 goto error;
85
86         if (cmdline_complete(NULL, "buffer", &i, dst, sizeof(dst)) >= 0)
87                 goto error;
88         if (cmdline_complete(&cl, NULL, &i, dst, sizeof(dst)) >= 0)
89                 goto error;
90         if (cmdline_complete(&cl, "buffer", NULL, dst, sizeof(dst)) >= 0)
91                 goto error;
92         if (cmdline_complete(&cl, "buffer", &i, NULL, sizeof(dst)) >= 0)
93                 goto error;
94
95         return 0;
96
97 error:
98         printf("Error: function accepted null parameter!\n");
99         return -1;
100 }
101
102 static int
103 test_cmdline_rdline_fns(void)
104 {
105         struct rdline rdl;
106         rdline_write_char_t *wc = &cmdline_write_char;
107         rdline_validate_t *v = &valid_buffer;
108         rdline_complete_t *c = &complete_buffer;
109
110         if (rdline_init(NULL, wc, v, c) >= 0)
111                 goto error;
112         if (rdline_init(&rdl, NULL, v, c) >= 0)
113                 goto error;
114         if (rdline_init(&rdl, wc, NULL, c) >= 0)
115                 goto error;
116         if (rdline_init(&rdl, wc, v, NULL) >= 0)
117                 goto error;
118         if (rdline_char_in(NULL, 0) >= 0)
119                 goto error;
120         if (rdline_get_buffer(NULL) != NULL)
121                 goto error;
122         if (rdline_add_history(NULL, "history") >= 0)
123                 goto error;
124         if (rdline_add_history(&rdl, NULL) >= 0)
125                 goto error;
126         if (rdline_get_history_item(NULL, 0) != NULL)
127                 goto error;
128
129         /* void functions */
130         rdline_newline(NULL, "prompt");
131         rdline_newline(&rdl, NULL);
132         rdline_stop(NULL);
133         rdline_quit(NULL);
134         rdline_restart(NULL);
135         rdline_redisplay(NULL);
136         rdline_reset(NULL);
137         rdline_clear_history(NULL);
138
139         return 0;
140
141 error:
142         printf("Error: function accepted null parameter!\n");
143         return -1;
144 }
145
146 static int
147 test_cmdline_vt100_fns(void)
148 {
149         if (vt100_parser(NULL, 0) >= 0) {
150                 printf("Error: function accepted null parameter!\n");
151                 return -1;
152         }
153
154         /* void functions */
155         vt100_init(NULL);
156
157         return 0;
158 }
159
160 static int
161 test_cmdline_socket_fns(void)
162 {
163         cmdline_parse_ctx_t ctx;
164
165         if (cmdline_stdin_new(NULL, "prompt") != NULL)
166                 goto error;
167         if (cmdline_stdin_new(&ctx, NULL) != NULL)
168                 goto error;
169         if (cmdline_file_new(NULL, "prompt", "/dev/null") != NULL)
170                 goto error;
171         if (cmdline_file_new(&ctx, NULL, "/dev/null") != NULL)
172                 goto error;
173         if (cmdline_file_new(&ctx, "prompt", NULL) != NULL)
174                 goto error;
175         if (cmdline_file_new(&ctx, "prompt", "-/invalid/~/path") != NULL) {
176                 printf("Error: succeeded in opening invalid file for reading!");
177                 return -1;
178         }
179         if (cmdline_file_new(&ctx, "prompt", "/dev/null") == NULL) {
180                 printf("Error: failed to open /dev/null for reading!");
181                 return -1;
182         }
183
184         /* void functions */
185         cmdline_stdin_exit(NULL);
186
187         return 0;
188 error:
189         printf("Error: function accepted null parameter!\n");
190         return -1;
191 }
192
193 static int
194 test_cmdline_fns(void)
195 {
196         cmdline_parse_ctx_t ctx;
197         struct cmdline cl, *tmp;
198
199         memset(&ctx, 0, sizeof(ctx));
200         tmp = cmdline_new(&ctx, "test", -1, -1);
201         if (tmp == NULL)
202                 goto error;
203
204         if (cmdline_new(NULL, "prompt", 0, 0) != NULL)
205                 goto error;
206         if (cmdline_new(&ctx, NULL, 0, 0) != NULL)
207                 goto error;
208         if (cmdline_in(NULL, "buffer", CMDLINE_TEST_BUFSIZE) >= 0)
209                 goto error;
210         if (cmdline_in(&cl, NULL, CMDLINE_TEST_BUFSIZE) >= 0)
211                 goto error;
212         if (cmdline_write_char(NULL, 0) >= 0)
213                 goto error;
214
215         /* void functions */
216         cmdline_set_prompt(NULL, "prompt");
217         cmdline_free(NULL);
218         cmdline_printf(NULL, "format");
219         /* this should fail as stream handles are invalid */
220         cmdline_printf(tmp, "format");
221         cmdline_interact(NULL);
222         cmdline_quit(NULL);
223
224         /* check if void calls change anything when they should fail */
225         cl = *tmp;
226
227         cmdline_printf(&cl, NULL);
228         if (memcmp(&cl, tmp, sizeof(cl))) goto mismatch;
229         cmdline_set_prompt(&cl, NULL);
230         if (memcmp(&cl, tmp, sizeof(cl))) goto mismatch;
231         cmdline_in(&cl, NULL, CMDLINE_TEST_BUFSIZE);
232         if (memcmp(&cl, tmp, sizeof(cl))) goto mismatch;
233
234         cmdline_free(tmp);
235
236         return 0;
237
238 error:
239         printf("Error: function accepted null parameter!\n");
240         return -1;
241 mismatch:
242         printf("Error: data changed!\n");
243         return -1;
244 }
245
246 /* test library functions. the point of these tests is not so much to test
247  * functions' behaviour as it is to make sure there are no segfaults if
248  * they are called with invalid parameters.
249  */
250 int
251 test_cmdline_lib(void)
252 {
253         if (test_cmdline_parse_fns() < 0)
254                 return -1;
255         if (test_cmdline_rdline_fns() < 0)
256                 return -1;
257         if (test_cmdline_vt100_fns() < 0)
258                 return -1;
259         if (test_cmdline_socket_fns() < 0)
260                 return -1;
261         if (test_cmdline_fns() < 0)
262                 return -1;
263         return 0;
264 }