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