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