]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/cli/test_commands.c
Merge pull request #2298 from qlyoung/pipe-actions-vtysh
[mirror_frr.git] / tests / lib / cli / test_commands.c
1 /*
2 * Test code for lib/command.c
3 *
4 * Copyright (C) 2013 by Open Source Routing.
5 * Copyright (C) 2013 by Internet Systems Consortium, Inc. ("ISC")
6 *
7 * This program reads in a list of commandlines from stdin
8 * and calls all the public functions of lib/command.c for
9 * both the given command lines and fuzzed versions thereof.
10 *
11 * The output is currently not validated but only logged. It can
12 * be diffed to find regressions between versions.
13 *
14 * Quagga is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2, or (at your option) any
17 * later version.
18 *
19 * Quagga is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; see the file COPYING; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29 #define REALLY_NEED_PLAIN_GETOPT 1
30
31 #include <zebra.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36
37 #include "command.h"
38 #include "memory.h"
39 #include "vector.h"
40 #include "prng.h"
41
42 extern vector cmdvec;
43 extern struct cmd_node vty_node;
44 extern void test_init_cmd(void); /* provided in test-commands-defun.c */
45
46 struct thread_master *master; /* dummy for libfrr*/
47
48 static vector test_cmds;
49 static char test_buf[32768];
50
51 static struct cmd_node bgp_node = {
52 BGP_NODE, "%s(config-router)# ",
53 };
54
55 static struct cmd_node rip_node = {
56 RIP_NODE, "%s(config-router)# ",
57 };
58
59 static struct cmd_node isis_node = {
60 ISIS_NODE, "%s(config-router)# ",
61 };
62
63 static struct cmd_node interface_node = {
64 INTERFACE_NODE, "%s(config-if)# ",
65 };
66
67 static struct cmd_node rmap_node = {RMAP_NODE, "%s(config-route-map)# "};
68
69 static struct cmd_node zebra_node = {ZEBRA_NODE, "%s(config-router)# "};
70
71 static struct cmd_node bgp_vpnv4_node = {BGP_VPNV4_NODE,
72 "%s(config-router-af)# "};
73
74 static struct cmd_node bgp_ipv4_node = {BGP_IPV4_NODE,
75 "%s(config-router-af)# "};
76
77 static struct cmd_node bgp_ipv4m_node = {BGP_IPV4M_NODE,
78 "%s(config-router-af)# "};
79
80 static struct cmd_node bgp_ipv6_node = {BGP_IPV6_NODE,
81 "%s(config-router-af)# "};
82
83 static struct cmd_node bgp_ipv6m_node = {BGP_IPV6M_NODE,
84 "%s(config-router-af)# "};
85
86 static struct cmd_node ospf_node = {OSPF_NODE, "%s(config-router)# "};
87
88 static struct cmd_node ripng_node = {RIPNG_NODE, "%s(config-router)# "};
89
90 static struct cmd_node ospf6_node = {OSPF6_NODE, "%s(config-ospf6)# "};
91
92 static struct cmd_node keychain_node = {KEYCHAIN_NODE, "%s(config-keychain)# "};
93
94 static struct cmd_node keychain_key_node = {KEYCHAIN_KEY_NODE,
95 "%s(config-keychain-key)# "};
96
97 static int test_callback(const struct cmd_element *cmd, struct vty *vty,
98 int argc, struct cmd_token *argv[])
99 {
100 int offset;
101 int rv;
102 int i;
103
104 offset = 0;
105 rv = snprintf(test_buf, sizeof(test_buf), "'%s'", cmd->string);
106 if (rv < 0)
107 abort();
108
109 offset += rv;
110
111 for (i = 0; i < argc; i++) {
112 rv = snprintf(test_buf + offset, sizeof(test_buf) - offset,
113 "%s'%s'", (i == 0) ? ": " : ", ", argv[i]->arg);
114 if (rv < 0)
115 abort();
116 offset += rv;
117 }
118
119 return CMD_SUCCESS;
120 }
121
122 static void test_load(void)
123 {
124 char line[4096];
125
126 test_cmds = vector_init(VECTOR_MIN_SIZE);
127
128 while (fgets(line, sizeof(line), stdin) != NULL) {
129 if (strlen(line))
130 line[strlen(line) - 1] = '\0';
131 if (line[0] == '#')
132 continue;
133 vector_set(test_cmds, XSTRDUP(MTYPE_TMP, line));
134 }
135 }
136
137 static void test_init(void)
138 {
139 unsigned int node;
140 unsigned int i;
141 struct cmd_node *cnode;
142 struct cmd_element *cmd;
143
144 cmd_init(1);
145
146 install_node(&bgp_node, NULL);
147 install_node(&rip_node, NULL);
148 install_node(&interface_node, NULL);
149 install_node(&rmap_node, NULL);
150 install_node(&zebra_node, NULL);
151 install_node(&bgp_vpnv4_node, NULL);
152 install_node(&bgp_ipv4_node, NULL);
153 install_node(&bgp_ipv4m_node, NULL);
154 install_node(&bgp_ipv6_node, NULL);
155 install_node(&bgp_ipv6m_node, NULL);
156 install_node(&ospf_node, NULL);
157 install_node(&ripng_node, NULL);
158 install_node(&ospf6_node, NULL);
159 install_node(&keychain_node, NULL);
160 install_node(&keychain_key_node, NULL);
161 install_node(&isis_node, NULL);
162 install_node(&vty_node, NULL);
163
164 test_init_cmd();
165
166 for (node = 0; node < vector_active(cmdvec); node++)
167 if ((cnode = vector_slot(cmdvec, node)) != NULL)
168 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
169 if ((cmd = vector_slot(cnode->cmd_vector, i))
170 != NULL) {
171 cmd->daemon = 0;
172 cmd->func = test_callback;
173 }
174 test_load();
175 vty_init_vtysh();
176 }
177
178 static void test_terminate(void)
179 {
180 unsigned int i;
181
182 vty_terminate();
183 for (i = 0; i < vector_active(test_cmds); i++)
184 XFREE(MTYPE_TMP, vector_slot(test_cmds, i));
185 vector_free(test_cmds);
186 cmd_terminate();
187 }
188
189 static void test_run(struct prng *prng, struct vty *vty, const char *cmd,
190 unsigned int edit_dist, unsigned int node_index,
191 int verbose)
192 {
193 const char *test_str;
194 vector vline;
195 int ret;
196 unsigned int i;
197 char **completions;
198 unsigned int j;
199 struct cmd_node *cnode;
200 vector descriptions;
201 int appended_null;
202 int no_match;
203
204 test_str = prng_fuzz(
205 prng, cmd,
206 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_:. /",
207 edit_dist);
208 vline = cmd_make_strvec(test_str);
209
210 if (vline == NULL)
211 return;
212
213 appended_null = 0;
214 for (i = 0; i < vector_active(cmdvec); i++)
215 if ((cnode = vector_slot(cmdvec, i)) != NULL) {
216 if (node_index != (unsigned int)-1 && i != node_index)
217 continue;
218
219 if (appended_null) {
220 vector_unset(vline, vector_active(vline) - 1);
221 appended_null = 0;
222 }
223 vty->node = cnode->node;
224 test_buf[0] = '\0';
225 ret = cmd_execute_command(vline, vty, NULL, 0);
226 no_match = (ret == CMD_ERR_NO_MATCH);
227 if (verbose || !no_match)
228 printf("execute relaxed '%s'@%d: rv==%d%s%s\n",
229 test_str, cnode->node, ret,
230 (test_buf[0] != '\0') ? ", " : "",
231 test_buf);
232
233 vty->node = cnode->node;
234 test_buf[0] = '\0';
235 ret = cmd_execute_command_strict(vline, vty, NULL);
236 if (verbose || !no_match)
237 printf("execute strict '%s'@%d: rv==%d%s%s\n",
238 test_str, cnode->node, ret,
239 (test_buf[0] != '\0') ? ", " : "",
240 test_buf);
241
242 if (isspace((int)test_str[strlen(test_str) - 1])) {
243 vector_set(vline, NULL);
244 appended_null = 1;
245 }
246
247 vty->node = cnode->node;
248 completions = cmd_complete_command(vline, vty, &ret);
249 if (verbose || !no_match)
250 printf("complete '%s'@%d: rv==%d\n", test_str,
251 cnode->node, ret);
252 if (completions != NULL) {
253 for (j = 0; completions[j] != NULL; j++) {
254 printf(" '%s'\n", completions[j]);
255 XFREE(MTYPE_TMP, completions[j]);
256 }
257 XFREE(MTYPE_TMP, completions);
258 }
259
260 vty->node = cnode->node;
261 descriptions = cmd_describe_command(vline, vty, &ret);
262 if (verbose || !no_match)
263 printf("describe '%s'@%d: rv==%d\n", test_str,
264 cnode->node, ret);
265 if (descriptions != NULL) {
266 for (j = 0; j < vector_active(descriptions);
267 j++) {
268 struct cmd_token *cmd =
269 vector_slot(descriptions, j);
270 printf(" '%s' '%s'\n", cmd->text,
271 cmd->desc);
272 }
273 vector_free(descriptions);
274 }
275 }
276 cmd_free_strvec(vline);
277 }
278
279 int main(int argc, char **argv)
280 {
281 int opt;
282 struct prng *prng;
283 struct vty *vty;
284 unsigned int edit_distance;
285 unsigned int max_edit_distance;
286 unsigned int node_index;
287 int verbose;
288 unsigned int test_cmd;
289 unsigned int iteration;
290 unsigned int num_iterations;
291
292 max_edit_distance = 3;
293 node_index = -1;
294 verbose = 0;
295
296 while ((opt = getopt(argc, argv, "e:n:v")) != -1) {
297 switch (opt) {
298 case 'e':
299 max_edit_distance = atoi(optarg);
300 break;
301 case 'n':
302 node_index = atoi(optarg);
303 break;
304 case 'v':
305 verbose++;
306 break;
307 default:
308 fprintf(stderr,
309 "Usage: %s [-e <edit_dist>] [-n <node_idx>] [-v]\n",
310 argv[0]);
311 exit(1);
312 break;
313 }
314 }
315
316 test_init();
317 prng = prng_new(0);
318
319 vty = vty_new();
320 vty->type = VTY_TERM;
321
322 fprintf(stderr, "Progress:\n0/%u", vector_active(test_cmds));
323 for (test_cmd = 0; test_cmd < vector_active(test_cmds); test_cmd++) {
324 for (edit_distance = 0; edit_distance <= max_edit_distance;
325 edit_distance++) {
326 num_iterations = 1 << edit_distance;
327 num_iterations *= num_iterations * num_iterations;
328
329 for (iteration = 0; iteration < num_iterations;
330 iteration++)
331 test_run(prng, vty,
332 vector_slot(test_cmds, test_cmd),
333 edit_distance, node_index, verbose);
334 }
335 fprintf(stderr, "\r%u/%u", test_cmd + 1,
336 vector_active(test_cmds));
337 }
338 fprintf(stderr, "\nDone.\n");
339
340 vty_close(vty);
341 prng_free(prng);
342 test_terminate();
343 return 0;
344 }