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