]>
git.proxmox.com Git - ceph.git/blob - ceph/src/dpdk/app/test/test_cmdline_lib.c
4 * Copyright(c) 2010-2014 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.
42 #include <sys/queue.h>
44 #include <cmdline_vt100.h>
45 #include <cmdline_rdline.h>
46 #include <cmdline_parse.h>
47 #include <cmdline_socket.h>
50 #include "test_cmdline.h"
52 /****************************************************************/
53 /* static functions required for some tests */
55 valid_buffer(__attribute__((unused
))struct rdline
*rdl
,
56 __attribute__((unused
))const char *buf
,
57 __attribute__((unused
)) unsigned int size
)
62 complete_buffer(__attribute__((unused
)) struct rdline
*rdl
,
63 __attribute__((unused
)) const char *buf
,
64 __attribute__((unused
)) char *dstbuf
,
65 __attribute__((unused
)) unsigned int dstsize
,
66 __attribute__((unused
)) int *state
)
71 /****************************************************************/
74 test_cmdline_parse_fns(void)
78 char dst
[CMDLINE_TEST_BUFSIZE
];
80 if (cmdline_parse(NULL
, "buffer") >= 0)
82 if (cmdline_parse(&cl
, NULL
) >= 0)
85 if (cmdline_complete(NULL
, "buffer", &i
, dst
, sizeof(dst
)) >= 0)
87 if (cmdline_complete(&cl
, NULL
, &i
, dst
, sizeof(dst
)) >= 0)
89 if (cmdline_complete(&cl
, "buffer", NULL
, dst
, sizeof(dst
)) >= 0)
91 if (cmdline_complete(&cl
, "buffer", &i
, NULL
, sizeof(dst
)) >= 0)
97 printf("Error: function accepted null parameter!\n");
102 test_cmdline_rdline_fns(void)
105 rdline_write_char_t
*wc
= &cmdline_write_char
;
106 rdline_validate_t
*v
= &valid_buffer
;
107 rdline_complete_t
*c
= &complete_buffer
;
109 if (rdline_init(NULL
, wc
, v
, c
) >= 0)
111 if (rdline_init(&rdl
, NULL
, v
, c
) >= 0)
113 if (rdline_init(&rdl
, wc
, NULL
, c
) >= 0)
115 if (rdline_init(&rdl
, wc
, v
, NULL
) >= 0)
117 if (rdline_char_in(NULL
, 0) >= 0)
119 if (rdline_get_buffer(NULL
) != NULL
)
121 if (rdline_add_history(NULL
, "history") >= 0)
123 if (rdline_add_history(&rdl
, NULL
) >= 0)
125 if (rdline_get_history_item(NULL
, 0) != NULL
)
129 rdline_newline(NULL
, "prompt");
130 rdline_newline(&rdl
, NULL
);
133 rdline_restart(NULL
);
134 rdline_redisplay(NULL
);
136 rdline_clear_history(NULL
);
141 printf("Error: function accepted null parameter!\n");
146 test_cmdline_vt100_fns(void)
148 if (vt100_parser(NULL
, 0) >= 0) {
149 printf("Error: function accepted null parameter!\n");
160 test_cmdline_socket_fns(void)
162 cmdline_parse_ctx_t ctx
;
164 if (cmdline_stdin_new(NULL
, "prompt") != NULL
)
166 if (cmdline_stdin_new(&ctx
, NULL
) != NULL
)
168 if (cmdline_file_new(NULL
, "prompt", "/dev/null") != NULL
)
170 if (cmdline_file_new(&ctx
, NULL
, "/dev/null") != NULL
)
172 if (cmdline_file_new(&ctx
, "prompt", NULL
) != NULL
)
174 if (cmdline_file_new(&ctx
, "prompt", "-/invalid/~/path") != NULL
) {
175 printf("Error: succeeded in opening invalid file for reading!");
178 if (cmdline_file_new(&ctx
, "prompt", "/dev/null") == NULL
) {
179 printf("Error: failed to open /dev/null for reading!");
184 cmdline_stdin_exit(NULL
);
188 printf("Error: function accepted null parameter!\n");
193 test_cmdline_fns(void)
195 cmdline_parse_ctx_t ctx
;
196 struct cmdline cl
, *tmp
;
198 memset(&ctx
, 0, sizeof(ctx
));
199 tmp
= cmdline_new(&ctx
, "test", -1, -1);
203 if (cmdline_new(NULL
, "prompt", 0, 0) != NULL
)
205 if (cmdline_new(&ctx
, NULL
, 0, 0) != NULL
)
207 if (cmdline_in(NULL
, "buffer", CMDLINE_TEST_BUFSIZE
) >= 0)
209 if (cmdline_in(&cl
, NULL
, CMDLINE_TEST_BUFSIZE
) >= 0)
211 if (cmdline_write_char(NULL
, 0) >= 0)
215 cmdline_set_prompt(NULL
, "prompt");
217 cmdline_printf(NULL
, "format");
218 /* this should fail as stream handles are invalid */
219 cmdline_printf(tmp
, "format");
220 cmdline_interact(NULL
);
223 /* check if void calls change anything when they should fail */
226 cmdline_printf(&cl
, NULL
);
227 if (memcmp(&cl
, tmp
, sizeof(cl
))) goto mismatch
;
228 cmdline_set_prompt(&cl
, NULL
);
229 if (memcmp(&cl
, tmp
, sizeof(cl
))) goto mismatch
;
230 cmdline_in(&cl
, NULL
, CMDLINE_TEST_BUFSIZE
);
231 if (memcmp(&cl
, tmp
, sizeof(cl
))) goto mismatch
;
238 printf("Error: function accepted null parameter!\n");
241 printf("Error: data changed!\n");
245 /* test library functions. the point of these tests is not so much to test
246 * functions' behaviour as it is to make sure there are no segfaults if
247 * they are called with invalid parameters.
250 test_cmdline_lib(void)
252 if (test_cmdline_parse_fns() < 0)
254 if (test_cmdline_rdline_fns() < 0)
256 if (test_cmdline_vt100_fns() < 0)
258 if (test_cmdline_socket_fns() < 0)
260 if (test_cmdline_fns() < 0)