de0d13575bed8975322b83d041fc4976ae89fd43
[strongswan.git] / Source / charon / testcases / generator_test.c
1 /**
2 * @file generator_test.c
3 *
4 * @brief Tests for the generator_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 "generator_test.h"
26
27 #include <daemon.h>
28 #include <encoding/generator.h>
29 #include <utils/allocator.h>
30 #include <utils/logger_manager.h>
31 #include <utils/logger.h>
32 #include <encoding/payloads/encodings.h>
33 #include <encoding/payloads/ike_header.h>
34 #include <encoding/payloads/transform_attribute.h>
35 #include <encoding/payloads/transform_substructure.h>
36 #include <encoding/payloads/proposal_substructure.h>
37 #include <encoding/payloads/sa_payload.h>
38 #include <encoding/payloads/ke_payload.h>
39 #include <encoding/payloads/notify_payload.h>
40 #include <encoding/payloads/nonce_payload.h>
41 #include <encoding/payloads/id_payload.h>
42 #include <encoding/payloads/auth_payload.h>
43 #include <encoding/payloads/cert_payload.h>
44 #include <encoding/payloads/ts_payload.h>
45
46 /*
47 * Described in Header
48 */
49 void test_generator_with_header_payload(tester_t *tester)
50 {
51 generator_t *generator;
52 ike_header_t *header_data;
53 chunk_t generated_data;
54 logger_t *logger;
55
56 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"header payload");
57
58 header_data = ike_header_create();
59 header_data->set_initiator_spi(header_data,1);
60 header_data->set_responder_spi(header_data,2);
61 ((payload_t *) header_data)->set_next_type((payload_t *) header_data, 3);
62 header_data->set_exchange_type(header_data, 6);
63 header_data->set_initiator_flag(header_data, TRUE);
64 header_data->set_response_flag(header_data, TRUE);
65 header_data->set_message_id(header_data,7);
66
67 generator = generator_create();
68 tester->assert_true(tester,(generator != NULL), "generator create check");
69
70 generator->generate_payload(generator,(payload_t *) header_data);
71
72 generator->write_to_chunk(generator,&generated_data);
73
74 u_int8_t expected_generation[] = {
75 0x01,0x00,0x00,0x00,
76 0x00,0x00,0x00,0x00,
77 0x02,0x00,0x00,0x00,
78 0x00,0x00,0x00,0x00,
79 0x03,0x20,0x06,0x28,
80 0x00,0x00,0x00,0x07,
81 0x00,0x00,0x00,0x1C,
82 };
83
84 logger->log_bytes(logger,RAW,"expected header",expected_generation,sizeof(expected_generation));
85 tester->assert_true(tester,(generated_data.len == sizeof(expected_generation)), "compare generated data length");
86 logger->log_chunk(logger,RAW,"generated header",&generated_data);
87 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data 1");
88 allocator_free_chunk(&generated_data);
89
90 generator->destroy(generator);
91
92 header_data->set_initiator_spi(header_data,0x22000054231234LL);
93 header_data->set_responder_spi(header_data,0x122398);
94 ((payload_t *) header_data)->set_next_type((payload_t *) header_data,0xF3);
95 header_data->set_exchange_type(header_data, 0x12);
96 header_data->set_initiator_flag(header_data, TRUE);
97 header_data->set_response_flag(header_data, TRUE);
98 header_data->set_message_id(header_data,0x33AFF3);
99
100 generator = generator_create();
101 tester->assert_true(tester,(generator != NULL), "generator create check");
102
103 generator->generate_payload(generator,(payload_t *)header_data);
104
105 generator->write_to_chunk(generator,&generated_data);
106
107 u_int8_t expected_generation2[] = {
108 0x34,0x12,0x23,0x54,
109 0x00,0x00,0x22,0x00,
110 0x98,0x23,0x12,0x00,
111 0x00,0x00,0x00,0x00,
112 0xF3,0x20,0x12,0x28,
113 0x00,0x33,0xAF,0xF3,
114 0x00,0x00,0x00,0x1C,
115 };
116
117
118 logger->log_bytes(logger,RAW,"expected header",expected_generation2,sizeof(expected_generation2));
119
120 logger->log_chunk(logger,RAW,"generated header",&generated_data);
121
122 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data 2");
123 allocator_free_chunk(&generated_data);
124
125 header_data->destroy(header_data);
126
127 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
128 generator->destroy(generator);
129 }
130
131 /*
132 * Described in header
133 */
134 void test_generator_with_transform_attribute(tester_t *tester)
135 {
136 generator_t *generator;
137 transform_attribute_t *attribute;
138 chunk_t generated_data;
139 logger_t *logger;
140
141 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"transform_attribute payload");
142
143
144 /* test empty attribute */
145 generator = generator_create();
146 tester->assert_true(tester,(generator != NULL), "generator create check");
147 attribute = transform_attribute_create();
148 generator->generate_payload(generator,(payload_t *)attribute);
149 generator->write_to_chunk(generator,&generated_data);
150 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
151
152 u_int8_t expected_generation[] = {
153 0x80,0x00,0x00,0x00,
154 };
155 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
156 allocator_free_chunk(&generated_data);
157 attribute->destroy(attribute);
158 generator->destroy(generator);
159
160 /* test attribute with 2 byte data */
161 generator = generator_create();
162 tester->assert_true(tester,(generator != NULL), "generator create check");
163
164 attribute = transform_attribute_create();
165 u_int16_t dataval = 5768;
166 chunk_t data;
167 data.ptr = (void *) &dataval;
168 data.len = 2;
169
170 attribute->set_value_chunk(attribute,data);
171
172 generator->generate_payload(generator,(payload_t *)attribute);
173 generator->write_to_chunk(generator,&generated_data);
174 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
175
176 u_int8_t expected_generation2[] = {
177 0x80,0x00,0x16,0x88,
178 };
179 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data");
180
181 allocator_free_chunk(&generated_data);
182 attribute->destroy(attribute);
183 generator->destroy(generator);
184
185
186
187 /* test attribute with 25 byte data */
188 generator = generator_create();
189 tester->assert_true(tester,(generator != NULL), "generator create check");
190
191 attribute = transform_attribute_create();
192 char *stringval = "ddddddddddeeeeeeeeeefffff";
193 data.ptr = (void *) stringval;
194 data.len = 25;
195
196 attribute->set_value_chunk(attribute,data);
197
198 attribute->set_attribute_type(attribute,456);
199
200
201 generator->generate_payload(generator,(payload_t *)attribute);
202 generator->write_to_chunk(generator,&generated_data);
203 logger->log_chunk(logger,RAW,"generated attribute",&generated_data);
204
205 u_int8_t expected_generation3[] = {
206 0x01,0xC8,0x00,0x19,
207 0x64,0x64,0x64,0x64,
208 0x64,0x64,0x64,0x64,
209 0x64,0x64,0x65,0x65,
210 0x65,0x65,0x65,0x65,
211 0x65,0x65,0x65,0x65,
212 0x66,0x66,0x66,0x66,
213 0x66
214 };
215 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
216
217 allocator_free_chunk(&generated_data);
218 attribute->destroy(attribute);
219 generator->destroy(generator);
220
221
222 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
223 }
224
225
226
227 /*
228 * Described in header
229 */
230 void test_generator_with_transform_substructure(tester_t *tester)
231 {
232 generator_t *generator;
233 transform_attribute_t *attribute1, *attribute2;
234 transform_substructure_t *transform;
235 chunk_t data;
236 chunk_t generated_data;
237 logger_t *logger;
238
239 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"transform substr.");
240
241 /* create generator */
242 generator = generator_create();
243 tester->assert_true(tester,(generator != NULL), "generator create check");
244
245 /* create attribute 1 */
246 attribute1 = transform_attribute_create();
247 char *stringval = "abcd";
248 data.ptr = (void *) stringval;
249 data.len = 4;
250 attribute1->set_value_chunk(attribute1,data);
251 attribute1->set_attribute_type(attribute1,0);
252 logger->log(logger,CONTROL,"attribute1 created");
253
254 /* create attribute 2 */
255 attribute2 = transform_attribute_create();
256 stringval = "efgh";
257 data.ptr = (void *) stringval;
258 data.len = 4;
259 attribute2->set_value_chunk(attribute2,data);
260 attribute2->set_attribute_type(attribute2,0);
261 logger->log(logger,CONTROL,"attribute2 created");
262
263 /* create transform */
264 transform = transform_substructure_create();
265 tester->assert_true(tester,(transform != NULL), "transform create check");
266 transform->add_transform_attribute(transform,attribute1);
267 transform->add_transform_attribute(transform,attribute2);
268 transform->set_transform_type(transform,5); /* hex 5 */
269 transform->set_transform_id(transform,65000); /* hex FDE8 */
270
271
272 logger->log(logger,CONTROL,"transform created");
273
274 generator->generate_payload(generator,(payload_t *)transform);
275 generator->write_to_chunk(generator,&generated_data);
276 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
277
278 u_int8_t expected_generation3[] = {
279 0x00,0x00,0x00,0x18,
280 0x05,0x00,0xFD,0xE8,
281 0x00,0x00,0x00,0x04,
282 0x61,0x62,0x63,0x64,
283 0x00,0x00,0x00,0x04,
284 0x65,0x66,0x67,0x68,
285 };
286 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
287
288 allocator_free_chunk(&generated_data);
289 transform->destroy(transform);
290 generator->destroy(generator);
291
292
293 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
294 }
295
296
297 /*
298 * Described in header
299 */
300 void test_generator_with_proposal_substructure(tester_t *tester)
301 {
302 generator_t *generator;
303 transform_attribute_t *attribute1, *attribute2, *attribute3;
304 transform_substructure_t *transform1, *transform2;
305 proposal_substructure_t *proposal;
306 chunk_t data;
307 chunk_t generated_data;
308 logger_t *logger;
309
310 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"transform substr.");
311
312 /* create generator */
313 generator = generator_create();
314 tester->assert_true(tester,(generator != NULL), "generator create check");
315
316 /* create attribute 1 */
317 attribute1 = transform_attribute_create();
318 char *stringval = "abcd";
319 data.ptr = (void *) stringval;
320 data.len = 4;
321 attribute1->set_value_chunk(attribute1,data);
322 attribute1->set_attribute_type(attribute1,0);
323
324 logger->log(logger,CONTROL,"attribute1 created");
325
326 /* create attribute 2 */
327 attribute2 = transform_attribute_create();
328 stringval = "efgh";
329 data.ptr = (void *) stringval;
330 data.len = 4;
331 attribute2->set_value_chunk(attribute2,data);
332 attribute2->set_attribute_type(attribute2,0);
333 logger->log(logger,CONTROL,"attribute2 created");
334
335 /* create attribute 3 */
336 attribute3 = transform_attribute_create();
337 stringval = "ijkl";
338 data.ptr = (void *) stringval;
339 data.len = 4;
340 attribute3->set_value_chunk(attribute3,data);
341 attribute3->set_attribute_type(attribute3,0);
342 logger->log(logger,CONTROL,"attribute3 created");
343
344 /* create transform 1*/
345 transform1 = transform_substructure_create();
346 tester->assert_true(tester,(transform1 != NULL), "transform create check");
347 transform1->add_transform_attribute(transform1,attribute1);
348 transform1->add_transform_attribute(transform1,attribute2);
349 transform1->set_transform_type(transform1,5); /* hex 5 */
350 transform1->set_transform_id(transform1,65000); /* hex FDE8 */
351
352 /* create transform 2*/
353 transform2 = transform_substructure_create();
354 tester->assert_true(tester,(transform2 != NULL), "transform create check");
355 transform2->add_transform_attribute(transform2,attribute3);
356 transform2->set_transform_type(transform2,3); /* hex 3 */
357 transform2->set_transform_id(transform2,4); /* hex 4 */
358
359 logger->log(logger,CONTROL,"transforms created");
360
361 proposal = proposal_substructure_create();
362 tester->assert_true(tester,(proposal != NULL), "proposal create check");
363
364 stringval = "ABCDEFGH";
365 data.ptr = (void *) stringval;
366 data.len = 8;
367
368 proposal->add_transform_substructure(proposal,transform1);
369 proposal->add_transform_substructure(proposal,transform2);
370 proposal->set_spi(proposal,data);
371 proposal->set_proposal_number(proposal,7);
372 proposal->set_protocol_id(proposal,4);
373
374 generator->generate_payload(generator,(payload_t *)proposal);
375 generator->write_to_chunk(generator,&generated_data);
376 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
377
378 u_int8_t expected_generation[] = {
379 /* proposal header */
380 0x00,0x00,0x00,0x38,
381 0x07,0x04,0x08,0x02,
382 /* SPI */
383 0x41,0x42,0x43,0x44,
384 0x45,0x46,0x47,0x48,
385 /* first transform */
386 0x03,0x00,0x00,0x18,
387 0x05,0x00,0xFD,0xE8,
388 /* first transform attributes */
389 0x00,0x00,0x00,0x04,
390 0x61,0x62,0x63,0x64,
391 0x00,0x00,0x00,0x04,
392 0x65,0x66,0x67,0x68,
393 /* second transform */
394 0x00,0x00,0x00,0x10,
395 0x03,0x00,0x00,0x04,
396 /* second transform attributes */
397 0x00,0x00,0x00,0x04,
398 0x69,0x6A,0x6B,0x6C
399 };
400 logger->log_bytes(logger,RAW,"expected transform",expected_generation,sizeof(expected_generation));
401
402 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
403
404 allocator_free_chunk(&generated_data);
405 proposal->destroy(proposal);
406 generator->destroy(generator);
407
408
409 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
410 }
411
412 /*
413 * Described in header
414 */
415 void test_generator_with_sa_payload(tester_t *tester)
416 {
417 generator_t *generator;
418 transform_attribute_t *attribute1, *attribute2, *attribute3;
419 transform_substructure_t *transform1, *transform2;
420 proposal_substructure_t *proposal1, *proposal2;
421 ike_proposal_t *ike_proposals;
422 size_t child_proposal_count;
423 child_proposal_t *child_proposals;
424 size_t ike_proposal_count;
425 sa_payload_t *sa_payload;
426 ike_header_t *ike_header;
427
428 chunk_t data;
429 chunk_t generated_data;
430 logger_t *logger;
431
432 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with SA Payload");
433
434 /* create generator */
435 generator = generator_create();
436 tester->assert_true(tester,(generator != NULL), "generator create check");
437
438 /* --------------------------- */
439 /* test first with self created proposals */
440
441 /* create attribute 1 */
442 attribute1 = transform_attribute_create();
443 char *stringval = "abcd";
444 data.ptr = (void *) stringval;
445 data.len = 4;
446 attribute1->set_value_chunk(attribute1,data);
447 attribute1->set_attribute_type(attribute1,0);
448 logger->log(logger,CONTROL,"attribute1 created");
449
450 /* create attribute 2 */
451 attribute2 = transform_attribute_create();
452 stringval = "efgh";
453 data.ptr = (void *) stringval;
454 data.len = 4;
455 attribute2->set_value_chunk(attribute2,data);
456 attribute2->set_attribute_type(attribute2,0);
457 logger->log(logger,CONTROL,"attribute2 created");
458
459 /* create attribute 3 */
460 attribute3 = transform_attribute_create();
461 stringval = "ijkl";
462 data.ptr = (void *) stringval;
463 data.len = 4;
464 attribute3->set_value_chunk(attribute3,data);
465 attribute3->set_attribute_type(attribute3,0);
466 logger->log(logger,CONTROL,"attribute3 created");
467
468 /* create transform 1*/
469 transform1 = transform_substructure_create();
470 tester->assert_true(tester,(transform1 != NULL), "transform create check");
471 transform1->add_transform_attribute(transform1,attribute1);
472 transform1->add_transform_attribute(transform1,attribute2);
473 transform1->set_transform_type(transform1,5); /* hex 5 */
474 transform1->set_transform_id(transform1,65000); /* hex FDE8 */
475
476 /* create transform 2*/
477 transform2 = transform_substructure_create();
478 tester->assert_true(tester,(transform2 != NULL), "transform create check");
479 transform2->add_transform_attribute(transform2,attribute3);
480 transform2->set_transform_type(transform2,3); /* hex 3 */
481 transform2->set_transform_id(transform2,4); /* hex 4 */
482
483 logger->log(logger,CONTROL,"transforms created");
484
485 /* create proposal 1 */
486 proposal1 = proposal_substructure_create();
487 tester->assert_true(tester,(proposal1 != NULL), "proposal create check");
488
489 stringval = "ABCDEFGH";
490 data.ptr = (void *) stringval;
491 data.len = 8;
492
493 proposal1->add_transform_substructure(proposal1,transform1);
494 proposal1->add_transform_substructure(proposal1,transform2);
495 proposal1->set_spi(proposal1,data);
496 proposal1->set_proposal_number(proposal1,7);
497 proposal1->set_protocol_id(proposal1,4);
498
499 /* create proposal 2 */
500 proposal2 = proposal_substructure_create();
501 tester->assert_true(tester,(proposal2 != NULL), "proposal create check");
502 proposal2->set_proposal_number(proposal2,7);
503 proposal2->set_protocol_id(proposal2,5);
504
505 /* create sa_payload */
506 sa_payload = sa_payload_create();
507
508 sa_payload->add_proposal_substructure(sa_payload,proposal1);
509 sa_payload->add_proposal_substructure(sa_payload,proposal2);
510
511 ike_header = ike_header_create();
512 ike_header->set_initiator_spi(ike_header,0x22000054231234LL);
513 ike_header->set_responder_spi(ike_header,0x122398);
514 ((payload_t *) ike_header)->set_next_type((payload_t *) ike_header,SECURITY_ASSOCIATION);
515 ike_header->set_exchange_type(ike_header, 0x12);
516 ike_header->set_initiator_flag(ike_header, TRUE);
517 ike_header->set_response_flag(ike_header, TRUE);
518 ike_header->set_message_id(ike_header,0x33AFF3);
519
520 generator->generate_payload(generator,(payload_t *)ike_header);
521 generator->generate_payload(generator,(payload_t *)sa_payload);
522 generator->write_to_chunk(generator,&generated_data);
523 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
524
525 u_int8_t expected_generation[] = {
526 /* sa payload header */
527 0x34,0x12,0x23,0x54,
528 0x00,0x00,0x22,0x00,
529 0x98,0x23,0x12,0x00,
530 0x00,0x00,0x00,0x00,
531 0x21,0x20,0x12,0x28,
532 0x00,0x33,0xAF,0xF3,
533 0x00,0x00,0x00,0x60,
534
535 /* sa payload header */
536 0x00,0x00,0x00,0x44,
537 /* proposal header */
538 0x02,0x00,0x00,0x38,
539 0x07,0x04,0x08,0x02,
540 /* SPI */
541 0x41,0x42,0x43,0x44,
542 0x45,0x46,0x47,0x48,
543 /* first transform */
544 0x03,0x00,0x00,0x18,
545 0x05,0x00,0xFD,0xE8,
546 /* first transform attributes */
547 0x00,0x00,0x00,0x04,
548 0x61,0x62,0x63,0x64,
549 0x00,0x00,0x00,0x04,
550 0x65,0x66,0x67,0x68,
551 /* second transform */
552 0x00,0x00,0x00,0x10,
553 0x03,0x00,0x00,0x04,
554 /* second transform attributes */
555 0x00,0x00,0x00,0x04,
556 0x69,0x6A,0x6B,0x6C,
557 /* proposal header 2*/
558 0x00,0x00,0x00,0x08,
559 0x07,0x05,0x00,0x00,
560
561 };
562
563 logger->log_bytes(logger,RAW,"expected transform",expected_generation,sizeof(expected_generation));
564
565 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
566
567 allocator_free_chunk(&generated_data);
568 ike_header->destroy(ike_header);
569 sa_payload->destroy(sa_payload);
570 generator->destroy(generator);
571
572 /* --------------------------- */
573 /* test with automatic created proposals */
574
575 generator = generator_create();
576 tester->assert_true(tester,(generator != NULL), "generator create check");
577
578
579 ike_proposal_count = 2;
580 ike_proposals = allocator_alloc(ike_proposal_count * (sizeof(ike_proposal_t)));
581
582 ike_proposals[0].encryption_algorithm = 1;
583 ike_proposals[0].encryption_algorithm_key_length = 20;
584 ike_proposals[0].pseudo_random_function = 2;
585 ike_proposals[0].pseudo_random_function_key_length = 22;
586 ike_proposals[0].integrity_algorithm = 3;
587 ike_proposals[0].integrity_algorithm_key_length = 24;
588 ike_proposals[0].diffie_hellman_group = 4;
589
590 ike_proposals[1].encryption_algorithm = 5;
591 ike_proposals[1].encryption_algorithm_key_length = 26;
592 ike_proposals[1].pseudo_random_function = 6;
593 ike_proposals[1].pseudo_random_function_key_length = 28;
594 ike_proposals[1].integrity_algorithm = 7;
595 ike_proposals[1].integrity_algorithm_key_length = 30;
596 ike_proposals[1].diffie_hellman_group = 8;
597
598 sa_payload = sa_payload_create_from_ike_proposals(ike_proposals,ike_proposal_count);
599 tester->assert_true(tester,(sa_payload != NULL), "sa_payload create check");
600
601 generator->generate_payload(generator,(payload_t *)sa_payload);
602 generator->write_to_chunk(generator,&generated_data);
603 logger->log_chunk(logger,RAW,"generated",&generated_data);
604
605 u_int8_t expected_generation2[] = {
606 0x00,0x00,0x00,0x6C, /* payload header*/
607 0x02,0x00,0x00,0x34, /* a proposal */
608 0x01,0x01,0x00,0x04,
609 0x03,0x00,0x00,0x0C, /* transform 1 */
610 0x01,0x00,0x00,0x01,
611 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
612 0x03,0x00,0x00,0x0C, /* transform 2 */
613 0x02,0x00,0x00,0x02,
614 0x80,0x0E,0x00,0x16, /* keylength attribute with 20 bytes length */
615 0x03,0x00,0x00,0x0C, /* transform 3 */
616 0x03,0x00,0x00,0x03,
617 0x80,0x0E,0x00,0x18, /* keylength attribute with 20 bytes length */
618 0x00,0x00,0x00,0x08, /* transform 4 */
619 0x04,0x00,0x00,0x04,
620 0x00,0x00,0x00,0x34, /* a proposal */
621 0x02,0x01,0x00,0x04,
622 0x03,0x00,0x00,0x0C, /* transform 1 */
623 0x01,0x00,0x00,0x05,
624 0x80,0x0E,0x00,0x1A, /* keylength attribute with 16 bytes length */
625 0x03,0x00,0x00,0x0C, /* transform 2 */
626 0x02,0x00,0x00,0x06,
627 0x80,0x0E,0x00,0x1C, /* keylength attribute with 16 bytes length */
628 0x03,0x00,0x00,0x0C, /* transform 3 */
629 0x03,0x00,0x00,0x07,
630 0x80,0x0E,0x00,0x1E, /* keylength attribute with 16 bytes length */
631 0x00,0x00,0x00,0x08, /* transform 4 */
632 0x04,0x00,0x00,0x08,
633
634 };
635
636 logger->log_bytes(logger,RAW,"expected",expected_generation2,sizeof(expected_generation2));
637
638 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data");
639
640 sa_payload->destroy(sa_payload);
641 allocator_free(ike_proposals);
642 allocator_free_chunk(&generated_data);
643 generator->destroy(generator);
644
645
646 /* --------------------------- */
647 /* test with automatic created child proposals */
648
649 generator = generator_create();
650 tester->assert_true(tester,(generator != NULL), "generator create check");
651
652
653 child_proposal_count = 2;
654 child_proposals = allocator_alloc(child_proposal_count * (sizeof(child_proposal_t)));
655
656 child_proposals[0].ah.is_set = TRUE;
657 child_proposals[0].ah.integrity_algorithm = AUTH_HMAC_MD5_96;
658 child_proposals[0].ah.integrity_algorithm_key_size = 20;
659 child_proposals[0].ah.diffie_hellman_group = MODP_2048_BIT;
660 child_proposals[0].ah.extended_sequence_numbers = EXT_SEQ_NUMBERS;
661 child_proposals[0].ah.spi[0] = 1;
662 child_proposals[0].ah.spi[1] = 1;
663 child_proposals[0].ah.spi[2] = 1;
664 child_proposals[0].ah.spi[3] = 1;
665
666 child_proposals[0].esp.is_set = TRUE;
667 child_proposals[0].esp.diffie_hellman_group = MODP_1024_BIT;
668 child_proposals[0].esp.encryption_algorithm = ENCR_AES_CBC;
669 child_proposals[0].esp.encryption_algorithm_key_size = 32;
670 child_proposals[0].esp.integrity_algorithm = AUTH_UNDEFINED;
671 child_proposals[0].esp.spi[0] = 2;
672 child_proposals[0].esp.spi[1] = 2;
673 child_proposals[0].esp.spi[2] = 2;
674 child_proposals[0].esp.spi[3] = 2;
675
676 child_proposals[1].ah.is_set = TRUE;
677 child_proposals[1].ah.integrity_algorithm = AUTH_HMAC_MD5_96;
678 child_proposals[1].ah.integrity_algorithm_key_size = 20;
679 child_proposals[1].ah.diffie_hellman_group = MODP_2048_BIT;
680 child_proposals[1].ah.extended_sequence_numbers = EXT_SEQ_NUMBERS;
681 child_proposals[1].ah.spi[0] = 1;
682 child_proposals[1].ah.spi[1] = 1;
683 child_proposals[1].ah.spi[2] = 1;
684 child_proposals[1].ah.spi[3] = 1;
685
686 child_proposals[1].esp.is_set = TRUE;
687 child_proposals[1].esp.diffie_hellman_group = MODP_1024_BIT;
688 child_proposals[1].esp.encryption_algorithm = ENCR_AES_CBC;
689 child_proposals[1].esp.encryption_algorithm_key_size = 32;
690 child_proposals[1].esp.integrity_algorithm = AUTH_HMAC_MD5_96;
691 child_proposals[1].esp.integrity_algorithm_key_size = 20;
692 child_proposals[1].esp.spi[0] = 2;
693 child_proposals[1].esp.spi[1] = 2;
694 child_proposals[1].esp.spi[2] = 2;
695 child_proposals[1].esp.spi[3] = 2;
696
697
698 sa_payload = sa_payload_create_from_child_proposals(child_proposals,child_proposal_count);
699 tester->assert_true(tester,(sa_payload != NULL), "sa_payload create check");
700
701 generator->generate_payload(generator,(payload_t *)sa_payload);
702 generator->write_to_chunk(generator,&generated_data);
703 logger->log_chunk(logger,RAW,"generated",&generated_data);
704
705 u_int8_t expected_generation3[] = {
706 0x00,0x00,0x00,0xA0, /* payload header*/
707
708 /* suite 1 */
709 0x02,0x00,0x00,0x28, /* a proposal */
710 0x01,0x02,0x04,0x03,
711 0x01,0x01,0x01,0x01,
712 0x03,0x00,0x00,0x0C, /* transform 1 */
713 0x03,0x00,0x00,0x01,
714 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
715
716 0x03,0x00,0x00,0x08, /* transform 2 */
717 0x04,0x00,0x00,0x0E,
718
719 0x00,0x00,0x00,0x08, /* transform 3 */
720 0x05,0x00,0x00,0x01,
721
722
723 0x02,0x00,0x00,0x20, /* a proposal */
724 0x01,0x03,0x04,0x02,
725 0x02,0x02,0x02,0x02,
726
727 0x03,0x00,0x00,0x0C, /* transform 1 */
728 0x01,0x00,0x00,0x0C,
729 0x80,0x0E,0x00,0x20, /* keylength attribute with 32 bytes length */
730
731 0x00,0x00,0x00,0x08, /* transform 2 */
732 0x04,0x00,0x00,0x02,
733
734 /* suite 2 */
735 0x02,0x00,0x00,0x28, /* a proposal */
736 0x02,0x02,0x04,0x03,
737 0x01,0x01,0x01,0x01,
738 0x03,0x00,0x00,0x0C, /* transform 1 */
739 0x03,0x00,0x00,0x01,
740 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
741
742 0x03,0x00,0x00,0x08, /* transform 2 */
743 0x04,0x00,0x00,0x0E,
744
745 0x00,0x00,0x00,0x08, /* transform 3 */
746 0x05,0x00,0x00,0x01,
747
748
749 0x00,0x00,0x00,0x2C, /* a proposal */
750 0x02,0x03,0x04,0x03,
751 0x02,0x02,0x02,0x02,
752
753 0x03,0x00,0x00,0x0C, /* transform 1 */
754 0x01,0x00,0x00,0x0C,
755 0x80,0x0E,0x00,0x20, /* keylength attribute with 32 bytes length */
756
757 0x03,0x00,0x00,0x0C, /* transform 2 */
758 0x03,0x00,0x00,0x01,
759 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
760
761 0x00,0x00,0x00,0x08, /* transform 3 */
762 0x04,0x00,0x00,0x02,
763
764 };
765
766
767 logger->log_bytes(logger,RAW,"expected",expected_generation3,sizeof(expected_generation3));
768
769 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
770
771 sa_payload->destroy(sa_payload);
772 allocator_free(child_proposals);
773 allocator_free_chunk(&generated_data);
774 generator->destroy(generator);
775
776
777 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
778
779 }
780
781 /*
782 * Described in header
783 */
784 void test_generator_with_ke_payload(tester_t *tester)
785 {
786 generator_t *generator;
787 ke_payload_t *ke_payload;
788 logger_t *logger;
789 chunk_t generated_data;
790 chunk_t key_exchange_data;
791
792 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with KE Payload");
793
794 /* create generator */
795 generator = generator_create();
796 tester->assert_true(tester,(generator != NULL), "generator create check");
797
798 ke_payload = ke_payload_create();
799
800
801 key_exchange_data.ptr = "test-text";
802 key_exchange_data.len = strlen(key_exchange_data.ptr);
803
804 ke_payload->set_key_exchange_data(ke_payload,key_exchange_data);
805
806 ke_payload->set_dh_group_number(ke_payload,7777);
807
808 generator->generate_payload(generator,(payload_t *)ke_payload);
809 generator->write_to_chunk(generator,&generated_data);
810 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
811
812 u_int8_t expected_generation[] = {
813 /* payload header */
814 0x00,0x00,0x00,0x11,
815 0x1E,0x61,0x00,0x00,
816 /* key exchange data */
817 0x74,0x65,0x73,0x74,
818 0x2D,0x74,0x65,0x78,
819 0x74
820 };
821
822
823 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
824
825 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
826
827 allocator_free_chunk(&generated_data);
828
829 ke_payload->destroy(ke_payload);
830 generator->destroy(generator);
831
832 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
833
834 }
835
836 /*
837 * Described in header
838 */
839 void test_generator_with_notify_payload(tester_t *tester)
840 {
841 generator_t *generator;
842 notify_payload_t *notify_payload;
843 logger_t *logger;
844 chunk_t generated_data;
845 chunk_t spi,notification_data;
846
847 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with Notify Payload");
848
849 /* create generator */
850 generator = generator_create();
851 tester->assert_true(tester,(generator != NULL), "generator create check");
852
853 notify_payload = notify_payload_create();
854
855
856 spi.ptr = "12345";
857 spi.len = strlen(spi.ptr);
858
859 notification_data.ptr = "67890";
860 notification_data.len = strlen(notification_data.ptr);
861
862 notify_payload->set_protocol_id(notify_payload,255);
863 notify_payload->set_notify_message_type(notify_payload,63333); /* Hex F765 */
864 notify_payload->set_spi(notify_payload,spi);
865 notify_payload->set_notification_data(notify_payload,notification_data);
866
867 generator->generate_payload(generator,(payload_t *)notify_payload);
868 generator->write_to_chunk(generator,&generated_data);
869 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
870
871 u_int8_t expected_generation[] = {
872 /* payload header */
873 0x00,0x00,0x00,0x12,
874 0xFF,0x05,0xF7,0x65,
875 /* spi */
876 0x31,0x32,0x33,0x34,
877 0x35,
878 /* notification data */
879 0x36,0x37,0x38,0x39,
880 0x30,
881 };
882
883 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
884
885 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
886
887 allocator_free_chunk(&generated_data);
888
889 notify_payload->destroy(notify_payload);
890 generator->destroy(generator);
891
892 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
893
894 }
895
896 /*
897 * Described in header
898 */
899 void test_generator_with_nonce_payload(tester_t *tester)
900 {
901 generator_t *generator;
902 nonce_payload_t *nonce_payload;
903 logger_t *logger;
904 chunk_t generated_data;
905 chunk_t nonce;
906
907 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with Nonce Payload");
908
909 /* create generator */
910 generator = generator_create();
911 tester->assert_true(tester,(generator != NULL), "generator create check");
912
913 nonce_payload = nonce_payload_create();
914
915
916 nonce.ptr = "1234567890123456";
917 nonce.len = strlen("1234567890123456");
918
919 nonce_payload->set_nonce(nonce_payload,nonce);
920
921 generator->generate_payload(generator,(payload_t *)nonce_payload);
922 generator->write_to_chunk(generator,&generated_data);
923 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
924
925
926 u_int8_t expected_generation[] = {
927 /* payload header */
928 0x00,0x00,0x00,0x14,
929 /* nonce data */
930 0x31,0x32,0x33,0x34,
931 0x35,0x36,0x37,0x38,
932 0x39,0x30,0x31,0x32,
933 0x33,0x34,0x35,0x36
934 };
935
936 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
937
938 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
939
940 allocator_free_chunk(&generated_data);
941
942
943 nonce_payload->destroy(nonce_payload);
944 generator->destroy(generator);
945
946 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
947
948 }
949
950 /*
951 * Described in header.
952 */
953 void test_generator_with_id_payload(tester_t *tester)
954 {
955 generator_t *generator;
956 id_payload_t *id_payload;
957 logger_t *logger;
958 chunk_t generated_data;
959 chunk_t id;
960
961 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with ID Payload");
962
963 /* create generator */
964 generator = generator_create();
965 tester->assert_true(tester,(generator != NULL), "generator create check");
966
967 id_payload = id_payload_create(FALSE);
968
969
970 id.ptr = "123456789012";
971 id.len = strlen(id.ptr);
972
973 id_payload->set_id_type(id_payload,ID_IPV4_ADDR);
974 id_payload->set_data(id_payload,id);
975
976 generator->generate_payload(generator,(payload_t *)id_payload);
977 generator->write_to_chunk(generator,&generated_data);
978 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
979
980
981 u_int8_t expected_generation[] = {
982 /* payload header */
983 0x00,0x00,0x00,0x14,
984 0x01,0x00,0x00,0x00,
985 /* id data */
986 0x31,0x32,0x33,0x34,
987 0x35,0x36,0x37,0x38,
988 0x39,0x30,0x31,0x32,
989 };
990
991 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
992
993 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
994
995 allocator_free_chunk(&generated_data);
996
997 id_payload->destroy(id_payload);
998 generator->destroy(generator);
999
1000 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1001 }
1002
1003 /*
1004 * Described in header.
1005 */
1006 void test_generator_with_auth_payload(tester_t *tester)
1007 {
1008 generator_t *generator;
1009 auth_payload_t *auth_payload;
1010 logger_t *logger;
1011 chunk_t generated_data;
1012 chunk_t auth;
1013
1014 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with AUTH Payload");
1015
1016 /* create generator */
1017 generator = generator_create();
1018 tester->assert_true(tester,(generator != NULL), "generator create check");
1019
1020 auth_payload = auth_payload_create(FALSE);
1021
1022
1023 auth.ptr = "123456789012";
1024 auth.len = strlen(auth.ptr);
1025
1026 auth_payload->set_auth_method(auth_payload,SHARED_KEY_MESSAGE_INTEGRITY_CODE);
1027 auth_payload->set_data(auth_payload,auth);
1028
1029 generator->generate_payload(generator,(payload_t *)auth_payload);
1030 generator->write_to_chunk(generator,&generated_data);
1031 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
1032
1033
1034 u_int8_t expected_generation[] = {
1035 /* payload header */
1036 0x00,0x00,0x00,0x14,
1037 0x02,0x00,0x00,0x00,
1038 /* auth data */
1039 0x31,0x32,0x33,0x34,
1040 0x35,0x36,0x37,0x38,
1041 0x39,0x30,0x31,0x32,
1042 };
1043
1044 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
1045
1046 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
1047
1048 allocator_free_chunk(&generated_data);
1049
1050 auth_payload->destroy(auth_payload);
1051 generator->destroy(generator);
1052
1053 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1054 }
1055
1056 /*
1057 * Described in header.
1058 */
1059 void test_generator_with_cert_payload(tester_t *tester)
1060 {
1061 generator_t *generator;
1062 cert_payload_t *cert_payload;
1063 logger_t *logger;
1064 chunk_t generated_data;
1065 chunk_t cert;
1066
1067 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with CERT Payload");
1068
1069 /* create generator */
1070 generator = generator_create();
1071 tester->assert_true(tester,(generator != NULL), "generator create check");
1072
1073 cert_payload = cert_payload_create();
1074
1075
1076 cert.ptr = "123456789012";
1077 cert.len = strlen(cert.ptr);
1078
1079 cert_payload->set_cert_encoding(cert_payload,PGP_CERTIFICATE);
1080 cert_payload->set_data(cert_payload,cert);
1081
1082 generator->generate_payload(generator,(payload_t *)cert_payload);
1083 generator->write_to_chunk(generator,&generated_data);
1084 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
1085
1086 u_int8_t expected_generation[] = {
1087 /* payload header */
1088 0x00,0x00,0x00,0x11,
1089 0x02,
1090 /* cert data */
1091 0x31,0x32,0x33,0x34,
1092 0x35,0x36,0x37,0x38,
1093 0x39,0x30,0x31,0x32,
1094 };
1095
1096 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
1097
1098 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
1099
1100 allocator_free_chunk(&generated_data);
1101
1102 cert_payload->destroy(cert_payload);
1103 generator->destroy(generator);
1104
1105 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1106 }
1107
1108 /*
1109 * Described in header.
1110 */
1111 void test_generator_with_ts_payload(tester_t *tester)
1112 {
1113 generator_t *generator;
1114 ts_payload_t *ts_payload;
1115 traffic_selector_substructure_t *ts1, *ts2;
1116 host_t *start_host1, *start_host2, *end_host1, *end_host2;
1117 logger_t *logger;
1118 chunk_t generated_data;
1119
1120 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with TS Payload");
1121
1122 /* create generator */
1123 generator = generator_create();
1124 tester->assert_true(tester,(generator != NULL), "generator create check");
1125
1126 ts_payload = ts_payload_create(TRUE);
1127
1128 /* first traffic selector */
1129 ts1 = traffic_selector_substructure_create();
1130
1131 start_host1 = host_create(AF_INET,"192.168.1.0",500);
1132 ts1->set_start_host(ts1,start_host1);
1133 start_host1->destroy(start_host1);
1134
1135 end_host1 = host_create(AF_INET,"192.168.1.255",500);
1136 ts1->set_end_host(ts1,end_host1);
1137 end_host1->destroy(end_host1);
1138
1139 ts_payload->add_traffic_selector_substructure(ts_payload,ts1);
1140
1141 /* second traffic selector */
1142
1143 ts2 = traffic_selector_substructure_create();
1144
1145 start_host2 = host_create(AF_INET,"0.0.0.0",0);
1146 ts2->set_start_host(ts2,start_host2);
1147 ts2->set_protocol_id(ts2,3);
1148 start_host2->destroy(start_host2);
1149
1150 end_host2 = host_create(AF_INET,"255.255.255.255",65535);
1151 ts2->set_end_host(ts2,end_host2);
1152 end_host2->destroy(end_host2);
1153
1154 ts_payload->add_traffic_selector_substructure(ts_payload,ts2);
1155
1156
1157 generator->generate_payload(generator,(payload_t *)ts_payload);
1158 generator->write_to_chunk(generator,&generated_data);
1159 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
1160
1161
1162 u_int8_t expected_generation[] = {
1163 /* payload header */
1164 0x00,0x00,0x00,0x28,
1165 0x02,0x00,0x00,0x00,
1166
1167 /* traffic selector 1 */
1168 0x07,0x00,0x00,0x10,
1169 0x01,0xF4,0x01,0xF4,
1170 0xC0,0xA8,0x01,0x00,
1171 0xC0,0xA8,0x01,0xFF,
1172
1173 /* traffic selector 2 */
1174 0x07,0x03,0x00,0x10,
1175 0x00,0x00,0xFF,0xFF,
1176 0x00,0x00,0x00,0x00,
1177 0xFF,0xFF,0xFF,0xFF,
1178 };
1179
1180 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
1181
1182 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
1183
1184 allocator_free_chunk(&generated_data);
1185
1186 ts_payload->destroy(ts_payload);
1187 generator->destroy(generator);
1188
1189 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1190 }