forked from CAIDA/mper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-msg-reader.c
418 lines (351 loc) · 12.6 KB
/
test-msg-reader.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
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
416
417
418
/*
** Tests mper_msg_reader and mper_keywords.
**
** --------------------------------------------------------------------------
** Author: Young Hyun
** Copyright (C) 2009 The Regents of the University of California.
**
** This program 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 of the License, or
** (at your option) any later version.
**
** This program 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 this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "internal.h"
#include "mper_keywords.h"
#include "mper_msg.h"
#include "mper_msg_reader.h"
#include "mper_base64.h"
int echo_message = 1; /* whether to echo the message in test() */
/* ====================================================================== */
static void test_parse_control_message(void);
static void pass(const char *message, size_t expected_length, ...);
static const char *create_long_msg(const char *start, const char *end,
size_t length);
static void pass_long_base64(size_t encoded_length);
static const char *create_long_base64(size_t length);
static void check_value(const control_word_t *value,
const control_word_t *expected, size_t i);
static void fail(const char *message);
static size_t test(const char *message, const control_word_t **words_out);
/* ====================================================================== */
int
main(int argc, char *argv[])
{
if (argc == 1) {
test_parse_control_message();
fprintf(stderr, "\n\n=== ALL PASSED ===\n");
}
else if (argc == 2) {
test(argv[1], NULL);
}
else {
fprintf(stderr, "usage: test-parser [<message>]\n");
exit(1);
}
exit(0);
}
/* ====================================================================== */
static void
test_parse_control_message(void)
{
/* test parsing of basic message fields and options */
fail("");
fail("A123");
fail("1ABC8");
fail(" 123");
fail("123");
fail("1234 nosuchcommand");
fail("1234 5678");
fail("1234 pi-ng");
pass("1234 ping", 2);
fail("1234 dest");
fail("1234 ping ^xyz");
fail("1234 ping xyz");
fail("1234 ping pi^g=5");
fail("1234 ping ^pi=5");
fail("1234 ping ping=5");
pass("1234 ping ttl=5", 3, KT_UINT, 5);
fail("1234 ping ttl= 1234");
fail("1234 ping ttl=-1234");
fail("1234 ping ttl=___");
fail("1234 ping ttl=^@$T");
fail("1234 ping ttl=123_4");
fail("1234 ping ttl=123.4");
/* $ ruby -e 'p [ "Hello, World!" ].pack("m0")' */
fail("1234 ping pkt=$");
fail("1234 ping pkt=$ ");
fail("1234 ping pkt=$=");
fail("1234 ping pkt=$!@#$");
fail("1234 ping pkt=$abcdef^^1234ABC");
fail("1234 ping pkt=$abcdef^^1234ABC");
pass("1234 ping pkt=$SGVsbG8sIFdvcmxkIQ==", 3,
KT_BLOB, "Hello, World!", 13);
pass("1234 ping pkt=$SGVsbG8sCgpXb3JsZCE=", 3,
KT_BLOB, "Hello,\n\nWorld!", 14);
pass("1234 ping pkt=$SGVsbG8sAFdvcmxkIQ==", 3,
KT_BLOB, "Hello,\0World!", 13);
fail("1234 ping pkt=$SGVsbG8sIFdvcmxkIQ="); /* malformed base64 encoding */
pass("1234 ping txt=$SGVsbG8sIFdvcmxkIQ==", 3,
KT_STR, "Hello, World!", 13);
pass("1234 ping txt=$SGVsbG8sCgpXb3JsZCE=", 3,
KT_STR, "Hello,\n\nWorld!", 14);
pass("1234 ping txt=$SGVsbG8sAFdvcmxkIQ==", 3,
KT_STR, "Hello,\0World!", 13);
fail("1234 ping meth=:");
fail("1234 ping meth=: ");
fail("1234 ping meth=:123");
fail("1234 ping meth=:^!@");
pass("1234 ping meth=:__123456", 3, KT_SYMBOL, "__123456");
pass("1234 ping meth=:ab-cd_f-234", 3, KT_SYMBOL, "ab-cd_f-234");
fail("1234 ping meth=:ab-cd_f-234@");
fail("1234 ping dest=@");
fail("1234 ping dest=@ ");
fail("1234 ping dest=@.");
fail("1234 ping [email protected]");
fail("1234 ping dest=@^!@#");
fail("1234 ping dest=@aaabcd");
fail("1234 ping [email protected]");
fail("1234 ping [email protected]");
fail("1234 ping [email protected]..");
fail("1234 ping [email protected]");
fail("1234 ping [email protected]");
fail("1234 ping [email protected]");
fail("1234 ping [email protected]");
fail("1234 ping [email protected]");
fail("1234 ping [email protected]");
pass("1234 ping [email protected]", 3, KT_ADDRESS, "255.199.99.249");
fail("1234 ping [email protected]/");
fail("1234 ping [email protected]./");
fail("1234 ping [email protected]/");
fail("1234 ping [email protected]/ ");
fail("1234 ping [email protected]/.");
fail("1234 ping [email protected]/0000");
fail("1234 ping [email protected]/01");
fail("1234 ping [email protected]/001");
fail("1234 ping [email protected]/33");
fail("1234 ping [email protected]/34");
fail("1234 ping [email protected]/45");
pass("1234 ping [email protected]/0", 3, KT_PREFIX, "1.2.3.4/0");
pass("1234 ping [email protected]/13", 3, KT_PREFIX, "1.2.3.4/13");
pass("1234 ping [email protected]/32", 3, KT_PREFIX, "1.2.3.4/32");
fail("1234 ping tx=T");
fail("1234 ping tx=T ");
fail("1234 ping tx=T^234");
fail("1234 ping tx=T234");
fail("1234 ping tx=T234.");
fail("1234 ping tx=T234:");
fail("1234 ping tx=T234: ");
fail("1234 ping tx=T234:567x");
pass("1234 ping tx=T234:567", 3, KT_TIMEVAL, (time_t)234, 567);
/* test non-interference of multiple str/blobs */
pass("1234 ping pkt=$SGVsbG8s txt=$IFdvcmxkIQ==", 4,
KT_BLOB, "Hello,", 6, KT_STR, " World!", 7);
pass("1234 ping txt=$IFdvcmxkIQ== pkt=$SGVsbG8s", 4,
KT_STR, " World!", 7, KT_BLOB, "Hello,", 6);
/* test type checking */
fail("1234 ping ttl=:foo");
fail("1234 ping ttl=$SGVsbG8sIFdvcmxkIQ==");
fail("1234 ping dest=$SGVsbG8sIFdvcmxkIQ==");
fail("1234 ping dest=255");
fail("1234 ping meth=123456");
fail("1234 ping [email protected]/0");
fail("1234 ping [email protected]");
fail("1234 ping net=:hey");
fail("1234 ping pkt=555");
fail("1234 ping pkt=:hi");
fail("1234 ping pkt=T123:456");
fail("1234 ping tx=123");
fail("1234 ping tx=$SGVsbG8sIFdvcmxkIQ==");
/* test parsing of options in different positions */
pass("1234 ping dport=1234 ttl=5 pkt=$SGVsbG8sIFdvcmxkIQ== meth=:__123456 [email protected] [email protected]/13", 8,
KT_UINT, 1234, KT_UINT, 5, KT_BLOB, "Hello, World!", 13,
KT_SYMBOL, "__123456", KT_ADDRESS, "255.199.99.249",
KT_PREFIX, "1.2.3.4/13");
pass("1234 ping meth=:__123456 dport=1234 [email protected]/13 ttl=5 [email protected] pkt=$SGVsbG8sIFdvcmxkIQ==", 8,
KT_SYMBOL, "__123456", KT_UINT, 1234, KT_PREFIX, "1.2.3.4/13",
KT_UINT, 5, KT_ADDRESS, "255.199.99.249", KT_BLOB, "Hello, World!", 13);
/* test length limits */
echo_message = 0;
pass(create_long_msg("1234 ping tx=T234:567", "ttl=5",
MPER_MSG_MAX_MESSAGE_SIZE), 4,
KT_TIMEVAL, (time_t)234, 567, KT_UINT, 5);
fail(create_long_msg("1234 ping tx=T234:567", "ttl=5",
MPER_MSG_MAX_MESSAGE_SIZE + 1));
fail(create_long_msg("1234 ping tx=T234:567", "ttl=5",
MPER_MSG_MAX_MESSAGE_SIZE + 2));
fail(create_long_msg("1234 ping tx=T234:567", "ttl=5",
MPER_MSG_MAX_MESSAGE_SIZE + 512));
pass_long_base64(MPER_MSG_MAX_ENCODED_VALUE_SIZE);
fail(create_long_base64(MPER_MSG_MAX_ENCODED_VALUE_SIZE + 4));
fail(create_long_base64(MPER_MSG_MAX_ENCODED_VALUE_SIZE + 512));
echo_message = 1;
}
/* ---------------------------------------------------------------------- */
static const char *
create_long_msg(const char *start, const char *end, size_t length)
{
static char msgbuf[MPER_MSG_MAX_MESSAGE_SIZE + 1024];
size_t fieldlen = length - strlen(end);
sprintf(msgbuf, "%-*s%s", (int)fieldlen, start, end);
return msgbuf;
}
/* ---------------------------------------------------------------------- */
static void
pass_long_base64(size_t encoded_length)
{
static char raw_value[MPER_MSG_MAX_RAW_VALUE_SIZE + 512];
size_t raw_length = 3 * (encoded_length / 4);
memset(raw_value, '.', raw_length);
raw_value[raw_length] = '\0';
pass(create_long_base64(encoded_length), 3, KT_BLOB, raw_value, raw_length);
}
static const char *
create_long_base64(size_t encoded_length)
{
static char msgbuf[MPER_MSG_MAX_MESSAGE_SIZE + 1];
static char encodebuf[MPER_MSG_MAX_ENCODED_VALUE_SIZE + 1024];
size_t raw_length = 3 * (encoded_length / 4);
fprintf(stderr, "\ncreating blob: %d bytes encoded, %d bytes decoded\n",
(int)encoded_length, (int)raw_length);
memset(msgbuf, '.', sizeof(msgbuf));
base64_encode((const unsigned char *)msgbuf, raw_length, encodebuf);
strcpy(msgbuf, "1234 ping pkt=$");
strcat(msgbuf, encodebuf);
return msgbuf;
}
/* ====================================================================== */
static void
pass(const char *message, size_t expected_length, ...)
{
va_list ap;
const control_word_t *words;
size_t length = test(message, &words);
if (length == 0) {
fprintf(stderr, "FAIL: parsing failed on well-formed input\n");
exit(1);
}
else if (length != expected_length) {
fprintf(stderr, "FAIL: mismatched length: expected length=%lu, actual length=%lu\n", (unsigned long)expected_length, (unsigned long)length);
exit(1);
}
else {
control_word_t w;
size_t i;
va_start(ap, expected_length);
for (i = 2; i < expected_length; i++) {
memset(&w, 0, sizeof(w));
w.cw_type = va_arg(ap, keyword_type);
switch (w.cw_type) {
case KT_UINT: w.cw_uint = va_arg(ap, uint32_t); break;
case KT_STR:
w.cw_str = va_arg(ap, const char *);
w.cw_len = va_arg(ap, size_t);
break;
case KT_BLOB:
w.cw_blob = va_arg(ap, const unsigned char *);
w.cw_len = va_arg(ap, size_t);
break;
case KT_SYMBOL: w.cw_symbol = va_arg(ap, const char *); break;
case KT_ADDRESS: w.cw_address = va_arg(ap, const char *); break;
case KT_PREFIX: w.cw_prefix = va_arg(ap, const char *); break;
case KT_TIMEVAL:
w.cw_timeval.tv_sec = va_arg(ap, time_t);
w.cw_timeval.tv_usec = va_arg(ap, long);
break;
default: fprintf(stderr, "ASSERTION FAILURE at %s:%d",
__FILE__, __LINE__); exit(1);
}
check_value(&words[i], &w, i);
}
va_end(ap);
fprintf(stderr, "ok\n");
}
}
static void
check_value(const control_word_t *value, const control_word_t *expected,
size_t i)
{
if (value->cw_type != expected->cw_type) {
fprintf(stderr, "FAIL: '%s' option at index %d has wrong type: expected %s, got %s\n",
value->cw_name, (int)i, keyword_type_names[expected->cw_type],
keyword_type_names[value->cw_type]);
exit(1);
}
switch (value->cw_type) {
case KT_UINT:
if (value->cw_uint == expected->cw_uint) return;
break;
case KT_STR:
if (value->cw_len == expected->cw_len
&& memcmp(value->cw_str, expected->cw_str, expected->cw_len) == 0)
return;
break;
case KT_BLOB:
if (value->cw_len == expected->cw_len
&& memcmp(value->cw_blob, expected->cw_blob, expected->cw_len) == 0)
return;
break;
case KT_SYMBOL:
if (strcmp(value->cw_symbol, expected->cw_symbol) == 0) return;
break;
case KT_ADDRESS:
if (strcmp(value->cw_address, expected->cw_address) == 0) return;
break;
case KT_PREFIX:
if (strcmp(value->cw_prefix, expected->cw_prefix) == 0) return;
break;
case KT_TIMEVAL:
if (value->cw_timeval.tv_sec == expected->cw_timeval.tv_sec
&& value->cw_timeval.tv_usec == expected->cw_timeval.tv_usec) return;
break;
default: fprintf(stderr, "ASSERTION FAILURE at %s:%d",
__FILE__, __LINE__); exit(1);
}
fprintf(stderr, "FAIL: '%s' option at index %d has wrong value\n",
value->cw_name, (int)i);
exit(1);
}
/* ====================================================================== */
static void
fail(const char *message)
{
if (test(message, NULL)) {
fprintf(stderr, "FAIL: parsing succeeded on malformed input\n");
exit(1);
}
else {
fprintf(stderr, "ok\n");
}
}
/* ====================================================================== */
static size_t
test(const char *message, const control_word_t **words_out)
{
const control_word_t *words;
size_t length;
if (echo_message) {
fprintf(stderr, "\n>> %s\n", message);
words = parse_control_message(message, &length);
dump_parsed_message(words, length);
}
else {
fprintf(stderr, "\n>> ### %d-byte message\n", (int)strlen(message));
words = parse_control_message(message, &length);
if (length == 0) fprintf(stderr, "PARSE ERROR: %s\n", words[1].cw_str);
else fprintf(stderr, "parsing succeeded\n");
}
if (words_out) *words_out = words;
return length;
}