]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/cli/test_commands.c
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[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 .name = "bgp",
53 .node = BGP_NODE,
54 .parent_node = CONFIG_NODE,
55 .prompt = "%s(config-router)# ",
56 };
57
58 static struct cmd_node rip_node = {
59 .name = "rip",
60 .node = RIP_NODE,
61 .parent_node = CONFIG_NODE,
62 .prompt = "%s(config-router)# ",
63 };
64
65 static struct cmd_node isis_node = {
66 .name = "isis",
67 .node = ISIS_NODE,
68 .parent_node = CONFIG_NODE,
69 .prompt = "%s(config-router)# ",
70 };
71
72 static struct cmd_node interface_node = {
73 .name = "interface",
74 .node = INTERFACE_NODE,
75 .parent_node = CONFIG_NODE,
76 .prompt = "%s(config-if)# ",
77 };
78
79 static struct cmd_node rmap_node = {
80 .name = "routemap",
81 .node = RMAP_NODE,
82 .parent_node = CONFIG_NODE,
83 .prompt = "%s(config-route-map)# ",
84 };
85
86 static struct cmd_node zebra_node = {
87 .name = "zebra",
88 .node = ZEBRA_NODE,
89 .parent_node = CONFIG_NODE,
90 .prompt = "%s(config-router)# ",
91 };
92
93 static struct cmd_node bgp_vpnv4_node = {
94 .name = "bgp vpnv4",
95 .node = BGP_VPNV4_NODE,
96 .parent_node = BGP_NODE,
97 .prompt = "%s(config-router-af)# ",
98 };
99
100 static struct cmd_node bgp_ipv4_node = {
101 .name = "bgp ipv4 unicast",
102 .node = BGP_IPV4_NODE,
103 .parent_node = BGP_NODE,
104 .prompt = "%s(config-router-af)# ",
105 };
106
107 static struct cmd_node bgp_ipv4m_node = {
108 .name = "bgp ipv4 multicast",
109 .node = BGP_IPV4M_NODE,
110 .parent_node = BGP_NODE,
111 .prompt = "%s(config-router-af)# ",
112 };
113
114 static struct cmd_node bgp_ipv6_node = {
115 .name = "bgp ipv6",
116 .node = BGP_IPV6_NODE,
117 .parent_node = BGP_NODE,
118 .prompt = "%s(config-router-af)# ",
119 };
120
121 static struct cmd_node bgp_ipv6m_node = {
122 .name = "bgp ipv6 multicast",
123 .node = BGP_IPV6M_NODE,
124 .parent_node = BGP_NODE,
125 .prompt = "%s(config-router-af)# ",
126 };
127
128 static struct cmd_node ospf_node = {
129 .name = "ospf",
130 .node = OSPF_NODE,
131 .parent_node = CONFIG_NODE,
132 .prompt = "%s(config-router)# ",
133 };
134
135 static struct cmd_node ripng_node = {
136 .name = "ripng",
137 .node = RIPNG_NODE,
138 .parent_node = CONFIG_NODE,
139 .prompt = "%s(config-router)# ",
140 };
141
142 static struct cmd_node ospf6_node = {
143 .name = "ospf6",
144 .node = OSPF6_NODE,
145 .parent_node = CONFIG_NODE,
146 .prompt = "%s(config-ospf6)# ",
147 };
148
149 static struct cmd_node keychain_node = {
150 .name = "keychain",
151 .node = KEYCHAIN_NODE,
152 .parent_node = CONFIG_NODE,
153 .prompt = "%s(config-keychain)# ",
154 };
155
156 static struct cmd_node keychain_key_node = {
157 .name = "keychain key",
158 .node = KEYCHAIN_KEY_NODE,
159 .parent_node = KEYCHAIN_NODE,
160 .prompt = "%s(config-keychain-key)# ",
161 };
162
163 static int test_callback(const struct cmd_element *cmd, struct vty *vty,
164 int argc, struct cmd_token *argv[])
165 {
166 int offset;
167 int rv;
168 int i;
169
170 offset = 0;
171 rv = snprintf(test_buf, sizeof(test_buf), "'%s'", cmd->string);
172 if (rv < 0)
173 abort();
174
175 offset += rv;
176
177 for (i = 0; i < argc; i++) {
178 rv = snprintf(test_buf + offset, sizeof(test_buf) - offset,
179 "%s'%s'", (i == 0) ? ": " : ", ", argv[i]->arg);
180 if (rv < 0)
181 abort();
182 offset += rv;
183 }
184
185 return CMD_SUCCESS;
186 }
187
188 static void test_load(void)
189 {
190 char line[4096];
191
192 test_cmds = vector_init(VECTOR_MIN_SIZE);
193
194 while (fgets(line, sizeof(line), stdin) != NULL) {
195 if (strlen(line))
196 line[strlen(line) - 1] = '\0';
197 if (line[0] == '#')
198 continue;
199 vector_set(test_cmds, XSTRDUP(MTYPE_TMP, line));
200 }
201 }
202
203 static void test_init(void)
204 {
205 unsigned int node;
206 unsigned int i;
207 struct cmd_node *cnode;
208 struct cmd_element *cmd;
209
210 cmd_init(1);
211 nb_init(master, NULL, 0, false);
212
213 install_node(&bgp_node);
214 install_node(&rip_node);
215 install_node(&interface_node);
216 install_node(&rmap_node);
217 install_node(&zebra_node);
218 install_node(&bgp_vpnv4_node);
219 install_node(&bgp_ipv4_node);
220 install_node(&bgp_ipv4m_node);
221 install_node(&bgp_ipv6_node);
222 install_node(&bgp_ipv6m_node);
223 install_node(&ospf_node);
224 install_node(&ripng_node);
225 install_node(&ospf6_node);
226 install_node(&keychain_node);
227 install_node(&keychain_key_node);
228 install_node(&isis_node);
229 install_node(&vty_node);
230
231 test_init_cmd();
232
233 for (node = 0; node < vector_active(cmdvec); node++)
234 if ((cnode = vector_slot(cmdvec, node)) != NULL)
235 for (i = 0; i < vector_active(cnode->cmd_vector); i++)
236 if ((cmd = vector_slot(cnode->cmd_vector, i))
237 != NULL) {
238 cmd->daemon = 0;
239 cmd->func = test_callback;
240 }
241 test_load();
242 vty_init_vtysh();
243 }
244
245 static void test_terminate(void)
246 {
247 unsigned int i;
248
249 vty_terminate();
250 for (i = 0; i < vector_active(test_cmds); i++)
251 XFREE(MTYPE_TMP, vector_slot(test_cmds, i));
252 vector_free(test_cmds);
253 cmd_terminate();
254 nb_terminate();
255 yang_terminate();
256 }
257
258 static void test_run(struct prng *prng, struct vty *vty, const char *cmd,
259 unsigned int edit_dist, unsigned int node_index,
260 int verbose)
261 {
262 const char *test_str;
263 vector vline;
264 int ret;
265 unsigned int i;
266 char **completions;
267 unsigned int j;
268 struct cmd_node *cnode;
269 vector descriptions;
270 int appended_null;
271 int no_match;
272
273 test_str = prng_fuzz(
274 prng, cmd,
275 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_:. /",
276 edit_dist);
277 vline = cmd_make_strvec(test_str);
278
279 if (vline == NULL)
280 return;
281
282 appended_null = 0;
283 for (i = 0; i < vector_active(cmdvec); i++)
284 if ((cnode = vector_slot(cmdvec, i)) != NULL) {
285 if (node_index != (unsigned int)-1 && i != node_index)
286 continue;
287
288 if (appended_null) {
289 vector_unset(vline, vector_active(vline) - 1);
290 appended_null = 0;
291 }
292 vty->node = cnode->node;
293 test_buf[0] = '\0';
294 ret = cmd_execute_command(vline, vty, NULL, 0);
295 no_match = (ret == CMD_ERR_NO_MATCH);
296 if (verbose || !no_match)
297 printf("execute relaxed '%s'@%d: rv==%d%s%s\n",
298 test_str, cnode->node, ret,
299 (test_buf[0] != '\0') ? ", " : "",
300 test_buf);
301
302 vty->node = cnode->node;
303 test_buf[0] = '\0';
304 ret = cmd_execute_command_strict(vline, vty, NULL);
305 if (verbose || !no_match)
306 printf("execute strict '%s'@%d: rv==%d%s%s\n",
307 test_str, cnode->node, ret,
308 (test_buf[0] != '\0') ? ", " : "",
309 test_buf);
310
311 if (isspace((unsigned char)test_str[
312 strlen(test_str) - 1])) {
313 vector_set(vline, NULL);
314 appended_null = 1;
315 }
316
317 vty->node = cnode->node;
318 completions = cmd_complete_command(vline, vty, &ret);
319 if (verbose || !no_match)
320 printf("complete '%s'@%d: rv==%d\n", test_str,
321 cnode->node, ret);
322 if (completions != NULL) {
323 for (j = 0; completions[j] != NULL; j++) {
324 printf(" '%s'\n", completions[j]);
325 XFREE(MTYPE_TMP, completions[j]);
326 }
327 XFREE(MTYPE_TMP, completions);
328 }
329
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", test_str,
334 cnode->node, ret);
335 if (descriptions != NULL) {
336 for (j = 0; j < vector_active(descriptions);
337 j++) {
338 struct cmd_token *ct =
339 vector_slot(descriptions, j);
340 printf(" '%s' '%s'\n", ct->text,
341 ct->desc);
342 }
343 vector_free(descriptions);
344 }
345 }
346 cmd_free_strvec(vline);
347 }
348
349 int main(int argc, char **argv)
350 {
351 int opt;
352 struct prng *prng;
353 struct vty *vty;
354 unsigned int edit_distance;
355 unsigned int max_edit_distance;
356 unsigned int node_index;
357 int verbose;
358 unsigned int test_cmd;
359 unsigned int iteration;
360 unsigned int num_iterations;
361
362 max_edit_distance = 3;
363 node_index = -1;
364 verbose = 0;
365
366 while ((opt = getopt(argc, argv, "e:n:v")) != -1) {
367 switch (opt) {
368 case 'e':
369 max_edit_distance = atoi(optarg);
370 break;
371 case 'n':
372 node_index = atoi(optarg);
373 break;
374 case 'v':
375 verbose++;
376 break;
377 default:
378 fprintf(stderr,
379 "Usage: %s [-e <edit_dist>] [-n <node_idx>] [-v]\n",
380 argv[0]);
381 exit(1);
382 break;
383 }
384 }
385
386 test_init();
387 prng = prng_new(0);
388
389 vty = vty_new();
390 vty->type = VTY_TERM;
391
392 fprintf(stderr, "Progress:\n0/%u", vector_active(test_cmds));
393 for (test_cmd = 0; test_cmd < vector_active(test_cmds); test_cmd++) {
394 for (edit_distance = 0; edit_distance <= max_edit_distance;
395 edit_distance++) {
396 num_iterations = 1 << edit_distance;
397 num_iterations *= num_iterations * num_iterations;
398
399 for (iteration = 0; iteration < num_iterations;
400 iteration++)
401 test_run(prng, vty,
402 vector_slot(test_cmds, test_cmd),
403 edit_distance, node_index, verbose);
404 }
405 fprintf(stderr, "\r%u/%u", test_cmd + 1,
406 vector_active(test_cmds));
407 }
408 fprintf(stderr, "\nDone.\n");
409
410 vty_close(vty);
411 prng_free(prng);
412 test_terminate();
413 return 0;
414 }