- refactored ike proposal
[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(protected_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(protected_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(protected_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(protected_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(protected_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 *proposal_str1, *proposal_str2;
426 linked_list_t *list;
427 proposal_t *proposal1, *proposal2;
428 sa_payload_t *sa_payload;
429 ike_header_t *ike_header;
430
431 chunk_t data;
432 chunk_t generated_data;
433 logger_t *logger;
434
435 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with SA Payload");
436
437 /* create generator */
438 generator = generator_create();
439 tester->assert_true(tester,(generator != NULL), "generator create check");
440
441 /* --------------------------- */
442 /* test first with self created proposals */
443
444 /* create attribute 1 */
445 attribute1 = transform_attribute_create();
446 char *stringval = "abcd";
447 data.ptr = (void *) stringval;
448 data.len = 4;
449 attribute1->set_value_chunk(attribute1,data);
450 attribute1->set_attribute_type(attribute1,0);
451 logger->log(logger,CONTROL,"attribute1 created");
452
453 /* create attribute 2 */
454 attribute2 = transform_attribute_create();
455 stringval = "efgh";
456 data.ptr = (void *) stringval;
457 data.len = 4;
458 attribute2->set_value_chunk(attribute2,data);
459 attribute2->set_attribute_type(attribute2,0);
460 logger->log(logger,CONTROL,"attribute2 created");
461
462 /* create attribute 3 */
463 attribute3 = transform_attribute_create();
464 stringval = "ijkl";
465 data.ptr = (void *) stringval;
466 data.len = 4;
467 attribute3->set_value_chunk(attribute3,data);
468 attribute3->set_attribute_type(attribute3,0);
469 logger->log(logger,CONTROL,"attribute3 created");
470
471 /* create transform 1*/
472 transform1 = transform_substructure_create();
473 tester->assert_true(tester,(transform1 != NULL), "transform create check");
474 transform1->add_transform_attribute(transform1,attribute1);
475 transform1->add_transform_attribute(transform1,attribute2);
476 transform1->set_transform_type(transform1,5); /* hex 5 */
477 transform1->set_transform_id(transform1,65000); /* hex FDE8 */
478
479 /* create transform 2*/
480 transform2 = transform_substructure_create();
481 tester->assert_true(tester,(transform2 != NULL), "transform create check");
482 transform2->add_transform_attribute(transform2,attribute3);
483 transform2->set_transform_type(transform2,3); /* hex 3 */
484 transform2->set_transform_id(transform2,4); /* hex 4 */
485
486 logger->log(logger,CONTROL,"transforms created");
487
488 /* create proposal 1 */
489 proposal_str1 = proposal_substructure_create();
490 tester->assert_true(tester,(proposal1 != NULL), "proposal create check");
491
492 stringval = "ABCDEFGH";
493 data.ptr = (void *) stringval;
494 data.len = 8;
495
496 proposal_str1->add_transform_substructure(proposal_str1,transform1);
497 proposal_str1->add_transform_substructure(proposal_str1,transform2);
498 proposal_str1->set_spi(proposal_str1,data);
499 proposal_str1->set_proposal_number(proposal_str1,7);
500 proposal_str1->set_protocol_id(proposal_str1,4);
501
502 /* create proposal 2 */
503 proposal_str2 = proposal_substructure_create();
504 tester->assert_true(tester,(proposal_str2 != NULL), "proposal create check");
505 proposal_str2->set_proposal_number(proposal_str2,7);
506 proposal_str2->set_protocol_id(proposal_str2,5);
507
508 /* create sa_payload */
509 sa_payload = sa_payload_create();
510
511 sa_payload->add_proposal_substructure(sa_payload,proposal_str1);
512 sa_payload->add_proposal_substructure(sa_payload,proposal_str2);
513
514 ike_header = ike_header_create();
515 ike_header->set_initiator_spi(ike_header,0x22000054231234LL);
516 ike_header->set_responder_spi(ike_header,0x122398);
517 ((payload_t *) ike_header)->set_next_type((payload_t *) ike_header,SECURITY_ASSOCIATION);
518 ike_header->set_exchange_type(ike_header, 0x12);
519 ike_header->set_initiator_flag(ike_header, TRUE);
520 ike_header->set_response_flag(ike_header, TRUE);
521 ike_header->set_message_id(ike_header,0x33AFF3);
522
523 generator->generate_payload(generator,(payload_t *)ike_header);
524 generator->generate_payload(generator,(payload_t *)sa_payload);
525 generator->write_to_chunk(generator,&generated_data);
526 logger->log_chunk(logger,RAW,"generated transform",&generated_data);
527
528 u_int8_t expected_generation[] = {
529 /* sa payload header */
530 0x34,0x12,0x23,0x54,
531 0x00,0x00,0x22,0x00,
532 0x98,0x23,0x12,0x00,
533 0x00,0x00,0x00,0x00,
534 0x21,0x20,0x12,0x28,
535 0x00,0x33,0xAF,0xF3,
536 0x00,0x00,0x00,0x60,
537
538 /* sa payload header */
539 0x00,0x00,0x00,0x44,
540 /* proposal header */
541 0x02,0x00,0x00,0x38,
542 0x07,0x04,0x08,0x02,
543 /* SPI */
544 0x41,0x42,0x43,0x44,
545 0x45,0x46,0x47,0x48,
546 /* first transform */
547 0x03,0x00,0x00,0x18,
548 0x05,0x00,0xFD,0xE8,
549 /* first transform attributes */
550 0x00,0x00,0x00,0x04,
551 0x61,0x62,0x63,0x64,
552 0x00,0x00,0x00,0x04,
553 0x65,0x66,0x67,0x68,
554 /* second transform */
555 0x00,0x00,0x00,0x10,
556 0x03,0x00,0x00,0x04,
557 /* second transform attributes */
558 0x00,0x00,0x00,0x04,
559 0x69,0x6A,0x6B,0x6C,
560 /* proposal header 2*/
561 0x00,0x00,0x00,0x08,
562 0x07,0x05,0x00,0x00,
563
564 };
565
566 logger->log_bytes(logger,RAW,"expected transform",expected_generation,sizeof(expected_generation));
567
568 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
569
570 allocator_free_chunk(&generated_data);
571 ike_header->destroy(ike_header);
572 sa_payload->destroy(sa_payload);
573 generator->destroy(generator);
574
575 /* --------------------------- */
576 /* test with automatic created proposals */
577
578 generator = generator_create();
579 tester->assert_true(tester,(generator != NULL), "generator create check");
580
581
582 proposal1 = proposal_create(1);
583 proposal1->add_algorithm(proposal1, IKE, ENCRYPTION_ALGORITHM, 1, 20);
584 proposal1->add_algorithm(proposal1, IKE, PSEUDO_RANDOM_FUNCTION, 2, 22);
585 proposal1->add_algorithm(proposal1, IKE, INTEGRITY_ALGORITHM, 3, 24);
586 proposal1->add_algorithm(proposal1, IKE, DIFFIE_HELLMAN_GROUP, 4, 0);
587
588 proposal2 = proposal_create(2);
589 proposal2->add_algorithm(proposal2, IKE, ENCRYPTION_ALGORITHM, 5, 26);
590 proposal2->add_algorithm(proposal2, IKE, PSEUDO_RANDOM_FUNCTION, 6, 28);
591 proposal2->add_algorithm(proposal2, IKE, INTEGRITY_ALGORITHM, 7, 30);
592 proposal2->add_algorithm(proposal2, IKE, DIFFIE_HELLMAN_GROUP, 8, 0);
593
594 list = linked_list_create();
595 list->insert_last(list, (void*)proposal1);
596 list->insert_last(list, (void*)proposal2);
597 sa_payload = sa_payload_create_from_proposal_list(list);
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 list->destroy(list);
641 proposal1->destroy(proposal1);
642 proposal2->destroy(proposal2);
643 allocator_free_chunk(&generated_data);
644 generator->destroy(generator);
645
646
647 /* --------------------------- */
648 /* test with automatic created child proposals */
649
650 generator = generator_create();
651 tester->assert_true(tester,(generator != NULL), "generator create check");
652
653
654 proposal1 = proposal_create(1);
655
656 proposal1->add_algorithm(proposal1, AH, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 20);
657 proposal1->add_algorithm(proposal1, AH, DIFFIE_HELLMAN_GROUP, MODP_2048_BIT, 0);
658 proposal1->add_algorithm(proposal1, AH, EXTENDED_SEQUENCE_NUMBERS, EXT_SEQ_NUMBERS, 0);
659 proposal1->set_spi(proposal1, AH, 0x01010101l);
660
661 proposal1->add_algorithm(proposal1, ESP, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 20);
662 proposal1->add_algorithm(proposal1, ESP, DIFFIE_HELLMAN_GROUP, MODP_1024_BIT, 0);
663 proposal1->set_spi(proposal1, ESP, 0x02020202);
664
665
666 proposal2->add_algorithm(proposal2, AH, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 20);
667 proposal2->add_algorithm(proposal2, AH, DIFFIE_HELLMAN_GROUP, MODP_2048_BIT, 0);
668 proposal2->add_algorithm(proposal2, AH, EXTENDED_SEQUENCE_NUMBERS, EXT_SEQ_NUMBERS, 0);
669 proposal2->set_spi(proposal2, AH, 0x01010101);
670
671 proposal2->add_algorithm(proposal2, ESP, ENCRYPTION_ALGORITHM, ENCR_AES_CBC, 32);
672 proposal2->add_algorithm(proposal2, ESP, INTEGRITY_ALGORITHM, AUTH_HMAC_MD5_96, 20);
673 proposal2->add_algorithm(proposal2, ESP, DIFFIE_HELLMAN_GROUP, MODP_1024_BIT, 0);
674 proposal2->set_spi(proposal2, ESP, 0x02020202);
675
676 list->insert_last(list, (void*)proposal1);
677 list->insert_last(list, (void*)proposal2);
678
679 sa_payload = sa_payload_create_from_proposal_list(list);
680 tester->assert_true(tester,(sa_payload != NULL), "sa_payload create check");
681
682 generator->generate_payload(generator,(payload_t *)sa_payload);
683 generator->write_to_chunk(generator,&generated_data);
684 logger->log_chunk(logger,RAW,"generated",&generated_data);
685
686 u_int8_t expected_generation3[] = {
687 0x00,0x00,0x00,0xA0, /* payload header*/
688
689 /* suite 1 */
690 0x02,0x00,0x00,0x28, /* a proposal */
691 0x01,0x02,0x04,0x03,
692 0x01,0x01,0x01,0x01,
693 0x03,0x00,0x00,0x0C, /* transform 1 */
694 0x03,0x00,0x00,0x01,
695 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
696
697 0x03,0x00,0x00,0x08, /* transform 2 */
698 0x04,0x00,0x00,0x0E,
699
700 0x00,0x00,0x00,0x08, /* transform 3 */
701 0x05,0x00,0x00,0x01,
702
703
704 0x02,0x00,0x00,0x20, /* a proposal */
705 0x01,0x03,0x04,0x02,
706 0x02,0x02,0x02,0x02,
707
708 0x03,0x00,0x00,0x0C, /* transform 1 */
709 0x01,0x00,0x00,0x0C,
710 0x80,0x0E,0x00,0x20, /* keylength attribute with 32 bytes length */
711
712 0x00,0x00,0x00,0x08, /* transform 2 */
713 0x04,0x00,0x00,0x02,
714
715 /* suite 2 */
716 0x02,0x00,0x00,0x28, /* a proposal */
717 0x02,0x02,0x04,0x03,
718 0x01,0x01,0x01,0x01,
719 0x03,0x00,0x00,0x0C, /* transform 1 */
720 0x03,0x00,0x00,0x01,
721 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
722
723 0x03,0x00,0x00,0x08, /* transform 2 */
724 0x04,0x00,0x00,0x0E,
725
726 0x00,0x00,0x00,0x08, /* transform 3 */
727 0x05,0x00,0x00,0x01,
728
729
730 0x00,0x00,0x00,0x2C, /* a proposal */
731 0x02,0x03,0x04,0x03,
732 0x02,0x02,0x02,0x02,
733
734 0x03,0x00,0x00,0x0C, /* transform 1 */
735 0x01,0x00,0x00,0x0C,
736 0x80,0x0E,0x00,0x20, /* keylength attribute with 32 bytes length */
737
738 0x03,0x00,0x00,0x0C, /* transform 2 */
739 0x03,0x00,0x00,0x01,
740 0x80,0x0E,0x00,0x14, /* keylength attribute with 20 bytes length */
741
742 0x00,0x00,0x00,0x08, /* transform 3 */
743 0x04,0x00,0x00,0x02,
744
745 };
746
747
748 logger->log_bytes(logger,RAW,"expected",expected_generation3,sizeof(expected_generation3));
749
750 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
751
752 sa_payload->destroy(sa_payload);
753 proposal1->destroy(proposal1);
754 proposal2->destroy(proposal2);
755 list->destroy(list);
756 allocator_free_chunk(&generated_data);
757 generator->destroy(generator);
758
759
760 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
761
762 }
763
764 /*
765 * Described in header
766 */
767 void test_generator_with_ke_payload(protected_tester_t *tester)
768 {
769 generator_t *generator;
770 ke_payload_t *ke_payload;
771 logger_t *logger;
772 chunk_t generated_data;
773 chunk_t key_exchange_data;
774
775 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with KE Payload");
776
777 /* create generator */
778 generator = generator_create();
779 tester->assert_true(tester,(generator != NULL), "generator create check");
780
781 ke_payload = ke_payload_create();
782
783
784 key_exchange_data.ptr = "test-text";
785 key_exchange_data.len = strlen(key_exchange_data.ptr);
786
787 ke_payload->set_key_exchange_data(ke_payload,key_exchange_data);
788
789 ke_payload->set_dh_group_number(ke_payload,7777);
790
791 generator->generate_payload(generator,(payload_t *)ke_payload);
792 generator->write_to_chunk(generator,&generated_data);
793 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
794
795 u_int8_t expected_generation[] = {
796 /* payload header */
797 0x00,0x00,0x00,0x11,
798 0x1E,0x61,0x00,0x00,
799 /* key exchange data */
800 0x74,0x65,0x73,0x74,
801 0x2D,0x74,0x65,0x78,
802 0x74
803 };
804
805
806 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
807
808 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
809
810 allocator_free_chunk(&generated_data);
811
812 ke_payload->destroy(ke_payload);
813 generator->destroy(generator);
814
815 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
816
817 }
818
819 /*
820 * Described in header
821 */
822 void test_generator_with_notify_payload(protected_tester_t *tester)
823 {
824 generator_t *generator;
825 notify_payload_t *notify_payload;
826 logger_t *logger;
827 chunk_t generated_data;
828 chunk_t spi,notification_data;
829
830 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with Notify Payload");
831
832 /* create generator */
833 generator = generator_create();
834 tester->assert_true(tester,(generator != NULL), "generator create check");
835
836 notify_payload = notify_payload_create();
837
838
839 spi.ptr = "12345";
840 spi.len = strlen(spi.ptr);
841
842 notification_data.ptr = "67890";
843 notification_data.len = strlen(notification_data.ptr);
844
845 notify_payload->set_protocol_id(notify_payload,255);
846 notify_payload->set_notify_message_type(notify_payload,63333); /* Hex F765 */
847 notify_payload->set_spi(notify_payload,spi);
848 notify_payload->set_notification_data(notify_payload,notification_data);
849
850 generator->generate_payload(generator,(payload_t *)notify_payload);
851 generator->write_to_chunk(generator,&generated_data);
852 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
853
854 u_int8_t expected_generation[] = {
855 /* payload header */
856 0x00,0x00,0x00,0x12,
857 0xFF,0x05,0xF7,0x65,
858 /* spi */
859 0x31,0x32,0x33,0x34,
860 0x35,
861 /* notification data */
862 0x36,0x37,0x38,0x39,
863 0x30,
864 };
865
866 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
867
868 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
869
870 allocator_free_chunk(&generated_data);
871
872 notify_payload->destroy(notify_payload);
873 generator->destroy(generator);
874
875 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
876
877 }
878
879 /*
880 * Described in header
881 */
882 void test_generator_with_nonce_payload(protected_tester_t *tester)
883 {
884 generator_t *generator;
885 nonce_payload_t *nonce_payload;
886 logger_t *logger;
887 chunk_t generated_data;
888 chunk_t nonce;
889
890 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with Nonce Payload");
891
892 /* create generator */
893 generator = generator_create();
894 tester->assert_true(tester,(generator != NULL), "generator create check");
895
896 nonce_payload = nonce_payload_create();
897
898
899 nonce.ptr = "1234567890123456";
900 nonce.len = strlen("1234567890123456");
901
902 nonce_payload->set_nonce(nonce_payload,nonce);
903
904 generator->generate_payload(generator,(payload_t *)nonce_payload);
905 generator->write_to_chunk(generator,&generated_data);
906 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
907
908
909 u_int8_t expected_generation[] = {
910 /* payload header */
911 0x00,0x00,0x00,0x14,
912 /* nonce data */
913 0x31,0x32,0x33,0x34,
914 0x35,0x36,0x37,0x38,
915 0x39,0x30,0x31,0x32,
916 0x33,0x34,0x35,0x36
917 };
918
919 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
920
921 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
922
923 allocator_free_chunk(&generated_data);
924
925
926 nonce_payload->destroy(nonce_payload);
927 generator->destroy(generator);
928
929 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
930
931 }
932
933 /*
934 * Described in header.
935 */
936 void test_generator_with_id_payload(protected_tester_t *tester)
937 {
938 generator_t *generator;
939 id_payload_t *id_payload;
940 logger_t *logger;
941 chunk_t generated_data;
942 chunk_t id;
943
944 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with ID Payload");
945
946 /* create generator */
947 generator = generator_create();
948 tester->assert_true(tester,(generator != NULL), "generator create check");
949
950 id_payload = id_payload_create(FALSE);
951
952
953 id.ptr = "123456789012";
954 id.len = strlen(id.ptr);
955
956 id_payload->set_id_type(id_payload,ID_IPV4_ADDR);
957 id_payload->set_data(id_payload,id);
958
959 generator->generate_payload(generator,(payload_t *)id_payload);
960 generator->write_to_chunk(generator,&generated_data);
961 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
962
963
964 u_int8_t expected_generation[] = {
965 /* payload header */
966 0x00,0x00,0x00,0x14,
967 0x01,0x00,0x00,0x00,
968 /* id data */
969 0x31,0x32,0x33,0x34,
970 0x35,0x36,0x37,0x38,
971 0x39,0x30,0x31,0x32,
972 };
973
974 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
975
976 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
977
978 allocator_free_chunk(&generated_data);
979
980 id_payload->destroy(id_payload);
981 generator->destroy(generator);
982
983 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
984 }
985
986 /*
987 * Described in header.
988 */
989 void test_generator_with_auth_payload(protected_tester_t *tester)
990 {
991 generator_t *generator;
992 auth_payload_t *auth_payload;
993 logger_t *logger;
994 chunk_t generated_data;
995 chunk_t auth;
996
997 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with AUTH Payload");
998
999 /* create generator */
1000 generator = generator_create();
1001 tester->assert_true(tester,(generator != NULL), "generator create check");
1002
1003 auth_payload = auth_payload_create(FALSE);
1004
1005
1006 auth.ptr = "123456789012";
1007 auth.len = strlen(auth.ptr);
1008
1009 auth_payload->set_auth_method(auth_payload,SHARED_KEY_MESSAGE_INTEGRITY_CODE);
1010 auth_payload->set_data(auth_payload,auth);
1011
1012 generator->generate_payload(generator,(payload_t *)auth_payload);
1013 generator->write_to_chunk(generator,&generated_data);
1014 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
1015
1016
1017 u_int8_t expected_generation[] = {
1018 /* payload header */
1019 0x00,0x00,0x00,0x14,
1020 0x02,0x00,0x00,0x00,
1021 /* auth data */
1022 0x31,0x32,0x33,0x34,
1023 0x35,0x36,0x37,0x38,
1024 0x39,0x30,0x31,0x32,
1025 };
1026
1027 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
1028
1029 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
1030
1031 allocator_free_chunk(&generated_data);
1032
1033 auth_payload->destroy(auth_payload);
1034 generator->destroy(generator);
1035
1036 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1037 }
1038
1039 /*
1040 * Described in header.
1041 */
1042 void test_generator_with_ts_payload(protected_tester_t *tester)
1043 {
1044 generator_t *generator;
1045 ts_payload_t *ts_payload;
1046 traffic_selector_substructure_t *ts1, *ts2;
1047 host_t *start_host1, *start_host2, *end_host1, *end_host2;
1048 logger_t *logger;
1049 chunk_t generated_data;
1050
1051 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with TS Payload");
1052
1053 /* create generator */
1054 generator = generator_create();
1055 tester->assert_true(tester,(generator != NULL), "generator create check");
1056
1057 ts_payload = ts_payload_create(TRUE);
1058
1059 /* first traffic selector */
1060 ts1 = traffic_selector_substructure_create();
1061
1062 start_host1 = host_create(AF_INET,"192.168.1.0",500);
1063 ts1->set_start_host(ts1,start_host1);
1064 start_host1->destroy(start_host1);
1065
1066 end_host1 = host_create(AF_INET,"192.168.1.255",500);
1067 ts1->set_end_host(ts1,end_host1);
1068 end_host1->destroy(end_host1);
1069
1070 ts_payload->add_traffic_selector_substructure(ts_payload,ts1);
1071
1072 /* second traffic selector */
1073
1074 ts2 = traffic_selector_substructure_create();
1075
1076 start_host2 = host_create(AF_INET,"0.0.0.0",0);
1077 ts2->set_start_host(ts2,start_host2);
1078 ts2->set_protocol_id(ts2,3);
1079 start_host2->destroy(start_host2);
1080
1081 end_host2 = host_create(AF_INET,"255.255.255.255",65535);
1082 ts2->set_end_host(ts2,end_host2);
1083 end_host2->destroy(end_host2);
1084
1085 ts_payload->add_traffic_selector_substructure(ts_payload,ts2);
1086
1087
1088 generator->generate_payload(generator,(payload_t *)ts_payload);
1089 generator->write_to_chunk(generator,&generated_data);
1090 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
1091
1092
1093 u_int8_t expected_generation[] = {
1094 /* payload header */
1095 0x00,0x00,0x00,0x28,
1096 0x02,0x00,0x00,0x00,
1097
1098 /* traffic selector 1 */
1099 0x07,0x00,0x00,0x10,
1100 0x01,0xF4,0x01,0xF4,
1101 0xC0,0xA8,0x01,0x00,
1102 0xC0,0xA8,0x01,0xFF,
1103
1104 /* traffic selector 2 */
1105 0x07,0x03,0x00,0x10,
1106 0x00,0x00,0xFF,0xFF,
1107 0x00,0x00,0x00,0x00,
1108 0xFF,0xFF,0xFF,0xFF,
1109 };
1110
1111 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
1112
1113 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
1114
1115 allocator_free_chunk(&generated_data);
1116
1117 ts_payload->destroy(ts_payload);
1118 generator->destroy(generator);
1119
1120 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1121 }
1122
1123 /*
1124 * Described in header.
1125 */
1126 void test_generator_with_cert_payload(protected_tester_t *tester)
1127 {
1128 generator_t *generator;
1129 cert_payload_t *cert_payload;
1130 logger_t *logger;
1131 chunk_t generated_data;
1132 chunk_t cert;
1133
1134 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with CERT Payload");
1135
1136 /* create generator */
1137 generator = generator_create();
1138 tester->assert_true(tester,(generator != NULL), "generator create check");
1139
1140 cert_payload = cert_payload_create();
1141
1142
1143 cert.ptr = "123456789012";
1144 cert.len = strlen(cert.ptr);
1145
1146 cert_payload->set_cert_encoding(cert_payload,PGP_CERTIFICATE);
1147 cert_payload->set_data(cert_payload,cert);
1148
1149 generator->generate_payload(generator,(payload_t *)cert_payload);
1150 generator->write_to_chunk(generator,&generated_data);
1151 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
1152
1153 u_int8_t expected_generation[] = {
1154 /* payload header */
1155 0x00,0x00,0x00,0x11,
1156 0x02,
1157 /* cert data */
1158 0x31,0x32,0x33,0x34,
1159 0x35,0x36,0x37,0x38,
1160 0x39,0x30,0x31,0x32,
1161 };
1162
1163 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
1164
1165 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
1166
1167 allocator_free_chunk(&generated_data);
1168
1169 cert_payload->destroy(cert_payload);
1170 generator->destroy(generator);
1171
1172 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1173 }
1174
1175 /*
1176 * Described in header.
1177 */
1178 void test_generator_with_certreq_payload(protected_tester_t *tester)
1179 {
1180 generator_t *generator;
1181 certreq_payload_t *certreq_payload;
1182 logger_t *logger;
1183 chunk_t generated_data;
1184 chunk_t certreq;
1185
1186 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with CERTREQ Payload");
1187
1188 /* create generator */
1189 generator = generator_create();
1190 tester->assert_true(tester,(generator != NULL), "generator create check");
1191
1192 certreq_payload = certreq_payload_create();
1193
1194
1195 certreq.ptr = "123456789012";
1196 certreq.len = strlen(certreq.ptr);
1197
1198 certreq_payload->set_cert_encoding(certreq_payload,PGP_CERTIFICATE);
1199 certreq_payload->set_data(certreq_payload,certreq);
1200
1201 generator->generate_payload(generator,(payload_t *)certreq_payload);
1202 generator->write_to_chunk(generator,&generated_data);
1203 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
1204
1205 u_int8_t expected_generation[] = {
1206 /* payload header */
1207 0x00,0x00,0x00,0x11,
1208 0x02,
1209 /* certreq data */
1210 0x31,0x32,0x33,0x34,
1211 0x35,0x36,0x37,0x38,
1212 0x39,0x30,0x31,0x32,
1213 };
1214
1215 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
1216
1217 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
1218
1219 allocator_free_chunk(&generated_data);
1220
1221 certreq_payload->destroy(certreq_payload);
1222 generator->destroy(generator);
1223
1224 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1225 }
1226
1227 /*
1228 * Described in header.
1229 */
1230 void test_generator_with_delete_payload(protected_tester_t *tester)
1231 {
1232 generator_t *generator;
1233 delete_payload_t *delete_payload;
1234 logger_t *logger;
1235 chunk_t generated_data;
1236 chunk_t spis;
1237
1238 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with DELETE Payload");
1239
1240 /* create generator */
1241 generator = generator_create();
1242 tester->assert_true(tester,(generator != NULL), "generator create check");
1243
1244 delete_payload = delete_payload_create();
1245
1246
1247 spis.ptr = "123456789012";
1248 spis.len = strlen(spis.ptr);
1249
1250 delete_payload->set_protocol_id(delete_payload,AH);
1251 delete_payload->set_spi_count(delete_payload,3);
1252 delete_payload->set_spi_size(delete_payload,4);
1253 delete_payload->set_spis(delete_payload,spis);
1254
1255 generator->generate_payload(generator,(payload_t *)delete_payload);
1256 generator->write_to_chunk(generator,&generated_data);
1257 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
1258
1259 u_int8_t expected_generation[] = {
1260 /* payload header */
1261 0x00,0x00,0x00,0x14,
1262 0x02,0x04,0x00,0x03,
1263 /* delete data */
1264 0x31,0x32,0x33,0x34,
1265 0x35,0x36,0x37,0x38,
1266 0x39,0x30,0x31,0x32,
1267 };
1268
1269 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
1270
1271 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
1272
1273 allocator_free_chunk(&generated_data);
1274
1275 delete_payload->destroy(delete_payload);
1276 generator->destroy(generator);
1277
1278 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1279 }
1280
1281 /*
1282 * Described in header.
1283 */
1284 void test_generator_with_vendor_id_payload(protected_tester_t *tester)
1285 {
1286 generator_t *generator;
1287 vendor_id_payload_t *vendor_id_payload;
1288 logger_t *logger;
1289 chunk_t generated_data;
1290 chunk_t data;
1291
1292 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with VENDOR ID Payload");
1293
1294 /* create generator */
1295 generator = generator_create();
1296 tester->assert_true(tester,(generator != NULL), "generator create check");
1297
1298 vendor_id_payload = vendor_id_payload_create();
1299
1300
1301 data.ptr = "123456789012";
1302 data.len = strlen(data.ptr);
1303 ;
1304 vendor_id_payload->set_data(vendor_id_payload,data);
1305 generator->generate_payload(generator,(payload_t *)vendor_id_payload);
1306 generator->write_to_chunk(generator,&generated_data);
1307 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
1308
1309 u_int8_t expected_generation[] = {
1310 /* payload header */
1311 0x00,0x00,0x00,0x10,
1312 /* vendor_id data */
1313 0x31,0x32,0x33,0x34,
1314 0x35,0x36,0x37,0x38,
1315 0x39,0x30,0x31,0x32,
1316 };
1317
1318 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
1319
1320 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
1321
1322 allocator_free_chunk(&generated_data);
1323
1324 vendor_id_payload->destroy(vendor_id_payload);
1325 generator->destroy(generator);
1326
1327 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1328 }
1329
1330 /*
1331 * Described in header
1332 */
1333 void test_generator_with_cp_payload(protected_tester_t *tester)
1334 {
1335 generator_t *generator;
1336 configuration_attribute_t *attribute1, *attribute2;
1337 cp_payload_t *configuration;
1338 chunk_t data;
1339 chunk_t generated_data;
1340 logger_t *logger;
1341
1342 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"CP Payload");
1343
1344 /* create generator */
1345 generator = generator_create();
1346 tester->assert_true(tester,(generator != NULL), "generator create check");
1347
1348 /* create attribute 1 */
1349 attribute1 = configuration_attribute_create();
1350 char *stringval = "abcd";
1351 data.ptr = (void *) stringval;
1352 data.len = 4;
1353 attribute1->set_value(attribute1,data);
1354 attribute1->set_attribute_type(attribute1,3);
1355 logger->log(logger,CONTROL,"attribute1 created");
1356
1357 /* create attribute 2 */
1358 attribute2 = configuration_attribute_create();
1359 stringval = "efgh";
1360 data.ptr = (void *) stringval;
1361 data.len = 4;
1362 attribute2->set_value(attribute2,data);
1363 attribute2->set_attribute_type(attribute2,4);
1364 logger->log(logger,CONTROL,"attribute2 created");
1365
1366 /* create configuration */
1367 configuration = cp_payload_create();
1368 tester->assert_true(tester,(configuration != NULL), "configuration create check");
1369 configuration->add_configuration_attribute(configuration,attribute1);
1370 configuration->add_configuration_attribute(configuration,attribute2);
1371 configuration->set_config_type(configuration,5); /* hex 5 */
1372
1373
1374 logger->log(logger,CONTROL,"cp payload created");
1375
1376 generator->generate_payload(generator,(payload_t *)configuration);
1377 generator->write_to_chunk(generator,&generated_data);
1378 logger->log_chunk(logger,RAW,"generated configuration",&generated_data);
1379
1380 u_int8_t expected_generation3[] = {
1381 /* cp payload header */
1382 0x00,0x00,0x00,0x18,
1383 0x05,0x00,0x00,0x00,
1384 /* configuration attribute 1*/
1385 0x00,0x03,0x00,0x04,
1386 0x61,0x62,0x63,0x64,
1387 /* configuration attribute 2*/
1388 0x00,0x04,0x00,0x04,
1389 0x65,0x66,0x67,0x68,
1390 };
1391
1392 logger->log_bytes(logger,RAW,"expected configuration",expected_generation3,sizeof(expected_generation3));
1393
1394 tester->assert_true(tester,(memcmp(expected_generation3,generated_data.ptr,sizeof(expected_generation3)) == 0), "compare generated data");
1395
1396 allocator_free_chunk(&generated_data);
1397 configuration->destroy(configuration);
1398 generator->destroy(generator);
1399
1400
1401 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1402 }
1403
1404 /*
1405 * Described in header.
1406 */
1407 void test_generator_with_eap_payload(protected_tester_t *tester)
1408 {
1409 generator_t *generator;
1410 eap_payload_t *eap_payload;
1411 logger_t *logger;
1412 chunk_t generated_data;
1413 chunk_t message;
1414
1415 logger = charon->logger_manager->create_logger(charon->logger_manager,TESTER,"Message with EAP Payload");
1416
1417 /* create generator */
1418 generator = generator_create();
1419 tester->assert_true(tester,(generator != NULL), "generator create check");
1420
1421 eap_payload = eap_payload_create();
1422
1423
1424 message.ptr = "123456789012";
1425 message.len = strlen(message.ptr);
1426 ;
1427 eap_payload->set_message(eap_payload,message);
1428 generator->generate_payload(generator,(payload_t *)eap_payload);
1429 generator->write_to_chunk(generator,&generated_data);
1430 logger->log_chunk(logger,RAW,"generated payload",&generated_data);
1431
1432 u_int8_t expected_generation[] = {
1433 /* payload header */
1434 0x00,0x00,0x00,0x10,
1435 /* eap data */
1436 0x31,0x32,0x33,0x34,
1437 0x35,0x36,0x37,0x38,
1438 0x39,0x30,0x31,0x32,
1439 };
1440
1441 logger->log_bytes(logger,RAW,"expected payload",expected_generation,sizeof(expected_generation));
1442
1443 tester->assert_true(tester,(memcmp(expected_generation,generated_data.ptr,sizeof(expected_generation)) == 0), "compare generated data");
1444
1445 allocator_free_chunk(&generated_data);
1446
1447 eap_payload->destroy(eap_payload);
1448 generator->destroy(generator);
1449
1450 charon->logger_manager->destroy_logger(charon->logger_manager,logger);
1451 }