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