4 * Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
43 #include <sys/queue.h>
45 #include <cmdline_vt100.h>
46 #include <cmdline_rdline.h>
47 #include <cmdline_parse.h>
48 #include <cmdline_socket.h>
51 #include "test_cmdline.h"
53 /****************************************************************/
54 /* static functions required for some tests */
56 valid_buffer(__attribute__((unused))struct rdline *rdl,
57 __attribute__((unused))const char *buf,
58 __attribute__((unused)) unsigned int size)
63 complete_buffer(__attribute__((unused)) struct rdline *rdl,
64 __attribute__((unused)) const char *buf,
65 __attribute__((unused)) char *dstbuf,
66 __attribute__((unused)) unsigned int dstsize,
67 __attribute__((unused)) int *state)
72 /****************************************************************/
75 test_cmdline_parse_fns(void)
79 char dst[CMDLINE_TEST_BUFSIZE];
81 if (cmdline_parse(NULL, "buffer") >= 0)
83 if (cmdline_parse(&cl, NULL) >= 0)
86 if (cmdline_complete(NULL, "buffer", &i, dst, sizeof(dst)) >= 0)
88 if (cmdline_complete(&cl, NULL, &i, dst, sizeof(dst)) >= 0)
90 if (cmdline_complete(&cl, "buffer", NULL, dst, sizeof(dst)) >= 0)
92 if (cmdline_complete(&cl, "buffer", &i, NULL, sizeof(dst)) >= 0)
98 printf("Error: function accepted null parameter!\n");
103 test_cmdline_rdline_fns(void)
106 rdline_write_char_t *wc = &cmdline_write_char;
107 rdline_validate_t *v = &valid_buffer;
108 rdline_complete_t *c = &complete_buffer;
110 if (rdline_init(NULL, wc, v, c) >= 0)
112 if (rdline_init(&rdl, NULL, v, c) >= 0)
114 if (rdline_init(&rdl, wc, NULL, c) >= 0)
116 if (rdline_init(&rdl, wc, v, NULL) >= 0)
118 if (rdline_char_in(NULL, 0) >= 0)
120 if (rdline_get_buffer(NULL) != NULL)
122 if (rdline_add_history(NULL, "history") >= 0)
124 if (rdline_add_history(&rdl, NULL) >= 0)
126 if (rdline_get_history_item(NULL, 0) != NULL)
130 rdline_newline(NULL, "prompt");
131 rdline_newline(&rdl, NULL);
134 rdline_restart(NULL);
135 rdline_redisplay(NULL);
137 rdline_clear_history(NULL);
142 printf("Error: function accepted null parameter!\n");
147 test_cmdline_vt100_fns(void)
149 if (vt100_parser(NULL, 0) >= 0) {
150 printf("Error: function accepted null parameter!\n");
161 test_cmdline_socket_fns(void)
163 cmdline_parse_ctx_t ctx;
165 if (cmdline_stdin_new(NULL, "prompt") != NULL)
167 if (cmdline_stdin_new(&ctx, NULL) != NULL)
169 if (cmdline_file_new(NULL, "prompt", "/dev/null") != NULL)
171 if (cmdline_file_new(&ctx, NULL, "/dev/null") != NULL)
173 if (cmdline_file_new(&ctx, "prompt", NULL) != NULL)
175 if (cmdline_file_new(&ctx, "prompt", "-/invalid/~/path") != NULL) {
176 printf("Error: succeeded in opening invalid file for reading!");
179 if (cmdline_file_new(&ctx, "prompt", "/dev/null") == NULL) {
180 printf("Error: failed to open /dev/null for reading!");
185 cmdline_stdin_exit(NULL);
189 printf("Error: function accepted null parameter!\n");
194 test_cmdline_fns(void)
196 cmdline_parse_ctx_t ctx;
197 struct cmdline cl, *tmp;
199 memset(&ctx, 0, sizeof(ctx));
200 tmp = cmdline_new(&ctx, "test", -1, -1);
204 if (cmdline_new(NULL, "prompt", 0, 0) != NULL)
206 if (cmdline_new(&ctx, NULL, 0, 0) != NULL)
208 if (cmdline_in(NULL, "buffer", CMDLINE_TEST_BUFSIZE) >= 0)
210 if (cmdline_in(&cl, NULL, CMDLINE_TEST_BUFSIZE) >= 0)
212 if (cmdline_write_char(NULL, 0) >= 0)
216 cmdline_set_prompt(NULL, "prompt");
218 cmdline_printf(NULL, "format");
219 /* this should fail as stream handles are invalid */
220 cmdline_printf(tmp, "format");
221 cmdline_interact(NULL);
224 /* check if void calls change anything when they should fail */
227 cmdline_printf(&cl, NULL);
228 if (memcmp(&cl, tmp, sizeof(cl))) goto mismatch;
229 cmdline_set_prompt(&cl, NULL);
230 if (memcmp(&cl, tmp, sizeof(cl))) goto mismatch;
231 cmdline_in(&cl, NULL, CMDLINE_TEST_BUFSIZE);
232 if (memcmp(&cl, tmp, sizeof(cl))) goto mismatch;
239 printf("Error: function accepted null parameter!\n");
242 printf("Error: data changed!\n");
246 /* test library functions. the point of these tests is not so much to test
247 * functions' behaviour as it is to make sure there are no segfaults if
248 * they are called with invalid parameters.
251 test_cmdline_lib(void)
253 if (test_cmdline_parse_fns() < 0)
255 if (test_cmdline_rdline_fns() < 0)
257 if (test_cmdline_vt100_fns() < 0)
259 if (test_cmdline_socket_fns() < 0)
261 if (test_cmdline_fns() < 0)