implemented and tested functionality to create 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 ike_proposal_count;
422 sa_payload_t *sa_payload;
423 ike_header_t *ike_header;
424
425 chunk_t data;
426 chunk_t generated_data;
427 logger_t *logger;
428
429 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with SA Payload");
430
431 /* create generator */
432 generator = generator_create();
433 tester->assert_true(tester,(generator != NULL), "generator create check");
434
435 /* --------------------------- */
436 /* test first with self created proposals */
437
438 /* create attribute 1 */
439 attribute1 = transform_attribute_create();
440 char *stringval = "abcd";
441 data.ptr = (void *) stringval;
442 data.len = 4;
443 attribute1->set_value_chunk(attribute1,data);
444 attribute1->set_attribute_type(attribute1,0);
445 logger->log(logger,CONTROL,"attribute1 created");
446
447 /* create attribute 2 */
448 attribute2 = transform_attribute_create();
449 stringval = "efgh";
450 data.ptr = (void *) stringval;
451 data.len = 4;
452 attribute2->set_value_chunk(attribute2,data);
453 attribute2->set_attribute_type(attribute2,0);
454 logger->log(logger,CONTROL,"attribute2 created");
455
456 /* create attribute 3 */
457 attribute3 = transform_attribute_create();
458 stringval = "ijkl";
459 data.ptr = (void *) stringval;
460 data.len = 4;
461 attribute3->set_value_chunk(attribute3,data);
462 attribute3->set_attribute_type(attribute3,0);
463 logger->log(logger,CONTROL,"attribute3 created");
464
465 /* create transform 1*/
466 transform1 = transform_substructure_create();
467 tester->assert_true(tester,(transform1 != NULL), "transform create check");
468 transform1->add_transform_attribute(transform1,attribute1);
469 transform1->add_transform_attribute(transform1,attribute2);
470 transform1->set_transform_type(transform1,5); /* hex 5 */
471 transform1->set_transform_id(transform1,65000); /* hex FDE8 */
472
473 /* create transform 2*/
474 transform2 = transform_substructure_create();
475 tester->assert_true(tester,(transform2 != NULL), "transform create check");
476 transform2->add_transform_attribute(transform2,attribute3);
477 transform2->set_transform_type(transform2,3); /* hex 3 */
478 transform2->set_transform_id(transform2,4); /* hex 4 */
479
480 logger->log(logger,CONTROL,"transforms created");
481
482 /* create proposal 1 */
483 proposal1 = proposal_substructure_create();
484 tester->assert_true(tester,(proposal1 != NULL), "proposal create check");
485
486 stringval = "ABCDEFGH";
487 data.ptr = (void *) stringval;
488 data.len = 8;
489
490 proposal1->add_transform_substructure(proposal1,transform1);
491 proposal1->add_transform_substructure(proposal1,transform2);
492 proposal1->set_spi(proposal1,data);
493 proposal1->set_proposal_number(proposal1,7);
494 proposal1->set_protocol_id(proposal1,4);
495
496 /* create proposal 2 */
497 proposal2 = proposal_substructure_create();
498 tester->assert_true(tester,(proposal2 != NULL), "proposal create check");
499 proposal2->set_proposal_number(proposal2,7);
500 proposal2->set_protocol_id(proposal2,5);
501
502 /* create sa_payload */
503 sa_payload = sa_payload_create();
504
505 sa_payload->add_proposal_substructure(sa_payload,proposal1);
506 sa_payload->add_proposal_substructure(sa_payload,proposal2);
507
508 ike_header = ike_header_create();
509 ike_header->set_initiator_spi(ike_header,0x22000054231234LL);
510 ike_header->set_responder_spi(ike_header,0x122398);
511 ((payload_t *) ike_header)->set_next_type((payload_t *) ike_header,SECURITY_ASSOCIATION);
512 ike_header->set_exchange_type(ike_header, 0x12);
513 ike_header->set_initiator_flag(ike_header, TRUE);
514 ike_header->set_response_flag(ike_header, TRUE);
515 ike_header->set_message_id(ike_header,0x33AFF3);
516
517 generator->generate_payload(generator,(payload_t *)ike_header);
518 generator->generate_payload(generator,(payload_t *)sa_payload);
519 generator->write_to_chunk(generator,&generated_data);
520 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
521
522 u_int8_t expected_generation[] = {
523 /* sa payload header */
524 0x34,0x12,0x23,0x54,
525 0x00,0x00,0x22,0x00,
526 0x98,0x23,0x12,0x00,
527 0x00,0x00,0x00,0x00,
528 0x21,0x20,0x12,0x28,
529 0x00,0x33,0xAF,0xF3,
530 0x00,0x00,0x00,0x60,
531
532 /* sa payload header */
533 0x00,0x00,0x00,0x44,
534 /* proposal header */
535 0x02,0x00,0x00,0x38,
536 0x07,0x04,0x08,0x02,
537 /* SPI */
538 0x41,0x42,0x43,0x44,
539 0x45,0x46,0x47,0x48,
540 /* first transform */
541 0x03,0x00,0x00,0x18,
542 0x05,0x00,0xFD,0xE8,
543 /* first transform attributes */
544 0x00,0x00,0x00,0x04,
545 0x61,0x62,0x63,0x64,
546 0x00,0x00,0x00,0x04,
547 0x65,0x66,0x67,0x68,
548 /* second transform */
549 0x00,0x00,0x00,0x10,
550 0x03,0x00,0x00,0x04,
551 /* second transform attributes */
552 0x00,0x00,0x00,0x04,
553 0x69,0x6A,0x6B,0x6C,
554 /* proposal header 2*/
555 0x00,0x00,0x00,0x08,
556 0x07,0x05,0x00,0x00,
557
558 };
559
560 logger->log_bytes(logger,RAW,"expected transform",expected_generation,sizeof(expected_generation));
561
562 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
563
564 allocator_free_chunk(&generated_data);
565 ike_header->destroy(ike_header);
566 sa_payload->destroy(sa_payload);
567 generator->destroy(generator);
568
569
570 /* test with automatic created proposals */
571
572 generator = generator_create();
573 tester->assert_true(tester,(generator != NULL), "generator create check");
574
575
576 ike_proposal_count = 2;
577 ike_proposals = allocator_alloc(ike_proposal_count * (sizeof(ike_proposal_t)));
578
579 ike_proposals[0].encryption_algorithm = 1;
580 ike_proposals[0].encryption_algorithm_key_length = 20;
581 ike_proposals[0].pseudo_random_function = 2;
582 ike_proposals[0].pseudo_random_function_key_length = 22;
583 ike_proposals[0].integrity_algorithm = 3;
584 ike_proposals[0].integrity_algorithm_key_length = 24;
585 ike_proposals[0].diffie_hellman_group = 4;
586
587 ike_proposals[1].encryption_algorithm = 5;
588 ike_proposals[1].encryption_algorithm_key_length = 26;
589 ike_proposals[1].pseudo_random_function = 6;
590 ike_proposals[1].pseudo_random_function_key_length = 28;
591 ike_proposals[1].integrity_algorithm = 7;
592 ike_proposals[1].integrity_algorithm_key_length = 30;
593 ike_proposals[1].diffie_hellman_group = 8;
594
595 sa_payload = sa_payload_create_from_ike_proposals(ike_proposals,ike_proposal_count);
596 tester->assert_true(tester,(sa_payload != NULL), "sa_payload create check");
597
598 generator->generate_payload(generator,(payload_t *)sa_payload);
599 generator->write_to_chunk(generator,&generated_data);
600 logger->log_chunk(logger,RAW,"generated",&generated_data);
601
602 u_int8_t expected_generation2[] = {
603 0x00,0x00,0x00,0x6C, /* payload header*/
604 0x02,0x00,0x00,0x34, /* a proposal */
605 0x01,0x01,0x00,0x04,
606 0x03,0x00,0x00,0x0C, /* transform 1 */
607 0x01,0x00,0x00,0x01,
608 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
609 0x03,0x00,0x00,0x0C, /* transform 2 */
610 0x02,0x00,0x00,0x02,
611 0x80,0x0E,0x00,0x16, /* keylength attribute with 20 bytes length */
612 0x03,0x00,0x00,0x0C, /* transform 3 */
613 0x03,0x00,0x00,0x03,
614 0x80,0x0E,0x00,0x18, /* keylength attribute with 20 bytes length */
615 0x00,0x00,0x00,0x08, /* transform 4 */
616 0x04,0x00,0x00,0x04,
617 0x00,0x00,0x00,0x34, /* a proposal */
618 0x02,0x01,0x00,0x04,
619 0x03,0x00,0x00,0x0C, /* transform 1 */
620 0x01,0x00,0x00,0x05,
621 0x80,0x0E,0x00,0x1A, /* keylength attribute with 16 bytes length */
622 0x03,0x00,0x00,0x0C, /* transform 2 */
623 0x02,0x00,0x00,0x06,
624 0x80,0x0E,0x00,0x1C, /* keylength attribute with 16 bytes length */
625 0x03,0x00,0x00,0x0C, /* transform 3 */
626 0x03,0x00,0x00,0x07,
627 0x80,0x0E,0x00,0x1E, /* keylength attribute with 16 bytes length */
628 0x00,0x00,0x00,0x08, /* transform 4 */
629 0x04,0x00,0x00,0x08,
630
631 };
632
633 logger->log_bytes(logger,RAW,"expected",expected_generation2,sizeof(expected_generation2));
634
635 tester->assert_true(tester,(memcmp(expected_generation2,generated_data.ptr,sizeof(expected_generation2)) == 0), "compare generated data");
636
637 sa_payload->destroy(sa_payload);
638 allocator_free(ike_proposals);
639 allocator_free_chunk(&generated_data);
640 generator->destroy(generator);
641
642 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
643
644 }
645
646 /*
647 * Described in header
648 */
649 void test_generator_with_ke_payload(tester_t *tester)
650 {
651 generator_t *generator;
652 ke_payload_t *ke_payload;
653 logger_t *logger;
654 chunk_t generated_data;
655 chunk_t key_exchange_data;
656
657 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with KE Payload");
658
659 /* create generator */
660 generator = generator_create();
661 tester->assert_true(tester,(generator != NULL), "generator create check");
662
663 ke_payload = ke_payload_create();
664
665
666 key_exchange_data.ptr = "test-text";
667 key_exchange_data.len = strlen(key_exchange_data.ptr);
668
669 ke_payload->set_key_exchange_data(ke_payload,key_exchange_data);
670
671 ke_payload->set_dh_group_number(ke_payload,7777);
672
673 generator->generate_payload(generator,(payload_t *)ke_payload);
674 generator->write_to_chunk(generator,&generated_data);
675 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
676
677 u_int8_t expected_generation[] = {
678 /* payload header */
679 0x00,0x00,0x00,0x11,
680 0x1E,0x61,0x00,0x00,
681 /* key exchange data */
682 0x74,0x65,0x73,0x74,
683 0x2D,0x74,0x65,0x78,
684 0x74
685 };
686
687
688 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
689
690 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
691
692 allocator_free_chunk(&generated_data);
693
694 ke_payload->destroy(ke_payload);
695 generator->destroy(generator);
696
697 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
698
699 }
700
701 /*
702 * Described in header
703 */
704 void test_generator_with_notify_payload(tester_t *tester)
705 {
706 generator_t *generator;
707 notify_payload_t *notify_payload;
708 logger_t *logger;
709 chunk_t generated_data;
710 chunk_t spi,notification_data;
711
712 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with Notify Payload");
713
714 /* create generator */
715 generator = generator_create();
716 tester->assert_true(tester,(generator != NULL), "generator create check");
717
718 notify_payload = notify_payload_create();
719
720
721 spi.ptr = "12345";
722 spi.len = strlen(spi.ptr);
723
724 notification_data.ptr = "67890";
725 notification_data.len = strlen(notification_data.ptr);
726
727 notify_payload->set_protocol_id(notify_payload,255);
728 notify_payload->set_notify_message_type(notify_payload,63333); /* Hex F765 */
729 notify_payload->set_spi(notify_payload,spi);
730 notify_payload->set_notification_data(notify_payload,notification_data);
731
732 generator->generate_payload(generator,(payload_t *)notify_payload);
733 generator->write_to_chunk(generator,&generated_data);
734 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
735
736 u_int8_t expected_generation[] = {
737 /* payload header */
738 0x00,0x00,0x00,0x12,
739 0xFF,0x05,0xF7,0x65,
740 /* spi */
741 0x31,0x32,0x33,0x34,
742 0x35,
743 /* notification data */
744 0x36,0x37,0x38,0x39,
745 0x30,
746 };
747
748 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
749
750 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
751
752 allocator_free_chunk(&generated_data);
753
754 notify_payload->destroy(notify_payload);
755 generator->destroy(generator);
756
757 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
758
759 }
760
761 /*
762 * Described in header
763 */
764 void test_generator_with_nonce_payload(tester_t *tester)
765 {
766 generator_t *generator;
767 nonce_payload_t *nonce_payload;
768 logger_t *logger;
769 chunk_t generated_data;
770 chunk_t nonce;
771
772 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with Nonce Payload");
773
774 /* create generator */
775 generator = generator_create();
776 tester->assert_true(tester,(generator != NULL), "generator create check");
777
778 nonce_payload = nonce_payload_create();
779
780
781 nonce.ptr = "1234567890123456";
782 nonce.len = strlen("1234567890123456");
783
784 nonce_payload->set_nonce(nonce_payload,nonce);
785
786 generator->generate_payload(generator,(payload_t *)nonce_payload);
787 generator->write_to_chunk(generator,&generated_data);
788 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
789
790
791 u_int8_t expected_generation[] = {
792 /* payload header */
793 0x00,0x00,0x00,0x14,
794 /* nonce data */
795 0x31,0x32,0x33,0x34,
796 0x35,0x36,0x37,0x38,
797 0x39,0x30,0x31,0x32,
798 0x33,0x34,0x35,0x36
799 };
800
801 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
802
803 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
804
805 allocator_free_chunk(&generated_data);
806
807
808 nonce_payload->destroy(nonce_payload);
809 generator->destroy(generator);
810
811 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
812
813 }
814
815 /*
816 * Described in header.
817 */
818 void test_generator_with_id_payload(tester_t *tester)
819 {
820 generator_t *generator;
821 id_payload_t *id_payload;
822 logger_t *logger;
823 chunk_t generated_data;
824 chunk_t id;
825
826 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with ID Payload");
827
828 /* create generator */
829 generator = generator_create();
830 tester->assert_true(tester,(generator != NULL), "generator create check");
831
832 id_payload = id_payload_create(FALSE);
833
834
835 id.ptr = "123456789012";
836 id.len = strlen(id.ptr);
837
838 id_payload->set_id_type(id_payload,ID_IPV4_ADDR);
839 id_payload->set_data(id_payload,id);
840
841 generator->generate_payload(generator,(payload_t *)id_payload);
842 generator->write_to_chunk(generator,&generated_data);
843 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
844
845
846 u_int8_t expected_generation[] = {
847 /* payload header */
848 0x00,0x00,0x00,0x14,
849 0x01,0x00,0x00,0x00,
850 /* id data */
851 0x31,0x32,0x33,0x34,
852 0x35,0x36,0x37,0x38,
853 0x39,0x30,0x31,0x32,
854 };
855
856 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
857
858 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
859
860 allocator_free_chunk(&generated_data);
861
862 id_payload->destroy(id_payload);
863 generator->destroy(generator);
864
865 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
866 }
867
868 /*
869 * Described in header.
870 */
871 void test_generator_with_auth_payload(tester_t *tester)
872 {
873 generator_t *generator;
874 auth_payload_t *auth_payload;
875 logger_t *logger;
876 chunk_t generated_data;
877 chunk_t auth;
878
879 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with AUTH Payload");
880
881 /* create generator */
882 generator = generator_create();
883 tester->assert_true(tester,(generator != NULL), "generator create check");
884
885 auth_payload = auth_payload_create(FALSE);
886
887
888 auth.ptr = "123456789012";
889 auth.len = strlen(auth.ptr);
890
891 auth_payload->set_auth_method(auth_payload,SHARED_KEY_MESSAGE_INTEGRITY_CODE);
892 auth_payload->set_data(auth_payload,auth);
893
894 generator->generate_payload(generator,(payload_t *)auth_payload);
895 generator->write_to_chunk(generator,&generated_data);
896 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
897
898
899 u_int8_t expected_generation[] = {
900 /* payload header */
901 0x00,0x00,0x00,0x14,
902 0x02,0x00,0x00,0x00,
903 /* auth data */
904 0x31,0x32,0x33,0x34,
905 0x35,0x36,0x37,0x38,
906 0x39,0x30,0x31,0x32,
907 };
908
909 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
910
911 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
912
913 allocator_free_chunk(&generated_data);
914
915 auth_payload->destroy(auth_payload);
916 generator->destroy(generator);
917
918 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
919 }
920
921 /*
922 * Described in header.
923 */
924 void test_generator_with_ts_payload(tester_t *tester)
925 {
926 generator_t *generator;
927 ts_payload_t *ts_payload;
928 traffic_selector_substructure_t *ts1, *ts2;
929 host_t *start_host1, *start_host2, *end_host1, *end_host2;
930 logger_t *logger;
931 chunk_t generated_data;
932
933 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with TS Payload");
934
935 /* create generator */
936 generator = generator_create();
937 tester->assert_true(tester,(generator != NULL), "generator create check");
938
939 ts_payload = ts_payload_create(TRUE);
940
941 /* first traffic selector */
942 ts1 = traffic_selector_substructure_create();
943
944 start_host1 = host_create(AF_INET,"192.168.1.0",500);
945 ts1->set_start_host(ts1,start_host1);
946 start_host1->destroy(start_host1);
947
948 end_host1 = host_create(AF_INET,"192.168.1.255",500);
949 ts1->set_end_host(ts1,end_host1);
950 end_host1->destroy(end_host1);
951
952 ts_payload->add_traffic_selector_substructure(ts_payload,ts1);
953
954 /* second traffic selector */
955
956 ts2 = traffic_selector_substructure_create();
957
958 start_host2 = host_create(AF_INET,"0.0.0.0",0);
959 ts2->set_start_host(ts2,start_host2);
960 ts2->set_protocol_id(ts2,3);
961 start_host2->destroy(start_host2);
962
963 end_host2 = host_create(AF_INET,"255.255.255.255",65535);
964 ts2->set_end_host(ts2,end_host2);
965 end_host2->destroy(end_host2);
966
967 ts_payload->add_traffic_selector_substructure(ts_payload,ts2);
968
969
970 generator->generate_payload(generator,(payload_t *)ts_payload);
971 generator->write_to_chunk(generator,&generated_data);
972 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
973
974
975 u_int8_t expected_generation[] = {
976 /* payload header */
977 0x00,0x00,0x00,0x28,
978 0x02,0x00,0x00,0x00,
979
980 /* traffic selector 1 */
981 0x07,0x00,0x00,0x10,
982 0x01,0xF4,0x01,0xF4,
983 0xC0,0xA8,0x01,0x00,
984 0xC0,0xA8,0x01,0xFF,
985
986 /* traffic selector 2 */
987 0x07,0x03,0x00,0x10,
988 0x00,0x00,0xFF,0xFF,
989 0x00,0x00,0x00,0x00,
990 0xFF,0xFF,0xFF,0xFF,
991 };
992
993 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
994
995 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
996
997 allocator_free_chunk(&generated_data);
998
999 ts_payload->destroy(ts_payload);
1000 generator->destroy(generator);
1001
1002 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1003 }