forked from sleinen/samplicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsetest.c
367 lines (334 loc) · 10.3 KB
/
parsetest.c
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
/*
parsetest.c
Date Created: Tue Feb 23 20:43:59 2010
Author: Simon Leinen <[email protected]>
Regression tests for samplicator's configuration file parsing.
I wanted to add functionality to samplicator's configuration file
parsing. For example, it should be able to handle IPv6 addresses in
addition to IPv4 addresses.
In order to make sure I don't break the existing functionality -
which someone else had written and which I didn't fully understand -
I decided to write exhaustive tests, at least for the cases where
files are parsed successfully. And I also wrote some tests for the
new functionality.
If all goes well, this program outputs a series of "ok" lines. If
any tests fail, the program will print a "fail" message. The ok/fail
messages only contain running numbers, so it's hard to find out what
exactly went wrong. The way I use this is that I run this test
program under a debugger such as GDB, with a breakpoint set at
test_fail(). When the breakpoint is hit, I move up the stack to see
what has gone wrong.
*/
#include "config.h"
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/socket.h>
#include <netinet/in.h>
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#if STDC_HEADERS
#define bzero(b,n) memset(b,0,n)
#else
#include <strings.h>
#ifndef HAVE_STRCHR
#define strchr index
#endif
#ifndef HAVE_MEMCPY
#define memcpy(d, s, n) bcopy ((s), (d), (n))
#define memmove(d, s, n) bcopy ((s), (d), (n))
#endif
#endif
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif
#include "samplicator.h"
#include "read_config.h"
#include "rawsend.h"
static int parse_cf_string (const char *, struct samplicator_context *);
static int check_int_equal (int, int);
static int check_non_null (const void *);
static int check_null (const void *);
static int check_address_equal (struct sockaddr *, const char *, unsigned, int);
static int test_ok (void);
static int test_fail (void);
static int test_index = 1;
static void
check_receiver (receiver, addr, port, af, freq, ttl)
const struct receiver *receiver;
const char *addr;
unsigned port;
int af;
int freq;
int ttl;
{
check_int_equal (receiver->addr.ss_family, af);
if (af == AF_INET)
check_int_equal (receiver->addrlen, sizeof (struct sockaddr_in));
else if (af == AF_INET6)
check_int_equal (receiver->addrlen, sizeof (struct sockaddr_in6));
else
test_fail ();
check_address_equal ((struct sockaddr *) &receiver->addr, addr, port, af);
check_int_equal (receiver->freq, freq);
check_int_equal (receiver->ttl, ttl);
}
static void
check_source_address_mask (sctx, source, mask, af)
const struct source_context *sctx;
const char *source, *mask;
int af;
{
check_address_equal ((struct sockaddr *) &sctx->source, source, 0, af);
check_address_equal ((struct sockaddr *) &sctx->mask, mask, 0, af);
}
int
main (int argc, char **argv)
{
struct samplicator_context ctx;
struct source_context *sctx;
if (argc != 1)
{
fprintf (stderr, "Usage: %s\n", argv[0]);
exit (1);
}
check_int_equal (parse_cf_string ("", &ctx), 0);
check_int_equal (ctx.fork, 0);
check_null (ctx.sources);
check_int_equal (parse_cf_string ("1.2.3.4/30+ 6.7.8.9/1234\n", &ctx), -1);
check_int_equal (parse_cf_string
("1.2.3.4/255.255.255.252: 6.7.8.9/1234/10,237\n2.3.4.5: 7.8.9.0/4321", &ctx), 0);
check_int_equal (ctx.fork, 0);
if (check_non_null (sctx = ctx.sources))
{
check_source_address_mask (sctx, "1.2.3.4", "255.255.255.252", AF_INET);
check_int_equal (sctx->nreceivers, 1);
check_receiver (&sctx->receivers[0], "6.7.8.9", 1234, AF_INET, 10, 237);
if (check_non_null (sctx = sctx->next))
{
check_source_address_mask (sctx, "2.3.4.5", "255.255.255.255", AF_INET);
check_int_equal (sctx->nreceivers, 1);
check_receiver (&sctx->receivers[0], "7.8.9.0", 4321, AF_INET, 1, DEFAULT_TTL);
check_null (sctx->next);
}
}
check_int_equal (parse_cf_string ("1.2.3.4/30: 6.7.8.9/1200-1210\n", &ctx), 0);
if (check_non_null (sctx = ctx.sources))
{
check_int_equal (sctx->nreceivers, 11);
check_receiver (&sctx->receivers[0], "6.7.8.9", 1200, AF_INET, 1, DEFAULT_TTL);
if (sctx->nreceivers == 11)
{
check_receiver (&sctx->receivers[10], "6.7.8.9", 1210, AF_INET, 1, DEFAULT_TTL);
}
}
check_int_equal (parse_cf_string ("1.2.3.4/30: 6.7.8.9/1200+10\n", &ctx), 0);
if (check_non_null (sctx = ctx.sources))
{
check_int_equal (sctx->nreceivers, 10);
check_receiver (&sctx->receivers[0], "6.7.8.9", 1200, AF_INET, 1, DEFAULT_TTL);
if (sctx->nreceivers == 10)
{
check_receiver (&sctx->receivers[9], "6.7.8.9", 1209, AF_INET, 1, DEFAULT_TTL);
}
}
check_int_equal (parse_cf_string ("1.2.3.4/30: 6.7.8.9/1234\n2.3.4.5: 7.8.9.0/4321", &ctx), 0);
check_int_equal (ctx.fork, 0);
if (check_non_null (sctx = ctx.sources))
{
check_source_address_mask (sctx, "1.2.3.4", "255.255.255.252", AF_INET);
check_int_equal (sctx->nreceivers, 1);
check_receiver (&sctx->receivers[0], "6.7.8.9", 1234, AF_INET, 1, DEFAULT_TTL);
if (check_non_null (sctx = sctx->next))
{
check_source_address_mask (sctx, "2.3.4.5", "255.255.255.255", AF_INET);
check_int_equal (sctx->nreceivers, 1);
check_receiver (&sctx->receivers[0], "7.8.9.0", 4321, AF_INET, 1, DEFAULT_TTL);
check_null (sctx->next);
}
}
#ifdef NOTYET
check_int_equal (parse_cf_string ("1.2.3.4/30: localhost/1234", &ctx), 0);
check_int_equal (ctx.fork, 0);
if (check_non_null (sctx = ctx.sources))
{
check_source_address_mask (sctx, "1.2.3.4", "255.255.255.252", AF_INET);
check_int_equal (sctx->nreceivers, 1);
check_receiver (&sctx->receivers[0], "127.0.0.1", 1234, AF_INET, 1, DEFAULT_TTL);
}
#endif
check_int_equal (parse_cf_string ("1.2.3.4/30: ip6-localhost/1234", &ctx), 0);
check_int_equal (ctx.fork, 0);
if (check_non_null (sctx = ctx.sources))
{
check_source_address_mask (sctx, "1.2.3.4", "255.255.255.252", AF_INET);
check_int_equal (sctx->nreceivers, 1);
check_receiver (&sctx->receivers[0], "::1", 1234, AF_INET6, 1, DEFAULT_TTL);
}
check_int_equal (parse_cf_string ("[0::0]/0: [2001:db8:0::1]/1234/10,34\n", &ctx), 0);
check_int_equal (ctx.fork, 0);
sctx = ctx.sources;
if (check_non_null (sctx))
{
check_source_address_mask (sctx, "::", "::", AF_INET6);
check_int_equal (sctx->nreceivers, 1);
check_receiver (&sctx->receivers[0], "2001:db8:0::1", 1234, AF_INET6, 10, 34);
check_null (sctx->next);
}
check_int_equal (parse_cf_string ("[0::0]/0: [2001:db8:0::1]/1234/10,34\n", &ctx), 0);
check_int_equal (ctx.fork, 0);
sctx = ctx.sources;
if (check_non_null (sctx))
{
check_source_address_mask (sctx, "::", "::", AF_INET6);
check_int_equal (sctx->nreceivers, 1);
check_receiver (&sctx->receivers[0], "2001:db8:0::1", 1234, AF_INET6, 10, 34);
check_null (sctx->next);
}
check_int_equal (parse_cf_string ("[2001:db8:0:4::]: [2001:db8:0::1]\n", &ctx), 0);
check_int_equal (ctx.fork, 0);
sctx = ctx.sources;
if (check_non_null (sctx))
{
check_source_address_mask (sctx, "2001:db8:0:4::", "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", AF_INET6);
check_int_equal (sctx->nreceivers, 1);
check_receiver (&sctx->receivers[0], "2001:db8:0::1", 2000, AF_INET6, 1, DEFAULT_TTL);
check_null (sctx->next);
}
#ifdef NOTYET
#endif
return 0;
}
static int
parse_cf_string (s, ctx)
const char *s;
struct samplicator_context *ctx;
{
const char *test_file_name = "parsetest.cf";
FILE *fp;
const char *args[20];
const char **ap;
int n_args;
unlink (test_file_name);
fp = fopen (test_file_name, "w");
if (fp == (FILE *) 0)
{
fprintf (stderr, "Could not create test file %s: %s", test_file_name, strerror (errno));
return -1;
}
if (fputs (s, fp) == EOF)
{
fprintf (stderr, "Error writing to test file %s: %s", test_file_name, strerror (errno));
return -1;
}
if (fclose (fp) != 0)
{
fprintf (stderr, "Error closing test file %s: %s", test_file_name, strerror (errno));
return -1;
}
ap = &args[0];
*ap++ = "parsetest";
*ap++ = "-c";
*ap++ = test_file_name;
n_args = ap - args;
*ap++ = (char *) 0;
return parse_args (n_args, args, ctx);
}
static int
check_int_equal (is, should)
int is;
int should;
{
if (is == should)
{
return test_ok ();
}
else
{
return test_fail ();
}
}
static int
check_non_null (ptr)
const void *ptr;
{
return check_int_equal (ptr == 0, 0);
}
static int
check_null (ptr)
const void *ptr;
{
return check_int_equal (ptr == 0, 1);
}
static int
check_sockaddrs_equal (sa1_generic, sa2_generic)
struct sockaddr *sa1_generic;
struct sockaddr *sa2_generic;
{
#define SPECIALIZE(VAR, STRUCT) \
struct STRUCT *VAR = (struct STRUCT *) VAR ## _generic
if (sa1_generic->sa_family != sa2_generic->sa_family)
return test_fail ();
if (sa1_generic->sa_family == AF_INET)
{
SPECIALIZE (sa1, sockaddr_in);
SPECIALIZE (sa2, sockaddr_in);
check_int_equal (sa1->sin_port, sa2->sin_port);
if (memcmp (&sa1->sin_addr, &sa2->sin_addr, sizeof (struct in_addr)) != 0)
return test_fail ();
return test_ok ();
}
else if (sa1_generic->sa_family == AF_INET6)
{
SPECIALIZE (sa1, sockaddr_in6);
SPECIALIZE (sa2, sockaddr_in6);
check_int_equal (sa1->sin6_port, sa2->sin6_port);
if (memcmp (&sa1->sin6_addr, &sa2->sin6_addr, sizeof (struct in6_addr)) != 0)
return test_fail ();
return test_ok ();
}
else
return test_fail ();
#undef SPECIALIZE
}
static int
check_address_equal (sa1, s2, port, af)
struct sockaddr *sa1;
const char *s2;
unsigned port;
int af;
{
struct addrinfo hints, *res;
int result;
char portspec[10];
sprintf (portspec, "%5.5d", port);
bzero (&hints, sizeof hints);
hints.ai_family = af;
result = getaddrinfo (s2, portspec, &hints, &res);
if (result != 0)
return test_fail ();
if (res->ai_family != af)
return test_fail ();
return check_sockaddrs_equal (sa1, res->ai_addr);
}
static int
test_ok ()
{
fprintf (stdout, "%3d... ok\n", test_index++);
return 1;
}
static int
test_fail ()
{
fprintf (stdout, "%3d... fail\n", test_index++);
return 0;
}