File: test-commands.c

package info (click to toggle)
quagga 0.99.23.1-1
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 14,748 kB
  • sloc: ansic: 215,042; sh: 12,693; perl: 805; makefile: 644; exp: 184; lisp: 62; awk: 30
file content (415 lines) | stat: -rw-r--r-- 9,871 bytes parent folder | download | duplicates (3)
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
404
405
406
407
408
409
410
411
412
413
414
415
/*
 * 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.
 *
 * Quagga is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * Quagga is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Quagga; see the file COPYING.  If not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#define REALLY_NEED_PLAIN_GETOPT 1

#include <zebra.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.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 thread_master *master; /* dummy for libzebra*/

static vector test_cmds;
static char test_buf[32768];

static struct cmd_node bgp_node =
{
  BGP_NODE,
  "%s(config-router)# ",
};

static struct cmd_node rip_node =
{
  RIP_NODE,
  "%s(config-router)# ",
};

static struct cmd_node isis_node =
{
  ISIS_NODE,
  "%s(config-router)# ",
};

static struct cmd_node interface_node =
{
  INTERFACE_NODE,
  "%s(config-if)# ",
};

static struct cmd_node rmap_node =
{
  RMAP_NODE,
  "%s(config-route-map)# "
};

static struct cmd_node zebra_node =
{
  ZEBRA_NODE,
  "%s(config-router)# "
};

static struct cmd_node bgp_vpnv4_node =
{
  BGP_VPNV4_NODE,
  "%s(config-router-af)# "
};

static struct cmd_node bgp_ipv4_node =
{
  BGP_IPV4_NODE,
  "%s(config-router-af)# "
};

static struct cmd_node bgp_ipv4m_node =
{
  BGP_IPV4M_NODE,
  "%s(config-router-af)# "
};

static struct cmd_node bgp_ipv6_node =
{
  BGP_IPV6_NODE,
  "%s(config-router-af)# "
};

static struct cmd_node bgp_ipv6m_node =
{
  BGP_IPV6M_NODE,
  "%s(config-router-af)# "
};

static struct cmd_node ospf_node =
{
  OSPF_NODE,
  "%s(config-router)# "
};

static struct cmd_node ripng_node =
{
  RIPNG_NODE,
  "%s(config-router)# "
};

static struct cmd_node ospf6_node =
{
  OSPF6_NODE,
  "%s(config-ospf6)# "
};

static struct cmd_node babel_node =
{
  BABEL_NODE,
  "%s(config-babel)# "
};

static struct cmd_node keychain_node =
{
  KEYCHAIN_NODE,
  "%s(config-keychain)# "
};

static struct cmd_node keychain_key_node =
{
  KEYCHAIN_KEY_NODE,
  "%s(config-keychain-key)# "
};

static int
test_callback(struct cmd_element *cmd, struct vty *vty, int argc, const char *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]);
      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_STRVEC, line));
    }
}

static void
test_init(void)
{
  unsigned int node;
  unsigned int i;
  struct cmd_node *cnode;
  struct cmd_element *cmd;

  cmd_init(1);

  install_node (&bgp_node, NULL);
  install_node (&rip_node, NULL);
  install_node (&interface_node, NULL);
  install_node (&rmap_node, NULL);
  install_node (&zebra_node, NULL);
  install_node (&bgp_vpnv4_node, NULL);
  install_node (&bgp_ipv4_node, NULL);
  install_node (&bgp_ipv4m_node, NULL);
  install_node (&bgp_ipv6_node, NULL);
  install_node (&bgp_ipv6m_node, NULL);
  install_node (&ospf_node, NULL);
  install_node (&ripng_node, NULL);
  install_node (&ospf6_node, NULL);
  install_node (&babel_node, NULL);
  install_node (&keychain_node, NULL);
  install_node (&keychain_key_node, NULL);
  install_node (&isis_node, NULL);
  install_node (&vty_node, NULL);

  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_STRVEC, vector_slot(test_cmds, i));
  vector_free(test_cmds);
  cmd_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((int) 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_VECTOR_INDEX, 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 *cmd = vector_slot(descriptions, j);
                printf("  '%s' '%s'\n", cmd->cmd, cmd->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;
}