791c6f9ec96cf8316ace03f2d2eea14bc31ce585
[strongswan.git] / Source / charon / testcases / parser_test.c
1 /**
2 * @file parser_test.c
3 *
4 * @brief Tests for the parser_t class.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <string.h>
24
25 #include "parser_test.h"
26
27 #include <utils/allocator.h>
28 #include <utils/logger_manager.h>
29 #include <encoding/generator.h>
30 #include <encoding/parser.h>
31 #include <encoding/payloads/encodings.h>
32 #include <encoding/payloads/ike_header.h>
33 #include <encoding/payloads/sa_payload.h>
34 #include <encoding/payloads/nonce_payload.h>
35 #include <encoding/payloads/id_payload.h>
36 #include <encoding/payloads/ke_payload.h>
37 #include <encoding/payloads/notify_payload.h>
38 #include <encoding/payloads/auth_payload.h>
39 #include <encoding/payloads/cert_payload.h>
40 #include <encoding/payloads/certreq_payload.h>
41 #include <encoding/payloads/ts_payload.h>
42 #include <encoding/payloads/delete_payload.h>
43 #include <encoding/payloads/vendor_id_payload.h>
44 #include <encoding/payloads/cp_payload.h>
45 #include <encoding/payloads/eap_payload.h>
46
47
48 /*
49 * Described in Header
50 */
51 void test_parser_with_header_payload(tester_t *tester)
52 {
53 parser_t *parser;
54 ike_header_t *ike_header;
55 status_t status;
56 chunk_t header_chunk;
57
58 u_int8_t header_bytes[] = {
59 0x01,0x00,0x00,0x00,
60 0x00,0x00,0x00,0x00,
61 0x02,0x00,0x00,0x00,
62 0x00,0x00,0x00,0x00,
63 0x03,0x45,0x06,0x28,
64 0x00,0x00,0x00,0x07,
65 0x00,0x00,0x00,0x1C,
66 };
67 header_chunk.ptr = header_bytes;
68 header_chunk.len = sizeof(header_bytes);
69
70
71 parser = parser_create(header_chunk);
72 tester->assert_true(tester,(parser != NULL), "parser create check");
73 status = parser->parse_payload(parser, HEADER, (payload_t**)&ike_header);
74 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
75 parser->destroy(parser);
76
77 if (status != SUCCESS)
78 {
79 return;
80 }
81
82 tester->assert_true(tester,(ike_header->get_initiator_spi(ike_header) == 1),"parsed initiator_spi value");
83 tester->assert_true(tester,(ike_header->get_responder_spi(ike_header) == 2),"parsed responder_spi value");
84 tester->assert_true(tester,(ike_header->payload_interface.get_next_type((payload_t*)ike_header) == 3),"parsed next_payload value");
85 tester->assert_true(tester,(ike_header->get_maj_version(ike_header) == 4),"parsed maj_version value");
86 tester->assert_true(tester,(ike_header->get_min_version(ike_header) == 5),"parsed min_version value");
87 tester->assert_true(tester,(ike_header->get_exchange_type(ike_header) == 6),"parsed exchange_type value");
88 tester->assert_true(tester,(ike_header->get_initiator_flag(ike_header) == TRUE),"parsed flags.initiator value");
89 tester->assert_true(tester,(ike_header->get_version_flag(ike_header) == FALSE),"parsed flags.version value");
90 tester->assert_true(tester,(ike_header->get_response_flag(ike_header) == TRUE),"parsed flags.response value");
91 tester->assert_true(tester,(ike_header->get_message_id(ike_header) == 7),"parsed message_id value");
92 tester->assert_true(tester,(ike_header->payload_interface.get_length((payload_t*)ike_header) == 0x1C),"parsed length value");
93
94 ike_header->destroy(ike_header);
95 }
96
97 /*
98 * Described in Header
99 */
100 void test_parser_with_sa_payload(tester_t *tester)
101 {
102 parser_t *parser;
103 sa_payload_t *sa_payload;
104 status_t status;
105 chunk_t sa_chunk, sa_chunk2, sa_chunk3;
106 iterator_t *proposals, *transforms, *attributes;
107 ike_proposal_t *ike_proposals;
108 size_t ike_proposal_count;
109 child_proposal_t *child_proposals;
110 size_t child_proposal_count;
111
112 /* first test generic parsing functionality */
113
114 u_int8_t sa_bytes[] = {
115 0x00,0x80,0x00,0x24, /* payload header*/
116 0x00,0x00,0x00,0x20, /* a proposal */
117 0x01,0x02,0x04,0x05,
118 0x01,0x02,0x03,0x04, /* spi */
119 0x00,0x00,0x00,0x14, /* transform */
120 0x07,0x00,0x00,0x03,
121 0x80,0x01,0x00,0x05, /* attribute without length */
122 0x00,0x03,0x00,0x04, /* attribute with length */
123 0x01,0x02,0x03,0x04
124
125
126 };
127
128 sa_chunk.ptr = sa_bytes;
129 sa_chunk.len = sizeof(sa_bytes);
130
131
132 parser = parser_create(sa_chunk);
133 tester->assert_true(tester,(parser != NULL), "parser create check");
134 status = parser->parse_payload(parser, SECURITY_ASSOCIATION, (payload_t**)&sa_payload);
135 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
136 parser->destroy(parser);
137
138 if (status != SUCCESS)
139 {
140 return;
141 }
142
143
144 proposals = sa_payload->create_proposal_substructure_iterator(sa_payload, TRUE);
145 while (proposals->has_next(proposals))
146 {
147 proposal_substructure_t *proposal;
148 proposals->current(proposals, (void**)&proposal);
149 chunk_t spi;
150 u_int8_t spi_should[] = {0x01, 0x02, 0x03, 0x04};
151
152 tester->assert_true(tester,(proposal->get_proposal_number(proposal) == 1),"proposal number");
153 tester->assert_true(tester,(proposal->get_protocol_id(proposal) == 2),"proposal id");
154 spi = proposal->get_spi(proposal);
155 tester->assert_false(tester,(memcmp(&spi_should, spi.ptr, spi.len)),"proposal spi");
156
157 transforms = proposal->create_transform_substructure_iterator(proposal, TRUE);
158 while(transforms->has_next(transforms))
159 {
160 transform_substructure_t *transform;
161 int loopi;
162 transforms->current(transforms, (void**)&transform);
163 tester->assert_true(tester,(transform->get_transform_type(transform) == 7),"transform type");
164 tester->assert_true(tester,(transform->get_transform_id(transform) == 3),"transform id");
165 attributes = transform->create_transform_attribute_iterator(transform, TRUE);
166 loopi = 0;
167 while (attributes->has_next(attributes))
168 {
169 transform_attribute_t *attribute;
170 attributes->current(attributes, (void**)&attribute);
171 if (loopi == 0)
172 {
173 u_int8_t value[] = {0x05, 0x00};
174 chunk_t attribute_value;
175 tester->assert_true(tester,(attribute->get_attribute_type(attribute) == 1),"attribute 1 type");
176 attribute_value = attribute->get_value_chunk(attribute);
177 tester->assert_false(tester,(memcmp(&value, attribute_value.ptr, attribute_value.len)),"attribute 1 value");
178 }
179 if (loopi == 1)
180 {
181 u_int8_t value[] = {0x01, 0x02, 0x03, 0x04};
182 chunk_t attribute_value;
183 tester->assert_true(tester,(attribute->get_attribute_type(attribute) == 3),"attribute 2 type");
184 attribute_value = attribute->get_value_chunk(attribute);
185 tester->assert_false(tester,(memcmp(&value, attribute_value.ptr, attribute_value.len)),"attribute 2 value");
186 }
187 loopi++;
188 }
189 attributes->destroy(attributes);
190 }
191 transforms->destroy(transforms);
192 }
193 proposals->destroy(proposals);
194
195 sa_payload->destroy(sa_payload);
196
197
198
199 /* now test SA functionality after parsing an SA payload*/
200
201 u_int8_t sa_bytes2[] = {
202 0x00,0x00,0x00,0x6C, /* payload header*/
203 0x02,0x00,0x00,0x34, /* a proposal */
204 0x01,0x01,0x00,0x04,
205 0x03,0x00,0x00,0x0C, /* transform 1 */
206 0x01,0x00,0x00,0x01,
207 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
208 0x03,0x00,0x00,0x0C, /* transform 2 */
209 0x02,0x00,0x00,0x01,
210 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
211 0x03,0x00,0x00,0x0C, /* transform 3 */
212 0x03,0x00,0x00,0x01,
213 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
214 0x00,0x00,0x00,0x08, /* transform 4 */
215 0x04,0x00,0x00,0x01,
216 0x00,0x00,0x00,0x34, /* a proposal */
217 0x01,0x01,0x00,0x04,
218 0x03,0x00,0x00,0x0C, /* transform 1 */
219 0x01,0x00,0x00,0x02,
220 0x80,0x0E,0x00,0x10, /* keylength attribute with 16 bytes length */
221 0x03,0x00,0x00,0x0C, /* transform 2 */
222 0x02,0x00,0x00,0x02,
223 0x80,0x0E,0x00,0x10, /* keylength attribute with 16 bytes length */
224 0x03,0x00,0x00,0x0C, /* transform 3 */
225 0x03,0x00,0x00,0x02,
226 0x80,0x0E,0x00,0x10, /* keylength attribute with 16 bytes length */
227 0x00,0x00,0x00,0x08, /* transform 4 */
228 0x04,0x00,0x00,0x02,
229 };
230
231 sa_chunk2.ptr = sa_bytes2;
232 sa_chunk2.len = sizeof(sa_bytes2);
233
234 parser = parser_create(sa_chunk2);
235 tester->assert_true(tester,(parser != NULL), "parser create check");
236 status = parser->parse_payload(parser, SECURITY_ASSOCIATION, (payload_t**)&sa_payload);
237 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
238 parser->destroy(parser);
239
240 if (status != SUCCESS)
241 {
242 return;
243 }
244
245 status = sa_payload->payload_interface.verify(&(sa_payload->payload_interface));
246 tester->assert_true(tester,(status == SUCCESS),"verify call check");
247
248 status = sa_payload->get_ike_proposals (sa_payload, &ike_proposals, &ike_proposal_count);
249 tester->assert_true(tester,(status == SUCCESS),"get ike proposals call check");
250
251 tester->assert_true(tester,(ike_proposal_count == 2),"ike proposal count check");
252 tester->assert_true(tester,(ike_proposals[0].encryption_algorithm == 1),"ike proposal content check");
253 tester->assert_true(tester,(ike_proposals[0].encryption_algorithm_key_length == 20),"ike proposal content check");
254 tester->assert_true(tester,(ike_proposals[0].integrity_algorithm == 1),"ike proposal content check");
255 tester->assert_true(tester,(ike_proposals[0].integrity_algorithm_key_length == 20),"ike proposal content check");
256 tester->assert_true(tester,(ike_proposals[0].pseudo_random_function == 1),"ike proposal content check");
257 tester->assert_true(tester,(ike_proposals[0].pseudo_random_function_key_length == 20),"ike proposal content check");
258 tester->assert_true(tester,(ike_proposals[0].diffie_hellman_group == 1),"ike proposal content check");
259
260 tester->assert_true(tester,(ike_proposals[1].encryption_algorithm == 2),"ike proposal content check");
261 tester->assert_true(tester,(ike_proposals[1].encryption_algorithm_key_length == 16),"ike proposal content check");
262 tester->assert_true(tester,(ike_proposals[1].integrity_algorithm == 2),"ike proposal content check");
263 tester->assert_true(tester,(ike_proposals[1].integrity_algorithm_key_length == 16),"ike proposal content check");
264 tester->assert_true(tester,(ike_proposals[1].pseudo_random_function == 2),"ike proposal content check");
265 tester->assert_true(tester,(ike_proposals[1].pseudo_random_function_key_length == 16),"ike proposal content check");
266 tester->assert_true(tester,(ike_proposals[1].diffie_hellman_group == 2),"ike proposal content check");
267
268
269 if (status == SUCCESS)
270 {
271 allocator_free(ike_proposals);
272 }
273 sa_payload->destroy(sa_payload);
274
275 /* now test SA functionality after parsing an SA payload with child sa proposals*/
276 u_int8_t sa_bytes3[] = {
277 0x00,0x00,0x00,0xA0, /* payload header*/
278
279 /* suite 1 */
280 0x02,0x00,0x00,0x28, /* a proposal */
281 0x01,0x02,0x04,0x03,
282 0x01,0x01,0x01,0x01,
283 0x03,0x00,0x00,0x0C, /* transform 1 */
284 0x03,0x00,0x00,0x01,
285 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
286
287 0x03,0x00,0x00,0x08, /* transform 2 */
288 0x04,0x00,0x00,0x0E,
289
290 0x00,0x00,0x00,0x08, /* transform 3 */
291 0x05,0x00,0x00,0x01,
292
293
294 0x02,0x00,0x00,0x20, /* a proposal */
295 0x01,0x03,0x04,0x02,
296 0x02,0x02,0x02,0x02,
297
298 0x03,0x00,0x00,0x0C, /* transform 1 */
299 0x01,0x00,0x00,0x0C,
300 0x80,0x0E,0x00,0x20, /* keylength attribute with 32 bytes length */
301
302 0x00,0x00,0x00,0x08, /* transform 2 */
303 0x04,0x00,0x00,0x02,
304
305 /* suite 2 */
306 0x02,0x00,0x00,0x28, /* a proposal */
307 0x02,0x02,0x04,0x03,
308 0x01,0x01,0x01,0x01,
309 0x03,0x00,0x00,0x0C, /* transform 1 */
310 0x03,0x00,0x00,0x01,
311 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
312
313 0x03,0x00,0x00,0x08, /* transform 2 */
314 0x04,0x00,0x00,0x0E,
315
316 0x00,0x00,0x00,0x08, /* transform 3 */
317 0x05,0x00,0x00,0x01,
318
319
320 0x00,0x00,0x00,0x2C, /* a proposal */
321 0x02,0x03,0x04,0x03,
322 0x02,0x02,0x02,0x02,
323
324 0x03,0x00,0x00,0x0C, /* transform 1 */
325 0x01,0x00,0x00,0x0C,
326 0x80,0x0E,0x00,0x20, /* keylength attribute with 32 bytes length */
327
328 0x03,0x00,0x00,0x0C, /* transform 2 */
329 0x03,0x00,0x00,0x01,
330 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
331
332 0x00,0x00,0x00,0x08, /* transform 3 */
333 0x04,0x00,0x00,0x02,
334 };
335
336 sa_chunk3.ptr = sa_bytes3;
337 sa_chunk3.len = sizeof(sa_bytes3);
338
339 parser = parser_create(sa_chunk3);
340 tester->assert_true(tester,(parser != NULL), "parser create check");
341 status = parser->parse_payload(parser, SECURITY_ASSOCIATION, (payload_t**)&sa_payload);
342 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
343 parser->destroy(parser);
344
345 if (status != SUCCESS)
346 {
347 return;
348 }
349
350 status = sa_payload->payload_interface.verify(&(sa_payload->payload_interface));
351 tester->assert_true(tester,(status == SUCCESS),"verify call check");
352
353 status = sa_payload->get_ike_proposals (sa_payload, &ike_proposals, &ike_proposal_count);
354 tester->assert_false(tester,(status == SUCCESS),"get ike proposals call check");
355
356 status = sa_payload->get_child_proposals (sa_payload, &child_proposals, &child_proposal_count);
357 tester->assert_true(tester,(status == SUCCESS),"get child proposals call check");
358
359
360 tester->assert_true(tester,(child_proposal_count == 2),"child proposal count check");
361 tester->assert_true(tester,(child_proposals[0].ah.is_set == TRUE),"is ah set check");
362 tester->assert_true(tester,(child_proposals[0].ah.integrity_algorithm == AUTH_HMAC_MD5_96),"integrity_algorithm check");
363 tester->assert_true(tester,(child_proposals[0].ah.integrity_algorithm_key_size == 20),"integrity_algorithm_key_size check");
364 tester->assert_true(tester,(child_proposals[0].ah.diffie_hellman_group == MODP_2048_BIT),"diffie_hellman_group check");
365 tester->assert_true(tester,(child_proposals[0].ah.extended_sequence_numbers == EXT_SEQ_NUMBERS),"extended_sequence_numbers check");
366 tester->assert_true(tester,(child_proposals[0].ah.spi[0] == 1),"spi check");
367 tester->assert_true(tester,(child_proposals[0].ah.spi[1] == 1),"spi check");
368 tester->assert_true(tester,(child_proposals[0].ah.spi[2] == 1),"spi check");
369 tester->assert_true(tester,(child_proposals[0].ah.spi[3] == 1),"spi check");
370
371 tester->assert_true(tester,(child_proposals[0].esp.is_set == TRUE),"is ah set check");
372 tester->assert_true(tester,(child_proposals[0].esp.encryption_algorithm == ENCR_AES_CBC),"integrity_algorithm check");
373 tester->assert_true(tester,(child_proposals[0].esp.encryption_algorithm_key_size == 32),"integrity_algorithm_key_size check");
374 tester->assert_true(tester,(child_proposals[0].esp.diffie_hellman_group == MODP_1024_BIT),"diffie_hellman_group check");
375 tester->assert_true(tester,(child_proposals[0].esp.integrity_algorithm == AUTH_UNDEFINED),"integrity_algorithm check");
376 tester->assert_true(tester,(child_proposals[0].esp.spi[0] == 2),"spi check");
377 tester->assert_true(tester,(child_proposals[0].esp.spi[1] == 2),"spi check");
378 tester->assert_true(tester,(child_proposals[0].esp.spi[2] == 2),"spi check");
379 tester->assert_true(tester,(child_proposals[0].esp.spi[3] == 2),"spi check");
380
381 tester->assert_true(tester,(child_proposals[1].ah.is_set == TRUE),"is ah set check");
382 tester->assert_true(tester,(child_proposals[1].ah.integrity_algorithm == AUTH_HMAC_MD5_96),"integrity_algorithm check");
383 tester->assert_true(tester,(child_proposals[1].ah.integrity_algorithm_key_size == 20),"integrity_algorithm_key_size check");
384 tester->assert_true(tester,(child_proposals[1].ah.diffie_hellman_group == MODP_2048_BIT),"diffie_hellman_group check");
385 tester->assert_true(tester,(child_proposals[1].ah.extended_sequence_numbers == EXT_SEQ_NUMBERS),"extended_sequence_numbers check");
386 tester->assert_true(tester,(child_proposals[1].ah.spi[0] == 1),"spi check");
387 tester->assert_true(tester,(child_proposals[1].ah.spi[1] == 1),"spi check");
388 tester->assert_true(tester,(child_proposals[1].ah.spi[2] == 1),"spi check");
389 tester->assert_true(tester,(child_proposals[1].ah.spi[3] == 1),"spi check");
390
391 tester->assert_true(tester,(child_proposals[1].esp.is_set == TRUE),"is ah set check");
392 tester->assert_true(tester,(child_proposals[1].esp.encryption_algorithm == ENCR_AES_CBC),"integrity_algorithm check");
393 tester->assert_true(tester,(child_proposals[1].esp.encryption_algorithm_key_size == 32),"integrity_algorithm_key_size check");
394 tester->assert_true(tester,(child_proposals[1].esp.diffie_hellman_group == MODP_1024_BIT),"diffie_hellman_group check");
395 tester->assert_true(tester,(child_proposals[1].esp.integrity_algorithm == AUTH_HMAC_MD5_96),"integrity_algorithm check");
396 tester->assert_true(tester,(child_proposals[1].esp.integrity_algorithm_key_size == 20),"integrity_algorithm check");
397 tester->assert_true(tester,(child_proposals[1].esp.spi[0] == 2),"spi check");
398 tester->assert_true(tester,(child_proposals[1].esp.spi[1] == 2),"spi check");
399 tester->assert_true(tester,(child_proposals[1].esp.spi[2] == 2),"spi check");
400 tester->assert_true(tester,(child_proposals[1].esp.spi[3] == 2),"spi check");
401
402 if (status == SUCCESS)
403 {
404 allocator_free(child_proposals);
405 }
406
407
408 sa_payload->destroy(sa_payload);
409 }
410
411 /*
412 * Described in Header
413 */
414 void test_parser_with_nonce_payload(tester_t *tester)
415 {
416 parser_t *parser;
417 nonce_payload_t *nonce_payload;
418 status_t status;
419 chunk_t nonce_chunk, result;
420
421 u_int8_t nonce_bytes[] = {
422 0x00,0x00,0x00,0x14, /* payload header */
423 0x00,0x01,0x02,0x03, /* 16 Byte nonce */
424 0x04,0x05,0x06,0x07,
425 0x08,0x09,0x0A,0x2B,
426 0x0C,0x0D,0x0E,0x0F
427 };
428
429 nonce_chunk.ptr = nonce_bytes;
430 nonce_chunk.len = sizeof(nonce_bytes);
431
432 parser = parser_create(nonce_chunk);
433 tester->assert_true(tester,(parser != NULL), "parser create check");
434 status = parser->parse_payload(parser, NONCE, (payload_t**)&nonce_payload);
435 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
436 parser->destroy(parser);
437
438 if (status != SUCCESS)
439 {
440 return;
441 }
442 result = nonce_payload->get_nonce(nonce_payload);
443 tester->assert_true(tester,(result.len == 16), "parsed nonce lenght");
444 tester->assert_false(tester,(memcmp(nonce_bytes + 4, result.ptr, result.len)), "parsed nonce data");
445 nonce_payload->destroy(nonce_payload);
446 allocator_free_chunk(&result);
447 }
448
449 /*
450 * Described in Header
451 */
452 void test_parser_with_id_payload(tester_t *tester)
453 {
454 parser_t *parser;
455 id_payload_t *id_payload;
456 status_t status;
457 chunk_t id_chunk, result;
458
459 u_int8_t id_bytes[] = {
460 0x00,0x00,0x00,0x14, /* payload header */
461 0x05,0x01,0x02,0x03,
462 0x04,0x05,0x06,0x07,/* 12 Byte nonce */
463 0x08,0x09,0x0A,0x2B,
464 0x0C,0x0D,0x0E,0x0F
465 };
466
467 id_chunk.ptr = id_bytes;
468 id_chunk.len = sizeof(id_bytes);
469
470 parser = parser_create(id_chunk);
471 tester->assert_true(tester,(parser != NULL), "parser create check");
472 status = parser->parse_payload(parser, ID_INITIATOR, (payload_t**)&id_payload);
473 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
474 parser->destroy(parser);
475
476 if (status != SUCCESS)
477 {
478 return;
479 }
480 result = id_payload->get_data_clone(id_payload);
481 tester->assert_true(tester,(id_payload->get_initiator(id_payload) == TRUE), "is IDi payload");
482 tester->assert_true(tester,(id_payload->get_id_type(id_payload) == ID_IPV6_ADDR), "is ID_IPV6_ADDR ID type");
483 tester->assert_true(tester,(result.len == 12), "parsed data lenght");
484 tester->assert_false(tester,(memcmp(id_bytes + 8, result.ptr, result.len)), "parsed nonce data");
485 id_payload->destroy(id_payload);
486 allocator_free_chunk(&result);
487 }
488
489
490 /*
491 * Described in Header
492 */
493 void test_parser_with_ke_payload(tester_t *tester)
494 {
495 parser_t *parser;
496 ke_payload_t *ke_payload;
497 status_t status;
498 chunk_t ke_chunk, result;
499
500 u_int8_t ke_bytes[] = {
501 0x00,0x00,0x00,0x18, /* payload header */
502 0x00,0x03,0x00,0x00, /* dh group 3 */
503 0x01,0x02,0x03,0x03, /* 16 Byte dh data */
504 0x04,0x05,0x06,0x07,
505 0x08,0x09,0x0A,0x2B,
506 0x0C,0x0D,0x0E,0x0F
507 };
508
509 ke_chunk.ptr = ke_bytes;
510 ke_chunk.len = sizeof(ke_bytes);
511
512 parser = parser_create(ke_chunk);
513 tester->assert_true(tester,(parser != NULL), "parser create check");
514 status = parser->parse_payload(parser, KEY_EXCHANGE, (payload_t**)&ke_payload);
515 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
516 parser->destroy(parser);
517
518 if (status != SUCCESS)
519 {
520 return;
521 }
522 tester->assert_true(tester,(ke_payload->get_dh_group_number(ke_payload) == 3), "DH group");
523 result = ke_payload->get_key_exchange_data(ke_payload);
524 tester->assert_true(tester,(result.len == 16), "parsed key lenght");
525 tester->assert_false(tester,(memcmp(ke_bytes + 8, result.ptr, result.len)), "parsed key data");
526 ke_payload->destroy(ke_payload);
527 }
528
529
530 /*
531 * Described in Header
532 */
533 void test_parser_with_notify_payload(tester_t *tester)
534 {
535 parser_t *parser;
536 notify_payload_t *notify_payload;
537 status_t status;
538 chunk_t notify_chunk, result;
539
540 u_int8_t notify_bytes[] = {
541 0x00,0x00,0x00,0x1C, /* payload header */
542 0x03,0x04,0x00,0x01,
543 0x01,0x02,0x03,0x03, /* spi */
544 0x04,0x05,0x06,0x07, /* noti dati */
545 0x08,0x09,0x0A,0x2B,
546 0x0C,0x0D,0x0E,0x0F,
547 0x0C,0x0D,0x0E,0x0F
548 };
549
550 notify_chunk.ptr = notify_bytes;
551 notify_chunk.len = sizeof(notify_bytes);
552
553 parser = parser_create(notify_chunk);
554 tester->assert_true(tester,(parser != NULL), "parser create check");
555 status = parser->parse_payload(parser, NOTIFY, (payload_t**)&notify_payload);
556 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
557 parser->destroy(parser);
558
559 if (status != SUCCESS)
560 {
561 return;
562 }
563 tester->assert_true(tester,(notify_payload->get_protocol_id(notify_payload) == 3), "Protocol id");
564 tester->assert_true(tester,(notify_payload->get_notify_message_type(notify_payload) == 1), "notify message type");
565
566 result = notify_payload->get_spi(notify_payload);
567 tester->assert_false(tester,(memcmp(notify_bytes + 8, result.ptr, result.len)), "parsed spi");
568
569 result = notify_payload->get_notification_data(notify_payload);
570 tester->assert_false(tester,(memcmp(notify_bytes + 12, result.ptr, result.len)), "parsed notification data");
571
572 notify_payload->destroy(notify_payload);
573 }
574
575 /*
576 * Described in Header
577 */
578 void test_parser_with_auth_payload(tester_t *tester)
579 {
580 parser_t *parser;
581 auth_payload_t *auth_payload;
582 status_t status;
583 chunk_t auth_chunk, result;
584
585 u_int8_t auth_bytes[] = {
586 0x00,0x00,0x00,0x14, /* payload header */
587 0x03,0x01,0x02,0x03,
588 0x04,0x05,0x06,0x07,/* 12 Byte nonce */
589 0x08,0x09,0x0A,0x2B,
590 0x0C,0x0D,0x0E,0x0F
591 };
592
593 auth_chunk.ptr = auth_bytes;
594 auth_chunk.len = sizeof(auth_bytes);
595
596 parser = parser_create(auth_chunk);
597 tester->assert_true(tester,(parser != NULL), "parser create check");
598 status = parser->parse_payload(parser, AUTHENTICATION, (payload_t**)&auth_payload);
599 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
600 parser->destroy(parser);
601
602 if (status != SUCCESS)
603 {
604 return;
605 }
606 result = auth_payload->get_data_clone(auth_payload);
607 tester->assert_true(tester,(auth_payload->get_auth_method(auth_payload) == DSS_DIGITAL_SIGNATURE), "is DSS_DIGITAL_SIGNATURE method");
608 tester->assert_true(tester,(result.len == 12), "parsed data lenght");
609 tester->assert_false(tester,(memcmp(auth_bytes + 8, result.ptr, result.len)), "parsed nonce data");
610 auth_payload->destroy(auth_payload);
611 allocator_free_chunk(&result);
612 }
613
614 /*
615 * Described in Header
616 */
617 void test_parser_with_ts_payload(tester_t *tester)
618 {
619 parser_t *parser;
620 ts_payload_t *ts_payload;
621 status_t status;
622 chunk_t ts_chunk;
623 traffic_selector_substructure_t *ts1, *ts2;
624 host_t *start_host1, *start_host2, *end_host1, *end_host2;
625 iterator_t *iterator;
626
627 u_int8_t ts_bytes[] = {
628 /* payload header */
629 0x00,0x00,0x00,0x28,
630 0x02,0x00,0x00,0x00,
631
632 /* traffic selector 1 */
633 0x07,0x00,0x00,0x10,
634 0x01,0xF4,0x01,0xF4,
635 0xC0,0xA8,0x01,0x00,
636 0xC0,0xA8,0x01,0xFF,
637
638 /* traffic selector 2 */
639 0x07,0x03,0x00,0x10,
640 0x00,0x00,0xFF,0xFF,
641 0x00,0x00,0x00,0x00,
642 0xFF,0xFF,0xFF,0xFF,
643 };
644
645 ts_chunk.ptr = ts_bytes;
646 ts_chunk.len = sizeof(ts_bytes);
647
648 parser = parser_create(ts_chunk);
649 tester->assert_true(tester,(parser != NULL), "parser create check");
650 status = parser->parse_payload(parser, TRAFFIC_SELECTOR_RESPONDER, (payload_t**)&ts_payload);
651 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
652 parser->destroy(parser);
653
654 if (status != SUCCESS)
655 {
656 return;
657 }
658
659 iterator = ts_payload->create_traffic_selector_substructure_iterator(ts_payload,TRUE);
660
661 tester->assert_true(tester,(iterator->has_next(iterator)), "has next check");
662
663 /* check first ts */
664 iterator->current(iterator,(void **)&ts1);
665 tester->assert_true(tester,(ts1->get_protocol_id(ts1) == 0), "ip protocol id check");
666 start_host1 = ts1->get_start_host(ts1);
667 end_host1 = ts1->get_end_host(ts1);
668 tester->assert_true(tester,(start_host1->get_port(start_host1) == 500), "start port check");
669 tester->assert_true(tester,(end_host1->get_port(end_host1) == 500), "start port check");
670 tester->assert_true(tester,(memcmp(start_host1->get_address(start_host1),"192.168.1.0",strlen("192.168.1.0")) == 0), "start address check");
671 tester->assert_true(tester,(memcmp(end_host1->get_address(end_host1),"192.168.1.255",strlen("192.168.1.255")) == 0), "end address check");
672
673 start_host1->destroy(start_host1);
674 end_host1->destroy(end_host1);
675
676 tester->assert_true(tester,(iterator->has_next(iterator)), "has next check");
677
678 /* check second ts */
679
680 iterator->current(iterator,(void **)&ts2);
681
682 tester->assert_true(tester,(ts2->get_protocol_id(ts2) == 3), "ip protocol id check");
683 start_host2 = ts2->get_start_host(ts2);
684 end_host2 = ts2->get_end_host(ts2);
685 tester->assert_true(tester,(start_host2->get_port(start_host2) == 0), "start port check");
686 tester->assert_true(tester,(end_host2->get_port(end_host2) == 65535), "start port check");
687 tester->assert_true(tester,(memcmp(start_host2->get_address(start_host2),"0.0.0.0",strlen("0.0.0.0")) == 0), "start address check");
688 tester->assert_true(tester,(memcmp(end_host2->get_address(end_host2),"255.255.255.255",strlen("255.255.255.255")) == 0), "end address check");
689 start_host2->destroy(start_host2);
690 end_host2->destroy(end_host2);
691
692
693
694 tester->assert_false(tester,(iterator->has_next(iterator)), "has next check");
695
696 iterator->destroy(iterator);
697
698 ts_payload->destroy(ts_payload);
699 }
700
701 /*
702 * Described in Header
703 */
704 void test_parser_with_cert_payload(tester_t *tester)
705 {
706 parser_t *parser;
707 cert_payload_t *cert_payload;
708 status_t status;
709 chunk_t cert_chunk, result;
710
711 u_int8_t cert_bytes[] = {
712 0x00,0x00,0x00,0x11, /* payload header */
713 0x03,
714 0x04,0x05,0x06,0x07,/* 12 Byte nonce */
715 0x08,0x09,0x0A,0x2B,
716 0x0C,0x0D,0x0E,0x0F
717 };
718
719 cert_chunk.ptr = cert_bytes;
720 cert_chunk.len = sizeof(cert_bytes);
721
722 parser = parser_create(cert_chunk);
723 tester->assert_true(tester,(parser != NULL), "parser create check");
724 status = parser->parse_payload(parser, CERTIFICATE, (payload_t**)&cert_payload);
725 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
726 parser->destroy(parser);
727
728 if (status != SUCCESS)
729 {
730 return;
731 }
732 result = cert_payload->get_data_clone(cert_payload);
733 tester->assert_true(tester,(cert_payload->get_cert_encoding(cert_payload) == DNS_SIGNED_KEY), "is DNS_SIGNED_KEY encoding");
734 tester->assert_true(tester,(result.len == 12), "parsed data lenght");
735 tester->assert_false(tester,(memcmp(cert_bytes + 5, result.ptr, result.len)), "parsed data");
736 cert_payload->destroy(cert_payload);
737 allocator_free_chunk(&result);
738 }
739
740 /*
741 * Described in Header
742 */
743 void test_parser_with_certreq_payload(tester_t *tester)
744 {
745 parser_t *parser;
746 certreq_payload_t *certreq_payload;
747 status_t status;
748 chunk_t certreq_chunk, result;
749
750 u_int8_t certreq_bytes[] = {
751 0x00,0x00,0x00,0x11, /* payload header */
752 0x03,
753 0x04,0x05,0x06,0x07,/* 12 Byte data */
754 0x08,0x09,0x0A,0x2B,
755 0x0C,0x0D,0x0E,0x0F
756 };
757
758 certreq_chunk.ptr = certreq_bytes;
759 certreq_chunk.len = sizeof(certreq_bytes);
760
761 parser = parser_create(certreq_chunk);
762 tester->assert_true(tester,(parser != NULL), "parser create check");
763 status = parser->parse_payload(parser, CERTIFICATE_REQUEST, (payload_t**)&certreq_payload);
764 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
765 parser->destroy(parser);
766
767 if (status != SUCCESS)
768 {
769 return;
770 }
771 result = certreq_payload->get_data_clone(certreq_payload);
772 tester->assert_true(tester,(certreq_payload->get_cert_encoding(certreq_payload) == DNS_SIGNED_KEY), "is DNS_SIGNED_KEY encoding");
773 tester->assert_true(tester,(result.len == 12), "parsed data lenght");
774 tester->assert_false(tester,(memcmp(certreq_bytes + 5, result.ptr, result.len)), "parsed data");
775 certreq_payload->destroy(certreq_payload);
776 allocator_free_chunk(&result);
777 }
778
779 /*
780 * Described in Header
781 */
782 void test_parser_with_delete_payload(tester_t *tester)
783 {
784 parser_t *parser;
785 delete_payload_t *delete_payload;
786 status_t status;
787 chunk_t delete_chunk, result;
788
789 u_int8_t delete_bytes[] = {
790 0x00,0x00,0x00,0x14, /* payload header */
791 0x03,0x03,0x00,0x04,
792 0x04,0x05,0x06,0x07,/* 12 Byte data */
793 0x08,0x09,0x0A,0x2B,
794 0x0C,0x0D,0x0E,0x0F
795 };
796
797 delete_chunk.ptr = delete_bytes;
798 delete_chunk.len = sizeof(delete_bytes);
799
800 parser = parser_create(delete_chunk);
801 tester->assert_true(tester,(parser != NULL), "parser create check");
802 status = parser->parse_payload(parser, DELETE, (payload_t**)&delete_payload);
803 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
804 parser->destroy(parser);
805
806 if (status != SUCCESS)
807 {
808 return;
809 }
810 result = delete_payload->get_spis(delete_payload);
811 tester->assert_true(tester,(delete_payload->get_protocol_id(delete_payload) == ESP), "is ESP protocol");
812 tester->assert_true(tester,(delete_payload->get_spi_size(delete_payload) == 3), "SPI size check");
813 tester->assert_true(tester,(delete_payload->get_spi_count(delete_payload) == 4), "SPI count check");
814 tester->assert_true(tester,(result.len == 12), "parsed data lenght");
815 tester->assert_false(tester,(memcmp(delete_bytes + 8, result.ptr, result.len)), "parsed data");
816 tester->assert_true(tester,(((payload_t *)delete_payload)->verify((payload_t *)delete_payload) == SUCCESS), "verify check");
817
818 delete_payload->destroy(delete_payload);
819 }
820
821
822 /*
823 * Described in Header
824 */
825 void test_parser_with_vendor_id_payload(tester_t *tester)
826 {
827 parser_t *parser;
828 vendor_id_payload_t *vendor_id_payload;
829 status_t status;
830 chunk_t vendor_id_chunk, result;
831
832 u_int8_t vendor_id_bytes[] = {
833 0x00,0x00,0x00,0x10, /* payload header */
834 0x04,0x05,0x06,0x07,/* 12 Byte data */
835 0x08,0x09,0x0A,0x2B,
836 0x0C,0x0D,0x0E,0x0F
837 };
838
839 vendor_id_chunk.ptr = vendor_id_bytes;
840 vendor_id_chunk.len = sizeof(vendor_id_bytes);
841
842 parser = parser_create(vendor_id_chunk);
843 tester->assert_true(tester,(parser != NULL), "parser create check");
844 status = parser->parse_payload(parser, VENDOR_ID, (payload_t**)&vendor_id_payload);
845 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
846 parser->destroy(parser);
847
848 if (status != SUCCESS)
849 {
850 return;
851 }
852 result = vendor_id_payload->get_data(vendor_id_payload);
853 tester->assert_true(tester,(result.len == 12), "parsed data lenght");
854 tester->assert_false(tester,(memcmp(vendor_id_bytes + 4, result.ptr, result.len)), "parsed data");
855 tester->assert_true(tester,(((payload_t *)vendor_id_payload)->verify((payload_t *)vendor_id_payload) == SUCCESS), "verify check");
856
857 vendor_id_payload->destroy(vendor_id_payload);
858 }
859
860 /*
861 * Described in Header
862 */
863 void test_parser_with_cp_payload(tester_t *tester)
864 {
865 parser_t *parser;
866 cp_payload_t *cp_payload;
867 configuration_attribute_t *attribute;
868 status_t status;
869 chunk_t cp_chunk;
870 iterator_t *iterator;
871
872 /* first test generic parsing functionality */
873
874 u_int8_t cp_bytes[] = {
875 /* cp payload header */
876 0x00,0x00,0x00,0x18,
877 0x05,0x00,0x00,0x00,
878 /* configuration attribute 1*/
879 0x00,0x03,0x00,0x04,
880 0x61,0x62,0x63,0x64,
881 /* configuration attribute 2*/
882 0x00,0x04,0x00,0x04,
883 0x65,0x66,0x67,0x68,
884 };
885
886 cp_chunk.ptr = cp_bytes;
887 cp_chunk.len = sizeof(cp_bytes);
888
889
890 parser = parser_create(cp_chunk);
891 tester->assert_true(tester,(parser != NULL), "parser create check");
892 status = parser->parse_payload(parser, CONFIGURATION, (payload_t**)&cp_payload);
893 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
894
895 iterator = cp_payload->create_configuration_attribute_iterator(cp_payload,TRUE);
896
897 tester->assert_true(tester,(iterator->has_next(iterator)),"has_next call check");
898
899 iterator->current(iterator,(void **)&attribute);
900
901
902 tester->assert_true(tester,(attribute->get_attribute_type(attribute) == 3),"get type check");
903 tester->assert_true(tester,(attribute->get_attribute_length(attribute) == 4),"get type check");
904
905 tester->assert_true(tester,(iterator->has_next(iterator)),"has_next call check");
906
907 iterator->current(iterator,(void **)&attribute);
908
909
910 tester->assert_true(tester,(attribute->get_attribute_type(attribute) == 4),"get type check");
911 tester->assert_true(tester,(attribute->get_attribute_length(attribute) == 4),"get type check");
912
913 iterator->current(iterator,(void **)&attribute);
914
915 tester->assert_false(tester,(iterator->has_next(iterator)),"has_next call check");
916
917
918 iterator->destroy(iterator);
919
920 if (status != SUCCESS)
921 {
922 return;
923 }
924
925 cp_payload->destroy(cp_payload);
926 parser->destroy(parser);
927
928 }
929
930 /*
931 * Described in Header
932 */
933 void test_parser_with_eap_payload(tester_t *tester)
934 {
935 parser_t *parser;
936 eap_payload_t *eap_payload;
937 status_t status;
938 chunk_t eap_chunk, result;
939
940 u_int8_t eap_bytes[] = {
941 0x00,0x00,0x00,0x10, /* payload header */
942 0x04,0x05,0x06,0x07,/* 12 Byte data */
943 0x08,0x09,0x0A,0x2B,
944 0x0C,0x0D,0x0E,0x0F
945 };
946
947 eap_chunk.ptr = eap_bytes;
948 eap_chunk.len = sizeof(eap_bytes);
949
950 parser = parser_create(eap_chunk);
951 tester->assert_true(tester,(parser != NULL), "parser create check");
952 status = parser->parse_payload(parser, VENDOR_ID, (payload_t**)&eap_payload);
953 tester->assert_true(tester,(status == SUCCESS),"parse_payload call check");
954 parser->destroy(parser);
955
956 if (status != SUCCESS)
957 {
958 return;
959 }
960 result = eap_payload->get_message(eap_payload);
961 tester->assert_true(tester,(result.len == 12), "parsed data lenght");
962 tester->assert_false(tester,(memcmp(eap_bytes + 4, result.ptr, result.len)), "parsed data");
963 tester->assert_true(tester,(((payload_t *)eap_payload)->verify((payload_t *)eap_payload) == SUCCESS), "verify check");
964
965 eap_payload->destroy(eap_payload);
966 }
967