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