forked from CAIDA/mper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-msg-writer.c
231 lines (192 loc) · 7.21 KB
/
test-msg-writer.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
/*
** Tests mper_msg_writer.
**
** --------------------------------------------------------------------------
** 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_msg_writer.h"
#include "mper_base64.h"
static control_word_t words[MPER_MSG_MAX_WORDS + 10]; /* +10 for testing */
static int echo_message = 1; /* whether to echo the message in test() */
/* ====================================================================== */
static void test_create_control_message(void);
static void pass(size_t length, const char *expected_msg);
static void fail(size_t length);
static size_t test(size_t length, const char **message_out);
/* ====================================================================== */
int
main(int argc, char *argv[])
{
if (argc == 1) {
test_create_control_message();
fprintf(stderr, "\n\n=== ALL PASSED ===\n");
}
exit(0);
}
/* ====================================================================== */
static void
test_create_control_message(void)
{
/* test parsing of basic message fields and options */
memset(words, 0, 3 * sizeof(control_word_t));
fail(0);
fail(1);
fail(2);
INIT_CMESSAGE(words, 1234, PING);
words[1].cw_code = KC_TXT_OPT;
fail(CMESSAGE_LEN(0));
INIT_CMESSAGE(words, 1234, PING);
pass(CMESSAGE_LEN(0), "1234 ping");
INIT_CMESSAGE(words, 1234, PING);
words[2].cw_code = KC_PING_CMD;
fail(CMESSAGE_LEN(1));
/* uint */
INIT_CMESSAGE(words, 1234, PING);
SET_UINT_CWORD(words, 1, TTL, 5);
pass(CMESSAGE_LEN(1), "1234 ping ttl=5");
/* blob */
INIT_CMESSAGE(words, 1234, PING);
SET_BLOB_CWORD(words, 1, PKT, (const unsigned char *)"Hello, World!", 0);
fail(CMESSAGE_LEN(1)); /* cw_len of 0 is illegal */
words[2].cw_len = 13;
pass(CMESSAGE_LEN(1), "1234 ping pkt=$SGVsbG8sIFdvcmxkIQ==");
INIT_CMESSAGE(words, 1234, PING);
SET_BLOB_CWORD(words, 1, PKT, (const unsigned char *)"Hello,\n\nWorld!", 14);
pass(CMESSAGE_LEN(1), "1234 ping pkt=$SGVsbG8sCgpXb3JsZCE=");
INIT_CMESSAGE(words, 1234, PING);
SET_BLOB_CWORD(words, 1, PKT, (const unsigned char *)"Hello,\0World!", 13);
pass(CMESSAGE_LEN(1), "1234 ping pkt=$SGVsbG8sAFdvcmxkIQ==");
/* str */
INIT_CMESSAGE(words, 1234, PING);
SET_STR_CWORD(words, 1, TXT, "Hello, World!", 13);
pass(CMESSAGE_LEN(1), "1234 ping txt=$SGVsbG8sIFdvcmxkIQ==");
INIT_CMESSAGE(words, 1234, PING);
SET_STR_CWORD(words, 1, TXT, "Hello,\n\nWorld!", 14);
pass(CMESSAGE_LEN(1), "1234 ping txt=$SGVsbG8sCgpXb3JsZCE=");
INIT_CMESSAGE(words, 1234, PING);
SET_STR_CWORD(words, 1, TXT, "Hello,\0World!", 13);
pass(CMESSAGE_LEN(1), "1234 ping txt=$SGVsbG8sAFdvcmxkIQ==");
/* symbol */
INIT_CMESSAGE(words, 1234, PING);
SET_SYMBOL_CWORD(words, 1, METH, "__123456");
pass(CMESSAGE_LEN(1), "1234 ping meth=:__123456");
INIT_CMESSAGE(words, 1234, PING);
SET_SYMBOL_CWORD(words, 1, METH, "ab-cd_f-234");
pass(CMESSAGE_LEN(1), "1234 ping meth=:ab-cd_f-234");
/* address and prefix */
INIT_CMESSAGE(words, 1234, PING);
SET_ADDRESS_CWORD(words, 1, DEST, "255.199.99.249");
pass(CMESSAGE_LEN(1), "1234 ping [email protected]");
INIT_CMESSAGE(words, 1234, PING);
SET_PREFIX_CWORD(words, 1, NET, "1.2.3.4/5");
pass(CMESSAGE_LEN(1), "1234 ping [email protected]/5");
/* timeval */
INIT_CMESSAGE(words, 1234, PING);
SET_TIMEVAL2_CWORD(words, 1, TX, 234, 567);
pass(CMESSAGE_LEN(1), "1234 ping tx=T234:567");
/* test non-interference of multiple str/blobs */
INIT_CMESSAGE(words, 1234, PING);
SET_BLOB_CWORD(words, 1, PKT, (const unsigned char *)"Hello,", 6);
SET_STR_CWORD(words, 2, TXT, " World!", 7);
pass(CMESSAGE_LEN(2), "1234 ping pkt=$SGVsbG8s txt=$IFdvcmxkIQ==");
INIT_CMESSAGE(words, 1234, PING);
SET_STR_CWORD(words, 1, TXT, " World!", 7);
SET_BLOB_CWORD(words, 2, PKT, (const unsigned char *)"Hello,", 6);
pass(CMESSAGE_LEN(2), "1234 ping txt=$IFdvcmxkIQ== pkt=$SGVsbG8s");
/* test options in different positions */
INIT_CMESSAGE(words, 1234, PING);
SET_UINT_CWORD(words, 1, DPORT, 1234);
SET_UINT_CWORD(words, 2, TTL, 5);
SET_BLOB_CWORD(words, 3, PKT, (const unsigned char *)"Hello, World!", 13);
SET_SYMBOL_CWORD(words, 4, METH, "__123456");
SET_ADDRESS_CWORD(words, 5, DEST, "255.199.99.249");
SET_PREFIX_CWORD(words, 6, NET, "1.2.3.4/13");
pass(CMESSAGE_LEN(6), "1234 ping dport=1234 ttl=5 pkt=$SGVsbG8sIFdvcmxkIQ== meth=:__123456 [email protected] [email protected]/13");
INIT_CMESSAGE(words, 1234, PING);
SET_SYMBOL_CWORD(words, 1, METH, "__123456");
SET_UINT_CWORD(words, 2, DPORT, 1234);
SET_PREFIX_CWORD(words, 3, NET, "1.2.3.4/13");
SET_UINT_CWORD(words, 4, TTL, 5);
SET_ADDRESS_CWORD(words, 5, DEST, "255.199.99.249");
SET_BLOB_CWORD(words, 6, PKT, (const unsigned char *)"Hello, World!", 13);
pass(CMESSAGE_LEN(6), "1234 ping meth=:__123456 dport=1234 [email protected]/13 ttl=5 [email protected] pkt=$SGVsbG8sIFdvcmxkIQ==");
}
/* ====================================================================== */
static void
pass(size_t length, const char *expected_msg)
{
const char *message;
if (!test(length, &message)) {
fprintf(stderr, "FAIL: creation failed on well-formed input\n");
exit(1);
}
else {
if (strcmp(message, expected_msg) == 0) {
fprintf(stderr, "ok\n");
}
else {
fprintf(stderr, "FAIL: created message is incorrect\n");
fprintf(stderr, " created >> %s\n", message);
fprintf(stderr, " expected >> %s\n", expected_msg);
exit(1);
}
}
}
/* ====================================================================== */
static void
fail(size_t length)
{
if (test(length, NULL)) {
fprintf(stderr, "FAIL: creation succeeded on malformed input\n");
exit(1);
}
else {
fprintf(stderr, "ok\n");
}
}
/* ====================================================================== */
static size_t
test(size_t length, const char **message_out)
{
size_t msg_length;
const char *message;
message = create_control_message(words, length, &msg_length);
fputc('\n', stderr);
if (msg_length == 0) {
fprintf(stderr, "CREATE ERROR: %s\n", message);
}
else {
if (echo_message) {
fprintf(stderr, ">> %s\n", message);
dump_parsed_message(words, length);
}
else {
fprintf(stderr, ">> ### %d-byte message\n", (int)strlen(message));
}
fprintf(stderr, "creation succeeded\n");
}
if (message_out) *message_out = message;
return msg_length;
}