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
|
/**
* @file parser_test.c
*
* @brief Tests for the parser_t class.
*
*/
/*
* Copyright (C) 2005 Jan Hutter, Martin Willi
* Hochschule fuer Technik Rapperswil
*
* 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. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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.
*/
#include <string.h>
#include "parser_test.h"
#include <utils/allocator.h>
#include <utils/logger_manager.h>
#include <encoding/generator.h>
#include <encoding/parser.h>
#include <encoding/payloads/encodings.h>
#include <encoding/payloads/ike_header.h>
#include <encoding/payloads/sa_payload.h>
#include <encoding/payloads/nonce_payload.h>
#include <encoding/payloads/id_payload.h>
#include <encoding/payloads/ke_payload.h>
#include <encoding/payloads/notify_payload.h>
/*
* Described in Header
*/
void test_parser_with_header_payload(tester_t *tester)
{
parser_t *parser;
ike_header_t *ike_header;
status_t status;
chunk_t header_chunk;
u_int8_t header_bytes[] = {
0x01,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x02,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x03,0x45,0x06,0x28,
0x00,0x00,0x00,0x07,
0x00,0x00,0x00,0x1C,
};
header_chunk.ptr = header_bytes;
header_chunk.len = sizeof(header_bytes);
parser = parser_create(header_chunk);
tester->assert_true(tester,(parser != NULL), "parser create check");
status = parser->parse_payload(parser, HEADER, (payload_t**)&ike_header);
tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
parser->destroy(parser);
if (status != SUCCESS)
{
return;
}
tester->assert_true(tester,(ike_header->get_initiator_spi(ike_header) == 1),"parsed initiator_spi value");
tester->assert_true(tester,(ike_header->get_responder_spi(ike_header) == 2),"parsed responder_spi value");
tester->assert_true(tester,(ike_header->payload_interface.get_next_type((payload_t*)ike_header) == 3),"parsed next_payload value");
tester->assert_true(tester,(ike_header->get_maj_version(ike_header) == 4),"parsed maj_version value");
tester->assert_true(tester,(ike_header->get_min_version(ike_header) == 5),"parsed min_version value");
tester->assert_true(tester,(ike_header->get_exchange_type(ike_header) == 6),"parsed exchange_type value");
tester->assert_true(tester,(ike_header->get_initiator_flag(ike_header) == TRUE),"parsed flags.initiator value");
tester->assert_true(tester,(ike_header->get_version_flag(ike_header) == FALSE),"parsed flags.version value");
tester->assert_true(tester,(ike_header->get_response_flag(ike_header) == TRUE),"parsed flags.response value");
tester->assert_true(tester,(ike_header->get_message_id(ike_header) == 7),"parsed message_id value");
tester->assert_true(tester,(ike_header->payload_interface.get_length((payload_t*)ike_header) == 0x1C),"parsed length value");
ike_header->destroy(ike_header);
}
/*
* Described in Header
*/
void test_parser_with_sa_payload(tester_t *tester)
{
parser_t *parser;
sa_payload_t *sa_payload;
status_t status;
chunk_t sa_chunk;
iterator_t *proposals, *transforms, *attributes;
u_int8_t sa_bytes[] = {
0x00,0x80,0x00,0x24, /* payload header*/
0x00,0x00,0x00,0x20, /* a proposal */
0x01,0x02,0x04,0x05,
0x01,0x02,0x03,0x04, /* spi */
0x00,0x00,0x00,0x14, /* transform */
0x07,0x00,0x00,0x03,
0x80,0x01,0x00,0x05, /* attribute without length */
0x00,0x03,0x00,0x04, /* attribute with lenngth */
0x01,0x02,0x03,0x04
};
sa_chunk.ptr = sa_bytes;
sa_chunk.len = sizeof(sa_bytes);
parser = parser_create(sa_chunk);
tester->assert_true(tester,(parser != NULL), "parser create check");
status = parser->parse_payload(parser, SECURITY_ASSOCIATION, (payload_t**)&sa_payload);
tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
parser->destroy(parser);
if (status != SUCCESS)
{
return;
}
proposals = sa_payload->create_proposal_substructure_iterator(sa_payload, TRUE);
while (proposals->has_next(proposals))
{
proposal_substructure_t *proposal;
proposals->current(proposals, (void**)&proposal);
chunk_t spi;
u_int8_t spi_should[] = {0x01, 0x02, 0x03, 0x04};
tester->assert_true(tester,(proposal->get_proposal_number(proposal) == 1),"proposal number");
tester->assert_true(tester,(proposal->get_protocol_id(proposal) == 2),"proposal id");
spi = proposal->get_spi(proposal);
tester->assert_false(tester,(memcmp(&spi_should, spi.ptr, spi.len)),"proposal spi");
transforms = proposal->create_transform_substructure_iterator(proposal, TRUE);
while(transforms->has_next(transforms))
{
transform_substructure_t *transform;
int loopi;
transforms->current(transforms, (void**)&transform);
tester->assert_true(tester,(transform->get_transform_type(transform) == 7),"transform type");
tester->assert_true(tester,(transform->get_transform_id(transform) == 3),"transform id");
attributes = transform->create_transform_attribute_iterator(transform, TRUE);
loopi = 0;
while (attributes->has_next(attributes))
{
transform_attribute_t *attribute;
attributes->current(attributes, (void**)&attribute);
if (loopi == 0)
{
u_int8_t value[] = {0x05, 0x00};
chunk_t attribute_value;
tester->assert_true(tester,(attribute->get_attribute_type(attribute) == 1),"attribute 1 type");
attribute_value = attribute->get_value_chunk(attribute);
tester->assert_false(tester,(memcmp(&value, attribute_value.ptr, attribute_value.len)),"attribute 1 value");
}
if (loopi == 1)
{
u_int8_t value[] = {0x01, 0x02, 0x03, 0x04};
chunk_t attribute_value;
tester->assert_true(tester,(attribute->get_attribute_type(attribute) == 3),"attribute 2 type");
attribute_value = attribute->get_value_chunk(attribute);
tester->assert_false(tester,(memcmp(&value, attribute_value.ptr, attribute_value.len)),"attribute 2 value");
}
loopi++;
}
attributes->destroy(attributes);
}
transforms->destroy(transforms);
}
proposals->destroy(proposals);
sa_payload->destroy(sa_payload);
}
/*
* Described in Header
*/
void test_parser_with_nonce_payload(tester_t *tester)
{
parser_t *parser;
nonce_payload_t *nonce_payload;
status_t status;
chunk_t nonce_chunk, result;
u_int8_t nonce_bytes[] = {
0x00,0x00,0x00,0x14, /* payload header */
0x00,0x01,0x02,0x03, /* 16 Byte nonce */
0x04,0x05,0x06,0x07,
0x08,0x09,0x0A,0x2B,
0x0C,0x0D,0x0E,0x0F
};
nonce_chunk.ptr = nonce_bytes;
nonce_chunk.len = sizeof(nonce_bytes);
parser = parser_create(nonce_chunk);
tester->assert_true(tester,(parser != NULL), "parser create check");
status = parser->parse_payload(parser, NONCE, (payload_t**)&nonce_payload);
tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
parser->destroy(parser);
if (status != SUCCESS)
{
return;
}
nonce_payload->get_nonce(nonce_payload, &result);
tester->assert_true(tester,(result.len == 16), "parsed nonce lenght");
tester->assert_false(tester,(memcmp(nonce_bytes + 4, result.ptr, result.len)), "parsed nonce data");
nonce_payload->destroy(nonce_payload);
allocator_free_chunk(&result);
}
/*
* Described in Header
*/
void test_parser_with_id_payload(tester_t *tester)
{
parser_t *parser;
id_payload_t *id_payload;
status_t status;
chunk_t id_chunk, result;
u_int8_t id_bytes[] = {
0x00,0x00,0x00,0x14, /* payload header */
0x05,0x01,0x02,0x03,
0x04,0x05,0x06,0x07,/* 12 Byte nonce */
0x08,0x09,0x0A,0x2B,
0x0C,0x0D,0x0E,0x0F
};
id_chunk.ptr = id_bytes;
id_chunk.len = sizeof(id_bytes);
parser = parser_create(id_chunk);
tester->assert_true(tester,(parser != NULL), "parser create check");
status = parser->parse_payload(parser, ID_INITIATOR, (payload_t**)&id_payload);
tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
parser->destroy(parser);
if (status != SUCCESS)
{
return;
}
result = id_payload->get_data(id_payload);
tester->assert_true(tester,(id_payload->get_initiator(id_payload) == TRUE), "is IDi payload");
tester->assert_true(tester,(id_payload->get_id_type(id_payload) == ID_IPV6_ADDR), "is ID_IPV6_ADDR ID type");
tester->assert_true(tester,(result.len == 12), "parsed data lenght");
tester->assert_false(tester,(memcmp(id_bytes + 8, result.ptr, result.len)), "parsed nonce data");
id_payload->destroy(id_payload);
allocator_free_chunk(&result);
}
/*
* Described in Header
*/
void test_parser_with_ke_payload(tester_t *tester)
{
parser_t *parser;
ke_payload_t *ke_payload;
status_t status;
chunk_t ke_chunk, result;
u_int8_t ke_bytes[] = {
0x00,0x00,0x00,0x18, /* payload header */
0x00,0x03,0x00,0x00, /* dh group 3 */
0x01,0x02,0x03,0x03, /* 16 Byte dh data */
0x04,0x05,0x06,0x07,
0x08,0x09,0x0A,0x2B,
0x0C,0x0D,0x0E,0x0F
};
ke_chunk.ptr = ke_bytes;
ke_chunk.len = sizeof(ke_bytes);
parser = parser_create(ke_chunk);
tester->assert_true(tester,(parser != NULL), "parser create check");
status = parser->parse_payload(parser, KEY_EXCHANGE, (payload_t**)&ke_payload);
tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
parser->destroy(parser);
if (status != SUCCESS)
{
return;
}
tester->assert_true(tester,(ke_payload->get_dh_group_number(ke_payload) == 3), "DH group");
result = ke_payload->get_key_exchange_data(ke_payload);
tester->assert_true(tester,(result.len == 16), "parsed key lenght");
tester->assert_false(tester,(memcmp(ke_bytes + 8, result.ptr, result.len)), "parsed key data");
ke_payload->destroy(ke_payload);
}
/*
* Described in Header
*/
void test_parser_with_notify_payload(tester_t *tester)
{
parser_t *parser;
notify_payload_t *notify_payload;
status_t status;
chunk_t notify_chunk, result;
u_int8_t notify_bytes[] = {
0x00,0x00,0x00,0x1C, /* payload header */
0x03,0x04,0x00,0x01,
0x01,0x02,0x03,0x03, /* spi */
0x04,0x05,0x06,0x07, /* noti dati */
0x08,0x09,0x0A,0x2B,
0x0C,0x0D,0x0E,0x0F,
0x0C,0x0D,0x0E,0x0F
};
notify_chunk.ptr = notify_bytes;
notify_chunk.len = sizeof(notify_bytes);
parser = parser_create(notify_chunk);
tester->assert_true(tester,(parser != NULL), "parser create check");
status = parser->parse_payload(parser, NOTIFY, (payload_t**)¬ify_payload);
tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
parser->destroy(parser);
if (status != SUCCESS)
{
return;
}
tester->assert_true(tester,(notify_payload->get_protocol_id(notify_payload) == 3), "Protocol id");
tester->assert_true(tester,(notify_payload->get_notify_message_type(notify_payload) == 1), "notify message type");
result = notify_payload->get_spi(notify_payload);
tester->assert_false(tester,(memcmp(notify_bytes + 8, result.ptr, result.len)), "parsed spi");
result = notify_payload->get_notification_data(notify_payload);
tester->assert_false(tester,(memcmp(notify_bytes + 12, result.ptr, result.len)), "parsed notification data");
notify_payload->destroy(notify_payload);
}
|