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