blob: e2f40c6a9581f6e7ff406d6e914ba6864fe1ee7c [file] [log] [blame]
Christian Franke8f399b02013-09-30 12:27:50 +00001/*
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
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
27 * 02111-1307, USA.
28 */
29
30#define REALLY_NEED_PLAIN_GETOPT 1
31
32#include <zebra.h>
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <unistd.h>
37
38#include "command.h"
39#include "memory.h"
40#include "vector.h"
41#include "prng.h"
42
43extern vector cmdvec;
44extern struct cmd_node vty_node;
45extern void test_init_cmd(void); /* provided in test-commands-defun.c */
46
47struct thread_master *master; /* dummy for libzebra*/
48
49static vector test_cmds;
50static char test_buf[32768];
51
52static struct cmd_node bgp_node =
53{
54 BGP_NODE,
55 "%s(config-router)# ",
56};
57
58static struct cmd_node rip_node =
59{
60 RIP_NODE,
61 "%s(config-router)# ",
62};
63
64static struct cmd_node isis_node =
65{
66 ISIS_NODE,
67 "%s(config-router)# ",
68};
69
70static struct cmd_node interface_node =
71{
72 INTERFACE_NODE,
73 "%s(config-if)# ",
74};
75
76static struct cmd_node rmap_node =
77{
78 RMAP_NODE,
79 "%s(config-route-map)# "
80};
81
82static struct cmd_node zebra_node =
83{
84 ZEBRA_NODE,
85 "%s(config-router)# "
86};
87
88static struct cmd_node bgp_vpnv4_node =
89{
90 BGP_VPNV4_NODE,
91 "%s(config-router-af)# "
92};
93
94static struct cmd_node bgp_ipv4_node =
95{
96 BGP_IPV4_NODE,
97 "%s(config-router-af)# "
98};
99
100static struct cmd_node bgp_ipv4m_node =
101{
102 BGP_IPV4M_NODE,
103 "%s(config-router-af)# "
104};
105
106static struct cmd_node bgp_ipv6_node =
107{
108 BGP_IPV6_NODE,
109 "%s(config-router-af)# "
110};
111
112static struct cmd_node bgp_ipv6m_node =
113{
114 BGP_IPV6M_NODE,
115 "%s(config-router-af)# "
116};
117
118static struct cmd_node ospf_node =
119{
120 OSPF_NODE,
121 "%s(config-router)# "
122};
123
124static struct cmd_node ripng_node =
125{
126 RIPNG_NODE,
127 "%s(config-router)# "
128};
129
130static struct cmd_node ospf6_node =
131{
132 OSPF6_NODE,
133 "%s(config-ospf6)# "
134};
135
136static struct cmd_node babel_node =
137{
138 BABEL_NODE,
139 "%s(config-babel)# "
140};
141
142static struct cmd_node keychain_node =
143{
144 KEYCHAIN_NODE,
145 "%s(config-keychain)# "
146};
147
148static struct cmd_node keychain_key_node =
149{
150 KEYCHAIN_KEY_NODE,
151 "%s(config-keychain-key)# "
152};
153
154static int
155test_callback(struct cmd_element *cmd, struct vty *vty, int argc, const char *argv[])
156{
157 int offset;
158 int rv;
159 int i;
160
161 offset = 0;
162 rv = snprintf(test_buf, sizeof(test_buf), "'%s'", cmd->string);
163 if (rv < 0)
164 abort();
165
166 offset += rv;
167
168 for (i = 0; i < argc; i++)
169 {
170 rv = snprintf(test_buf + offset, sizeof(test_buf) - offset, "%s'%s'",
171 (i == 0) ? ": " : ", ", argv[i]);
172 if (rv < 0)
173 abort();
174 offset += rv;
175 }
176
177 return CMD_SUCCESS;
178}
179
180static void
181test_load(void)
182{
183 char line[4096];
184
185 test_cmds = vector_init(VECTOR_MIN_SIZE);
186
187 while (fgets(line, sizeof(line), stdin) != NULL)
188 {
189 if (strlen(line))
190 line[strlen(line) - 1] = '\0';
191 if (line[0] == '#')
192 continue;
193 vector_set(test_cmds, XSTRDUP(MTYPE_STRVEC, line));
194 }
195}
196
197static void
198test_init(void)
199{
200 unsigned int node;
201 unsigned int i;
202 struct cmd_node *cnode;
203 struct cmd_element *cmd;
204
205 cmd_init(1);
206
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);
225
226 test_init_cmd();
227
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)
232 {
233 cmd->daemon = 0;
234 cmd->func = test_callback;
235 }
236 sort_node();
237
238 test_load();
239 vty_init_vtysh();
240}
241
242static void
243test_terminate(void)
244{
245 unsigned int i;
246
247 vty_terminate();
248 for (i = 0; i < vector_active(test_cmds); i++)
249 XFREE(MTYPE_STRVEC, vector_slot(test_cmds, i));
250 vector_free(test_cmds);
251 cmd_terminate();
252}
253
254static void
255test_run(struct prng *prng, struct vty *vty, const char *cmd, unsigned int edit_dist, unsigned int node_index, int verbose)
256{
257 const char *test_str;
258 vector vline;
259 int ret;
260 unsigned int i;
261 char **completions;
262 unsigned int j;
263 struct cmd_node *cnode;
264 vector descriptions;
265 int appended_null;
266 int no_match;
267
268 test_str = prng_fuzz(prng, cmd, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_:. /", edit_dist);
269 vline = cmd_make_strvec(test_str);
270
271 if (vline == NULL)
272 return;
273
274 appended_null = 0;
275 for (i = 0; i < vector_active(cmdvec); i++)
276 if ((cnode = vector_slot(cmdvec, i)) != NULL)
277 {
278 if (node_index != (unsigned int)-1 && i != node_index)
279 continue;
280
281 if (appended_null)
282 {
283 vector_unset(vline, vector_active(vline) - 1);
284 appended_null = 0;
285 }
286 vty->node = cnode->node;
287 test_buf[0] = '\0';
288 ret = cmd_execute_command(vline, vty, NULL, 0);
289 no_match = (ret == CMD_ERR_NO_MATCH);
290 if (verbose || !no_match)
291 printf("execute relaxed '%s'@%d: rv==%d%s%s\n",
292 test_str,
293 cnode->node,
294 ret,
295 (test_buf[0] != '\0') ? ", " : "",
296 test_buf);
297
298 vty->node = cnode->node;
299 test_buf[0] = '\0';
300 ret = cmd_execute_command_strict(vline, vty, NULL);
301 if (verbose || !no_match)
302 printf("execute strict '%s'@%d: rv==%d%s%s\n",
303 test_str,
304 cnode->node,
305 ret,
306 (test_buf[0] != '\0') ? ", " : "",
307 test_buf);
308
309 if (isspace((int) test_str[strlen(test_str) - 1]))
310 {
311 vector_set (vline, NULL);
312 appended_null = 1;
313 }
314
315 vty->node = cnode->node;
316 completions = cmd_complete_command(vline, vty, &ret);
317 if (verbose || !no_match)
318 printf("complete '%s'@%d: rv==%d\n",
319 test_str,
320 cnode->node,
321 ret);
322 if (completions != NULL)
323 {
324 for (j = 0; completions[j] != NULL; j++)
325 {
326 printf(" '%s'\n", completions[j]);
327 XFREE(MTYPE_TMP, completions[j]);
328 }
329 XFREE(MTYPE_VECTOR_INDEX, completions);
330 }
331
332 vty->node = cnode->node;
333 descriptions = cmd_describe_command(vline, vty, &ret);
334 if (verbose || !no_match)
335 printf("describe '%s'@%d: rv==%d\n",
336 test_str,
337 cnode->node,
338 ret);
339 if (descriptions != NULL)
340 {
341 for (j = 0; j < vector_active(descriptions); j++)
342 {
343 struct desc *cmd = vector_slot(descriptions, j);
344 printf(" '%s' '%s'\n", cmd->cmd, cmd->str);
345 }
346 vector_free(descriptions);
347 }
348 }
349 cmd_free_strvec(vline);
350}
351
352int
353main(int argc, char **argv)
354{
355 int opt;
356 struct prng *prng;
357 struct vty *vty;
358 unsigned int edit_distance;
359 unsigned int max_edit_distance;
360 unsigned int node_index;
361 int verbose;
362 unsigned int test_cmd;
363 unsigned int iteration;
364 unsigned int num_iterations;
365
366 max_edit_distance = 3;
367 node_index = -1;
368 verbose = 0;
369
370 while ((opt = getopt(argc, argv, "e:n:v")) != -1)
371 {
372 switch (opt)
373 {
374 case 'e':
375 max_edit_distance = atoi(optarg);
376 break;
377 case 'n':
378 node_index = atoi(optarg);
379 break;
380 case 'v':
381 verbose++;
382 break;
383 default:
384 fprintf(stderr, "Usage: %s [-e <edit_dist>] [-n <node_idx>] [-v]\n", argv[0]);
385 exit(1);
386 break;
387 }
388 }
389
390 test_init();
391 prng = prng_new(0);
392
393 vty = vty_new();
394 vty->type = VTY_TERM;
395
396 fprintf(stderr, "Progress:\n0/%u", vector_active(test_cmds));
397 for (test_cmd = 0; test_cmd < vector_active(test_cmds); test_cmd++)
398 {
399 for (edit_distance = 0;
400 edit_distance <= max_edit_distance;
401 edit_distance++)
402 {
403 num_iterations = 1 << edit_distance;
404 num_iterations *= num_iterations * num_iterations;
405
406 for (iteration = 0; iteration < num_iterations; iteration++)
407 test_run(prng, vty, vector_slot(test_cmds, test_cmd), edit_distance, node_index, verbose);
408 }
409 fprintf(stderr, "\r%u/%u", test_cmd + 1, vector_active(test_cmds));
410 }
411 fprintf(stderr, "\nDone.\n");
412
413 vty_close(vty);
414 prng_free(prng);
415 test_terminate();
416 return 0;
417}