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