2 * Test code for lib/command.c
4 * Copyright (C) 2013 by Open Source Routing.
5 * Copyright (C) 2013 by Internet Systems Consortium, Inc. ("ISC")
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.
11 * The output is currently not validated but only logged. It can
12 * be diffed to find regressions between versions.
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
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.
24 * You should have received a copy of the GNU General Public License
25 * along with Quagga; see the file COPYING. If not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
30 #define REALLY_NEED_PLAIN_GETOPT 1
44 extern struct cmd_node vty_node;
45 extern void test_init_cmd(void); /* provided in test-commands-defun.c */
47 struct thread_master *master; /* dummy for libzebra*/
49 static vector test_cmds;
50 static char test_buf[32768];
52 static struct cmd_node bgp_node =
55 "%s(config-router)# ",
58 static struct cmd_node rip_node =
61 "%s(config-router)# ",
64 static struct cmd_node isis_node =
67 "%s(config-router)# ",
70 static struct cmd_node interface_node =
76 static struct cmd_node rmap_node =
79 "%s(config-route-map)# "
82 static struct cmd_node zebra_node =
88 static struct cmd_node bgp_vpnv4_node =
91 "%s(config-router-af)# "
94 static struct cmd_node bgp_ipv4_node =
97 "%s(config-router-af)# "
100 static struct cmd_node bgp_ipv4m_node =
103 "%s(config-router-af)# "
106 static struct cmd_node bgp_ipv6_node =
109 "%s(config-router-af)# "
112 static struct cmd_node bgp_ipv6m_node =
115 "%s(config-router-af)# "
118 static struct cmd_node ospf_node =
121 "%s(config-router)# "
124 static struct cmd_node ripng_node =
127 "%s(config-router)# "
130 static struct cmd_node ospf6_node =
136 static struct cmd_node babel_node =
142 static struct cmd_node keychain_node =
145 "%s(config-keychain)# "
148 static struct cmd_node keychain_key_node =
151 "%s(config-keychain-key)# "
155 test_callback(struct cmd_element *cmd, struct vty *vty, int argc, const char *argv[])
162 rv = snprintf(test_buf, sizeof(test_buf), "'%s'", cmd->string);
168 for (i = 0; i < argc; i++)
170 rv = snprintf(test_buf + offset, sizeof(test_buf) - offset, "%s'%s'",
171 (i == 0) ? ": " : ", ", argv[i]);
185 test_cmds = vector_init(VECTOR_MIN_SIZE);
187 while (fgets(line, sizeof(line), stdin) != NULL)
190 line[strlen(line) - 1] = '\0';
193 vector_set(test_cmds, XSTRDUP(MTYPE_STRVEC, line));
202 struct cmd_node *cnode;
203 struct cmd_element *cmd;
207 install_node (&bgp_node, NULL);
208 install_node (&rip_node, NULL);
209 install_node (&interface_node, NULL);
210 install_node (&rmap_node, NULL);
211 install_node (&zebra_node, NULL);
212 install_node (&bgp_vpnv4_node, NULL);
213 install_node (&bgp_ipv4_node, NULL);
214 install_node (&bgp_ipv4m_node, NULL);
215 install_node (&bgp_ipv6_node, NULL);
216 install_node (&bgp_ipv6m_node, NULL);
217 install_node (&ospf_node, NULL);
218 install_node (&ripng_node, NULL);
219 install_node (&ospf6_node, NULL);
220 install_node (&babel_node, NULL);
221 install_node (&keychain_node, NULL);
222 install_node (&keychain_key_node, NULL);
223 install_node (&isis_node, NULL);
224 install_node (&vty_node, NULL);
228 for (node = 0; node < vector_active(cmdvec); node++)
229 if ((cnode = vector_slot(cmdvec, node)) != NULL)
230 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
231 if ((cmd = vector_slot(cnode->cmd_vector, i)) != NULL)
234 cmd->func = test_callback;
246 for (i = 0; i < vector_active(test_cmds); i++)
247 XFREE(MTYPE_STRVEC, vector_slot(test_cmds, i));
248 vector_free(test_cmds);
253 test_run(struct prng *prng, struct vty *vty, const char *cmd, unsigned int edit_dist, unsigned int node_index, int verbose)
255 const char *test_str;
261 struct cmd_node *cnode;
266 test_str = prng_fuzz(prng, cmd, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_:. /", edit_dist);
267 vline = cmd_make_strvec(test_str);
273 for (i = 0; i < vector_active(cmdvec); i++)
274 if ((cnode = vector_slot(cmdvec, i)) != NULL)
276 if (node_index != (unsigned int)-1 && i != node_index)
281 vector_unset(vline, vector_active(vline) - 1);
284 vty->node = cnode->node;
286 ret = cmd_execute_command(vline, vty, NULL, 0);
287 no_match = (ret == CMD_ERR_NO_MATCH);
288 if (verbose || !no_match)
289 printf("execute relaxed '%s'@%d: rv==%d%s%s\n",
293 (test_buf[0] != '\0') ? ", " : "",
296 vty->node = cnode->node;
298 ret = cmd_execute_command_strict(vline, vty, NULL);
299 if (verbose || !no_match)
300 printf("execute strict '%s'@%d: rv==%d%s%s\n",
304 (test_buf[0] != '\0') ? ", " : "",
307 if (isspace((int) test_str[strlen(test_str) - 1]))
309 vector_set (vline, NULL);
313 vty->node = cnode->node;
314 completions = cmd_complete_command(vline, vty, &ret);
315 if (verbose || !no_match)
316 printf("complete '%s'@%d: rv==%d\n",
320 if (completions != NULL)
322 for (j = 0; completions[j] != NULL; j++)
324 printf(" '%s'\n", completions[j]);
325 XFREE(MTYPE_TMP, completions[j]);
327 XFREE(MTYPE_VECTOR_INDEX, completions);
330 vty->node = cnode->node;
331 descriptions = cmd_describe_command(vline, vty, &ret);
332 if (verbose || !no_match)
333 printf("describe '%s'@%d: rv==%d\n",
337 if (descriptions != NULL)
339 for (j = 0; j < vector_active(descriptions); j++)
341 struct cmd_token *cmd = vector_slot(descriptions, j);
342 printf(" '%s' '%s'\n", cmd->cmd, cmd->desc);
344 vector_free(descriptions);
347 cmd_free_strvec(vline);
351 main(int argc, char **argv)
356 unsigned int edit_distance;
357 unsigned int max_edit_distance;
358 unsigned int node_index;
360 unsigned int test_cmd;
361 unsigned int iteration;
362 unsigned int num_iterations;
364 max_edit_distance = 3;
368 while ((opt = getopt(argc, argv, "e:n:v")) != -1)
373 max_edit_distance = atoi(optarg);
376 node_index = atoi(optarg);
382 fprintf(stderr, "Usage: %s [-e <edit_dist>] [-n <node_idx>] [-v]\n", argv[0]);
392 vty->type = VTY_TERM;
394 fprintf(stderr, "Progress:\n0/%u", vector_active(test_cmds));
395 for (test_cmd = 0; test_cmd < vector_active(test_cmds); test_cmd++)
397 for (edit_distance = 0;
398 edit_distance <= max_edit_distance;
401 num_iterations = 1 << edit_distance;
402 num_iterations *= num_iterations * num_iterations;
404 for (iteration = 0; iteration < num_iterations; iteration++)
405 test_run(prng, vty, vector_slot(test_cmds, test_cmd), edit_distance, node_index, verbose);
407 fprintf(stderr, "\r%u/%u", test_cmd + 1, vector_active(test_cmds));
409 fprintf(stderr, "\nDone.\n");