1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
13 #include <sys/queue.h>
15 #include <rte_common.h>
17 #include <cmdline_vt100.h>
18 #include <cmdline_rdline.h>
19 #include <cmdline_parse.h>
20 #include <cmdline_socket.h>
23 #include "test_cmdline.h"
25 /****************************************************************/
26 /* static functions required for some tests */
28 valid_buffer(__rte_unused struct rdline *rdl,
29 __rte_unused const char *buf,
30 __rte_unused unsigned int size)
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)
44 /****************************************************************/
47 test_cmdline_parse_fns(void)
50 cmdline_parse_ctx_t ctx;
52 char dst[CMDLINE_TEST_BUFSIZE];
54 cl = cmdline_new(&ctx, "prompt", -1, -1);
56 printf("Error: cannot create cmdline to test parse fns!\n");
60 if (cmdline_parse(NULL, "buffer") >= 0)
62 if (cmdline_parse(cl, NULL) >= 0)
65 if (cmdline_complete(NULL, "buffer", &i, dst, sizeof(dst)) >= 0)
67 if (cmdline_complete(cl, NULL, &i, dst, sizeof(dst)) >= 0)
69 if (cmdline_complete(cl, "buffer", NULL, dst, sizeof(dst)) >= 0)
71 if (cmdline_complete(cl, "buffer", &i, NULL, sizeof(dst)) >= 0)
78 printf("Error: function accepted null parameter!\n");
84 test_cmdline_rdline_fns(void)
87 rdline_write_char_t *wc = &cmdline_write_char;
88 rdline_validate_t *v = &valid_buffer;
89 rdline_complete_t *c = &complete_buffer;
91 if (rdline_init(NULL, wc, v, c) >= 0)
93 if (rdline_init(&rdl, NULL, v, c) >= 0)
95 if (rdline_init(&rdl, wc, NULL, c) >= 0)
97 if (rdline_init(&rdl, wc, v, NULL) >= 0)
99 if (rdline_char_in(NULL, 0) >= 0)
101 if (rdline_get_buffer(NULL) != NULL)
103 if (rdline_add_history(NULL, "history") >= 0)
105 if (rdline_add_history(&rdl, NULL) >= 0)
107 if (rdline_get_history_item(NULL, 0) != NULL)
111 rdline_newline(NULL, "prompt");
112 rdline_newline(&rdl, NULL);
115 rdline_restart(NULL);
116 rdline_redisplay(NULL);
118 rdline_clear_history(NULL);
123 printf("Error: function accepted null parameter!\n");
128 test_cmdline_vt100_fns(void)
130 if (vt100_parser(NULL, 0) >= 0) {
131 printf("Error: function accepted null parameter!\n");
142 test_cmdline_socket_fns(void)
144 cmdline_parse_ctx_t ctx;
147 cl = cmdline_stdin_new(NULL, "prompt");
150 cl = cmdline_stdin_new(&ctx, NULL);
153 cl = cmdline_file_new(NULL, "prompt", "/dev/null");
156 cl = cmdline_file_new(&ctx, NULL, "/dev/null");
159 cl = cmdline_file_new(&ctx, "prompt", NULL);
162 cl = cmdline_file_new(&ctx, "prompt", "-/invalid/~/path");
164 printf("Error: succeeded in opening invalid file for reading!");
168 cl = cmdline_file_new(&ctx, "prompt", "/dev/null");
170 printf("Error: failed to open /dev/null for reading!");
175 cmdline_stdin_exit(NULL);
180 printf("Error: function accepted null parameter!\n");
186 test_cmdline_fns(void)
188 cmdline_parse_ctx_t ctx;
191 memset(&ctx, 0, sizeof(ctx));
192 cl = cmdline_new(NULL, "prompt", 0, 0);
195 cl = cmdline_new(&ctx, NULL, 0, 0);
198 cl = cmdline_new(&ctx, "test", -1, -1);
201 if (cmdline_in(NULL, "buffer", CMDLINE_TEST_BUFSIZE) >= 0)
203 if (cmdline_in(cl, NULL, CMDLINE_TEST_BUFSIZE) >= 0)
205 if (cmdline_write_char(NULL, 0) >= 0)
209 cmdline_set_prompt(NULL, "prompt");
211 cmdline_printf(NULL, "format");
212 cmdline_interact(NULL);
219 printf("Error: function accepted null parameter!\n");
225 /* test library functions. the point of these tests is not so much to test
226 * functions' behaviour as it is to make sure there are no segfaults if
227 * they are called with invalid parameters.
230 test_cmdline_lib(void)
232 if (test_cmdline_parse_fns() < 0)
234 if (test_cmdline_rdline_fns() < 0)
236 if (test_cmdline_vt100_fns() < 0)
238 if (test_cmdline_socket_fns() < 0)
240 if (test_cmdline_fns() < 0)