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