File: test-nexthop-iter.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 (291 lines) | stat: -rw-r--r-- 6,441 bytes parent folder | download | duplicates (5)
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
/*
 * Recursive Nexthop Iterator test.
 * This tests the ALL_NEXTHOPS_RO macro.
 *
 * Copyright (C) 2012 by Open Source Routing.
 * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
 *
 * This file is part of Quagga
 *
 * 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.
 */

#include <zebra.h>
#include "zebra/rib.h"
#include "prng.h"

struct thread_master *master;
static int verbose;

static void
str_append(char **buf, const char *repr)
{
  if (*buf)
    {
      *buf = realloc(*buf, strlen(*buf) + strlen(repr) + 1);
      assert(*buf);
      strncpy((*buf) + strlen(*buf), repr, strlen(repr) + 1);
    }
  else
    {
      *buf = strdup(repr);
      assert(*buf);
    }
}

static void
str_appendf(char **buf, const char *format, ...)
{
  va_list ap;
  int rv;
  char *pbuf;

  va_start(ap, format);
  rv = vasprintf(&pbuf, format, ap);
  va_end(ap);
  assert(rv >= 0);

  str_append(buf, pbuf);
  free(pbuf);
}

/* This structure contains a nexthop chain
 * and its expected representation */
struct nexthop_chain
{
  /* Head of the chain */
  struct nexthop *head;
  /* Last nexthop in top chain */
  struct nexthop *current_top;
  /* Last nexthop in current recursive chain */
  struct nexthop *current_recursive;
  /* Expected string representation. */
  char *repr;
};

static struct nexthop_chain*
nexthop_chain_new(void)
{
  struct nexthop_chain *rv;

  rv = calloc(sizeof(*rv), 1);
  assert(rv);
  return rv;
}

static void
nexthop_chain_add_top(struct nexthop_chain *nc)
{
  struct nexthop *nh;

  nh = calloc(sizeof(*nh), 1);
  assert(nh);

  if (nc->head)
    {
      nc->current_top->next = nh;
      nh->prev = nc->current_top;
      nc->current_top = nh;
    }
  else
    {
      nc->head = nc->current_top = nh;
    }
  nc->current_recursive = NULL;
  str_appendf(&nc->repr, "%p\n", nh);
}

static void
nexthop_chain_add_recursive(struct nexthop_chain *nc)
{
  struct nexthop *nh;

  nh = calloc(sizeof(*nh), 1);
  assert(nh);

  assert(nc->current_top);
  if (nc->current_recursive)
    {
      nc->current_recursive->next = nh;
      nh->prev = nc->current_recursive;
      nc->current_recursive = nh;
    }
  else
    {
      SET_FLAG(nc->current_top->flags, NEXTHOP_FLAG_RECURSIVE);
      nc->current_top->resolved = nh;
      nc->current_recursive = nh;
    }
  str_appendf(&nc->repr, "  %p\n", nh);
}

static void
nexthop_chain_clear(struct nexthop_chain *nc)
{
  struct nexthop *tcur, *tnext;

  for (tcur = nc->head; tcur; tcur = tnext)
    {
      tnext = tcur->next;
      if (CHECK_FLAG(tcur->flags, NEXTHOP_FLAG_RECURSIVE))
        {
          struct nexthop *rcur, *rnext;
          for (rcur = tcur->resolved; rcur; rcur = rnext)
            {
              rnext = rcur->next;
              free(rcur);
            }
        }
      free(tcur);
    }
  nc->head = nc->current_top = nc->current_recursive = NULL;
  free(nc->repr);
  nc->repr = NULL;
}

static void
nexthop_chain_free(struct nexthop_chain *nc)
{
  if (!nc)
    return;
  nexthop_chain_clear(nc);
  free(nc);
}

/* This function builds a string representation of
 * the nexthop chain using the ALL_NEXTHOPS_RO macro.
 * It verifies that the ALL_NEXTHOPS_RO macro iterated
 * correctly over the nexthop chain by comparing the
 * generated representation with the expected representation.
 */
static void
nexthop_chain_verify_iter(struct nexthop_chain *nc)
{
  struct nexthop *nh, *tnh;
  int recursing;
  char *repr = NULL;

  for (ALL_NEXTHOPS_RO(nc->head, nh, tnh, recursing))
    {
      if (recursing)
        str_appendf(&repr, "  %p\n", nh);
      else
        str_appendf(&repr, "%p\n", nh);
    }

  if (repr && verbose)
    printf("===\n%s", repr);
  assert((!repr && !nc->repr) || (repr && nc->repr && !strcmp(repr, nc->repr)));
  free(repr);
}

/* This test run builds a simple nexthop chain
 * with some recursive nexthops and verifies that
 * the iterator works correctly in each stage along
 * the way.
 */
static void
test_run_first(void)
{
  struct nexthop_chain *nc;

  nc = nexthop_chain_new();
  nexthop_chain_verify_iter(nc);

  nexthop_chain_add_top(nc);
  nexthop_chain_verify_iter(nc);

  nexthop_chain_add_top(nc);
  nexthop_chain_verify_iter(nc);

  nexthop_chain_add_recursive(nc);
  nexthop_chain_verify_iter(nc);

  nexthop_chain_add_recursive(nc);
  nexthop_chain_verify_iter(nc);

  nexthop_chain_add_top(nc);
  nexthop_chain_verify_iter(nc);

  nexthop_chain_add_top(nc);
  nexthop_chain_verify_iter(nc);

  nexthop_chain_add_top(nc);
  nexthop_chain_verify_iter(nc);

  nexthop_chain_add_recursive(nc);
  nexthop_chain_verify_iter(nc);

  nexthop_chain_add_recursive(nc);
  nexthop_chain_verify_iter(nc);

  nexthop_chain_add_recursive(nc);
  nexthop_chain_verify_iter(nc);

  nexthop_chain_free(nc);
}

/* This test run builds numerous random
 * nexthop chain configurations and verifies
 * that the iterator correctly progresses
 * through each. */
static void
test_run_prng(void)
{
  struct nexthop_chain *nc;
  struct prng *prng;
  int i;

  nc = nexthop_chain_new();
  prng = prng_new(0);

  for (i = 0; i < 1000000; i++)
    {
      switch (prng_rand(prng) % 10)
        {
        case 0:
          nexthop_chain_clear(nc);
          break;
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
          nexthop_chain_add_top(nc);
          break;
        case 6:
        case 7:
        case 8:
        case 9:
          if (nc->current_top)
            nexthop_chain_add_recursive(nc);
          break;
        }
      nexthop_chain_verify_iter(nc);
    }
  nexthop_chain_free(nc);
  prng_free(prng);
}

int main(int argc, char **argv)
{
  if (argc >= 2 && !strcmp("-v", argv[1]))
    verbose = 1;
  test_run_first();
  printf("Simple test passed.\n");
  test_run_prng();
  printf("PRNG test passed.\n");
}