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