1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Test code for lib/command.c
*
* Copyright (C) 2013 by Open Source Routing.
* Copyright (C) 2013 by Internet Systems Consortium, Inc. ("ISC")
*
* This program reads in a list of commandlines from stdin
* and calls all the public functions of lib/command.c for
* both the given command lines and fuzzed versions thereof.
*
* The output is currently not validated but only logged. It can
* be diffed to find regressions between versions.
*/
#define REALLY_NEED_PLAIN_GETOPT 1
#include <zebra.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "debug.h"
#include "command.h"
#include "memory.h"
#include "vector.h"
#include "prng.h"
extern vector cmdvec;
extern struct cmd_node vty_node;
extern void test_init_cmd(void); /* provided in test-commands-defun.c */
struct event_loop *master; /* dummy for libfrr*/
static vector test_cmds;
static char test_buf[32768];
static struct cmd_node bgp_node = {
.name = "bgp",
.node = BGP_NODE,
.parent_node = CONFIG_NODE,
.prompt = "%s(config-router)# ",
};
static struct cmd_node rip_node = {
.name = "rip",
.node = RIP_NODE,
.parent_node = CONFIG_NODE,
.prompt = "%s(config-router)# ",
};
static struct cmd_node isis_node = {
.name = "isis",
.node = ISIS_NODE,
.parent_node = CONFIG_NODE,
.prompt = "%s(config-router)# ",
};
static struct cmd_node interface_node = {
.name = "interface",
.node = INTERFACE_NODE,
.parent_node = CONFIG_NODE,
.prompt = "%s(config-if)# ",
};
static struct cmd_node rmap_node = {
.name = "routemap",
.node = RMAP_NODE,
.parent_node = CONFIG_NODE,
.prompt = "%s(config-route-map)# ",
};
static struct cmd_node zebra_node = {
.name = "zebra",
.node = ZEBRA_NODE,
.parent_node = CONFIG_NODE,
.prompt = "%s(config-router)# ",
};
static struct cmd_node bgp_vpnv4_node = {
.name = "bgp vpnv4",
.node = BGP_VPNV4_NODE,
.parent_node = BGP_NODE,
.prompt = "%s(config-router-af)# ",
};
static struct cmd_node bgp_ipv4_node = {
.name = "bgp ipv4 unicast",
.node = BGP_IPV4_NODE,
.parent_node = BGP_NODE,
.prompt = "%s(config-router-af)# ",
};
static struct cmd_node bgp_ipv4m_node = {
.name = "bgp ipv4 multicast",
.node = BGP_IPV4M_NODE,
.parent_node = BGP_NODE,
.prompt = "%s(config-router-af)# ",
};
static struct cmd_node bgp_ipv6_node = {
.name = "bgp ipv6",
.node = BGP_IPV6_NODE,
.parent_node = BGP_NODE,
.prompt = "%s(config-router-af)# ",
};
static struct cmd_node bgp_ipv6m_node = {
.name = "bgp ipv6 multicast",
.node = BGP_IPV6M_NODE,
.parent_node = BGP_NODE,
.prompt = "%s(config-router-af)# ",
};
static struct cmd_node ospf_node = {
.name = "ospf",
.node = OSPF_NODE,
.parent_node = CONFIG_NODE,
.prompt = "%s(config-router)# ",
};
static struct cmd_node ripng_node = {
.name = "ripng",
.node = RIPNG_NODE,
.parent_node = CONFIG_NODE,
.prompt = "%s(config-router)# ",
};
static struct cmd_node ospf6_node = {
.name = "ospf6",
.node = OSPF6_NODE,
.parent_node = CONFIG_NODE,
.prompt = "%s(config-ospf6)# ",
};
static struct cmd_node keychain_node = {
.name = "keychain",
.node = KEYCHAIN_NODE,
.parent_node = CONFIG_NODE,
.prompt = "%s(config-keychain)# ",
};
static struct cmd_node keychain_key_node = {
.name = "keychain key",
.node = KEYCHAIN_KEY_NODE,
.parent_node = KEYCHAIN_NODE,
.prompt = "%s(config-keychain-key)# ",
};
static int test_callback(const struct cmd_element *cmd, struct vty *vty,
int argc, struct cmd_token *argv[])
{
int offset;
int rv;
int i;
offset = 0;
rv = snprintf(test_buf, sizeof(test_buf), "'%s'", cmd->string);
if (rv < 0)
abort();
offset += rv;
for (i = 0; i < argc; i++) {
rv = snprintf(test_buf + offset, sizeof(test_buf) - offset,
"%s'%s'", (i == 0) ? ": " : ", ", argv[i]->arg);
if (rv < 0)
abort();
offset += rv;
}
return CMD_SUCCESS;
}
static void test_load(void)
{
char line[4096];
test_cmds = vector_init(VECTOR_MIN_SIZE);
while (fgets(line, sizeof(line), stdin) != NULL) {
if (strlen(line))
line[strlen(line) - 1] = '\0';
if (line[0] == '#')
continue;
vector_set(test_cmds, XSTRDUP(MTYPE_TMP, line));
}
}
static void test_init(void)
{
unsigned int node;
unsigned int i;
struct cmd_node *cnode;
struct cmd_element *cmd;
cmd_init(1);
debug_init();
nb_init(master, NULL, 0, false, false);
install_node(&bgp_node);
install_node(&rip_node);
install_node(&interface_node);
install_node(&rmap_node);
install_node(&zebra_node);
install_node(&bgp_vpnv4_node);
install_node(&bgp_ipv4_node);
install_node(&bgp_ipv4m_node);
install_node(&bgp_ipv6_node);
install_node(&bgp_ipv6m_node);
install_node(&ospf_node);
install_node(&ripng_node);
install_node(&ospf6_node);
install_node(&keychain_node);
install_node(&keychain_key_node);
install_node(&isis_node);
install_node(&vty_node);
test_init_cmd();
for (node = 0; node < vector_active(cmdvec); node++)
if ((cnode = vector_slot(cmdvec, node)) != NULL)
for (i = 0; i < vector_active(cnode->cmd_vector); i++)
if ((cmd = vector_slot(cnode->cmd_vector, i))
!= NULL) {
cmd->daemon = 0;
cmd->func = test_callback;
}
test_load();
vty_init_vtysh();
}
static void test_terminate(void)
{
unsigned int i;
vty_terminate();
for (i = 0; i < vector_active(test_cmds); i++)
XFREE(MTYPE_TMP, vector_slot(test_cmds, i));
vector_free(test_cmds);
cmd_terminate();
nb_terminate();
yang_terminate();
}
static void test_run(struct prng *prng, struct vty *vty, const char *cmd,
unsigned int edit_dist, unsigned int node_index,
int verbose)
{
const char *test_str;
vector vline;
int ret;
unsigned int i;
char **completions;
unsigned int j;
struct cmd_node *cnode;
vector descriptions;
int appended_null;
int no_match;
test_str = prng_fuzz(
prng, cmd,
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_:. /",
edit_dist);
vline = cmd_make_strvec(test_str);
if (vline == NULL)
return;
appended_null = 0;
for (i = 0; i < vector_active(cmdvec); i++)
if ((cnode = vector_slot(cmdvec, i)) != NULL) {
if (node_index != (unsigned int)-1 && i != node_index)
continue;
if (appended_null) {
vector_unset(vline, vector_active(vline) - 1);
appended_null = 0;
}
vty->node = cnode->node;
test_buf[0] = '\0';
ret = cmd_execute_command(vline, vty, NULL, 0);
no_match = (ret == CMD_ERR_NO_MATCH);
if (verbose || !no_match)
printf("execute relaxed '%s'@%d: rv==%d%s%s\n",
test_str, cnode->node, ret,
(test_buf[0] != '\0') ? ", " : "",
test_buf);
vty->node = cnode->node;
test_buf[0] = '\0';
ret = cmd_execute_command_strict(vline, vty, NULL);
if (verbose || !no_match)
printf("execute strict '%s'@%d: rv==%d%s%s\n",
test_str, cnode->node, ret,
(test_buf[0] != '\0') ? ", " : "",
test_buf);
if (isspace((unsigned char)test_str[
strlen(test_str) - 1])) {
vector_set(vline, NULL);
appended_null = 1;
}
vty->node = cnode->node;
completions = cmd_complete_command(vline, vty, &ret);
if (verbose || !no_match)
printf("complete '%s'@%d: rv==%d\n", test_str,
cnode->node, ret);
if (completions != NULL) {
for (j = 0; completions[j] != NULL; j++) {
printf(" '%s'\n", completions[j]);
XFREE(MTYPE_TMP, completions[j]);
}
XFREE(MTYPE_TMP, completions);
}
vty->node = cnode->node;
descriptions = cmd_describe_command(vline, vty, &ret);
if (verbose || !no_match)
printf("describe '%s'@%d: rv==%d\n", test_str,
cnode->node, ret);
if (descriptions != NULL) {
for (j = 0; j < vector_active(descriptions);
j++) {
struct cmd_token *ct =
vector_slot(descriptions, j);
printf(" '%s' '%s'\n", ct->text,
ct->desc);
}
vector_free(descriptions);
}
}
cmd_free_strvec(vline);
}
int main(int argc, char **argv)
{
int opt;
struct prng *prng;
struct vty *vty;
unsigned int edit_distance;
unsigned int max_edit_distance;
unsigned int node_index;
int verbose;
unsigned int test_cmd;
unsigned int iteration;
unsigned int num_iterations;
max_edit_distance = 3;
node_index = -1;
verbose = 0;
while ((opt = getopt(argc, argv, "e:n:v")) != -1) {
switch (opt) {
case 'e':
max_edit_distance = atoi(optarg);
break;
case 'n':
node_index = atoi(optarg);
break;
case 'v':
verbose++;
break;
default:
fprintf(stderr,
"Usage: %s [-e <edit_dist>] [-n <node_idx>] [-v]\n",
argv[0]);
exit(1);
break;
}
}
test_init();
prng = prng_new(0);
vty = vty_new();
vty->type = VTY_TERM;
fprintf(stderr, "Progress:\n0/%u", vector_active(test_cmds));
for (test_cmd = 0; test_cmd < vector_active(test_cmds); test_cmd++) {
for (edit_distance = 0; edit_distance <= max_edit_distance;
edit_distance++) {
num_iterations = 1 << edit_distance;
num_iterations *= num_iterations * num_iterations;
for (iteration = 0; iteration < num_iterations;
iteration++)
test_run(prng, vty,
vector_slot(test_cmds, test_cmd),
edit_distance, node_index, verbose);
}
fprintf(stderr, "\r%u/%u", test_cmd + 1,
vector_active(test_cmds));
}
fprintf(stderr, "\nDone.\n");
vty_close(vty);
prng_free(prng);
test_terminate();
return 0;
}
|