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